1 /* $FreeBSD: src/sys/netinet6/nd6.c,v 1.2.2.15 2003/05/06 06:46:58 suz 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
42 #include "opt_inet6.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
52 #include <sys/kernel.h>
53 #include <sys/protosw.h>
54 #include <sys/errno.h>
55 #include <sys/syslog.h>
56 #include <sys/queue.h>
57 #include <sys/sysctl.h>
58 #include <sys/mutex.h>
60 #include <sys/thread2.h>
61 #include <sys/mutex2.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/if_ether.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet6/nd6.h>
74 #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};
107 struct nd_drhead nd_defrouter;
108 struct nd_prhead nd_prefix = { 0 };
109 struct mtx nd6_mtx = MTX_INITIALIZER;
111 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
112 static struct sockaddr_in6 all1_sa;
114 static void nd6_setmtu0 (struct ifnet *, struct nd_ifinfo *);
115 static void nd6_slowtimo (void *);
116 static int regen_tmpaddr (struct in6_ifaddr *);
118 struct callout nd6_slowtimo_ch;
119 struct callout nd6_timer_ch;
120 extern struct callout in6_tmpaddrtimer_ch;
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 callout_init(&nd6_slowtimo_ch);
145 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
150 nd6_ifattach(struct ifnet *ifp)
152 struct nd_ifinfo *nd;
154 nd = (struct nd_ifinfo *)kmalloc(sizeof(*nd), M_IP6NDP,
159 nd->linkmtu = ifp->if_mtu;
160 nd->chlim = IPV6_DEFHLIM;
161 nd->basereachable = REACHABLE_TIME;
162 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
163 nd->retrans = RETRANS_TIMER;
167 * Note that the default value of ip6_accept_rtadv is 0, which means
168 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
171 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
173 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
174 nd6_setmtu0(ifp, nd);
179 nd6_ifdetach(struct nd_ifinfo *nd)
185 * Reset ND level link MTU. This function is called when the physical MTU
186 * changes, which means we might have to adjust the ND level MTU.
189 nd6_setmtu(struct ifnet *ifp)
191 nd6_setmtu0(ifp, ND_IFINFO(ifp));
194 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
196 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
201 oldmaxmtu = ndi->maxmtu;
202 oldlinkmtu = ndi->linkmtu;
204 switch (ifp->if_type) {
206 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
208 case IFT_IEEE1394: /* XXX should be IEEE1394MTU(1500) */
209 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
212 case IFT_IEEE80211: /* XXX should be IEEE80211MTU(1500) */
213 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
217 ndi->maxmtu = ifp->if_mtu;
221 if (oldmaxmtu != ndi->maxmtu) {
223 * If the ND level MTU is not set yet, or if the maxmtu
224 * is reset to a smaller value than the ND level MTU,
225 * also reset the ND level MTU.
227 if (ndi->linkmtu == 0 ||
228 ndi->maxmtu < ndi->linkmtu) {
229 ndi->linkmtu = ndi->maxmtu;
230 /* also adjust in6_maxmtu if necessary. */
231 if (oldlinkmtu == 0) {
233 * XXX: the case analysis is grotty, but
234 * it is not efficient to call in6_setmaxmtu()
235 * here when we are during the initialization
238 if (in6_maxmtu < ndi->linkmtu)
239 in6_maxmtu = ndi->linkmtu;
248 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
250 bzero(ndopts, sizeof(*ndopts));
251 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
253 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
256 ndopts->nd_opts_done = 1;
257 ndopts->nd_opts_search = NULL;
262 * Take one ND option.
265 nd6_option(union nd_opts *ndopts)
267 struct nd_opt_hdr *nd_opt;
271 panic("ndopts == NULL in nd6_option");
272 if (!ndopts->nd_opts_last)
273 panic("uninitialized ndopts in nd6_option");
274 if (!ndopts->nd_opts_search)
276 if (ndopts->nd_opts_done)
279 nd_opt = ndopts->nd_opts_search;
281 /* make sure nd_opt_len is inside the buffer */
282 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
283 bzero(ndopts, sizeof(*ndopts));
287 olen = nd_opt->nd_opt_len << 3;
290 * Message validation requires that all included
291 * options have a length that is greater than zero.
293 bzero(ndopts, sizeof(*ndopts));
297 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
298 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
299 /* option overruns the end of buffer, invalid */
300 bzero(ndopts, sizeof(*ndopts));
302 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
303 /* reached the end of options chain */
304 ndopts->nd_opts_done = 1;
305 ndopts->nd_opts_search = NULL;
311 * Parse multiple ND options.
312 * This function is much easier to use, for ND routines that do not need
313 * multiple options of the same type.
316 nd6_options(union nd_opts *ndopts)
318 struct nd_opt_hdr *nd_opt;
322 panic("ndopts == NULL in nd6_options");
323 if (!ndopts->nd_opts_last)
324 panic("uninitialized ndopts in nd6_options");
325 if (!ndopts->nd_opts_search)
329 nd_opt = nd6_option(ndopts);
330 if (!nd_opt && !ndopts->nd_opts_last) {
332 * Message validation requires that all included
333 * options have a length that is greater than zero.
335 icmp6stat.icp6s_nd_badopt++;
336 bzero(ndopts, sizeof(*ndopts));
343 switch (nd_opt->nd_opt_type) {
344 case ND_OPT_SOURCE_LINKADDR:
345 case ND_OPT_TARGET_LINKADDR:
347 case ND_OPT_REDIRECTED_HEADER:
348 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
350 "duplicated ND6 option found (type=%d)\n",
351 nd_opt->nd_opt_type));
354 ndopts->nd_opt_array[nd_opt->nd_opt_type]
358 case ND_OPT_PREFIX_INFORMATION:
359 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
360 ndopts->nd_opt_array[nd_opt->nd_opt_type]
363 ndopts->nd_opts_pi_end =
364 (struct nd_opt_prefix_info *)nd_opt;
368 * Unknown options must be silently ignored,
369 * to accomodate future extension to the protocol.
372 "nd6_options: unsupported option %d - "
373 "option ignored\n", nd_opt->nd_opt_type));
378 if (i > nd6_maxndopt) {
379 icmp6stat.icp6s_nd_toomanyopt++;
380 nd6log((LOG_INFO, "too many loop in nd opt\n"));
384 if (ndopts->nd_opts_done)
392 * ND6 timer routine to expire default route list and prefix list
395 nd6_timer(void *ignored_arg)
397 struct llinfo_nd6 *ln;
398 struct nd_defrouter *dr;
399 struct nd_prefix *pr;
401 struct in6_ifaddr *ia6, *nia6;
404 callout_reset(&nd6_timer_ch, nd6_prune * hz,
407 ln = llinfo_nd6.ln_next;
408 while (ln && ln != &llinfo_nd6) {
410 struct sockaddr_in6 *dst;
411 struct llinfo_nd6 *next = ln->ln_next;
412 /* XXX: used for the DELAY case only: */
413 struct nd_ifinfo *ndi = NULL;
415 if ((rt = ln->ln_rt) == NULL) {
419 if ((ifp = rt->rt_ifp) == NULL) {
423 ndi = ND_IFINFO(ifp);
424 dst = (struct sockaddr_in6 *)rt_key(rt);
426 if (ln->ln_expire > time_uptime) {
433 panic("rt=0 in nd6_timer(ln=%p)", ln);
434 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
435 panic("rt_llinfo(%p) is not equal to ln(%p)",
438 panic("dst=0 in nd6_timer(ln=%p)", ln);
440 switch (ln->ln_state) {
441 case ND6_LLINFO_INCOMPLETE:
442 if (ln->ln_asked < nd6_mmaxtries) {
444 ln->ln_expire = time_uptime +
445 ND_IFINFO(ifp)->retrans / 1000;
446 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
449 struct mbuf *m = ln->ln_hold;
453 * Fake rcvif to make ICMP error
454 * more helpful in diagnosing
456 * XXX: should we consider
459 m->m_pkthdr.rcvif = rt->rt_ifp;
461 icmp6_error(m, ICMP6_DST_UNREACH,
462 ICMP6_DST_UNREACH_ADDR, 0);
468 case ND6_LLINFO_REACHABLE:
470 ln->ln_state = ND6_LLINFO_STALE;
471 ln->ln_expire = time_uptime + nd6_gctimer;
475 case ND6_LLINFO_STALE:
476 /* Garbage Collection(RFC 2461 5.3) */
481 case ND6_LLINFO_DELAY:
482 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD)) {
485 ln->ln_state = ND6_LLINFO_PROBE;
486 ln->ln_expire = time_uptime +
488 nd6_ns_output(ifp, &dst->sin6_addr,
492 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
493 ln->ln_expire = time_uptime + nd6_gctimer;
496 case ND6_LLINFO_PROBE:
497 if (ln->ln_asked < nd6_umaxtries) {
499 ln->ln_expire = time_uptime +
500 ND_IFINFO(ifp)->retrans / 1000;
501 nd6_ns_output(ifp, &dst->sin6_addr,
502 &dst->sin6_addr, ln, 0);
511 /* expire default router list */
512 dr = TAILQ_FIRST(&nd_defrouter);
514 if (dr->expire && dr->expire < time_uptime) {
515 struct nd_defrouter *t;
516 t = TAILQ_NEXT(dr, dr_entry);
520 dr = TAILQ_NEXT(dr, dr_entry);
525 * expire interface addresses.
526 * in the past the loop was inside prefix expiry processing.
527 * However, from a stricter speci-confrmance standpoint, we should
528 * rather separate address lifetimes and prefix lifetimes.
531 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
533 /* check address lifetime */
534 if (IFA6_IS_INVALID(ia6)) {
538 * If the expiring address is temporary, try
539 * regenerating a new one. This would be useful when
540 * we suspended a laptop PC, then turned it on after a
541 * period that could invalidate all temporary
542 * addresses. Although we may have to restart the
543 * loop (see below), it must be after purging the
544 * address. Otherwise, we'd see an infinite loop of
547 if (ip6_use_tempaddr &&
548 (ia6->ia6_flags & IN6_IFF_TEMPORARY)) {
549 if (regen_tmpaddr(ia6) == 0)
553 in6_purgeaddr(&ia6->ia_ifa);
556 goto addrloop; /* XXX: see below */
558 if (IFA6_IS_DEPRECATED(ia6)) {
559 int oldflags = ia6->ia6_flags;
561 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
564 * If a temporary address has just become deprecated,
565 * regenerate a new one if possible.
567 if (ip6_use_tempaddr &&
568 (ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
569 !(oldflags & IN6_IFF_DEPRECATED)) {
571 if (regen_tmpaddr(ia6) == 0) {
573 * A new temporary address is
575 * XXX: this means the address chain
576 * has changed while we are still in
577 * the loop. Although the change
578 * would not cause disaster (because
579 * it's not a deletion, but an
580 * addition,) we'd rather restart the
581 * loop just for safety. Or does this
582 * significantly reduce performance??
589 * A new RA might have made a deprecated address
592 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
596 /* expire prefix list */
597 pr = nd_prefix.lh_first;
600 * check prefix lifetime.
601 * since pltime is just for autoconf, pltime processing for
602 * prefix is not necessary.
604 if (pr->ndpr_expire && pr->ndpr_expire < time_uptime) {
609 * address expiration and prefix expiration are
610 * separate. NEVER perform in6_purgeaddr here.
618 mtx_unlock(&nd6_mtx);
622 regen_tmpaddr(struct in6_ifaddr *ia6) /* deprecated/invalidated temporary
625 struct ifaddr_container *ifac;
627 struct in6_ifaddr *public_ifa6 = NULL;
629 ifp = ia6->ia_ifa.ifa_ifp;
630 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
631 struct ifaddr *ifa = ifac->ifa;
632 struct in6_ifaddr *it6;
634 if (ifa->ifa_addr->sa_family != AF_INET6)
637 it6 = (struct in6_ifaddr *)ifa;
639 /* ignore no autoconf addresses. */
640 if (!(it6->ia6_flags & IN6_IFF_AUTOCONF))
643 /* ignore autoconf addresses with different prefixes. */
644 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
648 * Now we are looking at an autoconf address with the same
649 * prefix as ours. If the address is temporary and is still
650 * preferred, do not create another one. It would be rare, but
651 * could happen, for example, when we resume a laptop PC after
654 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) &&
655 !IFA6_IS_DEPRECATED(it6)) {
661 * This is a public autoconf address that has the same prefix
662 * as ours. If it is preferred, keep it. We can't break the
663 * loop here, because there may be a still-preferred temporary
664 * address with the prefix.
666 if (!IFA6_IS_DEPRECATED(it6))
670 if (public_ifa6 != NULL) {
673 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
674 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
675 " tmp addr,errno=%d\n", e);
685 * Nuke neighbor cache/prefix/default router management table, right before
689 nd6_purge(struct ifnet *ifp)
691 struct llinfo_nd6 *ln, *nln;
692 struct nd_defrouter *dr, *ndr, drany;
693 struct nd_prefix *pr, *npr;
695 /* Nuke default router list entries toward ifp */
696 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
698 * The first entry of the list may be stored in
699 * the routing table, so we'll delete it later.
701 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
702 ndr = TAILQ_NEXT(dr, dr_entry);
706 dr = TAILQ_FIRST(&nd_defrouter);
711 /* Nuke prefix list entries toward ifp */
712 for (pr = nd_prefix.lh_first; pr; pr = npr) {
714 if (pr->ndpr_ifp == ifp) {
716 * Previously, pr->ndpr_addr is removed as well,
717 * but I strongly believe we don't have to do it.
718 * nd6_purge() is only called from in6_ifdetach(),
719 * which removes all the associated interface addresses
721 * (jinmei@kame.net 20010129)
727 /* cancel default outgoing interface setting */
728 if (nd6_defifindex == ifp->if_index)
729 nd6_setdefaultiface(0);
731 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
732 /* refresh default router list */
733 bzero(&drany, sizeof(drany));
734 defrouter_delreq(&drany, 0);
739 * Nuke neighbor cache entries for the ifp.
740 * Note that rt->rt_ifp may not be the same as ifp,
741 * due to KAME goto ours hack. See RTM_RESOLVE case in
742 * nd6_rtrequest(), and ip6_input().
744 ln = llinfo_nd6.ln_next;
745 while (ln && ln != &llinfo_nd6) {
747 struct sockaddr_dl *sdl;
751 if (rt && rt->rt_gateway &&
752 rt->rt_gateway->sa_family == AF_LINK) {
753 sdl = (struct sockaddr_dl *)rt->rt_gateway;
754 if (sdl->sdl_index == ifp->if_index)
762 nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp)
765 struct sockaddr_in6 sin6;
767 bzero(&sin6, sizeof(sin6));
768 sin6.sin6_len = sizeof(struct sockaddr_in6);
769 sin6.sin6_family = AF_INET6;
770 sin6.sin6_addr = *addr6;
773 rt = rtlookup((struct sockaddr *)&sin6);
775 rt = rtpurelookup((struct sockaddr *)&sin6);
776 if (rt && !(rt->rt_flags & RTF_LLINFO)) {
778 * This is the case for the default route.
779 * If we want to create a neighbor cache for the address, we
780 * should free the route for the destination and allocate an
793 * If no route is available and create is set,
794 * we allocate a host route for the destination
795 * and treat it like an interface route.
796 * This hack is necessary for a neighbor which can't
797 * be covered by our own prefix.
800 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
805 * Create a new route. RTF_LLINFO is necessary
806 * to create a Neighbor Cache entry for the
807 * destination in nd6_rtrequest which will be
808 * called in rtrequest via ifa->ifa_rtrequest.
810 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
812 (struct sockaddr *)&all1_sa,
814 RTF_HOST | RTF_LLINFO) &
818 "nd6_lookup: failed to add route for a "
819 "neighbor(%s), errno=%d\n",
820 ip6_sprintf(addr6), e);
824 struct llinfo_nd6 *ln =
825 (struct llinfo_nd6 *)rt->rt_llinfo;
826 ln->ln_state = ND6_LLINFO_NOSTATE;
833 * Validation for the entry.
834 * Note that the check for rt_llinfo is necessary because a cloned
835 * route from a parent route that has the L flag (e.g. the default
836 * route to a p2p interface) may have the flag, too, while the
837 * destination is not actually a neighbor.
838 * XXX: we can't use rt->rt_ifp to check for the interface, since
839 * it might be the loopback interface if the entry is for our
840 * own address on a non-loopback interface. Instead, we should
841 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
844 if ((rt->rt_flags & RTF_GATEWAY) || !(rt->rt_flags & RTF_LLINFO) ||
845 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
846 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
848 log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n",
849 ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
850 /* xxx more logs... kazu */
858 * Detect if a given IPv6 address identifies a neighbor on a given link.
859 * XXX: should take care of the destination of a p2p link?
862 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
864 struct ifaddr_container *ifac;
867 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
868 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
871 * A link-local address is always a neighbor.
872 * XXX: we should use the sin6_scope_id field rather than the embedded
875 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
876 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
880 * If the address matches one of our addresses,
881 * it should be a neighbor.
883 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
884 struct ifaddr *ifa = ifac->ifa;
886 if (ifa->ifa_addr->sa_family != AF_INET6)
889 for (i = 0; i < 4; i++) {
890 if ((IFADDR6(ifa).s6_addr32[i] ^
891 addr->sin6_addr.s6_addr32[i]) &
892 IFMASK6(ifa).s6_addr32[i])
899 * Even if the address matches none of our addresses, it might be
900 * in the neighbor cache.
902 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
911 * Free an nd6 llinfo entry.
914 nd6_free(struct rtentry *rt)
916 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
917 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
918 struct nd_defrouter *dr;
921 * we used to have kpfctlinput(PRC_HOSTDEAD) here.
922 * even though it is not harmful, it was not really necessary.
925 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
927 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
930 if (ln->ln_router || dr) {
932 * rt6_flush must be called whether or not the neighbor
933 * is in the Default Router List.
934 * See a corresponding comment in nd6_na_input().
936 rt6_flush(&in6, rt->rt_ifp);
941 * Unreachablity of a router might affect the default
942 * router selection and on-link detection of advertised
947 * Temporarily fake the state to choose a new default
948 * router and to perform on-link determination of
949 * prefixes correctly.
950 * Below the state will be set correctly,
951 * or the entry itself will be deleted.
953 ln->ln_state = ND6_LLINFO_INCOMPLETE;
956 * Since defrouter_select() does not affect the
957 * on-link determination and MIP6 needs the check
958 * before the default router selection, we perform
961 pfxlist_onlink_check();
963 if (dr == TAILQ_FIRST(&nd_defrouter)) {
965 * It is used as the current default router,
966 * so we have to move it to the end of the
967 * list and choose a new one.
968 * XXX: it is not very efficient if this is
971 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
972 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
977 mtx_unlock(&nd6_mtx);
981 * Before deleting the entry, remember the next entry as the
982 * return value. We need this because pfxlist_onlink_check() above
983 * might have freed other entries (particularly the old next entry) as
984 * a side effect (XXX).
989 * Detach the route from the routing tree and the list of neighbor
990 * caches, and disable the route entry not to be used in already
993 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
999 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1001 * XXX cost-effective metods?
1004 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1006 struct llinfo_nd6 *ln;
1009 * If the caller specified "rt", use that. Otherwise, resolve the
1010 * routing table by supplied "dst6".
1015 if (!(rt = nd6_lookup(dst6, 0, NULL)))
1019 if ((rt->rt_flags & RTF_GATEWAY) ||
1020 !(rt->rt_flags & RTF_LLINFO) ||
1021 rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
1022 rt->rt_gateway->sa_family != AF_LINK) {
1023 /* This is not a host route. */
1027 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1028 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1032 * if we get upper-layer reachability confirmation many times,
1033 * it is possible we have false information.
1037 if (ln->ln_byhint > nd6_maxnudhint)
1041 ln->ln_state = ND6_LLINFO_REACHABLE;
1043 ln->ln_expire = time_uptime +
1044 ND_IFINFO(rt->rt_ifp)->reachable;
1048 nd6_rtrequest(int req, struct rtentry *rt)
1050 struct sockaddr *gate = rt->rt_gateway;
1051 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1052 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1053 struct ifnet *ifp = rt->rt_ifp;
1056 if ((rt->rt_flags & RTF_GATEWAY))
1059 if (nd6_need_cache(ifp) == 0 && !(rt->rt_flags & RTF_HOST)) {
1061 * This is probably an interface direct route for a link
1062 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1063 * We do not need special treatment below for such a route.
1064 * Moreover, the RTF_LLINFO flag which would be set below
1065 * would annoy the ndp(8) command.
1070 if (req == RTM_RESOLVE &&
1071 (nd6_need_cache(ifp) == 0 || /* stf case */
1072 !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp))) {
1074 * FreeBSD and BSD/OS often make a cloned host route based
1075 * on a less-specific route (e.g. the default route).
1076 * If the less specific route does not have a "gateway"
1077 * (this is the case when the route just goes to a p2p or an
1078 * stf interface), we'll mistakenly make a neighbor cache for
1079 * the host route, and will see strange neighbor solicitation
1080 * for the corresponding destination. In order to avoid the
1081 * confusion, we check if the destination of the route is
1082 * a neighbor in terms of neighbor discovery, and stop the
1083 * process if not. Additionally, we remove the LLINFO flag
1084 * so that ndp(8) will not try to get the neighbor information
1085 * of the destination.
1087 rt->rt_flags &= ~RTF_LLINFO;
1094 * There is no backward compatibility :)
1096 * if (!(rt->rt_flags & RTF_HOST) &&
1097 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1098 * rt->rt_flags |= RTF_CLONING;
1100 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1102 * Case 1: This route should come from
1103 * a route to interface. RTF_LLINFO flag is set
1104 * for a host route whose destination should be
1105 * treated as on-link.
1107 rt_setgate(rt, rt_key(rt),
1108 (struct sockaddr *)&null_sdl,
1110 gate = rt->rt_gateway;
1111 SDL(gate)->sdl_type = ifp->if_type;
1112 SDL(gate)->sdl_index = ifp->if_index;
1114 ln->ln_expire = time_uptime;
1116 if (ln && ln->ln_expire == 0) {
1117 /* kludge for desktops */
1119 kprintf("nd6_rtequest: time.tv_sec is zero; "
1125 if ((rt->rt_flags & RTF_CLONING))
1129 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1130 * We don't do that here since llinfo is not ready yet.
1132 * There are also couple of other things to be discussed:
1133 * - unsolicited NA code needs improvement beforehand
1134 * - RFC2461 says we MAY send multicast unsolicited NA
1135 * (7.2.6 paragraph 4), however, it also says that we
1136 * SHOULD provide a mechanism to prevent multicast NA storm.
1137 * we don't have anything like it right now.
1138 * note that the mechanism needs a mutual agreement
1139 * between proxies, which means that we need to implement
1140 * a new protocol, or a new kludge.
1141 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1142 * we need to check ip6forwarding before sending it.
1143 * (or should we allow proxy ND configuration only for
1144 * routers? there's no mention about proxy ND from hosts)
1147 /* XXX it does not work */
1148 if (rt->rt_flags & RTF_ANNOUNCE)
1150 &SIN6(rt_key(rt))->sin6_addr,
1151 &SIN6(rt_key(rt))->sin6_addr,
1152 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1157 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1159 * Address resolution isn't necessary for a point to
1160 * point link, so we can skip this test for a p2p link.
1162 if (gate->sa_family != AF_LINK ||
1163 gate->sa_len < sizeof(null_sdl)) {
1165 "nd6_rtrequest: bad gateway value: %s\n",
1169 SDL(gate)->sdl_type = ifp->if_type;
1170 SDL(gate)->sdl_index = ifp->if_index;
1173 break; /* This happens on a route change */
1175 * Case 2: This route may come from cloning, or a manual route
1176 * add with a LL address.
1178 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1179 rt->rt_llinfo = (caddr_t)ln;
1181 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1186 bzero(ln, sizeof(*ln));
1188 /* this is required for "ndp" command. - shin */
1189 if (req == RTM_ADD) {
1191 * gate should have some valid AF_LINK entry,
1192 * and ln->ln_expire should have some lifetime
1193 * which is specified by ndp command.
1195 ln->ln_state = ND6_LLINFO_REACHABLE;
1199 * When req == RTM_RESOLVE, rt is created and
1200 * initialized in rtrequest(), so rt_expire is 0.
1202 ln->ln_state = ND6_LLINFO_NOSTATE;
1203 ln->ln_expire = time_uptime;
1205 rt->rt_flags |= RTF_LLINFO;
1206 ln->ln_next = llinfo_nd6.ln_next;
1207 llinfo_nd6.ln_next = ln;
1208 ln->ln_prev = &llinfo_nd6;
1209 ln->ln_next->ln_prev = ln;
1212 * check if rt_key(rt) is one of my address assigned
1215 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1216 &SIN6(rt_key(rt))->sin6_addr);
1218 caddr_t macp = nd6_ifptomac(ifp);
1220 ln->ln_state = ND6_LLINFO_REACHABLE;
1223 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1224 SDL(gate)->sdl_alen = ifp->if_addrlen;
1226 if (nd6_useloopback) {
1227 rt->rt_ifp = &loif[0]; /* XXX */
1229 * Make sure rt_ifa be equal to the ifaddr
1230 * corresponding to the address.
1231 * We need this because when we refer
1232 * rt_ifa->ia6_flags in ip6_input, we assume
1233 * that the rt_ifa points to the address instead
1234 * of the loopback address.
1236 if (ifa != rt->rt_ifa) {
1237 IFAFREE(rt->rt_ifa);
1242 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1244 ln->ln_state = ND6_LLINFO_REACHABLE;
1247 /* join solicited node multicast for proxy ND */
1248 if (ifp->if_flags & IFF_MULTICAST) {
1249 struct in6_addr llsol;
1252 llsol = SIN6(rt_key(rt))->sin6_addr;
1253 llsol.s6_addr16[0] = htons(0xff02);
1254 llsol.s6_addr16[1] = htons(ifp->if_index);
1255 llsol.s6_addr32[1] = 0;
1256 llsol.s6_addr32[2] = htonl(1);
1257 llsol.s6_addr8[12] = 0xff;
1259 if (!in6_addmulti(&llsol, ifp, &error)) {
1260 nd6log((LOG_ERR, "%s: failed to join "
1261 "%s (errno=%d)\n", if_name(ifp),
1262 ip6_sprintf(&llsol), error));
1271 /* leave from solicited node multicast for proxy ND */
1272 if ((rt->rt_flags & RTF_ANNOUNCE) &&
1273 (ifp->if_flags & IFF_MULTICAST)) {
1274 struct in6_addr llsol;
1275 struct in6_multi *in6m;
1277 llsol = SIN6(rt_key(rt))->sin6_addr;
1278 llsol.s6_addr16[0] = htons(0xff02);
1279 llsol.s6_addr16[1] = htons(ifp->if_index);
1280 llsol.s6_addr32[1] = 0;
1281 llsol.s6_addr32[2] = htonl(1);
1282 llsol.s6_addr8[12] = 0xff;
1284 in6m = IN6_LOOKUP_MULTI(&llsol, ifp);
1289 ln->ln_next->ln_prev = ln->ln_prev;
1290 ln->ln_prev->ln_next = ln->ln_next;
1293 rt->rt_flags &= ~RTF_LLINFO;
1295 m_freem(ln->ln_hold);
1301 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1303 struct in6_drlist *drl = (struct in6_drlist *)data;
1304 struct in6_prlist *prl = (struct in6_prlist *)data;
1305 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1306 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1307 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1308 struct nd_defrouter *dr, any;
1309 struct nd_prefix *pr;
1311 int i = 0, error = 0;
1314 case SIOCGDRLST_IN6:
1316 * obsolete API, use sysctl under net.inet6.icmp6
1318 bzero(drl, sizeof(*drl));
1320 dr = TAILQ_FIRST(&nd_defrouter);
1321 while (dr && i < DRLSTSIZ) {
1322 drl->defrouter[i].rtaddr = dr->rtaddr;
1323 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1324 /* XXX: need to this hack for KAME stack */
1325 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1328 "default router list contains a "
1329 "non-linklocal address(%s)\n",
1330 ip6_sprintf(&drl->defrouter[i].rtaddr));
1332 drl->defrouter[i].flags = dr->flags;
1333 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1334 drl->defrouter[i].expire = dr->expire;
1335 drl->defrouter[i].if_index = dr->ifp->if_index;
1337 dr = TAILQ_NEXT(dr, dr_entry);
1339 mtx_unlock(&nd6_mtx);
1341 case SIOCGPRLST_IN6:
1343 * obsolete API, use sysctl under net.inet6.icmp6
1346 * XXX meaning of fields, especialy "raflags", is very
1347 * differnet between RA prefix list and RR/static prefix list.
1348 * how about separating ioctls into two?
1350 bzero(prl, sizeof(*prl));
1352 pr = nd_prefix.lh_first;
1353 while (pr && i < PRLSTSIZ) {
1354 struct nd_pfxrouter *pfr;
1357 in6_embedscope(&prl->prefix[i].prefix,
1358 &pr->ndpr_prefix, NULL, NULL);
1359 prl->prefix[i].raflags = pr->ndpr_raf;
1360 prl->prefix[i].prefixlen = pr->ndpr_plen;
1361 prl->prefix[i].vltime = pr->ndpr_vltime;
1362 prl->prefix[i].pltime = pr->ndpr_pltime;
1363 prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1364 prl->prefix[i].expire = pr->ndpr_expire;
1366 pfr = pr->ndpr_advrtrs.lh_first;
1370 #define RTRADDR prl->prefix[i].advrtr[j]
1371 RTRADDR = pfr->router->rtaddr;
1372 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1373 /* XXX: hack for KAME */
1374 RTRADDR.s6_addr16[1] = 0;
1377 "a router(%s) advertises "
1379 "non-link local address\n",
1380 ip6_sprintf(&RTRADDR));
1384 pfr = pfr->pfr_next;
1386 prl->prefix[i].advrtrs = j;
1387 prl->prefix[i].origin = PR_ORIG_RA;
1392 mtx_unlock(&nd6_mtx);
1395 case OSIOCGIFINFO_IN6:
1396 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1397 bzero(&ndi->ndi, sizeof(ndi->ndi));
1398 ndi->ndi.linkmtu = ND_IFINFO(ifp)->linkmtu;
1399 ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1400 ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1401 ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1402 ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1403 ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1404 ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1405 ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1406 ndi->ndi.receivedra = ND_IFINFO(ifp)->receivedra;
1408 case SIOCGIFINFO_IN6:
1409 ndi->ndi = *ND_IFINFO(ifp);
1411 case SIOCSIFINFO_FLAGS:
1412 ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1414 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1415 /* flush default router list */
1417 * xxx sumikawa: should not delete route if default
1418 * route equals to the top of default router list
1420 bzero(&any, sizeof(any));
1421 defrouter_delreq(&any, 0);
1423 /* xxx sumikawa: flush prefix list */
1425 case SIOCSPFXFLUSH_IN6:
1427 /* flush all the prefix advertised by routers */
1428 struct nd_prefix *pr, *next;
1431 for (pr = nd_prefix.lh_first; pr; pr = next) {
1432 struct in6_ifaddr *ia, *ia_next;
1434 next = pr->ndpr_next;
1436 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1439 /* do we really have to remove addresses as well? */
1440 for (ia = in6_ifaddr; ia; ia = ia_next) {
1441 /* ia might be removed. keep the next ptr. */
1442 ia_next = ia->ia_next;
1444 if (!(ia->ia6_flags & IN6_IFF_AUTOCONF))
1447 if (ia->ia6_ndpr == pr)
1448 in6_purgeaddr(&ia->ia_ifa);
1452 mtx_unlock(&nd6_mtx);
1455 case SIOCSRTRFLUSH_IN6:
1457 /* flush all the default routers */
1458 struct nd_defrouter *dr, *next;
1461 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1463 * The first entry of the list may be stored in
1464 * the routing table, so we'll delete it later.
1466 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1467 next = TAILQ_NEXT(dr, dr_entry);
1470 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1472 mtx_unlock(&nd6_mtx);
1475 case SIOCGNBRINFO_IN6:
1477 struct llinfo_nd6 *ln;
1478 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1481 * XXX: KAME specific hack for scoped addresses
1482 * XXXX: for other scopes than link-local?
1484 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1485 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1486 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1489 *idp = htons(ifp->if_index);
1493 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1495 mtx_unlock(&nd6_mtx);
1498 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1499 nbi->state = ln->ln_state;
1500 nbi->asked = ln->ln_asked;
1501 nbi->isrouter = ln->ln_router;
1502 nbi->expire = ln->ln_expire;
1503 mtx_unlock(&nd6_mtx);
1507 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1508 ndif->ifindex = nd6_defifindex;
1510 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1511 return (nd6_setdefaultiface(ndif->ifindex));
1518 * Create neighbor cache entry and cache link-layer address,
1519 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1522 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1524 int type, /* ICMP6 type */
1525 int code /* type dependent information */)
1527 struct rtentry *rt = NULL;
1528 struct llinfo_nd6 *ln = NULL;
1530 struct sockaddr_dl *sdl = NULL;
1537 panic("ifp == NULL in nd6_cache_lladdr");
1539 panic("from == NULL in nd6_cache_lladdr");
1541 /* nothing must be updated for unspecified address */
1542 if (IN6_IS_ADDR_UNSPECIFIED(from))
1546 * Validation about ifp->if_addrlen and lladdrlen must be done in
1549 * XXX If the link does not have link-layer adderss, what should
1550 * we do? (ifp->if_addrlen == 0)
1551 * Spec says nothing in sections for RA, RS and NA. There's small
1552 * description on it in NS section (RFC 2461 7.2.3).
1555 rt = nd6_lookup(from, 0, ifp);
1558 /* nothing must be done if there's no lladdr */
1559 if (!lladdr || !lladdrlen)
1563 rt = nd6_lookup(from, 1, ifp);
1566 /* do nothing if static ndp is set */
1567 if (rt->rt_flags & RTF_STATIC)
1574 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1579 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1582 if (!rt->rt_gateway)
1584 if (rt->rt_gateway->sa_family != AF_LINK)
1586 sdl = SDL(rt->rt_gateway);
1588 olladdr = (sdl->sdl_alen) ? 1 : 0;
1589 if (olladdr && lladdr) {
1590 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1598 * newentry olladdr lladdr llchange (*=record)
1601 * 0 n y -- (3) * STALE
1603 * 0 y y y (5) * STALE
1604 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1605 * 1 -- y -- (7) * STALE
1608 if (lladdr) { /* (3-5) and (7) */
1610 * Record source link-layer address
1611 * XXX is it dependent to ifp->if_type?
1613 sdl->sdl_alen = ifp->if_addrlen;
1614 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1618 if ((!olladdr && lladdr) /* (3) */
1619 || (olladdr && lladdr && llchange)) { /* (5) */
1621 newstate = ND6_LLINFO_STALE;
1622 } else /* (1-2,4) */
1626 if (!lladdr) /* (6) */
1627 newstate = ND6_LLINFO_NOSTATE;
1629 newstate = ND6_LLINFO_STALE;
1634 * Update the state of the neighbor cache.
1636 ln->ln_state = newstate;
1638 if (ln->ln_state == ND6_LLINFO_STALE) {
1640 * XXX: since nd6_output() below will cause
1641 * state tansition to DELAY and reset the timer,
1642 * we must set the timer now, although it is actually
1645 ln->ln_expire = time_uptime + nd6_gctimer;
1649 * we assume ifp is not a p2p here, so just
1650 * set the 2nd argument as the 1st one.
1652 nd6_output(ifp, ifp, ln->ln_hold,
1653 (struct sockaddr_in6 *)rt_key(rt),
1657 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1658 /* probe right away */
1659 ln->ln_expire = time_uptime;
1664 * ICMP6 type dependent behavior.
1666 * NS: clear IsRouter if new entry
1667 * RS: clear IsRouter
1668 * RA: set IsRouter if there's lladdr
1669 * redir: clear IsRouter if new entry
1672 * The spec says that we must set IsRouter in the following cases:
1673 * - If lladdr exist, set IsRouter. This means (1-5).
1674 * - If it is old entry (!newentry), set IsRouter. This means (7).
1675 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1676 * A quetion arises for (1) case. (1) case has no lladdr in the
1677 * neighbor cache, this is similar to (6).
1678 * This case is rare but we figured that we MUST NOT set IsRouter.
1680 * newentry olladdr lladdr llchange NS RS RA redir
1682 * 0 n n -- (1) c ? s
1683 * 0 y n -- (2) c s s
1684 * 0 n y -- (3) c s s
1687 * 1 -- n -- (6) c c c s
1688 * 1 -- y -- (7) c c s c s
1692 switch (type & 0xff) {
1693 case ND_NEIGHBOR_SOLICIT:
1695 * New entry must have is_router flag cleared.
1697 if (is_newentry) /* (6-7) */
1702 * If the icmp is a redirect to a better router, always set the
1703 * is_router flag. Otherwise, if the entry is newly created,
1704 * clear the flag. [RFC 2461, sec 8.3]
1706 if (code == ND_REDIRECT_ROUTER)
1708 else if (is_newentry) /* (6-7) */
1711 case ND_ROUTER_SOLICIT:
1713 * is_router flag must always be cleared.
1717 case ND_ROUTER_ADVERT:
1719 * Mark an entry with lladdr as a router.
1721 if ((!is_newentry && (olladdr || lladdr)) /* (2-5) */
1722 || (is_newentry && lladdr)) { /* (7) */
1729 * When the link-layer address of a router changes, select the
1730 * best router again. In particular, when the neighbor entry is newly
1731 * created, it might affect the selection policy.
1732 * Question: can we restrict the first condition to the "is_newentry"
1734 * XXX: when we hear an RA from a new router with the link-layer
1735 * address option, defrouter_select() is called twice, since
1736 * defrtrlist_update called the function as well. However, I believe
1737 * we can compromise the overhead, since it only happens the first
1739 * XXX: although defrouter_select() should not have a bad effect
1740 * for those are not autoconfigured hosts, we explicitly avoid such
1743 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1750 nd6_slowtimo(void *ignored_arg)
1752 struct nd_ifinfo *nd6if;
1756 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1757 nd6_slowtimo, NULL);
1758 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1759 if (ifp->if_afdata[AF_INET6] == NULL)
1761 nd6if = ND_IFINFO(ifp);
1762 if (nd6if->basereachable && /* already initialized */
1763 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1765 * Since reachable time rarely changes by router
1766 * advertisements, we SHOULD insure that a new random
1767 * value gets recomputed at least once every few hours.
1770 nd6if->recalctm = nd6_recalc_reachtm_interval;
1771 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1774 mtx_unlock(&nd6_mtx);
1777 #define gotoerr(e) { error = (e); goto bad;}
1780 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1781 struct sockaddr_in6 *dst, struct rtentry *rt)
1783 struct llinfo_nd6 *ln = NULL;
1786 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1789 if (nd6_need_cache(ifp) == 0)
1793 * next hop determination. This routine is derived from ether_outpout.
1796 if (!(rt->rt_flags & RTF_UP)) {
1797 rt = rtlookup((struct sockaddr *)dst);
1799 gotoerr(EHOSTUNREACH);
1801 if (rt->rt_ifp != ifp) {
1802 /* XXX: loop care? */
1803 return nd6_output(ifp, origifp, m, dst, rt);
1806 if (rt->rt_flags & RTF_GATEWAY) {
1807 struct sockaddr_in6 *gw6;
1810 * We skip link-layer address resolution and NUD
1811 * if the gateway is not a neighbor from ND point
1812 * of view, regardless of the value of nd_ifinfo.flags.
1813 * The second condition is a bit tricky; we skip
1814 * if the gateway is our own address, which is
1815 * sometimes used to install a route to a p2p link.
1817 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1818 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1819 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1821 * We allow this kind of tricky route only
1822 * when the outgoing interface is p2p.
1823 * XXX: we may need a more generic rule here.
1825 if (!(ifp->if_flags & IFF_POINTOPOINT))
1826 gotoerr(EHOSTUNREACH);
1831 if (rt->rt_gwroute == NULL) {
1832 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1833 if (rt->rt_gwroute == NULL)
1834 gotoerr(EHOSTUNREACH);
1835 } else if (!(rt->rt_gwroute->rt_flags & RTF_UP)) {
1836 rtfree(rt->rt_gwroute);
1837 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1838 if (rt->rt_gwroute == NULL)
1839 gotoerr(EHOSTUNREACH);
1845 * Address resolution or Neighbor Unreachability Detection
1847 * At this point, the destination of the packet must be a unicast
1848 * or an anycast address(i.e. not a multicast).
1851 /* Look up the neighbor cache for the nexthop */
1852 if (rt && (rt->rt_flags & RTF_LLINFO))
1853 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1856 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1857 * the condition below is not very efficient. But we believe
1858 * it is tolerable, because this should be a rare case.
1860 if (nd6_is_addr_neighbor(dst, ifp) &&
1861 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1862 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1865 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
1866 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1868 "nd6_output: can't allocate llinfo for %s "
1870 ip6_sprintf(&dst->sin6_addr), ln, rt);
1871 gotoerr(EIO); /* XXX: good error? */
1874 goto sendpkt; /* send anyway */
1877 /* We don't have to do link-layer address resolution on a p2p link. */
1878 if ((ifp->if_flags & IFF_POINTOPOINT) &&
1879 ln->ln_state < ND6_LLINFO_REACHABLE) {
1880 ln->ln_state = ND6_LLINFO_STALE;
1881 ln->ln_expire = time_uptime + nd6_gctimer;
1885 * The first time we send a packet to a neighbor whose entry is
1886 * STALE, we have to change the state to DELAY and a sets a timer to
1887 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1888 * neighbor unreachability detection on expiration.
1891 if (ln->ln_state == ND6_LLINFO_STALE) {
1893 ln->ln_state = ND6_LLINFO_DELAY;
1894 ln->ln_expire = time_uptime + nd6_delay;
1898 * If the neighbor cache entry has a state other than INCOMPLETE
1899 * (i.e. its link-layer address is already resolved), just
1902 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1906 * There is a neighbor cache entry, but no ethernet address
1907 * response yet. Replace the held mbuf (if any) with this
1910 * This code conforms to the rate-limiting rule described in Section
1911 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
1914 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1915 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1917 m_freem(ln->ln_hold);
1919 if (ln->ln_expire) {
1920 if (ln->ln_asked < nd6_mmaxtries &&
1921 ln->ln_expire < time_uptime) {
1923 ln->ln_expire = time_uptime +
1924 ND_IFINFO(ifp)->retrans / 1000;
1925 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1931 if (ifp->if_flags & IFF_LOOPBACK)
1932 error = ifp->if_output(origifp, m, (struct sockaddr *)dst, rt);
1934 error = ifp->if_output(ifp, m, (struct sockaddr *)dst, rt);
1944 nd6_need_cache(struct ifnet *ifp)
1947 * XXX: we currently do not make neighbor cache on any interface
1948 * other than Ethernet and GIF.
1951 * - unidirectional tunnels needs no ND
1953 switch (ifp->if_type) {
1959 #ifdef IFT_IEEE80211
1965 case IFT_GIF: /* XXX need more cases? */
1973 nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
1974 struct sockaddr *dst, u_char *desten)
1976 struct sockaddr_dl *sdl;
1980 if (m->m_flags & M_MCAST) {
1981 switch (ifp->if_type) {
1986 #ifdef IFT_IEEE80211
1989 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
1993 bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
2001 /* this could happen, if we could not allocate memory */
2005 if (rt_llroute(dst, rt0, &rt) != 0) {
2009 if (rt->rt_gateway->sa_family != AF_LINK) {
2010 kprintf("nd6_storelladdr: something odd happens\n");
2014 sdl = SDL(rt->rt_gateway);
2015 if (sdl->sdl_alen == 0) {
2016 /* this should be impossible, but we bark here for debugging */
2017 kprintf("nd6_storelladdr: sdl_alen == 0\n");
2022 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2026 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2027 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2029 SYSCTL_DECL(_net_inet6_icmp6);
2031 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2032 CTLFLAG_RD, nd6_sysctl_drlist, "List default routers");
2033 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2034 CTLFLAG_RD, nd6_sysctl_prlist, "List prefixes");
2037 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2041 struct in6_defrouter *d, *de;
2042 struct nd_defrouter *dr;
2048 for (dr = TAILQ_FIRST(&nd_defrouter);
2050 dr = TAILQ_NEXT(dr, dr_entry)) {
2051 d = (struct in6_defrouter *)buf;
2052 de = (struct in6_defrouter *)(buf + sizeof(buf));
2055 bzero(d, sizeof(*d));
2056 d->rtaddr.sin6_family = AF_INET6;
2057 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2058 if (in6_recoverscope(&d->rtaddr, &dr->rtaddr,
2062 "default router list (%s)\n",
2063 ip6_sprintf(&dr->rtaddr));
2064 d->flags = dr->flags;
2065 d->rtlifetime = dr->rtlifetime;
2066 d->expire = dr->expire;
2067 d->if_index = dr->ifp->if_index;
2069 panic("buffer too short");
2071 error = SYSCTL_OUT(req, buf, sizeof(*d));
2079 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2083 struct in6_prefix *p, *pe;
2084 struct nd_prefix *pr;
2090 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2093 struct sockaddr_in6 *sin6, *s6;
2094 struct nd_pfxrouter *pfr;
2096 p = (struct in6_prefix *)buf;
2097 pe = (struct in6_prefix *)(buf + sizeof(buf));
2100 bzero(p, sizeof(*p));
2101 sin6 = (struct sockaddr_in6 *)(p + 1);
2103 p->prefix = pr->ndpr_prefix;
2104 if (in6_recoverscope(&p->prefix,
2105 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2107 "scope error in prefix list (%s)\n",
2108 ip6_sprintf(&p->prefix.sin6_addr));
2109 p->raflags = pr->ndpr_raf;
2110 p->prefixlen = pr->ndpr_plen;
2111 p->vltime = pr->ndpr_vltime;
2112 p->pltime = pr->ndpr_pltime;
2113 p->if_index = pr->ndpr_ifp->if_index;
2114 p->expire = pr->ndpr_expire;
2115 p->refcnt = pr->ndpr_refcnt;
2116 p->flags = pr->ndpr_stateflags;
2117 p->origin = PR_ORIG_RA;
2119 for (pfr = pr->ndpr_advrtrs.lh_first;
2121 pfr = pfr->pfr_next) {
2122 if ((void *)&sin6[advrtrs + 1] >
2127 s6 = &sin6[advrtrs];
2128 bzero(s6, sizeof(*s6));
2129 s6->sin6_family = AF_INET6;
2130 s6->sin6_len = sizeof(*sin6);
2131 if (in6_recoverscope(s6, &pfr->router->rtaddr,
2132 pfr->router->ifp) != 0)
2135 "prefix list (%s)\n",
2136 ip6_sprintf(&pfr->router->rtaddr));
2139 p->advrtrs = advrtrs;
2141 panic("buffer too short");
2143 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2144 error = SYSCTL_OUT(req, buf, advance);