1 /* $FreeBSD: src/sys/netinet6/nd6.c,v 1.20 2002/08/02 20:49:14 rwatson Exp $ */
2 /* $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * BSD/OS version heavily modifies this code, related to llinfo.
37 * Since we don't have BSD/OS version of net/route.c in our hand,
38 * I left the code mostly as it was in 970310. -- itojun
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50 #include <sys/errno.h>
51 #include <sys/syslog.h>
52 #include <sys/protosw.h>
53 #include <kern/queue.h>
54 #include <kern/lock.h>
56 #define DONT_WARN_OBSOLETE
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/if_atm.h>
61 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
66 #include <netinet/if_fddi.h>
67 #include <netinet6/in6_var.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/nd6.h>
71 #include <netinet6/in6_prefix.h>
72 #include <netinet/icmp6.h>
76 #include <net/net_osdep.h>
78 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
79 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
81 #define SIN6(s) ((struct sockaddr_in6 *)s)
82 #define SDL(s) ((struct sockaddr_dl *)s)
85 int nd6_prune
= 1; /* walk list every 1 seconds */
86 int nd6_delay
= 5; /* delay first probe time 5 second */
87 int nd6_umaxtries
= 3; /* maximum unicast query */
88 int nd6_mmaxtries
= 3; /* maximum multicast query */
89 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
90 int nd6_gctimer
= (60 * 60 * 24); /* 1 day: garbage collection timer */
92 /* preventing too many loops in ND option parsing */
93 int nd6_maxndopt
= 10; /* max # of ND options allowed */
95 int nd6_maxnudhint
= 0; /* max # of subsequent upper layer hints */
104 static int nd6_inuse
, nd6_allocated
;
106 struct llinfo_nd6 llinfo_nd6
= {&llinfo_nd6
, &llinfo_nd6
, NULL
, NULL
, 0, 0, 0, 0, 0 };
107 size_t nd_ifinfo_indexlim
= 8;
108 struct nd_ifinfo
*nd_ifinfo
= NULL
;
109 struct nd_drhead nd_defrouter
;
110 struct nd_prhead nd_prefix
= { 0 };
112 int nd6_recalc_reachtm_interval
= ND6_RECALC_REACHTM_INTERVAL
;
113 static struct sockaddr_in6 all1_sa
;
115 static int regen_tmpaddr(struct in6_ifaddr
*);
116 extern lck_mtx_t
*rt_mtx
;
117 extern lck_mtx_t
*ip6_mutex
;
118 extern lck_mtx_t
*nd6_mutex
;
120 static void nd6_slowtimo(void *ignored_arg
);
125 static int nd6_init_done
= 0;
129 log(LOG_NOTICE
, "nd6_init called more than once(ignored)\n");
133 all1_sa
.sin6_family
= AF_INET6
;
134 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
135 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++)
136 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
138 /* initialization of the default router list */
139 TAILQ_INIT(&nd_defrouter
);
144 timeout(nd6_slowtimo
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
153 * We have some arrays that should be indexed by if_index.
154 * since if_index will grow dynamically, they should grow too.
156 if (nd_ifinfo
== NULL
|| if_index
>= nd_ifinfo_indexlim
) {
160 while (if_index
>= nd_ifinfo_indexlim
)
161 nd_ifinfo_indexlim
<<= 1;
164 n
= nd_ifinfo_indexlim
* sizeof(struct nd_ifinfo
);
165 q
= (caddr_t
)_MALLOC(n
, M_IP6NDP
, M_WAITOK
);
168 bcopy((caddr_t
)nd_ifinfo
, q
, n
/2);
169 FREE((caddr_t
)nd_ifinfo
, M_IP6NDP
);
171 nd_ifinfo
= (struct nd_ifinfo
*)q
;
174 #define ND nd_ifinfo[ifp->if_index]
177 * Don't initialize if called twice.
178 * XXX: to detect this, we should choose a member that is never set
179 * before initialization of the ND structure itself. We formaly used
180 * the linkmtu member, which was not suitable because it could be
181 * initialized via "ifconfig mtu".
183 if (ND
.basereachable
)
186 ND
.linkmtu
= ifindex2ifnet
[ifp
->if_index
]->if_mtu
;
187 ND
.chlim
= IPV6_DEFHLIM
;
188 ND
.basereachable
= REACHABLE_TIME
;
189 ND
.reachable
= ND_COMPUTE_RTIME(ND
.basereachable
);
190 ND
.retrans
= RETRANS_TIMER
;
192 ND
.flags
= ND6_IFF_PERFORMNUD
;
198 * Reset ND level link MTU. This function is called when the physical MTU
199 * changes, which means we might have to adjust the ND level MTU.
202 nd6_setmtu(struct ifnet
*ifp
)
204 struct nd_ifinfo
*ndi
;
208 * Make sure IPv6 is enabled for the interface first,
209 * because this can be called directly from SIOCSIFMTU for IPv4
212 if (ifp
->if_index
>= nd_ifinfo_indexlim
) {
213 return; /* we're out of bound for nd_ifinfo */
216 ndi
= &nd_ifinfo
[ifp
->if_index
];
217 oldmaxmtu
= ndi
->maxmtu
;
220 * The ND level maxmtu is somewhat redundant to the interface MTU
221 * and is an implementation artifact of KAME. Instead of hard-
222 * limiting the maxmtu based on the interface type here, we simply
223 * take the if_mtu value since SIOCSIFMTU would have taken care of
224 * the sanity checks related to the maximum MTU allowed for the
225 * interface (a value that is known only by the interface layer),
226 * by sending the request down via ifnet_ioctl(). The use of the
227 * ND level maxmtu and linkmtu (the latter obtained via RA) are done
228 * via IN6_LINKMTU() which does further checking against if_mtu.
230 ndi
->maxmtu
= ifp
->if_mtu
;
233 * Decreasing the interface MTU under IPV6 minimum MTU may cause
234 * undesirable situation. We thus notify the operator of the change
235 * explicitly. The check for oldmaxmtu is necessary to restrict the
236 * log to the case of changing the MTU, not initializing it.
238 if (oldmaxmtu
>= IPV6_MMTU
&& ndi
->maxmtu
< IPV6_MMTU
) {
239 log(LOG_NOTICE
, "nd6_setmtu: "
240 "new link MTU on %s%d (%lu) is too small for IPv6\n",
241 ifp
->if_name
, ifp
->if_unit
, (unsigned long)ndi
->maxmtu
);
244 /* also adjust in6_maxmtu if necessary. */
245 if (ndi
->maxmtu
> in6_maxmtu
)
253 union nd_opts
*ndopts
)
255 bzero(ndopts
, sizeof(*ndopts
));
256 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
258 = (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
261 ndopts
->nd_opts_done
= 1;
262 ndopts
->nd_opts_search
= NULL
;
267 * Take one ND option.
271 union nd_opts
*ndopts
)
273 struct nd_opt_hdr
*nd_opt
;
277 panic("ndopts == NULL in nd6_option\n");
278 if (!ndopts
->nd_opts_last
)
279 panic("uninitialized ndopts in nd6_option\n");
280 if (!ndopts
->nd_opts_search
)
282 if (ndopts
->nd_opts_done
)
285 nd_opt
= ndopts
->nd_opts_search
;
287 /* make sure nd_opt_len is inside the buffer */
288 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
289 bzero(ndopts
, sizeof(*ndopts
));
293 olen
= nd_opt
->nd_opt_len
<< 3;
296 * Message validation requires that all included
297 * options have a length that is greater than zero.
299 bzero(ndopts
, sizeof(*ndopts
));
303 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
304 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
305 /* option overruns the end of buffer, invalid */
306 bzero(ndopts
, sizeof(*ndopts
));
308 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
309 /* reached the end of options chain */
310 ndopts
->nd_opts_done
= 1;
311 ndopts
->nd_opts_search
= NULL
;
317 * Parse multiple ND options.
318 * This function is much easier to use, for ND routines that do not need
319 * multiple options of the same type.
323 union nd_opts
*ndopts
)
325 struct nd_opt_hdr
*nd_opt
;
329 panic("ndopts == NULL in nd6_options\n");
330 if (!ndopts
->nd_opts_last
)
331 panic("uninitialized ndopts in nd6_options\n");
332 if (!ndopts
->nd_opts_search
)
336 nd_opt
= nd6_option(ndopts
);
337 if (!nd_opt
&& !ndopts
->nd_opts_last
) {
339 * Message validation requires that all included
340 * options have a length that is greater than zero.
342 icmp6stat
.icp6s_nd_badopt
++;
343 bzero(ndopts
, sizeof(*ndopts
));
350 switch (nd_opt
->nd_opt_type
) {
351 case ND_OPT_SOURCE_LINKADDR
:
352 case ND_OPT_TARGET_LINKADDR
:
354 case ND_OPT_REDIRECTED_HEADER
:
355 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
357 "duplicated ND6 option found (type=%d)\n",
358 nd_opt
->nd_opt_type
));
361 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
365 case ND_OPT_PREFIX_INFORMATION
:
366 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
367 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
370 ndopts
->nd_opts_pi_end
=
371 (struct nd_opt_prefix_info
*)nd_opt
;
375 * Unknown options must be silently ignored,
376 * to accomodate future extension to the protocol.
379 "nd6_options: unsupported option %d - "
380 "option ignored\n", nd_opt
->nd_opt_type
));
385 if (i
> nd6_maxndopt
) {
386 icmp6stat
.icp6s_nd_toomanyopt
++;
387 nd6log((LOG_INFO
, "too many loop in nd opt\n"));
391 if (ndopts
->nd_opts_done
)
399 * ND6 timer routine to expire default route list and prefix list
403 __unused
void *ignored_arg
)
405 struct llinfo_nd6
*ln
;
406 struct nd_defrouter
*dr
;
407 struct nd_prefix
*pr
;
408 struct ifnet
*ifp
= NULL
;
409 struct in6_ifaddr
*ia6
, *nia6
;
410 struct in6_addrlifetime
*lt6
;
411 struct timeval timenow
;
413 getmicrotime(&timenow
);
417 ln
= llinfo_nd6
.ln_next
;
418 while (ln
&& ln
!= &llinfo_nd6
) {
420 struct sockaddr_in6
*dst
;
421 struct llinfo_nd6
*next
= ln
->ln_next
;
422 /* XXX: used for the DELAY case only: */
423 struct nd_ifinfo
*ndi
= NULL
;
425 if ((rt
= ln
->ln_rt
) == NULL
) {
429 if ((ifp
= rt
->rt_ifp
) == NULL
) {
433 ndi
= &nd_ifinfo
[ifp
->if_index
];
434 dst
= (struct sockaddr_in6
*)rt_key(rt
);
436 if (ln
->ln_expire
> timenow
.tv_sec
) {
443 printf("rt=0 in nd6_timer(ln=%p)\n", ln
);
447 if (rt
->rt_llinfo
&& (struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
) {
448 printf("rt_llinfo(%p) is not equal to ln(%p)\n",
454 printf("dst=0 in nd6_timer(ln=%p)\n", ln
);
459 switch (ln
->ln_state
) {
460 case ND6_LLINFO_INCOMPLETE
:
461 if (ln
->ln_asked
< nd6_mmaxtries
) {
463 ln
->ln_expire
= timenow
.tv_sec
+
464 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
465 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
,
468 struct mbuf
*m
= ln
->ln_hold
;
473 * Fake rcvif to make ICMP error
474 * more helpful in diagnosing
476 * XXX: should we consider
479 m
->m_pkthdr
.rcvif
= rt
->rt_ifp
;
481 icmp6_error(m
, ICMP6_DST_UNREACH
,
482 ICMP6_DST_UNREACH_ADDR
, 0);
488 case ND6_LLINFO_REACHABLE
:
490 ln
->ln_state
= ND6_LLINFO_STALE
;
491 ln
->ln_expire
= timenow
.tv_sec
+ nd6_gctimer
;
495 case ND6_LLINFO_STALE
:
496 /* Garbage Collection(RFC 2461 5.3) */
501 case ND6_LLINFO_DELAY
:
502 if (ndi
&& (ndi
->flags
& ND6_IFF_PERFORMNUD
) != 0) {
505 ln
->ln_state
= ND6_LLINFO_PROBE
;
506 ln
->ln_expire
= timenow
.tv_sec
+
508 nd6_ns_output(ifp
, &dst
->sin6_addr
,
512 ln
->ln_state
= ND6_LLINFO_STALE
; /* XXX */
513 ln
->ln_expire
= timenow
.tv_sec
+ nd6_gctimer
;
516 case ND6_LLINFO_PROBE
:
517 if (ln
->ln_asked
< nd6_umaxtries
) {
519 ln
->ln_expire
= timenow
.tv_sec
+
520 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
521 nd6_ns_output(ifp
, &dst
->sin6_addr
,
522 &dst
->sin6_addr
, ln
, 0, 0);
531 /* expire default router list */
532 lck_mtx_lock(nd6_mutex
);
533 dr
= TAILQ_FIRST(&nd_defrouter
);
535 if (dr
->expire
&& dr
->expire
< timenow
.tv_sec
) {
536 struct nd_defrouter
*t
;
537 t
= TAILQ_NEXT(dr
, dr_entry
);
538 defrtrlist_del(dr
, 1);
541 dr
= TAILQ_NEXT(dr
, dr_entry
);
546 * expire interface addresses.
547 * in the past the loop was inside prefix expiry processing.
548 * However, from a stricter speci-confrmance standpoint, we should
549 * rather separate address lifetimes and prefix lifetimes.
552 for (ia6
= in6_ifaddrs
; ia6
; ia6
= nia6
) {
554 /* check address lifetime */
555 lt6
= &ia6
->ia6_lifetime
;
556 if (IFA6_IS_INVALID(ia6
)) {
560 * If the expiring address is temporary, try
561 * regenerating a new one. This would be useful when
562 * we suspended a laptop PC, then turned it on after a
563 * period that could invalidate all temporary
564 * addresses. Although we may have to restart the
565 * loop (see below), it must be after purging the
566 * address. Otherwise, we'd see an infinite loop of
569 if (ip6_use_tempaddr
&&
570 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
571 /* NOTE: We have to drop the lock here because
572 * regen_tmpaddr() eventually calls in6_update_ifa(),
573 * which must take the lock and would otherwise cause a
574 * hang. This is safe because the goto addrloop
575 * leads to a reevaluation of the in6_ifaddrs list
577 lck_mtx_unlock(nd6_mutex
);
578 if (regen_tmpaddr(ia6
) == 0)
580 lck_mtx_lock(nd6_mutex
);
583 in6_purgeaddr(&ia6
->ia_ifa
, 1);
586 goto addrloop
; /* XXX: see below */
588 if (IFA6_IS_DEPRECATED(ia6
)) {
589 int oldflags
= ia6
->ia6_flags
;
591 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
594 * If a temporary address has just become deprecated,
595 * regenerate a new one if possible.
597 if (ip6_use_tempaddr
&&
598 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
599 (oldflags
& IN6_IFF_DEPRECATED
) == 0) {
602 lck_mtx_unlock(nd6_mutex
);
603 if (regen_tmpaddr(ia6
) == 0) {
605 * A new temporary address is
607 * XXX: this means the address chain
608 * has changed while we are still in
609 * the loop. Although the change
610 * would not cause disaster (because
611 * it's not a deletion, but an
612 * addition,) we'd rather restart the
613 * loop just for safety. Or does this
614 * significantly reduce performance??
616 lck_mtx_lock(nd6_mutex
);
619 lck_mtx_lock(nd6_mutex
);
623 * A new RA might have made a deprecated address
626 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
630 /* expire prefix list */
631 pr
= nd_prefix
.lh_first
;
634 * check prefix lifetime.
635 * since pltime is just for autoconf, pltime processing for
636 * prefix is not necessary.
638 if (pr
->ndpr_expire
&& pr
->ndpr_expire
< timenow
.tv_sec
) {
643 * address expiration and prefix expiration are
644 * separate. NEVER perform in6_purgeaddr here.
647 prelist_remove(pr
, 1);
652 lck_mtx_unlock(nd6_mutex
);
653 timeout(nd6_timer
, (caddr_t
)0, nd6_prune
* hz
);
658 struct in6_ifaddr
*ia6
) /* deprecated/invalidated temporary address */
662 struct in6_ifaddr
*public_ifa6
= NULL
;
663 struct timeval timenow
;
665 getmicrotime(&timenow
);
667 ifp
= ia6
->ia_ifa
.ifa_ifp
;
668 ifnet_lock_exclusive(ifp
);
669 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
670 ifa
= ifa
->ifa_list
.tqe_next
)
672 struct in6_ifaddr
*it6
;
674 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
677 it6
= (struct in6_ifaddr
*)ifa
;
679 /* ignore no autoconf addresses. */
680 if ((it6
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
683 /* ignore autoconf addresses with different prefixes. */
684 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
)
688 * Now we are looking at an autoconf address with the same
689 * prefix as ours. If the address is temporary and is still
690 * preferred, do not create another one. It would be rare, but
691 * could happen, for example, when we resume a laptop PC after
694 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
695 !IFA6_IS_DEPRECATED(it6
)) {
701 * This is a public autoconf address that has the same prefix
702 * as ours. If it is preferred, keep it. We can't break the
703 * loop here, because there may be a still-preferred temporary
704 * address with the prefix.
706 if (!IFA6_IS_DEPRECATED(it6
))
709 ifnet_lock_done(ifp
);
711 if (public_ifa6
!= NULL
) {
714 if ((e
= in6_tmpifadd(public_ifa6
, 0)) != 0) {
715 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
716 " tmp addr,errno=%d\n", e
);
726 * Nuke neighbor cache/prefix/default router management table, right before
733 struct llinfo_nd6
*ln
, *nln
;
734 struct nd_defrouter
*dr
, *ndr
, drany
;
735 struct nd_prefix
*pr
, *npr
;
737 /* Nuke default router list entries toward ifp */
738 lck_mtx_lock(nd6_mutex
);
739 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
741 * The first entry of the list may be stored in
742 * the routing table, so we'll delete it later.
744 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= ndr
) {
745 ndr
= TAILQ_NEXT(dr
, dr_entry
);
747 defrtrlist_del(dr
, 1);
749 dr
= TAILQ_FIRST(&nd_defrouter
);
751 defrtrlist_del(dr
, 1);
754 /* Nuke prefix list entries toward ifp */
755 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
757 if (pr
->ndpr_ifp
== ifp
) {
759 * Previously, pr->ndpr_addr is removed as well,
760 * but I strongly believe we don't have to do it.
761 * nd6_purge() is only called from in6_ifdetach(),
762 * which removes all the associated interface addresses
764 * (jinmei@kame.net 20010129)
766 prelist_remove(pr
, 1);
770 /* cancel default outgoing interface setting */
771 if (nd6_defifindex
== ifp
->if_index
)
772 nd6_setdefaultiface(0);
774 if (!ip6_forwarding
&& (ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
775 /* refresh default router list */
776 bzero(&drany
, sizeof(drany
));
777 defrouter_delreq(&drany
, 0);
780 lck_mtx_unlock(nd6_mutex
);
783 * Nuke neighbor cache entries for the ifp.
784 * Note that rt->rt_ifp may not be the same as ifp,
785 * due to KAME goto ours hack. See RTM_RESOLVE case in
786 * nd6_rtrequest(), and ip6_input().
788 ln
= llinfo_nd6
.ln_next
;
789 while (ln
&& ln
!= &llinfo_nd6
) {
791 struct sockaddr_dl
*sdl
;
795 if (rt
&& rt
->rt_gateway
&&
796 rt
->rt_gateway
->sa_family
== AF_LINK
) {
797 sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
;
798 if (sdl
->sdl_index
== ifp
->if_index
)
807 struct in6_addr
*addr6
,
813 struct sockaddr_in6 sin6
;
815 bzero(&sin6
, sizeof(sin6
));
816 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
817 sin6
.sin6_family
= AF_INET6
;
818 sin6
.sin6_addr
= *addr6
;
820 sin6
.sin6_scope_id
= in6_addr2scopeid(ifp
, addr6
);
823 lck_mtx_lock(rt_mtx
);
824 rt
= rtalloc1_locked((struct sockaddr
*)&sin6
, create
, 0UL);
825 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) == 0) {
827 * This is the case for the default route.
828 * If we want to create a neighbor cache for the address, we
829 * should free the route for the destination and allocate an
842 * If no route is available and create is set,
843 * we allocate a host route for the destination
844 * and treat it like an interface route.
845 * This hack is necessary for a neighbor which can't
846 * be covered by our own prefix.
849 ifaof_ifpforaddr((struct sockaddr
*)&sin6
, ifp
);
852 lck_mtx_unlock(rt_mtx
);
857 * Create a new route. RTF_LLINFO is necessary
858 * to create a Neighbor Cache entry for the
859 * destination in nd6_rtrequest which will be
860 * called in rtrequest via ifa->ifa_rtrequest.
862 if ((e
= rtrequest_locked(RTM_ADD
, (struct sockaddr
*)&sin6
,
864 (struct sockaddr
*)&all1_sa
,
866 RTF_HOST
| RTF_LLINFO
) &
871 "nd6_lookup: failed to add route for a "
872 "neighbor(%s), errno=%d\n",
873 ip6_sprintf(addr6
), e
);
878 lck_mtx_unlock(rt_mtx
);
882 struct llinfo_nd6
*ln
=
883 (struct llinfo_nd6
*)rt
->rt_llinfo
;
884 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
888 lck_mtx_unlock(rt_mtx
);
894 * Validation for the entry.
895 * Note that the check for rt_llinfo is necessary because a cloned
896 * route from a parent route that has the L flag (e.g. the default
897 * route to a p2p interface) may have the flag, too, while the
898 * destination is not actually a neighbor.
899 * XXX: we can't use rt->rt_ifp to check for the interface, since
900 * it might be the loopback interface if the entry is for our
901 * own address on a non-loopback interface. Instead, we should
902 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
905 if ((ifp
&& ifp
->if_type
!=IFT_PPP
) && ((rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
906 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
907 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
))) {
909 lck_mtx_unlock(rt_mtx
);
911 log(LOG_DEBUG
, "nd6_lookup: failed to lookup %s (if = %s)\n",
912 ip6_sprintf(addr6
), ifp
? if_name(ifp
) : "unspec");
913 /* xxx more logs... kazu */
918 lck_mtx_unlock(rt_mtx
);
923 * Detect if a given IPv6 address identifies a neighbor on a given link.
924 * XXX: should take care of the destination of a p2p link?
927 nd6_is_addr_neighbor(
928 struct sockaddr_in6
*addr
,
935 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
936 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
939 * A link-local address is always a neighbor.
940 * XXX: we should use the sin6_scope_id field rather than the embedded
943 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
) &&
944 ntohs(*(u_int16_t
*)&addr
->sin6_addr
.s6_addr
[2]) == ifp
->if_index
)
948 * If the address matches one of our addresses,
949 * it should be a neighbor.
951 ifnet_lock_shared(ifp
);
952 for (ifa
= ifp
->if_addrlist
.tqh_first
;
954 ifa
= ifa
->ifa_list
.tqe_next
)
956 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
959 for (i
= 0; i
< 4; i
++) {
960 if ((IFADDR6(ifa
).s6_addr32
[i
] ^
961 addr
->sin6_addr
.s6_addr32
[i
]) &
962 IFMASK6(ifa
).s6_addr32
[i
])
965 ifnet_lock_done(ifp
);
968 ifnet_lock_done(ifp
);
971 * Even if the address matches none of our addresses, it might be
972 * in the neighbor cache.
974 if (nd6_lookup(&addr
->sin6_addr
, 0, ifp
, rt_locked
) != NULL
)
983 * Free an nd6 llinfo entry.
989 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
, *next
;
990 struct in6_addr in6
= ((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
991 struct nd_defrouter
*dr
;
994 * we used to have pfctlinput(PRC_HOSTDEAD) here.
995 * even though it is not harmful, it was not really necessary.
998 if (!ip6_forwarding
&& (ip6_accept_rtadv
|| (rt
->rt_ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
999 lck_mtx_lock(nd6_mutex
);
1000 dr
= defrouter_lookup(&((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
,
1003 if ((ln
&& ln
->ln_router
) || dr
) {
1005 * rt6_flush must be called whether or not the neighbor
1006 * is in the Default Router List.
1007 * See a corresponding comment in nd6_na_input().
1009 rt6_flush(&in6
, rt
->rt_ifp
);
1014 * Unreachablity of a router might affect the default
1015 * router selection and on-link detection of advertised
1020 * Temporarily fake the state to choose a new default
1021 * router and to perform on-link determination of
1022 * prefixes correctly.
1023 * Below the state will be set correctly,
1024 * or the entry itself will be deleted.
1026 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1029 * Since defrouter_select() does not affect the
1030 * on-link determination and MIP6 needs the check
1031 * before the default router selection, we perform
1034 pfxlist_onlink_check(1);
1036 if (dr
== TAILQ_FIRST(&nd_defrouter
)) {
1038 * It is used as the current default router,
1039 * so we have to move it to the end of the
1040 * list and choose a new one.
1041 * XXX: it is not very efficient if this is
1044 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1045 TAILQ_INSERT_TAIL(&nd_defrouter
, dr
, dr_entry
);
1050 lck_mtx_unlock(nd6_mutex
);
1054 * Before deleting the entry, remember the next entry as the
1055 * return value. We need this because pfxlist_onlink_check() above
1056 * might have freed other entries (particularly the old next entry) as
1057 * a side effect (XXX).
1065 * Detach the route from the routing tree and the list of neighbor
1066 * caches, and disable the route entry not to be used in already
1069 rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
1070 rt_mask(rt
), 0, (struct rtentry
**)0);
1076 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1078 * XXX cost-effective metods?
1083 struct in6_addr
*dst6
,
1086 struct llinfo_nd6
*ln
;
1087 struct timeval timenow
;
1089 getmicrotime(&timenow
);
1092 * If the caller specified "rt", use that. Otherwise, resolve the
1093 * routing table by supplied "dst6".
1098 if (!(rt
= nd6_lookup(dst6
, 0, NULL
, 0)))
1102 if ((rt
->rt_flags
& RTF_GATEWAY
) != 0 ||
1103 (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1104 !rt
->rt_llinfo
|| !rt
->rt_gateway
||
1105 rt
->rt_gateway
->sa_family
!= AF_LINK
) {
1106 /* This is not a host route. */
1110 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1111 if (ln
->ln_state
< ND6_LLINFO_REACHABLE
)
1115 * if we get upper-layer reachability confirmation many times,
1116 * it is possible we have false information.
1120 if (ln
->ln_byhint
> nd6_maxnudhint
)
1124 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1126 ln
->ln_expire
= timenow
.tv_sec
+
1127 nd_ifinfo
[rt
->rt_ifp
->if_index
].reachable
;
1134 __unused
struct sockaddr
*sa
)
1136 struct sockaddr
*gate
= rt
->rt_gateway
;
1137 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1138 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
, 0, 0, 0, 0, 0,
1139 {0,0,0,0,0,0,0,0,0,0,0,0,} };
1140 struct ifnet
*ifp
= rt
->rt_ifp
;
1142 struct timeval timenow
;
1145 if ((rt
->rt_flags
& RTF_GATEWAY
))
1148 if (nd6_need_cache(ifp
) == 0 && (rt
->rt_flags
& RTF_HOST
) == 0) {
1150 * This is probably an interface direct route for a link
1151 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1152 * We do not need special treatment below for such a route.
1153 * Moreover, the RTF_LLINFO flag which would be set below
1154 * would annoy the ndp(8) command.
1159 if (req
== RTM_RESOLVE
&&
1160 (nd6_need_cache(ifp
) == 0 || /* stf case */
1161 !nd6_is_addr_neighbor((struct sockaddr_in6
*)rt_key(rt
), ifp
, 1))) {
1163 * FreeBSD and BSD/OS often make a cloned host route based
1164 * on a less-specific route (e.g. the default route).
1165 * If the less specific route does not have a "gateway"
1166 * (this is the case when the route just goes to a p2p or an
1167 * stf interface), we'll mistakenly make a neighbor cache for
1168 * the host route, and will see strange neighbor solicitation
1169 * for the corresponding destination. In order to avoid the
1170 * confusion, we check if the destination of the route is
1171 * a neighbor in terms of neighbor discovery, and stop the
1172 * process if not. Additionally, we remove the LLINFO flag
1173 * so that ndp(8) will not try to get the neighbor information
1174 * of the destination.
1176 rt
->rt_flags
&= ~RTF_LLINFO
;
1180 getmicrotime(&timenow
);
1184 * There is no backward compatibility :)
1186 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1187 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1188 * rt->rt_flags |= RTF_CLONING;
1190 if (rt
->rt_flags
& (RTF_CLONING
| RTF_LLINFO
)) {
1192 * Case 1: This route should come from
1193 * a route to interface. RTF_LLINFO flag is set
1194 * for a host route whose destination should be
1195 * treated as on-link.
1197 rt_setgate(rt
, rt_key(rt
),
1198 (struct sockaddr
*)&null_sdl
);
1199 gate
= rt
->rt_gateway
;
1200 SDL(gate
)->sdl_type
= ifp
->if_type
;
1201 SDL(gate
)->sdl_index
= ifp
->if_index
;
1203 ln
->ln_expire
= timenow
.tv_sec
;
1205 if (ln
&& ln
->ln_expire
== 0) {
1206 /* kludge for desktops */
1208 printf("nd6_rtequest: time.tv_sec is zero; "
1214 if ((rt
->rt_flags
& RTF_CLONING
))
1218 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1219 * We don't do that here since llinfo is not ready yet.
1221 * There are also couple of other things to be discussed:
1222 * - unsolicited NA code needs improvement beforehand
1223 * - RFC2461 says we MAY send multicast unsolicited NA
1224 * (7.2.6 paragraph 4), however, it also says that we
1225 * SHOULD provide a mechanism to prevent multicast NA storm.
1226 * we don't have anything like it right now.
1227 * note that the mechanism needs a mutual agreement
1228 * between proxies, which means that we need to implement
1229 * a new protocol, or a new kludge.
1230 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1231 * we need to check ip6forwarding before sending it.
1232 * (or should we allow proxy ND configuration only for
1233 * routers? there's no mention about proxy ND from hosts)
1236 /* XXX it does not work */
1237 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1239 &SIN6(rt_key(rt
))->sin6_addr
,
1240 &SIN6(rt_key(rt
))->sin6_addr
,
1241 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1246 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) == 0) {
1248 * Address resolution isn't necessary for a point to
1249 * point link, so we can skip this test for a p2p link.
1251 if (gate
->sa_family
!= AF_LINK
||
1252 gate
->sa_len
< sizeof(null_sdl
)) {
1254 "nd6_rtrequest: bad gateway value: %s\n",
1258 SDL(gate
)->sdl_type
= ifp
->if_type
;
1259 SDL(gate
)->sdl_index
= ifp
->if_index
;
1262 break; /* This happens on a route change */
1264 * Case 2: This route may come from cloning, or a manual route
1265 * add with a LL address.
1267 R_Malloc(ln
, struct llinfo_nd6
*, sizeof(*ln
));
1268 rt
->rt_llinfo
= (caddr_t
)ln
;
1270 log(LOG_DEBUG
, "nd6_rtrequest: malloc failed\n");
1275 Bzero(ln
, sizeof(*ln
));
1277 /* this is required for "ndp" command. - shin */
1278 if (req
== RTM_ADD
) {
1280 * gate should have some valid AF_LINK entry,
1281 * and ln->ln_expire should have some lifetime
1282 * which is specified by ndp command.
1284 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1288 * When req == RTM_RESOLVE, rt is created and
1289 * initialized in rtrequest(), so rt_expire is 0.
1291 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1292 ln
->ln_expire
= timenow
.tv_sec
;
1294 rt
->rt_flags
|= RTF_LLINFO
;
1295 ln
->ln_next
= llinfo_nd6
.ln_next
;
1296 llinfo_nd6
.ln_next
= ln
;
1297 ln
->ln_prev
= &llinfo_nd6
;
1298 ln
->ln_next
->ln_prev
= ln
;
1301 * check if rt_key(rt) is one of my address assigned
1304 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1305 &SIN6(rt_key(rt
))->sin6_addr
);
1307 caddr_t macp
= nd6_ifptomac(ifp
);
1309 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1312 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
1313 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
1315 if (nd6_useloopback
) {
1316 rt
->rt_ifp
= lo_ifp
; /* XXX */
1318 * Make sure rt_ifa be equal to the ifaddr
1319 * corresponding to the address.
1320 * We need this because when we refer
1321 * rt_ifa->ia6_flags in ip6_input, we assume
1322 * that the rt_ifa points to the address instead
1323 * of the loopback address.
1325 if (ifa
!= rt
->rt_ifa
) {
1329 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
1331 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1334 /* join solicited node multicast for proxy ND */
1335 if (ifp
->if_flags
& IFF_MULTICAST
) {
1336 struct in6_addr llsol
;
1339 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1340 llsol
.s6_addr16
[0] = htons(0xff02);
1341 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1342 llsol
.s6_addr32
[1] = 0;
1343 llsol
.s6_addr32
[2] = htonl(1);
1344 llsol
.s6_addr8
[12] = 0xff;
1346 if (!in6_addmulti(&llsol
, ifp
, &error
, 0)) {
1347 nd6log((LOG_ERR
, "%s: failed to join "
1348 "%s (errno=%d)\n", if_name(ifp
),
1349 ip6_sprintf(&llsol
), error
));
1358 /* leave from solicited node multicast for proxy ND */
1359 if ((rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
1360 (ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1361 struct in6_addr llsol
;
1362 struct in6_multi
*in6m
;
1364 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1365 llsol
.s6_addr16
[0] = htons(0xff02);
1366 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1367 llsol
.s6_addr32
[1] = 0;
1368 llsol
.s6_addr32
[2] = htonl(1);
1369 llsol
.s6_addr8
[12] = 0xff;
1371 ifnet_lock_shared(ifp
);
1372 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1373 ifnet_lock_done(ifp
);
1375 in6_delmulti(in6m
, 0);
1378 ln
->ln_next
->ln_prev
= ln
->ln_prev
;
1379 ln
->ln_prev
->ln_next
= ln
->ln_next
;
1382 rt
->rt_flags
&= ~RTF_LLINFO
;
1384 m_freem(ln
->ln_hold
);
1386 R_Free((caddr_t
)ln
);
1396 struct in6_drlist
*drl
= (struct in6_drlist
*)data
;
1397 struct in6_prlist
*prl
= (struct in6_prlist
*)data
;
1398 struct in6_ndireq
*ndi
= (struct in6_ndireq
*)data
;
1399 struct in6_nbrinfo
*nbi
= (struct in6_nbrinfo
*)data
;
1400 struct in6_ndifreq
*ndif
= (struct in6_ndifreq
*)data
;
1401 struct nd_defrouter
*dr
, any
;
1402 struct nd_prefix
*pr
;
1404 int i
= 0, error
= 0;
1407 case SIOCGDRLST_IN6
:
1409 * obsolete API, use sysctl under net.inet6.icmp6
1411 lck_mtx_lock(nd6_mutex
);
1412 bzero(drl
, sizeof(*drl
));
1413 dr
= TAILQ_FIRST(&nd_defrouter
);
1414 while (dr
&& i
< DRLSTSIZ
) {
1415 drl
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1416 if (IN6_IS_ADDR_LINKLOCAL(&drl
->defrouter
[i
].rtaddr
)) {
1417 /* XXX: need to this hack for KAME stack */
1418 drl
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1421 "default router list contains a "
1422 "non-linklocal address(%s)\n",
1423 ip6_sprintf(&drl
->defrouter
[i
].rtaddr
));
1425 drl
->defrouter
[i
].flags
= dr
->flags
;
1426 drl
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1427 drl
->defrouter
[i
].expire
= dr
->expire
;
1428 drl
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1430 dr
= TAILQ_NEXT(dr
, dr_entry
);
1432 lck_mtx_unlock(nd6_mutex
);
1434 case SIOCGPRLST_IN6
:
1436 * obsolete API, use sysctl under net.inet6.icmp6
1439 * XXX meaning of fields, especialy "raflags", is very
1440 * differnet between RA prefix list and RR/static prefix list.
1441 * how about separating ioctls into two?
1443 bzero(prl
, sizeof(*prl
));
1444 lck_mtx_lock(nd6_mutex
);
1445 pr
= nd_prefix
.lh_first
;
1446 while (pr
&& i
< PRLSTSIZ
) {
1447 struct nd_pfxrouter
*pfr
;
1450 (void)in6_embedscope(&prl
->prefix
[i
].prefix
,
1451 &pr
->ndpr_prefix
, NULL
, NULL
);
1452 prl
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1453 prl
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1454 prl
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1455 prl
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1456 prl
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1457 prl
->prefix
[i
].expire
= pr
->ndpr_expire
;
1459 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1463 #define RTRADDR prl->prefix[i].advrtr[j]
1464 RTRADDR
= pfr
->router
->rtaddr
;
1465 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1466 /* XXX: hack for KAME */
1467 RTRADDR
.s6_addr16
[1] = 0;
1470 "a router(%s) advertises "
1472 "non-link local address\n",
1473 ip6_sprintf(&RTRADDR
));
1477 pfr
= pfr
->pfr_next
;
1479 prl
->prefix
[i
].advrtrs
= j
;
1480 prl
->prefix
[i
].origin
= PR_ORIG_RA
;
1486 struct rr_prefix
*rpp
;
1488 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1489 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1492 (void)in6_embedscope(&prl
->prefix
[i
].prefix
,
1493 &pr
->ndpr_prefix
, NULL
, NULL
);
1494 prl
->prefix
[i
].raflags
= rpp
->rp_raf
;
1495 prl
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1496 prl
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1497 prl
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1498 prl
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1499 prl
->prefix
[i
].expire
= rpp
->rp_expire
;
1500 prl
->prefix
[i
].advrtrs
= 0;
1501 prl
->prefix
[i
].origin
= rpp
->rp_origin
;
1505 lck_mtx_unlock(nd6_mutex
);
1507 case OSIOCGIFINFO_IN6
:
1508 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1512 ndi
->ndi
.linkmtu
= IN6_LINKMTU(ifp
);
1513 ndi
->ndi
.maxmtu
= nd_ifinfo
[ifp
->if_index
].maxmtu
;
1514 ndi
->ndi
.basereachable
=
1515 nd_ifinfo
[ifp
->if_index
].basereachable
;
1516 ndi
->ndi
.reachable
= nd_ifinfo
[ifp
->if_index
].reachable
;
1517 ndi
->ndi
.retrans
= nd_ifinfo
[ifp
->if_index
].retrans
;
1518 ndi
->ndi
.flags
= nd_ifinfo
[ifp
->if_index
].flags
;
1519 ndi
->ndi
.recalctm
= nd_ifinfo
[ifp
->if_index
].recalctm
;
1520 ndi
->ndi
.chlim
= nd_ifinfo
[ifp
->if_index
].chlim
;
1521 ndi
->ndi
.receivedra
= nd_ifinfo
[ifp
->if_index
].receivedra
;
1523 case SIOCGIFINFO_IN6
:
1524 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1528 ndi
->ndi
= nd_ifinfo
[ifp
->if_index
];
1530 case SIOCSIFINFO_FLAGS
:
1531 /* XXX: almost all other fields of ndi->ndi is unused */
1532 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1536 nd_ifinfo
[ifp
->if_index
].flags
= ndi
->ndi
.flags
;
1538 case SIOCSNDFLUSH_IN6
: /* XXX: the ioctl name is confusing... */
1539 /* flush default router list */
1541 * xxx sumikawa: should not delete route if default
1542 * route equals to the top of default router list
1544 bzero(&any
, sizeof(any
));
1545 lck_mtx_lock(nd6_mutex
);
1546 defrouter_delreq(&any
, 1);
1548 lck_mtx_unlock(nd6_mutex
);
1549 /* xxx sumikawa: flush prefix list */
1551 case SIOCSPFXFLUSH_IN6
:
1553 /* flush all the prefix advertised by routers */
1554 struct nd_prefix
*next
;
1555 lck_mtx_lock(nd6_mutex
);
1557 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
1558 struct in6_ifaddr
*ia
, *ia_next
;
1560 next
= pr
->ndpr_next
;
1562 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
))
1565 /* do we really have to remove addresses as well? */
1566 for (ia
= in6_ifaddrs
; ia
; ia
= ia_next
) {
1567 /* ia might be removed. keep the next ptr. */
1568 ia_next
= ia
->ia_next
;
1570 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
1573 if (ia
->ia6_ndpr
== pr
)
1574 in6_purgeaddr(&ia
->ia_ifa
, 1);
1576 prelist_remove(pr
, 1);
1578 lck_mtx_unlock(nd6_mutex
);
1581 case SIOCSRTRFLUSH_IN6
:
1583 /* flush all the default routers */
1584 struct nd_defrouter
*next
;
1586 lck_mtx_lock(nd6_mutex
);
1587 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
1589 * The first entry of the list may be stored in
1590 * the routing table, so we'll delete it later.
1592 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
1593 next
= TAILQ_NEXT(dr
, dr_entry
);
1594 defrtrlist_del(dr
, 1);
1596 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
), 1);
1598 lck_mtx_unlock(nd6_mutex
);
1601 case SIOCGNBRINFO_IN6
:
1603 struct llinfo_nd6
*ln
;
1604 struct in6_addr nb_addr
= nbi
->addr
; /* make local for safety */
1607 * XXX: KAME specific hack for scoped addresses
1608 * XXXX: for other scopes than link-local?
1610 if (IN6_IS_ADDR_LINKLOCAL(&nbi
->addr
) ||
1611 IN6_IS_ADDR_MC_LINKLOCAL(&nbi
->addr
)) {
1612 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
1615 *idp
= htons(ifp
->if_index
);
1618 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
1622 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1623 nbi
->state
= ln
->ln_state
;
1624 nbi
->asked
= ln
->ln_asked
;
1625 nbi
->isrouter
= ln
->ln_router
;
1626 nbi
->expire
= ln
->ln_expire
;
1630 case SIOCGDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1631 ndif
->ifindex
= nd6_defifindex
;
1633 case SIOCSDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1634 return(nd6_setdefaultiface(ndif
->ifindex
));
1641 * Create neighbor cache entry and cache link-layer address,
1642 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1647 struct in6_addr
*from
,
1649 __unused
int lladdrlen
,
1650 int type
, /* ICMP6 type */
1651 int code
) /* type dependent information */
1653 struct rtentry
*rt
= NULL
;
1654 struct llinfo_nd6
*ln
= NULL
;
1656 struct sockaddr_dl
*sdl
= NULL
;
1661 struct timeval timenow
;
1664 panic("ifp == NULL in nd6_cache_lladdr");
1666 panic("from == NULL in nd6_cache_lladdr");
1668 /* nothing must be updated for unspecified address */
1669 if (IN6_IS_ADDR_UNSPECIFIED(from
))
1673 * Validation about ifp->if_addrlen and lladdrlen must be done in
1676 * XXX If the link does not have link-layer adderss, what should
1677 * we do? (ifp->if_addrlen == 0)
1678 * Spec says nothing in sections for RA, RS and NA. There's small
1679 * description on it in NS section (RFC 2461 7.2.3).
1681 getmicrotime(&timenow
);
1683 lck_mtx_lock(rt_mtx
);
1684 rt
= nd6_lookup(from
, 0, ifp
, 1);
1687 /* nothing must be done if there's no lladdr */
1688 if (!lladdr
|| !lladdrlen
)
1692 rt
= nd6_lookup(from
, 1, ifp
, 1);
1695 /* do nothing if static ndp is set */
1696 if (rt
->rt_flags
& RTF_STATIC
) {
1697 lck_mtx_unlock(rt_mtx
);
1703 lck_mtx_unlock(rt_mtx
);
1707 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
1712 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1715 if (!rt
->rt_gateway
)
1717 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
1719 sdl
= SDL(rt
->rt_gateway
);
1721 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
1722 if (olladdr
&& lladdr
) {
1723 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
1731 * newentry olladdr lladdr llchange (*=record)
1734 * 0 n y -- (3) * STALE
1736 * 0 y y y (5) * STALE
1737 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1738 * 1 -- y -- (7) * STALE
1741 if (lladdr
) { /* (3-5) and (7) */
1743 * Record source link-layer address
1744 * XXX is it dependent to ifp->if_type?
1746 sdl
->sdl_alen
= ifp
->if_addrlen
;
1747 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
1751 if ((!olladdr
&& lladdr
) /* (3) */
1752 || (olladdr
&& lladdr
&& llchange
)) { /* (5) */
1754 newstate
= ND6_LLINFO_STALE
;
1755 } else /* (1-2,4) */
1759 if (!lladdr
) /* (6) */
1760 newstate
= ND6_LLINFO_NOSTATE
;
1762 newstate
= ND6_LLINFO_STALE
;
1767 * Update the state of the neighbor cache.
1769 ln
->ln_state
= newstate
;
1771 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1773 * XXX: since nd6_output() below will cause
1774 * state tansition to DELAY and reset the timer,
1775 * we must set the timer now, although it is actually
1778 ln
->ln_expire
= timenow
.tv_sec
+ nd6_gctimer
;
1782 * we assume ifp is not a p2p here, so just
1783 * set the 2nd argument as the 1st one.
1785 nd6_output(ifp
, ifp
, ln
->ln_hold
,
1786 (struct sockaddr_in6
*)rt_key(rt
),
1790 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
1791 /* probe right away */
1792 ln
->ln_expire
= timenow
.tv_sec
;
1797 * ICMP6 type dependent behavior.
1799 * NS: clear IsRouter if new entry
1800 * RS: clear IsRouter
1801 * RA: set IsRouter if there's lladdr
1802 * redir: clear IsRouter if new entry
1805 * The spec says that we must set IsRouter in the following cases:
1806 * - If lladdr exist, set IsRouter. This means (1-5).
1807 * - If it is old entry (!newentry), set IsRouter. This means (7).
1808 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1809 * A quetion arises for (1) case. (1) case has no lladdr in the
1810 * neighbor cache, this is similar to (6).
1811 * This case is rare but we figured that we MUST NOT set IsRouter.
1813 * newentry olladdr lladdr llchange NS RS RA redir
1815 * 0 n n -- (1) c ? s
1816 * 0 y n -- (2) c s s
1817 * 0 n y -- (3) c s s
1820 * 1 -- n -- (6) c c c s
1821 * 1 -- y -- (7) c c s c s
1825 switch (type
& 0xff) {
1826 case ND_NEIGHBOR_SOLICIT
:
1828 * New entry must have is_router flag cleared.
1830 if (is_newentry
) /* (6-7) */
1835 * If the icmp is a redirect to a better router, always set the
1836 * is_router flag. Otherwise, if the entry is newly created,
1837 * clear the flag. [RFC 2461, sec 8.3]
1839 if (code
== ND_REDIRECT_ROUTER
)
1841 else if (is_newentry
) /* (6-7) */
1844 case ND_ROUTER_SOLICIT
:
1846 * is_router flag must always be cleared.
1850 case ND_ROUTER_ADVERT
:
1852 * Mark an entry with lladdr as a router.
1854 if ((!is_newentry
&& (olladdr
|| lladdr
)) /* (2-5) */
1855 || (is_newentry
&& lladdr
)) { /* (7) */
1862 * When the link-layer address of a router changes, select the
1863 * best router again. In particular, when the neighbor entry is newly
1864 * created, it might affect the selection policy.
1865 * Question: can we restrict the first condition to the "is_newentry"
1867 * XXX: when we hear an RA from a new router with the link-layer
1868 * address option, defrouter_select() is called twice, since
1869 * defrtrlist_update called the function as well. However, I believe
1870 * we can compromise the overhead, since it only happens the first
1872 * XXX: although defrouter_select() should not have a bad effect
1873 * for those are not autoconfigured hosts, we explicitly avoid such
1876 if (do_update
&& ln
->ln_router
&& !ip6_forwarding
&& (ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
1877 lck_mtx_lock(nd6_mutex
);
1879 lck_mtx_unlock(nd6_mutex
);
1887 __unused
void *ignored_arg
)
1890 struct nd_ifinfo
*nd6if
;
1892 lck_mtx_lock(nd6_mutex
);
1893 for (i
= 1; i
< if_index
+ 1; i
++) {
1894 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
)
1896 nd6if
= &nd_ifinfo
[i
];
1897 if (nd6if
->basereachable
&& /* already initialized */
1898 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
1900 * Since reachable time rarely changes by router
1901 * advertisements, we SHOULD insure that a new random
1902 * value gets recomputed at least once every few hours.
1905 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
1906 nd6if
->reachable
= ND_COMPUTE_RTIME(nd6if
->basereachable
);
1909 lck_mtx_unlock(nd6_mutex
);
1910 timeout(nd6_slowtimo
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
1914 #define senderr(e) { error = (e); goto bad;}
1918 struct ifnet
*origifp
,
1920 struct sockaddr_in6
*dst
,
1921 struct rtentry
*rt0
,
1924 struct mbuf
*m
= m0
;
1925 struct rtentry
*rt
= rt0
;
1926 struct sockaddr_in6
*gw6
= NULL
;
1927 struct llinfo_nd6
*ln
= NULL
;
1929 struct timeval timenow
;
1931 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
))
1934 if (nd6_need_cache(ifp
) == 0)
1938 * next hop determination. This routine is derived from ether_outpout.
1940 lck_mtx_lock(rt_mtx
);
1942 if ((rt
->rt_flags
& RTF_UP
) == 0) {
1943 if ((rt0
= rt
= rtalloc1_locked((struct sockaddr
*)dst
, 1, 0UL)) !=
1947 if (rt
->rt_ifp
!= ifp
) {
1948 /* XXX: loop care? */
1949 lck_mtx_unlock(rt_mtx
);
1950 return nd6_output(ifp
, origifp
, m0
,
1954 lck_mtx_unlock(rt_mtx
);
1955 senderr(EHOSTUNREACH
);
1959 if (rt
->rt_flags
& RTF_GATEWAY
) {
1960 gw6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
1963 * We skip link-layer address resolution and NUD
1964 * if the gateway is not a neighbor from ND point
1965 * of view, regardless of the value of nd_ifinfo.flags.
1966 * The second condition is a bit tricky; we skip
1967 * if the gateway is our own address, which is
1968 * sometimes used to install a route to a p2p link.
1970 if (!nd6_is_addr_neighbor(gw6
, ifp
, 1) ||
1971 in6ifa_ifpwithaddr(ifp
, &gw6
->sin6_addr
)) {
1973 * We allow this kind of tricky route only
1974 * when the outgoing interface is p2p.
1975 * XXX: we may need a more generic rule here.
1977 lck_mtx_unlock(rt_mtx
);
1978 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
1979 senderr(EHOSTUNREACH
);
1984 if (rt
->rt_gwroute
== 0)
1986 if (((rt
= rt
->rt_gwroute
)->rt_flags
& RTF_UP
) == 0) {
1987 rtfree_locked(rt
); rt
= rt0
;
1988 lookup
: rt
->rt_gwroute
= rtalloc1_locked(rt
->rt_gateway
, 1, 0UL);
1989 if ((rt
= rt
->rt_gwroute
) == 0) {
1990 lck_mtx_unlock(rt_mtx
);
1991 senderr(EHOSTUNREACH
);
1998 * Address resolution or Neighbor Unreachability Detection
2000 * At this point, the destination of the packet must be a unicast
2001 * or an anycast address(i.e. not a multicast).
2004 /* Look up the neighbor cache for the nexthop */
2005 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0)
2006 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
2009 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2010 * the condition below is not very efficient. But we believe
2011 * it is tolerable, because this should be a rare case.
2013 if (nd6_is_addr_neighbor(dst
, ifp
, 1) &&
2014 (rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
, 1)) != NULL
)
2015 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
2017 lck_mtx_unlock(rt_mtx
);
2019 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0 &&
2020 !(nd_ifinfo
[ifp
->if_index
].flags
& ND6_IFF_PERFORMNUD
)) {
2022 "nd6_output: can't allocate llinfo for %s "
2024 ip6_sprintf(&dst
->sin6_addr
), ln
, rt
);
2025 senderr(EIO
); /* XXX: good error? */
2028 goto sendpkt
; /* send anyway */
2031 getmicrotime(&timenow
);
2033 /* We don't have to do link-layer address resolution on a p2p link. */
2034 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
2035 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
2036 ln
->ln_state
= ND6_LLINFO_STALE
;
2037 ln
->ln_expire
= timenow
.tv_sec
+ nd6_gctimer
;
2041 * The first time we send a packet to a neighbor whose entry is
2042 * STALE, we have to change the state to DELAY and a sets a timer to
2043 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2044 * neighbor unreachability detection on expiration.
2047 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
2049 ln
->ln_state
= ND6_LLINFO_DELAY
;
2050 ln
->ln_expire
= timenow
.tv_sec
+ nd6_delay
;
2054 * If the neighbor cache entry has a state other than INCOMPLETE
2055 * (i.e. its link-layer address is already resolved), just
2058 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
)
2062 * There is a neighbor cache entry, but no ethernet address
2063 * response yet. Replace the held mbuf (if any) with this
2066 * This code conforms to the rate-limiting rule described in Section
2067 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
2070 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
)
2071 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
2073 m_freem(ln
->ln_hold
);
2075 if (ln
->ln_expire
) {
2076 if (ln
->ln_asked
< nd6_mmaxtries
&&
2077 ln
->ln_expire
< timenow
.tv_sec
) {
2079 ln
->ln_expire
= timenow
.tv_sec
+
2080 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
2081 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0, locked
);
2089 /* Make sure the HW checksum flags are cleaned before sending the packet */
2091 m
->m_pkthdr
.csum_data
= 0;
2092 m
->m_pkthdr
.csum_flags
= 0;
2094 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
2095 m
->m_pkthdr
.rcvif
= origifp
; /* forwarding rules require the original scope_id */
2097 lck_mtx_unlock(ip6_mutex
);
2098 error
= dlil_output(origifp
, PF_INET6
, m
, (caddr_t
)rt
, (struct sockaddr
*)dst
, 0);
2100 lck_mtx_lock(ip6_mutex
);
2103 /* Do not allow loopback address to wind up on a wire */
2104 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2106 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
2107 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
))) {
2108 ip6stat
.ip6s_badscope
++;
2110 * Do not simply drop the packet just like a firewall -- we want the
2111 * the application to feel the pain.
2112 * Return ENETUNREACH like ip6_output does in some similar cases.
2113 * This can startle the otherwise clueless process that specifies
2114 * loopback as the source address.
2116 error
= ENETUNREACH
;
2121 m
->m_pkthdr
.rcvif
= 0;
2123 lck_mtx_unlock(ip6_mutex
);
2124 error
= dlil_output(ifp
, PF_INET6
, m
, (caddr_t
)rt
, (struct sockaddr
*)dst
, 0);
2126 lck_mtx_lock(ip6_mutex
);
2129 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
2130 return((*ifp
->if_output
)(origifp
, m
, (struct sockaddr
*)dst
,
2133 return((*ifp
->if_output
)(ifp
, m
, (struct sockaddr
*)dst
, rt
));
2148 * XXX: we currently do not make neighbor cache on any interface
2149 * other than ARCnet, Ethernet, FDDI and GIF.
2152 * - unidirectional tunnels needs no ND
2154 switch (ifp
->if_type
) {
2160 case IFT_IEEE8023ADLAG
:
2164 case IFT_GIF
: /* XXX need more cases? */
2176 struct sockaddr
*dst
,
2180 struct sockaddr_dl
*sdl
;
2182 if (m
->m_flags
& M_MCAST
) {
2183 switch (ifp
->if_type
) {
2187 case IFT_IEEE8023ADLAG
:
2191 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
2195 for (i
= 0; i
< ifp
->if_addrlen
; i
++)
2202 return(0); /* caller will free mbuf */
2207 /* this could happen, if we could not allocate memory */
2208 return(0); /* caller will free mbuf */
2210 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
2211 printf("nd6_storelladdr: something odd happens\n");
2212 return(0); /* caller will free mbuf */
2214 sdl
= SDL(rt
->rt_gateway
);
2215 if (sdl
->sdl_alen
== 0) {
2216 /* this should be impossible, but we bark here for debugging */
2217 printf("nd6_storelladdr: sdl_alen == 0\n");
2218 return(0); /* caller will free mbuf */
2221 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
2225 extern errno_t
arp_route_to_gateway_route(const struct sockaddr
*net_dest
,
2226 route_t hint
, route_t
*out_route
);
2231 const struct sockaddr_in6
*ip6_dest
,
2232 struct sockaddr_dl
*ll_dest
,
2237 route_t route
= hint
;
2239 struct sockaddr_dl
*sdl
= NULL
;
2242 if (ip6_dest
->sin6_family
!= AF_INET6
)
2243 return EAFNOSUPPORT
;
2245 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
2249 result
= arp_route_to_gateway_route((const struct sockaddr
*)ip6_dest
, hint
, &route
);
2254 if ((packet
->m_flags
& M_MCAST
) != 0) {
2255 return dlil_resolve_multi(ifp
, (const struct sockaddr
*)ip6_dest
,
2256 (struct sockaddr
*)ll_dest
, ll_dest_len
);
2259 if (route
== NULL
) {
2260 /* this could happen, if we could not allocate memory */
2264 lck_mtx_lock(rt_mtx
);
2266 if (route
->rt_gateway
->sa_family
!= AF_LINK
) {
2267 printf("nd6_lookup_ipv6: gateway address not AF_LINK\n");
2268 result
= EADDRNOTAVAIL
;
2272 sdl
= SDL(route
->rt_gateway
);
2273 if (sdl
->sdl_alen
== 0) {
2274 /* this should be impossible, but we bark here for debugging */
2275 printf("nd6_storelladdr: sdl_alen == 0\n");
2276 result
= EHOSTUNREACH
;
2279 copy_len
= sdl
->sdl_len
<= ll_dest_len
? sdl
->sdl_len
: ll_dest_len
;
2280 bcopy(sdl
, ll_dest
, copy_len
);
2283 lck_mtx_unlock(rt_mtx
);
2287 SYSCTL_DECL(_net_inet6_icmp6
);
2290 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
2292 #pragma unused(oidp, arg1, arg2)
2295 struct in6_defrouter
*d
, *de
;
2296 struct nd_defrouter
*dr
;
2302 lck_mtx_lock(nd6_mutex
);
2303 for (dr
= TAILQ_FIRST(&nd_defrouter
);
2305 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2306 d
= (struct in6_defrouter
*)buf
;
2307 de
= (struct in6_defrouter
*)(buf
+ sizeof(buf
));
2310 bzero(d
, sizeof(*d
));
2311 d
->rtaddr
.sin6_family
= AF_INET6
;
2312 d
->rtaddr
.sin6_len
= sizeof(d
->rtaddr
);
2313 if (in6_recoverscope(&d
->rtaddr
, &dr
->rtaddr
,
2317 "default router list (%s)\n",
2318 ip6_sprintf(&dr
->rtaddr
));
2319 d
->flags
= dr
->flags
;
2320 d
->rtlifetime
= dr
->rtlifetime
;
2321 d
->expire
= dr
->expire
;
2322 d
->if_index
= dr
->ifp
->if_index
;
2324 panic("buffer too short");
2326 error
= SYSCTL_OUT(req
, buf
, sizeof(*d
));
2330 lck_mtx_unlock(nd6_mutex
);
2335 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
2337 #pragma unused(oidp, arg1, arg2)
2340 struct in6_prefix
*p
, *pe
;
2341 struct nd_prefix
*pr
;
2347 lck_mtx_lock(nd6_mutex
);
2349 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2350 u_short advrtrs
= 0;
2352 struct sockaddr_in6
*sin6
, *s6
;
2353 struct nd_pfxrouter
*pfr
;
2355 p
= (struct in6_prefix
*)buf
;
2356 pe
= (struct in6_prefix
*)(buf
+ sizeof(buf
));
2359 bzero(p
, sizeof(*p
));
2360 sin6
= (struct sockaddr_in6
*)(p
+ 1);
2362 p
->prefix
= pr
->ndpr_prefix
;
2363 if (in6_recoverscope(&p
->prefix
,
2364 &p
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
2366 "scope error in prefix list (%s)\n",
2367 ip6_sprintf(&p
->prefix
.sin6_addr
));
2368 p
->raflags
= pr
->ndpr_raf
;
2369 p
->prefixlen
= pr
->ndpr_plen
;
2370 p
->vltime
= pr
->ndpr_vltime
;
2371 p
->pltime
= pr
->ndpr_pltime
;
2372 p
->if_index
= pr
->ndpr_ifp
->if_index
;
2373 p
->expire
= pr
->ndpr_expire
;
2374 p
->refcnt
= pr
->ndpr_refcnt
;
2375 p
->flags
= pr
->ndpr_stateflags
;
2376 p
->origin
= PR_ORIG_RA
;
2378 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
2380 pfr
= pfr
->pfr_next
) {
2381 if ((void *)&sin6
[advrtrs
+ 1] >
2386 s6
= &sin6
[advrtrs
];
2387 bzero(s6
, sizeof(*s6
));
2388 s6
->sin6_family
= AF_INET6
;
2389 s6
->sin6_len
= sizeof(*sin6
);
2390 if (in6_recoverscope(s6
,
2391 &pfr
->router
->rtaddr
,
2392 pfr
->router
->ifp
) != 0)
2395 "prefix list (%s)\n",
2396 ip6_sprintf(&pfr
->router
->rtaddr
));
2399 p
->advrtrs
= advrtrs
;
2401 panic("buffer too short");
2403 advance
= sizeof(*p
) + sizeof(*sin6
) * advrtrs
;
2404 error
= SYSCTL_OUT(req
, buf
, advance
);
2408 lck_mtx_unlock(nd6_mutex
);
2411 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
2412 CTLFLAG_RD
, 0, 0, nd6_sysctl_drlist
, "S,in6_defrouter","");
2413 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
2414 CTLFLAG_RD
, 0, 0, nd6_sysctl_prlist
, "S,in6_defrouter","");