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>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
45 #include <sys/kernel.h>
46 #include <sys/protosw.h>
47 #include <sys/errno.h>
48 #include <sys/syslog.h>
49 #include <sys/queue.h>
50 #include <sys/sysctl.h>
53 #include <net/if_arc.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/iso88025.h>
58 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/if_ether.h>
62 #include <netinet6/in6_var.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/scope6_var.h>
66 #include <netinet6/nd6.h>
67 #include <netinet/icmp6.h>
69 #include <sys/limits.h>
71 #include <security/mac/mac_framework.h>
73 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
74 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
76 #define SIN6(s) ((struct sockaddr_in6 *)s)
77 #define SDL(s) ((struct sockaddr_dl *)s)
80 int nd6_prune = 1; /* walk list every 1 seconds */
81 int nd6_delay = 5; /* delay first probe time 5 second */
82 int nd6_umaxtries = 3; /* maximum unicast query */
83 int nd6_mmaxtries = 3; /* maximum multicast query */
84 int nd6_useloopback = 1; /* use loopback interface for local traffic */
85 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
87 /* preventing too many loops in ND option parsing */
88 int nd6_maxndopt = 10; /* max # of ND options allowed */
90 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
91 int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
100 static int nd6_inuse, nd6_allocated;
102 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
103 struct nd_drhead nd_defrouter;
104 struct nd_prhead nd_prefix = { 0 };
106 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
107 static struct sockaddr_in6 all1_sa;
109 static int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *,
111 static void nd6_setmtu0 __P((struct ifnet *, struct nd_ifinfo *));
112 static void nd6_slowtimo __P((void *));
113 static int regen_tmpaddr __P((struct in6_ifaddr *));
114 static struct llinfo_nd6 *nd6_free __P((struct rtentry *, int));
115 static void nd6_llinfo_timer __P((void *));
116 static void clear_llinfo_pqueue __P((struct llinfo_nd6 *));
118 struct callout nd6_slowtimo_ch;
119 struct callout nd6_timer_ch;
120 extern struct callout in6_tmpaddrtimer_ch;
125 static int nd6_init_done = 0;
129 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
133 all1_sa.sin6_family = AF_INET6;
134 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
135 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
136 all1_sa.sin6_addr.s6_addr[i] = 0xff;
138 /* initialization of the default router list */
139 TAILQ_INIT(&nd_defrouter);
144 callout_init(&nd6_slowtimo_ch, 0);
145 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
153 struct nd_ifinfo *nd;
155 nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
156 bzero(nd, sizeof(*nd));
160 nd->chlim = IPV6_DEFHLIM;
161 nd->basereachable = REACHABLE_TIME;
162 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
163 nd->retrans = RETRANS_TIMER;
165 * Note that the default value of ip6_accept_rtadv is 0, which means
166 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
169 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
171 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
172 nd6_setmtu0(ifp, nd);
179 struct nd_ifinfo *nd;
186 * Reset ND level link MTU. This function is called when the physical MTU
187 * changes, which means we might have to adjust the ND level MTU.
194 nd6_setmtu0(ifp, ND_IFINFO(ifp));
197 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
199 nd6_setmtu0(ifp, ndi)
201 struct nd_ifinfo *ndi;
205 omaxmtu = ndi->maxmtu;
207 switch (ifp->if_type) {
209 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
212 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
215 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
218 ndi->maxmtu = ifp->if_mtu;
223 * Decreasing the interface MTU under IPV6 minimum MTU may cause
224 * undesirable situation. We thus notify the operator of the change
225 * explicitly. The check for omaxmtu is necessary to restrict the
226 * log to the case of changing the MTU, not initializing it.
228 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
229 log(LOG_NOTICE, "nd6_setmtu0: "
230 "new link MTU on %s (%lu) is too small for IPv6\n",
231 if_name(ifp), (unsigned long)ndi->maxmtu);
234 if (ndi->maxmtu > in6_maxmtu)
235 in6_setmaxmtu(); /* check all interfaces just in case */
241 nd6_option_init(opt, icmp6len, ndopts)
244 union nd_opts *ndopts;
247 bzero(ndopts, sizeof(*ndopts));
248 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
250 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
253 ndopts->nd_opts_done = 1;
254 ndopts->nd_opts_search = NULL;
259 * Take one ND option.
263 union nd_opts *ndopts;
265 struct nd_opt_hdr *nd_opt;
269 panic("ndopts == NULL in nd6_option");
270 if (ndopts->nd_opts_last == NULL)
271 panic("uninitialized ndopts in nd6_option");
272 if (ndopts->nd_opts_search == NULL)
274 if (ndopts->nd_opts_done)
277 nd_opt = ndopts->nd_opts_search;
279 /* make sure nd_opt_len is inside the buffer */
280 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
281 bzero(ndopts, sizeof(*ndopts));
285 olen = nd_opt->nd_opt_len << 3;
288 * Message validation requires that all included
289 * options have a length that is greater than zero.
291 bzero(ndopts, sizeof(*ndopts));
295 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
296 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
297 /* option overruns the end of buffer, invalid */
298 bzero(ndopts, sizeof(*ndopts));
300 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
301 /* reached the end of options chain */
302 ndopts->nd_opts_done = 1;
303 ndopts->nd_opts_search = NULL;
309 * Parse multiple ND options.
310 * This function is much easier to use, for ND routines that do not need
311 * multiple options of the same type.
315 union nd_opts *ndopts;
317 struct nd_opt_hdr *nd_opt;
321 panic("ndopts == NULL in nd6_options");
322 if (ndopts->nd_opts_last == NULL)
323 panic("uninitialized ndopts in nd6_options");
324 if (ndopts->nd_opts_search == NULL)
328 nd_opt = nd6_option(ndopts);
329 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
331 * Message validation requires that all included
332 * options have a length that is greater than zero.
334 icmp6stat.icp6s_nd_badopt++;
335 bzero(ndopts, sizeof(*ndopts));
342 switch (nd_opt->nd_opt_type) {
343 case ND_OPT_SOURCE_LINKADDR:
344 case ND_OPT_TARGET_LINKADDR:
346 case ND_OPT_REDIRECTED_HEADER:
347 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
349 "duplicated ND6 option found (type=%d)\n",
350 nd_opt->nd_opt_type));
353 ndopts->nd_opt_array[nd_opt->nd_opt_type]
357 case ND_OPT_PREFIX_INFORMATION:
358 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
359 ndopts->nd_opt_array[nd_opt->nd_opt_type]
362 ndopts->nd_opts_pi_end =
363 (struct nd_opt_prefix_info *)nd_opt;
367 * Unknown options must be silently ignored,
368 * to accomodate future extension to the protocol.
371 "nd6_options: unsupported option %d - "
372 "option ignored\n", nd_opt->nd_opt_type));
377 if (i > nd6_maxndopt) {
378 icmp6stat.icp6s_nd_toomanyopt++;
379 nd6log((LOG_INFO, "too many loop in nd opt\n"));
383 if (ndopts->nd_opts_done)
391 * ND6 timer routine to handle ND6 entries
394 nd6_llinfo_settimer(ln, tick)
395 struct llinfo_nd6 *ln;
401 callout_stop(&ln->ln_timer_ch);
403 ln->ln_expire = time_second + tick / hz;
404 if (tick > INT_MAX) {
405 ln->ln_ntick = tick - INT_MAX;
406 callout_reset(&ln->ln_timer_ch, INT_MAX,
407 nd6_llinfo_timer, ln);
410 callout_reset(&ln->ln_timer_ch, tick,
411 nd6_llinfo_timer, ln);
417 nd6_llinfo_timer(arg)
420 struct llinfo_nd6 *ln;
422 struct in6_addr *dst;
424 struct nd_ifinfo *ndi = NULL;
426 ln = (struct llinfo_nd6 *)arg;
428 if (ln->ln_ntick > 0) {
429 if (ln->ln_ntick > INT_MAX) {
430 ln->ln_ntick -= INT_MAX;
431 nd6_llinfo_settimer(ln, INT_MAX);
434 nd6_llinfo_settimer(ln, ln->ln_ntick);
439 if ((rt = ln->ln_rt) == NULL)
440 panic("ln->ln_rt == NULL");
441 if ((ifp = rt->rt_ifp) == NULL)
442 panic("ln->ln_rt->rt_ifp == NULL");
443 ndi = ND_IFINFO(ifp);
446 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
447 panic("rt_llinfo(%p) is not equal to ln(%p)",
449 if (rt_key(rt) == NULL)
450 panic("rt key is NULL in nd6_timer(ln=%p)", ln);
452 dst = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
454 switch (ln->ln_state) {
455 case ND6_LLINFO_INCOMPLETE:
456 if (ln->ln_asked < nd6_mmaxtries) {
458 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
459 nd6_ns_output(ifp, NULL, dst, ln, 0);
461 struct mbuf *m = ln->ln_hold;
466 * assuming every packet in ln_hold has the
471 icmp6_error2(m, ICMP6_DST_UNREACH,
472 ICMP6_DST_UNREACH_ADDR, 0, rt->rt_ifp);
475 clear_llinfo_pqueue(ln);
478 (void)nd6_free(rt, 0);
482 case ND6_LLINFO_REACHABLE:
483 if (!ND6_LLINFO_PERMANENT(ln)) {
484 ln->ln_state = ND6_LLINFO_STALE;
485 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
489 case ND6_LLINFO_STALE:
490 /* Garbage Collection(RFC 2461 5.3) */
491 if (!ND6_LLINFO_PERMANENT(ln)) {
492 (void)nd6_free(rt, 1);
497 case ND6_LLINFO_DELAY:
498 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
501 ln->ln_state = ND6_LLINFO_PROBE;
502 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
503 nd6_ns_output(ifp, dst, dst, ln, 0);
505 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
506 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
509 case ND6_LLINFO_PROBE:
510 if (ln->ln_asked < nd6_umaxtries) {
512 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
513 nd6_ns_output(ifp, dst, dst, ln, 0);
514 } else if (rt->rt_ifa != NULL &&
515 rt->rt_ifa->ifa_addr->sa_family == AF_INET6 &&
516 (((struct in6_ifaddr *)rt->rt_ifa)->ia_flags & IFA_ROUTE)) {
518 * This is an unreachable neighbor whose address is
519 * specified as the destination of a p2p interface
520 * (see in6_ifinit()). We should not free the entry
521 * since this is sort of a "static" entry generated
522 * via interface address configuration.
525 ln->ln_expire = 0; /* make it permanent */
526 ln->ln_state = ND6_LLINFO_STALE;
528 (void)nd6_free(rt, 0);
537 * ND6 timer routine to expire default route list and prefix list
540 nd6_timer(ignored_arg)
544 struct nd_defrouter *dr;
545 struct nd_prefix *pr;
546 struct in6_ifaddr *ia6, *nia6;
547 struct in6_addrlifetime *lt6;
549 callout_reset(&nd6_timer_ch, nd6_prune * hz,
552 /* expire default router list */
554 dr = TAILQ_FIRST(&nd_defrouter);
556 if (dr->expire && dr->expire < time_second) {
557 struct nd_defrouter *t;
558 t = TAILQ_NEXT(dr, dr_entry);
562 dr = TAILQ_NEXT(dr, dr_entry);
567 * expire interface addresses.
568 * in the past the loop was inside prefix expiry processing.
569 * However, from a stricter speci-confrmance standpoint, we should
570 * rather separate address lifetimes and prefix lifetimes.
573 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
575 /* check address lifetime */
576 lt6 = &ia6->ia6_lifetime;
577 if (IFA6_IS_INVALID(ia6)) {
581 * If the expiring address is temporary, try
582 * regenerating a new one. This would be useful when
583 * we suspended a laptop PC, then turned it on after a
584 * period that could invalidate all temporary
585 * addresses. Although we may have to restart the
586 * loop (see below), it must be after purging the
587 * address. Otherwise, we'd see an infinite loop of
590 if (ip6_use_tempaddr &&
591 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
592 if (regen_tmpaddr(ia6) == 0)
596 in6_purgeaddr(&ia6->ia_ifa);
599 goto addrloop; /* XXX: see below */
600 } else if (IFA6_IS_DEPRECATED(ia6)) {
601 int oldflags = ia6->ia6_flags;
603 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
606 * If a temporary address has just become deprecated,
607 * regenerate a new one if possible.
609 if (ip6_use_tempaddr &&
610 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
611 (oldflags & IN6_IFF_DEPRECATED) == 0) {
613 if (regen_tmpaddr(ia6) == 0) {
615 * A new temporary address is
617 * XXX: this means the address chain
618 * has changed while we are still in
619 * the loop. Although the change
620 * would not cause disaster (because
621 * it's not a deletion, but an
622 * addition,) we'd rather restart the
623 * loop just for safety. Or does this
624 * significantly reduce performance??
631 * A new RA might have made a deprecated address
634 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
638 /* expire prefix list */
639 pr = nd_prefix.lh_first;
642 * check prefix lifetime.
643 * since pltime is just for autoconf, pltime processing for
644 * prefix is not necessary.
646 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
647 time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
652 * address expiration and prefix expiration are
653 * separate. NEVER perform in6_purgeaddr here.
666 struct in6_ifaddr *ia6; /* deprecated/invalidated temporary address */
670 struct in6_ifaddr *public_ifa6 = NULL;
672 ifp = ia6->ia_ifa.ifa_ifp;
673 for (ifa = ifp->if_addrlist.tqh_first; ifa;
674 ifa = ifa->ifa_list.tqe_next) {
675 struct in6_ifaddr *it6;
677 if (ifa->ifa_addr->sa_family != AF_INET6)
680 it6 = (struct in6_ifaddr *)ifa;
682 /* ignore no autoconf addresses. */
683 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
686 /* ignore autoconf addresses with different prefixes. */
687 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
691 * Now we are looking at an autoconf address with the same
692 * prefix as ours. If the address is temporary and is still
693 * preferred, do not create another one. It would be rare, but
694 * could happen, for example, when we resume a laptop PC after
697 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
698 !IFA6_IS_DEPRECATED(it6)) {
704 * This is a public autoconf address that has the same prefix
705 * as ours. If it is preferred, keep it. We can't break the
706 * loop here, because there may be a still-preferred temporary
707 * address with the prefix.
709 if (!IFA6_IS_DEPRECATED(it6))
713 if (public_ifa6 != NULL) {
716 if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
717 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
718 " tmp addr,errno=%d\n", e);
728 * Nuke neighbor cache/prefix/default router management table, right before
735 struct llinfo_nd6 *ln, *nln;
736 struct nd_defrouter *dr, *ndr;
737 struct nd_prefix *pr, *npr;
740 * Nuke default router list entries toward ifp.
741 * We defer removal of default router list entries that is installed
742 * in the routing table, in order to keep additional side effects as
745 for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
746 ndr = TAILQ_NEXT(dr, dr_entry);
754 for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
755 ndr = TAILQ_NEXT(dr, dr_entry);
763 /* Nuke prefix list entries toward ifp */
764 for (pr = nd_prefix.lh_first; pr; pr = npr) {
766 if (pr->ndpr_ifp == ifp) {
768 * Because if_detach() does *not* release prefixes
769 * while purging addresses the reference count will
770 * still be above zero. We therefore reset it to
771 * make sure that the prefix really gets purged.
776 * Previously, pr->ndpr_addr is removed as well,
777 * but I strongly believe we don't have to do it.
778 * nd6_purge() is only called from in6_ifdetach(),
779 * which removes all the associated interface addresses
781 * (jinmei@kame.net 20010129)
787 /* cancel default outgoing interface setting */
788 if (nd6_defifindex == ifp->if_index)
789 nd6_setdefaultiface(0);
791 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
792 /* refresh default router list */
797 * Nuke neighbor cache entries for the ifp.
798 * Note that rt->rt_ifp may not be the same as ifp,
799 * due to KAME goto ours hack. See RTM_RESOLVE case in
800 * nd6_rtrequest(), and ip6_input().
802 ln = llinfo_nd6.ln_next;
803 while (ln && ln != &llinfo_nd6) {
805 struct sockaddr_dl *sdl;
809 if (rt && rt->rt_gateway &&
810 rt->rt_gateway->sa_family == AF_LINK) {
811 sdl = (struct sockaddr_dl *)rt->rt_gateway;
812 if (sdl->sdl_index == ifp->if_index)
813 nln = nd6_free(rt, 0);
820 nd6_lookup(addr6, create, ifp)
821 struct in6_addr *addr6;
826 struct sockaddr_in6 sin6;
827 char ip6buf[INET6_ADDRSTRLEN];
829 bzero(&sin6, sizeof(sin6));
830 sin6.sin6_len = sizeof(struct sockaddr_in6);
831 sin6.sin6_family = AF_INET6;
832 sin6.sin6_addr = *addr6;
833 rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL);
835 if ((rt->rt_flags & RTF_LLINFO) == 0 && create) {
837 * This is the case for the default route.
838 * If we want to create a neighbor cache for the
839 * address, we should free the route for the
840 * destination and allocate an interface route.
851 * If no route is available and create is set,
852 * we allocate a host route for the destination
853 * and treat it like an interface route.
854 * This hack is necessary for a neighbor which can't
855 * be covered by our own prefix.
858 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
863 * Create a new route. RTF_LLINFO is necessary
864 * to create a Neighbor Cache entry for the
865 * destination in nd6_rtrequest which will be
866 * called in rtrequest via ifa->ifa_rtrequest.
868 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
869 ifa->ifa_addr, (struct sockaddr *)&all1_sa,
870 (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
871 ~RTF_CLONING, &rt)) != 0) {
873 "nd6_lookup: failed to add route for a "
874 "neighbor(%s), errno=%d\n",
875 ip6_sprintf(ip6buf, addr6), e);
881 struct llinfo_nd6 *ln =
882 (struct llinfo_nd6 *)rt->rt_llinfo;
883 ln->ln_state = ND6_LLINFO_NOSTATE;
891 * Validation for the entry.
892 * Note that the check for rt_llinfo is necessary because a cloned
893 * route from a parent route that has the L flag (e.g. the default
894 * route to a p2p interface) may have the flag, too, while the
895 * destination is not actually a neighbor.
896 * XXX: we can't use rt->rt_ifp to check for the interface, since
897 * it might be the loopback interface if the entry is for our
898 * own address on a non-loopback interface. Instead, we should
899 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
901 * Note also that ifa_ifp and ifp may differ when we connect two
902 * interfaces to a same link, install a link prefix to an interface,
903 * and try to install a neighbor cache on an interface that does not
904 * have a route to the prefix.
906 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
907 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
908 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
911 "nd6_lookup: failed to lookup %s (if = %s)\n",
912 ip6_sprintf(ip6buf, addr6),
913 ifp ? if_name(ifp) : "unspec"));
918 RT_UNLOCK(rt); /* XXX not ready to return rt locked */
923 * Test whether a given IPv6 address is a neighbor or not, ignoring
924 * the actual neighbor cache. The neighbor cache is ignored in order
925 * to not reenter the routing code from within itself.
928 nd6_is_new_addr_neighbor(addr, ifp)
929 struct sockaddr_in6 *addr;
932 struct nd_prefix *pr;
933 struct ifaddr *dstaddr;
936 * A link-local address is always a neighbor.
937 * XXX: a link does not necessarily specify a single interface.
939 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
940 struct sockaddr_in6 sin6_copy;
944 * We need sin6_copy since sa6_recoverscope() may modify the
948 if (sa6_recoverscope(&sin6_copy))
949 return (0); /* XXX: should be impossible */
950 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
952 if (sin6_copy.sin6_scope_id == zone)
959 * If the address matches one of our addresses,
960 * it should be a neighbor.
961 * If the address matches one of our on-link prefixes, it should be a
964 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
965 if (pr->ndpr_ifp != ifp)
968 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
971 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
972 &addr->sin6_addr, &pr->ndpr_mask))
977 * If the address is assigned on the node of the other side of
978 * a p2p interface, the address should be a neighbor.
980 dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr);
981 if ((dstaddr != NULL) && (dstaddr->ifa_ifp == ifp))
985 * If the default router list is empty, all addresses are regarded
986 * as on-link, and thus, as a neighbor.
987 * XXX: we restrict the condition to hosts, because routers usually do
988 * not have the "default router list".
990 if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
991 nd6_defifindex == ifp->if_index) {
1000 * Detect if a given IPv6 address identifies a neighbor on a given link.
1001 * XXX: should take care of the destination of a p2p link?
1004 nd6_is_addr_neighbor(addr, ifp)
1005 struct sockaddr_in6 *addr;
1009 if (nd6_is_new_addr_neighbor(addr, ifp))
1013 * Even if the address matches none of our addresses, it might be
1014 * in the neighbor cache.
1016 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
1023 * Free an nd6 llinfo entry.
1024 * Since the function would cause significant changes in the kernel, DO NOT
1025 * make it global, unless you have a strong reason for the change, and are sure
1026 * that the change is safe.
1028 static struct llinfo_nd6 *
1033 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
1034 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
1035 struct nd_defrouter *dr;
1038 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1039 * even though it is not harmful, it was not really necessary.
1043 nd6_llinfo_settimer(ln, -1);
1045 if (!ip6_forwarding) {
1048 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1051 if (dr != NULL && dr->expire &&
1052 ln->ln_state == ND6_LLINFO_STALE && gc) {
1054 * If the reason for the deletion is just garbage
1055 * collection, and the neighbor is an active default
1056 * router, do not delete it. Instead, reset the GC
1057 * timer using the router's lifetime.
1058 * Simply deleting the entry would affect default
1059 * router selection, which is not necessarily a good
1060 * thing, especially when we're using router preference
1062 * XXX: the check for ln_state would be redundant,
1063 * but we intentionally keep it just in case.
1065 if (dr->expire > time_second)
1066 nd6_llinfo_settimer(ln,
1067 (dr->expire - time_second) * hz);
1069 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1071 return (ln->ln_next);
1074 if (ln->ln_router || dr) {
1076 * rt6_flush must be called whether or not the neighbor
1077 * is in the Default Router List.
1078 * See a corresponding comment in nd6_na_input().
1080 rt6_flush(&in6, rt->rt_ifp);
1085 * Unreachablity of a router might affect the default
1086 * router selection and on-link detection of advertised
1091 * Temporarily fake the state to choose a new default
1092 * router and to perform on-link determination of
1093 * prefixes correctly.
1094 * Below the state will be set correctly,
1095 * or the entry itself will be deleted.
1097 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1100 * Since defrouter_select() does not affect the
1101 * on-link determination and MIP6 needs the check
1102 * before the default router selection, we perform
1105 pfxlist_onlink_check();
1108 * refresh default router list
1116 * Before deleting the entry, remember the next entry as the
1117 * return value. We need this because pfxlist_onlink_check() above
1118 * might have freed other entries (particularly the old next entry) as
1119 * a side effect (XXX).
1124 * Detach the route from the routing tree and the list of neighbor
1125 * caches, and disable the route entry not to be used in already
1128 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
1129 rt_mask(rt), 0, (struct rtentry **)0);
1135 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1137 * XXX cost-effective methods?
1140 nd6_nud_hint(rt, dst6, force)
1142 struct in6_addr *dst6;
1145 struct llinfo_nd6 *ln;
1148 * If the caller specified "rt", use that. Otherwise, resolve the
1149 * routing table by supplied "dst6".
1154 if ((rt = nd6_lookup(dst6, 0, NULL)) == NULL)
1158 if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
1159 (rt->rt_flags & RTF_LLINFO) == 0 ||
1160 rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
1161 rt->rt_gateway->sa_family != AF_LINK) {
1162 /* This is not a host route. */
1166 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1167 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1171 * if we get upper-layer reachability confirmation many times,
1172 * it is possible we have false information.
1176 if (ln->ln_byhint > nd6_maxnudhint)
1180 ln->ln_state = ND6_LLINFO_REACHABLE;
1181 if (!ND6_LLINFO_PERMANENT(ln)) {
1182 nd6_llinfo_settimer(ln,
1183 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1188 nd6_rtrequest(req, rt, info)
1191 struct rt_addrinfo *info; /* xxx unused */
1193 struct sockaddr *gate = rt->rt_gateway;
1194 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1195 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1196 struct ifnet *ifp = rt->rt_ifp;
1201 if ((rt->rt_flags & RTF_GATEWAY) != 0)
1204 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1206 * This is probably an interface direct route for a link
1207 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1208 * We do not need special treatment below for such a route.
1209 * Moreover, the RTF_LLINFO flag which would be set below
1210 * would annoy the ndp(8) command.
1215 if (req == RTM_RESOLVE &&
1216 (nd6_need_cache(ifp) == 0 || /* stf case */
1217 !nd6_is_new_addr_neighbor((struct sockaddr_in6 *)rt_key(rt),
1220 * FreeBSD and BSD/OS often make a cloned host route based
1221 * on a less-specific route (e.g. the default route).
1222 * If the less specific route does not have a "gateway"
1223 * (this is the case when the route just goes to a p2p or an
1224 * stf interface), we'll mistakenly make a neighbor cache for
1225 * the host route, and will see strange neighbor solicitation
1226 * for the corresponding destination. In order to avoid the
1227 * confusion, we check if the destination of the route is
1228 * a neighbor in terms of neighbor discovery, and stop the
1229 * process if not. Additionally, we remove the LLINFO flag
1230 * so that ndp(8) will not try to get the neighbor information
1231 * of the destination.
1233 rt->rt_flags &= ~RTF_LLINFO;
1240 * There is no backward compatibility :)
1242 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1243 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1244 * rt->rt_flags |= RTF_CLONING;
1246 if ((rt->rt_flags & RTF_CLONING) ||
1247 ((rt->rt_flags & RTF_LLINFO) && ln == NULL)) {
1249 * Case 1: This route should come from a route to
1250 * interface (RTF_CLONING case) or the route should be
1251 * treated as on-link but is currently not
1252 * (RTF_LLINFO && ln == NULL case).
1254 rt_setgate(rt, rt_key(rt),
1255 (struct sockaddr *)&null_sdl);
1256 gate = rt->rt_gateway;
1257 SDL(gate)->sdl_type = ifp->if_type;
1258 SDL(gate)->sdl_index = ifp->if_index;
1260 nd6_llinfo_settimer(ln, 0);
1261 if ((rt->rt_flags & RTF_CLONING) != 0)
1265 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1266 * We don't do that here since llinfo is not ready yet.
1268 * There are also couple of other things to be discussed:
1269 * - unsolicited NA code needs improvement beforehand
1270 * - RFC2461 says we MAY send multicast unsolicited NA
1271 * (7.2.6 paragraph 4), however, it also says that we
1272 * SHOULD provide a mechanism to prevent multicast NA storm.
1273 * we don't have anything like it right now.
1274 * note that the mechanism needs a mutual agreement
1275 * between proxies, which means that we need to implement
1276 * a new protocol, or a new kludge.
1277 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1278 * we need to check ip6forwarding before sending it.
1279 * (or should we allow proxy ND configuration only for
1280 * routers? there's no mention about proxy ND from hosts)
1284 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1286 * Address resolution isn't necessary for a point to
1287 * point link, so we can skip this test for a p2p link.
1289 if (gate->sa_family != AF_LINK ||
1290 gate->sa_len < sizeof(null_sdl)) {
1292 "nd6_rtrequest: bad gateway value: %s\n",
1296 SDL(gate)->sdl_type = ifp->if_type;
1297 SDL(gate)->sdl_index = ifp->if_index;
1300 break; /* This happens on a route change */
1302 * Case 2: This route may come from cloning, or a manual route
1303 * add with a LL address.
1305 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1306 rt->rt_llinfo = (caddr_t)ln;
1308 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1313 bzero(ln, sizeof(*ln));
1316 callout_init(&ln->ln_timer_ch, 0);
1318 /* this is required for "ndp" command. - shin */
1319 if (req == RTM_ADD) {
1321 * gate should have some valid AF_LINK entry,
1322 * and ln->ln_expire should have some lifetime
1323 * which is specified by ndp command.
1325 ln->ln_state = ND6_LLINFO_REACHABLE;
1329 * When req == RTM_RESOLVE, rt is created and
1330 * initialized in rtrequest(), so rt_expire is 0.
1332 ln->ln_state = ND6_LLINFO_NOSTATE;
1333 nd6_llinfo_settimer(ln, 0);
1335 rt->rt_flags |= RTF_LLINFO;
1336 ln->ln_next = llinfo_nd6.ln_next;
1337 llinfo_nd6.ln_next = ln;
1338 ln->ln_prev = &llinfo_nd6;
1339 ln->ln_next->ln_prev = ln;
1342 * check if rt_key(rt) is one of my address assigned
1345 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1346 &SIN6(rt_key(rt))->sin6_addr);
1348 caddr_t macp = nd6_ifptomac(ifp);
1349 nd6_llinfo_settimer(ln, -1);
1350 ln->ln_state = ND6_LLINFO_REACHABLE;
1353 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1354 SDL(gate)->sdl_alen = ifp->if_addrlen;
1356 if (nd6_useloopback) {
1357 rt->rt_ifp = &loif[0]; /* XXX */
1359 * Make sure rt_ifa be equal to the ifaddr
1360 * corresponding to the address.
1361 * We need this because when we refer
1362 * rt_ifa->ia6_flags in ip6_input, we assume
1363 * that the rt_ifa points to the address instead
1364 * of the loopback address.
1366 if (ifa != rt->rt_ifa) {
1367 IFAFREE(rt->rt_ifa);
1372 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1373 nd6_llinfo_settimer(ln, -1);
1374 ln->ln_state = ND6_LLINFO_REACHABLE;
1377 /* join solicited node multicast for proxy ND */
1378 if (ifp->if_flags & IFF_MULTICAST) {
1379 struct in6_addr llsol;
1382 llsol = SIN6(rt_key(rt))->sin6_addr;
1383 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1384 llsol.s6_addr32[1] = 0;
1385 llsol.s6_addr32[2] = htonl(1);
1386 llsol.s6_addr8[12] = 0xff;
1387 if (in6_setscope(&llsol, ifp, NULL))
1389 if (in6_addmulti(&llsol, ifp,
1390 &error, 0) == NULL) {
1391 char ip6buf[INET6_ADDRSTRLEN];
1392 nd6log((LOG_ERR, "%s: failed to join "
1393 "%s (errno=%d)\n", if_name(ifp),
1394 ip6_sprintf(ip6buf, &llsol),
1404 /* leave from solicited node multicast for proxy ND */
1405 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1406 (ifp->if_flags & IFF_MULTICAST) != 0) {
1407 struct in6_addr llsol;
1408 struct in6_multi *in6m;
1410 llsol = SIN6(rt_key(rt))->sin6_addr;
1411 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1412 llsol.s6_addr32[1] = 0;
1413 llsol.s6_addr32[2] = htonl(1);
1414 llsol.s6_addr8[12] = 0xff;
1415 if (in6_setscope(&llsol, ifp, NULL) == 0) {
1416 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1420 ; /* XXX: should not happen. bark here? */
1423 ln->ln_next->ln_prev = ln->ln_prev;
1424 ln->ln_prev->ln_next = ln->ln_next;
1426 nd6_llinfo_settimer(ln, -1);
1429 rt->rt_flags &= ~RTF_LLINFO;
1430 clear_llinfo_pqueue(ln);
1436 nd6_ioctl(cmd, data, ifp)
1441 struct in6_drlist *drl = (struct in6_drlist *)data;
1442 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1443 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1444 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1445 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1446 struct nd_defrouter *dr;
1447 struct nd_prefix *pr;
1449 int i = 0, error = 0;
1453 case SIOCGDRLST_IN6:
1455 * obsolete API, use sysctl under net.inet6.icmp6
1457 bzero(drl, sizeof(*drl));
1459 dr = TAILQ_FIRST(&nd_defrouter);
1460 while (dr && i < DRLSTSIZ) {
1461 drl->defrouter[i].rtaddr = dr->rtaddr;
1462 in6_clearscope(&drl->defrouter[i].rtaddr);
1464 drl->defrouter[i].flags = dr->flags;
1465 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1466 drl->defrouter[i].expire = dr->expire;
1467 drl->defrouter[i].if_index = dr->ifp->if_index;
1469 dr = TAILQ_NEXT(dr, dr_entry);
1473 case SIOCGPRLST_IN6:
1475 * obsolete API, use sysctl under net.inet6.icmp6
1477 * XXX the structure in6_prlist was changed in backward-
1478 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1479 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1482 * XXX meaning of fields, especialy "raflags", is very
1483 * differnet between RA prefix list and RR/static prefix list.
1484 * how about separating ioctls into two?
1486 bzero(oprl, sizeof(*oprl));
1488 pr = nd_prefix.lh_first;
1489 while (pr && i < PRLSTSIZ) {
1490 struct nd_pfxrouter *pfr;
1493 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1494 oprl->prefix[i].raflags = pr->ndpr_raf;
1495 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1496 oprl->prefix[i].vltime = pr->ndpr_vltime;
1497 oprl->prefix[i].pltime = pr->ndpr_pltime;
1498 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1499 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1500 oprl->prefix[i].expire = 0;
1504 /* XXX: we assume time_t is signed. */
1507 ((sizeof(maxexpire) * 8) - 1));
1508 if (pr->ndpr_vltime <
1509 maxexpire - pr->ndpr_lastupdate) {
1510 oprl->prefix[i].expire =
1511 pr->ndpr_lastupdate +
1514 oprl->prefix[i].expire = maxexpire;
1517 pfr = pr->ndpr_advrtrs.lh_first;
1521 #define RTRADDR oprl->prefix[i].advrtr[j]
1522 RTRADDR = pfr->router->rtaddr;
1523 in6_clearscope(&RTRADDR);
1527 pfr = pfr->pfr_next;
1529 oprl->prefix[i].advrtrs = j;
1530 oprl->prefix[i].origin = PR_ORIG_RA;
1538 case OSIOCGIFINFO_IN6:
1540 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1541 bzero(&ND, sizeof(ND));
1542 ND.linkmtu = IN6_LINKMTU(ifp);
1543 ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1544 ND.basereachable = ND_IFINFO(ifp)->basereachable;
1545 ND.reachable = ND_IFINFO(ifp)->reachable;
1546 ND.retrans = ND_IFINFO(ifp)->retrans;
1547 ND.flags = ND_IFINFO(ifp)->flags;
1548 ND.recalctm = ND_IFINFO(ifp)->recalctm;
1549 ND.chlim = ND_IFINFO(ifp)->chlim;
1551 case SIOCGIFINFO_IN6:
1552 ND = *ND_IFINFO(ifp);
1554 case SIOCSIFINFO_IN6:
1556 * used to change host variables from userland.
1557 * intented for a use on router to reflect RA configurations.
1559 /* 0 means 'unspecified' */
1560 if (ND.linkmtu != 0) {
1561 if (ND.linkmtu < IPV6_MMTU ||
1562 ND.linkmtu > IN6_LINKMTU(ifp)) {
1566 ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1569 if (ND.basereachable != 0) {
1570 int obasereachable = ND_IFINFO(ifp)->basereachable;
1572 ND_IFINFO(ifp)->basereachable = ND.basereachable;
1573 if (ND.basereachable != obasereachable)
1574 ND_IFINFO(ifp)->reachable =
1575 ND_COMPUTE_RTIME(ND.basereachable);
1577 if (ND.retrans != 0)
1578 ND_IFINFO(ifp)->retrans = ND.retrans;
1580 ND_IFINFO(ifp)->chlim = ND.chlim;
1582 case SIOCSIFINFO_FLAGS:
1583 ND_IFINFO(ifp)->flags = ND.flags;
1586 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1587 /* sync kernel routing table with the default router list */
1591 case SIOCSPFXFLUSH_IN6:
1593 /* flush all the prefix advertised by routers */
1594 struct nd_prefix *pr, *next;
1597 for (pr = nd_prefix.lh_first; pr; pr = next) {
1598 struct in6_ifaddr *ia, *ia_next;
1600 next = pr->ndpr_next;
1602 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1605 /* do we really have to remove addresses as well? */
1606 for (ia = in6_ifaddr; ia; ia = ia_next) {
1607 /* ia might be removed. keep the next ptr. */
1608 ia_next = ia->ia_next;
1610 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1613 if (ia->ia6_ndpr == pr)
1614 in6_purgeaddr(&ia->ia_ifa);
1621 case SIOCSRTRFLUSH_IN6:
1623 /* flush all the default routers */
1624 struct nd_defrouter *dr, *next;
1628 for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = next) {
1629 next = TAILQ_NEXT(dr, dr_entry);
1636 case SIOCGNBRINFO_IN6:
1638 struct llinfo_nd6 *ln;
1639 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1641 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1645 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1650 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1651 nbi->state = ln->ln_state;
1652 nbi->asked = ln->ln_asked;
1653 nbi->isrouter = ln->ln_router;
1654 nbi->expire = ln->ln_expire;
1659 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1660 ndif->ifindex = nd6_defifindex;
1662 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1663 return (nd6_setdefaultiface(ndif->ifindex));
1669 * Create neighbor cache entry and cache link-layer address,
1670 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1673 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1675 struct in6_addr *from;
1678 int type; /* ICMP6 type */
1679 int code; /* type dependent information */
1681 struct rtentry *rt = NULL;
1682 struct llinfo_nd6 *ln = NULL;
1684 struct sockaddr_dl *sdl = NULL;
1691 panic("ifp == NULL in nd6_cache_lladdr");
1693 panic("from == NULL in nd6_cache_lladdr");
1695 /* nothing must be updated for unspecified address */
1696 if (IN6_IS_ADDR_UNSPECIFIED(from))
1700 * Validation about ifp->if_addrlen and lladdrlen must be done in
1703 * XXX If the link does not have link-layer adderss, what should
1704 * we do? (ifp->if_addrlen == 0)
1705 * Spec says nothing in sections for RA, RS and NA. There's small
1706 * description on it in NS section (RFC 2461 7.2.3).
1709 rt = nd6_lookup(from, 0, ifp);
1711 rt = nd6_lookup(from, 1, ifp);
1714 /* do nothing if static ndp is set */
1715 if (rt->rt_flags & RTF_STATIC)
1722 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1724 (void)nd6_free(rt, 0);
1727 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1730 if (rt->rt_gateway == NULL)
1732 if (rt->rt_gateway->sa_family != AF_LINK)
1734 sdl = SDL(rt->rt_gateway);
1736 olladdr = (sdl->sdl_alen) ? 1 : 0;
1737 if (olladdr && lladdr) {
1738 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1746 * newentry olladdr lladdr llchange (*=record)
1749 * 0 n y -- (3) * STALE
1751 * 0 y y y (5) * STALE
1752 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1753 * 1 -- y -- (7) * STALE
1756 if (lladdr) { /* (3-5) and (7) */
1758 * Record source link-layer address
1759 * XXX is it dependent to ifp->if_type?
1761 sdl->sdl_alen = ifp->if_addrlen;
1762 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1766 if ((!olladdr && lladdr != NULL) || /* (3) */
1767 (olladdr && lladdr != NULL && llchange)) { /* (5) */
1769 newstate = ND6_LLINFO_STALE;
1770 } else /* (1-2,4) */
1774 if (lladdr == NULL) /* (6) */
1775 newstate = ND6_LLINFO_NOSTATE;
1777 newstate = ND6_LLINFO_STALE;
1782 * Update the state of the neighbor cache.
1784 ln->ln_state = newstate;
1786 if (ln->ln_state == ND6_LLINFO_STALE) {
1788 * XXX: since nd6_output() below will cause
1789 * state tansition to DELAY and reset the timer,
1790 * we must set the timer now, although it is actually
1793 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1796 struct mbuf *m_hold, *m_hold_next;
1799 * reset the ln_hold in advance, to explicitly
1800 * prevent a ln_hold lookup in nd6_output()
1801 * (wouldn't happen, though...)
1803 for (m_hold = ln->ln_hold, ln->ln_hold = NULL;
1804 m_hold; m_hold = m_hold_next) {
1805 m_hold_next = m_hold->m_nextpkt;
1806 m_hold->m_nextpkt = NULL;
1809 * we assume ifp is not a p2p here, so
1810 * just set the 2nd argument as the
1813 nd6_output(ifp, ifp, m_hold,
1814 (struct sockaddr_in6 *)rt_key(rt),
1818 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1819 /* probe right away */
1820 nd6_llinfo_settimer((void *)ln, 0);
1825 * ICMP6 type dependent behavior.
1827 * NS: clear IsRouter if new entry
1828 * RS: clear IsRouter
1829 * RA: set IsRouter if there's lladdr
1830 * redir: clear IsRouter if new entry
1833 * The spec says that we must set IsRouter in the following cases:
1834 * - If lladdr exist, set IsRouter. This means (1-5).
1835 * - If it is old entry (!newentry), set IsRouter. This means (7).
1836 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1837 * A quetion arises for (1) case. (1) case has no lladdr in the
1838 * neighbor cache, this is similar to (6).
1839 * This case is rare but we figured that we MUST NOT set IsRouter.
1841 * newentry olladdr lladdr llchange NS RS RA redir
1843 * 0 n n -- (1) c ? s
1844 * 0 y n -- (2) c s s
1845 * 0 n y -- (3) c s s
1848 * 1 -- n -- (6) c c c s
1849 * 1 -- y -- (7) c c s c s
1853 switch (type & 0xff) {
1854 case ND_NEIGHBOR_SOLICIT:
1856 * New entry must have is_router flag cleared.
1858 if (is_newentry) /* (6-7) */
1863 * If the icmp is a redirect to a better router, always set the
1864 * is_router flag. Otherwise, if the entry is newly created,
1865 * clear the flag. [RFC 2461, sec 8.3]
1867 if (code == ND_REDIRECT_ROUTER)
1869 else if (is_newentry) /* (6-7) */
1872 case ND_ROUTER_SOLICIT:
1874 * is_router flag must always be cleared.
1878 case ND_ROUTER_ADVERT:
1880 * Mark an entry with lladdr as a router.
1882 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1883 (is_newentry && lladdr)) { /* (7) */
1890 * When the link-layer address of a router changes, select the
1891 * best router again. In particular, when the neighbor entry is newly
1892 * created, it might affect the selection policy.
1893 * Question: can we restrict the first condition to the "is_newentry"
1895 * XXX: when we hear an RA from a new router with the link-layer
1896 * address option, defrouter_select() is called twice, since
1897 * defrtrlist_update called the function as well. However, I believe
1898 * we can compromise the overhead, since it only happens the first
1900 * XXX: although defrouter_select() should not have a bad effect
1901 * for those are not autoconfigured hosts, we explicitly avoid such
1904 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1911 nd6_slowtimo(ignored_arg)
1914 struct nd_ifinfo *nd6if;
1917 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1918 nd6_slowtimo, NULL);
1920 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1921 nd6if = ND_IFINFO(ifp);
1922 if (nd6if->basereachable && /* already initialized */
1923 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1925 * Since reachable time rarely changes by router
1926 * advertisements, we SHOULD insure that a new random
1927 * value gets recomputed at least once every few hours.
1930 nd6if->recalctm = nd6_recalc_reachtm_interval;
1931 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1937 #define senderr(e) { error = (e); goto bad;}
1939 nd6_output(ifp, origifp, m0, dst, rt0)
1941 struct ifnet *origifp;
1943 struct sockaddr_in6 *dst;
1944 struct rtentry *rt0;
1946 struct mbuf *m = m0;
1947 struct rtentry *rt = rt0;
1948 struct sockaddr_in6 *gw6 = NULL;
1949 struct llinfo_nd6 *ln = NULL;
1952 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1955 if (nd6_need_cache(ifp) == 0)
1959 * next hop determination. This routine is derived from ether_output.
1961 /* NB: the locking here is tortuous... */
1966 if ((rt->rt_flags & RTF_UP) == 0) {
1968 rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL);
1971 if (rt->rt_ifp != ifp)
1973 * XXX maybe we should update ifp too,
1974 * but the original code didn't and I
1975 * don't know what is correct here.
1979 senderr(EHOSTUNREACH);
1982 if (rt->rt_flags & RTF_GATEWAY) {
1983 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1986 * We skip link-layer address resolution and NUD
1987 * if the gateway is not a neighbor from ND point
1988 * of view, regardless of the value of nd_ifinfo.flags.
1989 * The second condition is a bit tricky; we skip
1990 * if the gateway is our own address, which is
1991 * sometimes used to install a route to a p2p link.
1993 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1994 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1997 * We allow this kind of tricky route only
1998 * when the outgoing interface is p2p.
1999 * XXX: we may need a more generic rule here.
2001 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2002 senderr(EHOSTUNREACH);
2007 if (rt->rt_gwroute == NULL)
2009 rt = rt->rt_gwroute;
2010 RT_LOCK(rt); /* NB: gwroute */
2011 if ((rt->rt_flags & RTF_UP) == 0) {
2012 rtfree(rt); /* unlock gwroute */
2016 rt = rtalloc1(rt->rt_gateway, 1, 0UL);
2018 rt0->rt_gwroute = NULL;
2021 senderr(EHOSTUNREACH);
2024 rt0->rt_gwroute = rt;
2027 senderr(EHOSTUNREACH);
2036 * Address resolution or Neighbor Unreachability Detection
2038 * At this point, the destination of the packet must be a unicast
2039 * or an anycast address(i.e. not a multicast).
2042 /* Look up the neighbor cache for the nexthop */
2043 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
2044 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2047 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2048 * the condition below is not very efficient. But we believe
2049 * it is tolerable, because this should be a rare case.
2051 if (nd6_is_addr_neighbor(dst, ifp) &&
2052 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
2053 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2055 if (ln == NULL || rt == NULL) {
2056 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
2057 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
2058 char ip6buf[INET6_ADDRSTRLEN];
2060 "nd6_output: can't allocate llinfo for %s "
2062 ip6_sprintf(ip6buf, &dst->sin6_addr), ln, rt);
2063 senderr(EIO); /* XXX: good error? */
2066 goto sendpkt; /* send anyway */
2069 /* We don't have to do link-layer address resolution on a p2p link. */
2070 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2071 ln->ln_state < ND6_LLINFO_REACHABLE) {
2072 ln->ln_state = ND6_LLINFO_STALE;
2073 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
2077 * The first time we send a packet to a neighbor whose entry is
2078 * STALE, we have to change the state to DELAY and a sets a timer to
2079 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2080 * neighbor unreachability detection on expiration.
2083 if (ln->ln_state == ND6_LLINFO_STALE) {
2085 ln->ln_state = ND6_LLINFO_DELAY;
2086 nd6_llinfo_settimer(ln, (long)nd6_delay * hz);
2090 * If the neighbor cache entry has a state other than INCOMPLETE
2091 * (i.e. its link-layer address is already resolved), just
2094 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
2098 * There is a neighbor cache entry, but no ethernet address
2099 * response yet. Append this latest packet to the end of the
2100 * packet queue in the mbuf, unless the number of the packet
2101 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen,
2102 * the oldest packet in the queue will be removed.
2104 if (ln->ln_state == ND6_LLINFO_NOSTATE)
2105 ln->ln_state = ND6_LLINFO_INCOMPLETE;
2107 struct mbuf *m_hold;
2111 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
2113 if (m_hold->m_nextpkt == NULL) {
2114 m_hold->m_nextpkt = m;
2118 while (i >= nd6_maxqueuelen) {
2119 m_hold = ln->ln_hold;
2120 ln->ln_hold = ln->ln_hold->m_nextpkt;
2129 * If there has been no NS for the neighbor after entering the
2130 * INCOMPLETE state, send the first solicitation.
2132 if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
2134 nd6_llinfo_settimer(ln,
2135 (long)ND_IFINFO(ifp)->retrans * hz / 1000);
2136 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
2141 /* discard the packet if IPv6 operation is disabled on the interface */
2142 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
2143 error = ENETDOWN; /* better error? */
2148 /* clean ipsec history once it goes out of the node */
2153 mac_create_mbuf_linklayer(ifp, m);
2155 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
2156 return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
2159 return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
2173 * XXX: we currently do not make neighbor cache on any interface
2174 * other than ARCnet, Ethernet, FDDI and GIF.
2177 * - unidirectional tunnels needs no ND
2179 switch (ifp->if_type) {
2187 #ifdef IFT_IEEE80211
2193 case IFT_GIF: /* XXX need more cases? */
2197 case IFT_PROPVIRTUAL:
2205 nd6_storelladdr(ifp, rt0, m, dst, desten)
2207 struct rtentry *rt0;
2209 struct sockaddr *dst;
2212 struct sockaddr_dl *sdl;
2216 if (m->m_flags & M_MCAST) {
2219 switch (ifp->if_type) {
2225 #ifdef IFT_IEEE80211
2230 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2235 * netbsd can use if_broadcastaddr, but we don't do so
2236 * to reduce # of ifdef.
2238 for (i = 0; i < ifp->if_addrlen; i++)
2246 return (EAFNOSUPPORT);
2251 /* this could happen, if we could not allocate memory */
2256 error = rt_check(&rt, &rt0, dst);
2263 if (rt->rt_gateway->sa_family != AF_LINK) {
2264 printf("nd6_storelladdr: something odd happens\n");
2268 sdl = SDL(rt->rt_gateway);
2269 if (sdl->sdl_alen == 0) {
2270 /* this should be impossible, but we bark here for debugging */
2271 printf("nd6_storelladdr: sdl_alen == 0\n");
2276 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2281 clear_llinfo_pqueue(ln)
2282 struct llinfo_nd6 *ln;
2284 struct mbuf *m_hold, *m_hold_next;
2286 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
2287 m_hold_next = m_hold->m_nextpkt;
2288 m_hold->m_nextpkt = NULL;
2296 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2297 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2299 SYSCTL_DECL(_net_inet6_icmp6);
2301 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2302 CTLFLAG_RD, nd6_sysctl_drlist, "");
2303 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2304 CTLFLAG_RD, nd6_sysctl_prlist, "");
2305 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen,
2306 CTLFLAG_RW, &nd6_maxqueuelen, 1, "");
2309 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2312 char buf[1024] __aligned(4);
2313 struct in6_defrouter *d, *de;
2314 struct nd_defrouter *dr;
2320 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2321 dr = TAILQ_NEXT(dr, dr_entry)) {
2322 d = (struct in6_defrouter *)buf;
2323 de = (struct in6_defrouter *)(buf + sizeof(buf));
2326 bzero(d, sizeof(*d));
2327 d->rtaddr.sin6_family = AF_INET6;
2328 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2329 d->rtaddr.sin6_addr = dr->rtaddr;
2330 sa6_recoverscope(&d->rtaddr);
2331 d->flags = dr->flags;
2332 d->rtlifetime = dr->rtlifetime;
2333 d->expire = dr->expire;
2334 d->if_index = dr->ifp->if_index;
2336 panic("buffer too short");
2338 error = SYSCTL_OUT(req, buf, sizeof(*d));
2347 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2350 char buf[1024] __aligned(4);
2351 struct in6_prefix *p, *pe;
2352 struct nd_prefix *pr;
2353 char ip6buf[INET6_ADDRSTRLEN];
2359 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2362 struct sockaddr_in6 *sin6, *s6;
2363 struct nd_pfxrouter *pfr;
2365 p = (struct in6_prefix *)buf;
2366 pe = (struct in6_prefix *)(buf + sizeof(buf));
2369 bzero(p, sizeof(*p));
2370 sin6 = (struct sockaddr_in6 *)(p + 1);
2372 p->prefix = pr->ndpr_prefix;
2373 if (sa6_recoverscope(&p->prefix)) {
2375 "scope error in prefix list (%s)\n",
2376 ip6_sprintf(ip6buf, &p->prefix.sin6_addr));
2377 /* XXX: press on... */
2379 p->raflags = pr->ndpr_raf;
2380 p->prefixlen = pr->ndpr_plen;
2381 p->vltime = pr->ndpr_vltime;
2382 p->pltime = pr->ndpr_pltime;
2383 p->if_index = pr->ndpr_ifp->if_index;
2384 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2389 /* XXX: we assume time_t is signed. */
2392 ((sizeof(maxexpire) * 8) - 1));
2393 if (pr->ndpr_vltime <
2394 maxexpire - pr->ndpr_lastupdate) {
2395 p->expire = pr->ndpr_lastupdate +
2398 p->expire = maxexpire;
2400 p->refcnt = pr->ndpr_refcnt;
2401 p->flags = pr->ndpr_stateflags;
2402 p->origin = PR_ORIG_RA;
2404 for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2405 pfr = pfr->pfr_next) {
2406 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2410 s6 = &sin6[advrtrs];
2411 bzero(s6, sizeof(*s6));
2412 s6->sin6_family = AF_INET6;
2413 s6->sin6_len = sizeof(*sin6);
2414 s6->sin6_addr = pfr->router->rtaddr;
2415 if (sa6_recoverscope(s6)) {
2418 "prefix list (%s)\n",
2420 &pfr->router->rtaddr));
2424 p->advrtrs = advrtrs;
2426 panic("buffer too short");
2428 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2429 error = SYSCTL_OUT(req, buf, advance);