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
34 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/callout.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
46 #include <sys/kernel.h>
47 #include <sys/protosw.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/queue.h>
51 #include <sys/sysctl.h>
54 #include <net/if_arc.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57 #include <net/iso88025.h>
59 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/if_ether.h>
63 #include <netinet6/in6_var.h>
64 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/nd6.h>
67 #include <netinet/icmp6.h>
69 #include <net/net_osdep.h>
71 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
72 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
74 #define SIN6(s) ((struct sockaddr_in6 *)s)
75 #define SDL(s) ((struct sockaddr_dl *)s)
78 int nd6_prune = 1; /* walk list every 1 seconds */
79 int nd6_delay = 5; /* delay first probe time 5 second */
80 int nd6_umaxtries = 3; /* maximum unicast query */
81 int nd6_mmaxtries = 3; /* maximum multicast query */
82 int nd6_useloopback = 1; /* use loopback interface for local traffic */
83 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
85 /* preventing too many loops in ND option parsing */
86 int nd6_maxndopt = 10; /* max # of ND options allowed */
88 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
97 static int nd6_inuse, nd6_allocated;
99 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
100 struct nd_drhead nd_defrouter;
101 struct nd_prhead nd_prefix = { 0 };
103 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
104 static struct sockaddr_in6 all1_sa;
106 static int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *,
108 static void nd6_setmtu0 __P((struct ifnet *, struct nd_ifinfo *));
109 static void nd6_slowtimo __P((void *));
110 static int regen_tmpaddr __P((struct in6_ifaddr *));
112 struct callout nd6_slowtimo_ch;
113 struct callout nd6_timer_ch;
114 extern struct callout in6_tmpaddrtimer_ch;
119 static int nd6_init_done = 0;
123 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
127 all1_sa.sin6_family = AF_INET6;
128 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
129 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
130 all1_sa.sin6_addr.s6_addr[i] = 0xff;
132 /* initialization of the default router list */
133 TAILQ_INIT(&nd_defrouter);
138 callout_init(&nd6_slowtimo_ch, 0);
139 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
147 struct nd_ifinfo *nd;
149 nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
150 bzero(nd, sizeof(*nd));
154 nd->chlim = IPV6_DEFHLIM;
155 nd->basereachable = REACHABLE_TIME;
156 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
157 nd->retrans = RETRANS_TIMER;
159 * Note that the default value of ip6_accept_rtadv is 0, which means
160 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
163 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
165 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
166 nd6_setmtu0(ifp, nd);
173 struct nd_ifinfo *nd;
180 * Reset ND level link MTU. This function is called when the physical MTU
181 * changes, which means we might have to adjust the ND level MTU.
188 nd6_setmtu0(ifp, ND_IFINFO(ifp));
191 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
193 nd6_setmtu0(ifp, ndi)
195 struct nd_ifinfo *ndi;
199 omaxmtu = ndi->maxmtu;
201 switch (ifp->if_type) {
203 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
206 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
209 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
212 ndi->maxmtu = ifp->if_mtu;
217 * Decreasing the interface MTU under IPV6 minimum MTU may cause
218 * undesirable situation. We thus notify the operator of the change
219 * explicitly. The check for omaxmtu is necessary to restrict the
220 * log to the case of changing the MTU, not initializing it.
222 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
223 log(LOG_NOTICE, "nd6_setmtu0: "
224 "new link MTU on %s (%lu) is too small for IPv6\n",
225 if_name(ifp), (unsigned long)ndi->maxmtu);
228 if (ndi->maxmtu > in6_maxmtu)
229 in6_setmaxmtu(); /* check all interfaces just in case */
235 nd6_option_init(opt, icmp6len, ndopts)
238 union nd_opts *ndopts;
241 bzero(ndopts, sizeof(*ndopts));
242 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
244 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
247 ndopts->nd_opts_done = 1;
248 ndopts->nd_opts_search = NULL;
253 * Take one ND option.
257 union nd_opts *ndopts;
259 struct nd_opt_hdr *nd_opt;
263 panic("ndopts == NULL in nd6_option");
264 if (!ndopts->nd_opts_last)
265 panic("uninitialized ndopts in nd6_option");
266 if (!ndopts->nd_opts_search)
268 if (ndopts->nd_opts_done)
271 nd_opt = ndopts->nd_opts_search;
273 /* make sure nd_opt_len is inside the buffer */
274 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
275 bzero(ndopts, sizeof(*ndopts));
279 olen = nd_opt->nd_opt_len << 3;
282 * Message validation requires that all included
283 * options have a length that is greater than zero.
285 bzero(ndopts, sizeof(*ndopts));
289 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
290 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
291 /* option overruns the end of buffer, invalid */
292 bzero(ndopts, sizeof(*ndopts));
294 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
295 /* reached the end of options chain */
296 ndopts->nd_opts_done = 1;
297 ndopts->nd_opts_search = NULL;
303 * Parse multiple ND options.
304 * This function is much easier to use, for ND routines that do not need
305 * multiple options of the same type.
309 union nd_opts *ndopts;
311 struct nd_opt_hdr *nd_opt;
315 panic("ndopts == NULL in nd6_options");
316 if (!ndopts->nd_opts_last)
317 panic("uninitialized ndopts in nd6_options");
318 if (!ndopts->nd_opts_search)
322 nd_opt = nd6_option(ndopts);
323 if (!nd_opt && !ndopts->nd_opts_last) {
325 * Message validation requires that all included
326 * options have a length that is greater than zero.
328 icmp6stat.icp6s_nd_badopt++;
329 bzero(ndopts, sizeof(*ndopts));
336 switch (nd_opt->nd_opt_type) {
337 case ND_OPT_SOURCE_LINKADDR:
338 case ND_OPT_TARGET_LINKADDR:
340 case ND_OPT_REDIRECTED_HEADER:
341 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
343 "duplicated ND6 option found (type=%d)\n",
344 nd_opt->nd_opt_type));
347 ndopts->nd_opt_array[nd_opt->nd_opt_type]
351 case ND_OPT_PREFIX_INFORMATION:
352 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
353 ndopts->nd_opt_array[nd_opt->nd_opt_type]
356 ndopts->nd_opts_pi_end =
357 (struct nd_opt_prefix_info *)nd_opt;
361 * Unknown options must be silently ignored,
362 * to accomodate future extension to the protocol.
365 "nd6_options: unsupported option %d - "
366 "option ignored\n", nd_opt->nd_opt_type));
371 if (i > nd6_maxndopt) {
372 icmp6stat.icp6s_nd_toomanyopt++;
373 nd6log((LOG_INFO, "too many loop in nd opt\n"));
377 if (ndopts->nd_opts_done)
385 * ND6 timer routine to expire default route list and prefix list
388 nd6_timer(ignored_arg)
392 struct llinfo_nd6 *ln;
393 struct nd_defrouter *dr;
394 struct nd_prefix *pr;
396 struct in6_ifaddr *ia6, *nia6;
397 struct in6_addrlifetime *lt6;
400 callout_reset(&nd6_timer_ch, nd6_prune * hz,
403 ln = llinfo_nd6.ln_next;
404 while (ln && ln != &llinfo_nd6) {
406 struct sockaddr_in6 *dst;
407 struct llinfo_nd6 *next = ln->ln_next;
408 /* XXX: used for the DELAY case only: */
409 struct nd_ifinfo *ndi = NULL;
411 if ((rt = ln->ln_rt) == NULL) {
415 if ((ifp = rt->rt_ifp) == NULL) {
419 ndi = ND_IFINFO(ifp);
420 dst = (struct sockaddr_in6 *)rt_key(rt);
422 if (ln->ln_expire > time_second) {
429 panic("rt=0 in nd6_timer(ln=%p)", ln);
430 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
431 panic("rt_llinfo(%p) is not equal to ln(%p)",
434 panic("dst=0 in nd6_timer(ln=%p)", ln);
436 switch (ln->ln_state) {
437 case ND6_LLINFO_INCOMPLETE:
438 if (ln->ln_asked < nd6_mmaxtries) {
440 ln->ln_expire = time_second +
441 ND_IFINFO(ifp)->retrans / 1000;
442 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
445 struct mbuf *m = ln->ln_hold;
449 * Fake rcvif to make ICMP error
450 * more helpful in diagnosing
452 * XXX: should we consider
455 m->m_pkthdr.rcvif = rt->rt_ifp;
457 icmp6_error(m, ICMP6_DST_UNREACH,
458 ICMP6_DST_UNREACH_ADDR, 0);
464 case ND6_LLINFO_REACHABLE:
466 ln->ln_state = ND6_LLINFO_STALE;
467 ln->ln_expire = time_second + nd6_gctimer;
471 case ND6_LLINFO_STALE:
472 /* Garbage Collection(RFC 2461 5.3) */
477 case ND6_LLINFO_DELAY:
478 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
481 ln->ln_state = ND6_LLINFO_PROBE;
482 ln->ln_expire = time_second +
484 nd6_ns_output(ifp, &dst->sin6_addr,
488 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
489 ln->ln_expire = time_second + nd6_gctimer;
492 case ND6_LLINFO_PROBE:
493 if (ln->ln_asked < nd6_umaxtries) {
495 ln->ln_expire = time_second +
496 ND_IFINFO(ifp)->retrans / 1000;
497 nd6_ns_output(ifp, &dst->sin6_addr,
498 &dst->sin6_addr, ln, 0);
507 /* expire default router list */
508 dr = TAILQ_FIRST(&nd_defrouter);
510 if (dr->expire && dr->expire < time_second) {
511 struct nd_defrouter *t;
512 t = TAILQ_NEXT(dr, dr_entry);
516 dr = TAILQ_NEXT(dr, dr_entry);
521 * expire interface addresses.
522 * in the past the loop was inside prefix expiry processing.
523 * However, from a stricter speci-confrmance standpoint, we should
524 * rather separate address lifetimes and prefix lifetimes.
527 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
529 /* check address lifetime */
530 lt6 = &ia6->ia6_lifetime;
531 if (IFA6_IS_INVALID(ia6)) {
535 * If the expiring address is temporary, try
536 * regenerating a new one. This would be useful when
537 * we suspended a laptop PC, then turned it on after a
538 * period that could invalidate all temporary
539 * addresses. Although we may have to restart the
540 * loop (see below), it must be after purging the
541 * address. Otherwise, we'd see an infinite loop of
544 if (ip6_use_tempaddr &&
545 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
546 if (regen_tmpaddr(ia6) == 0)
550 in6_purgeaddr(&ia6->ia_ifa);
553 goto addrloop; /* XXX: see below */
555 if (IFA6_IS_DEPRECATED(ia6)) {
556 int oldflags = ia6->ia6_flags;
558 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
561 * If a temporary address has just become deprecated,
562 * regenerate a new one if possible.
564 if (ip6_use_tempaddr &&
565 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
566 (oldflags & IN6_IFF_DEPRECATED) == 0) {
568 if (regen_tmpaddr(ia6) == 0) {
570 * A new temporary address is
572 * XXX: this means the address chain
573 * has changed while we are still in
574 * the loop. Although the change
575 * would not cause disaster (because
576 * it's not a deletion, but an
577 * addition,) we'd rather restart the
578 * loop just for safety. Or does this
579 * significantly reduce performance??
586 * A new RA might have made a deprecated address
589 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
593 /* expire prefix list */
594 pr = nd_prefix.lh_first;
597 * check prefix lifetime.
598 * since pltime is just for autoconf, pltime processing for
599 * prefix is not necessary.
601 if (pr->ndpr_expire && pr->ndpr_expire < time_second) {
606 * address expiration and prefix expiration are
607 * separate. NEVER perform in6_purgeaddr here.
620 struct in6_ifaddr *ia6; /* deprecated/invalidated temporary address */
624 struct in6_ifaddr *public_ifa6 = NULL;
626 ifp = ia6->ia_ifa.ifa_ifp;
627 for (ifa = ifp->if_addrlist.tqh_first; ifa;
628 ifa = ifa->ifa_list.tqe_next) {
629 struct in6_ifaddr *it6;
631 if (ifa->ifa_addr->sa_family != AF_INET6)
634 it6 = (struct in6_ifaddr *)ifa;
636 /* ignore no autoconf addresses. */
637 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
640 /* ignore autoconf addresses with different prefixes. */
641 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
645 * Now we are looking at an autoconf address with the same
646 * prefix as ours. If the address is temporary and is still
647 * preferred, do not create another one. It would be rare, but
648 * could happen, for example, when we resume a laptop PC after
651 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
652 !IFA6_IS_DEPRECATED(it6)) {
658 * This is a public autoconf address that has the same prefix
659 * as ours. If it is preferred, keep it. We can't break the
660 * loop here, because there may be a still-preferred temporary
661 * address with the prefix.
663 if (!IFA6_IS_DEPRECATED(it6))
667 if (public_ifa6 != NULL) {
670 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
671 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
672 " tmp addr,errno=%d\n", e);
682 * Nuke neighbor cache/prefix/default router management table, right before
689 struct llinfo_nd6 *ln, *nln;
690 struct nd_defrouter *dr, *ndr, drany;
691 struct nd_prefix *pr, *npr;
693 /* Nuke default router list entries toward ifp */
694 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
696 * The first entry of the list may be stored in
697 * the routing table, so we'll delete it later.
699 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
700 ndr = TAILQ_NEXT(dr, dr_entry);
704 dr = TAILQ_FIRST(&nd_defrouter);
709 /* Nuke prefix list entries toward ifp */
710 for (pr = nd_prefix.lh_first; pr; pr = npr) {
712 if (pr->ndpr_ifp == ifp) {
714 * Previously, pr->ndpr_addr is removed as well,
715 * but I strongly believe we don't have to do it.
716 * nd6_purge() is only called from in6_ifdetach(),
717 * which removes all the associated interface addresses
719 * (jinmei@kame.net 20010129)
725 /* cancel default outgoing interface setting */
726 if (nd6_defifindex == ifp->if_index)
727 nd6_setdefaultiface(0);
729 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
730 /* refresh default router list */
731 bzero(&drany, sizeof(drany));
732 defrouter_delreq(&drany, 0);
737 * Nuke neighbor cache entries for the ifp.
738 * Note that rt->rt_ifp may not be the same as ifp,
739 * due to KAME goto ours hack. See RTM_RESOLVE case in
740 * nd6_rtrequest(), and ip6_input().
742 ln = llinfo_nd6.ln_next;
743 while (ln && ln != &llinfo_nd6) {
745 struct sockaddr_dl *sdl;
749 if (rt && rt->rt_gateway &&
750 rt->rt_gateway->sa_family == AF_LINK) {
751 sdl = (struct sockaddr_dl *)rt->rt_gateway;
752 if (sdl->sdl_index == ifp->if_index)
760 nd6_lookup(addr6, create, ifp)
761 struct in6_addr *addr6;
766 struct sockaddr_in6 sin6;
768 bzero(&sin6, sizeof(sin6));
769 sin6.sin6_len = sizeof(struct sockaddr_in6);
770 sin6.sin6_family = AF_INET6;
771 sin6.sin6_addr = *addr6;
772 rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL);
774 if ((rt->rt_flags & RTF_LLINFO) == 0 && create) {
776 * This is the case for the default route.
777 * If we want to create a neighbor cache for the
778 * address, we should free the route for the
779 * destination and allocate an interface route.
790 * If no route is available and create is set,
791 * we allocate a host route for the destination
792 * and treat it like an interface route.
793 * This hack is necessary for a neighbor which can't
794 * be covered by our own prefix.
797 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
802 * Create a new route. RTF_LLINFO is necessary
803 * to create a Neighbor Cache entry for the
804 * destination in nd6_rtrequest which will be
805 * called in rtrequest via ifa->ifa_rtrequest.
807 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
808 ifa->ifa_addr, (struct sockaddr *)&all1_sa,
809 (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
810 ~RTF_CLONING, &rt)) != 0) {
812 "nd6_lookup: failed to add route for a "
813 "neighbor(%s), errno=%d\n",
814 ip6_sprintf(addr6), e);
820 struct llinfo_nd6 *ln =
821 (struct llinfo_nd6 *)rt->rt_llinfo;
822 ln->ln_state = ND6_LLINFO_NOSTATE;
830 * Validation for the entry.
831 * Note that the check for rt_llinfo is necessary because a cloned
832 * route from a parent route that has the L flag (e.g. the default
833 * route to a p2p interface) may have the flag, too, while the
834 * destination is not actually a neighbor.
835 * XXX: we can't use rt->rt_ifp to check for the interface, since
836 * it might be the loopback interface if the entry is for our
837 * own address on a non-loopback interface. Instead, we should
838 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
841 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
842 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
843 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
846 "nd6_lookup: failed to lookup %s (if = %s)\n",
848 ifp ? if_name(ifp) : "unspec");
849 /* xxx more logs... kazu */
854 RT_UNLOCK(rt); /* XXX not ready to return rt locked */
859 * Test whether a given IPv6 address is a neighbor or not, ignoring
860 * the actual neighbor cache. The neighbor cache is ignored in order
861 * to not reenter the routing code from within itself.
864 nd6_is_new_addr_neighbor(addr, ifp)
865 struct sockaddr_in6 *addr;
868 struct nd_prefix *pr;
871 * A link-local address is always a neighbor.
872 * XXX: we should use the sin6_scope_id field rather than the embedded
874 * XXX: a link does not necessarily specify a single interface.
876 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
877 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
881 * If the address matches one of our addresses,
882 * it should be a neighbor.
883 * If the address matches one of our on-link prefixes, it should be a
886 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
887 if (pr->ndpr_ifp != ifp)
890 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
893 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
894 &addr->sin6_addr, &pr->ndpr_mask))
899 * If the default router list is empty, all addresses are regarded
900 * as on-link, and thus, as a neighbor.
901 * XXX: we restrict the condition to hosts, because routers usually do
902 * not have the "default router list".
904 if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
905 nd6_defifindex == ifp->if_index) {
914 * Detect if a given IPv6 address identifies a neighbor on a given link.
915 * XXX: should take care of the destination of a p2p link?
918 nd6_is_addr_neighbor(addr, ifp)
919 struct sockaddr_in6 *addr;
923 if (nd6_is_new_addr_neighbor(addr, ifp))
927 * Even if the address matches none of our addresses, it might be
928 * in the neighbor cache.
930 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
937 * Free an nd6 llinfo entry.
943 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
944 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
945 struct nd_defrouter *dr;
948 * we used to have pfctlinput(PRC_HOSTDEAD) here.
949 * even though it is not harmful, it was not really necessary.
952 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
955 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
958 if (ln->ln_router || dr) {
960 * rt6_flush must be called whether or not the neighbor
961 * is in the Default Router List.
962 * See a corresponding comment in nd6_na_input().
964 rt6_flush(&in6, rt->rt_ifp);
969 * Unreachablity of a router might affect the default
970 * router selection and on-link detection of advertised
975 * Temporarily fake the state to choose a new default
976 * router and to perform on-link determination of
977 * prefixes correctly.
978 * Below the state will be set correctly,
979 * or the entry itself will be deleted.
981 ln->ln_state = ND6_LLINFO_INCOMPLETE;
984 * Since defrouter_select() does not affect the
985 * on-link determination and MIP6 needs the check
986 * before the default router selection, we perform
989 pfxlist_onlink_check();
991 if (dr == TAILQ_FIRST(&nd_defrouter)) {
993 * It is used as the current default router,
994 * so we have to move it to the end of the
995 * list and choose a new one.
996 * XXX: it is not very efficient if this is
999 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
1000 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
1009 * Before deleting the entry, remember the next entry as the
1010 * return value. We need this because pfxlist_onlink_check() above
1011 * might have freed other entries (particularly the old next entry) as
1012 * a side effect (XXX).
1017 * Detach the route from the routing tree and the list of neighbor
1018 * caches, and disable the route entry not to be used in already
1021 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
1022 rt_mask(rt), 0, (struct rtentry **)0);
1028 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1030 * XXX cost-effective metods?
1033 nd6_nud_hint(rt, dst6, force)
1035 struct in6_addr *dst6;
1038 struct llinfo_nd6 *ln;
1041 * If the caller specified "rt", use that. Otherwise, resolve the
1042 * routing table by supplied "dst6".
1047 if (!(rt = nd6_lookup(dst6, 0, NULL)))
1051 if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
1052 (rt->rt_flags & RTF_LLINFO) == 0 ||
1053 !rt->rt_llinfo || !rt->rt_gateway ||
1054 rt->rt_gateway->sa_family != AF_LINK) {
1055 /* This is not a host route. */
1059 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1060 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1064 * if we get upper-layer reachability confirmation many times,
1065 * it is possible we have false information.
1069 if (ln->ln_byhint > nd6_maxnudhint)
1073 ln->ln_state = ND6_LLINFO_REACHABLE;
1075 ln->ln_expire = time_second +
1076 ND_IFINFO(rt->rt_ifp)->reachable;
1080 nd6_rtrequest(req, rt, info)
1083 struct rt_addrinfo *info; /* xxx unused */
1085 struct sockaddr *gate = rt->rt_gateway;
1086 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1087 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1088 struct ifnet *ifp = rt->rt_ifp;
1093 if ((rt->rt_flags & RTF_GATEWAY) != 0)
1096 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1098 * This is probably an interface direct route for a link
1099 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1100 * We do not need special treatment below for such a route.
1101 * Moreover, the RTF_LLINFO flag which would be set below
1102 * would annoy the ndp(8) command.
1107 if (req == RTM_RESOLVE &&
1108 (nd6_need_cache(ifp) == 0 || /* stf case */
1109 !nd6_is_new_addr_neighbor((struct sockaddr_in6 *)rt_key(rt),
1112 * FreeBSD and BSD/OS often make a cloned host route based
1113 * on a less-specific route (e.g. the default route).
1114 * If the less specific route does not have a "gateway"
1115 * (this is the case when the route just goes to a p2p or an
1116 * stf interface), we'll mistakenly make a neighbor cache for
1117 * the host route, and will see strange neighbor solicitation
1118 * for the corresponding destination. In order to avoid the
1119 * confusion, we check if the destination of the route is
1120 * a neighbor in terms of neighbor discovery, and stop the
1121 * process if not. Additionally, we remove the LLINFO flag
1122 * so that ndp(8) will not try to get the neighbor information
1123 * of the destination.
1125 rt->rt_flags &= ~RTF_LLINFO;
1132 * There is no backward compatibility :)
1134 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1135 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1136 * rt->rt_flags |= RTF_CLONING;
1138 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1140 * Case 1: This route should come from
1141 * a route to interface. RTF_LLINFO flag is set
1142 * for a host route whose destination should be
1143 * treated as on-link.
1145 rt_setgate(rt, rt_key(rt),
1146 (struct sockaddr *)&null_sdl);
1147 gate = rt->rt_gateway;
1148 SDL(gate)->sdl_type = ifp->if_type;
1149 SDL(gate)->sdl_index = ifp->if_index;
1151 ln->ln_expire = time_second;
1152 if (ln && ln->ln_expire == 0) {
1153 /* kludge for desktops */
1156 if ((rt->rt_flags & RTF_CLONING) != 0)
1160 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1161 * We don't do that here since llinfo is not ready yet.
1163 * There are also couple of other things to be discussed:
1164 * - unsolicited NA code needs improvement beforehand
1165 * - RFC2461 says we MAY send multicast unsolicited NA
1166 * (7.2.6 paragraph 4), however, it also says that we
1167 * SHOULD provide a mechanism to prevent multicast NA storm.
1168 * we don't have anything like it right now.
1169 * note that the mechanism needs a mutual agreement
1170 * between proxies, which means that we need to implement
1171 * a new protocol, or a new kludge.
1172 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1173 * we need to check ip6forwarding before sending it.
1174 * (or should we allow proxy ND configuration only for
1175 * routers? there's no mention about proxy ND from hosts)
1178 /* XXX it does not work */
1179 if (rt->rt_flags & RTF_ANNOUNCE)
1181 &SIN6(rt_key(rt))->sin6_addr,
1182 &SIN6(rt_key(rt))->sin6_addr,
1183 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1188 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1190 * Address resolution isn't necessary for a point to
1191 * point link, so we can skip this test for a p2p link.
1193 if (gate->sa_family != AF_LINK ||
1194 gate->sa_len < sizeof(null_sdl)) {
1196 "nd6_rtrequest: bad gateway value: %s\n",
1200 SDL(gate)->sdl_type = ifp->if_type;
1201 SDL(gate)->sdl_index = ifp->if_index;
1204 break; /* This happens on a route change */
1206 * Case 2: This route may come from cloning, or a manual route
1207 * add with a LL address.
1209 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1210 rt->rt_llinfo = (caddr_t)ln;
1212 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1217 bzero(ln, sizeof(*ln));
1219 /* this is required for "ndp" command. - shin */
1220 if (req == RTM_ADD) {
1222 * gate should have some valid AF_LINK entry,
1223 * and ln->ln_expire should have some lifetime
1224 * which is specified by ndp command.
1226 ln->ln_state = ND6_LLINFO_REACHABLE;
1230 * When req == RTM_RESOLVE, rt is created and
1231 * initialized in rtrequest(), so rt_expire is 0.
1233 ln->ln_state = ND6_LLINFO_NOSTATE;
1234 ln->ln_expire = time_second;
1236 rt->rt_flags |= RTF_LLINFO;
1237 ln->ln_next = llinfo_nd6.ln_next;
1238 llinfo_nd6.ln_next = ln;
1239 ln->ln_prev = &llinfo_nd6;
1240 ln->ln_next->ln_prev = ln;
1243 * check if rt_key(rt) is one of my address assigned
1246 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1247 &SIN6(rt_key(rt))->sin6_addr);
1249 caddr_t macp = nd6_ifptomac(ifp);
1251 ln->ln_state = ND6_LLINFO_REACHABLE;
1254 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1255 SDL(gate)->sdl_alen = ifp->if_addrlen;
1257 if (nd6_useloopback) {
1258 rt->rt_ifp = &loif[0]; /* XXX */
1260 * Make sure rt_ifa be equal to the ifaddr
1261 * corresponding to the address.
1262 * We need this because when we refer
1263 * rt_ifa->ia6_flags in ip6_input, we assume
1264 * that the rt_ifa points to the address instead
1265 * of the loopback address.
1267 if (ifa != rt->rt_ifa) {
1268 IFAFREE(rt->rt_ifa);
1273 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1275 ln->ln_state = ND6_LLINFO_REACHABLE;
1278 /* join solicited node multicast for proxy ND */
1279 if (ifp->if_flags & IFF_MULTICAST) {
1280 struct in6_addr llsol;
1283 llsol = SIN6(rt_key(rt))->sin6_addr;
1284 llsol.s6_addr16[0] = htons(0xff02);
1285 llsol.s6_addr16[1] = htons(ifp->if_index);
1286 llsol.s6_addr32[1] = 0;
1287 llsol.s6_addr32[2] = htonl(1);
1288 llsol.s6_addr8[12] = 0xff;
1290 if (!in6_addmulti(&llsol, ifp, &error)) {
1291 nd6log((LOG_ERR, "%s: failed to join "
1292 "%s (errno=%d)\n", if_name(ifp),
1293 ip6_sprintf(&llsol), error));
1302 /* leave from solicited node multicast for proxy ND */
1303 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1304 (ifp->if_flags & IFF_MULTICAST) != 0) {
1305 struct in6_addr llsol;
1306 struct in6_multi *in6m;
1308 llsol = SIN6(rt_key(rt))->sin6_addr;
1309 llsol.s6_addr16[0] = htons(0xff02);
1310 llsol.s6_addr16[1] = htons(ifp->if_index);
1311 llsol.s6_addr32[1] = 0;
1312 llsol.s6_addr32[2] = htonl(1);
1313 llsol.s6_addr8[12] = 0xff;
1315 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1320 ln->ln_next->ln_prev = ln->ln_prev;
1321 ln->ln_prev->ln_next = ln->ln_next;
1324 rt->rt_flags &= ~RTF_LLINFO;
1326 m_freem(ln->ln_hold);
1332 nd6_ioctl(cmd, data, ifp)
1337 struct in6_drlist *drl = (struct in6_drlist *)data;
1338 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1339 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1340 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1341 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1342 struct nd_defrouter *dr, any;
1343 struct nd_prefix *pr;
1345 int i = 0, error = 0;
1349 case SIOCGDRLST_IN6:
1351 * obsolete API, use sysctl under net.inet6.icmp6
1353 bzero(drl, sizeof(*drl));
1355 dr = TAILQ_FIRST(&nd_defrouter);
1356 while (dr && i < DRLSTSIZ) {
1357 drl->defrouter[i].rtaddr = dr->rtaddr;
1358 in6_clearscope(&drl->defrouter[i].rtaddr);
1360 drl->defrouter[i].flags = dr->flags;
1361 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1362 drl->defrouter[i].expire = dr->expire;
1363 drl->defrouter[i].if_index = dr->ifp->if_index;
1365 dr = TAILQ_NEXT(dr, dr_entry);
1369 case SIOCGPRLST_IN6:
1371 * obsolete API, use sysctl under net.inet6.icmp6
1373 * XXX the structure in6_prlist was changed in backward-
1374 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1375 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1378 * XXX meaning of fields, especialy "raflags", is very
1379 * differnet between RA prefix list and RR/static prefix list.
1380 * how about separating ioctls into two?
1382 bzero(oprl, sizeof(*oprl));
1384 pr = nd_prefix.lh_first;
1385 while (pr && i < PRLSTSIZ) {
1386 struct nd_pfxrouter *pfr;
1389 (void)in6_embedscope(&oprl->prefix[i].prefix,
1390 &pr->ndpr_prefix, NULL, NULL);
1391 oprl->prefix[i].raflags = pr->ndpr_raf;
1392 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1393 oprl->prefix[i].vltime = pr->ndpr_vltime;
1394 oprl->prefix[i].pltime = pr->ndpr_pltime;
1395 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1396 oprl->prefix[i].expire = pr->ndpr_expire;
1398 pfr = pr->ndpr_advrtrs.lh_first;
1402 #define RTRADDR oprl->prefix[i].advrtr[j]
1403 RTRADDR = pfr->router->rtaddr;
1404 in6_clearscope(&RTRADDR);
1408 pfr = pfr->pfr_next;
1410 oprl->prefix[i].advrtrs = j;
1411 oprl->prefix[i].origin = PR_ORIG_RA;
1419 case OSIOCGIFINFO_IN6:
1420 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1421 bzero(&ndi->ndi, sizeof(ndi->ndi));
1422 ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1423 ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1424 ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1425 ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1426 ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1427 ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1428 ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1429 ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1431 case SIOCGIFINFO_IN6:
1432 ndi->ndi = *ND_IFINFO(ifp);
1433 ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1435 case SIOCSIFINFO_FLAGS:
1436 ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1438 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1439 /* flush default router list */
1441 * xxx sumikawa: should not delete route if default
1442 * route equals to the top of default router list
1444 bzero(&any, sizeof(any));
1445 defrouter_delreq(&any, 0);
1447 /* xxx sumikawa: flush prefix list */
1449 case SIOCSPFXFLUSH_IN6:
1451 /* flush all the prefix advertised by routers */
1452 struct nd_prefix *pr, *next;
1455 for (pr = nd_prefix.lh_first; pr; pr = next) {
1456 struct in6_ifaddr *ia, *ia_next;
1458 next = pr->ndpr_next;
1460 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1463 /* do we really have to remove addresses as well? */
1464 for (ia = in6_ifaddr; ia; ia = ia_next) {
1465 /* ia might be removed. keep the next ptr. */
1466 ia_next = ia->ia_next;
1468 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1471 if (ia->ia6_ndpr == pr)
1472 in6_purgeaddr(&ia->ia_ifa);
1479 case SIOCSRTRFLUSH_IN6:
1481 /* flush all the default routers */
1482 struct nd_defrouter *dr, *next;
1485 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1487 * The first entry of the list may be stored in
1488 * the routing table, so we'll delete it later.
1490 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1491 next = TAILQ_NEXT(dr, dr_entry);
1494 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1499 case SIOCGNBRINFO_IN6:
1501 struct llinfo_nd6 *ln;
1502 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1505 * XXX: KAME specific hack for scoped addresses
1506 * XXXX: for other scopes than link-local?
1508 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1509 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1510 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1513 *idp = htons(ifp->if_index);
1517 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1522 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1523 nbi->state = ln->ln_state;
1524 nbi->asked = ln->ln_asked;
1525 nbi->isrouter = ln->ln_router;
1526 nbi->expire = ln->ln_expire;
1531 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1532 ndif->ifindex = nd6_defifindex;
1534 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1535 return (nd6_setdefaultiface(ndif->ifindex));
1541 * Create neighbor cache entry and cache link-layer address,
1542 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1545 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1547 struct in6_addr *from;
1550 int type; /* ICMP6 type */
1551 int code; /* type dependent information */
1553 struct rtentry *rt = NULL;
1554 struct llinfo_nd6 *ln = NULL;
1556 struct sockaddr_dl *sdl = NULL;
1563 panic("ifp == NULL in nd6_cache_lladdr");
1565 panic("from == NULL in nd6_cache_lladdr");
1567 /* nothing must be updated for unspecified address */
1568 if (IN6_IS_ADDR_UNSPECIFIED(from))
1572 * Validation about ifp->if_addrlen and lladdrlen must be done in
1575 * XXX If the link does not have link-layer adderss, what should
1576 * we do? (ifp->if_addrlen == 0)
1577 * Spec says nothing in sections for RA, RS and NA. There's small
1578 * description on it in NS section (RFC 2461 7.2.3).
1581 rt = nd6_lookup(from, 0, ifp);
1584 /* nothing must be done if there's no lladdr */
1585 if (!lladdr || !lladdrlen)
1589 rt = nd6_lookup(from, 1, ifp);
1592 /* do nothing if static ndp is set */
1593 if (rt->rt_flags & RTF_STATIC)
1600 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1605 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1608 if (!rt->rt_gateway)
1610 if (rt->rt_gateway->sa_family != AF_LINK)
1612 sdl = SDL(rt->rt_gateway);
1614 olladdr = (sdl->sdl_alen) ? 1 : 0;
1615 if (olladdr && lladdr) {
1616 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1624 * newentry olladdr lladdr llchange (*=record)
1627 * 0 n y -- (3) * STALE
1629 * 0 y y y (5) * STALE
1630 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1631 * 1 -- y -- (7) * STALE
1634 if (lladdr) { /* (3-5) and (7) */
1636 * Record source link-layer address
1637 * XXX is it dependent to ifp->if_type?
1639 sdl->sdl_alen = ifp->if_addrlen;
1640 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1644 if ((!olladdr && lladdr) || /* (3) */
1645 (olladdr && lladdr && llchange)) { /* (5) */
1647 newstate = ND6_LLINFO_STALE;
1648 } else /* (1-2,4) */
1652 if (!lladdr) /* (6) */
1653 newstate = ND6_LLINFO_NOSTATE;
1655 newstate = ND6_LLINFO_STALE;
1660 * Update the state of the neighbor cache.
1662 ln->ln_state = newstate;
1664 if (ln->ln_state == ND6_LLINFO_STALE) {
1666 * XXX: since nd6_output() below will cause
1667 * state tansition to DELAY and reset the timer,
1668 * we must set the timer now, although it is actually
1671 ln->ln_expire = time_second + nd6_gctimer;
1675 * we assume ifp is not a p2p here, so just
1676 * set the 2nd argument as the 1st one.
1678 nd6_output(ifp, ifp, ln->ln_hold,
1679 (struct sockaddr_in6 *)rt_key(rt), rt);
1682 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1683 /* probe right away */
1684 ln->ln_expire = time_second;
1689 * ICMP6 type dependent behavior.
1691 * NS: clear IsRouter if new entry
1692 * RS: clear IsRouter
1693 * RA: set IsRouter if there's lladdr
1694 * redir: clear IsRouter if new entry
1697 * The spec says that we must set IsRouter in the following cases:
1698 * - If lladdr exist, set IsRouter. This means (1-5).
1699 * - If it is old entry (!newentry), set IsRouter. This means (7).
1700 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1701 * A quetion arises for (1) case. (1) case has no lladdr in the
1702 * neighbor cache, this is similar to (6).
1703 * This case is rare but we figured that we MUST NOT set IsRouter.
1705 * newentry olladdr lladdr llchange NS RS RA redir
1707 * 0 n n -- (1) c ? s
1708 * 0 y n -- (2) c s s
1709 * 0 n y -- (3) c s s
1712 * 1 -- n -- (6) c c c s
1713 * 1 -- y -- (7) c c s c s
1717 switch (type & 0xff) {
1718 case ND_NEIGHBOR_SOLICIT:
1720 * New entry must have is_router flag cleared.
1722 if (is_newentry) /* (6-7) */
1727 * If the icmp is a redirect to a better router, always set the
1728 * is_router flag. Otherwise, if the entry is newly created,
1729 * clear the flag. [RFC 2461, sec 8.3]
1731 if (code == ND_REDIRECT_ROUTER)
1733 else if (is_newentry) /* (6-7) */
1736 case ND_ROUTER_SOLICIT:
1738 * is_router flag must always be cleared.
1742 case ND_ROUTER_ADVERT:
1744 * Mark an entry with lladdr as a router.
1746 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1747 (is_newentry && lladdr)) { /* (7) */
1754 * When the link-layer address of a router changes, select the
1755 * best router again. In particular, when the neighbor entry is newly
1756 * created, it might affect the selection policy.
1757 * Question: can we restrict the first condition to the "is_newentry"
1759 * XXX: when we hear an RA from a new router with the link-layer
1760 * address option, defrouter_select() is called twice, since
1761 * defrtrlist_update called the function as well. However, I believe
1762 * we can compromise the overhead, since it only happens the first
1764 * XXX: although defrouter_select() should not have a bad effect
1765 * for those are not autoconfigured hosts, we explicitly avoid such
1768 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1775 nd6_slowtimo(ignored_arg)
1779 struct nd_ifinfo *nd6if;
1782 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1783 nd6_slowtimo, NULL);
1785 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1786 nd6if = ND_IFINFO(ifp);
1787 if (nd6if->basereachable && /* already initialized */
1788 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1790 * Since reachable time rarely changes by router
1791 * advertisements, we SHOULD insure that a new random
1792 * value gets recomputed at least once every few hours.
1795 nd6if->recalctm = nd6_recalc_reachtm_interval;
1796 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1803 #define senderr(e) { error = (e); goto bad;}
1805 nd6_output(ifp, origifp, m0, dst, rt0)
1807 struct ifnet *origifp;
1809 struct sockaddr_in6 *dst;
1810 struct rtentry *rt0;
1812 struct mbuf *m = m0;
1813 struct rtentry *rt = rt0;
1814 struct sockaddr_in6 *gw6 = NULL;
1815 struct llinfo_nd6 *ln = NULL;
1818 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1821 if (nd6_need_cache(ifp) == 0)
1825 * next hop determination. This routine is derived from ether_outpout.
1829 if ((rt->rt_flags & RTF_UP) == 0) {
1830 rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL);
1834 if (rt->rt_ifp != ifp)
1836 * XXX maybe we should update ifp too,
1837 * but the original code didn't and I
1838 * don't know what is correct here.
1842 senderr(EHOSTUNREACH);
1845 if (rt->rt_flags & RTF_GATEWAY) {
1846 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1849 * We skip link-layer address resolution and NUD
1850 * if the gateway is not a neighbor from ND point
1851 * of view, regardless of the value of nd_ifinfo.flags.
1852 * The second condition is a bit tricky; we skip
1853 * if the gateway is our own address, which is
1854 * sometimes used to install a route to a p2p link.
1856 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1857 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1859 * We allow this kind of tricky route only
1860 * when the outgoing interface is p2p.
1861 * XXX: we may need a more generic rule here.
1863 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1864 senderr(EHOSTUNREACH);
1869 if (rt->rt_gwroute == 0)
1871 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1873 rtfree(rt); rt = rt0;
1875 rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
1876 if ((rt = rt->rt_gwroute) == 0)
1877 senderr(EHOSTUNREACH);
1884 * Address resolution or Neighbor Unreachability Detection
1886 * At this point, the destination of the packet must be a unicast
1887 * or an anycast address(i.e. not a multicast).
1890 /* Look up the neighbor cache for the nexthop */
1891 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1892 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1895 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1896 * the condition below is not very efficient. But we believe
1897 * it is tolerable, because this should be a rare case.
1899 if (nd6_is_addr_neighbor(dst, ifp) &&
1900 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1901 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1904 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1905 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1907 "nd6_output: can't allocate llinfo for %s "
1909 ip6_sprintf(&dst->sin6_addr), ln, rt);
1910 senderr(EIO); /* XXX: good error? */
1913 goto sendpkt; /* send anyway */
1916 /* We don't have to do link-layer address resolution on a p2p link. */
1917 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1918 ln->ln_state < ND6_LLINFO_REACHABLE) {
1919 ln->ln_state = ND6_LLINFO_STALE;
1920 ln->ln_expire = time_second + nd6_gctimer;
1924 * The first time we send a packet to a neighbor whose entry is
1925 * STALE, we have to change the state to DELAY and a sets a timer to
1926 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1927 * neighbor unreachability detection on expiration.
1930 if (ln->ln_state == ND6_LLINFO_STALE) {
1932 ln->ln_state = ND6_LLINFO_DELAY;
1933 ln->ln_expire = time_second + nd6_delay;
1937 * If the neighbor cache entry has a state other than INCOMPLETE
1938 * (i.e. its link-layer address is already resolved), just
1941 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1945 * There is a neighbor cache entry, but no ethernet address
1946 * response yet. Replace the held mbuf (if any) with this
1949 * This code conforms to the rate-limiting rule described in Section
1950 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
1953 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1954 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1956 m_freem(ln->ln_hold);
1958 if (ln->ln_expire) {
1959 if (ln->ln_asked < nd6_mmaxtries &&
1960 ln->ln_expire < time_second) {
1962 ln->ln_expire = time_second +
1963 ND_IFINFO(ifp)->retrans / 1000;
1964 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1971 /* clean ipsec history once it goes out of the node */
1976 mac_create_mbuf_linklayer(ifp, m);
1978 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1979 return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
1982 return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1996 * XXX: we currently do not make neighbor cache on any interface
1997 * other than ARCnet, Ethernet, FDDI and GIF.
2000 * - unidirectional tunnels needs no ND
2002 switch (ifp->if_type) {
2010 #ifdef IFT_IEEE80211
2016 case IFT_GIF: /* XXX need more cases? */
2024 nd6_storelladdr(ifp, rt0, m, dst, desten)
2026 struct rtentry *rt0;
2028 struct sockaddr *dst;
2032 struct sockaddr_dl *sdl;
2035 if (m->m_flags & M_MCAST) {
2036 switch (ifp->if_type) {
2042 #ifdef IFT_IEEE80211
2046 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2051 * netbsd can use if_broadcastaddr, but we don't do so
2052 * to reduce # of ifdef.
2054 for (i = 0; i < ifp->if_addrlen; i++)
2062 return (EAFNOSUPPORT);
2066 i = rt_check(&rt, &rt0, dst);
2073 /* this could happen, if we could not allocate memory */
2077 if (rt->rt_gateway->sa_family != AF_LINK) {
2078 printf("nd6_storelladdr: something odd happens\n");
2082 sdl = SDL(rt->rt_gateway);
2083 if (sdl->sdl_alen == 0) {
2084 /* this should be impossible, but we bark here for debugging */
2085 printf("nd6_storelladdr: sdl_alen == 0\n");
2090 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2094 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2095 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2097 SYSCTL_DECL(_net_inet6_icmp6);
2099 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2100 CTLFLAG_RD, nd6_sysctl_drlist, "");
2101 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2102 CTLFLAG_RD, nd6_sysctl_prlist, "");
2105 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2109 struct in6_defrouter *d, *de;
2110 struct nd_defrouter *dr;
2116 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2117 dr = TAILQ_NEXT(dr, dr_entry)) {
2118 d = (struct in6_defrouter *)buf;
2119 de = (struct in6_defrouter *)(buf + sizeof(buf));
2122 bzero(d, sizeof(*d));
2123 d->rtaddr.sin6_family = AF_INET6;
2124 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2125 if (in6_recoverscope(&d->rtaddr, &dr->rtaddr,
2129 "default router list (%s)\n",
2130 ip6_sprintf(&dr->rtaddr));
2131 d->flags = dr->flags;
2132 d->rtlifetime = dr->rtlifetime;
2133 d->expire = dr->expire;
2134 d->if_index = dr->ifp->if_index;
2136 panic("buffer too short");
2138 error = SYSCTL_OUT(req, buf, sizeof(*d));
2147 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2151 struct in6_prefix *p, *pe;
2152 struct nd_prefix *pr;
2158 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2161 struct sockaddr_in6 *sin6, *s6;
2162 struct nd_pfxrouter *pfr;
2164 p = (struct in6_prefix *)buf;
2165 pe = (struct in6_prefix *)(buf + sizeof(buf));
2168 bzero(p, sizeof(*p));
2169 sin6 = (struct sockaddr_in6 *)(p + 1);
2171 p->prefix = pr->ndpr_prefix;
2172 if (in6_recoverscope(&p->prefix,
2173 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2175 "scope error in prefix list (%s)\n",
2176 ip6_sprintf(&p->prefix.sin6_addr));
2177 p->raflags = pr->ndpr_raf;
2178 p->prefixlen = pr->ndpr_plen;
2179 p->vltime = pr->ndpr_vltime;
2180 p->pltime = pr->ndpr_pltime;
2181 p->if_index = pr->ndpr_ifp->if_index;
2182 p->expire = pr->ndpr_expire;
2183 p->refcnt = pr->ndpr_refcnt;
2184 p->flags = pr->ndpr_stateflags;
2185 p->origin = PR_ORIG_RA;
2187 for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2188 pfr = pfr->pfr_next) {
2189 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2193 s6 = &sin6[advrtrs];
2194 bzero(s6, sizeof(*s6));
2195 s6->sin6_family = AF_INET6;
2196 s6->sin6_len = sizeof(*sin6);
2197 if (in6_recoverscope(s6, &pfr->router->rtaddr,
2198 pfr->router->ifp) != 0)
2201 "prefix list (%s)\n",
2202 ip6_sprintf(&pfr->router->rtaddr));
2205 p->advrtrs = advrtrs;
2207 panic("buffer too short");
2209 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2210 error = SYSCTL_OUT(req, buf, advance);