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/scope6_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(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
210 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
213 ndi->maxmtu = ifp->if_mtu;
218 * Decreasing the interface MTU under IPV6 minimum MTU may cause
219 * undesirable situation. We thus notify the operator of the change
220 * explicitly. The check for omaxmtu is necessary to restrict the
221 * log to the case of changing the MTU, not initializing it.
223 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
224 log(LOG_NOTICE, "nd6_setmtu0: "
225 "new link MTU on %s (%lu) is too small for IPv6\n",
226 if_name(ifp), (unsigned long)ndi->maxmtu);
229 if (ndi->maxmtu > in6_maxmtu)
230 in6_setmaxmtu(); /* check all interfaces just in case */
236 nd6_option_init(opt, icmp6len, ndopts)
239 union nd_opts *ndopts;
242 bzero(ndopts, sizeof(*ndopts));
243 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
245 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
248 ndopts->nd_opts_done = 1;
249 ndopts->nd_opts_search = NULL;
254 * Take one ND option.
258 union nd_opts *ndopts;
260 struct nd_opt_hdr *nd_opt;
264 panic("ndopts == NULL in nd6_option");
265 if (ndopts->nd_opts_last == NULL)
266 panic("uninitialized ndopts in nd6_option");
267 if (ndopts->nd_opts_search == NULL)
269 if (ndopts->nd_opts_done)
272 nd_opt = ndopts->nd_opts_search;
274 /* make sure nd_opt_len is inside the buffer */
275 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
276 bzero(ndopts, sizeof(*ndopts));
280 olen = nd_opt->nd_opt_len << 3;
283 * Message validation requires that all included
284 * options have a length that is greater than zero.
286 bzero(ndopts, sizeof(*ndopts));
290 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
291 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
292 /* option overruns the end of buffer, invalid */
293 bzero(ndopts, sizeof(*ndopts));
295 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
296 /* reached the end of options chain */
297 ndopts->nd_opts_done = 1;
298 ndopts->nd_opts_search = NULL;
304 * Parse multiple ND options.
305 * This function is much easier to use, for ND routines that do not need
306 * multiple options of the same type.
310 union nd_opts *ndopts;
312 struct nd_opt_hdr *nd_opt;
316 panic("ndopts == NULL in nd6_options");
317 if (ndopts->nd_opts_last == NULL)
318 panic("uninitialized ndopts in nd6_options");
319 if (ndopts->nd_opts_search == NULL)
323 nd_opt = nd6_option(ndopts);
324 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
326 * Message validation requires that all included
327 * options have a length that is greater than zero.
329 icmp6stat.icp6s_nd_badopt++;
330 bzero(ndopts, sizeof(*ndopts));
337 switch (nd_opt->nd_opt_type) {
338 case ND_OPT_SOURCE_LINKADDR:
339 case ND_OPT_TARGET_LINKADDR:
341 case ND_OPT_REDIRECTED_HEADER:
342 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
344 "duplicated ND6 option found (type=%d)\n",
345 nd_opt->nd_opt_type));
348 ndopts->nd_opt_array[nd_opt->nd_opt_type]
352 case ND_OPT_PREFIX_INFORMATION:
353 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
354 ndopts->nd_opt_array[nd_opt->nd_opt_type]
357 ndopts->nd_opts_pi_end =
358 (struct nd_opt_prefix_info *)nd_opt;
362 * Unknown options must be silently ignored,
363 * to accomodate future extension to the protocol.
366 "nd6_options: unsupported option %d - "
367 "option ignored\n", nd_opt->nd_opt_type));
372 if (i > nd6_maxndopt) {
373 icmp6stat.icp6s_nd_toomanyopt++;
374 nd6log((LOG_INFO, "too many loop in nd opt\n"));
378 if (ndopts->nd_opts_done)
386 * ND6 timer routine to expire default route list and prefix list
389 nd6_timer(ignored_arg)
393 struct llinfo_nd6 *ln;
394 struct nd_defrouter *dr;
395 struct nd_prefix *pr;
397 struct in6_ifaddr *ia6, *nia6;
398 struct in6_addrlifetime *lt6;
401 callout_reset(&nd6_timer_ch, nd6_prune * hz,
404 ln = llinfo_nd6.ln_next;
405 while (ln && ln != &llinfo_nd6) {
407 struct sockaddr_in6 *dst;
408 struct llinfo_nd6 *next = ln->ln_next;
409 /* XXX: used for the DELAY case only: */
410 struct nd_ifinfo *ndi = NULL;
412 if ((rt = ln->ln_rt) == NULL) {
416 if ((ifp = rt->rt_ifp) == NULL) {
420 ndi = ND_IFINFO(ifp);
421 dst = (struct sockaddr_in6 *)rt_key(rt);
423 if (ln->ln_expire > time_second) {
430 panic("rt=0 in nd6_timer(ln=%p)", ln);
431 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
432 panic("rt_llinfo(%p) is not equal to ln(%p)",
435 panic("dst=0 in nd6_timer(ln=%p)", ln);
437 switch (ln->ln_state) {
438 case ND6_LLINFO_INCOMPLETE:
439 if (ln->ln_asked < nd6_mmaxtries) {
441 ln->ln_expire = time_second +
442 ND_IFINFO(ifp)->retrans / 1000;
443 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
446 struct mbuf *m = ln->ln_hold;
449 * assuming every packet in ln_hold has
453 icmp6_error2(m, ICMP6_DST_UNREACH,
454 ICMP6_DST_UNREACH_ADDR, 0,
460 case ND6_LLINFO_REACHABLE:
462 ln->ln_state = ND6_LLINFO_STALE;
463 ln->ln_expire = time_second + nd6_gctimer;
467 case ND6_LLINFO_STALE:
468 /* Garbage Collection(RFC 2461 5.3) */
473 case ND6_LLINFO_DELAY:
474 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
477 ln->ln_state = ND6_LLINFO_PROBE;
478 ln->ln_expire = time_second +
480 nd6_ns_output(ifp, &dst->sin6_addr,
484 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
485 ln->ln_expire = time_second + nd6_gctimer;
488 case ND6_LLINFO_PROBE:
489 if (ln->ln_asked < nd6_umaxtries) {
491 ln->ln_expire = time_second +
492 ND_IFINFO(ifp)->retrans / 1000;
493 nd6_ns_output(ifp, &dst->sin6_addr,
494 &dst->sin6_addr, ln, 0);
503 /* expire default router list */
504 dr = TAILQ_FIRST(&nd_defrouter);
506 if (dr->expire && dr->expire < time_second) {
507 struct nd_defrouter *t;
508 t = TAILQ_NEXT(dr, dr_entry);
512 dr = TAILQ_NEXT(dr, dr_entry);
517 * expire interface addresses.
518 * in the past the loop was inside prefix expiry processing.
519 * However, from a stricter speci-confrmance standpoint, we should
520 * rather separate address lifetimes and prefix lifetimes.
523 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
525 /* check address lifetime */
526 lt6 = &ia6->ia6_lifetime;
527 if (IFA6_IS_INVALID(ia6)) {
531 * If the expiring address is temporary, try
532 * regenerating a new one. This would be useful when
533 * we suspended a laptop PC, then turned it on after a
534 * period that could invalidate all temporary
535 * addresses. Although we may have to restart the
536 * loop (see below), it must be after purging the
537 * address. Otherwise, we'd see an infinite loop of
540 if (ip6_use_tempaddr &&
541 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
542 if (regen_tmpaddr(ia6) == 0)
546 in6_purgeaddr(&ia6->ia_ifa);
549 goto addrloop; /* XXX: see below */
551 if (IFA6_IS_DEPRECATED(ia6)) {
552 int oldflags = ia6->ia6_flags;
554 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
557 * If a temporary address has just become deprecated,
558 * regenerate a new one if possible.
560 if (ip6_use_tempaddr &&
561 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
562 (oldflags & IN6_IFF_DEPRECATED) == 0) {
564 if (regen_tmpaddr(ia6) == 0) {
566 * A new temporary address is
568 * XXX: this means the address chain
569 * has changed while we are still in
570 * the loop. Although the change
571 * would not cause disaster (because
572 * it's not a deletion, but an
573 * addition,) we'd rather restart the
574 * loop just for safety. Or does this
575 * significantly reduce performance??
582 * A new RA might have made a deprecated address
585 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
589 /* expire prefix list */
590 pr = nd_prefix.lh_first;
593 * check prefix lifetime.
594 * since pltime is just for autoconf, pltime processing for
595 * prefix is not necessary.
597 if (pr->ndpr_expire && pr->ndpr_expire < time_second) {
602 * address expiration and prefix expiration are
603 * separate. NEVER perform in6_purgeaddr here.
616 struct in6_ifaddr *ia6; /* deprecated/invalidated temporary address */
620 struct in6_ifaddr *public_ifa6 = NULL;
622 ifp = ia6->ia_ifa.ifa_ifp;
623 for (ifa = ifp->if_addrlist.tqh_first; ifa;
624 ifa = ifa->ifa_list.tqe_next) {
625 struct in6_ifaddr *it6;
627 if (ifa->ifa_addr->sa_family != AF_INET6)
630 it6 = (struct in6_ifaddr *)ifa;
632 /* ignore no autoconf addresses. */
633 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
636 /* ignore autoconf addresses with different prefixes. */
637 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
641 * Now we are looking at an autoconf address with the same
642 * prefix as ours. If the address is temporary and is still
643 * preferred, do not create another one. It would be rare, but
644 * could happen, for example, when we resume a laptop PC after
647 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
648 !IFA6_IS_DEPRECATED(it6)) {
654 * This is a public autoconf address that has the same prefix
655 * as ours. If it is preferred, keep it. We can't break the
656 * loop here, because there may be a still-preferred temporary
657 * address with the prefix.
659 if (!IFA6_IS_DEPRECATED(it6))
663 if (public_ifa6 != NULL) {
666 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
667 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
668 " tmp addr,errno=%d\n", e);
678 * Nuke neighbor cache/prefix/default router management table, right before
685 struct llinfo_nd6 *ln, *nln;
686 struct nd_defrouter *dr, *ndr, drany;
687 struct nd_prefix *pr, *npr;
689 /* Nuke default router list entries toward ifp */
690 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
692 * The first entry of the list may be stored in
693 * the routing table, so we'll delete it later.
695 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
696 ndr = TAILQ_NEXT(dr, dr_entry);
700 dr = TAILQ_FIRST(&nd_defrouter);
705 /* Nuke prefix list entries toward ifp */
706 for (pr = nd_prefix.lh_first; pr; pr = npr) {
708 if (pr->ndpr_ifp == ifp) {
710 * Previously, pr->ndpr_addr is removed as well,
711 * but I strongly believe we don't have to do it.
712 * nd6_purge() is only called from in6_ifdetach(),
713 * which removes all the associated interface addresses
715 * (jinmei@kame.net 20010129)
721 /* cancel default outgoing interface setting */
722 if (nd6_defifindex == ifp->if_index)
723 nd6_setdefaultiface(0);
725 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
726 /* refresh default router list */
727 bzero(&drany, sizeof(drany));
728 defrouter_delreq(&drany, 0);
733 * Nuke neighbor cache entries for the ifp.
734 * Note that rt->rt_ifp may not be the same as ifp,
735 * due to KAME goto ours hack. See RTM_RESOLVE case in
736 * nd6_rtrequest(), and ip6_input().
738 ln = llinfo_nd6.ln_next;
739 while (ln && ln != &llinfo_nd6) {
741 struct sockaddr_dl *sdl;
745 if (rt && rt->rt_gateway &&
746 rt->rt_gateway->sa_family == AF_LINK) {
747 sdl = (struct sockaddr_dl *)rt->rt_gateway;
748 if (sdl->sdl_index == ifp->if_index)
756 nd6_lookup(addr6, create, ifp)
757 struct in6_addr *addr6;
762 struct sockaddr_in6 sin6;
764 bzero(&sin6, sizeof(sin6));
765 sin6.sin6_len = sizeof(struct sockaddr_in6);
766 sin6.sin6_family = AF_INET6;
767 sin6.sin6_addr = *addr6;
768 rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL);
770 if ((rt->rt_flags & RTF_LLINFO) == 0 && create) {
772 * This is the case for the default route.
773 * If we want to create a neighbor cache for the
774 * address, we should free the route for the
775 * destination and allocate an interface route.
786 * If no route is available and create is set,
787 * we allocate a host route for the destination
788 * and treat it like an interface route.
789 * This hack is necessary for a neighbor which can't
790 * be covered by our own prefix.
793 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
798 * Create a new route. RTF_LLINFO is necessary
799 * to create a Neighbor Cache entry for the
800 * destination in nd6_rtrequest which will be
801 * called in rtrequest via ifa->ifa_rtrequest.
803 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
804 ifa->ifa_addr, (struct sockaddr *)&all1_sa,
805 (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
806 ~RTF_CLONING, &rt)) != 0) {
808 "nd6_lookup: failed to add route for a "
809 "neighbor(%s), errno=%d\n",
810 ip6_sprintf(addr6), e);
816 struct llinfo_nd6 *ln =
817 (struct llinfo_nd6 *)rt->rt_llinfo;
818 ln->ln_state = ND6_LLINFO_NOSTATE;
826 * Validation for the entry.
827 * Note that the check for rt_llinfo is necessary because a cloned
828 * route from a parent route that has the L flag (e.g. the default
829 * route to a p2p interface) may have the flag, too, while the
830 * destination is not actually a neighbor.
831 * XXX: we can't use rt->rt_ifp to check for the interface, since
832 * it might be the loopback interface if the entry is for our
833 * own address on a non-loopback interface. Instead, we should
834 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
837 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
838 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
839 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
842 "nd6_lookup: failed to lookup %s (if = %s)\n",
844 ifp ? if_name(ifp) : "unspec"));
849 RT_UNLOCK(rt); /* XXX not ready to return rt locked */
854 * Test whether a given IPv6 address is a neighbor or not, ignoring
855 * the actual neighbor cache. The neighbor cache is ignored in order
856 * to not reenter the routing code from within itself.
859 nd6_is_new_addr_neighbor(addr, ifp)
860 struct sockaddr_in6 *addr;
863 struct nd_prefix *pr;
866 * A link-local address is always a neighbor.
867 * XXX: a link does not necessarily specify a single interface.
869 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
870 struct sockaddr_in6 sin6_copy;
874 * We need sin6_copy since sa6_recoverscope() may modify the
878 if (sa6_recoverscope(&sin6_copy))
879 return (0); /* XXX: should be impossible */
880 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
882 if (sin6_copy.sin6_scope_id == zone)
889 * If the address matches one of our addresses,
890 * it should be a neighbor.
891 * If the address matches one of our on-link prefixes, it should be a
894 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
895 if (pr->ndpr_ifp != ifp)
898 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
901 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
902 &addr->sin6_addr, &pr->ndpr_mask))
907 * If the default router list is empty, all addresses are regarded
908 * as on-link, and thus, as a neighbor.
909 * XXX: we restrict the condition to hosts, because routers usually do
910 * not have the "default router list".
912 if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
913 nd6_defifindex == ifp->if_index) {
922 * Detect if a given IPv6 address identifies a neighbor on a given link.
923 * XXX: should take care of the destination of a p2p link?
926 nd6_is_addr_neighbor(addr, ifp)
927 struct sockaddr_in6 *addr;
931 if (nd6_is_new_addr_neighbor(addr, ifp))
935 * Even if the address matches none of our addresses, it might be
936 * in the neighbor cache.
938 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
945 * Free an nd6 llinfo entry.
951 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
952 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
953 struct nd_defrouter *dr;
956 * we used to have pfctlinput(PRC_HOSTDEAD) here.
957 * even though it is not harmful, it was not really necessary.
960 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
963 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
966 if (ln->ln_router || dr) {
968 * rt6_flush must be called whether or not the neighbor
969 * is in the Default Router List.
970 * See a corresponding comment in nd6_na_input().
972 rt6_flush(&in6, rt->rt_ifp);
977 * Unreachablity of a router might affect the default
978 * router selection and on-link detection of advertised
983 * Temporarily fake the state to choose a new default
984 * router and to perform on-link determination of
985 * prefixes correctly.
986 * Below the state will be set correctly,
987 * or the entry itself will be deleted.
989 ln->ln_state = ND6_LLINFO_INCOMPLETE;
992 * Since defrouter_select() does not affect the
993 * on-link determination and MIP6 needs the check
994 * before the default router selection, we perform
997 pfxlist_onlink_check();
999 if (dr == TAILQ_FIRST(&nd_defrouter)) {
1001 * It is used as the current default router,
1002 * so we have to move it to the end of the
1003 * list and choose a new one.
1004 * XXX: it is not very efficient if this is
1007 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
1008 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
1017 * Before deleting the entry, remember the next entry as the
1018 * return value. We need this because pfxlist_onlink_check() above
1019 * might have freed other entries (particularly the old next entry) as
1020 * a side effect (XXX).
1025 * Detach the route from the routing tree and the list of neighbor
1026 * caches, and disable the route entry not to be used in already
1029 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
1030 rt_mask(rt), 0, (struct rtentry **)0);
1036 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1038 * XXX cost-effective methods?
1041 nd6_nud_hint(rt, dst6, force)
1043 struct in6_addr *dst6;
1046 struct llinfo_nd6 *ln;
1049 * If the caller specified "rt", use that. Otherwise, resolve the
1050 * routing table by supplied "dst6".
1055 if ((rt = nd6_lookup(dst6, 0, NULL)) == NULL)
1059 if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
1060 (rt->rt_flags & RTF_LLINFO) == 0 ||
1061 rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
1062 rt->rt_gateway->sa_family != AF_LINK) {
1063 /* This is not a host route. */
1067 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1068 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1072 * if we get upper-layer reachability confirmation many times,
1073 * it is possible we have false information.
1077 if (ln->ln_byhint > nd6_maxnudhint)
1081 ln->ln_state = ND6_LLINFO_REACHABLE;
1083 ln->ln_expire = time_second +
1084 ND_IFINFO(rt->rt_ifp)->reachable;
1088 nd6_rtrequest(req, rt, info)
1091 struct rt_addrinfo *info; /* xxx unused */
1093 struct sockaddr *gate = rt->rt_gateway;
1094 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1095 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1096 struct ifnet *ifp = rt->rt_ifp;
1101 if ((rt->rt_flags & RTF_GATEWAY) != 0)
1104 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1106 * This is probably an interface direct route for a link
1107 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1108 * We do not need special treatment below for such a route.
1109 * Moreover, the RTF_LLINFO flag which would be set below
1110 * would annoy the ndp(8) command.
1115 if (req == RTM_RESOLVE &&
1116 (nd6_need_cache(ifp) == 0 || /* stf case */
1117 !nd6_is_new_addr_neighbor((struct sockaddr_in6 *)rt_key(rt),
1120 * FreeBSD and BSD/OS often make a cloned host route based
1121 * on a less-specific route (e.g. the default route).
1122 * If the less specific route does not have a "gateway"
1123 * (this is the case when the route just goes to a p2p or an
1124 * stf interface), we'll mistakenly make a neighbor cache for
1125 * the host route, and will see strange neighbor solicitation
1126 * for the corresponding destination. In order to avoid the
1127 * confusion, we check if the destination of the route is
1128 * a neighbor in terms of neighbor discovery, and stop the
1129 * process if not. Additionally, we remove the LLINFO flag
1130 * so that ndp(8) will not try to get the neighbor information
1131 * of the destination.
1133 rt->rt_flags &= ~RTF_LLINFO;
1140 * There is no backward compatibility :)
1142 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1143 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1144 * rt->rt_flags |= RTF_CLONING;
1146 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1148 * Case 1: This route should come from
1149 * a route to interface. RTF_LLINFO flag is set
1150 * for a host route whose destination should be
1151 * treated as on-link.
1153 rt_setgate(rt, rt_key(rt),
1154 (struct sockaddr *)&null_sdl);
1155 gate = rt->rt_gateway;
1156 SDL(gate)->sdl_type = ifp->if_type;
1157 SDL(gate)->sdl_index = ifp->if_index;
1159 ln->ln_expire = time_second;
1160 if (ln && ln->ln_expire == 0) {
1161 /* kludge for desktops */
1164 if ((rt->rt_flags & RTF_CLONING) != 0)
1168 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1169 * We don't do that here since llinfo is not ready yet.
1171 * There are also couple of other things to be discussed:
1172 * - unsolicited NA code needs improvement beforehand
1173 * - RFC2461 says we MAY send multicast unsolicited NA
1174 * (7.2.6 paragraph 4), however, it also says that we
1175 * SHOULD provide a mechanism to prevent multicast NA storm.
1176 * we don't have anything like it right now.
1177 * note that the mechanism needs a mutual agreement
1178 * between proxies, which means that we need to implement
1179 * a new protocol, or a new kludge.
1180 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1181 * we need to check ip6forwarding before sending it.
1182 * (or should we allow proxy ND configuration only for
1183 * routers? there's no mention about proxy ND from hosts)
1187 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1189 * Address resolution isn't necessary for a point to
1190 * point link, so we can skip this test for a p2p link.
1192 if (gate->sa_family != AF_LINK ||
1193 gate->sa_len < sizeof(null_sdl)) {
1195 "nd6_rtrequest: bad gateway value: %s\n",
1199 SDL(gate)->sdl_type = ifp->if_type;
1200 SDL(gate)->sdl_index = ifp->if_index;
1203 break; /* This happens on a route change */
1205 * Case 2: This route may come from cloning, or a manual route
1206 * add with a LL address.
1208 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1209 rt->rt_llinfo = (caddr_t)ln;
1211 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1216 bzero(ln, sizeof(*ln));
1218 /* this is required for "ndp" command. - shin */
1219 if (req == RTM_ADD) {
1221 * gate should have some valid AF_LINK entry,
1222 * and ln->ln_expire should have some lifetime
1223 * which is specified by ndp command.
1225 ln->ln_state = ND6_LLINFO_REACHABLE;
1229 * When req == RTM_RESOLVE, rt is created and
1230 * initialized in rtrequest(), so rt_expire is 0.
1232 ln->ln_state = ND6_LLINFO_NOSTATE;
1233 ln->ln_expire = time_second;
1235 rt->rt_flags |= RTF_LLINFO;
1236 ln->ln_next = llinfo_nd6.ln_next;
1237 llinfo_nd6.ln_next = ln;
1238 ln->ln_prev = &llinfo_nd6;
1239 ln->ln_next->ln_prev = ln;
1242 * check if rt_key(rt) is one of my address assigned
1245 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1246 &SIN6(rt_key(rt))->sin6_addr);
1248 caddr_t macp = nd6_ifptomac(ifp);
1250 ln->ln_state = ND6_LLINFO_REACHABLE;
1253 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1254 SDL(gate)->sdl_alen = ifp->if_addrlen;
1256 if (nd6_useloopback) {
1257 rt->rt_ifp = &loif[0]; /* XXX */
1259 * Make sure rt_ifa be equal to the ifaddr
1260 * corresponding to the address.
1261 * We need this because when we refer
1262 * rt_ifa->ia6_flags in ip6_input, we assume
1263 * that the rt_ifa points to the address instead
1264 * of the loopback address.
1266 if (ifa != rt->rt_ifa) {
1267 IFAFREE(rt->rt_ifa);
1272 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1274 ln->ln_state = ND6_LLINFO_REACHABLE;
1277 /* join solicited node multicast for proxy ND */
1278 if (ifp->if_flags & IFF_MULTICAST) {
1279 struct in6_addr llsol;
1282 llsol = SIN6(rt_key(rt))->sin6_addr;
1283 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1284 llsol.s6_addr32[1] = 0;
1285 llsol.s6_addr32[2] = htonl(1);
1286 llsol.s6_addr8[12] = 0xff;
1287 if (in6_setscope(&llsol, ifp, NULL))
1289 if (!in6_addmulti(&llsol, ifp, &error)) {
1290 nd6log((LOG_ERR, "%s: failed to join "
1291 "%s (errno=%d)\n", if_name(ifp),
1292 ip6_sprintf(&llsol), error));
1301 /* leave from solicited node multicast for proxy ND */
1302 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1303 (ifp->if_flags & IFF_MULTICAST) != 0) {
1304 struct in6_addr llsol;
1305 struct in6_multi *in6m;
1307 llsol = SIN6(rt_key(rt))->sin6_addr;
1308 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1309 llsol.s6_addr32[1] = 0;
1310 llsol.s6_addr32[2] = htonl(1);
1311 llsol.s6_addr8[12] = 0xff;
1312 if (in6_setscope(&llsol, ifp, NULL) == 0) {
1313 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1317 ; /* XXX: should not happen. bark here? */
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 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1390 oprl->prefix[i].raflags = pr->ndpr_raf;
1391 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1392 oprl->prefix[i].vltime = pr->ndpr_vltime;
1393 oprl->prefix[i].pltime = pr->ndpr_pltime;
1394 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1395 oprl->prefix[i].expire = pr->ndpr_expire;
1397 pfr = pr->ndpr_advrtrs.lh_first;
1401 #define RTRADDR oprl->prefix[i].advrtr[j]
1402 RTRADDR = pfr->router->rtaddr;
1403 in6_clearscope(&RTRADDR);
1407 pfr = pfr->pfr_next;
1409 oprl->prefix[i].advrtrs = j;
1410 oprl->prefix[i].origin = PR_ORIG_RA;
1418 case OSIOCGIFINFO_IN6:
1420 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1421 bzero(&ND, sizeof(ND));
1422 ND.linkmtu = IN6_LINKMTU(ifp);
1423 ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1424 ND.basereachable = ND_IFINFO(ifp)->basereachable;
1425 ND.reachable = ND_IFINFO(ifp)->reachable;
1426 ND.retrans = ND_IFINFO(ifp)->retrans;
1427 ND.flags = ND_IFINFO(ifp)->flags;
1428 ND.recalctm = ND_IFINFO(ifp)->recalctm;
1429 ND.chlim = ND_IFINFO(ifp)->chlim;
1431 case SIOCGIFINFO_IN6:
1432 ND = *ND_IFINFO(ifp);
1433 ND.linkmtu = IN6_LINKMTU(ifp);
1435 case SIOCSIFINFO_IN6:
1437 * used to change host variables from userland.
1438 * intented for a use on router to reflect RA configurations.
1440 /* 0 means 'unspecified' */
1441 if (ND.linkmtu != 0) {
1442 if (ND.linkmtu < IPV6_MMTU ||
1443 ND.linkmtu > IN6_LINKMTU(ifp)) {
1447 ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1450 if (ND.basereachable != 0) {
1451 int obasereachable = ND_IFINFO(ifp)->basereachable;
1453 ND_IFINFO(ifp)->basereachable = ND.basereachable;
1454 if (ND.basereachable != obasereachable)
1455 ND_IFINFO(ifp)->reachable =
1456 ND_COMPUTE_RTIME(ND.basereachable);
1458 if (ND.retrans != 0)
1459 ND_IFINFO(ifp)->retrans = ND.retrans;
1461 ND_IFINFO(ifp)->chlim = ND.chlim;
1463 case SIOCSIFINFO_FLAGS:
1464 ND_IFINFO(ifp)->flags = ND.flags;
1467 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1468 /* flush default router list */
1470 * xxx sumikawa: should not delete route if default
1471 * route equals to the top of default router list
1473 bzero(&any, sizeof(any));
1474 defrouter_delreq(&any, 0);
1476 /* xxx sumikawa: flush prefix list */
1478 case SIOCSPFXFLUSH_IN6:
1480 /* flush all the prefix advertised by routers */
1481 struct nd_prefix *pr, *next;
1484 for (pr = nd_prefix.lh_first; pr; pr = next) {
1485 struct in6_ifaddr *ia, *ia_next;
1487 next = pr->ndpr_next;
1489 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1492 /* do we really have to remove addresses as well? */
1493 for (ia = in6_ifaddr; ia; ia = ia_next) {
1494 /* ia might be removed. keep the next ptr. */
1495 ia_next = ia->ia_next;
1497 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1500 if (ia->ia6_ndpr == pr)
1501 in6_purgeaddr(&ia->ia_ifa);
1508 case SIOCSRTRFLUSH_IN6:
1510 /* flush all the default routers */
1511 struct nd_defrouter *dr, *next;
1514 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1516 * The first entry of the list may be stored in
1517 * the routing table, so we'll delete it later.
1519 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1520 next = TAILQ_NEXT(dr, dr_entry);
1523 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1528 case SIOCGNBRINFO_IN6:
1530 struct llinfo_nd6 *ln;
1531 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1533 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1537 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1542 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1543 nbi->state = ln->ln_state;
1544 nbi->asked = ln->ln_asked;
1545 nbi->isrouter = ln->ln_router;
1546 nbi->expire = ln->ln_expire;
1551 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1552 ndif->ifindex = nd6_defifindex;
1554 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1555 return (nd6_setdefaultiface(ndif->ifindex));
1561 * Create neighbor cache entry and cache link-layer address,
1562 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1565 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1567 struct in6_addr *from;
1570 int type; /* ICMP6 type */
1571 int code; /* type dependent information */
1573 struct rtentry *rt = NULL;
1574 struct llinfo_nd6 *ln = NULL;
1576 struct sockaddr_dl *sdl = NULL;
1583 panic("ifp == NULL in nd6_cache_lladdr");
1585 panic("from == NULL in nd6_cache_lladdr");
1587 /* nothing must be updated for unspecified address */
1588 if (IN6_IS_ADDR_UNSPECIFIED(from))
1592 * Validation about ifp->if_addrlen and lladdrlen must be done in
1595 * XXX If the link does not have link-layer adderss, what should
1596 * we do? (ifp->if_addrlen == 0)
1597 * Spec says nothing in sections for RA, RS and NA. There's small
1598 * description on it in NS section (RFC 2461 7.2.3).
1601 rt = nd6_lookup(from, 0, ifp);
1603 rt = nd6_lookup(from, 1, ifp);
1606 /* do nothing if static ndp is set */
1607 if (rt->rt_flags & RTF_STATIC)
1614 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1619 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1622 if (rt->rt_gateway == NULL)
1624 if (rt->rt_gateway->sa_family != AF_LINK)
1626 sdl = SDL(rt->rt_gateway);
1628 olladdr = (sdl->sdl_alen) ? 1 : 0;
1629 if (olladdr && lladdr) {
1630 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1638 * newentry olladdr lladdr llchange (*=record)
1641 * 0 n y -- (3) * STALE
1643 * 0 y y y (5) * STALE
1644 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1645 * 1 -- y -- (7) * STALE
1648 if (lladdr) { /* (3-5) and (7) */
1650 * Record source link-layer address
1651 * XXX is it dependent to ifp->if_type?
1653 sdl->sdl_alen = ifp->if_addrlen;
1654 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1658 if ((!olladdr && lladdr != NULL) || /* (3) */
1659 (olladdr && lladdr != NULL && llchange)) { /* (5) */
1661 newstate = ND6_LLINFO_STALE;
1662 } else /* (1-2,4) */
1666 if (lladdr == NULL) /* (6) */
1667 newstate = ND6_LLINFO_NOSTATE;
1669 newstate = ND6_LLINFO_STALE;
1674 * Update the state of the neighbor cache.
1676 ln->ln_state = newstate;
1678 if (ln->ln_state == ND6_LLINFO_STALE) {
1680 * XXX: since nd6_output() below will cause
1681 * state tansition to DELAY and reset the timer,
1682 * we must set the timer now, although it is actually
1685 ln->ln_expire = time_second + nd6_gctimer;
1689 * we assume ifp is not a p2p here, so just
1690 * set the 2nd argument as the 1st one.
1692 nd6_output(ifp, ifp, ln->ln_hold,
1693 (struct sockaddr_in6 *)rt_key(rt), rt);
1696 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1697 /* probe right away */
1698 ln->ln_expire = time_second;
1703 * ICMP6 type dependent behavior.
1705 * NS: clear IsRouter if new entry
1706 * RS: clear IsRouter
1707 * RA: set IsRouter if there's lladdr
1708 * redir: clear IsRouter if new entry
1711 * The spec says that we must set IsRouter in the following cases:
1712 * - If lladdr exist, set IsRouter. This means (1-5).
1713 * - If it is old entry (!newentry), set IsRouter. This means (7).
1714 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1715 * A quetion arises for (1) case. (1) case has no lladdr in the
1716 * neighbor cache, this is similar to (6).
1717 * This case is rare but we figured that we MUST NOT set IsRouter.
1719 * newentry olladdr lladdr llchange NS RS RA redir
1721 * 0 n n -- (1) c ? s
1722 * 0 y n -- (2) c s s
1723 * 0 n y -- (3) c s s
1726 * 1 -- n -- (6) c c c s
1727 * 1 -- y -- (7) c c s c s
1731 switch (type & 0xff) {
1732 case ND_NEIGHBOR_SOLICIT:
1734 * New entry must have is_router flag cleared.
1736 if (is_newentry) /* (6-7) */
1741 * If the icmp is a redirect to a better router, always set the
1742 * is_router flag. Otherwise, if the entry is newly created,
1743 * clear the flag. [RFC 2461, sec 8.3]
1745 if (code == ND_REDIRECT_ROUTER)
1747 else if (is_newentry) /* (6-7) */
1750 case ND_ROUTER_SOLICIT:
1752 * is_router flag must always be cleared.
1756 case ND_ROUTER_ADVERT:
1758 * Mark an entry with lladdr as a router.
1760 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1761 (is_newentry && lladdr)) { /* (7) */
1768 * When the link-layer address of a router changes, select the
1769 * best router again. In particular, when the neighbor entry is newly
1770 * created, it might affect the selection policy.
1771 * Question: can we restrict the first condition to the "is_newentry"
1773 * XXX: when we hear an RA from a new router with the link-layer
1774 * address option, defrouter_select() is called twice, since
1775 * defrtrlist_update called the function as well. However, I believe
1776 * we can compromise the overhead, since it only happens the first
1778 * XXX: although defrouter_select() should not have a bad effect
1779 * for those are not autoconfigured hosts, we explicitly avoid such
1782 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1789 nd6_slowtimo(ignored_arg)
1793 struct nd_ifinfo *nd6if;
1796 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1797 nd6_slowtimo, NULL);
1799 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1800 nd6if = ND_IFINFO(ifp);
1801 if (nd6if->basereachable && /* already initialized */
1802 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1804 * Since reachable time rarely changes by router
1805 * advertisements, we SHOULD insure that a new random
1806 * value gets recomputed at least once every few hours.
1809 nd6if->recalctm = nd6_recalc_reachtm_interval;
1810 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1817 #define senderr(e) { error = (e); goto bad;}
1819 nd6_output(ifp, origifp, m0, dst, rt0)
1821 struct ifnet *origifp;
1823 struct sockaddr_in6 *dst;
1824 struct rtentry *rt0;
1826 struct mbuf *m = m0;
1827 struct rtentry *rt = rt0;
1828 struct sockaddr_in6 *gw6 = NULL;
1829 struct llinfo_nd6 *ln = NULL;
1832 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1835 if (nd6_need_cache(ifp) == 0)
1839 * next hop determination. This routine is derived from ether_output.
1843 if ((rt->rt_flags & RTF_UP) == 0) {
1844 rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL);
1848 if (rt->rt_ifp != ifp)
1850 * XXX maybe we should update ifp too,
1851 * but the original code didn't and I
1852 * don't know what is correct here.
1856 senderr(EHOSTUNREACH);
1859 if (rt->rt_flags & RTF_GATEWAY) {
1860 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1863 * We skip link-layer address resolution and NUD
1864 * if the gateway is not a neighbor from ND point
1865 * of view, regardless of the value of nd_ifinfo.flags.
1866 * The second condition is a bit tricky; we skip
1867 * if the gateway is our own address, which is
1868 * sometimes used to install a route to a p2p link.
1870 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1871 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1873 * We allow this kind of tricky route only
1874 * when the outgoing interface is p2p.
1875 * XXX: we may need a more generic rule here.
1877 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1878 senderr(EHOSTUNREACH);
1883 if (rt->rt_gwroute == 0)
1885 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1887 rtfree(rt); rt = rt0;
1889 rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
1890 if ((rt = rt->rt_gwroute) == 0)
1891 senderr(EHOSTUNREACH);
1898 * Address resolution or Neighbor Unreachability Detection
1900 * At this point, the destination of the packet must be a unicast
1901 * or an anycast address(i.e. not a multicast).
1904 /* Look up the neighbor cache for the nexthop */
1905 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1906 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1909 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1910 * the condition below is not very efficient. But we believe
1911 * it is tolerable, because this should be a rare case.
1913 if (nd6_is_addr_neighbor(dst, ifp) &&
1914 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1915 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1917 if (ln == NULL || rt == NULL) {
1918 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1919 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1921 "nd6_output: can't allocate llinfo for %s "
1923 ip6_sprintf(&dst->sin6_addr), ln, rt);
1924 senderr(EIO); /* XXX: good error? */
1927 goto sendpkt; /* send anyway */
1930 /* We don't have to do link-layer address resolution on a p2p link. */
1931 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1932 ln->ln_state < ND6_LLINFO_REACHABLE) {
1933 ln->ln_state = ND6_LLINFO_STALE;
1934 ln->ln_expire = time_second + nd6_gctimer;
1938 * The first time we send a packet to a neighbor whose entry is
1939 * STALE, we have to change the state to DELAY and a sets a timer to
1940 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1941 * neighbor unreachability detection on expiration.
1944 if (ln->ln_state == ND6_LLINFO_STALE) {
1946 ln->ln_state = ND6_LLINFO_DELAY;
1947 ln->ln_expire = time_second + nd6_delay;
1951 * If the neighbor cache entry has a state other than INCOMPLETE
1952 * (i.e. its link-layer address is already resolved), just
1955 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1959 * There is a neighbor cache entry, but no ethernet address
1960 * response yet. Replace the held mbuf (if any) with this
1963 * This code conforms to the rate-limiting rule described in Section
1964 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
1967 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1968 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1970 m_freem(ln->ln_hold);
1972 if (ln->ln_expire) {
1973 if (ln->ln_asked < nd6_mmaxtries &&
1974 ln->ln_expire < time_second) {
1976 ln->ln_expire = time_second +
1977 ND_IFINFO(ifp)->retrans / 1000;
1978 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1984 /* discard the packet if IPv6 operation is disabled on the interface */
1985 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
1986 error = ENETDOWN; /* better error? */
1991 /* clean ipsec history once it goes out of the node */
1996 mac_create_mbuf_linklayer(ifp, m);
1998 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1999 return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
2002 return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
2016 * XXX: we currently do not make neighbor cache on any interface
2017 * other than ARCnet, Ethernet, FDDI and GIF.
2020 * - unidirectional tunnels needs no ND
2022 switch (ifp->if_type) {
2030 #ifdef IFT_IEEE80211
2036 case IFT_GIF: /* XXX need more cases? */
2047 nd6_storelladdr(ifp, rt0, m, dst, desten)
2049 struct rtentry *rt0;
2051 struct sockaddr *dst;
2054 struct sockaddr_dl *sdl;
2058 if (m->m_flags & M_MCAST) {
2061 switch (ifp->if_type) {
2067 #ifdef IFT_IEEE80211
2072 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2077 * netbsd can use if_broadcastaddr, but we don't do so
2078 * to reduce # of ifdef.
2080 for (i = 0; i < ifp->if_addrlen; i++)
2088 return (EAFNOSUPPORT);
2093 /* this could happen, if we could not allocate memory */
2098 error = rt_check(&rt, &rt0, dst);
2105 if (rt->rt_gateway->sa_family != AF_LINK) {
2106 printf("nd6_storelladdr: something odd happens\n");
2110 sdl = SDL(rt->rt_gateway);
2111 if (sdl->sdl_alen == 0) {
2112 /* this should be impossible, but we bark here for debugging */
2113 printf("nd6_storelladdr: sdl_alen == 0\n");
2118 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2122 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2123 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2125 SYSCTL_DECL(_net_inet6_icmp6);
2127 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2128 CTLFLAG_RD, nd6_sysctl_drlist, "");
2129 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2130 CTLFLAG_RD, nd6_sysctl_prlist, "");
2133 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2137 struct in6_defrouter *d, *de;
2138 struct nd_defrouter *dr;
2144 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2145 dr = TAILQ_NEXT(dr, dr_entry)) {
2146 d = (struct in6_defrouter *)buf;
2147 de = (struct in6_defrouter *)(buf + sizeof(buf));
2150 bzero(d, sizeof(*d));
2151 d->rtaddr.sin6_family = AF_INET6;
2152 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2153 d->rtaddr.sin6_addr = dr->rtaddr;
2154 if (sa6_recoverscope(&d->rtaddr)) {
2156 "scope error in router list (%s)\n",
2157 ip6_sprintf(&d->rtaddr.sin6_addr));
2158 /* XXX: press on... */
2160 d->flags = dr->flags;
2161 d->rtlifetime = dr->rtlifetime;
2162 d->expire = dr->expire;
2163 d->if_index = dr->ifp->if_index;
2165 panic("buffer too short");
2167 error = SYSCTL_OUT(req, buf, sizeof(*d));
2176 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2180 struct in6_prefix *p, *pe;
2181 struct nd_prefix *pr;
2187 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2190 struct sockaddr_in6 *sin6, *s6;
2191 struct nd_pfxrouter *pfr;
2193 p = (struct in6_prefix *)buf;
2194 pe = (struct in6_prefix *)(buf + sizeof(buf));
2197 bzero(p, sizeof(*p));
2198 sin6 = (struct sockaddr_in6 *)(p + 1);
2200 p->prefix = pr->ndpr_prefix;
2201 if (sa6_recoverscope(&p->prefix)) {
2203 "scope error in prefix list (%s)\n",
2204 ip6_sprintf(&p->prefix.sin6_addr));
2205 /* XXX: press on... */
2207 p->raflags = pr->ndpr_raf;
2208 p->prefixlen = pr->ndpr_plen;
2209 p->vltime = pr->ndpr_vltime;
2210 p->pltime = pr->ndpr_pltime;
2211 p->if_index = pr->ndpr_ifp->if_index;
2212 p->expire = pr->ndpr_expire;
2213 p->refcnt = pr->ndpr_refcnt;
2214 p->flags = pr->ndpr_stateflags;
2215 p->origin = PR_ORIG_RA;
2217 for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2218 pfr = pfr->pfr_next) {
2219 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2223 s6 = &sin6[advrtrs];
2224 bzero(s6, sizeof(*s6));
2225 s6->sin6_family = AF_INET6;
2226 s6->sin6_len = sizeof(*sin6);
2227 s6->sin6_addr = pfr->router->rtaddr;
2228 if (sa6_recoverscope(s6)) {
2231 "prefix list (%s)\n",
2232 ip6_sprintf(&pfr->router->rtaddr));
2236 p->advrtrs = advrtrs;
2238 panic("buffer too short");
2240 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2241 error = SYSCTL_OUT(req, buf, advance);