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
36 * BSD/OS version heavily modifies this code, related to llinfo.
37 * Since we don't have BSD/OS version of net/route.c in our hand,
38 * I left the code mostly as it was in 970310. -- itojun
42 #include "opt_inet6.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
52 #include <sys/kernel.h>
53 #include <sys/protosw.h>
54 #include <sys/errno.h>
55 #include <sys/syslog.h>
56 #include <sys/queue.h>
57 #include <sys/sysctl.h>
58 #include <sys/mutex.h>
60 #include <sys/thread2.h>
61 #include <sys/mutex2.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/route.h>
67 #include <net/netisr2.h>
68 #include <net/netmsg2.h>
70 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
72 #include <netinet6/in6_var.h>
73 #include <netinet/ip6.h>
74 #include <netinet6/ip6_var.h>
75 #include <netinet6/nd6.h>
76 #include <netinet/icmp6.h>
78 #include <net/net_osdep.h>
80 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
81 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
83 #define SIN6(s) ((struct sockaddr_in6 *)s)
84 #define SDL(s) ((struct sockaddr_dl *)s)
87 int nd6_prune = 1; /* walk list every 1 seconds */
88 int nd6_delay = 5; /* delay first probe time 5 second */
89 int nd6_umaxtries = 3; /* maximum unicast query */
90 int nd6_mmaxtries = 3; /* maximum multicast query */
91 int nd6_useloopback = 1; /* use loopback interface for local traffic */
92 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
94 /* preventing too many loops in ND option parsing */
95 int nd6_maxndopt = 10; /* max # of ND options allowed */
97 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
106 static int nd6_inuse, nd6_allocated;
108 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
109 struct nd_drhead nd_defrouter;
110 struct nd_prhead nd_prefix = { 0 };
111 struct mtx nd6_mtx = MTX_INITIALIZER("nd6");
113 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
114 static struct sockaddr_in6 all1_sa;
116 static void nd6_setmtu0 (struct ifnet *, struct nd_ifinfo *);
117 static int regen_tmpaddr (struct in6_ifaddr *);
118 static void nd6_slowtimo(void *);
119 static void nd6_slowtimo_dispatch(netmsg_t);
120 static void nd6_timer(void *);
121 static void nd6_timer_dispatch(netmsg_t);
123 static struct callout nd6_slowtimo_ch;
124 static struct netmsg_base nd6_slowtimo_netmsg;
126 static struct callout nd6_timer_ch;
127 static struct netmsg_base nd6_timer_netmsg;
132 static int nd6_init_done = 0;
136 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
140 all1_sa.sin6_family = AF_INET6;
141 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
142 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
143 all1_sa.sin6_addr.s6_addr[i] = 0xff;
145 /* initialization of the default router list */
146 TAILQ_INIT(&nd_defrouter);
151 callout_init_mp(&nd6_slowtimo_ch);
152 netmsg_init(&nd6_slowtimo_netmsg, NULL, &netisr_adone_rport,
153 MSGF_PRIORITY, nd6_slowtimo_dispatch);
154 callout_reset_bycpu(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
155 nd6_slowtimo, NULL, 0);
159 nd6_ifattach(struct ifnet *ifp)
161 struct nd_ifinfo *nd;
163 nd = (struct nd_ifinfo *)kmalloc(sizeof(*nd), M_IP6NDP,
168 nd->linkmtu = ifp->if_mtu;
169 nd->chlim = IPV6_DEFHLIM;
170 nd->basereachable = REACHABLE_TIME;
171 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
172 nd->retrans = RETRANS_TIMER;
176 * Note that the default value of ip6_accept_rtadv is 0, which means
177 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
180 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
182 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
183 nd6_setmtu0(ifp, nd);
188 nd6_ifdetach(struct nd_ifinfo *nd)
194 * Reset ND level link MTU. This function is called when the physical MTU
195 * changes, which means we might have to adjust the ND level MTU.
198 nd6_setmtu(struct ifnet *ifp)
200 nd6_setmtu0(ifp, ND_IFINFO(ifp));
203 struct netmsg_nd6setmtu {
204 struct netmsg_base nmsg;
206 struct nd_ifinfo *ndi;
209 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
211 nd6_setmtu0_dispatch(netmsg_t msg)
213 struct netmsg_nd6setmtu *nmsg = (struct netmsg_nd6setmtu *)msg;
214 struct ifnet *ifp = nmsg->ifp;
215 struct nd_ifinfo *ndi = nmsg->ndi;
219 oldmaxmtu = ndi->maxmtu;
220 oldlinkmtu = ndi->linkmtu;
222 switch (ifp->if_type) {
224 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
226 case IFT_IEEE1394: /* XXX should be IEEE1394MTU(1500) */
227 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
230 case IFT_IEEE80211: /* XXX should be IEEE80211MTU(1500) */
231 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
235 ndi->maxmtu = ifp->if_mtu;
239 if (oldmaxmtu != ndi->maxmtu) {
241 * If the ND level MTU is not set yet, or if the maxmtu
242 * is reset to a smaller value than the ND level MTU,
243 * also reset the ND level MTU.
245 if (ndi->linkmtu == 0 ||
246 ndi->maxmtu < ndi->linkmtu) {
247 ndi->linkmtu = ndi->maxmtu;
248 /* also adjust in6_maxmtu if necessary. */
249 if (oldlinkmtu == 0) {
251 * XXX: the case analysis is grotty, but
252 * it is not efficient to call in6_setmaxmtu()
253 * here when we are during the initialization
256 if (in6_maxmtu < ndi->linkmtu)
257 in6_maxmtu = ndi->linkmtu;
264 lwkt_replymsg(&nmsg->nmsg.lmsg, 0);
268 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
270 struct netmsg_nd6setmtu nmsg;
272 netmsg_init(&nmsg.nmsg, NULL, &curthread->td_msgport, 0,
273 nd6_setmtu0_dispatch);
276 lwkt_domsg(netisr_cpuport(0), &nmsg.nmsg.lmsg, 0);
280 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
282 bzero(ndopts, sizeof(*ndopts));
283 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
285 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
288 ndopts->nd_opts_done = 1;
289 ndopts->nd_opts_search = NULL;
294 * Take one ND option.
297 nd6_option(union nd_opts *ndopts)
299 struct nd_opt_hdr *nd_opt;
303 panic("ndopts == NULL in nd6_option");
304 if (!ndopts->nd_opts_last)
305 panic("uninitialized ndopts in nd6_option");
306 if (!ndopts->nd_opts_search)
308 if (ndopts->nd_opts_done)
311 nd_opt = ndopts->nd_opts_search;
313 /* make sure nd_opt_len is inside the buffer */
314 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
315 bzero(ndopts, sizeof(*ndopts));
319 olen = nd_opt->nd_opt_len << 3;
322 * Message validation requires that all included
323 * options have a length that is greater than zero.
325 bzero(ndopts, sizeof(*ndopts));
329 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
330 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
331 /* option overruns the end of buffer, invalid */
332 bzero(ndopts, sizeof(*ndopts));
334 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
335 /* reached the end of options chain */
336 ndopts->nd_opts_done = 1;
337 ndopts->nd_opts_search = NULL;
343 * Parse multiple ND options.
344 * This function is much easier to use, for ND routines that do not need
345 * multiple options of the same type.
348 nd6_options(union nd_opts *ndopts)
350 struct nd_opt_hdr *nd_opt;
354 panic("ndopts == NULL in nd6_options");
355 if (!ndopts->nd_opts_last)
356 panic("uninitialized ndopts in nd6_options");
357 if (!ndopts->nd_opts_search)
361 nd_opt = nd6_option(ndopts);
362 if (!nd_opt && !ndopts->nd_opts_last) {
364 * Message validation requires that all included
365 * options have a length that is greater than zero.
367 icmp6stat.icp6s_nd_badopt++;
368 bzero(ndopts, sizeof(*ndopts));
375 switch (nd_opt->nd_opt_type) {
376 case ND_OPT_SOURCE_LINKADDR:
377 case ND_OPT_TARGET_LINKADDR:
379 case ND_OPT_REDIRECTED_HEADER:
380 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
382 "duplicated ND6 option found (type=%d)\n",
383 nd_opt->nd_opt_type));
386 ndopts->nd_opt_array[nd_opt->nd_opt_type]
390 case ND_OPT_PREFIX_INFORMATION:
391 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
392 ndopts->nd_opt_array[nd_opt->nd_opt_type]
395 ndopts->nd_opts_pi_end =
396 (struct nd_opt_prefix_info *)nd_opt;
400 * Unknown options must be silently ignored,
401 * to accomodate future extension to the protocol.
404 "nd6_options: unsupported option %d - "
405 "option ignored\n", nd_opt->nd_opt_type));
410 if (i > nd6_maxndopt) {
411 icmp6stat.icp6s_nd_toomanyopt++;
412 nd6log((LOG_INFO, "too many loop in nd opt\n"));
416 if (ndopts->nd_opts_done)
424 * ND6 timer routine to expire default route list and prefix list
427 nd6_timer_dispatch(netmsg_t nmsg)
429 struct llinfo_nd6 *ln;
430 struct nd_defrouter *dr;
431 struct nd_prefix *pr;
433 struct in6_ifaddr *ia6, *nia6;
435 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
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 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
601 * If a temporary address has just become deprecated,
602 * regenerate a new one if possible.
604 if (ip6_use_tempaddr &&
605 (ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
606 !(oldflags & IN6_IFF_DEPRECATED)) {
608 if (regen_tmpaddr(ia6) == 0) {
610 * A new temporary address is
612 * XXX: this means the address chain
613 * has changed while we are still in
614 * the loop. Although the change
615 * would not cause disaster (because
616 * it's not a deletion, but an
617 * addition,) we'd rather restart the
618 * loop just for safety. Or does this
619 * significantly reduce performance??
626 * A new RA might have made a deprecated address
629 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
633 /* expire prefix list */
634 pr = nd_prefix.lh_first;
637 * check prefix lifetime.
638 * since pltime is just for autoconf, pltime processing for
639 * prefix is not necessary.
641 if (pr->ndpr_expire && pr->ndpr_expire < time_uptime) {
646 * address expiration and prefix expiration are
647 * separate. NEVER perform in6_purgeaddr here.
656 mtx_unlock(&nd6_mtx);
658 callout_reset(&nd6_timer_ch, nd6_prune * hz, nd6_timer, NULL);
662 nd6_timer(void *arg __unused)
664 struct lwkt_msg *lmsg = &nd6_timer_netmsg.lmsg;
666 KASSERT(mycpuid == 0, ("not on cpu0"));
668 if (lmsg->ms_flags & MSGF_DONE)
669 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
676 callout_init_mp(&nd6_timer_ch);
677 netmsg_init(&nd6_timer_netmsg, NULL, &netisr_adone_rport,
678 MSGF_PRIORITY, nd6_timer_dispatch);
679 callout_reset_bycpu(&nd6_timer_ch, hz, nd6_timer, NULL, 0);
683 regen_tmpaddr(struct in6_ifaddr *ia6) /* deprecated/invalidated temporary
686 struct ifaddr_container *ifac;
688 struct in6_ifaddr *public_ifa6 = NULL;
690 ifp = ia6->ia_ifa.ifa_ifp;
691 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
692 struct ifaddr *ifa = ifac->ifa;
693 struct in6_ifaddr *it6;
695 if (ifa->ifa_addr->sa_family != AF_INET6)
698 it6 = (struct in6_ifaddr *)ifa;
700 /* ignore no autoconf addresses. */
701 if (!(it6->ia6_flags & IN6_IFF_AUTOCONF))
704 /* ignore autoconf addresses with different prefixes. */
705 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
709 * Now we are looking at an autoconf address with the same
710 * prefix as ours. If the address is temporary and is still
711 * preferred, do not create another one. It would be rare, but
712 * could happen, for example, when we resume a laptop PC after
715 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) &&
716 !IFA6_IS_DEPRECATED(it6)) {
722 * This is a public autoconf address that has the same prefix
723 * as ours. If it is preferred, keep it. We can't break the
724 * loop here, because there may be a still-preferred temporary
725 * address with the prefix.
727 if (!IFA6_IS_DEPRECATED(it6))
731 if (public_ifa6 != NULL) {
734 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
735 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
736 " tmp addr,errno=%d\n", e);
746 * Nuke neighbor cache/prefix/default router management table, right before
750 nd6_purge(struct ifnet *ifp)
752 struct llinfo_nd6 *ln, *nln;
753 struct nd_defrouter *dr, *ndr, drany;
754 struct nd_prefix *pr, *npr;
756 /* Nuke default router list entries toward ifp */
757 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
759 * The first entry of the list may be stored in
760 * the routing table, so we'll delete it later.
762 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
763 ndr = TAILQ_NEXT(dr, dr_entry);
767 dr = TAILQ_FIRST(&nd_defrouter);
772 /* Nuke prefix list entries toward ifp */
773 for (pr = nd_prefix.lh_first; pr; pr = npr) {
775 if (pr->ndpr_ifp == ifp) {
777 * Previously, pr->ndpr_addr is removed as well,
778 * but I strongly believe we don't have to do it.
779 * nd6_purge() is only called from in6_ifdetach(),
780 * which removes all the associated interface addresses
782 * (jinmei@kame.net 20010129)
788 /* cancel default outgoing interface setting */
789 if (nd6_defifindex == ifp->if_index)
790 nd6_setdefaultiface(0);
792 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
793 /* refresh default router list */
794 bzero(&drany, sizeof(drany));
795 defrouter_delreq(&drany, 0);
800 * Nuke neighbor cache entries for the ifp.
801 * Note that rt->rt_ifp may not be the same as ifp,
802 * due to KAME goto ours hack. See RTM_RESOLVE case in
803 * nd6_rtrequest(), and ip6_input().
805 ln = llinfo_nd6.ln_next;
806 while (ln && ln != &llinfo_nd6) {
808 struct sockaddr_dl *sdl;
812 if (rt && rt->rt_gateway &&
813 rt->rt_gateway->sa_family == AF_LINK) {
814 sdl = (struct sockaddr_dl *)rt->rt_gateway;
815 if (sdl->sdl_index == ifp->if_index)
823 nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp)
826 struct sockaddr_in6 sin6;
828 bzero(&sin6, sizeof(sin6));
829 sin6.sin6_len = sizeof(struct sockaddr_in6);
830 sin6.sin6_family = AF_INET6;
831 sin6.sin6_addr = *addr6;
834 rt = rtlookup((struct sockaddr *)&sin6);
836 rt = rtpurelookup((struct sockaddr *)&sin6);
837 if (rt && !(rt->rt_flags & RTF_LLINFO)) {
839 * This is the case for the default route.
840 * If we want to create a neighbor cache for the address, we
841 * should free the route for the destination and allocate an
854 * If no route is available and create is set,
855 * we allocate a host route for the destination
856 * and treat it like an interface route.
857 * This hack is necessary for a neighbor which can't
858 * be covered by our own prefix.
861 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
866 * Create a new route. RTF_LLINFO is necessary
867 * to create a Neighbor Cache entry for the
868 * destination in nd6_rtrequest which will be
869 * called in rtrequest via ifa->ifa_rtrequest.
871 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
873 (struct sockaddr *)&all1_sa,
875 RTF_HOST | RTF_LLINFO) &
879 "nd6_lookup: failed to add route for a "
880 "neighbor(%s), errno=%d\n",
881 ip6_sprintf(addr6), e);
885 struct llinfo_nd6 *ln =
886 (struct llinfo_nd6 *)rt->rt_llinfo;
887 ln->ln_state = ND6_LLINFO_NOSTATE;
894 * Validation for the entry.
895 * Note that the check for rt_llinfo is necessary because a cloned
896 * route from a parent route that has the L flag (e.g. the default
897 * route to a p2p interface) may have the flag, too, while the
898 * destination is not actually a neighbor.
899 * XXX: we can't use rt->rt_ifp to check for the interface, since
900 * it might be the loopback interface if the entry is for our
901 * own address on a non-loopback interface. Instead, we should
902 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
905 if ((rt->rt_flags & RTF_GATEWAY) || !(rt->rt_flags & RTF_LLINFO) ||
906 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
907 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
909 log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n",
910 ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
911 /* xxx more logs... kazu */
919 * Detect if a given IPv6 address identifies a neighbor on a given link.
920 * XXX: should take care of the destination of a p2p link?
923 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
925 struct ifaddr_container *ifac;
928 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
929 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
932 * A link-local address is always a neighbor.
933 * XXX: we should use the sin6_scope_id field rather than the embedded
936 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
937 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
941 * If the address matches one of our addresses,
942 * it should be a neighbor.
944 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
945 struct ifaddr *ifa = ifac->ifa;
947 if (ifa->ifa_addr->sa_family != AF_INET6)
950 for (i = 0; i < 4; i++) {
951 if ((IFADDR6(ifa).s6_addr32[i] ^
952 addr->sin6_addr.s6_addr32[i]) &
953 IFMASK6(ifa).s6_addr32[i])
960 * Even if the address matches none of our addresses, it might be
961 * in the neighbor cache.
963 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
972 * Free an nd6 llinfo entry.
975 nd6_free(struct rtentry *rt)
977 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
978 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
979 struct nd_defrouter *dr;
982 * we used to have kpfctlinput(PRC_HOSTDEAD) here.
983 * even though it is not harmful, it was not really necessary.
986 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
988 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
991 if (ln->ln_router || dr) {
993 * rt6_flush must be called whether or not the neighbor
994 * is in the Default Router List.
995 * See a corresponding comment in nd6_na_input().
997 rt6_flush(&in6, rt->rt_ifp);
1002 * Unreachablity of a router might affect the default
1003 * router selection and on-link detection of advertised
1008 * Temporarily fake the state to choose a new default
1009 * router and to perform on-link determination of
1010 * prefixes correctly.
1011 * Below the state will be set correctly,
1012 * or the entry itself will be deleted.
1014 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1017 * Since defrouter_select() does not affect the
1018 * on-link determination and MIP6 needs the check
1019 * before the default router selection, we perform
1022 pfxlist_onlink_check();
1024 if (dr == TAILQ_FIRST(&nd_defrouter)) {
1026 * It is used as the current default router,
1027 * so we have to move it to the end of the
1028 * list and choose a new one.
1029 * XXX: it is not very efficient if this is
1032 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
1033 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
1038 mtx_unlock(&nd6_mtx);
1042 * Before deleting the entry, remember the next entry as the
1043 * return value. We need this because pfxlist_onlink_check() above
1044 * might have freed other entries (particularly the old next entry) as
1045 * a side effect (XXX).
1050 * Detach the route from the routing tree and the list of neighbor
1051 * caches, and disable the route entry not to be used in already
1054 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1060 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1062 * XXX cost-effective metods?
1065 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1067 struct llinfo_nd6 *ln;
1070 * If the caller specified "rt", use that. Otherwise, resolve the
1071 * routing table by supplied "dst6".
1076 if (!(rt = nd6_lookup(dst6, 0, NULL)))
1080 if ((rt->rt_flags & RTF_GATEWAY) ||
1081 !(rt->rt_flags & RTF_LLINFO) ||
1082 rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
1083 rt->rt_gateway->sa_family != AF_LINK) {
1084 /* This is not a host route. */
1088 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1089 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1093 * if we get upper-layer reachability confirmation many times,
1094 * it is possible we have false information.
1098 if (ln->ln_byhint > nd6_maxnudhint)
1102 ln->ln_state = ND6_LLINFO_REACHABLE;
1104 ln->ln_expire = time_uptime +
1105 ND_IFINFO(rt->rt_ifp)->reachable;
1109 nd6_rtrequest(int req, struct rtentry *rt)
1111 struct sockaddr *gate = rt->rt_gateway;
1112 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1113 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1114 struct ifnet *ifp = rt->rt_ifp;
1117 if ((rt->rt_flags & RTF_GATEWAY))
1120 if (nd6_need_cache(ifp) == 0 && !(rt->rt_flags & RTF_HOST)) {
1122 * This is probably an interface direct route for a link
1123 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1124 * We do not need special treatment below for such a route.
1125 * Moreover, the RTF_LLINFO flag which would be set below
1126 * would annoy the ndp(8) command.
1131 if (req == RTM_RESOLVE &&
1132 (nd6_need_cache(ifp) == 0 || /* stf case */
1133 !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp))) {
1135 * FreeBSD and BSD/OS often make a cloned host route based
1136 * on a less-specific route (e.g. the default route).
1137 * If the less specific route does not have a "gateway"
1138 * (this is the case when the route just goes to a p2p or an
1139 * stf interface), we'll mistakenly make a neighbor cache for
1140 * the host route, and will see strange neighbor solicitation
1141 * for the corresponding destination. In order to avoid the
1142 * confusion, we check if the destination of the route is
1143 * a neighbor in terms of neighbor discovery, and stop the
1144 * process if not. Additionally, we remove the LLINFO flag
1145 * so that ndp(8) will not try to get the neighbor information
1146 * of the destination.
1148 rt->rt_flags &= ~RTF_LLINFO;
1155 * There is no backward compatibility :)
1157 * if (!(rt->rt_flags & RTF_HOST) &&
1158 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1159 * rt->rt_flags |= RTF_CLONING;
1161 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1163 * Case 1: This route should come from
1164 * a route to interface. RTF_LLINFO flag is set
1165 * for a host route whose destination should be
1166 * treated as on-link.
1168 rt_setgate(rt, rt_key(rt),
1169 (struct sockaddr *)&null_sdl,
1171 gate = rt->rt_gateway;
1172 SDL(gate)->sdl_type = ifp->if_type;
1173 SDL(gate)->sdl_index = ifp->if_index;
1175 ln->ln_expire = time_uptime;
1177 if (ln && ln->ln_expire == 0) {
1178 /* kludge for desktops */
1180 kprintf("nd6_rtequest: time.tv_sec is zero; "
1186 if ((rt->rt_flags & RTF_CLONING))
1190 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1191 * We don't do that here since llinfo is not ready yet.
1193 * There are also couple of other things to be discussed:
1194 * - unsolicited NA code needs improvement beforehand
1195 * - RFC2461 says we MAY send multicast unsolicited NA
1196 * (7.2.6 paragraph 4), however, it also says that we
1197 * SHOULD provide a mechanism to prevent multicast NA storm.
1198 * we don't have anything like it right now.
1199 * note that the mechanism needs a mutual agreement
1200 * between proxies, which means that we need to implement
1201 * a new protocol, or a new kludge.
1202 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1203 * we need to check ip6forwarding before sending it.
1204 * (or should we allow proxy ND configuration only for
1205 * routers? there's no mention about proxy ND from hosts)
1208 /* XXX it does not work */
1209 if (rt->rt_flags & RTF_ANNOUNCE)
1211 &SIN6(rt_key(rt))->sin6_addr,
1212 &SIN6(rt_key(rt))->sin6_addr,
1213 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1218 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1220 * Address resolution isn't necessary for a point to
1221 * point link, so we can skip this test for a p2p link.
1223 if (gate->sa_family != AF_LINK ||
1224 gate->sa_len < sizeof(null_sdl)) {
1226 "nd6_rtrequest: bad gateway value: %s\n",
1230 SDL(gate)->sdl_type = ifp->if_type;
1231 SDL(gate)->sdl_index = ifp->if_index;
1234 break; /* This happens on a route change */
1236 * Case 2: This route may come from cloning, or a manual route
1237 * add with a LL address.
1239 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1240 rt->rt_llinfo = (caddr_t)ln;
1242 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1247 bzero(ln, sizeof(*ln));
1249 /* this is required for "ndp" command. - shin */
1250 if (req == RTM_ADD) {
1252 * gate should have some valid AF_LINK entry,
1253 * and ln->ln_expire should have some lifetime
1254 * which is specified by ndp command.
1256 ln->ln_state = ND6_LLINFO_REACHABLE;
1260 * When req == RTM_RESOLVE, rt is created and
1261 * initialized in rtrequest(), so rt_expire is 0.
1263 ln->ln_state = ND6_LLINFO_NOSTATE;
1264 ln->ln_expire = time_uptime;
1266 rt->rt_flags |= RTF_LLINFO;
1267 ln->ln_next = llinfo_nd6.ln_next;
1268 llinfo_nd6.ln_next = ln;
1269 ln->ln_prev = &llinfo_nd6;
1270 ln->ln_next->ln_prev = ln;
1273 * check if rt_key(rt) is one of my address assigned
1276 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1277 &SIN6(rt_key(rt))->sin6_addr);
1279 caddr_t macp = nd6_ifptomac(ifp);
1281 ln->ln_state = ND6_LLINFO_REACHABLE;
1284 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1285 SDL(gate)->sdl_alen = ifp->if_addrlen;
1287 if (nd6_useloopback) {
1288 rt->rt_ifp = &loif[0]; /* XXX */
1290 * Make sure rt_ifa be equal to the ifaddr
1291 * corresponding to the address.
1292 * We need this because when we refer
1293 * rt_ifa->ia6_flags in ip6_input, we assume
1294 * that the rt_ifa points to the address instead
1295 * of the loopback address.
1297 if (ifa != rt->rt_ifa) {
1298 IFAFREE(rt->rt_ifa);
1303 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1305 ln->ln_state = ND6_LLINFO_REACHABLE;
1308 /* join solicited node multicast for proxy ND */
1309 if (ifp->if_flags & IFF_MULTICAST) {
1310 struct in6_addr llsol;
1313 llsol = SIN6(rt_key(rt))->sin6_addr;
1314 llsol.s6_addr16[0] = htons(0xff02);
1315 llsol.s6_addr16[1] = htons(ifp->if_index);
1316 llsol.s6_addr32[1] = 0;
1317 llsol.s6_addr32[2] = htonl(1);
1318 llsol.s6_addr8[12] = 0xff;
1320 if (!in6_addmulti(&llsol, ifp, &error)) {
1321 nd6log((LOG_ERR, "%s: failed to join "
1322 "%s (errno=%d)\n", if_name(ifp),
1323 ip6_sprintf(&llsol), error));
1332 /* leave from solicited node multicast for proxy ND */
1333 if ((rt->rt_flags & RTF_ANNOUNCE) &&
1334 (ifp->if_flags & IFF_MULTICAST)) {
1335 struct in6_addr llsol;
1336 struct in6_multi *in6m;
1338 llsol = SIN6(rt_key(rt))->sin6_addr;
1339 llsol.s6_addr16[0] = htons(0xff02);
1340 llsol.s6_addr16[1] = htons(ifp->if_index);
1341 llsol.s6_addr32[1] = 0;
1342 llsol.s6_addr32[2] = htonl(1);
1343 llsol.s6_addr8[12] = 0xff;
1345 in6m = IN6_LOOKUP_MULTI(&llsol, ifp);
1350 ln->ln_next->ln_prev = ln->ln_prev;
1351 ln->ln_prev->ln_next = ln->ln_next;
1354 rt->rt_flags &= ~RTF_LLINFO;
1356 m_freem(ln->ln_hold);
1362 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1364 struct in6_drlist *drl = (struct in6_drlist *)data;
1365 struct in6_prlist *prl = (struct in6_prlist *)data;
1366 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1367 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1368 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1369 struct nd_defrouter *dr, any;
1370 struct nd_prefix *pr;
1372 int i = 0, error = 0;
1375 case SIOCGDRLST_IN6:
1377 * obsolete API, use sysctl under net.inet6.icmp6
1379 bzero(drl, sizeof(*drl));
1381 dr = TAILQ_FIRST(&nd_defrouter);
1382 while (dr && i < DRLSTSIZ) {
1383 drl->defrouter[i].rtaddr = dr->rtaddr;
1384 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1385 /* XXX: need to this hack for KAME stack */
1386 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1389 "default router list contains a "
1390 "non-linklocal address(%s)\n",
1391 ip6_sprintf(&drl->defrouter[i].rtaddr));
1393 drl->defrouter[i].flags = dr->flags;
1394 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1395 drl->defrouter[i].expire = dr->expire;
1396 drl->defrouter[i].if_index = dr->ifp->if_index;
1398 dr = TAILQ_NEXT(dr, dr_entry);
1400 mtx_unlock(&nd6_mtx);
1402 case SIOCGPRLST_IN6:
1404 * obsolete API, use sysctl under net.inet6.icmp6
1407 * XXX meaning of fields, especialy "raflags", is very
1408 * differnet between RA prefix list and RR/static prefix list.
1409 * how about separating ioctls into two?
1411 bzero(prl, sizeof(*prl));
1413 pr = nd_prefix.lh_first;
1414 while (pr && i < PRLSTSIZ) {
1415 struct nd_pfxrouter *pfr;
1418 in6_embedscope(&prl->prefix[i].prefix,
1419 &pr->ndpr_prefix, NULL, NULL);
1420 prl->prefix[i].raflags = pr->ndpr_raf;
1421 prl->prefix[i].prefixlen = pr->ndpr_plen;
1422 prl->prefix[i].vltime = pr->ndpr_vltime;
1423 prl->prefix[i].pltime = pr->ndpr_pltime;
1424 prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1425 prl->prefix[i].expire = pr->ndpr_expire;
1427 pfr = pr->ndpr_advrtrs.lh_first;
1431 #define RTRADDR prl->prefix[i].advrtr[j]
1432 RTRADDR = pfr->router->rtaddr;
1433 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1434 /* XXX: hack for KAME */
1435 RTRADDR.s6_addr16[1] = 0;
1438 "a router(%s) advertises "
1440 "non-link local address\n",
1441 ip6_sprintf(&RTRADDR));
1445 pfr = pfr->pfr_next;
1447 prl->prefix[i].advrtrs = j;
1448 prl->prefix[i].origin = PR_ORIG_RA;
1453 mtx_unlock(&nd6_mtx);
1456 case OSIOCGIFINFO_IN6:
1457 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1458 bzero(&ndi->ndi, sizeof(ndi->ndi));
1459 ndi->ndi.linkmtu = ND_IFINFO(ifp)->linkmtu;
1460 ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1461 ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1462 ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1463 ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1464 ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1465 ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1466 ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1467 ndi->ndi.receivedra = ND_IFINFO(ifp)->receivedra;
1469 case SIOCGIFINFO_IN6:
1470 ndi->ndi = *ND_IFINFO(ifp);
1472 case SIOCSIFINFO_FLAGS:
1473 ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1475 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1476 /* flush default router list */
1478 * xxx sumikawa: should not delete route if default
1479 * route equals to the top of default router list
1481 bzero(&any, sizeof(any));
1482 defrouter_delreq(&any, 0);
1484 /* xxx sumikawa: flush prefix list */
1486 case SIOCSPFXFLUSH_IN6:
1488 /* flush all the prefix advertised by routers */
1489 struct nd_prefix *pr, *next;
1492 for (pr = nd_prefix.lh_first; pr; pr = next) {
1493 struct in6_ifaddr *ia, *ia_next;
1495 next = pr->ndpr_next;
1497 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1500 /* do we really have to remove addresses as well? */
1501 for (ia = in6_ifaddr; ia; ia = ia_next) {
1502 /* ia might be removed. keep the next ptr. */
1503 ia_next = ia->ia_next;
1505 if (!(ia->ia6_flags & IN6_IFF_AUTOCONF))
1508 if (ia->ia6_ndpr == pr)
1509 in6_purgeaddr(&ia->ia_ifa);
1513 mtx_unlock(&nd6_mtx);
1516 case SIOCSRTRFLUSH_IN6:
1518 /* flush all the default routers */
1519 struct nd_defrouter *dr, *next;
1522 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1524 * The first entry of the list may be stored in
1525 * the routing table, so we'll delete it later.
1527 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1528 next = TAILQ_NEXT(dr, dr_entry);
1531 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1533 mtx_unlock(&nd6_mtx);
1536 case SIOCGNBRINFO_IN6:
1538 struct llinfo_nd6 *ln;
1539 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1542 * XXX: KAME specific hack for scoped addresses
1543 * XXXX: for other scopes than link-local?
1545 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1546 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1547 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1550 *idp = htons(ifp->if_index);
1554 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1556 mtx_unlock(&nd6_mtx);
1559 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1560 nbi->state = ln->ln_state;
1561 nbi->asked = ln->ln_asked;
1562 nbi->isrouter = ln->ln_router;
1563 nbi->expire = ln->ln_expire;
1564 mtx_unlock(&nd6_mtx);
1568 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1569 ndif->ifindex = nd6_defifindex;
1571 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1572 return (nd6_setdefaultiface(ndif->ifindex));
1579 * Create neighbor cache entry and cache link-layer address,
1580 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1583 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1585 int type, /* ICMP6 type */
1586 int code /* type dependent information */)
1588 struct rtentry *rt = NULL;
1589 struct llinfo_nd6 *ln = NULL;
1591 struct sockaddr_dl *sdl = NULL;
1598 panic("ifp == NULL in nd6_cache_lladdr");
1600 panic("from == NULL in nd6_cache_lladdr");
1602 /* nothing must be updated for unspecified address */
1603 if (IN6_IS_ADDR_UNSPECIFIED(from))
1607 * Validation about ifp->if_addrlen and lladdrlen must be done in
1610 * XXX If the link does not have link-layer adderss, what should
1611 * we do? (ifp->if_addrlen == 0)
1612 * Spec says nothing in sections for RA, RS and NA. There's small
1613 * description on it in NS section (RFC 2461 7.2.3).
1616 rt = nd6_lookup(from, 0, ifp);
1619 /* nothing must be done if there's no lladdr */
1620 if (!lladdr || !lladdrlen)
1624 rt = nd6_lookup(from, 1, ifp);
1627 /* do nothing if static ndp is set */
1628 if (rt->rt_flags & RTF_STATIC)
1635 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1640 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1643 if (!rt->rt_gateway)
1645 if (rt->rt_gateway->sa_family != AF_LINK)
1647 sdl = SDL(rt->rt_gateway);
1649 olladdr = (sdl->sdl_alen) ? 1 : 0;
1650 if (olladdr && lladdr) {
1651 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1659 * newentry olladdr lladdr llchange (*=record)
1662 * 0 n y -- (3) * STALE
1664 * 0 y y y (5) * STALE
1665 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1666 * 1 -- y -- (7) * STALE
1669 if (lladdr) { /* (3-5) and (7) */
1671 * Record source link-layer address
1672 * XXX is it dependent to ifp->if_type?
1674 sdl->sdl_alen = ifp->if_addrlen;
1675 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1679 if ((!olladdr && lladdr) /* (3) */
1680 || (olladdr && lladdr && llchange)) { /* (5) */
1682 newstate = ND6_LLINFO_STALE;
1683 } else /* (1-2,4) */
1687 if (!lladdr) /* (6) */
1688 newstate = ND6_LLINFO_NOSTATE;
1690 newstate = ND6_LLINFO_STALE;
1695 * Update the state of the neighbor cache.
1697 ln->ln_state = newstate;
1699 if (ln->ln_state == ND6_LLINFO_STALE) {
1701 * XXX: since nd6_output() below will cause
1702 * state tansition to DELAY and reset the timer,
1703 * we must set the timer now, although it is actually
1706 ln->ln_expire = time_uptime + nd6_gctimer;
1710 * we assume ifp is not a p2p here, so just
1711 * set the 2nd argument as the 1st one.
1713 nd6_output(ifp, ifp, ln->ln_hold,
1714 (struct sockaddr_in6 *)rt_key(rt),
1718 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1719 /* probe right away */
1720 ln->ln_expire = time_uptime;
1725 * ICMP6 type dependent behavior.
1727 * NS: clear IsRouter if new entry
1728 * RS: clear IsRouter
1729 * RA: set IsRouter if there's lladdr
1730 * redir: clear IsRouter if new entry
1733 * The spec says that we must set IsRouter in the following cases:
1734 * - If lladdr exist, set IsRouter. This means (1-5).
1735 * - If it is old entry (!newentry), set IsRouter. This means (7).
1736 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1737 * A quetion arises for (1) case. (1) case has no lladdr in the
1738 * neighbor cache, this is similar to (6).
1739 * This case is rare but we figured that we MUST NOT set IsRouter.
1741 * newentry olladdr lladdr llchange NS RS RA redir
1743 * 0 n n -- (1) c ? s
1744 * 0 y n -- (2) c s s
1745 * 0 n y -- (3) c s s
1748 * 1 -- n -- (6) c c c s
1749 * 1 -- y -- (7) c c s c s
1753 switch (type & 0xff) {
1754 case ND_NEIGHBOR_SOLICIT:
1756 * New entry must have is_router flag cleared.
1758 if (is_newentry) /* (6-7) */
1763 * If the icmp is a redirect to a better router, always set the
1764 * is_router flag. Otherwise, if the entry is newly created,
1765 * clear the flag. [RFC 2461, sec 8.3]
1767 if (code == ND_REDIRECT_ROUTER)
1769 else if (is_newentry) /* (6-7) */
1772 case ND_ROUTER_SOLICIT:
1774 * is_router flag must always be cleared.
1778 case ND_ROUTER_ADVERT:
1780 * Mark an entry with lladdr as a router.
1782 if ((!is_newentry && (olladdr || lladdr)) /* (2-5) */
1783 || (is_newentry && lladdr)) { /* (7) */
1790 * When the link-layer address of a router changes, select the
1791 * best router again. In particular, when the neighbor entry is newly
1792 * created, it might affect the selection policy.
1793 * Question: can we restrict the first condition to the "is_newentry"
1795 * XXX: when we hear an RA from a new router with the link-layer
1796 * address option, defrouter_select() is called twice, since
1797 * defrtrlist_update called the function as well. However, I believe
1798 * we can compromise the overhead, since it only happens the first
1800 * XXX: although defrouter_select() should not have a bad effect
1801 * for those are not autoconfigured hosts, we explicitly avoid such
1804 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1811 nd6_slowtimo(void *arg __unused)
1813 struct lwkt_msg *lmsg = &nd6_slowtimo_netmsg.lmsg;
1815 KASSERT(mycpuid == 0, ("not on cpu0"));
1817 if (lmsg->ms_flags & MSGF_DONE)
1818 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1823 nd6_slowtimo_dispatch(netmsg_t nmsg)
1825 const struct ifnet_array *arr;
1826 struct nd_ifinfo *nd6if;
1829 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1830 ("not in netisr0"));
1833 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1836 arr = ifnet_array_get();
1839 for (i = 0; i < arr->ifnet_count; ++i) {
1840 struct ifnet *ifp = arr->ifnet_arr[i];
1842 if (ifp->if_afdata[AF_INET6] == NULL)
1844 nd6if = ND_IFINFO(ifp);
1845 if (nd6if->basereachable && /* already initialized */
1846 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1848 * Since reachable time rarely changes by router
1849 * advertisements, we SHOULD insure that a new random
1850 * value gets recomputed at least once every few hours.
1853 nd6if->recalctm = nd6_recalc_reachtm_interval;
1854 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1857 mtx_unlock(&nd6_mtx);
1859 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1860 nd6_slowtimo, NULL);
1863 #define gotoerr(e) { error = (e); goto bad;}
1866 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1867 struct sockaddr_in6 *dst, struct rtentry *rt)
1869 struct llinfo_nd6 *ln = NULL;
1872 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1875 if (nd6_need_cache(ifp) == 0)
1879 * next hop determination. This routine is derived from ether_outpout.
1882 if (!(rt->rt_flags & RTF_UP)) {
1883 rt = rtlookup((struct sockaddr *)dst);
1885 gotoerr(EHOSTUNREACH);
1887 if (rt->rt_ifp != ifp) {
1888 /* XXX: loop care? */
1889 return nd6_output(ifp, origifp, m, dst, rt);
1892 if (rt->rt_flags & RTF_GATEWAY) {
1893 struct sockaddr_in6 *gw6;
1896 * We skip link-layer address resolution and NUD
1897 * if the gateway is not a neighbor from ND point
1898 * of view, regardless of the value of nd_ifinfo.flags.
1899 * The second condition is a bit tricky; we skip
1900 * if the gateway is our own address, which is
1901 * sometimes used to install a route to a p2p link.
1903 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1904 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1905 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1907 * We allow this kind of tricky route only
1908 * when the outgoing interface is p2p.
1909 * XXX: we may need a more generic rule here.
1911 if (!(ifp->if_flags & IFF_POINTOPOINT))
1912 gotoerr(EHOSTUNREACH);
1917 if (rt->rt_gwroute == NULL) {
1918 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1919 if (rt->rt_gwroute == NULL)
1920 gotoerr(EHOSTUNREACH);
1921 } else if (!(rt->rt_gwroute->rt_flags & RTF_UP)) {
1922 rtfree(rt->rt_gwroute);
1923 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1924 if (rt->rt_gwroute == NULL)
1925 gotoerr(EHOSTUNREACH);
1931 * Address resolution or Neighbor Unreachability Detection
1933 * At this point, the destination of the packet must be a unicast
1934 * or an anycast address(i.e. not a multicast).
1937 /* Look up the neighbor cache for the nexthop */
1938 if (rt && (rt->rt_flags & RTF_LLINFO))
1939 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1942 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1943 * the condition below is not very efficient. But we believe
1944 * it is tolerable, because this should be a rare case.
1946 if (nd6_is_addr_neighbor(dst, ifp) &&
1947 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1948 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1951 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
1952 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1954 "nd6_output: can't allocate llinfo for %s "
1956 ip6_sprintf(&dst->sin6_addr), ln, rt);
1957 gotoerr(EIO); /* XXX: good error? */
1960 goto sendpkt; /* send anyway */
1963 /* We don't have to do link-layer address resolution on a p2p link. */
1964 if ((ifp->if_flags & IFF_POINTOPOINT) &&
1965 ln->ln_state < ND6_LLINFO_REACHABLE) {
1966 ln->ln_state = ND6_LLINFO_STALE;
1967 ln->ln_expire = time_uptime + nd6_gctimer;
1971 * The first time we send a packet to a neighbor whose entry is
1972 * STALE, we have to change the state to DELAY and a sets a timer to
1973 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1974 * neighbor unreachability detection on expiration.
1977 if (ln->ln_state == ND6_LLINFO_STALE) {
1979 ln->ln_state = ND6_LLINFO_DELAY;
1980 ln->ln_expire = time_uptime + nd6_delay;
1984 * If the neighbor cache entry has a state other than INCOMPLETE
1985 * (i.e. its link-layer address is already resolved), just
1988 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1992 * There is a neighbor cache entry, but no ethernet address
1993 * response yet. Replace the held mbuf (if any) with this
1996 * This code conforms to the rate-limiting rule described in Section
1997 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
2000 if (ln->ln_state == ND6_LLINFO_NOSTATE)
2001 ln->ln_state = ND6_LLINFO_INCOMPLETE;
2003 m_freem(ln->ln_hold);
2005 if (ln->ln_expire) {
2006 if (ln->ln_asked < nd6_mmaxtries &&
2007 ln->ln_expire < time_uptime) {
2009 ln->ln_expire = time_uptime +
2010 ND_IFINFO(ifp)->retrans / 1000;
2011 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
2017 if (ifp->if_flags & IFF_LOOPBACK)
2018 error = ifp->if_output(origifp, m, (struct sockaddr *)dst, rt);
2020 error = ifp->if_output(ifp, m, (struct sockaddr *)dst, rt);
2030 nd6_need_cache(struct ifnet *ifp)
2033 * XXX: we currently do not make neighbor cache on any interface
2034 * other than Ethernet and GIF.
2037 * - unidirectional tunnels needs no ND
2039 switch (ifp->if_type) {
2045 #ifdef IFT_IEEE80211
2051 case IFT_GIF: /* XXX need more cases? */
2059 nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
2060 struct sockaddr *dst, u_char *desten)
2062 struct sockaddr_dl *sdl;
2066 if (m->m_flags & M_MCAST) {
2067 switch (ifp->if_type) {
2072 #ifdef IFT_IEEE80211
2075 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2079 bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
2087 /* this could happen, if we could not allocate memory */
2091 if (rt_llroute(dst, rt0, &rt) != 0) {
2095 if (rt->rt_gateway->sa_family != AF_LINK) {
2096 kprintf("nd6_storelladdr: something odd happens\n");
2100 sdl = SDL(rt->rt_gateway);
2101 if (sdl->sdl_alen == 0) {
2102 /* this should be impossible, but we bark here for debugging */
2103 kprintf("nd6_storelladdr: sdl_alen == 0\n");
2108 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2112 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2113 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2115 SYSCTL_DECL(_net_inet6_icmp6);
2117 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2118 CTLFLAG_RD, nd6_sysctl_drlist, "List default routers");
2119 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2120 CTLFLAG_RD, nd6_sysctl_prlist, "List prefixes");
2123 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2127 struct in6_defrouter *d, *de;
2128 struct nd_defrouter *dr;
2134 for (dr = TAILQ_FIRST(&nd_defrouter);
2136 dr = TAILQ_NEXT(dr, dr_entry)) {
2137 d = (struct in6_defrouter *)buf;
2138 de = (struct in6_defrouter *)(buf + sizeof(buf));
2141 bzero(d, sizeof(*d));
2142 d->rtaddr.sin6_family = AF_INET6;
2143 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2144 if (in6_recoverscope(&d->rtaddr, &dr->rtaddr,
2148 "default router list (%s)\n",
2149 ip6_sprintf(&dr->rtaddr));
2150 d->flags = dr->flags;
2151 d->rtlifetime = dr->rtlifetime;
2152 d->expire = dr->expire;
2153 d->if_index = dr->ifp->if_index;
2155 panic("buffer too short");
2157 error = SYSCTL_OUT(req, buf, sizeof(*d));
2165 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2169 struct in6_prefix *p, *pe;
2170 struct nd_prefix *pr;
2176 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2179 struct sockaddr_in6 *sin6, *s6;
2180 struct nd_pfxrouter *pfr;
2182 p = (struct in6_prefix *)buf;
2183 pe = (struct in6_prefix *)(buf + sizeof(buf));
2186 bzero(p, sizeof(*p));
2187 sin6 = (struct sockaddr_in6 *)(p + 1);
2189 p->prefix = pr->ndpr_prefix;
2190 if (in6_recoverscope(&p->prefix,
2191 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2193 "scope error in prefix list (%s)\n",
2194 ip6_sprintf(&p->prefix.sin6_addr));
2195 p->raflags = pr->ndpr_raf;
2196 p->prefixlen = pr->ndpr_plen;
2197 p->vltime = pr->ndpr_vltime;
2198 p->pltime = pr->ndpr_pltime;
2199 p->if_index = pr->ndpr_ifp->if_index;
2200 p->expire = pr->ndpr_expire;
2201 p->refcnt = pr->ndpr_refcnt;
2202 p->flags = pr->ndpr_stateflags;
2203 p->origin = PR_ORIG_RA;
2205 for (pfr = pr->ndpr_advrtrs.lh_first;
2207 pfr = pfr->pfr_next) {
2208 if ((void *)&sin6[advrtrs + 1] >
2213 s6 = &sin6[advrtrs];
2214 bzero(s6, sizeof(*s6));
2215 s6->sin6_family = AF_INET6;
2216 s6->sin6_len = sizeof(*sin6);
2217 if (in6_recoverscope(s6, &pfr->router->rtaddr,
2218 pfr->router->ifp) != 0)
2221 "prefix list (%s)\n",
2222 ip6_sprintf(&pfr->router->rtaddr));
2225 p->advrtrs = advrtrs;
2227 panic("buffer too short");
2229 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2230 error = SYSCTL_OUT(req, buf, advance);