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/if_atm.h>
58 #include <net/iso88025.h>
60 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/if_ether.h>
64 #include <netinet6/in6_var.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #include <netinet6/nd6.h>
68 #include <netinet/icmp6.h>
70 #include <net/net_osdep.h>
72 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
73 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
75 #define SIN6(s) ((struct sockaddr_in6 *)s)
76 #define SDL(s) ((struct sockaddr_dl *)s)
79 int nd6_prune = 1; /* walk list every 1 seconds */
80 int nd6_delay = 5; /* delay first probe time 5 second */
81 int nd6_umaxtries = 3; /* maximum unicast query */
82 int nd6_mmaxtries = 3; /* maximum multicast query */
83 int nd6_useloopback = 1; /* use loopback interface for local traffic */
84 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
86 /* preventing too many loops in ND option parsing */
87 int nd6_maxndopt = 10; /* max # of ND options allowed */
89 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
98 static int nd6_inuse, nd6_allocated;
100 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
101 struct nd_drhead nd_defrouter;
102 struct nd_prhead nd_prefix = { 0 };
104 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
105 static struct sockaddr_in6 all1_sa;
107 static int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *,
109 static void nd6_setmtu0 __P((struct ifnet *, struct nd_ifinfo *));
110 static void nd6_slowtimo __P((void *));
111 static int regen_tmpaddr __P((struct in6_ifaddr *));
113 struct callout nd6_slowtimo_ch;
114 struct callout nd6_timer_ch;
115 extern struct callout in6_tmpaddrtimer_ch;
120 static int nd6_init_done = 0;
124 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
128 all1_sa.sin6_family = AF_INET6;
129 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
130 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
131 all1_sa.sin6_addr.s6_addr[i] = 0xff;
133 /* initialization of the default router list */
134 TAILQ_INIT(&nd_defrouter);
139 callout_init(&nd6_slowtimo_ch, 0);
140 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
148 struct nd_ifinfo *nd;
150 nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
151 bzero(nd, sizeof(*nd));
155 nd->chlim = IPV6_DEFHLIM;
156 nd->basereachable = REACHABLE_TIME;
157 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
158 nd->retrans = RETRANS_TIMER;
160 * Note that the default value of ip6_accept_rtadv is 0, which means
161 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
164 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
166 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
167 nd6_setmtu0(ifp, nd);
174 struct nd_ifinfo *nd;
181 * Reset ND level link MTU. This function is called when the physical MTU
182 * changes, which means we might have to adjust the ND level MTU.
189 nd6_setmtu0(ifp, ND_IFINFO(ifp));
192 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
194 nd6_setmtu0(ifp, ndi)
196 struct nd_ifinfo *ndi;
200 omaxmtu = ndi->maxmtu;
202 switch (ifp->if_type) {
204 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
207 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
210 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
213 ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu);
215 case IFT_IEEE1394: /* XXX should be IEEE1394MTU(1500) */
216 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
219 case IFT_IEEE80211: /* XXX should be IEEE80211MTU(1500) */
220 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
224 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
227 ndi->maxmtu = ifp->if_mtu;
232 * Decreasing the interface MTU under IPV6 minimum MTU may cause
233 * undesirable situation. We thus notify the operator of the change
234 * explicitly. The check for omaxmtu is necessary to restrict the
235 * log to the case of changing the MTU, not initializing it.
237 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
238 log(LOG_NOTICE, "nd6_setmtu0: "
239 "new link MTU on %s (%lu) is too small for IPv6\n",
240 if_name(ifp), (unsigned long)ndi->maxmtu);
243 if (ndi->maxmtu > in6_maxmtu)
244 in6_setmaxmtu(); /* check all interfaces just in case */
250 nd6_option_init(opt, icmp6len, ndopts)
253 union nd_opts *ndopts;
256 bzero(ndopts, sizeof(*ndopts));
257 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
259 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
262 ndopts->nd_opts_done = 1;
263 ndopts->nd_opts_search = NULL;
268 * Take one ND option.
272 union nd_opts *ndopts;
274 struct nd_opt_hdr *nd_opt;
278 panic("ndopts == NULL in nd6_option");
279 if (!ndopts->nd_opts_last)
280 panic("uninitialized ndopts in nd6_option");
281 if (!ndopts->nd_opts_search)
283 if (ndopts->nd_opts_done)
286 nd_opt = ndopts->nd_opts_search;
288 /* make sure nd_opt_len is inside the buffer */
289 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
290 bzero(ndopts, sizeof(*ndopts));
294 olen = nd_opt->nd_opt_len << 3;
297 * Message validation requires that all included
298 * options have a length that is greater than zero.
300 bzero(ndopts, sizeof(*ndopts));
304 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
305 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
306 /* option overruns the end of buffer, invalid */
307 bzero(ndopts, sizeof(*ndopts));
309 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
310 /* reached the end of options chain */
311 ndopts->nd_opts_done = 1;
312 ndopts->nd_opts_search = NULL;
318 * Parse multiple ND options.
319 * This function is much easier to use, for ND routines that do not need
320 * multiple options of the same type.
324 union nd_opts *ndopts;
326 struct nd_opt_hdr *nd_opt;
330 panic("ndopts == NULL in nd6_options");
331 if (!ndopts->nd_opts_last)
332 panic("uninitialized ndopts in nd6_options");
333 if (!ndopts->nd_opts_search)
337 nd_opt = nd6_option(ndopts);
338 if (!nd_opt && !ndopts->nd_opts_last) {
340 * Message validation requires that all included
341 * options have a length that is greater than zero.
343 icmp6stat.icp6s_nd_badopt++;
344 bzero(ndopts, sizeof(*ndopts));
351 switch (nd_opt->nd_opt_type) {
352 case ND_OPT_SOURCE_LINKADDR:
353 case ND_OPT_TARGET_LINKADDR:
355 case ND_OPT_REDIRECTED_HEADER:
356 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
358 "duplicated ND6 option found (type=%d)\n",
359 nd_opt->nd_opt_type));
362 ndopts->nd_opt_array[nd_opt->nd_opt_type]
366 case ND_OPT_PREFIX_INFORMATION:
367 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
368 ndopts->nd_opt_array[nd_opt->nd_opt_type]
371 ndopts->nd_opts_pi_end =
372 (struct nd_opt_prefix_info *)nd_opt;
376 * Unknown options must be silently ignored,
377 * to accomodate future extension to the protocol.
380 "nd6_options: unsupported option %d - "
381 "option ignored\n", nd_opt->nd_opt_type));
386 if (i > nd6_maxndopt) {
387 icmp6stat.icp6s_nd_toomanyopt++;
388 nd6log((LOG_INFO, "too many loop in nd opt\n"));
392 if (ndopts->nd_opts_done)
400 * ND6 timer routine to expire default route list and prefix list
403 nd6_timer(ignored_arg)
407 struct llinfo_nd6 *ln;
408 struct nd_defrouter *dr;
409 struct nd_prefix *pr;
411 struct in6_ifaddr *ia6, *nia6;
412 struct in6_addrlifetime *lt6;
415 callout_reset(&nd6_timer_ch, nd6_prune * hz,
418 ln = llinfo_nd6.ln_next;
419 while (ln && ln != &llinfo_nd6) {
421 struct sockaddr_in6 *dst;
422 struct llinfo_nd6 *next = ln->ln_next;
423 /* XXX: used for the DELAY case only: */
424 struct nd_ifinfo *ndi = NULL;
426 if ((rt = ln->ln_rt) == NULL) {
430 if ((ifp = rt->rt_ifp) == NULL) {
434 ndi = ND_IFINFO(ifp);
435 dst = (struct sockaddr_in6 *)rt_key(rt);
437 if (ln->ln_expire > time_second) {
444 panic("rt=0 in nd6_timer(ln=%p)", ln);
445 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
446 panic("rt_llinfo(%p) is not equal to ln(%p)",
449 panic("dst=0 in nd6_timer(ln=%p)", ln);
451 switch (ln->ln_state) {
452 case ND6_LLINFO_INCOMPLETE:
453 if (ln->ln_asked < nd6_mmaxtries) {
455 ln->ln_expire = time_second +
456 ND_IFINFO(ifp)->retrans / 1000;
457 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
460 struct mbuf *m = ln->ln_hold;
464 * Fake rcvif to make ICMP error
465 * more helpful in diagnosing
467 * XXX: should we consider
470 m->m_pkthdr.rcvif = rt->rt_ifp;
472 icmp6_error(m, ICMP6_DST_UNREACH,
473 ICMP6_DST_UNREACH_ADDR, 0);
479 case ND6_LLINFO_REACHABLE:
481 ln->ln_state = ND6_LLINFO_STALE;
482 ln->ln_expire = time_second + nd6_gctimer;
486 case ND6_LLINFO_STALE:
487 /* Garbage Collection(RFC 2461 5.3) */
492 case ND6_LLINFO_DELAY:
493 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
496 ln->ln_state = ND6_LLINFO_PROBE;
497 ln->ln_expire = time_second +
499 nd6_ns_output(ifp, &dst->sin6_addr,
503 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
504 ln->ln_expire = time_second + nd6_gctimer;
507 case ND6_LLINFO_PROBE:
508 if (ln->ln_asked < nd6_umaxtries) {
510 ln->ln_expire = time_second +
511 ND_IFINFO(ifp)->retrans / 1000;
512 nd6_ns_output(ifp, &dst->sin6_addr,
513 &dst->sin6_addr, ln, 0);
522 /* expire default router list */
523 dr = TAILQ_FIRST(&nd_defrouter);
525 if (dr->expire && dr->expire < time_second) {
526 struct nd_defrouter *t;
527 t = TAILQ_NEXT(dr, dr_entry);
531 dr = TAILQ_NEXT(dr, dr_entry);
536 * expire interface addresses.
537 * in the past the loop was inside prefix expiry processing.
538 * However, from a stricter speci-confrmance standpoint, we should
539 * rather separate address lifetimes and prefix lifetimes.
542 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
544 /* check address lifetime */
545 lt6 = &ia6->ia6_lifetime;
546 if (IFA6_IS_INVALID(ia6)) {
550 * If the expiring address is temporary, try
551 * regenerating a new one. This would be useful when
552 * we suspended a laptop PC, then turned it on after a
553 * period that could invalidate all temporary
554 * addresses. Although we may have to restart the
555 * loop (see below), it must be after purging the
556 * address. Otherwise, we'd see an infinite loop of
559 if (ip6_use_tempaddr &&
560 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
561 if (regen_tmpaddr(ia6) == 0)
565 in6_purgeaddr(&ia6->ia_ifa);
568 goto addrloop; /* XXX: see below */
570 if (IFA6_IS_DEPRECATED(ia6)) {
571 int oldflags = ia6->ia6_flags;
573 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
576 * If a temporary address has just become deprecated,
577 * regenerate a new one if possible.
579 if (ip6_use_tempaddr &&
580 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
581 (oldflags & IN6_IFF_DEPRECATED) == 0) {
583 if (regen_tmpaddr(ia6) == 0) {
585 * A new temporary address is
587 * XXX: this means the address chain
588 * has changed while we are still in
589 * the loop. Although the change
590 * would not cause disaster (because
591 * it's not a deletion, but an
592 * addition,) we'd rather restart the
593 * loop just for safety. Or does this
594 * significantly reduce performance??
601 * A new RA might have made a deprecated address
604 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
608 /* expire prefix list */
609 pr = nd_prefix.lh_first;
612 * check prefix lifetime.
613 * since pltime is just for autoconf, pltime processing for
614 * prefix is not necessary.
616 if (pr->ndpr_expire && pr->ndpr_expire < time_second) {
621 * address expiration and prefix expiration are
622 * separate. NEVER perform in6_purgeaddr here.
635 struct in6_ifaddr *ia6; /* deprecated/invalidated temporary address */
639 struct in6_ifaddr *public_ifa6 = NULL;
641 ifp = ia6->ia_ifa.ifa_ifp;
642 for (ifa = ifp->if_addrlist.tqh_first; ifa;
643 ifa = ifa->ifa_list.tqe_next) {
644 struct in6_ifaddr *it6;
646 if (ifa->ifa_addr->sa_family != AF_INET6)
649 it6 = (struct in6_ifaddr *)ifa;
651 /* ignore no autoconf addresses. */
652 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
655 /* ignore autoconf addresses with different prefixes. */
656 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
660 * Now we are looking at an autoconf address with the same
661 * prefix as ours. If the address is temporary and is still
662 * preferred, do not create another one. It would be rare, but
663 * could happen, for example, when we resume a laptop PC after
666 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
667 !IFA6_IS_DEPRECATED(it6)) {
673 * This is a public autoconf address that has the same prefix
674 * as ours. If it is preferred, keep it. We can't break the
675 * loop here, because there may be a still-preferred temporary
676 * address with the prefix.
678 if (!IFA6_IS_DEPRECATED(it6))
682 if (public_ifa6 != NULL) {
685 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
686 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
687 " tmp addr,errno=%d\n", e);
697 * Nuke neighbor cache/prefix/default router management table, right before
704 struct llinfo_nd6 *ln, *nln;
705 struct nd_defrouter *dr, *ndr, drany;
706 struct nd_prefix *pr, *npr;
708 /* Nuke default router list entries toward ifp */
709 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
711 * The first entry of the list may be stored in
712 * the routing table, so we'll delete it later.
714 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
715 ndr = TAILQ_NEXT(dr, dr_entry);
719 dr = TAILQ_FIRST(&nd_defrouter);
724 /* Nuke prefix list entries toward ifp */
725 for (pr = nd_prefix.lh_first; pr; pr = npr) {
727 if (pr->ndpr_ifp == ifp) {
729 * Previously, pr->ndpr_addr is removed as well,
730 * but I strongly believe we don't have to do it.
731 * nd6_purge() is only called from in6_ifdetach(),
732 * which removes all the associated interface addresses
734 * (jinmei@kame.net 20010129)
740 /* cancel default outgoing interface setting */
741 if (nd6_defifindex == ifp->if_index)
742 nd6_setdefaultiface(0);
744 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
745 /* refresh default router list */
746 bzero(&drany, sizeof(drany));
747 defrouter_delreq(&drany, 0);
752 * Nuke neighbor cache entries for the ifp.
753 * Note that rt->rt_ifp may not be the same as ifp,
754 * due to KAME goto ours hack. See RTM_RESOLVE case in
755 * nd6_rtrequest(), and ip6_input().
757 ln = llinfo_nd6.ln_next;
758 while (ln && ln != &llinfo_nd6) {
760 struct sockaddr_dl *sdl;
764 if (rt && rt->rt_gateway &&
765 rt->rt_gateway->sa_family == AF_LINK) {
766 sdl = (struct sockaddr_dl *)rt->rt_gateway;
767 if (sdl->sdl_index == ifp->if_index)
775 nd6_lookup(addr6, create, ifp)
776 struct in6_addr *addr6;
781 struct sockaddr_in6 sin6;
783 bzero(&sin6, sizeof(sin6));
784 sin6.sin6_len = sizeof(struct sockaddr_in6);
785 sin6.sin6_family = AF_INET6;
786 sin6.sin6_addr = *addr6;
787 rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL);
789 if ((rt->rt_flags & RTF_LLINFO) == 0 && create) {
791 * This is the case for the default route.
792 * If we want to create a neighbor cache for the
793 * address, we should free the route for the
794 * destination and allocate an interface route.
805 * If no route is available and create is set,
806 * we allocate a host route for the destination
807 * and treat it like an interface route.
808 * This hack is necessary for a neighbor which can't
809 * be covered by our own prefix.
812 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
817 * Create a new route. RTF_LLINFO is necessary
818 * to create a Neighbor Cache entry for the
819 * destination in nd6_rtrequest which will be
820 * called in rtrequest via ifa->ifa_rtrequest.
822 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
823 ifa->ifa_addr, (struct sockaddr *)&all1_sa,
824 (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
825 ~RTF_CLONING, &rt)) != 0) {
827 "nd6_lookup: failed to add route for a "
828 "neighbor(%s), errno=%d\n",
829 ip6_sprintf(addr6), e);
835 struct llinfo_nd6 *ln =
836 (struct llinfo_nd6 *)rt->rt_llinfo;
837 ln->ln_state = ND6_LLINFO_NOSTATE;
845 * Validation for the entry.
846 * Note that the check for rt_llinfo is necessary because a cloned
847 * route from a parent route that has the L flag (e.g. the default
848 * route to a p2p interface) may have the flag, too, while the
849 * destination is not actually a neighbor.
850 * XXX: we can't use rt->rt_ifp to check for the interface, since
851 * it might be the loopback interface if the entry is for our
852 * own address on a non-loopback interface. Instead, we should
853 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
856 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
857 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
858 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
861 "nd6_lookup: failed to lookup %s (if = %s)\n",
863 ifp ? if_name(ifp) : "unspec");
864 /* xxx more logs... kazu */
869 RT_UNLOCK(rt); /* XXX not ready to return rt locked */
874 * Test whether a given IPv6 address is a neighbor or not, ignoring
875 * the actual neighbor cache. The neighbor cache is ignored in order
876 * to not reenter the routing code from within itself.
879 nd6_is_new_addr_neighbor(addr, ifp)
880 struct sockaddr_in6 *addr;
883 struct nd_prefix *pr;
886 * A link-local address is always a neighbor.
887 * XXX: we should use the sin6_scope_id field rather than the embedded
889 * XXX: a link does not necessarily specify a single interface.
891 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
892 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
896 * If the address matches one of our addresses,
897 * it should be a neighbor.
898 * If the address matches one of our on-link prefixes, it should be a
901 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
902 if (pr->ndpr_ifp != ifp)
905 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
908 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
909 &addr->sin6_addr, &pr->ndpr_mask))
914 * If the default router list is empty, all addresses are regarded
915 * as on-link, and thus, as a neighbor.
916 * XXX: we restrict the condition to hosts, because routers usually do
917 * not have the "default router list".
919 if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
920 nd6_defifindex == ifp->if_index) {
929 * Detect if a given IPv6 address identifies a neighbor on a given link.
930 * XXX: should take care of the destination of a p2p link?
933 nd6_is_addr_neighbor(addr, ifp)
934 struct sockaddr_in6 *addr;
938 if (nd6_is_new_addr_neighbor(addr, ifp))
942 * Even if the address matches none of our addresses, it might be
943 * in the neighbor cache.
945 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
952 * Free an nd6 llinfo entry.
958 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
959 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
960 struct nd_defrouter *dr;
963 * we used to have pfctlinput(PRC_HOSTDEAD) here.
964 * even though it is not harmful, it was not really necessary.
967 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
970 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
973 if (ln->ln_router || dr) {
975 * rt6_flush must be called whether or not the neighbor
976 * is in the Default Router List.
977 * See a corresponding comment in nd6_na_input().
979 rt6_flush(&in6, rt->rt_ifp);
984 * Unreachablity of a router might affect the default
985 * router selection and on-link detection of advertised
990 * Temporarily fake the state to choose a new default
991 * router and to perform on-link determination of
992 * prefixes correctly.
993 * Below the state will be set correctly,
994 * or the entry itself will be deleted.
996 ln->ln_state = ND6_LLINFO_INCOMPLETE;
999 * Since defrouter_select() does not affect the
1000 * on-link determination and MIP6 needs the check
1001 * before the default router selection, we perform
1004 pfxlist_onlink_check();
1006 if (dr == TAILQ_FIRST(&nd_defrouter)) {
1008 * It is used as the current default router,
1009 * so we have to move it to the end of the
1010 * list and choose a new one.
1011 * XXX: it is not very efficient if this is
1014 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
1015 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
1024 * Before deleting the entry, remember the next entry as the
1025 * return value. We need this because pfxlist_onlink_check() above
1026 * might have freed other entries (particularly the old next entry) as
1027 * a side effect (XXX).
1032 * Detach the route from the routing tree and the list of neighbor
1033 * caches, and disable the route entry not to be used in already
1036 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
1037 rt_mask(rt), 0, (struct rtentry **)0);
1043 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1045 * XXX cost-effective metods?
1048 nd6_nud_hint(rt, dst6, force)
1050 struct in6_addr *dst6;
1053 struct llinfo_nd6 *ln;
1056 * If the caller specified "rt", use that. Otherwise, resolve the
1057 * routing table by supplied "dst6".
1062 if (!(rt = nd6_lookup(dst6, 0, NULL)))
1066 if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
1067 (rt->rt_flags & RTF_LLINFO) == 0 ||
1068 !rt->rt_llinfo || !rt->rt_gateway ||
1069 rt->rt_gateway->sa_family != AF_LINK) {
1070 /* This is not a host route. */
1074 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1075 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1079 * if we get upper-layer reachability confirmation many times,
1080 * it is possible we have false information.
1084 if (ln->ln_byhint > nd6_maxnudhint)
1088 ln->ln_state = ND6_LLINFO_REACHABLE;
1090 ln->ln_expire = time_second +
1091 ND_IFINFO(rt->rt_ifp)->reachable;
1095 nd6_rtrequest(req, rt, info)
1098 struct rt_addrinfo *info; /* xxx unused */
1100 struct sockaddr *gate = rt->rt_gateway;
1101 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1102 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1103 struct ifnet *ifp = rt->rt_ifp;
1108 if ((rt->rt_flags & RTF_GATEWAY) != 0)
1111 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1113 * This is probably an interface direct route for a link
1114 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1115 * We do not need special treatment below for such a route.
1116 * Moreover, the RTF_LLINFO flag which would be set below
1117 * would annoy the ndp(8) command.
1122 if (req == RTM_RESOLVE &&
1123 (nd6_need_cache(ifp) == 0 || /* stf case */
1124 !nd6_is_new_addr_neighbor((struct sockaddr_in6 *)rt_key(rt),
1127 * FreeBSD and BSD/OS often make a cloned host route based
1128 * on a less-specific route (e.g. the default route).
1129 * If the less specific route does not have a "gateway"
1130 * (this is the case when the route just goes to a p2p or an
1131 * stf interface), we'll mistakenly make a neighbor cache for
1132 * the host route, and will see strange neighbor solicitation
1133 * for the corresponding destination. In order to avoid the
1134 * confusion, we check if the destination of the route is
1135 * a neighbor in terms of neighbor discovery, and stop the
1136 * process if not. Additionally, we remove the LLINFO flag
1137 * so that ndp(8) will not try to get the neighbor information
1138 * of the destination.
1140 rt->rt_flags &= ~RTF_LLINFO;
1147 * There is no backward compatibility :)
1149 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1150 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1151 * rt->rt_flags |= RTF_CLONING;
1153 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1155 * Case 1: This route should come from
1156 * a route to interface. RTF_LLINFO flag is set
1157 * for a host route whose destination should be
1158 * treated as on-link.
1160 rt_setgate(rt, rt_key(rt),
1161 (struct sockaddr *)&null_sdl);
1162 gate = rt->rt_gateway;
1163 SDL(gate)->sdl_type = ifp->if_type;
1164 SDL(gate)->sdl_index = ifp->if_index;
1166 ln->ln_expire = time_second;
1167 if (ln && ln->ln_expire == 0) {
1168 /* kludge for desktops */
1171 if ((rt->rt_flags & RTF_CLONING) != 0)
1175 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1176 * We don't do that here since llinfo is not ready yet.
1178 * There are also couple of other things to be discussed:
1179 * - unsolicited NA code needs improvement beforehand
1180 * - RFC2461 says we MAY send multicast unsolicited NA
1181 * (7.2.6 paragraph 4), however, it also says that we
1182 * SHOULD provide a mechanism to prevent multicast NA storm.
1183 * we don't have anything like it right now.
1184 * note that the mechanism needs a mutual agreement
1185 * between proxies, which means that we need to implement
1186 * a new protocol, or a new kludge.
1187 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1188 * we need to check ip6forwarding before sending it.
1189 * (or should we allow proxy ND configuration only for
1190 * routers? there's no mention about proxy ND from hosts)
1193 /* XXX it does not work */
1194 if (rt->rt_flags & RTF_ANNOUNCE)
1196 &SIN6(rt_key(rt))->sin6_addr,
1197 &SIN6(rt_key(rt))->sin6_addr,
1198 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1203 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1205 * Address resolution isn't necessary for a point to
1206 * point link, so we can skip this test for a p2p link.
1208 if (gate->sa_family != AF_LINK ||
1209 gate->sa_len < sizeof(null_sdl)) {
1211 "nd6_rtrequest: bad gateway value: %s\n",
1215 SDL(gate)->sdl_type = ifp->if_type;
1216 SDL(gate)->sdl_index = ifp->if_index;
1219 break; /* This happens on a route change */
1221 * Case 2: This route may come from cloning, or a manual route
1222 * add with a LL address.
1224 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1225 rt->rt_llinfo = (caddr_t)ln;
1227 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1232 bzero(ln, sizeof(*ln));
1234 /* this is required for "ndp" command. - shin */
1235 if (req == RTM_ADD) {
1237 * gate should have some valid AF_LINK entry,
1238 * and ln->ln_expire should have some lifetime
1239 * which is specified by ndp command.
1241 ln->ln_state = ND6_LLINFO_REACHABLE;
1245 * When req == RTM_RESOLVE, rt is created and
1246 * initialized in rtrequest(), so rt_expire is 0.
1248 ln->ln_state = ND6_LLINFO_NOSTATE;
1249 ln->ln_expire = time_second;
1251 rt->rt_flags |= RTF_LLINFO;
1252 ln->ln_next = llinfo_nd6.ln_next;
1253 llinfo_nd6.ln_next = ln;
1254 ln->ln_prev = &llinfo_nd6;
1255 ln->ln_next->ln_prev = ln;
1258 * check if rt_key(rt) is one of my address assigned
1261 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1262 &SIN6(rt_key(rt))->sin6_addr);
1264 caddr_t macp = nd6_ifptomac(ifp);
1266 ln->ln_state = ND6_LLINFO_REACHABLE;
1269 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1270 SDL(gate)->sdl_alen = ifp->if_addrlen;
1272 if (nd6_useloopback) {
1273 rt->rt_ifp = &loif[0]; /* XXX */
1275 * Make sure rt_ifa be equal to the ifaddr
1276 * corresponding to the address.
1277 * We need this because when we refer
1278 * rt_ifa->ia6_flags in ip6_input, we assume
1279 * that the rt_ifa points to the address instead
1280 * of the loopback address.
1282 if (ifa != rt->rt_ifa) {
1283 IFAFREE(rt->rt_ifa);
1288 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1290 ln->ln_state = ND6_LLINFO_REACHABLE;
1293 /* join solicited node multicast for proxy ND */
1294 if (ifp->if_flags & IFF_MULTICAST) {
1295 struct in6_addr llsol;
1298 llsol = SIN6(rt_key(rt))->sin6_addr;
1299 llsol.s6_addr16[0] = htons(0xff02);
1300 llsol.s6_addr16[1] = htons(ifp->if_index);
1301 llsol.s6_addr32[1] = 0;
1302 llsol.s6_addr32[2] = htonl(1);
1303 llsol.s6_addr8[12] = 0xff;
1305 if (!in6_addmulti(&llsol, ifp, &error)) {
1306 nd6log((LOG_ERR, "%s: failed to join "
1307 "%s (errno=%d)\n", if_name(ifp),
1308 ip6_sprintf(&llsol), error));
1317 /* leave from solicited node multicast for proxy ND */
1318 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1319 (ifp->if_flags & IFF_MULTICAST) != 0) {
1320 struct in6_addr llsol;
1321 struct in6_multi *in6m;
1323 llsol = SIN6(rt_key(rt))->sin6_addr;
1324 llsol.s6_addr16[0] = htons(0xff02);
1325 llsol.s6_addr16[1] = htons(ifp->if_index);
1326 llsol.s6_addr32[1] = 0;
1327 llsol.s6_addr32[2] = htonl(1);
1328 llsol.s6_addr8[12] = 0xff;
1330 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1335 ln->ln_next->ln_prev = ln->ln_prev;
1336 ln->ln_prev->ln_next = ln->ln_next;
1339 rt->rt_flags &= ~RTF_LLINFO;
1341 m_freem(ln->ln_hold);
1347 nd6_ioctl(cmd, data, ifp)
1352 struct in6_drlist *drl = (struct in6_drlist *)data;
1353 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1354 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1355 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1356 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1357 struct nd_defrouter *dr, any;
1358 struct nd_prefix *pr;
1360 int i = 0, error = 0;
1364 case SIOCGDRLST_IN6:
1366 * obsolete API, use sysctl under net.inet6.icmp6
1368 bzero(drl, sizeof(*drl));
1370 dr = TAILQ_FIRST(&nd_defrouter);
1371 while (dr && i < DRLSTSIZ) {
1372 drl->defrouter[i].rtaddr = dr->rtaddr;
1373 in6_clearscope(&drl->defrouter[i].rtaddr);
1375 drl->defrouter[i].flags = dr->flags;
1376 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1377 drl->defrouter[i].expire = dr->expire;
1378 drl->defrouter[i].if_index = dr->ifp->if_index;
1380 dr = TAILQ_NEXT(dr, dr_entry);
1384 case SIOCGPRLST_IN6:
1386 * obsolete API, use sysctl under net.inet6.icmp6
1388 * XXX the structure in6_prlist was changed in backward-
1389 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1390 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1393 * XXX meaning of fields, especialy "raflags", is very
1394 * differnet between RA prefix list and RR/static prefix list.
1395 * how about separating ioctls into two?
1397 bzero(oprl, sizeof(*oprl));
1399 pr = nd_prefix.lh_first;
1400 while (pr && i < PRLSTSIZ) {
1401 struct nd_pfxrouter *pfr;
1404 (void)in6_embedscope(&oprl->prefix[i].prefix,
1405 &pr->ndpr_prefix, NULL, NULL);
1406 oprl->prefix[i].raflags = pr->ndpr_raf;
1407 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1408 oprl->prefix[i].vltime = pr->ndpr_vltime;
1409 oprl->prefix[i].pltime = pr->ndpr_pltime;
1410 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1411 oprl->prefix[i].expire = pr->ndpr_expire;
1413 pfr = pr->ndpr_advrtrs.lh_first;
1417 #define RTRADDR oprl->prefix[i].advrtr[j]
1418 RTRADDR = pfr->router->rtaddr;
1419 in6_clearscope(&RTRADDR);
1423 pfr = pfr->pfr_next;
1425 oprl->prefix[i].advrtrs = j;
1426 oprl->prefix[i].origin = PR_ORIG_RA;
1434 case OSIOCGIFINFO_IN6:
1435 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1436 bzero(&ndi->ndi, sizeof(ndi->ndi));
1437 ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1438 ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1439 ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1440 ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1441 ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1442 ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1443 ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1444 ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1446 case SIOCGIFINFO_IN6:
1447 ndi->ndi = *ND_IFINFO(ifp);
1448 ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1450 case SIOCSIFINFO_FLAGS:
1451 ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1453 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1454 /* flush default router list */
1456 * xxx sumikawa: should not delete route if default
1457 * route equals to the top of default router list
1459 bzero(&any, sizeof(any));
1460 defrouter_delreq(&any, 0);
1462 /* xxx sumikawa: flush prefix list */
1464 case SIOCSPFXFLUSH_IN6:
1466 /* flush all the prefix advertised by routers */
1467 struct nd_prefix *pr, *next;
1470 for (pr = nd_prefix.lh_first; pr; pr = next) {
1471 struct in6_ifaddr *ia, *ia_next;
1473 next = pr->ndpr_next;
1475 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1478 /* do we really have to remove addresses as well? */
1479 for (ia = in6_ifaddr; ia; ia = ia_next) {
1480 /* ia might be removed. keep the next ptr. */
1481 ia_next = ia->ia_next;
1483 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1486 if (ia->ia6_ndpr == pr)
1487 in6_purgeaddr(&ia->ia_ifa);
1494 case SIOCSRTRFLUSH_IN6:
1496 /* flush all the default routers */
1497 struct nd_defrouter *dr, *next;
1500 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1502 * The first entry of the list may be stored in
1503 * the routing table, so we'll delete it later.
1505 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1506 next = TAILQ_NEXT(dr, dr_entry);
1509 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1514 case SIOCGNBRINFO_IN6:
1516 struct llinfo_nd6 *ln;
1517 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1520 * XXX: KAME specific hack for scoped addresses
1521 * XXXX: for other scopes than link-local?
1523 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1524 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1525 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1528 *idp = htons(ifp->if_index);
1532 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1537 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1538 nbi->state = ln->ln_state;
1539 nbi->asked = ln->ln_asked;
1540 nbi->isrouter = ln->ln_router;
1541 nbi->expire = ln->ln_expire;
1546 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1547 ndif->ifindex = nd6_defifindex;
1549 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1550 return (nd6_setdefaultiface(ndif->ifindex));
1556 * Create neighbor cache entry and cache link-layer address,
1557 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1560 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1562 struct in6_addr *from;
1565 int type; /* ICMP6 type */
1566 int code; /* type dependent information */
1568 struct rtentry *rt = NULL;
1569 struct llinfo_nd6 *ln = NULL;
1571 struct sockaddr_dl *sdl = NULL;
1578 panic("ifp == NULL in nd6_cache_lladdr");
1580 panic("from == NULL in nd6_cache_lladdr");
1582 /* nothing must be updated for unspecified address */
1583 if (IN6_IS_ADDR_UNSPECIFIED(from))
1587 * Validation about ifp->if_addrlen and lladdrlen must be done in
1590 * XXX If the link does not have link-layer adderss, what should
1591 * we do? (ifp->if_addrlen == 0)
1592 * Spec says nothing in sections for RA, RS and NA. There's small
1593 * description on it in NS section (RFC 2461 7.2.3).
1596 rt = nd6_lookup(from, 0, ifp);
1599 /* nothing must be done if there's no lladdr */
1600 if (!lladdr || !lladdrlen)
1604 rt = nd6_lookup(from, 1, ifp);
1607 /* do nothing if static ndp is set */
1608 if (rt->rt_flags & RTF_STATIC)
1615 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1620 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1623 if (!rt->rt_gateway)
1625 if (rt->rt_gateway->sa_family != AF_LINK)
1627 sdl = SDL(rt->rt_gateway);
1629 olladdr = (sdl->sdl_alen) ? 1 : 0;
1630 if (olladdr && lladdr) {
1631 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1639 * newentry olladdr lladdr llchange (*=record)
1642 * 0 n y -- (3) * STALE
1644 * 0 y y y (5) * STALE
1645 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1646 * 1 -- y -- (7) * STALE
1649 if (lladdr) { /* (3-5) and (7) */
1651 * Record source link-layer address
1652 * XXX is it dependent to ifp->if_type?
1654 sdl->sdl_alen = ifp->if_addrlen;
1655 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1659 if ((!olladdr && lladdr) || /* (3) */
1660 (olladdr && lladdr && llchange)) { /* (5) */
1662 newstate = ND6_LLINFO_STALE;
1663 } else /* (1-2,4) */
1667 if (!lladdr) /* (6) */
1668 newstate = ND6_LLINFO_NOSTATE;
1670 newstate = ND6_LLINFO_STALE;
1675 * Update the state of the neighbor cache.
1677 ln->ln_state = newstate;
1679 if (ln->ln_state == ND6_LLINFO_STALE) {
1681 * XXX: since nd6_output() below will cause
1682 * state tansition to DELAY and reset the timer,
1683 * we must set the timer now, although it is actually
1686 ln->ln_expire = time_second + nd6_gctimer;
1690 * we assume ifp is not a p2p here, so just
1691 * set the 2nd argument as the 1st one.
1693 nd6_output(ifp, ifp, ln->ln_hold,
1694 (struct sockaddr_in6 *)rt_key(rt), rt);
1697 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1698 /* probe right away */
1699 ln->ln_expire = time_second;
1704 * ICMP6 type dependent behavior.
1706 * NS: clear IsRouter if new entry
1707 * RS: clear IsRouter
1708 * RA: set IsRouter if there's lladdr
1709 * redir: clear IsRouter if new entry
1712 * The spec says that we must set IsRouter in the following cases:
1713 * - If lladdr exist, set IsRouter. This means (1-5).
1714 * - If it is old entry (!newentry), set IsRouter. This means (7).
1715 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1716 * A quetion arises for (1) case. (1) case has no lladdr in the
1717 * neighbor cache, this is similar to (6).
1718 * This case is rare but we figured that we MUST NOT set IsRouter.
1720 * newentry olladdr lladdr llchange NS RS RA redir
1722 * 0 n n -- (1) c ? s
1723 * 0 y n -- (2) c s s
1724 * 0 n y -- (3) c s s
1727 * 1 -- n -- (6) c c c s
1728 * 1 -- y -- (7) c c s c s
1732 switch (type & 0xff) {
1733 case ND_NEIGHBOR_SOLICIT:
1735 * New entry must have is_router flag cleared.
1737 if (is_newentry) /* (6-7) */
1742 * If the icmp is a redirect to a better router, always set the
1743 * is_router flag. Otherwise, if the entry is newly created,
1744 * clear the flag. [RFC 2461, sec 8.3]
1746 if (code == ND_REDIRECT_ROUTER)
1748 else if (is_newentry) /* (6-7) */
1751 case ND_ROUTER_SOLICIT:
1753 * is_router flag must always be cleared.
1757 case ND_ROUTER_ADVERT:
1759 * Mark an entry with lladdr as a router.
1761 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1762 (is_newentry && lladdr)) { /* (7) */
1769 * When the link-layer address of a router changes, select the
1770 * best router again. In particular, when the neighbor entry is newly
1771 * created, it might affect the selection policy.
1772 * Question: can we restrict the first condition to the "is_newentry"
1774 * XXX: when we hear an RA from a new router with the link-layer
1775 * address option, defrouter_select() is called twice, since
1776 * defrtrlist_update called the function as well. However, I believe
1777 * we can compromise the overhead, since it only happens the first
1779 * XXX: although defrouter_select() should not have a bad effect
1780 * for those are not autoconfigured hosts, we explicitly avoid such
1783 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1790 nd6_slowtimo(ignored_arg)
1794 struct nd_ifinfo *nd6if;
1797 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1798 nd6_slowtimo, NULL);
1800 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1801 nd6if = ND_IFINFO(ifp);
1802 if (nd6if->basereachable && /* already initialized */
1803 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1805 * Since reachable time rarely changes by router
1806 * advertisements, we SHOULD insure that a new random
1807 * value gets recomputed at least once every few hours.
1810 nd6if->recalctm = nd6_recalc_reachtm_interval;
1811 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1818 #define senderr(e) { error = (e); goto bad;}
1820 nd6_output(ifp, origifp, m0, dst, rt0)
1822 struct ifnet *origifp;
1824 struct sockaddr_in6 *dst;
1825 struct rtentry *rt0;
1827 struct mbuf *m = m0;
1828 struct rtentry *rt = rt0;
1829 struct sockaddr_in6 *gw6 = NULL;
1830 struct llinfo_nd6 *ln = NULL;
1833 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1836 if (nd6_need_cache(ifp) == 0)
1840 * next hop determination. This routine is derived from ether_outpout.
1844 if ((rt->rt_flags & RTF_UP) == 0) {
1845 rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL);
1849 if (rt->rt_ifp != ifp)
1851 * XXX maybe we should update ifp too,
1852 * but the original code didn't and I
1853 * don't know what is correct here.
1857 senderr(EHOSTUNREACH);
1860 if (rt->rt_flags & RTF_GATEWAY) {
1861 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1864 * We skip link-layer address resolution and NUD
1865 * if the gateway is not a neighbor from ND point
1866 * of view, regardless of the value of nd_ifinfo.flags.
1867 * The second condition is a bit tricky; we skip
1868 * if the gateway is our own address, which is
1869 * sometimes used to install a route to a p2p link.
1871 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1872 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1874 * We allow this kind of tricky route only
1875 * when the outgoing interface is p2p.
1876 * XXX: we may need a more generic rule here.
1878 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1879 senderr(EHOSTUNREACH);
1884 if (rt->rt_gwroute == 0)
1886 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1888 rtfree(rt); rt = rt0;
1890 rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
1891 if ((rt = rt->rt_gwroute) == 0)
1892 senderr(EHOSTUNREACH);
1899 * Address resolution or Neighbor Unreachability Detection
1901 * At this point, the destination of the packet must be a unicast
1902 * or an anycast address(i.e. not a multicast).
1905 /* Look up the neighbor cache for the nexthop */
1906 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1907 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1910 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1911 * the condition below is not very efficient. But we believe
1912 * it is tolerable, because this should be a rare case.
1914 if (nd6_is_addr_neighbor(dst, ifp) &&
1915 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1916 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1919 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1920 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1922 "nd6_output: can't allocate llinfo for %s "
1924 ip6_sprintf(&dst->sin6_addr), ln, rt);
1925 senderr(EIO); /* XXX: good error? */
1928 goto sendpkt; /* send anyway */
1931 /* We don't have to do link-layer address resolution on a p2p link. */
1932 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1933 ln->ln_state < ND6_LLINFO_REACHABLE) {
1934 ln->ln_state = ND6_LLINFO_STALE;
1935 ln->ln_expire = time_second + nd6_gctimer;
1939 * The first time we send a packet to a neighbor whose entry is
1940 * STALE, we have to change the state to DELAY and a sets a timer to
1941 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1942 * neighbor unreachability detection on expiration.
1945 if (ln->ln_state == ND6_LLINFO_STALE) {
1947 ln->ln_state = ND6_LLINFO_DELAY;
1948 ln->ln_expire = time_second + nd6_delay;
1952 * If the neighbor cache entry has a state other than INCOMPLETE
1953 * (i.e. its link-layer address is already resolved), just
1956 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1960 * There is a neighbor cache entry, but no ethernet address
1961 * response yet. Replace the held mbuf (if any) with this
1964 * This code conforms to the rate-limiting rule described in Section
1965 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
1968 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1969 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1971 m_freem(ln->ln_hold);
1973 if (ln->ln_expire) {
1974 if (ln->ln_asked < nd6_mmaxtries &&
1975 ln->ln_expire < time_second) {
1977 ln->ln_expire = time_second +
1978 ND_IFINFO(ifp)->retrans / 1000;
1979 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1986 /* clean ipsec history once it goes out of the node */
1991 mac_create_mbuf_linklayer(ifp, m);
1993 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1994 return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
1997 return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
2011 * XXX: we currently do not make neighbor cache on any interface
2012 * other than ARCnet, Ethernet, FDDI and GIF.
2015 * - unidirectional tunnels needs no ND
2017 switch (ifp->if_type) {
2025 #ifdef IFT_IEEE80211
2031 case IFT_GIF: /* XXX need more cases? */
2039 nd6_storelladdr(ifp, rt0, m, dst, desten)
2041 struct rtentry *rt0;
2043 struct sockaddr *dst;
2047 struct sockaddr_dl *sdl;
2050 if (m->m_flags & M_MCAST) {
2051 switch (ifp->if_type) {
2057 #ifdef IFT_IEEE80211
2061 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2066 * netbsd can use if_broadcastaddr, but we don't do so
2067 * to reduce # of ifdef.
2069 for (i = 0; i < ifp->if_addrlen; i++)
2077 return (EAFNOSUPPORT);
2081 i = rt_check(&rt, &rt0, dst);
2088 /* this could happen, if we could not allocate memory */
2092 if (rt->rt_gateway->sa_family != AF_LINK) {
2093 printf("nd6_storelladdr: something odd happens\n");
2097 sdl = SDL(rt->rt_gateway);
2098 if (sdl->sdl_alen == 0) {
2099 /* this should be impossible, but we bark here for debugging */
2100 printf("nd6_storelladdr: sdl_alen == 0\n");
2105 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2109 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2110 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2112 SYSCTL_DECL(_net_inet6_icmp6);
2114 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2115 CTLFLAG_RD, nd6_sysctl_drlist, "");
2116 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2117 CTLFLAG_RD, nd6_sysctl_prlist, "");
2120 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2124 struct in6_defrouter *d, *de;
2125 struct nd_defrouter *dr;
2131 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2132 dr = TAILQ_NEXT(dr, dr_entry)) {
2133 d = (struct in6_defrouter *)buf;
2134 de = (struct in6_defrouter *)(buf + sizeof(buf));
2137 bzero(d, sizeof(*d));
2138 d->rtaddr.sin6_family = AF_INET6;
2139 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2140 if (in6_recoverscope(&d->rtaddr, &dr->rtaddr,
2144 "default router list (%s)\n",
2145 ip6_sprintf(&dr->rtaddr));
2146 d->flags = dr->flags;
2147 d->rtlifetime = dr->rtlifetime;
2148 d->expire = dr->expire;
2149 d->if_index = dr->ifp->if_index;
2151 panic("buffer too short");
2153 error = SYSCTL_OUT(req, buf, sizeof(*d));
2162 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2166 struct in6_prefix *p, *pe;
2167 struct nd_prefix *pr;
2173 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2176 struct sockaddr_in6 *sin6, *s6;
2177 struct nd_pfxrouter *pfr;
2179 p = (struct in6_prefix *)buf;
2180 pe = (struct in6_prefix *)(buf + sizeof(buf));
2183 bzero(p, sizeof(*p));
2184 sin6 = (struct sockaddr_in6 *)(p + 1);
2186 p->prefix = pr->ndpr_prefix;
2187 if (in6_recoverscope(&p->prefix,
2188 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2190 "scope error in prefix list (%s)\n",
2191 ip6_sprintf(&p->prefix.sin6_addr));
2192 p->raflags = pr->ndpr_raf;
2193 p->prefixlen = pr->ndpr_plen;
2194 p->vltime = pr->ndpr_vltime;
2195 p->pltime = pr->ndpr_pltime;
2196 p->if_index = pr->ndpr_ifp->if_index;
2197 p->expire = pr->ndpr_expire;
2198 p->refcnt = pr->ndpr_refcnt;
2199 p->flags = pr->ndpr_stateflags;
2200 p->origin = PR_ORIG_RA;
2202 for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2203 pfr = pfr->pfr_next) {
2204 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2208 s6 = &sin6[advrtrs];
2209 bzero(s6, sizeof(*s6));
2210 s6->sin6_family = AF_INET6;
2211 s6->sin6_len = sizeof(*sin6);
2212 if (in6_recoverscope(s6, &pfr->router->rtaddr,
2213 pfr->router->ifp) != 0)
2216 "prefix list (%s)\n",
2217 ip6_sprintf(&pfr->router->rtaddr));
2220 p->advrtrs = advrtrs;
2222 panic("buffer too short");
2224 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2225 error = SYSCTL_OUT(req, buf, advance);