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>
42 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
48 #include <sys/kernel.h>
49 #include <sys/protosw.h>
50 #include <sys/errno.h>
51 #include <sys/syslog.h>
52 #include <sys/rwlock.h>
53 #include <sys/queue.h>
55 #include <sys/sysctl.h>
58 #include <net/if_var.h>
59 #include <net/if_arc.h>
60 #include <net/if_dl.h>
61 #include <net/if_types.h>
62 #include <net/iso88025.h>
64 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_kdtrace.h>
69 #include <net/if_llatbl.h>
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))
89 MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
92 VNET_DEFINE(int, nd6_prune) = 1; /* walk list every 1 seconds */
93 VNET_DEFINE(int, nd6_delay) = 5; /* delay first probe time 5 second */
94 VNET_DEFINE(int, nd6_umaxtries) = 3; /* maximum unicast query */
95 VNET_DEFINE(int, nd6_mmaxtries) = 3; /* maximum multicast query */
96 VNET_DEFINE(int, nd6_useloopback) = 1; /* use loopback interface for
98 VNET_DEFINE(int, nd6_gctimer) = (60 * 60 * 24); /* 1 day: garbage
101 /* preventing too many loops in ND option parsing */
102 static VNET_DEFINE(int, nd6_maxndopt) = 10; /* max # of ND options allowed */
104 VNET_DEFINE(int, nd6_maxnudhint) = 0; /* max # of subsequent upper
106 static VNET_DEFINE(int, nd6_maxqueuelen) = 1; /* max pkts cached in unresolved
108 #define V_nd6_maxndopt VNET(nd6_maxndopt)
109 #define V_nd6_maxqueuelen VNET(nd6_maxqueuelen)
112 VNET_DEFINE(int, nd6_debug) = 1;
114 VNET_DEFINE(int, nd6_debug) = 0;
117 static eventhandler_tag lle_event_eh, iflladdr_event_eh;
119 VNET_DEFINE(struct nd_drhead, nd_defrouter);
120 VNET_DEFINE(struct nd_prhead, nd_prefix);
121 VNET_DEFINE(struct rwlock, nd6_lock);
122 VNET_DEFINE(uint64_t, nd6_list_genid);
123 VNET_DEFINE(struct mtx, nd6_onlink_mtx);
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(const 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 void nd6_free(struct llentry **, int);
136 static void nd6_free_redirect(const struct llentry *);
137 static void nd6_llinfo_timer(void *);
138 static void nd6_llinfo_settimer_locked(struct llentry *, long);
139 static void clear_llinfo_pqueue(struct llentry *);
140 static void nd6_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
141 static int nd6_resolve_slow(struct ifnet *, int, struct mbuf *,
142 const struct sockaddr_in6 *, u_char *, uint32_t *, struct llentry **);
143 static int nd6_need_cache(struct ifnet *);
146 static VNET_DEFINE(struct callout, nd6_slowtimo_ch);
147 #define V_nd6_slowtimo_ch VNET(nd6_slowtimo_ch)
149 VNET_DEFINE(struct callout, nd6_timer_ch);
150 #define V_nd6_timer_ch VNET(nd6_timer_ch)
153 nd6_lle_event(void *arg __unused, struct llentry *lle, int evt)
155 struct rt_addrinfo rtinfo;
156 struct sockaddr_in6 dst;
157 struct sockaddr_dl gw;
161 LLE_WLOCK_ASSERT(lle);
163 if (lltable_get_af(lle->lle_tbl) != AF_INET6)
167 case LLENTRY_RESOLVED:
169 KASSERT(lle->la_flags & LLE_VALID,
170 ("%s: %p resolved but not valid?", __func__, lle));
172 case LLENTRY_EXPIRED:
179 ifp = lltable_get_ifp(lle->lle_tbl);
181 bzero(&dst, sizeof(dst));
182 bzero(&gw, sizeof(gw));
183 bzero(&rtinfo, sizeof(rtinfo));
184 lltable_fill_sa_entry(lle, (struct sockaddr *)&dst);
185 dst.sin6_scope_id = in6_getscopezone(ifp,
186 in6_addrscope(&dst.sin6_addr));
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);
202 * A handler for interface link layer address change event.
205 nd6_iflladdr(void *arg __unused, struct ifnet *ifp)
208 lltable_update_ifaddr(LLTABLE6(ifp));
215 mtx_init(&V_nd6_onlink_mtx, "nd6 onlink", NULL, MTX_DEF);
216 rw_init(&V_nd6_lock, "nd6 list");
218 LIST_INIT(&V_nd_prefix);
219 TAILQ_INIT(&V_nd_defrouter);
222 callout_init(&V_nd6_slowtimo_ch, 0);
223 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
224 nd6_slowtimo, curvnet);
226 callout_init(&V_nd6_timer_ch, 0);
227 callout_reset(&V_nd6_timer_ch, hz, nd6_timer, curvnet);
230 if (IS_DEFAULT_VNET(curvnet)) {
231 lle_event_eh = EVENTHANDLER_REGISTER(lle_event, nd6_lle_event,
232 NULL, EVENTHANDLER_PRI_ANY);
233 iflladdr_event_eh = EVENTHANDLER_REGISTER(iflladdr_event,
234 nd6_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
243 callout_drain(&V_nd6_slowtimo_ch);
244 callout_drain(&V_nd6_timer_ch);
245 if (IS_DEFAULT_VNET(curvnet)) {
246 EVENTHANDLER_DEREGISTER(lle_event, lle_event_eh);
247 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_event_eh);
249 rw_destroy(&V_nd6_lock);
250 mtx_destroy(&V_nd6_onlink_mtx);
255 nd6_ifattach(struct ifnet *ifp)
257 struct nd_ifinfo *nd;
259 nd = malloc(sizeof(*nd), M_IP6NDP, M_WAITOK | M_ZERO);
262 nd->chlim = IPV6_DEFHLIM;
263 nd->basereachable = REACHABLE_TIME;
264 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
265 nd->retrans = RETRANS_TIMER;
267 nd->flags = ND6_IFF_PERFORMNUD;
269 /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
270 * XXXHRS: Clear ND6_IFF_AUTO_LINKLOCAL on an IFT_BRIDGE interface by
271 * default regardless of the V_ip6_auto_linklocal configuration to
272 * give a reasonable default behavior.
274 if ((V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
275 (ifp->if_flags & IFF_LOOPBACK))
276 nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
278 * A loopback interface does not need to accept RTADV.
279 * XXXHRS: Clear ND6_IFF_ACCEPT_RTADV on an IFT_BRIDGE interface by
280 * default regardless of the V_ip6_accept_rtadv configuration to
281 * prevent the interface from accepting RA messages arrived
282 * on one of the member interfaces with ND6_IFF_ACCEPT_RTADV.
284 if (V_ip6_accept_rtadv &&
285 !(ifp->if_flags & IFF_LOOPBACK) &&
286 (ifp->if_type != IFT_BRIDGE))
287 nd->flags |= ND6_IFF_ACCEPT_RTADV;
288 if (V_ip6_no_radr && !(ifp->if_flags & IFF_LOOPBACK))
289 nd->flags |= ND6_IFF_NO_RADR;
291 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
292 nd6_setmtu0(ifp, nd);
298 nd6_ifdetach(struct ifnet *ifp, struct nd_ifinfo *nd)
300 struct ifaddr *ifa, *next;
303 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
304 if (ifa->ifa_addr->sa_family != AF_INET6)
307 /* stop DAD processing */
310 IF_ADDR_RUNLOCK(ifp);
316 * Reset ND level link MTU. This function is called when the physical MTU
317 * changes, which means we might have to adjust the ND level MTU.
320 nd6_setmtu(struct ifnet *ifp)
322 if (ifp->if_afdata[AF_INET6] == NULL)
325 nd6_setmtu0(ifp, ND_IFINFO(ifp));
328 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
330 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
334 omaxmtu = ndi->maxmtu;
336 switch (ifp->if_type) {
338 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
341 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
344 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
347 ndi->maxmtu = ifp->if_mtu;
352 * Decreasing the interface MTU under IPV6 minimum MTU may cause
353 * undesirable situation. We thus notify the operator of the change
354 * explicitly. The check for omaxmtu is necessary to restrict the
355 * log to the case of changing the MTU, not initializing it.
357 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
358 log(LOG_NOTICE, "nd6_setmtu0: "
359 "new link MTU on %s (%lu) is too small for IPv6\n",
360 if_name(ifp), (unsigned long)ndi->maxmtu);
363 if (ndi->maxmtu > V_in6_maxmtu)
364 in6_setmaxmtu(); /* check all interfaces just in case */
369 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
372 bzero(ndopts, sizeof(*ndopts));
373 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
375 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
378 ndopts->nd_opts_done = 1;
379 ndopts->nd_opts_search = NULL;
384 * Take one ND option.
387 nd6_option(union nd_opts *ndopts)
389 struct nd_opt_hdr *nd_opt;
392 KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
393 KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
395 if (ndopts->nd_opts_search == NULL)
397 if (ndopts->nd_opts_done)
400 nd_opt = ndopts->nd_opts_search;
402 /* make sure nd_opt_len is inside the buffer */
403 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
404 bzero(ndopts, sizeof(*ndopts));
408 olen = nd_opt->nd_opt_len << 3;
411 * Message validation requires that all included
412 * options have a length that is greater than zero.
414 bzero(ndopts, sizeof(*ndopts));
418 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
419 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
420 /* option overruns the end of buffer, invalid */
421 bzero(ndopts, sizeof(*ndopts));
423 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
424 /* reached the end of options chain */
425 ndopts->nd_opts_done = 1;
426 ndopts->nd_opts_search = NULL;
432 * Parse multiple ND options.
433 * This function is much easier to use, for ND routines that do not need
434 * multiple options of the same type.
437 nd6_options(union nd_opts *ndopts)
439 struct nd_opt_hdr *nd_opt;
442 KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
443 KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
445 if (ndopts->nd_opts_search == NULL)
449 nd_opt = nd6_option(ndopts);
450 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
452 * Message validation requires that all included
453 * options have a length that is greater than zero.
455 ICMP6STAT_INC(icp6s_nd_badopt);
456 bzero(ndopts, sizeof(*ndopts));
463 switch (nd_opt->nd_opt_type) {
464 case ND_OPT_SOURCE_LINKADDR:
465 case ND_OPT_TARGET_LINKADDR:
467 case ND_OPT_REDIRECTED_HEADER:
469 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
471 "duplicated ND6 option found (type=%d)\n",
472 nd_opt->nd_opt_type));
475 ndopts->nd_opt_array[nd_opt->nd_opt_type]
479 case ND_OPT_PREFIX_INFORMATION:
480 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
481 ndopts->nd_opt_array[nd_opt->nd_opt_type]
484 ndopts->nd_opts_pi_end =
485 (struct nd_opt_prefix_info *)nd_opt;
487 /* What about ND_OPT_ROUTE_INFO? RFC 4191 */
488 case ND_OPT_RDNSS: /* RFC 6106 */
489 case ND_OPT_DNSSL: /* RFC 6106 */
491 * Silently ignore options we know and do not care about
497 * Unknown options must be silently ignored,
498 * to accommodate future extension to the protocol.
501 "nd6_options: unsupported option %d - "
502 "option ignored\n", nd_opt->nd_opt_type));
507 if (i > V_nd6_maxndopt) {
508 ICMP6STAT_INC(icp6s_nd_toomanyopt);
509 nd6log((LOG_INFO, "too many loop in nd opt\n"));
513 if (ndopts->nd_opts_done)
521 * ND6 timer routine to handle ND6 entries
524 nd6_llinfo_settimer_locked(struct llentry *ln, long tick)
528 LLE_WLOCK_ASSERT(ln);
533 canceled = callout_stop(&ln->lle_timer);
535 ln->la_expire = time_uptime + tick / hz;
537 if (tick > INT_MAX) {
538 ln->ln_ntick = tick - INT_MAX;
539 canceled = callout_reset(&ln->lle_timer, INT_MAX,
540 nd6_llinfo_timer, ln);
543 canceled = callout_reset(&ln->lle_timer, tick,
544 nd6_llinfo_timer, ln);
552 * Gets source address of the first packet in hold queue
553 * and stores it in @src.
554 * Returns pointer to @src (if hold queue is not empty) or NULL.
556 * Set noinline to be dtrace-friendly
558 static __noinline struct in6_addr *
559 nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src)
564 if (ln->la_hold == NULL)
568 * assume every packet in la_hold has the same IP header
571 if (sizeof(hdr) > m->m_len)
574 m_copydata(m, 0, sizeof(hdr), (caddr_t)&hdr);
581 * Checks if we need to switch from STALE state.
583 * RFC 4861 requires switching from STALE to DELAY state
584 * on first packet matching entry, waiting V_nd6_delay and
585 * transition to PROBE state (if upper layer confirmation was
588 * This code performs a bit differently:
589 * On packet hit we don't change state (but desired state
590 * can be guessed by control plane). However, after V_nd6_delay
591 * seconds code will transition to PROBE state (so DELAY state
592 * is kinda skipped in most situations).
594 * Typically, V_nd6_gctimer is bigger than V_nd6_delay, so
595 * we perform the following upon entering STALE state:
597 * 1) Arm timer to run each V_nd6_delay seconds to make sure that
598 * if packet was transmitted at the start of given interval, we
599 * would be able to switch to PROBE state in V_nd6_delay seconds
602 * 2) Reschedule timer until original V_nd6_gctimer expires keeping
603 * lle in STALE state (remaining timer value stored in lle_remtime).
605 * 3) Reschedule timer if packet was transmitted less that V_nd6_delay
608 * Returns non-zero value if the entry is still STALE (storing
609 * the next timer interval in @pdelay).
611 * Returns zero value if original timer expired or we need to switch to
612 * PROBE (store that in @do_switch variable).
615 nd6_is_stale(struct llentry *lle, long *pdelay, int *do_switch)
617 int nd_delay, nd_gctimer, r_skip_req;
622 nd_gctimer = V_nd6_gctimer;
623 nd_delay = V_nd6_delay;
626 r_skip_req = lle->r_skip_req;
627 lle_hittime = lle->lle_hittime;
630 if (r_skip_req > 0) {
633 * Nonzero r_skip_req value was set upon entering
634 * STALE state. Since value was not changed, no
635 * packets were passed using this lle. Ask for
636 * timer reschedule and keep STALE state.
638 delay = (long)(MIN(nd_gctimer, nd_delay));
640 if (lle->lle_remtime > delay)
641 lle->lle_remtime -= delay;
643 delay = lle->lle_remtime;
644 lle->lle_remtime = 0;
650 * The original ng6_gctime timeout ended,
651 * no more rescheduling.
661 * Packet received. Verify timestamp
663 delay = (long)(time_uptime - lle_hittime);
664 if (delay < nd_delay) {
667 * V_nd6_delay still not passed since the first
668 * hit in STALE state.
669 * Reshedule timer and return.
671 *pdelay = (long)(nd_delay - delay) * hz;
675 /* Request switching to probe */
682 * Switch @lle state to new state optionally arming timers.
684 * Set noinline to be dtrace-friendly
687 nd6_llinfo_setstate(struct llentry *lle, int newstate)
690 int nd_gctimer, nd_delay;
697 case ND6_LLINFO_INCOMPLETE:
698 ifp = lle->lle_tbl->llt_ifp;
699 delay = (long)ND_IFINFO(ifp)->retrans * hz / 1000;
701 case ND6_LLINFO_REACHABLE:
702 if (!ND6_LLINFO_PERMANENT(lle)) {
703 ifp = lle->lle_tbl->llt_ifp;
704 delay = (long)ND_IFINFO(ifp)->reachable * hz;
707 case ND6_LLINFO_STALE:
710 * Notify fast path that we want to know if any packet
711 * is transmitted by setting r_skip_req.
716 nd_delay = V_nd6_delay;
717 nd_gctimer = V_nd6_gctimer;
719 delay = (long)(MIN(nd_gctimer, nd_delay)) * hz;
720 remtime = (long)nd_gctimer * hz - delay;
722 case ND6_LLINFO_DELAY:
724 delay = (long)V_nd6_delay * hz;
729 nd6_llinfo_settimer_locked(lle, delay);
731 lle->lle_remtime = remtime;
732 lle->ln_state = newstate;
736 * Timer-dependent part of nd state machine.
738 * Set noinline to be dtrace-friendly
740 static __noinline void
741 nd6_llinfo_timer(void *arg)
744 struct in6_addr *dst, *pdst, *psrc, src;
746 struct nd_ifinfo *ndi;
747 int do_switch, send_ns;
750 KASSERT(arg != NULL, ("%s: arg NULL", __func__));
751 ln = (struct llentry *)arg;
752 ifp = lltable_get_ifp(ln->lle_tbl);
753 CURVNET_SET(ifp->if_vnet);
757 if (callout_pending(&ln->lle_timer)) {
759 * Here we are a bit odd here in the treatment of
760 * active/pending. If the pending bit is set, it got
761 * rescheduled before I ran. The active
762 * bit we ignore, since if it was stopped
763 * in ll_tablefree() and was currently running
764 * it would have return 0 so the code would
765 * not have deleted it since the callout could
766 * not be stopped so we want to go through
767 * with the delete here now. If the callout
768 * was restarted, the pending bit will be back on and
769 * we just want to bail since the callout_reset would
770 * return 1 and our reference would have been removed
771 * by nd6_llinfo_settimer_locked above since canceled
779 ndi = ND_IFINFO(ifp);
781 dst = &ln->r_l3addr.addr6;
784 if (ln->ln_ntick > 0) {
785 if (ln->ln_ntick > INT_MAX) {
786 ln->ln_ntick -= INT_MAX;
787 nd6_llinfo_settimer_locked(ln, INT_MAX);
790 nd6_llinfo_settimer_locked(ln, ln->ln_ntick);
795 if (ln->la_flags & LLE_STATIC) {
799 if (ln->la_flags & LLE_DELETED) {
804 switch (ln->ln_state) {
805 case ND6_LLINFO_INCOMPLETE:
806 if (ln->la_asked < V_nd6_mmaxtries) {
809 /* Send NS to multicast address */
812 struct mbuf *m = ln->la_hold;
817 * assuming every packet in la_hold has the
818 * same IP header. Send error after unlock.
823 clear_llinfo_pqueue(ln);
827 icmp6_error2(m, ICMP6_DST_UNREACH,
828 ICMP6_DST_UNREACH_ADDR, 0, ifp);
831 case ND6_LLINFO_REACHABLE:
832 if (!ND6_LLINFO_PERMANENT(ln))
833 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
836 case ND6_LLINFO_STALE:
837 if (nd6_is_stale(ln, &delay, &do_switch) != 0) {
840 * No packet has used this entry and GC timeout
841 * has not been passed. Reshedule timer and
844 nd6_llinfo_settimer_locked(ln, delay);
848 if (do_switch == 0) {
851 * GC timer has ended and entry hasn't been used.
852 * Run Garbage collector (RFC 4861, 5.3)
854 if (!ND6_LLINFO_PERMANENT(ln))
859 /* Entry has been used AND delay timer has ended. */
863 case ND6_LLINFO_DELAY:
864 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
867 nd6_llinfo_setstate(ln, ND6_LLINFO_PROBE);
870 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE); /* XXX */
872 case ND6_LLINFO_PROBE:
873 if (ln->la_asked < V_nd6_umaxtries) {
881 panic("%s: paths in a dark night can be confusing: %d",
882 __func__, ln->ln_state);
888 nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
889 psrc = nd6_llinfo_get_holdsrc(ln, &src);
892 nd6_ns_output(ifp, psrc, pdst, dst, NULL);
902 * ND6 timer routine to expire default route list and prefix list
907 CURVNET_SET((struct vnet *) arg);
908 struct nd_drhead drq;
909 struct nd_prhead prl;
910 struct nd_defrouter *dr, *ndr;
911 struct nd_prefix *pr, *npr;
912 struct in6_ifaddr *ia6, *nia6;
919 TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr)
920 if (dr->expire && dr->expire < time_uptime)
921 defrouter_unlink(dr, &drq);
924 while ((dr = TAILQ_FIRST(&drq)) != NULL) {
925 TAILQ_REMOVE(&drq, dr, dr_entry);
930 * expire interface addresses.
931 * in the past the loop was inside prefix expiry processing.
932 * However, from a stricter speci-confrmance standpoint, we should
933 * rather separate address lifetimes and prefix lifetimes.
935 * XXXRW: in6_ifaddrhead locking.
938 TAILQ_FOREACH_SAFE(ia6, &V_in6_ifaddrhead, ia_link, nia6) {
939 /* check address lifetime */
940 if (IFA6_IS_INVALID(ia6)) {
944 * If the expiring address is temporary, try
945 * regenerating a new one. This would be useful when
946 * we suspended a laptop PC, then turned it on after a
947 * period that could invalidate all temporary
948 * addresses. Although we may have to restart the
949 * loop (see below), it must be after purging the
950 * address. Otherwise, we'd see an infinite loop of
953 if (V_ip6_use_tempaddr &&
954 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
955 if (regen_tmpaddr(ia6) == 0)
959 in6_purgeaddr(&ia6->ia_ifa);
962 goto addrloop; /* XXX: see below */
963 } else if (IFA6_IS_DEPRECATED(ia6)) {
964 int oldflags = ia6->ia6_flags;
966 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
969 * If a temporary address has just become deprecated,
970 * regenerate a new one if possible.
972 if (V_ip6_use_tempaddr &&
973 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
974 (oldflags & IN6_IFF_DEPRECATED) == 0) {
976 if (regen_tmpaddr(ia6) == 0) {
978 * A new temporary address is
980 * XXX: this means the address chain
981 * has changed while we are still in
982 * the loop. Although the change
983 * would not cause disaster (because
984 * it's not a deletion, but an
985 * addition,) we'd rather restart the
986 * loop just for safety. Or does this
987 * significantly reduce performance??
992 } else if ((ia6->ia6_flags & IN6_IFF_TENTATIVE) != 0) {
994 * Schedule DAD for a tentative address. This happens
995 * if the interface was down or not running
996 * when the address was configured.
1000 delay = arc4random() %
1001 (MAX_RTR_SOLICITATION_DELAY * hz);
1002 nd6_dad_start((struct ifaddr *)ia6, delay);
1005 * Check status of the interface. If it is down,
1006 * mark the address as tentative for future DAD.
1008 if ((ia6->ia_ifp->if_flags & IFF_UP) == 0 ||
1009 (ia6->ia_ifp->if_drv_flags & IFF_DRV_RUNNING)
1011 (ND_IFINFO(ia6->ia_ifp)->flags &
1012 ND6_IFF_IFDISABLED) != 0) {
1013 ia6->ia6_flags &= ~IN6_IFF_DUPLICATED;
1014 ia6->ia6_flags |= IN6_IFF_TENTATIVE;
1017 * A new RA might have made a deprecated address
1020 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
1026 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
1028 * Expire prefixes. Since the pltime is only used for
1029 * autoconfigured addresses, pltime processing for prefixes is
1032 * Only unlink after all derived addresses have expired. This
1033 * may not occur until two hours after the prefix has expired
1034 * per RFC 4862. If the prefix expires before its derived
1035 * addresses, mark it off-link. This will be done automatically
1036 * after unlinking if no address references remain.
1038 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME ||
1039 time_uptime - pr->ndpr_lastupdate <= pr->ndpr_vltime)
1042 if (pr->ndpr_addrcnt == 0) {
1043 nd6_prefix_unlink(pr, &prl);
1046 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1047 genid = V_nd6_list_genid;
1051 (void)nd6_prefix_offlink(pr);
1052 ND6_ONLINK_UNLOCK();
1054 nd6_prefix_rele(pr);
1055 if (genid != V_nd6_list_genid)
1061 while ((pr = LIST_FIRST(&prl)) != NULL) {
1062 LIST_REMOVE(pr, ndpr_entry);
1066 callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz,
1067 nd6_timer, curvnet);
1073 * ia6 - deprecated/invalidated temporary address
1076 regen_tmpaddr(struct in6_ifaddr *ia6)
1080 struct in6_ifaddr *public_ifa6 = NULL;
1082 ifp = ia6->ia_ifa.ifa_ifp;
1084 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1085 struct in6_ifaddr *it6;
1087 if (ifa->ifa_addr->sa_family != AF_INET6)
1090 it6 = (struct in6_ifaddr *)ifa;
1092 /* ignore no autoconf addresses. */
1093 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1096 /* ignore autoconf addresses with different prefixes. */
1097 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
1101 * Now we are looking at an autoconf address with the same
1102 * prefix as ours. If the address is temporary and is still
1103 * preferred, do not create another one. It would be rare, but
1104 * could happen, for example, when we resume a laptop PC after
1107 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1108 !IFA6_IS_DEPRECATED(it6)) {
1114 * This is a public autoconf address that has the same prefix
1115 * as ours. If it is preferred, keep it. We can't break the
1116 * loop here, because there may be a still-preferred temporary
1117 * address with the prefix.
1119 if (!IFA6_IS_DEPRECATED(it6))
1122 if (public_ifa6 != NULL)
1123 ifa_ref(&public_ifa6->ia_ifa);
1124 IF_ADDR_RUNLOCK(ifp);
1126 if (public_ifa6 != NULL) {
1129 if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
1130 ifa_free(&public_ifa6->ia_ifa);
1131 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
1132 " tmp addr,errno=%d\n", e);
1135 ifa_free(&public_ifa6->ia_ifa);
1143 * Remove prefix and default router list entries corresponding to ifp. Neighbor
1144 * cache entries are freed in in6_domifdetach().
1147 nd6_purge(struct ifnet *ifp)
1149 struct nd_drhead drq;
1150 struct nd_prhead prl;
1151 struct nd_defrouter *dr, *ndr;
1152 struct nd_prefix *pr, *npr;
1158 * Nuke default router list entries toward ifp.
1159 * We defer removal of default router list entries that is installed
1160 * in the routing table, in order to keep additional side effects as
1161 * small as possible.
1164 TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
1168 defrouter_unlink(dr, &drq);
1170 TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
1174 defrouter_unlink(dr, &drq);
1178 * Remove prefixes on ifp. We should have already removed addresses on
1179 * this interface, so no addresses should be referencing these prefixes.
1181 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
1182 if (pr->ndpr_ifp == ifp)
1183 nd6_prefix_unlink(pr, &prl);
1187 /* Delete the unlinked router and prefix objects. */
1188 while ((dr = TAILQ_FIRST(&drq)) != NULL) {
1189 TAILQ_REMOVE(&drq, dr, dr_entry);
1192 while ((pr = LIST_FIRST(&prl)) != NULL) {
1193 LIST_REMOVE(pr, ndpr_entry);
1197 /* cancel default outgoing interface setting */
1198 if (V_nd6_defifindex == ifp->if_index)
1199 nd6_setdefaultiface(0);
1201 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1202 /* Refresh default router list. */
1208 * the caller acquires and releases the lock on the lltbls
1209 * Returns the llentry locked
1212 nd6_lookup(const struct in6_addr *addr6, int flags, struct ifnet *ifp)
1214 struct sockaddr_in6 sin6;
1217 bzero(&sin6, sizeof(sin6));
1218 sin6.sin6_len = sizeof(struct sockaddr_in6);
1219 sin6.sin6_family = AF_INET6;
1220 sin6.sin6_addr = *addr6;
1222 IF_AFDATA_LOCK_ASSERT(ifp);
1224 ln = lla_lookup(LLTABLE6(ifp), flags, (struct sockaddr *)&sin6);
1230 nd6_alloc(const struct in6_addr *addr6, int flags, struct ifnet *ifp)
1232 struct sockaddr_in6 sin6;
1235 bzero(&sin6, sizeof(sin6));
1236 sin6.sin6_len = sizeof(struct sockaddr_in6);
1237 sin6.sin6_family = AF_INET6;
1238 sin6.sin6_addr = *addr6;
1240 ln = lltable_alloc_entry(LLTABLE6(ifp), 0, (struct sockaddr *)&sin6);
1242 ln->ln_state = ND6_LLINFO_NOSTATE;
1248 * Test whether a given IPv6 address is a neighbor or not, ignoring
1249 * the actual neighbor cache. The neighbor cache is ignored in order
1250 * to not reenter the routing code from within itself.
1253 nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
1255 struct nd_prefix *pr;
1256 struct ifaddr *dstaddr;
1257 struct rt_addrinfo info;
1258 struct sockaddr_in6 rt_key;
1259 const struct sockaddr *dst6;
1264 * A link-local address is always a neighbor.
1265 * XXX: a link does not necessarily specify a single interface.
1267 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
1268 struct sockaddr_in6 sin6_copy;
1272 * We need sin6_copy since sa6_recoverscope() may modify the
1276 if (sa6_recoverscope(&sin6_copy))
1277 return (0); /* XXX: should be impossible */
1278 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
1280 if (sin6_copy.sin6_scope_id == zone)
1286 bzero(&rt_key, sizeof(rt_key));
1287 bzero(&info, sizeof(info));
1288 info.rti_info[RTAX_DST] = (struct sockaddr *)&rt_key;
1290 /* Always use the default FIB here. XXME - why? */
1291 fibnum = RT_DEFAULT_FIB;
1294 * If the address matches one of our addresses,
1295 * it should be a neighbor.
1296 * If the address matches one of our on-link prefixes, it should be a
1301 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1302 if (pr->ndpr_ifp != ifp)
1305 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1306 /* Always use the default FIB here. */
1307 dst6 = (const struct sockaddr *)&pr->ndpr_prefix;
1309 genid = V_nd6_list_genid;
1312 /* Restore length field before retrying lookup */
1313 rt_key.sin6_len = sizeof(rt_key);
1314 error = rib_lookup_info(fibnum, dst6, 0, 0, &info);
1317 if (genid != V_nd6_list_genid)
1323 * This is the case where multiple interfaces
1324 * have the same prefix, but only one is installed
1325 * into the routing table and that prefix entry
1326 * is not the one being examined here. In the case
1327 * where RADIX_MPATH is enabled, multiple route
1328 * entries (of the same rt_key value) will be
1329 * installed because the interface addresses all
1332 if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1337 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1338 &addr->sin6_addr, &pr->ndpr_mask)) {
1346 * If the address is assigned on the node of the other side of
1347 * a p2p interface, the address should be a neighbor.
1349 dstaddr = ifa_ifwithdstaddr((const struct sockaddr *)addr, RT_ALL_FIBS);
1350 if (dstaddr != NULL) {
1351 if (dstaddr->ifa_ifp == ifp) {
1359 * If the default router list is empty, all addresses are regarded
1360 * as on-link, and thus, as a neighbor.
1362 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV &&
1363 TAILQ_EMPTY(&V_nd_defrouter) &&
1364 V_nd6_defifindex == ifp->if_index) {
1373 * Detect if a given IPv6 address identifies a neighbor on a given link.
1374 * XXX: should take care of the destination of a p2p link?
1377 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
1379 struct llentry *lle;
1382 IF_AFDATA_UNLOCK_ASSERT(ifp);
1383 if (nd6_is_new_addr_neighbor(addr, ifp))
1387 * Even if the address matches none of our addresses, it might be
1388 * in the neighbor cache.
1390 IF_AFDATA_RLOCK(ifp);
1391 if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) {
1395 IF_AFDATA_RUNLOCK(ifp);
1400 * Free an nd6 llinfo entry.
1401 * Since the function would cause significant changes in the kernel, DO NOT
1402 * make it global, unless you have a strong reason for the change, and are sure
1403 * that the change is safe.
1405 * Set noinline to be dtrace-friendly
1407 static __noinline void
1408 nd6_free(struct llentry **lnp, int gc)
1412 struct nd_defrouter *dr;
1417 LLE_WLOCK_ASSERT(ln);
1420 ifp = lltable_get_ifp(ln->lle_tbl);
1421 if ((ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) != 0)
1422 dr = defrouter_lookup_locked(&ln->r_l3addr.addr6, ifp);
1427 if ((ln->la_flags & LLE_DELETED) == 0)
1428 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED);
1431 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1432 * even though it is not harmful, it was not really necessary.
1436 nd6_llinfo_settimer_locked(ln, -1);
1438 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1439 if (dr != NULL && dr->expire &&
1440 ln->ln_state == ND6_LLINFO_STALE && gc) {
1442 * If the reason for the deletion is just garbage
1443 * collection, and the neighbor is an active default
1444 * router, do not delete it. Instead, reset the GC
1445 * timer using the router's lifetime.
1446 * Simply deleting the entry would affect default
1447 * router selection, which is not necessarily a good
1448 * thing, especially when we're using router preference
1450 * XXX: the check for ln_state would be redundant,
1451 * but we intentionally keep it just in case.
1453 if (dr->expire > time_uptime)
1454 nd6_llinfo_settimer_locked(ln,
1455 (dr->expire - time_uptime) * hz);
1457 nd6_llinfo_settimer_locked(ln,
1458 (long)V_nd6_gctimer * hz);
1468 * Unreachablity of a router might affect the default
1469 * router selection and on-link detection of advertised
1474 * Temporarily fake the state to choose a new default
1475 * router and to perform on-link determination of
1476 * prefixes correctly.
1477 * Below the state will be set correctly,
1478 * or the entry itself will be deleted.
1480 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1483 if (ln->ln_router || dr) {
1486 * We need to unlock to avoid a LOR with rt6_flush() with the
1487 * rnh and for the calls to pfxlist_onlink_check() and
1488 * defrouter_select() in the block further down for calls
1489 * into nd6_lookup(). We still hold a ref.
1494 * rt6_flush must be called whether or not the neighbor
1495 * is in the Default Router List.
1496 * See a corresponding comment in nd6_na_input().
1498 rt6_flush(&ln->r_l3addr.addr6, ifp);
1503 * Since defrouter_select() does not affect the
1504 * on-link determination and MIP6 needs the check
1505 * before the default router selection, we perform
1508 pfxlist_onlink_check();
1511 * Refresh default router list.
1517 * If this entry was added by an on-link redirect, remove the
1518 * corresponding host route.
1520 if (ln->la_flags & LLE_REDIRECT)
1521 nd6_free_redirect(ln);
1523 if (ln->ln_router || dr)
1528 * Save to unlock. We still hold an extra reference and will not
1529 * free(9) in llentry_free() if someone else holds one as well.
1532 IF_AFDATA_LOCK(ifp);
1534 /* Guard against race with other llentry_free(). */
1535 if (ln->la_flags & LLE_LINKED) {
1536 /* Remove callout reference */
1538 lltable_unlink_entry(ln->lle_tbl, ln);
1540 IF_AFDATA_UNLOCK(ifp);
1548 nd6_isdynrte(const struct rtentry *rt, void *xap)
1551 if (rt->rt_flags == (RTF_UP | RTF_HOST | RTF_DYNAMIC))
1557 * Remove the rtentry for the given llentry,
1558 * both of which were installed by a redirect.
1561 nd6_free_redirect(const struct llentry *ln)
1564 struct sockaddr_in6 sin6;
1565 struct rt_addrinfo info;
1567 lltable_fill_sa_entry(ln, (struct sockaddr *)&sin6);
1568 memset(&info, 0, sizeof(info));
1569 info.rti_info[RTAX_DST] = (struct sockaddr *)&sin6;
1570 info.rti_filter = nd6_isdynrte;
1572 for (fibnum = 0; fibnum < rt_numfibs; fibnum++)
1573 rtrequest1_fib(RTM_DELETE, &info, NULL, fibnum);
1577 * Rejuvenate this function for routing operations related
1581 nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
1583 struct sockaddr_in6 *gateway;
1584 struct nd_defrouter *dr;
1587 gateway = (struct sockaddr_in6 *)rt->rt_gateway;
1598 * Only indirect routes are interesting.
1600 if ((rt->rt_flags & RTF_GATEWAY) == 0)
1603 * check for default route
1605 if (IN6_ARE_ADDR_EQUAL(&in6addr_any,
1606 &SIN6(rt_key(rt))->sin6_addr)) {
1607 dr = defrouter_lookup(&gateway->sin6_addr, ifp);
1619 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1621 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1622 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1623 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1626 if (ifp->if_afdata[AF_INET6] == NULL)
1627 return (EPFNOSUPPORT);
1629 case OSIOCGIFINFO_IN6:
1631 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1632 bzero(&ND, sizeof(ND));
1633 ND.linkmtu = IN6_LINKMTU(ifp);
1634 ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1635 ND.basereachable = ND_IFINFO(ifp)->basereachable;
1636 ND.reachable = ND_IFINFO(ifp)->reachable;
1637 ND.retrans = ND_IFINFO(ifp)->retrans;
1638 ND.flags = ND_IFINFO(ifp)->flags;
1639 ND.recalctm = ND_IFINFO(ifp)->recalctm;
1640 ND.chlim = ND_IFINFO(ifp)->chlim;
1642 case SIOCGIFINFO_IN6:
1643 ND = *ND_IFINFO(ifp);
1645 case SIOCSIFINFO_IN6:
1647 * used to change host variables from userland.
1648 * intended for a use on router to reflect RA configurations.
1650 /* 0 means 'unspecified' */
1651 if (ND.linkmtu != 0) {
1652 if (ND.linkmtu < IPV6_MMTU ||
1653 ND.linkmtu > IN6_LINKMTU(ifp)) {
1657 ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1660 if (ND.basereachable != 0) {
1661 int obasereachable = ND_IFINFO(ifp)->basereachable;
1663 ND_IFINFO(ifp)->basereachable = ND.basereachable;
1664 if (ND.basereachable != obasereachable)
1665 ND_IFINFO(ifp)->reachable =
1666 ND_COMPUTE_RTIME(ND.basereachable);
1668 if (ND.retrans != 0)
1669 ND_IFINFO(ifp)->retrans = ND.retrans;
1671 ND_IFINFO(ifp)->chlim = ND.chlim;
1673 case SIOCSIFINFO_FLAGS:
1676 struct in6_ifaddr *ia;
1678 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1679 !(ND.flags & ND6_IFF_IFDISABLED)) {
1680 /* ifdisabled 1->0 transision */
1683 * If the interface is marked as ND6_IFF_IFDISABLED and
1684 * has an link-local address with IN6_IFF_DUPLICATED,
1685 * do not clear ND6_IFF_IFDISABLED.
1686 * See RFC 4862, Section 5.4.5.
1689 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1690 if (ifa->ifa_addr->sa_family != AF_INET6)
1692 ia = (struct in6_ifaddr *)ifa;
1693 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1694 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1697 IF_ADDR_RUNLOCK(ifp);
1700 /* LLA is duplicated. */
1701 ND.flags |= ND6_IFF_IFDISABLED;
1702 log(LOG_ERR, "Cannot enable an interface"
1703 " with a link-local address marked"
1706 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
1707 if (ifp->if_flags & IFF_UP)
1710 } else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1711 (ND.flags & ND6_IFF_IFDISABLED)) {
1712 /* ifdisabled 0->1 transision */
1713 /* Mark all IPv6 address as tentative. */
1715 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1716 if (V_ip6_dad_count > 0 &&
1717 (ND_IFINFO(ifp)->flags & ND6_IFF_NO_DAD) == 0) {
1719 TAILQ_FOREACH(ifa, &ifp->if_addrhead,
1721 if (ifa->ifa_addr->sa_family !=
1724 ia = (struct in6_ifaddr *)ifa;
1725 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1727 IF_ADDR_RUNLOCK(ifp);
1731 if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
1732 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1733 /* auto_linklocal 0->1 transision */
1735 /* If no link-local address on ifp, configure */
1736 ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
1737 in6_ifattach(ifp, NULL);
1738 } else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
1739 ifp->if_flags & IFF_UP) {
1741 * When the IF already has
1742 * ND6_IFF_AUTO_LINKLOCAL, no link-local
1743 * address is assigned, and IFF_UP, try to
1747 TAILQ_FOREACH(ifa, &ifp->if_addrhead,
1749 if (ifa->ifa_addr->sa_family !=
1752 ia = (struct in6_ifaddr *)ifa;
1753 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1756 IF_ADDR_RUNLOCK(ifp);
1758 /* No LLA is configured. */
1759 in6_ifattach(ifp, NULL);
1763 ND_IFINFO(ifp)->flags = ND.flags;
1766 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1767 /* sync kernel routing table with the default router list */
1771 case SIOCSPFXFLUSH_IN6:
1773 /* flush all the prefix advertised by routers */
1774 struct in6_ifaddr *ia, *ia_next;
1775 struct nd_prefix *pr, *next;
1776 struct nd_prhead prl;
1781 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, next) {
1782 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1784 nd6_prefix_unlink(pr, &prl);
1788 while ((pr = LIST_FIRST(&prl)) != NULL) {
1789 LIST_REMOVE(pr, ndpr_entry);
1790 /* XXXRW: in6_ifaddrhead locking. */
1791 TAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link,
1793 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1796 if (ia->ia6_ndpr == pr)
1797 in6_purgeaddr(&ia->ia_ifa);
1803 case SIOCSRTRFLUSH_IN6:
1805 /* flush all the default routers */
1806 struct nd_drhead drq;
1807 struct nd_defrouter *dr;
1814 while ((dr = TAILQ_FIRST(&V_nd_defrouter)) != NULL)
1815 defrouter_unlink(dr, &drq);
1817 while ((dr = TAILQ_FIRST(&drq)) != NULL) {
1818 TAILQ_REMOVE(&drq, dr, dr_entry);
1825 case SIOCGNBRINFO_IN6:
1828 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1830 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1833 IF_AFDATA_RLOCK(ifp);
1834 ln = nd6_lookup(&nb_addr, 0, ifp);
1835 IF_AFDATA_RUNLOCK(ifp);
1841 nbi->state = ln->ln_state;
1842 nbi->asked = ln->la_asked;
1843 nbi->isrouter = ln->ln_router;
1844 if (ln->la_expire == 0)
1847 nbi->expire = ln->la_expire + ln->lle_remtime / hz +
1848 (time_second - time_uptime);
1852 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1853 ndif->ifindex = V_nd6_defifindex;
1855 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1856 return (nd6_setdefaultiface(ndif->ifindex));
1862 * Calculates new isRouter value based on provided parameters and
1866 nd6_is_router(int type, int code, int is_new, int old_addr, int new_addr,
1871 * ICMP6 type dependent behavior.
1873 * NS: clear IsRouter if new entry
1874 * RS: clear IsRouter
1875 * RA: set IsRouter if there's lladdr
1876 * redir: clear IsRouter if new entry
1879 * The spec says that we must set IsRouter in the following cases:
1880 * - If lladdr exist, set IsRouter. This means (1-5).
1881 * - If it is old entry (!newentry), set IsRouter. This means (7).
1882 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1883 * A quetion arises for (1) case. (1) case has no lladdr in the
1884 * neighbor cache, this is similar to (6).
1885 * This case is rare but we figured that we MUST NOT set IsRouter.
1887 * is_new old_addr new_addr NS RS RA redir
1894 * 1 -- n (6) c c c s
1895 * 1 -- y (7) c c s c s
1899 switch (type & 0xff) {
1900 case ND_NEIGHBOR_SOLICIT:
1902 * New entry must have is_router flag cleared.
1904 if (is_new) /* (6-7) */
1909 * If the icmp is a redirect to a better router, always set the
1910 * is_router flag. Otherwise, if the entry is newly created,
1911 * clear the flag. [RFC 2461, sec 8.3]
1913 if (code == ND_REDIRECT_ROUTER)
1916 if (is_new) /* (6-7) */
1920 case ND_ROUTER_SOLICIT:
1922 * is_router flag must always be cleared.
1926 case ND_ROUTER_ADVERT:
1928 * Mark an entry with lladdr as a router.
1930 if ((!is_new && (old_addr || new_addr)) || /* (2-5) */
1931 (is_new && new_addr)) { /* (7) */
1941 * Create neighbor cache entry and cache link-layer address,
1942 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1945 * code - type dependent information
1949 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1950 int lladdrlen, int type, int code)
1952 struct llentry *ln = NULL, *ln_tmp;
1958 uint16_t router = 0;
1959 struct sockaddr_in6 sin6;
1960 struct mbuf *chain = NULL;
1961 u_char linkhdr[LLE_MAX_LINKHDR];
1965 IF_AFDATA_UNLOCK_ASSERT(ifp);
1967 KASSERT(ifp != NULL, ("%s: ifp == NULL", __func__));
1968 KASSERT(from != NULL, ("%s: from == NULL", __func__));
1970 /* nothing must be updated for unspecified address */
1971 if (IN6_IS_ADDR_UNSPECIFIED(from))
1975 * Validation about ifp->if_addrlen and lladdrlen must be done in
1978 * XXX If the link does not have link-layer adderss, what should
1979 * we do? (ifp->if_addrlen == 0)
1980 * Spec says nothing in sections for RA, RS and NA. There's small
1981 * description on it in NS section (RFC 2461 7.2.3).
1983 flags = lladdr ? LLE_EXCLUSIVE : 0;
1984 IF_AFDATA_RLOCK(ifp);
1985 ln = nd6_lookup(from, flags, ifp);
1986 IF_AFDATA_RUNLOCK(ifp);
1989 flags |= LLE_EXCLUSIVE;
1990 ln = nd6_alloc(from, 0, ifp);
1995 * Since we already know all the data for the new entry,
1996 * fill it before insertion.
1998 if (lladdr != NULL) {
1999 linkhdrsize = sizeof(linkhdr);
2000 if (lltable_calc_llheader(ifp, AF_INET6, lladdr,
2001 linkhdr, &linkhdrsize, &lladdr_off) != 0)
2003 lltable_set_entry_addr(ifp, ln, linkhdr, linkhdrsize,
2007 IF_AFDATA_WLOCK(ifp);
2009 /* Prefer any existing lle over newly-created one */
2010 ln_tmp = nd6_lookup(from, LLE_EXCLUSIVE, ifp);
2012 lltable_link_entry(LLTABLE6(ifp), ln);
2013 IF_AFDATA_WUNLOCK(ifp);
2014 if (ln_tmp == NULL) {
2015 /* No existing lle, mark as new entry (6,7) */
2017 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
2018 if (lladdr != NULL) /* (7) */
2019 EVENTHANDLER_INVOKE(lle_event, ln,
2022 lltable_free_entry(LLTABLE6(ifp), ln);
2027 /* do nothing if static ndp is set */
2028 if ((ln->la_flags & LLE_STATIC)) {
2029 if (flags & LLE_EXCLUSIVE)
2036 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
2037 if (olladdr && lladdr) {
2038 llchange = bcmp(lladdr, ln->ll_addr,
2040 } else if (!olladdr && lladdr)
2046 * newentry olladdr lladdr llchange (*=record)
2049 * 0 n y y (3) * STALE
2051 * 0 y y y (5) * STALE
2052 * 1 -- n -- (6) NOSTATE(= PASSIVE)
2053 * 1 -- y -- (7) * STALE
2057 if (is_newentry == 0 && llchange != 0) {
2058 do_update = 1; /* (3,5) */
2061 * Record source link-layer address
2062 * XXX is it dependent to ifp->if_type?
2064 linkhdrsize = sizeof(linkhdr);
2065 if (lltable_calc_llheader(ifp, AF_INET6, lladdr,
2066 linkhdr, &linkhdrsize, &lladdr_off) != 0)
2069 if (lltable_try_set_entry_addr(ifp, ln, linkhdr, linkhdrsize,
2071 /* Entry was deleted */
2075 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
2077 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
2079 if (ln->la_hold != NULL)
2080 nd6_grab_holdchain(ln, &chain, &sin6);
2083 /* Calculates new router status */
2084 router = nd6_is_router(type, code, is_newentry, olladdr,
2085 lladdr != NULL ? 1 : 0, ln->ln_router);
2087 ln->ln_router = router;
2088 /* Mark non-router redirects with special flag */
2089 if ((type & 0xFF) == ND_REDIRECT && code != ND_REDIRECT_ROUTER)
2090 ln->la_flags |= LLE_REDIRECT;
2092 if (flags & LLE_EXCLUSIVE)
2098 nd6_flush_holdchain(ifp, ifp, chain, &sin6);
2101 * When the link-layer address of a router changes, select the
2102 * best router again. In particular, when the neighbor entry is newly
2103 * created, it might affect the selection policy.
2104 * Question: can we restrict the first condition to the "is_newentry"
2106 * XXX: when we hear an RA from a new router with the link-layer
2107 * address option, defrouter_select() is called twice, since
2108 * defrtrlist_update called the function as well. However, I believe
2109 * we can compromise the overhead, since it only happens the first
2111 * XXX: although defrouter_select() should not have a bad effect
2112 * for those are not autoconfigured hosts, we explicitly avoid such
2115 if ((do_update || is_newentry) && router &&
2116 ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
2118 * guaranteed recursion
2125 nd6_slowtimo(void *arg)
2127 CURVNET_SET((struct vnet *) arg);
2128 struct nd_ifinfo *nd6if;
2131 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
2132 nd6_slowtimo, curvnet);
2133 IFNET_RLOCK_NOSLEEP();
2134 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2135 if (ifp->if_afdata[AF_INET6] == NULL)
2137 nd6if = ND_IFINFO(ifp);
2138 if (nd6if->basereachable && /* already initialized */
2139 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
2141 * Since reachable time rarely changes by router
2142 * advertisements, we SHOULD insure that a new random
2143 * value gets recomputed at least once every few hours.
2146 nd6if->recalctm = V_nd6_recalc_reachtm_interval;
2147 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
2150 IFNET_RUNLOCK_NOSLEEP();
2155 nd6_grab_holdchain(struct llentry *ln, struct mbuf **chain,
2156 struct sockaddr_in6 *sin6)
2159 LLE_WLOCK_ASSERT(ln);
2161 *chain = ln->la_hold;
2163 lltable_fill_sa_entry(ln, (struct sockaddr *)sin6);
2165 if (ln->ln_state == ND6_LLINFO_STALE) {
2168 * The first time we send a packet to a
2169 * neighbor whose entry is STALE, we have
2170 * to change the state to DELAY and a sets
2171 * a timer to expire in DELAY_FIRST_PROBE_TIME
2172 * seconds to ensure do neighbor unreachability
2173 * detection on expiration.
2176 nd6_llinfo_setstate(ln, ND6_LLINFO_DELAY);
2181 nd6_output_ifp(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
2182 struct sockaddr_in6 *dst, struct route *ro)
2186 struct ip6_hdr *ip6;
2190 mac_netinet6_nd6_send(ifp, m);
2194 * If called from nd6_ns_output() (NS), nd6_na_output() (NA),
2195 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA
2196 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND
2197 * to be diverted to user space. When re-injected into the kernel,
2198 * send_output() will directly dispatch them to the outgoing interface.
2200 if (send_sendso_input_hook != NULL) {
2201 mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL);
2203 ip6 = mtod(m, struct ip6_hdr *);
2204 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
2205 /* Use the SEND socket */
2206 error = send_sendso_input_hook(m, ifp, SND_OUT,
2208 /* -1 == no app on SEND socket */
2209 if (error == 0 || error != -1)
2214 m_clrprotoflags(m); /* Avoid confusing lower layers. */
2215 IP_PROBE(send, NULL, NULL, mtod(m, struct ip6_hdr *), ifp, NULL,
2216 mtod(m, struct ip6_hdr *));
2218 if ((ifp->if_flags & IFF_LOOPBACK) == 0)
2221 error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst, ro);
2226 * Lookup link headerfor @sa_dst address. Stores found
2227 * data in @desten buffer. Copy of lle ln_flags can be also
2228 * saved in @pflags if @pflags is non-NULL.
2230 * If destination LLE does not exists or lle state modification
2231 * is required, call "slow" version.
2234 * - 0 on success (address copied to buffer).
2235 * - EWOULDBLOCK (no local error, but address is still unresolved)
2236 * - other errors (alloc failure, etc)
2239 nd6_resolve(struct ifnet *ifp, int is_gw, struct mbuf *m,
2240 const struct sockaddr *sa_dst, u_char *desten, uint32_t *pflags,
2241 struct llentry **plle)
2243 struct llentry *ln = NULL;
2244 const struct sockaddr_in6 *dst6;
2249 dst6 = (const struct sockaddr_in6 *)sa_dst;
2251 /* discard the packet if IPv6 operation is disabled on the interface */
2252 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
2254 return (ENETDOWN); /* better error? */
2257 if (m != NULL && m->m_flags & M_MCAST) {
2258 switch (ifp->if_type) {
2265 ETHER_MAP_IPV6_MULTICAST(&dst6->sin6_addr,
2270 return (EAFNOSUPPORT);
2274 IF_AFDATA_RLOCK(ifp);
2275 ln = nd6_lookup(&dst6->sin6_addr, plle ? LLE_EXCLUSIVE : LLE_UNLOCKED,
2277 if (ln != NULL && (ln->r_flags & RLLE_VALID) != 0) {
2278 /* Entry found, let's copy lle info */
2279 bcopy(ln->r_linkdata, desten, ln->r_hdrlen);
2281 *pflags = LLE_VALID | (ln->r_flags & RLLE_IFADDR);
2282 /* Check if we have feedback request from nd6 timer */
2283 if (ln->r_skip_req != 0) {
2285 ln->r_skip_req = 0; /* Notify that entry was used */
2286 ln->lle_hittime = time_uptime;
2294 IF_AFDATA_RUNLOCK(ifp);
2296 } else if (plle && ln)
2298 IF_AFDATA_RUNLOCK(ifp);
2300 return (nd6_resolve_slow(ifp, 0, m, dst6, desten, pflags, plle));
2305 * Do L2 address resolution for @sa_dst address. Stores found
2306 * address in @desten buffer. Copy of lle ln_flags can be also
2307 * saved in @pflags if @pflags is non-NULL.
2310 * Function assume that destination LLE does not exist,
2311 * is invalid or stale, so LLE_EXCLUSIVE lock needs to be acquired.
2313 * Set noinline to be dtrace-friendly
2315 static __noinline int
2316 nd6_resolve_slow(struct ifnet *ifp, int flags, struct mbuf *m,
2317 const struct sockaddr_in6 *dst, u_char *desten, uint32_t *pflags,
2318 struct llentry **plle)
2320 struct llentry *lle = NULL, *lle_tmp;
2321 struct in6_addr *psrc, src;
2322 int send_ns, ll_len;
2326 * Address resolution or Neighbor Unreachability Detection
2328 * At this point, the destination of the packet must be a unicast
2329 * or an anycast address(i.e. not a multicast).
2332 IF_AFDATA_RLOCK(ifp);
2333 lle = nd6_lookup(&dst->sin6_addr, LLE_EXCLUSIVE, ifp);
2334 IF_AFDATA_RUNLOCK(ifp);
2335 if ((lle == NULL) && nd6_is_addr_neighbor(dst, ifp)) {
2337 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2338 * the condition below is not very efficient. But we believe
2339 * it is tolerable, because this should be a rare case.
2341 lle = nd6_alloc(&dst->sin6_addr, 0, ifp);
2343 char ip6buf[INET6_ADDRSTRLEN];
2345 "nd6_output: can't allocate llinfo for %s "
2347 ip6_sprintf(ip6buf, &dst->sin6_addr), lle);
2352 IF_AFDATA_WLOCK(ifp);
2354 /* Prefer any existing entry over newly-created one */
2355 lle_tmp = nd6_lookup(&dst->sin6_addr, LLE_EXCLUSIVE, ifp);
2356 if (lle_tmp == NULL)
2357 lltable_link_entry(LLTABLE6(ifp), lle);
2358 IF_AFDATA_WUNLOCK(ifp);
2359 if (lle_tmp != NULL) {
2360 lltable_free_entry(LLTABLE6(ifp), lle);
2367 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
2377 LLE_WLOCK_ASSERT(lle);
2380 * The first time we send a packet to a neighbor whose entry is
2381 * STALE, we have to change the state to DELAY and a sets a timer to
2382 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2383 * neighbor unreachability detection on expiration.
2386 if (lle->ln_state == ND6_LLINFO_STALE)
2387 nd6_llinfo_setstate(lle, ND6_LLINFO_DELAY);
2390 * If the neighbor cache entry has a state other than INCOMPLETE
2391 * (i.e. its link-layer address is already resolved), just
2394 if (lle->ln_state > ND6_LLINFO_INCOMPLETE) {
2395 if (flags & LLE_ADDRONLY) {
2396 lladdr = lle->ll_addr;
2397 ll_len = ifp->if_addrlen;
2399 lladdr = lle->r_linkdata;
2400 ll_len = lle->r_hdrlen;
2402 bcopy(lladdr, desten, ll_len);
2404 *pflags = lle->la_flags;
2414 * There is a neighbor cache entry, but no ethernet address
2415 * response yet. Append this latest packet to the end of the
2416 * packet queue in the mbuf. When it exceeds nd6_maxqueuelen,
2417 * the oldest packet in the queue will be removed.
2420 if (lle->la_hold != NULL) {
2421 struct mbuf *m_hold;
2425 for (m_hold = lle->la_hold; m_hold; m_hold = m_hold->m_nextpkt){
2427 if (m_hold->m_nextpkt == NULL) {
2428 m_hold->m_nextpkt = m;
2432 while (i >= V_nd6_maxqueuelen) {
2433 m_hold = lle->la_hold;
2434 lle->la_hold = lle->la_hold->m_nextpkt;
2443 * If there has been no NS for the neighbor after entering the
2444 * INCOMPLETE state, send the first solicitation.
2445 * Note that for newly-created lle la_asked will be 0,
2446 * so we will transition from ND6_LLINFO_NOSTATE to
2447 * ND6_LLINFO_INCOMPLETE state here.
2451 if (lle->la_asked == 0) {
2454 psrc = nd6_llinfo_get_holdsrc(lle, &src);
2456 nd6_llinfo_setstate(lle, ND6_LLINFO_INCOMPLETE);
2460 nd6_ns_output(ifp, psrc, NULL, &dst->sin6_addr, NULL);
2462 return (EWOULDBLOCK);
2466 * Do L2 address resolution for @sa_dst address. Stores found
2467 * address in @desten buffer. Copy of lle ln_flags can be also
2468 * saved in @pflags if @pflags is non-NULL.
2471 * - 0 on success (address copied to buffer).
2472 * - EWOULDBLOCK (no local error, but address is still unresolved)
2473 * - other errors (alloc failure, etc)
2476 nd6_resolve_addr(struct ifnet *ifp, int flags, const struct sockaddr *dst,
2477 char *desten, uint32_t *pflags)
2481 flags |= LLE_ADDRONLY;
2482 error = nd6_resolve_slow(ifp, flags, NULL,
2483 (const struct sockaddr_in6 *)dst, desten, pflags, NULL);
2488 nd6_flush_holdchain(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain,
2489 struct sockaddr_in6 *dst)
2491 struct mbuf *m, *m_head;
2492 struct ifnet *outifp;
2496 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2503 m_head = m_head->m_nextpkt;
2504 error = nd6_output_ifp(ifp, origifp, m, dst, NULL);
2509 * note that intermediate errors are blindly ignored
2515 nd6_need_cache(struct ifnet *ifp)
2518 * XXX: we currently do not make neighbor cache on any interface
2519 * other than ARCnet, Ethernet, FDDI and GIF.
2522 * - unidirectional tunnels needs no ND
2524 switch (ifp->if_type) {
2531 case IFT_INFINIBAND:
2533 case IFT_PROPVIRTUAL:
2541 * Add pernament ND6 link-layer record for given
2542 * interface address.
2544 * Very similar to IPv4 arp_ifinit(), but:
2545 * 1) IPv6 DAD is performed in different place
2546 * 2) It is called by IPv6 protocol stack in contrast to
2547 * arp_ifinit() which is typically called in SIOCSIFADDR
2548 * driver ioctl handler.
2552 nd6_add_ifa_lle(struct in6_ifaddr *ia)
2555 struct llentry *ln, *ln_tmp;
2556 struct sockaddr *dst;
2558 ifp = ia->ia_ifa.ifa_ifp;
2559 if (nd6_need_cache(ifp) == 0)
2562 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
2563 dst = (struct sockaddr *)&ia->ia_addr;
2564 ln = lltable_alloc_entry(LLTABLE6(ifp), LLE_IFADDR, dst);
2568 IF_AFDATA_WLOCK(ifp);
2570 /* Unlink any entry if exists */
2571 ln_tmp = lla_lookup(LLTABLE6(ifp), LLE_EXCLUSIVE, dst);
2573 lltable_unlink_entry(LLTABLE6(ifp), ln_tmp);
2574 lltable_link_entry(LLTABLE6(ifp), ln);
2575 IF_AFDATA_WUNLOCK(ifp);
2578 EVENTHANDLER_INVOKE(lle_event, ln_tmp, LLENTRY_EXPIRED);
2579 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
2583 llentry_free(ln_tmp);
2589 * Removes either all lle entries for given @ia, or lle
2590 * corresponding to @ia address.
2593 nd6_rem_ifa_lle(struct in6_ifaddr *ia, int all)
2595 struct sockaddr_in6 mask, addr;
2596 struct sockaddr *saddr, *smask;
2599 ifp = ia->ia_ifa.ifa_ifp;
2600 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
2601 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
2602 saddr = (struct sockaddr *)&addr;
2603 smask = (struct sockaddr *)&mask;
2606 lltable_prefix_free(AF_INET6, saddr, smask, LLE_STATIC);
2608 lltable_delete_addr(LLTABLE6(ifp), LLE_IFADDR, saddr);
2612 clear_llinfo_pqueue(struct llentry *ln)
2614 struct mbuf *m_hold, *m_hold_next;
2616 for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) {
2617 m_hold_next = m_hold->m_nextpkt;
2624 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2625 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2627 SYSCTL_DECL(_net_inet6_icmp6);
2628 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2629 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
2630 NULL, 0, nd6_sysctl_drlist, "S,in6_defrouter",
2631 "NDP default router list");
2632 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2633 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
2634 NULL, 0, nd6_sysctl_prlist, "S,in6_prefix",
2636 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen,
2637 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, "");
2638 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_gctimer,
2639 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_gctimer), (60 * 60 * 24), "");
2642 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2644 struct in6_defrouter d;
2645 struct nd_defrouter *dr;
2648 if (req->newptr != NULL)
2651 error = sysctl_wire_old_buffer(req, 0);
2655 bzero(&d, sizeof(d));
2656 d.rtaddr.sin6_family = AF_INET6;
2657 d.rtaddr.sin6_len = sizeof(d.rtaddr);
2660 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
2661 d.rtaddr.sin6_addr = dr->rtaddr;
2662 error = sa6_recoverscope(&d.rtaddr);
2665 d.flags = dr->raflags;
2666 d.rtlifetime = dr->rtlifetime;
2667 d.expire = dr->expire + (time_second - time_uptime);
2668 d.if_index = dr->ifp->if_index;
2669 error = SYSCTL_OUT(req, &d, sizeof(d));
2678 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2680 struct in6_prefix p;
2681 struct sockaddr_in6 s6;
2682 struct nd_prefix *pr;
2683 struct nd_pfxrouter *pfr;
2686 char ip6buf[INET6_ADDRSTRLEN];
2691 error = sysctl_wire_old_buffer(req, 0);
2695 bzero(&p, sizeof(p));
2696 p.origin = PR_ORIG_RA;
2697 bzero(&s6, sizeof(s6));
2698 s6.sin6_family = AF_INET6;
2699 s6.sin6_len = sizeof(s6);
2702 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
2703 p.prefix = pr->ndpr_prefix;
2704 if (sa6_recoverscope(&p.prefix)) {
2705 log(LOG_ERR, "scope error in prefix list (%s)\n",
2706 ip6_sprintf(ip6buf, &p.prefix.sin6_addr));
2707 /* XXX: press on... */
2709 p.raflags = pr->ndpr_raf;
2710 p.prefixlen = pr->ndpr_plen;
2711 p.vltime = pr->ndpr_vltime;
2712 p.pltime = pr->ndpr_pltime;
2713 p.if_index = pr->ndpr_ifp->if_index;
2714 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2717 /* XXX: we assume time_t is signed. */
2719 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
2720 if (pr->ndpr_vltime < maxexpire - pr->ndpr_lastupdate)
2721 p.expire = pr->ndpr_lastupdate +
2723 (time_second - time_uptime);
2725 p.expire = maxexpire;
2727 p.refcnt = pr->ndpr_addrcnt;
2728 p.flags = pr->ndpr_stateflags;
2730 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
2732 error = SYSCTL_OUT(req, &p, sizeof(p));
2735 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2736 s6.sin6_addr = pfr->router->rtaddr;
2737 if (sa6_recoverscope(&s6))
2739 "scope error in prefix list (%s)\n",
2740 ip6_sprintf(ip6buf, &pfr->router->rtaddr));
2741 error = SYSCTL_OUT(req, &s6, sizeof(s6));