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>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
47 #include <sys/kernel.h>
48 #include <sys/protosw.h>
49 #include <sys/errno.h>
50 #include <sys/syslog.h>
51 #include <sys/queue.h>
52 #include <sys/sysctl.h>
55 #include <net/if_arc.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/iso88025.h>
60 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/if_ether.h>
64 #include <netinet6/in6_var.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #include <netinet6/scope6_var.h>
68 #include <netinet6/nd6.h>
69 #include <netinet/icmp6.h>
71 #include <sys/limits.h>
72 #include <sys/vimage.h>
74 #include <security/mac/mac_framework.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 INIT_VNET_INET6(curvnet);
129 static int nd6_init_done = 0;
133 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
137 all1_sa.sin6_family = AF_INET6;
138 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
139 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
140 all1_sa.sin6_addr.s6_addr[i] = 0xff;
142 /* initialization of the default router list */
143 TAILQ_INIT(&V_nd_defrouter);
145 callout_init(&V_nd6_slowtimo_ch, 0);
146 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
154 nd6_ifattach(struct ifnet *ifp)
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);
181 nd6_ifdetach(struct nd_ifinfo *nd)
188 * Reset ND level link MTU. This function is called when the physical MTU
189 * changes, which means we might have to adjust the ND level MTU.
192 nd6_setmtu(struct ifnet *ifp)
195 nd6_setmtu0(ifp, ND_IFINFO(ifp));
198 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
200 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
202 INIT_VNET_INET6(ifp->if_vnet);
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 > V_in6_maxmtu)
235 in6_setmaxmtu(); /* check all interfaces just in case */
241 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
244 bzero(ndopts, sizeof(*ndopts));
245 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
247 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
250 ndopts->nd_opts_done = 1;
251 ndopts->nd_opts_search = NULL;
256 * Take one ND option.
259 nd6_option(union nd_opts *ndopts)
261 struct nd_opt_hdr *nd_opt;
265 panic("ndopts == NULL in nd6_option");
266 if (ndopts->nd_opts_last == NULL)
267 panic("uninitialized ndopts in nd6_option");
268 if (ndopts->nd_opts_search == NULL)
270 if (ndopts->nd_opts_done)
273 nd_opt = ndopts->nd_opts_search;
275 /* make sure nd_opt_len is inside the buffer */
276 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
277 bzero(ndopts, sizeof(*ndopts));
281 olen = nd_opt->nd_opt_len << 3;
284 * Message validation requires that all included
285 * options have a length that is greater than zero.
287 bzero(ndopts, sizeof(*ndopts));
291 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
292 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
293 /* option overruns the end of buffer, invalid */
294 bzero(ndopts, sizeof(*ndopts));
296 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
297 /* reached the end of options chain */
298 ndopts->nd_opts_done = 1;
299 ndopts->nd_opts_search = NULL;
305 * Parse multiple ND options.
306 * This function is much easier to use, for ND routines that do not need
307 * multiple options of the same type.
310 nd6_options(union nd_opts *ndopts)
312 INIT_VNET_INET6(curvnet);
313 struct nd_opt_hdr *nd_opt;
317 panic("ndopts == NULL in nd6_options");
318 if (ndopts->nd_opts_last == NULL)
319 panic("uninitialized ndopts in nd6_options");
320 if (ndopts->nd_opts_search == NULL)
324 nd_opt = nd6_option(ndopts);
325 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
327 * Message validation requires that all included
328 * options have a length that is greater than zero.
330 V_icmp6stat.icp6s_nd_badopt++;
331 bzero(ndopts, sizeof(*ndopts));
338 switch (nd_opt->nd_opt_type) {
339 case ND_OPT_SOURCE_LINKADDR:
340 case ND_OPT_TARGET_LINKADDR:
342 case ND_OPT_REDIRECTED_HEADER:
343 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
345 "duplicated ND6 option found (type=%d)\n",
346 nd_opt->nd_opt_type));
349 ndopts->nd_opt_array[nd_opt->nd_opt_type]
353 case ND_OPT_PREFIX_INFORMATION:
354 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
355 ndopts->nd_opt_array[nd_opt->nd_opt_type]
358 ndopts->nd_opts_pi_end =
359 (struct nd_opt_prefix_info *)nd_opt;
363 * Unknown options must be silently ignored,
364 * to accomodate future extension to the protocol.
367 "nd6_options: unsupported option %d - "
368 "option ignored\n", nd_opt->nd_opt_type));
373 if (i > V_nd6_maxndopt) {
374 V_icmp6stat.icp6s_nd_toomanyopt++;
375 nd6log((LOG_INFO, "too many loop in nd opt\n"));
379 if (ndopts->nd_opts_done)
387 * ND6 timer routine to handle ND6 entries
390 nd6_llinfo_settimer(struct llinfo_nd6 *ln, long tick)
395 callout_stop(&ln->ln_timer_ch);
397 ln->ln_expire = time_second + tick / hz;
398 if (tick > INT_MAX) {
399 ln->ln_ntick = tick - INT_MAX;
400 callout_reset(&ln->ln_timer_ch, INT_MAX,
401 nd6_llinfo_timer, ln);
404 callout_reset(&ln->ln_timer_ch, tick,
405 nd6_llinfo_timer, ln);
411 nd6_llinfo_timer(void *arg)
413 struct llinfo_nd6 *ln;
415 struct in6_addr *dst;
417 struct nd_ifinfo *ndi = NULL;
419 ln = (struct llinfo_nd6 *)arg;
421 if (ln->ln_ntick > 0) {
422 if (ln->ln_ntick > INT_MAX) {
423 ln->ln_ntick -= INT_MAX;
424 nd6_llinfo_settimer(ln, INT_MAX);
427 nd6_llinfo_settimer(ln, ln->ln_ntick);
432 if ((rt = ln->ln_rt) == NULL)
433 panic("ln->ln_rt == NULL");
434 if ((ifp = rt->rt_ifp) == NULL)
435 panic("ln->ln_rt->rt_ifp == NULL");
436 ndi = ND_IFINFO(ifp);
438 CURVNET_SET(ifp->if_vnet);
439 INIT_VNET_INET6(curvnet);
442 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
443 panic("rt_llinfo(%p) is not equal to ln(%p)",
445 if (rt_key(rt) == NULL)
446 panic("rt key is NULL in nd6_timer(ln=%p)", ln);
448 dst = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
450 switch (ln->ln_state) {
451 case ND6_LLINFO_INCOMPLETE:
452 if (ln->ln_asked < V_nd6_mmaxtries) {
454 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
455 nd6_ns_output(ifp, NULL, dst, ln, 0);
457 struct mbuf *m = ln->ln_hold;
462 * assuming every packet in ln_hold has the
467 icmp6_error2(m, ICMP6_DST_UNREACH,
468 ICMP6_DST_UNREACH_ADDR, 0, rt->rt_ifp);
471 clear_llinfo_pqueue(ln);
473 if (rt && rt->rt_llinfo)
474 (void)nd6_free(rt, 0);
478 case ND6_LLINFO_REACHABLE:
479 if (!ND6_LLINFO_PERMANENT(ln)) {
480 ln->ln_state = ND6_LLINFO_STALE;
481 nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
485 case ND6_LLINFO_STALE:
486 /* Garbage Collection(RFC 2461 5.3) */
487 if (!ND6_LLINFO_PERMANENT(ln)) {
488 if (rt && rt->rt_llinfo)
489 (void)nd6_free(rt, 1);
494 case ND6_LLINFO_DELAY:
495 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
498 ln->ln_state = ND6_LLINFO_PROBE;
499 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
500 nd6_ns_output(ifp, dst, dst, ln, 0);
502 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
503 nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
506 case ND6_LLINFO_PROBE:
507 if (ln->ln_asked < V_nd6_umaxtries) {
509 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
510 nd6_ns_output(ifp, dst, dst, ln, 0);
511 } else if (rt->rt_ifa != NULL &&
512 rt->rt_ifa->ifa_addr->sa_family == AF_INET6 &&
513 (((struct in6_ifaddr *)rt->rt_ifa)->ia_flags & IFA_ROUTE)) {
515 * This is an unreachable neighbor whose address is
516 * specified as the destination of a p2p interface
517 * (see in6_ifinit()). We should not free the entry
518 * since this is sort of a "static" entry generated
519 * via interface address configuration.
522 ln->ln_expire = 0; /* make it permanent */
523 ln->ln_state = ND6_LLINFO_STALE;
525 if (rt && rt->rt_llinfo)
526 (void)nd6_free(rt, 0);
536 * ND6 timer routine to expire default route list and prefix list
541 CURVNET_SET_QUIET((struct vnet *) arg);
542 INIT_VNET_INET6((struct vnet *) 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(&V_nd6_timer_ch, V_nd6_prune * hz,
552 /* expire default router list */
554 dr = TAILQ_FIRST(&V_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 = V_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 (V_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 (V_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 = V_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 * ia6 - deprecated/invalidated temporary address
669 regen_tmpaddr(struct in6_ifaddr *ia6)
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
735 nd6_purge(struct ifnet *ifp)
737 INIT_VNET_INET6(ifp->if_vnet);
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(&V_nd_defrouter); dr; dr = ndr) {
749 ndr = TAILQ_NEXT(dr, dr_entry);
757 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = ndr) {
758 ndr = TAILQ_NEXT(dr, dr_entry);
766 /* Nuke prefix list entries toward ifp */
767 for (pr = V_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 (V_nd6_defifindex == ifp->if_index)
792 nd6_setdefaultiface(0);
794 if (!V_ip6_forwarding && V_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 = V_llinfo_nd6.ln_next;
806 while (ln && ln != &V_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(struct in6_addr *addr6, int create, struct ifnet *ifp)
825 INIT_VNET_INET6(curvnet);
827 struct sockaddr_in6 sin6;
828 char ip6buf[INET6_ADDRSTRLEN];
830 bzero(&sin6, sizeof(sin6));
831 sin6.sin6_len = sizeof(struct sockaddr_in6);
832 sin6.sin6_family = AF_INET6;
833 sin6.sin6_addr = *addr6;
834 rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL);
836 if ((rt->rt_flags & RTF_LLINFO) == 0 && create) {
838 * This is the case for the default route.
839 * If we want to create a neighbor cache for the
840 * address, we should free the route for the
841 * destination and allocate an interface route.
852 * If no route is available and create is set,
853 * we allocate a host route for the destination
854 * and treat it like an interface route.
855 * This hack is necessary for a neighbor which can't
856 * be covered by our own prefix.
859 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
864 * Create a new route. RTF_LLINFO is necessary
865 * to create a Neighbor Cache entry for the
866 * destination in nd6_rtrequest which will be
867 * called in rtrequest via ifa->ifa_rtrequest.
869 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
870 ifa->ifa_addr, (struct sockaddr *)&all1_sa,
871 (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
872 ~RTF_CLONING, &rt)) != 0) {
874 "nd6_lookup: failed to add route for a "
875 "neighbor(%s), errno=%d\n",
876 ip6_sprintf(ip6buf, addr6), e);
882 struct llinfo_nd6 *ln =
883 (struct llinfo_nd6 *)rt->rt_llinfo;
884 ln->ln_state = ND6_LLINFO_NOSTATE;
892 * Validation for the entry.
893 * Note that the check for rt_llinfo is necessary because a cloned
894 * route from a parent route that has the L flag (e.g. the default
895 * route to a p2p interface) may have the flag, too, while the
896 * destination is not actually a neighbor.
897 * XXX: we can't use rt->rt_ifp to check for the interface, since
898 * it might be the loopback interface if the entry is for our
899 * own address on a non-loopback interface. Instead, we should
900 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
902 * Note also that ifa_ifp and ifp may differ when we connect two
903 * interfaces to a same link, install a link prefix to an interface,
904 * and try to install a neighbor cache on an interface that does not
905 * have a route to the prefix.
907 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
908 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
909 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
912 "nd6_lookup: failed to lookup %s (if = %s)\n",
913 ip6_sprintf(ip6buf, addr6),
914 ifp ? if_name(ifp) : "unspec"));
919 RT_UNLOCK(rt); /* XXX not ready to return rt locked */
924 * Test whether a given IPv6 address is a neighbor or not, ignoring
925 * the actual neighbor cache. The neighbor cache is ignored in order
926 * to not reenter the routing code from within itself.
929 nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
931 INIT_VNET_INET6(ifp->if_vnet);
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 = V_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 (!V_ip6_forwarding && TAILQ_FIRST(&V_nd_defrouter) == NULL &&
991 V_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(struct sockaddr_in6 *addr, struct ifnet *ifp)
1007 if (nd6_is_new_addr_neighbor(addr, ifp))
1011 * Even if the address matches none of our addresses, it might be
1012 * in the neighbor cache.
1014 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
1021 * Free an nd6 llinfo entry.
1022 * Since the function would cause significant changes in the kernel, DO NOT
1023 * make it global, unless you have a strong reason for the change, and are sure
1024 * that the change is safe.
1026 static struct llinfo_nd6 *
1027 nd6_free(struct rtentry *rt, int gc)
1029 INIT_VNET_INET6(curvnet);
1030 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
1031 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
1032 struct nd_defrouter *dr;
1035 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1036 * even though it is not harmful, it was not really necessary.
1040 nd6_llinfo_settimer(ln, -1);
1042 if (!V_ip6_forwarding) {
1045 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1048 if (dr != NULL && dr->expire &&
1049 ln->ln_state == ND6_LLINFO_STALE && gc) {
1051 * If the reason for the deletion is just garbage
1052 * collection, and the neighbor is an active default
1053 * router, do not delete it. Instead, reset the GC
1054 * timer using the router's lifetime.
1055 * Simply deleting the entry would affect default
1056 * router selection, which is not necessarily a good
1057 * thing, especially when we're using router preference
1059 * XXX: the check for ln_state would be redundant,
1060 * but we intentionally keep it just in case.
1062 if (dr->expire > time_second)
1063 nd6_llinfo_settimer(ln,
1064 (dr->expire - time_second) * hz);
1066 nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
1068 return (ln->ln_next);
1071 if (ln->ln_router || dr) {
1073 * rt6_flush must be called whether or not the neighbor
1074 * is in the Default Router List.
1075 * See a corresponding comment in nd6_na_input().
1077 rt6_flush(&in6, rt->rt_ifp);
1082 * Unreachablity of a router might affect the default
1083 * router selection and on-link detection of advertised
1088 * Temporarily fake the state to choose a new default
1089 * router and to perform on-link determination of
1090 * prefixes correctly.
1091 * Below the state will be set correctly,
1092 * or the entry itself will be deleted.
1094 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1097 * Since defrouter_select() does not affect the
1098 * on-link determination and MIP6 needs the check
1099 * before the default router selection, we perform
1102 pfxlist_onlink_check();
1105 * refresh default router list
1113 * Before deleting the entry, remember the next entry as the
1114 * return value. We need this because pfxlist_onlink_check() above
1115 * might have freed other entries (particularly the old next entry) as
1116 * a side effect (XXX).
1121 * Detach the route from the routing tree and the list of neighbor
1122 * caches, and disable the route entry not to be used in already
1125 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
1126 rt_mask(rt), 0, (struct rtentry **)0);
1132 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1134 * XXX cost-effective methods?
1137 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1139 INIT_VNET_INET6(curvnet);
1140 struct llinfo_nd6 *ln;
1143 * If the caller specified "rt", use that. Otherwise, resolve the
1144 * routing table by supplied "dst6".
1149 if ((rt = nd6_lookup(dst6, 0, NULL)) == NULL)
1153 if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
1154 (rt->rt_flags & RTF_LLINFO) == 0 ||
1155 rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
1156 rt->rt_gateway->sa_family != AF_LINK) {
1157 /* This is not a host route. */
1161 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1162 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1166 * if we get upper-layer reachability confirmation many times,
1167 * it is possible we have false information.
1171 if (ln->ln_byhint > V_nd6_maxnudhint)
1175 ln->ln_state = ND6_LLINFO_REACHABLE;
1176 if (!ND6_LLINFO_PERMANENT(ln)) {
1177 nd6_llinfo_settimer(ln,
1178 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1186 nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
1188 struct sockaddr *gate = rt->rt_gateway;
1189 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1190 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1191 struct ifnet *ifp = rt->rt_ifp;
1193 INIT_VNET_NET(ifp->if_vnet);
1194 INIT_VNET_INET6(ifp->if_vnet);
1198 if ((rt->rt_flags & RTF_GATEWAY) != 0)
1201 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1203 * This is probably an interface direct route for a link
1204 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1205 * We do not need special treatment below for such a route.
1206 * Moreover, the RTF_LLINFO flag which would be set below
1207 * would annoy the ndp(8) command.
1212 if (req == RTM_RESOLVE &&
1213 (nd6_need_cache(ifp) == 0 || /* stf case */
1214 !nd6_is_new_addr_neighbor((struct sockaddr_in6 *)rt_key(rt),
1217 * FreeBSD and BSD/OS often make a cloned host route based
1218 * on a less-specific route (e.g. the default route).
1219 * If the less specific route does not have a "gateway"
1220 * (this is the case when the route just goes to a p2p or an
1221 * stf interface), we'll mistakenly make a neighbor cache for
1222 * the host route, and will see strange neighbor solicitation
1223 * for the corresponding destination. In order to avoid the
1224 * confusion, we check if the destination of the route is
1225 * a neighbor in terms of neighbor discovery, and stop the
1226 * process if not. Additionally, we remove the LLINFO flag
1227 * so that ndp(8) will not try to get the neighbor information
1228 * of the destination.
1230 rt->rt_flags &= ~RTF_LLINFO;
1237 * There is no backward compatibility :)
1239 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1240 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1241 * rt->rt_flags |= RTF_CLONING;
1243 if ((rt->rt_flags & RTF_CLONING) ||
1244 ((rt->rt_flags & RTF_LLINFO) && ln == NULL)) {
1246 * Case 1: This route should come from a route to
1247 * interface (RTF_CLONING case) or the route should be
1248 * treated as on-link but is currently not
1249 * (RTF_LLINFO && ln == NULL case).
1251 rt_setgate(rt, rt_key(rt),
1252 (struct sockaddr *)&null_sdl);
1253 gate = rt->rt_gateway;
1254 SDL(gate)->sdl_type = ifp->if_type;
1255 SDL(gate)->sdl_index = ifp->if_index;
1257 nd6_llinfo_settimer(ln, 0);
1258 if ((rt->rt_flags & RTF_CLONING) != 0)
1262 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1263 * We don't do that here since llinfo is not ready yet.
1265 * There are also couple of other things to be discussed:
1266 * - unsolicited NA code needs improvement beforehand
1267 * - RFC2461 says we MAY send multicast unsolicited NA
1268 * (7.2.6 paragraph 4), however, it also says that we
1269 * SHOULD provide a mechanism to prevent multicast NA storm.
1270 * we don't have anything like it right now.
1271 * note that the mechanism needs a mutual agreement
1272 * between proxies, which means that we need to implement
1273 * a new protocol, or a new kludge.
1274 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1275 * we need to check ip6forwarding before sending it.
1276 * (or should we allow proxy ND configuration only for
1277 * routers? there's no mention about proxy ND from hosts)
1281 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1283 * Address resolution isn't necessary for a point to
1284 * point link, so we can skip this test for a p2p link.
1286 if (gate->sa_family != AF_LINK ||
1287 gate->sa_len < sizeof(null_sdl)) {
1289 "nd6_rtrequest: bad gateway value: %s\n",
1293 SDL(gate)->sdl_type = ifp->if_type;
1294 SDL(gate)->sdl_index = ifp->if_index;
1297 break; /* This happens on a route change */
1299 * Case 2: This route may come from cloning, or a manual route
1300 * add with a LL address.
1302 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1303 rt->rt_llinfo = (caddr_t)ln;
1305 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1310 bzero(ln, sizeof(*ln));
1313 callout_init(&ln->ln_timer_ch, 0);
1315 /* this is required for "ndp" command. - shin */
1316 if (req == RTM_ADD) {
1318 * gate should have some valid AF_LINK entry,
1319 * and ln->ln_expire should have some lifetime
1320 * which is specified by ndp command.
1322 ln->ln_state = ND6_LLINFO_REACHABLE;
1326 * When req == RTM_RESOLVE, rt is created and
1327 * initialized in rtrequest(), so rt_expire is 0.
1329 ln->ln_state = ND6_LLINFO_NOSTATE;
1330 nd6_llinfo_settimer(ln, 0);
1332 rt->rt_flags |= RTF_LLINFO;
1333 ln->ln_next = V_llinfo_nd6.ln_next;
1334 V_llinfo_nd6.ln_next = ln;
1335 ln->ln_prev = &V_llinfo_nd6;
1336 ln->ln_next->ln_prev = ln;
1339 * check if rt_key(rt) is one of my address assigned
1342 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1343 &SIN6(rt_key(rt))->sin6_addr);
1345 caddr_t macp = nd6_ifptomac(ifp);
1346 nd6_llinfo_settimer(ln, -1);
1347 ln->ln_state = ND6_LLINFO_REACHABLE;
1350 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1351 SDL(gate)->sdl_alen = ifp->if_addrlen;
1353 if (V_nd6_useloopback) {
1354 rt->rt_ifp = &V_loif[0]; /* XXX */
1356 * Make sure rt_ifa be equal to the ifaddr
1357 * corresponding to the address.
1358 * We need this because when we refer
1359 * rt_ifa->ia6_flags in ip6_input, we assume
1360 * that the rt_ifa points to the address instead
1361 * of the loopback address.
1363 if (ifa != rt->rt_ifa) {
1364 IFAFREE(rt->rt_ifa);
1369 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1370 nd6_llinfo_settimer(ln, -1);
1371 ln->ln_state = ND6_LLINFO_REACHABLE;
1374 /* join solicited node multicast for proxy ND */
1375 if (ifp->if_flags & IFF_MULTICAST) {
1376 struct in6_addr llsol;
1379 llsol = SIN6(rt_key(rt))->sin6_addr;
1380 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1381 llsol.s6_addr32[1] = 0;
1382 llsol.s6_addr32[2] = htonl(1);
1383 llsol.s6_addr8[12] = 0xff;
1384 if (in6_setscope(&llsol, ifp, NULL))
1386 if (in6_addmulti(&llsol, ifp,
1387 &error, 0) == NULL) {
1388 char ip6buf[INET6_ADDRSTRLEN];
1389 nd6log((LOG_ERR, "%s: failed to join "
1390 "%s (errno=%d)\n", if_name(ifp),
1391 ip6_sprintf(ip6buf, &llsol),
1401 /* leave from solicited node multicast for proxy ND */
1402 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1403 (ifp->if_flags & IFF_MULTICAST) != 0) {
1404 struct in6_addr llsol;
1405 struct in6_multi *in6m;
1407 llsol = SIN6(rt_key(rt))->sin6_addr;
1408 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1409 llsol.s6_addr32[1] = 0;
1410 llsol.s6_addr32[2] = htonl(1);
1411 llsol.s6_addr8[12] = 0xff;
1412 if (in6_setscope(&llsol, ifp, NULL) == 0) {
1413 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1417 ; /* XXX: should not happen. bark here? */
1420 ln->ln_next->ln_prev = ln->ln_prev;
1421 ln->ln_prev->ln_next = ln->ln_next;
1423 nd6_llinfo_settimer(ln, -1);
1426 rt->rt_flags &= ~RTF_LLINFO;
1427 clear_llinfo_pqueue(ln);
1433 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1435 INIT_VNET_INET6(ifp->if_vnet);
1436 struct in6_drlist *drl = (struct in6_drlist *)data;
1437 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1438 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1439 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1440 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1441 struct nd_defrouter *dr;
1442 struct nd_prefix *pr;
1444 int i = 0, error = 0;
1448 case SIOCGDRLST_IN6:
1450 * obsolete API, use sysctl under net.inet6.icmp6
1452 bzero(drl, sizeof(*drl));
1454 dr = TAILQ_FIRST(&V_nd_defrouter);
1455 while (dr && i < DRLSTSIZ) {
1456 drl->defrouter[i].rtaddr = dr->rtaddr;
1457 in6_clearscope(&drl->defrouter[i].rtaddr);
1459 drl->defrouter[i].flags = dr->flags;
1460 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1461 drl->defrouter[i].expire = dr->expire;
1462 drl->defrouter[i].if_index = dr->ifp->if_index;
1464 dr = TAILQ_NEXT(dr, dr_entry);
1468 case SIOCGPRLST_IN6:
1470 * obsolete API, use sysctl under net.inet6.icmp6
1472 * XXX the structure in6_prlist was changed in backward-
1473 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1474 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1477 * XXX meaning of fields, especialy "raflags", is very
1478 * differnet between RA prefix list and RR/static prefix list.
1479 * how about separating ioctls into two?
1481 bzero(oprl, sizeof(*oprl));
1483 pr = V_nd_prefix.lh_first;
1484 while (pr && i < PRLSTSIZ) {
1485 struct nd_pfxrouter *pfr;
1488 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1489 oprl->prefix[i].raflags = pr->ndpr_raf;
1490 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1491 oprl->prefix[i].vltime = pr->ndpr_vltime;
1492 oprl->prefix[i].pltime = pr->ndpr_pltime;
1493 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1494 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1495 oprl->prefix[i].expire = 0;
1499 /* XXX: we assume time_t is signed. */
1502 ((sizeof(maxexpire) * 8) - 1));
1503 if (pr->ndpr_vltime <
1504 maxexpire - pr->ndpr_lastupdate) {
1505 oprl->prefix[i].expire =
1506 pr->ndpr_lastupdate +
1509 oprl->prefix[i].expire = maxexpire;
1512 pfr = pr->ndpr_advrtrs.lh_first;
1516 #define RTRADDR oprl->prefix[i].advrtr[j]
1517 RTRADDR = pfr->router->rtaddr;
1518 in6_clearscope(&RTRADDR);
1522 pfr = pfr->pfr_next;
1524 oprl->prefix[i].advrtrs = j;
1525 oprl->prefix[i].origin = PR_ORIG_RA;
1533 case OSIOCGIFINFO_IN6:
1535 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1536 bzero(&ND, sizeof(ND));
1537 ND.linkmtu = IN6_LINKMTU(ifp);
1538 ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1539 ND.basereachable = ND_IFINFO(ifp)->basereachable;
1540 ND.reachable = ND_IFINFO(ifp)->reachable;
1541 ND.retrans = ND_IFINFO(ifp)->retrans;
1542 ND.flags = ND_IFINFO(ifp)->flags;
1543 ND.recalctm = ND_IFINFO(ifp)->recalctm;
1544 ND.chlim = ND_IFINFO(ifp)->chlim;
1546 case SIOCGIFINFO_IN6:
1547 ND = *ND_IFINFO(ifp);
1549 case SIOCSIFINFO_IN6:
1551 * used to change host variables from userland.
1552 * intented for a use on router to reflect RA configurations.
1554 /* 0 means 'unspecified' */
1555 if (ND.linkmtu != 0) {
1556 if (ND.linkmtu < IPV6_MMTU ||
1557 ND.linkmtu > IN6_LINKMTU(ifp)) {
1561 ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1564 if (ND.basereachable != 0) {
1565 int obasereachable = ND_IFINFO(ifp)->basereachable;
1567 ND_IFINFO(ifp)->basereachable = ND.basereachable;
1568 if (ND.basereachable != obasereachable)
1569 ND_IFINFO(ifp)->reachable =
1570 ND_COMPUTE_RTIME(ND.basereachable);
1572 if (ND.retrans != 0)
1573 ND_IFINFO(ifp)->retrans = ND.retrans;
1575 ND_IFINFO(ifp)->chlim = ND.chlim;
1577 case SIOCSIFINFO_FLAGS:
1578 ND_IFINFO(ifp)->flags = ND.flags;
1581 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1582 /* sync kernel routing table with the default router list */
1586 case SIOCSPFXFLUSH_IN6:
1588 /* flush all the prefix advertised by routers */
1589 struct nd_prefix *pr, *next;
1592 for (pr = V_nd_prefix.lh_first; pr; pr = next) {
1593 struct in6_ifaddr *ia, *ia_next;
1595 next = pr->ndpr_next;
1597 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1600 /* do we really have to remove addresses as well? */
1601 for (ia = V_in6_ifaddr; ia; ia = ia_next) {
1602 /* ia might be removed. keep the next ptr. */
1603 ia_next = ia->ia_next;
1605 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1608 if (ia->ia6_ndpr == pr)
1609 in6_purgeaddr(&ia->ia_ifa);
1616 case SIOCSRTRFLUSH_IN6:
1618 /* flush all the default routers */
1619 struct nd_defrouter *dr, *next;
1623 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = next) {
1624 next = TAILQ_NEXT(dr, dr_entry);
1631 case SIOCGNBRINFO_IN6:
1633 struct llinfo_nd6 *ln;
1634 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1636 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1640 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1645 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1646 nbi->state = ln->ln_state;
1647 nbi->asked = ln->ln_asked;
1648 nbi->isrouter = ln->ln_router;
1649 nbi->expire = ln->ln_expire;
1654 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1655 ndif->ifindex = V_nd6_defifindex;
1657 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1658 return (nd6_setdefaultiface(ndif->ifindex));
1664 * Create neighbor cache entry and cache link-layer address,
1665 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1668 * code - type dependent information
1671 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1672 int lladdrlen, int type, int code)
1674 INIT_VNET_INET6(curvnet);
1675 struct rtentry *rt = NULL;
1676 struct llinfo_nd6 *ln = NULL;
1678 struct sockaddr_dl *sdl = NULL;
1685 panic("ifp == NULL in nd6_cache_lladdr");
1687 panic("from == NULL in nd6_cache_lladdr");
1689 /* nothing must be updated for unspecified address */
1690 if (IN6_IS_ADDR_UNSPECIFIED(from))
1694 * Validation about ifp->if_addrlen and lladdrlen must be done in
1697 * XXX If the link does not have link-layer adderss, what should
1698 * we do? (ifp->if_addrlen == 0)
1699 * Spec says nothing in sections for RA, RS and NA. There's small
1700 * description on it in NS section (RFC 2461 7.2.3).
1703 rt = nd6_lookup(from, 0, ifp);
1705 rt = nd6_lookup(from, 1, ifp);
1708 /* do nothing if static ndp is set */
1709 if (rt->rt_flags & RTF_STATIC)
1716 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1718 (void)nd6_free(rt, 0);
1721 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1724 if (rt->rt_gateway == NULL)
1726 if (rt->rt_gateway->sa_family != AF_LINK)
1728 sdl = SDL(rt->rt_gateway);
1730 olladdr = (sdl->sdl_alen) ? 1 : 0;
1731 if (olladdr && lladdr) {
1732 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1740 * newentry olladdr lladdr llchange (*=record)
1743 * 0 n y -- (3) * STALE
1745 * 0 y y y (5) * STALE
1746 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1747 * 1 -- y -- (7) * STALE
1750 if (lladdr) { /* (3-5) and (7) */
1752 * Record source link-layer address
1753 * XXX is it dependent to ifp->if_type?
1755 sdl->sdl_alen = ifp->if_addrlen;
1756 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1760 if ((!olladdr && lladdr != NULL) || /* (3) */
1761 (olladdr && lladdr != NULL && llchange)) { /* (5) */
1763 newstate = ND6_LLINFO_STALE;
1764 } else /* (1-2,4) */
1768 if (lladdr == NULL) /* (6) */
1769 newstate = ND6_LLINFO_NOSTATE;
1771 newstate = ND6_LLINFO_STALE;
1776 * Update the state of the neighbor cache.
1778 ln->ln_state = newstate;
1780 if (ln->ln_state == ND6_LLINFO_STALE) {
1782 * XXX: since nd6_output() below will cause
1783 * state tansition to DELAY and reset the timer,
1784 * we must set the timer now, although it is actually
1787 nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
1790 struct mbuf *m_hold, *m_hold_next;
1793 * reset the ln_hold in advance, to explicitly
1794 * prevent a ln_hold lookup in nd6_output()
1795 * (wouldn't happen, though...)
1797 for (m_hold = ln->ln_hold, ln->ln_hold = NULL;
1798 m_hold; m_hold = m_hold_next) {
1799 m_hold_next = m_hold->m_nextpkt;
1800 m_hold->m_nextpkt = NULL;
1803 * we assume ifp is not a p2p here, so
1804 * just set the 2nd argument as the
1807 nd6_output(ifp, ifp, m_hold,
1808 (struct sockaddr_in6 *)rt_key(rt),
1812 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1813 /* probe right away */
1814 nd6_llinfo_settimer((void *)ln, 0);
1819 * ICMP6 type dependent behavior.
1821 * NS: clear IsRouter if new entry
1822 * RS: clear IsRouter
1823 * RA: set IsRouter if there's lladdr
1824 * redir: clear IsRouter if new entry
1827 * The spec says that we must set IsRouter in the following cases:
1828 * - If lladdr exist, set IsRouter. This means (1-5).
1829 * - If it is old entry (!newentry), set IsRouter. This means (7).
1830 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1831 * A quetion arises for (1) case. (1) case has no lladdr in the
1832 * neighbor cache, this is similar to (6).
1833 * This case is rare but we figured that we MUST NOT set IsRouter.
1835 * newentry olladdr lladdr llchange NS RS RA redir
1837 * 0 n n -- (1) c ? s
1838 * 0 y n -- (2) c s s
1839 * 0 n y -- (3) c s s
1842 * 1 -- n -- (6) c c c s
1843 * 1 -- y -- (7) c c s c s
1847 switch (type & 0xff) {
1848 case ND_NEIGHBOR_SOLICIT:
1850 * New entry must have is_router flag cleared.
1852 if (is_newentry) /* (6-7) */
1857 * If the icmp is a redirect to a better router, always set the
1858 * is_router flag. Otherwise, if the entry is newly created,
1859 * clear the flag. [RFC 2461, sec 8.3]
1861 if (code == ND_REDIRECT_ROUTER)
1863 else if (is_newentry) /* (6-7) */
1866 case ND_ROUTER_SOLICIT:
1868 * is_router flag must always be cleared.
1872 case ND_ROUTER_ADVERT:
1874 * Mark an entry with lladdr as a router.
1876 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1877 (is_newentry && lladdr)) { /* (7) */
1884 * When the link-layer address of a router changes, select the
1885 * best router again. In particular, when the neighbor entry is newly
1886 * created, it might affect the selection policy.
1887 * Question: can we restrict the first condition to the "is_newentry"
1889 * XXX: when we hear an RA from a new router with the link-layer
1890 * address option, defrouter_select() is called twice, since
1891 * defrtrlist_update called the function as well. However, I believe
1892 * we can compromise the overhead, since it only happens the first
1894 * XXX: although defrouter_select() should not have a bad effect
1895 * for those are not autoconfigured hosts, we explicitly avoid such
1898 if (do_update && ln->ln_router && !V_ip6_forwarding && V_ip6_accept_rtadv)
1905 nd6_slowtimo(void *arg)
1907 CURVNET_SET((struct vnet *) arg);
1908 INIT_VNET_NET((struct vnet *) arg);
1909 INIT_VNET_INET6((struct vnet *) arg);
1910 struct nd_ifinfo *nd6if;
1913 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1914 nd6_slowtimo, NULL);
1916 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
1917 ifp = TAILQ_NEXT(ifp, if_list)) {
1918 nd6if = ND_IFINFO(ifp);
1919 if (nd6if->basereachable && /* already initialized */
1920 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1922 * Since reachable time rarely changes by router
1923 * advertisements, we SHOULD insure that a new random
1924 * value gets recomputed at least once every few hours.
1927 nd6if->recalctm = V_nd6_recalc_reachtm_interval;
1928 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1935 #define senderr(e) { error = (e); goto bad;}
1937 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1938 struct sockaddr_in6 *dst, struct rtentry *rt0)
1940 INIT_VNET_INET6(curvnet);
1941 struct mbuf *m = m0;
1942 struct rtentry *rt = rt0;
1943 struct sockaddr_in6 *gw6 = NULL;
1944 struct llinfo_nd6 *ln = NULL;
1947 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1950 if (nd6_need_cache(ifp) == 0)
1954 * next hop determination. This routine is derived from ether_output.
1956 /* NB: the locking here is tortuous... */
1961 if ((rt->rt_flags & RTF_UP) == 0) {
1963 rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL);
1966 if (rt->rt_ifp != ifp)
1968 * XXX maybe we should update ifp too,
1969 * but the original code didn't and I
1970 * don't know what is correct here.
1974 senderr(EHOSTUNREACH);
1977 if (rt->rt_flags & RTF_GATEWAY) {
1978 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1981 * We skip link-layer address resolution and NUD
1982 * if the gateway is not a neighbor from ND point
1983 * of view, regardless of the value of nd_ifinfo.flags.
1984 * The second condition is a bit tricky; we skip
1985 * if the gateway is our own address, which is
1986 * sometimes used to install a route to a p2p link.
1988 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1989 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1992 * We allow this kind of tricky route only
1993 * when the outgoing interface is p2p.
1994 * XXX: we may need a more generic rule here.
1996 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1997 senderr(EHOSTUNREACH);
2002 if (rt->rt_gwroute == NULL)
2004 rt = rt->rt_gwroute;
2005 RT_LOCK(rt); /* NB: gwroute */
2006 if ((rt->rt_flags & RTF_UP) == 0) {
2007 RTFREE_LOCKED(rt); /* unlock gwroute */
2009 rt0->rt_gwroute = NULL;
2012 rt = rtalloc1(rt->rt_gateway, 1, 0UL);
2016 senderr(EHOSTUNREACH);
2019 if (rt0->rt_gwroute != NULL)
2020 RTFREE(rt0->rt_gwroute);
2021 rt0->rt_gwroute = rt;
2024 senderr(EHOSTUNREACH);
2033 * Address resolution or Neighbor Unreachability Detection
2035 * At this point, the destination of the packet must be a unicast
2036 * or an anycast address(i.e. not a multicast).
2039 /* Look up the neighbor cache for the nexthop */
2040 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
2041 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2044 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2045 * the condition below is not very efficient. But we believe
2046 * it is tolerable, because this should be a rare case.
2048 if (nd6_is_addr_neighbor(dst, ifp) &&
2049 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
2050 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2052 if (ln == NULL || rt == NULL) {
2053 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
2054 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
2055 char ip6buf[INET6_ADDRSTRLEN];
2057 "nd6_output: can't allocate llinfo for %s "
2059 ip6_sprintf(ip6buf, &dst->sin6_addr), ln, rt);
2060 senderr(EIO); /* XXX: good error? */
2063 goto sendpkt; /* send anyway */
2066 /* We don't have to do link-layer address resolution on a p2p link. */
2067 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2068 ln->ln_state < ND6_LLINFO_REACHABLE) {
2069 ln->ln_state = ND6_LLINFO_STALE;
2070 nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
2074 * The first time we send a packet to a neighbor whose entry is
2075 * STALE, we have to change the state to DELAY and a sets a timer to
2076 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2077 * neighbor unreachability detection on expiration.
2080 if (ln->ln_state == ND6_LLINFO_STALE) {
2082 ln->ln_state = ND6_LLINFO_DELAY;
2083 nd6_llinfo_settimer(ln, (long)V_nd6_delay * hz);
2087 * If the neighbor cache entry has a state other than INCOMPLETE
2088 * (i.e. its link-layer address is already resolved), just
2091 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
2095 * There is a neighbor cache entry, but no ethernet address
2096 * response yet. Append this latest packet to the end of the
2097 * packet queue in the mbuf, unless the number of the packet
2098 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen,
2099 * the oldest packet in the queue will be removed.
2101 if (ln->ln_state == ND6_LLINFO_NOSTATE)
2102 ln->ln_state = ND6_LLINFO_INCOMPLETE;
2104 struct mbuf *m_hold;
2108 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
2110 if (m_hold->m_nextpkt == NULL) {
2111 m_hold->m_nextpkt = m;
2115 while (i >= V_nd6_maxqueuelen) {
2116 m_hold = ln->ln_hold;
2117 ln->ln_hold = ln->ln_hold->m_nextpkt;
2126 * If there has been no NS for the neighbor after entering the
2127 * INCOMPLETE state, send the first solicitation.
2129 if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
2131 nd6_llinfo_settimer(ln,
2132 (long)ND_IFINFO(ifp)->retrans * hz / 1000);
2133 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
2138 /* discard the packet if IPv6 operation is disabled on the interface */
2139 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
2140 error = ENETDOWN; /* better error? */
2145 mac_netinet6_nd6_send(ifp, m);
2147 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
2148 return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
2151 return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
2161 nd6_need_cache(struct ifnet *ifp)
2164 * XXX: we currently do not make neighbor cache on any interface
2165 * other than ARCnet, Ethernet, FDDI and GIF.
2168 * - unidirectional tunnels needs no ND
2170 switch (ifp->if_type) {
2178 #ifdef IFT_IEEE80211
2184 case IFT_GIF: /* XXX need more cases? */
2188 case IFT_PROPVIRTUAL:
2196 nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
2197 struct sockaddr *dst, u_char *desten)
2199 struct sockaddr_dl *sdl;
2203 if (m->m_flags & M_MCAST) {
2206 switch (ifp->if_type) {
2212 #ifdef IFT_IEEE80211
2217 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2222 * netbsd can use if_broadcastaddr, but we don't do so
2223 * to reduce # of ifdef.
2225 for (i = 0; i < ifp->if_addrlen; i++)
2233 return (EAFNOSUPPORT);
2238 /* this could happen, if we could not allocate memory */
2243 error = rt_check(&rt, &rt0, dst);
2250 if (rt->rt_gateway->sa_family != AF_LINK) {
2251 printf("nd6_storelladdr: something odd happens\n");
2255 sdl = SDL(rt->rt_gateway);
2256 if (sdl->sdl_alen == 0) {
2257 /* this should be impossible, but we bark here for debugging */
2258 printf("nd6_storelladdr: sdl_alen == 0\n");
2263 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2268 clear_llinfo_pqueue(struct llinfo_nd6 *ln)
2270 struct mbuf *m_hold, *m_hold_next;
2272 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
2273 m_hold_next = m_hold->m_nextpkt;
2274 m_hold->m_nextpkt = NULL;
2282 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2283 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2285 SYSCTL_DECL(_net_inet6_icmp6);
2287 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2288 CTLFLAG_RD, nd6_sysctl_drlist, "");
2289 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2290 CTLFLAG_RD, nd6_sysctl_prlist, "");
2291 SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN,
2292 nd6_maxqueuelen, CTLFLAG_RW, nd6_maxqueuelen, 1, "");
2295 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2297 INIT_VNET_INET6(curvnet);
2299 char buf[1024] __aligned(4);
2300 struct in6_defrouter *d, *de;
2301 struct nd_defrouter *dr;
2307 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr;
2308 dr = TAILQ_NEXT(dr, dr_entry)) {
2309 d = (struct in6_defrouter *)buf;
2310 de = (struct in6_defrouter *)(buf + sizeof(buf));
2313 bzero(d, sizeof(*d));
2314 d->rtaddr.sin6_family = AF_INET6;
2315 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2316 d->rtaddr.sin6_addr = dr->rtaddr;
2317 error = sa6_recoverscope(&d->rtaddr);
2320 d->flags = dr->flags;
2321 d->rtlifetime = dr->rtlifetime;
2322 d->expire = dr->expire;
2323 d->if_index = dr->ifp->if_index;
2325 panic("buffer too short");
2327 error = SYSCTL_OUT(req, buf, sizeof(*d));
2336 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2338 INIT_VNET_INET6(curvnet);
2340 char buf[1024] __aligned(4);
2341 struct in6_prefix *p, *pe;
2342 struct nd_prefix *pr;
2343 char ip6buf[INET6_ADDRSTRLEN];
2349 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2352 struct sockaddr_in6 *sin6, *s6;
2353 struct nd_pfxrouter *pfr;
2355 p = (struct in6_prefix *)buf;
2356 pe = (struct in6_prefix *)(buf + sizeof(buf));
2359 bzero(p, sizeof(*p));
2360 sin6 = (struct sockaddr_in6 *)(p + 1);
2362 p->prefix = pr->ndpr_prefix;
2363 if (sa6_recoverscope(&p->prefix)) {
2365 "scope error in prefix list (%s)\n",
2366 ip6_sprintf(ip6buf, &p->prefix.sin6_addr));
2367 /* XXX: press on... */
2369 p->raflags = pr->ndpr_raf;
2370 p->prefixlen = pr->ndpr_plen;
2371 p->vltime = pr->ndpr_vltime;
2372 p->pltime = pr->ndpr_pltime;
2373 p->if_index = pr->ndpr_ifp->if_index;
2374 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2379 /* XXX: we assume time_t is signed. */
2382 ((sizeof(maxexpire) * 8) - 1));
2383 if (pr->ndpr_vltime <
2384 maxexpire - pr->ndpr_lastupdate) {
2385 p->expire = pr->ndpr_lastupdate +
2388 p->expire = maxexpire;
2390 p->refcnt = pr->ndpr_refcnt;
2391 p->flags = pr->ndpr_stateflags;
2392 p->origin = PR_ORIG_RA;
2394 for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2395 pfr = pfr->pfr_next) {
2396 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2400 s6 = &sin6[advrtrs];
2401 bzero(s6, sizeof(*s6));
2402 s6->sin6_family = AF_INET6;
2403 s6->sin6_len = sizeof(*sin6);
2404 s6->sin6_addr = pfr->router->rtaddr;
2405 if (sa6_recoverscope(s6)) {
2408 "prefix list (%s)\n",
2410 &pfr->router->rtaddr));
2414 p->advrtrs = advrtrs;
2416 panic("buffer too short");
2418 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2419 error = SYSCTL_OUT(req, buf, advance);