2 * Copyright (c) 2000-2011 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@
29 /* $FreeBSD: src/sys/netinet6/nd6.c,v 1.20 2002/08/02 20:49:14 rwatson Exp $ */
30 /* $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * BSD/OS version heavily modifies this code, related to llinfo.
65 * Since we don't have BSD/OS version of net/route.c in our hand,
66 * I left the code mostly as it was in 970310. -- itojun
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
73 #include <sys/socket.h>
74 #include <sys/sockio.h>
76 #include <sys/kernel.h>
77 #include <sys/sysctl.h>
78 #include <sys/errno.h>
79 #include <sys/syslog.h>
80 #include <sys/protosw.h>
82 #include <sys/mcache.h>
84 #include <kern/queue.h>
85 #include <kern/zalloc.h>
87 #define DONT_WARN_OBSOLETE
89 #include <net/if_dl.h>
90 #include <net/if_types.h>
91 #include <net/if_llreach.h>
92 #include <net/route.h>
94 #include <net/ntstat.h>
96 #include <netinet/in.h>
97 #include <netinet/in_arp.h>
98 #include <netinet/if_ether.h>
99 #include <netinet6/in6_var.h>
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/nd6.h>
103 #include <netinet6/scope6_var.h>
104 #include <netinet/icmp6.h>
108 #include <net/net_osdep.h>
110 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
111 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
113 #define SA(p) ((struct sockaddr *)(p))
114 #define SIN6(s) ((struct sockaddr_in6 *)s)
115 #define SDL(s) ((struct sockaddr_dl *)s)
116 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
119 int nd6_prune
= 1; /* walk list every 1 seconds */
120 int nd6_delay
= 5; /* delay first probe time 5 second */
121 int nd6_umaxtries
= 3; /* maximum unicast query */
122 int nd6_mmaxtries
= 3; /* maximum multicast query */
123 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
124 int nd6_gctimer
= (60 * 60 * 24); /* 1 day: garbage collection timer */
126 /* preventing too many loops in ND option parsing */
127 int nd6_maxndopt
= 10; /* max # of ND options allowed */
129 int nd6_maxnudhint
= 0; /* max # of subsequent upper layer hints */
130 int nd6_maxqueuelen
= 1; /* max # of packets cached in unresolved ND entries */
138 static int nd6_is_new_addr_neighbor (struct sockaddr_in6
*, struct ifnet
*);
141 static int nd6_inuse
, nd6_allocated
;
144 * Synchronization notes:
146 * The global list of ND entries are stored in llinfo_nd6; an entry
147 * gets inserted into the list when the route is created and gets
148 * removed from the list when it is deleted; this is done as part
149 * of RTM_ADD/RTM_RESOLVE/RTM_DELETE in nd6_rtrequest().
151 * Because rnh_lock and rt_lock for the entry are held during those
152 * operations, the same locks (and thus lock ordering) must be used
153 * elsewhere to access the relevant data structure fields:
155 * ln_next, ln_prev, ln_rt
157 * - Routing lock (rnh_lock)
159 * ln_hold, ln_asked, ln_expire, ln_state, ln_router, ln_byhint, ln_flags,
160 * ln_llreach, ln_lastused
162 * - Routing entry lock (rt_lock)
164 * Due to the dependency on rt_lock, llinfo_nd6 has the same lifetime
165 * as the route entry itself. When a route is deleted (RTM_DELETE),
166 * it is simply removed from the global list but the memory is not
167 * freed until the route itself is freed.
169 struct llinfo_nd6 llinfo_nd6
= {
170 &llinfo_nd6
, &llinfo_nd6
, NULL
, NULL
, 0, 0, 0, 0, 0, 0, NULL
, 0
173 /* Protected by nd_if_rwlock */
174 size_t nd_ifinfo_indexlim
= 32; /* increased for 5589193 */
175 struct nd_ifinfo
*nd_ifinfo
= NULL
;
177 static lck_grp_attr_t
*nd_if_rwlock_grp_attr
;
178 static lck_grp_t
*nd_if_rwlock_grp
;
179 static lck_attr_t
*nd_if_rwlock_attr
;
180 lck_rw_t
*nd_if_rwlock
;
182 /* Protected by nd6_mutex */
183 struct nd_drhead nd_defrouter
;
184 struct nd_prhead nd_prefix
= { 0 };
186 /* Serialization variables for nd6_drain() */
187 static boolean_t nd6_drain_busy
;
188 static void *nd6_drain_waitchan
= &nd6_drain_busy
;
189 static int nd6_drain_waiters
= 0;
191 int nd6_recalc_reachtm_interval
= ND6_RECALC_REACHTM_INTERVAL
;
192 static struct sockaddr_in6 all1_sa
;
194 static int regen_tmpaddr(struct in6_ifaddr
*);
195 extern lck_mtx_t
*nd6_mutex
;
197 static void nd6_slowtimo(void *ignored_arg
);
198 static struct llinfo_nd6
*nd6_llinfo_alloc(void);
199 static void nd6_llinfo_free(void *);
200 static void nd6_llinfo_purge(struct rtentry
*);
201 static void nd6_llinfo_get_ri(struct rtentry
*, struct rt_reach_info
*);
203 static void nd6_siocgdrlst(void *, int);
204 static void nd6_siocgprlst(void *, int);
207 * Insertion and removal from llinfo_nd6 must be done with rnh_lock held.
209 #define LN_DEQUEUE(_ln) do { \
210 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); \
211 RT_LOCK_ASSERT_HELD((_ln)->ln_rt); \
212 (_ln)->ln_next->ln_prev = (_ln)->ln_prev; \
213 (_ln)->ln_prev->ln_next = (_ln)->ln_next; \
214 (_ln)->ln_prev = (_ln)->ln_next = NULL; \
215 (_ln)->ln_flags &= ~ND6_LNF_IN_USE; \
218 #define LN_INSERTHEAD(_ln) do { \
219 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); \
220 RT_LOCK_ASSERT_HELD((_ln)->ln_rt); \
221 (_ln)->ln_next = llinfo_nd6.ln_next; \
222 llinfo_nd6.ln_next = (_ln); \
223 (_ln)->ln_prev = &llinfo_nd6; \
224 (_ln)->ln_next->ln_prev = (_ln); \
225 (_ln)->ln_flags |= ND6_LNF_IN_USE; \
228 static struct zone
*llinfo_nd6_zone
;
229 #define LLINFO_ND6_ZONE_MAX 256 /* maximum elements in zone */
230 #define LLINFO_ND6_ZONE_NAME "llinfo_nd6" /* name for zone */
235 static int nd6_init_done
= 0;
239 log(LOG_NOTICE
, "nd6_init called more than once (ignored)\n");
243 all1_sa
.sin6_family
= AF_INET6
;
244 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
245 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++)
246 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
248 /* initialization of the default router list */
249 TAILQ_INIT(&nd_defrouter
);
251 nd_if_rwlock_grp_attr
= lck_grp_attr_alloc_init();
252 nd_if_rwlock_grp
= lck_grp_alloc_init("nd_if_rwlock",
253 nd_if_rwlock_grp_attr
);
254 nd_if_rwlock_attr
= lck_attr_alloc_init();
255 nd_if_rwlock
= lck_rw_alloc_init(nd_if_rwlock_grp
, nd_if_rwlock_attr
);
257 llinfo_nd6_zone
= zinit(sizeof (struct llinfo_nd6
),
258 LLINFO_ND6_ZONE_MAX
* sizeof (struct llinfo_nd6
), 0,
259 LLINFO_ND6_ZONE_NAME
);
260 if (llinfo_nd6_zone
== NULL
)
261 panic("%s: failed allocating llinfo_nd6_zone", __func__
);
263 zone_change(llinfo_nd6_zone
, Z_EXPAND
, TRUE
);
264 zone_change(llinfo_nd6_zone
, Z_CALLERACCT
, FALSE
);
272 timeout(nd6_slowtimo
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
275 static struct llinfo_nd6
*
276 nd6_llinfo_alloc(void)
278 return (zalloc(llinfo_nd6_zone
));
282 nd6_llinfo_free(void *arg
)
284 struct llinfo_nd6
*ln
= arg
;
286 if (ln
->ln_next
!= NULL
|| ln
->ln_prev
!= NULL
) {
287 panic("%s: trying to free %p when it is in use", __func__
, ln
);
291 /* Just in case there's anything there, free it */
292 if (ln
->ln_hold
!= NULL
) {
293 m_freem(ln
->ln_hold
);
297 /* Purge any link-layer info caching */
298 VERIFY(ln
->ln_rt
->rt_llinfo
== ln
);
299 if (ln
->ln_rt
->rt_llinfo_purge
!= NULL
)
300 ln
->ln_rt
->rt_llinfo_purge(ln
->ln_rt
);
302 zfree(llinfo_nd6_zone
, ln
);
306 nd6_llinfo_purge(struct rtentry
*rt
)
308 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
310 RT_LOCK_ASSERT_HELD(rt
);
311 VERIFY(rt
->rt_llinfo_purge
== nd6_llinfo_purge
&& ln
!= NULL
);
313 if (ln
->ln_llreach
!= NULL
) {
315 ifnet_llreach_free(ln
->ln_llreach
);
316 ln
->ln_llreach
= NULL
;
322 nd6_llinfo_get_ri(struct rtentry
*rt
, struct rt_reach_info
*ri
)
324 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
325 struct if_llreach
*lr
= ln
->ln_llreach
;
328 bzero(ri
, sizeof (*ri
));
331 /* Export to rt_reach_info structure */
333 /* Export ND6 send expiration time */
334 ri
->ri_snd_expire
= ifnet_llreach_up2cal(lr
, ln
->ln_lastused
);
340 nd6_ifattach(struct ifnet
*ifp
)
344 * We have some arrays that should be indexed by if_index.
345 * since if_index will grow dynamically, they should grow too.
347 lck_rw_lock_exclusive(nd_if_rwlock
);
348 if (nd_ifinfo
== NULL
|| if_index
>= nd_ifinfo_indexlim
) {
351 size_t newlim
= nd_ifinfo_indexlim
;
353 while (if_index
>= newlim
)
357 n
= newlim
* sizeof(struct nd_ifinfo
);
358 q
= (caddr_t
)_MALLOC(n
, M_IP6NDP
, M_WAITOK
);
360 lck_rw_done(nd_if_rwlock
);
364 nd_ifinfo_indexlim
= newlim
;
366 bcopy((caddr_t
)nd_ifinfo
, q
, n
/2);
368 * We might want to pattern fill the old
369 * array to catch use-after-free cases.
371 FREE((caddr_t
)nd_ifinfo
, M_IP6NDP
);
373 nd_ifinfo
= (struct nd_ifinfo
*)q
;
375 lck_rw_done(nd_if_rwlock
);
377 #define ND nd_ifinfo[ifp->if_index]
380 * Don't initialize if called twice.
381 * XXX: to detect this, we should choose a member that is never set
382 * before initialization of the ND structure itself. We formaly used
383 * the linkmtu member, which was not suitable because it could be
384 * initialized via "ifconfig mtu".
386 lck_rw_lock_shared(nd_if_rwlock
);
387 if (ND
.basereachable
) {
388 lck_rw_done(nd_if_rwlock
);
391 ND
.linkmtu
= ifp
->if_mtu
;
392 ND
.chlim
= IPV6_DEFHLIM
;
393 ND
.basereachable
= REACHABLE_TIME
;
394 ND
.reachable
= ND_COMPUTE_RTIME(ND
.basereachable
);
395 ND
.retrans
= RETRANS_TIMER
;
396 ND
.flags
= ND6_IFF_PERFORMNUD
;
397 lck_rw_done(nd_if_rwlock
);
405 * Reset ND level link MTU. This function is called when the physical MTU
406 * changes, which means we might have to adjust the ND level MTU.
409 nd6_setmtu(struct ifnet
*ifp
)
411 struct nd_ifinfo
*ndi
;
412 u_int32_t oldmaxmtu
, maxmtu
;
415 * Make sure IPv6 is enabled for the interface first,
416 * because this can be called directly from SIOCSIFMTU for IPv4
418 lck_rw_lock_shared(nd_if_rwlock
);
419 if (ifp
->if_index
>= nd_ifinfo_indexlim
) {
420 lck_rw_done(nd_if_rwlock
);
421 return; /* we're out of bound for nd_ifinfo */
424 ndi
= &nd_ifinfo
[ifp
->if_index
];
425 oldmaxmtu
= ndi
->maxmtu
;
428 * The ND level maxmtu is somewhat redundant to the interface MTU
429 * and is an implementation artifact of KAME. Instead of hard-
430 * limiting the maxmtu based on the interface type here, we simply
431 * take the if_mtu value since SIOCSIFMTU would have taken care of
432 * the sanity checks related to the maximum MTU allowed for the
433 * interface (a value that is known only by the interface layer),
434 * by sending the request down via ifnet_ioctl(). The use of the
435 * ND level maxmtu and linkmtu are done via IN6_LINKMTU() which
436 * does further checking against if_mtu.
438 maxmtu
= ndi
->maxmtu
= ifp
->if_mtu
;
441 * Decreasing the interface MTU under IPV6 minimum MTU may cause
442 * undesirable situation. We thus notify the operator of the change
443 * explicitly. The check for oldmaxmtu is necessary to restrict the
444 * log to the case of changing the MTU, not initializing it.
446 if (oldmaxmtu
>= IPV6_MMTU
&& ndi
->maxmtu
< IPV6_MMTU
) {
447 log(LOG_NOTICE
, "nd6_setmtu: "
448 "new link MTU on %s%d (%u) is too small for IPv6\n",
449 ifp
->if_name
, ifp
->if_unit
, (uint32_t)ndi
->maxmtu
);
451 ndi
->linkmtu
= ifp
->if_mtu
;
452 lck_rw_done(nd_if_rwlock
);
454 /* also adjust in6_maxmtu if necessary. */
455 if (maxmtu
> in6_maxmtu
)
463 union nd_opts
*ndopts
)
465 bzero(ndopts
, sizeof(*ndopts
));
466 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
468 = (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
471 ndopts
->nd_opts_done
= 1;
472 ndopts
->nd_opts_search
= NULL
;
477 * Take one ND option.
481 union nd_opts
*ndopts
)
483 struct nd_opt_hdr
*nd_opt
;
487 panic("ndopts == NULL in nd6_option\n");
488 if (!ndopts
->nd_opts_last
)
489 panic("uninitialized ndopts in nd6_option\n");
490 if (!ndopts
->nd_opts_search
)
492 if (ndopts
->nd_opts_done
)
495 nd_opt
= ndopts
->nd_opts_search
;
497 /* make sure nd_opt_len is inside the buffer */
498 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
499 bzero(ndopts
, sizeof(*ndopts
));
503 olen
= nd_opt
->nd_opt_len
<< 3;
506 * Message validation requires that all included
507 * options have a length that is greater than zero.
509 bzero(ndopts
, sizeof(*ndopts
));
513 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
514 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
515 /* option overruns the end of buffer, invalid */
516 bzero(ndopts
, sizeof(*ndopts
));
518 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
519 /* reached the end of options chain */
520 ndopts
->nd_opts_done
= 1;
521 ndopts
->nd_opts_search
= NULL
;
527 * Parse multiple ND options.
528 * This function is much easier to use, for ND routines that do not need
529 * multiple options of the same type.
533 union nd_opts
*ndopts
)
535 struct nd_opt_hdr
*nd_opt
;
539 panic("ndopts == NULL in nd6_options");
540 if (ndopts
->nd_opts_last
== NULL
)
541 panic("uninitialized ndopts in nd6_options");
542 if (ndopts
->nd_opts_search
== NULL
)
546 nd_opt
= nd6_option(ndopts
);
547 if (nd_opt
== NULL
&& ndopts
->nd_opts_last
== NULL
) {
549 * Message validation requires that all included
550 * options have a length that is greater than zero.
552 icmp6stat
.icp6s_nd_badopt
++;
553 bzero(ndopts
, sizeof(*ndopts
));
560 switch (nd_opt
->nd_opt_type
) {
561 case ND_OPT_SOURCE_LINKADDR
:
562 case ND_OPT_TARGET_LINKADDR
:
564 case ND_OPT_REDIRECTED_HEADER
:
565 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
567 "duplicated ND6 option found (type=%d)\n",
568 nd_opt
->nd_opt_type
));
571 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
575 case ND_OPT_PREFIX_INFORMATION
:
576 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
577 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
580 ndopts
->nd_opts_pi_end
=
581 (struct nd_opt_prefix_info
*)nd_opt
;
588 * Unknown options must be silently ignored,
589 * to accomodate future extension to the protocol.
592 "nd6_options: unsupported option %d - "
593 "option ignored\n", nd_opt
->nd_opt_type
));
598 if (i
> nd6_maxndopt
) {
599 icmp6stat
.icp6s_nd_toomanyopt
++;
600 nd6log((LOG_INFO
, "too many loop in nd opt\n"));
604 if (ndopts
->nd_opts_done
)
612 nd6_drain(__unused
void *ignored_arg
)
614 struct llinfo_nd6
*ln
;
615 struct nd_defrouter
*dr
;
616 struct nd_prefix
*pr
;
617 struct ifnet
*ifp
= NULL
;
618 struct in6_ifaddr
*ia6
, *nia6
;
619 struct in6_addrlifetime
*lt6
;
620 struct timeval timenow
;
622 getmicrotime(&timenow
);
625 * The global list llinfo_nd6 is modified by nd6_request() and is
626 * therefore protected by rnh_lock. For obvious reasons, we cannot
627 * hold rnh_lock across calls that might lead to code paths which
628 * attempt to acquire rnh_lock, else we deadlock. Hence for such
629 * cases we drop rt_lock and rnh_lock, make the calls, and repeat the
630 * loop. To ensure that we don't process the same entry more than
631 * once in a single timeout, we mark the "already-seen" entries with
632 * ND6_LNF_TIMER_SKIP flag. At the end of the loop, we do a second
633 * pass thru the entries and clear the flag so they can be processed
634 * during the next timeout.
636 lck_mtx_lock(rnh_lock
);
637 ln
= llinfo_nd6
.ln_next
;
638 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
640 struct sockaddr_in6
*dst
;
641 struct llinfo_nd6
*next
;
643 /* ln_next/prev/rt is protected by rnh_lock */
648 /* We've seen this already; skip it */
649 if (ln
->ln_flags
& ND6_LNF_TIMER_SKIP
) {
655 /* rt->rt_ifp should never be NULL */
656 if ((ifp
= rt
->rt_ifp
) == NULL
) {
657 panic("%s: ln(%p) rt(%p) rt_ifp == NULL", __func__
,
662 /* rt_llinfo must always be equal to ln */
663 if ((struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
) {
664 panic("%s: rt_llinfo(%p) is not equal to ln(%p)",
665 __func__
, rt
->rt_llinfo
, ln
);
669 /* rt_key should never be NULL */
670 dst
= (struct sockaddr_in6
*)rt_key(rt
);
672 panic("%s: rt(%p) key is NULL ln(%p)", __func__
,
677 /* Set the flag in case we jump to "again" */
678 ln
->ln_flags
|= ND6_LNF_TIMER_SKIP
;
680 if (ln
->ln_expire
> timenow
.tv_sec
) {
686 /* Make a copy (we're using it read-only anyway) */
687 lck_rw_lock_shared(nd_if_rwlock
);
688 if (ifp
->if_index
>= nd_ifinfo_indexlim
) {
689 lck_rw_done(nd_if_rwlock
);
694 lck_rw_done(nd_if_rwlock
);
696 RT_LOCK_ASSERT_HELD(rt
);
698 switch (ln
->ln_state
) {
699 case ND6_LLINFO_INCOMPLETE
:
700 if (ln
->ln_asked
< nd6_mmaxtries
) {
702 lck_rw_lock_shared(nd_if_rwlock
);
703 ln
->ln_expire
= timenow
.tv_sec
+
704 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
705 lck_rw_done(nd_if_rwlock
);
706 RT_ADDREF_LOCKED(rt
);
708 lck_mtx_unlock(rnh_lock
);
709 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
,
713 struct mbuf
*m
= ln
->ln_hold
;
717 * Fake rcvif to make ICMP error
718 * more helpful in diagnosing
720 * XXX: should we consider
723 m
->m_pkthdr
.rcvif
= ifp
;
725 lck_mtx_unlock(rnh_lock
);
726 icmp6_error(m
, ICMP6_DST_UNREACH
,
727 ICMP6_DST_UNREACH_ADDR
, 0);
730 lck_mtx_unlock(rnh_lock
);
734 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
737 case ND6_LLINFO_REACHABLE
:
739 ln
->ln_state
= ND6_LLINFO_STALE
;
740 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
,
746 case ND6_LLINFO_STALE
:
747 case ND6_LLINFO_PURGE
:
748 /* Garbage Collection(RFC 2461 5.3) */
751 lck_mtx_unlock(rnh_lock
);
753 lck_mtx_assert(rnh_lock
,
754 LCK_MTX_ASSERT_NOTOWNED
);
761 case ND6_LLINFO_DELAY
:
762 lck_rw_lock_shared(nd_if_rwlock
);
763 if ((nd_ifinfo
[ifp
->if_index
].flags
&
764 ND6_IFF_PERFORMNUD
) != 0) {
767 ln
->ln_state
= ND6_LLINFO_PROBE
;
768 ln
->ln_expire
= timenow
.tv_sec
+
769 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
770 lck_rw_done(nd_if_rwlock
);
771 RT_ADDREF_LOCKED(rt
);
773 lck_mtx_unlock(rnh_lock
);
774 nd6_ns_output(ifp
, &dst
->sin6_addr
,
775 &dst
->sin6_addr
, ln
, 0);
776 lck_mtx_assert(rnh_lock
,
777 LCK_MTX_ASSERT_NOTOWNED
);
781 lck_rw_done(nd_if_rwlock
);
782 ln
->ln_state
= ND6_LLINFO_STALE
; /* XXX */
783 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
,
788 case ND6_LLINFO_PROBE
:
789 if (ln
->ln_asked
< nd6_umaxtries
) {
791 lck_rw_lock_shared(nd_if_rwlock
);
792 ln
->ln_expire
= timenow
.tv_sec
+
793 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
794 lck_rw_done(nd_if_rwlock
);
795 RT_ADDREF_LOCKED(rt
);
797 lck_mtx_unlock(rnh_lock
);
798 nd6_ns_output(ifp
, &dst
->sin6_addr
,
799 &dst
->sin6_addr
, ln
, 0);
803 lck_mtx_unlock(rnh_lock
);
806 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
815 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
817 /* Now clear the flag from all entries */
818 ln
= llinfo_nd6
.ln_next
;
819 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
820 struct rtentry
*rt
= ln
->ln_rt
;
821 struct llinfo_nd6
*next
= ln
->ln_next
;
824 if (ln
->ln_flags
& ND6_LNF_TIMER_SKIP
)
825 ln
->ln_flags
&= ~ND6_LNF_TIMER_SKIP
;
829 lck_mtx_unlock(rnh_lock
);
831 /* expire default router list */
832 lck_mtx_lock(nd6_mutex
);
833 dr
= TAILQ_FIRST(&nd_defrouter
);
835 if (dr
->expire
&& dr
->expire
< timenow
.tv_sec
) {
836 struct nd_defrouter
*t
;
837 t
= TAILQ_NEXT(dr
, dr_entry
);
841 dr
= TAILQ_NEXT(dr
, dr_entry
);
844 lck_mtx_unlock(nd6_mutex
);
847 * expire interface addresses.
848 * in the past the loop was inside prefix expiry processing.
849 * However, from a stricter speci-confrmance standpoint, we should
850 * rather separate address lifetimes and prefix lifetimes.
853 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
854 for (ia6
= in6_ifaddrs
; ia6
; ia6
= nia6
) {
856 IFA_LOCK(&ia6
->ia_ifa
);
858 * Extra reference for ourselves; it's no-op if
859 * we don't have to regenerate temporary address,
860 * otherwise it protects the address from going
861 * away since we drop in6_ifaddr_rwlock below.
863 IFA_ADDREF_LOCKED(&ia6
->ia_ifa
);
864 /* check address lifetime */
865 lt6
= &ia6
->ia6_lifetime
;
866 if (IFA6_IS_INVALID(ia6
)) {
868 * If the expiring address is temporary, try
869 * regenerating a new one. This would be useful when
870 * we suspended a laptop PC, then turned it on after a
871 * period that could invalidate all temporary
872 * addresses. Although we may have to restart the
873 * loop (see below), it must be after purging the
874 * address. Otherwise, we'd see an infinite loop of
877 if (ip6_use_tempaddr
&&
878 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
879 /* NOTE: We have to drop the lock here because
880 * regen_tmpaddr() eventually calls in6_update_ifa(),
881 * which must take the lock and would otherwise cause a
882 * hang. This is safe because the goto addrloop
883 * leads to a reevaluation of the in6_ifaddrs list
885 IFA_UNLOCK(&ia6
->ia_ifa
);
886 lck_rw_done(&in6_ifaddr_rwlock
);
887 (void) regen_tmpaddr(ia6
);
889 IFA_UNLOCK(&ia6
->ia_ifa
);
890 lck_rw_done(&in6_ifaddr_rwlock
);
894 * Purging the address would have caused
895 * in6_ifaddr_rwlock to be dropped and reacquired;
896 * therefore search again from the beginning
897 * of in6_ifaddrs list.
899 in6_purgeaddr(&ia6
->ia_ifa
);
901 /* Release extra reference taken above */
902 IFA_REMREF(&ia6
->ia_ifa
);
905 IFA_LOCK_ASSERT_HELD(&ia6
->ia_ifa
);
906 if (IFA6_IS_DEPRECATED(ia6
)) {
907 int oldflags
= ia6
->ia6_flags
;
909 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
912 * If a temporary address has just become deprecated,
913 * regenerate a new one if possible.
915 if (ip6_use_tempaddr
&&
916 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
917 (oldflags
& IN6_IFF_DEPRECATED
) == 0) {
920 IFA_UNLOCK(&ia6
->ia_ifa
);
921 lck_rw_done(&in6_ifaddr_rwlock
);
922 if (regen_tmpaddr(ia6
) == 0) {
924 * A new temporary address is
926 * XXX: this means the address chain
927 * has changed while we are still in
928 * the loop. Although the change
929 * would not cause disaster (because
930 * it's not a deletion, but an
931 * addition,) we'd rather restart the
932 * loop just for safety. Or does this
933 * significantly reduce performance??
935 /* Release extra reference */
936 IFA_REMREF(&ia6
->ia_ifa
);
939 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
941 IFA_UNLOCK(&ia6
->ia_ifa
);
945 * A new RA might have made a deprecated address
948 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
949 IFA_UNLOCK(&ia6
->ia_ifa
);
951 lck_rw_assert(&in6_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
952 /* Release extra reference taken above */
953 IFA_REMREF(&ia6
->ia_ifa
);
955 lck_rw_done(&in6_ifaddr_rwlock
);
957 lck_mtx_lock(nd6_mutex
);
959 * Since we drop the nd6_mutex in prelist_remove, we want to run this
960 * section single threaded.
962 while (nd6_drain_busy
) {
964 msleep(nd6_drain_waitchan
, nd6_mutex
, (PZERO
-1),
966 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
968 nd6_drain_busy
= TRUE
;
970 /* expire prefix list */
971 pr
= nd_prefix
.lh_first
;
974 * check prefix lifetime.
975 * since pltime is just for autoconf, pltime processing for
976 * prefix is not necessary.
979 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED
) {
984 if (pr
->ndpr_expire
&& pr
->ndpr_expire
< timenow
.tv_sec
) {
986 * address expiration and prefix expiration are
987 * separate. NEVER perform in6_purgeaddr here.
989 pr
->ndpr_stateflags
|= NDPRF_PROCESSED
;
990 NDPR_ADDREF_LOCKED(pr
);
994 pr
= nd_prefix
.lh_first
;
996 pr
->ndpr_stateflags
|= NDPRF_PROCESSED
;
1001 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
1003 pr
->ndpr_stateflags
&= ~NDPRF_PROCESSED
;
1006 nd6_drain_busy
= FALSE
;
1007 if (nd6_drain_waiters
> 0) {
1008 nd6_drain_waiters
= 0;
1009 wakeup(nd6_drain_waitchan
);
1011 lck_mtx_unlock(nd6_mutex
);
1015 * ND6 timer routine to expire default route list and prefix list
1018 nd6_timer(__unused
void *ignored_arg
)
1021 timeout(nd6_timer
, (caddr_t
)0, nd6_prune
* hz
);
1026 struct in6_ifaddr
*ia6
) /* deprecated/invalidated temporary address */
1030 struct in6_ifaddr
*public_ifa6
= NULL
;
1031 struct timeval timenow
;
1033 getmicrotime(&timenow
);
1035 ifp
= ia6
->ia_ifa
.ifa_ifp
;
1036 ifnet_lock_shared(ifp
);
1037 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1038 ifa
= ifa
->ifa_list
.tqe_next
)
1040 struct in6_ifaddr
*it6
;
1043 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1047 it6
= (struct in6_ifaddr
*)ifa
;
1049 /* ignore no autoconf addresses. */
1050 if ((it6
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
1054 /* ignore autoconf addresses with different prefixes. */
1055 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
) {
1060 * Now we are looking at an autoconf address with the same
1061 * prefix as ours. If the address is temporary and is still
1062 * preferred, do not create another one. It would be rare, but
1063 * could happen, for example, when we resume a laptop PC after
1066 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1067 !IFA6_IS_DEPRECATED(it6
)) {
1069 if (public_ifa6
!= NULL
)
1070 IFA_REMREF(&public_ifa6
->ia_ifa
);
1076 * This is a public autoconf address that has the same prefix
1077 * as ours. If it is preferred, keep it. We can't break the
1078 * loop here, because there may be a still-preferred temporary
1079 * address with the prefix.
1081 if (!IFA6_IS_DEPRECATED(it6
)) {
1082 IFA_ADDREF_LOCKED(ifa
); /* for public_ifa6 */
1084 if (public_ifa6
!= NULL
)
1085 IFA_REMREF(&public_ifa6
->ia_ifa
);
1091 ifnet_lock_done(ifp
);
1093 if (public_ifa6
!= NULL
) {
1096 if ((e
= in6_tmpifadd(public_ifa6
, 0, M_WAITOK
)) != 0) {
1097 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
1098 " tmp addr,errno=%d\n", e
);
1099 IFA_REMREF(&public_ifa6
->ia_ifa
);
1102 IFA_REMREF(&public_ifa6
->ia_ifa
);
1110 * Nuke neighbor cache/prefix/default router management table, right before
1117 struct llinfo_nd6
*ln
;
1118 struct nd_defrouter
*dr
, *ndr
;
1119 struct nd_prefix
*pr
, *npr
;
1121 /* Nuke default router list entries toward ifp */
1122 lck_mtx_lock(nd6_mutex
);
1123 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
1125 * The first entry of the list may be stored in
1126 * the routing table, so we'll delete it later.
1128 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= ndr
) {
1129 ndr
= TAILQ_NEXT(dr
, dr_entry
);
1130 if (dr
->stateflags
& NDDRF_INSTALLED
)
1135 dr
= TAILQ_FIRST(&nd_defrouter
);
1140 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
; dr
= ndr
) {
1141 ndr
= TAILQ_NEXT(dr
, dr_entry
);
1142 if (!(dr
->stateflags
& NDDRF_INSTALLED
))
1149 /* Nuke prefix list entries toward ifp */
1150 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
1151 npr
= pr
->ndpr_next
;
1153 if (pr
->ndpr_ifp
== ifp
) {
1155 * Because if_detach() does *not* release prefixes
1156 * while purging addresses the reference count will
1157 * still be above zero. We therefore reset it to
1158 * make sure that the prefix really gets purged.
1160 pr
->ndpr_addrcnt
= 0;
1163 * Previously, pr->ndpr_addr is removed as well,
1164 * but I strongly believe we don't have to do it.
1165 * nd6_purge() is only called from in6_ifdetach(),
1166 * which removes all the associated interface addresses
1168 * (jinmei@kame.net 20010129)
1170 NDPR_ADDREF_LOCKED(pr
);
1178 lck_mtx_unlock(nd6_mutex
);
1180 /* cancel default outgoing interface setting */
1181 if (nd6_defifindex
== ifp
->if_index
) {
1182 nd6_setdefaultiface(0);
1185 if (!ip6_forwarding
&& (ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
1186 lck_mtx_lock(nd6_mutex
);
1187 /* refresh default router list */
1189 defrouter_select(ifp
);
1190 lck_mtx_unlock(nd6_mutex
);
1194 * Nuke neighbor cache entries for the ifp.
1195 * Note that rt->rt_ifp may not be the same as ifp,
1196 * due to KAME goto ours hack. See RTM_RESOLVE case in
1197 * nd6_rtrequest(), and ip6_input().
1200 lck_mtx_lock(rnh_lock
);
1201 ln
= llinfo_nd6
.ln_next
;
1202 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
1204 struct llinfo_nd6
*nln
;
1209 if (rt
->rt_gateway
!= NULL
&&
1210 rt
->rt_gateway
->sa_family
== AF_LINK
&&
1211 SDL(rt
->rt_gateway
)->sdl_index
== ifp
->if_index
) {
1213 lck_mtx_unlock(rnh_lock
);
1215 * See comments on nd6_timer() for reasons why
1216 * this loop is repeated; we bite the costs of
1217 * going thru the same llinfo_nd6 more than once
1218 * here, since this purge happens during detach,
1219 * and that unlike the timer case, it's possible
1220 * there's more than one purges happening at the
1221 * same time (thus a flag wouldn't buy anything).
1224 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1231 lck_mtx_unlock(rnh_lock
);
1235 * Upon success, the returned route will be locked and the caller is
1236 * responsible for releasing the reference and doing RT_UNLOCK(rt).
1237 * This routine does not require rnh_lock to be held by the caller,
1238 * although it needs to be indicated of such a case in order to call
1239 * the correct variant of the relevant routing routines.
1243 struct in6_addr
*addr6
,
1249 struct sockaddr_in6 sin6
;
1250 unsigned int ifscope
;
1252 bzero(&sin6
, sizeof(sin6
));
1253 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1254 sin6
.sin6_family
= AF_INET6
;
1255 sin6
.sin6_addr
= *addr6
;
1257 ifscope
= (ifp
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
1259 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1260 rt
= rtalloc1_scoped_locked((struct sockaddr
*)&sin6
,
1261 create
, 0, ifscope
);
1263 rt
= rtalloc1_scoped((struct sockaddr
*)&sin6
,
1264 create
, 0, ifscope
);
1269 if ((rt
->rt_flags
& RTF_LLINFO
) == 0) {
1271 * This is the case for the default route.
1272 * If we want to create a neighbor cache for the
1273 * address, we should free the route for the
1274 * destination and allocate an interface route.
1287 if (create
&& ifp
) {
1289 u_int32_t ifa_flags
;
1293 * If no route is available and create is set,
1294 * we allocate a host route for the destination
1295 * and treat it like an interface route.
1296 * This hack is necessary for a neighbor which can't
1297 * be covered by our own prefix.
1299 ifa
= ifaof_ifpforaddr((struct sockaddr
*)&sin6
, ifp
);
1304 * Create a new route. RTF_LLINFO is necessary
1305 * to create a Neighbor Cache entry for the
1306 * destination in nd6_rtrequest which will be
1307 * called in rtrequest via ifa->ifa_rtrequest.
1310 lck_mtx_lock(rnh_lock
);
1312 ifa_flags
= ifa
->ifa_flags
;
1314 if ((e
= rtrequest_scoped_locked(RTM_ADD
,
1315 (struct sockaddr
*)&sin6
, ifa
->ifa_addr
,
1316 (struct sockaddr
*)&all1_sa
,
1317 (ifa_flags
| RTF_HOST
| RTF_LLINFO
) &
1318 ~RTF_CLONING
, &rt
, ifscope
)) != 0) {
1320 log(LOG_ERR
, "%s: failed to add route "
1321 "for a neighbor(%s), errno=%d\n",
1322 __func__
, ip6_sprintf(addr6
), e
);
1325 lck_mtx_unlock(rnh_lock
);
1331 if (rt
->rt_llinfo
) {
1332 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
1333 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1339 RT_LOCK_ASSERT_HELD(rt
);
1341 * Validation for the entry.
1342 * Note that the check for rt_llinfo is necessary because a cloned
1343 * route from a parent route that has the L flag (e.g. the default
1344 * route to a p2p interface) may have the flag, too, while the
1345 * destination is not actually a neighbor.
1346 * XXX: we can't use rt->rt_ifp to check for the interface, since
1347 * it might be the loopback interface if the entry is for our
1348 * own address on a non-loopback interface. Instead, we should
1349 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
1351 * Note also that ifa_ifp and ifp may differ when we connect two
1352 * interfaces to a same link, install a link prefix to an interface,
1353 * and try to install a neighbor cache on an interface that does not
1354 * have a route to the prefix.
1357 (rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1358 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
1359 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
)) {
1360 RT_REMREF_LOCKED(rt
);
1363 log(LOG_DEBUG
, "%s: failed to lookup %s "
1364 "(if = %s)\n", __func__
, ip6_sprintf(addr6
),
1365 ifp
? if_name(ifp
) : "unspec");
1366 /* xxx more logs... kazu */
1371 * Caller needs to release reference and call RT_UNLOCK(rt).
1377 * Test whether a given IPv6 address is a neighbor or not, ignoring
1378 * the actual neighbor cache. The neighbor cache is ignored in order
1379 * to not reenter the routing code from within itself.
1382 nd6_is_new_addr_neighbor(
1383 struct sockaddr_in6
*addr
,
1386 struct nd_prefix
*pr
;
1387 struct ifaddr
*dstaddr
;
1389 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1392 * A link-local address is always a neighbor.
1393 * XXX: a link does not necessarily specify a single interface.
1395 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
)) {
1396 struct sockaddr_in6 sin6_copy
;
1400 * We need sin6_copy since sa6_recoverscope() may modify the
1404 if (sa6_recoverscope(&sin6_copy
))
1405 return (0); /* XXX: should be impossible */
1406 if (in6_setscope(&sin6_copy
.sin6_addr
, ifp
, &zone
))
1408 if (sin6_copy
.sin6_scope_id
== zone
)
1415 * If the address matches one of our addresses,
1416 * it should be a neighbor.
1417 * If the address matches one of our on-link prefixes, it should be a
1420 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
1422 if (pr
->ndpr_ifp
!= ifp
) {
1426 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
1430 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr
->ndpr_prefix
.sin6_addr
,
1431 &addr
->sin6_addr
, &pr
->ndpr_mask
)) {
1439 * If the address is assigned on the node of the other side of
1440 * a p2p interface, the address should be a neighbor.
1442 dstaddr
= ifa_ifwithdstaddr((struct sockaddr
*)addr
);
1443 if (dstaddr
!= NULL
) {
1444 if (dstaddr
->ifa_ifp
== ifp
) {
1445 IFA_REMREF(dstaddr
);
1448 IFA_REMREF(dstaddr
);
1453 * If the default router list is empty, all addresses are regarded
1454 * as on-link, and thus, as a neighbor.
1455 * XXX: we restrict the condition to hosts, because routers usually do
1456 * not have the "default router list".
1458 if (!ip6_forwarding
&& TAILQ_FIRST(&nd_defrouter
) == NULL
&&
1459 nd6_defifindex
== ifp
->if_index
) {
1468 * Detect if a given IPv6 address identifies a neighbor on a given link.
1469 * XXX: should take care of the destination of a p2p link?
1472 nd6_is_addr_neighbor(struct sockaddr_in6
*addr
, struct ifnet
*ifp
, int rt_locked
)
1476 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1477 lck_mtx_lock(nd6_mutex
);
1478 if (nd6_is_new_addr_neighbor(addr
, ifp
)) {
1479 lck_mtx_unlock(nd6_mutex
);
1482 lck_mtx_unlock(nd6_mutex
);
1485 * Even if the address matches none of our addresses, it might be
1486 * in the neighbor cache.
1488 if ((rt
= nd6_lookup(&addr
->sin6_addr
, 0, ifp
, rt_locked
)) != NULL
) {
1489 RT_LOCK_ASSERT_HELD(rt
);
1490 RT_REMREF_LOCKED(rt
);
1499 * Free an nd6 llinfo entry.
1500 * Since the function would cause significant changes in the kernel, DO NOT
1501 * make it global, unless you have a strong reason for the change, and are sure
1502 * that the change is safe.
1508 struct llinfo_nd6
*ln
;
1509 struct in6_addr in6
;
1510 struct nd_defrouter
*dr
;
1512 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1513 RT_LOCK_ASSERT_NOTHELD(rt
);
1514 lck_mtx_lock(nd6_mutex
);
1517 RT_ADDREF_LOCKED(rt
); /* Extra ref */
1519 in6
= ((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
1522 * Prevent another thread from modifying rt_key, rt_gateway
1523 * via rt_setgate() after the rt_lock is dropped by marking
1524 * the route as defunct.
1526 rt
->rt_flags
|= RTF_CONDEMNED
;
1529 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1530 * even though it is not harmful, it was not really necessary.
1533 if (!ip6_forwarding
&& (ip6_accept_rtadv
||
1534 (rt
->rt_ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
1535 dr
= defrouter_lookup(&((struct sockaddr_in6
*)rt_key(rt
))->
1536 sin6_addr
, rt
->rt_ifp
);
1538 if ((ln
&& ln
->ln_router
) || dr
) {
1540 * rt6_flush must be called whether or not the neighbor
1541 * is in the Default Router List.
1542 * See a corresponding comment in nd6_na_input().
1545 lck_mtx_unlock(nd6_mutex
);
1546 rt6_flush(&in6
, rt
->rt_ifp
);
1547 lck_mtx_lock(nd6_mutex
);
1555 * Unreachablity of a router might affect the default
1556 * router selection and on-link detection of advertised
1561 * Temporarily fake the state to choose a new default
1562 * router and to perform on-link determination of
1563 * prefixes correctly.
1564 * Below the state will be set correctly,
1565 * or the entry itself will be deleted.
1568 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1571 * Since defrouter_select() does not affect the
1572 * on-link determination and MIP6 needs the check
1573 * before the default router selection, we perform
1577 pfxlist_onlink_check();
1580 * refresh default router list
1582 defrouter_select(rt
->rt_ifp
);
1584 RT_LOCK_ASSERT_NOTHELD(rt
);
1589 lck_mtx_unlock(nd6_mutex
);
1591 * Detach the route from the routing tree and the list of neighbor
1592 * caches, and disable the route entry not to be used in already
1595 (void) rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
1596 rt_mask(rt
), 0, (struct rtentry
**)0);
1598 /* Extra ref held above; now free it */
1603 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1605 * XXX cost-effective methods?
1610 struct in6_addr
*dst6
,
1613 struct llinfo_nd6
*ln
;
1614 struct timeval timenow
;
1616 getmicrotime(&timenow
);
1619 * If the caller specified "rt", use that. Otherwise, resolve the
1620 * routing table by supplied "dst6".
1625 /* Callee returns a locked route upon success */
1626 if ((rt
= nd6_lookup(dst6
, 0, NULL
, 0)) == NULL
)
1628 RT_LOCK_ASSERT_HELD(rt
);
1631 RT_ADDREF_LOCKED(rt
);
1634 if ((rt
->rt_flags
& RTF_GATEWAY
) != 0 ||
1635 (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1636 !rt
->rt_llinfo
|| !rt
->rt_gateway
||
1637 rt
->rt_gateway
->sa_family
!= AF_LINK
) {
1638 /* This is not a host route. */
1643 if (ln
->ln_state
< ND6_LLINFO_REACHABLE
)
1647 * if we get upper-layer reachability confirmation many times,
1648 * it is possible we have false information.
1652 if (ln
->ln_byhint
> nd6_maxnudhint
)
1656 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1657 if (ln
->ln_expire
) {
1658 lck_rw_lock_shared(nd_if_rwlock
);
1659 ln
->ln_expire
= timenow
.tv_sec
+
1660 nd_ifinfo
[rt
->rt_ifp
->if_index
].reachable
;
1661 lck_rw_done(nd_if_rwlock
);
1664 RT_REMREF_LOCKED(rt
);
1672 __unused
struct sockaddr
*sa
)
1674 struct sockaddr
*gate
= rt
->rt_gateway
;
1675 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
1676 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
, 0, 0, 0, 0, 0,
1677 {0,0,0,0,0,0,0,0,0,0,0,0,} };
1678 struct ifnet
*ifp
= rt
->rt_ifp
;
1680 struct timeval timenow
;
1682 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1683 RT_LOCK_ASSERT_HELD(rt
);
1685 if ((rt
->rt_flags
& RTF_GATEWAY
))
1688 if (nd6_need_cache(ifp
) == 0 && (rt
->rt_flags
& RTF_HOST
) == 0) {
1690 * This is probably an interface direct route for a link
1691 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1692 * We do not need special treatment below for such a route.
1693 * Moreover, the RTF_LLINFO flag which would be set below
1694 * would annoy the ndp(8) command.
1699 if (req
== RTM_RESOLVE
) {
1702 if (!nd6_need_cache(ifp
)) { /* stf case */
1705 struct sockaddr_in6 sin6
;
1707 rtkey_to_sa6(rt
, &sin6
);
1709 * nd6_is_addr_neighbor() may call nd6_lookup(),
1710 * therefore we drop rt_lock to avoid deadlock
1711 * during the lookup.
1713 RT_ADDREF_LOCKED(rt
);
1715 no_nd_cache
= !nd6_is_addr_neighbor(&sin6
, ifp
, 1);
1717 RT_REMREF_LOCKED(rt
);
1721 * FreeBSD and BSD/OS often make a cloned host route based
1722 * on a less-specific route (e.g. the default route).
1723 * If the less specific route does not have a "gateway"
1724 * (this is the case when the route just goes to a p2p or an
1725 * stf interface), we'll mistakenly make a neighbor cache for
1726 * the host route, and will see strange neighbor solicitation
1727 * for the corresponding destination. In order to avoid the
1728 * confusion, we check if the destination of the route is
1729 * a neighbor in terms of neighbor discovery, and stop the
1730 * process if not. Additionally, we remove the LLINFO flag
1731 * so that ndp(8) will not try to get the neighbor information
1732 * of the destination.
1735 rt
->rt_flags
&= ~RTF_LLINFO
;
1740 getmicrotime(&timenow
);
1744 * There is no backward compatibility :)
1746 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1747 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1748 * rt->rt_flags |= RTF_CLONING;
1750 if ((rt
->rt_flags
& RTF_CLONING
) ||
1751 ((rt
->rt_flags
& RTF_LLINFO
) && ln
== NULL
)) {
1753 * Case 1: This route should come from a route to
1754 * interface (RTF_CLONING case) or the route should be
1755 * treated as on-link but is currently not
1756 * (RTF_LLINFO && ln == NULL case).
1758 if (rt_setgate(rt
, rt_key(rt
),
1759 (struct sockaddr
*)&null_sdl
) == 0) {
1760 gate
= rt
->rt_gateway
;
1761 SDL(gate
)->sdl_type
= ifp
->if_type
;
1762 SDL(gate
)->sdl_index
= ifp
->if_index
;
1764 * In case we're called before 1.0 sec.
1768 ln
->ln_expire
= MAX(timenow
.tv_sec
, 1);
1770 if ((rt
->rt_flags
& RTF_CLONING
))
1774 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1775 * We don't do that here since llinfo is not ready yet.
1777 * There are also couple of other things to be discussed:
1778 * - unsolicited NA code needs improvement beforehand
1779 * - RFC2461 says we MAY send multicast unsolicited NA
1780 * (7.2.6 paragraph 4), however, it also says that we
1781 * SHOULD provide a mechanism to prevent multicast NA storm.
1782 * we don't have anything like it right now.
1783 * note that the mechanism needs a mutual agreement
1784 * between proxies, which means that we need to implement
1785 * a new protocol, or a new kludge.
1786 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1787 * we need to check ip6forwarding before sending it.
1788 * (or should we allow proxy ND configuration only for
1789 * routers? there's no mention about proxy ND from hosts)
1793 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) == 0) {
1795 * Address resolution isn't necessary for a point to
1796 * point link, so we can skip this test for a p2p link.
1798 if (gate
->sa_family
!= AF_LINK
||
1799 gate
->sa_len
< sizeof(null_sdl
)) {
1800 /* Don't complain in case of RTM_ADD */
1801 if (req
== RTM_RESOLVE
) {
1803 "nd6_rtrequest: bad gateway "
1804 "value: %s\n", if_name(ifp
));
1808 SDL(gate
)->sdl_type
= ifp
->if_type
;
1809 SDL(gate
)->sdl_index
= ifp
->if_index
;
1812 break; /* This happens on a route change */
1814 * Case 2: This route may come from cloning, or a manual route
1815 * add with a LL address.
1817 rt
->rt_llinfo
= ln
= nd6_llinfo_alloc();
1819 log(LOG_DEBUG
, "nd6_rtrequest: malloc failed\n");
1822 rt
->rt_llinfo_get_ri
= nd6_llinfo_get_ri
;
1823 rt
->rt_llinfo_purge
= nd6_llinfo_purge
;
1824 rt
->rt_llinfo_free
= nd6_llinfo_free
;
1828 Bzero(ln
, sizeof(*ln
));
1830 /* this is required for "ndp" command. - shin */
1831 if (req
== RTM_ADD
) {
1833 * gate should have some valid AF_LINK entry,
1834 * and ln->ln_expire should have some lifetime
1835 * which is specified by ndp command.
1837 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1841 * When req == RTM_RESOLVE, rt is created and
1842 * initialized in rtrequest(), so rt_expire is 0.
1844 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1845 /* In case we're called before 1.0 sec. has elapsed */
1846 ln
->ln_expire
= MAX(timenow
.tv_sec
, 1);
1848 rt
->rt_flags
|= RTF_LLINFO
;
1852 * If we have too many cache entries, initiate immediate
1853 * purging for some "less recently used" entries. Note that
1854 * we cannot directly call nd6_free() here because it would
1855 * cause re-entering rtable related routines triggering an LOR
1858 if (ip6_neighborgcthresh
>= 0 &&
1859 nd6_inuse
>= ip6_neighborgcthresh
) {
1862 for (i
= 0; i
< 10 && llinfo_nd6
.ln_prev
!= ln
; i
++) {
1863 struct llinfo_nd6
*ln_end
= llinfo_nd6
.ln_prev
;
1864 struct rtentry
*rt_end
= ln_end
->ln_rt
;
1866 /* Move this entry to the head */
1869 LN_INSERTHEAD(ln_end
);
1871 if (ln_end
->ln_expire
== 0) {
1875 if (ln_end
->ln_state
> ND6_LLINFO_INCOMPLETE
)
1876 ln_end
->ln_state
= ND6_LLINFO_STALE
;
1878 ln_end
->ln_state
= ND6_LLINFO_PURGE
;
1879 ln_end
->ln_expire
= timenow
.tv_sec
;
1885 * check if rt_key(rt) is one of my address assigned
1888 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1889 &SIN6(rt_key(rt
))->sin6_addr
);
1891 caddr_t macp
= nd6_ifptomac(ifp
);
1893 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1896 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
1897 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
1899 if (nd6_useloopback
) {
1900 if (rt
->rt_ifp
!= lo_ifp
) {
1902 * Purge any link-layer info caching.
1904 if (rt
->rt_llinfo_purge
!= NULL
)
1905 rt
->rt_llinfo_purge(rt
);
1908 * Adjust route ref count for the
1911 if (rt
->rt_if_ref_fn
!= NULL
) {
1912 rt
->rt_if_ref_fn(lo_ifp
, 1);
1913 rt
->rt_if_ref_fn(rt
->rt_ifp
, -1);
1916 rt
->rt_ifp
= lo_ifp
; /* XXX */
1918 * Make sure rt_ifa be equal to the ifaddr
1919 * corresponding to the address.
1920 * We need this because when we refer
1921 * rt_ifa->ia6_flags in ip6_input, we assume
1922 * that the rt_ifa points to the address instead
1923 * of the loopback address.
1925 if (ifa
!= rt
->rt_ifa
) {
1930 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
1932 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1935 /* join solicited node multicast for proxy ND */
1936 if (ifp
->if_flags
& IFF_MULTICAST
) {
1937 struct in6_addr llsol
;
1938 struct in6_multi
*in6m
;
1941 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1942 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
1943 llsol
.s6_addr32
[1] = 0;
1944 llsol
.s6_addr32
[2] = htonl(1);
1945 llsol
.s6_addr8
[12] = 0xff;
1946 if (in6_setscope(&llsol
, ifp
, NULL
))
1948 error
= in6_mc_join(ifp
, &llsol
, NULL
, &in6m
, 0);
1950 nd6log((LOG_ERR
, "%s: failed to join "
1951 "%s (errno=%d)\n", if_name(ifp
),
1952 ip6_sprintf(&llsol
), error
));
1963 /* leave from solicited node multicast for proxy ND */
1964 if ((rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
1965 (ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1966 struct in6_addr llsol
;
1967 struct in6_multi
*in6m
;
1969 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1970 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
1971 llsol
.s6_addr32
[1] = 0;
1972 llsol
.s6_addr32
[2] = htonl(1);
1973 llsol
.s6_addr8
[12] = 0xff;
1974 if (in6_setscope(&llsol
, ifp
, NULL
) == 0) {
1975 in6_multihead_lock_shared();
1976 IN6_LOOKUP_MULTI(&llsol
, ifp
, in6m
);
1977 in6_multihead_lock_done();
1979 in6_mc_leave(in6m
, NULL
);
1986 * Unchain it but defer the actual freeing until the route
1987 * itself is to be freed. rt->rt_llinfo still points to
1988 * llinfo_nd6, and likewise, ln->ln_rt stil points to this
1989 * route entry, except that RTF_LLINFO is now cleared.
1991 if (ln
->ln_flags
& ND6_LNF_IN_USE
)
1995 * Purge any link-layer info caching.
1997 if (rt
->rt_llinfo_purge
!= NULL
)
1998 rt
->rt_llinfo_purge(rt
);
2000 rt
->rt_flags
&= ~RTF_LLINFO
;
2001 if (ln
->ln_hold
!= NULL
) {
2002 m_freem(ln
->ln_hold
);
2009 nd6_siocgdrlst(void *data
, int data_is_64
)
2011 struct in6_drlist_64
*drl_64
= (struct in6_drlist_64
*)data
;
2012 struct in6_drlist_32
*drl_32
= (struct in6_drlist_32
*)data
;
2013 struct nd_defrouter
*dr
;
2016 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2018 bzero(data
, data_is_64
? sizeof (*drl_64
) : sizeof (*drl_32
));
2019 dr
= TAILQ_FIRST(&nd_defrouter
);
2021 /* For 64-bit process */
2022 while (dr
&& i
< DRLSTSIZ
) {
2023 drl_64
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
2024 if (IN6_IS_ADDR_LINKLOCAL(&drl_64
->defrouter
[i
].rtaddr
)) {
2025 /* XXX: need to this hack for KAME stack */
2026 drl_64
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
2029 "default router list contains a "
2030 "non-linklocal address(%s)\n",
2031 ip6_sprintf(&drl_64
->defrouter
[i
].rtaddr
));
2033 drl_64
->defrouter
[i
].flags
= dr
->flags
;
2034 drl_64
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
2035 drl_64
->defrouter
[i
].expire
= dr
->expire
;
2036 drl_64
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
2038 dr
= TAILQ_NEXT(dr
, dr_entry
);
2042 /* For 32-bit process */
2043 while (dr
&& i
< DRLSTSIZ
) {
2044 drl_32
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
2045 if (IN6_IS_ADDR_LINKLOCAL(&drl_32
->defrouter
[i
].rtaddr
)) {
2046 /* XXX: need to this hack for KAME stack */
2047 drl_32
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
2050 "default router list contains a "
2051 "non-linklocal address(%s)\n",
2052 ip6_sprintf(&drl_32
->defrouter
[i
].rtaddr
));
2054 drl_32
->defrouter
[i
].flags
= dr
->flags
;
2055 drl_32
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
2056 drl_32
->defrouter
[i
].expire
= dr
->expire
;
2057 drl_32
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
2059 dr
= TAILQ_NEXT(dr
, dr_entry
);
2064 nd6_siocgprlst(void *data
, int data_is_64
)
2066 struct in6_prlist_64
*prl_64
= (struct in6_prlist_64
*)data
;
2067 struct in6_prlist_32
*prl_32
= (struct in6_prlist_32
*)data
;
2068 struct nd_prefix
*pr
;
2071 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2073 * XXX meaning of fields, especialy "raflags", is very
2074 * differnet between RA prefix list and RR/static prefix list.
2075 * how about separating ioctls into two?
2077 bzero(data
, data_is_64
? sizeof (*prl_64
) : sizeof (*prl_32
));
2078 pr
= nd_prefix
.lh_first
;
2080 /* For 64-bit process */
2081 while (pr
&& i
< PRLSTSIZ
) {
2082 struct nd_pfxrouter
*pfr
;
2086 (void) in6_embedscope(&prl_64
->prefix
[i
].prefix
,
2087 &pr
->ndpr_prefix
, NULL
, NULL
, NULL
);
2088 prl_64
->prefix
[i
].raflags
= pr
->ndpr_raf
;
2089 prl_64
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
2090 prl_64
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
2091 prl_64
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
2092 prl_64
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
2093 prl_64
->prefix
[i
].expire
= pr
->ndpr_expire
;
2095 pfr
= pr
->ndpr_advrtrs
.lh_first
;
2099 #define RTRADDR prl_64->prefix[i].advrtr[j]
2100 RTRADDR
= pfr
->router
->rtaddr
;
2101 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
2102 /* XXX: hack for KAME */
2103 RTRADDR
.s6_addr16
[1] = 0;
2106 "a router(%s) advertises "
2108 "non-link local address\n",
2109 ip6_sprintf(&RTRADDR
));
2114 pfr
= pfr
->pfr_next
;
2116 prl_64
->prefix
[i
].advrtrs
= j
;
2117 prl_64
->prefix
[i
].origin
= PR_ORIG_RA
;
2126 /* For 32-bit process */
2127 while (pr
&& i
< PRLSTSIZ
) {
2128 struct nd_pfxrouter
*pfr
;
2132 (void) in6_embedscope(&prl_32
->prefix
[i
].prefix
,
2133 &pr
->ndpr_prefix
, NULL
, NULL
, NULL
);
2134 prl_32
->prefix
[i
].raflags
= pr
->ndpr_raf
;
2135 prl_32
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
2136 prl_32
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
2137 prl_32
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
2138 prl_32
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
2139 prl_32
->prefix
[i
].expire
= pr
->ndpr_expire
;
2141 pfr
= pr
->ndpr_advrtrs
.lh_first
;
2145 #define RTRADDR prl_32->prefix[i].advrtr[j]
2146 RTRADDR
= pfr
->router
->rtaddr
;
2147 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
2148 /* XXX: hack for KAME */
2149 RTRADDR
.s6_addr16
[1] = 0;
2152 "a router(%s) advertises "
2154 "non-link local address\n",
2155 ip6_sprintf(&RTRADDR
));
2160 pfr
= pfr
->pfr_next
;
2162 prl_32
->prefix
[i
].advrtrs
= j
;
2163 prl_32
->prefix
[i
].origin
= PR_ORIG_RA
;
2172 nd6_ioctl(u_long cmd
, caddr_t data
, struct ifnet
*ifp
)
2174 struct in6_ndireq
*ndi
= (struct in6_ndireq
*)data
;
2175 struct in6_ondireq
*ondi
= (struct in6_ondireq
*)data
;
2176 struct nd_defrouter
*dr
;
2177 struct nd_prefix
*pr
;
2179 int i
= ifp
->if_index
, error
= 0;
2182 case SIOCGDRLST_IN6_32
:
2183 case SIOCGDRLST_IN6_64
:
2185 * obsolete API, use sysctl under net.inet6.icmp6
2187 lck_mtx_lock(nd6_mutex
);
2188 nd6_siocgdrlst(data
, cmd
== SIOCGDRLST_IN6_64
);
2189 lck_mtx_unlock(nd6_mutex
);
2192 case SIOCGPRLST_IN6_32
:
2193 case SIOCGPRLST_IN6_64
:
2195 * obsolete API, use sysctl under net.inet6.icmp6
2197 lck_mtx_lock(nd6_mutex
);
2198 nd6_siocgprlst(data
, cmd
== SIOCGPRLST_IN6_64
);
2199 lck_mtx_unlock(nd6_mutex
);
2202 case OSIOCGIFINFO_IN6
:
2203 case SIOCGIFINFO_IN6
:
2205 * SIOCGIFINFO_IN6 ioctl is encoded with in6_ondireq
2206 * instead of in6_ndireq, so we treat it as such.
2208 lck_rw_lock_shared(nd_if_rwlock
);
2209 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
2210 lck_rw_done(nd_if_rwlock
);
2214 ondi
->ndi
.linkmtu
= IN6_LINKMTU(ifp
);
2215 ondi
->ndi
.maxmtu
= nd_ifinfo
[i
].maxmtu
;
2216 ondi
->ndi
.basereachable
= nd_ifinfo
[i
].basereachable
;
2217 ondi
->ndi
.reachable
= nd_ifinfo
[i
].reachable
;
2218 ondi
->ndi
.retrans
= nd_ifinfo
[i
].retrans
;
2219 ondi
->ndi
.flags
= nd_ifinfo
[i
].flags
;
2220 ondi
->ndi
.recalctm
= nd_ifinfo
[i
].recalctm
;
2221 ondi
->ndi
.chlim
= nd_ifinfo
[i
].chlim
;
2222 lck_rw_done(nd_if_rwlock
);
2225 case SIOCSIFINFO_FLAGS
:
2226 /* XXX: almost all other fields of ndi->ndi is unused */
2227 lck_rw_lock_shared(nd_if_rwlock
);
2228 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
2229 lck_rw_done(nd_if_rwlock
);
2233 nd_ifinfo
[i
].flags
= ndi
->ndi
.flags
;
2234 lck_rw_done(nd_if_rwlock
);
2237 case SIOCSNDFLUSH_IN6
: /* XXX: the ioctl name is confusing... */
2238 /* flush default router list */
2240 * xxx sumikawa: should not delete route if default
2241 * route equals to the top of default router list
2243 lck_mtx_lock(nd6_mutex
);
2245 defrouter_select(ifp
);
2246 lck_mtx_unlock(nd6_mutex
);
2247 /* xxx sumikawa: flush prefix list */
2250 case SIOCSPFXFLUSH_IN6
: {
2251 /* flush all the prefix advertised by routers */
2252 struct nd_prefix
*next
;
2254 lck_mtx_lock(nd6_mutex
);
2255 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
2256 struct in6_ifaddr
*ia
;
2258 next
= pr
->ndpr_next
;
2261 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
2265 if (ifp
!= lo_ifp
&& pr
->ndpr_ifp
!= ifp
) {
2269 /* do we really have to remove addresses as well? */
2270 NDPR_ADDREF_LOCKED(pr
);
2272 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2274 while (ia
!= NULL
) {
2275 IFA_LOCK(&ia
->ia_ifa
);
2276 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
2277 IFA_UNLOCK(&ia
->ia_ifa
);
2282 if (ia
->ia6_ndpr
== pr
) {
2283 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
2284 IFA_UNLOCK(&ia
->ia_ifa
);
2285 lck_rw_done(&in6_ifaddr_rwlock
);
2286 lck_mtx_unlock(nd6_mutex
);
2287 in6_purgeaddr(&ia
->ia_ifa
);
2288 lck_mtx_lock(nd6_mutex
);
2289 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2290 IFA_REMREF(&ia
->ia_ifa
);
2292 * Purging the address caused
2293 * in6_ifaddr_rwlock to be
2295 * reacquired; therefore search again
2296 * from the beginning of in6_ifaddrs.
2297 * The same applies for the prefix list.
2300 next
= nd_prefix
.lh_first
;
2304 IFA_UNLOCK(&ia
->ia_ifa
);
2307 lck_rw_done(&in6_ifaddr_rwlock
);
2312 * If we were trying to restart this loop
2313 * above by changing the value of 'next', we might
2314 * end up freeing the only element on the list
2315 * when we call NDPR_REMREF().
2316 * When this happens, we also have get out of this
2317 * loop because we have nothing else to do.
2323 lck_mtx_unlock(nd6_mutex
);
2327 case SIOCSRTRFLUSH_IN6
: {
2328 /* flush all the default routers */
2329 struct nd_defrouter
*next
;
2331 lck_mtx_lock(nd6_mutex
);
2332 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
2334 * The first entry of the list may be stored in
2335 * the routing table, so we'll delete it later.
2337 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
2338 next
= TAILQ_NEXT(dr
, dr_entry
);
2339 if (ifp
== lo_ifp
|| dr
->ifp
== ifp
)
2342 if (ifp
== lo_ifp
||
2343 TAILQ_FIRST(&nd_defrouter
)->ifp
== ifp
)
2344 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
));
2346 lck_mtx_unlock(nd6_mutex
);
2350 case SIOCGNBRINFO_IN6_32
: {
2351 struct llinfo_nd6
*ln
;
2352 struct in6_nbrinfo_32
*nbi_32
= (struct in6_nbrinfo_32
*)data
;
2353 /* make local for safety */
2354 struct in6_addr nb_addr
= nbi_32
->addr
;
2357 * XXX: KAME specific hack for scoped addresses
2358 * XXXX: for other scopes than link-local?
2360 if (IN6_IS_ADDR_LINKLOCAL(&nbi_32
->addr
) ||
2361 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_32
->addr
)) {
2362 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
2365 *idp
= htons(ifp
->if_index
);
2368 /* Callee returns a locked route upon success */
2369 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
2373 RT_LOCK_ASSERT_HELD(rt
);
2375 nbi_32
->state
= ln
->ln_state
;
2376 nbi_32
->asked
= ln
->ln_asked
;
2377 nbi_32
->isrouter
= ln
->ln_router
;
2378 nbi_32
->expire
= ln
->ln_expire
;
2379 RT_REMREF_LOCKED(rt
);
2384 case SIOCGNBRINFO_IN6_64
: {
2385 struct llinfo_nd6
*ln
;
2386 struct in6_nbrinfo_64
*nbi_64
= (struct in6_nbrinfo_64
*)data
;
2387 /* make local for safety */
2388 struct in6_addr nb_addr
= nbi_64
->addr
;
2391 * XXX: KAME specific hack for scoped addresses
2392 * XXXX: for other scopes than link-local?
2394 if (IN6_IS_ADDR_LINKLOCAL(&nbi_64
->addr
) ||
2395 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_64
->addr
)) {
2396 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
2399 *idp
= htons(ifp
->if_index
);
2402 /* Callee returns a locked route upon success */
2403 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
2407 RT_LOCK_ASSERT_HELD(rt
);
2409 nbi_64
->state
= ln
->ln_state
;
2410 nbi_64
->asked
= ln
->ln_asked
;
2411 nbi_64
->isrouter
= ln
->ln_router
;
2412 nbi_64
->expire
= ln
->ln_expire
;
2413 RT_REMREF_LOCKED(rt
);
2418 case SIOCGDEFIFACE_IN6_32
: /* XXX: should be implemented as a sysctl? */
2419 case SIOCGDEFIFACE_IN6_64
: {
2420 struct in6_ndifreq_64
*ndif_64
= (struct in6_ndifreq_64
*)data
;
2421 struct in6_ndifreq_32
*ndif_32
= (struct in6_ndifreq_32
*)data
;
2423 if (cmd
== SIOCGDEFIFACE_IN6_64
)
2424 ndif_64
->ifindex
= nd6_defifindex
;
2426 ndif_32
->ifindex
= nd6_defifindex
;
2430 case SIOCSDEFIFACE_IN6_32
: /* XXX: should be implemented as a sysctl? */
2431 case SIOCSDEFIFACE_IN6_64
: {
2432 struct in6_ndifreq_64
*ndif_64
= (struct in6_ndifreq_64
*)data
;
2433 struct in6_ndifreq_32
*ndif_32
= (struct in6_ndifreq_32
*)data
;
2435 error
= nd6_setdefaultiface(cmd
== SIOCSDEFIFACE_IN6_64
?
2436 ndif_64
->ifindex
: ndif_32
->ifindex
);
2445 * Create neighbor cache entry and cache link-layer address,
2446 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
2451 struct in6_addr
*from
,
2453 __unused
int lladdrlen
,
2454 int type
, /* ICMP6 type */
2455 int code
) /* type dependent information */
2457 struct rtentry
*rt
= NULL
;
2458 struct llinfo_nd6
*ln
= NULL
;
2460 struct sockaddr_dl
*sdl
= NULL
;
2465 struct timeval timenow
;
2468 panic("ifp == NULL in nd6_cache_lladdr");
2470 panic("from == NULL in nd6_cache_lladdr");
2472 /* nothing must be updated for unspecified address */
2473 if (IN6_IS_ADDR_UNSPECIFIED(from
))
2477 * Validation about ifp->if_addrlen and lladdrlen must be done in
2480 * XXX If the link does not have link-layer adderss, what should
2481 * we do? (ifp->if_addrlen == 0)
2482 * Spec says nothing in sections for RA, RS and NA. There's small
2483 * description on it in NS section (RFC 2461 7.2.3).
2485 getmicrotime(&timenow
);
2487 rt
= nd6_lookup(from
, 0, ifp
, 0);
2489 if ((rt
= nd6_lookup(from
, 1, ifp
, 0)) == NULL
)
2491 RT_LOCK_ASSERT_HELD(rt
);
2494 RT_LOCK_ASSERT_HELD(rt
);
2495 /* do nothing if static ndp is set */
2496 if (rt
->rt_flags
& RTF_STATIC
) {
2497 RT_REMREF_LOCKED(rt
);
2506 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
2513 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
2516 if (rt
->rt_gateway
== NULL
)
2518 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
2520 sdl
= SDL(rt
->rt_gateway
);
2522 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
2523 if (olladdr
&& lladdr
) {
2524 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
2532 * newentry olladdr lladdr llchange (*=record)
2535 * 0 n y -- (3) * STALE
2537 * 0 y y y (5) * STALE
2538 * 1 -- n -- (6) NOSTATE(= PASSIVE)
2539 * 1 -- y -- (7) * STALE
2542 if (lladdr
) { /* (3-5) and (7) */
2544 * Record source link-layer address
2545 * XXX is it dependent to ifp->if_type?
2547 sdl
->sdl_alen
= ifp
->if_addrlen
;
2548 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
2550 /* cache the gateway (sender HW) address */
2551 nd6_llreach_alloc(rt
, ifp
, LLADDR(sdl
), sdl
->sdl_alen
, FALSE
);
2555 if ((!olladdr
&& lladdr
!= NULL
) || /* (3) */
2556 (olladdr
&& lladdr
!= NULL
&& llchange
)) { /* (5) */
2558 newstate
= ND6_LLINFO_STALE
;
2559 } else /* (1-2,4) */
2563 if (lladdr
== NULL
) /* (6) */
2564 newstate
= ND6_LLINFO_NOSTATE
;
2566 newstate
= ND6_LLINFO_STALE
;
2571 * Update the state of the neighbor cache.
2573 ln
->ln_state
= newstate
;
2575 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
2576 struct mbuf
*m
= ln
->ln_hold
;
2578 * XXX: since nd6_output() below will cause
2579 * state tansition to DELAY and reset the timer,
2580 * we must set the timer now, although it is actually
2583 ln
->ln_expire
= timenow
.tv_sec
+ nd6_gctimer
;
2587 struct sockaddr_in6 sin6
;
2589 rtkey_to_sa6(rt
, &sin6
);
2591 * we assume ifp is not a p2p here, so just
2592 * set the 2nd argument as the 1st one.
2595 nd6_output(ifp
, ifp
, m
, &sin6
, rt
);
2598 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
2599 /* probe right away */
2600 ln
->ln_expire
= timenow
.tv_sec
;
2605 * ICMP6 type dependent behavior.
2607 * NS: clear IsRouter if new entry
2608 * RS: clear IsRouter
2609 * RA: set IsRouter if there's lladdr
2610 * redir: clear IsRouter if new entry
2613 * The spec says that we must set IsRouter in the following cases:
2614 * - If lladdr exist, set IsRouter. This means (1-5).
2615 * - If it is old entry (!newentry), set IsRouter. This means (7).
2616 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
2617 * A quetion arises for (1) case. (1) case has no lladdr in the
2618 * neighbor cache, this is similar to (6).
2619 * This case is rare but we figured that we MUST NOT set IsRouter.
2621 * newentry olladdr lladdr llchange NS RS RA redir
2623 * 0 n n -- (1) c ? s
2624 * 0 y n -- (2) c s s
2625 * 0 n y -- (3) c s s
2628 * 1 -- n -- (6) c c c s
2629 * 1 -- y -- (7) c c s c s
2633 switch (type
& 0xff) {
2634 case ND_NEIGHBOR_SOLICIT
:
2636 * New entry must have is_router flag cleared.
2638 if (is_newentry
) /* (6-7) */
2643 * If the icmp is a redirect to a better router, always set the
2644 * is_router flag. Otherwise, if the entry is newly created,
2645 * clear the flag. [RFC 2461, sec 8.3]
2647 if (code
== ND_REDIRECT_ROUTER
)
2649 else if (is_newentry
) /* (6-7) */
2652 case ND_ROUTER_SOLICIT
:
2654 * is_router flag must always be cleared.
2658 case ND_ROUTER_ADVERT
:
2660 * Mark an entry with lladdr as a router.
2662 if ((!is_newentry
&& (olladdr
|| lladdr
)) || /* (2-5) */
2663 (is_newentry
&& lladdr
)) { /* (7) */
2670 * When the link-layer address of a router changes, select the
2671 * best router again. In particular, when the neighbor entry is newly
2672 * created, it might affect the selection policy.
2673 * Question: can we restrict the first condition to the "is_newentry"
2675 * XXX: when we hear an RA from a new router with the link-layer
2676 * address option, defrouter_select() is called twice, since
2677 * defrtrlist_update called the function as well. However, I believe
2678 * we can compromise the overhead, since it only happens the first
2680 * XXX: although defrouter_select() should not have a bad effect
2681 * for those are not autoconfigured hosts, we explicitly avoid such
2684 if (do_update
&& ln
->ln_router
&& !ip6_forwarding
&&
2685 (ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
2686 RT_REMREF_LOCKED(rt
);
2688 lck_mtx_lock(nd6_mutex
);
2689 defrouter_select(ifp
);
2690 lck_mtx_unlock(nd6_mutex
);
2692 RT_REMREF_LOCKED(rt
);
2699 __unused
void *ignored_arg
)
2702 struct nd_ifinfo
*nd6if
;
2704 lck_rw_lock_shared(nd_if_rwlock
);
2705 for (i
= 1; i
< if_index
+ 1; i
++) {
2706 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
)
2708 nd6if
= &nd_ifinfo
[i
];
2709 if (nd6if
->basereachable
&& /* already initialized */
2710 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
2712 * Since reachable time rarely changes by router
2713 * advertisements, we SHOULD insure that a new random
2714 * value gets recomputed at least once every few hours.
2717 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
2718 nd6if
->reachable
= ND_COMPUTE_RTIME(nd6if
->basereachable
);
2721 lck_rw_done(nd_if_rwlock
);
2722 timeout(nd6_slowtimo
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
2725 #define senderr(e) { error = (e); goto bad;}
2727 nd6_output(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m0
,
2728 struct sockaddr_in6
*dst
, struct rtentry
*hint0
)
2730 struct mbuf
*m
= m0
;
2731 struct rtentry
*rt
= hint0
, *hint
= hint0
;
2732 struct llinfo_nd6
*ln
= NULL
;
2734 struct timeval timenow
;
2735 struct rtentry
*rtrele
= NULL
;
2739 RT_ADDREF_LOCKED(rt
);
2742 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
) || !nd6_need_cache(ifp
)) {
2749 * Next hop determination. Because we may involve the gateway route
2750 * in addition to the original route, locking is rather complicated.
2751 * The general concept is that regardless of whether the route points
2752 * to the original route or to the gateway route, this routine takes
2753 * an extra reference on such a route. This extra reference will be
2754 * released at the end.
2756 * Care must be taken to ensure that the "hint0" route never gets freed
2757 * via rtfree(), since the caller may have stored it inside a struct
2758 * route with a reference held for that placeholder.
2760 * This logic is similar to, though not exactly the same as the one
2761 * used by arp_route_to_gateway_route().
2765 * We have a reference to "rt" by now (or below via rtalloc1),
2766 * which will either be released or freed at the end of this
2769 RT_LOCK_ASSERT_HELD(rt
);
2770 if (!(rt
->rt_flags
& RTF_UP
)) {
2771 RT_REMREF_LOCKED(rt
);
2773 if ((hint
= rt
= rtalloc1_scoped((struct sockaddr
*)dst
,
2774 1, 0, ifp
->if_index
)) != NULL
) {
2776 if (rt
->rt_ifp
!= ifp
) {
2777 /* XXX: loop care? */
2779 error
= nd6_output(ifp
, origifp
, m0
,
2785 senderr(EHOSTUNREACH
);
2789 if (rt
->rt_flags
& RTF_GATEWAY
) {
2790 struct rtentry
*gwrt
;
2791 struct in6_ifaddr
*ia6
= NULL
;
2792 struct sockaddr_in6 gw6
;
2794 rtgw_to_sa6(rt
, &gw6
);
2796 * Must drop rt_lock since nd6_is_addr_neighbor()
2797 * calls nd6_lookup() and acquires rnh_lock.
2802 * We skip link-layer address resolution and NUD
2803 * if the gateway is not a neighbor from ND point
2804 * of view, regardless of the value of nd_ifinfo.flags.
2805 * The second condition is a bit tricky; we skip
2806 * if the gateway is our own address, which is
2807 * sometimes used to install a route to a p2p link.
2809 if (!nd6_is_addr_neighbor(&gw6
, ifp
, 0) ||
2810 (ia6
= in6ifa_ifpwithaddr(ifp
, &gw6
.sin6_addr
))) {
2812 * We allow this kind of tricky route only
2813 * when the outgoing interface is p2p.
2814 * XXX: we may need a more generic rule here.
2817 IFA_REMREF(&ia6
->ia_ifa
);
2818 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
2819 senderr(EHOSTUNREACH
);
2824 gw6
= *((struct sockaddr_in6
*)rt
->rt_gateway
);
2826 /* If hint is now down, give up */
2827 if (!(rt
->rt_flags
& RTF_UP
)) {
2829 senderr(EHOSTUNREACH
);
2832 /* If there's no gateway route, look it up */
2833 if ((gwrt
= rt
->rt_gwroute
) == NULL
) {
2837 /* Become a regular mutex */
2838 RT_CONVERT_LOCK(rt
);
2841 * Take gwrt's lock while holding route's lock;
2842 * this is okay since gwrt never points back
2843 * to rt, so no lock ordering issues.
2846 if (!(gwrt
->rt_flags
& RTF_UP
)) {
2847 struct rtentry
*ogwrt
;
2849 rt
->rt_gwroute
= NULL
;
2854 gwrt
= rtalloc1_scoped((struct sockaddr
*)&gw6
,
2855 1, 0, ifp
->if_index
);
2859 * Bail out if the route is down, no route
2860 * to gateway, circular route, or if the
2861 * gateway portion of "rt" has changed.
2863 if (!(rt
->rt_flags
& RTF_UP
) ||
2864 gwrt
== NULL
|| gwrt
== rt
||
2865 !equal(SA(&gw6
), rt
->rt_gateway
)) {
2867 RT_REMREF_LOCKED(gwrt
);
2873 senderr(EHOSTUNREACH
);
2876 /* Remove any existing gwrt */
2877 ogwrt
= rt
->rt_gwroute
;
2878 if ((rt
->rt_gwroute
= gwrt
) != NULL
)
2882 /* Now free the replaced gwrt */
2885 /* If still no route to gateway, bail out */
2887 senderr(EHOSTUNREACH
);
2888 /* Remember to release/free "rt" at the end */
2892 /* If gwrt is now down, give up */
2893 if (!(rt
->rt_flags
& RTF_UP
)) {
2897 /* "rtrele" == original "rt" */
2898 senderr(EHOSTUNREACH
);
2901 RT_ADDREF_LOCKED(gwrt
);
2905 /* If gwrt is now down, give up */
2906 if (!(gwrt
->rt_flags
& RTF_UP
)) {
2909 senderr(EHOSTUNREACH
);
2911 /* Remember to release/free "rt" at the end */
2916 /* Become a regular mutex */
2917 RT_CONVERT_LOCK(rt
);
2921 RT_LOCK_ASSERT_HELD(rt
);
2924 * Address resolution or Neighbor Unreachability Detection
2926 * At this point, the destination of the packet must be a unicast
2927 * or an anycast address(i.e. not a multicast).
2930 /* Look up the neighbor cache for the nexthop */
2931 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0) {
2934 struct sockaddr_in6 sin6
;
2936 * Clear out Scope ID field in case it is set.
2939 sin6
.sin6_scope_id
= 0;
2941 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2942 * the condition below is not very efficient. But we believe
2943 * it is tolerable, because this should be a rare case.
2944 * Must drop rt_lock since nd6_is_addr_neighbor() calls
2945 * nd6_lookup() and acquires rnh_lock.
2949 if (nd6_is_addr_neighbor(&sin6
, ifp
, 0)) {
2950 /* "rtrele" may have been used, so clean up "rt" now */
2952 /* Don't free "hint0" */
2958 /* Callee returns a locked route upon success */
2959 rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
, 0);
2961 RT_LOCK_ASSERT_HELD(rt
);
2964 } else if (rt
!= NULL
) {
2972 lck_rw_lock_shared(nd_if_rwlock
);
2973 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0 &&
2974 !(nd_ifinfo
[ifp
->if_index
].flags
& ND6_IFF_PERFORMNUD
)) {
2975 lck_rw_done(nd_if_rwlock
);
2977 "nd6_output: can't allocate llinfo for %s "
2979 ip6_sprintf(&dst
->sin6_addr
), ln
, rt
);
2980 senderr(EIO
); /* XXX: good error? */
2982 lck_rw_done(nd_if_rwlock
);
2984 goto sendpkt
; /* send anyway */
2987 getmicrotime(&timenow
);
2989 /* We don't have to do link-layer address resolution on a p2p link. */
2990 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
2991 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
2992 ln
->ln_state
= ND6_LLINFO_STALE
;
2993 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
, nd6_gctimer
);
2997 * The first time we send a packet to a neighbor whose entry is
2998 * STALE, we have to change the state to DELAY and a sets a timer to
2999 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
3000 * neighbor unreachability detection on expiration.
3003 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
3005 ln
->ln_state
= ND6_LLINFO_DELAY
;
3006 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
, nd6_delay
);
3010 * If the neighbor cache entry has a state other than INCOMPLETE
3011 * (i.e. its link-layer address is already resolved), just
3014 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) {
3017 * Move this entry to the head of the queue so that it is
3018 * less likely for this entry to be a target of forced
3019 * garbage collection (see nd6_rtrequest()).
3021 lck_mtx_lock(rnh_lock
);
3023 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
3028 lck_mtx_unlock(rnh_lock
);
3033 * There is a neighbor cache entry, but no ethernet address
3034 * response yet. Replace the held mbuf (if any) with this
3037 * This code conforms to the rate-limiting rule described in Section
3038 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
3041 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
)
3042 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
3044 m_freem(ln
->ln_hold
);
3046 if (ln
->ln_expire
&& ln
->ln_asked
< nd6_mmaxtries
&&
3047 ln
->ln_expire
< timenow
.tv_sec
) {
3049 lck_rw_lock_shared(nd_if_rwlock
);
3050 ln
->ln_expire
= timenow
.tv_sec
+
3051 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
3052 lck_rw_done(nd_if_rwlock
);
3054 /* We still have a reference on rt (for ln) */
3055 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0);
3060 * Move this entry to the head of the queue so that it is
3061 * less likely for this entry to be a target of forced
3062 * garbage collection (see nd6_rtrequest()).
3064 lck_mtx_lock(rnh_lock
);
3066 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
3070 /* Clean up "rt" now while we can */
3072 RT_REMREF_LOCKED(rt
);
3078 rt
= NULL
; /* "rt" has been taken care of */
3079 lck_mtx_unlock(rnh_lock
);
3086 RT_LOCK_ASSERT_NOTHELD(rt
);
3088 /* discard the packet if IPv6 operation is disabled on the interface */
3089 lck_rw_lock_shared(nd_if_rwlock
);
3090 if ((nd_ifinfo
[ifp
->if_index
].flags
& ND6_IFF_IFDISABLED
)) {
3091 lck_rw_done(nd_if_rwlock
);
3092 error
= ENETDOWN
; /* better error? */
3095 lck_rw_done(nd_if_rwlock
);
3097 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
3098 /* forwarding rules require the original scope_id */
3099 m
->m_pkthdr
.rcvif
= origifp
;
3100 error
= dlil_output(origifp
, PF_INET6
, m
, (caddr_t
)rt
,
3101 (struct sockaddr
*)dst
, 0);
3104 /* Do not allow loopback address to wind up on a wire */
3105 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
3107 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
3108 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
))) {
3109 ip6stat
.ip6s_badscope
++;
3111 * Do not simply drop the packet just like a
3112 * firewall -- we want the the application to feel
3113 * the pain. Return ENETUNREACH like ip6_output
3114 * does in some similar cases. This can startle
3115 * the otherwise clueless process that specifies
3116 * loopback as the source address.
3118 error
= ENETUNREACH
;
3125 /* Mark use timestamp */
3126 if (rt
->rt_llinfo
!= NULL
)
3127 nd6_llreach_use(rt
->rt_llinfo
);
3131 if (hint
&& nstat_collect
)
3132 nstat_route_tx(hint
, 1, m
->m_pkthdr
.len
, 0);
3134 m
->m_pkthdr
.rcvif
= NULL
;
3135 error
= dlil_output(ifp
, PF_INET6
, m
, (caddr_t
)rt
,
3136 (struct sockaddr
*)dst
, 0);
3144 /* Clean up "rt" unless it's already been done */
3148 RT_REMREF_LOCKED(rt
);
3155 /* And now clean up "rtrele" if there is any */
3156 if (rtrele
!= NULL
) {
3157 RT_LOCK_SPIN(rtrele
);
3158 if (rtrele
== hint0
) {
3159 RT_REMREF_LOCKED(rtrele
);
3175 * XXX: we currently do not make neighbor cache on any interface
3176 * other than ARCnet, Ethernet, FDDI and GIF.
3179 * - unidirectional tunnels needs no ND
3181 switch (ifp
->if_type
) {
3187 case IFT_IEEE8023ADLAG
:
3191 case IFT_GIF
: /* XXX need more cases? */
3209 struct sockaddr
*dst
,
3213 struct sockaddr_dl
*sdl
;
3215 if (m
->m_flags
& M_MCAST
) {
3216 switch (ifp
->if_type
) {
3220 case IFT_IEEE8023ADLAG
:
3225 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
3229 for (i
= 0; i
< ifp
->if_addrlen
; i
++)
3236 return(0); /* caller will free mbuf */
3241 /* this could happen, if we could not allocate memory */
3242 return(0); /* caller will free mbuf */
3245 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
3246 printf("nd6_storelladdr: something odd happens\n");
3248 return(0); /* caller will free mbuf */
3250 sdl
= SDL(rt
->rt_gateway
);
3251 if (sdl
->sdl_alen
== 0) {
3252 /* this should be impossible, but we bark here for debugging */
3253 printf("nd6_storelladdr: sdl_alen == 0\n");
3255 return(0); /* caller will free mbuf */
3258 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
3264 * This is the ND pre-output routine; care must be taken to ensure that
3265 * the "hint" route never gets freed via rtfree(), since the caller may
3266 * have stored it inside a struct route with a reference held for that
3270 nd6_lookup_ipv6(ifnet_t ifp
, const struct sockaddr_in6
*ip6_dest
,
3271 struct sockaddr_dl
*ll_dest
, size_t ll_dest_len
, route_t hint
,
3274 route_t route
= hint
;
3276 struct sockaddr_dl
*sdl
= NULL
;
3279 if (ip6_dest
->sin6_family
!= AF_INET6
)
3280 return (EAFNOSUPPORT
);
3282 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
3287 * Callee holds a reference on the route and returns
3288 * with the route entry locked, upon success.
3290 result
= arp_route_to_gateway_route(
3291 (const struct sockaddr
*)ip6_dest
, hint
, &route
);
3295 RT_LOCK_ASSERT_HELD(route
);
3298 if ((packet
->m_flags
& M_MCAST
) != 0) {
3301 result
= dlil_resolve_multi(ifp
,
3302 (const struct sockaddr
*)ip6_dest
,
3303 (struct sockaddr
*)ll_dest
, ll_dest_len
);
3309 if (route
== NULL
) {
3311 * This could happen, if we could not allocate memory or
3312 * if arp_route_to_gateway_route() didn't return a route.
3318 if (route
->rt_gateway
->sa_family
!= AF_LINK
) {
3319 printf("nd6_lookup_ipv6: gateway address not AF_LINK\n");
3320 result
= EADDRNOTAVAIL
;
3324 sdl
= SDL(route
->rt_gateway
);
3325 if (sdl
->sdl_alen
== 0) {
3326 /* this should be impossible, but we bark here for debugging */
3327 printf("nd6_lookup_ipv6: sdl_alen == 0\n");
3328 result
= EHOSTUNREACH
;
3332 copy_len
= sdl
->sdl_len
<= ll_dest_len
? sdl
->sdl_len
: ll_dest_len
;
3333 bcopy(sdl
, ll_dest
, copy_len
);
3336 if (route
!= NULL
) {
3337 if (route
== hint
) {
3338 RT_REMREF_LOCKED(route
);
3348 SYSCTL_DECL(_net_inet6_icmp6
);
3351 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
3353 #pragma unused(oidp, arg1, arg2)
3356 struct nd_defrouter
*dr
;
3357 int p64
= proc_is64bit(req
->p
);
3362 lck_mtx_lock(nd6_mutex
);
3364 struct in6_defrouter_64
*d
, *de
;
3366 for (dr
= TAILQ_FIRST(&nd_defrouter
);
3368 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
3369 d
= (struct in6_defrouter_64
*)buf
;
3370 de
= (struct in6_defrouter_64
*)(buf
+ sizeof (buf
));
3373 bzero(d
, sizeof (*d
));
3374 d
->rtaddr
.sin6_family
= AF_INET6
;
3375 d
->rtaddr
.sin6_len
= sizeof (d
->rtaddr
);
3376 if (in6_recoverscope(&d
->rtaddr
, &dr
->rtaddr
,
3380 "default router list (%s)\n",
3381 ip6_sprintf(&dr
->rtaddr
));
3382 d
->flags
= dr
->flags
;
3383 d
->stateflags
= dr
->stateflags
;
3384 d
->stateflags
&= ~NDDRF_PROCESSED
;
3385 d
->rtlifetime
= dr
->rtlifetime
;
3386 d
->expire
= dr
->expire
;
3387 d
->if_index
= dr
->ifp
->if_index
;
3389 panic("buffer too short");
3391 error
= SYSCTL_OUT(req
, buf
, sizeof (*d
));
3396 struct in6_defrouter_32
*d_32
, *de_32
;
3398 for (dr
= TAILQ_FIRST(&nd_defrouter
);
3400 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
3401 d_32
= (struct in6_defrouter_32
*)buf
;
3402 de_32
= (struct in6_defrouter_32
*)(buf
+ sizeof (buf
));
3404 if (d_32
+ 1 <= de_32
) {
3405 bzero(d_32
, sizeof (*d_32
));
3406 d_32
->rtaddr
.sin6_family
= AF_INET6
;
3407 d_32
->rtaddr
.sin6_len
= sizeof (d_32
->rtaddr
);
3408 if (in6_recoverscope(&d_32
->rtaddr
, &dr
->rtaddr
,
3412 "default router list (%s)\n",
3413 ip6_sprintf(&dr
->rtaddr
));
3414 d_32
->flags
= dr
->flags
;
3415 d_32
->stateflags
= dr
->stateflags
;
3416 d_32
->stateflags
&= ~NDDRF_PROCESSED
;
3417 d_32
->rtlifetime
= dr
->rtlifetime
;
3418 d_32
->expire
= dr
->expire
;
3419 d_32
->if_index
= dr
->ifp
->if_index
;
3421 panic("buffer too short");
3423 error
= SYSCTL_OUT(req
, buf
, sizeof (*d_32
));
3428 lck_mtx_unlock(nd6_mutex
);
3433 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
3435 #pragma unused(oidp, arg1, arg2)
3438 struct nd_prefix
*pr
;
3439 int p64
= proc_is64bit(req
->p
);
3444 lck_mtx_lock(nd6_mutex
);
3446 struct in6_prefix_64
*p
, *pe
;
3448 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3449 u_short advrtrs
= 0;
3451 struct sockaddr_in6
*sin6
, *s6
;
3452 struct nd_pfxrouter
*pfr
;
3454 p
= (struct in6_prefix_64
*)buf
;
3455 pe
= (struct in6_prefix_64
*)(buf
+ sizeof (buf
));
3458 bzero(p
, sizeof (*p
));
3459 sin6
= (struct sockaddr_in6
*)(p
+ 1);
3462 p
->prefix
= pr
->ndpr_prefix
;
3463 if (in6_recoverscope(&p
->prefix
,
3464 &p
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
3466 "scope error in prefix list (%s)\n",
3467 ip6_sprintf(&p
->prefix
.sin6_addr
));
3468 p
->raflags
= pr
->ndpr_raf
;
3469 p
->prefixlen
= pr
->ndpr_plen
;
3470 p
->vltime
= pr
->ndpr_vltime
;
3471 p
->pltime
= pr
->ndpr_pltime
;
3472 p
->if_index
= pr
->ndpr_ifp
->if_index
;
3473 p
->expire
= pr
->ndpr_expire
;
3474 p
->refcnt
= pr
->ndpr_addrcnt
;
3475 p
->flags
= pr
->ndpr_stateflags
;
3476 p
->origin
= PR_ORIG_RA
;
3478 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
3480 pfr
= pfr
->pfr_next
) {
3481 if ((void *)&sin6
[advrtrs
+ 1] >
3486 s6
= &sin6
[advrtrs
];
3487 bzero(s6
, sizeof (*s6
));
3488 s6
->sin6_family
= AF_INET6
;
3489 s6
->sin6_len
= sizeof (*sin6
);
3490 if (in6_recoverscope(s6
,
3491 &pfr
->router
->rtaddr
,
3492 pfr
->router
->ifp
) != 0)
3493 log(LOG_ERR
, "scope error in "
3494 "prefix list (%s)\n",
3495 ip6_sprintf(&pfr
->router
->
3499 p
->advrtrs
= advrtrs
;
3502 panic("buffer too short");
3504 advance
= sizeof (*p
) + sizeof (*sin6
) * advrtrs
;
3505 error
= SYSCTL_OUT(req
, buf
, advance
);
3510 struct in6_prefix_32
*p_32
, *pe_32
;
3512 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3513 u_short advrtrs
= 0;
3515 struct sockaddr_in6
*sin6
, *s6
;
3516 struct nd_pfxrouter
*pfr
;
3518 p_32
= (struct in6_prefix_32
*)buf
;
3519 pe_32
= (struct in6_prefix_32
*)(buf
+ sizeof (buf
));
3521 if (p_32
+ 1 <= pe_32
) {
3522 bzero(p_32
, sizeof (*p_32
));
3523 sin6
= (struct sockaddr_in6
*)(p_32
+ 1);
3526 p_32
->prefix
= pr
->ndpr_prefix
;
3527 if (in6_recoverscope(&p_32
->prefix
,
3528 &p_32
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
3529 log(LOG_ERR
, "scope error in prefix "
3530 "list (%s)\n", ip6_sprintf(&p_32
->
3532 p_32
->raflags
= pr
->ndpr_raf
;
3533 p_32
->prefixlen
= pr
->ndpr_plen
;
3534 p_32
->vltime
= pr
->ndpr_vltime
;
3535 p_32
->pltime
= pr
->ndpr_pltime
;
3536 p_32
->if_index
= pr
->ndpr_ifp
->if_index
;
3537 p_32
->expire
= pr
->ndpr_expire
;
3538 p_32
->refcnt
= pr
->ndpr_addrcnt
;
3539 p_32
->flags
= pr
->ndpr_stateflags
;
3540 p_32
->origin
= PR_ORIG_RA
;
3542 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
3544 pfr
= pfr
->pfr_next
) {
3545 if ((void *)&sin6
[advrtrs
+ 1] >
3550 s6
= &sin6
[advrtrs
];
3551 bzero(s6
, sizeof (*s6
));
3552 s6
->sin6_family
= AF_INET6
;
3553 s6
->sin6_len
= sizeof (*sin6
);
3554 if (in6_recoverscope(s6
,
3555 &pfr
->router
->rtaddr
,
3556 pfr
->router
->ifp
) != 0)
3557 log(LOG_ERR
, "scope error in "
3558 "prefix list (%s)\n",
3559 ip6_sprintf(&pfr
->router
->
3563 p_32
->advrtrs
= advrtrs
;
3566 panic("buffer too short");
3568 advance
= sizeof (*p_32
) + sizeof (*sin6
) * advrtrs
;
3569 error
= SYSCTL_OUT(req
, buf
, advance
);
3574 lck_mtx_unlock(nd6_mutex
);
3577 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
3578 CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, nd6_sysctl_drlist
, "S,in6_defrouter","");
3579 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
3580 CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, nd6_sysctl_prlist
, "S,in6_defrouter","");