1 /* $FreeBSD: src/sys/netinet6/nd6.c,v 1.2.2.15 2003/05/06 06:46:58 suz Exp $ */
2 /* $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/callout.h>
39 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
44 #include <sys/kernel.h>
45 #include <sys/protosw.h>
46 #include <sys/errno.h>
47 #include <sys/syslog.h>
48 #include <sys/queue.h>
49 #include <sys/sysctl.h>
50 #include <sys/mutex.h>
52 #include <sys/thread2.h>
53 #include <sys/mutex2.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59 #include <net/netisr2.h>
60 #include <net/netmsg2.h>
62 #include <netinet/in.h>
63 #include <netinet/if_ether.h>
64 #include <netinet6/in6_var.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #include <netinet6/nd6.h>
68 #include <netinet/icmp6.h>
70 #include <net/net_osdep.h>
72 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
73 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
75 #define SIN6(s) ((struct sockaddr_in6 *)s)
76 #define SDL(s) ((struct sockaddr_dl *)s)
79 * Note that the check for rt_llinfo is necessary because a cloned
80 * route from a parent route that has the L flag (e.g. the default
81 * route to a p2p interface) may have the flag, too, while the
82 * destination is not actually a neighbor.
83 * XXX: we can't use rt->rt_ifp to check for the interface, since
84 * it might be the loopback interface if the entry is for our
85 * own address on a non-loopback interface. Instead, we should
86 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
89 #define ND6_RTENTRY_IS_NEIGHBOR(rt, ifp) \
90 !(((rt)->rt_flags & RTF_GATEWAY) || \
91 ((rt)->rt_flags & RTF_LLINFO) == 0 || \
92 (rt)->rt_gateway->sa_family != AF_LINK || \
93 (rt)->rt_llinfo == NULL || \
94 ((ifp) != NULL && (rt)->rt_ifa->ifa_ifp != (ifp)))
96 #define ND6_RTENTRY_IS_LLCLONING(rt) \
97 (((rt)->rt_flags & (RTF_PRCLONING | RTF_LLINFO)) == \
98 (RTF_PRCLONING | RTF_LLINFO) || \
99 ((rt)->rt_flags & RTF_CLONING))
102 int nd6_prune = 1; /* walk list every 1 seconds */
103 int nd6_delay = 5; /* delay first probe time 5 second */
104 int nd6_umaxtries = 3; /* maximum unicast query */
105 int nd6_mmaxtries = 3; /* maximum multicast query */
106 int nd6_useloopback = 1; /* use loopback interface for local traffic */
107 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
109 /* preventing too many loops in ND option parsing */
110 int nd6_maxndopt = 10; /* max # of ND options allowed */
112 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
121 static int nd6_inuse, nd6_allocated;
123 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
124 struct nd_drhead nd_defrouter;
125 struct nd_prhead nd_prefix = { 0 };
126 struct mtx nd6_mtx = MTX_INITIALIZER("nd6");
128 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
129 static struct sockaddr_in6 all1_sa;
131 static void nd6_setmtu0 (struct ifnet *, struct nd_ifinfo *);
132 static int regen_tmpaddr (struct in6_ifaddr *);
133 static void nd6_slowtimo(void *);
134 static void nd6_slowtimo_dispatch(netmsg_t);
135 static void nd6_timer(void *);
136 static void nd6_timer_dispatch(netmsg_t);
138 static struct callout nd6_slowtimo_ch;
139 static struct netmsg_base nd6_slowtimo_netmsg;
141 static struct callout nd6_timer_ch;
142 static struct netmsg_base nd6_timer_netmsg;
147 static int nd6_init_done = 0;
151 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
155 all1_sa.sin6_family = AF_INET6;
156 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
157 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
158 all1_sa.sin6_addr.s6_addr[i] = 0xff;
160 /* initialization of the default router list */
161 TAILQ_INIT(&nd_defrouter);
166 callout_init_mp(&nd6_slowtimo_ch);
167 netmsg_init(&nd6_slowtimo_netmsg, NULL, &netisr_adone_rport,
168 MSGF_PRIORITY, nd6_slowtimo_dispatch);
169 callout_reset_bycpu(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
170 nd6_slowtimo, NULL, 0);
174 nd6_ifattach(struct ifnet *ifp)
176 struct nd_ifinfo *nd;
178 nd = (struct nd_ifinfo *)kmalloc(sizeof(*nd), M_IP6NDP,
183 nd->chlim = IPV6_DEFHLIM;
184 nd->basereachable = REACHABLE_TIME;
185 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
186 nd->retrans = RETRANS_TIMER;
189 * Note that the default value of ip6_accept_rtadv is 0, which means
190 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
193 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
195 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
196 nd6_setmtu0(ifp, nd);
201 nd6_ifdetach(struct nd_ifinfo *nd)
207 * Reset ND level link MTU. This function is called when the physical MTU
208 * changes, which means we might have to adjust the ND level MTU.
211 nd6_setmtu(struct ifnet *ifp)
213 nd6_setmtu0(ifp, ND_IFINFO(ifp));
216 struct netmsg_nd6setmtu {
217 struct netmsg_base nmsg;
219 struct nd_ifinfo *ndi;
222 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
224 nd6_setmtu0_dispatch(netmsg_t msg)
226 struct netmsg_nd6setmtu *nmsg = (struct netmsg_nd6setmtu *)msg;
227 struct ifnet *ifp = nmsg->ifp;
228 struct nd_ifinfo *ndi = nmsg->ndi;
231 omaxmtu = ndi->maxmtu;
233 switch (ifp->if_type) {
235 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
237 case IFT_IEEE1394: /* XXX should be IEEE1394MTU(1500) */
238 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
241 case IFT_IEEE80211: /* XXX should be IEEE80211MTU(1500) */
242 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
246 ndi->maxmtu = ifp->if_mtu;
251 * Decreasing the interface MTU under IPV6 minimum MTU may cause
252 * undesirable situation. We thus notify the operator of the change
253 * explicitly. The check for omaxmtu is necessary to restrict the
254 * log to the case of changing the MTU, not initializing it.
256 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
257 log(LOG_NOTICE, "nd6_setmtu0: "
258 "new link MTU on %s (%lu) is too small for IPv6\n",
259 if_name(ifp), (unsigned long)ndi->maxmtu);
262 if (ndi->maxmtu > in6_maxmtu)
263 in6_setmaxmtu(); /* check all interfaces just in case */
265 lwkt_replymsg(&nmsg->nmsg.lmsg, 0);
269 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
271 struct netmsg_nd6setmtu nmsg;
273 netmsg_init(&nmsg.nmsg, NULL, &curthread->td_msgport, 0,
274 nd6_setmtu0_dispatch);
277 lwkt_domsg(netisr_cpuport(0), &nmsg.nmsg.lmsg, 0);
281 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
283 bzero(ndopts, sizeof(*ndopts));
284 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
286 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
289 ndopts->nd_opts_done = 1;
290 ndopts->nd_opts_search = NULL;
295 * Take one ND option.
298 nd6_option(union nd_opts *ndopts)
300 struct nd_opt_hdr *nd_opt;
304 panic("ndopts == NULL in nd6_option");
305 if (!ndopts->nd_opts_last)
306 panic("uninitialized ndopts in nd6_option");
307 if (!ndopts->nd_opts_search)
309 if (ndopts->nd_opts_done)
312 nd_opt = ndopts->nd_opts_search;
314 /* make sure nd_opt_len is inside the buffer */
315 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
316 bzero(ndopts, sizeof(*ndopts));
320 olen = nd_opt->nd_opt_len << 3;
323 * Message validation requires that all included
324 * options have a length that is greater than zero.
326 bzero(ndopts, sizeof(*ndopts));
330 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
331 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
332 /* option overruns the end of buffer, invalid */
333 bzero(ndopts, sizeof(*ndopts));
335 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
336 /* reached the end of options chain */
337 ndopts->nd_opts_done = 1;
338 ndopts->nd_opts_search = NULL;
344 * Parse multiple ND options.
345 * This function is much easier to use, for ND routines that do not need
346 * multiple options of the same type.
349 nd6_options(union nd_opts *ndopts)
351 struct nd_opt_hdr *nd_opt;
355 panic("ndopts == NULL in nd6_options");
356 if (!ndopts->nd_opts_last)
357 panic("uninitialized ndopts in nd6_options");
358 if (!ndopts->nd_opts_search)
362 nd_opt = nd6_option(ndopts);
363 if (!nd_opt && !ndopts->nd_opts_last) {
365 * Message validation requires that all included
366 * options have a length that is greater than zero.
368 icmp6stat.icp6s_nd_badopt++;
369 bzero(ndopts, sizeof(*ndopts));
376 switch (nd_opt->nd_opt_type) {
377 case ND_OPT_SOURCE_LINKADDR:
378 case ND_OPT_TARGET_LINKADDR:
380 case ND_OPT_REDIRECTED_HEADER:
381 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
383 "duplicated ND6 option found (type=%d)\n",
384 nd_opt->nd_opt_type));
387 ndopts->nd_opt_array[nd_opt->nd_opt_type]
391 case ND_OPT_PREFIX_INFORMATION:
392 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
393 ndopts->nd_opt_array[nd_opt->nd_opt_type]
396 ndopts->nd_opts_pi_end =
397 (struct nd_opt_prefix_info *)nd_opt;
401 * Unknown options must be silently ignored,
402 * to accomodate future extension to the protocol.
405 "nd6_options: unsupported option %d - "
406 "option ignored\n", nd_opt->nd_opt_type));
411 if (i > nd6_maxndopt) {
412 icmp6stat.icp6s_nd_toomanyopt++;
413 nd6log((LOG_INFO, "too many loop in nd opt\n"));
417 if (ndopts->nd_opts_done)
425 * ND6 timer routine to expire default route list and prefix list
428 nd6_timer_dispatch(netmsg_t nmsg)
430 struct llinfo_nd6 *ln;
431 struct nd_defrouter *dr;
432 struct nd_prefix *pr;
434 struct in6_ifaddr *ia6, *nia6;
439 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
444 ln = llinfo_nd6.ln_next;
445 while (ln && ln != &llinfo_nd6) {
447 struct sockaddr_in6 *dst;
448 struct llinfo_nd6 *next = ln->ln_next;
449 /* XXX: used for the DELAY case only: */
450 struct nd_ifinfo *ndi = NULL;
452 if ((rt = ln->ln_rt) == NULL) {
456 if ((ifp = rt->rt_ifp) == NULL) {
460 ndi = ND_IFINFO(ifp);
461 dst = (struct sockaddr_in6 *)rt_key(rt);
463 if (ln->ln_expire > time_uptime) {
470 panic("rt=0 in nd6_timer(ln=%p)", ln);
471 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
472 panic("rt_llinfo(%p) is not equal to ln(%p)",
475 panic("dst=0 in nd6_timer(ln=%p)", ln);
477 switch (ln->ln_state) {
478 case ND6_LLINFO_INCOMPLETE:
479 if (ln->ln_asked < nd6_mmaxtries) {
481 ln->ln_expire = time_uptime +
482 ND_IFINFO(ifp)->retrans / 1000;
483 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
486 struct mbuf *m = ln->ln_hold;
490 * Fake rcvif to make ICMP error
491 * more helpful in diagnosing
493 * XXX: should we consider
496 m->m_pkthdr.rcvif = rt->rt_ifp;
498 icmp6_error(m, ICMP6_DST_UNREACH,
499 ICMP6_DST_UNREACH_ADDR, 0);
505 case ND6_LLINFO_REACHABLE:
507 ln->ln_state = ND6_LLINFO_STALE;
508 ln->ln_expire = time_uptime + nd6_gctimer;
512 case ND6_LLINFO_STALE:
513 /* Garbage Collection(RFC 2461 5.3) */
518 case ND6_LLINFO_DELAY:
519 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD)) {
522 ln->ln_state = ND6_LLINFO_PROBE;
523 ln->ln_expire = time_uptime +
525 nd6_ns_output(ifp, &dst->sin6_addr,
529 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
530 ln->ln_expire = time_uptime + nd6_gctimer;
533 case ND6_LLINFO_PROBE:
534 if (ln->ln_asked < nd6_umaxtries) {
536 ln->ln_expire = time_uptime +
537 ND_IFINFO(ifp)->retrans / 1000;
538 nd6_ns_output(ifp, &dst->sin6_addr,
539 &dst->sin6_addr, ln, 0);
548 /* expire default router list */
549 dr = TAILQ_FIRST(&nd_defrouter);
551 if (dr->expire && dr->expire < time_uptime) {
552 struct nd_defrouter *t;
553 t = TAILQ_NEXT(dr, dr_entry);
557 dr = TAILQ_NEXT(dr, dr_entry);
562 * expire interface addresses.
563 * in the past the loop was inside prefix expiry processing.
564 * However, from a stricter speci-confrmance standpoint, we should
565 * rather separate address lifetimes and prefix lifetimes.
568 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
570 /* check address lifetime */
571 if (IFA6_IS_INVALID(ia6)) {
575 * If the expiring address is temporary, try
576 * regenerating a new one. This would be useful when
577 * we suspended a laptop PC, then turned it on after a
578 * period that could invalidate all temporary
579 * addresses. Although we may have to restart the
580 * loop (see below), it must be after purging the
581 * address. Otherwise, we'd see an infinite loop of
584 if (ip6_use_tempaddr &&
585 (ia6->ia6_flags & IN6_IFF_TEMPORARY)) {
586 if (regen_tmpaddr(ia6) == 0)
590 in6_purgeaddr(&ia6->ia_ifa);
593 goto addrloop; /* XXX: see below */
595 if (IFA6_IS_DEPRECATED(ia6)) {
596 int oldflags = ia6->ia6_flags;
598 if ((oldflags & IN6_IFF_DEPRECATED) == 0) {
599 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
600 in6_newaddrmsg((struct ifaddr *)ia6);
604 * If a temporary address has just become deprecated,
605 * regenerate a new one if possible.
607 if (ip6_use_tempaddr &&
608 (ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
609 !(oldflags & IN6_IFF_DEPRECATED)) {
611 if (regen_tmpaddr(ia6) == 0) {
613 * A new temporary address is
615 * XXX: this means the address chain
616 * has changed while we are still in
617 * the loop. Although the change
618 * would not cause disaster (because
619 * it's not a deletion, but an
620 * addition,) we'd rather restart the
621 * loop just for safety. Or does this
622 * significantly reduce performance??
629 * A new RA might have made a deprecated address
632 if (ia6->ia6_flags & IN6_IFF_DEPRECATED) {
633 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
634 in6_newaddrmsg((struct ifaddr *)ia6);
639 /* expire prefix list */
640 pr = nd_prefix.lh_first;
643 * check prefix lifetime.
644 * since pltime is just for autoconf, pltime processing for
645 * prefix is not necessary.
647 if (pr->ndpr_expire && pr->ndpr_expire < time_uptime) {
652 * address expiration and prefix expiration are
653 * separate. NEVER perform in6_purgeaddr here.
662 mtx_unlock(&nd6_mtx);
664 callout_reset(&nd6_timer_ch, nd6_prune * hz, nd6_timer, NULL);
668 nd6_timer(void *arg __unused)
670 struct lwkt_msg *lmsg = &nd6_timer_netmsg.lmsg;
672 KASSERT(mycpuid == 0, ("not on cpu0"));
674 if (lmsg->ms_flags & MSGF_DONE)
675 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
682 callout_init_mp(&nd6_timer_ch);
683 netmsg_init(&nd6_timer_netmsg, NULL, &netisr_adone_rport,
684 MSGF_PRIORITY, nd6_timer_dispatch);
685 callout_reset_bycpu(&nd6_timer_ch, hz, nd6_timer, NULL, 0);
689 regen_tmpaddr(struct in6_ifaddr *ia6) /* deprecated/invalidated temporary
692 struct ifaddr_container *ifac;
694 struct in6_ifaddr *public_ifa6 = NULL;
696 ifp = ia6->ia_ifa.ifa_ifp;
697 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
698 struct ifaddr *ifa = ifac->ifa;
699 struct in6_ifaddr *it6;
701 if (ifa->ifa_addr->sa_family != AF_INET6)
704 it6 = (struct in6_ifaddr *)ifa;
706 /* ignore no autoconf addresses. */
707 if (!(it6->ia6_flags & IN6_IFF_AUTOCONF))
710 /* ignore autoconf addresses with different prefixes. */
711 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
715 * Now we are looking at an autoconf address with the same
716 * prefix as ours. If the address is temporary and is still
717 * preferred, do not create another one. It would be rare, but
718 * could happen, for example, when we resume a laptop PC after
721 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) &&
722 !IFA6_IS_DEPRECATED(it6)) {
728 * This is a public autoconf address that has the same prefix
729 * as ours. If it is preferred, keep it. We can't break the
730 * loop here, because there may be a still-preferred temporary
731 * address with the prefix.
733 if (!IFA6_IS_DEPRECATED(it6))
737 if (public_ifa6 != NULL) {
740 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
741 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
742 " tmp addr,errno=%d\n", e);
752 * Nuke neighbor cache/prefix/default router management table, right before
756 nd6_purge(struct ifnet *ifp)
758 struct llinfo_nd6 *ln, *nln;
759 struct nd_defrouter *dr, *ndr, drany;
760 struct nd_prefix *pr, *npr;
762 /* Nuke default router list entries toward ifp */
763 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
765 * The first entry of the list may be stored in
766 * the routing table, so we'll delete it later.
768 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
769 ndr = TAILQ_NEXT(dr, dr_entry);
773 dr = TAILQ_FIRST(&nd_defrouter);
778 /* Nuke prefix list entries toward ifp */
779 for (pr = nd_prefix.lh_first; pr; pr = npr) {
781 if (pr->ndpr_ifp == ifp) {
783 * Previously, pr->ndpr_addr is removed as well,
784 * but I strongly believe we don't have to do it.
785 * nd6_purge() is only called from in6_ifdetach(),
786 * which removes all the associated interface addresses
788 * (jinmei@kame.net 20010129)
794 /* cancel default outgoing interface setting */
795 if (nd6_defifindex == ifp->if_index)
796 nd6_setdefaultiface(0);
798 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
799 /* refresh default router list */
800 bzero(&drany, sizeof(drany));
801 defrouter_delreq(&drany, 0);
806 * Nuke neighbor cache entries for the ifp.
807 * Note that rt->rt_ifp may not be the same as ifp,
808 * due to KAME goto ours hack. See RTM_RESOLVE case in
809 * nd6_rtrequest(), and ip6_input().
811 ln = llinfo_nd6.ln_next;
812 while (ln && ln != &llinfo_nd6) {
814 struct sockaddr_dl *sdl;
818 if (rt && rt->rt_gateway &&
819 rt->rt_gateway->sa_family == AF_LINK) {
820 sdl = (struct sockaddr_dl *)rt->rt_gateway;
821 if (sdl->sdl_index == ifp->if_index)
829 nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp)
832 struct sockaddr_in6 sin6;
834 bzero(&sin6, sizeof(sin6));
835 sin6.sin6_len = sizeof(struct sockaddr_in6);
836 sin6.sin6_family = AF_INET6;
837 sin6.sin6_addr = *addr6;
840 rt = rtlookup((struct sockaddr *)&sin6);
842 rt = rtpurelookup((struct sockaddr *)&sin6);
843 if (rt && !(rt->rt_flags & RTF_LLINFO)) {
845 * This is the case for the default route.
846 * If we want to create a neighbor cache for the address, we
847 * should free the route for the destination and allocate an
860 * If no route is available and create is set,
861 * we allocate a host route for the destination
862 * and treat it like an interface route.
863 * This hack is necessary for a neighbor which can't
864 * be covered by our own prefix.
868 ifa = ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
873 * Create a new route. RTF_LLINFO is necessary
874 * to create a Neighbor Cache entry for the
875 * destination in nd6_rtrequest which will be
876 * called in rtrequest via ifa->ifa_rtrequest.
878 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
879 ifa->ifa_addr, (struct sockaddr *)&all1_sa,
880 (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
881 ~RTF_CLONING, &rt)) != 0) {
883 "nd6_lookup: failed to add route for a "
884 "neighbor(%s), errno=%d\n",
885 ip6_sprintf(addr6), e);
890 struct llinfo_nd6 *ln =
891 (struct llinfo_nd6 *)rt->rt_llinfo;
893 ln->ln_state = ND6_LLINFO_NOSTATE;
900 if (!ND6_RTENTRY_IS_NEIGHBOR(rt, ifp)) {
903 "nd6_lookup: failed to lookup %s (if = %s)\n",
904 ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
905 /* xxx more logs... kazu */
912 static struct rtentry *
913 nd6_neighbor_lookup(struct in6_addr *addr6, struct ifnet *ifp)
916 struct sockaddr_in6 sin6;
918 bzero(&sin6, sizeof(sin6));
919 sin6.sin6_len = sizeof(struct sockaddr_in6);
920 sin6.sin6_family = AF_INET6;
921 sin6.sin6_addr = *addr6;
923 rt = rtpurelookup((struct sockaddr *)&sin6);
928 if (!ND6_RTENTRY_IS_NEIGHBOR(rt, ifp)) {
929 if (nd6_onlink_ns_rfc4861 &&
930 (ND6_RTENTRY_IS_LLCLONING(rt) || /* not cloned yet */
931 (rt->rt_parent != NULL && /* cloning */
932 ND6_RTENTRY_IS_LLCLONING(rt->rt_parent)))) {
934 * If cloning ever happened or is happening,
935 * rtentry for addr6 would or will become a
946 * Detect if a given IPv6 address identifies a neighbor on a given link.
947 * XXX: should take care of the destination of a p2p link?
950 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
952 struct ifaddr_container *ifac;
955 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
956 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
959 * A link-local address is always a neighbor.
960 * XXX: we should use the sin6_scope_id field rather than the embedded
963 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
964 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
968 * If the address matches one of our addresses,
969 * it should be a neighbor.
971 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
972 struct ifaddr *ifa = ifac->ifa;
974 if (ifa->ifa_addr->sa_family != AF_INET6)
977 for (i = 0; i < 4; i++) {
978 if ((IFADDR6(ifa).s6_addr32[i] ^
979 addr->sin6_addr.s6_addr32[i]) &
980 IFMASK6(ifa).s6_addr32[i])
987 * Even if the address matches none of our addresses, it might be
988 * in the neighbor cache.
990 if (nd6_neighbor_lookup(&addr->sin6_addr, ifp) != NULL)
999 * Free an nd6 llinfo entry.
1002 nd6_free(struct rtentry *rt)
1004 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
1005 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
1006 struct nd_defrouter *dr;
1009 * we used to have kpfctlinput(PRC_HOSTDEAD) here.
1010 * even though it is not harmful, it was not really necessary.
1013 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
1015 dr = defrouter_lookup(
1016 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1019 if (ln->ln_router || dr) {
1021 * rt6_flush must be called whether or not the neighbor
1022 * is in the Default Router List.
1023 * See a corresponding comment in nd6_na_input().
1025 rt6_flush(&in6, rt->rt_ifp);
1030 * Unreachablity of a router might affect the default
1031 * router selection and on-link detection of advertised
1036 * Temporarily fake the state to choose a new default
1037 * router and to perform on-link determination of
1038 * prefixes correctly.
1039 * Below the state will be set correctly,
1040 * or the entry itself will be deleted.
1042 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1045 * Since defrouter_select() does not affect the
1046 * on-link determination and MIP6 needs the check
1047 * before the default router selection, we perform
1050 pfxlist_onlink_check();
1052 if (dr == TAILQ_FIRST(&nd_defrouter)) {
1054 * It is used as the current default router,
1055 * so we have to move it to the end of the
1056 * list and choose a new one.
1057 * XXX: it is not very efficient if this is
1060 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
1061 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
1066 mtx_unlock(&nd6_mtx);
1070 * Before deleting the entry, remember the next entry as the
1071 * return value. We need this because pfxlist_onlink_check() above
1072 * might have freed other entries (particularly the old next entry) as
1073 * a side effect (XXX).
1078 * Detach the route from the routing tree and the list of neighbor
1079 * caches, and disable the route entry not to be used in already
1082 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1088 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1090 * XXX cost-effective metods?
1093 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1095 struct llinfo_nd6 *ln;
1098 * If the caller specified "rt", use that. Otherwise, resolve the
1099 * routing table by supplied "dst6".
1104 if (!(rt = nd6_lookup(dst6, 0, NULL)))
1108 if ((rt->rt_flags & RTF_GATEWAY) ||
1109 !(rt->rt_flags & RTF_LLINFO) ||
1110 rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
1111 rt->rt_gateway->sa_family != AF_LINK) {
1112 /* This is not a host route. */
1116 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1117 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1121 * if we get upper-layer reachability confirmation many times,
1122 * it is possible we have false information.
1126 if (ln->ln_byhint > nd6_maxnudhint)
1130 ln->ln_state = ND6_LLINFO_REACHABLE;
1132 ln->ln_expire = time_uptime +
1133 ND_IFINFO(rt->rt_ifp)->reachable;
1137 nd6_rtrequest(int req, struct rtentry *rt)
1139 struct sockaddr *gate = rt->rt_gateway;
1140 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1141 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1142 struct ifnet *ifp = rt->rt_ifp;
1145 if ((rt->rt_flags & RTF_GATEWAY))
1148 if (nd6_need_cache(ifp) == 0 && !(rt->rt_flags & RTF_HOST)) {
1150 * This is probably an interface direct route for a link
1151 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1152 * We do not need special treatment below for such a route.
1153 * Moreover, the RTF_LLINFO flag which would be set below
1154 * would annoy the ndp(8) command.
1159 if (req == RTM_RESOLVE &&
1160 (nd6_need_cache(ifp) == 0 || /* stf case */
1161 !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp))) {
1163 * FreeBSD and BSD/OS often make a cloned host route based
1164 * on a less-specific route (e.g. the default route).
1165 * If the less specific route does not have a "gateway"
1166 * (this is the case when the route just goes to a p2p or an
1167 * stf interface), we'll mistakenly make a neighbor cache for
1168 * the host route, and will see strange neighbor solicitation
1169 * for the corresponding destination. In order to avoid the
1170 * confusion, we check if the destination of the route is
1171 * a neighbor in terms of neighbor discovery, and stop the
1172 * process if not. Additionally, we remove the LLINFO flag
1173 * so that ndp(8) will not try to get the neighbor information
1174 * of the destination.
1176 rt->rt_flags &= ~RTF_LLINFO;
1183 * There is no backward compatibility :)
1185 * if (!(rt->rt_flags & RTF_HOST) &&
1186 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1187 * rt->rt_flags |= RTF_CLONING;
1189 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1191 * Case 1: This route should come from
1192 * a route to interface. RTF_LLINFO flag is set
1193 * for a host route whose destination should be
1194 * treated as on-link.
1196 rt_setgate(rt, rt_key(rt),
1197 (struct sockaddr *)&null_sdl,
1199 gate = rt->rt_gateway;
1200 SDL(gate)->sdl_type = ifp->if_type;
1201 SDL(gate)->sdl_index = ifp->if_index;
1203 ln->ln_expire = time_uptime;
1204 if (ln && ln->ln_expire == 0) {
1205 /* kludge for desktops */
1208 if ((rt->rt_flags & RTF_CLONING))
1212 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1213 * We don't do that here since llinfo is not ready yet.
1215 * There are also couple of other things to be discussed:
1216 * - unsolicited NA code needs improvement beforehand
1217 * - RFC2461 says we MAY send multicast unsolicited NA
1218 * (7.2.6 paragraph 4), however, it also says that we
1219 * SHOULD provide a mechanism to prevent multicast NA storm.
1220 * we don't have anything like it right now.
1221 * note that the mechanism needs a mutual agreement
1222 * between proxies, which means that we need to implement
1223 * a new protocol, or a new kludge.
1224 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1225 * we need to check ip6forwarding before sending it.
1226 * (or should we allow proxy ND configuration only for
1227 * routers? there's no mention about proxy ND from hosts)
1230 /* XXX it does not work */
1231 if ((rt->rt_flags & RTF_ANNOUNCE) && mycpuid == 0) {
1233 &SIN6(rt_key(rt))->sin6_addr,
1234 &SIN6(rt_key(rt))->sin6_addr,
1235 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1241 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1243 * Address resolution isn't necessary for a point to
1244 * point link, so we can skip this test for a p2p link.
1246 if (gate->sa_family != AF_LINK ||
1247 gate->sa_len < sizeof(null_sdl)) {
1249 "nd6_rtrequest: bad gateway value: %s\n",
1253 SDL(gate)->sdl_type = ifp->if_type;
1254 SDL(gate)->sdl_index = ifp->if_index;
1257 break; /* This happens on a route change */
1259 * Case 2: This route may come from cloning, or a manual route
1260 * add with a LL address.
1262 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1263 rt->rt_llinfo = (caddr_t)ln;
1265 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1270 bzero(ln, sizeof(*ln));
1272 /* this is required for "ndp" command. - shin */
1273 if (req == RTM_ADD) {
1275 * gate should have some valid AF_LINK entry,
1276 * and ln->ln_expire should have some lifetime
1277 * which is specified by ndp command.
1279 ln->ln_state = ND6_LLINFO_REACHABLE;
1283 * When req == RTM_RESOLVE, rt is created and
1284 * initialized in rtrequest(), so rt_expire is 0.
1286 ln->ln_state = ND6_LLINFO_NOSTATE;
1287 ln->ln_expire = time_uptime;
1289 rt->rt_flags |= RTF_LLINFO;
1290 ln->ln_next = llinfo_nd6.ln_next;
1291 llinfo_nd6.ln_next = ln;
1292 ln->ln_prev = &llinfo_nd6;
1293 ln->ln_next->ln_prev = ln;
1296 * check if rt_key(rt) is one of my address assigned
1299 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1300 &SIN6(rt_key(rt))->sin6_addr);
1302 caddr_t macp = nd6_ifptomac(ifp);
1304 ln->ln_state = ND6_LLINFO_REACHABLE;
1307 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1308 SDL(gate)->sdl_alen = ifp->if_addrlen;
1310 if (nd6_useloopback) {
1311 rt->rt_ifp = loif; /* XXX */
1313 * Make sure rt_ifa be equal to the ifaddr
1314 * corresponding to the address.
1315 * We need this because when we refer
1316 * rt_ifa->ia6_flags in ip6_input, we assume
1317 * that the rt_ifa points to the address instead
1318 * of the loopback address.
1320 if (ifa != rt->rt_ifa) {
1321 IFAFREE(rt->rt_ifa);
1326 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1328 ln->ln_state = ND6_LLINFO_REACHABLE;
1332 * Join solicited node multicast for proxy ND, and only
1333 * join it once on cpu0.
1335 if ((ifp->if_flags & IFF_MULTICAST) && mycpuid == 0) {
1336 struct in6_addr llsol;
1339 llsol = SIN6(rt_key(rt))->sin6_addr;
1340 llsol.s6_addr16[0] = htons(0xff02);
1341 llsol.s6_addr16[1] = htons(ifp->if_index);
1342 llsol.s6_addr32[1] = 0;
1343 llsol.s6_addr32[2] = htonl(1);
1344 llsol.s6_addr8[12] = 0xff;
1346 if (!in6_addmulti(&llsol, ifp, &error)) {
1347 nd6log((LOG_ERR, "%s: failed to join "
1348 "%s (errno=%d)\n", if_name(ifp),
1349 ip6_sprintf(&llsol), error));
1359 * Leave from solicited node multicast for proxy ND, and only
1360 * leave it once on cpu0 (since we joined it once on cpu0).
1362 if ((rt->rt_flags & RTF_ANNOUNCE) &&
1363 (ifp->if_flags & IFF_MULTICAST) && mycpuid == 0) {
1364 struct in6_addr llsol;
1365 struct in6_multi *in6m;
1367 llsol = SIN6(rt_key(rt))->sin6_addr;
1368 llsol.s6_addr16[0] = htons(0xff02);
1369 llsol.s6_addr16[1] = htons(ifp->if_index);
1370 llsol.s6_addr32[1] = 0;
1371 llsol.s6_addr32[2] = htonl(1);
1372 llsol.s6_addr8[12] = 0xff;
1374 in6m = IN6_LOOKUP_MULTI(&llsol, ifp);
1379 ln->ln_next->ln_prev = ln->ln_prev;
1380 ln->ln_prev->ln_next = ln->ln_next;
1383 rt->rt_flags &= ~RTF_LLINFO;
1385 m_freem(ln->ln_hold);
1391 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1393 struct in6_drlist *drl = (struct in6_drlist *)data;
1394 struct in6_prlist *prl = (struct in6_prlist *)data;
1395 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1396 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1397 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1398 struct nd_defrouter *dr, any;
1399 struct nd_prefix *pr;
1401 int i = 0, error = 0;
1404 case SIOCGDRLST_IN6:
1406 * obsolete API, use sysctl under net.inet6.icmp6
1408 bzero(drl, sizeof(*drl));
1410 dr = TAILQ_FIRST(&nd_defrouter);
1411 while (dr && i < DRLSTSIZ) {
1412 drl->defrouter[i].rtaddr = dr->rtaddr;
1413 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1414 /* XXX: need to this hack for KAME stack */
1415 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1418 "default router list contains a "
1419 "non-linklocal address(%s)\n",
1420 ip6_sprintf(&drl->defrouter[i].rtaddr));
1422 drl->defrouter[i].flags = dr->flags;
1423 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1424 drl->defrouter[i].expire = dr->expire;
1425 drl->defrouter[i].if_index = dr->ifp->if_index;
1427 dr = TAILQ_NEXT(dr, dr_entry);
1429 mtx_unlock(&nd6_mtx);
1431 case SIOCGPRLST_IN6:
1433 * obsolete API, use sysctl under net.inet6.icmp6
1436 * XXX meaning of fields, especialy "raflags", is very
1437 * differnet between RA prefix list and RR/static prefix list.
1438 * how about separating ioctls into two?
1440 bzero(prl, sizeof(*prl));
1442 pr = nd_prefix.lh_first;
1443 while (pr && i < PRLSTSIZ) {
1444 struct nd_pfxrouter *pfr;
1447 in6_embedscope(&prl->prefix[i].prefix,
1448 &pr->ndpr_prefix, NULL, NULL);
1449 prl->prefix[i].raflags = pr->ndpr_raf;
1450 prl->prefix[i].prefixlen = pr->ndpr_plen;
1451 prl->prefix[i].vltime = pr->ndpr_vltime;
1452 prl->prefix[i].pltime = pr->ndpr_pltime;
1453 prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1454 prl->prefix[i].expire = pr->ndpr_expire;
1456 pfr = pr->ndpr_advrtrs.lh_first;
1460 #define RTRADDR prl->prefix[i].advrtr[j]
1461 RTRADDR = pfr->router->rtaddr;
1462 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1463 /* XXX: hack for KAME */
1464 RTRADDR.s6_addr16[1] = 0;
1467 "a router(%s) advertises "
1469 "non-link local address\n",
1470 ip6_sprintf(&RTRADDR));
1474 pfr = pfr->pfr_next;
1476 prl->prefix[i].advrtrs = j;
1477 prl->prefix[i].origin = PR_ORIG_RA;
1482 mtx_unlock(&nd6_mtx);
1485 case OSIOCGIFINFO_IN6:
1486 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1487 bzero(&ndi->ndi, sizeof(ndi->ndi));
1488 ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1489 ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1490 ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1491 ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1492 ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1493 ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1494 ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1495 ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1497 case SIOCGIFINFO_IN6:
1498 ndi->ndi = *ND_IFINFO(ifp);
1499 ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1501 case SIOCSIFINFO_FLAGS:
1502 ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1504 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1505 /* flush default router list */
1507 * xxx sumikawa: should not delete route if default
1508 * route equals to the top of default router list
1510 bzero(&any, sizeof(any));
1511 defrouter_delreq(&any, 0);
1513 /* xxx sumikawa: flush prefix list */
1515 case SIOCSPFXFLUSH_IN6:
1517 /* flush all the prefix advertised by routers */
1518 struct nd_prefix *pr, *next;
1521 for (pr = nd_prefix.lh_first; pr; pr = next) {
1522 struct in6_ifaddr *ia, *ia_next;
1524 next = pr->ndpr_next;
1526 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1529 /* do we really have to remove addresses as well? */
1530 for (ia = in6_ifaddr; ia; ia = ia_next) {
1531 /* ia might be removed. keep the next ptr. */
1532 ia_next = ia->ia_next;
1534 if (!(ia->ia6_flags & IN6_IFF_AUTOCONF))
1537 if (ia->ia6_ndpr == pr)
1538 in6_purgeaddr(&ia->ia_ifa);
1542 mtx_unlock(&nd6_mtx);
1545 case SIOCSRTRFLUSH_IN6:
1547 /* flush all the default routers */
1548 struct nd_defrouter *dr, *next;
1551 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1553 * The first entry of the list may be stored in
1554 * the routing table, so we'll delete it later.
1556 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1557 next = TAILQ_NEXT(dr, dr_entry);
1560 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1562 mtx_unlock(&nd6_mtx);
1565 case SIOCGNBRINFO_IN6:
1567 struct llinfo_nd6 *ln;
1568 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1571 * XXX: KAME specific hack for scoped addresses
1572 * XXXX: for other scopes than link-local?
1574 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1575 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1576 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1579 *idp = htons(ifp->if_index);
1583 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1585 mtx_unlock(&nd6_mtx);
1588 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1589 nbi->state = ln->ln_state;
1590 nbi->asked = ln->ln_asked;
1591 nbi->isrouter = ln->ln_router;
1592 nbi->expire = ln->ln_expire;
1593 mtx_unlock(&nd6_mtx);
1597 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1598 ndif->ifindex = nd6_defifindex;
1600 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1601 return (nd6_setdefaultiface(ndif->ifindex));
1607 * Create neighbor cache entry and cache link-layer address,
1608 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1611 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1613 int type, /* ICMP6 type */
1614 int code /* type dependent information */)
1616 struct rtentry *rt = NULL;
1617 struct llinfo_nd6 *ln = NULL;
1619 struct sockaddr_dl *sdl = NULL;
1626 panic("ifp == NULL in nd6_cache_lladdr");
1628 panic("from == NULL in nd6_cache_lladdr");
1630 /* nothing must be updated for unspecified address */
1631 if (IN6_IS_ADDR_UNSPECIFIED(from))
1635 * Validation about ifp->if_addrlen and lladdrlen must be done in
1638 * XXX If the link does not have link-layer adderss, what should
1639 * we do? (ifp->if_addrlen == 0)
1640 * Spec says nothing in sections for RA, RS and NA. There's small
1641 * description on it in NS section (RFC 2461 7.2.3).
1644 rt = nd6_lookup(from, 0, ifp);
1647 /* nothing must be done if there's no lladdr */
1648 if (!lladdr || !lladdrlen)
1652 rt = nd6_lookup(from, 1, ifp);
1655 /* do nothing if static ndp is set */
1656 if (rt->rt_flags & RTF_STATIC)
1663 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1668 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1671 if (!rt->rt_gateway)
1673 if (rt->rt_gateway->sa_family != AF_LINK)
1675 sdl = SDL(rt->rt_gateway);
1677 olladdr = (sdl->sdl_alen) ? 1 : 0;
1678 if (olladdr && lladdr) {
1679 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1687 * newentry olladdr lladdr llchange (*=record)
1690 * 0 n y -- (3) * STALE
1692 * 0 y y y (5) * STALE
1693 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1694 * 1 -- y -- (7) * STALE
1697 if (lladdr) { /* (3-5) and (7) */
1699 * Record source link-layer address
1700 * XXX is it dependent to ifp->if_type?
1702 sdl->sdl_alen = ifp->if_addrlen;
1703 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1707 if ((!olladdr && lladdr) || /* (3) */
1708 (olladdr && lladdr && llchange)) { /* (5) */
1710 newstate = ND6_LLINFO_STALE;
1711 } else { /* (1-2,4) */
1716 if (!lladdr) /* (6) */
1717 newstate = ND6_LLINFO_NOSTATE;
1719 newstate = ND6_LLINFO_STALE;
1724 * Update the state of the neighbor cache.
1726 ln->ln_state = newstate;
1728 if (ln->ln_state == ND6_LLINFO_STALE) {
1730 * XXX: since nd6_output() below will cause
1731 * state tansition to DELAY and reset the timer,
1732 * we must set the timer now, although it is actually
1735 ln->ln_expire = time_uptime + nd6_gctimer;
1739 * we assume ifp is not a p2p here, so just
1740 * set the 2nd argument as the 1st one.
1742 nd6_output(ifp, ifp, ln->ln_hold,
1743 (struct sockaddr_in6 *)rt_key(rt), rt);
1746 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1747 /* probe right away */
1748 ln->ln_expire = time_uptime;
1753 * ICMP6 type dependent behavior.
1755 * NS: clear IsRouter if new entry
1756 * RS: clear IsRouter
1757 * RA: set IsRouter if there's lladdr
1758 * redir: clear IsRouter if new entry
1761 * The spec says that we must set IsRouter in the following cases:
1762 * - If lladdr exist, set IsRouter. This means (1-5).
1763 * - If it is old entry (!newentry), set IsRouter. This means (7).
1764 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1765 * A quetion arises for (1) case. (1) case has no lladdr in the
1766 * neighbor cache, this is similar to (6).
1767 * This case is rare but we figured that we MUST NOT set IsRouter.
1769 * newentry olladdr lladdr llchange NS RS RA redir
1771 * 0 n n -- (1) c ? s
1772 * 0 y n -- (2) c s s
1773 * 0 n y -- (3) c s s
1776 * 1 -- n -- (6) c c c s
1777 * 1 -- y -- (7) c c s c s
1781 switch (type & 0xff) {
1782 case ND_NEIGHBOR_SOLICIT:
1784 * New entry must have is_router flag cleared.
1786 if (is_newentry) /* (6-7) */
1791 * If the icmp is a redirect to a better router, always set the
1792 * is_router flag. Otherwise, if the entry is newly created,
1793 * clear the flag. [RFC 2461, sec 8.3]
1795 if (code == ND_REDIRECT_ROUTER)
1797 else if (is_newentry) /* (6-7) */
1800 case ND_ROUTER_SOLICIT:
1802 * is_router flag must always be cleared.
1806 case ND_ROUTER_ADVERT:
1808 * Mark an entry with lladdr as a router.
1810 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1811 (is_newentry && lladdr)) { /* (7) */
1818 * When the link-layer address of a router changes, select the
1819 * best router again. In particular, when the neighbor entry is newly
1820 * created, it might affect the selection policy.
1821 * Question: can we restrict the first condition to the "is_newentry"
1823 * XXX: when we hear an RA from a new router with the link-layer
1824 * address option, defrouter_select() is called twice, since
1825 * defrtrlist_update called the function as well. However, I believe
1826 * we can compromise the overhead, since it only happens the first
1828 * XXX: although defrouter_select() should not have a bad effect
1829 * for those are not autoconfigured hosts, we explicitly avoid such
1832 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1839 nd6_slowtimo(void *arg __unused)
1841 struct lwkt_msg *lmsg = &nd6_slowtimo_netmsg.lmsg;
1843 KASSERT(mycpuid == 0, ("not on cpu0"));
1845 if (lmsg->ms_flags & MSGF_DONE)
1846 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1851 nd6_slowtimo_dispatch(netmsg_t nmsg)
1853 const struct ifnet_array *arr;
1854 struct nd_ifinfo *nd6if;
1860 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1863 arr = ifnet_array_get();
1866 for (i = 0; i < arr->ifnet_count; ++i) {
1867 struct ifnet *ifp = arr->ifnet_arr[i];
1869 if (ifp->if_afdata[AF_INET6] == NULL)
1871 nd6if = ND_IFINFO(ifp);
1872 if (nd6if->basereachable && /* already initialized */
1873 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1875 * Since reachable time rarely changes by router
1876 * advertisements, we SHOULD insure that a new random
1877 * value gets recomputed at least once every few hours.
1880 nd6if->recalctm = nd6_recalc_reachtm_interval;
1881 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1884 mtx_unlock(&nd6_mtx);
1886 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1887 nd6_slowtimo, NULL);
1890 #define gotoerr(e) { error = (e); goto bad;}
1893 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1894 struct sockaddr_in6 *dst, struct rtentry *rt)
1896 struct llinfo_nd6 *ln = NULL;
1899 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1902 if (nd6_need_cache(ifp) == 0)
1906 * Next hop determination. This routine is derived from rt_llroute.
1909 if (!(rt->rt_flags & RTF_UP)) {
1910 rt = rtlookup((struct sockaddr *)dst);
1912 gotoerr(EHOSTUNREACH);
1914 if (rt->rt_ifp != ifp) {
1915 /* XXX: loop care? */
1916 return nd6_output(ifp, origifp, m, dst, rt);
1919 if (rt->rt_flags & RTF_GATEWAY) {
1920 struct sockaddr_in6 *gw6;
1923 * We skip link-layer address resolution and NUD
1924 * if the gateway is not a neighbor from ND point
1925 * of view, regardless of the value of nd_ifinfo.flags.
1926 * The second condition is a bit tricky; we skip
1927 * if the gateway is our own address, which is
1928 * sometimes used to install a route to a p2p link.
1930 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1931 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1932 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1934 * We allow this kind of tricky route only
1935 * when the outgoing interface is p2p.
1936 * XXX: we may need a more generic rule here.
1938 if (!(ifp->if_flags & IFF_POINTOPOINT))
1939 gotoerr(EHOSTUNREACH);
1944 if (rt->rt_gwroute == NULL) {
1945 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1946 if (rt->rt_gwroute == NULL)
1947 gotoerr(EHOSTUNREACH);
1948 } else if (!(rt->rt_gwroute->rt_flags & RTF_UP)) {
1949 rtfree(rt->rt_gwroute);
1950 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1951 if (rt->rt_gwroute == NULL)
1952 gotoerr(EHOSTUNREACH);
1954 rt = rt->rt_gwroute;
1959 * Address resolution or Neighbor Unreachability Detection
1961 * At this point, the destination of the packet must be a unicast
1962 * or an anycast address(i.e. not a multicast).
1965 /* Look up the neighbor cache for the nexthop */
1966 if (rt && (rt->rt_flags & RTF_LLINFO))
1967 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1970 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1971 * the condition below is not very efficient. But we believe
1972 * it is tolerable, because this should be a rare case.
1974 if (nd6_is_addr_neighbor(dst, ifp) &&
1975 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1976 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1979 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
1980 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1982 "nd6_output: can't allocate llinfo for %s "
1984 ip6_sprintf(&dst->sin6_addr), ln, rt);
1985 gotoerr(EIO); /* XXX: good error? */
1988 goto sendpkt; /* send anyway */
1991 /* We don't have to do link-layer address resolution on a p2p link. */
1992 if ((ifp->if_flags & IFF_POINTOPOINT) &&
1993 ln->ln_state < ND6_LLINFO_REACHABLE) {
1994 ln->ln_state = ND6_LLINFO_STALE;
1995 ln->ln_expire = time_uptime + nd6_gctimer;
1999 * The first time we send a packet to a neighbor whose entry is
2000 * STALE, we have to change the state to DELAY and a sets a timer to
2001 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2002 * neighbor unreachability detection on expiration.
2005 if (ln->ln_state == ND6_LLINFO_STALE) {
2007 ln->ln_state = ND6_LLINFO_DELAY;
2008 ln->ln_expire = time_uptime + nd6_delay;
2012 * If the neighbor cache entry has a state other than INCOMPLETE
2013 * (i.e. its link-layer address is already resolved), just
2016 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
2020 * There is a neighbor cache entry, but no ethernet address
2021 * response yet. Replace the held mbuf (if any) with this
2024 * This code conforms to the rate-limiting rule described in Section
2025 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
2028 if (ln->ln_state == ND6_LLINFO_NOSTATE) {
2030 * This neighbor cache entry was just created; change its
2031 * state to INCOMPLETE and start its life cycle.
2033 * We force an NS output below by setting ln_expire to 1
2034 * (nd6_rtrequest() could set it to the current time_uptime)
2035 * and zeroing out ln_asked (XXX this may not be necessary).
2037 ln->ln_state = ND6_LLINFO_INCOMPLETE;
2042 m_freem(ln->ln_hold);
2044 if (ln->ln_expire) {
2045 if (ln->ln_asked < nd6_mmaxtries &&
2046 ln->ln_expire < time_uptime) {
2048 ln->ln_expire = time_uptime +
2049 ND_IFINFO(ifp)->retrans / 1000;
2050 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
2056 if (ifp->if_flags & IFF_LOOPBACK)
2057 error = ifp->if_output(origifp, m, (struct sockaddr *)dst, rt);
2059 error = ifp->if_output(ifp, m, (struct sockaddr *)dst, rt);
2069 nd6_need_cache(struct ifnet *ifp)
2072 * XXX: we currently do not make neighbor cache on any interface
2073 * other than Ethernet and GIF.
2076 * - unidirectional tunnels needs no ND
2078 switch (ifp->if_type) {
2084 #ifdef IFT_IEEE80211
2090 case IFT_GIF: /* XXX need more cases? */
2098 nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
2099 struct sockaddr *dst, u_char *desten)
2101 struct sockaddr_dl *sdl;
2105 if (m->m_flags & M_MCAST) {
2106 switch (ifp->if_type) {
2111 #ifdef IFT_IEEE80211
2114 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2118 bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
2126 /* this could happen, if we could not allocate memory */
2130 if (rt_llroute(dst, rt0, &rt) != 0) {
2134 if (rt->rt_gateway->sa_family != AF_LINK) {
2135 kprintf("nd6_storelladdr: something odd happens\n");
2139 sdl = SDL(rt->rt_gateway);
2140 if (sdl->sdl_alen == 0) {
2141 /* this should be impossible, but we bark here for debugging */
2142 kprintf("nd6_storelladdr: sdl_alen == 0\n");
2147 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2151 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2152 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2154 SYSCTL_DECL(_net_inet6_icmp6);
2156 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2157 CTLFLAG_RD, nd6_sysctl_drlist, "List default routers");
2158 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2159 CTLFLAG_RD, nd6_sysctl_prlist, "List prefixes");
2162 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2166 struct in6_defrouter *d, *de;
2167 struct nd_defrouter *dr;
2173 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2174 dr = TAILQ_NEXT(dr, dr_entry)) {
2175 d = (struct in6_defrouter *)buf;
2176 de = (struct in6_defrouter *)(buf + sizeof(buf));
2179 bzero(d, sizeof(*d));
2180 d->rtaddr.sin6_family = AF_INET6;
2181 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2182 if (in6_recoverscope(&d->rtaddr, &dr->rtaddr,
2186 "default router list (%s)\n",
2187 ip6_sprintf(&dr->rtaddr));
2188 d->flags = dr->flags;
2189 d->rtlifetime = dr->rtlifetime;
2190 d->expire = dr->expire;
2191 d->if_index = dr->ifp->if_index;
2193 panic("buffer too short");
2195 error = SYSCTL_OUT(req, buf, sizeof(*d));
2203 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2207 struct in6_prefix *p, *pe;
2208 struct nd_prefix *pr;
2214 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2217 struct sockaddr_in6 *sin6, *s6;
2218 struct nd_pfxrouter *pfr;
2220 p = (struct in6_prefix *)buf;
2221 pe = (struct in6_prefix *)(buf + sizeof(buf));
2224 bzero(p, sizeof(*p));
2225 sin6 = (struct sockaddr_in6 *)(p + 1);
2227 p->prefix = pr->ndpr_prefix;
2228 if (in6_recoverscope(&p->prefix,
2229 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2231 "scope error in prefix list (%s)\n",
2232 ip6_sprintf(&p->prefix.sin6_addr));
2233 p->raflags = pr->ndpr_raf;
2234 p->prefixlen = pr->ndpr_plen;
2235 p->vltime = pr->ndpr_vltime;
2236 p->pltime = pr->ndpr_pltime;
2237 p->if_index = pr->ndpr_ifp->if_index;
2238 p->expire = pr->ndpr_expire;
2239 p->refcnt = pr->ndpr_refcnt;
2240 p->flags = pr->ndpr_stateflags;
2241 p->origin = PR_ORIG_RA;
2243 for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2244 pfr = pfr->pfr_next) {
2245 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2249 s6 = &sin6[advrtrs];
2250 bzero(s6, sizeof(*s6));
2251 s6->sin6_family = AF_INET6;
2252 s6->sin6_len = sizeof(*sin6);
2253 if (in6_recoverscope(s6, &pfr->router->rtaddr,
2254 pfr->router->ifp) != 0)
2257 "prefix list (%s)\n",
2258 ip6_sprintf(&pfr->router->rtaddr));
2261 p->advrtrs = advrtrs;
2263 panic("buffer too short");
2266 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2267 error = SYSCTL_OUT(req, buf, advance);