1 //==========================================================================
3 // src/sys/netinet6/nd6.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
22 /* $KAME: nd6.c,v 1.221 2001/12/18 02:23:45 itojun Exp $ */
25 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the project nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * BSD/OS version heavily modifies this code, related to llinfo.
57 * Since we don't have BSD/OS version of net/route.c in our hand,
58 * I left the code mostly as it was in 970310. -- itojun
61 #include <sys/param.h>
62 #include <sys/malloc.h>
64 #include <sys/socket.h>
65 #include <sys/sockio.h>
66 #include <sys/protosw.h>
67 #include <sys/errno.h>
68 #include <sys/queue.h>
69 #include <sys/sysctl.h>
72 #include <net/if_dl.h>
73 #include <net/if_types.h>
74 #include <net/route.h>
76 #include <netinet/in.h>
78 #include <netinet/if_ether.h>
80 #include <net/if_fddi.h>
83 #include <netinet/ip_ipsp.h>
85 #else /* __NetBSD__ */
86 #include <net/if_ether.h>
87 #include <netinet/if_inarp.h>
88 #include <net/if_fddi.h>
89 #endif /* __NetBSD__ */
90 #include <netinet6/in6_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet6/nd6.h>
94 #include <netinet/icmp6.h>
96 #if defined(__NetBSD__)
97 extern struct ifnet loif[NLOOP];
100 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
101 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
103 #define SIN6(s) ((struct sockaddr_in6 *)s)
104 #define SDL(s) ((struct sockaddr_dl *)s)
107 int nd6_prune = 1; /* walk list every 1 seconds */
108 int nd6_delay = 5; /* delay first probe time 5 second */
109 int nd6_umaxtries = 3; /* maximum unicast query */
110 int nd6_mmaxtries = 3; /* maximum multicast query */
111 int nd6_useloopback = 1; /* use loopback interface for local traffic */
112 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
114 /* preventing too many loops in ND option parsing */
115 int nd6_maxndopt = 10; /* max # of ND options allowed */
117 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
122 static int nd6_inuse, nd6_allocated;
124 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
125 static size_t nd_ifinfo_indexlim = 8;
126 struct nd_ifinfo *nd_ifinfo = NULL;
127 struct nd_drhead nd_defrouter;
128 struct nd_prhead nd_prefix = { 0 };
130 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
131 static struct sockaddr_in6 all1_sa;
133 static void nd6_slowtimo __P((void *));
134 static int regen_tmpaddr __P((struct in6_ifaddr *));
135 static struct llinfo_nd6 *nd6_free __P((struct rtentry *, int));
138 struct callout nd6_slowtimo_ch = CALLOUT_INITIALIZER;
139 struct callout nd6_timer_ch = CALLOUT_INITIALIZER;
140 extern struct callout in6_tmpaddrtimer_ch;
141 #elif (defined(__FreeBSD__) && __FreeBSD__ >= 3)
142 struct callout nd6_slowtimo_ch;
143 struct callout nd6_timer_ch;
144 extern struct callout in6_tmpaddrtimer_ch;
145 #elif defined(__OpenBSD__)
146 struct timeout nd6_slowtimo_ch;
147 struct timeout nd6_timer_ch;
148 extern struct timeout in6_tmpaddrtimer_ch;
154 static int nd6_init_done = 0;
158 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
162 all1_sa.sin6_family = AF_INET6;
163 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
164 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
165 all1_sa.sin6_addr.s6_addr[i] = 0xff;
167 /* initialization of the default router list */
168 TAILQ_INIT(&nd_defrouter);
173 #if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
174 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
176 #elif defined(__OpenBSD__)
177 timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
178 timeout_add(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz);
180 timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
190 * We have some arrays that should be indexed by if_index.
191 * since if_index will grow dynamically, they should grow too.
193 if (nd_ifinfo == NULL || if_index >= nd_ifinfo_indexlim) {
197 while (if_index >= nd_ifinfo_indexlim)
198 nd_ifinfo_indexlim <<= 1;
201 n = nd_ifinfo_indexlim * sizeof(struct nd_ifinfo);
202 q = (caddr_t)malloc(n, M_IP6NDP, M_WAITOK);
205 bcopy((caddr_t)nd_ifinfo, q, n/2);
206 free((caddr_t)nd_ifinfo, M_IP6NDP);
208 nd_ifinfo = (struct nd_ifinfo *)q;
211 #define ND nd_ifinfo[ifp->if_index]
214 * Don't initialize if called twice.
215 * XXX: to detect this, we should choose a member that is never set
216 * before initialization of the ND structure itself. We formaly used
217 * the linkmtu member, which was not suitable because it could be
218 * initialized via "ifconfig mtu".
220 if (ND.basereachable)
224 #if defined(__FreeBSD__) && __FreeBSD__ >= 5
225 if (!ifnet_byindex(ifp->if_index))
226 panic("nd6_ifattach: ifnet_byindex is NULL");
228 if (!ifindex2ifnet[ifp->if_index])
229 panic("nd6_ifattach: ifindex2ifnet is NULL");
232 #if defined(__FreeBSD__) && __FreeBSD__ >= 5
233 ND.linkmtu = ifnet_byindex(ifp->if_index)->if_mtu;
235 ND.linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu;
237 ND.chlim = IPV6_DEFHLIM;
238 ND.basereachable = REACHABLE_TIME;
239 ND.reachable = ND_COMPUTE_RTIME(ND.basereachable);
240 ND.retrans = RETRANS_TIMER;
243 * Note that the default value of ip6_accept_rtadv is 0, which means
244 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
247 ND.flags = ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV;
253 * Reset ND level link MTU. This function is called when the physical MTU
254 * changes, which means we might have to adjust the ND level MTU.
261 #define MIN(a,b) ((a) < (b) ? (a) : (b))
263 struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
264 u_long oldmaxmtu = ndi->maxmtu;
265 u_long oldlinkmtu = ndi->linkmtu;
267 switch (ifp->if_type) {
268 case IFT_ARCNET: /* XXX MTU handling needs more work */
269 ndi->maxmtu = MIN(60480, ifp->if_mtu);
272 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
274 #if defined(__FreeBSD__) || defined(__bsdi__)
277 #if defined(__bsdi__) && _BSDI_VERSION >= 199802
278 ndi->maxmtu = MIN(FDDIMTU, ifp->if_mtu);
280 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
285 #if !(defined(__bsdi__) && _BSDI_VERSION >= 199802)
288 ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu);
292 case IFT_IEEE1394: /* XXX should be IEEE1394MTU(1500) */
293 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
296 case IFT_IEEE80211: /* XXX should be IEEE80211MTU(1500) */
297 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
301 ndi->maxmtu = ifp->if_mtu;
305 if (oldmaxmtu != ndi->maxmtu) {
307 * If the ND level MTU is not set yet, or if the maxmtu
308 * is reset to a smaller value than the ND level MTU,
309 * also reset the ND level MTU.
311 if (ndi->linkmtu == 0 ||
312 ndi->maxmtu < ndi->linkmtu) {
313 ndi->linkmtu = ndi->maxmtu;
314 /* also adjust in6_maxmtu if necessary. */
315 if (oldlinkmtu == 0) {
317 * XXX: the case analysis is grotty, but
318 * it is not efficient to call in6_setmaxmtu()
319 * here when we are during the initialization
322 if (in6_maxmtu < ndi->linkmtu)
323 in6_maxmtu = ndi->linkmtu;
332 nd6_option_init(opt, icmp6len, ndopts)
335 union nd_opts *ndopts;
337 bzero(ndopts, sizeof(*ndopts));
338 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
340 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
343 ndopts->nd_opts_done = 1;
344 ndopts->nd_opts_search = NULL;
349 * Take one ND option.
353 union nd_opts *ndopts;
355 struct nd_opt_hdr *nd_opt;
359 panic("ndopts == NULL in nd6_option\n");
360 if (!ndopts->nd_opts_last)
361 panic("uninitialized ndopts in nd6_option\n");
362 if (!ndopts->nd_opts_search)
364 if (ndopts->nd_opts_done)
367 nd_opt = ndopts->nd_opts_search;
369 /* make sure nd_opt_len is inside the buffer */
370 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
371 bzero(ndopts, sizeof(*ndopts));
375 olen = nd_opt->nd_opt_len << 3;
378 * Message validation requires that all included
379 * options have a length that is greater than zero.
381 bzero(ndopts, sizeof(*ndopts));
385 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
386 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
387 /* option overruns the end of buffer, invalid */
388 bzero(ndopts, sizeof(*ndopts));
390 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
391 /* reached the end of options chain */
392 ndopts->nd_opts_done = 1;
393 ndopts->nd_opts_search = NULL;
399 * Parse multiple ND options.
400 * This function is much easier to use, for ND routines that do not need
401 * multiple options of the same type.
405 union nd_opts *ndopts;
407 struct nd_opt_hdr *nd_opt;
411 panic("ndopts == NULL in nd6_options\n");
412 if (!ndopts->nd_opts_last)
413 panic("uninitialized ndopts in nd6_options\n");
414 if (!ndopts->nd_opts_search)
418 nd_opt = nd6_option(ndopts);
419 if (!nd_opt && !ndopts->nd_opts_last) {
421 * Message validation requires that all included
422 * options have a length that is greater than zero.
424 icmp6stat.icp6s_nd_badopt++;
425 bzero(ndopts, sizeof(*ndopts));
432 switch (nd_opt->nd_opt_type) {
433 case ND_OPT_SOURCE_LINKADDR:
434 case ND_OPT_TARGET_LINKADDR:
436 case ND_OPT_REDIRECTED_HEADER:
437 case ND_OPT_ADVINTERVAL:
438 case ND_OPT_SOURCE_ADDRLIST:
439 case ND_OPT_TARGET_ADDRLIST:
440 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
442 "duplicated ND6 option found (type=%d)\n",
443 nd_opt->nd_opt_type));
446 ndopts->nd_opt_array[nd_opt->nd_opt_type]
450 case ND_OPT_PREFIX_INFORMATION:
451 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
452 ndopts->nd_opt_array[nd_opt->nd_opt_type]
455 ndopts->nd_opts_pi_end =
456 (struct nd_opt_prefix_info *)nd_opt;
458 case ND_OPT_HOMEAGENT_INFO:
462 * Unknown options must be silently ignored,
463 * to accomodate future extension to the protocol.
466 "nd6_options: unsupported option %d - "
467 "option ignored\n", nd_opt->nd_opt_type));
472 if (i > nd6_maxndopt) {
473 icmp6stat.icp6s_nd_toomanyopt++;
474 nd6log((LOG_INFO, "too many loop in nd opt\n"));
478 if (ndopts->nd_opts_done)
486 * ND6 timer routine to expire default route list and prefix list
489 nd6_timer(ignored_arg)
493 struct llinfo_nd6 *ln;
494 struct nd_defrouter *dr;
495 struct nd_prefix *pr;
496 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
497 long time_second = time.tv_sec;
500 struct in6_ifaddr *ia6, *nia6;
501 struct in6_addrlifetime *lt6;
508 #if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
509 callout_reset(&nd6_timer_ch, nd6_prune * hz,
511 #elif defined(__OpenBSD__)
512 timeout_set(&nd6_timer_ch, nd6_timer, NULL);
513 timeout_add(&nd6_timer_ch, nd6_prune * hz);
515 timeout(nd6_timer, (caddr_t)0, nd6_prune * hz);
518 ln = llinfo_nd6.ln_next;
519 while (ln && ln != &llinfo_nd6) {
521 struct sockaddr_in6 *dst;
522 struct llinfo_nd6 *next = ln->ln_next;
523 /* XXX: used for the DELAY case only: */
524 struct nd_ifinfo *ndi = NULL;
526 if ((rt = ln->ln_rt) == NULL) {
530 if ((ifp = rt->rt_ifp) == NULL) {
534 ndi = &nd_ifinfo[ifp->if_index];
535 dst = (struct sockaddr_in6 *)rt_key(rt);
537 if (ln->ln_expire > time_second) {
544 panic("rt=0 in nd6_timer(ln=%p)\n", ln);
545 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
546 panic("rt_llinfo(%p) is not equal to ln(%p)\n",
549 panic("dst=0 in nd6_timer(ln=%p)\n", ln);
551 switch (ln->ln_state) {
552 case ND6_LLINFO_INCOMPLETE:
553 if (ln->ln_asked < nd6_mmaxtries) {
555 ln->ln_expire = time_second +
556 nd_ifinfo[ifp->if_index].retrans / 1000;
557 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
560 struct mbuf *m = ln->ln_hold;
562 struct ip6_hdr *ip6_in;
563 struct sockaddr_in6 sin6_in;
564 int64_t szoneid, dzoneid;
567 * Fake rcvif to make the ICMP error
568 * more helpful in diagnosing for the
570 * XXX: should we consider
573 m->m_pkthdr.rcvif = rt->rt_ifp;
576 * XXX: for scoped addresses, we should
577 * disambiguate the zone. We should
578 * perhaps hang sockaddr_in6 as aux
581 ip6_in = mtod(m, struct ip6_hdr *);
582 szoneid = in6_addr2zoneid(rt->rt_ifp,
584 dzoneid = in6_addr2zoneid(rt->rt_ifp,
586 if (szoneid < 0 || dzoneid < 0) {
592 sin6_in.sin6_addr = ip6_in->ip6_src;
593 sin6_in.sin6_scope_id = szoneid;
594 in6_embedscope(&ip6_in->ip6_src,
598 sin6_in.sin6_addr = ip6_in->ip6_dst;
599 sin6_in.sin6_scope_id = dzoneid;
600 in6_embedscope(&ip6_in->ip6_dst,
604 ICMP6_DST_UNREACH_ADDR, 0);
608 next = nd6_free(rt, 0);
611 case ND6_LLINFO_REACHABLE:
613 ln->ln_state = ND6_LLINFO_STALE;
614 ln->ln_expire = time_second + nd6_gctimer;
618 case ND6_LLINFO_STALE:
619 /* Garbage Collection(RFC 2461 5.3) */
621 next = nd6_free(rt, 1);
624 case ND6_LLINFO_DELAY:
625 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
628 ln->ln_state = ND6_LLINFO_PROBE;
629 ln->ln_expire = time_second +
631 nd6_ns_output(ifp, &dst->sin6_addr,
635 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
636 ln->ln_expire = time_second + nd6_gctimer;
639 case ND6_LLINFO_PROBE:
640 if (ln->ln_asked < nd6_umaxtries) {
642 ln->ln_expire = time_second +
643 nd_ifinfo[ifp->if_index].retrans / 1000;
644 nd6_ns_output(ifp, &dst->sin6_addr,
645 &dst->sin6_addr, ln, 0);
647 next = nd6_free(rt, 0);
654 /* expire default router list */
655 dr = TAILQ_FIRST(&nd_defrouter);
657 if (dr->expire && dr->expire < time_second) {
658 struct nd_defrouter *t;
659 t = TAILQ_NEXT(dr, dr_entry);
663 dr = TAILQ_NEXT(dr, dr_entry);
668 * expire interface addresses.
669 * in the past the loop was inside prefix expiry processing.
670 * However, from a stricter speci-confrmance standpoint, we should
671 * rather separate address lifetimes and prefix lifetimes.
674 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
676 /* check address lifetime */
677 lt6 = &ia6->ia6_lifetime;
678 if (IFA6_IS_INVALID(ia6)) {
682 * If the expiring address is temporary, try
683 * regenerating a new one. This would be useful when
684 * we suspended a laptop PC, then turned it on after a
685 * period that could invalidate all temporary
686 * addresses. Although we may have to restart the
687 * loop (see below), it must be after purging the
688 * address. Otherwise, we'd see an infinite loop of
691 if (ip6_use_tempaddr &&
692 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
693 if (regen_tmpaddr(ia6) == 0)
697 in6_purgeaddr(&ia6->ia_ifa);
700 goto addrloop; /* XXX: see below */
702 if (IFA6_IS_DEPRECATED(ia6)) {
703 int oldflags = ia6->ia6_flags;
705 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
708 * If a temporary address has just become deprecated,
709 * regenerate a new one if possible.
711 if (ip6_use_tempaddr &&
712 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
713 (oldflags & IN6_IFF_DEPRECATED) == 0) {
715 if (regen_tmpaddr(ia6) == 0) {
717 * A new temporary address is
719 * XXX: this means the address chain
720 * has changed while we are still in
721 * the loop. Although the change
722 * would not cause disaster (because
723 * it's not a deletion, but an
724 * addition,) we'd rather restart the
725 * loop just for safety. Or does this
726 * significantly reduce performance??
733 * A new RA might have made a deprecated address
736 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
740 /* expire prefix list */
741 pr = nd_prefix.lh_first;
744 * check prefix lifetime.
745 * since pltime is just for autoconf, pltime processing for
746 * prefix is not necessary.
748 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
749 time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
754 * address expiration and prefix expiration are
755 * separate. NEVER perform in6_purgeaddr here.
768 struct in6_ifaddr *ia6; /* deprecated/invalidated temporary address */
772 struct in6_ifaddr *public_ifa6 = NULL;
774 ifp = ia6->ia_ifa.ifa_ifp;
775 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
776 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
778 for (ifa = ifp->if_addrlist.tqh_first; ifa;
779 ifa = ifa->ifa_list.tqe_next)
782 struct in6_ifaddr *it6;
784 if (ifa->ifa_addr->sa_family != AF_INET6)
787 it6 = (struct in6_ifaddr *)ifa;
789 /* ignore no autoconf addresses. */
790 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
793 /* ignore autoconf addresses with different prefixes. */
794 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
798 * Now we are looking at an autoconf address with the same
799 * prefix as ours. If the address is temporary and is still
800 * preferred, do not create another one. It would be rare, but
801 * could happen, for example, when we resume a laptop PC after
804 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
805 !IFA6_IS_DEPRECATED(it6)) {
811 * This is a public autoconf address that has the same prefix
812 * as ours. If it is preferred, keep it. We can't break the
813 * loop here, because there may be a still-preferred temporary
814 * address with the prefix.
816 if (!IFA6_IS_DEPRECATED(it6))
820 if (public_ifa6 != NULL) {
823 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
824 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
825 " tmp addr,errno=%d\n", e);
835 * Nuke neighbor cache/prefix/default router management table, right before
842 struct llinfo_nd6 *ln, *nln;
843 struct nd_defrouter *dr, *ndr;
844 struct nd_prefix *pr, *npr;
847 * Nuke default router list entries toward ifp.
848 * We defer removal of default router list entries that is installed
849 * in the routing table, in order to keep additional side effects as
852 for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
853 ndr = TAILQ_NEXT(dr, dr_entry);
860 for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
861 ndr = TAILQ_NEXT(dr, dr_entry);
869 /* Nuke prefix list entries toward ifp */
870 for (pr = nd_prefix.lh_first; pr; pr = npr) {
872 if (pr->ndpr_ifp == ifp) {
874 * Previously, pr->ndpr_addr is removed as well,
875 * but I strongly believe we don't have to do it.
876 * nd6_purge() is only called from in6_ifdetach(),
877 * which removes all the associated interface addresses
879 * (jinmei@kame.net 20010129)
885 /* cancel default outgoing interface setting */
886 if (nd6_defifindex == ifp->if_index)
887 nd6_setdefaultiface(0);
889 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
890 /* refresh default router list */
895 * Nuke neighbor cache entries for the ifp.
896 * Note that rt->rt_ifp may not be the same as ifp,
897 * due to KAME goto ours hack. See RTM_RESOLVE case in
898 * nd6_rtrequest(), and ip6_input().
900 ln = llinfo_nd6.ln_next;
901 while (ln && ln != &llinfo_nd6) {
903 struct sockaddr_dl *sdl;
907 if (rt && rt->rt_gateway &&
908 rt->rt_gateway->sa_family == AF_LINK) {
909 sdl = (struct sockaddr_dl *)rt->rt_gateway;
910 if (sdl->sdl_index == ifp->if_index)
911 nln = nd6_free(rt, 0);
918 nd6_lookup(addr6, create, ifp)
919 struct in6_addr *addr6;
924 struct sockaddr_in6 sin6;
929 bzero(&sin6, sizeof(sin6));
930 sin6.sin6_len = sizeof(struct sockaddr_in6);
931 sin6.sin6_family = AF_INET6;
932 sin6.sin6_addr = *addr6;
934 if ((zoneid = in6_addr2zoneid(ifp, addr6)) < 0)
936 sin6.sin6_scope_id = zoneid;
938 rt = rtalloc1((struct sockaddr *)&sin6, create
941 #endif /* __FreeBSD__ */
943 if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
945 * This is the case for the default route.
946 * If we want to create a neighbor cache for the address, we
947 * should free the route for the destination and allocate an
960 * If no route is available and create is set,
961 * we allocate a host route for the destination
962 * and treat it like an interface route.
963 * This hack is necessary for a neighbor which can't
964 * be covered by our own prefix.
967 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
972 * Create a new route. RTF_LLINFO is necessary
973 * to create a Neighbor Cache entry for the
974 * destination in nd6_rtrequest which will be
975 * called in rtrequest via ifa->ifa_rtrequest.
977 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
979 (struct sockaddr *)&all1_sa,
981 RTF_HOST | RTF_LLINFO) &
986 "nd6_lookup: failed to add route for a "
987 "neighbor(%s), errno=%d\n",
988 ip6_sprintf(addr6), e);
995 struct llinfo_nd6 *ln =
996 (struct llinfo_nd6 *)rt->rt_llinfo;
997 ln->ln_state = ND6_LLINFO_NOSTATE;
1004 * Validation for the entry.
1005 * XXX: we can't use rt->rt_ifp to check for the interface, since
1006 * it might be the loopback interface if the entry is for our
1007 * own address on a non-loopback interface. Instead, we should
1008 * use rt->rt_ifa->ifa_ifp, which would specify the REAL interface.
1010 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
1011 rt->rt_gateway->sa_family != AF_LINK ||
1012 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
1014 log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n",
1015 ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
1023 * Detect if a given IPv6 address identifies a neighbor on a given link.
1024 * XXX: should take care of the destination of a p2p link?
1027 nd6_is_addr_neighbor(addr, ifp)
1028 struct sockaddr_in6 *addr;
1031 struct nd_prefix *pr;
1035 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
1036 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
1039 * A link-local address is always a neighbor.
1040 * XXX: we should use the sin6_scope_id field rather than the embedded
1042 * XXX: a link does not necessarily specify a single interface.
1044 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
1045 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
1049 * If the address matches one of our on-link prefixes, it should be a
1052 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1054 if (pr->ndpr_ifp != ifp)
1057 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
1060 for (i = 0; i < 4; i++) {
1061 if ((pr->ndpr_mask.s6_addr32[i] &
1062 addr->sin6_addr.s6_addr32[i]) !=
1063 pr->ndpr_prefix.sin6_addr.s6_addr32[i])
1066 if (i == 4) /* full match */
1071 * If the default router list is empty, all addresses are regarded
1072 * as on-link, and thus, as a neighbor.
1073 * XXX: we restrict the condition to hosts, because routers usually do
1074 * not have the "default router list".
1076 if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
1077 nd6_defifindex == ifp->if_index) {
1082 * Even if the address matches none of our addresses, it might be
1083 * in the neighbor cache.
1085 if ((rt = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL &&
1086 rt->rt_llinfo != NULL)
1095 * Free an nd6 llinfo entry.
1096 * Since the function would cause significant changes in the kernel, DO NOT
1097 * make it global, unless you have a strong reason for the change, and are sure
1098 * that the change is safe.
1100 static struct llinfo_nd6 *
1105 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
1106 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
1107 struct nd_defrouter *dr;
1110 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1111 * even though it is not harmful, it was not really necessary.
1114 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
1121 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1124 if (dr != NULL && dr->expire &&
1125 ln->ln_state == ND6_LLINFO_STALE && gc) {
1127 * If the reason for the deletion is just garbage
1128 * collection, and the neighbor is an active default
1129 * router, do not delete it. Instead, reset the GC
1130 * timer using the router's lifetime.
1131 * Simply deleting the entry would affect default
1132 * router selection, which is not necessarily a good
1133 * thing, especially when we're using router preference
1135 * XXX: the check for ln_state would be redundant,
1136 * but we intentionally keep it just in case.
1138 ln->ln_expire = dr->expire;
1140 return(ln->ln_next);
1143 if (ln->ln_router || dr) {
1145 * rt6_flush must be called whether or not the neighbor
1146 * is in the Default Router List.
1147 * See a corresponding comment in nd6_na_input().
1149 rt6_flush(&in6, rt->rt_ifp);
1154 * Unreachablity of a router might affect the default
1155 * router selection and on-link detection of advertised
1160 * Temporarily fake the state to choose a new default
1161 * router and to perform on-link determination of
1162 * prefixes correctly.
1163 * Below the state will be set correctly,
1164 * or the entry itself will be deleted.
1166 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1169 * Since defrouter_select() does not affect the
1170 * on-link determination and MIP6 needs the check
1171 * before the default router selection, we perform
1174 pfxlist_onlink_check();
1177 * refresh default router list
1185 * Before deleting the entry, remember the next entry as the
1186 * return value. We need this because pfxlist_onlink_check() above
1187 * might have freed other entries (particularly the old next entry) as
1188 * a side effect (XXX).
1193 * Detach the route from the routing tree and the list of neighbor
1194 * caches, and disable the route entry not to be used in already
1197 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
1198 rt_mask(rt), 0, (struct rtentry **)0);
1204 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1206 * XXX cost-effective metods?
1209 nd6_nud_hint(rt, dst6, force)
1211 struct in6_addr *dst6;
1214 struct llinfo_nd6 *ln;
1215 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
1216 long time_second = time.tv_sec;
1220 * If the caller specified "rt", use that. Otherwise, resolve the
1221 * routing table by supplied "dst6".
1226 if (!(rt = nd6_lookup(dst6, 0, NULL)))
1230 if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
1231 (rt->rt_flags & RTF_LLINFO) == 0 ||
1232 !rt->rt_llinfo || !rt->rt_gateway ||
1233 rt->rt_gateway->sa_family != AF_LINK) {
1234 /* This is not a host route. */
1238 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1239 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1243 * if we get upper-layer reachability confirmation many times,
1244 * it is possible we have false information.
1248 if (ln->ln_byhint > nd6_maxnudhint)
1252 ln->ln_state = ND6_LLINFO_REACHABLE;
1254 ln->ln_expire = time_second +
1255 nd_ifinfo[rt->rt_ifp->if_index].reachable;
1259 #if (defined(__bsdi__) && _BSDI_VERSION >= 199802) || defined(__NetBSD__) || defined(__OpenBSD__)
1260 nd6_rtrequest(req, rt, info)
1263 struct rt_addrinfo *info; /* xxx unused */
1265 nd6_rtrequest(req, rt, sa)
1268 struct sockaddr *sa; /* xxx unused */
1271 struct sockaddr *gate = rt->rt_gateway;
1272 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1273 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1274 struct ifnet *ifp = rt->rt_ifp;
1276 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
1277 long time_second = time.tv_sec;
1280 if ((rt->rt_flags & RTF_GATEWAY))
1283 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1285 * This is probably an interface direct route for a link
1286 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1287 * We do not need special treatment below for such a route.
1288 * Moreover, the RTF_LLINFO flag which would be set below
1289 * would annoy the ndp(8) command.
1294 if (req == RTM_RESOLVE &&
1295 !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp)) {
1297 * FreeBSD and BSD/OS often make a cloned host route based
1298 * on a less-specific route (e.g. the default route).
1299 * If the less specific route does not have a "gateway"
1300 * (this is the case when the route just goes to a p2p
1301 * interface), we'll mistakenly make a neighbor cache for
1302 * the host route, and will see strange neighbor solicitation
1303 * for the corresponding destination. In order to avoid the
1304 * confusion, we check if the destination of the route is
1305 * a neighbor in terms of neighbor discovery, and stop the
1306 * process if not. Additionally, we remove the LLINFO flag
1307 * so that ndp(8) will not try to get the neighbor information
1308 * of the destination.
1310 rt->rt_flags &= ~RTF_LLINFO;
1317 * There is no backward compatibility :)
1319 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1320 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1321 * rt->rt_flags |= RTF_CLONING;
1323 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1325 * Case 1: This route should come from
1326 * a route to interface. RTF_LLINFO flag is set
1327 * for a host route whose destination should be
1328 * treated as on-link.
1330 rt_setgate(rt, rt_key(rt),
1331 (struct sockaddr *)&null_sdl);
1332 gate = rt->rt_gateway;
1333 SDL(gate)->sdl_type = ifp->if_type;
1334 SDL(gate)->sdl_index = ifp->if_index;
1336 ln->ln_expire = time_second;
1338 if (ln && ln->ln_expire == 0) {
1339 /* kludge for desktops */
1341 printf("nd6_rtequest: time.tv_sec is zero; "
1347 if ((rt->rt_flags & RTF_CLONING))
1351 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1352 * We don't do that here since llinfo is not ready yet.
1354 * There are also couple of other things to be discussed:
1355 * - unsolicited NA code needs improvement beforehand
1356 * - RFC2461 says we MAY send multicast unsolicited NA
1357 * (7.2.6 paragraph 4), however, it also says that we
1358 * SHOULD provide a mechanism to prevent multicast NA storm.
1359 * we don't have anything like it right now.
1360 * note that the mechanism needs a mutual agreement
1361 * between proxies, which means that we need to implement
1362 * a new protocol, or a new kludge.
1363 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1364 * we need to check ip6forwarding before sending it.
1365 * (or should we allow proxy ND configuration only for
1366 * routers? there's no mention about proxy ND from hosts)
1369 /* XXX it does not work */
1370 if (rt->rt_flags & RTF_ANNOUNCE)
1372 &SIN6(rt_key(rt))->sin6_addr,
1373 &SIN6(rt_key(rt))->sin6_addr,
1374 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1379 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1381 * Address resolution isn't necessary for a point to
1382 * point link, so we can skip this test for a p2p link.
1384 if (gate->sa_family != AF_LINK ||
1385 gate->sa_len < sizeof(null_sdl)) {
1387 "nd6_rtrequest: bad gateway value: %s\n",
1391 SDL(gate)->sdl_type = ifp->if_type;
1392 SDL(gate)->sdl_index = ifp->if_index;
1395 break; /* This happens on a route change */
1397 * Case 2: This route may come from cloning, or a manual route
1398 * add with a LL address.
1400 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1401 rt->rt_llinfo = (caddr_t)ln;
1403 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1408 Bzero(ln, sizeof(*ln));
1410 /* this is required for "ndp" command. - shin */
1411 if (req == RTM_ADD) {
1413 * gate should have some valid AF_LINK entry,
1414 * and ln->ln_expire should have some lifetime
1415 * which is specified by ndp command.
1417 ln->ln_state = ND6_LLINFO_REACHABLE;
1421 * When req == RTM_RESOLVE, rt is created and
1422 * initialized in rtrequest(), so rt_expire is 0.
1424 ln->ln_state = ND6_LLINFO_NOSTATE;
1425 ln->ln_expire = time_second;
1427 rt->rt_flags |= RTF_LLINFO;
1428 ln->ln_next = llinfo_nd6.ln_next;
1429 llinfo_nd6.ln_next = ln;
1430 ln->ln_prev = &llinfo_nd6;
1431 ln->ln_next->ln_prev = ln;
1434 * check if rt_key(rt) is one of my address assigned
1437 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1438 &SIN6(rt_key(rt))->sin6_addr);
1440 caddr_t macp = nd6_ifptomac(ifp);
1442 ln->ln_state = ND6_LLINFO_REACHABLE;
1445 Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1446 SDL(gate)->sdl_alen = ifp->if_addrlen;
1448 if (nd6_useloopback) {
1450 #if _BSDI_VERSION >= 199802
1451 extern struct ifnet *loifp;
1452 rt->rt_ifp = loifp; /* XXX */
1454 extern struct ifnet loif;
1455 rt->rt_ifp = &loif; /* XXX */
1457 #elif defined(__OpenBSD__)
1458 rt->rt_ifp = lo0ifp; /* XXX */
1460 rt->rt_ifp = &loif[0]; /* XXX */
1463 * Make sure rt_ifa be equal to the ifaddr
1464 * corresponding to the address.
1465 * We need this because when we refer
1466 * rt_ifa->ia6_flags in ip6_input, we assume
1467 * that the rt_ifa points to the address instead
1468 * of the loopback address.
1470 if (ifa != rt->rt_ifa) {
1471 IFAFREE(rt->rt_ifa);
1476 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1478 ln->ln_state = ND6_LLINFO_REACHABLE;
1481 /* join solicited node multicast for proxy ND */
1482 if (ifp->if_flags & IFF_MULTICAST) {
1483 struct in6_addr llsol;
1486 llsol = SIN6(rt_key(rt))->sin6_addr;
1487 llsol.s6_addr16[0] = htons(0xff02);
1488 llsol.s6_addr16[1] = htons(ifp->if_index);
1489 llsol.s6_addr32[1] = 0;
1490 llsol.s6_addr32[2] = htonl(1);
1491 llsol.s6_addr8[12] = 0xff;
1493 if (!in6_addmulti(&llsol, ifp, &error)) {
1494 nd6log((LOG_ERR, "%s: failed to join "
1495 "%s (errno=%d)\n", if_name(ifp),
1496 ip6_sprintf(&llsol), error));
1505 /* leave from solicited node multicast for proxy ND */
1506 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1507 (ifp->if_flags & IFF_MULTICAST) != 0) {
1508 struct in6_addr llsol;
1509 struct in6_multi *in6m;
1511 llsol = SIN6(rt_key(rt))->sin6_addr;
1512 llsol.s6_addr16[0] = htons(0xff02);
1513 llsol.s6_addr16[1] = htons(ifp->if_index);
1514 llsol.s6_addr32[1] = 0;
1515 llsol.s6_addr32[2] = htonl(1);
1516 llsol.s6_addr8[12] = 0xff;
1518 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1523 ln->ln_next->ln_prev = ln->ln_prev;
1524 ln->ln_prev->ln_next = ln->ln_next;
1527 rt->rt_flags &= ~RTF_LLINFO;
1529 m_freem(ln->ln_hold);
1530 R_Free((caddr_t)ln);
1535 nd6_ioctl(cmd, data, ifp)
1540 struct in6_drlist *drl = (struct in6_drlist *)data;
1541 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1542 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1543 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1544 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1545 struct nd_defrouter *dr;
1546 struct nd_prefix *pr;
1548 int i = 0, error = 0;
1552 case SIOCGDRLST_IN6:
1554 * obsolete API, use sysctl under net.inet6.icmp6
1556 bzero(drl, sizeof(*drl));
1562 dr = TAILQ_FIRST(&nd_defrouter);
1563 while (dr && i < DRLSTSIZ) {
1564 drl->defrouter[i].rtaddr = dr->rtaddr;
1565 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1566 /* XXX: need to this hack for KAME stack */
1567 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1570 "default router list contains a "
1571 "non-linklocal address(%s)\n",
1572 ip6_sprintf(&drl->defrouter[i].rtaddr));
1574 drl->defrouter[i].flags = dr->flags;
1575 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1576 drl->defrouter[i].expire = dr->expire;
1577 drl->defrouter[i].if_index = dr->ifp->if_index;
1579 dr = TAILQ_NEXT(dr, dr_entry);
1583 case SIOCGPRLST_IN6:
1585 * obsolete API, use sysctl under net.inet6.icmp6
1587 * XXX the structure in6_prlist was changed in backward-
1588 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1589 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1592 * XXX meaning of fields, especialy "raflags", is very
1593 * differnet between RA prefix list and RR/static prefix list.
1594 * how about separating ioctls into two?
1596 bzero(oprl, sizeof(*oprl));
1602 pr = nd_prefix.lh_first;
1603 while (pr && i < PRLSTSIZ) {
1604 struct nd_pfxrouter *pfr;
1607 (void)in6_embedscope(&oprl->prefix[i].prefix,
1609 oprl->prefix[i].raflags = pr->ndpr_raf;
1610 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1611 oprl->prefix[i].vltime = pr->ndpr_vltime;
1612 oprl->prefix[i].pltime = pr->ndpr_pltime;
1613 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1614 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1615 oprl->prefix[i].expire = 0;
1619 /* XXX: we assume time_t is signed. */
1621 ~(1 << ((sizeof(maxexpire) * 8) - 1));
1622 if (pr->ndpr_vltime <
1623 maxexpire - pr->ndpr_lastupdate) {
1624 oprl->prefix[i].expire =
1625 pr->ndpr_lastupdate +
1628 oprl->prefix[i].expire = maxexpire;
1630 pfr = pr->ndpr_advrtrs.lh_first;
1634 #define RTRADDR oprl->prefix[i].advrtr[j]
1635 RTRADDR = pfr->router->rtaddr;
1636 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1637 /* XXX: hack for KAME */
1638 RTRADDR.s6_addr16[1] = 0;
1641 "a router(%s) advertises "
1643 "non-link local address\n",
1644 ip6_sprintf(&RTRADDR));
1648 pfr = pfr->pfr_next;
1650 oprl->prefix[i].advrtrs = j;
1651 oprl->prefix[i].origin = PR_ORIG_RA;
1659 case OSIOCGIFINFO_IN6:
1660 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1664 ndi->ndi.linkmtu = nd_ifinfo[ifp->if_index].linkmtu;
1665 ndi->ndi.maxmtu = nd_ifinfo[ifp->if_index].maxmtu;
1666 ndi->ndi.basereachable =
1667 nd_ifinfo[ifp->if_index].basereachable;
1668 ndi->ndi.reachable = nd_ifinfo[ifp->if_index].reachable;
1669 ndi->ndi.retrans = nd_ifinfo[ifp->if_index].retrans;
1670 ndi->ndi.flags = nd_ifinfo[ifp->if_index].flags;
1671 ndi->ndi.recalctm = nd_ifinfo[ifp->if_index].recalctm;
1672 ndi->ndi.chlim = nd_ifinfo[ifp->if_index].chlim;
1673 ndi->ndi.receivedra = nd_ifinfo[ifp->if_index].receivedra;
1675 case SIOCGIFINFO_IN6:
1676 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1680 ndi->ndi = nd_ifinfo[ifp->if_index];
1682 case SIOCSIFINFO_FLAGS:
1683 /* XXX: almost all other fields of ndi->ndi is unused */
1684 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1688 nd_ifinfo[ifp->if_index].flags = ndi->ndi.flags;
1690 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1691 /* sync kernel routing table with the default router list */
1695 case SIOCSPFXFLUSH_IN6:
1697 /* flush all the prefix advertised by routers */
1698 struct nd_prefix *pr, *next;
1705 for (pr = nd_prefix.lh_first; pr; pr = next) {
1706 struct in6_ifaddr *ia, *ia_next;
1708 next = pr->ndpr_next;
1710 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1713 /* do we really have to remove addresses as well? */
1714 for (ia = in6_ifaddr; ia; ia = ia_next) {
1715 /* ia might be removed. keep the next ptr. */
1716 ia_next = ia->ia_next;
1718 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1721 if (ia->ia6_ndpr == pr)
1722 in6_purgeaddr(&ia->ia_ifa);
1729 case SIOCSRTRFLUSH_IN6:
1731 /* flush all the default routers */
1732 struct nd_defrouter *dr, *next;
1740 for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = next) {
1741 next = TAILQ_NEXT(dr, dr_entry);
1748 case SIOCGNBRINFO_IN6:
1750 struct llinfo_nd6 *ln;
1751 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1754 * XXX: KAME specific hack for scoped addresses
1755 * XXXX: for other scopes than link-local?
1757 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1758 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1759 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1762 *idp = htons(ifp->if_index);
1770 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL ||
1771 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
1776 nbi->state = ln->ln_state;
1777 nbi->asked = ln->ln_asked;
1778 nbi->isrouter = ln->ln_router;
1779 nbi->expire = ln->ln_expire;
1784 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1785 ndif->ifindex = nd6_defifindex;
1787 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1788 return(nd6_setdefaultiface(ndif->ifindex));
1795 * Create neighbor cache entry and cache link-layer address,
1796 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1799 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1801 struct in6_addr *from;
1804 int type; /* ICMP6 type */
1805 int code; /* type dependent information */
1807 struct rtentry *rt = NULL;
1808 struct llinfo_nd6 *ln = NULL;
1810 struct sockaddr_dl *sdl = NULL;
1815 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
1816 long time_second = time.tv_sec;
1820 panic("ifp == NULL in nd6_cache_lladdr");
1822 panic("from == NULL in nd6_cache_lladdr");
1824 /* nothing must be updated for unspecified address */
1825 if (IN6_IS_ADDR_UNSPECIFIED(from))
1829 * Validation about ifp->if_addrlen and lladdrlen must be done in
1832 * XXX If the link does not have link-layer adderss, what should
1833 * we do? (ifp->if_addrlen == 0)
1834 * Spec says nothing in sections for RA, RS and NA. There's small
1835 * description on it in NS section (RFC 2461 7.2.3).
1838 rt = nd6_lookup(from, 0, ifp);
1841 /* nothing must be done if there's no lladdr */
1842 if (!lladdr || !lladdrlen)
1846 rt = nd6_lookup(from, 1, ifp);
1849 /* do nothing if static ndp is set */
1850 if (rt->rt_flags & RTF_STATIC)
1857 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1859 (void)nd6_free(rt, 0);
1862 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1865 if (!rt->rt_gateway)
1867 if (rt->rt_gateway->sa_family != AF_LINK)
1869 sdl = SDL(rt->rt_gateway);
1871 olladdr = (sdl->sdl_alen) ? 1 : 0;
1872 if (olladdr && lladdr) {
1873 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1881 * newentry olladdr lladdr llchange (*=record)
1884 * 0 n y -- (3) * STALE
1886 * 0 y y y (5) * STALE
1887 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1888 * 1 -- y -- (7) * STALE
1891 if (lladdr) { /* (3-5) and (7) */
1893 * Record source link-layer address
1894 * XXX is it dependent to ifp->if_type?
1896 sdl->sdl_alen = ifp->if_addrlen;
1897 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1901 if ((!olladdr && lladdr) /* (3) */
1902 || (olladdr && lladdr && llchange)) { /* (5) */
1904 newstate = ND6_LLINFO_STALE;
1905 } else /* (1-2,4) */
1909 if (!lladdr) /* (6) */
1910 newstate = ND6_LLINFO_NOSTATE;
1912 newstate = ND6_LLINFO_STALE;
1917 * Update the state of the neighbor cache.
1919 ln->ln_state = newstate;
1921 if (ln->ln_state == ND6_LLINFO_STALE) {
1923 * XXX: since nd6_output() below will cause
1924 * state tansition to DELAY and reset the timer,
1925 * we must set the timer now, although it is actually
1928 ln->ln_expire = time_second + nd6_gctimer;
1932 * we assume ifp is not a p2p here, so just
1933 * set the 2nd argument as the 1st one.
1935 nd6_output(ifp, ifp, ln->ln_hold,
1936 (struct sockaddr_in6 *)rt_key(rt),
1940 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1941 /* probe right away */
1942 ln->ln_expire = time_second;
1947 * ICMP6 type dependent behavior.
1949 * NS: clear IsRouter if new entry
1950 * RS: clear IsRouter
1951 * RA: set IsRouter if there's lladdr
1952 * redir: clear IsRouter if new entry
1955 * The spec says that we must set IsRouter in the following cases:
1956 * - If lladdr exist, set IsRouter. This means (1-5).
1957 * - If it is old entry (!newentry), set IsRouter. This means (7).
1958 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1959 * A quetion arises for (1) case. (1) case has no lladdr in the
1960 * neighbor cache, this is similar to (6).
1961 * This case is rare but we figured that we MUST NOT set IsRouter.
1963 * newentry olladdr lladdr llchange NS RS RA redir
1965 * 0 n n -- (1) c ? s
1966 * 0 y n -- (2) c s s
1967 * 0 n y -- (3) c s s
1970 * 1 -- n -- (6) c c c s
1971 * 1 -- y -- (7) c c s c s
1975 switch (type & 0xff) {
1976 case ND_NEIGHBOR_SOLICIT:
1978 * New entry must have is_router flag cleared.
1980 if (is_newentry) /* (6-7) */
1985 * If the icmp is a redirect to a better router, always set the
1986 * is_router flag. Otherwise, if the entry is newly created,
1987 * clear the flag. [RFC 2461, sec 8.3]
1989 if (code == ND_REDIRECT_ROUTER)
1991 else if (is_newentry) /* (6-7) */
1994 case ND_ROUTER_SOLICIT:
1996 * is_router flag must always be cleared.
2000 case ND_ROUTER_ADVERT:
2002 * Mark an entry with lladdr as a router.
2004 if ((!is_newentry && (olladdr || lladdr)) /* (2-5) */
2005 || (is_newentry && lladdr)) { /* (7) */
2012 * When the link-layer address of a router changes, select the
2013 * best router again. In particular, when the neighbor entry is newly
2014 * created, it might affect the selection policy.
2015 * Question: can we restrict the first condition to the "is_newentry"
2017 * XXX: when we hear an RA from a new router with the link-layer
2018 * address option, defrouter_select() is called twice, since
2019 * defrtrlist_update called the function as well. However, I believe
2020 * we can compromise the overhead, since it only happens the first
2022 * XXX: although defrouter_select() should not have a bad effect
2023 * for those are not autoconfigured hosts, we explicitly avoid such
2026 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
2033 nd6_slowtimo(ignored_arg)
2037 int s = splsoftnet();
2042 struct nd_ifinfo *nd6if;
2044 #if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
2045 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
2046 nd6_slowtimo, NULL);
2047 #elif defined(__OpenBSD__)
2048 timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
2049 timeout_add(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz);
2051 timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
2053 for (i = 1; i < if_index + 1; i++) {
2054 if (!nd_ifinfo || i >= nd_ifinfo_indexlim)
2056 nd6if = &nd_ifinfo[i];
2057 if (nd6if->basereachable && /* already initialized */
2058 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
2060 * Since reachable time rarely changes by router
2061 * advertisements, we SHOULD insure that a new random
2062 * value gets recomputed at least once every few hours.
2065 nd6if->recalctm = nd6_recalc_reachtm_interval;
2066 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
2072 #define senderr(e) { error = (e); goto bad;}
2074 nd6_output(ifp, origifp, m0, dst, rt0)
2076 struct ifnet *origifp;
2078 struct sockaddr_in6 *dst;
2079 struct rtentry *rt0;
2081 struct mbuf *m = m0;
2082 struct rtentry *rt = rt0;
2083 struct sockaddr_in6 *gw6 = NULL;
2084 struct llinfo_nd6 *ln = NULL;
2086 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
2087 long time_second = time.tv_sec;
2089 #if defined(__OpenBSD__) && defined(IPSEC)
2093 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
2096 if (nd6_need_cache(ifp) == 0)
2100 * next hop determination. This routine is derived from ether_outpout.
2103 if ((rt->rt_flags & RTF_UP) == 0) {
2105 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL)) !=
2108 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1)) !=
2113 if (rt->rt_ifp != ifp) {
2114 /* XXX: loop care? */
2115 return nd6_output(ifp, origifp, m0,
2119 senderr(EHOSTUNREACH);
2122 if (rt->rt_flags & RTF_GATEWAY) {
2123 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
2126 * We skip link-layer address resolution and NUD
2127 * if the gateway is not a neighbor from ND point
2128 * of view, regardless of the value of nd_ifinfo.flags.
2129 * The second condition is a bit tricky; we skip
2130 * if the gateway is our own address, which is
2131 * sometimes used to install a route to a p2p link.
2133 if (!nd6_is_addr_neighbor(gw6, ifp) ||
2134 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
2136 * We allow this kind of tricky route only
2137 * when the outgoing interface is p2p.
2138 * XXX: we may need a more generic rule here.
2140 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2141 senderr(EHOSTUNREACH);
2146 if (rt->rt_gwroute == 0)
2148 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
2149 rtfree(rt); rt = rt0;
2152 rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
2154 rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
2156 if ((rt = rt->rt_gwroute) == 0)
2157 senderr(EHOSTUNREACH);
2158 #if defined(__bsdi__) || defined(__NetBSD__)
2159 /* the "G" test below also prevents rt == rt0 */
2160 if ((rt->rt_flags & RTF_GATEWAY) ||
2161 (rt->rt_ifp != ifp)) {
2163 rt0->rt_gwroute = 0;
2164 senderr(EHOSTUNREACH);
2172 * Address resolution or Neighbor Unreachability Detection
2174 * At this point, the destination of the packet must be a unicast
2175 * or an anycast address(i.e. not a multicast).
2178 /* Look up the neighbor cache for the nexthop */
2179 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
2180 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2183 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2184 * the condition below is not very efficient. But we believe
2185 * it is tolerable, because this should be a rare case.
2187 if (nd6_is_addr_neighbor(dst, ifp) &&
2188 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
2189 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2192 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
2193 !(nd_ifinfo[ifp->if_index].flags & ND6_IFF_PERFORMNUD)) {
2195 "nd6_output: can't allocate llinfo for %s "
2197 ip6_sprintf(&dst->sin6_addr), ln, rt);
2198 senderr(EIO); /* XXX: good error? */
2201 goto sendpkt; /* send anyway */
2204 /* We don't have to do link-layer address resolution on a p2p link. */
2205 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2206 ln->ln_state < ND6_LLINFO_REACHABLE) {
2207 ln->ln_state = ND6_LLINFO_STALE;
2208 ln->ln_expire = time_second + nd6_gctimer;
2212 * The first time we send a packet to a neighbor whose entry is
2213 * STALE, we have to change the state to DELAY and a sets a timer to
2214 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2215 * neighbor unreachability detection on expiration.
2218 if (ln->ln_state == ND6_LLINFO_STALE) {
2220 ln->ln_state = ND6_LLINFO_DELAY;
2221 ln->ln_expire = time_second + nd6_delay;
2225 * If the neighbor cache entry has a state other than INCOMPLETE
2226 * (i.e. its link-layer address is already resolved), just
2229 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
2233 * There is a neighbor cache entry, but no ethernet address
2234 * response yet. Replace the held mbuf (if any) with this
2236 * This code conforms to the rate-limiting rule described in Section
2237 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
2240 if (ln->ln_state == ND6_LLINFO_NOSTATE)
2241 ln->ln_state = ND6_LLINFO_INCOMPLETE;
2243 m_freem(ln->ln_hold);
2245 if (ln->ln_expire) {
2246 if (ln->ln_asked < nd6_mmaxtries &&
2247 ln->ln_expire < time_second) {
2249 ln->ln_expire = time_second +
2250 nd_ifinfo[ifp->if_index].retrans / 1000;
2251 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
2257 #if defined(__OpenBSD__) && defined(IPSEC)
2259 * If the packet needs outgoing IPsec crypto processing and the
2260 * interface doesn't support it, drop it.
2262 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
2265 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
2266 #if defined(__OpenBSD__) && defined(IPSEC)
2268 (origifp->if_capabilities & IFCAP_IPSEC) == 0) {
2269 /* Tell IPsec to do its own crypto. */
2270 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
2275 return((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
2278 #if defined(__OpenBSD__) && defined(IPSEC)
2280 (ifp->if_capabilities & IFCAP_IPSEC) == 0) {
2281 /* Tell IPsec to do its own crypto. */
2282 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
2287 return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
2301 * XXX: we currently do not make neighbor cache on any interface
2302 * other than ARCnet, Ethernet, FDDI and GIF.
2305 * - unidirectional tunnels needs no ND
2307 switch (ifp->if_type) {
2312 #ifdef IFT_PROPVIRTUAL
2313 case IFT_PROPVIRTUAL:
2318 #ifdef IFT_IEEE80211
2321 case IFT_GIF: /* XXX need more cases? */
2329 nd6_storelladdr(ifp, rt, m, dst, desten)
2333 struct sockaddr *dst;
2337 struct sockaddr_dl *sdl;
2339 if (m->m_flags & M_MCAST) {
2340 switch (ifp->if_type) {
2343 #ifdef IFT_PROPVIRTUAL
2344 case IFT_PROPVIRTUAL:
2349 #ifdef IFT_IEEE80211
2352 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2357 * netbsd can use if_broadcastaddr, but we don't do so
2358 * to reduce # of ifdef.
2360 for (i = 0; i < ifp->if_addrlen; i++)
2373 /* this could happen, if we could not allocate memory */
2377 if (rt->rt_gateway->sa_family != AF_LINK) {
2378 printf("nd6_storelladdr: something odd happens\n");
2382 sdl = SDL(rt->rt_gateway);
2383 if (sdl->sdl_alen == 0) {
2384 /* this should be impossible, but we bark here for debugging */
2385 printf("nd6_storelladdr: sdl_alen == 0, dst=%s, if=%s\n",
2386 ip6_sprintf(&SIN6(dst)->sin6_addr), if_name(ifp));
2391 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2395 #ifdef CYGPKG_NET_FREEBSD_SYSCTL
2396 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2397 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2400 SYSCTL_DECL(_net_inet6_icmp6);
2402 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2403 CTLFLAG_RD, nd6_sysctl_drlist, "");
2404 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2405 CTLFLAG_RD, nd6_sysctl_prlist, "");
2407 #ifdef CYGPKG_NET_FREEBSD_SYSCTL
2409 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2413 struct in6_defrouter *d, *de;
2414 struct nd_defrouter *dr;
2420 for (dr = TAILQ_FIRST(&nd_defrouter);
2422 dr = TAILQ_NEXT(dr, dr_entry)) {
2423 d = (struct in6_defrouter *)buf;
2424 de = (struct in6_defrouter *)(buf + sizeof(buf));
2427 bzero(d, sizeof(*d));
2428 d->rtaddr.sin6_family = AF_INET6;
2429 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2430 if (in6_recoverscope(&d->rtaddr, &dr->rtaddr,
2434 "default router list (%s)\n",
2435 ip6_sprintf(&dr->rtaddr));
2436 d->flags = dr->flags;
2437 d->rtlifetime = dr->rtlifetime;
2438 d->expire = dr->expire;
2439 d->if_index = dr->ifp->if_index;
2441 panic("buffer too short");
2443 error = SYSCTL_OUT(req, buf, sizeof(*d));
2451 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2455 struct in6_prefix *p, *pe;
2456 struct nd_prefix *pr;
2462 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2465 struct sockaddr_in6 *sin6, *s6;
2466 struct nd_pfxrouter *pfr;
2468 p = (struct in6_prefix *)buf;
2469 pe = (struct in6_prefix *)(buf + sizeof(buf));
2472 bzero(p, sizeof(*p));
2473 sin6 = (struct sockaddr_in6 *)(p + 1);
2475 p->prefix = pr->ndpr_prefix;
2476 if (in6_recoverscope(&p->prefix,
2477 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2479 "scope error in prefix list (%s)\n",
2480 ip6_sprintf(&p->prefix.sin6_addr));
2481 p->raflags = pr->ndpr_raf;
2482 p->prefixlen = pr->ndpr_plen;
2483 p->vltime = pr->ndpr_vltime;
2484 p->pltime = pr->ndpr_pltime;
2485 p->if_index = pr->ndpr_ifp->if_index;
2486 p->expire = pr->ndpr_expire;
2487 p->refcnt = pr->ndpr_refcnt;
2488 p->flags = pr->ndpr_stateflags;
2489 p->origin = PR_ORIG_RA;
2491 for (pfr = pr->ndpr_advrtrs.lh_first;
2493 pfr = pfr->pfr_next) {
2494 if ((void *)&sin6[advrtrs + 1] >
2499 s6 = &sin6[advrtrs];
2500 bzero(s6, sizeof(*s6));
2501 s6->sin6_family = AF_INET6;
2502 s6->sin6_len = sizeof(*sin6);
2503 if (in6_recoverscope(s6,
2504 &pfr->router->rtaddr,
2505 pfr->router->ifp) != 0)
2508 "prefix list (%s)\n",
2509 ip6_sprintf(&pfr->router->rtaddr));
2512 p->advrtrs = advrtrs;
2514 panic("buffer too short");
2516 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2517 error = SYSCTL_OUT(req, buf, advance);