2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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
->ln_expire
= timenow
;
426 ln
->ln_state
= ND6_LLINFO_PROBE
;
433 ln_setexpire(struct llinfo_nd6
*ln
, uint64_t expiry
)
435 ln
->ln_expire
= expiry
;
439 ln_getexpire(struct llinfo_nd6
*ln
)
441 struct timeval caltime
;
444 if (ln
->ln_expire
!= 0) {
445 struct rtentry
*rt
= ln
->ln_rt
;
448 /* account for system time change */
449 getmicrotime(&caltime
);
451 rt
->base_calendartime
+=
452 NET_CALCULATE_CLOCKSKEW(caltime
,
453 rt
->base_calendartime
, net_uptime(), rt
->base_uptime
);
455 expiry
= rt
->base_calendartime
+
456 ln
->ln_expire
- rt
->base_uptime
;
464 nd6_ifreset(struct ifnet
*ifp
)
466 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
468 VERIFY(ndi
->initialized
);
470 lck_mtx_assert(&ndi
->lock
, LCK_MTX_ASSERT_OWNED
);
471 ndi
->linkmtu
= ifp
->if_mtu
;
472 ndi
->chlim
= IPV6_DEFHLIM
;
473 ndi
->basereachable
= REACHABLE_TIME
;
474 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
475 ndi
->retrans
= RETRANS_TIMER
;
479 nd6_ifattach(struct ifnet
*ifp
)
481 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
484 if (!ndi
->initialized
) {
485 lck_mtx_init(&ndi
->lock
, nd_if_lock_grp
, nd_if_lock_attr
);
486 ndi
->flags
= ND6_IFF_PERFORMNUD
;
487 ndi
->initialized
= TRUE
;
490 lck_mtx_lock(&ndi
->lock
);
492 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
493 ndi
->flags
|= ND6_IFF_IFDISABLED
;
497 lck_mtx_unlock(&ndi
->lock
);
504 * XXX Look more into this. Especially since we recycle ifnets and do delayed
508 nd6_ifdetach(struct nd_ifinfo
*nd
)
510 /* XXX destroy nd's lock? */
516 nd6_setmtu(struct ifnet
*ifp
)
518 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
519 u_int32_t oldmaxmtu
, maxmtu
;
521 if ((NULL
== ndi
) || (FALSE
== ndi
->initialized
)) {
525 lck_mtx_lock(&ndi
->lock
);
526 oldmaxmtu
= ndi
->maxmtu
;
529 * The ND level maxmtu is somewhat redundant to the interface MTU
530 * and is an implementation artifact of KAME. Instead of hard-
531 * limiting the maxmtu based on the interface type here, we simply
532 * take the if_mtu value since SIOCSIFMTU would have taken care of
533 * the sanity checks related to the maximum MTU allowed for the
534 * interface (a value that is known only by the interface layer),
535 * by sending the request down via ifnet_ioctl(). The use of the
536 * ND level maxmtu and linkmtu are done via IN6_LINKMTU() which
537 * does further checking against if_mtu.
539 maxmtu
= ndi
->maxmtu
= ifp
->if_mtu
;
542 * Decreasing the interface MTU under IPV6 minimum MTU may cause
543 * undesirable situation. We thus notify the operator of the change
544 * explicitly. The check for oldmaxmtu is necessary to restrict the
545 * log to the case of changing the MTU, not initializing it.
547 if (oldmaxmtu
>= IPV6_MMTU
&& ndi
->maxmtu
< IPV6_MMTU
) {
548 log(LOG_NOTICE
, "nd6_setmtu: "
549 "new link MTU on %s (%u) is too small for IPv6\n",
550 if_name(ifp
), (uint32_t)ndi
->maxmtu
);
552 ndi
->linkmtu
= ifp
->if_mtu
;
553 lck_mtx_unlock(&ndi
->lock
);
555 /* also adjust in6_maxmtu if necessary. */
556 if (maxmtu
> in6_maxmtu
) {
562 nd6_option_init(void *opt
, int icmp6len
, union nd_opts
*ndopts
)
564 bzero(ndopts
, sizeof (*ndopts
));
565 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
566 ndopts
->nd_opts_last
=
567 (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
570 ndopts
->nd_opts_done
= 1;
571 ndopts
->nd_opts_search
= NULL
;
576 * Take one ND option.
579 nd6_option(union nd_opts
*ndopts
)
581 struct nd_opt_hdr
*nd_opt
;
585 panic("ndopts == NULL in nd6_option\n");
586 if (!ndopts
->nd_opts_last
)
587 panic("uninitialized ndopts in nd6_option\n");
588 if (!ndopts
->nd_opts_search
)
590 if (ndopts
->nd_opts_done
)
593 nd_opt
= ndopts
->nd_opts_search
;
595 /* make sure nd_opt_len is inside the buffer */
596 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
597 bzero(ndopts
, sizeof (*ndopts
));
601 olen
= nd_opt
->nd_opt_len
<< 3;
604 * Message validation requires that all included
605 * options have a length that is greater than zero.
607 bzero(ndopts
, sizeof (*ndopts
));
611 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
612 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
613 /* option overruns the end of buffer, invalid */
614 bzero(ndopts
, sizeof (*ndopts
));
616 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
617 /* reached the end of options chain */
618 ndopts
->nd_opts_done
= 1;
619 ndopts
->nd_opts_search
= NULL
;
625 * Parse multiple ND options.
626 * This function is much easier to use, for ND routines that do not need
627 * multiple options of the same type.
630 nd6_options(union nd_opts
*ndopts
)
632 struct nd_opt_hdr
*nd_opt
;
636 panic("ndopts == NULL in nd6_options");
637 if (ndopts
->nd_opts_last
== NULL
)
638 panic("uninitialized ndopts in nd6_options");
639 if (ndopts
->nd_opts_search
== NULL
)
643 nd_opt
= nd6_option(ndopts
);
644 if (nd_opt
== NULL
&& ndopts
->nd_opts_last
== NULL
) {
646 * Message validation requires that all included
647 * options have a length that is greater than zero.
649 icmp6stat
.icp6s_nd_badopt
++;
650 bzero(ndopts
, sizeof (*ndopts
));
657 switch (nd_opt
->nd_opt_type
) {
658 case ND_OPT_SOURCE_LINKADDR
:
659 case ND_OPT_TARGET_LINKADDR
:
661 case ND_OPT_REDIRECTED_HEADER
:
662 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
664 "duplicated ND6 option found (type=%d)\n",
665 nd_opt
->nd_opt_type
));
668 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] =
672 case ND_OPT_PREFIX_INFORMATION
:
673 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
674 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] =
677 ndopts
->nd_opts_pi_end
=
678 (struct nd_opt_prefix_info
*)nd_opt
;
685 * Unknown options must be silently ignored,
686 * to accomodate future extension to the protocol.
689 "nd6_options: unsupported option %d - "
690 "option ignored\n", nd_opt
->nd_opt_type
));
695 if (i
> nd6_maxndopt
) {
696 icmp6stat
.icp6s_nd_toomanyopt
++;
697 nd6log((LOG_INFO
, "too many loop in nd opt\n"));
701 if (ndopts
->nd_opts_done
)
718 * ND6 service routine to expire default route list and prefix list
721 nd6_service(void *arg
)
723 struct nd6svc_arg
*ap
= arg
;
724 struct llinfo_nd6
*ln
;
725 struct nd_defrouter
*dr
;
726 struct nd_prefix
*pr
;
727 struct ifnet
*ifp
= NULL
;
728 struct in6_ifaddr
*ia6
, *nia6
;
730 bool send_nc_failure_kev
= false;
732 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
734 * Since we may drop rnh_lock and nd6_mutex below, we want
735 * to run this entire operation single threaded.
737 while (nd6_service_busy
) {
738 nd6log2((LOG_DEBUG
, "%s: %s is blocked by %d waiters\n",
739 __func__
, ap
->draining
? "drainer" : "timer",
740 nd6_service_waiters
));
741 nd6_service_waiters
++;
742 (void) msleep(nd6_service_wc
, rnh_lock
, (PZERO
-1),
744 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
747 /* We are busy now; tell everyone else to go away */
748 nd6_service_busy
= TRUE
;
751 timenow
= net_uptime();
754 * send_nc_failure_kev gets set when default router's IPv6 address
756 * That can happen either:
757 * 1. When the entry has resolved once but can't be
758 * resolved later and the neighbor cache entry for gateway is deleted
759 * after max probe attempts.
761 * 2. When the entry is in ND6_LLINFO_INCOMPLETE but can not be resolved
762 * after max neighbor address resolution attempts.
764 * Both set send_nc_failure_kev to true. ifp is also set to the previous
765 * neighbor cache entry's route's ifp.
766 * Once we are done sending the notification, set send_nc_failure_kev
767 * to false to stop sending false notifications for non default router
770 * We may to send more information like Gateway's IP that could not be
771 * resolved, however right now we do not install more than one default
772 * route per interface in the routing table.
774 if (send_nc_failure_kev
&& ifp
->if_addrlen
== IF_LLREACH_MAXLEN
) {
775 struct kev_msg ev_msg
;
776 struct kev_nd6_ndfailure nd6_ndfailure
;
777 bzero(&ev_msg
, sizeof(ev_msg
));
778 bzero(&nd6_ndfailure
, sizeof(nd6_ndfailure
));
779 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
780 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
781 ev_msg
.kev_subclass
= KEV_ND6_SUBCLASS
;
782 ev_msg
.event_code
= KEV_ND6_NDFAILURE
;
784 nd6_ndfailure
.link_data
.if_family
= ifp
->if_family
;
785 nd6_ndfailure
.link_data
.if_unit
= ifp
->if_unit
;
786 strlcpy(nd6_ndfailure
.link_data
.if_name
,
788 sizeof(nd6_ndfailure
.link_data
.if_name
));
789 ev_msg
.dv
[0].data_ptr
= &nd6_ndfailure
;
790 ev_msg
.dv
[0].data_length
=
791 sizeof(nd6_ndfailure
);
792 kev_post_msg(&ev_msg
);
795 send_nc_failure_kev
= false;
798 * The global list llinfo_nd6 is modified by nd6_request() and is
799 * therefore protected by rnh_lock. For obvious reasons, we cannot
800 * hold rnh_lock across calls that might lead to code paths which
801 * attempt to acquire rnh_lock, else we deadlock. Hence for such
802 * cases we drop rt_lock and rnh_lock, make the calls, and repeat the
803 * loop. To ensure that we don't process the same entry more than
804 * once in a single timeout, we mark the "already-seen" entries with
805 * ND6_LNF_TIMER_SKIP flag. At the end of the loop, we do a second
806 * pass thru the entries and clear the flag so they can be processed
807 * during the next timeout.
809 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
811 ln
= llinfo_nd6
.ln_next
;
812 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
814 struct sockaddr_in6
*dst
;
815 struct llinfo_nd6
*next
;
816 u_int32_t retrans
, flags
;
817 struct nd_ifinfo
*ndi
= NULL
;
819 /* ln_next/prev/rt is protected by rnh_lock */
824 /* We've seen this already; skip it */
825 if (ln
->ln_flags
& ND6_LNF_TIMER_SKIP
) {
832 /* rt->rt_ifp should never be NULL */
833 if ((ifp
= rt
->rt_ifp
) == NULL
) {
834 panic("%s: ln(%p) rt(%p) rt_ifp == NULL", __func__
,
839 /* rt_llinfo must always be equal to ln */
840 if ((struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
) {
841 panic("%s: rt_llinfo(%p) is not equal to ln(%p)",
842 __func__
, rt
->rt_llinfo
, ln
);
846 /* rt_key should never be NULL */
847 dst
= SIN6(rt_key(rt
));
849 panic("%s: rt(%p) key is NULL ln(%p)", __func__
,
854 /* Set the flag in case we jump to "again" */
855 ln
->ln_flags
|= ND6_LNF_TIMER_SKIP
;
857 if (ln
->ln_expire
== 0 || (rt
->rt_flags
& RTF_STATIC
)) {
859 } else if (ap
->draining
&& (rt
->rt_refcnt
== 0)) {
861 * If we are draining, immediately purge non-static
862 * entries without oustanding route refcnt.
864 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
)
865 ln
->ln_state
= ND6_LLINFO_STALE
;
867 ln
->ln_state
= ND6_LLINFO_PURGE
;
868 ln_setexpire(ln
, timenow
);
872 * If the entry has not expired, skip it. Take note on the
873 * state, as entries that are in the STALE state are simply
874 * waiting to be garbage collected, in which case we can
875 * relax the callout scheduling (use nd6_prune_lazy).
877 if (ln
->ln_expire
> timenow
) {
878 switch (ln
->ln_state
) {
879 case ND6_LLINFO_STALE
:
891 ndi
= ND_IFINFO(ifp
);
892 VERIFY(ndi
->initialized
);
893 retrans
= ndi
->retrans
;
896 RT_LOCK_ASSERT_HELD(rt
);
898 switch (ln
->ln_state
) {
899 case ND6_LLINFO_INCOMPLETE
:
900 if (ln
->ln_asked
< nd6_mmaxtries
) {
901 struct ifnet
*exclifp
= ln
->ln_exclifp
;
903 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
904 RT_ADDREF_LOCKED(rt
);
906 lck_mtx_unlock(rnh_lock
);
907 if (ip6_forwarding
) {
908 nd6_prproxy_ns_output(ifp
, exclifp
,
909 NULL
, &dst
->sin6_addr
, ln
);
911 nd6_ns_output(ifp
, NULL
,
912 &dst
->sin6_addr
, ln
, 0);
916 lck_mtx_lock(rnh_lock
);
918 struct mbuf
*m
= ln
->ln_hold
;
920 send_nc_failure_kev
= (rt
->rt_flags
& RTF_ROUTER
) ? true : false;
922 RT_ADDREF_LOCKED(rt
);
924 lck_mtx_unlock(rnh_lock
);
928 mnext
= m
->m_nextpkt
;
930 m
->m_pkthdr
.rcvif
= ifp
;
931 icmp6_error_flag(m
, ICMP6_DST_UNREACH
,
932 ICMP6_DST_UNREACH_ADDR
, 0, 0);
936 RT_ADDREF_LOCKED(rt
);
938 lck_mtx_unlock(rnh_lock
);
942 lck_mtx_lock(rnh_lock
);
945 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
948 case ND6_LLINFO_REACHABLE
:
949 if (ln
->ln_expire
!= 0) {
950 ln
->ln_state
= ND6_LLINFO_STALE
;
951 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
957 case ND6_LLINFO_STALE
:
958 case ND6_LLINFO_PURGE
:
959 /* Garbage Collection(RFC 4861 5.3) */
960 if (ln
->ln_expire
!= 0) {
961 RT_ADDREF_LOCKED(rt
);
963 lck_mtx_unlock(rnh_lock
);
966 lck_mtx_lock(rnh_lock
);
974 case ND6_LLINFO_DELAY
:
975 if ((flags
& ND6_IFF_PERFORMNUD
) != 0) {
978 ln
->ln_state
= ND6_LLINFO_PROBE
;
979 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
980 RT_ADDREF_LOCKED(rt
);
982 lck_mtx_unlock(rnh_lock
);
983 nd6_ns_output(ifp
, &dst
->sin6_addr
,
984 &dst
->sin6_addr
, ln
, 0);
987 lck_mtx_lock(rnh_lock
);
990 ln
->ln_state
= ND6_LLINFO_STALE
; /* XXX */
991 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
996 case ND6_LLINFO_PROBE
:
997 if (ln
->ln_asked
< nd6_umaxtries
) {
999 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
1000 RT_ADDREF_LOCKED(rt
);
1002 lck_mtx_unlock(rnh_lock
);
1003 nd6_ns_output(ifp
, &dst
->sin6_addr
,
1004 &dst
->sin6_addr
, ln
, 0);
1007 lck_mtx_lock(rnh_lock
);
1009 send_nc_failure_kev
= (rt
->rt_flags
& RTF_ROUTER
) ? true : false;
1010 RT_ADDREF_LOCKED(rt
);
1012 lck_mtx_unlock(rnh_lock
);
1015 lck_mtx_lock(rnh_lock
);
1018 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1027 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1029 /* Now clear the flag from all entries */
1030 ln
= llinfo_nd6
.ln_next
;
1031 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
1032 struct rtentry
*rt
= ln
->ln_rt
;
1033 struct llinfo_nd6
*next
= ln
->ln_next
;
1036 if (ln
->ln_flags
& ND6_LNF_TIMER_SKIP
)
1037 ln
->ln_flags
&= ~ND6_LNF_TIMER_SKIP
;
1041 lck_mtx_unlock(rnh_lock
);
1043 /* expire default router list */
1044 lck_mtx_lock(nd6_mutex
);
1045 dr
= TAILQ_FIRST(&nd_defrouter
);
1048 if (dr
->expire
!= 0 && dr
->expire
< timenow
) {
1049 struct nd_defrouter
*t
;
1050 t
= TAILQ_NEXT(dr
, dr_entry
);
1055 if (dr
->expire
== 0 || (dr
->stateflags
& NDDRF_STATIC
))
1059 dr
= TAILQ_NEXT(dr
, dr_entry
);
1062 lck_mtx_unlock(nd6_mutex
);
1065 * expire interface addresses.
1066 * in the past the loop was inside prefix expiry processing.
1067 * However, from a stricter speci-confrmance standpoint, we should
1068 * rather separate address lifetimes and prefix lifetimes.
1071 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1072 for (ia6
= in6_ifaddrs
; ia6
; ia6
= nia6
) {
1074 nia6
= ia6
->ia_next
;
1075 IFA_LOCK(&ia6
->ia_ifa
);
1077 * Extra reference for ourselves; it's no-op if
1078 * we don't have to regenerate temporary address,
1079 * otherwise it protects the address from going
1080 * away since we drop in6_ifaddr_rwlock below.
1082 IFA_ADDREF_LOCKED(&ia6
->ia_ifa
);
1083 /* check address lifetime */
1084 if (IFA6_IS_INVALID(ia6
, timenow
)) {
1086 * If the expiring address is temporary, try
1087 * regenerating a new one. This would be useful when
1088 * we suspended a laptop PC, then turned it on after a
1089 * period that could invalidate all temporary
1090 * addresses. Although we may have to restart the
1091 * loop (see below), it must be after purging the
1092 * address. Otherwise, we'd see an infinite loop of
1095 if (ip6_use_tempaddr
&&
1096 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
1098 * NOTE: We have to drop the lock here
1099 * because regen_tmpaddr() eventually calls
1100 * in6_update_ifa(), which must take the lock
1101 * and would otherwise cause a hang. This is
1102 * safe because the goto addrloop leads to a
1103 * re-evaluation of the in6_ifaddrs list
1105 IFA_UNLOCK(&ia6
->ia_ifa
);
1106 lck_rw_done(&in6_ifaddr_rwlock
);
1107 (void) regen_tmpaddr(ia6
);
1109 IFA_UNLOCK(&ia6
->ia_ifa
);
1110 lck_rw_done(&in6_ifaddr_rwlock
);
1114 * Purging the address would have caused
1115 * in6_ifaddr_rwlock to be dropped and reacquired;
1116 * therefore search again from the beginning
1117 * of in6_ifaddrs list.
1119 in6_purgeaddr(&ia6
->ia_ifa
);
1122 /* Release extra reference taken above */
1123 IFA_REMREF(&ia6
->ia_ifa
);
1127 * The lazy timer runs every nd6_prune_lazy seconds with at
1128 * most "2 * nd6_prune_lazy - 1" leeway. We consider the worst
1129 * case here and make sure we schedule the regular timer if an
1130 * interface address is about to expire.
1132 if (IFA6_IS_INVALID(ia6
, timenow
+ 3 * nd6_prune_lazy
))
1136 IFA_LOCK_ASSERT_HELD(&ia6
->ia_ifa
);
1137 if (IFA6_IS_DEPRECATED(ia6
, timenow
)) {
1138 int oldflags
= ia6
->ia6_flags
;
1140 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
1143 * If a temporary address has just become deprecated,
1144 * regenerate a new one if possible.
1146 if (ip6_use_tempaddr
&&
1147 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1148 (oldflags
& IN6_IFF_DEPRECATED
) == 0) {
1150 /* see NOTE above */
1151 IFA_UNLOCK(&ia6
->ia_ifa
);
1152 lck_rw_done(&in6_ifaddr_rwlock
);
1153 if (regen_tmpaddr(ia6
) == 0) {
1155 * A new temporary address is
1157 * XXX: this means the address chain
1158 * has changed while we are still in
1159 * the loop. Although the change
1160 * would not cause disaster (because
1161 * it's not a deletion, but an
1162 * addition,) we'd rather restart the
1163 * loop just for safety. Or does this
1164 * significantly reduce performance??
1166 /* Release extra reference */
1167 IFA_REMREF(&ia6
->ia_ifa
);
1170 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1172 IFA_UNLOCK(&ia6
->ia_ifa
);
1176 * A new RA might have made a deprecated address
1179 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
1180 IFA_UNLOCK(&ia6
->ia_ifa
);
1182 lck_rw_assert(&in6_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
1183 /* Release extra reference taken above */
1184 IFA_REMREF(&ia6
->ia_ifa
);
1186 lck_rw_done(&in6_ifaddr_rwlock
);
1188 lck_mtx_lock(nd6_mutex
);
1189 /* expire prefix list */
1190 pr
= nd_prefix
.lh_first
;
1191 while (pr
!= NULL
) {
1194 * check prefix lifetime.
1195 * since pltime is just for autoconf, pltime processing for
1196 * prefix is not necessary.
1199 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED_SERVICE
||
1200 pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
1201 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1206 if (pr
->ndpr_expire
!= 0 && pr
->ndpr_expire
< timenow
) {
1208 * address expiration and prefix expiration are
1209 * separate. NEVER perform in6_purgeaddr here.
1211 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1212 NDPR_ADDREF_LOCKED(pr
);
1216 pfxlist_onlink_check();
1217 pr
= nd_prefix
.lh_first
;
1220 if (pr
->ndpr_expire
== 0 ||
1221 (pr
->ndpr_stateflags
& NDPRF_STATIC
))
1225 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1230 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
1232 pr
->ndpr_stateflags
&= ~NDPRF_PROCESSED_SERVICE
;
1235 lck_mtx_unlock(nd6_mutex
);
1237 lck_mtx_lock(rnh_lock
);
1238 /* We're done; let others enter */
1239 nd6_service_busy
= FALSE
;
1240 if (nd6_service_waiters
> 0) {
1241 nd6_service_waiters
= 0;
1242 wakeup(nd6_service_wc
);
1247 nd6_drain(void *arg
)
1250 struct nd6svc_arg sarg
;
1252 nd6log2((LOG_DEBUG
, "%s: draining ND6 entries\n", __func__
));
1254 lck_mtx_lock(rnh_lock
);
1255 bzero(&sarg
, sizeof (sarg
));
1258 nd6log2((LOG_DEBUG
, "%s: found %u, aging_lazy %u, aging %u, "
1259 "sticky %u, killed %u\n", __func__
, sarg
.found
, sarg
.aging_lazy
,
1260 sarg
.aging
, sarg
.sticky
, sarg
.killed
));
1261 lck_mtx_unlock(rnh_lock
);
1265 * We use the ``arg'' variable to decide whether or not the timer we're
1266 * running is the fast timer. We do this to reset the nd6_fast_timer_on
1267 * variable so that later we don't end up ignoring a ``fast timer''
1268 * request if the 5 second timer is running (see nd6_sched_timeout).
1271 nd6_timeout(void *arg
)
1273 struct nd6svc_arg sarg
;
1276 lck_mtx_lock(rnh_lock
);
1277 bzero(&sarg
, sizeof (sarg
));
1279 nd6log2((LOG_DEBUG
, "%s: found %u, aging_lazy %u, aging %u, "
1280 "sticky %u, killed %u\n", __func__
, sarg
.found
, sarg
.aging_lazy
,
1281 sarg
.aging
, sarg
.sticky
, sarg
.killed
));
1282 /* re-arm the timer if there's work to do */
1284 VERIFY(nd6_timeout_run
>= 0 && nd6_timeout_run
< 2);
1285 if (arg
== &nd6_fast_timer_on
)
1286 nd6_fast_timer_on
= FALSE
;
1287 if (sarg
.aging_lazy
> 0 || sarg
.aging
> 0 || nd6_sched_timeout_want
) {
1288 struct timeval atv
, ltv
, *leeway
;
1289 int lazy
= nd6_prune_lazy
;
1291 if (sarg
.aging
> 0 || lazy
< 1) {
1293 atv
.tv_sec
= nd6_prune
;
1298 atv
.tv_sec
= MAX(nd6_prune
, lazy
);
1300 read_frandom(&buf
, sizeof(buf
));
1301 ltv
.tv_sec
= MAX(buf
% lazy
, 1) * 2;
1304 nd6_sched_timeout(&atv
, leeway
);
1305 } else if (nd6_debug
) {
1306 nd6log2((LOG_DEBUG
, "%s: not rescheduling timer\n", __func__
));
1308 lck_mtx_unlock(rnh_lock
);
1312 nd6_sched_timeout(struct timeval
*atv
, struct timeval
*ltv
)
1316 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1319 tv
.tv_sec
= MAX(nd6_prune
, 1);
1321 ltv
= NULL
; /* ignore leeway */
1323 /* see comments on top of this file */
1324 if (nd6_timeout_run
== 0) {
1326 nd6log2((LOG_DEBUG
, "%s: timer scheduled in "
1327 "T+%llus.%lluu (demand %d)\n", __func__
,
1328 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1329 nd6_sched_timeout_want
));
1330 nd6_fast_timer_on
= TRUE
;
1331 timeout(nd6_timeout
, &nd6_fast_timer_on
, tvtohz(atv
));
1333 nd6log2((LOG_DEBUG
, "%s: timer scheduled in "
1334 "T+%llus.%lluu with %llus.%lluu leeway "
1335 "(demand %d)\n", __func__
, (uint64_t)atv
->tv_sec
,
1336 (uint64_t)atv
->tv_usec
, (uint64_t)ltv
->tv_sec
,
1337 (uint64_t)ltv
->tv_usec
, nd6_sched_timeout_want
));
1338 nd6_fast_timer_on
= FALSE
;
1339 timeout_with_leeway(nd6_timeout
, NULL
,
1340 tvtohz(atv
), tvtohz(ltv
));
1343 nd6_sched_timeout_want
= 0;
1344 } else if (nd6_timeout_run
== 1 && ltv
== NULL
&&
1345 nd6_fast_timer_on
== FALSE
) {
1346 nd6log2((LOG_DEBUG
, "%s: fast timer scheduled in "
1347 "T+%llus.%lluu (demand %d)\n", __func__
,
1348 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1349 nd6_sched_timeout_want
));
1350 nd6_fast_timer_on
= TRUE
;
1351 nd6_sched_timeout_want
= 0;
1353 timeout(nd6_timeout
, &nd6_fast_timer_on
, tvtohz(atv
));
1356 nd6log2((LOG_DEBUG
, "%s: not scheduling timer: "
1357 "timers %d, fast_timer %d, T+%llus.%lluu\n",
1358 __func__
, nd6_timeout_run
, nd6_fast_timer_on
,
1359 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
));
1361 nd6log2((LOG_DEBUG
, "%s: not scheduling timer: "
1362 "timers %d, fast_timer %d, T+%llus.%lluu "
1363 "with %llus.%lluu leeway\n", __func__
,
1364 nd6_timeout_run
, nd6_fast_timer_on
,
1365 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1366 (uint64_t)ltv
->tv_sec
, (uint64_t)ltv
->tv_usec
));
1372 * ND6 router advertisement kernel notification
1375 nd6_post_msg(u_int32_t code
, struct nd_prefix_list
*prefix_list
,
1376 u_int32_t list_length
, u_int32_t mtu
, char *dl_addr
, u_int32_t dl_addr_len
)
1378 struct kev_msg ev_msg
;
1379 struct kev_nd6_ra_data nd6_ra_msg_data
;
1380 struct nd_prefix_list
*itr
= prefix_list
;
1382 bzero(&ev_msg
, sizeof (struct kev_msg
));
1383 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1384 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1385 ev_msg
.kev_subclass
= KEV_ND6_SUBCLASS
;
1386 ev_msg
.event_code
= code
;
1388 bzero(&nd6_ra_msg_data
, sizeof (nd6_ra_msg_data
));
1389 nd6_ra_msg_data
.lladdrlen
= (dl_addr_len
<= ND6_ROUTER_LL_SIZE
) ?
1390 dl_addr_len
: ND6_ROUTER_LL_SIZE
;
1391 bcopy(dl_addr
, &nd6_ra_msg_data
.lladdr
, nd6_ra_msg_data
.lladdrlen
);
1393 if (mtu
> 0 && mtu
>= IPV6_MMTU
) {
1394 nd6_ra_msg_data
.mtu
= mtu
;
1395 nd6_ra_msg_data
.flags
|= KEV_ND6_DATA_VALID_MTU
;
1398 if (list_length
> 0 && prefix_list
!= NULL
) {
1399 nd6_ra_msg_data
.list_length
= list_length
;
1400 nd6_ra_msg_data
.flags
|= KEV_ND6_DATA_VALID_PREFIX
;
1403 while (itr
!= NULL
&& nd6_ra_msg_data
.list_index
< list_length
) {
1404 bcopy(&itr
->pr
.ndpr_prefix
, &nd6_ra_msg_data
.prefix
.prefix
,
1405 sizeof (nd6_ra_msg_data
.prefix
.prefix
));
1406 nd6_ra_msg_data
.prefix
.raflags
= itr
->pr
.ndpr_raf
;
1407 nd6_ra_msg_data
.prefix
.prefixlen
= itr
->pr
.ndpr_plen
;
1408 nd6_ra_msg_data
.prefix
.origin
= PR_ORIG_RA
;
1409 nd6_ra_msg_data
.prefix
.vltime
= itr
->pr
.ndpr_vltime
;
1410 nd6_ra_msg_data
.prefix
.pltime
= itr
->pr
.ndpr_pltime
;
1411 nd6_ra_msg_data
.prefix
.expire
= ndpr_getexpire(&itr
->pr
);
1412 nd6_ra_msg_data
.prefix
.flags
= itr
->pr
.ndpr_stateflags
;
1413 nd6_ra_msg_data
.prefix
.refcnt
= itr
->pr
.ndpr_addrcnt
;
1414 nd6_ra_msg_data
.prefix
.if_index
= itr
->pr
.ndpr_ifp
->if_index
;
1416 /* send the message up */
1417 ev_msg
.dv
[0].data_ptr
= &nd6_ra_msg_data
;
1418 ev_msg
.dv
[0].data_length
= sizeof (nd6_ra_msg_data
);
1419 ev_msg
.dv
[1].data_length
= 0;
1420 kev_post_msg(&ev_msg
);
1422 /* clean up for the next prefix */
1423 bzero(&nd6_ra_msg_data
.prefix
, sizeof (nd6_ra_msg_data
.prefix
));
1425 nd6_ra_msg_data
.list_index
++;
1430 * Regenerate deprecated/invalidated temporary address
1433 regen_tmpaddr(struct in6_ifaddr
*ia6
)
1437 struct in6_ifaddr
*public_ifa6
= NULL
;
1438 uint64_t timenow
= net_uptime();
1440 ifp
= ia6
->ia_ifa
.ifa_ifp
;
1441 ifnet_lock_shared(ifp
);
1442 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
1443 struct in6_ifaddr
*it6
;
1446 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1450 it6
= (struct in6_ifaddr
*)ifa
;
1452 /* ignore no autoconf addresses. */
1453 if ((it6
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
1457 /* ignore autoconf addresses with different prefixes. */
1458 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
) {
1463 * Now we are looking at an autoconf address with the same
1464 * prefix as ours. If the address is temporary and is still
1465 * preferred, do not create another one. It would be rare, but
1466 * could happen, for example, when we resume a laptop PC after
1469 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1470 !IFA6_IS_DEPRECATED(it6
, timenow
)) {
1472 if (public_ifa6
!= NULL
)
1473 IFA_REMREF(&public_ifa6
->ia_ifa
);
1479 * This is a public autoconf address that has the same prefix
1480 * as ours. If it is preferred, keep it. We can't break the
1481 * loop here, because there may be a still-preferred temporary
1482 * address with the prefix.
1484 if (!IFA6_IS_DEPRECATED(it6
, timenow
)) {
1485 IFA_ADDREF_LOCKED(ifa
); /* for public_ifa6 */
1487 if (public_ifa6
!= NULL
)
1488 IFA_REMREF(&public_ifa6
->ia_ifa
);
1494 ifnet_lock_done(ifp
);
1496 if (public_ifa6
!= NULL
) {
1499 if ((e
= in6_tmpifadd(public_ifa6
, 0)) != 0) {
1500 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
1501 " tmp addr,errno=%d\n", e
);
1502 IFA_REMREF(&public_ifa6
->ia_ifa
);
1505 IFA_REMREF(&public_ifa6
->ia_ifa
);
1513 * Nuke neighbor cache/prefix/default router management table, right before
1517 nd6_purge(struct ifnet
*ifp
)
1519 struct llinfo_nd6
*ln
;
1520 struct nd_defrouter
*dr
, *ndr
;
1521 struct nd_prefix
*pr
, *npr
;
1524 /* Nuke default router list entries toward ifp */
1525 lck_mtx_lock(nd6_mutex
);
1526 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
1528 * The first entry of the list may be stored in
1529 * the routing table, so we'll delete it later.
1531 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= ndr
) {
1532 ndr
= TAILQ_NEXT(dr
, dr_entry
);
1533 if (dr
->stateflags
& NDDRF_INSTALLED
)
1538 dr
= TAILQ_FIRST(&nd_defrouter
);
1543 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
; dr
= ndr
) {
1544 ndr
= TAILQ_NEXT(dr
, dr_entry
);
1545 if (!(dr
->stateflags
& NDDRF_INSTALLED
))
1552 /* Nuke prefix list entries toward ifp */
1554 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
1556 npr
= pr
->ndpr_next
;
1557 if (pr
->ndpr_ifp
== ifp
&&
1558 !(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)) {
1560 * Because if_detach() does *not* release prefixes
1561 * while purging addresses the reference count will
1562 * still be above zero. We therefore reset it to
1563 * make sure that the prefix really gets purged.
1565 pr
->ndpr_addrcnt
= 0;
1568 * Previously, pr->ndpr_addr is removed as well,
1569 * but I strongly believe we don't have to do it.
1570 * nd6_purge() is only called from in6_ifdetach(),
1571 * which removes all the associated interface addresses
1573 * (jinmei@kame.net 20010129)
1575 NDPR_ADDREF_LOCKED(pr
);
1580 npr
= nd_prefix
.lh_first
;
1586 pfxlist_onlink_check();
1587 lck_mtx_unlock(nd6_mutex
);
1589 /* cancel default outgoing interface setting */
1590 if (nd6_defifindex
== ifp
->if_index
) {
1591 nd6_setdefaultiface(0);
1595 * Perform default router selection even when we are a router,
1596 * if Scoped Routing is enabled.
1598 if (ip6_doscopedroute
|| !ip6_forwarding
) {
1599 lck_mtx_lock(nd6_mutex
);
1600 /* refresh default router list */
1601 defrouter_select(ifp
);
1602 lck_mtx_unlock(nd6_mutex
);
1606 * Nuke neighbor cache entries for the ifp.
1607 * Note that rt->rt_ifp may not be the same as ifp,
1608 * due to KAME goto ours hack. See RTM_RESOLVE case in
1609 * nd6_rtrequest(), and ip6_input().
1612 lck_mtx_lock(rnh_lock
);
1613 ln
= llinfo_nd6
.ln_next
;
1614 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
1616 struct llinfo_nd6
*nln
;
1621 if (rt
->rt_gateway
!= NULL
&&
1622 rt
->rt_gateway
->sa_family
== AF_LINK
&&
1623 SDL(rt
->rt_gateway
)->sdl_index
== ifp
->if_index
) {
1624 RT_ADDREF_LOCKED(rt
);
1626 lck_mtx_unlock(rnh_lock
);
1628 * See comments on nd6_service() for reasons why
1629 * this loop is repeated; we bite the costs of
1630 * going thru the same llinfo_nd6 more than once
1631 * here, since this purge happens during detach,
1632 * and that unlike the timer case, it's possible
1633 * there's more than one purges happening at the
1634 * same time (thus a flag wouldn't buy anything).
1638 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1645 lck_mtx_unlock(rnh_lock
);
1649 * Upon success, the returned route will be locked and the caller is
1650 * responsible for releasing the reference and doing RT_UNLOCK(rt).
1651 * This routine does not require rnh_lock to be held by the caller,
1652 * although it needs to be indicated of such a case in order to call
1653 * the correct variant of the relevant routing routines.
1656 nd6_lookup(struct in6_addr
*addr6
, int create
, struct ifnet
*ifp
, int rt_locked
)
1659 struct sockaddr_in6 sin6
;
1660 unsigned int ifscope
;
1662 bzero(&sin6
, sizeof (sin6
));
1663 sin6
.sin6_len
= sizeof (struct sockaddr_in6
);
1664 sin6
.sin6_family
= AF_INET6
;
1665 sin6
.sin6_addr
= *addr6
;
1667 ifscope
= (ifp
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
1669 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1670 rt
= rtalloc1_scoped_locked(SA(&sin6
), create
, 0, ifscope
);
1672 rt
= rtalloc1_scoped(SA(&sin6
), create
, 0, ifscope
);
1677 if ((rt
->rt_flags
& RTF_LLINFO
) == 0) {
1679 * This is the case for the default route.
1680 * If we want to create a neighbor cache for the
1681 * address, we should free the route for the
1682 * destination and allocate an interface route.
1695 if (create
&& ifp
) {
1697 u_int32_t ifa_flags
;
1701 * If no route is available and create is set,
1702 * we allocate a host route for the destination
1703 * and treat it like an interface route.
1704 * This hack is necessary for a neighbor which can't
1705 * be covered by our own prefix.
1707 ifa
= ifaof_ifpforaddr(SA(&sin6
), ifp
);
1712 * Create a new route. RTF_LLINFO is necessary
1713 * to create a Neighbor Cache entry for the
1714 * destination in nd6_rtrequest which will be
1715 * called in rtrequest via ifa->ifa_rtrequest.
1718 lck_mtx_lock(rnh_lock
);
1720 ifa_flags
= ifa
->ifa_flags
;
1722 if ((e
= rtrequest_scoped_locked(RTM_ADD
,
1723 SA(&sin6
), ifa
->ifa_addr
, SA(&all1_sa
),
1724 (ifa_flags
| RTF_HOST
| RTF_LLINFO
) &
1725 ~RTF_CLONING
, &rt
, ifscope
)) != 0) {
1727 log(LOG_ERR
, "%s: failed to add route "
1728 "for a neighbor(%s), errno=%d\n",
1729 __func__
, ip6_sprintf(addr6
), e
);
1732 lck_mtx_unlock(rnh_lock
);
1738 if (rt
->rt_llinfo
) {
1739 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
1740 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1746 RT_LOCK_ASSERT_HELD(rt
);
1748 * Validation for the entry.
1749 * Note that the check for rt_llinfo is necessary because a cloned
1750 * route from a parent route that has the L flag (e.g. the default
1751 * route to a p2p interface) may have the flag, too, while the
1752 * destination is not actually a neighbor.
1753 * XXX: we can't use rt->rt_ifp to check for the interface, since
1754 * it might be the loopback interface if the entry is for our
1755 * own address on a non-loopback interface. Instead, we should
1756 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
1758 * Note also that ifa_ifp and ifp may differ when we connect two
1759 * interfaces to a same link, install a link prefix to an interface,
1760 * and try to install a neighbor cache on an interface that does not
1761 * have a route to the prefix.
1763 * If the address is from a proxied prefix, the ifa_ifp and ifp might
1764 * not match, because nd6_na_input() could have modified the ifp
1765 * of the route to point to the interface where the NA arrived on,
1766 * hence the test for RTF_PROXY.
1768 if ((rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1769 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
1770 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
&&
1771 !(rt
->rt_flags
& RTF_PROXY
))) {
1772 RT_REMREF_LOCKED(rt
);
1775 log(LOG_DEBUG
, "%s: failed to lookup %s "
1776 "(if = %s)\n", __func__
, ip6_sprintf(addr6
),
1777 ifp
? if_name(ifp
) : "unspec");
1778 /* xxx more logs... kazu */
1783 * Caller needs to release reference and call RT_UNLOCK(rt).
1789 * Test whether a given IPv6 address is a neighbor or not, ignoring
1790 * the actual neighbor cache. The neighbor cache is ignored in order
1791 * to not reenter the routing code from within itself.
1794 nd6_is_new_addr_neighbor(struct sockaddr_in6
*addr
, struct ifnet
*ifp
)
1796 struct nd_prefix
*pr
;
1797 struct ifaddr
*dstaddr
;
1799 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1802 * A link-local address is always a neighbor.
1803 * XXX: a link does not necessarily specify a single interface.
1805 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
)) {
1806 struct sockaddr_in6 sin6_copy
;
1810 * We need sin6_copy since sa6_recoverscope() may modify the
1814 if (sa6_recoverscope(&sin6_copy
, FALSE
))
1815 return (0); /* XXX: should be impossible */
1816 if (in6_setscope(&sin6_copy
.sin6_addr
, ifp
, &zone
))
1818 if (sin6_copy
.sin6_scope_id
== zone
)
1825 * If the address matches one of our addresses,
1826 * it should be a neighbor.
1827 * If the address matches one of our on-link prefixes, it should be a
1830 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
1832 if (pr
->ndpr_ifp
!= ifp
) {
1836 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
1840 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr
->ndpr_prefix
.sin6_addr
,
1841 &addr
->sin6_addr
, &pr
->ndpr_mask
)) {
1849 * If the address is assigned on the node of the other side of
1850 * a p2p interface, the address should be a neighbor.
1852 dstaddr
= ifa_ifwithdstaddr(SA(addr
));
1853 if (dstaddr
!= NULL
) {
1854 if (dstaddr
->ifa_ifp
== ifp
) {
1855 IFA_REMREF(dstaddr
);
1858 IFA_REMREF(dstaddr
);
1863 * If the default router list is empty, all addresses are regarded
1864 * as on-link, and thus, as a neighbor.
1865 * XXX: we restrict the condition to hosts, because routers usually do
1866 * not have the "default router list".
1867 * XXX: this block should eventually be removed (it is disabled when
1868 * Scoped Routing is in effect); treating all destinations as on-link
1869 * in the absence of a router is rather harmful.
1871 if (!ip6_doscopedroute
&& !ip6_forwarding
&&
1872 TAILQ_FIRST(&nd_defrouter
) == NULL
&&
1873 nd6_defifindex
== ifp
->if_index
) {
1882 * Detect if a given IPv6 address identifies a neighbor on a given link.
1883 * XXX: should take care of the destination of a p2p link?
1886 nd6_is_addr_neighbor(struct sockaddr_in6
*addr
, struct ifnet
*ifp
,
1891 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1892 lck_mtx_lock(nd6_mutex
);
1893 if (nd6_is_new_addr_neighbor(addr
, ifp
)) {
1894 lck_mtx_unlock(nd6_mutex
);
1897 lck_mtx_unlock(nd6_mutex
);
1900 * Even if the address matches none of our addresses, it might be
1901 * in the neighbor cache.
1903 if ((rt
= nd6_lookup(&addr
->sin6_addr
, 0, ifp
, rt_locked
)) != NULL
) {
1904 RT_LOCK_ASSERT_HELD(rt
);
1905 RT_REMREF_LOCKED(rt
);
1914 * Free an nd6 llinfo entry.
1915 * Since the function would cause significant changes in the kernel, DO NOT
1916 * make it global, unless you have a strong reason for the change, and are sure
1917 * that the change is safe.
1920 nd6_free(struct rtentry
*rt
)
1922 struct llinfo_nd6
*ln
;
1923 struct in6_addr in6
;
1924 struct nd_defrouter
*dr
;
1926 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1927 RT_LOCK_ASSERT_NOTHELD(rt
);
1928 lck_mtx_lock(nd6_mutex
);
1931 RT_ADDREF_LOCKED(rt
); /* Extra ref */
1933 in6
= SIN6(rt_key(rt
))->sin6_addr
;
1936 * Prevent another thread from modifying rt_key, rt_gateway
1937 * via rt_setgate() after the rt_lock is dropped by marking
1938 * the route as defunct.
1940 rt
->rt_flags
|= RTF_CONDEMNED
;
1943 * We used to have pfctlinput(PRC_HOSTDEAD) here. Even though it is
1944 * not harmful, it was not really necessary. Perform default router
1945 * selection even when we are a router, if Scoped Routing is enabled.
1947 if (ip6_doscopedroute
|| !ip6_forwarding
) {
1948 dr
= defrouter_lookup(&SIN6(rt_key(rt
))->sin6_addr
, rt
->rt_ifp
);
1950 if ((ln
&& ln
->ln_router
) || dr
) {
1952 * rt6_flush must be called whether or not the neighbor
1953 * is in the Default Router List.
1954 * See a corresponding comment in nd6_na_input().
1957 lck_mtx_unlock(nd6_mutex
);
1958 rt6_flush(&in6
, rt
->rt_ifp
);
1959 lck_mtx_lock(nd6_mutex
);
1967 * Unreachablity of a router might affect the default
1968 * router selection and on-link detection of advertised
1973 * Temporarily fake the state to choose a new default
1974 * router and to perform on-link determination of
1975 * prefixes correctly.
1976 * Below the state will be set correctly,
1977 * or the entry itself will be deleted.
1980 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1983 * Since defrouter_select() does not affect the
1984 * on-link determination and MIP6 needs the check
1985 * before the default router selection, we perform
1989 pfxlist_onlink_check();
1992 * refresh default router list
1994 defrouter_select(rt
->rt_ifp
);
1996 RT_LOCK_ASSERT_NOTHELD(rt
);
2001 lck_mtx_unlock(nd6_mutex
);
2003 * Detach the route from the routing tree and the list of neighbor
2004 * caches, and disable the route entry not to be used in already
2007 (void) rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
), 0, NULL
);
2009 /* Extra ref held above; now free it */
2014 nd6_rtrequest(int req
, struct rtentry
*rt
, struct sockaddr
*sa
)
2017 struct sockaddr
*gate
= rt
->rt_gateway
;
2018 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
2019 static struct sockaddr_dl null_sdl
=
2020 { .sdl_len
= sizeof (null_sdl
), .sdl_family
= AF_LINK
};
2021 struct ifnet
*ifp
= rt
->rt_ifp
;
2024 char buf
[MAX_IPv6_STR_LEN
];
2026 VERIFY(nd6_init_done
);
2027 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2028 RT_LOCK_ASSERT_HELD(rt
);
2031 * We have rnh_lock held, see if we need to schedule the timer;
2032 * we might do this again below during RTM_RESOLVE, but doing it
2033 * now handles all other cases.
2035 if (nd6_sched_timeout_want
)
2036 nd6_sched_timeout(NULL
, NULL
);
2038 if (rt
->rt_flags
& RTF_GATEWAY
)
2041 if (!nd6_need_cache(ifp
) && !(rt
->rt_flags
& RTF_HOST
)) {
2043 * This is probably an interface direct route for a link
2044 * which does not need neighbor caches (e.g. fe80::%lo0/64).
2045 * We do not need special treatment below for such a route.
2046 * Moreover, the RTF_LLINFO flag which would be set below
2047 * would annoy the ndp(8) command.
2052 if (req
== RTM_RESOLVE
) {
2055 if (!nd6_need_cache(ifp
)) { /* stf case */
2058 struct sockaddr_in6 sin6
;
2060 rtkey_to_sa6(rt
, &sin6
);
2062 * nd6_is_addr_neighbor() may call nd6_lookup(),
2063 * therefore we drop rt_lock to avoid deadlock
2064 * during the lookup.
2066 RT_ADDREF_LOCKED(rt
);
2068 no_nd_cache
= !nd6_is_addr_neighbor(&sin6
, ifp
, 1);
2070 RT_REMREF_LOCKED(rt
);
2074 * FreeBSD and BSD/OS often make a cloned host route based
2075 * on a less-specific route (e.g. the default route).
2076 * If the less specific route does not have a "gateway"
2077 * (this is the case when the route just goes to a p2p or an
2078 * stf interface), we'll mistakenly make a neighbor cache for
2079 * the host route, and will see strange neighbor solicitation
2080 * for the corresponding destination. In order to avoid the
2081 * confusion, we check if the destination of the route is
2082 * a neighbor in terms of neighbor discovery, and stop the
2083 * process if not. Additionally, we remove the LLINFO flag
2084 * so that ndp(8) will not try to get the neighbor information
2085 * of the destination.
2088 rt
->rt_flags
&= ~RTF_LLINFO
;
2093 timenow
= net_uptime();
2098 * There is no backward compatibility :)
2100 * if ((rt->rt_flags & RTF_HOST) == 0 &&
2101 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
2102 * rt->rt_flags |= RTF_CLONING;
2104 if ((rt
->rt_flags
& RTF_CLONING
) ||
2105 ((rt
->rt_flags
& RTF_LLINFO
) && ln
== NULL
)) {
2107 * Case 1: This route should come from a route to
2108 * interface (RTF_CLONING case) or the route should be
2109 * treated as on-link but is currently not
2110 * (RTF_LLINFO && ln == NULL case).
2112 if (rt_setgate(rt
, rt_key(rt
), SA(&null_sdl
)) == 0) {
2113 gate
= rt
->rt_gateway
;
2114 SDL(gate
)->sdl_type
= ifp
->if_type
;
2115 SDL(gate
)->sdl_index
= ifp
->if_index
;
2117 * In case we're called before 1.0 sec.
2122 (ifp
->if_eflags
& IFEF_IPV6_ND6ALT
)
2123 ? 0 : MAX(timenow
, 1));
2126 if (rt
->rt_flags
& RTF_CLONING
)
2130 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
2131 * We don't do that here since llinfo is not ready yet.
2133 * There are also couple of other things to be discussed:
2134 * - unsolicited NA code needs improvement beforehand
2135 * - RFC4861 says we MAY send multicast unsolicited NA
2136 * (7.2.6 paragraph 4), however, it also says that we
2137 * SHOULD provide a mechanism to prevent multicast NA storm.
2138 * we don't have anything like it right now.
2139 * note that the mechanism needs a mutual agreement
2140 * between proxies, which means that we need to implement
2141 * a new protocol, or a new kludge.
2142 * - from RFC4861 6.2.4, host MUST NOT send an unsolicited RA.
2143 * we need to check ip6forwarding before sending it.
2144 * (or should we allow proxy ND configuration only for
2145 * routers? there's no mention about proxy ND from hosts)
2149 if (!(ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
))) {
2151 * Address resolution isn't necessary for a point to
2152 * point link, so we can skip this test for a p2p link.
2154 if (gate
->sa_family
!= AF_LINK
||
2155 gate
->sa_len
< sizeof (null_sdl
)) {
2156 /* Don't complain in case of RTM_ADD */
2157 if (req
== RTM_RESOLVE
) {
2158 log(LOG_ERR
, "%s: route to %s has bad "
2159 "gateway address (sa_family %u "
2160 "sa_len %u) on %s\n", __func__
,
2162 &SIN6(rt_key(rt
))->sin6_addr
, buf
,
2163 sizeof (buf
)), gate
->sa_family
,
2164 gate
->sa_len
, if_name(ifp
));
2168 SDL(gate
)->sdl_type
= ifp
->if_type
;
2169 SDL(gate
)->sdl_index
= ifp
->if_index
;
2172 break; /* This happens on a route change */
2174 * Case 2: This route may come from cloning, or a manual route
2175 * add with a LL address.
2177 rt
->rt_llinfo
= ln
= nd6_llinfo_alloc(M_WAITOK
);
2182 rt
->rt_llinfo_get_ri
= nd6_llinfo_get_ri
;
2183 rt
->rt_llinfo_get_iflri
= nd6_llinfo_get_iflri
;
2184 rt
->rt_llinfo_purge
= nd6_llinfo_purge
;
2185 rt
->rt_llinfo_free
= nd6_llinfo_free
;
2186 rt
->rt_llinfo_refresh
= nd6_llinfo_refresh
;
2187 rt
->rt_flags
|= RTF_LLINFO
;
2189 /* this is required for "ndp" command. - shin */
2190 if (req
== RTM_ADD
) {
2192 * gate should have some valid AF_LINK entry,
2193 * and ln->ln_expire should have some lifetime
2194 * which is specified by ndp command.
2196 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
2199 * When req == RTM_RESOLVE, rt is created and
2200 * initialized in rtrequest(), so rt_expire is 0.
2202 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
2204 /* In case we're called before 1.0 sec. has elapsed */
2205 ln_setexpire(ln
, (ifp
->if_eflags
& IFEF_IPV6_ND6ALT
) ?
2206 0 : MAX(timenow
, 1));
2211 /* We have at least one entry; arm the timer if not already */
2212 nd6_sched_timeout(NULL
, NULL
);
2215 * If we have too many cache entries, initiate immediate
2216 * purging for some "less recently used" entries. Note that
2217 * we cannot directly call nd6_free() here because it would
2218 * cause re-entering rtable related routines triggering an LOR
2221 if (ip6_neighborgcthresh
> 0 &&
2222 nd6_inuse
>= ip6_neighborgcthresh
) {
2225 for (i
= 0; i
< 10 && llinfo_nd6
.ln_prev
!= ln
; i
++) {
2226 struct llinfo_nd6
*ln_end
= llinfo_nd6
.ln_prev
;
2227 struct rtentry
*rt_end
= ln_end
->ln_rt
;
2229 /* Move this entry to the head */
2232 LN_INSERTHEAD(ln_end
);
2234 if (ln_end
->ln_expire
== 0) {
2238 if (ln_end
->ln_state
> ND6_LLINFO_INCOMPLETE
)
2239 ln_end
->ln_state
= ND6_LLINFO_STALE
;
2241 ln_end
->ln_state
= ND6_LLINFO_PURGE
;
2242 ln_setexpire(ln_end
, timenow
);
2248 * check if rt_key(rt) is one of my address assigned
2251 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
2252 &SIN6(rt_key(rt
))->sin6_addr
);
2254 caddr_t macp
= nd6_ifptomac(ifp
);
2255 ln_setexpire(ln
, 0);
2256 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
2258 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
2259 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
2261 if (nd6_useloopback
) {
2262 if (rt
->rt_ifp
!= lo_ifp
) {
2264 * Purge any link-layer info caching.
2266 if (rt
->rt_llinfo_purge
!= NULL
)
2267 rt
->rt_llinfo_purge(rt
);
2270 * Adjust route ref count for the
2273 if (rt
->rt_if_ref_fn
!= NULL
) {
2274 rt
->rt_if_ref_fn(lo_ifp
, 1);
2275 rt
->rt_if_ref_fn(rt
->rt_ifp
,
2279 rt
->rt_ifp
= lo_ifp
;
2281 * If rmx_mtu is not locked, update it
2282 * to the MTU used by the new interface.
2284 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
))
2285 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
2287 * Make sure rt_ifa be equal to the ifaddr
2288 * corresponding to the address.
2289 * We need this because when we refer
2290 * rt_ifa->ia6_flags in ip6_input, we assume
2291 * that the rt_ifa points to the address instead
2292 * of the loopback address.
2294 if (ifa
!= rt
->rt_ifa
) {
2299 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
2300 ln_setexpire(ln
, 0);
2301 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
2303 /* join solicited node multicast for proxy ND */
2304 if (ifp
->if_flags
& IFF_MULTICAST
) {
2305 struct in6_addr llsol
;
2306 struct in6_multi
*in6m
;
2309 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
2310 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
2311 llsol
.s6_addr32
[1] = 0;
2312 llsol
.s6_addr32
[2] = htonl(1);
2313 llsol
.s6_addr8
[12] = 0xff;
2314 if (in6_setscope(&llsol
, ifp
, NULL
))
2316 error
= in6_mc_join(ifp
, &llsol
,
2319 nd6log((LOG_ERR
, "%s: failed to join "
2320 "%s (errno=%d)\n", if_name(ifp
),
2321 ip6_sprintf(&llsol
), error
));
2332 /* leave from solicited node multicast for proxy ND */
2333 if ((rt
->rt_flags
& RTF_ANNOUNCE
) &&
2334 (ifp
->if_flags
& IFF_MULTICAST
)) {
2335 struct in6_addr llsol
;
2336 struct in6_multi
*in6m
;
2338 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
2339 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
2340 llsol
.s6_addr32
[1] = 0;
2341 llsol
.s6_addr32
[2] = htonl(1);
2342 llsol
.s6_addr8
[12] = 0xff;
2343 if (in6_setscope(&llsol
, ifp
, NULL
) == 0) {
2344 in6_multihead_lock_shared();
2345 IN6_LOOKUP_MULTI(&llsol
, ifp
, in6m
);
2346 in6_multihead_lock_done();
2348 in6_mc_leave(in6m
, NULL
);
2355 * Unchain it but defer the actual freeing until the route
2356 * itself is to be freed. rt->rt_llinfo still points to
2357 * llinfo_nd6, and likewise, ln->ln_rt stil points to this
2358 * route entry, except that RTF_LLINFO is now cleared.
2360 if (ln
->ln_flags
& ND6_LNF_IN_USE
)
2364 * Purge any link-layer info caching.
2366 if (rt
->rt_llinfo_purge
!= NULL
)
2367 rt
->rt_llinfo_purge(rt
);
2369 rt
->rt_flags
&= ~RTF_LLINFO
;
2370 if (ln
->ln_hold
!= NULL
) {
2371 m_freem_list(ln
->ln_hold
);
2378 nd6_siocgdrlst(void *data
, int data_is_64
)
2380 struct in6_drlist_32
*drl_32
;
2381 struct nd_defrouter
*dr
;
2384 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2386 dr
= TAILQ_FIRST(&nd_defrouter
);
2388 /* For 64-bit process */
2390 struct in6_drlist_64
*drl_64
;
2392 drl_64
= _MALLOC(sizeof (*drl_64
), M_TEMP
, M_WAITOK
|M_ZERO
);
2396 /* preserve the interface name */
2397 bcopy(data
, drl_64
, sizeof (drl_64
->ifname
));
2399 while (dr
&& i
< DRLSTSIZ
) {
2400 drl_64
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
2401 if (IN6_IS_ADDR_LINKLOCAL(
2402 &drl_64
->defrouter
[i
].rtaddr
)) {
2403 /* XXX: need to this hack for KAME stack */
2404 drl_64
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
2407 "default router list contains a "
2408 "non-linklocal address(%s)\n",
2409 ip6_sprintf(&drl_64
->defrouter
[i
].rtaddr
));
2411 drl_64
->defrouter
[i
].flags
= dr
->flags
;
2412 drl_64
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
2413 drl_64
->defrouter
[i
].expire
= nddr_getexpire(dr
);
2414 drl_64
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
2416 dr
= TAILQ_NEXT(dr
, dr_entry
);
2418 bcopy(drl_64
, data
, sizeof (*drl_64
));
2419 _FREE(drl_64
, M_TEMP
);
2423 /* For 32-bit process */
2424 drl_32
= _MALLOC(sizeof (*drl_32
), M_TEMP
, M_WAITOK
|M_ZERO
);
2428 /* preserve the interface name */
2429 bcopy(data
, drl_32
, sizeof (drl_32
->ifname
));
2431 while (dr
!= NULL
&& i
< DRLSTSIZ
) {
2432 drl_32
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
2433 if (IN6_IS_ADDR_LINKLOCAL(&drl_32
->defrouter
[i
].rtaddr
)) {
2434 /* XXX: need to this hack for KAME stack */
2435 drl_32
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
2438 "default router list contains a "
2439 "non-linklocal address(%s)\n",
2440 ip6_sprintf(&drl_32
->defrouter
[i
].rtaddr
));
2442 drl_32
->defrouter
[i
].flags
= dr
->flags
;
2443 drl_32
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
2444 drl_32
->defrouter
[i
].expire
= nddr_getexpire(dr
);
2445 drl_32
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
2447 dr
= TAILQ_NEXT(dr
, dr_entry
);
2449 bcopy(drl_32
, data
, sizeof (*drl_32
));
2450 _FREE(drl_32
, M_TEMP
);
2455 * XXX meaning of fields, especialy "raflags", is very
2456 * differnet between RA prefix list and RR/static prefix list.
2457 * how about separating ioctls into two?
2460 nd6_siocgprlst(void *data
, int data_is_64
)
2462 struct in6_prlist_32
*prl_32
;
2463 struct nd_prefix
*pr
;
2466 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2468 pr
= nd_prefix
.lh_first
;
2470 /* For 64-bit process */
2472 struct in6_prlist_64
*prl_64
;
2474 prl_64
= _MALLOC(sizeof (*prl_64
), M_TEMP
, M_WAITOK
|M_ZERO
);
2478 /* preserve the interface name */
2479 bcopy(data
, prl_64
, sizeof (prl_64
->ifname
));
2481 while (pr
&& i
< PRLSTSIZ
) {
2482 struct nd_pfxrouter
*pfr
;
2486 (void) in6_embedscope(&prl_64
->prefix
[i
].prefix
,
2487 &pr
->ndpr_prefix
, NULL
, NULL
, NULL
);
2488 prl_64
->prefix
[i
].raflags
= pr
->ndpr_raf
;
2489 prl_64
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
2490 prl_64
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
2491 prl_64
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
2492 prl_64
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
2493 prl_64
->prefix
[i
].expire
= ndpr_getexpire(pr
);
2495 pfr
= pr
->ndpr_advrtrs
.lh_first
;
2499 #define RTRADDR prl_64->prefix[i].advrtr[j]
2500 RTRADDR
= pfr
->router
->rtaddr
;
2501 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
2502 /* XXX: hack for KAME */
2503 RTRADDR
.s6_addr16
[1] = 0;
2506 "a router(%s) advertises "
2508 "non-link local address\n",
2509 ip6_sprintf(&RTRADDR
));
2514 pfr
= pfr
->pfr_next
;
2516 prl_64
->prefix
[i
].advrtrs
= j
;
2517 prl_64
->prefix
[i
].origin
= PR_ORIG_RA
;
2523 bcopy(prl_64
, data
, sizeof (*prl_64
));
2524 _FREE(prl_64
, M_TEMP
);
2528 /* For 32-bit process */
2529 prl_32
= _MALLOC(sizeof (*prl_32
), M_TEMP
, M_WAITOK
|M_ZERO
);
2533 /* preserve the interface name */
2534 bcopy(data
, prl_32
, sizeof (prl_32
->ifname
));
2536 while (pr
&& i
< PRLSTSIZ
) {
2537 struct nd_pfxrouter
*pfr
;
2541 (void) in6_embedscope(&prl_32
->prefix
[i
].prefix
,
2542 &pr
->ndpr_prefix
, NULL
, NULL
, NULL
);
2543 prl_32
->prefix
[i
].raflags
= pr
->ndpr_raf
;
2544 prl_32
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
2545 prl_32
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
2546 prl_32
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
2547 prl_32
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
2548 prl_32
->prefix
[i
].expire
= ndpr_getexpire(pr
);
2550 pfr
= pr
->ndpr_advrtrs
.lh_first
;
2554 #define RTRADDR prl_32->prefix[i].advrtr[j]
2555 RTRADDR
= pfr
->router
->rtaddr
;
2556 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
2557 /* XXX: hack for KAME */
2558 RTRADDR
.s6_addr16
[1] = 0;
2561 "a router(%s) advertises "
2563 "non-link local address\n",
2564 ip6_sprintf(&RTRADDR
));
2569 pfr
= pfr
->pfr_next
;
2571 prl_32
->prefix
[i
].advrtrs
= j
;
2572 prl_32
->prefix
[i
].origin
= PR_ORIG_RA
;
2578 bcopy(prl_32
, data
, sizeof (*prl_32
));
2579 _FREE(prl_32
, M_TEMP
);
2584 nd6_ioctl(u_long cmd
, caddr_t data
, struct ifnet
*ifp
)
2586 struct nd_defrouter
*dr
;
2587 struct nd_prefix
*pr
;
2591 VERIFY(ifp
!= NULL
);
2594 case SIOCGDRLST_IN6_32
: /* struct in6_drlist_32 */
2595 case SIOCGDRLST_IN6_64
: /* struct in6_drlist_64 */
2597 * obsolete API, use sysctl under net.inet6.icmp6
2599 lck_mtx_lock(nd6_mutex
);
2600 error
= nd6_siocgdrlst(data
, cmd
== SIOCGDRLST_IN6_64
);
2601 lck_mtx_unlock(nd6_mutex
);
2604 case SIOCGPRLST_IN6_32
: /* struct in6_prlist_32 */
2605 case SIOCGPRLST_IN6_64
: /* struct in6_prlist_64 */
2607 * obsolete API, use sysctl under net.inet6.icmp6
2609 lck_mtx_lock(nd6_mutex
);
2610 error
= nd6_siocgprlst(data
, cmd
== SIOCGPRLST_IN6_64
);
2611 lck_mtx_unlock(nd6_mutex
);
2614 case OSIOCGIFINFO_IN6
: /* struct in6_ondireq */
2615 case SIOCGIFINFO_IN6
: { /* struct in6_ondireq */
2617 struct in6_ondireq
*ondi
= (struct in6_ondireq
*)(void *)data
;
2618 struct nd_ifinfo
*ndi
;
2620 * SIOCGIFINFO_IN6 ioctl is encoded with in6_ondireq
2621 * instead of in6_ndireq, so we treat it as such.
2623 ndi
= ND_IFINFO(ifp
);
2624 if ((NULL
== ndi
) || (FALSE
== ndi
->initialized
)){
2628 lck_mtx_lock(&ndi
->lock
);
2629 linkmtu
= IN6_LINKMTU(ifp
);
2630 bcopy(&linkmtu
, &ondi
->ndi
.linkmtu
, sizeof (linkmtu
));
2631 bcopy(&ndi
->maxmtu
, &ondi
->ndi
.maxmtu
,
2632 sizeof (u_int32_t
));
2633 bcopy(&ndi
->basereachable
, &ondi
->ndi
.basereachable
,
2634 sizeof (u_int32_t
));
2635 bcopy(&ndi
->reachable
, &ondi
->ndi
.reachable
,
2636 sizeof (u_int32_t
));
2637 bcopy(&ndi
->retrans
, &ondi
->ndi
.retrans
,
2638 sizeof (u_int32_t
));
2639 bcopy(&ndi
->flags
, &ondi
->ndi
.flags
,
2640 sizeof (u_int32_t
));
2641 bcopy(&ndi
->recalctm
, &ondi
->ndi
.recalctm
,
2643 ondi
->ndi
.chlim
= ndi
->chlim
;
2644 ondi
->ndi
.receivedra
= 0;
2645 lck_mtx_unlock(&ndi
->lock
);
2649 case SIOCSIFINFO_FLAGS
: { /* struct in6_ndireq */
2651 * XXX BSD has a bunch of checks here to ensure
2652 * that interface disabled flag is not reset if
2653 * link local address has failed DAD.
2654 * Investigate that part.
2656 struct in6_ndireq
*cndi
= (struct in6_ndireq
*)(void *)data
;
2657 u_int32_t oflags
, flags
;
2658 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
2660 /* XXX: almost all other fields of cndi->ndi is unused */
2661 if ((NULL
== ndi
) || !ndi
->initialized
) {
2666 lck_mtx_lock(&ndi
->lock
);
2667 oflags
= ndi
->flags
;
2668 bcopy(&cndi
->ndi
.flags
, &(ndi
->flags
), sizeof (flags
));
2670 lck_mtx_unlock(&ndi
->lock
);
2672 if (oflags
== flags
) {
2676 error
= nd6_setifinfo(ifp
, oflags
, flags
);
2680 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
2681 /* flush default router list */
2683 * xxx sumikawa: should not delete route if default
2684 * route equals to the top of default router list
2686 lck_mtx_lock(nd6_mutex
);
2688 defrouter_select(ifp
);
2689 lck_mtx_unlock(nd6_mutex
);
2690 /* xxx sumikawa: flush prefix list */
2693 case SIOCSPFXFLUSH_IN6
: { /* struct in6_ifreq */
2694 /* flush all the prefix advertised by routers */
2695 struct nd_prefix
*next
= NULL
;
2697 lck_mtx_lock(nd6_mutex
);
2698 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
2699 struct in6_ifaddr
*ia
= NULL
;
2700 bool iterate_pfxlist_again
= false;
2702 next
= pr
->ndpr_next
;
2705 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
2709 if (ifp
!= lo_ifp
&& pr
->ndpr_ifp
!= ifp
) {
2713 /* do we really have to remove addresses as well? */
2714 NDPR_ADDREF_LOCKED(pr
);
2716 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2718 while (ia
!= NULL
) {
2719 IFA_LOCK(&ia
->ia_ifa
);
2720 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
2721 IFA_UNLOCK(&ia
->ia_ifa
);
2726 if (ia
->ia6_ndpr
== pr
) {
2727 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
2728 IFA_UNLOCK(&ia
->ia_ifa
);
2729 lck_rw_done(&in6_ifaddr_rwlock
);
2730 lck_mtx_unlock(nd6_mutex
);
2731 in6_purgeaddr(&ia
->ia_ifa
);
2732 IFA_REMREF(&ia
->ia_ifa
);
2733 lck_mtx_lock(nd6_mutex
);
2734 lck_rw_lock_exclusive(
2735 &in6_ifaddr_rwlock
);
2737 * Purging the address caused
2738 * in6_ifaddr_rwlock to be
2740 * reacquired; therefore search again
2741 * from the beginning of in6_ifaddrs.
2742 * The same applies for the prefix list.
2745 iterate_pfxlist_again
= true;
2748 IFA_UNLOCK(&ia
->ia_ifa
);
2751 lck_rw_done(&in6_ifaddr_rwlock
);
2755 pfxlist_onlink_check();
2757 if (iterate_pfxlist_again
) {
2758 next
= nd_prefix
.lh_first
;
2761 lck_mtx_unlock(nd6_mutex
);
2765 case SIOCSRTRFLUSH_IN6
: { /* struct in6_ifreq */
2766 /* flush all the default routers */
2767 struct nd_defrouter
*next
;
2769 lck_mtx_lock(nd6_mutex
);
2770 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
2772 * The first entry of the list may be stored in
2773 * the routing table, so we'll delete it later.
2775 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
2776 next
= TAILQ_NEXT(dr
, dr_entry
);
2777 if (ifp
== lo_ifp
|| dr
->ifp
== ifp
)
2780 if (ifp
== lo_ifp
||
2781 TAILQ_FIRST(&nd_defrouter
)->ifp
== ifp
)
2782 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
));
2784 lck_mtx_unlock(nd6_mutex
);
2788 case SIOCGNBRINFO_IN6_32
: { /* struct in6_nbrinfo_32 */
2789 struct llinfo_nd6
*ln
;
2790 struct in6_nbrinfo_32 nbi_32
;
2791 struct in6_addr nb_addr
; /* make local for safety */
2793 bcopy(data
, &nbi_32
, sizeof (nbi_32
));
2794 nb_addr
= nbi_32
.addr
;
2796 * XXX: KAME specific hack for scoped addresses
2797 * XXXX: for other scopes than link-local?
2799 if (IN6_IS_ADDR_LINKLOCAL(&nbi_32
.addr
) ||
2800 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_32
.addr
)) {
2802 (u_int16_t
*)(void *)&nb_addr
.s6_addr
[2];
2805 *idp
= htons(ifp
->if_index
);
2808 /* Callee returns a locked route upon success */
2809 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
2813 RT_LOCK_ASSERT_HELD(rt
);
2815 nbi_32
.state
= ln
->ln_state
;
2816 nbi_32
.asked
= ln
->ln_asked
;
2817 nbi_32
.isrouter
= ln
->ln_router
;
2818 nbi_32
.expire
= ln_getexpire(ln
);
2819 RT_REMREF_LOCKED(rt
);
2821 bcopy(&nbi_32
, data
, sizeof (nbi_32
));
2825 case SIOCGNBRINFO_IN6_64
: { /* struct in6_nbrinfo_64 */
2826 struct llinfo_nd6
*ln
;
2827 struct in6_nbrinfo_64 nbi_64
;
2828 struct in6_addr nb_addr
; /* make local for safety */
2830 bcopy(data
, &nbi_64
, sizeof (nbi_64
));
2831 nb_addr
= nbi_64
.addr
;
2833 * XXX: KAME specific hack for scoped addresses
2834 * XXXX: for other scopes than link-local?
2836 if (IN6_IS_ADDR_LINKLOCAL(&nbi_64
.addr
) ||
2837 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_64
.addr
)) {
2839 (u_int16_t
*)(void *)&nb_addr
.s6_addr
[2];
2842 *idp
= htons(ifp
->if_index
);
2845 /* Callee returns a locked route upon success */
2846 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
2850 RT_LOCK_ASSERT_HELD(rt
);
2852 nbi_64
.state
= ln
->ln_state
;
2853 nbi_64
.asked
= ln
->ln_asked
;
2854 nbi_64
.isrouter
= ln
->ln_router
;
2855 nbi_64
.expire
= ln_getexpire(ln
);
2856 RT_REMREF_LOCKED(rt
);
2858 bcopy(&nbi_64
, data
, sizeof (nbi_64
));
2862 case SIOCGDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
2863 case SIOCGDEFIFACE_IN6_64
: { /* struct in6_ndifreq_64 */
2864 struct in6_ndifreq_64
*ndif_64
=
2865 (struct in6_ndifreq_64
*)(void *)data
;
2866 struct in6_ndifreq_32
*ndif_32
=
2867 (struct in6_ndifreq_32
*)(void *)data
;
2869 if (cmd
== SIOCGDEFIFACE_IN6_64
) {
2870 u_int64_t j
= nd6_defifindex
;
2871 bcopy(&j
, &ndif_64
->ifindex
, sizeof (j
));
2873 bcopy(&nd6_defifindex
, &ndif_32
->ifindex
,
2874 sizeof (u_int32_t
));
2879 case SIOCSDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
2880 case SIOCSDEFIFACE_IN6_64
: { /* struct in6_ndifreq_64 */
2881 struct in6_ndifreq_64
*ndif_64
=
2882 (struct in6_ndifreq_64
*)(void *)data
;
2883 struct in6_ndifreq_32
*ndif_32
=
2884 (struct in6_ndifreq_32
*)(void *)data
;
2887 if (cmd
== SIOCSDEFIFACE_IN6_64
) {
2889 bcopy(&ndif_64
->ifindex
, &j
, sizeof (j
));
2892 bcopy(&ndif_32
->ifindex
, &idx
, sizeof (idx
));
2895 error
= nd6_setdefaultiface(idx
);
2904 * Create neighbor cache entry and cache link-layer address,
2905 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
2908 nd6_cache_lladdr(struct ifnet
*ifp
, struct in6_addr
*from
, char *lladdr
,
2909 int lladdrlen
, int type
, int code
)
2911 #pragma unused(lladdrlen)
2912 struct rtentry
*rt
= NULL
;
2913 struct llinfo_nd6
*ln
= NULL
;
2915 struct sockaddr_dl
*sdl
= NULL
;
2921 boolean_t sched_timeout
= FALSE
;
2924 panic("ifp == NULL in nd6_cache_lladdr");
2926 panic("from == NULL in nd6_cache_lladdr");
2928 /* nothing must be updated for unspecified address */
2929 if (IN6_IS_ADDR_UNSPECIFIED(from
))
2933 * Validation about ifp->if_addrlen and lladdrlen must be done in
2936 timenow
= net_uptime();
2938 rt
= nd6_lookup(from
, 0, ifp
, 0);
2940 if ((rt
= nd6_lookup(from
, 1, ifp
, 0)) == NULL
)
2942 RT_LOCK_ASSERT_HELD(rt
);
2945 RT_LOCK_ASSERT_HELD(rt
);
2946 /* do nothing if static ndp is set */
2947 if (rt
->rt_flags
& RTF_STATIC
) {
2948 RT_REMREF_LOCKED(rt
);
2957 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
2964 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
2967 if (rt
->rt_gateway
== NULL
)
2969 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
2971 sdl
= SDL(rt
->rt_gateway
);
2973 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
2974 if (olladdr
&& lladdr
) {
2975 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
2983 * newentry olladdr lladdr llchange (*=record)
2986 * 0 n y -- (3) * STALE
2988 * 0 y y y (5) * STALE
2989 * 1 -- n -- (6) NOSTATE(= PASSIVE)
2990 * 1 -- y -- (7) * STALE
2993 if (lladdr
) { /* (3-5) and (7) */
2995 * Record source link-layer address
2996 * XXX is it dependent to ifp->if_type?
2998 sdl
->sdl_alen
= ifp
->if_addrlen
;
2999 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
3001 /* cache the gateway (sender HW) address */
3002 nd6_llreach_alloc(rt
, ifp
, LLADDR(sdl
), sdl
->sdl_alen
, FALSE
);
3006 if ((!olladdr
&& lladdr
!= NULL
) || /* (3) */
3007 (olladdr
&& lladdr
!= NULL
&& llchange
)) { /* (5) */
3009 newstate
= ND6_LLINFO_STALE
;
3010 } else /* (1-2,4) */
3014 if (lladdr
== NULL
) /* (6) */
3015 newstate
= ND6_LLINFO_NOSTATE
;
3017 newstate
= ND6_LLINFO_STALE
;
3022 * Update the state of the neighbor cache.
3024 ln
->ln_state
= newstate
;
3026 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
3027 struct mbuf
*m
= ln
->ln_hold
;
3029 * XXX: since nd6_output() below will cause
3030 * state tansition to DELAY and reset the timer,
3031 * we must set the timer now, although it is actually
3034 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
3038 struct sockaddr_in6 sin6
;
3040 rtkey_to_sa6(rt
, &sin6
);
3042 * we assume ifp is not a p2p here, so just
3043 * set the 2nd argument as the 1st one.
3046 nd6_output_list(ifp
, ifp
, m
, &sin6
, rt
, NULL
);
3049 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
3050 /* probe right away */
3051 ln_setexpire(ln
, timenow
);
3052 sched_timeout
= TRUE
;
3057 * ICMP6 type dependent behavior.
3059 * NS: clear IsRouter if new entry
3060 * RS: clear IsRouter
3061 * RA: set IsRouter if there's lladdr
3062 * redir: clear IsRouter if new entry
3065 * The spec says that we must set IsRouter in the following cases:
3066 * - If lladdr exist, set IsRouter. This means (1-5).
3067 * - If it is old entry (!newentry), set IsRouter. This means (7).
3068 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
3069 * A quetion arises for (1) case. (1) case has no lladdr in the
3070 * neighbor cache, this is similar to (6).
3071 * This case is rare but we figured that we MUST NOT set IsRouter.
3073 * newentry olladdr lladdr llchange NS RS RA redir
3075 * 0 n n -- (1) c ? s
3076 * 0 y n -- (2) c s s
3077 * 0 n y -- (3) c s s
3080 * 1 -- n -- (6) c c c s
3081 * 1 -- y -- (7) c c s c s
3085 switch (type
& 0xff) {
3086 case ND_NEIGHBOR_SOLICIT
:
3088 * New entry must have is_router flag cleared.
3090 if (is_newentry
) /* (6-7) */
3095 * If the ICMP message is a Redirect to a better router, always
3096 * set the is_router flag. Otherwise, if the entry is newly
3097 * created, then clear the flag. [RFC 4861, sec 8.3]
3099 if (code
== ND_REDIRECT_ROUTER
)
3101 else if (is_newentry
) /* (6-7) */
3104 case ND_ROUTER_SOLICIT
:
3106 * is_router flag must always be cleared.
3110 case ND_ROUTER_ADVERT
:
3112 * Mark an entry with lladdr as a router.
3114 if ((!is_newentry
&& (olladdr
|| lladdr
)) || /* (2-5) */
3115 (is_newentry
&& lladdr
)) { /* (7) */
3122 * When the link-layer address of a router changes, select the
3123 * best router again. In particular, when the neighbor entry is newly
3124 * created, it might affect the selection policy.
3125 * Question: can we restrict the first condition to the "is_newentry"
3128 * Note: Perform default router selection even when we are a router,
3129 * if Scoped Routing is enabled.
3131 if (do_update
&& ln
->ln_router
&&
3132 (ip6_doscopedroute
|| !ip6_forwarding
)) {
3133 RT_REMREF_LOCKED(rt
);
3135 lck_mtx_lock(nd6_mutex
);
3136 defrouter_select(ifp
);
3137 lck_mtx_unlock(nd6_mutex
);
3139 RT_REMREF_LOCKED(rt
);
3142 if (sched_timeout
) {
3143 lck_mtx_lock(rnh_lock
);
3144 nd6_sched_timeout(NULL
, NULL
);
3145 lck_mtx_unlock(rnh_lock
);
3150 nd6_slowtimo(void *arg
)
3153 struct nd_ifinfo
*nd6if
= NULL
;
3154 struct ifnet
*ifp
= NULL
;
3156 ifnet_head_lock_shared();
3157 for (ifp
= ifnet_head
.tqh_first
; ifp
;
3158 ifp
= ifp
->if_link
.tqe_next
) {
3159 nd6if
= ND_IFINFO(ifp
);
3160 if ((NULL
== nd6if
) || (FALSE
== nd6if
->initialized
)) {
3164 lck_mtx_lock(&nd6if
->lock
);
3165 if (nd6if
->basereachable
&& /* already initialized */
3166 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
3168 * Since reachable time rarely changes by router
3169 * advertisements, we SHOULD insure that a new random
3170 * value gets recomputed at least once every few hours.
3173 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
3175 ND_COMPUTE_RTIME(nd6if
->basereachable
);
3177 lck_mtx_unlock(&nd6if
->lock
);
3180 timeout(nd6_slowtimo
, NULL
, ND6_SLOWTIMER_INTERVAL
* hz
);
3184 nd6_output(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m0
,
3185 struct sockaddr_in6
*dst
, struct rtentry
*hint0
, struct flowadv
*adv
)
3187 return nd6_output_list(ifp
, origifp
, m0
, dst
, hint0
, adv
);
3193 * Assumption: route determination for first packet can be correctly applied to
3194 * all packets in the chain.
3196 #define senderr(e) { error = (e); goto bad; }
3198 nd6_output_list(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m0
,
3199 struct sockaddr_in6
*dst
, struct rtentry
*hint0
, struct flowadv
*adv
)
3201 struct rtentry
*rt
= hint0
, *hint
= hint0
;
3202 struct llinfo_nd6
*ln
= NULL
;
3205 struct rtentry
*rtrele
= NULL
;
3206 struct nd_ifinfo
*ndi
= NULL
;
3210 RT_ADDREF_LOCKED(rt
);
3213 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
) || !nd6_need_cache(ifp
)) {
3220 * Next hop determination. Because we may involve the gateway route
3221 * in addition to the original route, locking is rather complicated.
3222 * The general concept is that regardless of whether the route points
3223 * to the original route or to the gateway route, this routine takes
3224 * an extra reference on such a route. This extra reference will be
3225 * released at the end.
3227 * Care must be taken to ensure that the "hint0" route never gets freed
3228 * via rtfree(), since the caller may have stored it inside a struct
3229 * route with a reference held for that placeholder.
3231 * This logic is similar to, though not exactly the same as the one
3232 * used by route_to_gwroute().
3236 * We have a reference to "rt" by now (or below via rtalloc1),
3237 * which will either be released or freed at the end of this
3240 RT_LOCK_ASSERT_HELD(rt
);
3241 if (!(rt
->rt_flags
& RTF_UP
)) {
3242 RT_REMREF_LOCKED(rt
);
3244 if ((hint
= rt
= rtalloc1_scoped(SA(dst
), 1, 0,
3245 ifp
->if_index
)) != NULL
) {
3247 if (rt
->rt_ifp
!= ifp
) {
3248 /* XXX: loop care? */
3250 error
= nd6_output_list(ifp
, origifp
, m0
,
3256 senderr(EHOSTUNREACH
);
3260 if (rt
->rt_flags
& RTF_GATEWAY
) {
3261 struct rtentry
*gwrt
;
3262 struct in6_ifaddr
*ia6
= NULL
;
3263 struct sockaddr_in6 gw6
;
3265 rtgw_to_sa6(rt
, &gw6
);
3267 * Must drop rt_lock since nd6_is_addr_neighbor()
3268 * calls nd6_lookup() and acquires rnh_lock.
3273 * We skip link-layer address resolution and NUD
3274 * if the gateway is not a neighbor from ND point
3275 * of view, regardless of the value of nd_ifinfo.flags.
3276 * The second condition is a bit tricky; we skip
3277 * if the gateway is our own address, which is
3278 * sometimes used to install a route to a p2p link.
3280 if (!nd6_is_addr_neighbor(&gw6
, ifp
, 0) ||
3281 (ia6
= in6ifa_ifpwithaddr(ifp
, &gw6
.sin6_addr
))) {
3283 * We allow this kind of tricky route only
3284 * when the outgoing interface is p2p.
3285 * XXX: we may need a more generic rule here.
3288 IFA_REMREF(&ia6
->ia_ifa
);
3289 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
3290 senderr(EHOSTUNREACH
);
3295 gw6
= *(SIN6(rt
->rt_gateway
));
3297 /* If hint is now down, give up */
3298 if (!(rt
->rt_flags
& RTF_UP
)) {
3300 senderr(EHOSTUNREACH
);
3303 /* If there's no gateway route, look it up */
3304 if ((gwrt
= rt
->rt_gwroute
) == NULL
) {
3308 /* Become a regular mutex */
3309 RT_CONVERT_LOCK(rt
);
3312 * Take gwrt's lock while holding route's lock;
3313 * this is okay since gwrt never points back
3314 * to rt, so no lock ordering issues.
3317 if (!(gwrt
->rt_flags
& RTF_UP
)) {
3318 rt
->rt_gwroute
= NULL
;
3323 lck_mtx_lock(rnh_lock
);
3324 gwrt
= rtalloc1_scoped_locked(SA(&gw6
), 1, 0,
3329 * Bail out if the route is down, no route
3330 * to gateway, circular route, or if the
3331 * gateway portion of "rt" has changed.
3333 if (!(rt
->rt_flags
& RTF_UP
) ||
3334 gwrt
== NULL
|| gwrt
== rt
||
3335 !equal(SA(&gw6
), rt
->rt_gateway
)) {
3337 RT_REMREF_LOCKED(gwrt
);
3342 rtfree_locked(gwrt
);
3343 lck_mtx_unlock(rnh_lock
);
3344 senderr(EHOSTUNREACH
);
3346 VERIFY(gwrt
!= NULL
);
3348 * Set gateway route; callee adds ref to gwrt;
3349 * gwrt has an extra ref from rtalloc1() for
3352 rt_set_gwroute(rt
, rt_key(rt
), gwrt
);
3354 lck_mtx_unlock(rnh_lock
);
3355 /* Remember to release/free "rt" at the end */
3359 RT_ADDREF_LOCKED(gwrt
);
3362 /* Remember to release/free "rt" at the end */
3369 * This is an opportunity to revalidate the parent
3370 * route's gwroute, in case it now points to a dead
3371 * route entry. Parent route won't go away since the
3372 * clone (hint) holds a reference to it. rt == gwrt.
3375 if ((hint
->rt_flags
& (RTF_WASCLONED
| RTF_UP
)) ==
3376 (RTF_WASCLONED
| RTF_UP
)) {
3377 struct rtentry
*prt
= hint
->rt_parent
;
3378 VERIFY(prt
!= NULL
);
3380 RT_CONVERT_LOCK(hint
);
3383 rt_revalidate_gwroute(prt
, rt
);
3390 /* rt == gwrt; if it is now down, give up */
3391 if (!(rt
->rt_flags
& RTF_UP
)) {
3395 /* "rtrele" == original "rt" */
3396 senderr(EHOSTUNREACH
);
3400 /* Become a regular mutex */
3401 RT_CONVERT_LOCK(rt
);
3405 * Address resolution or Neighbor Unreachability Detection
3407 * At this point, the destination of the packet must be a unicast
3408 * or an anycast address(i.e. not a multicast).
3411 /* Look up the neighbor cache for the nexthop */
3412 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0) {
3415 struct sockaddr_in6 sin6
;
3417 * Clear out Scope ID field in case it is set.
3420 sin6
.sin6_scope_id
= 0;
3422 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
3423 * the condition below is not very efficient. But we believe
3424 * it is tolerable, because this should be a rare case.
3425 * Must drop rt_lock since nd6_is_addr_neighbor() calls
3426 * nd6_lookup() and acquires rnh_lock.
3430 if (nd6_is_addr_neighbor(&sin6
, ifp
, 0)) {
3431 /* "rtrele" may have been used, so clean up "rt" now */
3433 /* Don't free "hint0" */
3439 /* Callee returns a locked route upon success */
3440 rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
, 0);
3442 RT_LOCK_ASSERT_HELD(rt
);
3445 } else if (rt
!= NULL
) {
3454 ndi
= ND_IFINFO(ifp
);
3455 VERIFY(ndi
!= NULL
&& ndi
->initialized
);
3456 lck_mtx_lock(&ndi
->lock
);
3457 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0 &&
3458 !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
3459 lck_mtx_unlock(&ndi
->lock
);
3461 "nd6_output: can't allocate llinfo for %s "
3462 "(ln=0x%llx, rt=0x%llx)\n",
3463 ip6_sprintf(&dst
->sin6_addr
),
3464 (uint64_t)VM_KERNEL_ADDRPERM(ln
),
3465 (uint64_t)VM_KERNEL_ADDRPERM(rt
));
3466 senderr(EIO
); /* XXX: good error? */
3468 lck_mtx_unlock(&ndi
->lock
);
3470 goto sendpkt
; /* send anyway */
3473 net_update_uptime();
3474 timenow
= net_uptime();
3476 /* We don't have to do link-layer address resolution on a p2p link. */
3477 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
3478 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
3479 ln
->ln_state
= ND6_LLINFO_STALE
;
3480 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
3484 * The first time we send a packet to a neighbor whose entry is
3485 * STALE, we have to change the state to DELAY and a sets a timer to
3486 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
3487 * neighbor unreachability detection on expiration.
3490 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
3492 ln
->ln_state
= ND6_LLINFO_DELAY
;
3493 ln_setexpire(ln
, timenow
+ nd6_delay
);
3494 /* N.B.: we will re-arm the timer below. */
3495 _CASSERT(ND6_LLINFO_DELAY
> ND6_LLINFO_INCOMPLETE
);
3499 * If the neighbor cache entry has a state other than INCOMPLETE
3500 * (i.e. its link-layer address is already resolved), just
3503 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) {
3506 * Move this entry to the head of the queue so that it is
3507 * less likely for this entry to be a target of forced
3508 * garbage collection (see nd6_rtrequest()). Do this only
3509 * if the entry is non-permanent (as permanent ones will
3510 * never be purged), and if the number of active entries
3511 * is at least half of the threshold.
3513 if (ln
->ln_state
== ND6_LLINFO_DELAY
||
3514 (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
3515 nd6_inuse
>= (ip6_neighborgcthresh
>> 1))) {
3516 lck_mtx_lock(rnh_lock
);
3517 if (ln
->ln_state
== ND6_LLINFO_DELAY
)
3518 nd6_sched_timeout(NULL
, NULL
);
3519 if (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
3520 nd6_inuse
>= (ip6_neighborgcthresh
>> 1)) {
3522 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
3528 lck_mtx_unlock(rnh_lock
);
3534 * If this is a prefix proxy route, record the inbound interface
3535 * so that it can be excluded from the list of interfaces eligible
3536 * for forwarding the proxied NS in nd6_prproxy_ns_output().
3538 if (rt
->rt_flags
& RTF_PROXY
)
3539 ln
->ln_exclifp
= ((origifp
== ifp
) ? NULL
: origifp
);
3542 * There is a neighbor cache entry, but no ethernet address
3543 * response yet. Replace the held mbuf (if any) with this
3546 * This code conforms to the rate-limiting rule described in Section
3547 * 7.2.2 of RFC 4861, because the timer is set correctly after sending
3550 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
)
3551 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
3553 m_freem_list(ln
->ln_hold
);
3555 if (ln
->ln_expire
!= 0 && ln
->ln_asked
< nd6_mmaxtries
&&
3556 ln
->ln_expire
<= timenow
) {
3558 ndi
= ND_IFINFO(ifp
);
3559 VERIFY(ndi
!= NULL
&& ndi
->initialized
);
3560 lck_mtx_lock(&ndi
->lock
);
3561 ln_setexpire(ln
, timenow
+ ndi
->retrans
/ 1000);
3562 lck_mtx_unlock(&ndi
->lock
);
3564 /* We still have a reference on rt (for ln) */
3566 nd6_prproxy_ns_output(ifp
, origifp
, NULL
,
3567 &dst
->sin6_addr
, ln
);
3569 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0);
3570 lck_mtx_lock(rnh_lock
);
3571 nd6_sched_timeout(NULL
, NULL
);
3572 lck_mtx_unlock(rnh_lock
);
3574 if(ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
3575 ln
->ln_expire
= timenow
;
3580 * Move this entry to the head of the queue so that it is
3581 * less likely for this entry to be a target of forced
3582 * garbage collection (see nd6_rtrequest()). Do this only
3583 * if the entry is non-permanent (as permanent ones will
3584 * never be purged), and if the number of active entries
3585 * is at least half of the threshold.
3587 if (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
3588 nd6_inuse
>= (ip6_neighborgcthresh
>> 1)) {
3589 lck_mtx_lock(rnh_lock
);
3591 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
3595 /* Clean up "rt" now while we can */
3597 RT_REMREF_LOCKED(rt
);
3603 rt
= NULL
; /* "rt" has been taken care of */
3604 lck_mtx_unlock(rnh_lock
);
3611 RT_LOCK_ASSERT_NOTHELD(rt
);
3613 /* discard the packet if IPv6 operation is disabled on the interface */
3614 if (ifp
->if_eflags
& IFEF_IPV6_DISABLED
) {
3615 error
= ENETDOWN
; /* better error? */
3619 if (ifp
->if_flags
& IFF_LOOPBACK
) {
3620 /* forwarding rules require the original scope_id */
3621 m0
->m_pkthdr
.rcvif
= origifp
;
3622 error
= dlil_output(origifp
, PF_INET6
, m0
, (caddr_t
)rt
,
3626 /* Do not allow loopback address to wind up on a wire */
3627 struct ip6_hdr
*ip6
= mtod(m0
, struct ip6_hdr
*);
3629 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
3630 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
))) {
3631 ip6stat
.ip6s_badscope
++;
3632 error
= EADDRNOTAVAIL
;
3639 /* Mark use timestamp */
3640 if (rt
->rt_llinfo
!= NULL
)
3641 nd6_llreach_use(rt
->rt_llinfo
);
3645 struct mbuf
*mcur
= m0
;
3646 uint32_t pktcnt
= 0;
3649 if (hint
!= NULL
&& nstat_collect
) {
3652 if ((mcur
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV6
) &&
3653 (mcur
->m_pkthdr
.tso_segsz
> 0))
3654 scnt
= mcur
->m_pkthdr
.len
/ mcur
->m_pkthdr
.tso_segsz
;
3658 nstat_route_tx(hint
, scnt
, mcur
->m_pkthdr
.len
, 0);
3662 mcur
->m_pkthdr
.rcvif
= NULL
;
3663 mcur
= mcur
->m_nextpkt
;
3665 if (pktcnt
> ip6_maxchainsent
)
3666 ip6_maxchainsent
= pktcnt
;
3667 error
= dlil_output(ifp
, PF_INET6
, m0
, (caddr_t
)rt
, SA(dst
), 0, adv
);
3675 /* Clean up "rt" unless it's already been done */
3679 RT_REMREF_LOCKED(rt
);
3686 /* And now clean up "rtrele" if there is any */
3687 if (rtrele
!= NULL
) {
3688 RT_LOCK_SPIN(rtrele
);
3689 if (rtrele
== hint0
) {
3690 RT_REMREF_LOCKED(rtrele
);
3702 nd6_need_cache(struct ifnet
*ifp
)
3705 * XXX: we currently do not make neighbor cache on any interface
3706 * other than ARCnet, Ethernet, FDDI and GIF.
3709 * - unidirectional tunnels needs no ND
3711 switch (ifp
->if_type
) {
3717 case IFT_IEEE8023ADLAG
:
3721 case IFT_GIF
: /* XXX need more cases? */
3735 nd6_storelladdr(struct ifnet
*ifp
, struct rtentry
*rt
, struct mbuf
*m
,
3736 struct sockaddr
*dst
, u_char
*desten
)
3739 struct sockaddr_dl
*sdl
;
3741 if (m
->m_flags
& M_MCAST
) {
3742 switch (ifp
->if_type
) {
3746 case IFT_IEEE8023ADLAG
:
3751 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
, desten
);
3754 for (i
= 0; i
< ifp
->if_addrlen
; i
++)
3761 return (0); /* caller will free mbuf */
3766 /* this could happen, if we could not allocate memory */
3767 return (0); /* caller will free mbuf */
3770 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
3771 printf("nd6_storelladdr: something odd happens\n");
3773 return (0); /* caller will free mbuf */
3775 sdl
= SDL(rt
->rt_gateway
);
3776 if (sdl
->sdl_alen
== 0) {
3777 /* this should be impossible, but we bark here for debugging */
3778 printf("nd6_storelladdr: sdl_alen == 0\n");
3780 return (0); /* caller will free mbuf */
3783 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
3789 * This is the ND pre-output routine; care must be taken to ensure that
3790 * the "hint" route never gets freed via rtfree(), since the caller may
3791 * have stored it inside a struct route with a reference held for that
3795 nd6_lookup_ipv6(ifnet_t ifp
, const struct sockaddr_in6
*ip6_dest
,
3796 struct sockaddr_dl
*ll_dest
, size_t ll_dest_len
, route_t hint
,
3799 route_t route
= hint
;
3801 struct sockaddr_dl
*sdl
= NULL
;
3804 if (ip6_dest
->sin6_family
!= AF_INET6
)
3805 return (EAFNOSUPPORT
);
3807 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
3812 * Callee holds a reference on the route and returns
3813 * with the route entry locked, upon success.
3815 result
= route_to_gwroute((const struct sockaddr
*)ip6_dest
,
3820 RT_LOCK_ASSERT_HELD(route
);
3823 if ((packet
->m_flags
& M_MCAST
) != 0) {
3826 result
= dlil_resolve_multi(ifp
,
3827 (const struct sockaddr
*)ip6_dest
,
3828 SA(ll_dest
), ll_dest_len
);
3834 if (route
== NULL
) {
3836 * This could happen, if we could not allocate memory or
3837 * if route_to_gwroute() didn't return a route.
3843 if (route
->rt_gateway
->sa_family
!= AF_LINK
) {
3844 printf("%s: route %s on %s%d gateway address not AF_LINK\n",
3845 __func__
, ip6_sprintf(&ip6_dest
->sin6_addr
),
3846 route
->rt_ifp
->if_name
, route
->rt_ifp
->if_unit
);
3847 result
= EADDRNOTAVAIL
;
3851 sdl
= SDL(route
->rt_gateway
);
3852 if (sdl
->sdl_alen
== 0) {
3853 /* this should be impossible, but we bark here for debugging */
3854 printf("%s: route %s on %s%d sdl_alen == 0\n", __func__
,
3855 ip6_sprintf(&ip6_dest
->sin6_addr
), route
->rt_ifp
->if_name
,
3856 route
->rt_ifp
->if_unit
);
3857 result
= EHOSTUNREACH
;
3861 copy_len
= sdl
->sdl_len
<= ll_dest_len
? sdl
->sdl_len
: ll_dest_len
;
3862 bcopy(sdl
, ll_dest
, copy_len
);
3865 if (route
!= NULL
) {
3866 if (route
== hint
) {
3867 RT_REMREF_LOCKED(route
);
3878 nd6_setifinfo(struct ifnet
*ifp
, u_int32_t before
, u_int32_t after
)
3884 * Handle ND6_IFF_IFDISABLED
3886 if ((before
& ND6_IFF_IFDISABLED
) ||
3887 (after
& ND6_IFF_IFDISABLED
)) {
3888 b
= (before
& ND6_IFF_IFDISABLED
);
3889 a
= (after
& ND6_IFF_IFDISABLED
);
3891 if (b
!= a
&& (err
= nd6_if_disable(ifp
,
3892 ((int32_t)(a
- b
) > 0))) != 0)
3897 * Handle ND6_IFF_PROXY_PREFIXES
3899 if ((before
& ND6_IFF_PROXY_PREFIXES
) ||
3900 (after
& ND6_IFF_PROXY_PREFIXES
)) {
3901 b
= (before
& ND6_IFF_PROXY_PREFIXES
);
3902 a
= (after
& ND6_IFF_PROXY_PREFIXES
);
3904 if (b
!= a
&& (err
= nd6_if_prproxy(ifp
,
3905 ((int32_t)(a
- b
) > 0))) != 0)
3913 * Enable/disable IPv6 on an interface, called as part of
3914 * setting/clearing ND6_IFF_IFDISABLED, or during DAD failure.
3917 nd6_if_disable(struct ifnet
*ifp
, boolean_t enable
)
3919 ifnet_lock_shared(ifp
);
3921 ifp
->if_eflags
|= IFEF_IPV6_DISABLED
;
3923 ifp
->if_eflags
&= ~IFEF_IPV6_DISABLED
;
3924 ifnet_lock_done(ifp
);
3930 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
3932 #pragma unused(oidp, arg1, arg2)
3933 char pbuf
[MAX_IPv6_STR_LEN
];
3934 struct nd_defrouter
*dr
;
3937 if (req
->newptr
!= USER_ADDR_NULL
)
3940 lck_mtx_lock(nd6_mutex
);
3941 if (proc_is64bit(req
->p
)) {
3942 struct in6_defrouter_64 d
;
3944 bzero(&d
, sizeof (d
));
3945 d
.rtaddr
.sin6_family
= AF_INET6
;
3946 d
.rtaddr
.sin6_len
= sizeof (d
.rtaddr
);
3948 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
3949 d
.rtaddr
.sin6_addr
= dr
->rtaddr
;
3950 if (in6_recoverscope(&d
.rtaddr
,
3951 &dr
->rtaddr
, dr
->ifp
) != 0)
3952 log(LOG_ERR
, "scope error in default router "
3953 "list (%s)\n", inet_ntop(AF_INET6
,
3954 &dr
->rtaddr
, pbuf
, sizeof (pbuf
)));
3955 d
.flags
= dr
->flags
;
3956 d
.stateflags
= dr
->stateflags
;
3957 d
.stateflags
&= ~NDDRF_PROCESSED
;
3958 d
.rtlifetime
= dr
->rtlifetime
;
3959 d
.expire
= nddr_getexpire(dr
);
3960 d
.if_index
= dr
->ifp
->if_index
;
3961 error
= SYSCTL_OUT(req
, &d
, sizeof (d
));
3966 struct in6_defrouter_32 d
;
3968 bzero(&d
, sizeof (d
));
3969 d
.rtaddr
.sin6_family
= AF_INET6
;
3970 d
.rtaddr
.sin6_len
= sizeof (d
.rtaddr
);
3972 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
3973 d
.rtaddr
.sin6_addr
= dr
->rtaddr
;
3974 if (in6_recoverscope(&d
.rtaddr
,
3975 &dr
->rtaddr
, dr
->ifp
) != 0)
3976 log(LOG_ERR
, "scope error in default router "
3977 "list (%s)\n", inet_ntop(AF_INET6
,
3978 &dr
->rtaddr
, pbuf
, sizeof (pbuf
)));
3979 d
.flags
= dr
->flags
;
3980 d
.stateflags
= dr
->stateflags
;
3981 d
.stateflags
&= ~NDDRF_PROCESSED
;
3982 d
.rtlifetime
= dr
->rtlifetime
;
3983 d
.expire
= nddr_getexpire(dr
);
3984 d
.if_index
= dr
->ifp
->if_index
;
3985 error
= SYSCTL_OUT(req
, &d
, sizeof (d
));
3990 lck_mtx_unlock(nd6_mutex
);
3995 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
3997 #pragma unused(oidp, arg1, arg2)
3998 char pbuf
[MAX_IPv6_STR_LEN
];
3999 struct nd_pfxrouter
*pfr
;
4000 struct sockaddr_in6 s6
;
4001 struct nd_prefix
*pr
;
4004 if (req
->newptr
!= USER_ADDR_NULL
)
4007 bzero(&s6
, sizeof (s6
));
4008 s6
.sin6_family
= AF_INET6
;
4009 s6
.sin6_len
= sizeof (s6
);
4011 lck_mtx_lock(nd6_mutex
);
4012 if (proc_is64bit(req
->p
)) {
4013 struct in6_prefix_64 p
;
4015 bzero(&p
, sizeof (p
));
4016 p
.origin
= PR_ORIG_RA
;
4018 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
4020 p
.prefix
= pr
->ndpr_prefix
;
4021 if (in6_recoverscope(&p
.prefix
,
4022 &pr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
4023 log(LOG_ERR
, "scope error in "
4024 "prefix list (%s)\n", inet_ntop(AF_INET6
,
4025 &p
.prefix
.sin6_addr
, pbuf
, sizeof (pbuf
)));
4026 p
.raflags
= pr
->ndpr_raf
;
4027 p
.prefixlen
= pr
->ndpr_plen
;
4028 p
.vltime
= pr
->ndpr_vltime
;
4029 p
.pltime
= pr
->ndpr_pltime
;
4030 p
.if_index
= pr
->ndpr_ifp
->if_index
;
4031 p
.expire
= ndpr_getexpire(pr
);
4032 p
.refcnt
= pr
->ndpr_addrcnt
;
4033 p
.flags
= pr
->ndpr_stateflags
;
4035 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
)
4037 error
= SYSCTL_OUT(req
, &p
, sizeof (p
));
4042 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
) {
4043 s6
.sin6_addr
= pfr
->router
->rtaddr
;
4044 if (in6_recoverscope(&s6
, &pfr
->router
->rtaddr
,
4045 pfr
->router
->ifp
) != 0)
4047 "scope error in prefix list (%s)\n",
4048 inet_ntop(AF_INET6
, &s6
.sin6_addr
,
4049 pbuf
, sizeof (pbuf
)));
4050 error
= SYSCTL_OUT(req
, &s6
, sizeof (s6
));
4059 struct in6_prefix_32 p
;
4061 bzero(&p
, sizeof (p
));
4062 p
.origin
= PR_ORIG_RA
;
4064 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
4066 p
.prefix
= pr
->ndpr_prefix
;
4067 if (in6_recoverscope(&p
.prefix
,
4068 &pr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
4070 "scope error in prefix list (%s)\n",
4071 inet_ntop(AF_INET6
, &p
.prefix
.sin6_addr
,
4072 pbuf
, sizeof (pbuf
)));
4073 p
.raflags
= pr
->ndpr_raf
;
4074 p
.prefixlen
= pr
->ndpr_plen
;
4075 p
.vltime
= pr
->ndpr_vltime
;
4076 p
.pltime
= pr
->ndpr_pltime
;
4077 p
.if_index
= pr
->ndpr_ifp
->if_index
;
4078 p
.expire
= ndpr_getexpire(pr
);
4079 p
.refcnt
= pr
->ndpr_addrcnt
;
4080 p
.flags
= pr
->ndpr_stateflags
;
4082 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
)
4084 error
= SYSCTL_OUT(req
, &p
, sizeof (p
));
4089 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
) {
4090 s6
.sin6_addr
= pfr
->router
->rtaddr
;
4091 if (in6_recoverscope(&s6
, &pfr
->router
->rtaddr
,
4092 pfr
->router
->ifp
) != 0)
4094 "scope error in prefix list (%s)\n",
4095 inet_ntop(AF_INET6
, &s6
.sin6_addr
,
4096 pbuf
, sizeof (pbuf
)));
4097 error
= SYSCTL_OUT(req
, &s6
, sizeof (s6
));
4106 lck_mtx_unlock(nd6_mutex
);