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/errno.h>
50 #include <sys/syslog.h>
51 #include <sys/protosw.h>
52 #include <kern/queue.h>
53 #include <kern/lock.h>
55 #define DONT_WARN_OBSOLETE
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/if_atm.h>
60 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/if_ether.h>
65 #include <netinet/if_fddi.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/nd6.h>
70 #include <netinet6/in6_prefix.h>
71 #include <netinet/icmp6.h>
75 #include <net/net_osdep.h>
77 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
78 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
80 #define SIN6(s) ((struct sockaddr_in6 *)s)
81 #define SDL(s) ((struct sockaddr_dl *)s)
84 int nd6_prune
= 1; /* walk list every 1 seconds */
85 int nd6_delay
= 5; /* delay first probe time 5 second */
86 int nd6_umaxtries
= 3; /* maximum unicast query */
87 int nd6_mmaxtries
= 3; /* maximum multicast query */
88 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
89 int nd6_gctimer
= (60 * 60 * 24); /* 1 day: garbage collection timer */
91 /* preventing too many loops in ND option parsing */
92 int nd6_maxndopt
= 10; /* max # of ND options allowed */
94 int nd6_maxnudhint
= 0; /* max # of subsequent upper layer hints */
103 static int nd6_inuse
, nd6_allocated
;
105 struct llinfo_nd6 llinfo_nd6
= {&llinfo_nd6
, &llinfo_nd6
};
106 size_t nd_ifinfo_indexlim
= 8;
107 struct nd_ifinfo
*nd_ifinfo
= NULL
;
108 struct nd_drhead nd_defrouter
;
109 struct nd_prhead nd_prefix
= { 0 };
111 int nd6_recalc_reachtm_interval
= ND6_RECALC_REACHTM_INTERVAL
;
112 static struct sockaddr_in6 all1_sa
;
114 static int regen_tmpaddr(struct in6_ifaddr
*);
115 extern lck_mtx_t
*rt_mtx
;
116 extern lck_mtx_t
*ip6_mutex
;
117 extern lck_mtx_t
*nd6_mutex
;
119 static void nd6_slowtimo(void *ignored_arg
);
124 static int nd6_init_done
= 0;
128 log(LOG_NOTICE
, "nd6_init called more than once(ignored)\n");
132 all1_sa
.sin6_family
= AF_INET6
;
133 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
134 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++)
135 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
137 /* initialization of the default router list */
138 TAILQ_INIT(&nd_defrouter
);
143 timeout(nd6_slowtimo
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
152 * We have some arrays that should be indexed by if_index.
153 * since if_index will grow dynamically, they should grow too.
155 if (nd_ifinfo
== NULL
|| if_index
>= nd_ifinfo_indexlim
) {
159 while (if_index
>= nd_ifinfo_indexlim
)
160 nd_ifinfo_indexlim
<<= 1;
163 n
= nd_ifinfo_indexlim
* sizeof(struct nd_ifinfo
);
164 q
= (caddr_t
)_MALLOC(n
, M_IP6NDP
, M_WAITOK
);
167 bcopy((caddr_t
)nd_ifinfo
, q
, n
/2);
168 FREE((caddr_t
)nd_ifinfo
, M_IP6NDP
);
170 nd_ifinfo
= (struct nd_ifinfo
*)q
;
173 #define ND nd_ifinfo[ifp->if_index]
176 * Don't initialize if called twice.
177 * XXX: to detect this, we should choose a member that is never set
178 * before initialization of the ND structure itself. We formaly used
179 * the linkmtu member, which was not suitable because it could be
180 * initialized via "ifconfig mtu".
182 if (ND
.basereachable
)
185 ND
.linkmtu
= ifindex2ifnet
[ifp
->if_index
]->if_mtu
;
186 ND
.chlim
= IPV6_DEFHLIM
;
187 ND
.basereachable
= REACHABLE_TIME
;
188 ND
.reachable
= ND_COMPUTE_RTIME(ND
.basereachable
);
189 ND
.retrans
= RETRANS_TIMER
;
191 ND
.flags
= ND6_IFF_PERFORMNUD
;
197 * Reset ND level link MTU. This function is called when the physical MTU
198 * changes, which means we might have to adjust the ND level MTU.
205 #define MIN(a,b) ((a) < (b) ? (a) : (b))
208 struct nd_ifinfo
*ndi
;
209 u_long oldmaxmtu
, oldlinkmtu
;
212 * Make sure IPv6 is enabled for the interface first,
213 * because this can be called directly from SIOCSIFMTU for IPv4
216 if (ifp
->if_index
>= nd_ifinfo_indexlim
) {
217 return; /* we're out of bound for nd_ifinfo */
220 ndi
= &nd_ifinfo
[ifp
->if_index
];
221 oldmaxmtu
= ndi
->maxmtu
;
222 oldlinkmtu
= ndi
->linkmtu
;
224 switch (ifp
->if_type
) {
225 case IFT_ARCNET
: /* XXX MTU handling needs more work */
226 ndi
->maxmtu
= MIN(60480, ifp
->if_mtu
);
228 case IFT_L2VLAN
: /* XXX what if the VLAN isn't over ethernet? */
229 case IFT_IEEE8023ADLAG
:
231 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
234 ndi
->maxmtu
= MIN(FDDIIPMTU
, ifp
->if_mtu
);
237 ndi
->maxmtu
= MIN(ATMMTU
, ifp
->if_mtu
);
239 case IFT_IEEE1394
: /* XXX should be IEEE1394MTU(1500) */
240 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
243 case IFT_IEEE80211
: /* XXX should be IEEE80211MTU(1500) */
244 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
248 ndi
->maxmtu
= ifp
->if_mtu
;
252 if (oldmaxmtu
!= ndi
->maxmtu
) {
254 * If the ND level MTU is not set yet, or if the maxmtu
255 * is reset to a smaller value than the ND level MTU,
256 * also reset the ND level MTU.
258 if (ndi
->linkmtu
== 0 ||
259 ndi
->maxmtu
< ndi
->linkmtu
) {
260 ndi
->linkmtu
= ndi
->maxmtu
;
261 /* also adjust in6_maxmtu if necessary. */
262 if (oldlinkmtu
== 0) {
264 * XXX: the case analysis is grotty, but
265 * it is not efficient to call in6_setmaxmtu()
266 * here when we are during the initialization
269 if (in6_maxmtu
< ndi
->linkmtu
)
270 in6_maxmtu
= ndi
->linkmtu
;
282 union nd_opts
*ndopts
)
284 bzero(ndopts
, sizeof(*ndopts
));
285 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
287 = (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
290 ndopts
->nd_opts_done
= 1;
291 ndopts
->nd_opts_search
= NULL
;
296 * Take one ND option.
300 union nd_opts
*ndopts
)
302 struct nd_opt_hdr
*nd_opt
;
306 panic("ndopts == NULL in nd6_option\n");
307 if (!ndopts
->nd_opts_last
)
308 panic("uninitialized ndopts in nd6_option\n");
309 if (!ndopts
->nd_opts_search
)
311 if (ndopts
->nd_opts_done
)
314 nd_opt
= ndopts
->nd_opts_search
;
316 /* make sure nd_opt_len is inside the buffer */
317 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
318 bzero(ndopts
, sizeof(*ndopts
));
322 olen
= nd_opt
->nd_opt_len
<< 3;
325 * Message validation requires that all included
326 * options have a length that is greater than zero.
328 bzero(ndopts
, sizeof(*ndopts
));
332 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
333 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
334 /* option overruns the end of buffer, invalid */
335 bzero(ndopts
, sizeof(*ndopts
));
337 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
338 /* reached the end of options chain */
339 ndopts
->nd_opts_done
= 1;
340 ndopts
->nd_opts_search
= NULL
;
346 * Parse multiple ND options.
347 * This function is much easier to use, for ND routines that do not need
348 * multiple options of the same type.
352 union nd_opts
*ndopts
)
354 struct nd_opt_hdr
*nd_opt
;
358 panic("ndopts == NULL in nd6_options\n");
359 if (!ndopts
->nd_opts_last
)
360 panic("uninitialized ndopts in nd6_options\n");
361 if (!ndopts
->nd_opts_search
)
365 nd_opt
= nd6_option(ndopts
);
366 if (!nd_opt
&& !ndopts
->nd_opts_last
) {
368 * Message validation requires that all included
369 * options have a length that is greater than zero.
371 icmp6stat
.icp6s_nd_badopt
++;
372 bzero(ndopts
, sizeof(*ndopts
));
379 switch (nd_opt
->nd_opt_type
) {
380 case ND_OPT_SOURCE_LINKADDR
:
381 case ND_OPT_TARGET_LINKADDR
:
383 case ND_OPT_REDIRECTED_HEADER
:
384 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
386 "duplicated ND6 option found (type=%d)\n",
387 nd_opt
->nd_opt_type
));
390 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
394 case ND_OPT_PREFIX_INFORMATION
:
395 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
396 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
399 ndopts
->nd_opts_pi_end
=
400 (struct nd_opt_prefix_info
*)nd_opt
;
404 * Unknown options must be silently ignored,
405 * to accomodate future extension to the protocol.
408 "nd6_options: unsupported option %d - "
409 "option ignored\n", nd_opt
->nd_opt_type
));
414 if (i
> nd6_maxndopt
) {
415 icmp6stat
.icp6s_nd_toomanyopt
++;
416 nd6log((LOG_INFO
, "too many loop in nd opt\n"));
420 if (ndopts
->nd_opts_done
)
428 * ND6 timer routine to expire default route list and prefix list
434 struct llinfo_nd6
*ln
;
435 struct nd_defrouter
*dr
;
436 struct nd_prefix
*pr
;
437 struct ifnet
*ifp
= NULL
;
438 struct in6_ifaddr
*ia6
, *nia6
;
439 struct in6_addrlifetime
*lt6
;
440 struct timeval timenow
;
442 getmicrotime(&timenow
);
446 ln
= llinfo_nd6
.ln_next
;
447 while (ln
&& ln
!= &llinfo_nd6
) {
449 struct sockaddr_in6
*dst
;
450 struct llinfo_nd6
*next
= ln
->ln_next
;
451 /* XXX: used for the DELAY case only: */
452 struct nd_ifinfo
*ndi
= NULL
;
454 if ((rt
= ln
->ln_rt
) == NULL
) {
458 if ((ifp
= rt
->rt_ifp
) == NULL
) {
462 ndi
= &nd_ifinfo
[ifp
->if_index
];
463 dst
= (struct sockaddr_in6
*)rt_key(rt
);
465 if (ln
->ln_expire
> timenow
.tv_sec
) {
472 printf("rt=0 in nd6_timer(ln=%p)\n", ln
);
476 if (rt
->rt_llinfo
&& (struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
) {
477 printf("rt_llinfo(%p) is not equal to ln(%p)\n",
483 printf("dst=0 in nd6_timer(ln=%p)\n", ln
);
488 switch (ln
->ln_state
) {
489 case ND6_LLINFO_INCOMPLETE
:
490 if (ln
->ln_asked
< nd6_mmaxtries
) {
492 ln
->ln_expire
= timenow
.tv_sec
+
493 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
494 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
,
497 struct mbuf
*m
= ln
->ln_hold
;
502 * Fake rcvif to make ICMP error
503 * more helpful in diagnosing
505 * XXX: should we consider
508 m
->m_pkthdr
.rcvif
= rt
->rt_ifp
;
510 icmp6_error(m
, ICMP6_DST_UNREACH
,
511 ICMP6_DST_UNREACH_ADDR
, 0);
517 case ND6_LLINFO_REACHABLE
:
519 ln
->ln_state
= ND6_LLINFO_STALE
;
520 ln
->ln_expire
= timenow
.tv_sec
+ nd6_gctimer
;
524 case ND6_LLINFO_STALE
:
525 /* Garbage Collection(RFC 2461 5.3) */
530 case ND6_LLINFO_DELAY
:
531 if (ndi
&& (ndi
->flags
& ND6_IFF_PERFORMNUD
) != 0) {
534 ln
->ln_state
= ND6_LLINFO_PROBE
;
535 ln
->ln_expire
= timenow
.tv_sec
+
537 nd6_ns_output(ifp
, &dst
->sin6_addr
,
541 ln
->ln_state
= ND6_LLINFO_STALE
; /* XXX */
542 ln
->ln_expire
= timenow
.tv_sec
+ nd6_gctimer
;
545 case ND6_LLINFO_PROBE
:
546 if (ln
->ln_asked
< nd6_umaxtries
) {
548 ln
->ln_expire
= timenow
.tv_sec
+
549 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
550 nd6_ns_output(ifp
, &dst
->sin6_addr
,
551 &dst
->sin6_addr
, ln
, 0, 0);
560 /* expire default router list */
561 lck_mtx_lock(nd6_mutex
);
562 dr
= TAILQ_FIRST(&nd_defrouter
);
564 if (dr
->expire
&& dr
->expire
< timenow
.tv_sec
) {
565 struct nd_defrouter
*t
;
566 t
= TAILQ_NEXT(dr
, dr_entry
);
567 defrtrlist_del(dr
, 1);
570 dr
= TAILQ_NEXT(dr
, dr_entry
);
575 * expire interface addresses.
576 * in the past the loop was inside prefix expiry processing.
577 * However, from a stricter speci-confrmance standpoint, we should
578 * rather separate address lifetimes and prefix lifetimes.
581 for (ia6
= in6_ifaddrs
; ia6
; ia6
= nia6
) {
583 /* check address lifetime */
584 lt6
= &ia6
->ia6_lifetime
;
585 if (IFA6_IS_INVALID(ia6
)) {
589 * If the expiring address is temporary, try
590 * regenerating a new one. This would be useful when
591 * we suspended a laptop PC, then turned it on after a
592 * period that could invalidate all temporary
593 * addresses. Although we may have to restart the
594 * loop (see below), it must be after purging the
595 * address. Otherwise, we'd see an infinite loop of
598 if (ip6_use_tempaddr
&&
599 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
600 if (regen_tmpaddr(ia6
) == 0)
604 in6_purgeaddr(&ia6
->ia_ifa
, 1);
607 goto addrloop
; /* XXX: see below */
609 if (IFA6_IS_DEPRECATED(ia6
)) {
610 int oldflags
= ia6
->ia6_flags
;
612 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
615 * If a temporary address has just become deprecated,
616 * regenerate a new one if possible.
618 if (ip6_use_tempaddr
&&
619 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
620 (oldflags
& IN6_IFF_DEPRECATED
) == 0) {
622 if (regen_tmpaddr(ia6
) == 0) {
624 * A new temporary address is
626 * XXX: this means the address chain
627 * has changed while we are still in
628 * the loop. Although the change
629 * would not cause disaster (because
630 * it's not a deletion, but an
631 * addition,) we'd rather restart the
632 * loop just for safety. Or does this
633 * significantly reduce performance??
640 * A new RA might have made a deprecated address
643 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
647 /* expire prefix list */
648 pr
= nd_prefix
.lh_first
;
651 * check prefix lifetime.
652 * since pltime is just for autoconf, pltime processing for
653 * prefix is not necessary.
655 if (pr
->ndpr_expire
&& pr
->ndpr_expire
< timenow
.tv_sec
) {
660 * address expiration and prefix expiration are
661 * separate. NEVER perform in6_purgeaddr here.
664 prelist_remove(pr
, 1);
669 lck_mtx_unlock(nd6_mutex
);
670 timeout(nd6_timer
, (caddr_t
)0, nd6_prune
* hz
);
675 struct in6_ifaddr
*ia6
) /* deprecated/invalidated temporary address */
679 struct in6_ifaddr
*public_ifa6
= NULL
;
680 struct timeval timenow
;
682 getmicrotime(&timenow
);
684 ifp
= ia6
->ia_ifa
.ifa_ifp
;
685 ifnet_lock_exclusive(ifp
);
686 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
687 ifa
= ifa
->ifa_list
.tqe_next
)
689 struct in6_ifaddr
*it6
;
691 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
694 it6
= (struct in6_ifaddr
*)ifa
;
696 /* ignore no autoconf addresses. */
697 if ((it6
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
700 /* ignore autoconf addresses with different prefixes. */
701 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
)
705 * Now we are looking at an autoconf address with the same
706 * prefix as ours. If the address is temporary and is still
707 * preferred, do not create another one. It would be rare, but
708 * could happen, for example, when we resume a laptop PC after
711 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
712 !IFA6_IS_DEPRECATED(it6
)) {
718 * This is a public autoconf address that has the same prefix
719 * as ours. If it is preferred, keep it. We can't break the
720 * loop here, because there may be a still-preferred temporary
721 * address with the prefix.
723 if (!IFA6_IS_DEPRECATED(it6
))
726 ifnet_lock_done(ifp
);
728 if (public_ifa6
!= NULL
) {
731 if ((e
= in6_tmpifadd(public_ifa6
, 0)) != 0) {
732 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
733 " tmp addr,errno=%d\n", e
);
743 * Nuke neighbor cache/prefix/default router management table, right before
750 struct llinfo_nd6
*ln
, *nln
;
751 struct nd_defrouter
*dr
, *ndr
, drany
;
752 struct nd_prefix
*pr
, *npr
;
754 /* Nuke default router list entries toward ifp */
755 lck_mtx_lock(nd6_mutex
);
756 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
758 * The first entry of the list may be stored in
759 * the routing table, so we'll delete it later.
761 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= ndr
) {
762 ndr
= TAILQ_NEXT(dr
, dr_entry
);
764 defrtrlist_del(dr
, 1);
766 dr
= TAILQ_FIRST(&nd_defrouter
);
768 defrtrlist_del(dr
, 1);
771 /* Nuke prefix list entries toward ifp */
772 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
774 if (pr
->ndpr_ifp
== ifp
) {
776 * Previously, pr->ndpr_addr is removed as well,
777 * but I strongly believe we don't have to do it.
778 * nd6_purge() is only called from in6_ifdetach(),
779 * which removes all the associated interface addresses
781 * (jinmei@kame.net 20010129)
783 prelist_remove(pr
, 1);
787 /* cancel default outgoing interface setting */
788 if (nd6_defifindex
== ifp
->if_index
)
789 nd6_setdefaultiface(0);
791 if (!ip6_forwarding
&& (ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
792 /* refresh default router list */
793 bzero(&drany
, sizeof(drany
));
794 defrouter_delreq(&drany
, 0);
797 lck_mtx_unlock(nd6_mutex
);
800 * Nuke neighbor cache entries for the ifp.
801 * Note that rt->rt_ifp may not be the same as ifp,
802 * due to KAME goto ours hack. See RTM_RESOLVE case in
803 * nd6_rtrequest(), and ip6_input().
805 ln
= llinfo_nd6
.ln_next
;
806 while (ln
&& ln
!= &llinfo_nd6
) {
808 struct sockaddr_dl
*sdl
;
812 if (rt
&& rt
->rt_gateway
&&
813 rt
->rt_gateway
->sa_family
== AF_LINK
) {
814 sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
;
815 if (sdl
->sdl_index
== ifp
->if_index
)
824 struct in6_addr
*addr6
,
830 struct sockaddr_in6 sin6
;
832 bzero(&sin6
, sizeof(sin6
));
833 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
834 sin6
.sin6_family
= AF_INET6
;
835 sin6
.sin6_addr
= *addr6
;
837 sin6
.sin6_scope_id
= in6_addr2scopeid(ifp
, addr6
);
840 lck_mtx_lock(rt_mtx
);
841 rt
= rtalloc1_locked((struct sockaddr
*)&sin6
, create
, 0UL);
842 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) == 0) {
844 * This is the case for the default route.
845 * If we want to create a neighbor cache for the address, we
846 * should free the route for the destination and allocate an
859 * If no route is available and create is set,
860 * we allocate a host route for the destination
861 * and treat it like an interface route.
862 * This hack is necessary for a neighbor which can't
863 * be covered by our own prefix.
866 ifaof_ifpforaddr((struct sockaddr
*)&sin6
, ifp
);
869 lck_mtx_unlock(rt_mtx
);
874 * Create a new route. RTF_LLINFO is necessary
875 * to create a Neighbor Cache entry for the
876 * destination in nd6_rtrequest which will be
877 * called in rtrequest via ifa->ifa_rtrequest.
879 if ((e
= rtrequest_locked(RTM_ADD
, (struct sockaddr
*)&sin6
,
881 (struct sockaddr
*)&all1_sa
,
883 RTF_HOST
| RTF_LLINFO
) &
888 "nd6_lookup: failed to add route for a "
889 "neighbor(%s), errno=%d\n",
890 ip6_sprintf(addr6
), e
);
895 lck_mtx_unlock(rt_mtx
);
899 struct llinfo_nd6
*ln
=
900 (struct llinfo_nd6
*)rt
->rt_llinfo
;
901 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
905 lck_mtx_unlock(rt_mtx
);
911 * Validation for the entry.
912 * Note that the check for rt_llinfo is necessary because a cloned
913 * route from a parent route that has the L flag (e.g. the default
914 * route to a p2p interface) may have the flag, too, while the
915 * destination is not actually a neighbor.
916 * XXX: we can't use rt->rt_ifp to check for the interface, since
917 * it might be the loopback interface if the entry is for our
918 * own address on a non-loopback interface. Instead, we should
919 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
922 if ((ifp
->if_type
!=IFT_PPP
) && ((rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
923 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
925 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
))) {
927 lck_mtx_unlock(rt_mtx
);
929 log(LOG_DEBUG
, "nd6_lookup: failed to lookup %s (if = %s)\n",
930 ip6_sprintf(addr6
), ifp
? if_name(ifp
) : "unspec");
931 /* xxx more logs... kazu */
936 lck_mtx_unlock(rt_mtx
);
941 * Detect if a given IPv6 address identifies a neighbor on a given link.
942 * XXX: should take care of the destination of a p2p link?
945 nd6_is_addr_neighbor(
946 struct sockaddr_in6
*addr
,
953 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
954 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
957 * A link-local address is always a neighbor.
958 * XXX: we should use the sin6_scope_id field rather than the embedded
961 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
) &&
962 ntohs(*(u_int16_t
*)&addr
->sin6_addr
.s6_addr
[2]) == ifp
->if_index
)
966 * If the address matches one of our addresses,
967 * it should be a neighbor.
969 ifnet_lock_shared(ifp
);
970 for (ifa
= ifp
->if_addrlist
.tqh_first
;
972 ifa
= ifa
->ifa_list
.tqe_next
)
974 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
977 for (i
= 0; i
< 4; i
++) {
978 if ((IFADDR6(ifa
).s6_addr32
[i
] ^
979 addr
->sin6_addr
.s6_addr32
[i
]) &
980 IFMASK6(ifa
).s6_addr32
[i
])
983 ifnet_lock_done(ifp
);
986 ifnet_lock_done(ifp
);
989 * Even if the address matches none of our addresses, it might be
990 * in the neighbor cache.
992 if (nd6_lookup(&addr
->sin6_addr
, 0, ifp
, rt_locked
) != NULL
)
1001 * Free an nd6 llinfo entry.
1007 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
, *next
;
1008 struct in6_addr in6
= ((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
1009 struct nd_defrouter
*dr
;
1012 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1013 * even though it is not harmful, it was not really necessary.
1016 if (!ip6_forwarding
&& (ip6_accept_rtadv
|| (rt
->rt_ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
1017 lck_mtx_lock(nd6_mutex
);
1018 dr
= defrouter_lookup(&((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
,
1021 if ((ln
&& ln
->ln_router
) || dr
) {
1023 * rt6_flush must be called whether or not the neighbor
1024 * is in the Default Router List.
1025 * See a corresponding comment in nd6_na_input().
1027 rt6_flush(&in6
, rt
->rt_ifp
);
1032 * Unreachablity of a router might affect the default
1033 * router selection and on-link detection of advertised
1038 * Temporarily fake the state to choose a new default
1039 * router and to perform on-link determination of
1040 * prefixes correctly.
1041 * Below the state will be set correctly,
1042 * or the entry itself will be deleted.
1044 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1047 * Since defrouter_select() does not affect the
1048 * on-link determination and MIP6 needs the check
1049 * before the default router selection, we perform
1052 pfxlist_onlink_check(1);
1054 if (dr
== TAILQ_FIRST(&nd_defrouter
)) {
1056 * It is used as the current default router,
1057 * so we have to move it to the end of the
1058 * list and choose a new one.
1059 * XXX: it is not very efficient if this is
1062 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1063 TAILQ_INSERT_TAIL(&nd_defrouter
, dr
, dr_entry
);
1068 lck_mtx_unlock(nd6_mutex
);
1072 * Before deleting the entry, remember the next entry as the
1073 * return value. We need this because pfxlist_onlink_check() above
1074 * might have freed other entries (particularly the old next entry) as
1075 * a side effect (XXX).
1083 * Detach the route from the routing tree and the list of neighbor
1084 * caches, and disable the route entry not to be used in already
1087 rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
1088 rt_mask(rt
), 0, (struct rtentry
**)0);
1094 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1096 * XXX cost-effective metods?
1101 struct in6_addr
*dst6
,
1104 struct llinfo_nd6
*ln
;
1105 struct timeval timenow
;
1107 getmicrotime(&timenow
);
1110 * If the caller specified "rt", use that. Otherwise, resolve the
1111 * routing table by supplied "dst6".
1116 if (!(rt
= nd6_lookup(dst6
, 0, NULL
, 0)))
1120 if ((rt
->rt_flags
& RTF_GATEWAY
) != 0 ||
1121 (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1122 !rt
->rt_llinfo
|| !rt
->rt_gateway
||
1123 rt
->rt_gateway
->sa_family
!= AF_LINK
) {
1124 /* This is not a host route. */
1128 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1129 if (ln
->ln_state
< ND6_LLINFO_REACHABLE
)
1133 * if we get upper-layer reachability confirmation many times,
1134 * it is possible we have false information.
1138 if (ln
->ln_byhint
> nd6_maxnudhint
)
1142 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1144 ln
->ln_expire
= timenow
.tv_sec
+
1145 nd_ifinfo
[rt
->rt_ifp
->if_index
].reachable
;
1152 struct sockaddr
*sa
) /* xxx unused */
1154 struct sockaddr
*gate
= rt
->rt_gateway
;
1155 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1156 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
};
1157 struct ifnet
*ifp
= rt
->rt_ifp
;
1159 struct timeval timenow
;
1162 if ((rt
->rt_flags
& RTF_GATEWAY
))
1165 if (nd6_need_cache(ifp
) == 0 && (rt
->rt_flags
& RTF_HOST
) == 0) {
1167 * This is probably an interface direct route for a link
1168 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1169 * We do not need special treatment below for such a route.
1170 * Moreover, the RTF_LLINFO flag which would be set below
1171 * would annoy the ndp(8) command.
1176 if (req
== RTM_RESOLVE
&&
1177 (nd6_need_cache(ifp
) == 0 || /* stf case */
1178 !nd6_is_addr_neighbor((struct sockaddr_in6
*)rt_key(rt
), ifp
, 1))) {
1180 * FreeBSD and BSD/OS often make a cloned host route based
1181 * on a less-specific route (e.g. the default route).
1182 * If the less specific route does not have a "gateway"
1183 * (this is the case when the route just goes to a p2p or an
1184 * stf interface), we'll mistakenly make a neighbor cache for
1185 * the host route, and will see strange neighbor solicitation
1186 * for the corresponding destination. In order to avoid the
1187 * confusion, we check if the destination of the route is
1188 * a neighbor in terms of neighbor discovery, and stop the
1189 * process if not. Additionally, we remove the LLINFO flag
1190 * so that ndp(8) will not try to get the neighbor information
1191 * of the destination.
1193 rt
->rt_flags
&= ~RTF_LLINFO
;
1197 getmicrotime(&timenow
);
1201 * There is no backward compatibility :)
1203 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1204 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1205 * rt->rt_flags |= RTF_CLONING;
1207 if (rt
->rt_flags
& (RTF_CLONING
| RTF_LLINFO
)) {
1209 * Case 1: This route should come from
1210 * a route to interface. RTF_LLINFO flag is set
1211 * for a host route whose destination should be
1212 * treated as on-link.
1214 rt_setgate(rt
, rt_key(rt
),
1215 (struct sockaddr
*)&null_sdl
);
1216 gate
= rt
->rt_gateway
;
1217 SDL(gate
)->sdl_type
= ifp
->if_type
;
1218 SDL(gate
)->sdl_index
= ifp
->if_index
;
1220 ln
->ln_expire
= timenow
.tv_sec
;
1222 if (ln
&& ln
->ln_expire
== 0) {
1223 /* kludge for desktops */
1225 printf("nd6_rtequest: time.tv_sec is zero; "
1231 if ((rt
->rt_flags
& RTF_CLONING
))
1235 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1236 * We don't do that here since llinfo is not ready yet.
1238 * There are also couple of other things to be discussed:
1239 * - unsolicited NA code needs improvement beforehand
1240 * - RFC2461 says we MAY send multicast unsolicited NA
1241 * (7.2.6 paragraph 4), however, it also says that we
1242 * SHOULD provide a mechanism to prevent multicast NA storm.
1243 * we don't have anything like it right now.
1244 * note that the mechanism needs a mutual agreement
1245 * between proxies, which means that we need to implement
1246 * a new protocol, or a new kludge.
1247 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1248 * we need to check ip6forwarding before sending it.
1249 * (or should we allow proxy ND configuration only for
1250 * routers? there's no mention about proxy ND from hosts)
1253 /* XXX it does not work */
1254 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1256 &SIN6(rt_key(rt
))->sin6_addr
,
1257 &SIN6(rt_key(rt
))->sin6_addr
,
1258 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1263 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) == 0) {
1265 * Address resolution isn't necessary for a point to
1266 * point link, so we can skip this test for a p2p link.
1268 if (gate
->sa_family
!= AF_LINK
||
1269 gate
->sa_len
< sizeof(null_sdl
)) {
1271 "nd6_rtrequest: bad gateway value: %s\n",
1275 SDL(gate
)->sdl_type
= ifp
->if_type
;
1276 SDL(gate
)->sdl_index
= ifp
->if_index
;
1279 break; /* This happens on a route change */
1281 * Case 2: This route may come from cloning, or a manual route
1282 * add with a LL address.
1284 R_Malloc(ln
, struct llinfo_nd6
*, sizeof(*ln
));
1285 rt
->rt_llinfo
= (caddr_t
)ln
;
1287 log(LOG_DEBUG
, "nd6_rtrequest: malloc failed\n");
1292 Bzero(ln
, sizeof(*ln
));
1294 /* this is required for "ndp" command. - shin */
1295 if (req
== RTM_ADD
) {
1297 * gate should have some valid AF_LINK entry,
1298 * and ln->ln_expire should have some lifetime
1299 * which is specified by ndp command.
1301 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1305 * When req == RTM_RESOLVE, rt is created and
1306 * initialized in rtrequest(), so rt_expire is 0.
1308 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1309 ln
->ln_expire
= timenow
.tv_sec
;
1311 rt
->rt_flags
|= RTF_LLINFO
;
1312 ln
->ln_next
= llinfo_nd6
.ln_next
;
1313 llinfo_nd6
.ln_next
= ln
;
1314 ln
->ln_prev
= &llinfo_nd6
;
1315 ln
->ln_next
->ln_prev
= ln
;
1318 * check if rt_key(rt) is one of my address assigned
1321 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1322 &SIN6(rt_key(rt
))->sin6_addr
);
1324 caddr_t macp
= nd6_ifptomac(ifp
);
1326 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1329 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
1330 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
1332 if (nd6_useloopback
) {
1333 rt
->rt_ifp
= &loif
[0]; /* XXX */
1335 * Make sure rt_ifa be equal to the ifaddr
1336 * corresponding to the address.
1337 * We need this because when we refer
1338 * rt_ifa->ia6_flags in ip6_input, we assume
1339 * that the rt_ifa points to the address instead
1340 * of the loopback address.
1342 if (ifa
!= rt
->rt_ifa
) {
1346 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
1348 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1351 /* join solicited node multicast for proxy ND */
1352 if (ifp
->if_flags
& IFF_MULTICAST
) {
1353 struct in6_addr llsol
;
1356 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1357 llsol
.s6_addr16
[0] = htons(0xff02);
1358 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1359 llsol
.s6_addr32
[1] = 0;
1360 llsol
.s6_addr32
[2] = htonl(1);
1361 llsol
.s6_addr8
[12] = 0xff;
1363 if (!in6_addmulti(&llsol
, ifp
, &error
, 0)) {
1364 nd6log((LOG_ERR
, "%s: failed to join "
1365 "%s (errno=%d)\n", if_name(ifp
),
1366 ip6_sprintf(&llsol
), error
));
1375 /* leave from solicited node multicast for proxy ND */
1376 if ((rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
1377 (ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1378 struct in6_addr llsol
;
1379 struct in6_multi
*in6m
;
1381 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1382 llsol
.s6_addr16
[0] = htons(0xff02);
1383 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1384 llsol
.s6_addr32
[1] = 0;
1385 llsol
.s6_addr32
[2] = htonl(1);
1386 llsol
.s6_addr8
[12] = 0xff;
1388 ifnet_lock_shared(ifp
);
1389 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1390 ifnet_lock_done(ifp
);
1392 in6_delmulti(in6m
, 0);
1395 ln
->ln_next
->ln_prev
= ln
->ln_prev
;
1396 ln
->ln_prev
->ln_next
= ln
->ln_next
;
1399 rt
->rt_flags
&= ~RTF_LLINFO
;
1401 m_freem(ln
->ln_hold
);
1403 R_Free((caddr_t
)ln
);
1413 struct in6_drlist
*drl
= (struct in6_drlist
*)data
;
1414 struct in6_prlist
*prl
= (struct in6_prlist
*)data
;
1415 struct in6_ndireq
*ndi
= (struct in6_ndireq
*)data
;
1416 struct in6_nbrinfo
*nbi
= (struct in6_nbrinfo
*)data
;
1417 struct in6_ndifreq
*ndif
= (struct in6_ndifreq
*)data
;
1418 struct nd_defrouter
*dr
, any
;
1419 struct nd_prefix
*pr
;
1421 int i
= 0, error
= 0;
1424 case SIOCGDRLST_IN6
:
1426 * obsolete API, use sysctl under net.inet6.icmp6
1428 lck_mtx_lock(nd6_mutex
);
1429 bzero(drl
, sizeof(*drl
));
1430 dr
= TAILQ_FIRST(&nd_defrouter
);
1431 while (dr
&& i
< DRLSTSIZ
) {
1432 drl
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1433 if (IN6_IS_ADDR_LINKLOCAL(&drl
->defrouter
[i
].rtaddr
)) {
1434 /* XXX: need to this hack for KAME stack */
1435 drl
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1438 "default router list contains a "
1439 "non-linklocal address(%s)\n",
1440 ip6_sprintf(&drl
->defrouter
[i
].rtaddr
));
1442 drl
->defrouter
[i
].flags
= dr
->flags
;
1443 drl
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1444 drl
->defrouter
[i
].expire
= dr
->expire
;
1445 drl
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1447 dr
= TAILQ_NEXT(dr
, dr_entry
);
1449 lck_mtx_unlock(nd6_mutex
);
1451 case SIOCGPRLST_IN6
:
1453 * obsolete API, use sysctl under net.inet6.icmp6
1456 * XXX meaning of fields, especialy "raflags", is very
1457 * differnet between RA prefix list and RR/static prefix list.
1458 * how about separating ioctls into two?
1460 bzero(prl
, sizeof(*prl
));
1461 lck_mtx_lock(nd6_mutex
);
1462 pr
= nd_prefix
.lh_first
;
1463 while (pr
&& i
< PRLSTSIZ
) {
1464 struct nd_pfxrouter
*pfr
;
1467 (void)in6_embedscope(&prl
->prefix
[i
].prefix
,
1468 &pr
->ndpr_prefix
, NULL
, NULL
);
1469 prl
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1470 prl
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1471 prl
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1472 prl
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1473 prl
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1474 prl
->prefix
[i
].expire
= pr
->ndpr_expire
;
1476 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1480 #define RTRADDR prl->prefix[i].advrtr[j]
1481 RTRADDR
= pfr
->router
->rtaddr
;
1482 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1483 /* XXX: hack for KAME */
1484 RTRADDR
.s6_addr16
[1] = 0;
1487 "a router(%s) advertises "
1489 "non-link local address\n",
1490 ip6_sprintf(&RTRADDR
));
1494 pfr
= pfr
->pfr_next
;
1496 prl
->prefix
[i
].advrtrs
= j
;
1497 prl
->prefix
[i
].origin
= PR_ORIG_RA
;
1503 struct rr_prefix
*rpp
;
1505 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1506 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1509 (void)in6_embedscope(&prl
->prefix
[i
].prefix
,
1510 &pr
->ndpr_prefix
, NULL
, NULL
);
1511 prl
->prefix
[i
].raflags
= rpp
->rp_raf
;
1512 prl
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1513 prl
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1514 prl
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1515 prl
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1516 prl
->prefix
[i
].expire
= rpp
->rp_expire
;
1517 prl
->prefix
[i
].advrtrs
= 0;
1518 prl
->prefix
[i
].origin
= rpp
->rp_origin
;
1522 lck_mtx_unlock(nd6_mutex
);
1524 case OSIOCGIFINFO_IN6
:
1525 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1529 ndi
->ndi
.linkmtu
= nd_ifinfo
[ifp
->if_index
].linkmtu
;
1530 ndi
->ndi
.maxmtu
= nd_ifinfo
[ifp
->if_index
].maxmtu
;
1531 ndi
->ndi
.basereachable
=
1532 nd_ifinfo
[ifp
->if_index
].basereachable
;
1533 ndi
->ndi
.reachable
= nd_ifinfo
[ifp
->if_index
].reachable
;
1534 ndi
->ndi
.retrans
= nd_ifinfo
[ifp
->if_index
].retrans
;
1535 ndi
->ndi
.flags
= nd_ifinfo
[ifp
->if_index
].flags
;
1536 ndi
->ndi
.recalctm
= nd_ifinfo
[ifp
->if_index
].recalctm
;
1537 ndi
->ndi
.chlim
= nd_ifinfo
[ifp
->if_index
].chlim
;
1538 ndi
->ndi
.receivedra
= nd_ifinfo
[ifp
->if_index
].receivedra
;
1540 case SIOCGIFINFO_IN6
:
1541 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1545 ndi
->ndi
= nd_ifinfo
[ifp
->if_index
];
1547 case SIOCSIFINFO_FLAGS
:
1548 /* XXX: almost all other fields of ndi->ndi is unused */
1549 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1553 nd_ifinfo
[ifp
->if_index
].flags
= ndi
->ndi
.flags
;
1555 case SIOCSNDFLUSH_IN6
: /* XXX: the ioctl name is confusing... */
1556 /* flush default router list */
1558 * xxx sumikawa: should not delete route if default
1559 * route equals to the top of default router list
1561 bzero(&any
, sizeof(any
));
1562 lck_mtx_lock(nd6_mutex
);
1563 defrouter_delreq(&any
, 1);
1565 lck_mtx_unlock(nd6_mutex
);
1566 /* xxx sumikawa: flush prefix list */
1568 case SIOCSPFXFLUSH_IN6
:
1570 /* flush all the prefix advertised by routers */
1571 struct nd_prefix
*pr
, *next
;
1572 lck_mtx_lock(nd6_mutex
);
1574 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
1575 struct in6_ifaddr
*ia
, *ia_next
;
1577 next
= pr
->ndpr_next
;
1579 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
))
1582 /* do we really have to remove addresses as well? */
1583 for (ia
= in6_ifaddrs
; ia
; ia
= ia_next
) {
1584 /* ia might be removed. keep the next ptr. */
1585 ia_next
= ia
->ia_next
;
1587 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
1590 if (ia
->ia6_ndpr
== pr
)
1591 in6_purgeaddr(&ia
->ia_ifa
, 1);
1593 prelist_remove(pr
, 1);
1595 lck_mtx_unlock(nd6_mutex
);
1598 case SIOCSRTRFLUSH_IN6
:
1600 /* flush all the default routers */
1601 struct nd_defrouter
*dr
, *next
;
1603 lck_mtx_lock(nd6_mutex
);
1604 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
1606 * The first entry of the list may be stored in
1607 * the routing table, so we'll delete it later.
1609 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
1610 next
= TAILQ_NEXT(dr
, dr_entry
);
1611 defrtrlist_del(dr
, 1);
1613 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
), 1);
1615 lck_mtx_unlock(nd6_mutex
);
1618 case SIOCGNBRINFO_IN6
:
1620 struct llinfo_nd6
*ln
;
1621 struct in6_addr nb_addr
= nbi
->addr
; /* make local for safety */
1624 * XXX: KAME specific hack for scoped addresses
1625 * XXXX: for other scopes than link-local?
1627 if (IN6_IS_ADDR_LINKLOCAL(&nbi
->addr
) ||
1628 IN6_IS_ADDR_MC_LINKLOCAL(&nbi
->addr
)) {
1629 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
1632 *idp
= htons(ifp
->if_index
);
1635 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
1639 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1640 nbi
->state
= ln
->ln_state
;
1641 nbi
->asked
= ln
->ln_asked
;
1642 nbi
->isrouter
= ln
->ln_router
;
1643 nbi
->expire
= ln
->ln_expire
;
1647 case SIOCGDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1648 ndif
->ifindex
= nd6_defifindex
;
1650 case SIOCSDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1651 return(nd6_setdefaultiface(ndif
->ifindex
));
1658 * Create neighbor cache entry and cache link-layer address,
1659 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1664 struct in6_addr
*from
,
1667 int type
, /* ICMP6 type */
1668 int code
) /* type dependent information */
1670 struct rtentry
*rt
= NULL
;
1671 struct llinfo_nd6
*ln
= NULL
;
1673 struct sockaddr_dl
*sdl
= NULL
;
1678 struct timeval timenow
;
1681 panic("ifp == NULL in nd6_cache_lladdr");
1683 panic("from == NULL in nd6_cache_lladdr");
1685 /* nothing must be updated for unspecified address */
1686 if (IN6_IS_ADDR_UNSPECIFIED(from
))
1690 * Validation about ifp->if_addrlen and lladdrlen must be done in
1693 * XXX If the link does not have link-layer adderss, what should
1694 * we do? (ifp->if_addrlen == 0)
1695 * Spec says nothing in sections for RA, RS and NA. There's small
1696 * description on it in NS section (RFC 2461 7.2.3).
1698 getmicrotime(&timenow
);
1700 lck_mtx_lock(rt_mtx
);
1701 rt
= nd6_lookup(from
, 0, ifp
, 1);
1704 /* nothing must be done if there's no lladdr */
1705 if (!lladdr
|| !lladdrlen
)
1709 rt
= nd6_lookup(from
, 1, ifp
, 1);
1712 /* do nothing if static ndp is set */
1713 if (rt
->rt_flags
& RTF_STATIC
) {
1714 lck_mtx_unlock(rt_mtx
);
1720 lck_mtx_unlock(rt_mtx
);
1724 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
1729 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1732 if (!rt
->rt_gateway
)
1734 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
1736 sdl
= SDL(rt
->rt_gateway
);
1738 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
1739 if (olladdr
&& lladdr
) {
1740 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
1748 * newentry olladdr lladdr llchange (*=record)
1751 * 0 n y -- (3) * STALE
1753 * 0 y y y (5) * STALE
1754 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1755 * 1 -- y -- (7) * STALE
1758 if (lladdr
) { /* (3-5) and (7) */
1760 * Record source link-layer address
1761 * XXX is it dependent to ifp->if_type?
1763 sdl
->sdl_alen
= ifp
->if_addrlen
;
1764 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
1768 if ((!olladdr
&& lladdr
) /* (3) */
1769 || (olladdr
&& lladdr
&& llchange
)) { /* (5) */
1771 newstate
= ND6_LLINFO_STALE
;
1772 } else /* (1-2,4) */
1776 if (!lladdr
) /* (6) */
1777 newstate
= ND6_LLINFO_NOSTATE
;
1779 newstate
= ND6_LLINFO_STALE
;
1784 * Update the state of the neighbor cache.
1786 ln
->ln_state
= newstate
;
1788 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1790 * XXX: since nd6_output() below will cause
1791 * state tansition to DELAY and reset the timer,
1792 * we must set the timer now, although it is actually
1795 ln
->ln_expire
= timenow
.tv_sec
+ nd6_gctimer
;
1799 * we assume ifp is not a p2p here, so just
1800 * set the 2nd argument as the 1st one.
1802 nd6_output(ifp
, ifp
, ln
->ln_hold
,
1803 (struct sockaddr_in6
*)rt_key(rt
),
1807 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
1808 /* probe right away */
1809 ln
->ln_expire
= timenow
.tv_sec
;
1814 * ICMP6 type dependent behavior.
1816 * NS: clear IsRouter if new entry
1817 * RS: clear IsRouter
1818 * RA: set IsRouter if there's lladdr
1819 * redir: clear IsRouter if new entry
1822 * The spec says that we must set IsRouter in the following cases:
1823 * - If lladdr exist, set IsRouter. This means (1-5).
1824 * - If it is old entry (!newentry), set IsRouter. This means (7).
1825 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1826 * A quetion arises for (1) case. (1) case has no lladdr in the
1827 * neighbor cache, this is similar to (6).
1828 * This case is rare but we figured that we MUST NOT set IsRouter.
1830 * newentry olladdr lladdr llchange NS RS RA redir
1832 * 0 n n -- (1) c ? s
1833 * 0 y n -- (2) c s s
1834 * 0 n y -- (3) c s s
1837 * 1 -- n -- (6) c c c s
1838 * 1 -- y -- (7) c c s c s
1842 switch (type
& 0xff) {
1843 case ND_NEIGHBOR_SOLICIT
:
1845 * New entry must have is_router flag cleared.
1847 if (is_newentry
) /* (6-7) */
1852 * If the icmp is a redirect to a better router, always set the
1853 * is_router flag. Otherwise, if the entry is newly created,
1854 * clear the flag. [RFC 2461, sec 8.3]
1856 if (code
== ND_REDIRECT_ROUTER
)
1858 else if (is_newentry
) /* (6-7) */
1861 case ND_ROUTER_SOLICIT
:
1863 * is_router flag must always be cleared.
1867 case ND_ROUTER_ADVERT
:
1869 * Mark an entry with lladdr as a router.
1871 if ((!is_newentry
&& (olladdr
|| lladdr
)) /* (2-5) */
1872 || (is_newentry
&& lladdr
)) { /* (7) */
1879 * When the link-layer address of a router changes, select the
1880 * best router again. In particular, when the neighbor entry is newly
1881 * created, it might affect the selection policy.
1882 * Question: can we restrict the first condition to the "is_newentry"
1884 * XXX: when we hear an RA from a new router with the link-layer
1885 * address option, defrouter_select() is called twice, since
1886 * defrtrlist_update called the function as well. However, I believe
1887 * we can compromise the overhead, since it only happens the first
1889 * XXX: although defrouter_select() should not have a bad effect
1890 * for those are not autoconfigured hosts, we explicitly avoid such
1893 if (do_update
&& ln
->ln_router
&& !ip6_forwarding
&& (ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
1894 lck_mtx_lock(nd6_mutex
);
1896 lck_mtx_unlock(nd6_mutex
);
1907 struct nd_ifinfo
*nd6if
;
1909 lck_mtx_lock(nd6_mutex
);
1910 for (i
= 1; i
< if_index
+ 1; i
++) {
1911 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
)
1913 nd6if
= &nd_ifinfo
[i
];
1914 if (nd6if
->basereachable
&& /* already initialized */
1915 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
1917 * Since reachable time rarely changes by router
1918 * advertisements, we SHOULD insure that a new random
1919 * value gets recomputed at least once every few hours.
1922 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
1923 nd6if
->reachable
= ND_COMPUTE_RTIME(nd6if
->basereachable
);
1926 lck_mtx_unlock(nd6_mutex
);
1927 timeout(nd6_slowtimo
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
1931 #define senderr(e) { error = (e); goto bad;}
1935 struct ifnet
*origifp
,
1937 struct sockaddr_in6
*dst
,
1938 struct rtentry
*rt0
,
1941 struct mbuf
*m
= m0
;
1942 struct rtentry
*rt
= rt0
;
1943 struct sockaddr_in6
*gw6
= NULL
;
1944 struct llinfo_nd6
*ln
= NULL
;
1946 struct timeval timenow
;
1948 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
))
1951 if (nd6_need_cache(ifp
) == 0)
1955 * next hop determination. This routine is derived from ether_outpout.
1957 lck_mtx_lock(rt_mtx
);
1959 if ((rt
->rt_flags
& RTF_UP
) == 0) {
1960 if ((rt0
= rt
= rtalloc1_locked((struct sockaddr
*)dst
, 1, 0UL)) !=
1964 if (rt
->rt_ifp
!= ifp
) {
1965 /* XXX: loop care? */
1966 lck_mtx_unlock(rt_mtx
);
1967 return nd6_output(ifp
, origifp
, m0
,
1971 lck_mtx_unlock(rt_mtx
);
1972 senderr(EHOSTUNREACH
);
1976 if (rt
->rt_flags
& RTF_GATEWAY
) {
1977 gw6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
1980 * We skip link-layer address resolution and NUD
1981 * if the gateway is not a neighbor from ND point
1982 * of view, regardless of the value of nd_ifinfo.flags.
1983 * The second condition is a bit tricky; we skip
1984 * if the gateway is our own address, which is
1985 * sometimes used to install a route to a p2p link.
1987 if (!nd6_is_addr_neighbor(gw6
, ifp
, 1) ||
1988 in6ifa_ifpwithaddr(ifp
, &gw6
->sin6_addr
)) {
1990 * We allow this kind of tricky route only
1991 * when the outgoing interface is p2p.
1992 * XXX: we may need a more generic rule here.
1994 lck_mtx_unlock(rt_mtx
);
1995 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
1996 senderr(EHOSTUNREACH
);
2001 if (rt
->rt_gwroute
== 0)
2003 if (((rt
= rt
->rt_gwroute
)->rt_flags
& RTF_UP
) == 0) {
2004 rtfree_locked(rt
); rt
= rt0
;
2005 lookup
: rt
->rt_gwroute
= rtalloc1_locked(rt
->rt_gateway
, 1, 0UL);
2006 if ((rt
= rt
->rt_gwroute
) == 0) {
2007 lck_mtx_unlock(rt_mtx
);
2008 senderr(EHOSTUNREACH
);
2015 * Address resolution or Neighbor Unreachability Detection
2017 * At this point, the destination of the packet must be a unicast
2018 * or an anycast address(i.e. not a multicast).
2021 /* Look up the neighbor cache for the nexthop */
2022 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0)
2023 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
2026 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2027 * the condition below is not very efficient. But we believe
2028 * it is tolerable, because this should be a rare case.
2030 if (nd6_is_addr_neighbor(dst
, ifp
, 1) &&
2031 (rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
, 1)) != NULL
)
2032 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
2034 lck_mtx_unlock(rt_mtx
);
2036 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0 &&
2037 !(nd_ifinfo
[ifp
->if_index
].flags
& ND6_IFF_PERFORMNUD
)) {
2039 "nd6_output: can't allocate llinfo for %s "
2041 ip6_sprintf(&dst
->sin6_addr
), ln
, rt
);
2042 senderr(EIO
); /* XXX: good error? */
2045 goto sendpkt
; /* send anyway */
2048 getmicrotime(&timenow
);
2050 /* We don't have to do link-layer address resolution on a p2p link. */
2051 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
2052 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
2053 ln
->ln_state
= ND6_LLINFO_STALE
;
2054 ln
->ln_expire
= timenow
.tv_sec
+ nd6_gctimer
;
2058 * The first time we send a packet to a neighbor whose entry is
2059 * STALE, we have to change the state to DELAY and a sets a timer to
2060 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2061 * neighbor unreachability detection on expiration.
2064 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
2066 ln
->ln_state
= ND6_LLINFO_DELAY
;
2067 ln
->ln_expire
= timenow
.tv_sec
+ nd6_delay
;
2071 * If the neighbor cache entry has a state other than INCOMPLETE
2072 * (i.e. its link-layer address is already resolved), just
2075 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
)
2079 * There is a neighbor cache entry, but no ethernet address
2080 * response yet. Replace the held mbuf (if any) with this
2083 * This code conforms to the rate-limiting rule described in Section
2084 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
2087 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
)
2088 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
2090 m_freem(ln
->ln_hold
);
2092 if (ln
->ln_expire
) {
2093 if (ln
->ln_asked
< nd6_mmaxtries
&&
2094 ln
->ln_expire
< timenow
.tv_sec
) {
2096 ln
->ln_expire
= timenow
.tv_sec
+
2097 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
2098 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0, locked
);
2106 /* Make sure the HW checksum flags are cleaned before sending the packet */
2108 m
->m_pkthdr
.csum_data
= 0;
2109 m
->m_pkthdr
.csum_flags
= 0;
2111 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
2112 m
->m_pkthdr
.rcvif
= origifp
; /* forwarding rules require the original scope_id */
2114 lck_mtx_unlock(ip6_mutex
);
2115 error
= dlil_output(origifp
, PF_INET6
, m
, (caddr_t
)rt
, (struct sockaddr
*)dst
, 0);
2117 lck_mtx_lock(ip6_mutex
);
2120 /* Do not allow loopback address to wind up on a wire */
2121 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2123 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
2124 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
))) {
2125 ip6stat
.ip6s_badscope
++;
2127 * Do not simply drop the packet just like a firewall -- we want the
2128 * the application to feel the pain.
2129 * Return ENETUNREACH like ip6_output does in some similar cases.
2130 * This can startle the otherwise clueless process that specifies
2131 * loopback as the source address.
2133 error
= ENETUNREACH
;
2138 m
->m_pkthdr
.rcvif
= 0;
2140 lck_mtx_unlock(ip6_mutex
);
2141 error
= dlil_output(ifp
, PF_INET6
, m
, (caddr_t
)rt
, (struct sockaddr
*)dst
, 0);
2143 lck_mtx_lock(ip6_mutex
);
2146 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
2147 return((*ifp
->if_output
)(origifp
, m
, (struct sockaddr
*)dst
,
2150 return((*ifp
->if_output
)(ifp
, m
, (struct sockaddr
*)dst
, rt
));
2165 * XXX: we currently do not make neighbor cache on any interface
2166 * other than ARCnet, Ethernet, FDDI and GIF.
2169 * - unidirectional tunnels needs no ND
2171 switch (ifp
->if_type
) {
2177 case IFT_IEEE8023ADLAG
:
2181 case IFT_GIF
: /* XXX need more cases? */
2193 struct sockaddr
*dst
,
2197 struct sockaddr_dl
*sdl
;
2199 if (m
->m_flags
& M_MCAST
) {
2200 switch (ifp
->if_type
) {
2204 case IFT_IEEE8023ADLAG
:
2208 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
2212 for (i
= 0; i
< ifp
->if_addrlen
; i
++)
2219 return(0); /* caller will free mbuf */
2224 /* this could happen, if we could not allocate memory */
2225 return(0); /* caller will free mbuf */
2227 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
2228 printf("nd6_storelladdr: something odd happens\n");
2229 return(0); /* caller will free mbuf */
2231 sdl
= SDL(rt
->rt_gateway
);
2232 if (sdl
->sdl_alen
== 0) {
2233 /* this should be impossible, but we bark here for debugging */
2234 printf("nd6_storelladdr: sdl_alen == 0\n");
2235 return(0); /* caller will free mbuf */
2238 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
2242 extern errno_t
arp_route_to_gateway_route(const struct sockaddr
*net_dest
,
2243 route_t hint
, route_t
*out_route
);
2248 const struct sockaddr_in6
*ip6_dest
,
2249 struct sockaddr_dl
*ll_dest
,
2254 route_t route
= hint
;
2256 struct sockaddr_dl
*sdl
= NULL
;
2259 if (ip6_dest
->sin6_family
!= AF_INET6
)
2260 return EAFNOSUPPORT
;
2262 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
2266 result
= arp_route_to_gateway_route((const struct sockaddr
*)ip6_dest
, hint
, &route
);
2271 if ((packet
->m_flags
& M_MCAST
) != 0) {
2272 return dlil_resolve_multi(ifp
, (const struct sockaddr
*)ip6_dest
,
2273 ll_dest
, ll_dest_len
);
2276 if (route
== NULL
) {
2277 /* this could happen, if we could not allocate memory */
2281 lck_mtx_lock(rt_mtx
);
2283 if (route
->rt_gateway
->sa_family
!= AF_LINK
) {
2284 printf("nd6_lookup_ipv6: gateway address not AF_LINK\n");
2285 result
= EADDRNOTAVAIL
;
2289 sdl
= SDL(route
->rt_gateway
);
2290 if (sdl
->sdl_alen
== 0) {
2291 /* this should be impossible, but we bark here for debugging */
2292 printf("nd6_storelladdr: sdl_alen == 0\n");
2293 result
= EHOSTUNREACH
;
2296 copy_len
= sdl
->sdl_len
<= ll_dest_len
? sdl
->sdl_len
: ll_dest_len
;
2297 bcopy(sdl
, ll_dest
, copy_len
);
2300 lck_mtx_unlock(rt_mtx
);
2305 static int nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
;
2306 static int nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
;
2307 SYSCTL_DECL(_net_inet6_icmp6
);
2308 SYSCTL_NODE(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
2309 CTLFLAG_RD
, nd6_sysctl_drlist
, "");
2310 SYSCTL_NODE(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
2311 CTLFLAG_RD
, nd6_sysctl_prlist
, "");
2314 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
2318 struct in6_defrouter
*d
, *de
;
2319 struct nd_defrouter
*dr
;
2325 lck_mtx_lock(nd6_mutex
);
2326 for (dr
= TAILQ_FIRST(&nd_defrouter
);
2328 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2329 d
= (struct in6_defrouter
*)buf
;
2330 de
= (struct in6_defrouter
*)(buf
+ sizeof(buf
));
2333 bzero(d
, sizeof(*d
));
2334 d
->rtaddr
.sin6_family
= AF_INET6
;
2335 d
->rtaddr
.sin6_len
= sizeof(d
->rtaddr
);
2336 if (in6_recoverscope(&d
->rtaddr
, &dr
->rtaddr
,
2340 "default router list (%s)\n",
2341 ip6_sprintf(&dr
->rtaddr
));
2342 d
->flags
= dr
->flags
;
2343 d
->rtlifetime
= dr
->rtlifetime
;
2344 d
->expire
= dr
->expire
;
2345 d
->if_index
= dr
->ifp
->if_index
;
2347 panic("buffer too short");
2349 error
= SYSCTL_OUT(req
, buf
, sizeof(*d
));
2353 lck_mtx_unlock(nd6_mutex
);
2358 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
2362 struct in6_prefix
*p
, *pe
;
2363 struct nd_prefix
*pr
;
2369 lck_mtx_lock(nd6_mutex
);
2371 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2374 struct sockaddr_in6
*sin6
, *s6
;
2375 struct nd_pfxrouter
*pfr
;
2377 p
= (struct in6_prefix
*)buf
;
2378 pe
= (struct in6_prefix
*)(buf
+ sizeof(buf
));
2381 bzero(p
, sizeof(*p
));
2382 sin6
= (struct sockaddr_in6
*)(p
+ 1);
2384 p
->prefix
= pr
->ndpr_prefix
;
2385 if (in6_recoverscope(&p
->prefix
,
2386 &p
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
2388 "scope error in prefix list (%s)\n",
2389 ip6_sprintf(&p
->prefix
.sin6_addr
));
2390 p
->raflags
= pr
->ndpr_raf
;
2391 p
->prefixlen
= pr
->ndpr_plen
;
2392 p
->vltime
= pr
->ndpr_vltime
;
2393 p
->pltime
= pr
->ndpr_pltime
;
2394 p
->if_index
= pr
->ndpr_ifp
->if_index
;
2395 p
->expire
= pr
->ndpr_expire
;
2396 p
->refcnt
= pr
->ndpr_refcnt
;
2397 p
->flags
= pr
->ndpr_stateflags
;
2398 p
->origin
= PR_ORIG_RA
;
2400 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
2402 pfr
= pfr
->pfr_next
) {
2403 if ((void *)&sin6
[advrtrs
+ 1] >
2408 s6
= &sin6
[advrtrs
];
2409 bzero(s6
, sizeof(*s6
));
2410 s6
->sin6_family
= AF_INET6
;
2411 s6
->sin6_len
= sizeof(*sin6
);
2412 if (in6_recoverscope(s6
,
2413 &pfr
->router
->rtaddr
,
2414 pfr
->router
->ifp
) != 0)
2417 "prefix list (%s)\n",
2418 ip6_sprintf(&pfr
->router
->rtaddr
));
2421 p
->advrtrs
= advrtrs
;
2423 panic("buffer too short");
2425 advance
= sizeof(*p
) + sizeof(*sin6
) * advrtrs
;
2426 error
= SYSCTL_OUT(req
, buf
, advance
);
2430 lck_mtx_unlock(nd6_mutex
);