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>
54 #include <sys/vimage.h>
57 #include <net/if_arc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/iso88025.h>
62 #include <net/route.h>
65 #include <netinet/in.h>
66 #include <net/if_llatbl.h>
67 #define L3_ADDR_SIN6(le) ((struct sockaddr_in6 *) L3_ADDR(le))
68 #include <netinet/if_ether.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/scope6_var.h>
73 #include <netinet6/nd6.h>
74 #include <netinet/icmp6.h>
75 #include <netinet6/vinet6.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)
94 /* preventing too many loops in ND option parsing */
104 static int nd6_inuse, nd6_allocated;
107 struct nd_drhead nd_defrouter;
108 struct nd_prhead nd_prefix;
110 int nd6_recalc_reachtm_interval;
111 #endif /* VIMAGE_GLOBALS */
113 static struct sockaddr_in6 all1_sa;
115 static int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *,
117 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
118 static void nd6_slowtimo(void *);
119 static int regen_tmpaddr(struct in6_ifaddr *);
120 static struct llentry *nd6_free(struct llentry *, int);
121 static void nd6_llinfo_timer(void *);
122 static void clear_llinfo_pqueue(struct llentry *);
124 #ifdef VIMAGE_GLOBALS
125 struct callout nd6_slowtimo_ch;
126 struct callout nd6_timer_ch;
127 extern struct callout in6_tmpaddrtimer_ch;
128 extern int dad_ignore_ns;
129 extern int dad_maxtry;
135 INIT_VNET_INET6(curvnet);
138 V_nd6_prune = 1; /* walk list every 1 seconds */
139 V_nd6_delay = 5; /* delay first probe time 5 second */
140 V_nd6_umaxtries = 3; /* maximum unicast query */
141 V_nd6_mmaxtries = 3; /* maximum multicast query */
142 V_nd6_useloopback = 1; /* use loopback interface for local traffic */
143 V_nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
145 /* preventing too many loops in ND option parsing */
146 V_nd6_maxndopt = 10; /* max # of ND options allowed */
148 V_nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
149 V_nd6_maxqueuelen = 1; /* max pkts cached in unresolved ND entries */
157 V_nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
159 V_dad_ignore_ns = 0; /* ignore NS in DAD - specwise incorrect*/
160 V_dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
163 * XXX just to get this to compile KMM
166 V_llinfo_nd6.ln_next = &V_llinfo_nd6;
167 V_llinfo_nd6.ln_prev = &V_llinfo_nd6;
169 LIST_INIT(&V_nd_prefix);
171 V_ip6_use_tempaddr = 0;
172 V_ip6_temp_preferred_lifetime = DEF_TEMP_PREFERRED_LIFETIME;
173 V_ip6_temp_valid_lifetime = DEF_TEMP_VALID_LIFETIME;
174 V_ip6_temp_regen_advance = TEMPADDR_REGEN_ADVANCE;
176 V_ip6_desync_factor = 0;
178 all1_sa.sin6_family = AF_INET6;
179 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
180 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
181 all1_sa.sin6_addr.s6_addr[i] = 0xff;
183 /* initialization of the default router list */
184 TAILQ_INIT(&V_nd_defrouter);
186 callout_init(&V_nd6_slowtimo_ch, 0);
187 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
188 nd6_slowtimo, curvnet);
196 INIT_VNET_INET6(curvnet);
198 callout_drain(&V_nd6_slowtimo_ch);
199 callout_drain(&V_nd6_timer_ch);
204 nd6_ifattach(struct ifnet *ifp)
206 struct nd_ifinfo *nd;
208 nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
209 bzero(nd, sizeof(*nd));
213 nd->chlim = IPV6_DEFHLIM;
214 nd->basereachable = REACHABLE_TIME;
215 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
216 nd->retrans = RETRANS_TIMER;
218 * Note that the default value of ip6_accept_rtadv is 0, which means
219 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
222 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
224 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
225 nd6_setmtu0(ifp, nd);
231 nd6_ifdetach(struct nd_ifinfo *nd)
238 * Reset ND level link MTU. This function is called when the physical MTU
239 * changes, which means we might have to adjust the ND level MTU.
242 nd6_setmtu(struct ifnet *ifp)
245 nd6_setmtu0(ifp, ND_IFINFO(ifp));
248 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
250 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
252 INIT_VNET_INET6(ifp->if_vnet);
255 omaxmtu = ndi->maxmtu;
257 switch (ifp->if_type) {
259 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
262 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
265 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
268 ndi->maxmtu = ifp->if_mtu;
273 * Decreasing the interface MTU under IPV6 minimum MTU may cause
274 * undesirable situation. We thus notify the operator of the change
275 * explicitly. The check for omaxmtu is necessary to restrict the
276 * log to the case of changing the MTU, not initializing it.
278 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
279 log(LOG_NOTICE, "nd6_setmtu0: "
280 "new link MTU on %s (%lu) is too small for IPv6\n",
281 if_name(ifp), (unsigned long)ndi->maxmtu);
284 if (ndi->maxmtu > V_in6_maxmtu)
285 in6_setmaxmtu(); /* check all interfaces just in case */
290 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
293 bzero(ndopts, sizeof(*ndopts));
294 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
296 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
299 ndopts->nd_opts_done = 1;
300 ndopts->nd_opts_search = NULL;
305 * Take one ND option.
308 nd6_option(union nd_opts *ndopts)
310 struct nd_opt_hdr *nd_opt;
314 panic("ndopts == NULL in nd6_option");
315 if (ndopts->nd_opts_last == NULL)
316 panic("uninitialized ndopts in nd6_option");
317 if (ndopts->nd_opts_search == NULL)
319 if (ndopts->nd_opts_done)
322 nd_opt = ndopts->nd_opts_search;
324 /* make sure nd_opt_len is inside the buffer */
325 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
326 bzero(ndopts, sizeof(*ndopts));
330 olen = nd_opt->nd_opt_len << 3;
333 * Message validation requires that all included
334 * options have a length that is greater than zero.
336 bzero(ndopts, sizeof(*ndopts));
340 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
341 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
342 /* option overruns the end of buffer, invalid */
343 bzero(ndopts, sizeof(*ndopts));
345 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
346 /* reached the end of options chain */
347 ndopts->nd_opts_done = 1;
348 ndopts->nd_opts_search = NULL;
354 * Parse multiple ND options.
355 * This function is much easier to use, for ND routines that do not need
356 * multiple options of the same type.
359 nd6_options(union nd_opts *ndopts)
361 INIT_VNET_INET6(curvnet);
362 struct nd_opt_hdr *nd_opt;
366 panic("ndopts == NULL in nd6_options");
367 if (ndopts->nd_opts_last == NULL)
368 panic("uninitialized ndopts in nd6_options");
369 if (ndopts->nd_opts_search == NULL)
373 nd_opt = nd6_option(ndopts);
374 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
376 * Message validation requires that all included
377 * options have a length that is greater than zero.
379 ICMP6STAT_INC(icp6s_nd_badopt);
380 bzero(ndopts, sizeof(*ndopts));
387 switch (nd_opt->nd_opt_type) {
388 case ND_OPT_SOURCE_LINKADDR:
389 case ND_OPT_TARGET_LINKADDR:
391 case ND_OPT_REDIRECTED_HEADER:
392 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
394 "duplicated ND6 option found (type=%d)\n",
395 nd_opt->nd_opt_type));
398 ndopts->nd_opt_array[nd_opt->nd_opt_type]
402 case ND_OPT_PREFIX_INFORMATION:
403 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
404 ndopts->nd_opt_array[nd_opt->nd_opt_type]
407 ndopts->nd_opts_pi_end =
408 (struct nd_opt_prefix_info *)nd_opt;
412 * Unknown options must be silently ignored,
413 * to accomodate future extension to the protocol.
416 "nd6_options: unsupported option %d - "
417 "option ignored\n", nd_opt->nd_opt_type));
422 if (i > V_nd6_maxndopt) {
423 ICMP6STAT_INC(icp6s_nd_toomanyopt);
424 nd6log((LOG_INFO, "too many loop in nd opt\n"));
428 if (ndopts->nd_opts_done)
436 * ND6 timer routine to handle ND6 entries
439 nd6_llinfo_settimer_locked(struct llentry *ln, long tick)
444 callout_stop(&ln->ln_timer_ch);
446 * XXX - do we know that there is
447 * callout installed? i.e. are we
448 * guaranteed that we're not dropping
449 * a reference that we did not add?
454 ln->la_expire = time_second + tick / hz;
456 if (tick > INT_MAX) {
457 ln->ln_ntick = tick - INT_MAX;
458 callout_reset(&ln->ln_timer_ch, INT_MAX,
459 nd6_llinfo_timer, ln);
462 callout_reset(&ln->ln_timer_ch, tick,
463 nd6_llinfo_timer, ln);
469 nd6_llinfo_settimer(struct llentry *ln, long tick)
473 nd6_llinfo_settimer_locked(ln, tick);
478 nd6_llinfo_timer(void *arg)
481 struct in6_addr *dst;
483 struct nd_ifinfo *ndi = NULL;
485 ln = (struct llentry *)arg;
487 panic("%s: NULL entry!\n", __func__);
491 if ((ifp = ((ln->lle_tbl != NULL) ? ln->lle_tbl->llt_ifp : NULL)) == NULL)
492 panic("ln ifp == NULL");
494 CURVNET_SET(ifp->if_vnet);
495 INIT_VNET_INET6(curvnet);
497 if (ln->ln_ntick > 0) {
498 if (ln->ln_ntick > INT_MAX) {
499 ln->ln_ntick -= INT_MAX;
500 nd6_llinfo_settimer(ln, INT_MAX);
503 nd6_llinfo_settimer(ln, ln->ln_ntick);
508 ndi = ND_IFINFO(ifp);
509 dst = &L3_ADDR_SIN6(ln)->sin6_addr;
510 if ((ln->la_flags & LLE_STATIC) || (ln->la_expire > time_second)) {
514 if (ln->la_flags & LLE_DELETED) {
515 (void)nd6_free(ln, 0);
519 switch (ln->ln_state) {
520 case ND6_LLINFO_INCOMPLETE:
521 if (ln->la_asked < V_nd6_mmaxtries) {
523 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
524 nd6_ns_output(ifp, NULL, dst, ln, 0);
526 struct mbuf *m = ln->la_hold;
531 * assuming every packet in la_hold has the
536 icmp6_error2(m, ICMP6_DST_UNREACH,
537 ICMP6_DST_UNREACH_ADDR, 0, ifp);
540 clear_llinfo_pqueue(ln);
542 (void)nd6_free(ln, 0);
546 case ND6_LLINFO_REACHABLE:
547 if (!ND6_LLINFO_PERMANENT(ln)) {
548 ln->ln_state = ND6_LLINFO_STALE;
549 nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
553 case ND6_LLINFO_STALE:
554 /* Garbage Collection(RFC 2461 5.3) */
555 if (!ND6_LLINFO_PERMANENT(ln)) {
556 (void)nd6_free(ln, 1);
561 case ND6_LLINFO_DELAY:
562 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
565 ln->ln_state = ND6_LLINFO_PROBE;
566 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
567 nd6_ns_output(ifp, dst, dst, ln, 0);
569 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
570 nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
573 case ND6_LLINFO_PROBE:
574 if (ln->la_asked < V_nd6_umaxtries) {
576 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
577 nd6_ns_output(ifp, dst, dst, ln, 0);
579 (void)nd6_free(ln, 0);
592 * ND6 timer routine to expire default route list and prefix list
597 CURVNET_SET((struct vnet *) arg);
598 INIT_VNET_INET6(curvnet);
600 struct nd_defrouter *dr;
601 struct nd_prefix *pr;
602 struct in6_ifaddr *ia6, *nia6;
603 struct in6_addrlifetime *lt6;
605 callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz,
608 /* expire default router list */
610 dr = TAILQ_FIRST(&V_nd_defrouter);
612 if (dr->expire && dr->expire < time_second) {
613 struct nd_defrouter *t;
614 t = TAILQ_NEXT(dr, dr_entry);
618 dr = TAILQ_NEXT(dr, dr_entry);
623 * expire interface addresses.
624 * in the past the loop was inside prefix expiry processing.
625 * However, from a stricter speci-confrmance standpoint, we should
626 * rather separate address lifetimes and prefix lifetimes.
629 for (ia6 = V_in6_ifaddr; ia6; ia6 = nia6) {
631 /* check address lifetime */
632 lt6 = &ia6->ia6_lifetime;
633 if (IFA6_IS_INVALID(ia6)) {
637 * If the expiring address is temporary, try
638 * regenerating a new one. This would be useful when
639 * we suspended a laptop PC, then turned it on after a
640 * period that could invalidate all temporary
641 * addresses. Although we may have to restart the
642 * loop (see below), it must be after purging the
643 * address. Otherwise, we'd see an infinite loop of
646 if (V_ip6_use_tempaddr &&
647 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
648 if (regen_tmpaddr(ia6) == 0)
652 in6_purgeaddr(&ia6->ia_ifa);
655 goto addrloop; /* XXX: see below */
656 } else if (IFA6_IS_DEPRECATED(ia6)) {
657 int oldflags = ia6->ia6_flags;
659 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
662 * If a temporary address has just become deprecated,
663 * regenerate a new one if possible.
665 if (V_ip6_use_tempaddr &&
666 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
667 (oldflags & IN6_IFF_DEPRECATED) == 0) {
669 if (regen_tmpaddr(ia6) == 0) {
671 * A new temporary address is
673 * XXX: this means the address chain
674 * has changed while we are still in
675 * the loop. Although the change
676 * would not cause disaster (because
677 * it's not a deletion, but an
678 * addition,) we'd rather restart the
679 * loop just for safety. Or does this
680 * significantly reduce performance??
687 * A new RA might have made a deprecated address
690 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
694 /* expire prefix list */
695 pr = V_nd_prefix.lh_first;
698 * check prefix lifetime.
699 * since pltime is just for autoconf, pltime processing for
700 * prefix is not necessary.
702 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
703 time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
708 * address expiration and prefix expiration are
709 * separate. NEVER perform in6_purgeaddr here.
722 * ia6 - deprecated/invalidated temporary address
725 regen_tmpaddr(struct in6_ifaddr *ia6)
729 struct in6_ifaddr *public_ifa6 = NULL;
731 ifp = ia6->ia_ifa.ifa_ifp;
733 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
734 struct in6_ifaddr *it6;
736 if (ifa->ifa_addr->sa_family != AF_INET6)
739 it6 = (struct in6_ifaddr *)ifa;
741 /* ignore no autoconf addresses. */
742 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
745 /* ignore autoconf addresses with different prefixes. */
746 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
750 * Now we are looking at an autoconf address with the same
751 * prefix as ours. If the address is temporary and is still
752 * preferred, do not create another one. It would be rare, but
753 * could happen, for example, when we resume a laptop PC after
756 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
757 !IFA6_IS_DEPRECATED(it6)) {
763 * This is a public autoconf address that has the same prefix
764 * as ours. If it is preferred, keep it. We can't break the
765 * loop here, because there may be a still-preferred temporary
766 * address with the prefix.
768 if (!IFA6_IS_DEPRECATED(it6))
772 if (public_ifa6 != NULL) {
775 if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
777 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
778 " tmp addr,errno=%d\n", e);
790 * Nuke neighbor cache/prefix/default router management table, right before
794 nd6_purge(struct ifnet *ifp)
796 INIT_VNET_INET6(ifp->if_vnet);
797 struct nd_defrouter *dr, *ndr;
798 struct nd_prefix *pr, *npr;
801 * Nuke default router list entries toward ifp.
802 * We defer removal of default router list entries that is installed
803 * in the routing table, in order to keep additional side effects as
806 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = ndr) {
807 ndr = TAILQ_NEXT(dr, dr_entry);
815 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = ndr) {
816 ndr = TAILQ_NEXT(dr, dr_entry);
824 /* Nuke prefix list entries toward ifp */
825 for (pr = V_nd_prefix.lh_first; pr; pr = npr) {
827 if (pr->ndpr_ifp == ifp) {
829 * Because if_detach() does *not* release prefixes
830 * while purging addresses the reference count will
831 * still be above zero. We therefore reset it to
832 * make sure that the prefix really gets purged.
837 * Previously, pr->ndpr_addr is removed as well,
838 * but I strongly believe we don't have to do it.
839 * nd6_purge() is only called from in6_ifdetach(),
840 * which removes all the associated interface addresses
842 * (jinmei@kame.net 20010129)
848 /* cancel default outgoing interface setting */
849 if (V_nd6_defifindex == ifp->if_index)
850 nd6_setdefaultiface(0);
852 if (!V_ip6_forwarding && V_ip6_accept_rtadv) { /* XXX: too restrictive? */
853 /* refresh default router list
862 * We do not nuke the neighbor cache entries here any more
863 * because the neighbor cache is kept in if_afdata[AF_INET6].
864 * nd6_purge() is invoked by in6_ifdetach() which is called
865 * from if_detach() where everything gets purged. So let
866 * in6_domifdetach() do the actual L2 table purging work.
871 * the caller acquires and releases the lock on the lltbls
872 * Returns the llentry locked
875 nd6_lookup(struct in6_addr *addr6, int flags, struct ifnet *ifp)
877 struct sockaddr_in6 sin6;
881 bzero(&sin6, sizeof(sin6));
882 sin6.sin6_len = sizeof(struct sockaddr_in6);
883 sin6.sin6_family = AF_INET6;
884 sin6.sin6_addr = *addr6;
886 IF_AFDATA_LOCK_ASSERT(ifp);
888 if (flags & ND6_CREATE)
889 llflags |= LLE_CREATE;
890 if (flags & ND6_EXCLUSIVE)
891 llflags |= LLE_EXCLUSIVE;
893 ln = lla_lookup(LLTABLE6(ifp), llflags, (struct sockaddr *)&sin6);
894 if ((ln != NULL) && (flags & LLE_CREATE)) {
895 ln->ln_state = ND6_LLINFO_NOSTATE;
896 callout_init(&ln->ln_timer_ch, 0);
903 * Test whether a given IPv6 address is a neighbor or not, ignoring
904 * the actual neighbor cache. The neighbor cache is ignored in order
905 * to not reenter the routing code from within itself.
908 nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
910 INIT_VNET_INET6(ifp->if_vnet);
911 struct nd_prefix *pr;
912 struct ifaddr *dstaddr;
915 * A link-local address is always a neighbor.
916 * XXX: a link does not necessarily specify a single interface.
918 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
919 struct sockaddr_in6 sin6_copy;
923 * We need sin6_copy since sa6_recoverscope() may modify the
927 if (sa6_recoverscope(&sin6_copy))
928 return (0); /* XXX: should be impossible */
929 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
931 if (sin6_copy.sin6_scope_id == zone)
938 * If the address matches one of our addresses,
939 * it should be a neighbor.
940 * If the address matches one of our on-link prefixes, it should be a
943 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
944 if (pr->ndpr_ifp != ifp)
947 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
950 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
951 &addr->sin6_addr, &pr->ndpr_mask))
956 * If the address is assigned on the node of the other side of
957 * a p2p interface, the address should be a neighbor.
959 dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr);
960 if (dstaddr != NULL) {
961 if (dstaddr->ifa_ifp == ifp) {
969 * If the default router list is empty, all addresses are regarded
970 * as on-link, and thus, as a neighbor.
971 * XXX: we restrict the condition to hosts, because routers usually do
972 * not have the "default router list".
974 if (!V_ip6_forwarding && TAILQ_FIRST(&V_nd_defrouter) == NULL &&
975 V_nd6_defifindex == ifp->if_index) {
984 * Detect if a given IPv6 address identifies a neighbor on a given link.
985 * XXX: should take care of the destination of a p2p link?
988 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
993 IF_AFDATA_UNLOCK_ASSERT(ifp);
994 if (nd6_is_new_addr_neighbor(addr, ifp))
998 * Even if the address matches none of our addresses, it might be
999 * in the neighbor cache.
1001 IF_AFDATA_LOCK(ifp);
1002 if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) {
1006 IF_AFDATA_UNLOCK(ifp);
1011 * Free an nd6 llinfo entry.
1012 * Since the function would cause significant changes in the kernel, DO NOT
1013 * make it global, unless you have a strong reason for the change, and are sure
1014 * that the change is safe.
1016 static struct llentry *
1017 nd6_free(struct llentry *ln, int gc)
1019 INIT_VNET_INET6(curvnet);
1020 struct llentry *next;
1021 struct nd_defrouter *dr;
1022 struct ifnet *ifp=NULL;
1025 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1026 * even though it is not harmful, it was not really necessary.
1030 nd6_llinfo_settimer(ln, -1);
1032 if (!V_ip6_forwarding) {
1035 dr = defrouter_lookup(&L3_ADDR_SIN6(ln)->sin6_addr, ln->lle_tbl->llt_ifp);
1037 if (dr != NULL && dr->expire &&
1038 ln->ln_state == ND6_LLINFO_STALE && gc) {
1040 * If the reason for the deletion is just garbage
1041 * collection, and the neighbor is an active default
1042 * router, do not delete it. Instead, reset the GC
1043 * timer using the router's lifetime.
1044 * Simply deleting the entry would affect default
1045 * router selection, which is not necessarily a good
1046 * thing, especially when we're using router preference
1048 * XXX: the check for ln_state would be redundant,
1049 * but we intentionally keep it just in case.
1051 if (dr->expire > time_second)
1052 nd6_llinfo_settimer(ln,
1053 (dr->expire - time_second) * hz);
1055 nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
1057 return (LIST_NEXT(ln, lle_next));
1060 if (ln->ln_router || dr) {
1062 * rt6_flush must be called whether or not the neighbor
1063 * is in the Default Router List.
1064 * See a corresponding comment in nd6_na_input().
1066 rt6_flush(&L3_ADDR_SIN6(ln)->sin6_addr, ln->lle_tbl->llt_ifp);
1071 * Unreachablity of a router might affect the default
1072 * router selection and on-link detection of advertised
1077 * Temporarily fake the state to choose a new default
1078 * router and to perform on-link determination of
1079 * prefixes correctly.
1080 * Below the state will be set correctly,
1081 * or the entry itself will be deleted.
1083 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1086 * Since defrouter_select() does not affect the
1087 * on-link determination and MIP6 needs the check
1088 * before the default router selection, we perform
1091 pfxlist_onlink_check();
1094 * refresh default router list
1102 * Before deleting the entry, remember the next entry as the
1103 * return value. We need this because pfxlist_onlink_check() above
1104 * might have freed other entries (particularly the old next entry) as
1105 * a side effect (XXX).
1107 next = LIST_NEXT(ln, lle_next);
1109 ifp = ln->lle_tbl->llt_ifp;
1110 IF_AFDATA_LOCK(ifp);
1113 IF_AFDATA_UNLOCK(ifp);
1119 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1121 * XXX cost-effective methods?
1124 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1126 INIT_VNET_INET6(curvnet);
1130 if ((dst6 == NULL) || (rt == NULL))
1134 IF_AFDATA_LOCK(ifp);
1135 ln = nd6_lookup(dst6, ND6_EXCLUSIVE, NULL);
1136 IF_AFDATA_UNLOCK(ifp);
1140 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1144 * if we get upper-layer reachability confirmation many times,
1145 * it is possible we have false information.
1149 if (ln->ln_byhint > V_nd6_maxnudhint) {
1154 ln->ln_state = ND6_LLINFO_REACHABLE;
1155 if (!ND6_LLINFO_PERMANENT(ln)) {
1156 nd6_llinfo_settimer(ln,
1157 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1165 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1167 INIT_VNET_INET6(ifp->if_vnet);
1168 struct in6_drlist *drl = (struct in6_drlist *)data;
1169 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1170 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1171 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1172 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1173 struct nd_defrouter *dr;
1174 struct nd_prefix *pr;
1175 int i = 0, error = 0;
1179 case SIOCGDRLST_IN6:
1181 * obsolete API, use sysctl under net.inet6.icmp6
1183 bzero(drl, sizeof(*drl));
1185 dr = TAILQ_FIRST(&V_nd_defrouter);
1186 while (dr && i < DRLSTSIZ) {
1187 drl->defrouter[i].rtaddr = dr->rtaddr;
1188 in6_clearscope(&drl->defrouter[i].rtaddr);
1190 drl->defrouter[i].flags = dr->flags;
1191 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1192 drl->defrouter[i].expire = dr->expire;
1193 drl->defrouter[i].if_index = dr->ifp->if_index;
1195 dr = TAILQ_NEXT(dr, dr_entry);
1199 case SIOCGPRLST_IN6:
1201 * obsolete API, use sysctl under net.inet6.icmp6
1203 * XXX the structure in6_prlist was changed in backward-
1204 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1205 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1208 * XXX meaning of fields, especialy "raflags", is very
1209 * differnet between RA prefix list and RR/static prefix list.
1210 * how about separating ioctls into two?
1212 bzero(oprl, sizeof(*oprl));
1214 pr = V_nd_prefix.lh_first;
1215 while (pr && i < PRLSTSIZ) {
1216 struct nd_pfxrouter *pfr;
1219 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1220 oprl->prefix[i].raflags = pr->ndpr_raf;
1221 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1222 oprl->prefix[i].vltime = pr->ndpr_vltime;
1223 oprl->prefix[i].pltime = pr->ndpr_pltime;
1224 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1225 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1226 oprl->prefix[i].expire = 0;
1230 /* XXX: we assume time_t is signed. */
1233 ((sizeof(maxexpire) * 8) - 1));
1234 if (pr->ndpr_vltime <
1235 maxexpire - pr->ndpr_lastupdate) {
1236 oprl->prefix[i].expire =
1237 pr->ndpr_lastupdate +
1240 oprl->prefix[i].expire = maxexpire;
1243 pfr = pr->ndpr_advrtrs.lh_first;
1247 #define RTRADDR oprl->prefix[i].advrtr[j]
1248 RTRADDR = pfr->router->rtaddr;
1249 in6_clearscope(&RTRADDR);
1253 pfr = pfr->pfr_next;
1255 oprl->prefix[i].advrtrs = j;
1256 oprl->prefix[i].origin = PR_ORIG_RA;
1264 case OSIOCGIFINFO_IN6:
1266 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1267 bzero(&ND, sizeof(ND));
1268 ND.linkmtu = IN6_LINKMTU(ifp);
1269 ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1270 ND.basereachable = ND_IFINFO(ifp)->basereachable;
1271 ND.reachable = ND_IFINFO(ifp)->reachable;
1272 ND.retrans = ND_IFINFO(ifp)->retrans;
1273 ND.flags = ND_IFINFO(ifp)->flags;
1274 ND.recalctm = ND_IFINFO(ifp)->recalctm;
1275 ND.chlim = ND_IFINFO(ifp)->chlim;
1277 case SIOCGIFINFO_IN6:
1278 ND = *ND_IFINFO(ifp);
1280 case SIOCSIFINFO_IN6:
1282 * used to change host variables from userland.
1283 * intented for a use on router to reflect RA configurations.
1285 /* 0 means 'unspecified' */
1286 if (ND.linkmtu != 0) {
1287 if (ND.linkmtu < IPV6_MMTU ||
1288 ND.linkmtu > IN6_LINKMTU(ifp)) {
1292 ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1295 if (ND.basereachable != 0) {
1296 int obasereachable = ND_IFINFO(ifp)->basereachable;
1298 ND_IFINFO(ifp)->basereachable = ND.basereachable;
1299 if (ND.basereachable != obasereachable)
1300 ND_IFINFO(ifp)->reachable =
1301 ND_COMPUTE_RTIME(ND.basereachable);
1303 if (ND.retrans != 0)
1304 ND_IFINFO(ifp)->retrans = ND.retrans;
1306 ND_IFINFO(ifp)->chlim = ND.chlim;
1308 case SIOCSIFINFO_FLAGS:
1309 ND_IFINFO(ifp)->flags = ND.flags;
1312 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1313 /* sync kernel routing table with the default router list */
1317 case SIOCSPFXFLUSH_IN6:
1319 /* flush all the prefix advertised by routers */
1320 struct nd_prefix *pr, *next;
1323 for (pr = V_nd_prefix.lh_first; pr; pr = next) {
1324 struct in6_ifaddr *ia, *ia_next;
1326 next = pr->ndpr_next;
1328 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1331 /* do we really have to remove addresses as well? */
1332 for (ia = V_in6_ifaddr; ia; ia = ia_next) {
1333 /* ia might be removed. keep the next ptr. */
1334 ia_next = ia->ia_next;
1336 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1339 if (ia->ia6_ndpr == pr)
1340 in6_purgeaddr(&ia->ia_ifa);
1347 case SIOCSRTRFLUSH_IN6:
1349 /* flush all the default routers */
1350 struct nd_defrouter *dr, *next;
1354 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = next) {
1355 next = TAILQ_NEXT(dr, dr_entry);
1362 case SIOCGNBRINFO_IN6:
1365 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1367 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1370 IF_AFDATA_LOCK(ifp);
1371 ln = nd6_lookup(&nb_addr, 0, ifp);
1372 IF_AFDATA_UNLOCK(ifp);
1378 nbi->state = ln->ln_state;
1379 nbi->asked = ln->la_asked;
1380 nbi->isrouter = ln->ln_router;
1381 nbi->expire = ln->la_expire;
1385 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1386 ndif->ifindex = V_nd6_defifindex;
1388 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1389 return (nd6_setdefaultiface(ndif->ifindex));
1395 * Create neighbor cache entry and cache link-layer address,
1396 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1399 * code - type dependent information
1402 * The caller of this function already acquired the ndp
1403 * cache table lock because the cache entry is returned.
1406 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1407 int lladdrlen, int type, int code)
1409 INIT_VNET_INET6(curvnet);
1410 struct llentry *ln = NULL;
1417 uint16_t router = 0;
1418 struct sockaddr_in6 sin6;
1419 struct mbuf *chain = NULL;
1420 int static_route = 0;
1422 IF_AFDATA_UNLOCK_ASSERT(ifp);
1425 panic("ifp == NULL in nd6_cache_lladdr");
1427 panic("from == NULL in nd6_cache_lladdr");
1429 /* nothing must be updated for unspecified address */
1430 if (IN6_IS_ADDR_UNSPECIFIED(from))
1434 * Validation about ifp->if_addrlen and lladdrlen must be done in
1437 * XXX If the link does not have link-layer adderss, what should
1438 * we do? (ifp->if_addrlen == 0)
1439 * Spec says nothing in sections for RA, RS and NA. There's small
1440 * description on it in NS section (RFC 2461 7.2.3).
1442 flags |= lladdr ? ND6_EXCLUSIVE : 0;
1443 IF_AFDATA_LOCK(ifp);
1444 ln = nd6_lookup(from, flags, ifp);
1447 flags |= LLE_EXCLUSIVE;
1448 ln = nd6_lookup(from, flags |ND6_CREATE, ifp);
1449 IF_AFDATA_UNLOCK(ifp);
1452 IF_AFDATA_UNLOCK(ifp);
1453 /* do nothing if static ndp is set */
1454 if (ln->la_flags & LLE_STATIC) {
1463 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
1464 if (olladdr && lladdr) {
1465 llchange = bcmp(lladdr, &ln->ll_addr,
1471 * newentry olladdr lladdr llchange (*=record)
1474 * 0 n y -- (3) * STALE
1476 * 0 y y y (5) * STALE
1477 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1478 * 1 -- y -- (7) * STALE
1481 if (lladdr) { /* (3-5) and (7) */
1483 * Record source link-layer address
1484 * XXX is it dependent to ifp->if_type?
1486 bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
1487 ln->la_flags |= LLE_VALID;
1491 if ((!olladdr && lladdr != NULL) || /* (3) */
1492 (olladdr && lladdr != NULL && llchange)) { /* (5) */
1494 newstate = ND6_LLINFO_STALE;
1495 } else /* (1-2,4) */
1499 if (lladdr == NULL) /* (6) */
1500 newstate = ND6_LLINFO_NOSTATE;
1502 newstate = ND6_LLINFO_STALE;
1507 * Update the state of the neighbor cache.
1509 ln->ln_state = newstate;
1511 if (ln->ln_state == ND6_LLINFO_STALE) {
1513 * XXX: since nd6_output() below will cause
1514 * state tansition to DELAY and reset the timer,
1515 * we must set the timer now, although it is actually
1518 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
1521 struct mbuf *m_hold, *m_hold_next;
1524 * reset the la_hold in advance, to explicitly
1525 * prevent a la_hold lookup in nd6_output()
1526 * (wouldn't happen, though...)
1528 for (m_hold = ln->la_hold, ln->la_hold = NULL;
1529 m_hold; m_hold = m_hold_next) {
1530 m_hold_next = m_hold->m_nextpkt;
1531 m_hold->m_nextpkt = NULL;
1534 * we assume ifp is not a p2p here, so
1535 * just set the 2nd argument as the
1538 nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
1541 * If we have mbufs in the chain we need to do
1542 * deferred transmit. Copy the address from the
1543 * llentry before dropping the lock down below.
1546 memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
1548 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1549 /* probe right away */
1550 nd6_llinfo_settimer_locked((void *)ln, 0);
1555 * ICMP6 type dependent behavior.
1557 * NS: clear IsRouter if new entry
1558 * RS: clear IsRouter
1559 * RA: set IsRouter if there's lladdr
1560 * redir: clear IsRouter if new entry
1563 * The spec says that we must set IsRouter in the following cases:
1564 * - If lladdr exist, set IsRouter. This means (1-5).
1565 * - If it is old entry (!newentry), set IsRouter. This means (7).
1566 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1567 * A quetion arises for (1) case. (1) case has no lladdr in the
1568 * neighbor cache, this is similar to (6).
1569 * This case is rare but we figured that we MUST NOT set IsRouter.
1571 * newentry olladdr lladdr llchange NS RS RA redir
1573 * 0 n n -- (1) c ? s
1574 * 0 y n -- (2) c s s
1575 * 0 n y -- (3) c s s
1578 * 1 -- n -- (6) c c c s
1579 * 1 -- y -- (7) c c s c s
1583 switch (type & 0xff) {
1584 case ND_NEIGHBOR_SOLICIT:
1586 * New entry must have is_router flag cleared.
1588 if (is_newentry) /* (6-7) */
1593 * If the icmp is a redirect to a better router, always set the
1594 * is_router flag. Otherwise, if the entry is newly created,
1595 * clear the flag. [RFC 2461, sec 8.3]
1597 if (code == ND_REDIRECT_ROUTER)
1599 else if (is_newentry) /* (6-7) */
1602 case ND_ROUTER_SOLICIT:
1604 * is_router flag must always be cleared.
1608 case ND_ROUTER_ADVERT:
1610 * Mark an entry with lladdr as a router.
1612 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1613 (is_newentry && lladdr)) { /* (7) */
1620 static_route = (ln->la_flags & LLE_STATIC);
1621 router = ln->ln_router;
1623 if (flags & ND6_EXCLUSIVE)
1631 nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
1634 * When the link-layer address of a router changes, select the
1635 * best router again. In particular, when the neighbor entry is newly
1636 * created, it might affect the selection policy.
1637 * Question: can we restrict the first condition to the "is_newentry"
1639 * XXX: when we hear an RA from a new router with the link-layer
1640 * address option, defrouter_select() is called twice, since
1641 * defrtrlist_update called the function as well. However, I believe
1642 * we can compromise the overhead, since it only happens the first
1644 * XXX: although defrouter_select() should not have a bad effect
1645 * for those are not autoconfigured hosts, we explicitly avoid such
1648 if (do_update && router && !V_ip6_forwarding && V_ip6_accept_rtadv) {
1650 * guaranteed recursion
1658 if (flags & ND6_EXCLUSIVE)
1669 nd6_slowtimo(void *arg)
1671 CURVNET_SET((struct vnet *) arg);
1672 INIT_VNET_NET((struct vnet *) arg);
1673 INIT_VNET_INET6((struct vnet *) arg);
1674 struct nd_ifinfo *nd6if;
1677 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1678 nd6_slowtimo, curvnet);
1680 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
1681 ifp = TAILQ_NEXT(ifp, if_list)) {
1682 nd6if = ND_IFINFO(ifp);
1683 if (nd6if->basereachable && /* already initialized */
1684 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1686 * Since reachable time rarely changes by router
1687 * advertisements, we SHOULD insure that a new random
1688 * value gets recomputed at least once every few hours.
1691 nd6if->recalctm = V_nd6_recalc_reachtm_interval;
1692 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1700 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1701 struct sockaddr_in6 *dst, struct rtentry *rt0)
1704 return (nd6_output_lle(ifp, origifp, m0, dst, rt0, NULL, NULL));
1709 * Note that I'm not enforcing any global serialization
1710 * lle state or asked changes here as the logic is too
1711 * complicated to avoid having to always acquire an exclusive
1716 #define senderr(e) { error = (e); goto bad;}
1719 nd6_output_lle(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1720 struct sockaddr_in6 *dst, struct rtentry *rt0, struct llentry *lle,
1721 struct mbuf **chain)
1723 INIT_VNET_INET6(curvnet);
1724 struct mbuf *m = m0;
1725 struct llentry *ln = lle;
1732 LLE_WLOCK_ASSERT(lle);
1734 KASSERT(chain != NULL, (" lle locked but no mbuf chain pointer passed"));
1737 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1740 if (nd6_need_cache(ifp) == 0)
1744 * next hop determination. This routine is derived from ether_output.
1748 * Address resolution or Neighbor Unreachability Detection
1750 * At this point, the destination of the packet must be a unicast
1751 * or an anycast address(i.e. not a multicast).
1754 flags = ((m != NULL) || (lle != NULL)) ? LLE_EXCLUSIVE : 0;
1757 IF_AFDATA_LOCK(ifp);
1758 ln = lla_lookup(LLTABLE6(ifp), flags, (struct sockaddr *)dst);
1759 IF_AFDATA_UNLOCK(ifp);
1760 if ((ln == NULL) && nd6_is_addr_neighbor(dst, ifp)) {
1762 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1763 * the condition below is not very efficient. But we believe
1764 * it is tolerable, because this should be a rare case.
1766 flags = ND6_CREATE | (m ? ND6_EXCLUSIVE : 0);
1767 IF_AFDATA_LOCK(ifp);
1768 ln = nd6_lookup(&dst->sin6_addr, flags, ifp);
1769 IF_AFDATA_UNLOCK(ifp);
1773 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1774 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1775 char ip6buf[INET6_ADDRSTRLEN];
1777 "nd6_output: can't allocate llinfo for %s "
1779 ip6_sprintf(ip6buf, &dst->sin6_addr), ln);
1780 senderr(EIO); /* XXX: good error? */
1782 goto sendpkt; /* send anyway */
1785 /* We don't have to do link-layer address resolution on a p2p link. */
1786 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1787 ln->ln_state < ND6_LLINFO_REACHABLE) {
1788 if ((flags & LLE_EXCLUSIVE) == 0) {
1789 flags |= LLE_EXCLUSIVE;
1792 ln->ln_state = ND6_LLINFO_STALE;
1793 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
1797 * The first time we send a packet to a neighbor whose entry is
1798 * STALE, we have to change the state to DELAY and a sets a timer to
1799 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1800 * neighbor unreachability detection on expiration.
1803 if (ln->ln_state == ND6_LLINFO_STALE) {
1804 if ((flags & LLE_EXCLUSIVE) == 0) {
1805 flags |= LLE_EXCLUSIVE;
1810 ln->ln_state = ND6_LLINFO_DELAY;
1811 nd6_llinfo_settimer_locked(ln, (long)V_nd6_delay * hz);
1815 * If the neighbor cache entry has a state other than INCOMPLETE
1816 * (i.e. its link-layer address is already resolved), just
1819 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1823 * There is a neighbor cache entry, but no ethernet address
1824 * response yet. Append this latest packet to the end of the
1825 * packet queue in the mbuf, unless the number of the packet
1826 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen,
1827 * the oldest packet in the queue will be removed.
1829 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1830 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1832 if ((flags & LLE_EXCLUSIVE) == 0) {
1833 flags |= LLE_EXCLUSIVE;
1838 struct mbuf *m_hold;
1842 for (m_hold = ln->la_hold; m_hold; m_hold = m_hold->m_nextpkt) {
1844 if (m_hold->m_nextpkt == NULL) {
1845 m_hold->m_nextpkt = m;
1849 while (i >= V_nd6_maxqueuelen) {
1850 m_hold = ln->la_hold;
1851 ln->la_hold = ln->la_hold->m_nextpkt;
1859 * We did the lookup (no lle arg) so we
1860 * need to do the unlock here
1863 if (flags & LLE_EXCLUSIVE)
1870 * If there has been no NS for the neighbor after entering the
1871 * INCOMPLETE state, send the first solicitation.
1873 if (!ND6_LLINFO_PERMANENT(ln) && ln->la_asked == 0) {
1876 nd6_llinfo_settimer(ln,
1877 (long)ND_IFINFO(ifp)->retrans * hz / 1000);
1878 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1883 /* discard the packet if IPv6 operation is disabled on the interface */
1884 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
1885 error = ENETDOWN; /* better error? */
1889 * ln is valid and the caller did not pass in
1892 if ((ln != NULL) && (lle == NULL)) {
1893 if (flags & LLE_EXCLUSIVE)
1900 mac_netinet6_nd6_send(ifp, m);
1903 * We were passed in a pointer to an lle with the lock held
1904 * this means that we can't call if_output as we will
1905 * recurse on the lle lock - so what we do is we create
1906 * a list of mbufs to send and transmit them in the caller
1907 * after the lock is dropped
1913 struct mbuf *m = *chain;
1916 * append mbuf to end of deferred chain
1918 while (m->m_nextpkt != NULL)
1924 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1925 return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
1928 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, NULL);
1933 * ln is valid and the caller did not pass in
1936 if ((ln != NULL) && (lle == NULL)) {
1937 if (flags & LLE_EXCLUSIVE)
1950 nd6_output_flush(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain,
1951 struct sockaddr_in6 *dst, struct route *ro)
1953 struct mbuf *m, *m_head;
1954 struct ifnet *outifp;
1958 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
1965 m_head = m_head->m_nextpkt;
1966 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, ro);
1971 * note that intermediate errors are blindly ignored - but this is
1972 * the same convention as used with nd6_output when called by
1980 nd6_need_cache(struct ifnet *ifp)
1983 * XXX: we currently do not make neighbor cache on any interface
1984 * other than ARCnet, Ethernet, FDDI and GIF.
1987 * - unidirectional tunnels needs no ND
1989 switch (ifp->if_type) {
1997 #ifdef IFT_IEEE80211
2003 case IFT_GIF: /* XXX need more cases? */
2007 case IFT_PROPVIRTUAL:
2015 * the callers of this function need to be re-worked to drop
2016 * the lle lock, drop here for now
2019 nd6_storelladdr(struct ifnet *ifp, struct mbuf *m,
2020 struct sockaddr *dst, u_char *desten, struct llentry **lle)
2025 IF_AFDATA_UNLOCK_ASSERT(ifp);
2026 if (m->m_flags & M_MCAST) {
2029 switch (ifp->if_type) {
2035 #ifdef IFT_IEEE80211
2040 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2045 * netbsd can use if_broadcastaddr, but we don't do so
2046 * to reduce # of ifdef.
2048 for (i = 0; i < ifp->if_addrlen; i++)
2056 return (EAFNOSUPPORT);
2062 * the entry should have been created in nd6_store_lladdr
2064 IF_AFDATA_LOCK(ifp);
2065 ln = lla_lookup(LLTABLE6(ifp), 0, dst);
2066 IF_AFDATA_UNLOCK(ifp);
2067 if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) {
2070 /* this could happen, if we could not allocate memory */
2075 bcopy(&ln->ll_addr, desten, ifp->if_addrlen);
2079 * A *small* use after free race exists here
2085 clear_llinfo_pqueue(struct llentry *ln)
2087 struct mbuf *m_hold, *m_hold_next;
2089 for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) {
2090 m_hold_next = m_hold->m_nextpkt;
2091 m_hold->m_nextpkt = NULL;
2099 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2100 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2102 SYSCTL_DECL(_net_inet6_icmp6);
2104 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2105 CTLFLAG_RD, nd6_sysctl_drlist, "");
2106 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2107 CTLFLAG_RD, nd6_sysctl_prlist, "");
2108 SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN,
2109 nd6_maxqueuelen, CTLFLAG_RW, nd6_maxqueuelen, 1, "");
2112 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2114 INIT_VNET_INET6(curvnet);
2116 char buf[1024] __aligned(4);
2117 struct in6_defrouter *d, *de;
2118 struct nd_defrouter *dr;
2124 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr;
2125 dr = TAILQ_NEXT(dr, dr_entry)) {
2126 d = (struct in6_defrouter *)buf;
2127 de = (struct in6_defrouter *)(buf + sizeof(buf));
2130 bzero(d, sizeof(*d));
2131 d->rtaddr.sin6_family = AF_INET6;
2132 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2133 d->rtaddr.sin6_addr = dr->rtaddr;
2134 error = sa6_recoverscope(&d->rtaddr);
2137 d->flags = dr->flags;
2138 d->rtlifetime = dr->rtlifetime;
2139 d->expire = dr->expire;
2140 d->if_index = dr->ifp->if_index;
2142 panic("buffer too short");
2144 error = SYSCTL_OUT(req, buf, sizeof(*d));
2153 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2155 INIT_VNET_INET6(curvnet);
2157 char buf[1024] __aligned(4);
2158 struct in6_prefix *p, *pe;
2159 struct nd_prefix *pr;
2160 char ip6buf[INET6_ADDRSTRLEN];
2166 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2169 struct sockaddr_in6 *sin6, *s6;
2170 struct nd_pfxrouter *pfr;
2172 p = (struct in6_prefix *)buf;
2173 pe = (struct in6_prefix *)(buf + sizeof(buf));
2176 bzero(p, sizeof(*p));
2177 sin6 = (struct sockaddr_in6 *)(p + 1);
2179 p->prefix = pr->ndpr_prefix;
2180 if (sa6_recoverscope(&p->prefix)) {
2182 "scope error in prefix list (%s)\n",
2183 ip6_sprintf(ip6buf, &p->prefix.sin6_addr));
2184 /* XXX: press on... */
2186 p->raflags = pr->ndpr_raf;
2187 p->prefixlen = pr->ndpr_plen;
2188 p->vltime = pr->ndpr_vltime;
2189 p->pltime = pr->ndpr_pltime;
2190 p->if_index = pr->ndpr_ifp->if_index;
2191 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2196 /* XXX: we assume time_t is signed. */
2199 ((sizeof(maxexpire) * 8) - 1));
2200 if (pr->ndpr_vltime <
2201 maxexpire - pr->ndpr_lastupdate) {
2202 p->expire = pr->ndpr_lastupdate +
2205 p->expire = maxexpire;
2207 p->refcnt = pr->ndpr_refcnt;
2208 p->flags = pr->ndpr_stateflags;
2209 p->origin = PR_ORIG_RA;
2211 for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2212 pfr = pfr->pfr_next) {
2213 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2217 s6 = &sin6[advrtrs];
2218 bzero(s6, sizeof(*s6));
2219 s6->sin6_family = AF_INET6;
2220 s6->sin6_len = sizeof(*sin6);
2221 s6->sin6_addr = pfr->router->rtaddr;
2222 if (sa6_recoverscope(s6)) {
2225 "prefix list (%s)\n",
2227 &pfr->router->rtaddr));
2231 p->advrtrs = advrtrs;
2233 panic("buffer too short");
2235 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2236 error = SYSCTL_OUT(req, buf, advance);