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
== NULL
|| (ifp
->if_type
== IFT_PPP
) ||
1197 (ifp
->if_eflags
& IFEF_NOAUTOIPV6LL
) ||
1198 (rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1199 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
1200 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
)) {
1201 RT_REMREF_LOCKED(rt
);
1204 log(LOG_DEBUG
, "%s: failed to lookup %s "
1205 "(if = %s)\n", __func__
, ip6_sprintf(addr6
),
1206 ifp
? if_name(ifp
) : "unspec");
1207 /* xxx more logs... kazu */
1212 * Caller needs to release reference and call RT_UNLOCK(rt).
1218 * Detect if a given IPv6 address identifies a neighbor on a given link.
1219 * XXX: should take care of the destination of a p2p link?
1222 nd6_is_addr_neighbor(
1223 struct sockaddr_in6
*addr
,
1231 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
1232 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
1235 * A link-local address is always a neighbor.
1236 * XXX: we should use the sin6_scope_id field rather than the embedded
1239 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
) &&
1240 ntohs(*(u_int16_t
*)&addr
->sin6_addr
.s6_addr
[2]) == ifp
->if_index
)
1244 * If the address matches one of our addresses,
1245 * it should be a neighbor.
1247 ifnet_lock_shared(ifp
);
1248 for (ifa
= ifp
->if_addrlist
.tqh_first
;
1250 ifa
= ifa
->ifa_list
.tqe_next
)
1252 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1255 for (i
= 0; i
< 4; i
++) {
1256 if ((IFADDR6(ifa
).s6_addr32
[i
] ^
1257 addr
->sin6_addr
.s6_addr32
[i
]) &
1258 IFMASK6(ifa
).s6_addr32
[i
])
1261 ifnet_lock_done(ifp
);
1264 ifnet_lock_done(ifp
);
1267 * Even if the address matches none of our addresses, it might be
1268 * in the neighbor cache. Callee returns a locked route upon
1271 if ((rt
= nd6_lookup(&addr
->sin6_addr
, 0, ifp
, rt_locked
)) != NULL
) {
1272 RT_LOCK_ASSERT_HELD(rt
);
1273 RT_REMREF_LOCKED(rt
);
1284 * Free an nd6 llinfo entry.
1290 struct llinfo_nd6
*ln
;
1291 struct in6_addr in6
;
1292 struct nd_defrouter
*dr
;
1294 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1295 RT_LOCK_ASSERT_NOTHELD(rt
);
1296 lck_mtx_lock(nd6_mutex
);
1299 RT_ADDREF_LOCKED(rt
); /* Extra ref */
1301 in6
= ((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
1304 * Prevent another thread from modifying rt_key, rt_gateway
1305 * via rt_setgate() after the rt_lock is dropped by marking
1306 * the route as defunct.
1308 rt
->rt_flags
|= RTF_CONDEMNED
;
1311 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1312 * even though it is not harmful, it was not really necessary.
1315 if (!ip6_forwarding
&& (ip6_accept_rtadv
||
1316 (rt
->rt_ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
1317 dr
= defrouter_lookup(&((struct sockaddr_in6
*)rt_key(rt
))->
1318 sin6_addr
, rt
->rt_ifp
);
1320 if ((ln
&& ln
->ln_router
) || dr
) {
1322 * rt6_flush must be called whether or not the neighbor
1323 * is in the Default Router List.
1324 * See a corresponding comment in nd6_na_input().
1327 rt6_flush(&in6
, rt
->rt_ifp
);
1334 * Unreachablity of a router might affect the default
1335 * router selection and on-link detection of advertised
1340 * Temporarily fake the state to choose a new default
1341 * router and to perform on-link determination of
1342 * prefixes correctly.
1343 * Below the state will be set correctly,
1344 * or the entry itself will be deleted.
1347 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1350 * Since defrouter_select() does not affect the
1351 * on-link determination and MIP6 needs the check
1352 * before the default router selection, we perform
1356 pfxlist_onlink_check(1);
1358 if (dr
== TAILQ_FIRST(&nd_defrouter
)) {
1360 * It is used as the current default router,
1361 * so we have to move it to the end of the
1362 * list and choose a new one.
1363 * XXX: it is not very efficient if this is
1366 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1367 TAILQ_INSERT_TAIL(&nd_defrouter
, dr
, dr_entry
);
1372 RT_LOCK_ASSERT_NOTHELD(rt
);
1377 lck_mtx_unlock(nd6_mutex
);
1379 * Detach the route from the routing tree and the list of neighbor
1380 * caches, and disable the route entry not to be used in already
1383 (void) rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
1384 rt_mask(rt
), 0, (struct rtentry
**)0);
1386 /* Extra ref held above; now free it */
1391 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1393 * XXX cost-effective metods?
1398 struct in6_addr
*dst6
,
1401 struct llinfo_nd6
*ln
;
1402 struct timeval timenow
;
1404 getmicrotime(&timenow
);
1407 * If the caller specified "rt", use that. Otherwise, resolve the
1408 * routing table by supplied "dst6".
1413 /* Callee returns a locked route upon success */
1414 if ((rt
= nd6_lookup(dst6
, 0, NULL
, 0)) == NULL
)
1416 RT_LOCK_ASSERT_HELD(rt
);
1419 RT_ADDREF_LOCKED(rt
);
1422 if ((rt
->rt_flags
& RTF_GATEWAY
) != 0 ||
1423 (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1424 !rt
->rt_llinfo
|| !rt
->rt_gateway
||
1425 rt
->rt_gateway
->sa_family
!= AF_LINK
) {
1426 /* This is not a host route. */
1431 if (ln
->ln_state
< ND6_LLINFO_REACHABLE
)
1435 * if we get upper-layer reachability confirmation many times,
1436 * it is possible we have false information.
1440 if (ln
->ln_byhint
> nd6_maxnudhint
)
1444 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1445 if (ln
->ln_expire
) {
1446 lck_rw_lock_shared(nd_if_rwlock
);
1447 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
,
1448 nd_ifinfo
[rt
->rt_ifp
->if_index
].reachable
);
1449 lck_rw_done(nd_if_rwlock
);
1452 RT_REMREF_LOCKED(rt
);
1460 __unused
struct sockaddr
*sa
)
1462 struct sockaddr
*gate
= rt
->rt_gateway
;
1463 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
1464 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
, 0, 0, 0, 0, 0,
1465 {0,0,0,0,0,0,0,0,0,0,0,0,} };
1466 struct ifnet
*ifp
= rt
->rt_ifp
;
1468 struct timeval timenow
;
1470 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1471 RT_LOCK_ASSERT_HELD(rt
);
1473 if ((rt
->rt_flags
& RTF_GATEWAY
))
1476 if (nd6_need_cache(ifp
) == 0 && (rt
->rt_flags
& RTF_HOST
) == 0) {
1478 * This is probably an interface direct route for a link
1479 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1480 * We do not need special treatment below for such a route.
1481 * Moreover, the RTF_LLINFO flag which would be set below
1482 * would annoy the ndp(8) command.
1487 if (req
== RTM_RESOLVE
) {
1490 if (!nd6_need_cache(ifp
)) { /* stf case */
1494 * nd6_is_addr_neighbor() may call nd6_lookup(),
1495 * therefore we drop rt_lock to avoid deadlock
1496 * during the lookup. Using rt_key(rt) is still
1497 * safe because it won't change while rnh_lock
1500 RT_ADDREF_LOCKED(rt
);
1502 no_nd_cache
= !nd6_is_addr_neighbor(
1503 (struct sockaddr_in6
*)rt_key(rt
), ifp
, 1);
1505 RT_REMREF_LOCKED(rt
);
1509 * FreeBSD and BSD/OS often make a cloned host route based
1510 * on a less-specific route (e.g. the default route).
1511 * If the less specific route does not have a "gateway"
1512 * (this is the case when the route just goes to a p2p or an
1513 * stf interface), we'll mistakenly make a neighbor cache for
1514 * the host route, and will see strange neighbor solicitation
1515 * for the corresponding destination. In order to avoid the
1516 * confusion, we check if the destination of the route is
1517 * a neighbor in terms of neighbor discovery, and stop the
1518 * process if not. Additionally, we remove the LLINFO flag
1519 * so that ndp(8) will not try to get the neighbor information
1520 * of the destination.
1523 rt
->rt_flags
&= ~RTF_LLINFO
;
1528 getmicrotime(&timenow
);
1532 * There is no backward compatibility :)
1534 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1535 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1536 * rt->rt_flags |= RTF_CLONING;
1538 if (rt
->rt_flags
& (RTF_CLONING
| RTF_LLINFO
)) {
1540 * Case 1: This route should come from
1541 * a route to interface. RTF_LLINFO flag is set
1542 * for a host route whose destination should be
1543 * treated as on-link.
1545 if (rt_setgate(rt
, rt_key(rt
),
1546 (struct sockaddr
*)&null_sdl
) == 0) {
1547 gate
= rt
->rt_gateway
;
1548 SDL(gate
)->sdl_type
= ifp
->if_type
;
1549 SDL(gate
)->sdl_index
= ifp
->if_index
;
1551 * In case we're called before 1.0 sec.
1555 ln
->ln_expire
= MAX(timenow
.tv_sec
, 1);
1557 if ((rt
->rt_flags
& RTF_CLONING
))
1561 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1562 * We don't do that here since llinfo is not ready yet.
1564 * There are also couple of other things to be discussed:
1565 * - unsolicited NA code needs improvement beforehand
1566 * - RFC2461 says we MAY send multicast unsolicited NA
1567 * (7.2.6 paragraph 4), however, it also says that we
1568 * SHOULD provide a mechanism to prevent multicast NA storm.
1569 * we don't have anything like it right now.
1570 * note that the mechanism needs a mutual agreement
1571 * between proxies, which means that we need to implement
1572 * a new protocol, or a new kludge.
1573 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1574 * we need to check ip6forwarding before sending it.
1575 * (or should we allow proxy ND configuration only for
1576 * routers? there's no mention about proxy ND from hosts)
1579 /* XXX it does not work */
1580 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1582 &SIN6(rt_key(rt
))->sin6_addr
,
1583 &SIN6(rt_key(rt
))->sin6_addr
,
1584 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1589 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) == 0) {
1591 * Address resolution isn't necessary for a point to
1592 * point link, so we can skip this test for a p2p link.
1594 if (gate
->sa_family
!= AF_LINK
||
1595 gate
->sa_len
< sizeof(null_sdl
)) {
1597 "nd6_rtrequest: bad gateway value: %s\n",
1601 SDL(gate
)->sdl_type
= ifp
->if_type
;
1602 SDL(gate
)->sdl_index
= ifp
->if_index
;
1605 break; /* This happens on a route change */
1607 * Case 2: This route may come from cloning, or a manual route
1608 * add with a LL address.
1610 rt
->rt_llinfo
= ln
= nd6_llinfo_alloc();
1612 log(LOG_DEBUG
, "nd6_rtrequest: malloc failed\n");
1615 rt
->rt_llinfo_free
= nd6_llinfo_free
;
1619 Bzero(ln
, sizeof(*ln
));
1621 /* this is required for "ndp" command. - shin */
1622 if (req
== RTM_ADD
) {
1624 * gate should have some valid AF_LINK entry,
1625 * and ln->ln_expire should have some lifetime
1626 * which is specified by ndp command.
1628 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1632 * When req == RTM_RESOLVE, rt is created and
1633 * initialized in rtrequest(), so rt_expire is 0.
1635 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1636 /* In case we're called before 1.0 sec. has elapsed */
1637 ln
->ln_expire
= MAX(timenow
.tv_sec
, 1);
1639 rt
->rt_flags
|= RTF_LLINFO
;
1643 * If we have too many cache entries, initiate immediate
1644 * purging for some "less recently used" entries. Note that
1645 * we cannot directly call nd6_free() here because it would
1646 * cause re-entering rtable related routines triggering an LOR
1649 if (ip6_neighborgcthresh
>= 0 &&
1650 nd6_inuse
>= ip6_neighborgcthresh
) {
1653 for (i
= 0; i
< 10 && llinfo_nd6
.ln_prev
!= ln
; i
++) {
1654 struct llinfo_nd6
*ln_end
= llinfo_nd6
.ln_prev
;
1655 struct rtentry
*rt_end
= ln_end
->ln_rt
;
1657 /* Move this entry to the head */
1660 LN_INSERTHEAD(ln_end
);
1662 if (ln_end
->ln_expire
== 0) {
1666 if (ln_end
->ln_state
> ND6_LLINFO_INCOMPLETE
)
1667 ln_end
->ln_state
= ND6_LLINFO_STALE
;
1669 ln_end
->ln_state
= ND6_LLINFO_PURGE
;
1670 ln_end
->ln_expire
= timenow
.tv_sec
;
1676 * check if rt_key(rt) is one of my address assigned
1679 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1680 &SIN6(rt_key(rt
))->sin6_addr
);
1682 caddr_t macp
= nd6_ifptomac(ifp
);
1684 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1687 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
1688 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
1690 if (nd6_useloopback
) {
1691 #if IFNET_ROUTE_REFCNT
1692 /* Adjust route ref count for the interfaces */
1693 if (rt
->rt_if_ref_fn
!= NULL
&&
1694 rt
->rt_ifp
!= lo_ifp
) {
1695 rt
->rt_if_ref_fn(lo_ifp
, 1);
1696 rt
->rt_if_ref_fn(rt
->rt_ifp
, -1);
1698 #endif /* IFNET_ROUTE_REFCNT */
1699 rt
->rt_ifp
= lo_ifp
; /* XXX */
1701 * Make sure rt_ifa be equal to the ifaddr
1702 * corresponding to the address.
1703 * We need this because when we refer
1704 * rt_ifa->ia6_flags in ip6_input, we assume
1705 * that the rt_ifa points to the address instead
1706 * of the loopback address.
1708 if (ifa
!= rt
->rt_ifa
) {
1713 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
1715 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1718 /* join solicited node multicast for proxy ND */
1719 if (ifp
->if_flags
& IFF_MULTICAST
) {
1720 struct in6_addr llsol
;
1723 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1724 llsol
.s6_addr16
[0] = htons(0xff02);
1725 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1726 llsol
.s6_addr32
[1] = 0;
1727 llsol
.s6_addr32
[2] = htonl(1);
1728 llsol
.s6_addr8
[12] = 0xff;
1730 if (!in6_addmulti(&llsol
, ifp
, &error
, 0)) {
1731 nd6log((LOG_ERR
, "%s: failed to join "
1732 "%s (errno=%d)\n", if_name(ifp
),
1733 ip6_sprintf(&llsol
), error
));
1742 /* leave from solicited node multicast for proxy ND */
1743 if ((rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
1744 (ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1745 struct in6_addr llsol
;
1746 struct in6_multi
*in6m
;
1748 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1749 llsol
.s6_addr16
[0] = htons(0xff02);
1750 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1751 llsol
.s6_addr32
[1] = 0;
1752 llsol
.s6_addr32
[2] = htonl(1);
1753 llsol
.s6_addr8
[12] = 0xff;
1755 ifnet_lock_shared(ifp
);
1756 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1757 ifnet_lock_done(ifp
);
1759 in6_delmulti(in6m
, 0);
1763 * Unchain it but defer the actual freeing until the route
1764 * itself is to be freed. rt->rt_llinfo still points to
1765 * llinfo_nd6, and likewise, ln->ln_rt stil points to this
1766 * route entry, except that RTF_LLINFO is now cleared.
1768 if (ln
->ln_flags
& ND6_LNF_IN_USE
)
1770 rt
->rt_flags
&= ~RTF_LLINFO
;
1771 if (ln
->ln_hold
!= NULL
)
1772 m_freem(ln
->ln_hold
);
1778 nd6_siocgdrlst(void *data
, int data_is_64
)
1780 struct in6_drlist_64
*drl_64
= (struct in6_drlist_64
*)data
;
1781 struct in6_drlist_32
*drl_32
= (struct in6_drlist_32
*)data
;
1782 struct nd_defrouter
*dr
;
1785 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1787 bzero(data
, data_is_64
? sizeof (*drl_64
) : sizeof (*drl_32
));
1788 dr
= TAILQ_FIRST(&nd_defrouter
);
1790 /* For 64-bit process */
1791 while (dr
&& i
< DRLSTSIZ
) {
1792 drl_64
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1793 if (IN6_IS_ADDR_LINKLOCAL(&drl_64
->defrouter
[i
].rtaddr
)) {
1794 /* XXX: need to this hack for KAME stack */
1795 drl_64
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1798 "default router list contains a "
1799 "non-linklocal address(%s)\n",
1800 ip6_sprintf(&drl_64
->defrouter
[i
].rtaddr
));
1802 drl_64
->defrouter
[i
].flags
= dr
->flags
;
1803 drl_64
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1804 drl_64
->defrouter
[i
].expire
= dr
->expire
;
1805 drl_64
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1807 dr
= TAILQ_NEXT(dr
, dr_entry
);
1811 /* For 32-bit process */
1812 while (dr
&& i
< DRLSTSIZ
) {
1813 drl_32
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1814 if (IN6_IS_ADDR_LINKLOCAL(&drl_32
->defrouter
[i
].rtaddr
)) {
1815 /* XXX: need to this hack for KAME stack */
1816 drl_32
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1819 "default router list contains a "
1820 "non-linklocal address(%s)\n",
1821 ip6_sprintf(&drl_32
->defrouter
[i
].rtaddr
));
1823 drl_32
->defrouter
[i
].flags
= dr
->flags
;
1824 drl_32
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1825 drl_32
->defrouter
[i
].expire
= dr
->expire
;
1826 drl_32
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1828 dr
= TAILQ_NEXT(dr
, dr_entry
);
1833 nd6_siocgprlst(void *data
, int data_is_64
)
1835 struct in6_prlist_64
*prl_64
= (struct in6_prlist_64
*)data
;
1836 struct in6_prlist_32
*prl_32
= (struct in6_prlist_32
*)data
;
1837 struct nd_prefix
*pr
;
1838 struct rr_prefix
*rpp
;
1841 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1843 * XXX meaning of fields, especialy "raflags", is very
1844 * differnet between RA prefix list and RR/static prefix list.
1845 * how about separating ioctls into two?
1847 bzero(data
, data_is_64
? sizeof (*prl_64
) : sizeof (*prl_32
));
1848 pr
= nd_prefix
.lh_first
;
1850 /* For 64-bit process */
1851 while (pr
&& i
< PRLSTSIZ
) {
1852 struct nd_pfxrouter
*pfr
;
1855 (void) in6_embedscope(&prl_64
->prefix
[i
].prefix
,
1856 &pr
->ndpr_prefix
, NULL
, NULL
);
1857 prl_64
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1858 prl_64
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1859 prl_64
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1860 prl_64
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1861 prl_64
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1862 prl_64
->prefix
[i
].expire
= pr
->ndpr_expire
;
1864 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1868 #define RTRADDR prl_64->prefix[i].advrtr[j]
1869 RTRADDR
= pfr
->router
->rtaddr
;
1870 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1871 /* XXX: hack for KAME */
1872 RTRADDR
.s6_addr16
[1] = 0;
1875 "a router(%s) advertises "
1877 "non-link local address\n",
1878 ip6_sprintf(&RTRADDR
));
1883 pfr
= pfr
->pfr_next
;
1885 prl_64
->prefix
[i
].advrtrs
= j
;
1886 prl_64
->prefix
[i
].origin
= PR_ORIG_RA
;
1892 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1893 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1896 (void) in6_embedscope(&prl_64
->prefix
[i
].prefix
,
1897 &pr
->ndpr_prefix
, NULL
, NULL
);
1898 prl_64
->prefix
[i
].raflags
= rpp
->rp_raf
;
1899 prl_64
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1900 prl_64
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1901 prl_64
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1902 prl_64
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1903 prl_64
->prefix
[i
].expire
= rpp
->rp_expire
;
1904 prl_64
->prefix
[i
].advrtrs
= 0;
1905 prl_64
->prefix
[i
].origin
= rpp
->rp_origin
;
1910 /* For 32-bit process */
1911 while (pr
&& i
< PRLSTSIZ
) {
1912 struct nd_pfxrouter
*pfr
;
1915 (void) in6_embedscope(&prl_32
->prefix
[i
].prefix
,
1916 &pr
->ndpr_prefix
, NULL
, NULL
);
1917 prl_32
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1918 prl_32
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1919 prl_32
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1920 prl_32
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1921 prl_32
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1922 prl_32
->prefix
[i
].expire
= pr
->ndpr_expire
;
1924 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1928 #define RTRADDR prl_32->prefix[i].advrtr[j]
1929 RTRADDR
= pfr
->router
->rtaddr
;
1930 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1931 /* XXX: hack for KAME */
1932 RTRADDR
.s6_addr16
[1] = 0;
1935 "a router(%s) advertises "
1937 "non-link local address\n",
1938 ip6_sprintf(&RTRADDR
));
1943 pfr
= pfr
->pfr_next
;
1945 prl_32
->prefix
[i
].advrtrs
= j
;
1946 prl_32
->prefix
[i
].origin
= PR_ORIG_RA
;
1952 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1953 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1956 (void) in6_embedscope(&prl_32
->prefix
[i
].prefix
,
1957 &pr
->ndpr_prefix
, NULL
, NULL
);
1958 prl_32
->prefix
[i
].raflags
= rpp
->rp_raf
;
1959 prl_32
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1960 prl_32
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1961 prl_32
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1962 prl_32
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1963 prl_32
->prefix
[i
].expire
= rpp
->rp_expire
;
1964 prl_32
->prefix
[i
].advrtrs
= 0;
1965 prl_32
->prefix
[i
].origin
= rpp
->rp_origin
;
1971 nd6_ioctl(u_long cmd
, caddr_t data
, struct ifnet
*ifp
)
1973 struct in6_ndireq
*ndi
= (struct in6_ndireq
*)data
;
1974 struct in6_ondireq
*ondi
= (struct in6_ondireq
*)data
;
1975 struct nd_defrouter
*dr
, any
;
1976 struct nd_prefix
*pr
;
1978 int i
= ifp
->if_index
, error
= 0;
1981 case SIOCGDRLST_IN6_32
:
1982 case SIOCGDRLST_IN6_64
:
1984 * obsolete API, use sysctl under net.inet6.icmp6
1986 lck_mtx_lock(nd6_mutex
);
1987 nd6_siocgdrlst(data
, cmd
== SIOCGDRLST_IN6_64
);
1988 lck_mtx_unlock(nd6_mutex
);
1991 case SIOCGPRLST_IN6_32
:
1992 case SIOCGPRLST_IN6_64
:
1994 * obsolete API, use sysctl under net.inet6.icmp6
1996 lck_mtx_lock(nd6_mutex
);
1997 nd6_siocgprlst(data
, cmd
== SIOCGPRLST_IN6_64
);
1998 lck_mtx_unlock(nd6_mutex
);
2001 case OSIOCGIFINFO_IN6
:
2002 case SIOCGIFINFO_IN6
:
2004 * SIOCGIFINFO_IN6 ioctl is encoded with in6_ondireq
2005 * instead of in6_ndireq, so we treat it as such.
2007 lck_rw_lock_shared(nd_if_rwlock
);
2008 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
2009 lck_rw_done(nd_if_rwlock
);
2013 ondi
->ndi
.linkmtu
= IN6_LINKMTU(ifp
);
2014 ondi
->ndi
.maxmtu
= nd_ifinfo
[i
].maxmtu
;
2015 ondi
->ndi
.basereachable
= nd_ifinfo
[i
].basereachable
;
2016 ondi
->ndi
.reachable
= nd_ifinfo
[i
].reachable
;
2017 ondi
->ndi
.retrans
= nd_ifinfo
[i
].retrans
;
2018 ondi
->ndi
.flags
= nd_ifinfo
[i
].flags
;
2019 ondi
->ndi
.recalctm
= nd_ifinfo
[i
].recalctm
;
2020 ondi
->ndi
.chlim
= nd_ifinfo
[i
].chlim
;
2021 ondi
->ndi
.receivedra
= nd_ifinfo
[i
].receivedra
;
2022 lck_rw_done(nd_if_rwlock
);
2025 case SIOCSIFINFO_FLAGS
:
2026 /* XXX: almost all other fields of ndi->ndi is unused */
2027 lck_rw_lock_shared(nd_if_rwlock
);
2028 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
2029 lck_rw_done(nd_if_rwlock
);
2033 nd_ifinfo
[i
].flags
= ndi
->ndi
.flags
;
2034 lck_rw_done(nd_if_rwlock
);
2037 case SIOCSNDFLUSH_IN6
: /* XXX: the ioctl name is confusing... */
2038 /* flush default router list */
2040 * xxx sumikawa: should not delete route if default
2041 * route equals to the top of default router list
2043 bzero(&any
, sizeof(any
));
2044 lck_mtx_lock(nd6_mutex
);
2045 defrouter_delreq(&any
, 1);
2047 lck_mtx_unlock(nd6_mutex
);
2048 /* xxx sumikawa: flush prefix list */
2051 case SIOCSPFXFLUSH_IN6
: {
2052 /* flush all the prefix advertised by routers */
2053 struct nd_prefix
*next
;
2054 lck_mtx_lock(nd6_mutex
);
2056 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
2057 struct in6_ifaddr
*ia
, *ia_next
;
2059 next
= pr
->ndpr_next
;
2061 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
))
2064 /* do we really have to remove addresses as well? */
2065 for (ia
= in6_ifaddrs
; ia
; ia
= ia_next
) {
2066 /* ia might be removed. keep the next ptr. */
2067 ia_next
= ia
->ia_next
;
2069 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
2072 if (ia
->ia6_ndpr
== pr
)
2073 in6_purgeaddr(&ia
->ia_ifa
, 1);
2075 prelist_remove(pr
, 1);
2077 lck_mtx_unlock(nd6_mutex
);
2081 case SIOCSRTRFLUSH_IN6
: {
2082 /* flush all the default routers */
2083 struct nd_defrouter
*next
;
2085 lck_mtx_lock(nd6_mutex
);
2086 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
2088 * The first entry of the list may be stored in
2089 * the routing table, so we'll delete it later.
2091 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
2092 next
= TAILQ_NEXT(dr
, dr_entry
);
2093 defrtrlist_del(dr
, 1);
2095 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
), 1);
2097 lck_mtx_unlock(nd6_mutex
);
2101 case SIOCGNBRINFO_IN6_32
: {
2102 struct llinfo_nd6
*ln
;
2103 struct in6_nbrinfo_32
*nbi_32
= (struct in6_nbrinfo_32
*)data
;
2104 /* make local for safety */
2105 struct in6_addr nb_addr
= nbi_32
->addr
;
2108 * XXX: KAME specific hack for scoped addresses
2109 * XXXX: for other scopes than link-local?
2111 if (IN6_IS_ADDR_LINKLOCAL(&nbi_32
->addr
) ||
2112 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_32
->addr
)) {
2113 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
2116 *idp
= htons(ifp
->if_index
);
2119 /* Callee returns a locked route upon success */
2120 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
2124 RT_LOCK_ASSERT_HELD(rt
);
2126 nbi_32
->state
= ln
->ln_state
;
2127 nbi_32
->asked
= ln
->ln_asked
;
2128 nbi_32
->isrouter
= ln
->ln_router
;
2129 nbi_32
->expire
= ln
->ln_expire
;
2130 RT_REMREF_LOCKED(rt
);
2135 case SIOCGNBRINFO_IN6_64
: {
2136 struct llinfo_nd6
*ln
;
2137 struct in6_nbrinfo_64
*nbi_64
= (struct in6_nbrinfo_64
*)data
;
2138 /* make local for safety */
2139 struct in6_addr nb_addr
= nbi_64
->addr
;
2142 * XXX: KAME specific hack for scoped addresses
2143 * XXXX: for other scopes than link-local?
2145 if (IN6_IS_ADDR_LINKLOCAL(&nbi_64
->addr
) ||
2146 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_64
->addr
)) {
2147 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
2150 *idp
= htons(ifp
->if_index
);
2153 /* Callee returns a locked route upon success */
2154 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
2158 RT_LOCK_ASSERT_HELD(rt
);
2160 nbi_64
->state
= ln
->ln_state
;
2161 nbi_64
->asked
= ln
->ln_asked
;
2162 nbi_64
->isrouter
= ln
->ln_router
;
2163 nbi_64
->expire
= ln
->ln_expire
;
2164 RT_REMREF_LOCKED(rt
);
2169 case SIOCGDEFIFACE_IN6_32
: /* XXX: should be implemented as a sysctl? */
2170 case SIOCGDEFIFACE_IN6_64
: {
2171 struct in6_ndifreq_64
*ndif_64
= (struct in6_ndifreq_64
*)data
;
2172 struct in6_ndifreq_32
*ndif_32
= (struct in6_ndifreq_32
*)data
;
2174 if (cmd
== SIOCGDEFIFACE_IN6_64
)
2175 ndif_64
->ifindex
= nd6_defifindex
;
2177 ndif_32
->ifindex
= nd6_defifindex
;
2181 case SIOCSDEFIFACE_IN6_32
: /* XXX: should be implemented as a sysctl? */
2182 case SIOCSDEFIFACE_IN6_64
: {
2183 struct in6_ndifreq_64
*ndif_64
= (struct in6_ndifreq_64
*)data
;
2184 struct in6_ndifreq_32
*ndif_32
= (struct in6_ndifreq_32
*)data
;
2186 return (nd6_setdefaultiface(cmd
== SIOCSDEFIFACE_IN6_64
?
2187 ndif_64
->ifindex
: ndif_32
->ifindex
));
2195 * Create neighbor cache entry and cache link-layer address,
2196 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
2201 struct in6_addr
*from
,
2203 __unused
int lladdrlen
,
2204 int type
, /* ICMP6 type */
2205 int code
) /* type dependent information */
2207 struct rtentry
*rt
= NULL
;
2208 struct llinfo_nd6
*ln
= NULL
;
2210 struct sockaddr_dl
*sdl
= NULL
;
2215 struct timeval timenow
;
2218 panic("ifp == NULL in nd6_cache_lladdr");
2220 panic("from == NULL in nd6_cache_lladdr");
2222 /* nothing must be updated for unspecified address */
2223 if (IN6_IS_ADDR_UNSPECIFIED(from
))
2227 * Validation about ifp->if_addrlen and lladdrlen must be done in
2230 * XXX If the link does not have link-layer adderss, what should
2231 * we do? (ifp->if_addrlen == 0)
2232 * Spec says nothing in sections for RA, RS and NA. There's small
2233 * description on it in NS section (RFC 2461 7.2.3).
2235 getmicrotime(&timenow
);
2237 rt
= nd6_lookup(from
, 0, ifp
, 0);
2240 /* nothing must be done if there's no lladdr */
2241 if (!lladdr
|| !lladdrlen
)
2245 if ((rt
= nd6_lookup(from
, 1, ifp
, 0)) == NULL
)
2247 RT_LOCK_ASSERT_HELD(rt
);
2250 RT_LOCK_ASSERT_HELD(rt
);
2251 /* do nothing if static ndp is set */
2252 if (rt
->rt_flags
& RTF_STATIC
) {
2253 RT_REMREF_LOCKED(rt
);
2260 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
2270 if (!rt
->rt_gateway
)
2272 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
2274 sdl
= SDL(rt
->rt_gateway
);
2276 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
2277 if (olladdr
&& lladdr
) {
2278 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
2286 * newentry olladdr lladdr llchange (*=record)
2289 * 0 n y -- (3) * STALE
2291 * 0 y y y (5) * STALE
2292 * 1 -- n -- (6) NOSTATE(= PASSIVE)
2293 * 1 -- y -- (7) * STALE
2296 if (lladdr
) { /* (3-5) and (7) */
2298 * Record source link-layer address
2299 * XXX is it dependent to ifp->if_type?
2301 sdl
->sdl_alen
= ifp
->if_addrlen
;
2302 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
2306 if ((!olladdr
&& lladdr
) /* (3) */
2307 || (olladdr
&& lladdr
&& llchange
)) { /* (5) */
2309 newstate
= ND6_LLINFO_STALE
;
2310 } else /* (1-2,4) */
2314 if (!lladdr
) /* (6) */
2315 newstate
= ND6_LLINFO_NOSTATE
;
2317 newstate
= ND6_LLINFO_STALE
;
2322 * Update the state of the neighbor cache.
2324 ln
->ln_state
= newstate
;
2326 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
2327 struct mbuf
*m
= ln
->ln_hold
;
2329 * XXX: since nd6_output() below will cause
2330 * state tansition to DELAY and reset the timer,
2331 * we must set the timer now, although it is actually
2334 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
,
2340 * we assume ifp is not a p2p here, so just
2341 * set the 2nd argument as the 1st one.
2344 nd6_output(ifp
, ifp
, m
,
2345 (struct sockaddr_in6
*)rt_key(rt
), rt
, 0);
2348 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
2349 /* probe right away */
2350 ln
->ln_expire
= timenow
.tv_sec
;
2355 * ICMP6 type dependent behavior.
2357 * NS: clear IsRouter if new entry
2358 * RS: clear IsRouter
2359 * RA: set IsRouter if there's lladdr
2360 * redir: clear IsRouter if new entry
2363 * The spec says that we must set IsRouter in the following cases:
2364 * - If lladdr exist, set IsRouter. This means (1-5).
2365 * - If it is old entry (!newentry), set IsRouter. This means (7).
2366 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
2367 * A quetion arises for (1) case. (1) case has no lladdr in the
2368 * neighbor cache, this is similar to (6).
2369 * This case is rare but we figured that we MUST NOT set IsRouter.
2371 * newentry olladdr lladdr llchange NS RS RA redir
2373 * 0 n n -- (1) c ? s
2374 * 0 y n -- (2) c s s
2375 * 0 n y -- (3) c s s
2378 * 1 -- n -- (6) c c c s
2379 * 1 -- y -- (7) c c s c s
2383 switch (type
& 0xff) {
2384 case ND_NEIGHBOR_SOLICIT
:
2386 * New entry must have is_router flag cleared.
2388 if (is_newentry
) /* (6-7) */
2393 * If the icmp is a redirect to a better router, always set the
2394 * is_router flag. Otherwise, if the entry is newly created,
2395 * clear the flag. [RFC 2461, sec 8.3]
2397 if (code
== ND_REDIRECT_ROUTER
)
2399 else if (is_newentry
) /* (6-7) */
2402 case ND_ROUTER_SOLICIT
:
2404 * is_router flag must always be cleared.
2408 case ND_ROUTER_ADVERT
:
2410 * Mark an entry with lladdr as a router.
2412 if ((!is_newentry
&& (olladdr
|| lladdr
)) /* (2-5) */
2413 || (is_newentry
&& lladdr
)) { /* (7) */
2420 * When the link-layer address of a router changes, select the
2421 * best router again. In particular, when the neighbor entry is newly
2422 * created, it might affect the selection policy.
2423 * Question: can we restrict the first condition to the "is_newentry"
2425 * XXX: when we hear an RA from a new router with the link-layer
2426 * address option, defrouter_select() is called twice, since
2427 * defrtrlist_update called the function as well. However, I believe
2428 * we can compromise the overhead, since it only happens the first
2430 * XXX: although defrouter_select() should not have a bad effect
2431 * for those are not autoconfigured hosts, we explicitly avoid such
2434 if (do_update
&& ln
->ln_router
&& !ip6_forwarding
&&
2435 (ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
2436 RT_REMREF_LOCKED(rt
);
2438 lck_mtx_lock(nd6_mutex
);
2440 lck_mtx_unlock(nd6_mutex
);
2442 RT_REMREF_LOCKED(rt
);
2449 __unused
void *ignored_arg
)
2452 struct nd_ifinfo
*nd6if
;
2454 lck_rw_lock_shared(nd_if_rwlock
);
2455 for (i
= 1; i
< if_index
+ 1; i
++) {
2456 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
)
2458 nd6if
= &nd_ifinfo
[i
];
2459 if (nd6if
->basereachable
&& /* already initialized */
2460 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
2462 * Since reachable time rarely changes by router
2463 * advertisements, we SHOULD insure that a new random
2464 * value gets recomputed at least once every few hours.
2467 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
2468 nd6if
->reachable
= ND_COMPUTE_RTIME(nd6if
->basereachable
);
2471 lck_rw_done(nd_if_rwlock
);
2472 timeout(nd6_slowtimo
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
2475 #define senderr(e) { error = (e); goto bad;}
2477 nd6_output(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m0
,
2478 struct sockaddr_in6
*dst
, struct rtentry
*hint0
, int locked
)
2480 struct mbuf
*m
= m0
;
2481 struct rtentry
*rt
= hint0
, *hint
= hint0
;
2482 struct llinfo_nd6
*ln
= NULL
;
2484 struct timeval timenow
;
2485 struct rtentry
*rtrele
= NULL
;
2489 RT_ADDREF_LOCKED(rt
);
2492 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
) || !nd6_need_cache(ifp
)) {
2499 * Next hop determination. Because we may involve the gateway route
2500 * in addition to the original route, locking is rather complicated.
2501 * The general concept is that regardless of whether the route points
2502 * to the original route or to the gateway route, this routine takes
2503 * an extra reference on such a route. This extra reference will be
2504 * released at the end.
2506 * Care must be taken to ensure that the "hint0" route never gets freed
2507 * via rtfree(), since the caller may have stored it inside a struct
2508 * route with a reference held for that placeholder.
2510 * This logic is similar to, though not exactly the same as the one
2511 * used by arp_route_to_gateway_route().
2515 * We have a reference to "rt" by now (or below via rtalloc1),
2516 * which will either be released or freed at the end of this
2519 RT_LOCK_ASSERT_HELD(rt
);
2520 if (!(rt
->rt_flags
& RTF_UP
)) {
2521 RT_REMREF_LOCKED(rt
);
2523 if ((hint
= rt
= rtalloc1((struct sockaddr
*)dst
,
2526 if (rt
->rt_ifp
!= ifp
) {
2527 /* XXX: loop care? */
2529 error
= nd6_output(ifp
, origifp
, m0
,
2535 senderr(EHOSTUNREACH
);
2539 if (rt
->rt_flags
& RTF_GATEWAY
) {
2540 struct rtentry
*gwrt
;
2541 struct in6_ifaddr
*ia6
= NULL
;
2542 struct sockaddr_in6 gw6
;
2544 gw6
= *((struct sockaddr_in6
*)rt
->rt_gateway
);
2546 * Must drop rt_lock since nd6_is_addr_neighbor()
2547 * calls nd6_lookup() and acquires rnh_lock.
2552 * We skip link-layer address resolution and NUD
2553 * if the gateway is not a neighbor from ND point
2554 * of view, regardless of the value of nd_ifinfo.flags.
2555 * The second condition is a bit tricky; we skip
2556 * if the gateway is our own address, which is
2557 * sometimes used to install a route to a p2p link.
2559 if (!nd6_is_addr_neighbor(&gw6
, ifp
, 0) ||
2560 (ia6
= in6ifa_ifpwithaddr(ifp
, &gw6
.sin6_addr
))) {
2562 * We allow this kind of tricky route only
2563 * when the outgoing interface is p2p.
2564 * XXX: we may need a more generic rule here.
2567 ifafree(&ia6
->ia_ifa
);
2568 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
2569 senderr(EHOSTUNREACH
);
2574 gw6
= *((struct sockaddr_in6
*)rt
->rt_gateway
);
2576 /* If hint is now down, give up */
2577 if (!(rt
->rt_flags
& RTF_UP
)) {
2579 senderr(EHOSTUNREACH
);
2582 /* If there's no gateway route, look it up */
2583 if ((gwrt
= rt
->rt_gwroute
) == NULL
) {
2587 /* Become a regular mutex */
2588 RT_CONVERT_LOCK(rt
);
2591 * Take gwrt's lock while holding route's lock;
2592 * this is okay since gwrt never points back
2593 * to rt, so no lock ordering issues.
2596 if (!(gwrt
->rt_flags
& RTF_UP
)) {
2597 struct rtentry
*ogwrt
;
2599 rt
->rt_gwroute
= NULL
;
2604 gwrt
= rtalloc1((struct sockaddr
*)&gw6
, 1, 0);
2608 * Bail out if the route is down, no route
2609 * to gateway, circular route, or if the
2610 * gateway portion of "rt" has changed.
2612 if (!(rt
->rt_flags
& RTF_UP
) ||
2613 gwrt
== NULL
|| gwrt
== rt
||
2614 !equal(SA(&gw6
), rt
->rt_gateway
)) {
2616 RT_REMREF_LOCKED(gwrt
);
2622 senderr(EHOSTUNREACH
);
2625 /* Remove any existing gwrt */
2626 ogwrt
= rt
->rt_gwroute
;
2627 if ((rt
->rt_gwroute
= gwrt
) != NULL
)
2631 /* Now free the replaced gwrt */
2634 /* If still no route to gateway, bail out */
2636 senderr(EHOSTUNREACH
);
2637 /* Remember to release/free "rt" at the end */
2641 /* If gwrt is now down, give up */
2642 if (!(rt
->rt_flags
& RTF_UP
)) {
2646 /* "rtrele" == original "rt" */
2647 senderr(EHOSTUNREACH
);
2650 RT_ADDREF_LOCKED(gwrt
);
2654 /* If gwrt is now down, give up */
2655 if (!(gwrt
->rt_flags
& RTF_UP
)) {
2658 senderr(EHOSTUNREACH
);
2660 /* Remember to release/free "rt" at the end */
2665 /* Become a regular mutex */
2666 RT_CONVERT_LOCK(rt
);
2670 RT_LOCK_ASSERT_HELD(rt
);
2673 * Address resolution or Neighbor Unreachability Detection
2675 * At this point, the destination of the packet must be a unicast
2676 * or an anycast address(i.e. not a multicast).
2679 /* Look up the neighbor cache for the nexthop */
2680 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0) {
2684 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2685 * the condition below is not very efficient. But we believe
2686 * it is tolerable, because this should be a rare case.
2687 * Must drop rt_lock since nd6_is_addr_neighbor() calls
2688 * nd6_lookup() and acquires rnh_lock.
2692 if (nd6_is_addr_neighbor(dst
, ifp
, 0)) {
2693 /* "rtrele" may have been used, so clean up "rt" now */
2695 /* Don't free "hint0" */
2701 /* Callee returns a locked route upon success */
2702 rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
, 0);
2704 RT_LOCK_ASSERT_HELD(rt
);
2707 } else if (rt
!= NULL
) {
2715 lck_rw_lock_shared(nd_if_rwlock
);
2716 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0 &&
2717 !(nd_ifinfo
[ifp
->if_index
].flags
& ND6_IFF_PERFORMNUD
)) {
2718 lck_rw_done(nd_if_rwlock
);
2720 "nd6_output: can't allocate llinfo for %s "
2722 ip6_sprintf(&dst
->sin6_addr
), ln
, rt
);
2723 senderr(EIO
); /* XXX: good error? */
2725 lck_rw_done(nd_if_rwlock
);
2727 goto sendpkt
; /* send anyway */
2730 getmicrotime(&timenow
);
2732 /* We don't have to do link-layer address resolution on a p2p link. */
2733 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
2734 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
2735 ln
->ln_state
= ND6_LLINFO_STALE
;
2736 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
, nd6_gctimer
);
2740 * The first time we send a packet to a neighbor whose entry is
2741 * STALE, we have to change the state to DELAY and a sets a timer to
2742 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2743 * neighbor unreachability detection on expiration.
2746 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
2748 ln
->ln_state
= ND6_LLINFO_DELAY
;
2749 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
, nd6_delay
);
2753 * If the neighbor cache entry has a state other than INCOMPLETE
2754 * (i.e. its link-layer address is already resolved), just
2757 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) {
2760 * Move this entry to the head of the queue so that it is
2761 * less likely for this entry to be a target of forced
2762 * garbage collection (see nd6_rtrequest()).
2764 lck_mtx_lock(rnh_lock
);
2766 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
2771 lck_mtx_unlock(rnh_lock
);
2776 * There is a neighbor cache entry, but no ethernet address
2777 * response yet. Replace the held mbuf (if any) with this
2780 * This code conforms to the rate-limiting rule described in Section
2781 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
2784 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
)
2785 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
2787 m_freem(ln
->ln_hold
);
2789 if (ln
->ln_expire
&& ln
->ln_asked
< nd6_mmaxtries
&&
2790 ln
->ln_expire
< timenow
.tv_sec
) {
2792 lck_rw_lock_shared(nd_if_rwlock
);
2793 ln
->ln_expire
= timenow
.tv_sec
+
2794 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
2795 lck_rw_done(nd_if_rwlock
);
2797 /* We still have a reference on rt (for ln) */
2798 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0, locked
);
2803 * Move this entry to the head of the queue so that it is
2804 * less likely for this entry to be a target of forced
2805 * garbage collection (see nd6_rtrequest()).
2807 lck_mtx_lock(rnh_lock
);
2809 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
2813 /* Clean up "rt" now while we can */
2815 RT_REMREF_LOCKED(rt
);
2821 rt
= NULL
; /* "rt" has been taken care of */
2822 lck_mtx_unlock(rnh_lock
);
2829 RT_LOCK_ASSERT_NOTHELD(rt
);
2831 /* Clean up HW checksum flags before sending the packet */
2832 m
->m_pkthdr
.csum_data
= 0;
2833 m
->m_pkthdr
.csum_flags
= 0;
2835 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
2836 /* forwarding rules require the original scope_id */
2837 m
->m_pkthdr
.rcvif
= origifp
;
2839 lck_mtx_unlock(ip6_mutex
);
2840 error
= dlil_output(origifp
, PF_INET6
, m
, (caddr_t
)rt
,
2841 (struct sockaddr
*)dst
, 0);
2843 lck_mtx_lock(ip6_mutex
);
2846 /* Do not allow loopback address to wind up on a wire */
2847 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2849 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
2850 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
))) {
2851 ip6stat
.ip6s_badscope
++;
2853 * Do not simply drop the packet just like a
2854 * firewall -- we want the the application to feel
2855 * the pain. Return ENETUNREACH like ip6_output
2856 * does in some similar cases. This can startle
2857 * the otherwise clueless process that specifies
2858 * loopback as the source address.
2860 error
= ENETUNREACH
;
2865 m
->m_pkthdr
.rcvif
= NULL
;
2867 lck_mtx_unlock(ip6_mutex
);
2868 error
= dlil_output(ifp
, PF_INET6
, m
, (caddr_t
)rt
,
2869 (struct sockaddr
*)dst
, 0);
2871 lck_mtx_lock(ip6_mutex
);
2879 /* Clean up "rt" unless it's already been done */
2883 RT_REMREF_LOCKED(rt
);
2890 /* And now clean up "rtrele" if there is any */
2891 if (rtrele
!= NULL
) {
2892 RT_LOCK_SPIN(rtrele
);
2893 if (rtrele
== hint0
) {
2894 RT_REMREF_LOCKED(rtrele
);
2910 * XXX: we currently do not make neighbor cache on any interface
2911 * other than ARCnet, Ethernet, FDDI and GIF.
2914 * - unidirectional tunnels needs no ND
2916 switch (ifp
->if_type
) {
2922 case IFT_IEEE8023ADLAG
:
2927 case IFT_GIF
: /* XXX need more cases? */
2939 struct sockaddr
*dst
,
2943 struct sockaddr_dl
*sdl
;
2945 if (m
->m_flags
& M_MCAST
) {
2946 switch (ifp
->if_type
) {
2950 case IFT_IEEE8023ADLAG
:
2955 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
2959 for (i
= 0; i
< ifp
->if_addrlen
; i
++)
2966 return(0); /* caller will free mbuf */
2971 /* this could happen, if we could not allocate memory */
2972 return(0); /* caller will free mbuf */
2975 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
2976 printf("nd6_storelladdr: something odd happens\n");
2978 return(0); /* caller will free mbuf */
2980 sdl
= SDL(rt
->rt_gateway
);
2981 if (sdl
->sdl_alen
== 0) {
2982 /* this should be impossible, but we bark here for debugging */
2983 printf("nd6_storelladdr: sdl_alen == 0\n");
2985 return(0); /* caller will free mbuf */
2988 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
2994 * This is the ND pre-output routine; care must be taken to ensure that
2995 * the "hint" route never gets freed via rtfree(), since the caller may
2996 * have stored it inside a struct route with a reference held for that
3000 nd6_lookup_ipv6(ifnet_t ifp
, const struct sockaddr_in6
*ip6_dest
,
3001 struct sockaddr_dl
*ll_dest
, size_t ll_dest_len
, route_t hint
,
3004 route_t route
= hint
;
3006 struct sockaddr_dl
*sdl
= NULL
;
3009 if (ip6_dest
->sin6_family
!= AF_INET6
)
3010 return (EAFNOSUPPORT
);
3012 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
3017 * Callee holds a reference on the route and returns
3018 * with the route entry locked, upon success.
3020 result
= arp_route_to_gateway_route(
3021 (const struct sockaddr
*)ip6_dest
, hint
, &route
);
3025 RT_LOCK_ASSERT_HELD(route
);
3028 if ((packet
->m_flags
& M_MCAST
) != 0) {
3031 result
= dlil_resolve_multi(ifp
,
3032 (const struct sockaddr
*)ip6_dest
,
3033 (struct sockaddr
*)ll_dest
, ll_dest_len
);
3039 if (route
== NULL
) {
3041 * This could happen, if we could not allocate memory or
3042 * if arp_route_to_gateway_route() didn't return a route.
3048 if (route
->rt_gateway
->sa_family
!= AF_LINK
) {
3049 printf("nd6_lookup_ipv6: gateway address not AF_LINK\n");
3050 result
= EADDRNOTAVAIL
;
3054 sdl
= SDL(route
->rt_gateway
);
3055 if (sdl
->sdl_alen
== 0) {
3056 /* this should be impossible, but we bark here for debugging */
3057 printf("nd6_lookup_ipv6: sdl_alen == 0\n");
3058 result
= EHOSTUNREACH
;
3062 copy_len
= sdl
->sdl_len
<= ll_dest_len
? sdl
->sdl_len
: ll_dest_len
;
3063 bcopy(sdl
, ll_dest
, copy_len
);
3066 if (route
!= NULL
) {
3067 if (route
== hint
) {
3068 RT_REMREF_LOCKED(route
);
3078 SYSCTL_DECL(_net_inet6_icmp6
);
3081 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
3083 #pragma unused(oidp, arg1, arg2)
3086 struct nd_defrouter
*dr
;
3087 int p64
= proc_is64bit(req
->p
);
3092 lck_mtx_lock(nd6_mutex
);
3094 struct in6_defrouter_64
*d
, *de
;
3096 for (dr
= TAILQ_FIRST(&nd_defrouter
);
3098 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
3099 d
= (struct in6_defrouter_64
*)buf
;
3100 de
= (struct in6_defrouter_64
*)(buf
+ sizeof (buf
));
3103 bzero(d
, sizeof (*d
));
3104 d
->rtaddr
.sin6_family
= AF_INET6
;
3105 d
->rtaddr
.sin6_len
= sizeof (d
->rtaddr
);
3106 if (in6_recoverscope(&d
->rtaddr
, &dr
->rtaddr
,
3110 "default router list (%s)\n",
3111 ip6_sprintf(&dr
->rtaddr
));
3112 d
->flags
= dr
->flags
;
3113 d
->rtlifetime
= dr
->rtlifetime
;
3114 d
->expire
= dr
->expire
;
3115 d
->if_index
= dr
->ifp
->if_index
;
3117 panic("buffer too short");
3119 error
= SYSCTL_OUT(req
, buf
, sizeof (*d
));
3124 struct in6_defrouter_32
*d_32
, *de_32
;
3126 for (dr
= TAILQ_FIRST(&nd_defrouter
);
3128 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
3129 d_32
= (struct in6_defrouter_32
*)buf
;
3130 de_32
= (struct in6_defrouter_32
*)(buf
+ sizeof (buf
));
3132 if (d_32
+ 1 <= de_32
) {
3133 bzero(d_32
, sizeof (*d_32
));
3134 d_32
->rtaddr
.sin6_family
= AF_INET6
;
3135 d_32
->rtaddr
.sin6_len
= sizeof (d_32
->rtaddr
);
3136 if (in6_recoverscope(&d_32
->rtaddr
, &dr
->rtaddr
,
3140 "default router list (%s)\n",
3141 ip6_sprintf(&dr
->rtaddr
));
3142 d_32
->flags
= dr
->flags
;
3143 d_32
->rtlifetime
= dr
->rtlifetime
;
3144 d_32
->expire
= dr
->expire
;
3145 d_32
->if_index
= dr
->ifp
->if_index
;
3147 panic("buffer too short");
3149 error
= SYSCTL_OUT(req
, buf
, sizeof (*d_32
));
3154 lck_mtx_unlock(nd6_mutex
);
3159 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
3161 #pragma unused(oidp, arg1, arg2)
3164 struct nd_prefix
*pr
;
3165 int p64
= proc_is64bit(req
->p
);
3170 lck_mtx_lock(nd6_mutex
);
3172 struct in6_prefix_64
*p
, *pe
;
3174 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3175 u_short advrtrs
= 0;
3177 struct sockaddr_in6
*sin6
, *s6
;
3178 struct nd_pfxrouter
*pfr
;
3180 p
= (struct in6_prefix_64
*)buf
;
3181 pe
= (struct in6_prefix_64
*)(buf
+ sizeof (buf
));
3184 bzero(p
, sizeof (*p
));
3185 sin6
= (struct sockaddr_in6
*)(p
+ 1);
3187 p
->prefix
= pr
->ndpr_prefix
;
3188 if (in6_recoverscope(&p
->prefix
,
3189 &p
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
3191 "scope error in prefix list (%s)\n",
3192 ip6_sprintf(&p
->prefix
.sin6_addr
));
3193 p
->raflags
= pr
->ndpr_raf
;
3194 p
->prefixlen
= pr
->ndpr_plen
;
3195 p
->vltime
= pr
->ndpr_vltime
;
3196 p
->pltime
= pr
->ndpr_pltime
;
3197 p
->if_index
= pr
->ndpr_ifp
->if_index
;
3198 p
->expire
= pr
->ndpr_expire
;
3199 p
->refcnt
= pr
->ndpr_refcnt
;
3200 p
->flags
= pr
->ndpr_stateflags
;
3201 p
->origin
= PR_ORIG_RA
;
3203 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
3205 pfr
= pfr
->pfr_next
) {
3206 if ((void *)&sin6
[advrtrs
+ 1] >
3211 s6
= &sin6
[advrtrs
];
3212 bzero(s6
, sizeof (*s6
));
3213 s6
->sin6_family
= AF_INET6
;
3214 s6
->sin6_len
= sizeof (*sin6
);
3215 if (in6_recoverscope(s6
,
3216 &pfr
->router
->rtaddr
,
3217 pfr
->router
->ifp
) != 0)
3218 log(LOG_ERR
, "scope error in "
3219 "prefix list (%s)\n",
3220 ip6_sprintf(&pfr
->router
->
3224 p
->advrtrs
= advrtrs
;
3226 panic("buffer too short");
3228 advance
= sizeof (*p
) + sizeof (*sin6
) * advrtrs
;
3229 error
= SYSCTL_OUT(req
, buf
, advance
);
3234 struct in6_prefix_32
*p_32
, *pe_32
;
3236 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3237 u_short advrtrs
= 0;
3239 struct sockaddr_in6
*sin6
, *s6
;
3240 struct nd_pfxrouter
*pfr
;
3242 p_32
= (struct in6_prefix_32
*)buf
;
3243 pe_32
= (struct in6_prefix_32
*)(buf
+ sizeof (buf
));
3245 if (p_32
+ 1 <= pe_32
) {
3246 bzero(p_32
, sizeof (*p_32
));
3247 sin6
= (struct sockaddr_in6
*)(p_32
+ 1);
3249 p_32
->prefix
= pr
->ndpr_prefix
;
3250 if (in6_recoverscope(&p_32
->prefix
,
3251 &p_32
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
3252 log(LOG_ERR
, "scope error in prefix "
3253 "list (%s)\n", ip6_sprintf(&p_32
->
3255 p_32
->raflags
= pr
->ndpr_raf
;
3256 p_32
->prefixlen
= pr
->ndpr_plen
;
3257 p_32
->vltime
= pr
->ndpr_vltime
;
3258 p_32
->pltime
= pr
->ndpr_pltime
;
3259 p_32
->if_index
= pr
->ndpr_ifp
->if_index
;
3260 p_32
->expire
= pr
->ndpr_expire
;
3261 p_32
->refcnt
= pr
->ndpr_refcnt
;
3262 p_32
->flags
= pr
->ndpr_stateflags
;
3263 p_32
->origin
= PR_ORIG_RA
;
3265 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
3267 pfr
= pfr
->pfr_next
) {
3268 if ((void *)&sin6
[advrtrs
+ 1] >
3273 s6
= &sin6
[advrtrs
];
3274 bzero(s6
, sizeof (*s6
));
3275 s6
->sin6_family
= AF_INET6
;
3276 s6
->sin6_len
= sizeof (*sin6
);
3277 if (in6_recoverscope(s6
,
3278 &pfr
->router
->rtaddr
,
3279 pfr
->router
->ifp
) != 0)
3280 log(LOG_ERR
, "scope error in "
3281 "prefix list (%s)\n",
3282 ip6_sprintf(&pfr
->router
->
3286 p_32
->advrtrs
= advrtrs
;
3288 panic("buffer too short");
3290 advance
= sizeof (*p_32
) + sizeof (*sin6
) * advrtrs
;
3291 error
= SYSCTL_OUT(req
, buf
, advance
);
3296 lck_mtx_unlock(nd6_mutex
);
3299 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
3300 CTLFLAG_RD
, 0, 0, nd6_sysctl_drlist
, "S,in6_defrouter","");
3301 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
3302 CTLFLAG_RD
, 0, 0, nd6_sysctl_prlist
, "S,in6_defrouter","");