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>
54 #include <sys/sysctl.h>
57 #include <net/if_var.h>
58 #include <net/if_arc.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/iso88025.h>
63 #include <net/route.h>
66 #include <netinet/in.h>
67 #include <netinet/in_kdtrace.h>
68 #include <net/if_llatbl.h>
69 #define L3_ADDR_SIN6(le) ((struct sockaddr_in6 *) L3_ADDR(le))
70 #include <netinet/if_ether.h>
71 #include <netinet6/in6_var.h>
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet6/scope6_var.h>
75 #include <netinet6/nd6.h>
76 #include <netinet6/in6_ifattach.h>
77 #include <netinet/icmp6.h>
78 #include <netinet6/send.h>
80 #include <sys/limits.h>
82 #include <security/mac/mac_framework.h>
84 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
85 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
87 #define SIN6(s) ((const struct sockaddr_in6 *)(s))
90 VNET_DEFINE(int, nd6_prune) = 1; /* walk list every 1 seconds */
91 VNET_DEFINE(int, nd6_delay) = 5; /* delay first probe time 5 second */
92 VNET_DEFINE(int, nd6_umaxtries) = 3; /* maximum unicast query */
93 VNET_DEFINE(int, nd6_mmaxtries) = 3; /* maximum multicast query */
94 VNET_DEFINE(int, nd6_useloopback) = 1; /* use loopback interface for
96 VNET_DEFINE(int, nd6_gctimer) = (60 * 60 * 24); /* 1 day: garbage
99 /* preventing too many loops in ND option parsing */
100 static VNET_DEFINE(int, nd6_maxndopt) = 10; /* max # of ND options allowed */
102 VNET_DEFINE(int, nd6_maxnudhint) = 0; /* max # of subsequent upper
104 static VNET_DEFINE(int, nd6_maxqueuelen) = 1; /* max pkts cached in unresolved
106 #define V_nd6_maxndopt VNET(nd6_maxndopt)
107 #define V_nd6_maxqueuelen VNET(nd6_maxqueuelen)
110 VNET_DEFINE(int, nd6_debug) = 1;
112 VNET_DEFINE(int, nd6_debug) = 0;
115 static eventhandler_tag lle_event_eh;
119 static int nd6_inuse, nd6_allocated;
122 VNET_DEFINE(struct nd_drhead, nd_defrouter);
123 VNET_DEFINE(struct nd_prhead, nd_prefix);
125 VNET_DEFINE(int, nd6_recalc_reachtm_interval) = ND6_RECALC_REACHTM_INTERVAL;
126 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
128 int (*send_sendso_input_hook)(struct mbuf *, struct ifnet *, int, int);
130 static int nd6_is_new_addr_neighbor(struct sockaddr_in6 *,
132 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
133 static void nd6_slowtimo(void *);
134 static int regen_tmpaddr(struct in6_ifaddr *);
135 static struct llentry *nd6_free(struct llentry *, int);
136 static void nd6_llinfo_timer(void *);
137 static void clear_llinfo_pqueue(struct llentry *);
138 static void nd6_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
139 static int nd6_output_lle(struct ifnet *, struct ifnet *, struct mbuf *,
140 struct sockaddr_in6 *);
141 static int nd6_output_ifp(struct ifnet *, struct ifnet *, struct mbuf *,
142 struct sockaddr_in6 *);
144 static VNET_DEFINE(struct callout, nd6_slowtimo_ch);
145 #define V_nd6_slowtimo_ch VNET(nd6_slowtimo_ch)
147 VNET_DEFINE(struct callout, nd6_timer_ch);
150 nd6_lle_event(void *arg __unused, struct llentry *lle, int evt)
152 struct rt_addrinfo rtinfo;
153 struct sockaddr_in6 dst, *sa6;
154 struct sockaddr_dl gw;
158 LLE_WLOCK_ASSERT(lle);
161 case LLENTRY_RESOLVED:
163 KASSERT(lle->la_flags & LLE_VALID,
164 ("%s: %p resolved but not valid?", __func__, lle));
166 case LLENTRY_EXPIRED:
173 sa6 = L3_ADDR_SIN6(lle);
174 if (sa6->sin6_family != AF_INET6)
176 ifp = lle->lle_tbl->llt_ifp;
178 bzero(&dst, sizeof(dst));
179 bzero(&gw, sizeof(gw));
180 bzero(&rtinfo, sizeof(rtinfo));
181 dst.sin6_len = sizeof(struct sockaddr_in6);
182 dst.sin6_family = AF_INET6;
183 dst.sin6_addr = sa6->sin6_addr;
184 dst.sin6_scope_id = in6_getscopezone(ifp,
185 in6_addrscope(&sa6->sin6_addr));
186 in6_clearscope(&dst.sin6_addr); /* XXX */
187 gw.sdl_len = sizeof(struct sockaddr_dl);
188 gw.sdl_family = AF_LINK;
189 gw.sdl_alen = ifp->if_addrlen;
190 gw.sdl_index = ifp->if_index;
191 gw.sdl_type = ifp->if_type;
192 if (evt == LLENTRY_RESOLVED)
193 bcopy(&lle->ll_addr, gw.sdl_data, ifp->if_addrlen);
194 rtinfo.rti_info[RTAX_DST] = (struct sockaddr *)&dst;
195 rtinfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gw;
196 rtinfo.rti_addrs = RTA_DST | RTA_GATEWAY;
197 rt_missmsg_fib(type, &rtinfo, RTF_HOST | RTF_LLDATA | (
198 type == RTM_ADD ? RTF_UP: 0), 0, RT_DEFAULT_FIB);
205 LIST_INIT(&V_nd_prefix);
207 /* initialization of the default router list */
208 TAILQ_INIT(&V_nd_defrouter);
211 callout_init(&V_nd6_slowtimo_ch, 0);
212 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
213 nd6_slowtimo, curvnet);
216 if (IS_DEFAULT_VNET(curvnet))
217 lle_event_eh = EVENTHANDLER_REGISTER(lle_event, nd6_lle_event,
218 NULL, EVENTHANDLER_PRI_ANY);
226 callout_drain(&V_nd6_slowtimo_ch);
227 callout_drain(&V_nd6_timer_ch);
228 if (IS_DEFAULT_VNET(curvnet))
229 EVENTHANDLER_DEREGISTER(lle_event, lle_event_eh);
234 nd6_ifattach(struct ifnet *ifp)
236 struct nd_ifinfo *nd;
238 nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK|M_ZERO);
241 nd->chlim = IPV6_DEFHLIM;
242 nd->basereachable = REACHABLE_TIME;
243 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
244 nd->retrans = RETRANS_TIMER;
246 nd->flags = ND6_IFF_PERFORMNUD;
248 /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
249 * XXXHRS: Clear ND6_IFF_AUTO_LINKLOCAL on an IFT_BRIDGE interface by
250 * default regardless of the V_ip6_auto_linklocal configuration to
251 * give a reasonable default behavior.
253 if ((V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
254 (ifp->if_flags & IFF_LOOPBACK))
255 nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
257 * A loopback interface does not need to accept RTADV.
258 * XXXHRS: Clear ND6_IFF_ACCEPT_RTADV on an IFT_BRIDGE interface by
259 * default regardless of the V_ip6_accept_rtadv configuration to
260 * prevent the interface from accepting RA messages arrived
261 * on one of the member interfaces with ND6_IFF_ACCEPT_RTADV.
263 if (V_ip6_accept_rtadv &&
264 !(ifp->if_flags & IFF_LOOPBACK) &&
265 (ifp->if_type != IFT_BRIDGE))
266 nd->flags |= ND6_IFF_ACCEPT_RTADV;
267 if (V_ip6_no_radr && !(ifp->if_flags & IFF_LOOPBACK))
268 nd->flags |= ND6_IFF_NO_RADR;
270 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
271 nd6_setmtu0(ifp, nd);
277 nd6_ifdetach(struct nd_ifinfo *nd)
284 * Reset ND level link MTU. This function is called when the physical MTU
285 * changes, which means we might have to adjust the ND level MTU.
288 nd6_setmtu(struct ifnet *ifp)
291 nd6_setmtu0(ifp, ND_IFINFO(ifp));
294 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
296 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
300 omaxmtu = ndi->maxmtu;
302 switch (ifp->if_type) {
304 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
307 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
310 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
313 ndi->maxmtu = ifp->if_mtu;
318 * Decreasing the interface MTU under IPV6 minimum MTU may cause
319 * undesirable situation. We thus notify the operator of the change
320 * explicitly. The check for omaxmtu is necessary to restrict the
321 * log to the case of changing the MTU, not initializing it.
323 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
324 log(LOG_NOTICE, "nd6_setmtu0: "
325 "new link MTU on %s (%lu) is too small for IPv6\n",
326 if_name(ifp), (unsigned long)ndi->maxmtu);
329 if (ndi->maxmtu > V_in6_maxmtu)
330 in6_setmaxmtu(); /* check all interfaces just in case */
335 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
338 bzero(ndopts, sizeof(*ndopts));
339 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
341 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
344 ndopts->nd_opts_done = 1;
345 ndopts->nd_opts_search = NULL;
350 * Take one ND option.
353 nd6_option(union nd_opts *ndopts)
355 struct nd_opt_hdr *nd_opt;
358 KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
359 KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
361 if (ndopts->nd_opts_search == NULL)
363 if (ndopts->nd_opts_done)
366 nd_opt = ndopts->nd_opts_search;
368 /* make sure nd_opt_len is inside the buffer */
369 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
370 bzero(ndopts, sizeof(*ndopts));
374 olen = nd_opt->nd_opt_len << 3;
377 * Message validation requires that all included
378 * options have a length that is greater than zero.
380 bzero(ndopts, sizeof(*ndopts));
384 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
385 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
386 /* option overruns the end of buffer, invalid */
387 bzero(ndopts, sizeof(*ndopts));
389 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
390 /* reached the end of options chain */
391 ndopts->nd_opts_done = 1;
392 ndopts->nd_opts_search = NULL;
398 * Parse multiple ND options.
399 * This function is much easier to use, for ND routines that do not need
400 * multiple options of the same type.
403 nd6_options(union nd_opts *ndopts)
405 struct nd_opt_hdr *nd_opt;
408 KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
409 KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
411 if (ndopts->nd_opts_search == NULL)
415 nd_opt = nd6_option(ndopts);
416 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
418 * Message validation requires that all included
419 * options have a length that is greater than zero.
421 ICMP6STAT_INC(icp6s_nd_badopt);
422 bzero(ndopts, sizeof(*ndopts));
429 switch (nd_opt->nd_opt_type) {
430 case ND_OPT_SOURCE_LINKADDR:
431 case ND_OPT_TARGET_LINKADDR:
433 case ND_OPT_REDIRECTED_HEADER:
435 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
437 "duplicated ND6 option found (type=%d)\n",
438 nd_opt->nd_opt_type));
441 ndopts->nd_opt_array[nd_opt->nd_opt_type]
445 case ND_OPT_PREFIX_INFORMATION:
446 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
447 ndopts->nd_opt_array[nd_opt->nd_opt_type]
450 ndopts->nd_opts_pi_end =
451 (struct nd_opt_prefix_info *)nd_opt;
453 /* What about ND_OPT_ROUTE_INFO? RFC 4191 */
454 case ND_OPT_RDNSS: /* RFC 6106 */
455 case ND_OPT_DNSSL: /* RFC 6106 */
457 * Silently ignore options we know and do not care about
463 * Unknown options must be silently ignored,
464 * to accomodate future extension to the protocol.
467 "nd6_options: unsupported option %d - "
468 "option ignored\n", nd_opt->nd_opt_type));
473 if (i > V_nd6_maxndopt) {
474 ICMP6STAT_INC(icp6s_nd_toomanyopt);
475 nd6log((LOG_INFO, "too many loop in nd opt\n"));
479 if (ndopts->nd_opts_done)
487 * ND6 timer routine to handle ND6 entries
490 nd6_llinfo_settimer_locked(struct llentry *ln, long tick)
494 LLE_WLOCK_ASSERT(ln);
499 canceled = callout_stop(&ln->ln_timer_ch);
501 ln->la_expire = time_uptime + tick / hz;
503 if (tick > INT_MAX) {
504 ln->ln_ntick = tick - INT_MAX;
505 canceled = callout_reset(&ln->ln_timer_ch, INT_MAX,
506 nd6_llinfo_timer, ln);
509 canceled = callout_reset(&ln->ln_timer_ch, tick,
510 nd6_llinfo_timer, ln);
518 nd6_llinfo_settimer(struct llentry *ln, long tick)
522 nd6_llinfo_settimer_locked(ln, tick);
527 nd6_llinfo_timer(void *arg)
530 struct in6_addr *dst;
532 struct nd_ifinfo *ndi = NULL;
534 KASSERT(arg != NULL, ("%s: arg NULL", __func__));
535 ln = (struct llentry *)arg;
537 if (callout_pending(&ln->la_timer)) {
539 * Here we are a bit odd here in the treatment of
540 * active/pending. If the pending bit is set, it got
541 * rescheduled before I ran. The active
542 * bit we ignore, since if it was stopped
543 * in ll_tablefree() and was currently running
544 * it would have return 0 so the code would
545 * not have deleted it since the callout could
546 * not be stopped so we want to go through
547 * with the delete here now. If the callout
548 * was restarted, the pending bit will be back on and
549 * we just want to bail since the callout_reset would
550 * return 1 and our reference would have been removed
551 * by nd6_llinfo_settimer_locked above since canceled
557 ifp = ln->lle_tbl->llt_ifp;
558 CURVNET_SET(ifp->if_vnet);
560 if (ln->ln_ntick > 0) {
561 if (ln->ln_ntick > INT_MAX) {
562 ln->ln_ntick -= INT_MAX;
563 nd6_llinfo_settimer_locked(ln, INT_MAX);
566 nd6_llinfo_settimer_locked(ln, ln->ln_ntick);
571 ndi = ND_IFINFO(ifp);
572 dst = &L3_ADDR_SIN6(ln)->sin6_addr;
573 if (ln->la_flags & LLE_STATIC) {
577 if (ln->la_flags & LLE_DELETED) {
578 (void)nd6_free(ln, 0);
583 switch (ln->ln_state) {
584 case ND6_LLINFO_INCOMPLETE:
585 if (ln->la_asked < V_nd6_mmaxtries) {
587 nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
589 nd6_ns_output(ifp, NULL, dst, ln, NULL);
592 struct mbuf *m = ln->la_hold;
597 * assuming every packet in la_hold has the
598 * same IP header. Send error after unlock.
603 clear_llinfo_pqueue(ln);
605 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_TIMEDOUT);
606 (void)nd6_free(ln, 0);
609 icmp6_error2(m, ICMP6_DST_UNREACH,
610 ICMP6_DST_UNREACH_ADDR, 0, ifp);
613 case ND6_LLINFO_REACHABLE:
614 if (!ND6_LLINFO_PERMANENT(ln)) {
615 ln->ln_state = ND6_LLINFO_STALE;
616 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
620 case ND6_LLINFO_STALE:
621 /* Garbage Collection(RFC 2461 5.3) */
622 if (!ND6_LLINFO_PERMANENT(ln)) {
623 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED);
624 (void)nd6_free(ln, 1);
629 case ND6_LLINFO_DELAY:
630 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
633 ln->ln_state = ND6_LLINFO_PROBE;
634 nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
636 nd6_ns_output(ifp, dst, dst, ln, NULL);
639 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
640 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
643 case ND6_LLINFO_PROBE:
644 if (ln->la_asked < V_nd6_umaxtries) {
646 nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
648 nd6_ns_output(ifp, dst, dst, ln, NULL);
651 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED);
652 (void)nd6_free(ln, 0);
657 panic("%s: paths in a dark night can be confusing: %d",
658 __func__, ln->ln_state);
668 * ND6 timer routine to expire default route list and prefix list
673 CURVNET_SET((struct vnet *) arg);
674 struct nd_defrouter *dr, *ndr;
675 struct nd_prefix *pr, *npr;
676 struct in6_ifaddr *ia6, *nia6;
678 callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz,
681 /* expire default router list */
682 TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
683 if (dr->expire && dr->expire < time_uptime)
688 * expire interface addresses.
689 * in the past the loop was inside prefix expiry processing.
690 * However, from a stricter speci-confrmance standpoint, we should
691 * rather separate address lifetimes and prefix lifetimes.
693 * XXXRW: in6_ifaddrhead locking.
696 TAILQ_FOREACH_SAFE(ia6, &V_in6_ifaddrhead, ia_link, nia6) {
697 /* check address lifetime */
698 if (IFA6_IS_INVALID(ia6)) {
702 * If the expiring address is temporary, try
703 * regenerating a new one. This would be useful when
704 * we suspended a laptop PC, then turned it on after a
705 * period that could invalidate all temporary
706 * addresses. Although we may have to restart the
707 * loop (see below), it must be after purging the
708 * address. Otherwise, we'd see an infinite loop of
711 if (V_ip6_use_tempaddr &&
712 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
713 if (regen_tmpaddr(ia6) == 0)
717 in6_purgeaddr(&ia6->ia_ifa);
720 goto addrloop; /* XXX: see below */
721 } else if (IFA6_IS_DEPRECATED(ia6)) {
722 int oldflags = ia6->ia6_flags;
724 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
727 * If a temporary address has just become deprecated,
728 * regenerate a new one if possible.
730 if (V_ip6_use_tempaddr &&
731 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
732 (oldflags & IN6_IFF_DEPRECATED) == 0) {
734 if (regen_tmpaddr(ia6) == 0) {
736 * A new temporary address is
738 * XXX: this means the address chain
739 * has changed while we are still in
740 * the loop. Although the change
741 * would not cause disaster (because
742 * it's not a deletion, but an
743 * addition,) we'd rather restart the
744 * loop just for safety. Or does this
745 * significantly reduce performance??
752 * A new RA might have made a deprecated address
755 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
759 /* expire prefix list */
760 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
762 * check prefix lifetime.
763 * since pltime is just for autoconf, pltime processing for
764 * prefix is not necessary.
766 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
767 time_uptime - pr->ndpr_lastupdate > pr->ndpr_vltime) {
770 * address expiration and prefix expiration are
771 * separate. NEVER perform in6_purgeaddr here.
780 * ia6 - deprecated/invalidated temporary address
783 regen_tmpaddr(struct in6_ifaddr *ia6)
787 struct in6_ifaddr *public_ifa6 = NULL;
789 ifp = ia6->ia_ifa.ifa_ifp;
791 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
792 struct in6_ifaddr *it6;
794 if (ifa->ifa_addr->sa_family != AF_INET6)
797 it6 = (struct in6_ifaddr *)ifa;
799 /* ignore no autoconf addresses. */
800 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
803 /* ignore autoconf addresses with different prefixes. */
804 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
808 * Now we are looking at an autoconf address with the same
809 * prefix as ours. If the address is temporary and is still
810 * preferred, do not create another one. It would be rare, but
811 * could happen, for example, when we resume a laptop PC after
814 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
815 !IFA6_IS_DEPRECATED(it6)) {
821 * This is a public autoconf address that has the same prefix
822 * as ours. If it is preferred, keep it. We can't break the
823 * loop here, because there may be a still-preferred temporary
824 * address with the prefix.
826 if (!IFA6_IS_DEPRECATED(it6))
829 if (public_ifa6 != NULL)
830 ifa_ref(&public_ifa6->ia_ifa);
831 IF_ADDR_RUNLOCK(ifp);
833 if (public_ifa6 != NULL) {
836 if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
837 ifa_free(&public_ifa6->ia_ifa);
838 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
839 " tmp addr,errno=%d\n", e);
842 ifa_free(&public_ifa6->ia_ifa);
850 * Nuke neighbor cache/prefix/default router management table, right before
854 nd6_purge(struct ifnet *ifp)
856 struct nd_defrouter *dr, *ndr;
857 struct nd_prefix *pr, *npr;
860 * Nuke default router list entries toward ifp.
861 * We defer removal of default router list entries that is installed
862 * in the routing table, in order to keep additional side effects as
865 TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
873 TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
881 /* Nuke prefix list entries toward ifp */
882 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
883 if (pr->ndpr_ifp == ifp) {
885 * Because if_detach() does *not* release prefixes
886 * while purging addresses the reference count will
887 * still be above zero. We therefore reset it to
888 * make sure that the prefix really gets purged.
893 * Previously, pr->ndpr_addr is removed as well,
894 * but I strongly believe we don't have to do it.
895 * nd6_purge() is only called from in6_ifdetach(),
896 * which removes all the associated interface addresses
898 * (jinmei@kame.net 20010129)
904 /* cancel default outgoing interface setting */
905 if (V_nd6_defifindex == ifp->if_index)
906 nd6_setdefaultiface(0);
908 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
909 /* Refresh default router list. */
914 * We do not nuke the neighbor cache entries here any more
915 * because the neighbor cache is kept in if_afdata[AF_INET6].
916 * nd6_purge() is invoked by in6_ifdetach() which is called
917 * from if_detach() where everything gets purged. So let
918 * in6_domifdetach() do the actual L2 table purging work.
923 * the caller acquires and releases the lock on the lltbls
924 * Returns the llentry locked
927 nd6_lookup(struct in6_addr *addr6, int flags, struct ifnet *ifp)
929 struct sockaddr_in6 sin6;
933 bzero(&sin6, sizeof(sin6));
934 sin6.sin6_len = sizeof(struct sockaddr_in6);
935 sin6.sin6_family = AF_INET6;
936 sin6.sin6_addr = *addr6;
938 IF_AFDATA_LOCK_ASSERT(ifp);
941 if (flags & ND6_CREATE)
942 llflags |= LLE_CREATE;
943 if (flags & ND6_EXCLUSIVE)
944 llflags |= LLE_EXCLUSIVE;
946 ln = lla_lookup(LLTABLE6(ifp), llflags, (struct sockaddr *)&sin6);
947 if ((ln != NULL) && (llflags & LLE_CREATE))
948 ln->ln_state = ND6_LLINFO_NOSTATE;
954 * Test whether a given IPv6 address is a neighbor or not, ignoring
955 * the actual neighbor cache. The neighbor cache is ignored in order
956 * to not reenter the routing code from within itself.
959 nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
961 struct nd_prefix *pr;
962 struct ifaddr *dstaddr;
965 * A link-local address is always a neighbor.
966 * XXX: a link does not necessarily specify a single interface.
968 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
969 struct sockaddr_in6 sin6_copy;
973 * We need sin6_copy since sa6_recoverscope() may modify the
977 if (sa6_recoverscope(&sin6_copy))
978 return (0); /* XXX: should be impossible */
979 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
981 if (sin6_copy.sin6_scope_id == zone)
988 * If the address matches one of our addresses,
989 * it should be a neighbor.
990 * If the address matches one of our on-link prefixes, it should be a
993 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
994 if (pr->ndpr_ifp != ifp)
997 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
1000 /* Always use the default FIB here. */
1001 rt = in6_rtalloc1((struct sockaddr *)&pr->ndpr_prefix,
1002 0, 0, RT_DEFAULT_FIB);
1006 * This is the case where multiple interfaces
1007 * have the same prefix, but only one is installed
1008 * into the routing table and that prefix entry
1009 * is not the one being examined here. In the case
1010 * where RADIX_MPATH is enabled, multiple route
1011 * entries (of the same rt_key value) will be
1012 * installed because the interface addresses all
1015 if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1016 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr)) {
1023 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1024 &addr->sin6_addr, &pr->ndpr_mask))
1029 * If the address is assigned on the node of the other side of
1030 * a p2p interface, the address should be a neighbor.
1032 dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr, RT_ALL_FIBS);
1033 if (dstaddr != NULL) {
1034 if (dstaddr->ifa_ifp == ifp) {
1042 * If the default router list is empty, all addresses are regarded
1043 * as on-link, and thus, as a neighbor.
1045 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV &&
1046 TAILQ_EMPTY(&V_nd_defrouter) &&
1047 V_nd6_defifindex == ifp->if_index) {
1056 * Detect if a given IPv6 address identifies a neighbor on a given link.
1057 * XXX: should take care of the destination of a p2p link?
1060 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
1062 struct llentry *lle;
1065 IF_AFDATA_UNLOCK_ASSERT(ifp);
1066 if (nd6_is_new_addr_neighbor(addr, ifp))
1070 * Even if the address matches none of our addresses, it might be
1071 * in the neighbor cache.
1073 IF_AFDATA_RLOCK(ifp);
1074 if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) {
1078 IF_AFDATA_RUNLOCK(ifp);
1083 * Free an nd6 llinfo entry.
1084 * Since the function would cause significant changes in the kernel, DO NOT
1085 * make it global, unless you have a strong reason for the change, and are sure
1086 * that the change is safe.
1088 static struct llentry *
1089 nd6_free(struct llentry *ln, int gc)
1091 struct llentry *next;
1092 struct nd_defrouter *dr;
1095 LLE_WLOCK_ASSERT(ln);
1098 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1099 * even though it is not harmful, it was not really necessary.
1103 nd6_llinfo_settimer_locked(ln, -1);
1105 ifp = ln->lle_tbl->llt_ifp;
1107 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1108 dr = defrouter_lookup(&L3_ADDR_SIN6(ln)->sin6_addr, ifp);
1110 if (dr != NULL && dr->expire &&
1111 ln->ln_state == ND6_LLINFO_STALE && gc) {
1113 * If the reason for the deletion is just garbage
1114 * collection, and the neighbor is an active default
1115 * router, do not delete it. Instead, reset the GC
1116 * timer using the router's lifetime.
1117 * Simply deleting the entry would affect default
1118 * router selection, which is not necessarily a good
1119 * thing, especially when we're using router preference
1121 * XXX: the check for ln_state would be redundant,
1122 * but we intentionally keep it just in case.
1124 if (dr->expire > time_uptime)
1125 nd6_llinfo_settimer_locked(ln,
1126 (dr->expire - time_uptime) * hz);
1128 nd6_llinfo_settimer_locked(ln,
1129 (long)V_nd6_gctimer * hz);
1131 next = LIST_NEXT(ln, lle_next);
1139 * Unreachablity of a router might affect the default
1140 * router selection and on-link detection of advertised
1145 * Temporarily fake the state to choose a new default
1146 * router and to perform on-link determination of
1147 * prefixes correctly.
1148 * Below the state will be set correctly,
1149 * or the entry itself will be deleted.
1151 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1154 if (ln->ln_router || dr) {
1157 * We need to unlock to avoid a LOR with rt6_flush() with the
1158 * rnh and for the calls to pfxlist_onlink_check() and
1159 * defrouter_select() in the block further down for calls
1160 * into nd6_lookup(). We still hold a ref.
1165 * rt6_flush must be called whether or not the neighbor
1166 * is in the Default Router List.
1167 * See a corresponding comment in nd6_na_input().
1169 rt6_flush(&L3_ADDR_SIN6(ln)->sin6_addr, ifp);
1174 * Since defrouter_select() does not affect the
1175 * on-link determination and MIP6 needs the check
1176 * before the default router selection, we perform
1179 pfxlist_onlink_check();
1182 * Refresh default router list.
1187 if (ln->ln_router || dr)
1192 * Before deleting the entry, remember the next entry as the
1193 * return value. We need this because pfxlist_onlink_check() above
1194 * might have freed other entries (particularly the old next entry) as
1195 * a side effect (XXX).
1197 next = LIST_NEXT(ln, lle_next);
1200 * Save to unlock. We still hold an extra reference and will not
1201 * free(9) in llentry_free() if someone else holds one as well.
1204 IF_AFDATA_LOCK(ifp);
1207 /* Guard against race with other llentry_free(). */
1208 if (ln->la_flags & LLE_LINKED) {
1212 LLE_FREE_LOCKED(ln);
1214 IF_AFDATA_UNLOCK(ifp);
1220 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1222 * XXX cost-effective methods?
1225 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1230 if ((dst6 == NULL) || (rt == NULL))
1234 IF_AFDATA_RLOCK(ifp);
1235 ln = nd6_lookup(dst6, ND6_EXCLUSIVE, NULL);
1236 IF_AFDATA_RUNLOCK(ifp);
1240 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1244 * if we get upper-layer reachability confirmation many times,
1245 * it is possible we have false information.
1249 if (ln->ln_byhint > V_nd6_maxnudhint) {
1254 ln->ln_state = ND6_LLINFO_REACHABLE;
1255 if (!ND6_LLINFO_PERMANENT(ln)) {
1256 nd6_llinfo_settimer_locked(ln,
1257 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1265 * Rejuvenate this function for routing operations related
1269 nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
1271 struct sockaddr_in6 *gateway;
1272 struct nd_defrouter *dr;
1275 gateway = (struct sockaddr_in6 *)rt->rt_gateway;
1286 * Only indirect routes are interesting.
1288 if ((rt->rt_flags & RTF_GATEWAY) == 0)
1291 * check for default route
1293 if (IN6_ARE_ADDR_EQUAL(&in6addr_any,
1294 &SIN6(rt_key(rt))->sin6_addr)) {
1296 dr = defrouter_lookup(&gateway->sin6_addr, ifp);
1306 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1308 struct in6_drlist *drl = (struct in6_drlist *)data;
1309 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1310 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1311 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1312 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1313 struct nd_defrouter *dr;
1314 struct nd_prefix *pr;
1315 int i = 0, error = 0;
1317 if (ifp->if_afdata[AF_INET6] == NULL)
1318 return (EPFNOSUPPORT);
1320 case SIOCGDRLST_IN6:
1322 * obsolete API, use sysctl under net.inet6.icmp6
1324 bzero(drl, sizeof(*drl));
1325 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1328 drl->defrouter[i].rtaddr = dr->rtaddr;
1329 in6_clearscope(&drl->defrouter[i].rtaddr);
1331 drl->defrouter[i].flags = dr->flags;
1332 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1333 drl->defrouter[i].expire = dr->expire +
1334 (time_second - time_uptime);
1335 drl->defrouter[i].if_index = dr->ifp->if_index;
1339 case SIOCGPRLST_IN6:
1341 * obsolete API, use sysctl under net.inet6.icmp6
1343 * XXX the structure in6_prlist was changed in backward-
1344 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1345 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1348 * XXX meaning of fields, especialy "raflags", is very
1349 * differnet between RA prefix list and RR/static prefix list.
1350 * how about separating ioctls into two?
1352 bzero(oprl, sizeof(*oprl));
1353 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1354 struct nd_pfxrouter *pfr;
1359 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1360 oprl->prefix[i].raflags = pr->ndpr_raf;
1361 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1362 oprl->prefix[i].vltime = pr->ndpr_vltime;
1363 oprl->prefix[i].pltime = pr->ndpr_pltime;
1364 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1365 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1366 oprl->prefix[i].expire = 0;
1370 /* XXX: we assume time_t is signed. */
1373 ((sizeof(maxexpire) * 8) - 1));
1374 if (pr->ndpr_vltime <
1375 maxexpire - pr->ndpr_lastupdate) {
1376 oprl->prefix[i].expire =
1377 pr->ndpr_lastupdate +
1379 (time_second - time_uptime);
1381 oprl->prefix[i].expire = maxexpire;
1385 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1387 #define RTRADDR oprl->prefix[i].advrtr[j]
1388 RTRADDR = pfr->router->rtaddr;
1389 in6_clearscope(&RTRADDR);
1394 oprl->prefix[i].advrtrs = j;
1395 oprl->prefix[i].origin = PR_ORIG_RA;
1401 case OSIOCGIFINFO_IN6:
1403 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1404 bzero(&ND, sizeof(ND));
1405 ND.linkmtu = IN6_LINKMTU(ifp);
1406 ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1407 ND.basereachable = ND_IFINFO(ifp)->basereachable;
1408 ND.reachable = ND_IFINFO(ifp)->reachable;
1409 ND.retrans = ND_IFINFO(ifp)->retrans;
1410 ND.flags = ND_IFINFO(ifp)->flags;
1411 ND.recalctm = ND_IFINFO(ifp)->recalctm;
1412 ND.chlim = ND_IFINFO(ifp)->chlim;
1414 case SIOCGIFINFO_IN6:
1415 ND = *ND_IFINFO(ifp);
1417 case SIOCSIFINFO_IN6:
1419 * used to change host variables from userland.
1420 * intented for a use on router to reflect RA configurations.
1422 /* 0 means 'unspecified' */
1423 if (ND.linkmtu != 0) {
1424 if (ND.linkmtu < IPV6_MMTU ||
1425 ND.linkmtu > IN6_LINKMTU(ifp)) {
1429 ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1432 if (ND.basereachable != 0) {
1433 int obasereachable = ND_IFINFO(ifp)->basereachable;
1435 ND_IFINFO(ifp)->basereachable = ND.basereachable;
1436 if (ND.basereachable != obasereachable)
1437 ND_IFINFO(ifp)->reachable =
1438 ND_COMPUTE_RTIME(ND.basereachable);
1440 if (ND.retrans != 0)
1441 ND_IFINFO(ifp)->retrans = ND.retrans;
1443 ND_IFINFO(ifp)->chlim = ND.chlim;
1445 case SIOCSIFINFO_FLAGS:
1448 struct in6_ifaddr *ia;
1450 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1451 !(ND.flags & ND6_IFF_IFDISABLED)) {
1452 /* ifdisabled 1->0 transision */
1455 * If the interface is marked as ND6_IFF_IFDISABLED and
1456 * has an link-local address with IN6_IFF_DUPLICATED,
1457 * do not clear ND6_IFF_IFDISABLED.
1458 * See RFC 4862, Section 5.4.5.
1460 int duplicated_linklocal = 0;
1463 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1464 if (ifa->ifa_addr->sa_family != AF_INET6)
1466 ia = (struct in6_ifaddr *)ifa;
1467 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1468 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) {
1469 duplicated_linklocal = 1;
1473 IF_ADDR_RUNLOCK(ifp);
1475 if (duplicated_linklocal) {
1476 ND.flags |= ND6_IFF_IFDISABLED;
1477 log(LOG_ERR, "Cannot enable an interface"
1478 " with a link-local address marked"
1481 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
1482 if (ifp->if_flags & IFF_UP)
1485 } else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1486 (ND.flags & ND6_IFF_IFDISABLED)) {
1487 /* ifdisabled 0->1 transision */
1488 /* Mark all IPv6 address as tentative. */
1490 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1492 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1493 if (ifa->ifa_addr->sa_family != AF_INET6)
1495 ia = (struct in6_ifaddr *)ifa;
1496 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1498 IF_ADDR_RUNLOCK(ifp);
1501 if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
1502 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1503 /* auto_linklocal 0->1 transision */
1505 /* If no link-local address on ifp, configure */
1506 ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
1507 in6_ifattach(ifp, NULL);
1508 } else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
1509 ifp->if_flags & IFF_UP) {
1511 * When the IF already has
1512 * ND6_IFF_AUTO_LINKLOCAL, no link-local
1513 * address is assigned, and IFF_UP, try to
1516 int haslinklocal = 0;
1519 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1520 if (ifa->ifa_addr->sa_family != AF_INET6)
1522 ia = (struct in6_ifaddr *)ifa;
1523 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) {
1528 IF_ADDR_RUNLOCK(ifp);
1530 in6_ifattach(ifp, NULL);
1534 ND_IFINFO(ifp)->flags = ND.flags;
1537 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1538 /* sync kernel routing table with the default router list */
1542 case SIOCSPFXFLUSH_IN6:
1544 /* flush all the prefix advertised by routers */
1545 struct nd_prefix *pr, *next;
1547 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, next) {
1548 struct in6_ifaddr *ia, *ia_next;
1550 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1553 /* do we really have to remove addresses as well? */
1554 /* XXXRW: in6_ifaddrhead locking. */
1555 TAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link,
1557 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1560 if (ia->ia6_ndpr == pr)
1561 in6_purgeaddr(&ia->ia_ifa);
1567 case SIOCSRTRFLUSH_IN6:
1569 /* flush all the default routers */
1570 struct nd_defrouter *dr, *next;
1573 TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, next) {
1579 case SIOCGNBRINFO_IN6:
1582 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1584 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1587 IF_AFDATA_RLOCK(ifp);
1588 ln = nd6_lookup(&nb_addr, 0, ifp);
1589 IF_AFDATA_RUNLOCK(ifp);
1595 nbi->state = ln->ln_state;
1596 nbi->asked = ln->la_asked;
1597 nbi->isrouter = ln->ln_router;
1598 if (ln->la_expire == 0)
1601 nbi->expire = ln->la_expire +
1602 (time_second - time_uptime);
1606 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1607 ndif->ifindex = V_nd6_defifindex;
1609 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1610 return (nd6_setdefaultiface(ndif->ifindex));
1616 * Create neighbor cache entry and cache link-layer address,
1617 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1620 * code - type dependent information
1623 * The caller of this function already acquired the ndp
1624 * cache table lock because the cache entry is returned.
1627 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1628 int lladdrlen, int type, int code)
1630 struct llentry *ln = NULL;
1637 uint16_t router = 0;
1638 struct sockaddr_in6 sin6;
1639 struct mbuf *chain = NULL;
1640 int static_route = 0;
1642 IF_AFDATA_UNLOCK_ASSERT(ifp);
1644 KASSERT(ifp != NULL, ("%s: ifp == NULL", __func__));
1645 KASSERT(from != NULL, ("%s: from == NULL", __func__));
1647 /* nothing must be updated for unspecified address */
1648 if (IN6_IS_ADDR_UNSPECIFIED(from))
1652 * Validation about ifp->if_addrlen and lladdrlen must be done in
1655 * XXX If the link does not have link-layer adderss, what should
1656 * we do? (ifp->if_addrlen == 0)
1657 * Spec says nothing in sections for RA, RS and NA. There's small
1658 * description on it in NS section (RFC 2461 7.2.3).
1660 flags = lladdr ? ND6_EXCLUSIVE : 0;
1661 IF_AFDATA_RLOCK(ifp);
1662 ln = nd6_lookup(from, flags, ifp);
1663 IF_AFDATA_RUNLOCK(ifp);
1665 flags |= ND6_EXCLUSIVE;
1666 IF_AFDATA_LOCK(ifp);
1667 ln = nd6_lookup(from, flags | ND6_CREATE, ifp);
1668 IF_AFDATA_UNLOCK(ifp);
1671 /* do nothing if static ndp is set */
1672 if (ln->la_flags & LLE_STATIC) {
1681 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
1682 if (olladdr && lladdr) {
1683 llchange = bcmp(lladdr, &ln->ll_addr,
1689 * newentry olladdr lladdr llchange (*=record)
1692 * 0 n y -- (3) * STALE
1694 * 0 y y y (5) * STALE
1695 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1696 * 1 -- y -- (7) * STALE
1699 if (lladdr) { /* (3-5) and (7) */
1701 * Record source link-layer address
1702 * XXX is it dependent to ifp->if_type?
1704 bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
1705 ln->la_flags |= LLE_VALID;
1706 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
1710 if ((!olladdr && lladdr != NULL) || /* (3) */
1711 (olladdr && lladdr != NULL && llchange)) { /* (5) */
1713 newstate = ND6_LLINFO_STALE;
1714 } else /* (1-2,4) */
1718 if (lladdr == NULL) /* (6) */
1719 newstate = ND6_LLINFO_NOSTATE;
1721 newstate = ND6_LLINFO_STALE;
1726 * Update the state of the neighbor cache.
1728 ln->ln_state = newstate;
1730 if (ln->ln_state == ND6_LLINFO_STALE) {
1731 if (ln->la_hold != NULL)
1732 nd6_grab_holdchain(ln, &chain, &sin6);
1733 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1734 /* probe right away */
1735 nd6_llinfo_settimer_locked((void *)ln, 0);
1740 * ICMP6 type dependent behavior.
1742 * NS: clear IsRouter if new entry
1743 * RS: clear IsRouter
1744 * RA: set IsRouter if there's lladdr
1745 * redir: clear IsRouter if new entry
1748 * The spec says that we must set IsRouter in the following cases:
1749 * - If lladdr exist, set IsRouter. This means (1-5).
1750 * - If it is old entry (!newentry), set IsRouter. This means (7).
1751 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1752 * A quetion arises for (1) case. (1) case has no lladdr in the
1753 * neighbor cache, this is similar to (6).
1754 * This case is rare but we figured that we MUST NOT set IsRouter.
1756 * newentry olladdr lladdr llchange NS RS RA redir
1758 * 0 n n -- (1) c ? s
1759 * 0 y n -- (2) c s s
1760 * 0 n y -- (3) c s s
1763 * 1 -- n -- (6) c c c s
1764 * 1 -- y -- (7) c c s c s
1768 switch (type & 0xff) {
1769 case ND_NEIGHBOR_SOLICIT:
1771 * New entry must have is_router flag cleared.
1773 if (is_newentry) /* (6-7) */
1778 * If the icmp is a redirect to a better router, always set the
1779 * is_router flag. Otherwise, if the entry is newly created,
1780 * clear the flag. [RFC 2461, sec 8.3]
1782 if (code == ND_REDIRECT_ROUTER)
1784 else if (is_newentry) /* (6-7) */
1787 case ND_ROUTER_SOLICIT:
1789 * is_router flag must always be cleared.
1793 case ND_ROUTER_ADVERT:
1795 * Mark an entry with lladdr as a router.
1797 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1798 (is_newentry && lladdr)) { /* (7) */
1805 static_route = (ln->la_flags & LLE_STATIC);
1806 router = ln->ln_router;
1808 if (flags & ND6_EXCLUSIVE)
1816 nd6_flush_holdchain(ifp, ifp, chain, &sin6);
1819 * When the link-layer address of a router changes, select the
1820 * best router again. In particular, when the neighbor entry is newly
1821 * created, it might affect the selection policy.
1822 * Question: can we restrict the first condition to the "is_newentry"
1824 * XXX: when we hear an RA from a new router with the link-layer
1825 * address option, defrouter_select() is called twice, since
1826 * defrtrlist_update called the function as well. However, I believe
1827 * we can compromise the overhead, since it only happens the first
1829 * XXX: although defrouter_select() should not have a bad effect
1830 * for those are not autoconfigured hosts, we explicitly avoid such
1833 if (do_update && router &&
1834 ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1836 * guaranteed recursion
1844 if (flags & ND6_EXCLUSIVE)
1855 nd6_slowtimo(void *arg)
1857 CURVNET_SET((struct vnet *) arg);
1858 struct nd_ifinfo *nd6if;
1861 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1862 nd6_slowtimo, curvnet);
1863 IFNET_RLOCK_NOSLEEP();
1864 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1865 if (ifp->if_afdata[AF_INET6] == NULL)
1867 nd6if = ND_IFINFO(ifp);
1868 if (nd6if->basereachable && /* already initialized */
1869 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1871 * Since reachable time rarely changes by router
1872 * advertisements, we SHOULD insure that a new random
1873 * value gets recomputed at least once every few hours.
1876 nd6if->recalctm = V_nd6_recalc_reachtm_interval;
1877 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1880 IFNET_RUNLOCK_NOSLEEP();
1885 nd6_grab_holdchain(struct llentry *ln, struct mbuf **chain,
1886 struct sockaddr_in6 *sin6)
1889 LLE_WLOCK_ASSERT(ln);
1891 *chain = ln->la_hold;
1893 memcpy(sin6, L3_ADDR_SIN6(ln), sizeof(*sin6));
1895 if (ln->ln_state == ND6_LLINFO_STALE) {
1898 * The first time we send a packet to a
1899 * neighbor whose entry is STALE, we have
1900 * to change the state to DELAY and a sets
1901 * a timer to expire in DELAY_FIRST_PROBE_TIME
1902 * seconds to ensure do neighbor unreachability
1903 * detection on expiration.
1907 ln->ln_state = ND6_LLINFO_DELAY;
1908 nd6_llinfo_settimer_locked(ln, (long)V_nd6_delay * hz);
1913 nd6_output_ifp(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1914 struct sockaddr_in6 *dst)
1918 struct ip6_hdr *ip6;
1922 mac_netinet6_nd6_send(ifp, m);
1926 * If called from nd6_ns_output() (NS), nd6_na_output() (NA),
1927 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA
1928 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND
1929 * to be diverted to user space. When re-injected into the kernel,
1930 * send_output() will directly dispatch them to the outgoing interface.
1932 if (send_sendso_input_hook != NULL) {
1933 mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL);
1935 ip6 = mtod(m, struct ip6_hdr *);
1936 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
1937 /* Use the SEND socket */
1938 error = send_sendso_input_hook(m, ifp, SND_OUT,
1940 /* -1 == no app on SEND socket */
1941 if (error == 0 || error != -1)
1946 m_clrprotoflags(m); /* Avoid confusing lower layers. */
1947 IP_PROBE(send, NULL, NULL, mtod(m, struct ip6_hdr *), ifp, NULL,
1948 mtod(m, struct ip6_hdr *));
1950 if ((ifp->if_flags & IFF_LOOPBACK) == 0)
1953 error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst, NULL);
1958 * IPv6 packet output - light version.
1959 * Checks if destination LLE exists and is in proper state
1960 * (e.g no modification required). If not true, fall back to
1964 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1965 struct sockaddr_in6 *dst, struct rtentry *rt0)
1967 struct llentry *ln = NULL;
1969 /* discard the packet if IPv6 operation is disabled on the interface */
1970 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
1972 return (ENETDOWN); /* better error? */
1975 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1978 if (nd6_need_cache(ifp) == 0)
1981 IF_AFDATA_RLOCK(ifp);
1982 ln = nd6_lookup(&dst->sin6_addr, 0, ifp);
1983 IF_AFDATA_RUNLOCK(ifp);
1986 * Perform fast path for the following cases:
1987 * 1) lle state is REACHABLE
1988 * 2) lle state is DELAY (NS message sentNS message sent)
1990 * Every other case involves lle modification, so we handle
1993 if (ln == NULL || (ln->ln_state != ND6_LLINFO_REACHABLE &&
1994 ln->ln_state != ND6_LLINFO_DELAY)) {
1995 /* Fall back to slow processing path */
1998 return (nd6_output_lle(ifp, origifp, m, dst));
2005 return (nd6_output_ifp(ifp, origifp, m, dst));
2010 * Output IPv6 packet - heavy version.
2011 * Function assume that either
2012 * 1) destination LLE does not exist, is invalid or stale, so
2013 * ND6_EXCLUSIVE lock needs to be acquired
2014 * 2) destination lle is provided (with ND6_EXCLUSIVE lock),
2015 * in that case packets are queued in &chain.
2019 nd6_output_lle(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
2020 struct sockaddr_in6 *dst)
2022 struct llentry *lle = NULL;
2025 KASSERT(m != NULL, ("NULL mbuf, nothing to send"));
2026 /* discard the packet if IPv6 operation is disabled on the interface */
2027 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
2029 return (ENETDOWN); /* better error? */
2032 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
2035 if (nd6_need_cache(ifp) == 0)
2039 * Address resolution or Neighbor Unreachability Detection
2041 * At this point, the destination of the packet must be a unicast
2042 * or an anycast address(i.e. not a multicast).
2045 IF_AFDATA_RLOCK(ifp);
2046 lle = nd6_lookup(&dst->sin6_addr, ND6_EXCLUSIVE, ifp);
2047 IF_AFDATA_RUNLOCK(ifp);
2048 if ((lle == NULL) && nd6_is_addr_neighbor(dst, ifp)) {
2050 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2051 * the condition below is not very efficient. But we believe
2052 * it is tolerable, because this should be a rare case.
2054 flags = ND6_CREATE | ND6_EXCLUSIVE;
2055 IF_AFDATA_LOCK(ifp);
2056 lle = nd6_lookup(&dst->sin6_addr, flags, ifp);
2057 IF_AFDATA_UNLOCK(ifp);
2061 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
2062 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
2063 char ip6buf[INET6_ADDRSTRLEN];
2065 "nd6_output: can't allocate llinfo for %s "
2067 ip6_sprintf(ip6buf, &dst->sin6_addr), lle);
2071 goto sendpkt; /* send anyway */
2074 LLE_WLOCK_ASSERT(lle);
2076 /* We don't have to do link-layer address resolution on a p2p link. */
2077 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2078 lle->ln_state < ND6_LLINFO_REACHABLE) {
2079 lle->ln_state = ND6_LLINFO_STALE;
2080 nd6_llinfo_settimer_locked(lle, (long)V_nd6_gctimer * hz);
2084 * The first time we send a packet to a neighbor whose entry is
2085 * STALE, we have to change the state to DELAY and a sets a timer to
2086 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2087 * neighbor unreachability detection on expiration.
2090 if (lle->ln_state == ND6_LLINFO_STALE) {
2092 lle->ln_state = ND6_LLINFO_DELAY;
2093 nd6_llinfo_settimer_locked(lle, (long)V_nd6_delay * hz);
2097 * If the neighbor cache entry has a state other than INCOMPLETE
2098 * (i.e. its link-layer address is already resolved), just
2101 if (lle->ln_state > ND6_LLINFO_INCOMPLETE)
2105 * There is a neighbor cache entry, but no ethernet address
2106 * response yet. Append this latest packet to the end of the
2107 * packet queue in the mbuf, unless the number of the packet
2108 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen,
2109 * the oldest packet in the queue will be removed.
2111 if (lle->ln_state == ND6_LLINFO_NOSTATE)
2112 lle->ln_state = ND6_LLINFO_INCOMPLETE;
2114 if (lle->la_hold != NULL) {
2115 struct mbuf *m_hold;
2119 for (m_hold = lle->la_hold; m_hold; m_hold = m_hold->m_nextpkt){
2121 if (m_hold->m_nextpkt == NULL) {
2122 m_hold->m_nextpkt = m;
2126 while (i >= V_nd6_maxqueuelen) {
2127 m_hold = lle->la_hold;
2128 lle->la_hold = lle->la_hold->m_nextpkt;
2137 * If there has been no NS for the neighbor after entering the
2138 * INCOMPLETE state, send the first solicitation.
2140 if (!ND6_LLINFO_PERMANENT(lle) && lle->la_asked == 0) {
2143 nd6_llinfo_settimer_locked(lle,
2144 (long)ND_IFINFO(ifp)->retrans * hz / 1000);
2146 nd6_ns_output(ifp, NULL, &dst->sin6_addr, lle, NULL);
2148 /* We did the lookup so we need to do the unlock here. */
2158 return (nd6_output_ifp(ifp, origifp, m, dst));
2163 nd6_flush_holdchain(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain,
2164 struct sockaddr_in6 *dst)
2166 struct mbuf *m, *m_head;
2167 struct ifnet *outifp;
2171 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2178 m_head = m_head->m_nextpkt;
2179 error = nd6_output_ifp(ifp, origifp, m, dst);
2184 * note that intermediate errors are blindly ignored - but this is
2185 * the same convention as used with nd6_output when called by
2193 nd6_need_cache(struct ifnet *ifp)
2196 * XXX: we currently do not make neighbor cache on any interface
2197 * other than ARCnet, Ethernet, FDDI and GIF.
2200 * - unidirectional tunnels needs no ND
2202 switch (ifp->if_type) {
2209 case IFT_INFINIBAND:
2211 case IFT_PROPVIRTUAL:
2219 * Add pernament ND6 link-layer record for given
2220 * interface address.
2222 * Very similar to IPv4 arp_ifinit(), but:
2223 * 1) IPv6 DAD is performed in different place
2224 * 2) It is called by IPv6 protocol stack in contrast to
2225 * arp_ifinit() which is typically called in SIOCSIFADDR
2226 * driver ioctl handler.
2230 nd6_add_ifa_lle(struct in6_ifaddr *ia)
2235 ifp = ia->ia_ifa.ifa_ifp;
2236 if (nd6_need_cache(ifp) == 0)
2238 IF_AFDATA_LOCK(ifp);
2239 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
2240 ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR |
2241 LLE_EXCLUSIVE), (struct sockaddr *)&ia->ia_addr);
2242 IF_AFDATA_UNLOCK(ifp);
2244 ln->la_expire = 0; /* for IPv6 this means permanent */
2245 ln->ln_state = ND6_LLINFO_REACHABLE;
2254 * Removes ALL lle records for interface address prefix.
2255 * XXXME: That's probably not we really want to do, we need
2256 * to remove address record only and keep other records
2257 * until we determine if given prefix is really going
2261 nd6_rem_ifa_lle(struct in6_ifaddr *ia)
2263 struct sockaddr_in6 mask, addr;
2266 ifp = ia->ia_ifa.ifa_ifp;
2267 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
2268 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
2269 lltable_prefix_free(AF_INET6, (struct sockaddr *)&addr,
2270 (struct sockaddr *)&mask, LLE_STATIC);
2274 * the callers of this function need to be re-worked to drop
2275 * the lle lock, drop here for now
2278 nd6_storelladdr(struct ifnet *ifp, struct mbuf *m,
2279 const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
2285 IF_AFDATA_UNLOCK_ASSERT(ifp);
2286 if (m != NULL && m->m_flags & M_MCAST) {
2287 switch (ifp->if_type) {
2294 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2299 return (EAFNOSUPPORT);
2305 * the entry should have been created in nd6_store_lladdr
2307 IF_AFDATA_RLOCK(ifp);
2308 ln = lla_lookup(LLTABLE6(ifp), 0, dst);
2309 IF_AFDATA_RUNLOCK(ifp);
2310 if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) {
2313 /* this could happen, if we could not allocate memory */
2318 bcopy(&ln->ll_addr, desten, ifp->if_addrlen);
2320 *pflags = ln->la_flags;
2323 * A *small* use after free race exists here
2329 clear_llinfo_pqueue(struct llentry *ln)
2331 struct mbuf *m_hold, *m_hold_next;
2333 for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) {
2334 m_hold_next = m_hold->m_nextpkt;
2342 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2343 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2345 SYSCTL_DECL(_net_inet6_icmp6);
2347 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2348 CTLFLAG_RD, nd6_sysctl_drlist, "");
2349 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2350 CTLFLAG_RD, nd6_sysctl_prlist, "");
2351 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen,
2352 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, "");
2353 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_gctimer,
2354 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_gctimer), (60 * 60 * 24), "");
2357 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2359 struct in6_defrouter d;
2360 struct nd_defrouter *dr;
2366 bzero(&d, sizeof(d));
2367 d.rtaddr.sin6_family = AF_INET6;
2368 d.rtaddr.sin6_len = sizeof(d.rtaddr);
2373 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
2374 d.rtaddr.sin6_addr = dr->rtaddr;
2375 error = sa6_recoverscope(&d.rtaddr);
2378 d.flags = dr->flags;
2379 d.rtlifetime = dr->rtlifetime;
2380 d.expire = dr->expire + (time_second - time_uptime);
2381 d.if_index = dr->ifp->if_index;
2382 error = SYSCTL_OUT(req, &d, sizeof(d));
2390 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2392 struct in6_prefix p;
2393 struct sockaddr_in6 s6;
2394 struct nd_prefix *pr;
2395 struct nd_pfxrouter *pfr;
2398 char ip6buf[INET6_ADDRSTRLEN];
2403 bzero(&p, sizeof(p));
2404 p.origin = PR_ORIG_RA;
2405 bzero(&s6, sizeof(s6));
2406 s6.sin6_family = AF_INET6;
2407 s6.sin6_len = sizeof(s6);
2412 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
2413 p.prefix = pr->ndpr_prefix;
2414 if (sa6_recoverscope(&p.prefix)) {
2415 log(LOG_ERR, "scope error in prefix list (%s)\n",
2416 ip6_sprintf(ip6buf, &p.prefix.sin6_addr));
2417 /* XXX: press on... */
2419 p.raflags = pr->ndpr_raf;
2420 p.prefixlen = pr->ndpr_plen;
2421 p.vltime = pr->ndpr_vltime;
2422 p.pltime = pr->ndpr_pltime;
2423 p.if_index = pr->ndpr_ifp->if_index;
2424 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2427 /* XXX: we assume time_t is signed. */
2429 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
2430 if (pr->ndpr_vltime < maxexpire - pr->ndpr_lastupdate)
2431 p.expire = pr->ndpr_lastupdate +
2433 (time_second - time_uptime);
2435 p.expire = maxexpire;
2437 p.refcnt = pr->ndpr_refcnt;
2438 p.flags = pr->ndpr_stateflags;
2440 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
2442 error = SYSCTL_OUT(req, &p, sizeof(p));
2445 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2446 s6.sin6_addr = pfr->router->rtaddr;
2447 if (sa6_recoverscope(&s6))
2449 "scope error in prefix list (%s)\n",
2450 ip6_sprintf(ip6buf, &pfr->router->rtaddr));
2451 error = SYSCTL_OUT(req, &s6, sizeof(s6));