2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/callout.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
46 #include <sys/kernel.h>
47 #include <sys/protosw.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
51 #include <sys/rwlock.h>
52 #include <sys/queue.h>
53 #include <sys/sysctl.h>
56 #include <net/if_arc.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/iso88025.h>
61 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <net/if_llatbl.h>
66 #define L3_ADDR_SIN6(le) ((struct sockaddr_in6 *) L3_ADDR(le))
67 #include <netinet/if_ether.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet/ip6.h>
70 #include <netinet6/ip6_var.h>
71 #include <netinet6/scope6_var.h>
72 #include <netinet6/nd6.h>
73 #include <netinet6/in6_ifattach.h>
74 #include <netinet/icmp6.h>
75 #include <netinet6/send.h>
77 #include <sys/limits.h>
79 #include <security/mac/mac_framework.h>
81 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
82 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
84 #define SIN6(s) ((struct sockaddr_in6 *)s)
87 VNET_DEFINE(int, nd6_prune) = 1; /* walk list every 1 seconds */
88 VNET_DEFINE(int, nd6_delay) = 5; /* delay first probe time 5 second */
89 VNET_DEFINE(int, nd6_umaxtries) = 3; /* maximum unicast query */
90 VNET_DEFINE(int, nd6_mmaxtries) = 3; /* maximum multicast query */
91 VNET_DEFINE(int, nd6_useloopback) = 1; /* use loopback interface for
93 VNET_DEFINE(int, nd6_gctimer) = (60 * 60 * 24); /* 1 day: garbage
96 /* preventing too many loops in ND option parsing */
97 static VNET_DEFINE(int, nd6_maxndopt) = 10; /* max # of ND options allowed */
99 VNET_DEFINE(int, nd6_maxnudhint) = 0; /* max # of subsequent upper
101 static VNET_DEFINE(int, nd6_maxqueuelen) = 1; /* max pkts cached in unresolved
103 #define V_nd6_maxndopt VNET(nd6_maxndopt)
104 #define V_nd6_maxqueuelen VNET(nd6_maxqueuelen)
107 VNET_DEFINE(int, nd6_debug) = 1;
109 VNET_DEFINE(int, nd6_debug) = 0;
114 static int nd6_inuse, nd6_allocated;
117 VNET_DEFINE(struct nd_drhead, nd_defrouter);
118 VNET_DEFINE(struct nd_prhead, nd_prefix);
120 VNET_DEFINE(int, nd6_recalc_reachtm_interval) = ND6_RECALC_REACHTM_INTERVAL;
121 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
123 static struct sockaddr_in6 all1_sa;
125 int (*send_sendso_input_hook)(struct mbuf *, struct ifnet *, int, int);
127 static int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *,
129 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
130 static void nd6_slowtimo(void *);
131 static int regen_tmpaddr(struct in6_ifaddr *);
132 static struct llentry *nd6_free(struct llentry *, int);
133 static void nd6_llinfo_timer(void *);
134 static void clear_llinfo_pqueue(struct llentry *);
136 static VNET_DEFINE(struct callout, nd6_slowtimo_ch);
137 #define V_nd6_slowtimo_ch VNET(nd6_slowtimo_ch)
139 VNET_DEFINE(struct callout, nd6_timer_ch);
146 LIST_INIT(&V_nd_prefix);
148 all1_sa.sin6_family = AF_INET6;
149 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
150 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
151 all1_sa.sin6_addr.s6_addr[i] = 0xff;
153 /* initialization of the default router list */
154 TAILQ_INIT(&V_nd_defrouter);
157 callout_init(&V_nd6_slowtimo_ch, 0);
158 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
159 nd6_slowtimo, curvnet);
167 callout_drain(&V_nd6_slowtimo_ch);
168 callout_drain(&V_nd6_timer_ch);
173 nd6_ifattach(struct ifnet *ifp)
175 struct nd_ifinfo *nd;
177 nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
178 bzero(nd, sizeof(*nd));
182 nd->chlim = IPV6_DEFHLIM;
183 nd->basereachable = REACHABLE_TIME;
184 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
185 nd->retrans = RETRANS_TIMER;
187 nd->flags = ND6_IFF_PERFORMNUD;
189 /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL. */
190 if (V_ip6_auto_linklocal || (ifp->if_flags & IFF_LOOPBACK))
191 nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
193 /* A loopback interface does not need to accept RTADV. */
194 if (V_ip6_accept_rtadv && !(ifp->if_flags & IFF_LOOPBACK))
195 nd->flags |= ND6_IFF_ACCEPT_RTADV;
196 if (V_ip6_no_radr && !(ifp->if_flags & IFF_LOOPBACK))
197 nd->flags |= ND6_IFF_NO_RADR;
199 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
200 nd6_setmtu0(ifp, nd);
206 nd6_ifdetach(struct nd_ifinfo *nd)
213 * Reset ND level link MTU. This function is called when the physical MTU
214 * changes, which means we might have to adjust the ND level MTU.
217 nd6_setmtu(struct ifnet *ifp)
220 nd6_setmtu0(ifp, ND_IFINFO(ifp));
223 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
225 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
229 omaxmtu = ndi->maxmtu;
231 switch (ifp->if_type) {
233 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
236 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
239 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
242 ndi->maxmtu = ifp->if_mtu;
247 * Decreasing the interface MTU under IPV6 minimum MTU may cause
248 * undesirable situation. We thus notify the operator of the change
249 * explicitly. The check for omaxmtu is necessary to restrict the
250 * log to the case of changing the MTU, not initializing it.
252 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
253 log(LOG_NOTICE, "nd6_setmtu0: "
254 "new link MTU on %s (%lu) is too small for IPv6\n",
255 if_name(ifp), (unsigned long)ndi->maxmtu);
258 if (ndi->maxmtu > V_in6_maxmtu)
259 in6_setmaxmtu(); /* check all interfaces just in case */
264 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
267 bzero(ndopts, sizeof(*ndopts));
268 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
270 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
273 ndopts->nd_opts_done = 1;
274 ndopts->nd_opts_search = NULL;
279 * Take one ND option.
282 nd6_option(union nd_opts *ndopts)
284 struct nd_opt_hdr *nd_opt;
288 panic("ndopts == NULL in nd6_option");
289 if (ndopts->nd_opts_last == NULL)
290 panic("uninitialized ndopts in nd6_option");
291 if (ndopts->nd_opts_search == NULL)
293 if (ndopts->nd_opts_done)
296 nd_opt = ndopts->nd_opts_search;
298 /* make sure nd_opt_len is inside the buffer */
299 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
300 bzero(ndopts, sizeof(*ndopts));
304 olen = nd_opt->nd_opt_len << 3;
307 * Message validation requires that all included
308 * options have a length that is greater than zero.
310 bzero(ndopts, sizeof(*ndopts));
314 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
315 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
316 /* option overruns the end of buffer, invalid */
317 bzero(ndopts, sizeof(*ndopts));
319 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
320 /* reached the end of options chain */
321 ndopts->nd_opts_done = 1;
322 ndopts->nd_opts_search = NULL;
328 * Parse multiple ND options.
329 * This function is much easier to use, for ND routines that do not need
330 * multiple options of the same type.
333 nd6_options(union nd_opts *ndopts)
335 struct nd_opt_hdr *nd_opt;
339 panic("ndopts == NULL in nd6_options");
340 if (ndopts->nd_opts_last == NULL)
341 panic("uninitialized ndopts in nd6_options");
342 if (ndopts->nd_opts_search == NULL)
346 nd_opt = nd6_option(ndopts);
347 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
349 * Message validation requires that all included
350 * options have a length that is greater than zero.
352 ICMP6STAT_INC(icp6s_nd_badopt);
353 bzero(ndopts, sizeof(*ndopts));
360 switch (nd_opt->nd_opt_type) {
361 case ND_OPT_SOURCE_LINKADDR:
362 case ND_OPT_TARGET_LINKADDR:
364 case ND_OPT_REDIRECTED_HEADER:
365 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
367 "duplicated ND6 option found (type=%d)\n",
368 nd_opt->nd_opt_type));
371 ndopts->nd_opt_array[nd_opt->nd_opt_type]
375 case ND_OPT_PREFIX_INFORMATION:
376 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
377 ndopts->nd_opt_array[nd_opt->nd_opt_type]
380 ndopts->nd_opts_pi_end =
381 (struct nd_opt_prefix_info *)nd_opt;
385 * Unknown options must be silently ignored,
386 * to accomodate future extension to the protocol.
389 "nd6_options: unsupported option %d - "
390 "option ignored\n", nd_opt->nd_opt_type));
395 if (i > V_nd6_maxndopt) {
396 ICMP6STAT_INC(icp6s_nd_toomanyopt);
397 nd6log((LOG_INFO, "too many loop in nd opt\n"));
401 if (ndopts->nd_opts_done)
409 * ND6 timer routine to handle ND6 entries
412 nd6_llinfo_settimer_locked(struct llentry *ln, long tick)
416 LLE_WLOCK_ASSERT(ln);
421 canceled = callout_stop(&ln->ln_timer_ch);
423 ln->la_expire = time_second + tick / hz;
425 if (tick > INT_MAX) {
426 ln->ln_ntick = tick - INT_MAX;
427 canceled = callout_reset(&ln->ln_timer_ch, INT_MAX,
428 nd6_llinfo_timer, ln);
431 canceled = callout_reset(&ln->ln_timer_ch, tick,
432 nd6_llinfo_timer, ln);
440 nd6_llinfo_settimer(struct llentry *ln, long tick)
444 nd6_llinfo_settimer_locked(ln, tick);
449 nd6_llinfo_timer(void *arg)
452 struct in6_addr *dst;
454 struct nd_ifinfo *ndi = NULL;
456 KASSERT(arg != NULL, ("%s: arg NULL", __func__));
457 ln = (struct llentry *)arg;
458 LLE_WLOCK_ASSERT(ln);
459 ifp = ln->lle_tbl->llt_ifp;
461 CURVNET_SET(ifp->if_vnet);
463 if (ln->ln_ntick > 0) {
464 if (ln->ln_ntick > INT_MAX) {
465 ln->ln_ntick -= INT_MAX;
466 nd6_llinfo_settimer_locked(ln, INT_MAX);
469 nd6_llinfo_settimer_locked(ln, ln->ln_ntick);
474 ndi = ND_IFINFO(ifp);
475 dst = &L3_ADDR_SIN6(ln)->sin6_addr;
476 if (ln->la_flags & LLE_STATIC) {
480 if (ln->la_flags & LLE_DELETED) {
481 (void)nd6_free(ln, 0);
486 switch (ln->ln_state) {
487 case ND6_LLINFO_INCOMPLETE:
488 if (ln->la_asked < V_nd6_mmaxtries) {
490 nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
492 nd6_ns_output(ifp, NULL, dst, ln, 0);
495 struct mbuf *m = ln->la_hold;
500 * assuming every packet in la_hold has the
501 * same IP header. Send error after unlock.
506 clear_llinfo_pqueue(ln);
508 (void)nd6_free(ln, 0);
511 icmp6_error2(m, ICMP6_DST_UNREACH,
512 ICMP6_DST_UNREACH_ADDR, 0, ifp);
515 case ND6_LLINFO_REACHABLE:
516 if (!ND6_LLINFO_PERMANENT(ln)) {
517 ln->ln_state = ND6_LLINFO_STALE;
518 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
522 case ND6_LLINFO_STALE:
523 /* Garbage Collection(RFC 2461 5.3) */
524 if (!ND6_LLINFO_PERMANENT(ln)) {
525 (void)nd6_free(ln, 1);
530 case ND6_LLINFO_DELAY:
531 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
534 ln->ln_state = ND6_LLINFO_PROBE;
535 nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
537 nd6_ns_output(ifp, dst, dst, ln, 0);
540 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
541 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
544 case ND6_LLINFO_PROBE:
545 if (ln->la_asked < V_nd6_umaxtries) {
547 nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
549 nd6_ns_output(ifp, dst, dst, ln, 0);
552 (void)nd6_free(ln, 0);
557 panic("%s: paths in a dark night can be confusing: %d",
558 __func__, ln->ln_state);
568 * ND6 timer routine to expire default route list and prefix list
573 CURVNET_SET((struct vnet *) arg);
575 struct nd_defrouter *dr;
576 struct nd_prefix *pr;
577 struct in6_ifaddr *ia6, *nia6;
578 struct in6_addrlifetime *lt6;
580 callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz,
583 /* expire default router list */
585 dr = TAILQ_FIRST(&V_nd_defrouter);
587 if (dr->expire && dr->expire < time_second) {
588 struct nd_defrouter *t;
589 t = TAILQ_NEXT(dr, dr_entry);
593 dr = TAILQ_NEXT(dr, dr_entry);
598 * expire interface addresses.
599 * in the past the loop was inside prefix expiry processing.
600 * However, from a stricter speci-confrmance standpoint, we should
601 * rather separate address lifetimes and prefix lifetimes.
603 * XXXRW: in6_ifaddrhead locking.
606 TAILQ_FOREACH_SAFE(ia6, &V_in6_ifaddrhead, ia_link, nia6) {
607 /* check address lifetime */
608 lt6 = &ia6->ia6_lifetime;
609 if (IFA6_IS_INVALID(ia6)) {
613 * If the expiring address is temporary, try
614 * regenerating a new one. This would be useful when
615 * we suspended a laptop PC, then turned it on after a
616 * period that could invalidate all temporary
617 * addresses. Although we may have to restart the
618 * loop (see below), it must be after purging the
619 * address. Otherwise, we'd see an infinite loop of
622 if (V_ip6_use_tempaddr &&
623 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
624 if (regen_tmpaddr(ia6) == 0)
628 in6_purgeaddr(&ia6->ia_ifa);
631 goto addrloop; /* XXX: see below */
632 } else if (IFA6_IS_DEPRECATED(ia6)) {
633 int oldflags = ia6->ia6_flags;
635 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
638 * If a temporary address has just become deprecated,
639 * regenerate a new one if possible.
641 if (V_ip6_use_tempaddr &&
642 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
643 (oldflags & IN6_IFF_DEPRECATED) == 0) {
645 if (regen_tmpaddr(ia6) == 0) {
647 * A new temporary address is
649 * XXX: this means the address chain
650 * has changed while we are still in
651 * the loop. Although the change
652 * would not cause disaster (because
653 * it's not a deletion, but an
654 * addition,) we'd rather restart the
655 * loop just for safety. Or does this
656 * significantly reduce performance??
663 * A new RA might have made a deprecated address
666 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
670 /* expire prefix list */
671 pr = V_nd_prefix.lh_first;
674 * check prefix lifetime.
675 * since pltime is just for autoconf, pltime processing for
676 * prefix is not necessary.
678 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
679 time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
684 * address expiration and prefix expiration are
685 * separate. NEVER perform in6_purgeaddr here.
698 * ia6 - deprecated/invalidated temporary address
701 regen_tmpaddr(struct in6_ifaddr *ia6)
705 struct in6_ifaddr *public_ifa6 = NULL;
707 ifp = ia6->ia_ifa.ifa_ifp;
709 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
710 struct in6_ifaddr *it6;
712 if (ifa->ifa_addr->sa_family != AF_INET6)
715 it6 = (struct in6_ifaddr *)ifa;
717 /* ignore no autoconf addresses. */
718 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
721 /* ignore autoconf addresses with different prefixes. */
722 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
726 * Now we are looking at an autoconf address with the same
727 * prefix as ours. If the address is temporary and is still
728 * preferred, do not create another one. It would be rare, but
729 * could happen, for example, when we resume a laptop PC after
732 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
733 !IFA6_IS_DEPRECATED(it6)) {
739 * This is a public autoconf address that has the same prefix
740 * as ours. If it is preferred, keep it. We can't break the
741 * loop here, because there may be a still-preferred temporary
742 * address with the prefix.
744 if (!IFA6_IS_DEPRECATED(it6))
747 if (public_ifa6 != NULL)
748 ifa_ref(&public_ifa6->ia_ifa);
752 if (public_ifa6 != NULL) {
755 if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
756 ifa_free(&public_ifa6->ia_ifa);
757 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
758 " tmp addr,errno=%d\n", e);
761 ifa_free(&public_ifa6->ia_ifa);
769 * Nuke neighbor cache/prefix/default router management table, right before
773 nd6_purge(struct ifnet *ifp)
775 struct nd_defrouter *dr, *ndr;
776 struct nd_prefix *pr, *npr;
779 * Nuke default router list entries toward ifp.
780 * We defer removal of default router list entries that is installed
781 * in the routing table, in order to keep additional side effects as
784 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = ndr) {
785 ndr = TAILQ_NEXT(dr, dr_entry);
793 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = ndr) {
794 ndr = TAILQ_NEXT(dr, dr_entry);
802 /* Nuke prefix list entries toward ifp */
803 for (pr = V_nd_prefix.lh_first; pr; pr = npr) {
805 if (pr->ndpr_ifp == ifp) {
807 * Because if_detach() does *not* release prefixes
808 * while purging addresses the reference count will
809 * still be above zero. We therefore reset it to
810 * make sure that the prefix really gets purged.
815 * Previously, pr->ndpr_addr is removed as well,
816 * but I strongly believe we don't have to do it.
817 * nd6_purge() is only called from in6_ifdetach(),
818 * which removes all the associated interface addresses
820 * (jinmei@kame.net 20010129)
826 /* cancel default outgoing interface setting */
827 if (V_nd6_defifindex == ifp->if_index)
828 nd6_setdefaultiface(0);
830 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
831 /* Refresh default router list. */
836 * We do not nuke the neighbor cache entries here any more
837 * because the neighbor cache is kept in if_afdata[AF_INET6].
838 * nd6_purge() is invoked by in6_ifdetach() which is called
839 * from if_detach() where everything gets purged. So let
840 * in6_domifdetach() do the actual L2 table purging work.
845 * the caller acquires and releases the lock on the lltbls
846 * Returns the llentry locked
849 nd6_lookup(struct in6_addr *addr6, int flags, struct ifnet *ifp)
851 struct sockaddr_in6 sin6;
855 bzero(&sin6, sizeof(sin6));
856 sin6.sin6_len = sizeof(struct sockaddr_in6);
857 sin6.sin6_family = AF_INET6;
858 sin6.sin6_addr = *addr6;
860 IF_AFDATA_LOCK_ASSERT(ifp);
863 if (flags & ND6_CREATE)
864 llflags |= LLE_CREATE;
865 if (flags & ND6_EXCLUSIVE)
866 llflags |= LLE_EXCLUSIVE;
868 ln = lla_lookup(LLTABLE6(ifp), llflags, (struct sockaddr *)&sin6);
869 if ((ln != NULL) && (llflags & LLE_CREATE))
870 ln->ln_state = ND6_LLINFO_NOSTATE;
876 * Test whether a given IPv6 address is a neighbor or not, ignoring
877 * the actual neighbor cache. The neighbor cache is ignored in order
878 * to not reenter the routing code from within itself.
881 nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
883 struct nd_prefix *pr;
884 struct ifaddr *dstaddr;
887 * A link-local address is always a neighbor.
888 * XXX: a link does not necessarily specify a single interface.
890 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
891 struct sockaddr_in6 sin6_copy;
895 * We need sin6_copy since sa6_recoverscope() may modify the
899 if (sa6_recoverscope(&sin6_copy))
900 return (0); /* XXX: should be impossible */
901 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
903 if (sin6_copy.sin6_scope_id == zone)
910 * If the address matches one of our addresses,
911 * it should be a neighbor.
912 * If the address matches one of our on-link prefixes, it should be a
915 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
916 if (pr->ndpr_ifp != ifp)
919 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
921 rt = rtalloc1((struct sockaddr *)&pr->ndpr_prefix, 0, 0);
925 * This is the case where multiple interfaces
926 * have the same prefix, but only one is installed
927 * into the routing table and that prefix entry
928 * is not the one being examined here. In the case
929 * where RADIX_MPATH is enabled, multiple route
930 * entries (of the same rt_key value) will be
931 * installed because the interface addresses all
934 if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
935 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr)) {
942 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
943 &addr->sin6_addr, &pr->ndpr_mask))
948 * If the address is assigned on the node of the other side of
949 * a p2p interface, the address should be a neighbor.
951 dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr);
952 if (dstaddr != NULL) {
953 if (dstaddr->ifa_ifp == ifp) {
961 * If the default router list is empty, all addresses are regarded
962 * as on-link, and thus, as a neighbor.
964 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV &&
965 TAILQ_FIRST(&V_nd_defrouter) == NULL &&
966 V_nd6_defifindex == ifp->if_index) {
975 * Detect if a given IPv6 address identifies a neighbor on a given link.
976 * XXX: should take care of the destination of a p2p link?
979 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
984 IF_AFDATA_UNLOCK_ASSERT(ifp);
985 if (nd6_is_new_addr_neighbor(addr, ifp))
989 * Even if the address matches none of our addresses, it might be
990 * in the neighbor cache.
993 if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) {
997 IF_AFDATA_UNLOCK(ifp);
1002 * Free an nd6 llinfo entry.
1003 * Since the function would cause significant changes in the kernel, DO NOT
1004 * make it global, unless you have a strong reason for the change, and are sure
1005 * that the change is safe.
1007 static struct llentry *
1008 nd6_free(struct llentry *ln, int gc)
1010 struct llentry *next;
1011 struct nd_defrouter *dr;
1014 LLE_WLOCK_ASSERT(ln);
1017 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1018 * even though it is not harmful, it was not really necessary.
1022 nd6_llinfo_settimer_locked(ln, -1);
1024 ifp = ln->lle_tbl->llt_ifp;
1026 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1027 dr = defrouter_lookup(&L3_ADDR_SIN6(ln)->sin6_addr, ifp);
1029 if (dr != NULL && dr->expire &&
1030 ln->ln_state == ND6_LLINFO_STALE && gc) {
1032 * If the reason for the deletion is just garbage
1033 * collection, and the neighbor is an active default
1034 * router, do not delete it. Instead, reset the GC
1035 * timer using the router's lifetime.
1036 * Simply deleting the entry would affect default
1037 * router selection, which is not necessarily a good
1038 * thing, especially when we're using router preference
1040 * XXX: the check for ln_state would be redundant,
1041 * but we intentionally keep it just in case.
1043 if (dr->expire > time_second)
1044 nd6_llinfo_settimer_locked(ln,
1045 (dr->expire - time_second) * hz);
1047 nd6_llinfo_settimer_locked(ln,
1048 (long)V_nd6_gctimer * hz);
1050 next = LIST_NEXT(ln, lle_next);
1058 * Unreachablity of a router might affect the default
1059 * router selection and on-link detection of advertised
1064 * Temporarily fake the state to choose a new default
1065 * router and to perform on-link determination of
1066 * prefixes correctly.
1067 * Below the state will be set correctly,
1068 * or the entry itself will be deleted.
1070 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1073 if (ln->ln_router || dr) {
1076 * We need to unlock to avoid a LOR with rt6_flush() with the
1077 * rnh and for the calls to pfxlist_onlink_check() and
1078 * defrouter_select() in the block further down for calls
1079 * into nd6_lookup(). We still hold a ref.
1084 * rt6_flush must be called whether or not the neighbor
1085 * is in the Default Router List.
1086 * See a corresponding comment in nd6_na_input().
1088 rt6_flush(&L3_ADDR_SIN6(ln)->sin6_addr, ifp);
1093 * Since defrouter_select() does not affect the
1094 * on-link determination and MIP6 needs the check
1095 * before the default router selection, we perform
1098 pfxlist_onlink_check();
1101 * Refresh default router list.
1106 if (ln->ln_router || dr)
1111 * Before deleting the entry, remember the next entry as the
1112 * return value. We need this because pfxlist_onlink_check() above
1113 * might have freed other entries (particularly the old next entry) as
1114 * a side effect (XXX).
1116 next = LIST_NEXT(ln, lle_next);
1119 * Save to unlock. We still hold an extra reference and will not
1120 * free(9) in llentry_free() if someone else holds one as well.
1123 IF_AFDATA_LOCK(ifp);
1127 IF_AFDATA_UNLOCK(ifp);
1133 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1135 * XXX cost-effective methods?
1138 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1143 if ((dst6 == NULL) || (rt == NULL))
1147 IF_AFDATA_LOCK(ifp);
1148 ln = nd6_lookup(dst6, ND6_EXCLUSIVE, NULL);
1149 IF_AFDATA_UNLOCK(ifp);
1153 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1157 * if we get upper-layer reachability confirmation many times,
1158 * it is possible we have false information.
1162 if (ln->ln_byhint > V_nd6_maxnudhint) {
1167 ln->ln_state = ND6_LLINFO_REACHABLE;
1168 if (!ND6_LLINFO_PERMANENT(ln)) {
1169 nd6_llinfo_settimer_locked(ln,
1170 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1178 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1180 struct in6_drlist *drl = (struct in6_drlist *)data;
1181 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1182 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1183 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1184 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1185 struct nd_defrouter *dr;
1186 struct nd_prefix *pr;
1187 int i = 0, error = 0;
1191 case SIOCGDRLST_IN6:
1193 * obsolete API, use sysctl under net.inet6.icmp6
1195 bzero(drl, sizeof(*drl));
1197 dr = TAILQ_FIRST(&V_nd_defrouter);
1198 while (dr && i < DRLSTSIZ) {
1199 drl->defrouter[i].rtaddr = dr->rtaddr;
1200 in6_clearscope(&drl->defrouter[i].rtaddr);
1202 drl->defrouter[i].flags = dr->flags;
1203 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1204 drl->defrouter[i].expire = dr->expire;
1205 drl->defrouter[i].if_index = dr->ifp->if_index;
1207 dr = TAILQ_NEXT(dr, dr_entry);
1211 case SIOCGPRLST_IN6:
1213 * obsolete API, use sysctl under net.inet6.icmp6
1215 * XXX the structure in6_prlist was changed in backward-
1216 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1217 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1220 * XXX meaning of fields, especialy "raflags", is very
1221 * differnet between RA prefix list and RR/static prefix list.
1222 * how about separating ioctls into two?
1224 bzero(oprl, sizeof(*oprl));
1226 pr = V_nd_prefix.lh_first;
1227 while (pr && i < PRLSTSIZ) {
1228 struct nd_pfxrouter *pfr;
1231 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1232 oprl->prefix[i].raflags = pr->ndpr_raf;
1233 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1234 oprl->prefix[i].vltime = pr->ndpr_vltime;
1235 oprl->prefix[i].pltime = pr->ndpr_pltime;
1236 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1237 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1238 oprl->prefix[i].expire = 0;
1242 /* XXX: we assume time_t is signed. */
1245 ((sizeof(maxexpire) * 8) - 1));
1246 if (pr->ndpr_vltime <
1247 maxexpire - pr->ndpr_lastupdate) {
1248 oprl->prefix[i].expire =
1249 pr->ndpr_lastupdate +
1252 oprl->prefix[i].expire = maxexpire;
1255 pfr = pr->ndpr_advrtrs.lh_first;
1259 #define RTRADDR oprl->prefix[i].advrtr[j]
1260 RTRADDR = pfr->router->rtaddr;
1261 in6_clearscope(&RTRADDR);
1265 pfr = pfr->pfr_next;
1267 oprl->prefix[i].advrtrs = j;
1268 oprl->prefix[i].origin = PR_ORIG_RA;
1276 case OSIOCGIFINFO_IN6:
1278 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1279 bzero(&ND, sizeof(ND));
1280 ND.linkmtu = IN6_LINKMTU(ifp);
1281 ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1282 ND.basereachable = ND_IFINFO(ifp)->basereachable;
1283 ND.reachable = ND_IFINFO(ifp)->reachable;
1284 ND.retrans = ND_IFINFO(ifp)->retrans;
1285 ND.flags = ND_IFINFO(ifp)->flags;
1286 ND.recalctm = ND_IFINFO(ifp)->recalctm;
1287 ND.chlim = ND_IFINFO(ifp)->chlim;
1289 case SIOCGIFINFO_IN6:
1290 ND = *ND_IFINFO(ifp);
1292 case SIOCSIFINFO_IN6:
1294 * used to change host variables from userland.
1295 * intented for a use on router to reflect RA configurations.
1297 /* 0 means 'unspecified' */
1298 if (ND.linkmtu != 0) {
1299 if (ND.linkmtu < IPV6_MMTU ||
1300 ND.linkmtu > IN6_LINKMTU(ifp)) {
1304 ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1307 if (ND.basereachable != 0) {
1308 int obasereachable = ND_IFINFO(ifp)->basereachable;
1310 ND_IFINFO(ifp)->basereachable = ND.basereachable;
1311 if (ND.basereachable != obasereachable)
1312 ND_IFINFO(ifp)->reachable =
1313 ND_COMPUTE_RTIME(ND.basereachable);
1315 if (ND.retrans != 0)
1316 ND_IFINFO(ifp)->retrans = ND.retrans;
1318 ND_IFINFO(ifp)->chlim = ND.chlim;
1320 case SIOCSIFINFO_FLAGS:
1323 struct in6_ifaddr *ia;
1326 * Try to clear ifdisabled flag when enabling
1327 * accept_rtadv or auto_linklocal.
1329 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1330 !(ND.flags & ND6_IFF_IFDISABLED) &&
1331 (ND.flags & (ND6_IFF_ACCEPT_RTADV |
1332 ND6_IFF_AUTO_LINKLOCAL)))
1333 ND.flags &= ~ND6_IFF_IFDISABLED;
1335 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1336 !(ND.flags & ND6_IFF_IFDISABLED)) {
1337 /* ifdisabled 1->0 transision */
1340 * If the interface is marked as ND6_IFF_IFDISABLED and
1341 * has an link-local address with IN6_IFF_DUPLICATED,
1342 * do not clear ND6_IFF_IFDISABLED.
1343 * See RFC 4862, Section 5.4.5.
1345 int duplicated_linklocal = 0;
1348 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1349 if (ifa->ifa_addr->sa_family != AF_INET6)
1351 ia = (struct in6_ifaddr *)ifa;
1352 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1353 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) {
1354 duplicated_linklocal = 1;
1358 IF_ADDR_UNLOCK(ifp);
1360 if (duplicated_linklocal) {
1361 ND.flags |= ND6_IFF_IFDISABLED;
1362 log(LOG_ERR, "Cannot enable an interface"
1363 " with a link-local address marked"
1366 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
1369 } else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1370 (ND.flags & ND6_IFF_IFDISABLED)) {
1371 /* ifdisabled 0->1 transision */
1372 /* Mark all IPv6 address as tentative. */
1374 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1376 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1377 if (ifa->ifa_addr->sa_family != AF_INET6)
1379 ia = (struct in6_ifaddr *)ifa;
1380 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1382 IF_ADDR_UNLOCK(ifp);
1385 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) &&
1386 (ND.flags & ND6_IFF_AUTO_LINKLOCAL)) {
1387 /* auto_linklocal 0->1 transision */
1389 /* If no link-local address on ifp, configure */
1390 ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
1391 in6_ifattach(ifp, NULL);
1392 } else if ((ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) &&
1393 !(ND.flags & ND6_IFF_IFDISABLED)) {
1395 * When the IF already has
1396 * ND6_IFF_AUTO_LINKLOCAL and no link-local
1397 * address is assigned, try to assign one.
1399 int haslinklocal = 0;
1402 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1403 if (ifa->ifa_addr->sa_family != AF_INET6)
1405 ia = (struct in6_ifaddr *)ifa;
1406 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) {
1411 IF_ADDR_UNLOCK(ifp);
1413 in6_ifattach(ifp, NULL);
1416 ND_IFINFO(ifp)->flags = ND.flags;
1419 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1420 /* sync kernel routing table with the default router list */
1424 case SIOCSPFXFLUSH_IN6:
1426 /* flush all the prefix advertised by routers */
1427 struct nd_prefix *pr, *next;
1430 for (pr = V_nd_prefix.lh_first; pr; pr = next) {
1431 struct in6_ifaddr *ia, *ia_next;
1433 next = pr->ndpr_next;
1435 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1438 /* do we really have to remove addresses as well? */
1439 /* XXXRW: in6_ifaddrhead locking. */
1440 TAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link,
1442 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1445 if (ia->ia6_ndpr == pr)
1446 in6_purgeaddr(&ia->ia_ifa);
1453 case SIOCSRTRFLUSH_IN6:
1455 /* flush all the default routers */
1456 struct nd_defrouter *dr, *next;
1460 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = next) {
1461 next = TAILQ_NEXT(dr, dr_entry);
1468 case SIOCGNBRINFO_IN6:
1471 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1473 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1476 IF_AFDATA_LOCK(ifp);
1477 ln = nd6_lookup(&nb_addr, 0, ifp);
1478 IF_AFDATA_UNLOCK(ifp);
1484 nbi->state = ln->ln_state;
1485 nbi->asked = ln->la_asked;
1486 nbi->isrouter = ln->ln_router;
1487 nbi->expire = ln->la_expire;
1491 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1492 ndif->ifindex = V_nd6_defifindex;
1494 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1495 return (nd6_setdefaultiface(ndif->ifindex));
1501 * Create neighbor cache entry and cache link-layer address,
1502 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1505 * code - type dependent information
1508 * The caller of this function already acquired the ndp
1509 * cache table lock because the cache entry is returned.
1512 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1513 int lladdrlen, int type, int code)
1515 struct llentry *ln = NULL;
1522 uint16_t router = 0;
1523 struct sockaddr_in6 sin6;
1524 struct mbuf *chain = NULL;
1525 int static_route = 0;
1527 IF_AFDATA_UNLOCK_ASSERT(ifp);
1530 panic("ifp == NULL in nd6_cache_lladdr");
1532 panic("from == NULL in nd6_cache_lladdr");
1534 /* nothing must be updated for unspecified address */
1535 if (IN6_IS_ADDR_UNSPECIFIED(from))
1539 * Validation about ifp->if_addrlen and lladdrlen must be done in
1542 * XXX If the link does not have link-layer adderss, what should
1543 * we do? (ifp->if_addrlen == 0)
1544 * Spec says nothing in sections for RA, RS and NA. There's small
1545 * description on it in NS section (RFC 2461 7.2.3).
1547 flags = lladdr ? ND6_EXCLUSIVE : 0;
1548 IF_AFDATA_LOCK(ifp);
1549 ln = nd6_lookup(from, flags, ifp);
1552 flags |= ND6_EXCLUSIVE;
1553 ln = nd6_lookup(from, flags | ND6_CREATE, ifp);
1554 IF_AFDATA_UNLOCK(ifp);
1557 IF_AFDATA_UNLOCK(ifp);
1558 /* do nothing if static ndp is set */
1559 if (ln->la_flags & LLE_STATIC) {
1568 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
1569 if (olladdr && lladdr) {
1570 llchange = bcmp(lladdr, &ln->ll_addr,
1576 * newentry olladdr lladdr llchange (*=record)
1579 * 0 n y -- (3) * STALE
1581 * 0 y y y (5) * STALE
1582 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1583 * 1 -- y -- (7) * STALE
1586 if (lladdr) { /* (3-5) and (7) */
1588 * Record source link-layer address
1589 * XXX is it dependent to ifp->if_type?
1591 bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
1592 ln->la_flags |= LLE_VALID;
1596 if ((!olladdr && lladdr != NULL) || /* (3) */
1597 (olladdr && lladdr != NULL && llchange)) { /* (5) */
1599 newstate = ND6_LLINFO_STALE;
1600 } else /* (1-2,4) */
1604 if (lladdr == NULL) /* (6) */
1605 newstate = ND6_LLINFO_NOSTATE;
1607 newstate = ND6_LLINFO_STALE;
1612 * Update the state of the neighbor cache.
1614 ln->ln_state = newstate;
1616 if (ln->ln_state == ND6_LLINFO_STALE) {
1618 * XXX: since nd6_output() below will cause
1619 * state tansition to DELAY and reset the timer,
1620 * we must set the timer now, although it is actually
1623 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
1626 struct mbuf *m_hold, *m_hold_next;
1629 * reset the la_hold in advance, to explicitly
1630 * prevent a la_hold lookup in nd6_output()
1631 * (wouldn't happen, though...)
1633 for (m_hold = ln->la_hold, ln->la_hold = NULL;
1634 m_hold; m_hold = m_hold_next) {
1635 m_hold_next = m_hold->m_nextpkt;
1636 m_hold->m_nextpkt = NULL;
1639 * we assume ifp is not a p2p here, so
1640 * just set the 2nd argument as the
1643 nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
1646 * If we have mbufs in the chain we need to do
1647 * deferred transmit. Copy the address from the
1648 * llentry before dropping the lock down below.
1651 memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
1653 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1654 /* probe right away */
1655 nd6_llinfo_settimer_locked((void *)ln, 0);
1660 * ICMP6 type dependent behavior.
1662 * NS: clear IsRouter if new entry
1663 * RS: clear IsRouter
1664 * RA: set IsRouter if there's lladdr
1665 * redir: clear IsRouter if new entry
1668 * The spec says that we must set IsRouter in the following cases:
1669 * - If lladdr exist, set IsRouter. This means (1-5).
1670 * - If it is old entry (!newentry), set IsRouter. This means (7).
1671 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1672 * A quetion arises for (1) case. (1) case has no lladdr in the
1673 * neighbor cache, this is similar to (6).
1674 * This case is rare but we figured that we MUST NOT set IsRouter.
1676 * newentry olladdr lladdr llchange NS RS RA redir
1678 * 0 n n -- (1) c ? s
1679 * 0 y n -- (2) c s s
1680 * 0 n y -- (3) c s s
1683 * 1 -- n -- (6) c c c s
1684 * 1 -- y -- (7) c c s c s
1688 switch (type & 0xff) {
1689 case ND_NEIGHBOR_SOLICIT:
1691 * New entry must have is_router flag cleared.
1693 if (is_newentry) /* (6-7) */
1698 * If the icmp is a redirect to a better router, always set the
1699 * is_router flag. Otherwise, if the entry is newly created,
1700 * clear the flag. [RFC 2461, sec 8.3]
1702 if (code == ND_REDIRECT_ROUTER)
1704 else if (is_newentry) /* (6-7) */
1707 case ND_ROUTER_SOLICIT:
1709 * is_router flag must always be cleared.
1713 case ND_ROUTER_ADVERT:
1715 * Mark an entry with lladdr as a router.
1717 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1718 (is_newentry && lladdr)) { /* (7) */
1725 static_route = (ln->la_flags & LLE_STATIC);
1726 router = ln->ln_router;
1728 if (flags & ND6_EXCLUSIVE)
1736 nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
1739 * When the link-layer address of a router changes, select the
1740 * best router again. In particular, when the neighbor entry is newly
1741 * created, it might affect the selection policy.
1742 * Question: can we restrict the first condition to the "is_newentry"
1744 * XXX: when we hear an RA from a new router with the link-layer
1745 * address option, defrouter_select() is called twice, since
1746 * defrtrlist_update called the function as well. However, I believe
1747 * we can compromise the overhead, since it only happens the first
1749 * XXX: although defrouter_select() should not have a bad effect
1750 * for those are not autoconfigured hosts, we explicitly avoid such
1753 if (do_update && router &&
1754 ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1756 * guaranteed recursion
1764 if (flags & ND6_EXCLUSIVE)
1775 nd6_slowtimo(void *arg)
1777 CURVNET_SET((struct vnet *) arg);
1778 struct nd_ifinfo *nd6if;
1781 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1782 nd6_slowtimo, curvnet);
1783 IFNET_RLOCK_NOSLEEP();
1784 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
1785 ifp = TAILQ_NEXT(ifp, if_list)) {
1786 nd6if = ND_IFINFO(ifp);
1787 if (nd6if->basereachable && /* already initialized */
1788 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1790 * Since reachable time rarely changes by router
1791 * advertisements, we SHOULD insure that a new random
1792 * value gets recomputed at least once every few hours.
1795 nd6if->recalctm = V_nd6_recalc_reachtm_interval;
1796 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1799 IFNET_RUNLOCK_NOSLEEP();
1804 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1805 struct sockaddr_in6 *dst, struct rtentry *rt0)
1808 return (nd6_output_lle(ifp, origifp, m0, dst, rt0, NULL, NULL));
1813 * Note that I'm not enforcing any global serialization
1814 * lle state or asked changes here as the logic is too
1815 * complicated to avoid having to always acquire an exclusive
1820 #define senderr(e) { error = (e); goto bad;}
1823 nd6_output_lle(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1824 struct sockaddr_in6 *dst, struct rtentry *rt0, struct llentry *lle,
1825 struct mbuf **chain)
1827 struct mbuf *m = m0;
1829 struct llentry *ln = lle;
1830 struct ip6_hdr *ip6;
1838 LLE_WLOCK_ASSERT(lle);
1840 KASSERT(chain != NULL, (" lle locked but no mbuf chain pointer passed"));
1843 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1846 if (nd6_need_cache(ifp) == 0)
1850 * next hop determination. This routine is derived from ether_output.
1854 * Address resolution or Neighbor Unreachability Detection
1856 * At this point, the destination of the packet must be a unicast
1857 * or an anycast address(i.e. not a multicast).
1860 flags = ((m != NULL) || (lle != NULL)) ? LLE_EXCLUSIVE : 0;
1863 IF_AFDATA_LOCK(ifp);
1864 ln = lla_lookup(LLTABLE6(ifp), flags, (struct sockaddr *)dst);
1865 IF_AFDATA_UNLOCK(ifp);
1866 if ((ln == NULL) && nd6_is_addr_neighbor(dst, ifp)) {
1868 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1869 * the condition below is not very efficient. But we believe
1870 * it is tolerable, because this should be a rare case.
1872 flags = ND6_CREATE | (m ? ND6_EXCLUSIVE : 0);
1873 IF_AFDATA_LOCK(ifp);
1874 ln = nd6_lookup(&dst->sin6_addr, flags, ifp);
1875 IF_AFDATA_UNLOCK(ifp);
1879 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1880 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1881 char ip6buf[INET6_ADDRSTRLEN];
1883 "nd6_output: can't allocate llinfo for %s "
1885 ip6_sprintf(ip6buf, &dst->sin6_addr), ln);
1886 senderr(EIO); /* XXX: good error? */
1888 goto sendpkt; /* send anyway */
1891 /* We don't have to do link-layer address resolution on a p2p link. */
1892 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1893 ln->ln_state < ND6_LLINFO_REACHABLE) {
1894 if ((flags & LLE_EXCLUSIVE) == 0) {
1895 flags |= LLE_EXCLUSIVE;
1898 ln->ln_state = ND6_LLINFO_STALE;
1899 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
1903 * The first time we send a packet to a neighbor whose entry is
1904 * STALE, we have to change the state to DELAY and a sets a timer to
1905 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1906 * neighbor unreachability detection on expiration.
1909 if (ln->ln_state == ND6_LLINFO_STALE) {
1910 if ((flags & LLE_EXCLUSIVE) == 0) {
1911 flags |= LLE_EXCLUSIVE;
1916 ln->ln_state = ND6_LLINFO_DELAY;
1917 nd6_llinfo_settimer_locked(ln, (long)V_nd6_delay * hz);
1921 * If the neighbor cache entry has a state other than INCOMPLETE
1922 * (i.e. its link-layer address is already resolved), just
1925 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1929 * There is a neighbor cache entry, but no ethernet address
1930 * response yet. Append this latest packet to the end of the
1931 * packet queue in the mbuf, unless the number of the packet
1932 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen,
1933 * the oldest packet in the queue will be removed.
1935 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1936 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1938 if ((flags & LLE_EXCLUSIVE) == 0) {
1939 flags |= LLE_EXCLUSIVE;
1944 LLE_WLOCK_ASSERT(ln);
1947 struct mbuf *m_hold;
1951 for (m_hold = ln->la_hold; m_hold; m_hold = m_hold->m_nextpkt) {
1953 if (m_hold->m_nextpkt == NULL) {
1954 m_hold->m_nextpkt = m;
1958 while (i >= V_nd6_maxqueuelen) {
1959 m_hold = ln->la_hold;
1960 ln->la_hold = ln->la_hold->m_nextpkt;
1969 * If there has been no NS for the neighbor after entering the
1970 * INCOMPLETE state, send the first solicitation.
1972 if (!ND6_LLINFO_PERMANENT(ln) && ln->la_asked == 0) {
1975 nd6_llinfo_settimer_locked(ln,
1976 (long)ND_IFINFO(ifp)->retrans * hz / 1000);
1978 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1979 if (lle != NULL && ln == lle)
1982 } else if (lle == NULL || ln != lle) {
1984 * We did the lookup (no lle arg) so we
1985 * need to do the unlock here.
1993 /* discard the packet if IPv6 operation is disabled on the interface */
1994 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
1995 error = ENETDOWN; /* better error? */
1999 * ln is valid and the caller did not pass in
2002 if ((ln != NULL) && (lle == NULL)) {
2003 if (flags & LLE_EXCLUSIVE)
2010 mac_netinet6_nd6_send(ifp, m);
2014 * If called from nd6_ns_output() (NS), nd6_na_output() (NA),
2015 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA
2016 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND
2017 * to be diverted to user space. When re-injected into the kernel,
2018 * send_output() will directly dispatch them to the outgoing interface.
2020 if (send_sendso_input_hook != NULL) {
2021 mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL);
2023 ip6 = mtod(m, struct ip6_hdr *);
2024 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
2025 /* Use the SEND socket */
2026 error = send_sendso_input_hook(m, ifp, SND_OUT,
2028 /* -1 == no app on SEND socket */
2029 if (error == 0 || error != -1)
2035 * We were passed in a pointer to an lle with the lock held
2036 * this means that we can't call if_output as we will
2037 * recurse on the lle lock - so what we do is we create
2038 * a list of mbufs to send and transmit them in the caller
2039 * after the lock is dropped
2048 * append mbuf to end of deferred chain
2051 while (mb->m_nextpkt != NULL)
2057 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
2058 return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
2061 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, NULL);
2066 * ln is valid and the caller did not pass in
2069 if ((ln != NULL) && (lle == NULL)) {
2070 if (flags & LLE_EXCLUSIVE)
2083 nd6_output_flush(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain,
2084 struct sockaddr_in6 *dst, struct route *ro)
2086 struct mbuf *m, *m_head;
2087 struct ifnet *outifp;
2091 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2098 m_head = m_head->m_nextpkt;
2099 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, ro);
2104 * note that intermediate errors are blindly ignored - but this is
2105 * the same convention as used with nd6_output when called by
2113 nd6_need_cache(struct ifnet *ifp)
2116 * XXX: we currently do not make neighbor cache on any interface
2117 * other than ARCnet, Ethernet, FDDI and GIF.
2120 * - unidirectional tunnels needs no ND
2122 switch (ifp->if_type) {
2130 #ifdef IFT_IEEE80211
2136 case IFT_INFINIBAND:
2137 case IFT_GIF: /* XXX need more cases? */
2141 case IFT_PROPVIRTUAL:
2149 * the callers of this function need to be re-worked to drop
2150 * the lle lock, drop here for now
2153 nd6_storelladdr(struct ifnet *ifp, struct mbuf *m,
2154 struct sockaddr *dst, u_char *desten, struct llentry **lle)
2159 IF_AFDATA_UNLOCK_ASSERT(ifp);
2160 if (m->m_flags & M_MCAST) {
2163 switch (ifp->if_type) {
2169 #ifdef IFT_IEEE80211
2174 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2179 * netbsd can use if_broadcastaddr, but we don't do so
2180 * to reduce # of ifdef.
2182 for (i = 0; i < ifp->if_addrlen; i++)
2190 return (EAFNOSUPPORT);
2196 * the entry should have been created in nd6_store_lladdr
2198 IF_AFDATA_LOCK(ifp);
2199 ln = lla_lookup(LLTABLE6(ifp), 0, dst);
2200 IF_AFDATA_UNLOCK(ifp);
2201 if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) {
2204 /* this could happen, if we could not allocate memory */
2209 bcopy(&ln->ll_addr, desten, ifp->if_addrlen);
2213 * A *small* use after free race exists here
2219 clear_llinfo_pqueue(struct llentry *ln)
2221 struct mbuf *m_hold, *m_hold_next;
2223 for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) {
2224 m_hold_next = m_hold->m_nextpkt;
2225 m_hold->m_nextpkt = NULL;
2233 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2234 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2236 SYSCTL_DECL(_net_inet6_icmp6);
2238 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2239 CTLFLAG_RD, nd6_sysctl_drlist, "");
2240 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2241 CTLFLAG_RD, nd6_sysctl_prlist, "");
2242 SYSCTL_VNET_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen,
2243 CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, "");
2246 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2249 char buf[1024] __aligned(4);
2250 struct in6_defrouter *d, *de;
2251 struct nd_defrouter *dr;
2257 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr;
2258 dr = TAILQ_NEXT(dr, dr_entry)) {
2259 d = (struct in6_defrouter *)buf;
2260 de = (struct in6_defrouter *)(buf + sizeof(buf));
2263 bzero(d, sizeof(*d));
2264 d->rtaddr.sin6_family = AF_INET6;
2265 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2266 d->rtaddr.sin6_addr = dr->rtaddr;
2267 error = sa6_recoverscope(&d->rtaddr);
2270 d->flags = dr->flags;
2271 d->rtlifetime = dr->rtlifetime;
2272 d->expire = dr->expire;
2273 d->if_index = dr->ifp->if_index;
2275 panic("buffer too short");
2277 error = SYSCTL_OUT(req, buf, sizeof(*d));
2286 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2289 char buf[1024] __aligned(4);
2290 struct in6_prefix *p, *pe;
2291 struct nd_prefix *pr;
2292 char ip6buf[INET6_ADDRSTRLEN];
2298 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2301 struct sockaddr_in6 *sin6, *s6;
2302 struct nd_pfxrouter *pfr;
2304 p = (struct in6_prefix *)buf;
2305 pe = (struct in6_prefix *)(buf + sizeof(buf));
2308 bzero(p, sizeof(*p));
2309 sin6 = (struct sockaddr_in6 *)(p + 1);
2311 p->prefix = pr->ndpr_prefix;
2312 if (sa6_recoverscope(&p->prefix)) {
2314 "scope error in prefix list (%s)\n",
2315 ip6_sprintf(ip6buf, &p->prefix.sin6_addr));
2316 /* XXX: press on... */
2318 p->raflags = pr->ndpr_raf;
2319 p->prefixlen = pr->ndpr_plen;
2320 p->vltime = pr->ndpr_vltime;
2321 p->pltime = pr->ndpr_pltime;
2322 p->if_index = pr->ndpr_ifp->if_index;
2323 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2328 /* XXX: we assume time_t is signed. */
2331 ((sizeof(maxexpire) * 8) - 1));
2332 if (pr->ndpr_vltime <
2333 maxexpire - pr->ndpr_lastupdate) {
2334 p->expire = pr->ndpr_lastupdate +
2337 p->expire = maxexpire;
2339 p->refcnt = pr->ndpr_refcnt;
2340 p->flags = pr->ndpr_stateflags;
2341 p->origin = PR_ORIG_RA;
2343 for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2344 pfr = pfr->pfr_next) {
2345 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2349 s6 = &sin6[advrtrs];
2350 bzero(s6, sizeof(*s6));
2351 s6->sin6_family = AF_INET6;
2352 s6->sin6_len = sizeof(*sin6);
2353 s6->sin6_addr = pfr->router->rtaddr;
2354 if (sa6_recoverscope(s6)) {
2357 "prefix list (%s)\n",
2359 &pfr->router->rtaddr));
2363 p->advrtrs = advrtrs;
2365 panic("buffer too short");
2367 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2368 error = SYSCTL_OUT(req, buf, advance);