Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / sys / netinet6 / nd6.c
blob6c95e9c54218e4a3d2b9b438f95f0459032188ef
1 /* $FreeBSD: src/sys/netinet6/nd6.c,v 1.2.2.15 2003/05/06 06:46:58 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/nd6.c,v 1.30 2008/05/27 01:10:43 dillon Exp $ */
3 /* $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ */
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * XXX
36 * KAME 970409 note:
37 * BSD/OS version heavily modifies this code, related to llinfo.
38 * Since we don't have BSD/OS version of net/route.c in our hand,
39 * I left the code mostly as it was in 970310. -- itojun
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/time.h>
53 #include <sys/kernel.h>
54 #include <sys/protosw.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
57 #include <sys/queue.h>
58 #include <sys/sysctl.h>
59 #include <sys/thread2.h>
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 #include <net/if_atm.h>
65 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/nd6.h>
73 #include <netinet6/in6_prefix.h>
74 #include <netinet/icmp6.h>
76 #include "use_loop.h"
78 #include <net/net_osdep.h>
80 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
81 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
83 #define SIN6(s) ((struct sockaddr_in6 *)s)
84 #define SDL(s) ((struct sockaddr_dl *)s)
86 /* timer values */
87 int nd6_prune = 1; /* walk list every 1 seconds */
88 int nd6_delay = 5; /* delay first probe time 5 second */
89 int nd6_umaxtries = 3; /* maximum unicast query */
90 int nd6_mmaxtries = 3; /* maximum multicast query */
91 int nd6_useloopback = 1; /* use loopback interface for local traffic */
92 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
94 /* preventing too many loops in ND option parsing */
95 int nd6_maxndopt = 10; /* max # of ND options allowed */
97 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
99 #ifdef ND6_DEBUG
100 int nd6_debug = 1;
101 #else
102 int nd6_debug = 0;
103 #endif
105 /* for debugging? */
106 static int nd6_inuse, nd6_allocated;
108 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
109 struct nd_drhead nd_defrouter;
110 struct nd_prhead nd_prefix = { 0 };
112 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
113 static struct sockaddr_in6 all1_sa;
115 static void nd6_setmtu0 (struct ifnet *, struct nd_ifinfo *);
116 static void nd6_slowtimo (void *);
117 static int regen_tmpaddr (struct in6_ifaddr *);
119 struct callout nd6_slowtimo_ch;
120 struct callout nd6_timer_ch;
121 extern struct callout in6_tmpaddrtimer_ch;
123 void
124 nd6_init(void)
126 static int nd6_init_done = 0;
127 int i;
129 if (nd6_init_done) {
130 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
131 return;
134 all1_sa.sin6_family = AF_INET6;
135 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
136 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
137 all1_sa.sin6_addr.s6_addr[i] = 0xff;
139 /* initialization of the default router list */
140 TAILQ_INIT(&nd_defrouter);
142 nd6_init_done = 1;
144 /* start timer */
145 callout_init(&nd6_slowtimo_ch);
146 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
147 nd6_slowtimo, NULL);
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->linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu;
161 nd->chlim = IPV6_DEFHLIM;
162 nd->basereachable = REACHABLE_TIME;
163 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
164 nd->retrans = RETRANS_TIMER;
165 nd->receivedra = 0;
168 * Note that the default value of ip6_accept_rtadv is 0, which means
169 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
170 * here.
172 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
174 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
175 nd6_setmtu0(ifp, nd);
176 return nd;
179 void
180 nd6_ifdetach(struct nd_ifinfo *nd)
182 kfree(nd, M_IP6NDP);
186 * Reset ND level link MTU. This function is called when the physical MTU
187 * changes, which means we might have to adjust the ND level MTU.
189 void
190 nd6_setmtu(struct ifnet *ifp)
192 nd6_setmtu0(ifp, ND_IFINFO(ifp));
195 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
196 void
197 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
199 u_long oldmaxmtu;
200 u_long oldlinkmtu;
202 oldmaxmtu = ndi->maxmtu;
203 oldlinkmtu = ndi->linkmtu;
205 switch (ifp->if_type) {
206 case IFT_ETHER:
207 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
208 break;
209 case IFT_ATM:
210 ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu);
211 break;
212 case IFT_IEEE1394: /* XXX should be IEEE1394MTU(1500) */
213 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
214 break;
215 #ifdef IFT_IEEE80211
216 case IFT_IEEE80211: /* XXX should be IEEE80211MTU(1500) */
217 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
218 break;
219 #endif
220 default:
221 ndi->maxmtu = ifp->if_mtu;
222 break;
225 if (oldmaxmtu != ndi->maxmtu) {
227 * If the ND level MTU is not set yet, or if the maxmtu
228 * is reset to a smaller value than the ND level MTU,
229 * also reset the ND level MTU.
231 if (ndi->linkmtu == 0 ||
232 ndi->maxmtu < ndi->linkmtu) {
233 ndi->linkmtu = ndi->maxmtu;
234 /* also adjust in6_maxmtu if necessary. */
235 if (oldlinkmtu == 0) {
237 * XXX: the case analysis is grotty, but
238 * it is not efficient to call in6_setmaxmtu()
239 * here when we are during the initialization
240 * procedure.
242 if (in6_maxmtu < ndi->linkmtu)
243 in6_maxmtu = ndi->linkmtu;
244 } else
245 in6_setmaxmtu();
248 #undef MIN
251 void
252 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
254 bzero(ndopts, sizeof(*ndopts));
255 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
256 ndopts->nd_opts_last
257 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
259 if (icmp6len == 0) {
260 ndopts->nd_opts_done = 1;
261 ndopts->nd_opts_search = NULL;
266 * Take one ND option.
268 struct nd_opt_hdr *
269 nd6_option(union nd_opts *ndopts)
271 struct nd_opt_hdr *nd_opt;
272 int olen;
274 if (!ndopts)
275 panic("ndopts == NULL in nd6_option");
276 if (!ndopts->nd_opts_last)
277 panic("uninitialized ndopts in nd6_option");
278 if (!ndopts->nd_opts_search)
279 return NULL;
280 if (ndopts->nd_opts_done)
281 return NULL;
283 nd_opt = ndopts->nd_opts_search;
285 /* make sure nd_opt_len is inside the buffer */
286 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
287 bzero(ndopts, sizeof(*ndopts));
288 return NULL;
291 olen = nd_opt->nd_opt_len << 3;
292 if (olen == 0) {
294 * Message validation requires that all included
295 * options have a length that is greater than zero.
297 bzero(ndopts, sizeof(*ndopts));
298 return NULL;
301 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
302 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
303 /* option overruns the end of buffer, invalid */
304 bzero(ndopts, sizeof(*ndopts));
305 return NULL;
306 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
307 /* reached the end of options chain */
308 ndopts->nd_opts_done = 1;
309 ndopts->nd_opts_search = NULL;
311 return nd_opt;
315 * Parse multiple ND options.
316 * This function is much easier to use, for ND routines that do not need
317 * multiple options of the same type.
320 nd6_options(union nd_opts *ndopts)
322 struct nd_opt_hdr *nd_opt;
323 int i = 0;
325 if (!ndopts)
326 panic("ndopts == NULL in nd6_options");
327 if (!ndopts->nd_opts_last)
328 panic("uninitialized ndopts in nd6_options");
329 if (!ndopts->nd_opts_search)
330 return 0;
332 while (1) {
333 nd_opt = nd6_option(ndopts);
334 if (!nd_opt && !ndopts->nd_opts_last) {
336 * Message validation requires that all included
337 * options have a length that is greater than zero.
339 icmp6stat.icp6s_nd_badopt++;
340 bzero(ndopts, sizeof(*ndopts));
341 return -1;
344 if (!nd_opt)
345 goto skip1;
347 switch (nd_opt->nd_opt_type) {
348 case ND_OPT_SOURCE_LINKADDR:
349 case ND_OPT_TARGET_LINKADDR:
350 case ND_OPT_MTU:
351 case ND_OPT_REDIRECTED_HEADER:
352 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
353 nd6log((LOG_INFO,
354 "duplicated ND6 option found (type=%d)\n",
355 nd_opt->nd_opt_type));
356 /* XXX bark? */
357 } else {
358 ndopts->nd_opt_array[nd_opt->nd_opt_type]
359 = nd_opt;
361 break;
362 case ND_OPT_PREFIX_INFORMATION:
363 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
364 ndopts->nd_opt_array[nd_opt->nd_opt_type]
365 = nd_opt;
367 ndopts->nd_opts_pi_end =
368 (struct nd_opt_prefix_info *)nd_opt;
369 break;
370 default:
372 * Unknown options must be silently ignored,
373 * to accomodate future extension to the protocol.
375 nd6log((LOG_DEBUG,
376 "nd6_options: unsupported option %d - "
377 "option ignored\n", nd_opt->nd_opt_type));
380 skip1:
381 i++;
382 if (i > nd6_maxndopt) {
383 icmp6stat.icp6s_nd_toomanyopt++;
384 nd6log((LOG_INFO, "too many loop in nd opt\n"));
385 break;
388 if (ndopts->nd_opts_done)
389 break;
392 return 0;
396 * ND6 timer routine to expire default route list and prefix list
398 void
399 nd6_timer(void *ignored_arg)
401 struct llinfo_nd6 *ln;
402 struct nd_defrouter *dr;
403 struct nd_prefix *pr;
404 struct ifnet *ifp;
405 struct in6_ifaddr *ia6, *nia6;
406 struct in6_addrlifetime *lt6;
408 crit_enter();
409 callout_reset(&nd6_timer_ch, nd6_prune * hz,
410 nd6_timer, NULL);
412 ln = llinfo_nd6.ln_next;
413 while (ln && ln != &llinfo_nd6) {
414 struct rtentry *rt;
415 struct sockaddr_in6 *dst;
416 struct llinfo_nd6 *next = ln->ln_next;
417 /* XXX: used for the DELAY case only: */
418 struct nd_ifinfo *ndi = NULL;
420 if ((rt = ln->ln_rt) == NULL) {
421 ln = next;
422 continue;
424 if ((ifp = rt->rt_ifp) == NULL) {
425 ln = next;
426 continue;
428 ndi = ND_IFINFO(ifp);
429 dst = (struct sockaddr_in6 *)rt_key(rt);
431 if (ln->ln_expire > time_second) {
432 ln = next;
433 continue;
436 /* sanity check */
437 if (!rt)
438 panic("rt=0 in nd6_timer(ln=%p)", ln);
439 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
440 panic("rt_llinfo(%p) is not equal to ln(%p)",
441 rt->rt_llinfo, ln);
442 if (!dst)
443 panic("dst=0 in nd6_timer(ln=%p)", ln);
445 switch (ln->ln_state) {
446 case ND6_LLINFO_INCOMPLETE:
447 if (ln->ln_asked < nd6_mmaxtries) {
448 ln->ln_asked++;
449 ln->ln_expire = time_second +
450 ND_IFINFO(ifp)->retrans / 1000;
451 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
452 ln, 0);
453 } else {
454 struct mbuf *m = ln->ln_hold;
455 if (m) {
456 if (rt->rt_ifp) {
458 * Fake rcvif to make ICMP error
459 * more helpful in diagnosing
460 * for the receiver.
461 * XXX: should we consider
462 * older rcvif?
464 m->m_pkthdr.rcvif = rt->rt_ifp;
466 icmp6_error(m, ICMP6_DST_UNREACH,
467 ICMP6_DST_UNREACH_ADDR, 0);
468 ln->ln_hold = NULL;
470 next = nd6_free(rt);
472 break;
473 case ND6_LLINFO_REACHABLE:
474 if (ln->ln_expire) {
475 ln->ln_state = ND6_LLINFO_STALE;
476 ln->ln_expire = time_second + nd6_gctimer;
478 break;
480 case ND6_LLINFO_STALE:
481 /* Garbage Collection(RFC 2461 5.3) */
482 if (ln->ln_expire)
483 next = nd6_free(rt);
484 break;
486 case ND6_LLINFO_DELAY:
487 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD)) {
488 /* We need NUD */
489 ln->ln_asked = 1;
490 ln->ln_state = ND6_LLINFO_PROBE;
491 ln->ln_expire = time_second +
492 ndi->retrans / 1000;
493 nd6_ns_output(ifp, &dst->sin6_addr,
494 &dst->sin6_addr,
495 ln, 0);
496 } else {
497 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
498 ln->ln_expire = time_second + nd6_gctimer;
500 break;
501 case ND6_LLINFO_PROBE:
502 if (ln->ln_asked < nd6_umaxtries) {
503 ln->ln_asked++;
504 ln->ln_expire = time_second +
505 ND_IFINFO(ifp)->retrans / 1000;
506 nd6_ns_output(ifp, &dst->sin6_addr,
507 &dst->sin6_addr, ln, 0);
508 } else {
509 next = nd6_free(rt);
511 break;
513 ln = next;
516 /* expire default router list */
517 dr = TAILQ_FIRST(&nd_defrouter);
518 while (dr) {
519 if (dr->expire && dr->expire < time_second) {
520 struct nd_defrouter *t;
521 t = TAILQ_NEXT(dr, dr_entry);
522 defrtrlist_del(dr);
523 dr = t;
524 } else {
525 dr = TAILQ_NEXT(dr, dr_entry);
530 * expire interface addresses.
531 * in the past the loop was inside prefix expiry processing.
532 * However, from a stricter speci-confrmance standpoint, we should
533 * rather separate address lifetimes and prefix lifetimes.
535 addrloop:
536 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
537 nia6 = ia6->ia_next;
538 /* check address lifetime */
539 lt6 = &ia6->ia6_lifetime;
540 if (IFA6_IS_INVALID(ia6)) {
541 int regen = 0;
544 * If the expiring address is temporary, try
545 * regenerating a new one. This would be useful when
546 * we suspended a laptop PC, then turned it on after a
547 * period that could invalidate all temporary
548 * addresses. Although we may have to restart the
549 * loop (see below), it must be after purging the
550 * address. Otherwise, we'd see an infinite loop of
551 * regeneration.
553 if (ip6_use_tempaddr &&
554 (ia6->ia6_flags & IN6_IFF_TEMPORARY)) {
555 if (regen_tmpaddr(ia6) == 0)
556 regen = 1;
559 in6_purgeaddr(&ia6->ia_ifa);
561 if (regen)
562 goto addrloop; /* XXX: see below */
564 if (IFA6_IS_DEPRECATED(ia6)) {
565 int oldflags = ia6->ia6_flags;
567 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
570 * If a temporary address has just become deprecated,
571 * regenerate a new one if possible.
573 if (ip6_use_tempaddr &&
574 (ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
575 !(oldflags & IN6_IFF_DEPRECATED)) {
577 if (regen_tmpaddr(ia6) == 0) {
579 * A new temporary address is
580 * generated.
581 * XXX: this means the address chain
582 * has changed while we are still in
583 * the loop. Although the change
584 * would not cause disaster (because
585 * it's not a deletion, but an
586 * addition,) we'd rather restart the
587 * loop just for safety. Or does this
588 * significantly reduce performance??
590 goto addrloop;
593 } else {
595 * A new RA might have made a deprecated address
596 * preferred.
598 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
602 /* expire prefix list */
603 pr = nd_prefix.lh_first;
604 while (pr) {
606 * check prefix lifetime.
607 * since pltime is just for autoconf, pltime processing for
608 * prefix is not necessary.
610 if (pr->ndpr_expire && pr->ndpr_expire < time_second) {
611 struct nd_prefix *t;
612 t = pr->ndpr_next;
615 * address expiration and prefix expiration are
616 * separate. NEVER perform in6_purgeaddr here.
619 prelist_remove(pr);
620 pr = t;
621 } else
622 pr = pr->ndpr_next;
624 crit_exit();
627 static int
628 regen_tmpaddr(struct in6_ifaddr *ia6) /* deprecated/invalidated temporary
629 address */
631 struct ifaddr_container *ifac;
632 struct ifnet *ifp;
633 struct in6_ifaddr *public_ifa6 = NULL;
635 ifp = ia6->ia_ifa.ifa_ifp;
636 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
637 struct ifaddr *ifa = ifac->ifa;
638 struct in6_ifaddr *it6;
640 if (ifa->ifa_addr->sa_family != AF_INET6)
641 continue;
643 it6 = (struct in6_ifaddr *)ifa;
645 /* ignore no autoconf addresses. */
646 if (!(it6->ia6_flags & IN6_IFF_AUTOCONF))
647 continue;
649 /* ignore autoconf addresses with different prefixes. */
650 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
651 continue;
654 * Now we are looking at an autoconf address with the same
655 * prefix as ours. If the address is temporary and is still
656 * preferred, do not create another one. It would be rare, but
657 * could happen, for example, when we resume a laptop PC after
658 * a long period.
660 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) &&
661 !IFA6_IS_DEPRECATED(it6)) {
662 public_ifa6 = NULL;
663 break;
667 * This is a public autoconf address that has the same prefix
668 * as ours. If it is preferred, keep it. We can't break the
669 * loop here, because there may be a still-preferred temporary
670 * address with the prefix.
672 if (!IFA6_IS_DEPRECATED(it6))
673 public_ifa6 = it6;
676 if (public_ifa6 != NULL) {
677 int e;
679 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
680 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
681 " tmp addr,errno=%d\n", e);
682 return (-1);
684 return (0);
687 return (-1);
691 * Nuke neighbor cache/prefix/default router management table, right before
692 * ifp goes away.
694 void
695 nd6_purge(struct ifnet *ifp)
697 struct llinfo_nd6 *ln, *nln;
698 struct nd_defrouter *dr, *ndr, drany;
699 struct nd_prefix *pr, *npr;
701 /* Nuke default router list entries toward ifp */
702 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
704 * The first entry of the list may be stored in
705 * the routing table, so we'll delete it later.
707 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
708 ndr = TAILQ_NEXT(dr, dr_entry);
709 if (dr->ifp == ifp)
710 defrtrlist_del(dr);
712 dr = TAILQ_FIRST(&nd_defrouter);
713 if (dr->ifp == ifp)
714 defrtrlist_del(dr);
717 /* Nuke prefix list entries toward ifp */
718 for (pr = nd_prefix.lh_first; pr; pr = npr) {
719 npr = pr->ndpr_next;
720 if (pr->ndpr_ifp == ifp) {
722 * Previously, pr->ndpr_addr is removed as well,
723 * but I strongly believe we don't have to do it.
724 * nd6_purge() is only called from in6_ifdetach(),
725 * which removes all the associated interface addresses
726 * by itself.
727 * (jinmei@kame.net 20010129)
729 prelist_remove(pr);
733 /* cancel default outgoing interface setting */
734 if (nd6_defifindex == ifp->if_index)
735 nd6_setdefaultiface(0);
737 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
738 /* refresh default router list */
739 bzero(&drany, sizeof(drany));
740 defrouter_delreq(&drany, 0);
741 defrouter_select();
745 * Nuke neighbor cache entries for the ifp.
746 * Note that rt->rt_ifp may not be the same as ifp,
747 * due to KAME goto ours hack. See RTM_RESOLVE case in
748 * nd6_rtrequest(), and ip6_input().
750 ln = llinfo_nd6.ln_next;
751 while (ln && ln != &llinfo_nd6) {
752 struct rtentry *rt;
753 struct sockaddr_dl *sdl;
755 nln = ln->ln_next;
756 rt = ln->ln_rt;
757 if (rt && rt->rt_gateway &&
758 rt->rt_gateway->sa_family == AF_LINK) {
759 sdl = (struct sockaddr_dl *)rt->rt_gateway;
760 if (sdl->sdl_index == ifp->if_index)
761 nln = nd6_free(rt);
763 ln = nln;
767 struct rtentry *
768 nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp)
770 struct rtentry *rt;
771 struct sockaddr_in6 sin6;
773 bzero(&sin6, sizeof(sin6));
774 sin6.sin6_len = sizeof(struct sockaddr_in6);
775 sin6.sin6_family = AF_INET6;
776 sin6.sin6_addr = *addr6;
778 if (create)
779 rt = rtlookup((struct sockaddr *)&sin6);
780 else
781 rt = rtpurelookup((struct sockaddr *)&sin6);
782 if (rt && !(rt->rt_flags & RTF_LLINFO)) {
784 * This is the case for the default route.
785 * If we want to create a neighbor cache for the address, we
786 * should free the route for the destination and allocate an
787 * interface route.
789 if (create) {
790 --rt->rt_refcnt;
791 rt = NULL;
794 if (!rt) {
795 if (create && ifp) {
796 int e;
799 * If no route is available and create is set,
800 * we allocate a host route for the destination
801 * and treat it like an interface route.
802 * This hack is necessary for a neighbor which can't
803 * be covered by our own prefix.
805 struct ifaddr *ifa =
806 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
807 if (ifa == NULL)
808 return (NULL);
811 * Create a new route. RTF_LLINFO is necessary
812 * to create a Neighbor Cache entry for the
813 * destination in nd6_rtrequest which will be
814 * called in rtrequest via ifa->ifa_rtrequest.
816 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
817 ifa->ifa_addr,
818 (struct sockaddr *)&all1_sa,
819 (ifa->ifa_flags |
820 RTF_HOST | RTF_LLINFO) &
821 ~RTF_CLONING,
822 &rt)) != 0)
823 log(LOG_ERR,
824 "nd6_lookup: failed to add route for a "
825 "neighbor(%s), errno=%d\n",
826 ip6_sprintf(addr6), e);
827 if (rt == NULL)
828 return (NULL);
829 if (rt->rt_llinfo) {
830 struct llinfo_nd6 *ln =
831 (struct llinfo_nd6 *)rt->rt_llinfo;
832 ln->ln_state = ND6_LLINFO_NOSTATE;
834 } else
835 return (NULL);
837 rt->rt_refcnt--;
839 * Validation for the entry.
840 * Note that the check for rt_llinfo is necessary because a cloned
841 * route from a parent route that has the L flag (e.g. the default
842 * route to a p2p interface) may have the flag, too, while the
843 * destination is not actually a neighbor.
844 * XXX: we can't use rt->rt_ifp to check for the interface, since
845 * it might be the loopback interface if the entry is for our
846 * own address on a non-loopback interface. Instead, we should
847 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
848 * interface.
850 if ((rt->rt_flags & RTF_GATEWAY) || !(rt->rt_flags & RTF_LLINFO) ||
851 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
852 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
853 if (create) {
854 log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n",
855 ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
856 /* xxx more logs... kazu */
858 return (NULL);
860 return (rt);
864 * Detect if a given IPv6 address identifies a neighbor on a given link.
865 * XXX: should take care of the destination of a p2p link?
868 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
870 struct ifaddr_container *ifac;
871 int i;
873 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
874 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
877 * A link-local address is always a neighbor.
878 * XXX: we should use the sin6_scope_id field rather than the embedded
879 * interface index.
881 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
882 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
883 return (1);
886 * If the address matches one of our addresses,
887 * it should be a neighbor.
889 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
890 struct ifaddr *ifa = ifac->ifa;
892 if (ifa->ifa_addr->sa_family != AF_INET6)
893 next: continue;
895 for (i = 0; i < 4; i++) {
896 if ((IFADDR6(ifa).s6_addr32[i] ^
897 addr->sin6_addr.s6_addr32[i]) &
898 IFMASK6(ifa).s6_addr32[i])
899 goto next;
901 return (1);
905 * Even if the address matches none of our addresses, it might be
906 * in the neighbor cache.
908 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
909 return (1);
911 return (0);
912 #undef IFADDR6
913 #undef IFMASK6
917 * Free an nd6 llinfo entry.
919 struct llinfo_nd6 *
920 nd6_free(struct rtentry *rt)
922 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
923 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
924 struct nd_defrouter *dr;
927 * we used to have kpfctlinput(PRC_HOSTDEAD) here.
928 * even though it is not harmful, it was not really necessary.
931 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
932 crit_enter();
933 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
934 rt->rt_ifp);
936 if (ln->ln_router || dr) {
938 * rt6_flush must be called whether or not the neighbor
939 * is in the Default Router List.
940 * See a corresponding comment in nd6_na_input().
942 rt6_flush(&in6, rt->rt_ifp);
945 if (dr) {
947 * Unreachablity of a router might affect the default
948 * router selection and on-link detection of advertised
949 * prefixes.
953 * Temporarily fake the state to choose a new default
954 * router and to perform on-link determination of
955 * prefixes correctly.
956 * Below the state will be set correctly,
957 * or the entry itself will be deleted.
959 ln->ln_state = ND6_LLINFO_INCOMPLETE;
962 * Since defrouter_select() does not affect the
963 * on-link determination and MIP6 needs the check
964 * before the default router selection, we perform
965 * the check now.
967 pfxlist_onlink_check();
969 if (dr == TAILQ_FIRST(&nd_defrouter)) {
971 * It is used as the current default router,
972 * so we have to move it to the end of the
973 * list and choose a new one.
974 * XXX: it is not very efficient if this is
975 * the only router.
977 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
978 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
980 defrouter_select();
983 crit_exit();
987 * Before deleting the entry, remember the next entry as the
988 * return value. We need this because pfxlist_onlink_check() above
989 * might have freed other entries (particularly the old next entry) as
990 * a side effect (XXX).
992 next = ln->ln_next;
995 * Detach the route from the routing tree and the list of neighbor
996 * caches, and disable the route entry not to be used in already
997 * cached routes.
999 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1001 return (next);
1005 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1007 * XXX cost-effective metods?
1009 void
1010 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1012 struct llinfo_nd6 *ln;
1015 * If the caller specified "rt", use that. Otherwise, resolve the
1016 * routing table by supplied "dst6".
1018 if (!rt) {
1019 if (!dst6)
1020 return;
1021 if (!(rt = nd6_lookup(dst6, 0, NULL)))
1022 return;
1025 if ((rt->rt_flags & RTF_GATEWAY) ||
1026 !(rt->rt_flags & RTF_LLINFO) ||
1027 rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
1028 rt->rt_gateway->sa_family != AF_LINK) {
1029 /* This is not a host route. */
1030 return;
1033 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1034 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1035 return;
1038 * if we get upper-layer reachability confirmation many times,
1039 * it is possible we have false information.
1041 if (!force) {
1042 ln->ln_byhint++;
1043 if (ln->ln_byhint > nd6_maxnudhint)
1044 return;
1047 ln->ln_state = ND6_LLINFO_REACHABLE;
1048 if (ln->ln_expire)
1049 ln->ln_expire = time_second +
1050 ND_IFINFO(rt->rt_ifp)->reachable;
1053 void
1054 nd6_rtrequest(int req, struct rtentry *rt,
1055 struct rt_addrinfo *info) /* xxx unused */
1057 struct sockaddr *gate = rt->rt_gateway;
1058 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1059 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1060 struct ifnet *ifp = rt->rt_ifp;
1061 struct ifaddr *ifa;
1063 if ((rt->rt_flags & RTF_GATEWAY))
1064 return;
1066 if (nd6_need_cache(ifp) == 0 && !(rt->rt_flags & RTF_HOST)) {
1068 * This is probably an interface direct route for a link
1069 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1070 * We do not need special treatment below for such a route.
1071 * Moreover, the RTF_LLINFO flag which would be set below
1072 * would annoy the ndp(8) command.
1074 return;
1077 if (req == RTM_RESOLVE &&
1078 (nd6_need_cache(ifp) == 0 || /* stf case */
1079 !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp))) {
1081 * FreeBSD and BSD/OS often make a cloned host route based
1082 * on a less-specific route (e.g. the default route).
1083 * If the less specific route does not have a "gateway"
1084 * (this is the case when the route just goes to a p2p or an
1085 * stf interface), we'll mistakenly make a neighbor cache for
1086 * the host route, and will see strange neighbor solicitation
1087 * for the corresponding destination. In order to avoid the
1088 * confusion, we check if the destination of the route is
1089 * a neighbor in terms of neighbor discovery, and stop the
1090 * process if not. Additionally, we remove the LLINFO flag
1091 * so that ndp(8) will not try to get the neighbor information
1092 * of the destination.
1094 rt->rt_flags &= ~RTF_LLINFO;
1095 return;
1098 switch (req) {
1099 case RTM_ADD:
1101 * There is no backward compatibility :)
1103 * if (!(rt->rt_flags & RTF_HOST) &&
1104 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1105 * rt->rt_flags |= RTF_CLONING;
1107 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1109 * Case 1: This route should come from
1110 * a route to interface. RTF_LLINFO flag is set
1111 * for a host route whose destination should be
1112 * treated as on-link.
1114 rt_setgate(rt, rt_key(rt),
1115 (struct sockaddr *)&null_sdl,
1116 RTL_DONTREPORT);
1117 gate = rt->rt_gateway;
1118 SDL(gate)->sdl_type = ifp->if_type;
1119 SDL(gate)->sdl_index = ifp->if_index;
1120 if (ln)
1121 ln->ln_expire = time_second;
1122 #if 1
1123 if (ln && ln->ln_expire == 0) {
1124 /* kludge for desktops */
1125 #if 0
1126 kprintf("nd6_rtequest: time.tv_sec is zero; "
1127 "treat it as 1\n");
1128 #endif
1129 ln->ln_expire = 1;
1131 #endif
1132 if ((rt->rt_flags & RTF_CLONING))
1133 break;
1136 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1137 * We don't do that here since llinfo is not ready yet.
1139 * There are also couple of other things to be discussed:
1140 * - unsolicited NA code needs improvement beforehand
1141 * - RFC2461 says we MAY send multicast unsolicited NA
1142 * (7.2.6 paragraph 4), however, it also says that we
1143 * SHOULD provide a mechanism to prevent multicast NA storm.
1144 * we don't have anything like it right now.
1145 * note that the mechanism needs a mutual agreement
1146 * between proxies, which means that we need to implement
1147 * a new protocol, or a new kludge.
1148 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1149 * we need to check ip6forwarding before sending it.
1150 * (or should we allow proxy ND configuration only for
1151 * routers? there's no mention about proxy ND from hosts)
1153 #if 0
1154 /* XXX it does not work */
1155 if (rt->rt_flags & RTF_ANNOUNCE)
1156 nd6_na_output(ifp,
1157 &SIN6(rt_key(rt))->sin6_addr,
1158 &SIN6(rt_key(rt))->sin6_addr,
1159 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1160 1, NULL);
1161 #endif
1162 /* FALLTHROUGH */
1163 case RTM_RESOLVE:
1164 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1166 * Address resolution isn't necessary for a point to
1167 * point link, so we can skip this test for a p2p link.
1169 if (gate->sa_family != AF_LINK ||
1170 gate->sa_len < sizeof(null_sdl)) {
1171 log(LOG_DEBUG,
1172 "nd6_rtrequest: bad gateway value: %s\n",
1173 if_name(ifp));
1174 break;
1176 SDL(gate)->sdl_type = ifp->if_type;
1177 SDL(gate)->sdl_index = ifp->if_index;
1179 if (ln != NULL)
1180 break; /* This happens on a route change */
1182 * Case 2: This route may come from cloning, or a manual route
1183 * add with a LL address.
1185 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1186 rt->rt_llinfo = (caddr_t)ln;
1187 if (!ln) {
1188 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1189 break;
1191 nd6_inuse++;
1192 nd6_allocated++;
1193 bzero(ln, sizeof(*ln));
1194 ln->ln_rt = rt;
1195 /* this is required for "ndp" command. - shin */
1196 if (req == RTM_ADD) {
1198 * gate should have some valid AF_LINK entry,
1199 * and ln->ln_expire should have some lifetime
1200 * which is specified by ndp command.
1202 ln->ln_state = ND6_LLINFO_REACHABLE;
1203 ln->ln_byhint = 0;
1204 } else {
1206 * When req == RTM_RESOLVE, rt is created and
1207 * initialized in rtrequest(), so rt_expire is 0.
1209 ln->ln_state = ND6_LLINFO_NOSTATE;
1210 ln->ln_expire = time_second;
1212 rt->rt_flags |= RTF_LLINFO;
1213 ln->ln_next = llinfo_nd6.ln_next;
1214 llinfo_nd6.ln_next = ln;
1215 ln->ln_prev = &llinfo_nd6;
1216 ln->ln_next->ln_prev = ln;
1219 * check if rt_key(rt) is one of my address assigned
1220 * to the interface.
1222 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1223 &SIN6(rt_key(rt))->sin6_addr);
1224 if (ifa) {
1225 caddr_t macp = nd6_ifptomac(ifp);
1226 ln->ln_expire = 0;
1227 ln->ln_state = ND6_LLINFO_REACHABLE;
1228 ln->ln_byhint = 0;
1229 if (macp) {
1230 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1231 SDL(gate)->sdl_alen = ifp->if_addrlen;
1233 if (nd6_useloopback) {
1234 rt->rt_ifp = &loif[0]; /* XXX */
1236 * Make sure rt_ifa be equal to the ifaddr
1237 * corresponding to the address.
1238 * We need this because when we refer
1239 * rt_ifa->ia6_flags in ip6_input, we assume
1240 * that the rt_ifa points to the address instead
1241 * of the loopback address.
1243 if (ifa != rt->rt_ifa) {
1244 IFAFREE(rt->rt_ifa);
1245 IFAREF(ifa);
1246 rt->rt_ifa = ifa;
1249 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1250 ln->ln_expire = 0;
1251 ln->ln_state = ND6_LLINFO_REACHABLE;
1252 ln->ln_byhint = 0;
1254 /* join solicited node multicast for proxy ND */
1255 if (ifp->if_flags & IFF_MULTICAST) {
1256 struct in6_addr llsol;
1257 int error;
1259 llsol = SIN6(rt_key(rt))->sin6_addr;
1260 llsol.s6_addr16[0] = htons(0xff02);
1261 llsol.s6_addr16[1] = htons(ifp->if_index);
1262 llsol.s6_addr32[1] = 0;
1263 llsol.s6_addr32[2] = htonl(1);
1264 llsol.s6_addr8[12] = 0xff;
1266 if (!in6_addmulti(&llsol, ifp, &error)) {
1267 nd6log((LOG_ERR, "%s: failed to join "
1268 "%s (errno=%d)\n", if_name(ifp),
1269 ip6_sprintf(&llsol), error));
1273 break;
1275 case RTM_DELETE:
1276 if (!ln)
1277 break;
1278 /* leave from solicited node multicast for proxy ND */
1279 if ((rt->rt_flags & RTF_ANNOUNCE) &&
1280 (ifp->if_flags & IFF_MULTICAST)) {
1281 struct in6_addr llsol;
1282 struct in6_multi *in6m;
1284 llsol = SIN6(rt_key(rt))->sin6_addr;
1285 llsol.s6_addr16[0] = htons(0xff02);
1286 llsol.s6_addr16[1] = htons(ifp->if_index);
1287 llsol.s6_addr32[1] = 0;
1288 llsol.s6_addr32[2] = htonl(1);
1289 llsol.s6_addr8[12] = 0xff;
1291 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1292 if (in6m)
1293 in6_delmulti(in6m);
1295 nd6_inuse--;
1296 ln->ln_next->ln_prev = ln->ln_prev;
1297 ln->ln_prev->ln_next = ln->ln_next;
1298 ln->ln_prev = NULL;
1299 rt->rt_llinfo = 0;
1300 rt->rt_flags &= ~RTF_LLINFO;
1301 if (ln->ln_hold)
1302 m_freem(ln->ln_hold);
1303 Free((caddr_t)ln);
1308 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1310 struct in6_drlist *drl = (struct in6_drlist *)data;
1311 struct in6_prlist *prl = (struct in6_prlist *)data;
1312 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1313 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1314 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1315 struct nd_defrouter *dr, any;
1316 struct nd_prefix *pr;
1317 struct rtentry *rt;
1318 int i = 0, error = 0;
1320 switch (cmd) {
1321 case SIOCGDRLST_IN6:
1323 * obsolete API, use sysctl under net.inet6.icmp6
1325 bzero(drl, sizeof(*drl));
1326 crit_enter();
1327 dr = TAILQ_FIRST(&nd_defrouter);
1328 while (dr && i < DRLSTSIZ) {
1329 drl->defrouter[i].rtaddr = dr->rtaddr;
1330 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1331 /* XXX: need to this hack for KAME stack */
1332 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1333 } else
1334 log(LOG_ERR,
1335 "default router list contains a "
1336 "non-linklocal address(%s)\n",
1337 ip6_sprintf(&drl->defrouter[i].rtaddr));
1339 drl->defrouter[i].flags = dr->flags;
1340 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1341 drl->defrouter[i].expire = dr->expire;
1342 drl->defrouter[i].if_index = dr->ifp->if_index;
1343 i++;
1344 dr = TAILQ_NEXT(dr, dr_entry);
1346 crit_exit();
1347 break;
1348 case SIOCGPRLST_IN6:
1350 * obsolete API, use sysctl under net.inet6.icmp6
1353 * XXX meaning of fields, especialy "raflags", is very
1354 * differnet between RA prefix list and RR/static prefix list.
1355 * how about separating ioctls into two?
1357 bzero(prl, sizeof(*prl));
1358 crit_enter();
1359 pr = nd_prefix.lh_first;
1360 while (pr && i < PRLSTSIZ) {
1361 struct nd_pfxrouter *pfr;
1362 int j;
1364 in6_embedscope(&prl->prefix[i].prefix,
1365 &pr->ndpr_prefix, NULL, NULL);
1366 prl->prefix[i].raflags = pr->ndpr_raf;
1367 prl->prefix[i].prefixlen = pr->ndpr_plen;
1368 prl->prefix[i].vltime = pr->ndpr_vltime;
1369 prl->prefix[i].pltime = pr->ndpr_pltime;
1370 prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1371 prl->prefix[i].expire = pr->ndpr_expire;
1373 pfr = pr->ndpr_advrtrs.lh_first;
1374 j = 0;
1375 while (pfr) {
1376 if (j < DRLSTSIZ) {
1377 #define RTRADDR prl->prefix[i].advrtr[j]
1378 RTRADDR = pfr->router->rtaddr;
1379 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1380 /* XXX: hack for KAME */
1381 RTRADDR.s6_addr16[1] = 0;
1382 } else
1383 log(LOG_ERR,
1384 "a router(%s) advertises "
1385 "a prefix with "
1386 "non-link local address\n",
1387 ip6_sprintf(&RTRADDR));
1388 #undef RTRADDR
1390 j++;
1391 pfr = pfr->pfr_next;
1393 prl->prefix[i].advrtrs = j;
1394 prl->prefix[i].origin = PR_ORIG_RA;
1396 i++;
1397 pr = pr->ndpr_next;
1400 struct rr_prefix *rpp;
1402 for (rpp = LIST_FIRST(&rr_prefix); rpp;
1403 rpp = LIST_NEXT(rpp, rp_entry)) {
1404 if (i >= PRLSTSIZ)
1405 break;
1406 in6_embedscope(&prl->prefix[i].prefix,
1407 &pr->ndpr_prefix, NULL, NULL);
1408 prl->prefix[i].raflags = rpp->rp_raf;
1409 prl->prefix[i].prefixlen = rpp->rp_plen;
1410 prl->prefix[i].vltime = rpp->rp_vltime;
1411 prl->prefix[i].pltime = rpp->rp_pltime;
1412 prl->prefix[i].if_index = rpp->rp_ifp->if_index;
1413 prl->prefix[i].expire = rpp->rp_expire;
1414 prl->prefix[i].advrtrs = 0;
1415 prl->prefix[i].origin = rpp->rp_origin;
1416 i++;
1419 crit_exit();
1421 break;
1422 case OSIOCGIFINFO_IN6:
1423 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1424 bzero(&ndi->ndi, sizeof(ndi->ndi));
1425 ndi->ndi.linkmtu = ND_IFINFO(ifp)->linkmtu;
1426 ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1427 ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1428 ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1429 ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1430 ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1431 ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1432 ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1433 ndi->ndi.receivedra = ND_IFINFO(ifp)->receivedra;
1434 break;
1435 case SIOCGIFINFO_IN6:
1436 ndi->ndi = *ND_IFINFO(ifp);
1437 break;
1438 case SIOCSIFINFO_FLAGS:
1439 ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1440 break;
1441 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1442 /* flush default router list */
1444 * xxx sumikawa: should not delete route if default
1445 * route equals to the top of default router list
1447 bzero(&any, sizeof(any));
1448 defrouter_delreq(&any, 0);
1449 defrouter_select();
1450 /* xxx sumikawa: flush prefix list */
1451 break;
1452 case SIOCSPFXFLUSH_IN6:
1454 /* flush all the prefix advertised by routers */
1455 struct nd_prefix *pr, *next;
1457 crit_enter();
1458 for (pr = nd_prefix.lh_first; pr; pr = next) {
1459 struct in6_ifaddr *ia, *ia_next;
1461 next = pr->ndpr_next;
1463 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1464 continue; /* XXX */
1466 /* do we really have to remove addresses as well? */
1467 for (ia = in6_ifaddr; ia; ia = ia_next) {
1468 /* ia might be removed. keep the next ptr. */
1469 ia_next = ia->ia_next;
1471 if (!(ia->ia6_flags & IN6_IFF_AUTOCONF))
1472 continue;
1474 if (ia->ia6_ndpr == pr)
1475 in6_purgeaddr(&ia->ia_ifa);
1477 prelist_remove(pr);
1479 crit_exit();
1480 break;
1482 case SIOCSRTRFLUSH_IN6:
1484 /* flush all the default routers */
1485 struct nd_defrouter *dr, *next;
1487 crit_enter();
1488 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1490 * The first entry of the list may be stored in
1491 * the routing table, so we'll delete it later.
1493 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1494 next = TAILQ_NEXT(dr, dr_entry);
1495 defrtrlist_del(dr);
1497 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1499 crit_exit();
1500 break;
1502 case SIOCGNBRINFO_IN6:
1504 struct llinfo_nd6 *ln;
1505 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1508 * XXX: KAME specific hack for scoped addresses
1509 * XXXX: for other scopes than link-local?
1511 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1512 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1513 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1515 if (*idp == 0)
1516 *idp = htons(ifp->if_index);
1519 crit_enter();
1520 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1521 error = EINVAL;
1522 crit_exit();
1523 break;
1525 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1526 nbi->state = ln->ln_state;
1527 nbi->asked = ln->ln_asked;
1528 nbi->isrouter = ln->ln_router;
1529 nbi->expire = ln->ln_expire;
1530 crit_exit();
1532 break;
1534 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1535 ndif->ifindex = nd6_defifindex;
1536 break;
1537 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1538 return (nd6_setdefaultiface(ndif->ifindex));
1539 break;
1541 return (error);
1545 * Create neighbor cache entry and cache link-layer address,
1546 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1548 struct rtentry *
1549 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1550 int lladdrlen,
1551 int type, /* ICMP6 type */
1552 int code /* type dependent information */)
1554 struct rtentry *rt = NULL;
1555 struct llinfo_nd6 *ln = NULL;
1556 int is_newentry;
1557 struct sockaddr_dl *sdl = NULL;
1558 int do_update;
1559 int olladdr;
1560 int llchange;
1561 int newstate = 0;
1563 if (!ifp)
1564 panic("ifp == NULL in nd6_cache_lladdr");
1565 if (!from)
1566 panic("from == NULL in nd6_cache_lladdr");
1568 /* nothing must be updated for unspecified address */
1569 if (IN6_IS_ADDR_UNSPECIFIED(from))
1570 return NULL;
1573 * Validation about ifp->if_addrlen and lladdrlen must be done in
1574 * the caller.
1576 * XXX If the link does not have link-layer adderss, what should
1577 * we do? (ifp->if_addrlen == 0)
1578 * Spec says nothing in sections for RA, RS and NA. There's small
1579 * description on it in NS section (RFC 2461 7.2.3).
1582 rt = nd6_lookup(from, 0, ifp);
1583 if (!rt) {
1584 #if 0
1585 /* nothing must be done if there's no lladdr */
1586 if (!lladdr || !lladdrlen)
1587 return NULL;
1588 #endif
1590 rt = nd6_lookup(from, 1, ifp);
1591 is_newentry = 1;
1592 } else {
1593 /* do nothing if static ndp is set */
1594 if (rt->rt_flags & RTF_STATIC)
1595 return NULL;
1596 is_newentry = 0;
1599 if (!rt)
1600 return NULL;
1601 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1602 fail:
1603 nd6_free(rt);
1604 return NULL;
1606 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1607 if (!ln)
1608 goto fail;
1609 if (!rt->rt_gateway)
1610 goto fail;
1611 if (rt->rt_gateway->sa_family != AF_LINK)
1612 goto fail;
1613 sdl = SDL(rt->rt_gateway);
1615 olladdr = (sdl->sdl_alen) ? 1 : 0;
1616 if (olladdr && lladdr) {
1617 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1618 llchange = 1;
1619 else
1620 llchange = 0;
1621 } else
1622 llchange = 0;
1625 * newentry olladdr lladdr llchange (*=record)
1626 * 0 n n -- (1)
1627 * 0 y n -- (2)
1628 * 0 n y -- (3) * STALE
1629 * 0 y y n (4) *
1630 * 0 y y y (5) * STALE
1631 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1632 * 1 -- y -- (7) * STALE
1635 if (lladdr) { /* (3-5) and (7) */
1637 * Record source link-layer address
1638 * XXX is it dependent to ifp->if_type?
1640 sdl->sdl_alen = ifp->if_addrlen;
1641 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1644 if (!is_newentry) {
1645 if ((!olladdr && lladdr) /* (3) */
1646 || (olladdr && lladdr && llchange)) { /* (5) */
1647 do_update = 1;
1648 newstate = ND6_LLINFO_STALE;
1649 } else /* (1-2,4) */
1650 do_update = 0;
1651 } else {
1652 do_update = 1;
1653 if (!lladdr) /* (6) */
1654 newstate = ND6_LLINFO_NOSTATE;
1655 else /* (7) */
1656 newstate = ND6_LLINFO_STALE;
1659 if (do_update) {
1661 * Update the state of the neighbor cache.
1663 ln->ln_state = newstate;
1665 if (ln->ln_state == ND6_LLINFO_STALE) {
1667 * XXX: since nd6_output() below will cause
1668 * state tansition to DELAY and reset the timer,
1669 * we must set the timer now, although it is actually
1670 * meaningless.
1672 ln->ln_expire = time_second + nd6_gctimer;
1674 if (ln->ln_hold) {
1676 * we assume ifp is not a p2p here, so just
1677 * set the 2nd argument as the 1st one.
1679 nd6_output(ifp, ifp, ln->ln_hold,
1680 (struct sockaddr_in6 *)rt_key(rt),
1681 rt);
1682 ln->ln_hold = NULL;
1684 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1685 /* probe right away */
1686 ln->ln_expire = time_second;
1691 * ICMP6 type dependent behavior.
1693 * NS: clear IsRouter if new entry
1694 * RS: clear IsRouter
1695 * RA: set IsRouter if there's lladdr
1696 * redir: clear IsRouter if new entry
1698 * RA case, (1):
1699 * The spec says that we must set IsRouter in the following cases:
1700 * - If lladdr exist, set IsRouter. This means (1-5).
1701 * - If it is old entry (!newentry), set IsRouter. This means (7).
1702 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1703 * A quetion arises for (1) case. (1) case has no lladdr in the
1704 * neighbor cache, this is similar to (6).
1705 * This case is rare but we figured that we MUST NOT set IsRouter.
1707 * newentry olladdr lladdr llchange NS RS RA redir
1708 * D R
1709 * 0 n n -- (1) c ? s
1710 * 0 y n -- (2) c s s
1711 * 0 n y -- (3) c s s
1712 * 0 y y n (4) c s s
1713 * 0 y y y (5) c s s
1714 * 1 -- n -- (6) c c c s
1715 * 1 -- y -- (7) c c s c s
1717 * (c=clear s=set)
1719 switch (type & 0xff) {
1720 case ND_NEIGHBOR_SOLICIT:
1722 * New entry must have is_router flag cleared.
1724 if (is_newentry) /* (6-7) */
1725 ln->ln_router = 0;
1726 break;
1727 case ND_REDIRECT:
1729 * If the icmp is a redirect to a better router, always set the
1730 * is_router flag. Otherwise, if the entry is newly created,
1731 * clear the flag. [RFC 2461, sec 8.3]
1733 if (code == ND_REDIRECT_ROUTER)
1734 ln->ln_router = 1;
1735 else if (is_newentry) /* (6-7) */
1736 ln->ln_router = 0;
1737 break;
1738 case ND_ROUTER_SOLICIT:
1740 * is_router flag must always be cleared.
1742 ln->ln_router = 0;
1743 break;
1744 case ND_ROUTER_ADVERT:
1746 * Mark an entry with lladdr as a router.
1748 if ((!is_newentry && (olladdr || lladdr)) /* (2-5) */
1749 || (is_newentry && lladdr)) { /* (7) */
1750 ln->ln_router = 1;
1752 break;
1756 * When the link-layer address of a router changes, select the
1757 * best router again. In particular, when the neighbor entry is newly
1758 * created, it might affect the selection policy.
1759 * Question: can we restrict the first condition to the "is_newentry"
1760 * case?
1761 * XXX: when we hear an RA from a new router with the link-layer
1762 * address option, defrouter_select() is called twice, since
1763 * defrtrlist_update called the function as well. However, I believe
1764 * we can compromise the overhead, since it only happens the first
1765 * time.
1766 * XXX: although defrouter_select() should not have a bad effect
1767 * for those are not autoconfigured hosts, we explicitly avoid such
1768 * cases for safety.
1770 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1771 defrouter_select();
1773 return rt;
1776 static void
1777 nd6_slowtimo(void *ignored_arg)
1779 struct nd_ifinfo *nd6if;
1780 struct ifnet *ifp;
1782 crit_enter();
1783 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1784 nd6_slowtimo, NULL);
1785 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1786 nd6if = ND_IFINFO(ifp);
1787 if (nd6if->basereachable && /* already initialized */
1788 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1790 * Since reachable time rarely changes by router
1791 * advertisements, we SHOULD insure that a new random
1792 * value gets recomputed at least once every few hours.
1793 * (RFC 2461, 6.3.4)
1795 nd6if->recalctm = nd6_recalc_reachtm_interval;
1796 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1799 crit_exit();
1802 #define gotoerr(e) { error = (e); goto bad;}
1805 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1806 struct sockaddr_in6 *dst, struct rtentry *rt)
1808 struct llinfo_nd6 *ln = NULL;
1809 int error = 0;
1811 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1812 goto sendpkt;
1814 if (nd6_need_cache(ifp) == 0)
1815 goto sendpkt;
1818 * next hop determination. This routine is derived from ether_outpout.
1820 if (rt != NULL) {
1821 if (!(rt->rt_flags & RTF_UP)) {
1822 rt = rtlookup((struct sockaddr *)dst);
1823 if (rt == NULL)
1824 gotoerr(EHOSTUNREACH);
1825 rt->rt_refcnt--;
1826 if (rt->rt_ifp != ifp) {
1827 /* XXX: loop care? */
1828 return nd6_output(ifp, origifp, m, dst, rt);
1831 if (rt->rt_flags & RTF_GATEWAY) {
1832 struct sockaddr_in6 *gw6;
1835 * We skip link-layer address resolution and NUD
1836 * if the gateway is not a neighbor from ND point
1837 * of view, regardless of the value of nd_ifinfo.flags.
1838 * The second condition is a bit tricky; we skip
1839 * if the gateway is our own address, which is
1840 * sometimes used to install a route to a p2p link.
1842 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1843 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1844 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1846 * We allow this kind of tricky route only
1847 * when the outgoing interface is p2p.
1848 * XXX: we may need a more generic rule here.
1850 if (!(ifp->if_flags & IFF_POINTOPOINT))
1851 gotoerr(EHOSTUNREACH);
1853 goto sendpkt;
1856 if (rt->rt_gwroute == NULL) {
1857 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1858 if (rt->rt_gwroute == NULL)
1859 gotoerr(EHOSTUNREACH);
1860 } else if (!(rt->rt_gwroute->rt_flags & RTF_UP)) {
1861 rtfree(rt->rt_gwroute);
1862 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1863 if (rt->rt_gwroute == NULL)
1864 gotoerr(EHOSTUNREACH);
1870 * Address resolution or Neighbor Unreachability Detection
1871 * for the next hop.
1872 * At this point, the destination of the packet must be a unicast
1873 * or an anycast address(i.e. not a multicast).
1876 /* Look up the neighbor cache for the nexthop */
1877 if (rt && (rt->rt_flags & RTF_LLINFO))
1878 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1879 else {
1881 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1882 * the condition below is not very efficient. But we believe
1883 * it is tolerable, because this should be a rare case.
1885 if (nd6_is_addr_neighbor(dst, ifp) &&
1886 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1887 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1889 if (!ln || !rt) {
1890 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
1891 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1892 log(LOG_DEBUG,
1893 "nd6_output: can't allocate llinfo for %s "
1894 "(ln=%p, rt=%p)\n",
1895 ip6_sprintf(&dst->sin6_addr), ln, rt);
1896 gotoerr(EIO); /* XXX: good error? */
1899 goto sendpkt; /* send anyway */
1902 /* We don't have to do link-layer address resolution on a p2p link. */
1903 if ((ifp->if_flags & IFF_POINTOPOINT) &&
1904 ln->ln_state < ND6_LLINFO_REACHABLE) {
1905 ln->ln_state = ND6_LLINFO_STALE;
1906 ln->ln_expire = time_second + nd6_gctimer;
1910 * The first time we send a packet to a neighbor whose entry is
1911 * STALE, we have to change the state to DELAY and a sets a timer to
1912 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1913 * neighbor unreachability detection on expiration.
1914 * (RFC 2461 7.3.3)
1916 if (ln->ln_state == ND6_LLINFO_STALE) {
1917 ln->ln_asked = 0;
1918 ln->ln_state = ND6_LLINFO_DELAY;
1919 ln->ln_expire = time_second + nd6_delay;
1923 * If the neighbor cache entry has a state other than INCOMPLETE
1924 * (i.e. its link-layer address is already resolved), just
1925 * send the packet.
1927 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1928 goto sendpkt;
1931 * There is a neighbor cache entry, but no ethernet address
1932 * response yet. Replace the held mbuf (if any) with this
1933 * latest one.
1935 * This code conforms to the rate-limiting rule described in Section
1936 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
1937 * an NS below.
1939 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1940 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1941 if (ln->ln_hold)
1942 m_freem(ln->ln_hold);
1943 ln->ln_hold = m;
1944 if (ln->ln_expire) {
1945 if (ln->ln_asked < nd6_mmaxtries &&
1946 ln->ln_expire < time_second) {
1947 ln->ln_asked++;
1948 ln->ln_expire = time_second +
1949 ND_IFINFO(ifp)->retrans / 1000;
1950 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1953 return (0);
1955 sendpkt:
1956 if (ifp->if_flags & IFF_LOOPBACK)
1957 error = ifp->if_output(origifp, m, (struct sockaddr *)dst, rt);
1958 else
1959 error = ifp->if_output(ifp, m, (struct sockaddr *)dst, rt);
1960 return (error);
1962 bad:
1963 m_freem(m);
1964 return (error);
1966 #undef gotoerr
1969 nd6_need_cache(struct ifnet *ifp)
1972 * XXX: we currently do not make neighbor cache on any interface
1973 * other than Ethernet and GIF.
1975 * RFC2893 says:
1976 * - unidirectional tunnels needs no ND
1978 switch (ifp->if_type) {
1979 case IFT_ETHER:
1980 case IFT_IEEE1394:
1981 #ifdef IFT_L2VLAN
1982 case IFT_L2VLAN:
1983 #endif
1984 #ifdef IFT_IEEE80211
1985 case IFT_IEEE80211:
1986 #endif
1987 #ifdef IFT_CARP
1988 case IFT_CARP:
1989 #endif
1990 case IFT_GIF: /* XXX need more cases? */
1991 return (1);
1992 default:
1993 return (0);
1998 nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
1999 struct sockaddr *dst, u_char *desten)
2001 struct sockaddr_dl *sdl;
2002 struct rtentry *rt;
2005 if (m->m_flags & M_MCAST) {
2006 switch (ifp->if_type) {
2007 case IFT_ETHER:
2008 #ifdef IFT_L2VLAN
2009 case IFT_L2VLAN:
2010 #endif
2011 #ifdef IFT_IEEE80211
2012 case IFT_IEEE80211:
2013 #endif
2014 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2015 desten);
2016 return (1);
2017 case IFT_IEEE1394:
2018 bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
2019 return (1);
2020 default:
2021 m_freem(m);
2022 return (0);
2025 if (rt0 == NULL) {
2026 /* this could happen, if we could not allocate memory */
2027 m_freem(m);
2028 return (0);
2030 if (rt_llroute(dst, rt0, &rt) != 0) {
2031 m_freem(m);
2032 return (0);
2034 if (rt->rt_gateway->sa_family != AF_LINK) {
2035 kprintf("nd6_storelladdr: something odd happens\n");
2036 m_freem(m);
2037 return (0);
2039 sdl = SDL(rt->rt_gateway);
2040 if (sdl->sdl_alen == 0) {
2041 /* this should be impossible, but we bark here for debugging */
2042 kprintf("nd6_storelladdr: sdl_alen == 0\n");
2043 m_freem(m);
2044 return (0);
2047 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2048 return (1);
2051 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2052 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2053 #ifdef SYSCTL_DECL
2054 SYSCTL_DECL(_net_inet6_icmp6);
2055 #endif
2056 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2057 CTLFLAG_RD, nd6_sysctl_drlist, "");
2058 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2059 CTLFLAG_RD, nd6_sysctl_prlist, "");
2061 static int
2062 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2064 int error;
2065 char buf[1024];
2066 struct in6_defrouter *d, *de;
2067 struct nd_defrouter *dr;
2069 if (req->newptr)
2070 return EPERM;
2071 error = 0;
2073 for (dr = TAILQ_FIRST(&nd_defrouter);
2075 dr = TAILQ_NEXT(dr, dr_entry)) {
2076 d = (struct in6_defrouter *)buf;
2077 de = (struct in6_defrouter *)(buf + sizeof(buf));
2079 if (d + 1 <= de) {
2080 bzero(d, sizeof(*d));
2081 d->rtaddr.sin6_family = AF_INET6;
2082 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2083 if (in6_recoverscope(&d->rtaddr, &dr->rtaddr,
2084 dr->ifp) != 0)
2085 log(LOG_ERR,
2086 "scope error in "
2087 "default router list (%s)\n",
2088 ip6_sprintf(&dr->rtaddr));
2089 d->flags = dr->flags;
2090 d->rtlifetime = dr->rtlifetime;
2091 d->expire = dr->expire;
2092 d->if_index = dr->ifp->if_index;
2093 } else
2094 panic("buffer too short");
2096 error = SYSCTL_OUT(req, buf, sizeof(*d));
2097 if (error)
2098 break;
2100 return error;
2103 static int
2104 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2106 int error;
2107 char buf[1024];
2108 struct in6_prefix *p, *pe;
2109 struct nd_prefix *pr;
2111 if (req->newptr)
2112 return EPERM;
2113 error = 0;
2115 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2116 u_short advrtrs;
2117 size_t advance;
2118 struct sockaddr_in6 *sin6, *s6;
2119 struct nd_pfxrouter *pfr;
2121 p = (struct in6_prefix *)buf;
2122 pe = (struct in6_prefix *)(buf + sizeof(buf));
2124 if (p + 1 <= pe) {
2125 bzero(p, sizeof(*p));
2126 sin6 = (struct sockaddr_in6 *)(p + 1);
2128 p->prefix = pr->ndpr_prefix;
2129 if (in6_recoverscope(&p->prefix,
2130 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2131 log(LOG_ERR,
2132 "scope error in prefix list (%s)\n",
2133 ip6_sprintf(&p->prefix.sin6_addr));
2134 p->raflags = pr->ndpr_raf;
2135 p->prefixlen = pr->ndpr_plen;
2136 p->vltime = pr->ndpr_vltime;
2137 p->pltime = pr->ndpr_pltime;
2138 p->if_index = pr->ndpr_ifp->if_index;
2139 p->expire = pr->ndpr_expire;
2140 p->refcnt = pr->ndpr_refcnt;
2141 p->flags = pr->ndpr_stateflags;
2142 p->origin = PR_ORIG_RA;
2143 advrtrs = 0;
2144 for (pfr = pr->ndpr_advrtrs.lh_first;
2145 pfr;
2146 pfr = pfr->pfr_next) {
2147 if ((void *)&sin6[advrtrs + 1] >
2148 (void *)pe) {
2149 advrtrs++;
2150 continue;
2152 s6 = &sin6[advrtrs];
2153 bzero(s6, sizeof(*s6));
2154 s6->sin6_family = AF_INET6;
2155 s6->sin6_len = sizeof(*sin6);
2156 if (in6_recoverscope(s6, &pfr->router->rtaddr,
2157 pfr->router->ifp) != 0)
2158 log(LOG_ERR,
2159 "scope error in "
2160 "prefix list (%s)\n",
2161 ip6_sprintf(&pfr->router->rtaddr));
2162 advrtrs++;
2164 p->advrtrs = advrtrs;
2165 } else
2166 panic("buffer too short");
2168 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2169 error = SYSCTL_OUT(req, buf, advance);
2170 if (error)
2171 break;
2173 return error;