drm: Improve integration with syscons. Move taskqueue handling to syscons.
[dragonfly.git] / sys / netinet6 / nd6.c
blobdeb03dc1e5c1d0df3e0293e6158b6f9ed8286988
1 /* $FreeBSD: src/sys/netinet6/nd6.c,v 1.2.2.15 2003/05/06 06:46:58 suz Exp $ */
2 /* $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/callout.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/protosw.h>
46 #include <sys/errno.h>
47 #include <sys/syslog.h>
48 #include <sys/queue.h>
49 #include <sys/sysctl.h>
50 #include <sys/mutex.h>
52 #include <sys/thread2.h>
53 #include <sys/mutex2.h>
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59 #include <net/netisr2.h>
60 #include <net/netmsg2.h>
62 #include <netinet/in.h>
63 #include <netinet/if_ether.h>
64 #include <netinet6/in6_var.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #include <netinet6/nd6.h>
68 #include <netinet/icmp6.h>
70 #include <net/net_osdep.h>
72 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
73 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
75 #define SIN6(s) ((struct sockaddr_in6 *)s)
76 #define SDL(s) ((struct sockaddr_dl *)s)
78 /* timer values */
79 int nd6_prune = 1; /* walk list every 1 seconds */
80 int nd6_delay = 5; /* delay first probe time 5 second */
81 int nd6_umaxtries = 3; /* maximum unicast query */
82 int nd6_mmaxtries = 3; /* maximum multicast query */
83 int nd6_useloopback = 1; /* use loopback interface for local traffic */
84 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
86 /* preventing too many loops in ND option parsing */
87 int nd6_maxndopt = 10; /* max # of ND options allowed */
89 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
91 #ifdef ND6_DEBUG
92 int nd6_debug = 1;
93 #else
94 int nd6_debug = 0;
95 #endif
97 /* for debugging? */
98 static int nd6_inuse, nd6_allocated;
100 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
101 struct nd_drhead nd_defrouter;
102 struct nd_prhead nd_prefix = { 0 };
103 struct mtx nd6_mtx = MTX_INITIALIZER("nd6");
105 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
106 static struct sockaddr_in6 all1_sa;
108 static void nd6_setmtu0 (struct ifnet *, struct nd_ifinfo *);
109 static int regen_tmpaddr (struct in6_ifaddr *);
110 static void nd6_slowtimo(void *);
111 static void nd6_slowtimo_dispatch(netmsg_t);
112 static void nd6_timer(void *);
113 static void nd6_timer_dispatch(netmsg_t);
115 static struct callout nd6_slowtimo_ch;
116 static struct netmsg_base nd6_slowtimo_netmsg;
118 static struct callout nd6_timer_ch;
119 static struct netmsg_base nd6_timer_netmsg;
121 void
122 nd6_init(void)
124 static int nd6_init_done = 0;
125 int i;
127 if (nd6_init_done) {
128 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
129 return;
132 all1_sa.sin6_family = AF_INET6;
133 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
134 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
135 all1_sa.sin6_addr.s6_addr[i] = 0xff;
137 /* initialization of the default router list */
138 TAILQ_INIT(&nd_defrouter);
140 nd6_init_done = 1;
142 /* start timer */
143 callout_init_mp(&nd6_slowtimo_ch);
144 netmsg_init(&nd6_slowtimo_netmsg, NULL, &netisr_adone_rport,
145 MSGF_PRIORITY, nd6_slowtimo_dispatch);
146 callout_reset_bycpu(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
147 nd6_slowtimo, NULL, 0);
150 struct nd_ifinfo *
151 nd6_ifattach(struct ifnet *ifp)
153 struct nd_ifinfo *nd;
155 nd = (struct nd_ifinfo *)kmalloc(sizeof(*nd), M_IP6NDP,
156 M_WAITOK | M_ZERO);
158 nd->initialized = 1;
160 nd->chlim = IPV6_DEFHLIM;
161 nd->basereachable = REACHABLE_TIME;
162 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
163 nd->retrans = RETRANS_TIMER;
166 * Note that the default value of ip6_accept_rtadv is 0, which means
167 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
168 * here.
170 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
172 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
173 nd6_setmtu0(ifp, nd);
174 return nd;
177 void
178 nd6_ifdetach(struct nd_ifinfo *nd)
180 kfree(nd, M_IP6NDP);
184 * Reset ND level link MTU. This function is called when the physical MTU
185 * changes, which means we might have to adjust the ND level MTU.
187 void
188 nd6_setmtu(struct ifnet *ifp)
190 nd6_setmtu0(ifp, ND_IFINFO(ifp));
193 struct netmsg_nd6setmtu {
194 struct netmsg_base nmsg;
195 struct ifnet *ifp;
196 struct nd_ifinfo *ndi;
199 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
200 static void
201 nd6_setmtu0_dispatch(netmsg_t msg)
203 struct netmsg_nd6setmtu *nmsg = (struct netmsg_nd6setmtu *)msg;
204 struct ifnet *ifp = nmsg->ifp;
205 struct nd_ifinfo *ndi = nmsg->ndi;
206 uint32_t omaxmtu;
208 omaxmtu = ndi->maxmtu;
210 switch (ifp->if_type) {
211 case IFT_ETHER:
212 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
213 break;
214 case IFT_IEEE1394: /* XXX should be IEEE1394MTU(1500) */
215 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
216 break;
217 #ifdef IFT_IEEE80211
218 case IFT_IEEE80211: /* XXX should be IEEE80211MTU(1500) */
219 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
220 break;
221 #endif
222 default:
223 ndi->maxmtu = ifp->if_mtu;
224 break;
228 * Decreasing the interface MTU under IPV6 minimum MTU may cause
229 * undesirable situation. We thus notify the operator of the change
230 * explicitly. The check for omaxmtu is necessary to restrict the
231 * log to the case of changing the MTU, not initializing it.
233 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
234 log(LOG_NOTICE, "nd6_setmtu0: "
235 "new link MTU on %s (%lu) is too small for IPv6\n",
236 if_name(ifp), (unsigned long)ndi->maxmtu);
239 if (ndi->maxmtu > in6_maxmtu)
240 in6_setmaxmtu(); /* check all interfaces just in case */
242 lwkt_replymsg(&nmsg->nmsg.lmsg, 0);
245 void
246 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
248 struct netmsg_nd6setmtu nmsg;
250 netmsg_init(&nmsg.nmsg, NULL, &curthread->td_msgport, 0,
251 nd6_setmtu0_dispatch);
252 nmsg.ifp = ifp;
253 nmsg.ndi = ndi;
254 lwkt_domsg(netisr_cpuport(0), &nmsg.nmsg.lmsg, 0);
257 void
258 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
260 bzero(ndopts, sizeof(*ndopts));
261 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
262 ndopts->nd_opts_last
263 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
265 if (icmp6len == 0) {
266 ndopts->nd_opts_done = 1;
267 ndopts->nd_opts_search = NULL;
272 * Take one ND option.
274 struct nd_opt_hdr *
275 nd6_option(union nd_opts *ndopts)
277 struct nd_opt_hdr *nd_opt;
278 int olen;
280 if (!ndopts)
281 panic("ndopts == NULL in nd6_option");
282 if (!ndopts->nd_opts_last)
283 panic("uninitialized ndopts in nd6_option");
284 if (!ndopts->nd_opts_search)
285 return NULL;
286 if (ndopts->nd_opts_done)
287 return NULL;
289 nd_opt = ndopts->nd_opts_search;
291 /* make sure nd_opt_len is inside the buffer */
292 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
293 bzero(ndopts, sizeof(*ndopts));
294 return NULL;
297 olen = nd_opt->nd_opt_len << 3;
298 if (olen == 0) {
300 * Message validation requires that all included
301 * options have a length that is greater than zero.
303 bzero(ndopts, sizeof(*ndopts));
304 return NULL;
307 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
308 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
309 /* option overruns the end of buffer, invalid */
310 bzero(ndopts, sizeof(*ndopts));
311 return NULL;
312 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
313 /* reached the end of options chain */
314 ndopts->nd_opts_done = 1;
315 ndopts->nd_opts_search = NULL;
317 return nd_opt;
321 * Parse multiple ND options.
322 * This function is much easier to use, for ND routines that do not need
323 * multiple options of the same type.
326 nd6_options(union nd_opts *ndopts)
328 struct nd_opt_hdr *nd_opt;
329 int i = 0;
331 if (!ndopts)
332 panic("ndopts == NULL in nd6_options");
333 if (!ndopts->nd_opts_last)
334 panic("uninitialized ndopts in nd6_options");
335 if (!ndopts->nd_opts_search)
336 return 0;
338 while (1) {
339 nd_opt = nd6_option(ndopts);
340 if (!nd_opt && !ndopts->nd_opts_last) {
342 * Message validation requires that all included
343 * options have a length that is greater than zero.
345 icmp6stat.icp6s_nd_badopt++;
346 bzero(ndopts, sizeof(*ndopts));
347 return -1;
350 if (!nd_opt)
351 goto skip1;
353 switch (nd_opt->nd_opt_type) {
354 case ND_OPT_SOURCE_LINKADDR:
355 case ND_OPT_TARGET_LINKADDR:
356 case ND_OPT_MTU:
357 case ND_OPT_REDIRECTED_HEADER:
358 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
359 nd6log((LOG_INFO,
360 "duplicated ND6 option found (type=%d)\n",
361 nd_opt->nd_opt_type));
362 /* XXX bark? */
363 } else {
364 ndopts->nd_opt_array[nd_opt->nd_opt_type]
365 = nd_opt;
367 break;
368 case ND_OPT_PREFIX_INFORMATION:
369 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
370 ndopts->nd_opt_array[nd_opt->nd_opt_type]
371 = nd_opt;
373 ndopts->nd_opts_pi_end =
374 (struct nd_opt_prefix_info *)nd_opt;
375 break;
376 default:
378 * Unknown options must be silently ignored,
379 * to accomodate future extension to the protocol.
381 nd6log((LOG_DEBUG,
382 "nd6_options: unsupported option %d - "
383 "option ignored\n", nd_opt->nd_opt_type));
386 skip1:
387 i++;
388 if (i > nd6_maxndopt) {
389 icmp6stat.icp6s_nd_toomanyopt++;
390 nd6log((LOG_INFO, "too many loop in nd opt\n"));
391 break;
394 if (ndopts->nd_opts_done)
395 break;
398 return 0;
402 * ND6 timer routine to expire default route list and prefix list
404 static void
405 nd6_timer_dispatch(netmsg_t nmsg)
407 struct llinfo_nd6 *ln;
408 struct nd_defrouter *dr;
409 struct nd_prefix *pr;
410 struct ifnet *ifp;
411 struct in6_ifaddr *ia6, *nia6;
413 ASSERT_IN_NETISR(0);
415 crit_enter();
416 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
417 crit_exit();
419 mtx_lock(&nd6_mtx);
421 ln = llinfo_nd6.ln_next;
422 while (ln && ln != &llinfo_nd6) {
423 struct rtentry *rt;
424 struct sockaddr_in6 *dst;
425 struct llinfo_nd6 *next = ln->ln_next;
426 /* XXX: used for the DELAY case only: */
427 struct nd_ifinfo *ndi = NULL;
429 if ((rt = ln->ln_rt) == NULL) {
430 ln = next;
431 continue;
433 if ((ifp = rt->rt_ifp) == NULL) {
434 ln = next;
435 continue;
437 ndi = ND_IFINFO(ifp);
438 dst = (struct sockaddr_in6 *)rt_key(rt);
440 if (ln->ln_expire > time_uptime) {
441 ln = next;
442 continue;
445 /* sanity check */
446 if (!rt)
447 panic("rt=0 in nd6_timer(ln=%p)", ln);
448 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
449 panic("rt_llinfo(%p) is not equal to ln(%p)",
450 rt->rt_llinfo, ln);
451 if (!dst)
452 panic("dst=0 in nd6_timer(ln=%p)", ln);
454 switch (ln->ln_state) {
455 case ND6_LLINFO_INCOMPLETE:
456 if (ln->ln_asked < nd6_mmaxtries) {
457 ln->ln_asked++;
458 ln->ln_expire = time_uptime +
459 ND_IFINFO(ifp)->retrans / 1000;
460 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
461 ln, 0);
462 } else {
463 struct mbuf *m = ln->ln_hold;
464 if (m) {
465 if (rt->rt_ifp) {
467 * Fake rcvif to make ICMP error
468 * more helpful in diagnosing
469 * for the receiver.
470 * XXX: should we consider
471 * older rcvif?
473 m->m_pkthdr.rcvif = rt->rt_ifp;
475 icmp6_error(m, ICMP6_DST_UNREACH,
476 ICMP6_DST_UNREACH_ADDR, 0);
477 ln->ln_hold = NULL;
479 next = nd6_free(rt);
481 break;
482 case ND6_LLINFO_REACHABLE:
483 if (ln->ln_expire) {
484 ln->ln_state = ND6_LLINFO_STALE;
485 ln->ln_expire = time_uptime + nd6_gctimer;
487 break;
489 case ND6_LLINFO_STALE:
490 /* Garbage Collection(RFC 2461 5.3) */
491 if (ln->ln_expire)
492 next = nd6_free(rt);
493 break;
495 case ND6_LLINFO_DELAY:
496 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD)) {
497 /* We need NUD */
498 ln->ln_asked = 1;
499 ln->ln_state = ND6_LLINFO_PROBE;
500 ln->ln_expire = time_uptime +
501 ndi->retrans / 1000;
502 nd6_ns_output(ifp, &dst->sin6_addr,
503 &dst->sin6_addr,
504 ln, 0);
505 } else {
506 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
507 ln->ln_expire = time_uptime + nd6_gctimer;
509 break;
510 case ND6_LLINFO_PROBE:
511 if (ln->ln_asked < nd6_umaxtries) {
512 ln->ln_asked++;
513 ln->ln_expire = time_uptime +
514 ND_IFINFO(ifp)->retrans / 1000;
515 nd6_ns_output(ifp, &dst->sin6_addr,
516 &dst->sin6_addr, ln, 0);
517 } else {
518 next = nd6_free(rt);
520 break;
522 ln = next;
525 /* expire default router list */
526 dr = TAILQ_FIRST(&nd_defrouter);
527 while (dr) {
528 if (dr->expire && dr->expire < time_uptime) {
529 struct nd_defrouter *t;
530 t = TAILQ_NEXT(dr, dr_entry);
531 defrtrlist_del(dr);
532 dr = t;
533 } else {
534 dr = TAILQ_NEXT(dr, dr_entry);
539 * expire interface addresses.
540 * in the past the loop was inside prefix expiry processing.
541 * However, from a stricter speci-confrmance standpoint, we should
542 * rather separate address lifetimes and prefix lifetimes.
544 addrloop:
545 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
546 nia6 = ia6->ia_next;
547 /* check address lifetime */
548 if (IFA6_IS_INVALID(ia6)) {
549 int regen = 0;
552 * If the expiring address is temporary, try
553 * regenerating a new one. This would be useful when
554 * we suspended a laptop PC, then turned it on after a
555 * period that could invalidate all temporary
556 * addresses. Although we may have to restart the
557 * loop (see below), it must be after purging the
558 * address. Otherwise, we'd see an infinite loop of
559 * regeneration.
561 if (ip6_use_tempaddr &&
562 (ia6->ia6_flags & IN6_IFF_TEMPORARY)) {
563 if (regen_tmpaddr(ia6) == 0)
564 regen = 1;
567 in6_purgeaddr(&ia6->ia_ifa);
569 if (regen)
570 goto addrloop; /* XXX: see below */
572 if (IFA6_IS_DEPRECATED(ia6)) {
573 int oldflags = ia6->ia6_flags;
575 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
578 * If a temporary address has just become deprecated,
579 * regenerate a new one if possible.
581 if (ip6_use_tempaddr &&
582 (ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
583 !(oldflags & IN6_IFF_DEPRECATED)) {
585 if (regen_tmpaddr(ia6) == 0) {
587 * A new temporary address is
588 * generated.
589 * XXX: this means the address chain
590 * has changed while we are still in
591 * the loop. Although the change
592 * would not cause disaster (because
593 * it's not a deletion, but an
594 * addition,) we'd rather restart the
595 * loop just for safety. Or does this
596 * significantly reduce performance??
598 goto addrloop;
601 } else {
603 * A new RA might have made a deprecated address
604 * preferred.
606 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
610 /* expire prefix list */
611 pr = nd_prefix.lh_first;
612 while (pr) {
614 * check prefix lifetime.
615 * since pltime is just for autoconf, pltime processing for
616 * prefix is not necessary.
618 if (pr->ndpr_expire && pr->ndpr_expire < time_uptime) {
619 struct nd_prefix *t;
620 t = pr->ndpr_next;
623 * address expiration and prefix expiration are
624 * separate. NEVER perform in6_purgeaddr here.
627 prelist_remove(pr);
628 pr = t;
629 } else
630 pr = pr->ndpr_next;
633 mtx_unlock(&nd6_mtx);
635 callout_reset(&nd6_timer_ch, nd6_prune * hz, nd6_timer, NULL);
638 static void
639 nd6_timer(void *arg __unused)
641 struct lwkt_msg *lmsg = &nd6_timer_netmsg.lmsg;
643 KASSERT(mycpuid == 0, ("not on cpu0"));
644 crit_enter();
645 if (lmsg->ms_flags & MSGF_DONE)
646 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
647 crit_exit();
650 void
651 nd6_timer_init(void)
653 callout_init_mp(&nd6_timer_ch);
654 netmsg_init(&nd6_timer_netmsg, NULL, &netisr_adone_rport,
655 MSGF_PRIORITY, nd6_timer_dispatch);
656 callout_reset_bycpu(&nd6_timer_ch, hz, nd6_timer, NULL, 0);
659 static int
660 regen_tmpaddr(struct in6_ifaddr *ia6) /* deprecated/invalidated temporary
661 address */
663 struct ifaddr_container *ifac;
664 struct ifnet *ifp;
665 struct in6_ifaddr *public_ifa6 = NULL;
667 ifp = ia6->ia_ifa.ifa_ifp;
668 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
669 struct ifaddr *ifa = ifac->ifa;
670 struct in6_ifaddr *it6;
672 if (ifa->ifa_addr->sa_family != AF_INET6)
673 continue;
675 it6 = (struct in6_ifaddr *)ifa;
677 /* ignore no autoconf addresses. */
678 if (!(it6->ia6_flags & IN6_IFF_AUTOCONF))
679 continue;
681 /* ignore autoconf addresses with different prefixes. */
682 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
683 continue;
686 * Now we are looking at an autoconf address with the same
687 * prefix as ours. If the address is temporary and is still
688 * preferred, do not create another one. It would be rare, but
689 * could happen, for example, when we resume a laptop PC after
690 * a long period.
692 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) &&
693 !IFA6_IS_DEPRECATED(it6)) {
694 public_ifa6 = NULL;
695 break;
699 * This is a public autoconf address that has the same prefix
700 * as ours. If it is preferred, keep it. We can't break the
701 * loop here, because there may be a still-preferred temporary
702 * address with the prefix.
704 if (!IFA6_IS_DEPRECATED(it6))
705 public_ifa6 = it6;
708 if (public_ifa6 != NULL) {
709 int e;
711 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
712 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
713 " tmp addr,errno=%d\n", e);
714 return (-1);
716 return (0);
719 return (-1);
723 * Nuke neighbor cache/prefix/default router management table, right before
724 * ifp goes away.
726 void
727 nd6_purge(struct ifnet *ifp)
729 struct llinfo_nd6 *ln, *nln;
730 struct nd_defrouter *dr, *ndr, drany;
731 struct nd_prefix *pr, *npr;
733 /* Nuke default router list entries toward ifp */
734 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
736 * The first entry of the list may be stored in
737 * the routing table, so we'll delete it later.
739 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
740 ndr = TAILQ_NEXT(dr, dr_entry);
741 if (dr->ifp == ifp)
742 defrtrlist_del(dr);
744 dr = TAILQ_FIRST(&nd_defrouter);
745 if (dr->ifp == ifp)
746 defrtrlist_del(dr);
749 /* Nuke prefix list entries toward ifp */
750 for (pr = nd_prefix.lh_first; pr; pr = npr) {
751 npr = pr->ndpr_next;
752 if (pr->ndpr_ifp == ifp) {
754 * Previously, pr->ndpr_addr is removed as well,
755 * but I strongly believe we don't have to do it.
756 * nd6_purge() is only called from in6_ifdetach(),
757 * which removes all the associated interface addresses
758 * by itself.
759 * (jinmei@kame.net 20010129)
761 prelist_remove(pr);
765 /* cancel default outgoing interface setting */
766 if (nd6_defifindex == ifp->if_index)
767 nd6_setdefaultiface(0);
769 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
770 /* refresh default router list */
771 bzero(&drany, sizeof(drany));
772 defrouter_delreq(&drany, 0);
773 defrouter_select();
777 * Nuke neighbor cache entries for the ifp.
778 * Note that rt->rt_ifp may not be the same as ifp,
779 * due to KAME goto ours hack. See RTM_RESOLVE case in
780 * nd6_rtrequest(), and ip6_input().
782 ln = llinfo_nd6.ln_next;
783 while (ln && ln != &llinfo_nd6) {
784 struct rtentry *rt;
785 struct sockaddr_dl *sdl;
787 nln = ln->ln_next;
788 rt = ln->ln_rt;
789 if (rt && rt->rt_gateway &&
790 rt->rt_gateway->sa_family == AF_LINK) {
791 sdl = (struct sockaddr_dl *)rt->rt_gateway;
792 if (sdl->sdl_index == ifp->if_index)
793 nln = nd6_free(rt);
795 ln = nln;
799 struct rtentry *
800 nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp)
802 struct rtentry *rt;
803 struct sockaddr_in6 sin6;
805 bzero(&sin6, sizeof(sin6));
806 sin6.sin6_len = sizeof(struct sockaddr_in6);
807 sin6.sin6_family = AF_INET6;
808 sin6.sin6_addr = *addr6;
810 if (create)
811 rt = rtlookup((struct sockaddr *)&sin6);
812 else
813 rt = rtpurelookup((struct sockaddr *)&sin6);
814 if (rt && !(rt->rt_flags & RTF_LLINFO)) {
816 * This is the case for the default route.
817 * If we want to create a neighbor cache for the address, we
818 * should free the route for the destination and allocate an
819 * interface route.
821 if (create) {
822 --rt->rt_refcnt;
823 rt = NULL;
826 if (!rt) {
827 if (create && ifp) {
828 int e;
831 * If no route is available and create is set,
832 * we allocate a host route for the destination
833 * and treat it like an interface route.
834 * This hack is necessary for a neighbor which can't
835 * be covered by our own prefix.
837 struct ifaddr *ifa;
839 ifa = ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
840 if (ifa == NULL)
841 return (NULL);
844 * Create a new route. RTF_LLINFO is necessary
845 * to create a Neighbor Cache entry for the
846 * destination in nd6_rtrequest which will be
847 * called in rtrequest via ifa->ifa_rtrequest.
849 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
850 ifa->ifa_addr, (struct sockaddr *)&all1_sa,
851 (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
852 ~RTF_CLONING, &rt)) != 0) {
853 log(LOG_ERR,
854 "nd6_lookup: failed to add route for a "
855 "neighbor(%s), errno=%d\n",
856 ip6_sprintf(addr6), e);
858 if (rt == NULL)
859 return (NULL);
860 if (rt->rt_llinfo) {
861 struct llinfo_nd6 *ln =
862 (struct llinfo_nd6 *)rt->rt_llinfo;
864 ln->ln_state = ND6_LLINFO_NOSTATE;
866 } else
867 return (NULL);
869 rt->rt_refcnt--;
871 * Validation for the entry.
872 * Note that the check for rt_llinfo is necessary because a cloned
873 * route from a parent route that has the L flag (e.g. the default
874 * route to a p2p interface) may have the flag, too, while the
875 * destination is not actually a neighbor.
876 * XXX: we can't use rt->rt_ifp to check for the interface, since
877 * it might be the loopback interface if the entry is for our
878 * own address on a non-loopback interface. Instead, we should
879 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
880 * interface.
882 if ((rt->rt_flags & RTF_GATEWAY) || !(rt->rt_flags & RTF_LLINFO) ||
883 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
884 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
885 if (create) {
886 log(LOG_DEBUG,
887 "nd6_lookup: failed to lookup %s (if = %s)\n",
888 ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
889 /* xxx more logs... kazu */
891 return (NULL);
893 return (rt);
897 * Detect if a given IPv6 address identifies a neighbor on a given link.
898 * XXX: should take care of the destination of a p2p link?
901 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
903 struct ifaddr_container *ifac;
904 int i;
906 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
907 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
910 * A link-local address is always a neighbor.
911 * XXX: we should use the sin6_scope_id field rather than the embedded
912 * interface index.
914 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
915 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
916 return (1);
919 * If the address matches one of our addresses,
920 * it should be a neighbor.
922 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
923 struct ifaddr *ifa = ifac->ifa;
925 if (ifa->ifa_addr->sa_family != AF_INET6)
926 next: continue;
928 for (i = 0; i < 4; i++) {
929 if ((IFADDR6(ifa).s6_addr32[i] ^
930 addr->sin6_addr.s6_addr32[i]) &
931 IFMASK6(ifa).s6_addr32[i])
932 goto next;
934 return (1);
938 * Even if the address matches none of our addresses, it might be
939 * in the neighbor cache.
941 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
942 return (1);
944 return (0);
945 #undef IFADDR6
946 #undef IFMASK6
950 * Free an nd6 llinfo entry.
952 struct llinfo_nd6 *
953 nd6_free(struct rtentry *rt)
955 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
956 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
957 struct nd_defrouter *dr;
960 * we used to have kpfctlinput(PRC_HOSTDEAD) here.
961 * even though it is not harmful, it was not really necessary.
964 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
965 mtx_lock(&nd6_mtx);
966 dr = defrouter_lookup(
967 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
968 rt->rt_ifp);
970 if (ln->ln_router || dr) {
972 * rt6_flush must be called whether or not the neighbor
973 * is in the Default Router List.
974 * See a corresponding comment in nd6_na_input().
976 rt6_flush(&in6, rt->rt_ifp);
979 if (dr) {
981 * Unreachablity of a router might affect the default
982 * router selection and on-link detection of advertised
983 * prefixes.
987 * Temporarily fake the state to choose a new default
988 * router and to perform on-link determination of
989 * prefixes correctly.
990 * Below the state will be set correctly,
991 * or the entry itself will be deleted.
993 ln->ln_state = ND6_LLINFO_INCOMPLETE;
996 * Since defrouter_select() does not affect the
997 * on-link determination and MIP6 needs the check
998 * before the default router selection, we perform
999 * the check now.
1001 pfxlist_onlink_check();
1003 if (dr == TAILQ_FIRST(&nd_defrouter)) {
1005 * It is used as the current default router,
1006 * so we have to move it to the end of the
1007 * list and choose a new one.
1008 * XXX: it is not very efficient if this is
1009 * the only router.
1011 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
1012 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
1014 defrouter_select();
1017 mtx_unlock(&nd6_mtx);
1021 * Before deleting the entry, remember the next entry as the
1022 * return value. We need this because pfxlist_onlink_check() above
1023 * might have freed other entries (particularly the old next entry) as
1024 * a side effect (XXX).
1026 next = ln->ln_next;
1029 * Detach the route from the routing tree and the list of neighbor
1030 * caches, and disable the route entry not to be used in already
1031 * cached routes.
1033 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1035 return (next);
1039 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1041 * XXX cost-effective metods?
1043 void
1044 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1046 struct llinfo_nd6 *ln;
1049 * If the caller specified "rt", use that. Otherwise, resolve the
1050 * routing table by supplied "dst6".
1052 if (!rt) {
1053 if (!dst6)
1054 return;
1055 if (!(rt = nd6_lookup(dst6, 0, NULL)))
1056 return;
1059 if ((rt->rt_flags & RTF_GATEWAY) ||
1060 !(rt->rt_flags & RTF_LLINFO) ||
1061 rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
1062 rt->rt_gateway->sa_family != AF_LINK) {
1063 /* This is not a host route. */
1064 return;
1067 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1068 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1069 return;
1072 * if we get upper-layer reachability confirmation many times,
1073 * it is possible we have false information.
1075 if (!force) {
1076 ln->ln_byhint++;
1077 if (ln->ln_byhint > nd6_maxnudhint)
1078 return;
1081 ln->ln_state = ND6_LLINFO_REACHABLE;
1082 if (ln->ln_expire)
1083 ln->ln_expire = time_uptime +
1084 ND_IFINFO(rt->rt_ifp)->reachable;
1087 void
1088 nd6_rtrequest(int req, struct rtentry *rt)
1090 struct sockaddr *gate = rt->rt_gateway;
1091 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1092 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1093 struct ifnet *ifp = rt->rt_ifp;
1094 struct ifaddr *ifa;
1096 if ((rt->rt_flags & RTF_GATEWAY))
1097 return;
1099 if (nd6_need_cache(ifp) == 0 && !(rt->rt_flags & RTF_HOST)) {
1101 * This is probably an interface direct route for a link
1102 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1103 * We do not need special treatment below for such a route.
1104 * Moreover, the RTF_LLINFO flag which would be set below
1105 * would annoy the ndp(8) command.
1107 return;
1110 if (req == RTM_RESOLVE &&
1111 (nd6_need_cache(ifp) == 0 || /* stf case */
1112 !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp))) {
1114 * FreeBSD and BSD/OS often make a cloned host route based
1115 * on a less-specific route (e.g. the default route).
1116 * If the less specific route does not have a "gateway"
1117 * (this is the case when the route just goes to a p2p or an
1118 * stf interface), we'll mistakenly make a neighbor cache for
1119 * the host route, and will see strange neighbor solicitation
1120 * for the corresponding destination. In order to avoid the
1121 * confusion, we check if the destination of the route is
1122 * a neighbor in terms of neighbor discovery, and stop the
1123 * process if not. Additionally, we remove the LLINFO flag
1124 * so that ndp(8) will not try to get the neighbor information
1125 * of the destination.
1127 rt->rt_flags &= ~RTF_LLINFO;
1128 return;
1131 switch (req) {
1132 case RTM_ADD:
1134 * There is no backward compatibility :)
1136 * if (!(rt->rt_flags & RTF_HOST) &&
1137 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1138 * rt->rt_flags |= RTF_CLONING;
1140 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1142 * Case 1: This route should come from
1143 * a route to interface. RTF_LLINFO flag is set
1144 * for a host route whose destination should be
1145 * treated as on-link.
1147 rt_setgate(rt, rt_key(rt),
1148 (struct sockaddr *)&null_sdl,
1149 RTL_DONTREPORT);
1150 gate = rt->rt_gateway;
1151 SDL(gate)->sdl_type = ifp->if_type;
1152 SDL(gate)->sdl_index = ifp->if_index;
1153 if (ln)
1154 ln->ln_expire = time_uptime;
1155 if (ln && ln->ln_expire == 0) {
1156 /* kludge for desktops */
1157 ln->ln_expire = 1;
1159 if ((rt->rt_flags & RTF_CLONING))
1160 break;
1163 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1164 * We don't do that here since llinfo is not ready yet.
1166 * There are also couple of other things to be discussed:
1167 * - unsolicited NA code needs improvement beforehand
1168 * - RFC2461 says we MAY send multicast unsolicited NA
1169 * (7.2.6 paragraph 4), however, it also says that we
1170 * SHOULD provide a mechanism to prevent multicast NA storm.
1171 * we don't have anything like it right now.
1172 * note that the mechanism needs a mutual agreement
1173 * between proxies, which means that we need to implement
1174 * a new protocol, or a new kludge.
1175 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1176 * we need to check ip6forwarding before sending it.
1177 * (or should we allow proxy ND configuration only for
1178 * routers? there's no mention about proxy ND from hosts)
1180 #if 0
1181 /* XXX it does not work */
1182 if ((rt->rt_flags & RTF_ANNOUNCE) && mycpuid == 0) {
1183 nd6_na_output(ifp,
1184 &SIN6(rt_key(rt))->sin6_addr,
1185 &SIN6(rt_key(rt))->sin6_addr,
1186 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1187 1, NULL);
1189 #endif
1190 /* FALLTHROUGH */
1191 case RTM_RESOLVE:
1192 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1194 * Address resolution isn't necessary for a point to
1195 * point link, so we can skip this test for a p2p link.
1197 if (gate->sa_family != AF_LINK ||
1198 gate->sa_len < sizeof(null_sdl)) {
1199 log(LOG_DEBUG,
1200 "nd6_rtrequest: bad gateway value: %s\n",
1201 if_name(ifp));
1202 break;
1204 SDL(gate)->sdl_type = ifp->if_type;
1205 SDL(gate)->sdl_index = ifp->if_index;
1207 if (ln != NULL)
1208 break; /* This happens on a route change */
1210 * Case 2: This route may come from cloning, or a manual route
1211 * add with a LL address.
1213 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1214 rt->rt_llinfo = (caddr_t)ln;
1215 if (!ln) {
1216 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1217 break;
1219 nd6_inuse++;
1220 nd6_allocated++;
1221 bzero(ln, sizeof(*ln));
1222 ln->ln_rt = rt;
1223 /* this is required for "ndp" command. - shin */
1224 if (req == RTM_ADD) {
1226 * gate should have some valid AF_LINK entry,
1227 * and ln->ln_expire should have some lifetime
1228 * which is specified by ndp command.
1230 ln->ln_state = ND6_LLINFO_REACHABLE;
1231 ln->ln_byhint = 0;
1232 } else {
1234 * When req == RTM_RESOLVE, rt is created and
1235 * initialized in rtrequest(), so rt_expire is 0.
1237 ln->ln_state = ND6_LLINFO_NOSTATE;
1238 ln->ln_expire = time_uptime;
1240 rt->rt_flags |= RTF_LLINFO;
1241 ln->ln_next = llinfo_nd6.ln_next;
1242 llinfo_nd6.ln_next = ln;
1243 ln->ln_prev = &llinfo_nd6;
1244 ln->ln_next->ln_prev = ln;
1247 * check if rt_key(rt) is one of my address assigned
1248 * to the interface.
1250 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1251 &SIN6(rt_key(rt))->sin6_addr);
1252 if (ifa) {
1253 caddr_t macp = nd6_ifptomac(ifp);
1254 ln->ln_expire = 0;
1255 ln->ln_state = ND6_LLINFO_REACHABLE;
1256 ln->ln_byhint = 0;
1257 if (macp) {
1258 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1259 SDL(gate)->sdl_alen = ifp->if_addrlen;
1261 if (nd6_useloopback) {
1262 rt->rt_ifp = &loif[0]; /* XXX */
1264 * Make sure rt_ifa be equal to the ifaddr
1265 * corresponding to the address.
1266 * We need this because when we refer
1267 * rt_ifa->ia6_flags in ip6_input, we assume
1268 * that the rt_ifa points to the address instead
1269 * of the loopback address.
1271 if (ifa != rt->rt_ifa) {
1272 IFAFREE(rt->rt_ifa);
1273 IFAREF(ifa);
1274 rt->rt_ifa = ifa;
1277 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1278 ln->ln_expire = 0;
1279 ln->ln_state = ND6_LLINFO_REACHABLE;
1280 ln->ln_byhint = 0;
1283 * Join solicited node multicast for proxy ND, and only
1284 * join it once on cpu0.
1286 if ((ifp->if_flags & IFF_MULTICAST) && mycpuid == 0) {
1287 struct in6_addr llsol;
1288 int error;
1290 llsol = SIN6(rt_key(rt))->sin6_addr;
1291 llsol.s6_addr16[0] = htons(0xff02);
1292 llsol.s6_addr16[1] = htons(ifp->if_index);
1293 llsol.s6_addr32[1] = 0;
1294 llsol.s6_addr32[2] = htonl(1);
1295 llsol.s6_addr8[12] = 0xff;
1297 if (!in6_addmulti(&llsol, ifp, &error)) {
1298 nd6log((LOG_ERR, "%s: failed to join "
1299 "%s (errno=%d)\n", if_name(ifp),
1300 ip6_sprintf(&llsol), error));
1304 break;
1306 case RTM_DELETE:
1307 if (!ln)
1308 break;
1310 * Leave from solicited node multicast for proxy ND, and only
1311 * leave it once on cpu0 (since we joined it once on cpu0).
1313 if ((rt->rt_flags & RTF_ANNOUNCE) &&
1314 (ifp->if_flags & IFF_MULTICAST) && mycpuid == 0) {
1315 struct in6_addr llsol;
1316 struct in6_multi *in6m;
1318 llsol = SIN6(rt_key(rt))->sin6_addr;
1319 llsol.s6_addr16[0] = htons(0xff02);
1320 llsol.s6_addr16[1] = htons(ifp->if_index);
1321 llsol.s6_addr32[1] = 0;
1322 llsol.s6_addr32[2] = htonl(1);
1323 llsol.s6_addr8[12] = 0xff;
1325 in6m = IN6_LOOKUP_MULTI(&llsol, ifp);
1326 if (in6m)
1327 in6_delmulti(in6m);
1329 nd6_inuse--;
1330 ln->ln_next->ln_prev = ln->ln_prev;
1331 ln->ln_prev->ln_next = ln->ln_next;
1332 ln->ln_prev = NULL;
1333 rt->rt_llinfo = 0;
1334 rt->rt_flags &= ~RTF_LLINFO;
1335 if (ln->ln_hold)
1336 m_freem(ln->ln_hold);
1337 Free((caddr_t)ln);
1342 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1344 struct in6_drlist *drl = (struct in6_drlist *)data;
1345 struct in6_prlist *prl = (struct in6_prlist *)data;
1346 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1347 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1348 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1349 struct nd_defrouter *dr, any;
1350 struct nd_prefix *pr;
1351 struct rtentry *rt;
1352 int i = 0, error = 0;
1354 switch (cmd) {
1355 case SIOCGDRLST_IN6:
1357 * obsolete API, use sysctl under net.inet6.icmp6
1359 bzero(drl, sizeof(*drl));
1360 mtx_lock(&nd6_mtx);
1361 dr = TAILQ_FIRST(&nd_defrouter);
1362 while (dr && i < DRLSTSIZ) {
1363 drl->defrouter[i].rtaddr = dr->rtaddr;
1364 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1365 /* XXX: need to this hack for KAME stack */
1366 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1367 } else
1368 log(LOG_ERR,
1369 "default router list contains a "
1370 "non-linklocal address(%s)\n",
1371 ip6_sprintf(&drl->defrouter[i].rtaddr));
1373 drl->defrouter[i].flags = dr->flags;
1374 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1375 drl->defrouter[i].expire = dr->expire;
1376 drl->defrouter[i].if_index = dr->ifp->if_index;
1377 i++;
1378 dr = TAILQ_NEXT(dr, dr_entry);
1380 mtx_unlock(&nd6_mtx);
1381 break;
1382 case SIOCGPRLST_IN6:
1384 * obsolete API, use sysctl under net.inet6.icmp6
1387 * XXX meaning of fields, especialy "raflags", is very
1388 * differnet between RA prefix list and RR/static prefix list.
1389 * how about separating ioctls into two?
1391 bzero(prl, sizeof(*prl));
1392 mtx_lock(&nd6_mtx);
1393 pr = nd_prefix.lh_first;
1394 while (pr && i < PRLSTSIZ) {
1395 struct nd_pfxrouter *pfr;
1396 int j;
1398 in6_embedscope(&prl->prefix[i].prefix,
1399 &pr->ndpr_prefix, NULL, NULL);
1400 prl->prefix[i].raflags = pr->ndpr_raf;
1401 prl->prefix[i].prefixlen = pr->ndpr_plen;
1402 prl->prefix[i].vltime = pr->ndpr_vltime;
1403 prl->prefix[i].pltime = pr->ndpr_pltime;
1404 prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1405 prl->prefix[i].expire = pr->ndpr_expire;
1407 pfr = pr->ndpr_advrtrs.lh_first;
1408 j = 0;
1409 while (pfr) {
1410 if (j < DRLSTSIZ) {
1411 #define RTRADDR prl->prefix[i].advrtr[j]
1412 RTRADDR = pfr->router->rtaddr;
1413 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1414 /* XXX: hack for KAME */
1415 RTRADDR.s6_addr16[1] = 0;
1416 } else
1417 log(LOG_ERR,
1418 "a router(%s) advertises "
1419 "a prefix with "
1420 "non-link local address\n",
1421 ip6_sprintf(&RTRADDR));
1422 #undef RTRADDR
1424 j++;
1425 pfr = pfr->pfr_next;
1427 prl->prefix[i].advrtrs = j;
1428 prl->prefix[i].origin = PR_ORIG_RA;
1430 i++;
1431 pr = pr->ndpr_next;
1433 mtx_unlock(&nd6_mtx);
1435 break;
1436 case OSIOCGIFINFO_IN6:
1437 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1438 bzero(&ndi->ndi, sizeof(ndi->ndi));
1439 ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1440 ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1441 ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1442 ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1443 ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1444 ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1445 ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1446 ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1447 break;
1448 case SIOCGIFINFO_IN6:
1449 ndi->ndi = *ND_IFINFO(ifp);
1450 ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1451 break;
1452 case SIOCSIFINFO_FLAGS:
1453 ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1454 break;
1455 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1456 /* flush default router list */
1458 * xxx sumikawa: should not delete route if default
1459 * route equals to the top of default router list
1461 bzero(&any, sizeof(any));
1462 defrouter_delreq(&any, 0);
1463 defrouter_select();
1464 /* xxx sumikawa: flush prefix list */
1465 break;
1466 case SIOCSPFXFLUSH_IN6:
1468 /* flush all the prefix advertised by routers */
1469 struct nd_prefix *pr, *next;
1471 mtx_lock(&nd6_mtx);
1472 for (pr = nd_prefix.lh_first; pr; pr = next) {
1473 struct in6_ifaddr *ia, *ia_next;
1475 next = pr->ndpr_next;
1477 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1478 continue; /* XXX */
1480 /* do we really have to remove addresses as well? */
1481 for (ia = in6_ifaddr; ia; ia = ia_next) {
1482 /* ia might be removed. keep the next ptr. */
1483 ia_next = ia->ia_next;
1485 if (!(ia->ia6_flags & IN6_IFF_AUTOCONF))
1486 continue;
1488 if (ia->ia6_ndpr == pr)
1489 in6_purgeaddr(&ia->ia_ifa);
1491 prelist_remove(pr);
1493 mtx_unlock(&nd6_mtx);
1494 break;
1496 case SIOCSRTRFLUSH_IN6:
1498 /* flush all the default routers */
1499 struct nd_defrouter *dr, *next;
1501 mtx_lock(&nd6_mtx);
1502 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1504 * The first entry of the list may be stored in
1505 * the routing table, so we'll delete it later.
1507 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1508 next = TAILQ_NEXT(dr, dr_entry);
1509 defrtrlist_del(dr);
1511 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1513 mtx_unlock(&nd6_mtx);
1514 break;
1516 case SIOCGNBRINFO_IN6:
1518 struct llinfo_nd6 *ln;
1519 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1522 * XXX: KAME specific hack for scoped addresses
1523 * XXXX: for other scopes than link-local?
1525 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1526 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1527 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1529 if (*idp == 0)
1530 *idp = htons(ifp->if_index);
1533 mtx_lock(&nd6_mtx);
1534 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1535 error = EINVAL;
1536 mtx_unlock(&nd6_mtx);
1537 break;
1539 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1540 nbi->state = ln->ln_state;
1541 nbi->asked = ln->ln_asked;
1542 nbi->isrouter = ln->ln_router;
1543 nbi->expire = ln->ln_expire;
1544 mtx_unlock(&nd6_mtx);
1546 break;
1548 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1549 ndif->ifindex = nd6_defifindex;
1550 break;
1551 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1552 return (nd6_setdefaultiface(ndif->ifindex));
1554 return (error);
1558 * Create neighbor cache entry and cache link-layer address,
1559 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1561 struct rtentry *
1562 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1563 int lladdrlen,
1564 int type, /* ICMP6 type */
1565 int code /* type dependent information */)
1567 struct rtentry *rt = NULL;
1568 struct llinfo_nd6 *ln = NULL;
1569 int is_newentry;
1570 struct sockaddr_dl *sdl = NULL;
1571 int do_update;
1572 int olladdr;
1573 int llchange;
1574 int newstate = 0;
1576 if (!ifp)
1577 panic("ifp == NULL in nd6_cache_lladdr");
1578 if (!from)
1579 panic("from == NULL in nd6_cache_lladdr");
1581 /* nothing must be updated for unspecified address */
1582 if (IN6_IS_ADDR_UNSPECIFIED(from))
1583 return NULL;
1586 * Validation about ifp->if_addrlen and lladdrlen must be done in
1587 * the caller.
1589 * XXX If the link does not have link-layer adderss, what should
1590 * we do? (ifp->if_addrlen == 0)
1591 * Spec says nothing in sections for RA, RS and NA. There's small
1592 * description on it in NS section (RFC 2461 7.2.3).
1595 rt = nd6_lookup(from, 0, ifp);
1596 if (!rt) {
1597 #if 0
1598 /* nothing must be done if there's no lladdr */
1599 if (!lladdr || !lladdrlen)
1600 return NULL;
1601 #endif
1603 rt = nd6_lookup(from, 1, ifp);
1604 is_newentry = 1;
1605 } else {
1606 /* do nothing if static ndp is set */
1607 if (rt->rt_flags & RTF_STATIC)
1608 return NULL;
1609 is_newentry = 0;
1612 if (!rt)
1613 return NULL;
1614 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1615 fail:
1616 nd6_free(rt);
1617 return NULL;
1619 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1620 if (!ln)
1621 goto fail;
1622 if (!rt->rt_gateway)
1623 goto fail;
1624 if (rt->rt_gateway->sa_family != AF_LINK)
1625 goto fail;
1626 sdl = SDL(rt->rt_gateway);
1628 olladdr = (sdl->sdl_alen) ? 1 : 0;
1629 if (olladdr && lladdr) {
1630 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1631 llchange = 1;
1632 else
1633 llchange = 0;
1634 } else
1635 llchange = 0;
1638 * newentry olladdr lladdr llchange (*=record)
1639 * 0 n n -- (1)
1640 * 0 y n -- (2)
1641 * 0 n y -- (3) * STALE
1642 * 0 y y n (4) *
1643 * 0 y y y (5) * STALE
1644 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1645 * 1 -- y -- (7) * STALE
1648 if (lladdr) { /* (3-5) and (7) */
1650 * Record source link-layer address
1651 * XXX is it dependent to ifp->if_type?
1653 sdl->sdl_alen = ifp->if_addrlen;
1654 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1657 if (!is_newentry) {
1658 if ((!olladdr && lladdr) || /* (3) */
1659 (olladdr && lladdr && llchange)) { /* (5) */
1660 do_update = 1;
1661 newstate = ND6_LLINFO_STALE;
1662 } else { /* (1-2,4) */
1663 do_update = 0;
1665 } else {
1666 do_update = 1;
1667 if (!lladdr) /* (6) */
1668 newstate = ND6_LLINFO_NOSTATE;
1669 else /* (7) */
1670 newstate = ND6_LLINFO_STALE;
1673 if (do_update) {
1675 * Update the state of the neighbor cache.
1677 ln->ln_state = newstate;
1679 if (ln->ln_state == ND6_LLINFO_STALE) {
1681 * XXX: since nd6_output() below will cause
1682 * state tansition to DELAY and reset the timer,
1683 * we must set the timer now, although it is actually
1684 * meaningless.
1686 ln->ln_expire = time_uptime + nd6_gctimer;
1688 if (ln->ln_hold) {
1690 * we assume ifp is not a p2p here, so just
1691 * set the 2nd argument as the 1st one.
1693 nd6_output(ifp, ifp, ln->ln_hold,
1694 (struct sockaddr_in6 *)rt_key(rt), rt);
1695 ln->ln_hold = NULL;
1697 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1698 /* probe right away */
1699 ln->ln_expire = time_uptime;
1704 * ICMP6 type dependent behavior.
1706 * NS: clear IsRouter if new entry
1707 * RS: clear IsRouter
1708 * RA: set IsRouter if there's lladdr
1709 * redir: clear IsRouter if new entry
1711 * RA case, (1):
1712 * The spec says that we must set IsRouter in the following cases:
1713 * - If lladdr exist, set IsRouter. This means (1-5).
1714 * - If it is old entry (!newentry), set IsRouter. This means (7).
1715 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1716 * A quetion arises for (1) case. (1) case has no lladdr in the
1717 * neighbor cache, this is similar to (6).
1718 * This case is rare but we figured that we MUST NOT set IsRouter.
1720 * newentry olladdr lladdr llchange NS RS RA redir
1721 * D R
1722 * 0 n n -- (1) c ? s
1723 * 0 y n -- (2) c s s
1724 * 0 n y -- (3) c s s
1725 * 0 y y n (4) c s s
1726 * 0 y y y (5) c s s
1727 * 1 -- n -- (6) c c c s
1728 * 1 -- y -- (7) c c s c s
1730 * (c=clear s=set)
1732 switch (type & 0xff) {
1733 case ND_NEIGHBOR_SOLICIT:
1735 * New entry must have is_router flag cleared.
1737 if (is_newentry) /* (6-7) */
1738 ln->ln_router = 0;
1739 break;
1740 case ND_REDIRECT:
1742 * If the icmp is a redirect to a better router, always set the
1743 * is_router flag. Otherwise, if the entry is newly created,
1744 * clear the flag. [RFC 2461, sec 8.3]
1746 if (code == ND_REDIRECT_ROUTER)
1747 ln->ln_router = 1;
1748 else if (is_newentry) /* (6-7) */
1749 ln->ln_router = 0;
1750 break;
1751 case ND_ROUTER_SOLICIT:
1753 * is_router flag must always be cleared.
1755 ln->ln_router = 0;
1756 break;
1757 case ND_ROUTER_ADVERT:
1759 * Mark an entry with lladdr as a router.
1761 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1762 (is_newentry && lladdr)) { /* (7) */
1763 ln->ln_router = 1;
1765 break;
1769 * When the link-layer address of a router changes, select the
1770 * best router again. In particular, when the neighbor entry is newly
1771 * created, it might affect the selection policy.
1772 * Question: can we restrict the first condition to the "is_newentry"
1773 * case?
1774 * XXX: when we hear an RA from a new router with the link-layer
1775 * address option, defrouter_select() is called twice, since
1776 * defrtrlist_update called the function as well. However, I believe
1777 * we can compromise the overhead, since it only happens the first
1778 * time.
1779 * XXX: although defrouter_select() should not have a bad effect
1780 * for those are not autoconfigured hosts, we explicitly avoid such
1781 * cases for safety.
1783 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1784 defrouter_select();
1786 return rt;
1789 static void
1790 nd6_slowtimo(void *arg __unused)
1792 struct lwkt_msg *lmsg = &nd6_slowtimo_netmsg.lmsg;
1794 KASSERT(mycpuid == 0, ("not on cpu0"));
1795 crit_enter();
1796 if (lmsg->ms_flags & MSGF_DONE)
1797 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1798 crit_exit();
1801 static void
1802 nd6_slowtimo_dispatch(netmsg_t nmsg)
1804 const struct ifnet_array *arr;
1805 struct nd_ifinfo *nd6if;
1806 int i;
1808 ASSERT_IN_NETISR(0);
1810 crit_enter();
1811 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1812 crit_exit();
1814 arr = ifnet_array_get();
1816 mtx_lock(&nd6_mtx);
1817 for (i = 0; i < arr->ifnet_count; ++i) {
1818 struct ifnet *ifp = arr->ifnet_arr[i];
1820 if (ifp->if_afdata[AF_INET6] == NULL)
1821 continue;
1822 nd6if = ND_IFINFO(ifp);
1823 if (nd6if->basereachable && /* already initialized */
1824 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1826 * Since reachable time rarely changes by router
1827 * advertisements, we SHOULD insure that a new random
1828 * value gets recomputed at least once every few hours.
1829 * (RFC 2461, 6.3.4)
1831 nd6if->recalctm = nd6_recalc_reachtm_interval;
1832 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1835 mtx_unlock(&nd6_mtx);
1837 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1838 nd6_slowtimo, NULL);
1841 #define gotoerr(e) { error = (e); goto bad;}
1844 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1845 struct sockaddr_in6 *dst, struct rtentry *rt)
1847 struct llinfo_nd6 *ln = NULL;
1848 int error = 0;
1850 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1851 goto sendpkt;
1853 if (nd6_need_cache(ifp) == 0)
1854 goto sendpkt;
1857 * Next hop determination. This routine is derived from rt_llroute.
1859 if (rt != NULL) {
1860 if (!(rt->rt_flags & RTF_UP)) {
1861 rt = rtlookup((struct sockaddr *)dst);
1862 if (rt == NULL)
1863 gotoerr(EHOSTUNREACH);
1864 rt->rt_refcnt--;
1865 if (rt->rt_ifp != ifp) {
1866 /* XXX: loop care? */
1867 return nd6_output(ifp, origifp, m, dst, rt);
1870 if (rt->rt_flags & RTF_GATEWAY) {
1871 struct sockaddr_in6 *gw6;
1874 * We skip link-layer address resolution and NUD
1875 * if the gateway is not a neighbor from ND point
1876 * of view, regardless of the value of nd_ifinfo.flags.
1877 * The second condition is a bit tricky; we skip
1878 * if the gateway is our own address, which is
1879 * sometimes used to install a route to a p2p link.
1881 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1882 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1883 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1885 * We allow this kind of tricky route only
1886 * when the outgoing interface is p2p.
1887 * XXX: we may need a more generic rule here.
1889 if (!(ifp->if_flags & IFF_POINTOPOINT))
1890 gotoerr(EHOSTUNREACH);
1892 goto sendpkt;
1895 if (rt->rt_gwroute == NULL) {
1896 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1897 if (rt->rt_gwroute == NULL)
1898 gotoerr(EHOSTUNREACH);
1899 } else if (!(rt->rt_gwroute->rt_flags & RTF_UP)) {
1900 rtfree(rt->rt_gwroute);
1901 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1902 if (rt->rt_gwroute == NULL)
1903 gotoerr(EHOSTUNREACH);
1905 rt = rt->rt_gwroute;
1910 * Address resolution or Neighbor Unreachability Detection
1911 * for the next hop.
1912 * At this point, the destination of the packet must be a unicast
1913 * or an anycast address(i.e. not a multicast).
1916 /* Look up the neighbor cache for the nexthop */
1917 if (rt && (rt->rt_flags & RTF_LLINFO))
1918 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1919 else {
1921 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1922 * the condition below is not very efficient. But we believe
1923 * it is tolerable, because this should be a rare case.
1925 if (nd6_is_addr_neighbor(dst, ifp) &&
1926 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1927 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1929 if (!ln || !rt) {
1930 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
1931 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1932 log(LOG_DEBUG,
1933 "nd6_output: can't allocate llinfo for %s "
1934 "(ln=%p, rt=%p)\n",
1935 ip6_sprintf(&dst->sin6_addr), ln, rt);
1936 gotoerr(EIO); /* XXX: good error? */
1939 goto sendpkt; /* send anyway */
1942 /* We don't have to do link-layer address resolution on a p2p link. */
1943 if ((ifp->if_flags & IFF_POINTOPOINT) &&
1944 ln->ln_state < ND6_LLINFO_REACHABLE) {
1945 ln->ln_state = ND6_LLINFO_STALE;
1946 ln->ln_expire = time_uptime + nd6_gctimer;
1950 * The first time we send a packet to a neighbor whose entry is
1951 * STALE, we have to change the state to DELAY and a sets a timer to
1952 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1953 * neighbor unreachability detection on expiration.
1954 * (RFC 2461 7.3.3)
1956 if (ln->ln_state == ND6_LLINFO_STALE) {
1957 ln->ln_asked = 0;
1958 ln->ln_state = ND6_LLINFO_DELAY;
1959 ln->ln_expire = time_uptime + nd6_delay;
1963 * If the neighbor cache entry has a state other than INCOMPLETE
1964 * (i.e. its link-layer address is already resolved), just
1965 * send the packet.
1967 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1968 goto sendpkt;
1971 * There is a neighbor cache entry, but no ethernet address
1972 * response yet. Replace the held mbuf (if any) with this
1973 * latest one.
1975 * This code conforms to the rate-limiting rule described in Section
1976 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
1977 * an NS below.
1979 if (ln->ln_state == ND6_LLINFO_NOSTATE) {
1981 * This neighbor cache entry was just created; change its
1982 * state to INCOMPLETE and start its life cycle.
1984 * We force an NS output below by setting ln_expire to 1
1985 * (nd6_rtrequest() could set it to the current time_uptime)
1986 * and zeroing out ln_asked (XXX this may not be necessary).
1988 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1989 ln->ln_expire = 1;
1990 ln->ln_asked = 0;
1992 if (ln->ln_hold)
1993 m_freem(ln->ln_hold);
1994 ln->ln_hold = m;
1995 if (ln->ln_expire) {
1996 if (ln->ln_asked < nd6_mmaxtries &&
1997 ln->ln_expire < time_uptime) {
1998 ln->ln_asked++;
1999 ln->ln_expire = time_uptime +
2000 ND_IFINFO(ifp)->retrans / 1000;
2001 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
2004 return (0);
2006 sendpkt:
2007 if (ifp->if_flags & IFF_LOOPBACK)
2008 error = ifp->if_output(origifp, m, (struct sockaddr *)dst, rt);
2009 else
2010 error = ifp->if_output(ifp, m, (struct sockaddr *)dst, rt);
2011 return (error);
2013 bad:
2014 m_freem(m);
2015 return (error);
2017 #undef gotoerr
2020 nd6_need_cache(struct ifnet *ifp)
2023 * XXX: we currently do not make neighbor cache on any interface
2024 * other than Ethernet and GIF.
2026 * RFC2893 says:
2027 * - unidirectional tunnels needs no ND
2029 switch (ifp->if_type) {
2030 case IFT_ETHER:
2031 case IFT_IEEE1394:
2032 #ifdef IFT_L2VLAN
2033 case IFT_L2VLAN:
2034 #endif
2035 #ifdef IFT_IEEE80211
2036 case IFT_IEEE80211:
2037 #endif
2038 #ifdef IFT_CARP
2039 case IFT_CARP:
2040 #endif
2041 case IFT_GIF: /* XXX need more cases? */
2042 return (1);
2043 default:
2044 return (0);
2049 nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
2050 struct sockaddr *dst, u_char *desten)
2052 struct sockaddr_dl *sdl;
2053 struct rtentry *rt;
2056 if (m->m_flags & M_MCAST) {
2057 switch (ifp->if_type) {
2058 case IFT_ETHER:
2059 #ifdef IFT_L2VLAN
2060 case IFT_L2VLAN:
2061 #endif
2062 #ifdef IFT_IEEE80211
2063 case IFT_IEEE80211:
2064 #endif
2065 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2066 desten);
2067 return (1);
2068 case IFT_IEEE1394:
2069 bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
2070 return (1);
2071 default:
2072 m_freem(m);
2073 return (0);
2076 if (rt0 == NULL) {
2077 /* this could happen, if we could not allocate memory */
2078 m_freem(m);
2079 return (0);
2081 if (rt_llroute(dst, rt0, &rt) != 0) {
2082 m_freem(m);
2083 return (0);
2085 if (rt->rt_gateway->sa_family != AF_LINK) {
2086 kprintf("nd6_storelladdr: something odd happens\n");
2087 m_freem(m);
2088 return (0);
2090 sdl = SDL(rt->rt_gateway);
2091 if (sdl->sdl_alen == 0) {
2092 /* this should be impossible, but we bark here for debugging */
2093 kprintf("nd6_storelladdr: sdl_alen == 0\n");
2094 m_freem(m);
2095 return (0);
2098 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2099 return (1);
2102 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2103 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2104 #ifdef SYSCTL_DECL
2105 SYSCTL_DECL(_net_inet6_icmp6);
2106 #endif
2107 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2108 CTLFLAG_RD, nd6_sysctl_drlist, "List default routers");
2109 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2110 CTLFLAG_RD, nd6_sysctl_prlist, "List prefixes");
2112 static int
2113 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2115 int error;
2116 char buf[1024];
2117 struct in6_defrouter *d, *de;
2118 struct nd_defrouter *dr;
2120 if (req->newptr)
2121 return EPERM;
2122 error = 0;
2124 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2125 dr = TAILQ_NEXT(dr, dr_entry)) {
2126 d = (struct in6_defrouter *)buf;
2127 de = (struct in6_defrouter *)(buf + sizeof(buf));
2129 if (d + 1 <= de) {
2130 bzero(d, sizeof(*d));
2131 d->rtaddr.sin6_family = AF_INET6;
2132 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2133 if (in6_recoverscope(&d->rtaddr, &dr->rtaddr,
2134 dr->ifp) != 0)
2135 log(LOG_ERR,
2136 "scope error in "
2137 "default router list (%s)\n",
2138 ip6_sprintf(&dr->rtaddr));
2139 d->flags = dr->flags;
2140 d->rtlifetime = dr->rtlifetime;
2141 d->expire = dr->expire;
2142 d->if_index = dr->ifp->if_index;
2143 } else
2144 panic("buffer too short");
2146 error = SYSCTL_OUT(req, buf, sizeof(*d));
2147 if (error)
2148 break;
2150 return error;
2153 static int
2154 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2156 int error;
2157 char buf[1024];
2158 struct in6_prefix *p, *pe;
2159 struct nd_prefix *pr;
2161 if (req->newptr)
2162 return EPERM;
2163 error = 0;
2165 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2166 u_short advrtrs;
2167 size_t advance;
2168 struct sockaddr_in6 *sin6, *s6;
2169 struct nd_pfxrouter *pfr;
2171 p = (struct in6_prefix *)buf;
2172 pe = (struct in6_prefix *)(buf + sizeof(buf));
2174 if (p + 1 <= pe) {
2175 bzero(p, sizeof(*p));
2176 sin6 = (struct sockaddr_in6 *)(p + 1);
2178 p->prefix = pr->ndpr_prefix;
2179 if (in6_recoverscope(&p->prefix,
2180 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2181 log(LOG_ERR,
2182 "scope error in prefix list (%s)\n",
2183 ip6_sprintf(&p->prefix.sin6_addr));
2184 p->raflags = pr->ndpr_raf;
2185 p->prefixlen = pr->ndpr_plen;
2186 p->vltime = pr->ndpr_vltime;
2187 p->pltime = pr->ndpr_pltime;
2188 p->if_index = pr->ndpr_ifp->if_index;
2189 p->expire = pr->ndpr_expire;
2190 p->refcnt = pr->ndpr_refcnt;
2191 p->flags = pr->ndpr_stateflags;
2192 p->origin = PR_ORIG_RA;
2193 advrtrs = 0;
2194 for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2195 pfr = pfr->pfr_next) {
2196 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2197 advrtrs++;
2198 continue;
2200 s6 = &sin6[advrtrs];
2201 bzero(s6, sizeof(*s6));
2202 s6->sin6_family = AF_INET6;
2203 s6->sin6_len = sizeof(*sin6);
2204 if (in6_recoverscope(s6, &pfr->router->rtaddr,
2205 pfr->router->ifp) != 0)
2206 log(LOG_ERR,
2207 "scope error in "
2208 "prefix list (%s)\n",
2209 ip6_sprintf(&pfr->router->rtaddr));
2210 advrtrs++;
2212 p->advrtrs = advrtrs;
2213 } else {
2214 panic("buffer too short");
2217 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2218 error = SYSCTL_OUT(req, buf, advance);
2219 if (error)
2220 break;
2222 return error;