Repo-copy numerous files from sys/emulation/posix4 to sys/sys and sys/kern
[dragonfly/vkernel-mp.git] / sys / netinet6 / nd6.c
blobcf8e8d2052640c93fd930a04d193a27273172c61
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.22 2006/12/23 00:57:53 swildner 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 <netinet/if_fddi.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet6/nd6.h>
74 #include <netinet6/in6_prefix.h>
75 #include <netinet/icmp6.h>
77 #include "use_loop.h"
79 #include <net/net_osdep.h>
81 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
82 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
84 #define SIN6(s) ((struct sockaddr_in6 *)s)
85 #define SDL(s) ((struct sockaddr_dl *)s)
87 /* timer values */
88 int nd6_prune = 1; /* walk list every 1 seconds */
89 int nd6_delay = 5; /* delay first probe time 5 second */
90 int nd6_umaxtries = 3; /* maximum unicast query */
91 int nd6_mmaxtries = 3; /* maximum multicast query */
92 int nd6_useloopback = 1; /* use loopback interface for local traffic */
93 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
95 /* preventing too many loops in ND option parsing */
96 int nd6_maxndopt = 10; /* max # of ND options allowed */
98 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
100 #ifdef ND6_DEBUG
101 int nd6_debug = 1;
102 #else
103 int nd6_debug = 0;
104 #endif
106 /* for debugging? */
107 static int nd6_inuse, nd6_allocated;
109 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
110 struct nd_drhead nd_defrouter;
111 struct nd_prhead nd_prefix = { 0 };
113 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
114 static struct sockaddr_in6 all1_sa;
116 static void nd6_setmtu0 (struct ifnet *, struct nd_ifinfo *);
117 static void nd6_slowtimo (void *);
118 static int regen_tmpaddr (struct in6_ifaddr *);
120 struct callout nd6_slowtimo_ch;
121 struct callout nd6_timer_ch;
122 extern struct callout in6_tmpaddrtimer_ch;
124 void
125 nd6_init(void)
127 static int nd6_init_done = 0;
128 int i;
130 if (nd6_init_done) {
131 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
132 return;
135 all1_sa.sin6_family = AF_INET6;
136 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
137 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
138 all1_sa.sin6_addr.s6_addr[i] = 0xff;
140 /* initialization of the default router list */
141 TAILQ_INIT(&nd_defrouter);
143 nd6_init_done = 1;
145 /* start timer */
146 callout_init(&nd6_slowtimo_ch);
147 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
148 nd6_slowtimo, NULL);
151 struct nd_ifinfo *
152 nd6_ifattach(struct ifnet *ifp)
154 struct nd_ifinfo *nd;
156 nd = (struct nd_ifinfo *)kmalloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
157 bzero(nd, sizeof(*nd));
159 nd->initialized = 1;
161 nd->linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu;
162 nd->chlim = IPV6_DEFHLIM;
163 nd->basereachable = REACHABLE_TIME;
164 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
165 nd->retrans = RETRANS_TIMER;
166 nd->receivedra = 0;
169 * Note that the default value of ip6_accept_rtadv is 0, which means
170 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
171 * here.
173 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
175 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
176 nd6_setmtu0(ifp, nd);
177 return nd;
180 void
181 nd6_ifdetach(struct nd_ifinfo *nd)
183 kfree(nd, M_IP6NDP);
187 * Reset ND level link MTU. This function is called when the physical MTU
188 * changes, which means we might have to adjust the ND level MTU.
190 void
191 nd6_setmtu(struct ifnet *ifp)
193 nd6_setmtu0(ifp, ND_IFINFO(ifp));
196 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
197 void
198 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
200 u_long oldmaxmtu;
201 u_long oldlinkmtu;
203 oldmaxmtu = ndi->maxmtu;
204 oldlinkmtu = ndi->linkmtu;
206 switch (ifp->if_type) {
207 case IFT_ARCNET: /* XXX MTU handling needs more work */
208 ndi->maxmtu = MIN(60480, ifp->if_mtu);
209 break;
210 case IFT_ETHER:
211 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
212 break;
213 case IFT_FDDI:
214 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
215 break;
216 case IFT_ATM:
217 ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu);
218 break;
219 case IFT_IEEE1394: /* XXX should be IEEE1394MTU(1500) */
220 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
221 break;
222 #ifdef IFT_IEEE80211
223 case IFT_IEEE80211: /* XXX should be IEEE80211MTU(1500) */
224 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
225 break;
226 #endif
227 default:
228 ndi->maxmtu = ifp->if_mtu;
229 break;
232 if (oldmaxmtu != ndi->maxmtu) {
234 * If the ND level MTU is not set yet, or if the maxmtu
235 * is reset to a smaller value than the ND level MTU,
236 * also reset the ND level MTU.
238 if (ndi->linkmtu == 0 ||
239 ndi->maxmtu < ndi->linkmtu) {
240 ndi->linkmtu = ndi->maxmtu;
241 /* also adjust in6_maxmtu if necessary. */
242 if (oldlinkmtu == 0) {
244 * XXX: the case analysis is grotty, but
245 * it is not efficient to call in6_setmaxmtu()
246 * here when we are during the initialization
247 * procedure.
249 if (in6_maxmtu < ndi->linkmtu)
250 in6_maxmtu = ndi->linkmtu;
251 } else
252 in6_setmaxmtu();
255 #undef MIN
258 void
259 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
261 bzero(ndopts, sizeof(*ndopts));
262 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
263 ndopts->nd_opts_last
264 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
266 if (icmp6len == 0) {
267 ndopts->nd_opts_done = 1;
268 ndopts->nd_opts_search = NULL;
273 * Take one ND option.
275 struct nd_opt_hdr *
276 nd6_option(union nd_opts *ndopts)
278 struct nd_opt_hdr *nd_opt;
279 int olen;
281 if (!ndopts)
282 panic("ndopts == NULL in nd6_option");
283 if (!ndopts->nd_opts_last)
284 panic("uninitialized ndopts in nd6_option");
285 if (!ndopts->nd_opts_search)
286 return NULL;
287 if (ndopts->nd_opts_done)
288 return NULL;
290 nd_opt = ndopts->nd_opts_search;
292 /* make sure nd_opt_len is inside the buffer */
293 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
294 bzero(ndopts, sizeof(*ndopts));
295 return NULL;
298 olen = nd_opt->nd_opt_len << 3;
299 if (olen == 0) {
301 * Message validation requires that all included
302 * options have a length that is greater than zero.
304 bzero(ndopts, sizeof(*ndopts));
305 return NULL;
308 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
309 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
310 /* option overruns the end of buffer, invalid */
311 bzero(ndopts, sizeof(*ndopts));
312 return NULL;
313 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
314 /* reached the end of options chain */
315 ndopts->nd_opts_done = 1;
316 ndopts->nd_opts_search = NULL;
318 return nd_opt;
322 * Parse multiple ND options.
323 * This function is much easier to use, for ND routines that do not need
324 * multiple options of the same type.
327 nd6_options(union nd_opts *ndopts)
329 struct nd_opt_hdr *nd_opt;
330 int i = 0;
332 if (!ndopts)
333 panic("ndopts == NULL in nd6_options");
334 if (!ndopts->nd_opts_last)
335 panic("uninitialized ndopts in nd6_options");
336 if (!ndopts->nd_opts_search)
337 return 0;
339 while (1) {
340 nd_opt = nd6_option(ndopts);
341 if (!nd_opt && !ndopts->nd_opts_last) {
343 * Message validation requires that all included
344 * options have a length that is greater than zero.
346 icmp6stat.icp6s_nd_badopt++;
347 bzero(ndopts, sizeof(*ndopts));
348 return -1;
351 if (!nd_opt)
352 goto skip1;
354 switch (nd_opt->nd_opt_type) {
355 case ND_OPT_SOURCE_LINKADDR:
356 case ND_OPT_TARGET_LINKADDR:
357 case ND_OPT_MTU:
358 case ND_OPT_REDIRECTED_HEADER:
359 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
360 nd6log((LOG_INFO,
361 "duplicated ND6 option found (type=%d)\n",
362 nd_opt->nd_opt_type));
363 /* XXX bark? */
364 } else {
365 ndopts->nd_opt_array[nd_opt->nd_opt_type]
366 = nd_opt;
368 break;
369 case ND_OPT_PREFIX_INFORMATION:
370 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
371 ndopts->nd_opt_array[nd_opt->nd_opt_type]
372 = nd_opt;
374 ndopts->nd_opts_pi_end =
375 (struct nd_opt_prefix_info *)nd_opt;
376 break;
377 default:
379 * Unknown options must be silently ignored,
380 * to accomodate future extension to the protocol.
382 nd6log((LOG_DEBUG,
383 "nd6_options: unsupported option %d - "
384 "option ignored\n", nd_opt->nd_opt_type));
387 skip1:
388 i++;
389 if (i > nd6_maxndopt) {
390 icmp6stat.icp6s_nd_toomanyopt++;
391 nd6log((LOG_INFO, "too many loop in nd opt\n"));
392 break;
395 if (ndopts->nd_opts_done)
396 break;
399 return 0;
403 * ND6 timer routine to expire default route list and prefix list
405 void
406 nd6_timer(void *ignored_arg)
408 struct llinfo_nd6 *ln;
409 struct nd_defrouter *dr;
410 struct nd_prefix *pr;
411 struct ifnet *ifp;
412 struct in6_ifaddr *ia6, *nia6;
413 struct in6_addrlifetime *lt6;
415 crit_enter();
416 callout_reset(&nd6_timer_ch, nd6_prune * hz,
417 nd6_timer, NULL);
419 ln = llinfo_nd6.ln_next;
420 while (ln && ln != &llinfo_nd6) {
421 struct rtentry *rt;
422 struct sockaddr_in6 *dst;
423 struct llinfo_nd6 *next = ln->ln_next;
424 /* XXX: used for the DELAY case only: */
425 struct nd_ifinfo *ndi = NULL;
427 if ((rt = ln->ln_rt) == NULL) {
428 ln = next;
429 continue;
431 if ((ifp = rt->rt_ifp) == NULL) {
432 ln = next;
433 continue;
435 ndi = ND_IFINFO(ifp);
436 dst = (struct sockaddr_in6 *)rt_key(rt);
438 if (ln->ln_expire > time_second) {
439 ln = next;
440 continue;
443 /* sanity check */
444 if (!rt)
445 panic("rt=0 in nd6_timer(ln=%p)", ln);
446 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
447 panic("rt_llinfo(%p) is not equal to ln(%p)",
448 rt->rt_llinfo, ln);
449 if (!dst)
450 panic("dst=0 in nd6_timer(ln=%p)", ln);
452 switch (ln->ln_state) {
453 case ND6_LLINFO_INCOMPLETE:
454 if (ln->ln_asked < nd6_mmaxtries) {
455 ln->ln_asked++;
456 ln->ln_expire = time_second +
457 ND_IFINFO(ifp)->retrans / 1000;
458 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
459 ln, 0);
460 } else {
461 struct mbuf *m = ln->ln_hold;
462 if (m) {
463 if (rt->rt_ifp) {
465 * Fake rcvif to make ICMP error
466 * more helpful in diagnosing
467 * for the receiver.
468 * XXX: should we consider
469 * older rcvif?
471 m->m_pkthdr.rcvif = rt->rt_ifp;
473 icmp6_error(m, ICMP6_DST_UNREACH,
474 ICMP6_DST_UNREACH_ADDR, 0);
475 ln->ln_hold = NULL;
477 next = nd6_free(rt);
479 break;
480 case ND6_LLINFO_REACHABLE:
481 if (ln->ln_expire) {
482 ln->ln_state = ND6_LLINFO_STALE;
483 ln->ln_expire = time_second + nd6_gctimer;
485 break;
487 case ND6_LLINFO_STALE:
488 /* Garbage Collection(RFC 2461 5.3) */
489 if (ln->ln_expire)
490 next = nd6_free(rt);
491 break;
493 case ND6_LLINFO_DELAY:
494 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD)) {
495 /* We need NUD */
496 ln->ln_asked = 1;
497 ln->ln_state = ND6_LLINFO_PROBE;
498 ln->ln_expire = time_second +
499 ndi->retrans / 1000;
500 nd6_ns_output(ifp, &dst->sin6_addr,
501 &dst->sin6_addr,
502 ln, 0);
503 } else {
504 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
505 ln->ln_expire = time_second + nd6_gctimer;
507 break;
508 case ND6_LLINFO_PROBE:
509 if (ln->ln_asked < nd6_umaxtries) {
510 ln->ln_asked++;
511 ln->ln_expire = time_second +
512 ND_IFINFO(ifp)->retrans / 1000;
513 nd6_ns_output(ifp, &dst->sin6_addr,
514 &dst->sin6_addr, ln, 0);
515 } else {
516 next = nd6_free(rt);
518 break;
520 ln = next;
523 /* expire default router list */
524 dr = TAILQ_FIRST(&nd_defrouter);
525 while (dr) {
526 if (dr->expire && dr->expire < time_second) {
527 struct nd_defrouter *t;
528 t = TAILQ_NEXT(dr, dr_entry);
529 defrtrlist_del(dr);
530 dr = t;
531 } else {
532 dr = TAILQ_NEXT(dr, dr_entry);
537 * expire interface addresses.
538 * in the past the loop was inside prefix expiry processing.
539 * However, from a stricter speci-confrmance standpoint, we should
540 * rather separate address lifetimes and prefix lifetimes.
542 addrloop:
543 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
544 nia6 = ia6->ia_next;
545 /* check address lifetime */
546 lt6 = &ia6->ia6_lifetime;
547 if (IFA6_IS_INVALID(ia6)) {
548 int regen = 0;
551 * If the expiring address is temporary, try
552 * regenerating a new one. This would be useful when
553 * we suspended a laptop PC, then turned it on after a
554 * period that could invalidate all temporary
555 * addresses. Although we may have to restart the
556 * loop (see below), it must be after purging the
557 * address. Otherwise, we'd see an infinite loop of
558 * regeneration.
560 if (ip6_use_tempaddr &&
561 (ia6->ia6_flags & IN6_IFF_TEMPORARY)) {
562 if (regen_tmpaddr(ia6) == 0)
563 regen = 1;
566 in6_purgeaddr(&ia6->ia_ifa);
568 if (regen)
569 goto addrloop; /* XXX: see below */
571 if (IFA6_IS_DEPRECATED(ia6)) {
572 int oldflags = ia6->ia6_flags;
574 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
577 * If a temporary address has just become deprecated,
578 * regenerate a new one if possible.
580 if (ip6_use_tempaddr &&
581 (ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
582 !(oldflags & IN6_IFF_DEPRECATED)) {
584 if (regen_tmpaddr(ia6) == 0) {
586 * A new temporary address is
587 * generated.
588 * XXX: this means the address chain
589 * has changed while we are still in
590 * the loop. Although the change
591 * would not cause disaster (because
592 * it's not a deletion, but an
593 * addition,) we'd rather restart the
594 * loop just for safety. Or does this
595 * significantly reduce performance??
597 goto addrloop;
600 } else {
602 * A new RA might have made a deprecated address
603 * preferred.
605 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
609 /* expire prefix list */
610 pr = nd_prefix.lh_first;
611 while (pr) {
613 * check prefix lifetime.
614 * since pltime is just for autoconf, pltime processing for
615 * prefix is not necessary.
617 if (pr->ndpr_expire && pr->ndpr_expire < time_second) {
618 struct nd_prefix *t;
619 t = pr->ndpr_next;
622 * address expiration and prefix expiration are
623 * separate. NEVER perform in6_purgeaddr here.
626 prelist_remove(pr);
627 pr = t;
628 } else
629 pr = pr->ndpr_next;
631 crit_exit();
634 static int
635 regen_tmpaddr(struct in6_ifaddr *ia6) /* deprecated/invalidated temporary
636 address */
638 struct ifaddr *ifa;
639 struct ifnet *ifp;
640 struct in6_ifaddr *public_ifa6 = NULL;
642 ifp = ia6->ia_ifa.ifa_ifp;
643 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_list) {
644 struct in6_ifaddr *it6;
646 if (ifa->ifa_addr->sa_family != AF_INET6)
647 continue;
649 it6 = (struct in6_ifaddr *)ifa;
651 /* ignore no autoconf addresses. */
652 if (!(it6->ia6_flags & IN6_IFF_AUTOCONF))
653 continue;
655 /* ignore autoconf addresses with different prefixes. */
656 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
657 continue;
660 * Now we are looking at an autoconf address with the same
661 * prefix as ours. If the address is temporary and is still
662 * preferred, do not create another one. It would be rare, but
663 * could happen, for example, when we resume a laptop PC after
664 * a long period.
666 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) &&
667 !IFA6_IS_DEPRECATED(it6)) {
668 public_ifa6 = NULL;
669 break;
673 * This is a public autoconf address that has the same prefix
674 * as ours. If it is preferred, keep it. We can't break the
675 * loop here, because there may be a still-preferred temporary
676 * address with the prefix.
678 if (!IFA6_IS_DEPRECATED(it6))
679 public_ifa6 = it6;
682 if (public_ifa6 != NULL) {
683 int e;
685 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
686 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
687 " tmp addr,errno=%d\n", e);
688 return (-1);
690 return (0);
693 return (-1);
697 * Nuke neighbor cache/prefix/default router management table, right before
698 * ifp goes away.
700 void
701 nd6_purge(struct ifnet *ifp)
703 struct llinfo_nd6 *ln, *nln;
704 struct nd_defrouter *dr, *ndr, drany;
705 struct nd_prefix *pr, *npr;
707 /* Nuke default router list entries toward ifp */
708 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
710 * The first entry of the list may be stored in
711 * the routing table, so we'll delete it later.
713 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
714 ndr = TAILQ_NEXT(dr, dr_entry);
715 if (dr->ifp == ifp)
716 defrtrlist_del(dr);
718 dr = TAILQ_FIRST(&nd_defrouter);
719 if (dr->ifp == ifp)
720 defrtrlist_del(dr);
723 /* Nuke prefix list entries toward ifp */
724 for (pr = nd_prefix.lh_first; pr; pr = npr) {
725 npr = pr->ndpr_next;
726 if (pr->ndpr_ifp == ifp) {
728 * Previously, pr->ndpr_addr is removed as well,
729 * but I strongly believe we don't have to do it.
730 * nd6_purge() is only called from in6_ifdetach(),
731 * which removes all the associated interface addresses
732 * by itself.
733 * (jinmei@kame.net 20010129)
735 prelist_remove(pr);
739 /* cancel default outgoing interface setting */
740 if (nd6_defifindex == ifp->if_index)
741 nd6_setdefaultiface(0);
743 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
744 /* refresh default router list */
745 bzero(&drany, sizeof(drany));
746 defrouter_delreq(&drany, 0);
747 defrouter_select();
751 * Nuke neighbor cache entries for the ifp.
752 * Note that rt->rt_ifp may not be the same as ifp,
753 * due to KAME goto ours hack. See RTM_RESOLVE case in
754 * nd6_rtrequest(), and ip6_input().
756 ln = llinfo_nd6.ln_next;
757 while (ln && ln != &llinfo_nd6) {
758 struct rtentry *rt;
759 struct sockaddr_dl *sdl;
761 nln = ln->ln_next;
762 rt = ln->ln_rt;
763 if (rt && rt->rt_gateway &&
764 rt->rt_gateway->sa_family == AF_LINK) {
765 sdl = (struct sockaddr_dl *)rt->rt_gateway;
766 if (sdl->sdl_index == ifp->if_index)
767 nln = nd6_free(rt);
769 ln = nln;
773 struct rtentry *
774 nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp)
776 struct rtentry *rt;
777 struct sockaddr_in6 sin6;
779 bzero(&sin6, sizeof(sin6));
780 sin6.sin6_len = sizeof(struct sockaddr_in6);
781 sin6.sin6_family = AF_INET6;
782 sin6.sin6_addr = *addr6;
784 if (create)
785 rt = rtlookup((struct sockaddr *)&sin6);
786 else
787 rt = rtpurelookup((struct sockaddr *)&sin6);
788 if (rt && !(rt->rt_flags & RTF_LLINFO)) {
790 * This is the case for the default route.
791 * If we want to create a neighbor cache for the address, we
792 * should free the route for the destination and allocate an
793 * interface route.
795 if (create) {
796 --rt->rt_refcnt;
797 rt = NULL;
800 if (!rt) {
801 if (create && ifp) {
802 int e;
805 * If no route is available and create is set,
806 * we allocate a host route for the destination
807 * and treat it like an interface route.
808 * This hack is necessary for a neighbor which can't
809 * be covered by our own prefix.
811 struct ifaddr *ifa =
812 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
813 if (ifa == NULL)
814 return (NULL);
817 * Create a new route. RTF_LLINFO is necessary
818 * to create a Neighbor Cache entry for the
819 * destination in nd6_rtrequest which will be
820 * called in rtrequest via ifa->ifa_rtrequest.
822 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
823 ifa->ifa_addr,
824 (struct sockaddr *)&all1_sa,
825 (ifa->ifa_flags |
826 RTF_HOST | RTF_LLINFO) &
827 ~RTF_CLONING,
828 &rt)) != 0)
829 log(LOG_ERR,
830 "nd6_lookup: failed to add route for a "
831 "neighbor(%s), errno=%d\n",
832 ip6_sprintf(addr6), e);
833 if (rt == NULL)
834 return (NULL);
835 if (rt->rt_llinfo) {
836 struct llinfo_nd6 *ln =
837 (struct llinfo_nd6 *)rt->rt_llinfo;
838 ln->ln_state = ND6_LLINFO_NOSTATE;
840 } else
841 return (NULL);
843 rt->rt_refcnt--;
845 * Validation for the entry.
846 * Note that the check for rt_llinfo is necessary because a cloned
847 * route from a parent route that has the L flag (e.g. the default
848 * route to a p2p interface) may have the flag, too, while the
849 * destination is not actually a neighbor.
850 * XXX: we can't use rt->rt_ifp to check for the interface, since
851 * it might be the loopback interface if the entry is for our
852 * own address on a non-loopback interface. Instead, we should
853 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
854 * interface.
856 if ((rt->rt_flags & RTF_GATEWAY) || !(rt->rt_flags & RTF_LLINFO) ||
857 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
858 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
859 if (create) {
860 log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n",
861 ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
862 /* xxx more logs... kazu */
864 return (NULL);
866 return (rt);
870 * Detect if a given IPv6 address identifies a neighbor on a given link.
871 * XXX: should take care of the destination of a p2p link?
874 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
876 struct ifaddr *ifa;
877 int i;
879 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
880 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
883 * A link-local address is always a neighbor.
884 * XXX: we should use the sin6_scope_id field rather than the embedded
885 * interface index.
887 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
888 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
889 return (1);
892 * If the address matches one of our addresses,
893 * it should be a neighbor.
895 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
896 if (ifa->ifa_addr->sa_family != AF_INET6)
897 next: continue;
899 for (i = 0; i < 4; i++) {
900 if ((IFADDR6(ifa).s6_addr32[i] ^
901 addr->sin6_addr.s6_addr32[i]) &
902 IFMASK6(ifa).s6_addr32[i])
903 goto next;
905 return (1);
909 * Even if the address matches none of our addresses, it might be
910 * in the neighbor cache.
912 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
913 return (1);
915 return (0);
916 #undef IFADDR6
917 #undef IFMASK6
921 * Free an nd6 llinfo entry.
923 struct llinfo_nd6 *
924 nd6_free(struct rtentry *rt)
926 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
927 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
928 struct nd_defrouter *dr;
931 * we used to have pfctlinput(PRC_HOSTDEAD) here.
932 * even though it is not harmful, it was not really necessary.
935 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
936 crit_enter();
937 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
938 rt->rt_ifp);
940 if (ln->ln_router || dr) {
942 * rt6_flush must be called whether or not the neighbor
943 * is in the Default Router List.
944 * See a corresponding comment in nd6_na_input().
946 rt6_flush(&in6, rt->rt_ifp);
949 if (dr) {
951 * Unreachablity of a router might affect the default
952 * router selection and on-link detection of advertised
953 * prefixes.
957 * Temporarily fake the state to choose a new default
958 * router and to perform on-link determination of
959 * prefixes correctly.
960 * Below the state will be set correctly,
961 * or the entry itself will be deleted.
963 ln->ln_state = ND6_LLINFO_INCOMPLETE;
966 * Since defrouter_select() does not affect the
967 * on-link determination and MIP6 needs the check
968 * before the default router selection, we perform
969 * the check now.
971 pfxlist_onlink_check();
973 if (dr == TAILQ_FIRST(&nd_defrouter)) {
975 * It is used as the current default router,
976 * so we have to move it to the end of the
977 * list and choose a new one.
978 * XXX: it is not very efficient if this is
979 * the only router.
981 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
982 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
984 defrouter_select();
987 crit_exit();
991 * Before deleting the entry, remember the next entry as the
992 * return value. We need this because pfxlist_onlink_check() above
993 * might have freed other entries (particularly the old next entry) as
994 * a side effect (XXX).
996 next = ln->ln_next;
999 * Detach the route from the routing tree and the list of neighbor
1000 * caches, and disable the route entry not to be used in already
1001 * cached routes.
1003 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
1004 rt_mask(rt), 0, (struct rtentry **)0);
1006 return (next);
1010 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1012 * XXX cost-effective metods?
1014 void
1015 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1017 struct llinfo_nd6 *ln;
1020 * If the caller specified "rt", use that. Otherwise, resolve the
1021 * routing table by supplied "dst6".
1023 if (!rt) {
1024 if (!dst6)
1025 return;
1026 if (!(rt = nd6_lookup(dst6, 0, NULL)))
1027 return;
1030 if ((rt->rt_flags & RTF_GATEWAY) ||
1031 !(rt->rt_flags & RTF_LLINFO) ||
1032 rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
1033 rt->rt_gateway->sa_family != AF_LINK) {
1034 /* This is not a host route. */
1035 return;
1038 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1039 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1040 return;
1043 * if we get upper-layer reachability confirmation many times,
1044 * it is possible we have false information.
1046 if (!force) {
1047 ln->ln_byhint++;
1048 if (ln->ln_byhint > nd6_maxnudhint)
1049 return;
1052 ln->ln_state = ND6_LLINFO_REACHABLE;
1053 if (ln->ln_expire)
1054 ln->ln_expire = time_second +
1055 ND_IFINFO(rt->rt_ifp)->reachable;
1058 void
1059 nd6_rtrequest(int req, struct rtentry *rt,
1060 struct rt_addrinfo *info) /* xxx unused */
1062 struct sockaddr *gate = rt->rt_gateway;
1063 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1064 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1065 struct ifnet *ifp = rt->rt_ifp;
1066 struct ifaddr *ifa;
1068 if ((rt->rt_flags & RTF_GATEWAY))
1069 return;
1071 if (nd6_need_cache(ifp) == 0 && !(rt->rt_flags & RTF_HOST)) {
1073 * This is probably an interface direct route for a link
1074 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1075 * We do not need special treatment below for such a route.
1076 * Moreover, the RTF_LLINFO flag which would be set below
1077 * would annoy the ndp(8) command.
1079 return;
1082 if (req == RTM_RESOLVE &&
1083 (nd6_need_cache(ifp) == 0 || /* stf case */
1084 !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp))) {
1086 * FreeBSD and BSD/OS often make a cloned host route based
1087 * on a less-specific route (e.g. the default route).
1088 * If the less specific route does not have a "gateway"
1089 * (this is the case when the route just goes to a p2p or an
1090 * stf interface), we'll mistakenly make a neighbor cache for
1091 * the host route, and will see strange neighbor solicitation
1092 * for the corresponding destination. In order to avoid the
1093 * confusion, we check if the destination of the route is
1094 * a neighbor in terms of neighbor discovery, and stop the
1095 * process if not. Additionally, we remove the LLINFO flag
1096 * so that ndp(8) will not try to get the neighbor information
1097 * of the destination.
1099 rt->rt_flags &= ~RTF_LLINFO;
1100 return;
1103 switch (req) {
1104 case RTM_ADD:
1106 * There is no backward compatibility :)
1108 * if (!(rt->rt_flags & RTF_HOST) &&
1109 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1110 * rt->rt_flags |= RTF_CLONING;
1112 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1114 * Case 1: This route should come from
1115 * a route to interface. RTF_LLINFO flag is set
1116 * for a host route whose destination should be
1117 * treated as on-link.
1119 rt_setgate(rt, rt_key(rt),
1120 (struct sockaddr *)&null_sdl);
1121 gate = rt->rt_gateway;
1122 SDL(gate)->sdl_type = ifp->if_type;
1123 SDL(gate)->sdl_index = ifp->if_index;
1124 if (ln)
1125 ln->ln_expire = time_second;
1126 #if 1
1127 if (ln && ln->ln_expire == 0) {
1128 /* kludge for desktops */
1129 #if 0
1130 kprintf("nd6_rtequest: time.tv_sec is zero; "
1131 "treat it as 1\n");
1132 #endif
1133 ln->ln_expire = 1;
1135 #endif
1136 if ((rt->rt_flags & RTF_CLONING))
1137 break;
1140 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1141 * We don't do that here since llinfo is not ready yet.
1143 * There are also couple of other things to be discussed:
1144 * - unsolicited NA code needs improvement beforehand
1145 * - RFC2461 says we MAY send multicast unsolicited NA
1146 * (7.2.6 paragraph 4), however, it also says that we
1147 * SHOULD provide a mechanism to prevent multicast NA storm.
1148 * we don't have anything like it right now.
1149 * note that the mechanism needs a mutual agreement
1150 * between proxies, which means that we need to implement
1151 * a new protocol, or a new kludge.
1152 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1153 * we need to check ip6forwarding before sending it.
1154 * (or should we allow proxy ND configuration only for
1155 * routers? there's no mention about proxy ND from hosts)
1157 #if 0
1158 /* XXX it does not work */
1159 if (rt->rt_flags & RTF_ANNOUNCE)
1160 nd6_na_output(ifp,
1161 &SIN6(rt_key(rt))->sin6_addr,
1162 &SIN6(rt_key(rt))->sin6_addr,
1163 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1164 1, NULL);
1165 #endif
1166 /* FALLTHROUGH */
1167 case RTM_RESOLVE:
1168 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1170 * Address resolution isn't necessary for a point to
1171 * point link, so we can skip this test for a p2p link.
1173 if (gate->sa_family != AF_LINK ||
1174 gate->sa_len < sizeof(null_sdl)) {
1175 log(LOG_DEBUG,
1176 "nd6_rtrequest: bad gateway value: %s\n",
1177 if_name(ifp));
1178 break;
1180 SDL(gate)->sdl_type = ifp->if_type;
1181 SDL(gate)->sdl_index = ifp->if_index;
1183 if (ln != NULL)
1184 break; /* This happens on a route change */
1186 * Case 2: This route may come from cloning, or a manual route
1187 * add with a LL address.
1189 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1190 rt->rt_llinfo = (caddr_t)ln;
1191 if (!ln) {
1192 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1193 break;
1195 nd6_inuse++;
1196 nd6_allocated++;
1197 bzero(ln, sizeof(*ln));
1198 ln->ln_rt = rt;
1199 /* this is required for "ndp" command. - shin */
1200 if (req == RTM_ADD) {
1202 * gate should have some valid AF_LINK entry,
1203 * and ln->ln_expire should have some lifetime
1204 * which is specified by ndp command.
1206 ln->ln_state = ND6_LLINFO_REACHABLE;
1207 ln->ln_byhint = 0;
1208 } else {
1210 * When req == RTM_RESOLVE, rt is created and
1211 * initialized in rtrequest(), so rt_expire is 0.
1213 ln->ln_state = ND6_LLINFO_NOSTATE;
1214 ln->ln_expire = time_second;
1216 rt->rt_flags |= RTF_LLINFO;
1217 ln->ln_next = llinfo_nd6.ln_next;
1218 llinfo_nd6.ln_next = ln;
1219 ln->ln_prev = &llinfo_nd6;
1220 ln->ln_next->ln_prev = ln;
1223 * check if rt_key(rt) is one of my address assigned
1224 * to the interface.
1226 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1227 &SIN6(rt_key(rt))->sin6_addr);
1228 if (ifa) {
1229 caddr_t macp = nd6_ifptomac(ifp);
1230 ln->ln_expire = 0;
1231 ln->ln_state = ND6_LLINFO_REACHABLE;
1232 ln->ln_byhint = 0;
1233 if (macp) {
1234 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1235 SDL(gate)->sdl_alen = ifp->if_addrlen;
1237 if (nd6_useloopback) {
1238 rt->rt_ifp = &loif[0]; /* XXX */
1240 * Make sure rt_ifa be equal to the ifaddr
1241 * corresponding to the address.
1242 * We need this because when we refer
1243 * rt_ifa->ia6_flags in ip6_input, we assume
1244 * that the rt_ifa points to the address instead
1245 * of the loopback address.
1247 if (ifa != rt->rt_ifa) {
1248 IFAFREE(rt->rt_ifa);
1249 IFAREF(ifa);
1250 rt->rt_ifa = ifa;
1253 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1254 ln->ln_expire = 0;
1255 ln->ln_state = ND6_LLINFO_REACHABLE;
1256 ln->ln_byhint = 0;
1258 /* join solicited node multicast for proxy ND */
1259 if (ifp->if_flags & IFF_MULTICAST) {
1260 struct in6_addr llsol;
1261 int error;
1263 llsol = SIN6(rt_key(rt))->sin6_addr;
1264 llsol.s6_addr16[0] = htons(0xff02);
1265 llsol.s6_addr16[1] = htons(ifp->if_index);
1266 llsol.s6_addr32[1] = 0;
1267 llsol.s6_addr32[2] = htonl(1);
1268 llsol.s6_addr8[12] = 0xff;
1270 if (!in6_addmulti(&llsol, ifp, &error)) {
1271 nd6log((LOG_ERR, "%s: failed to join "
1272 "%s (errno=%d)\n", if_name(ifp),
1273 ip6_sprintf(&llsol), error));
1277 break;
1279 case RTM_DELETE:
1280 if (!ln)
1281 break;
1282 /* leave from solicited node multicast for proxy ND */
1283 if ((rt->rt_flags & RTF_ANNOUNCE) &&
1284 (ifp->if_flags & IFF_MULTICAST)) {
1285 struct in6_addr llsol;
1286 struct in6_multi *in6m;
1288 llsol = SIN6(rt_key(rt))->sin6_addr;
1289 llsol.s6_addr16[0] = htons(0xff02);
1290 llsol.s6_addr16[1] = htons(ifp->if_index);
1291 llsol.s6_addr32[1] = 0;
1292 llsol.s6_addr32[2] = htonl(1);
1293 llsol.s6_addr8[12] = 0xff;
1295 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1296 if (in6m)
1297 in6_delmulti(in6m);
1299 nd6_inuse--;
1300 ln->ln_next->ln_prev = ln->ln_prev;
1301 ln->ln_prev->ln_next = ln->ln_next;
1302 ln->ln_prev = NULL;
1303 rt->rt_llinfo = 0;
1304 rt->rt_flags &= ~RTF_LLINFO;
1305 if (ln->ln_hold)
1306 m_freem(ln->ln_hold);
1307 Free((caddr_t)ln);
1312 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1314 struct in6_drlist *drl = (struct in6_drlist *)data;
1315 struct in6_prlist *prl = (struct in6_prlist *)data;
1316 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1317 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1318 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1319 struct nd_defrouter *dr, any;
1320 struct nd_prefix *pr;
1321 struct rtentry *rt;
1322 int i = 0, error = 0;
1324 switch (cmd) {
1325 case SIOCGDRLST_IN6:
1327 * obsolete API, use sysctl under net.inet6.icmp6
1329 bzero(drl, sizeof(*drl));
1330 crit_enter();
1331 dr = TAILQ_FIRST(&nd_defrouter);
1332 while (dr && i < DRLSTSIZ) {
1333 drl->defrouter[i].rtaddr = dr->rtaddr;
1334 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1335 /* XXX: need to this hack for KAME stack */
1336 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1337 } else
1338 log(LOG_ERR,
1339 "default router list contains a "
1340 "non-linklocal address(%s)\n",
1341 ip6_sprintf(&drl->defrouter[i].rtaddr));
1343 drl->defrouter[i].flags = dr->flags;
1344 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1345 drl->defrouter[i].expire = dr->expire;
1346 drl->defrouter[i].if_index = dr->ifp->if_index;
1347 i++;
1348 dr = TAILQ_NEXT(dr, dr_entry);
1350 crit_exit();
1351 break;
1352 case SIOCGPRLST_IN6:
1354 * obsolete API, use sysctl under net.inet6.icmp6
1357 * XXX meaning of fields, especialy "raflags", is very
1358 * differnet between RA prefix list and RR/static prefix list.
1359 * how about separating ioctls into two?
1361 bzero(prl, sizeof(*prl));
1362 crit_enter();
1363 pr = nd_prefix.lh_first;
1364 while (pr && i < PRLSTSIZ) {
1365 struct nd_pfxrouter *pfr;
1366 int j;
1368 in6_embedscope(&prl->prefix[i].prefix,
1369 &pr->ndpr_prefix, NULL, NULL);
1370 prl->prefix[i].raflags = pr->ndpr_raf;
1371 prl->prefix[i].prefixlen = pr->ndpr_plen;
1372 prl->prefix[i].vltime = pr->ndpr_vltime;
1373 prl->prefix[i].pltime = pr->ndpr_pltime;
1374 prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1375 prl->prefix[i].expire = pr->ndpr_expire;
1377 pfr = pr->ndpr_advrtrs.lh_first;
1378 j = 0;
1379 while (pfr) {
1380 if (j < DRLSTSIZ) {
1381 #define RTRADDR prl->prefix[i].advrtr[j]
1382 RTRADDR = pfr->router->rtaddr;
1383 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1384 /* XXX: hack for KAME */
1385 RTRADDR.s6_addr16[1] = 0;
1386 } else
1387 log(LOG_ERR,
1388 "a router(%s) advertises "
1389 "a prefix with "
1390 "non-link local address\n",
1391 ip6_sprintf(&RTRADDR));
1392 #undef RTRADDR
1394 j++;
1395 pfr = pfr->pfr_next;
1397 prl->prefix[i].advrtrs = j;
1398 prl->prefix[i].origin = PR_ORIG_RA;
1400 i++;
1401 pr = pr->ndpr_next;
1404 struct rr_prefix *rpp;
1406 for (rpp = LIST_FIRST(&rr_prefix); rpp;
1407 rpp = LIST_NEXT(rpp, rp_entry)) {
1408 if (i >= PRLSTSIZ)
1409 break;
1410 in6_embedscope(&prl->prefix[i].prefix,
1411 &pr->ndpr_prefix, NULL, NULL);
1412 prl->prefix[i].raflags = rpp->rp_raf;
1413 prl->prefix[i].prefixlen = rpp->rp_plen;
1414 prl->prefix[i].vltime = rpp->rp_vltime;
1415 prl->prefix[i].pltime = rpp->rp_pltime;
1416 prl->prefix[i].if_index = rpp->rp_ifp->if_index;
1417 prl->prefix[i].expire = rpp->rp_expire;
1418 prl->prefix[i].advrtrs = 0;
1419 prl->prefix[i].origin = rpp->rp_origin;
1420 i++;
1423 crit_exit();
1425 break;
1426 case OSIOCGIFINFO_IN6:
1427 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1428 bzero(&ndi->ndi, sizeof(ndi->ndi));
1429 ndi->ndi.linkmtu = ND_IFINFO(ifp)->linkmtu;
1430 ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1431 ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1432 ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1433 ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1434 ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1435 ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1436 ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1437 ndi->ndi.receivedra = ND_IFINFO(ifp)->receivedra;
1438 break;
1439 case SIOCGIFINFO_IN6:
1440 ndi->ndi = *ND_IFINFO(ifp);
1441 break;
1442 case SIOCSIFINFO_FLAGS:
1443 ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1444 break;
1445 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1446 /* flush default router list */
1448 * xxx sumikawa: should not delete route if default
1449 * route equals to the top of default router list
1451 bzero(&any, sizeof(any));
1452 defrouter_delreq(&any, 0);
1453 defrouter_select();
1454 /* xxx sumikawa: flush prefix list */
1455 break;
1456 case SIOCSPFXFLUSH_IN6:
1458 /* flush all the prefix advertised by routers */
1459 struct nd_prefix *pr, *next;
1461 crit_enter();
1462 for (pr = nd_prefix.lh_first; pr; pr = next) {
1463 struct in6_ifaddr *ia, *ia_next;
1465 next = pr->ndpr_next;
1467 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1468 continue; /* XXX */
1470 /* do we really have to remove addresses as well? */
1471 for (ia = in6_ifaddr; ia; ia = ia_next) {
1472 /* ia might be removed. keep the next ptr. */
1473 ia_next = ia->ia_next;
1475 if (!(ia->ia6_flags & IN6_IFF_AUTOCONF))
1476 continue;
1478 if (ia->ia6_ndpr == pr)
1479 in6_purgeaddr(&ia->ia_ifa);
1481 prelist_remove(pr);
1483 crit_exit();
1484 break;
1486 case SIOCSRTRFLUSH_IN6:
1488 /* flush all the default routers */
1489 struct nd_defrouter *dr, *next;
1491 crit_enter();
1492 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1494 * The first entry of the list may be stored in
1495 * the routing table, so we'll delete it later.
1497 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1498 next = TAILQ_NEXT(dr, dr_entry);
1499 defrtrlist_del(dr);
1501 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1503 crit_exit();
1504 break;
1506 case SIOCGNBRINFO_IN6:
1508 struct llinfo_nd6 *ln;
1509 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1512 * XXX: KAME specific hack for scoped addresses
1513 * XXXX: for other scopes than link-local?
1515 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1516 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1517 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1519 if (*idp == 0)
1520 *idp = htons(ifp->if_index);
1523 crit_enter();
1524 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1525 error = EINVAL;
1526 crit_exit();
1527 break;
1529 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1530 nbi->state = ln->ln_state;
1531 nbi->asked = ln->ln_asked;
1532 nbi->isrouter = ln->ln_router;
1533 nbi->expire = ln->ln_expire;
1534 crit_exit();
1536 break;
1538 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1539 ndif->ifindex = nd6_defifindex;
1540 break;
1541 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1542 return (nd6_setdefaultiface(ndif->ifindex));
1543 break;
1545 return (error);
1549 * Create neighbor cache entry and cache link-layer address,
1550 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1552 struct rtentry *
1553 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1554 int lladdrlen,
1555 int type, /* ICMP6 type */
1556 int code /* type dependent information */)
1558 struct rtentry *rt = NULL;
1559 struct llinfo_nd6 *ln = NULL;
1560 int is_newentry;
1561 struct sockaddr_dl *sdl = NULL;
1562 int do_update;
1563 int olladdr;
1564 int llchange;
1565 int newstate = 0;
1567 if (!ifp)
1568 panic("ifp == NULL in nd6_cache_lladdr");
1569 if (!from)
1570 panic("from == NULL in nd6_cache_lladdr");
1572 /* nothing must be updated for unspecified address */
1573 if (IN6_IS_ADDR_UNSPECIFIED(from))
1574 return NULL;
1577 * Validation about ifp->if_addrlen and lladdrlen must be done in
1578 * the caller.
1580 * XXX If the link does not have link-layer adderss, what should
1581 * we do? (ifp->if_addrlen == 0)
1582 * Spec says nothing in sections for RA, RS and NA. There's small
1583 * description on it in NS section (RFC 2461 7.2.3).
1586 rt = nd6_lookup(from, 0, ifp);
1587 if (!rt) {
1588 #if 0
1589 /* nothing must be done if there's no lladdr */
1590 if (!lladdr || !lladdrlen)
1591 return NULL;
1592 #endif
1594 rt = nd6_lookup(from, 1, ifp);
1595 is_newentry = 1;
1596 } else {
1597 /* do nothing if static ndp is set */
1598 if (rt->rt_flags & RTF_STATIC)
1599 return NULL;
1600 is_newentry = 0;
1603 if (!rt)
1604 return NULL;
1605 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1606 fail:
1607 nd6_free(rt);
1608 return NULL;
1610 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1611 if (!ln)
1612 goto fail;
1613 if (!rt->rt_gateway)
1614 goto fail;
1615 if (rt->rt_gateway->sa_family != AF_LINK)
1616 goto fail;
1617 sdl = SDL(rt->rt_gateway);
1619 olladdr = (sdl->sdl_alen) ? 1 : 0;
1620 if (olladdr && lladdr) {
1621 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1622 llchange = 1;
1623 else
1624 llchange = 0;
1625 } else
1626 llchange = 0;
1629 * newentry olladdr lladdr llchange (*=record)
1630 * 0 n n -- (1)
1631 * 0 y n -- (2)
1632 * 0 n y -- (3) * STALE
1633 * 0 y y n (4) *
1634 * 0 y y y (5) * STALE
1635 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1636 * 1 -- y -- (7) * STALE
1639 if (lladdr) { /* (3-5) and (7) */
1641 * Record source link-layer address
1642 * XXX is it dependent to ifp->if_type?
1644 sdl->sdl_alen = ifp->if_addrlen;
1645 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1648 if (!is_newentry) {
1649 if ((!olladdr && lladdr) /* (3) */
1650 || (olladdr && lladdr && llchange)) { /* (5) */
1651 do_update = 1;
1652 newstate = ND6_LLINFO_STALE;
1653 } else /* (1-2,4) */
1654 do_update = 0;
1655 } else {
1656 do_update = 1;
1657 if (!lladdr) /* (6) */
1658 newstate = ND6_LLINFO_NOSTATE;
1659 else /* (7) */
1660 newstate = ND6_LLINFO_STALE;
1663 if (do_update) {
1665 * Update the state of the neighbor cache.
1667 ln->ln_state = newstate;
1669 if (ln->ln_state == ND6_LLINFO_STALE) {
1671 * XXX: since nd6_output() below will cause
1672 * state tansition to DELAY and reset the timer,
1673 * we must set the timer now, although it is actually
1674 * meaningless.
1676 ln->ln_expire = time_second + nd6_gctimer;
1678 if (ln->ln_hold) {
1680 * we assume ifp is not a p2p here, so just
1681 * set the 2nd argument as the 1st one.
1683 nd6_output(ifp, ifp, ln->ln_hold,
1684 (struct sockaddr_in6 *)rt_key(rt),
1685 rt);
1686 ln->ln_hold = NULL;
1688 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1689 /* probe right away */
1690 ln->ln_expire = time_second;
1695 * ICMP6 type dependent behavior.
1697 * NS: clear IsRouter if new entry
1698 * RS: clear IsRouter
1699 * RA: set IsRouter if there's lladdr
1700 * redir: clear IsRouter if new entry
1702 * RA case, (1):
1703 * The spec says that we must set IsRouter in the following cases:
1704 * - If lladdr exist, set IsRouter. This means (1-5).
1705 * - If it is old entry (!newentry), set IsRouter. This means (7).
1706 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1707 * A quetion arises for (1) case. (1) case has no lladdr in the
1708 * neighbor cache, this is similar to (6).
1709 * This case is rare but we figured that we MUST NOT set IsRouter.
1711 * newentry olladdr lladdr llchange NS RS RA redir
1712 * D R
1713 * 0 n n -- (1) c ? s
1714 * 0 y n -- (2) c s s
1715 * 0 n y -- (3) c s s
1716 * 0 y y n (4) c s s
1717 * 0 y y y (5) c s s
1718 * 1 -- n -- (6) c c c s
1719 * 1 -- y -- (7) c c s c s
1721 * (c=clear s=set)
1723 switch (type & 0xff) {
1724 case ND_NEIGHBOR_SOLICIT:
1726 * New entry must have is_router flag cleared.
1728 if (is_newentry) /* (6-7) */
1729 ln->ln_router = 0;
1730 break;
1731 case ND_REDIRECT:
1733 * If the icmp is a redirect to a better router, always set the
1734 * is_router flag. Otherwise, if the entry is newly created,
1735 * clear the flag. [RFC 2461, sec 8.3]
1737 if (code == ND_REDIRECT_ROUTER)
1738 ln->ln_router = 1;
1739 else if (is_newentry) /* (6-7) */
1740 ln->ln_router = 0;
1741 break;
1742 case ND_ROUTER_SOLICIT:
1744 * is_router flag must always be cleared.
1746 ln->ln_router = 0;
1747 break;
1748 case ND_ROUTER_ADVERT:
1750 * Mark an entry with lladdr as a router.
1752 if ((!is_newentry && (olladdr || lladdr)) /* (2-5) */
1753 || (is_newentry && lladdr)) { /* (7) */
1754 ln->ln_router = 1;
1756 break;
1760 * When the link-layer address of a router changes, select the
1761 * best router again. In particular, when the neighbor entry is newly
1762 * created, it might affect the selection policy.
1763 * Question: can we restrict the first condition to the "is_newentry"
1764 * case?
1765 * XXX: when we hear an RA from a new router with the link-layer
1766 * address option, defrouter_select() is called twice, since
1767 * defrtrlist_update called the function as well. However, I believe
1768 * we can compromise the overhead, since it only happens the first
1769 * time.
1770 * XXX: although defrouter_select() should not have a bad effect
1771 * for those are not autoconfigured hosts, we explicitly avoid such
1772 * cases for safety.
1774 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1775 defrouter_select();
1777 return rt;
1780 static void
1781 nd6_slowtimo(void *ignored_arg)
1783 struct nd_ifinfo *nd6if;
1784 struct ifnet *ifp;
1786 crit_enter();
1787 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1788 nd6_slowtimo, NULL);
1789 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1790 nd6if = ND_IFINFO(ifp);
1791 if (nd6if->basereachable && /* already initialized */
1792 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1794 * Since reachable time rarely changes by router
1795 * advertisements, we SHOULD insure that a new random
1796 * value gets recomputed at least once every few hours.
1797 * (RFC 2461, 6.3.4)
1799 nd6if->recalctm = nd6_recalc_reachtm_interval;
1800 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1803 crit_exit();
1806 #define gotoerr(e) { error = (e); goto bad;}
1809 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1810 struct sockaddr_in6 *dst, struct rtentry *rt)
1812 struct llinfo_nd6 *ln = NULL;
1813 int error = 0;
1815 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1816 goto sendpkt;
1818 if (nd6_need_cache(ifp) == 0)
1819 goto sendpkt;
1822 * next hop determination. This routine is derived from ether_outpout.
1824 if (rt != NULL) {
1825 if (!(rt->rt_flags & RTF_UP)) {
1826 rt = rtlookup((struct sockaddr *)dst);
1827 if (rt == NULL)
1828 gotoerr(EHOSTUNREACH);
1829 rt->rt_refcnt--;
1830 if (rt->rt_ifp != ifp) {
1831 /* XXX: loop care? */
1832 return nd6_output(ifp, origifp, m, dst, rt);
1835 if (rt->rt_flags & RTF_GATEWAY) {
1836 struct sockaddr_in6 *gw6;
1839 * We skip link-layer address resolution and NUD
1840 * if the gateway is not a neighbor from ND point
1841 * of view, regardless of the value of nd_ifinfo.flags.
1842 * The second condition is a bit tricky; we skip
1843 * if the gateway is our own address, which is
1844 * sometimes used to install a route to a p2p link.
1846 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1847 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1848 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1850 * We allow this kind of tricky route only
1851 * when the outgoing interface is p2p.
1852 * XXX: we may need a more generic rule here.
1854 if (!(ifp->if_flags & IFF_POINTOPOINT))
1855 gotoerr(EHOSTUNREACH);
1857 goto sendpkt;
1860 if (rt->rt_gwroute == NULL) {
1861 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1862 if (rt->rt_gwroute == NULL)
1863 gotoerr(EHOSTUNREACH);
1864 } else if (!(rt->rt_gwroute->rt_flags & RTF_UP)) {
1865 rtfree(rt->rt_gwroute);
1866 rt->rt_gwroute = rtlookup(rt->rt_gateway);
1867 if (rt->rt_gwroute == NULL)
1868 gotoerr(EHOSTUNREACH);
1874 * Address resolution or Neighbor Unreachability Detection
1875 * for the next hop.
1876 * At this point, the destination of the packet must be a unicast
1877 * or an anycast address(i.e. not a multicast).
1880 /* Look up the neighbor cache for the nexthop */
1881 if (rt && (rt->rt_flags & RTF_LLINFO))
1882 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1883 else {
1885 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1886 * the condition below is not very efficient. But we believe
1887 * it is tolerable, because this should be a rare case.
1889 if (nd6_is_addr_neighbor(dst, ifp) &&
1890 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1891 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1893 if (!ln || !rt) {
1894 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
1895 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1896 log(LOG_DEBUG,
1897 "nd6_output: can't allocate llinfo for %s "
1898 "(ln=%p, rt=%p)\n",
1899 ip6_sprintf(&dst->sin6_addr), ln, rt);
1900 gotoerr(EIO); /* XXX: good error? */
1903 goto sendpkt; /* send anyway */
1906 /* We don't have to do link-layer address resolution on a p2p link. */
1907 if ((ifp->if_flags & IFF_POINTOPOINT) &&
1908 ln->ln_state < ND6_LLINFO_REACHABLE) {
1909 ln->ln_state = ND6_LLINFO_STALE;
1910 ln->ln_expire = time_second + nd6_gctimer;
1914 * The first time we send a packet to a neighbor whose entry is
1915 * STALE, we have to change the state to DELAY and a sets a timer to
1916 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1917 * neighbor unreachability detection on expiration.
1918 * (RFC 2461 7.3.3)
1920 if (ln->ln_state == ND6_LLINFO_STALE) {
1921 ln->ln_asked = 0;
1922 ln->ln_state = ND6_LLINFO_DELAY;
1923 ln->ln_expire = time_second + nd6_delay;
1927 * If the neighbor cache entry has a state other than INCOMPLETE
1928 * (i.e. its link-layer address is already resolved), just
1929 * send the packet.
1931 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1932 goto sendpkt;
1935 * There is a neighbor cache entry, but no ethernet address
1936 * response yet. Replace the held mbuf (if any) with this
1937 * latest one.
1939 * This code conforms to the rate-limiting rule described in Section
1940 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
1941 * an NS below.
1943 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1944 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1945 if (ln->ln_hold)
1946 m_freem(ln->ln_hold);
1947 ln->ln_hold = m;
1948 if (ln->ln_expire) {
1949 if (ln->ln_asked < nd6_mmaxtries &&
1950 ln->ln_expire < time_second) {
1951 ln->ln_asked++;
1952 ln->ln_expire = time_second +
1953 ND_IFINFO(ifp)->retrans / 1000;
1954 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1957 return (0);
1959 sendpkt:
1960 lwkt_serialize_enter(ifp->if_serializer);
1961 if (ifp->if_flags & IFF_LOOPBACK) {
1962 error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
1963 rt);
1964 } else {
1965 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt);
1967 lwkt_serialize_exit(ifp->if_serializer);
1968 return (error);
1970 bad:
1971 m_freem(m);
1972 return (error);
1974 #undef gotoerr
1977 nd6_need_cache(struct ifnet *ifp)
1980 * XXX: we currently do not make neighbor cache on any interface
1981 * other than ARCnet, Ethernet, FDDI and GIF.
1983 * RFC2893 says:
1984 * - unidirectional tunnels needs no ND
1986 switch (ifp->if_type) {
1987 case IFT_ARCNET:
1988 case IFT_ETHER:
1989 case IFT_FDDI:
1990 case IFT_IEEE1394:
1991 #ifdef IFT_L2VLAN
1992 case IFT_L2VLAN:
1993 #endif
1994 #ifdef IFT_IEEE80211
1995 case IFT_IEEE80211:
1996 #endif
1997 case IFT_GIF: /* XXX need more cases? */
1998 return (1);
1999 default:
2000 return (0);
2005 nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
2006 struct sockaddr *dst, u_char *desten)
2008 struct sockaddr_dl *sdl;
2009 struct rtentry *rt;
2012 if (m->m_flags & M_MCAST) {
2013 switch (ifp->if_type) {
2014 case IFT_ETHER:
2015 case IFT_FDDI:
2016 #ifdef IFT_L2VLAN
2017 case IFT_L2VLAN:
2018 #endif
2019 #ifdef IFT_IEEE80211
2020 case IFT_IEEE80211:
2021 #endif
2022 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2023 desten);
2024 return (1);
2025 case IFT_IEEE1394:
2026 bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
2027 return (1);
2028 case IFT_ARCNET:
2029 *desten = 0;
2030 return (1);
2031 default:
2032 m_freem(m);
2033 return (0);
2036 if (rt0 == NULL) {
2037 /* this could happen, if we could not allocate memory */
2038 m_freem(m);
2039 return (0);
2041 if (rt_llroute(dst, rt0, &rt) != 0) {
2042 m_freem(m);
2043 return (0);
2045 if (rt->rt_gateway->sa_family != AF_LINK) {
2046 kprintf("nd6_storelladdr: something odd happens\n");
2047 m_freem(m);
2048 return (0);
2050 sdl = SDL(rt->rt_gateway);
2051 if (sdl->sdl_alen == 0) {
2052 /* this should be impossible, but we bark here for debugging */
2053 kprintf("nd6_storelladdr: sdl_alen == 0\n");
2054 m_freem(m);
2055 return (0);
2058 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2059 return (1);
2062 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2063 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2064 #ifdef SYSCTL_DECL
2065 SYSCTL_DECL(_net_inet6_icmp6);
2066 #endif
2067 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2068 CTLFLAG_RD, nd6_sysctl_drlist, "");
2069 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2070 CTLFLAG_RD, nd6_sysctl_prlist, "");
2072 static int
2073 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2075 int error;
2076 char buf[1024];
2077 struct in6_defrouter *d, *de;
2078 struct nd_defrouter *dr;
2080 if (req->newptr)
2081 return EPERM;
2082 error = 0;
2084 for (dr = TAILQ_FIRST(&nd_defrouter);
2086 dr = TAILQ_NEXT(dr, dr_entry)) {
2087 d = (struct in6_defrouter *)buf;
2088 de = (struct in6_defrouter *)(buf + sizeof(buf));
2090 if (d + 1 <= de) {
2091 bzero(d, sizeof(*d));
2092 d->rtaddr.sin6_family = AF_INET6;
2093 d->rtaddr.sin6_len = sizeof(d->rtaddr);
2094 if (in6_recoverscope(&d->rtaddr, &dr->rtaddr,
2095 dr->ifp) != 0)
2096 log(LOG_ERR,
2097 "scope error in "
2098 "default router list (%s)\n",
2099 ip6_sprintf(&dr->rtaddr));
2100 d->flags = dr->flags;
2101 d->rtlifetime = dr->rtlifetime;
2102 d->expire = dr->expire;
2103 d->if_index = dr->ifp->if_index;
2104 } else
2105 panic("buffer too short");
2107 error = SYSCTL_OUT(req, buf, sizeof(*d));
2108 if (error)
2109 break;
2111 return error;
2114 static int
2115 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2117 int error;
2118 char buf[1024];
2119 struct in6_prefix *p, *pe;
2120 struct nd_prefix *pr;
2122 if (req->newptr)
2123 return EPERM;
2124 error = 0;
2126 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2127 u_short advrtrs;
2128 size_t advance;
2129 struct sockaddr_in6 *sin6, *s6;
2130 struct nd_pfxrouter *pfr;
2132 p = (struct in6_prefix *)buf;
2133 pe = (struct in6_prefix *)(buf + sizeof(buf));
2135 if (p + 1 <= pe) {
2136 bzero(p, sizeof(*p));
2137 sin6 = (struct sockaddr_in6 *)(p + 1);
2139 p->prefix = pr->ndpr_prefix;
2140 if (in6_recoverscope(&p->prefix,
2141 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2142 log(LOG_ERR,
2143 "scope error in prefix list (%s)\n",
2144 ip6_sprintf(&p->prefix.sin6_addr));
2145 p->raflags = pr->ndpr_raf;
2146 p->prefixlen = pr->ndpr_plen;
2147 p->vltime = pr->ndpr_vltime;
2148 p->pltime = pr->ndpr_pltime;
2149 p->if_index = pr->ndpr_ifp->if_index;
2150 p->expire = pr->ndpr_expire;
2151 p->refcnt = pr->ndpr_refcnt;
2152 p->flags = pr->ndpr_stateflags;
2153 p->origin = PR_ORIG_RA;
2154 advrtrs = 0;
2155 for (pfr = pr->ndpr_advrtrs.lh_first;
2156 pfr;
2157 pfr = pfr->pfr_next) {
2158 if ((void *)&sin6[advrtrs + 1] >
2159 (void *)pe) {
2160 advrtrs++;
2161 continue;
2163 s6 = &sin6[advrtrs];
2164 bzero(s6, sizeof(*s6));
2165 s6->sin6_family = AF_INET6;
2166 s6->sin6_len = sizeof(*sin6);
2167 if (in6_recoverscope(s6, &pfr->router->rtaddr,
2168 pfr->router->ifp) != 0)
2169 log(LOG_ERR,
2170 "scope error in "
2171 "prefix list (%s)\n",
2172 ip6_sprintf(&pfr->router->rtaddr));
2173 advrtrs++;
2175 p->advrtrs = advrtrs;
2176 } else
2177 panic("buffer too short");
2179 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2180 error = SYSCTL_OUT(req, buf, advance);
2181 if (error)
2182 break;
2184 return error;