2 * Copyright (c) 2008-2009 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 <kern/queue.h>
83 #include <kern/zalloc.h>
85 #define DONT_WARN_OBSOLETE
87 #include <net/if_dl.h>
88 #include <net/if_types.h>
89 #include <net/if_atm.h>
90 #include <net/route.h>
93 #include <netinet/in.h>
94 #include <netinet/in_arp.h>
95 #include <netinet/if_ether.h>
96 #include <netinet/if_fddi.h>
97 #include <netinet6/in6_var.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/nd6.h>
101 #include <netinet6/in6_prefix.h>
102 #include <netinet/icmp6.h>
106 #include <net/net_osdep.h>
108 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
109 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
111 #define SA(p) ((struct sockaddr *)(p))
112 #define SIN6(s) ((struct sockaddr_in6 *)s)
113 #define SDL(s) ((struct sockaddr_dl *)s)
114 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
117 int nd6_prune
= 1; /* walk list every 1 seconds */
118 int nd6_delay
= 5; /* delay first probe time 5 second */
119 int nd6_umaxtries
= 3; /* maximum unicast query */
120 int nd6_mmaxtries
= 3; /* maximum multicast query */
121 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
122 int nd6_gctimer
= (60 * 60 * 24); /* 1 day: garbage collection timer */
124 /* preventing too many loops in ND option parsing */
125 int nd6_maxndopt
= 10; /* max # of ND options allowed */
127 int nd6_maxnudhint
= 0; /* max # of subsequent upper layer hints */
136 static int nd6_inuse
, nd6_allocated
;
139 * Synchronization notes:
141 * The global list of ND entries are stored in llinfo_nd6; an entry
142 * gets inserted into the list when the route is created and gets
143 * removed from the list when it is deleted; this is done as part
144 * of RTM_ADD/RTM_RESOLVE/RTM_DELETE in nd6_rtrequest().
146 * Because rnh_lock and rt_lock for the entry are held during those
147 * operations, the same locks (and thus lock ordering) must be used
148 * elsewhere to access the relevant data structure fields:
150 * ln_next, ln_prev, ln_rt
152 * - Routing lock (rnh_lock)
154 * ln_hold, ln_asked, ln_expire, ln_state, ln_router, ln_byhint, ln_flags
156 * - Routing entry lock (rt_lock)
158 * Due to the dependency on rt_lock, llinfo_nd6 has the same lifetime
159 * as the route entry itself. When a route is deleted (RTM_DELETE),
160 * it is simply removed from the global list but the memory is not
161 * freed until the route itself is freed.
163 struct llinfo_nd6 llinfo_nd6
= {
164 &llinfo_nd6
, &llinfo_nd6
, NULL
, NULL
, 0, 0, 0, 0, 0, 0
167 /* Protected by nd_if_rwlock */
168 size_t nd_ifinfo_indexlim
= 32; /* increased for 5589193 */
169 struct nd_ifinfo
*nd_ifinfo
= NULL
;
171 static lck_grp_attr_t
*nd_if_rwlock_grp_attr
;
172 static lck_grp_t
*nd_if_rwlock_grp
;
173 static lck_attr_t
*nd_if_rwlock_attr
;
174 lck_rw_t
*nd_if_rwlock
;
176 /* Protected by nd6_mutex */
177 struct nd_drhead nd_defrouter
;
178 struct nd_prhead nd_prefix
= { 0 };
180 int nd6_recalc_reachtm_interval
= ND6_RECALC_REACHTM_INTERVAL
;
181 static struct sockaddr_in6 all1_sa
;
183 static int regen_tmpaddr(struct in6_ifaddr
*);
184 extern lck_mtx_t
*ip6_mutex
;
185 extern lck_mtx_t
*nd6_mutex
;
187 static void nd6_slowtimo(void *ignored_arg
);
188 static struct llinfo_nd6
*nd6_llinfo_alloc(void);
189 static void nd6_llinfo_free(void *);
191 static void nd6_siocgdrlst(void *, int);
192 static void nd6_siocgprlst(void *, int);
195 * Insertion and removal from llinfo_nd6 must be done with rnh_lock held.
197 #define LN_DEQUEUE(_ln) do { \
198 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); \
199 RT_LOCK_ASSERT_HELD((_ln)->ln_rt); \
200 (_ln)->ln_next->ln_prev = (_ln)->ln_prev; \
201 (_ln)->ln_prev->ln_next = (_ln)->ln_next; \
202 (_ln)->ln_prev = (_ln)->ln_next = NULL; \
203 (_ln)->ln_flags &= ~ND6_LNF_IN_USE; \
206 #define LN_INSERTHEAD(_ln) do { \
207 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); \
208 RT_LOCK_ASSERT_HELD((_ln)->ln_rt); \
209 (_ln)->ln_next = llinfo_nd6.ln_next; \
210 llinfo_nd6.ln_next = (_ln); \
211 (_ln)->ln_prev = &llinfo_nd6; \
212 (_ln)->ln_next->ln_prev = (_ln); \
213 (_ln)->ln_flags |= ND6_LNF_IN_USE; \
216 static struct zone
*llinfo_nd6_zone
;
217 #define LLINFO_ND6_ZONE_MAX 256 /* maximum elements in zone */
218 #define LLINFO_ND6_ZONE_NAME "llinfo_nd6" /* name for zone */
223 static int nd6_init_done
= 0;
227 log(LOG_NOTICE
, "nd6_init called more than once (ignored)\n");
231 all1_sa
.sin6_family
= AF_INET6
;
232 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
233 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++)
234 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
236 /* initialization of the default router list */
237 TAILQ_INIT(&nd_defrouter
);
239 nd_if_rwlock_grp_attr
= lck_grp_attr_alloc_init();
240 nd_if_rwlock_grp
= lck_grp_alloc_init("nd_if_rwlock",
241 nd_if_rwlock_grp_attr
);
242 nd_if_rwlock_attr
= lck_attr_alloc_init();
243 nd_if_rwlock
= lck_rw_alloc_init(nd_if_rwlock_grp
, nd_if_rwlock_attr
);
245 llinfo_nd6_zone
= zinit(sizeof (struct llinfo_nd6
),
246 LLINFO_ND6_ZONE_MAX
* sizeof (struct llinfo_nd6
), 0,
247 LLINFO_ND6_ZONE_NAME
);
248 if (llinfo_nd6_zone
== NULL
)
249 panic("%s: failed allocating llinfo_nd6_zone", __func__
);
251 zone_change(llinfo_nd6_zone
, Z_EXPAND
, TRUE
);
256 timeout(nd6_slowtimo
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
259 static struct llinfo_nd6
*
260 nd6_llinfo_alloc(void)
262 return (zalloc(llinfo_nd6_zone
));
266 nd6_llinfo_free(void *arg
)
268 struct llinfo_nd6
*ln
= arg
;
270 if (ln
->ln_next
!= NULL
|| ln
->ln_prev
!= NULL
) {
271 panic("%s: trying to free %p when it is in use", __func__
, ln
);
275 /* Just in case there's anything there, free it */
276 if (ln
->ln_hold
!= NULL
) {
277 m_freem(ln
->ln_hold
);
281 zfree(llinfo_nd6_zone
, ln
);
285 nd6_ifattach(struct ifnet
*ifp
)
289 * We have some arrays that should be indexed by if_index.
290 * since if_index will grow dynamically, they should grow too.
292 lck_rw_lock_exclusive(nd_if_rwlock
);
293 if (nd_ifinfo
== NULL
|| if_index
>= nd_ifinfo_indexlim
) {
296 size_t newlim
= nd_ifinfo_indexlim
;
298 while (if_index
>= newlim
)
302 n
= newlim
* sizeof(struct nd_ifinfo
);
303 q
= (caddr_t
)_MALLOC(n
, M_IP6NDP
, M_WAITOK
);
305 lck_rw_done(nd_if_rwlock
);
309 nd_ifinfo_indexlim
= newlim
;
311 bcopy((caddr_t
)nd_ifinfo
, q
, n
/2);
313 * We might want to pattern fill the old
314 * array to catch use-after-free cases.
316 FREE((caddr_t
)nd_ifinfo
, M_IP6NDP
);
318 nd_ifinfo
= (struct nd_ifinfo
*)q
;
320 lck_rw_done(nd_if_rwlock
);
322 #define ND nd_ifinfo[ifp->if_index]
325 * Don't initialize if called twice.
326 * XXX: to detect this, we should choose a member that is never set
327 * before initialization of the ND structure itself. We formaly used
328 * the linkmtu member, which was not suitable because it could be
329 * initialized via "ifconfig mtu".
331 lck_rw_lock_shared(nd_if_rwlock
);
332 if (ND
.basereachable
) {
333 lck_rw_done(nd_if_rwlock
);
336 ND
.linkmtu
= ifp
->if_mtu
;
337 ND
.chlim
= IPV6_DEFHLIM
;
338 ND
.basereachable
= REACHABLE_TIME
;
339 ND
.reachable
= ND_COMPUTE_RTIME(ND
.basereachable
);
340 ND
.retrans
= RETRANS_TIMER
;
342 ND
.flags
= ND6_IFF_PERFORMNUD
;
343 lck_rw_done(nd_if_rwlock
);
351 * Reset ND level link MTU. This function is called when the physical MTU
352 * changes, which means we might have to adjust the ND level MTU.
355 nd6_setmtu(struct ifnet
*ifp
)
357 struct nd_ifinfo
*ndi
;
358 u_int32_t oldmaxmtu
, maxmtu
;
361 * Make sure IPv6 is enabled for the interface first,
362 * because this can be called directly from SIOCSIFMTU for IPv4
364 lck_rw_lock_shared(nd_if_rwlock
);
365 if (ifp
->if_index
>= nd_ifinfo_indexlim
) {
366 lck_rw_done(nd_if_rwlock
);
367 return; /* we're out of bound for nd_ifinfo */
370 ndi
= &nd_ifinfo
[ifp
->if_index
];
371 oldmaxmtu
= ndi
->maxmtu
;
374 * The ND level maxmtu is somewhat redundant to the interface MTU
375 * and is an implementation artifact of KAME. Instead of hard-
376 * limiting the maxmtu based on the interface type here, we simply
377 * take the if_mtu value since SIOCSIFMTU would have taken care of
378 * the sanity checks related to the maximum MTU allowed for the
379 * interface (a value that is known only by the interface layer),
380 * by sending the request down via ifnet_ioctl(). The use of the
381 * ND level maxmtu and linkmtu (the latter obtained via RA) are done
382 * via IN6_LINKMTU() which does further checking against if_mtu.
384 maxmtu
= ndi
->maxmtu
= ifp
->if_mtu
;
387 * Decreasing the interface MTU under IPV6 minimum MTU may cause
388 * undesirable situation. We thus notify the operator of the change
389 * explicitly. The check for oldmaxmtu is necessary to restrict the
390 * log to the case of changing the MTU, not initializing it.
392 if (oldmaxmtu
>= IPV6_MMTU
&& ndi
->maxmtu
< IPV6_MMTU
) {
393 log(LOG_NOTICE
, "nd6_setmtu: "
394 "new link MTU on %s%d (%u) is too small for IPv6\n",
395 ifp
->if_name
, ifp
->if_unit
, (uint32_t)ndi
->maxmtu
);
397 lck_rw_done(nd_if_rwlock
);
399 /* also adjust in6_maxmtu if necessary. */
400 if (maxmtu
> in6_maxmtu
)
408 union nd_opts
*ndopts
)
410 bzero(ndopts
, sizeof(*ndopts
));
411 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
413 = (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
416 ndopts
->nd_opts_done
= 1;
417 ndopts
->nd_opts_search
= NULL
;
422 * Take one ND option.
426 union nd_opts
*ndopts
)
428 struct nd_opt_hdr
*nd_opt
;
432 panic("ndopts == NULL in nd6_option\n");
433 if (!ndopts
->nd_opts_last
)
434 panic("uninitialized ndopts in nd6_option\n");
435 if (!ndopts
->nd_opts_search
)
437 if (ndopts
->nd_opts_done
)
440 nd_opt
= ndopts
->nd_opts_search
;
442 /* make sure nd_opt_len is inside the buffer */
443 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
444 bzero(ndopts
, sizeof(*ndopts
));
448 olen
= nd_opt
->nd_opt_len
<< 3;
451 * Message validation requires that all included
452 * options have a length that is greater than zero.
454 bzero(ndopts
, sizeof(*ndopts
));
458 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
459 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
460 /* option overruns the end of buffer, invalid */
461 bzero(ndopts
, sizeof(*ndopts
));
463 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
464 /* reached the end of options chain */
465 ndopts
->nd_opts_done
= 1;
466 ndopts
->nd_opts_search
= NULL
;
472 * Parse multiple ND options.
473 * This function is much easier to use, for ND routines that do not need
474 * multiple options of the same type.
478 union nd_opts
*ndopts
)
480 struct nd_opt_hdr
*nd_opt
;
484 panic("ndopts == NULL in nd6_options\n");
485 if (!ndopts
->nd_opts_last
)
486 panic("uninitialized ndopts in nd6_options\n");
487 if (!ndopts
->nd_opts_search
)
491 nd_opt
= nd6_option(ndopts
);
492 if (!nd_opt
&& !ndopts
->nd_opts_last
) {
494 * Message validation requires that all included
495 * options have a length that is greater than zero.
497 icmp6stat
.icp6s_nd_badopt
++;
498 bzero(ndopts
, sizeof(*ndopts
));
505 switch (nd_opt
->nd_opt_type
) {
506 case ND_OPT_SOURCE_LINKADDR
:
507 case ND_OPT_TARGET_LINKADDR
:
509 case ND_OPT_REDIRECTED_HEADER
:
510 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
512 "duplicated ND6 option found (type=%d)\n",
513 nd_opt
->nd_opt_type
));
516 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
520 case ND_OPT_PREFIX_INFORMATION
:
521 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
522 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
525 ndopts
->nd_opts_pi_end
=
526 (struct nd_opt_prefix_info
*)nd_opt
;
530 * Unknown options must be silently ignored,
531 * to accomodate future extension to the protocol.
534 "nd6_options: unsupported option %d - "
535 "option ignored\n", nd_opt
->nd_opt_type
));
540 if (i
> nd6_maxndopt
) {
541 icmp6stat
.icp6s_nd_toomanyopt
++;
542 nd6log((LOG_INFO
, "too many loop in nd opt\n"));
546 if (ndopts
->nd_opts_done
)
554 nd6_drain(__unused
void *ignored_arg
)
556 struct llinfo_nd6
*ln
;
557 struct nd_defrouter
*dr
;
558 struct nd_prefix
*pr
;
559 struct ifnet
*ifp
= NULL
;
560 struct in6_ifaddr
*ia6
, *nia6
;
561 struct in6_addrlifetime
*lt6
;
562 struct timeval timenow
;
564 getmicrotime(&timenow
);
567 * The global list llinfo_nd6 is modified by nd6_request() and is
568 * therefore protected by rnh_lock. For obvious reasons, we cannot
569 * hold rnh_lock across calls that might lead to code paths which
570 * attempt to acquire rnh_lock, else we deadlock. Hence for such
571 * cases we drop rt_lock and rnh_lock, make the calls, and repeat the
572 * loop. To ensure that we don't process the same entry more than
573 * once in a single timeout, we mark the "already-seen" entries with
574 * ND6_LNF_TIMER_SKIP flag. At the end of the loop, we do a second
575 * pass thru the entries and clear the flag so they can be processed
576 * during the next timeout.
578 lck_mtx_lock(rnh_lock
);
579 ln
= llinfo_nd6
.ln_next
;
580 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
582 struct sockaddr_in6
*dst
;
583 struct llinfo_nd6
*next
;
584 struct nd_ifinfo ndi
;
586 /* ln_next/prev/rt is protected by rnh_lock */
591 /* We've seen this already; skip it */
592 if (ln
->ln_flags
& ND6_LNF_TIMER_SKIP
) {
598 /* rt->rt_ifp should never be NULL */
599 if ((ifp
= rt
->rt_ifp
) == NULL
) {
600 panic("%s: ln(%p) rt(%p) rt_ifp == NULL", __func__
,
605 /* rt_llinfo must always be equal to ln */
606 if ((struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
) {
607 panic("%s: rt_llinfo(%p) is not equal to ln(%p)",
608 __func__
, rt
->rt_llinfo
, ln
);
612 /* rt_key should never be NULL */
613 dst
= (struct sockaddr_in6
*)rt_key(rt
);
615 panic("%s: rt(%p) key is NULL ln(%p)", __func__
,
620 /* Set the flag in case we jump to "again" */
621 ln
->ln_flags
|= ND6_LNF_TIMER_SKIP
;
623 if (ln
->ln_expire
> timenow
.tv_sec
) {
629 /* Make a copy (we're using it read-only anyway) */
630 lck_rw_lock_shared(nd_if_rwlock
);
631 if (ifp
->if_index
>= nd_ifinfo_indexlim
) {
632 lck_rw_done(nd_if_rwlock
);
637 ndi
= nd_ifinfo
[ifp
->if_index
];
638 lck_rw_done(nd_if_rwlock
);
640 RT_LOCK_ASSERT_HELD(rt
);
642 switch (ln
->ln_state
) {
643 case ND6_LLINFO_INCOMPLETE
:
644 if (ln
->ln_asked
< nd6_mmaxtries
) {
646 ln
->ln_expire
= timenow
.tv_sec
+
648 RT_ADDREF_LOCKED(rt
);
650 lck_mtx_unlock(rnh_lock
);
651 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
,
655 struct mbuf
*m
= ln
->ln_hold
;
659 * Fake rcvif to make ICMP error
660 * more helpful in diagnosing
662 * XXX: should we consider
665 m
->m_pkthdr
.rcvif
= ifp
;
667 lck_mtx_unlock(rnh_lock
);
668 icmp6_error(m
, ICMP6_DST_UNREACH
,
669 ICMP6_DST_UNREACH_ADDR
, 0);
672 lck_mtx_unlock(rnh_lock
);
676 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
679 case ND6_LLINFO_REACHABLE
:
681 ln
->ln_state
= ND6_LLINFO_STALE
;
682 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
,
688 case ND6_LLINFO_STALE
:
689 case ND6_LLINFO_PURGE
:
690 /* Garbage Collection(RFC 2461 5.3) */
693 lck_mtx_unlock(rnh_lock
);
695 lck_mtx_assert(rnh_lock
,
696 LCK_MTX_ASSERT_NOTOWNED
);
703 case ND6_LLINFO_DELAY
:
704 if ((ndi
.flags
& ND6_IFF_PERFORMNUD
) != 0) {
707 ln
->ln_state
= ND6_LLINFO_PROBE
;
708 ln
->ln_expire
= timenow
.tv_sec
+
710 RT_ADDREF_LOCKED(rt
);
712 lck_mtx_unlock(rnh_lock
);
713 nd6_ns_output(ifp
, &dst
->sin6_addr
,
714 &dst
->sin6_addr
, ln
, 0, 0);
715 lck_mtx_assert(rnh_lock
,
716 LCK_MTX_ASSERT_NOTOWNED
);
720 ln
->ln_state
= ND6_LLINFO_STALE
; /* XXX */
721 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
,
726 case ND6_LLINFO_PROBE
:
727 if (ln
->ln_asked
< nd6_umaxtries
) {
729 ln
->ln_expire
= timenow
.tv_sec
+
731 RT_ADDREF_LOCKED(rt
);
733 lck_mtx_unlock(rnh_lock
);
734 nd6_ns_output(ifp
, &dst
->sin6_addr
,
735 &dst
->sin6_addr
, ln
, 0, 0);
739 lck_mtx_unlock(rnh_lock
);
742 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
751 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
753 /* Now clear the flag from all entries */
754 ln
= llinfo_nd6
.ln_next
;
755 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
756 struct rtentry
*rt
= ln
->ln_rt
;
757 struct llinfo_nd6
*next
= ln
->ln_next
;
760 if (ln
->ln_flags
& ND6_LNF_TIMER_SKIP
)
761 ln
->ln_flags
&= ~ND6_LNF_TIMER_SKIP
;
765 lck_mtx_unlock(rnh_lock
);
767 /* expire default router list */
768 lck_mtx_lock(nd6_mutex
);
769 dr
= TAILQ_FIRST(&nd_defrouter
);
771 if (dr
->expire
&& dr
->expire
< timenow
.tv_sec
) {
772 struct nd_defrouter
*t
;
773 t
= TAILQ_NEXT(dr
, dr_entry
);
774 defrtrlist_del(dr
, 1);
777 dr
= TAILQ_NEXT(dr
, dr_entry
);
782 * expire interface addresses.
783 * in the past the loop was inside prefix expiry processing.
784 * However, from a stricter speci-confrmance standpoint, we should
785 * rather separate address lifetimes and prefix lifetimes.
788 for (ia6
= in6_ifaddrs
; ia6
; ia6
= nia6
) {
790 /* check address lifetime */
791 lt6
= &ia6
->ia6_lifetime
;
792 if (IFA6_IS_INVALID(ia6
)) {
796 * Extra reference for ourselves; it's no-op if
797 * we don't have to regenerate temporary address,
798 * otherwise it protects the address from going
799 * away since we drop nd6_mutex below.
801 ifaref(&ia6
->ia_ifa
);
804 * If the expiring address is temporary, try
805 * regenerating a new one. This would be useful when
806 * we suspended a laptop PC, then turned it on after a
807 * period that could invalidate all temporary
808 * addresses. Although we may have to restart the
809 * loop (see below), it must be after purging the
810 * address. Otherwise, we'd see an infinite loop of
813 if (ip6_use_tempaddr
&&
814 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
815 /* NOTE: We have to drop the lock here because
816 * regen_tmpaddr() eventually calls in6_update_ifa(),
817 * which must take the lock and would otherwise cause a
818 * hang. This is safe because the goto addrloop
819 * leads to a reevaluation of the in6_ifaddrs list
821 lck_mtx_unlock(nd6_mutex
);
822 if (regen_tmpaddr(ia6
) == 0)
824 lck_mtx_lock(nd6_mutex
);
827 in6_purgeaddr(&ia6
->ia_ifa
, 1);
829 /* Release extra reference taken above */
830 ifafree(&ia6
->ia_ifa
);
833 goto addrloop
; /* XXX: see below */
835 if (IFA6_IS_DEPRECATED(ia6
)) {
836 int oldflags
= ia6
->ia6_flags
;
838 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
841 * If a temporary address has just become deprecated,
842 * regenerate a new one if possible.
844 if (ip6_use_tempaddr
&&
845 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
846 (oldflags
& IN6_IFF_DEPRECATED
) == 0) {
849 lck_mtx_unlock(nd6_mutex
);
850 if (regen_tmpaddr(ia6
) == 0) {
852 * A new temporary address is
854 * XXX: this means the address chain
855 * has changed while we are still in
856 * the loop. Although the change
857 * would not cause disaster (because
858 * it's not a deletion, but an
859 * addition,) we'd rather restart the
860 * loop just for safety. Or does this
861 * significantly reduce performance??
863 lck_mtx_lock(nd6_mutex
);
866 lck_mtx_lock(nd6_mutex
);
870 * A new RA might have made a deprecated address
873 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
877 /* expire prefix list */
878 pr
= nd_prefix
.lh_first
;
881 * check prefix lifetime.
882 * since pltime is just for autoconf, pltime processing for
883 * prefix is not necessary.
885 if (pr
->ndpr_expire
&& pr
->ndpr_expire
< timenow
.tv_sec
) {
890 * address expiration and prefix expiration are
891 * separate. NEVER perform in6_purgeaddr here.
894 prelist_remove(pr
, 1);
899 lck_mtx_unlock(nd6_mutex
);
903 * ND6 timer routine to expire default route list and prefix list
906 nd6_timer(__unused
void *ignored_arg
)
909 timeout(nd6_timer
, (caddr_t
)0, nd6_prune
* hz
);
914 struct in6_ifaddr
*ia6
) /* deprecated/invalidated temporary address */
918 struct in6_ifaddr
*public_ifa6
= NULL
;
919 struct timeval timenow
;
921 getmicrotime(&timenow
);
923 ifp
= ia6
->ia_ifa
.ifa_ifp
;
924 ifnet_lock_exclusive(ifp
);
925 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
926 ifa
= ifa
->ifa_list
.tqe_next
)
928 struct in6_ifaddr
*it6
;
930 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
933 it6
= (struct in6_ifaddr
*)ifa
;
935 /* ignore no autoconf addresses. */
936 if ((it6
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
939 /* ignore autoconf addresses with different prefixes. */
940 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
)
944 * Now we are looking at an autoconf address with the same
945 * prefix as ours. If the address is temporary and is still
946 * preferred, do not create another one. It would be rare, but
947 * could happen, for example, when we resume a laptop PC after
950 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
951 !IFA6_IS_DEPRECATED(it6
)) {
957 * This is a public autoconf address that has the same prefix
958 * as ours. If it is preferred, keep it. We can't break the
959 * loop here, because there may be a still-preferred temporary
960 * address with the prefix.
962 if (!IFA6_IS_DEPRECATED(it6
))
965 ifnet_lock_done(ifp
);
967 if (public_ifa6
!= NULL
) {
970 if ((e
= in6_tmpifadd(public_ifa6
, 0, M_WAITOK
)) != 0) {
971 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
972 " tmp addr,errno=%d\n", e
);
982 * Nuke neighbor cache/prefix/default router management table, right before
989 struct llinfo_nd6
*ln
;
990 struct nd_defrouter
*dr
, *ndr
, drany
;
991 struct nd_prefix
*pr
, *npr
;
993 /* Nuke default router list entries toward ifp */
994 lck_mtx_lock(nd6_mutex
);
995 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
997 * The first entry of the list may be stored in
998 * the routing table, so we'll delete it later.
1000 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= ndr
) {
1001 ndr
= TAILQ_NEXT(dr
, dr_entry
);
1003 defrtrlist_del(dr
, 1);
1005 dr
= TAILQ_FIRST(&nd_defrouter
);
1007 defrtrlist_del(dr
, 1);
1010 /* Nuke prefix list entries toward ifp */
1011 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
1012 npr
= pr
->ndpr_next
;
1013 if (pr
->ndpr_ifp
== ifp
) {
1015 * Previously, pr->ndpr_addr is removed as well,
1016 * but I strongly believe we don't have to do it.
1017 * nd6_purge() is only called from in6_ifdetach(),
1018 * which removes all the associated interface addresses
1020 * (jinmei@kame.net 20010129)
1022 prelist_remove(pr
, 1);
1026 /* cancel default outgoing interface setting */
1027 if (nd6_defifindex
== ifp
->if_index
) {
1028 /* Release nd6_mutex as it will be acquired
1029 * during nd6_setdefaultiface again
1031 lck_mtx_unlock(nd6_mutex
);
1032 nd6_setdefaultiface(0);
1033 lck_mtx_lock(nd6_mutex
);
1036 if (!ip6_forwarding
&& (ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
1037 /* refresh default router list */
1038 bzero(&drany
, sizeof(drany
));
1039 defrouter_delreq(&drany
, 0);
1042 lck_mtx_unlock(nd6_mutex
);
1045 * Nuke neighbor cache entries for the ifp.
1046 * Note that rt->rt_ifp may not be the same as ifp,
1047 * due to KAME goto ours hack. See RTM_RESOLVE case in
1048 * nd6_rtrequest(), and ip6_input().
1051 lck_mtx_lock(rnh_lock
);
1052 ln
= llinfo_nd6
.ln_next
;
1053 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
1055 struct llinfo_nd6
*nln
;
1060 if (rt
->rt_gateway
!= NULL
&&
1061 rt
->rt_gateway
->sa_family
== AF_LINK
&&
1062 SDL(rt
->rt_gateway
)->sdl_index
== ifp
->if_index
) {
1064 lck_mtx_unlock(rnh_lock
);
1066 * See comments on nd6_timer() for reasons why
1067 * this loop is repeated; we bite the costs of
1068 * going thru the same llinfo_nd6 more than once
1069 * here, since this purge happens during detach,
1070 * and that unlike the timer case, it's possible
1071 * there's more than one purges happening at the
1072 * same time (thus a flag wouldn't buy anything).
1075 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1082 lck_mtx_unlock(rnh_lock
);
1086 * Upon success, the returned route will be locked and the caller is
1087 * responsible for releasing the reference and doing RT_UNLOCK(rt).
1088 * This routine does not require rnh_lock to be held by the caller,
1089 * although it needs to be indicated of such a case in order to call
1090 * the correct variant of the relevant routing routines.
1094 struct in6_addr
*addr6
,
1100 struct sockaddr_in6 sin6
;
1102 bzero(&sin6
, sizeof(sin6
));
1103 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1104 sin6
.sin6_family
= AF_INET6
;
1105 sin6
.sin6_addr
= *addr6
;
1107 sin6
.sin6_scope_id
= in6_addr2scopeid(ifp
, addr6
);
1110 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1112 rt
= rt_locked
? rtalloc1_locked((struct sockaddr
*)&sin6
, create
, 0) :
1113 rtalloc1((struct sockaddr
*)&sin6
, create
, 0);
1117 if ((rt
->rt_flags
& RTF_LLINFO
) == 0) {
1119 * This is the case for the default route. If we
1120 * want to create a neighbor cache for the address,
1121 * we should free the route for the destination and
1122 * allocate an interface route.
1135 if (create
&& ifp
) {
1140 * If no route is available and create is set,
1141 * we allocate a host route for the destination
1142 * and treat it like an interface route.
1143 * This hack is necessary for a neighbor which can't
1144 * be covered by our own prefix.
1146 ifa
= ifaof_ifpforaddr((struct sockaddr
*)&sin6
, ifp
);
1151 * Create a new route. RTF_LLINFO is necessary
1152 * to create a Neighbor Cache entry for the
1153 * destination in nd6_rtrequest which will be
1154 * called in rtrequest via ifa->ifa_rtrequest.
1157 lck_mtx_lock(rnh_lock
);
1158 if ((e
= rtrequest_locked(RTM_ADD
,
1159 (struct sockaddr
*)&sin6
, ifa
->ifa_addr
,
1160 (struct sockaddr
*)&all1_sa
,
1161 (ifa
->ifa_flags
| RTF_HOST
| RTF_LLINFO
) &
1162 ~RTF_CLONING
, &rt
)) != 0) {
1164 log(LOG_ERR
, "%s: failed to add route "
1165 "for a neighbor(%s), errno=%d\n",
1166 __func__
, ip6_sprintf(addr6
), e
);
1169 lck_mtx_unlock(rnh_lock
);
1175 if (rt
->rt_llinfo
) {
1176 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
1177 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1183 RT_LOCK_ASSERT_HELD(rt
);
1185 * Validation for the entry.
1186 * Note that the check for rt_llinfo is necessary because a cloned
1187 * route from a parent route that has the L flag (e.g. the default
1188 * route to a p2p interface) may have the flag, too, while the
1189 * destination is not actually a neighbor.
1190 * XXX: we can't use rt->rt_ifp to check for the interface, since
1191 * it might be the loopback interface if the entry is for our
1192 * own address on a non-loopback interface. Instead, we should
1193 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
1196 if (((ifp
&& (ifp
->if_type
!= IFT_PPP
)) && ((ifp
->if_eflags
& IFEF_NOAUTOIPV6LL
) == 0)) &&
1197 ((rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1198 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
1199 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
))) {
1200 RT_REMREF_LOCKED(rt
);
1203 log(LOG_DEBUG
, "%s: failed to lookup %s "
1204 "(if = %s)\n", __func__
, ip6_sprintf(addr6
),
1205 ifp
? if_name(ifp
) : "unspec");
1206 /* xxx more logs... kazu */
1211 * Caller needs to release reference and call RT_UNLOCK(rt).
1217 * Detect if a given IPv6 address identifies a neighbor on a given link.
1218 * XXX: should take care of the destination of a p2p link?
1221 nd6_is_addr_neighbor(
1222 struct sockaddr_in6
*addr
,
1230 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
1231 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
1234 * A link-local address is always a neighbor.
1235 * XXX: we should use the sin6_scope_id field rather than the embedded
1238 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
) &&
1239 ntohs(*(u_int16_t
*)&addr
->sin6_addr
.s6_addr
[2]) == ifp
->if_index
)
1243 * If the address matches one of our addresses,
1244 * it should be a neighbor.
1246 ifnet_lock_shared(ifp
);
1247 for (ifa
= ifp
->if_addrlist
.tqh_first
;
1249 ifa
= ifa
->ifa_list
.tqe_next
)
1251 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1254 for (i
= 0; i
< 4; i
++) {
1255 if ((IFADDR6(ifa
).s6_addr32
[i
] ^
1256 addr
->sin6_addr
.s6_addr32
[i
]) &
1257 IFMASK6(ifa
).s6_addr32
[i
])
1260 ifnet_lock_done(ifp
);
1263 ifnet_lock_done(ifp
);
1266 * Even if the address matches none of our addresses, it might be
1267 * in the neighbor cache. Callee returns a locked route upon
1270 if ((rt
= nd6_lookup(&addr
->sin6_addr
, 0, ifp
, rt_locked
)) != NULL
) {
1271 RT_LOCK_ASSERT_HELD(rt
);
1272 RT_REMREF_LOCKED(rt
);
1283 * Free an nd6 llinfo entry.
1289 struct llinfo_nd6
*ln
;
1290 struct in6_addr in6
;
1291 struct nd_defrouter
*dr
;
1293 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1294 RT_LOCK_ASSERT_NOTHELD(rt
);
1295 lck_mtx_lock(nd6_mutex
);
1298 RT_ADDREF_LOCKED(rt
); /* Extra ref */
1300 in6
= ((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
1303 * Prevent another thread from modifying rt_key, rt_gateway
1304 * via rt_setgate() after the rt_lock is dropped by marking
1305 * the route as defunct.
1307 rt
->rt_flags
|= RTF_CONDEMNED
;
1310 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1311 * even though it is not harmful, it was not really necessary.
1314 if (!ip6_forwarding
&& (ip6_accept_rtadv
||
1315 (rt
->rt_ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
1316 dr
= defrouter_lookup(&((struct sockaddr_in6
*)rt_key(rt
))->
1317 sin6_addr
, rt
->rt_ifp
);
1319 if ((ln
&& ln
->ln_router
) || dr
) {
1321 * rt6_flush must be called whether or not the neighbor
1322 * is in the Default Router List.
1323 * See a corresponding comment in nd6_na_input().
1326 rt6_flush(&in6
, rt
->rt_ifp
);
1333 * Unreachablity of a router might affect the default
1334 * router selection and on-link detection of advertised
1339 * Temporarily fake the state to choose a new default
1340 * router and to perform on-link determination of
1341 * prefixes correctly.
1342 * Below the state will be set correctly,
1343 * or the entry itself will be deleted.
1346 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1349 * Since defrouter_select() does not affect the
1350 * on-link determination and MIP6 needs the check
1351 * before the default router selection, we perform
1355 pfxlist_onlink_check(1);
1357 if (dr
== TAILQ_FIRST(&nd_defrouter
)) {
1359 * It is used as the current default router,
1360 * so we have to move it to the end of the
1361 * list and choose a new one.
1362 * XXX: it is not very efficient if this is
1365 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1366 TAILQ_INSERT_TAIL(&nd_defrouter
, dr
, dr_entry
);
1371 RT_LOCK_ASSERT_NOTHELD(rt
);
1376 lck_mtx_unlock(nd6_mutex
);
1378 * Detach the route from the routing tree and the list of neighbor
1379 * caches, and disable the route entry not to be used in already
1382 (void) rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
1383 rt_mask(rt
), 0, (struct rtentry
**)0);
1385 /* Extra ref held above; now free it */
1390 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1392 * XXX cost-effective metods?
1397 struct in6_addr
*dst6
,
1400 struct llinfo_nd6
*ln
;
1401 struct timeval timenow
;
1403 getmicrotime(&timenow
);
1406 * If the caller specified "rt", use that. Otherwise, resolve the
1407 * routing table by supplied "dst6".
1412 /* Callee returns a locked route upon success */
1413 if ((rt
= nd6_lookup(dst6
, 0, NULL
, 0)) == NULL
)
1415 RT_LOCK_ASSERT_HELD(rt
);
1418 RT_ADDREF_LOCKED(rt
);
1421 if ((rt
->rt_flags
& RTF_GATEWAY
) != 0 ||
1422 (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1423 !rt
->rt_llinfo
|| !rt
->rt_gateway
||
1424 rt
->rt_gateway
->sa_family
!= AF_LINK
) {
1425 /* This is not a host route. */
1430 if (ln
->ln_state
< ND6_LLINFO_REACHABLE
)
1434 * if we get upper-layer reachability confirmation many times,
1435 * it is possible we have false information.
1439 if (ln
->ln_byhint
> nd6_maxnudhint
)
1443 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1444 if (ln
->ln_expire
) {
1445 lck_rw_lock_shared(nd_if_rwlock
);
1446 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
,
1447 nd_ifinfo
[rt
->rt_ifp
->if_index
].reachable
);
1448 lck_rw_done(nd_if_rwlock
);
1451 RT_REMREF_LOCKED(rt
);
1459 __unused
struct sockaddr
*sa
)
1461 struct sockaddr
*gate
= rt
->rt_gateway
;
1462 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
1463 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
, 0, 0, 0, 0, 0,
1464 {0,0,0,0,0,0,0,0,0,0,0,0,} };
1465 struct ifnet
*ifp
= rt
->rt_ifp
;
1467 struct timeval timenow
;
1469 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1470 RT_LOCK_ASSERT_HELD(rt
);
1472 if ((rt
->rt_flags
& RTF_GATEWAY
))
1475 if (nd6_need_cache(ifp
) == 0 && (rt
->rt_flags
& RTF_HOST
) == 0) {
1477 * This is probably an interface direct route for a link
1478 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1479 * We do not need special treatment below for such a route.
1480 * Moreover, the RTF_LLINFO flag which would be set below
1481 * would annoy the ndp(8) command.
1486 if (req
== RTM_RESOLVE
) {
1489 if (!nd6_need_cache(ifp
)) { /* stf case */
1493 * nd6_is_addr_neighbor() may call nd6_lookup(),
1494 * therefore we drop rt_lock to avoid deadlock
1495 * during the lookup. Using rt_key(rt) is still
1496 * safe because it won't change while rnh_lock
1499 RT_ADDREF_LOCKED(rt
);
1501 no_nd_cache
= !nd6_is_addr_neighbor(
1502 (struct sockaddr_in6
*)rt_key(rt
), ifp
, 1);
1504 RT_REMREF_LOCKED(rt
);
1508 * FreeBSD and BSD/OS often make a cloned host route based
1509 * on a less-specific route (e.g. the default route).
1510 * If the less specific route does not have a "gateway"
1511 * (this is the case when the route just goes to a p2p or an
1512 * stf interface), we'll mistakenly make a neighbor cache for
1513 * the host route, and will see strange neighbor solicitation
1514 * for the corresponding destination. In order to avoid the
1515 * confusion, we check if the destination of the route is
1516 * a neighbor in terms of neighbor discovery, and stop the
1517 * process if not. Additionally, we remove the LLINFO flag
1518 * so that ndp(8) will not try to get the neighbor information
1519 * of the destination.
1522 rt
->rt_flags
&= ~RTF_LLINFO
;
1527 getmicrotime(&timenow
);
1531 * There is no backward compatibility :)
1533 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1534 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1535 * rt->rt_flags |= RTF_CLONING;
1537 if (rt
->rt_flags
& (RTF_CLONING
| RTF_LLINFO
)) {
1539 * Case 1: This route should come from
1540 * a route to interface. RTF_LLINFO flag is set
1541 * for a host route whose destination should be
1542 * treated as on-link.
1544 if (rt_setgate(rt
, rt_key(rt
),
1545 (struct sockaddr
*)&null_sdl
) == 0) {
1546 gate
= rt
->rt_gateway
;
1547 SDL(gate
)->sdl_type
= ifp
->if_type
;
1548 SDL(gate
)->sdl_index
= ifp
->if_index
;
1550 * In case we're called before 1.0 sec.
1554 ln
->ln_expire
= MAX(timenow
.tv_sec
, 1);
1556 if ((rt
->rt_flags
& RTF_CLONING
))
1560 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1561 * We don't do that here since llinfo is not ready yet.
1563 * There are also couple of other things to be discussed:
1564 * - unsolicited NA code needs improvement beforehand
1565 * - RFC2461 says we MAY send multicast unsolicited NA
1566 * (7.2.6 paragraph 4), however, it also says that we
1567 * SHOULD provide a mechanism to prevent multicast NA storm.
1568 * we don't have anything like it right now.
1569 * note that the mechanism needs a mutual agreement
1570 * between proxies, which means that we need to implement
1571 * a new protocol, or a new kludge.
1572 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1573 * we need to check ip6forwarding before sending it.
1574 * (or should we allow proxy ND configuration only for
1575 * routers? there's no mention about proxy ND from hosts)
1578 /* XXX it does not work */
1579 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1581 &SIN6(rt_key(rt
))->sin6_addr
,
1582 &SIN6(rt_key(rt
))->sin6_addr
,
1583 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1588 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) == 0) {
1590 * Address resolution isn't necessary for a point to
1591 * point link, so we can skip this test for a p2p link.
1593 if (gate
->sa_family
!= AF_LINK
||
1594 gate
->sa_len
< sizeof(null_sdl
)) {
1596 "nd6_rtrequest: bad gateway value: %s\n",
1600 SDL(gate
)->sdl_type
= ifp
->if_type
;
1601 SDL(gate
)->sdl_index
= ifp
->if_index
;
1604 break; /* This happens on a route change */
1606 * Case 2: This route may come from cloning, or a manual route
1607 * add with a LL address.
1609 rt
->rt_llinfo
= ln
= nd6_llinfo_alloc();
1611 log(LOG_DEBUG
, "nd6_rtrequest: malloc failed\n");
1614 rt
->rt_llinfo_free
= nd6_llinfo_free
;
1618 Bzero(ln
, sizeof(*ln
));
1620 /* this is required for "ndp" command. - shin */
1621 if (req
== RTM_ADD
) {
1623 * gate should have some valid AF_LINK entry,
1624 * and ln->ln_expire should have some lifetime
1625 * which is specified by ndp command.
1627 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1631 * When req == RTM_RESOLVE, rt is created and
1632 * initialized in rtrequest(), so rt_expire is 0.
1634 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1635 /* In case we're called before 1.0 sec. has elapsed */
1636 ln
->ln_expire
= MAX(timenow
.tv_sec
, 1);
1638 rt
->rt_flags
|= RTF_LLINFO
;
1642 * If we have too many cache entries, initiate immediate
1643 * purging for some "less recently used" entries. Note that
1644 * we cannot directly call nd6_free() here because it would
1645 * cause re-entering rtable related routines triggering an LOR
1648 if (ip6_neighborgcthresh
>= 0 &&
1649 nd6_inuse
>= ip6_neighborgcthresh
) {
1652 for (i
= 0; i
< 10 && llinfo_nd6
.ln_prev
!= ln
; i
++) {
1653 struct llinfo_nd6
*ln_end
= llinfo_nd6
.ln_prev
;
1654 struct rtentry
*rt_end
= ln_end
->ln_rt
;
1656 /* Move this entry to the head */
1659 LN_INSERTHEAD(ln_end
);
1661 if (ln_end
->ln_expire
== 0) {
1665 if (ln_end
->ln_state
> ND6_LLINFO_INCOMPLETE
)
1666 ln_end
->ln_state
= ND6_LLINFO_STALE
;
1668 ln_end
->ln_state
= ND6_LLINFO_PURGE
;
1669 ln_end
->ln_expire
= timenow
.tv_sec
;
1675 * check if rt_key(rt) is one of my address assigned
1678 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1679 &SIN6(rt_key(rt
))->sin6_addr
);
1681 caddr_t macp
= nd6_ifptomac(ifp
);
1683 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1686 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
1687 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
1689 if (nd6_useloopback
) {
1690 #if IFNET_ROUTE_REFCNT
1691 /* Adjust route ref count for the interfaces */
1692 if (rt
->rt_if_ref_fn
!= NULL
&&
1693 rt
->rt_ifp
!= lo_ifp
) {
1694 rt
->rt_if_ref_fn(lo_ifp
, 1);
1695 rt
->rt_if_ref_fn(rt
->rt_ifp
, -1);
1697 #endif /* IFNET_ROUTE_REFCNT */
1698 rt
->rt_ifp
= lo_ifp
; /* XXX */
1700 * Make sure rt_ifa be equal to the ifaddr
1701 * corresponding to the address.
1702 * We need this because when we refer
1703 * rt_ifa->ia6_flags in ip6_input, we assume
1704 * that the rt_ifa points to the address instead
1705 * of the loopback address.
1707 if (ifa
!= rt
->rt_ifa
) {
1712 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
1714 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1717 /* join solicited node multicast for proxy ND */
1718 if (ifp
->if_flags
& IFF_MULTICAST
) {
1719 struct in6_addr llsol
;
1722 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1723 llsol
.s6_addr16
[0] = htons(0xff02);
1724 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1725 llsol
.s6_addr32
[1] = 0;
1726 llsol
.s6_addr32
[2] = htonl(1);
1727 llsol
.s6_addr8
[12] = 0xff;
1729 if (!in6_addmulti(&llsol
, ifp
, &error
, 0)) {
1730 nd6log((LOG_ERR
, "%s: failed to join "
1731 "%s (errno=%d)\n", if_name(ifp
),
1732 ip6_sprintf(&llsol
), error
));
1741 /* leave from solicited node multicast for proxy ND */
1742 if ((rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
1743 (ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1744 struct in6_addr llsol
;
1745 struct in6_multi
*in6m
;
1747 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1748 llsol
.s6_addr16
[0] = htons(0xff02);
1749 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1750 llsol
.s6_addr32
[1] = 0;
1751 llsol
.s6_addr32
[2] = htonl(1);
1752 llsol
.s6_addr8
[12] = 0xff;
1754 ifnet_lock_shared(ifp
);
1755 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1756 ifnet_lock_done(ifp
);
1758 in6_delmulti(in6m
, 0);
1762 * Unchain it but defer the actual freeing until the route
1763 * itself is to be freed. rt->rt_llinfo still points to
1764 * llinfo_nd6, and likewise, ln->ln_rt stil points to this
1765 * route entry, except that RTF_LLINFO is now cleared.
1767 if (ln
->ln_flags
& ND6_LNF_IN_USE
)
1769 rt
->rt_flags
&= ~RTF_LLINFO
;
1770 if (ln
->ln_hold
!= NULL
)
1771 m_freem(ln
->ln_hold
);
1777 nd6_siocgdrlst(void *data
, int data_is_64
)
1779 struct in6_drlist_64
*drl_64
= (struct in6_drlist_64
*)data
;
1780 struct in6_drlist_32
*drl_32
= (struct in6_drlist_32
*)data
;
1781 struct nd_defrouter
*dr
;
1784 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1786 bzero(data
, data_is_64
? sizeof (*drl_64
) : sizeof (*drl_32
));
1787 dr
= TAILQ_FIRST(&nd_defrouter
);
1789 /* For 64-bit process */
1790 while (dr
&& i
< DRLSTSIZ
) {
1791 drl_64
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1792 if (IN6_IS_ADDR_LINKLOCAL(&drl_64
->defrouter
[i
].rtaddr
)) {
1793 /* XXX: need to this hack for KAME stack */
1794 drl_64
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1797 "default router list contains a "
1798 "non-linklocal address(%s)\n",
1799 ip6_sprintf(&drl_64
->defrouter
[i
].rtaddr
));
1801 drl_64
->defrouter
[i
].flags
= dr
->flags
;
1802 drl_64
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1803 drl_64
->defrouter
[i
].expire
= dr
->expire
;
1804 drl_64
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1806 dr
= TAILQ_NEXT(dr
, dr_entry
);
1810 /* For 32-bit process */
1811 while (dr
&& i
< DRLSTSIZ
) {
1812 drl_32
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1813 if (IN6_IS_ADDR_LINKLOCAL(&drl_32
->defrouter
[i
].rtaddr
)) {
1814 /* XXX: need to this hack for KAME stack */
1815 drl_32
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1818 "default router list contains a "
1819 "non-linklocal address(%s)\n",
1820 ip6_sprintf(&drl_32
->defrouter
[i
].rtaddr
));
1822 drl_32
->defrouter
[i
].flags
= dr
->flags
;
1823 drl_32
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1824 drl_32
->defrouter
[i
].expire
= dr
->expire
;
1825 drl_32
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1827 dr
= TAILQ_NEXT(dr
, dr_entry
);
1832 nd6_siocgprlst(void *data
, int data_is_64
)
1834 struct in6_prlist_64
*prl_64
= (struct in6_prlist_64
*)data
;
1835 struct in6_prlist_32
*prl_32
= (struct in6_prlist_32
*)data
;
1836 struct nd_prefix
*pr
;
1837 struct rr_prefix
*rpp
;
1840 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1842 * XXX meaning of fields, especialy "raflags", is very
1843 * differnet between RA prefix list and RR/static prefix list.
1844 * how about separating ioctls into two?
1846 bzero(data
, data_is_64
? sizeof (*prl_64
) : sizeof (*prl_32
));
1847 pr
= nd_prefix
.lh_first
;
1849 /* For 64-bit process */
1850 while (pr
&& i
< PRLSTSIZ
) {
1851 struct nd_pfxrouter
*pfr
;
1854 (void) in6_embedscope(&prl_64
->prefix
[i
].prefix
,
1855 &pr
->ndpr_prefix
, NULL
, NULL
);
1856 prl_64
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1857 prl_64
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1858 prl_64
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1859 prl_64
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1860 prl_64
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1861 prl_64
->prefix
[i
].expire
= pr
->ndpr_expire
;
1863 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1867 #define RTRADDR prl_64->prefix[i].advrtr[j]
1868 RTRADDR
= pfr
->router
->rtaddr
;
1869 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1870 /* XXX: hack for KAME */
1871 RTRADDR
.s6_addr16
[1] = 0;
1874 "a router(%s) advertises "
1876 "non-link local address\n",
1877 ip6_sprintf(&RTRADDR
));
1882 pfr
= pfr
->pfr_next
;
1884 prl_64
->prefix
[i
].advrtrs
= j
;
1885 prl_64
->prefix
[i
].origin
= PR_ORIG_RA
;
1891 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1892 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1895 (void) in6_embedscope(&prl_64
->prefix
[i
].prefix
,
1896 &pr
->ndpr_prefix
, NULL
, NULL
);
1897 prl_64
->prefix
[i
].raflags
= rpp
->rp_raf
;
1898 prl_64
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1899 prl_64
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1900 prl_64
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1901 prl_64
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1902 prl_64
->prefix
[i
].expire
= rpp
->rp_expire
;
1903 prl_64
->prefix
[i
].advrtrs
= 0;
1904 prl_64
->prefix
[i
].origin
= rpp
->rp_origin
;
1909 /* For 32-bit process */
1910 while (pr
&& i
< PRLSTSIZ
) {
1911 struct nd_pfxrouter
*pfr
;
1914 (void) in6_embedscope(&prl_32
->prefix
[i
].prefix
,
1915 &pr
->ndpr_prefix
, NULL
, NULL
);
1916 prl_32
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1917 prl_32
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1918 prl_32
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1919 prl_32
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1920 prl_32
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1921 prl_32
->prefix
[i
].expire
= pr
->ndpr_expire
;
1923 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1927 #define RTRADDR prl_32->prefix[i].advrtr[j]
1928 RTRADDR
= pfr
->router
->rtaddr
;
1929 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1930 /* XXX: hack for KAME */
1931 RTRADDR
.s6_addr16
[1] = 0;
1934 "a router(%s) advertises "
1936 "non-link local address\n",
1937 ip6_sprintf(&RTRADDR
));
1942 pfr
= pfr
->pfr_next
;
1944 prl_32
->prefix
[i
].advrtrs
= j
;
1945 prl_32
->prefix
[i
].origin
= PR_ORIG_RA
;
1951 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1952 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1955 (void) in6_embedscope(&prl_32
->prefix
[i
].prefix
,
1956 &pr
->ndpr_prefix
, NULL
, NULL
);
1957 prl_32
->prefix
[i
].raflags
= rpp
->rp_raf
;
1958 prl_32
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1959 prl_32
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1960 prl_32
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1961 prl_32
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1962 prl_32
->prefix
[i
].expire
= rpp
->rp_expire
;
1963 prl_32
->prefix
[i
].advrtrs
= 0;
1964 prl_32
->prefix
[i
].origin
= rpp
->rp_origin
;
1970 nd6_ioctl(u_long cmd
, caddr_t data
, struct ifnet
*ifp
)
1972 struct in6_ndireq
*ndi
= (struct in6_ndireq
*)data
;
1973 struct in6_ondireq
*ondi
= (struct in6_ondireq
*)data
;
1974 struct nd_defrouter
*dr
, any
;
1975 struct nd_prefix
*pr
;
1977 int i
= ifp
->if_index
, error
= 0;
1980 case SIOCGDRLST_IN6_32
:
1981 case SIOCGDRLST_IN6_64
:
1983 * obsolete API, use sysctl under net.inet6.icmp6
1985 lck_mtx_lock(nd6_mutex
);
1986 nd6_siocgdrlst(data
, cmd
== SIOCGDRLST_IN6_64
);
1987 lck_mtx_unlock(nd6_mutex
);
1990 case SIOCGPRLST_IN6_32
:
1991 case SIOCGPRLST_IN6_64
:
1993 * obsolete API, use sysctl under net.inet6.icmp6
1995 lck_mtx_lock(nd6_mutex
);
1996 nd6_siocgprlst(data
, cmd
== SIOCGPRLST_IN6_64
);
1997 lck_mtx_unlock(nd6_mutex
);
2000 case OSIOCGIFINFO_IN6
:
2001 case SIOCGIFINFO_IN6
:
2003 * SIOCGIFINFO_IN6 ioctl is encoded with in6_ondireq
2004 * instead of in6_ndireq, so we treat it as such.
2006 lck_rw_lock_shared(nd_if_rwlock
);
2007 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
2008 lck_rw_done(nd_if_rwlock
);
2012 ondi
->ndi
.linkmtu
= IN6_LINKMTU(ifp
);
2013 ondi
->ndi
.maxmtu
= nd_ifinfo
[i
].maxmtu
;
2014 ondi
->ndi
.basereachable
= nd_ifinfo
[i
].basereachable
;
2015 ondi
->ndi
.reachable
= nd_ifinfo
[i
].reachable
;
2016 ondi
->ndi
.retrans
= nd_ifinfo
[i
].retrans
;
2017 ondi
->ndi
.flags
= nd_ifinfo
[i
].flags
;
2018 ondi
->ndi
.recalctm
= nd_ifinfo
[i
].recalctm
;
2019 ondi
->ndi
.chlim
= nd_ifinfo
[i
].chlim
;
2020 ondi
->ndi
.receivedra
= nd_ifinfo
[i
].receivedra
;
2021 lck_rw_done(nd_if_rwlock
);
2024 case SIOCSIFINFO_FLAGS
:
2025 /* XXX: almost all other fields of ndi->ndi is unused */
2026 lck_rw_lock_shared(nd_if_rwlock
);
2027 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
2028 lck_rw_done(nd_if_rwlock
);
2032 nd_ifinfo
[i
].flags
= ndi
->ndi
.flags
;
2033 lck_rw_done(nd_if_rwlock
);
2036 case SIOCSNDFLUSH_IN6
: /* XXX: the ioctl name is confusing... */
2037 /* flush default router list */
2039 * xxx sumikawa: should not delete route if default
2040 * route equals to the top of default router list
2042 bzero(&any
, sizeof(any
));
2043 lck_mtx_lock(nd6_mutex
);
2044 defrouter_delreq(&any
, 1);
2046 lck_mtx_unlock(nd6_mutex
);
2047 /* xxx sumikawa: flush prefix list */
2050 case SIOCSPFXFLUSH_IN6
: {
2051 /* flush all the prefix advertised by routers */
2052 struct nd_prefix
*next
;
2053 lck_mtx_lock(nd6_mutex
);
2055 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
2056 struct in6_ifaddr
*ia
, *ia_next
;
2058 next
= pr
->ndpr_next
;
2060 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
))
2063 /* do we really have to remove addresses as well? */
2064 for (ia
= in6_ifaddrs
; ia
; ia
= ia_next
) {
2065 /* ia might be removed. keep the next ptr. */
2066 ia_next
= ia
->ia_next
;
2068 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
2071 if (ia
->ia6_ndpr
== pr
)
2072 in6_purgeaddr(&ia
->ia_ifa
, 1);
2074 prelist_remove(pr
, 1);
2076 lck_mtx_unlock(nd6_mutex
);
2080 case SIOCSRTRFLUSH_IN6
: {
2081 /* flush all the default routers */
2082 struct nd_defrouter
*next
;
2084 lck_mtx_lock(nd6_mutex
);
2085 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
2087 * The first entry of the list may be stored in
2088 * the routing table, so we'll delete it later.
2090 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
2091 next
= TAILQ_NEXT(dr
, dr_entry
);
2092 defrtrlist_del(dr
, 1);
2094 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
), 1);
2096 lck_mtx_unlock(nd6_mutex
);
2100 case SIOCGNBRINFO_IN6_32
: {
2101 struct llinfo_nd6
*ln
;
2102 struct in6_nbrinfo_32
*nbi_32
= (struct in6_nbrinfo_32
*)data
;
2103 /* make local for safety */
2104 struct in6_addr nb_addr
= nbi_32
->addr
;
2107 * XXX: KAME specific hack for scoped addresses
2108 * XXXX: for other scopes than link-local?
2110 if (IN6_IS_ADDR_LINKLOCAL(&nbi_32
->addr
) ||
2111 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_32
->addr
)) {
2112 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
2115 *idp
= htons(ifp
->if_index
);
2118 /* Callee returns a locked route upon success */
2119 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
2123 RT_LOCK_ASSERT_HELD(rt
);
2125 nbi_32
->state
= ln
->ln_state
;
2126 nbi_32
->asked
= ln
->ln_asked
;
2127 nbi_32
->isrouter
= ln
->ln_router
;
2128 nbi_32
->expire
= ln
->ln_expire
;
2129 RT_REMREF_LOCKED(rt
);
2134 case SIOCGNBRINFO_IN6_64
: {
2135 struct llinfo_nd6
*ln
;
2136 struct in6_nbrinfo_64
*nbi_64
= (struct in6_nbrinfo_64
*)data
;
2137 /* make local for safety */
2138 struct in6_addr nb_addr
= nbi_64
->addr
;
2141 * XXX: KAME specific hack for scoped addresses
2142 * XXXX: for other scopes than link-local?
2144 if (IN6_IS_ADDR_LINKLOCAL(&nbi_64
->addr
) ||
2145 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_64
->addr
)) {
2146 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
2149 *idp
= htons(ifp
->if_index
);
2152 /* Callee returns a locked route upon success */
2153 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
2157 RT_LOCK_ASSERT_HELD(rt
);
2159 nbi_64
->state
= ln
->ln_state
;
2160 nbi_64
->asked
= ln
->ln_asked
;
2161 nbi_64
->isrouter
= ln
->ln_router
;
2162 nbi_64
->expire
= ln
->ln_expire
;
2163 RT_REMREF_LOCKED(rt
);
2168 case SIOCGDEFIFACE_IN6_32
: /* XXX: should be implemented as a sysctl? */
2169 case SIOCGDEFIFACE_IN6_64
: {
2170 struct in6_ndifreq_64
*ndif_64
= (struct in6_ndifreq_64
*)data
;
2171 struct in6_ndifreq_32
*ndif_32
= (struct in6_ndifreq_32
*)data
;
2173 if (cmd
== SIOCGDEFIFACE_IN6_64
)
2174 ndif_64
->ifindex
= nd6_defifindex
;
2176 ndif_32
->ifindex
= nd6_defifindex
;
2180 case SIOCSDEFIFACE_IN6_32
: /* XXX: should be implemented as a sysctl? */
2181 case SIOCSDEFIFACE_IN6_64
: {
2182 struct in6_ndifreq_64
*ndif_64
= (struct in6_ndifreq_64
*)data
;
2183 struct in6_ndifreq_32
*ndif_32
= (struct in6_ndifreq_32
*)data
;
2185 return (nd6_setdefaultiface(cmd
== SIOCSDEFIFACE_IN6_64
?
2186 ndif_64
->ifindex
: ndif_32
->ifindex
));
2194 * Create neighbor cache entry and cache link-layer address,
2195 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
2200 struct in6_addr
*from
,
2202 __unused
int lladdrlen
,
2203 int type
, /* ICMP6 type */
2204 int code
) /* type dependent information */
2206 struct rtentry
*rt
= NULL
;
2207 struct llinfo_nd6
*ln
= NULL
;
2209 struct sockaddr_dl
*sdl
= NULL
;
2214 struct timeval timenow
;
2217 panic("ifp == NULL in nd6_cache_lladdr");
2219 panic("from == NULL in nd6_cache_lladdr");
2221 /* nothing must be updated for unspecified address */
2222 if (IN6_IS_ADDR_UNSPECIFIED(from
))
2226 * Validation about ifp->if_addrlen and lladdrlen must be done in
2229 * XXX If the link does not have link-layer adderss, what should
2230 * we do? (ifp->if_addrlen == 0)
2231 * Spec says nothing in sections for RA, RS and NA. There's small
2232 * description on it in NS section (RFC 2461 7.2.3).
2234 getmicrotime(&timenow
);
2236 rt
= nd6_lookup(from
, 0, ifp
, 0);
2239 /* nothing must be done if there's no lladdr */
2240 if (!lladdr
|| !lladdrlen
)
2244 if ((rt
= nd6_lookup(from
, 1, ifp
, 0)) == NULL
)
2246 RT_LOCK_ASSERT_HELD(rt
);
2249 RT_LOCK_ASSERT_HELD(rt
);
2250 /* do nothing if static ndp is set */
2251 if (rt
->rt_flags
& RTF_STATIC
) {
2252 RT_REMREF_LOCKED(rt
);
2259 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
2269 if (!rt
->rt_gateway
)
2271 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
2273 sdl
= SDL(rt
->rt_gateway
);
2275 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
2276 if (olladdr
&& lladdr
) {
2277 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
2285 * newentry olladdr lladdr llchange (*=record)
2288 * 0 n y -- (3) * STALE
2290 * 0 y y y (5) * STALE
2291 * 1 -- n -- (6) NOSTATE(= PASSIVE)
2292 * 1 -- y -- (7) * STALE
2295 if (lladdr
) { /* (3-5) and (7) */
2297 * Record source link-layer address
2298 * XXX is it dependent to ifp->if_type?
2300 sdl
->sdl_alen
= ifp
->if_addrlen
;
2301 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
2305 if ((!olladdr
&& lladdr
) /* (3) */
2306 || (olladdr
&& lladdr
&& llchange
)) { /* (5) */
2308 newstate
= ND6_LLINFO_STALE
;
2309 } else /* (1-2,4) */
2313 if (!lladdr
) /* (6) */
2314 newstate
= ND6_LLINFO_NOSTATE
;
2316 newstate
= ND6_LLINFO_STALE
;
2321 * Update the state of the neighbor cache.
2323 ln
->ln_state
= newstate
;
2325 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
2326 struct mbuf
*m
= ln
->ln_hold
;
2328 * XXX: since nd6_output() below will cause
2329 * state tansition to DELAY and reset the timer,
2330 * we must set the timer now, although it is actually
2333 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
,
2339 * we assume ifp is not a p2p here, so just
2340 * set the 2nd argument as the 1st one.
2343 nd6_output(ifp
, ifp
, m
,
2344 (struct sockaddr_in6
*)rt_key(rt
), rt
, 0);
2347 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
2348 /* probe right away */
2349 ln
->ln_expire
= timenow
.tv_sec
;
2354 * ICMP6 type dependent behavior.
2356 * NS: clear IsRouter if new entry
2357 * RS: clear IsRouter
2358 * RA: set IsRouter if there's lladdr
2359 * redir: clear IsRouter if new entry
2362 * The spec says that we must set IsRouter in the following cases:
2363 * - If lladdr exist, set IsRouter. This means (1-5).
2364 * - If it is old entry (!newentry), set IsRouter. This means (7).
2365 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
2366 * A quetion arises for (1) case. (1) case has no lladdr in the
2367 * neighbor cache, this is similar to (6).
2368 * This case is rare but we figured that we MUST NOT set IsRouter.
2370 * newentry olladdr lladdr llchange NS RS RA redir
2372 * 0 n n -- (1) c ? s
2373 * 0 y n -- (2) c s s
2374 * 0 n y -- (3) c s s
2377 * 1 -- n -- (6) c c c s
2378 * 1 -- y -- (7) c c s c s
2382 switch (type
& 0xff) {
2383 case ND_NEIGHBOR_SOLICIT
:
2385 * New entry must have is_router flag cleared.
2387 if (is_newentry
) /* (6-7) */
2392 * If the icmp is a redirect to a better router, always set the
2393 * is_router flag. Otherwise, if the entry is newly created,
2394 * clear the flag. [RFC 2461, sec 8.3]
2396 if (code
== ND_REDIRECT_ROUTER
)
2398 else if (is_newentry
) /* (6-7) */
2401 case ND_ROUTER_SOLICIT
:
2403 * is_router flag must always be cleared.
2407 case ND_ROUTER_ADVERT
:
2409 * Mark an entry with lladdr as a router.
2411 if ((!is_newentry
&& (olladdr
|| lladdr
)) /* (2-5) */
2412 || (is_newentry
&& lladdr
)) { /* (7) */
2419 * When the link-layer address of a router changes, select the
2420 * best router again. In particular, when the neighbor entry is newly
2421 * created, it might affect the selection policy.
2422 * Question: can we restrict the first condition to the "is_newentry"
2424 * XXX: when we hear an RA from a new router with the link-layer
2425 * address option, defrouter_select() is called twice, since
2426 * defrtrlist_update called the function as well. However, I believe
2427 * we can compromise the overhead, since it only happens the first
2429 * XXX: although defrouter_select() should not have a bad effect
2430 * for those are not autoconfigured hosts, we explicitly avoid such
2433 if (do_update
&& ln
->ln_router
&& !ip6_forwarding
&&
2434 (ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
2435 RT_REMREF_LOCKED(rt
);
2437 lck_mtx_lock(nd6_mutex
);
2439 lck_mtx_unlock(nd6_mutex
);
2441 RT_REMREF_LOCKED(rt
);
2448 __unused
void *ignored_arg
)
2451 struct nd_ifinfo
*nd6if
;
2453 lck_rw_lock_shared(nd_if_rwlock
);
2454 for (i
= 1; i
< if_index
+ 1; i
++) {
2455 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
)
2457 nd6if
= &nd_ifinfo
[i
];
2458 if (nd6if
->basereachable
&& /* already initialized */
2459 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
2461 * Since reachable time rarely changes by router
2462 * advertisements, we SHOULD insure that a new random
2463 * value gets recomputed at least once every few hours.
2466 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
2467 nd6if
->reachable
= ND_COMPUTE_RTIME(nd6if
->basereachable
);
2470 lck_rw_done(nd_if_rwlock
);
2471 timeout(nd6_slowtimo
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
2474 #define senderr(e) { error = (e); goto bad;}
2476 nd6_output(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m0
,
2477 struct sockaddr_in6
*dst
, struct rtentry
*hint0
, int locked
)
2479 struct mbuf
*m
= m0
;
2480 struct rtentry
*rt
= hint0
, *hint
= hint0
;
2481 struct llinfo_nd6
*ln
= NULL
;
2483 struct timeval timenow
;
2484 struct rtentry
*rtrele
= NULL
;
2488 RT_ADDREF_LOCKED(rt
);
2491 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
) || !nd6_need_cache(ifp
)) {
2498 * Next hop determination. Because we may involve the gateway route
2499 * in addition to the original route, locking is rather complicated.
2500 * The general concept is that regardless of whether the route points
2501 * to the original route or to the gateway route, this routine takes
2502 * an extra reference on such a route. This extra reference will be
2503 * released at the end.
2505 * Care must be taken to ensure that the "hint0" route never gets freed
2506 * via rtfree(), since the caller may have stored it inside a struct
2507 * route with a reference held for that placeholder.
2509 * This logic is similar to, though not exactly the same as the one
2510 * used by arp_route_to_gateway_route().
2514 * We have a reference to "rt" by now (or below via rtalloc1),
2515 * which will either be released or freed at the end of this
2518 RT_LOCK_ASSERT_HELD(rt
);
2519 if (!(rt
->rt_flags
& RTF_UP
)) {
2520 RT_REMREF_LOCKED(rt
);
2522 if ((hint
= rt
= rtalloc1((struct sockaddr
*)dst
,
2525 if (rt
->rt_ifp
!= ifp
) {
2526 /* XXX: loop care? */
2528 error
= nd6_output(ifp
, origifp
, m0
,
2534 senderr(EHOSTUNREACH
);
2538 if (rt
->rt_flags
& RTF_GATEWAY
) {
2539 struct rtentry
*gwrt
;
2540 struct in6_ifaddr
*ia6
= NULL
;
2541 struct sockaddr_in6 gw6
;
2543 gw6
= *((struct sockaddr_in6
*)rt
->rt_gateway
);
2545 * Must drop rt_lock since nd6_is_addr_neighbor()
2546 * calls nd6_lookup() and acquires rnh_lock.
2551 * We skip link-layer address resolution and NUD
2552 * if the gateway is not a neighbor from ND point
2553 * of view, regardless of the value of nd_ifinfo.flags.
2554 * The second condition is a bit tricky; we skip
2555 * if the gateway is our own address, which is
2556 * sometimes used to install a route to a p2p link.
2558 if (!nd6_is_addr_neighbor(&gw6
, ifp
, 0) ||
2559 (ia6
= in6ifa_ifpwithaddr(ifp
, &gw6
.sin6_addr
))) {
2561 * We allow this kind of tricky route only
2562 * when the outgoing interface is p2p.
2563 * XXX: we may need a more generic rule here.
2566 ifafree(&ia6
->ia_ifa
);
2567 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
2568 senderr(EHOSTUNREACH
);
2573 gw6
= *((struct sockaddr_in6
*)rt
->rt_gateway
);
2575 /* If hint is now down, give up */
2576 if (!(rt
->rt_flags
& RTF_UP
)) {
2578 senderr(EHOSTUNREACH
);
2581 /* If there's no gateway route, look it up */
2582 if ((gwrt
= rt
->rt_gwroute
) == NULL
) {
2586 /* Become a regular mutex */
2587 RT_CONVERT_LOCK(rt
);
2590 * Take gwrt's lock while holding route's lock;
2591 * this is okay since gwrt never points back
2592 * to rt, so no lock ordering issues.
2595 if (!(gwrt
->rt_flags
& RTF_UP
)) {
2596 struct rtentry
*ogwrt
;
2598 rt
->rt_gwroute
= NULL
;
2603 gwrt
= rtalloc1((struct sockaddr
*)&gw6
, 1, 0);
2607 * Bail out if the route is down, no route
2608 * to gateway, circular route, or if the
2609 * gateway portion of "rt" has changed.
2611 if (!(rt
->rt_flags
& RTF_UP
) ||
2612 gwrt
== NULL
|| gwrt
== rt
||
2613 !equal(SA(&gw6
), rt
->rt_gateway
)) {
2615 RT_REMREF_LOCKED(gwrt
);
2621 senderr(EHOSTUNREACH
);
2624 /* Remove any existing gwrt */
2625 ogwrt
= rt
->rt_gwroute
;
2626 if ((rt
->rt_gwroute
= gwrt
) != NULL
)
2630 /* Now free the replaced gwrt */
2633 /* If still no route to gateway, bail out */
2635 senderr(EHOSTUNREACH
);
2636 /* Remember to release/free "rt" at the end */
2640 /* If gwrt is now down, give up */
2641 if (!(rt
->rt_flags
& RTF_UP
)) {
2645 /* "rtrele" == original "rt" */
2646 senderr(EHOSTUNREACH
);
2649 RT_ADDREF_LOCKED(gwrt
);
2653 /* If gwrt is now down, give up */
2654 if (!(gwrt
->rt_flags
& RTF_UP
)) {
2657 senderr(EHOSTUNREACH
);
2659 /* Remember to release/free "rt" at the end */
2664 /* Become a regular mutex */
2665 RT_CONVERT_LOCK(rt
);
2669 RT_LOCK_ASSERT_HELD(rt
);
2672 * Address resolution or Neighbor Unreachability Detection
2674 * At this point, the destination of the packet must be a unicast
2675 * or an anycast address(i.e. not a multicast).
2678 /* Look up the neighbor cache for the nexthop */
2679 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0) {
2683 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2684 * the condition below is not very efficient. But we believe
2685 * it is tolerable, because this should be a rare case.
2686 * Must drop rt_lock since nd6_is_addr_neighbor() calls
2687 * nd6_lookup() and acquires rnh_lock.
2691 if (nd6_is_addr_neighbor(dst
, ifp
, 0)) {
2692 /* "rtrele" may have been used, so clean up "rt" now */
2694 /* Don't free "hint0" */
2700 /* Callee returns a locked route upon success */
2701 rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
, 0);
2703 RT_LOCK_ASSERT_HELD(rt
);
2706 } else if (rt
!= NULL
) {
2714 lck_rw_lock_shared(nd_if_rwlock
);
2715 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0 &&
2716 !(nd_ifinfo
[ifp
->if_index
].flags
& ND6_IFF_PERFORMNUD
)) {
2717 lck_rw_done(nd_if_rwlock
);
2719 "nd6_output: can't allocate llinfo for %s "
2721 ip6_sprintf(&dst
->sin6_addr
), ln
, rt
);
2722 senderr(EIO
); /* XXX: good error? */
2724 lck_rw_done(nd_if_rwlock
);
2726 goto sendpkt
; /* send anyway */
2729 getmicrotime(&timenow
);
2731 /* We don't have to do link-layer address resolution on a p2p link. */
2732 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
2733 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
2734 ln
->ln_state
= ND6_LLINFO_STALE
;
2735 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
, nd6_gctimer
);
2739 * The first time we send a packet to a neighbor whose entry is
2740 * STALE, we have to change the state to DELAY and a sets a timer to
2741 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2742 * neighbor unreachability detection on expiration.
2745 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
2747 ln
->ln_state
= ND6_LLINFO_DELAY
;
2748 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
, nd6_delay
);
2752 * If the neighbor cache entry has a state other than INCOMPLETE
2753 * (i.e. its link-layer address is already resolved), just
2756 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) {
2759 * Move this entry to the head of the queue so that it is
2760 * less likely for this entry to be a target of forced
2761 * garbage collection (see nd6_rtrequest()).
2763 lck_mtx_lock(rnh_lock
);
2765 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
2770 lck_mtx_unlock(rnh_lock
);
2775 * There is a neighbor cache entry, but no ethernet address
2776 * response yet. Replace the held mbuf (if any) with this
2779 * This code conforms to the rate-limiting rule described in Section
2780 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
2783 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
)
2784 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
2786 m_freem(ln
->ln_hold
);
2788 if (ln
->ln_expire
&& ln
->ln_asked
< nd6_mmaxtries
&&
2789 ln
->ln_expire
< timenow
.tv_sec
) {
2791 lck_rw_lock_shared(nd_if_rwlock
);
2792 ln
->ln_expire
= timenow
.tv_sec
+
2793 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
2794 lck_rw_done(nd_if_rwlock
);
2796 /* We still have a reference on rt (for ln) */
2797 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0, locked
);
2802 * Move this entry to the head of the queue so that it is
2803 * less likely for this entry to be a target of forced
2804 * garbage collection (see nd6_rtrequest()).
2806 lck_mtx_lock(rnh_lock
);
2808 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
2812 /* Clean up "rt" now while we can */
2814 RT_REMREF_LOCKED(rt
);
2820 rt
= NULL
; /* "rt" has been taken care of */
2821 lck_mtx_unlock(rnh_lock
);
2828 RT_LOCK_ASSERT_NOTHELD(rt
);
2830 /* Clean up HW checksum flags before sending the packet */
2831 m
->m_pkthdr
.csum_data
= 0;
2832 m
->m_pkthdr
.csum_flags
= 0;
2834 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
2835 /* forwarding rules require the original scope_id */
2836 m
->m_pkthdr
.rcvif
= origifp
;
2838 lck_mtx_unlock(ip6_mutex
);
2839 error
= dlil_output(origifp
, PF_INET6
, m
, (caddr_t
)rt
,
2840 (struct sockaddr
*)dst
, 0);
2842 lck_mtx_lock(ip6_mutex
);
2845 /* Do not allow loopback address to wind up on a wire */
2846 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2848 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
2849 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
))) {
2850 ip6stat
.ip6s_badscope
++;
2852 * Do not simply drop the packet just like a
2853 * firewall -- we want the the application to feel
2854 * the pain. Return ENETUNREACH like ip6_output
2855 * does in some similar cases. This can startle
2856 * the otherwise clueless process that specifies
2857 * loopback as the source address.
2859 error
= ENETUNREACH
;
2864 m
->m_pkthdr
.rcvif
= NULL
;
2866 lck_mtx_unlock(ip6_mutex
);
2867 error
= dlil_output(ifp
, PF_INET6
, m
, (caddr_t
)rt
,
2868 (struct sockaddr
*)dst
, 0);
2870 lck_mtx_lock(ip6_mutex
);
2878 /* Clean up "rt" unless it's already been done */
2882 RT_REMREF_LOCKED(rt
);
2889 /* And now clean up "rtrele" if there is any */
2890 if (rtrele
!= NULL
) {
2891 RT_LOCK_SPIN(rtrele
);
2892 if (rtrele
== hint0
) {
2893 RT_REMREF_LOCKED(rtrele
);
2909 * XXX: we currently do not make neighbor cache on any interface
2910 * other than ARCnet, Ethernet, FDDI and GIF.
2913 * - unidirectional tunnels needs no ND
2915 switch (ifp
->if_type
) {
2921 case IFT_IEEE8023ADLAG
:
2926 case IFT_GIF
: /* XXX need more cases? */
2938 struct sockaddr
*dst
,
2942 struct sockaddr_dl
*sdl
;
2944 if (m
->m_flags
& M_MCAST
) {
2945 switch (ifp
->if_type
) {
2949 case IFT_IEEE8023ADLAG
:
2954 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
2958 for (i
= 0; i
< ifp
->if_addrlen
; i
++)
2965 return(0); /* caller will free mbuf */
2970 /* this could happen, if we could not allocate memory */
2971 return(0); /* caller will free mbuf */
2974 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
2975 printf("nd6_storelladdr: something odd happens\n");
2977 return(0); /* caller will free mbuf */
2979 sdl
= SDL(rt
->rt_gateway
);
2980 if (sdl
->sdl_alen
== 0) {
2981 /* this should be impossible, but we bark here for debugging */
2982 printf("nd6_storelladdr: sdl_alen == 0\n");
2984 return(0); /* caller will free mbuf */
2987 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
2993 * This is the ND pre-output routine; care must be taken to ensure that
2994 * the "hint" route never gets freed via rtfree(), since the caller may
2995 * have stored it inside a struct route with a reference held for that
2999 nd6_lookup_ipv6(ifnet_t ifp
, const struct sockaddr_in6
*ip6_dest
,
3000 struct sockaddr_dl
*ll_dest
, size_t ll_dest_len
, route_t hint
,
3003 route_t route
= hint
;
3005 struct sockaddr_dl
*sdl
= NULL
;
3008 if (ip6_dest
->sin6_family
!= AF_INET6
)
3009 return (EAFNOSUPPORT
);
3011 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
3016 * Callee holds a reference on the route and returns
3017 * with the route entry locked, upon success.
3019 result
= arp_route_to_gateway_route(
3020 (const struct sockaddr
*)ip6_dest
, hint
, &route
);
3024 RT_LOCK_ASSERT_HELD(route
);
3027 if ((packet
->m_flags
& M_MCAST
) != 0) {
3030 result
= dlil_resolve_multi(ifp
,
3031 (const struct sockaddr
*)ip6_dest
,
3032 (struct sockaddr
*)ll_dest
, ll_dest_len
);
3038 if (route
== NULL
) {
3040 * This could happen, if we could not allocate memory or
3041 * if arp_route_to_gateway_route() didn't return a route.
3047 if (route
->rt_gateway
->sa_family
!= AF_LINK
) {
3048 printf("nd6_lookup_ipv6: gateway address not AF_LINK\n");
3049 result
= EADDRNOTAVAIL
;
3053 sdl
= SDL(route
->rt_gateway
);
3054 if (sdl
->sdl_alen
== 0) {
3055 /* this should be impossible, but we bark here for debugging */
3056 printf("nd6_lookup_ipv6: sdl_alen == 0\n");
3057 result
= EHOSTUNREACH
;
3061 copy_len
= sdl
->sdl_len
<= ll_dest_len
? sdl
->sdl_len
: ll_dest_len
;
3062 bcopy(sdl
, ll_dest
, copy_len
);
3065 if (route
!= NULL
) {
3066 if (route
== hint
) {
3067 RT_REMREF_LOCKED(route
);
3077 SYSCTL_DECL(_net_inet6_icmp6
);
3080 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
3082 #pragma unused(oidp, arg1, arg2)
3085 struct nd_defrouter
*dr
;
3086 int p64
= proc_is64bit(req
->p
);
3091 lck_mtx_lock(nd6_mutex
);
3093 struct in6_defrouter_64
*d
, *de
;
3095 for (dr
= TAILQ_FIRST(&nd_defrouter
);
3097 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
3098 d
= (struct in6_defrouter_64
*)buf
;
3099 de
= (struct in6_defrouter_64
*)(buf
+ sizeof (buf
));
3102 bzero(d
, sizeof (*d
));
3103 d
->rtaddr
.sin6_family
= AF_INET6
;
3104 d
->rtaddr
.sin6_len
= sizeof (d
->rtaddr
);
3105 if (in6_recoverscope(&d
->rtaddr
, &dr
->rtaddr
,
3109 "default router list (%s)\n",
3110 ip6_sprintf(&dr
->rtaddr
));
3111 d
->flags
= dr
->flags
;
3112 d
->rtlifetime
= dr
->rtlifetime
;
3113 d
->expire
= dr
->expire
;
3114 d
->if_index
= dr
->ifp
->if_index
;
3116 panic("buffer too short");
3118 error
= SYSCTL_OUT(req
, buf
, sizeof (*d
));
3123 struct in6_defrouter_32
*d_32
, *de_32
;
3125 for (dr
= TAILQ_FIRST(&nd_defrouter
);
3127 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
3128 d_32
= (struct in6_defrouter_32
*)buf
;
3129 de_32
= (struct in6_defrouter_32
*)(buf
+ sizeof (buf
));
3131 if (d_32
+ 1 <= de_32
) {
3132 bzero(d_32
, sizeof (*d_32
));
3133 d_32
->rtaddr
.sin6_family
= AF_INET6
;
3134 d_32
->rtaddr
.sin6_len
= sizeof (d_32
->rtaddr
);
3135 if (in6_recoverscope(&d_32
->rtaddr
, &dr
->rtaddr
,
3139 "default router list (%s)\n",
3140 ip6_sprintf(&dr
->rtaddr
));
3141 d_32
->flags
= dr
->flags
;
3142 d_32
->rtlifetime
= dr
->rtlifetime
;
3143 d_32
->expire
= dr
->expire
;
3144 d_32
->if_index
= dr
->ifp
->if_index
;
3146 panic("buffer too short");
3148 error
= SYSCTL_OUT(req
, buf
, sizeof (*d_32
));
3153 lck_mtx_unlock(nd6_mutex
);
3158 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
3160 #pragma unused(oidp, arg1, arg2)
3163 struct nd_prefix
*pr
;
3164 int p64
= proc_is64bit(req
->p
);
3169 lck_mtx_lock(nd6_mutex
);
3171 struct in6_prefix_64
*p
, *pe
;
3173 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3174 u_short advrtrs
= 0;
3176 struct sockaddr_in6
*sin6
, *s6
;
3177 struct nd_pfxrouter
*pfr
;
3179 p
= (struct in6_prefix_64
*)buf
;
3180 pe
= (struct in6_prefix_64
*)(buf
+ sizeof (buf
));
3183 bzero(p
, sizeof (*p
));
3184 sin6
= (struct sockaddr_in6
*)(p
+ 1);
3186 p
->prefix
= pr
->ndpr_prefix
;
3187 if (in6_recoverscope(&p
->prefix
,
3188 &p
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
3190 "scope error in prefix list (%s)\n",
3191 ip6_sprintf(&p
->prefix
.sin6_addr
));
3192 p
->raflags
= pr
->ndpr_raf
;
3193 p
->prefixlen
= pr
->ndpr_plen
;
3194 p
->vltime
= pr
->ndpr_vltime
;
3195 p
->pltime
= pr
->ndpr_pltime
;
3196 p
->if_index
= pr
->ndpr_ifp
->if_index
;
3197 p
->expire
= pr
->ndpr_expire
;
3198 p
->refcnt
= pr
->ndpr_refcnt
;
3199 p
->flags
= pr
->ndpr_stateflags
;
3200 p
->origin
= PR_ORIG_RA
;
3202 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
3204 pfr
= pfr
->pfr_next
) {
3205 if ((void *)&sin6
[advrtrs
+ 1] >
3210 s6
= &sin6
[advrtrs
];
3211 bzero(s6
, sizeof (*s6
));
3212 s6
->sin6_family
= AF_INET6
;
3213 s6
->sin6_len
= sizeof (*sin6
);
3214 if (in6_recoverscope(s6
,
3215 &pfr
->router
->rtaddr
,
3216 pfr
->router
->ifp
) != 0)
3217 log(LOG_ERR
, "scope error in "
3218 "prefix list (%s)\n",
3219 ip6_sprintf(&pfr
->router
->
3223 p
->advrtrs
= advrtrs
;
3225 panic("buffer too short");
3227 advance
= sizeof (*p
) + sizeof (*sin6
) * advrtrs
;
3228 error
= SYSCTL_OUT(req
, buf
, advance
);
3233 struct in6_prefix_32
*p_32
, *pe_32
;
3235 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3236 u_short advrtrs
= 0;
3238 struct sockaddr_in6
*sin6
, *s6
;
3239 struct nd_pfxrouter
*pfr
;
3241 p_32
= (struct in6_prefix_32
*)buf
;
3242 pe_32
= (struct in6_prefix_32
*)(buf
+ sizeof (buf
));
3244 if (p_32
+ 1 <= pe_32
) {
3245 bzero(p_32
, sizeof (*p_32
));
3246 sin6
= (struct sockaddr_in6
*)(p_32
+ 1);
3248 p_32
->prefix
= pr
->ndpr_prefix
;
3249 if (in6_recoverscope(&p_32
->prefix
,
3250 &p_32
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
3251 log(LOG_ERR
, "scope error in prefix "
3252 "list (%s)\n", ip6_sprintf(&p_32
->
3254 p_32
->raflags
= pr
->ndpr_raf
;
3255 p_32
->prefixlen
= pr
->ndpr_plen
;
3256 p_32
->vltime
= pr
->ndpr_vltime
;
3257 p_32
->pltime
= pr
->ndpr_pltime
;
3258 p_32
->if_index
= pr
->ndpr_ifp
->if_index
;
3259 p_32
->expire
= pr
->ndpr_expire
;
3260 p_32
->refcnt
= pr
->ndpr_refcnt
;
3261 p_32
->flags
= pr
->ndpr_stateflags
;
3262 p_32
->origin
= PR_ORIG_RA
;
3264 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
3266 pfr
= pfr
->pfr_next
) {
3267 if ((void *)&sin6
[advrtrs
+ 1] >
3272 s6
= &sin6
[advrtrs
];
3273 bzero(s6
, sizeof (*s6
));
3274 s6
->sin6_family
= AF_INET6
;
3275 s6
->sin6_len
= sizeof (*sin6
);
3276 if (in6_recoverscope(s6
,
3277 &pfr
->router
->rtaddr
,
3278 pfr
->router
->ifp
) != 0)
3279 log(LOG_ERR
, "scope error in "
3280 "prefix list (%s)\n",
3281 ip6_sprintf(&pfr
->router
->
3285 p_32
->advrtrs
= advrtrs
;
3287 panic("buffer too short");
3289 advance
= sizeof (*p_32
) + sizeof (*sin6
) * advrtrs
;
3290 error
= SYSCTL_OUT(req
, buf
, advance
);
3295 lck_mtx_unlock(nd6_mutex
);
3298 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
3299 CTLFLAG_RD
, 0, 0, nd6_sysctl_drlist
, "S,in6_defrouter","");
3300 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
3301 CTLFLAG_RD
, 0, 0, nd6_sysctl_prlist
, "S,in6_defrouter","");