2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/callout.h>
43 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
48 #include <sys/kernel.h>
49 #include <sys/protosw.h>
50 #include <sys/errno.h>
51 #include <sys/syslog.h>
52 #include <sys/queue.h>
53 #include <sys/sysctl.h>
56 #include <net/if_arc.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/iso88025.h>
61 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/if_ether.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/ip6_var.h>
68 #include <netinet6/scope6_var.h>
69 #include <netinet6/nd6.h>
70 #include <netinet/icmp6.h>
72 #include <sys/limits.h>
74 #include <net/net_osdep.h>
76 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
77 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
79 #define SIN6(s) ((struct sockaddr_in6 *)s)
80 #define SDL(s) ((struct sockaddr_dl *)s)
83 int nd6_prune = 1; /* walk list every 1 seconds */
84 int nd6_delay = 5; /* delay first probe time 5 second */
85 int nd6_umaxtries = 3; /* maximum unicast query */
86 int nd6_mmaxtries = 3; /* maximum multicast query */
87 int nd6_useloopback = 1; /* use loopback interface for local traffic */
88 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
90 /* preventing too many loops in ND option parsing */
91 int nd6_maxndopt = 10; /* max # of ND options allowed */
93 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
94 int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
103 static int nd6_inuse, nd6_allocated;
105 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
106 struct nd_drhead nd_defrouter;
107 struct nd_prhead nd_prefix = { 0 };
109 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
110 static struct sockaddr_in6 all1_sa;
112 static int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *,
114 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
115 static void nd6_slowtimo(void *);
116 static int regen_tmpaddr(struct in6_ifaddr *);
117 static struct llinfo_nd6 *nd6_free(struct rtentry *, int);
118 static void nd6_llinfo_timer(void *);
119 static void clear_llinfo_pqueue(struct llinfo_nd6 *);
121 struct callout nd6_slowtimo_ch;
122 struct callout nd6_timer_ch;
123 extern struct callout in6_tmpaddrtimer_ch;
128 static int nd6_init_done = 0;
132 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
136 all1_sa.sin6_family = AF_INET6;
137 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
138 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
139 all1_sa.sin6_addr.s6_addr[i] = 0xff;
141 /* initialization of the default router list */
142 TAILQ_INIT(&nd_defrouter);
147 callout_init(&nd6_slowtimo_ch, 0);
148 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
156 struct nd_ifinfo *nd;
158 nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
159 bzero(nd, sizeof(*nd));
163 nd->chlim = IPV6_DEFHLIM;
164 nd->basereachable = REACHABLE_TIME;
165 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
166 nd->retrans = RETRANS_TIMER;
168 * Note that the default value of ip6_accept_rtadv is 0, which means
169 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
172 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
174 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
175 nd6_setmtu0(ifp, nd);
182 struct nd_ifinfo *nd;
189 * Reset ND level link MTU. This function is called when the physical MTU
190 * changes, which means we might have to adjust the ND level MTU.
197 nd6_setmtu0(ifp, ND_IFINFO(ifp));
200 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
202 nd6_setmtu0(ifp, ndi)
204 struct nd_ifinfo *ndi;
208 omaxmtu = ndi->maxmtu;
210 switch (ifp->if_type) {
212 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
215 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
218 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
221 ndi->maxmtu = ifp->if_mtu;
226 * Decreasing the interface MTU under IPV6 minimum MTU may cause
227 * undesirable situation. We thus notify the operator of the change
228 * explicitly. The check for omaxmtu is necessary to restrict the
229 * log to the case of changing the MTU, not initializing it.
231 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
232 log(LOG_NOTICE, "nd6_setmtu0: "
233 "new link MTU on %s (%lu) is too small for IPv6\n",
234 if_name(ifp), (unsigned long)ndi->maxmtu);
237 if (ndi->maxmtu > in6_maxmtu)
238 in6_setmaxmtu(); /* check all interfaces just in case */
244 nd6_option_init(opt, icmp6len, ndopts)
247 union nd_opts *ndopts;
250 bzero(ndopts, sizeof(*ndopts));
251 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
253 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
256 ndopts->nd_opts_done = 1;
257 ndopts->nd_opts_search = NULL;
262 * Take one ND option.
266 union nd_opts *ndopts;
268 struct nd_opt_hdr *nd_opt;
272 panic("ndopts == NULL in nd6_option");
273 if (ndopts->nd_opts_last == NULL)
274 panic("uninitialized ndopts in nd6_option");
275 if (ndopts->nd_opts_search == NULL)
277 if (ndopts->nd_opts_done)
280 nd_opt = ndopts->nd_opts_search;
282 /* make sure nd_opt_len is inside the buffer */
283 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
284 bzero(ndopts, sizeof(*ndopts));
288 olen = nd_opt->nd_opt_len << 3;
291 * Message validation requires that all included
292 * options have a length that is greater than zero.
294 bzero(ndopts, sizeof(*ndopts));
298 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
299 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
300 /* option overruns the end of buffer, invalid */
301 bzero(ndopts, sizeof(*ndopts));
303 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
304 /* reached the end of options chain */
305 ndopts->nd_opts_done = 1;
306 ndopts->nd_opts_search = NULL;
312 * Parse multiple ND options.
313 * This function is much easier to use, for ND routines that do not need
314 * multiple options of the same type.
318 union nd_opts *ndopts;
320 struct nd_opt_hdr *nd_opt;
324 panic("ndopts == NULL in nd6_options");
325 if (ndopts->nd_opts_last == NULL)
326 panic("uninitialized ndopts in nd6_options");
327 if (ndopts->nd_opts_search == NULL)
331 nd_opt = nd6_option(ndopts);
332 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
334 * Message validation requires that all included
335 * options have a length that is greater than zero.
337 icmp6stat.icp6s_nd_badopt++;
338 bzero(ndopts, sizeof(*ndopts));
345 switch (nd_opt->nd_opt_type) {
346 case ND_OPT_SOURCE_LINKADDR:
347 case ND_OPT_TARGET_LINKADDR:
349 case ND_OPT_REDIRECTED_HEADER:
350 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
352 "duplicated ND6 option found (type=%d)\n",
353 nd_opt->nd_opt_type));
356 ndopts->nd_opt_array[nd_opt->nd_opt_type]
360 case ND_OPT_PREFIX_INFORMATION:
361 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
362 ndopts->nd_opt_array[nd_opt->nd_opt_type]
365 ndopts->nd_opts_pi_end =
366 (struct nd_opt_prefix_info *)nd_opt;
370 * Unknown options must be silently ignored,
371 * to accomodate future extension to the protocol.
374 "nd6_options: unsupported option %d - "
375 "option ignored\n", nd_opt->nd_opt_type));
380 if (i > nd6_maxndopt) {
381 icmp6stat.icp6s_nd_toomanyopt++;
382 nd6log((LOG_INFO, "too many loop in nd opt\n"));
386 if (ndopts->nd_opts_done)
394 * ND6 timer routine to handle ND6 entries
397 nd6_llinfo_settimer(ln, tick)
398 struct llinfo_nd6 *ln;
404 callout_stop(&ln->ln_timer_ch);
406 ln->ln_expire = time_second + tick / hz;
407 if (tick > INT_MAX) {
408 ln->ln_ntick = tick - INT_MAX;
409 callout_reset(&ln->ln_timer_ch, INT_MAX,
410 nd6_llinfo_timer, ln);
413 callout_reset(&ln->ln_timer_ch, tick,
414 nd6_llinfo_timer, ln);
420 nd6_llinfo_timer(arg)
423 struct llinfo_nd6 *ln;
425 struct in6_addr *dst;
427 struct nd_ifinfo *ndi = NULL;
429 ln = (struct llinfo_nd6 *)arg;
431 if (ln->ln_ntick > 0) {
432 if (ln->ln_ntick > INT_MAX) {
433 ln->ln_ntick -= INT_MAX;
434 nd6_llinfo_settimer(ln, INT_MAX);
437 nd6_llinfo_settimer(ln, ln->ln_ntick);
442 if ((rt = ln->ln_rt) == NULL)
443 panic("ln->ln_rt == NULL");
444 if ((ifp = rt->rt_ifp) == NULL)
445 panic("ln->ln_rt->rt_ifp == NULL");
446 ndi = ND_IFINFO(ifp);
449 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
450 panic("rt_llinfo(%p) is not equal to ln(%p)",
452 if (rt_key(rt) == NULL)
453 panic("rt key is NULL in nd6_timer(ln=%p)", ln);
455 dst = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
457 switch (ln->ln_state) {
458 case ND6_LLINFO_INCOMPLETE:
459 if (ln->ln_asked < nd6_mmaxtries) {
461 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
462 nd6_ns_output(ifp, NULL, dst, ln, 0);
464 struct mbuf *m = ln->ln_hold;
469 * assuming every packet in ln_hold has the
474 icmp6_error2(m, ICMP6_DST_UNREACH,
475 ICMP6_DST_UNREACH_ADDR, 0, rt->rt_ifp);
478 clear_llinfo_pqueue(ln);
481 (void)nd6_free(rt, 0);
485 case ND6_LLINFO_REACHABLE:
486 if (!ND6_LLINFO_PERMANENT(ln)) {
487 ln->ln_state = ND6_LLINFO_STALE;
488 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
492 case ND6_LLINFO_STALE:
493 /* Garbage Collection(RFC 2461 5.3) */
494 if (!ND6_LLINFO_PERMANENT(ln)) {
495 (void)nd6_free(rt, 1);
500 case ND6_LLINFO_DELAY:
501 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
504 ln->ln_state = ND6_LLINFO_PROBE;
505 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
506 nd6_ns_output(ifp, dst, dst, ln, 0);
508 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
509 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
512 case ND6_LLINFO_PROBE:
513 if (ln->ln_asked < nd6_umaxtries) {
515 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
516 nd6_ns_output(ifp, dst, dst, ln, 0);
517 } else if (rt->rt_ifa != NULL &&
518 rt->rt_ifa->ifa_addr->sa_family == AF_INET6 &&
519 (((struct in6_ifaddr *)rt->rt_ifa)->ia_flags & IFA_ROUTE)) {
521 * This is an unreachable neighbor whose address is
522 * specified as the destination of a p2p interface
523 * (see in6_ifinit()). We should not free the entry
524 * since this is sort of a "static" entry generated
525 * via interface address configuration.
528 ln->ln_expire = 0; /* make it permanent */
529 ln->ln_state = ND6_LLINFO_STALE;
531 (void)nd6_free(rt, 0);
540 * ND6 timer routine to expire default route list and prefix list
543 nd6_timer(ignored_arg)
547 struct nd_defrouter *dr;
548 struct nd_prefix *pr;
549 struct in6_ifaddr *ia6, *nia6;
550 struct in6_addrlifetime *lt6;
552 callout_reset(&nd6_timer_ch, nd6_prune * hz,
555 /* expire default router list */
557 dr = TAILQ_FIRST(&nd_defrouter);
559 if (dr->expire && dr->expire < time_second) {
560 struct nd_defrouter *t;
561 t = TAILQ_NEXT(dr, dr_entry);
565 dr = TAILQ_NEXT(dr, dr_entry);
570 * expire interface addresses.
571 * in the past the loop was inside prefix expiry processing.
572 * However, from a stricter speci-confrmance standpoint, we should
573 * rather separate address lifetimes and prefix lifetimes.
576 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
578 /* check address lifetime */
579 lt6 = &ia6->ia6_lifetime;
580 if (IFA6_IS_INVALID(ia6)) {
584 * If the expiring address is temporary, try
585 * regenerating a new one. This would be useful when
586 * we suspended a laptop PC, then turned it on after a
587 * period that could invalidate all temporary
588 * addresses. Although we may have to restart the
589 * loop (see below), it must be after purging the
590 * address. Otherwise, we'd see an infinite loop of
593 if (ip6_use_tempaddr &&
594 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
595 if (regen_tmpaddr(ia6) == 0)
599 in6_purgeaddr(&ia6->ia_ifa);
602 goto addrloop; /* XXX: see below */
603 } else if (IFA6_IS_DEPRECATED(ia6)) {
604 int oldflags = ia6->ia6_flags;
606 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
609 * If a temporary address has just become deprecated,
610 * regenerate a new one if possible.
612 if (ip6_use_tempaddr &&
613 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
614 (oldflags & IN6_IFF_DEPRECATED) == 0) {
616 if (regen_tmpaddr(ia6) == 0) {
618 * A new temporary address is
620 * XXX: this means the address chain
621 * has changed while we are still in
622 * the loop. Although the change
623 * would not cause disaster (because
624 * it's not a deletion, but an
625 * addition,) we'd rather restart the
626 * loop just for safety. Or does this
627 * significantly reduce performance??
634 * A new RA might have made a deprecated address
637 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
641 /* expire prefix list */
642 pr = nd_prefix.lh_first;
645 * check prefix lifetime.
646 * since pltime is just for autoconf, pltime processing for
647 * prefix is not necessary.
649 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
650 time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
655 * address expiration and prefix expiration are
656 * separate. NEVER perform in6_purgeaddr here.
669 struct in6_ifaddr *ia6; /* deprecated/invalidated temporary address */
673 struct in6_ifaddr *public_ifa6 = NULL;
675 ifp = ia6->ia_ifa.ifa_ifp;
676 for (ifa = ifp->if_addrlist.tqh_first; ifa;
677 ifa = ifa->ifa_list.tqe_next) {
678 struct in6_ifaddr *it6;
680 if (ifa->ifa_addr->sa_family != AF_INET6)
683 it6 = (struct in6_ifaddr *)ifa;
685 /* ignore no autoconf addresses. */
686 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
689 /* ignore autoconf addresses with different prefixes. */
690 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
694 * Now we are looking at an autoconf address with the same
695 * prefix as ours. If the address is temporary and is still
696 * preferred, do not create another one. It would be rare, but
697 * could happen, for example, when we resume a laptop PC after
700 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
701 !IFA6_IS_DEPRECATED(it6)) {
707 * This is a public autoconf address that has the same prefix
708 * as ours. If it is preferred, keep it. We can't break the
709 * loop here, because there may be a still-preferred temporary
710 * address with the prefix.
712 if (!IFA6_IS_DEPRECATED(it6))
716 if (public_ifa6 != NULL) {
719 if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
720 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
721 " tmp addr,errno=%d\n", e);
731 * Nuke neighbor cache/prefix/default router management table, right before
738 struct llinfo_nd6 *ln, *nln;
739 struct nd_defrouter *dr, *ndr;
740 struct nd_prefix *pr, *npr;
743 * Nuke default router list entries toward ifp.
744 * We defer removal of default router list entries that is installed
745 * in the routing table, in order to keep additional side effects as
748 for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
749 ndr = TAILQ_NEXT(dr, dr_entry);
757 for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
758 ndr = TAILQ_NEXT(dr, dr_entry);
766 /* Nuke prefix list entries toward ifp */
767 for (pr = nd_prefix.lh_first; pr; pr = npr) {
769 if (pr->ndpr_ifp == ifp) {
771 * Because if_detach() does *not* release prefixes
772 * while purging addresses the reference count will
773 * still be above zero. We therefore reset it to
774 * make sure that the prefix really gets purged.
779 * Previously, pr->ndpr_addr is removed as well,
780 * but I strongly believe we don't have to do it.
781 * nd6_purge() is only called from in6_ifdetach(),
782 * which removes all the associated interface addresses
784 * (jinmei@kame.net 20010129)
790 /* cancel default outgoing interface setting */
791 if (nd6_defifindex == ifp->if_index)
792 nd6_setdefaultiface(0);
794 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
795 /* refresh default router list */
800 * Nuke neighbor cache entries for the ifp.
801 * Note that rt->rt_ifp may not be the same as ifp,
802 * due to KAME goto ours hack. See RTM_RESOLVE case in
803 * nd6_rtrequest(), and ip6_input().
805 ln = llinfo_nd6.ln_next;
806 while (ln && ln != &llinfo_nd6) {
808 struct sockaddr_dl *sdl;
812 if (rt && rt->rt_gateway &&
813 rt->rt_gateway->sa_family == AF_LINK) {
814 sdl = (struct sockaddr_dl *)rt->rt_gateway;
815 if (sdl->sdl_index == ifp->if_index)
816 nln = nd6_free(rt, 0);
823 nd6_lookup(addr6, create, ifp)
824 struct in6_addr *addr6;
829 struct sockaddr_in6 sin6;
831 bzero(&sin6, sizeof(sin6));
832 sin6.sin6_len = sizeof(struct sockaddr_in6);
833 sin6.sin6_family = AF_INET6;
834 sin6.sin6_addr = *addr6;
835 rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL);
837 if ((rt->rt_flags & RTF_LLINFO) == 0 && create) {
839 * This is the case for the default route.
840 * If we want to create a neighbor cache for the
841 * address, we should free the route for the
842 * destination and allocate an interface route.
853 * If no route is available and create is set,
854 * we allocate a host route for the destination
855 * and treat it like an interface route.
856 * This hack is necessary for a neighbor which can't
857 * be covered by our own prefix.
860 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
865 * Create a new route. RTF_LLINFO is necessary
866 * to create a Neighbor Cache entry for the
867 * destination in nd6_rtrequest which will be
868 * called in rtrequest via ifa->ifa_rtrequest.
870 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
871 ifa->ifa_addr, (struct sockaddr *)&all1_sa,
872 (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
873 ~RTF_CLONING, &rt)) != 0) {
875 "nd6_lookup: failed to add route for a "
876 "neighbor(%s), errno=%d\n",
877 ip6_sprintf(addr6), e);
883 struct llinfo_nd6 *ln =
884 (struct llinfo_nd6 *)rt->rt_llinfo;
885 ln->ln_state = ND6_LLINFO_NOSTATE;
893 * Validation for the entry.
894 * Note that the check for rt_llinfo is necessary because a cloned
895 * route from a parent route that has the L flag (e.g. the default
896 * route to a p2p interface) may have the flag, too, while the
897 * destination is not actually a neighbor.
898 * XXX: we can't use rt->rt_ifp to check for the interface, since
899 * it might be the loopback interface if the entry is for our
900 * own address on a non-loopback interface. Instead, we should
901 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
903 * Note also that ifa_ifp and ifp may differ when we connect two
904 * interfaces to a same link, install a link prefix to an interface,
905 * and try to install a neighbor cache on an interface that does not
906 * have a route to the prefix.
908 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
909 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
910 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
913 "nd6_lookup: failed to lookup %s (if = %s)\n",
915 ifp ? if_name(ifp) : "unspec"));
920 RT_UNLOCK(rt); /* XXX not ready to return rt locked */
925 * Test whether a given IPv6 address is a neighbor or not, ignoring
926 * the actual neighbor cache. The neighbor cache is ignored in order
927 * to not reenter the routing code from within itself.
930 nd6_is_new_addr_neighbor(addr, ifp)
931 struct sockaddr_in6 *addr;
934 struct nd_prefix *pr;
935 struct ifaddr *dstaddr;
938 * A link-local address is always a neighbor.
939 * XXX: a link does not necessarily specify a single interface.
941 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
942 struct sockaddr_in6 sin6_copy;
946 * We need sin6_copy since sa6_recoverscope() may modify the
950 if (sa6_recoverscope(&sin6_copy))
951 return (0); /* XXX: should be impossible */
952 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
954 if (sin6_copy.sin6_scope_id == zone)
961 * If the address matches one of our addresses,
962 * it should be a neighbor.
963 * If the address matches one of our on-link prefixes, it should be a
966 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
967 if (pr->ndpr_ifp != ifp)
970 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
973 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
974 &addr->sin6_addr, &pr->ndpr_mask))
979 * If the address is assigned on the node of the other side of
980 * a p2p interface, the address should be a neighbor.
982 dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr);
983 if ((dstaddr != NULL) && (dstaddr->ifa_ifp == ifp))
987 * If the default router list is empty, all addresses are regarded
988 * as on-link, and thus, as a neighbor.
989 * XXX: we restrict the condition to hosts, because routers usually do
990 * not have the "default router list".
992 if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
993 nd6_defifindex == ifp->if_index) {
1002 * Detect if a given IPv6 address identifies a neighbor on a given link.
1003 * XXX: should take care of the destination of a p2p link?
1006 nd6_is_addr_neighbor(addr, ifp)
1007 struct sockaddr_in6 *addr;
1011 if (nd6_is_new_addr_neighbor(addr, ifp))
1015 * Even if the address matches none of our addresses, it might be
1016 * in the neighbor cache.
1018 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
1025 * Free an nd6 llinfo entry.
1026 * Since the function would cause significant changes in the kernel, DO NOT
1027 * make it global, unless you have a strong reason for the change, and are sure
1028 * that the change is safe.
1030 static struct llinfo_nd6 *
1035 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
1036 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
1037 struct nd_defrouter *dr;
1040 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1041 * even though it is not harmful, it was not really necessary.
1045 nd6_llinfo_settimer(ln, -1);
1047 if (!ip6_forwarding) {
1050 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1053 if (dr != NULL && dr->expire &&
1054 ln->ln_state == ND6_LLINFO_STALE && gc) {
1056 * If the reason for the deletion is just garbage
1057 * collection, and the neighbor is an active default
1058 * router, do not delete it. Instead, reset the GC
1059 * timer using the router's lifetime.
1060 * Simply deleting the entry would affect default
1061 * router selection, which is not necessarily a good
1062 * thing, especially when we're using router preference
1064 * XXX: the check for ln_state would be redundant,
1065 * but we intentionally keep it just in case.
1067 if (dr->expire > time_second)
1068 nd6_llinfo_settimer(ln,
1069 (dr->expire - time_second) * hz);
1071 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1073 return (ln->ln_next);
1076 if (ln->ln_router || dr) {
1078 * rt6_flush must be called whether or not the neighbor
1079 * is in the Default Router List.
1080 * See a corresponding comment in nd6_na_input().
1082 rt6_flush(&in6, rt->rt_ifp);
1087 * Unreachablity of a router might affect the default
1088 * router selection and on-link detection of advertised
1093 * Temporarily fake the state to choose a new default
1094 * router and to perform on-link determination of
1095 * prefixes correctly.
1096 * Below the state will be set correctly,
1097 * or the entry itself will be deleted.
1099 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1102 * Since defrouter_select() does not affect the
1103 * on-link determination and MIP6 needs the check
1104 * before the default router selection, we perform
1107 pfxlist_onlink_check();
1110 * refresh default router list
1118 * Before deleting the entry, remember the next entry as the
1119 * return value. We need this because pfxlist_onlink_check() above
1120 * might have freed other entries (particularly the old next entry) as
1121 * a side effect (XXX).
1126 * Detach the route from the routing tree and the list of neighbor
1127 * caches, and disable the route entry not to be used in already
1130 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
1131 rt_mask(rt), 0, (struct rtentry **)0);
1137 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1139 * XXX cost-effective methods?
1142 nd6_nud_hint(rt, dst6, force)
1144 struct in6_addr *dst6;
1147 struct llinfo_nd6 *ln;
1150 * If the caller specified "rt", use that. Otherwise, resolve the
1151 * routing table by supplied "dst6".
1156 if ((rt = nd6_lookup(dst6, 0, NULL)) == NULL)
1160 if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
1161 (rt->rt_flags & RTF_LLINFO) == 0 ||
1162 rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
1163 rt->rt_gateway->sa_family != AF_LINK) {
1164 /* This is not a host route. */
1168 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1169 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1173 * if we get upper-layer reachability confirmation many times,
1174 * it is possible we have false information.
1178 if (ln->ln_byhint > nd6_maxnudhint)
1182 ln->ln_state = ND6_LLINFO_REACHABLE;
1183 if (!ND6_LLINFO_PERMANENT(ln)) {
1184 nd6_llinfo_settimer(ln,
1185 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1190 nd6_rtrequest(req, rt, info)
1193 struct rt_addrinfo *info; /* xxx unused */
1195 struct sockaddr *gate = rt->rt_gateway;
1196 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1197 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1198 struct ifnet *ifp = rt->rt_ifp;
1203 if ((rt->rt_flags & RTF_GATEWAY) != 0)
1206 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1208 * This is probably an interface direct route for a link
1209 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1210 * We do not need special treatment below for such a route.
1211 * Moreover, the RTF_LLINFO flag which would be set below
1212 * would annoy the ndp(8) command.
1217 if (req == RTM_RESOLVE &&
1218 (nd6_need_cache(ifp) == 0 || /* stf case */
1219 !nd6_is_new_addr_neighbor((struct sockaddr_in6 *)rt_key(rt),
1222 * FreeBSD and BSD/OS often make a cloned host route based
1223 * on a less-specific route (e.g. the default route).
1224 * If the less specific route does not have a "gateway"
1225 * (this is the case when the route just goes to a p2p or an
1226 * stf interface), we'll mistakenly make a neighbor cache for
1227 * the host route, and will see strange neighbor solicitation
1228 * for the corresponding destination. In order to avoid the
1229 * confusion, we check if the destination of the route is
1230 * a neighbor in terms of neighbor discovery, and stop the
1231 * process if not. Additionally, we remove the LLINFO flag
1232 * so that ndp(8) will not try to get the neighbor information
1233 * of the destination.
1235 rt->rt_flags &= ~RTF_LLINFO;
1242 * There is no backward compatibility :)
1244 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1245 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1246 * rt->rt_flags |= RTF_CLONING;
1248 if ((rt->rt_flags & RTF_CLONING) ||
1249 ((rt->rt_flags & RTF_LLINFO) && ln == NULL)) {
1251 * Case 1: This route should come from a route to
1252 * interface (RTF_CLONING case) or the route should be
1253 * treated as on-link but is currently not
1254 * (RTF_LLINFO && ln == NULL case).
1256 rt_setgate(rt, rt_key(rt),
1257 (struct sockaddr *)&null_sdl);
1258 gate = rt->rt_gateway;
1259 SDL(gate)->sdl_type = ifp->if_type;
1260 SDL(gate)->sdl_index = ifp->if_index;
1262 nd6_llinfo_settimer(ln, 0);
1263 if ((rt->rt_flags & RTF_CLONING) != 0)
1267 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1268 * We don't do that here since llinfo is not ready yet.
1270 * There are also couple of other things to be discussed:
1271 * - unsolicited NA code needs improvement beforehand
1272 * - RFC2461 says we MAY send multicast unsolicited NA
1273 * (7.2.6 paragraph 4), however, it also says that we
1274 * SHOULD provide a mechanism to prevent multicast NA storm.
1275 * we don't have anything like it right now.
1276 * note that the mechanism needs a mutual agreement
1277 * between proxies, which means that we need to implement
1278 * a new protocol, or a new kludge.
1279 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1280 * we need to check ip6forwarding before sending it.
1281 * (or should we allow proxy ND configuration only for
1282 * routers? there's no mention about proxy ND from hosts)
1286 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1288 * Address resolution isn't necessary for a point to
1289 * point link, so we can skip this test for a p2p link.
1291 if (gate->sa_family != AF_LINK ||
1292 gate->sa_len < sizeof(null_sdl)) {
1294 "nd6_rtrequest: bad gateway value: %s\n",
1298 SDL(gate)->sdl_type = ifp->if_type;
1299 SDL(gate)->sdl_index = ifp->if_index;
1302 break; /* This happens on a route change */
1304 * Case 2: This route may come from cloning, or a manual route
1305 * add with a LL address.
1307 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1308 rt->rt_llinfo = (caddr_t)ln;
1310 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1315 bzero(ln, sizeof(*ln));
1318 callout_init(&ln->ln_timer_ch, 0);
1320 /* this is required for "ndp" command. - shin */
1321 if (req == RTM_ADD) {
1323 * gate should have some valid AF_LINK entry,
1324 * and ln->ln_expire should have some lifetime
1325 * which is specified by ndp command.
1327 ln->ln_state = ND6_LLINFO_REACHABLE;
1331 * When req == RTM_RESOLVE, rt is created and
1332 * initialized in rtrequest(), so rt_expire is 0.
1334 ln->ln_state = ND6_LLINFO_NOSTATE;
1335 nd6_llinfo_settimer(ln, 0);
1337 rt->rt_flags |= RTF_LLINFO;
1338 ln->ln_next = llinfo_nd6.ln_next;
1339 llinfo_nd6.ln_next = ln;
1340 ln->ln_prev = &llinfo_nd6;
1341 ln->ln_next->ln_prev = ln;
1344 * check if rt_key(rt) is one of my address assigned
1347 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1348 &SIN6(rt_key(rt))->sin6_addr);
1350 caddr_t macp = nd6_ifptomac(ifp);
1351 nd6_llinfo_settimer(ln, -1);
1352 ln->ln_state = ND6_LLINFO_REACHABLE;
1355 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1356 SDL(gate)->sdl_alen = ifp->if_addrlen;
1358 if (nd6_useloopback) {
1359 rt->rt_ifp = &loif[0]; /* XXX */
1361 * Make sure rt_ifa be equal to the ifaddr
1362 * corresponding to the address.
1363 * We need this because when we refer
1364 * rt_ifa->ia6_flags in ip6_input, we assume
1365 * that the rt_ifa points to the address instead
1366 * of the loopback address.
1368 if (ifa != rt->rt_ifa) {
1369 IFAFREE(rt->rt_ifa);
1374 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1375 nd6_llinfo_settimer(ln, -1);
1376 ln->ln_state = ND6_LLINFO_REACHABLE;
1379 /* join solicited node multicast for proxy ND */
1380 if (ifp->if_flags & IFF_MULTICAST) {
1381 struct in6_addr llsol;
1384 llsol = SIN6(rt_key(rt))->sin6_addr;
1385 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1386 llsol.s6_addr32[1] = 0;
1387 llsol.s6_addr32[2] = htonl(1);
1388 llsol.s6_addr8[12] = 0xff;
1389 if (in6_setscope(&llsol, ifp, NULL))
1391 if (in6_addmulti(&llsol, ifp,
1392 &error, 0) == NULL) {
1393 nd6log((LOG_ERR, "%s: failed to join "
1394 "%s (errno=%d)\n", if_name(ifp),
1395 ip6_sprintf(&llsol), error));
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 */
2014 rt0->rt_gwroute = NULL;
2017 rt = rtalloc1(rt->rt_gateway, 1, 0UL);
2021 senderr(EHOSTUNREACH);
2024 if (rt0->rt_gwroute != NULL)
2025 RTFREE(rt0->rt_gwroute);
2026 rt0->rt_gwroute = rt;
2029 senderr(EHOSTUNREACH);
2038 * Address resolution or Neighbor Unreachability Detection
2040 * At this point, the destination of the packet must be a unicast
2041 * or an anycast address(i.e. not a multicast).
2044 /* Look up the neighbor cache for the nexthop */
2045 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
2046 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2049 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2050 * the condition below is not very efficient. But we believe
2051 * it is tolerable, because this should be a rare case.
2053 if (nd6_is_addr_neighbor(dst, ifp) &&
2054 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
2055 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2057 if (ln == NULL || rt == NULL) {
2058 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
2059 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
2061 "nd6_output: can't allocate llinfo for %s "
2063 ip6_sprintf(&dst->sin6_addr), ln, rt);
2064 senderr(EIO); /* XXX: good error? */
2067 goto sendpkt; /* send anyway */
2070 /* We don't have to do link-layer address resolution on a p2p link. */
2071 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2072 ln->ln_state < ND6_LLINFO_REACHABLE) {
2073 ln->ln_state = ND6_LLINFO_STALE;
2074 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
2078 * The first time we send a packet to a neighbor whose entry is
2079 * STALE, we have to change the state to DELAY and a sets a timer to
2080 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2081 * neighbor unreachability detection on expiration.
2084 if (ln->ln_state == ND6_LLINFO_STALE) {
2086 ln->ln_state = ND6_LLINFO_DELAY;
2087 nd6_llinfo_settimer(ln, (long)nd6_delay * hz);
2091 * If the neighbor cache entry has a state other than INCOMPLETE
2092 * (i.e. its link-layer address is already resolved), just
2095 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
2099 * There is a neighbor cache entry, but no ethernet address
2100 * response yet. Append this latest packet to the end of the
2101 * packet queue in the mbuf, unless the number of the packet
2102 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen,
2103 * the oldest packet in the queue will be removed.
2105 if (ln->ln_state == ND6_LLINFO_NOSTATE)
2106 ln->ln_state = ND6_LLINFO_INCOMPLETE;
2108 struct mbuf *m_hold;
2112 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
2114 if (m_hold->m_nextpkt == NULL) {
2115 m_hold->m_nextpkt = m;
2119 while (i >= nd6_maxqueuelen) {
2120 m_hold = ln->ln_hold;
2121 ln->ln_hold = ln->ln_hold->m_nextpkt;
2130 * If there has been no NS for the neighbor after entering the
2131 * INCOMPLETE state, send the first solicitation.
2133 if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
2135 nd6_llinfo_settimer(ln,
2136 (long)ND_IFINFO(ifp)->retrans * hz / 1000);
2137 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
2142 /* discard the packet if IPv6 operation is disabled on the interface */
2143 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
2144 error = ENETDOWN; /* better error? */
2149 /* clean ipsec history once it goes out of the node */
2154 mac_create_mbuf_linklayer(ifp, m);
2156 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
2157 return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
2160 return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
2174 * XXX: we currently do not make neighbor cache on any interface
2175 * other than ARCnet, Ethernet, FDDI and GIF.
2178 * - unidirectional tunnels needs no ND
2180 switch (ifp->if_type) {
2188 #ifdef IFT_IEEE80211
2194 case IFT_GIF: /* XXX need more cases? */
2198 case IFT_PROPVIRTUAL:
2206 nd6_storelladdr(ifp, rt0, m, dst, desten)
2208 struct rtentry *rt0;
2210 struct sockaddr *dst;
2213 struct sockaddr_dl *sdl;
2217 if (m->m_flags & M_MCAST) {
2220 switch (ifp->if_type) {
2226 #ifdef IFT_IEEE80211
2231 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2236 * netbsd can use if_broadcastaddr, but we don't do so
2237 * to reduce # of ifdef.
2239 for (i = 0; i < ifp->if_addrlen; i++)
2247 return (EAFNOSUPPORT);
2252 /* this could happen, if we could not allocate memory */
2257 error = rt_check(&rt, &rt0, dst);
2264 if (rt->rt_gateway->sa_family != AF_LINK) {
2265 printf("nd6_storelladdr: something odd happens\n");
2269 sdl = SDL(rt->rt_gateway);
2270 if (sdl->sdl_alen == 0) {
2271 /* this should be impossible, but we bark here for debugging */
2272 printf("nd6_storelladdr: sdl_alen == 0\n");
2277 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2282 clear_llinfo_pqueue(ln)
2283 struct llinfo_nd6 *ln;
2285 struct mbuf *m_hold, *m_hold_next;
2287 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
2288 m_hold_next = m_hold->m_nextpkt;
2289 m_hold->m_nextpkt = NULL;
2297 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2298 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2300 SYSCTL_DECL(_net_inet6_icmp6);
2302 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2303 CTLFLAG_RD, nd6_sysctl_drlist, "");
2304 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2305 CTLFLAG_RD, nd6_sysctl_prlist, "");
2306 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen,
2307 CTLFLAG_RW, &nd6_maxqueuelen, 1, "");
2310 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2314 struct in6_defrouter *d, *de;
2315 struct nd_defrouter *dr;
2321 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2322 dr = TAILQ_NEXT(dr, dr_entry)) {
2323 d = (struct in6_defrouter *)buf;
2324 de = (struct in6_defrouter *)(buf + sizeof(buf));
2327 bzero(d, sizeof(*d));
2328 d->rtaddr.sin6_family = AF_INET6;
2329 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2330 d->rtaddr.sin6_addr = dr->rtaddr;
2331 sa6_recoverscope(&d->rtaddr);
2332 d->flags = dr->flags;
2333 d->rtlifetime = dr->rtlifetime;
2334 d->expire = dr->expire;
2335 d->if_index = dr->ifp->if_index;
2337 panic("buffer too short");
2339 error = SYSCTL_OUT(req, buf, sizeof(*d));
2348 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2352 struct in6_prefix *p, *pe;
2353 struct nd_prefix *pr;
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(&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",
2419 ip6_sprintf(&pfr->router->rtaddr));
2423 p->advrtrs = advrtrs;
2425 panic("buffer too short");
2427 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2428 error = SYSCTL_OUT(req, buf, advance);