nd6.c revision 1.69 1 /* $NetBSD: nd6.c,v 1.69 2002/08/19 23:14:39 itojun Exp $ */
2 /* $KAME: nd6.c,v 1.279 2002/06/08 11:16:51 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: nd6.c,v 1.69 2002/08/19 23:14:39 itojun Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/callout.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/protosw.h>
46 #include <sys/errno.h>
47 #include <sys/ioctl.h>
48 #include <sys/syslog.h>
49 #include <sys/queue.h>
50
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/route.h>
55 #include <net/if_ether.h>
56 #include <net/if_fddi.h>
57 #include <net/if_arc.h>
58
59 #include <netinet/in.h>
60 #include <netinet6/in6_var.h>
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet6/nd6.h>
64 #include <netinet/icmp6.h>
65
66 #include "loop.h"
67 extern struct ifnet loif[NLOOP];
68
69 #include <net/net_osdep.h>
70
71 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
72 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
73
74 #define SIN6(s) ((struct sockaddr_in6 *)s)
75 #define SDL(s) ((struct sockaddr_dl *)s)
76
77 /* timer values */
78 int nd6_prune = 1; /* walk list every 1 seconds */
79 int nd6_delay = 5; /* delay first probe time 5 second */
80 int nd6_umaxtries = 3; /* maximum unicast query */
81 int nd6_mmaxtries = 3; /* maximum multicast query */
82 int nd6_useloopback = 1; /* use loopback interface for local traffic */
83 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
84
85 /* preventing too many loops in ND option parsing */
86 int nd6_maxndopt = 10; /* max # of ND options allowed */
87
88 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
89
90 #ifdef ND6_DEBUG
91 int nd6_debug = 1;
92 #else
93 int nd6_debug = 0;
94 #endif
95
96 /* for debugging? */
97 static int nd6_inuse, nd6_allocated;
98
99 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
100 struct nd_drhead nd_defrouter;
101 struct nd_prhead nd_prefix = { 0 };
102
103 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
104 static struct sockaddr_in6 all1_sa;
105
106 static void nd6_setmtu0 __P((struct ifnet *, struct nd_ifinfo *));
107 static void nd6_slowtimo __P((void *));
108 static struct llinfo_nd6 *nd6_free __P((struct rtentry *, int));
109
110 struct callout nd6_slowtimo_ch;
111 struct callout nd6_timer_ch;
112
113 static int fill_drlist __P((void *, size_t *, size_t));
114 static int fill_prlist __P((void *, size_t *, size_t));
115
116 void
117 nd6_init()
118 {
119 static int nd6_init_done = 0;
120 int i;
121
122 if (nd6_init_done) {
123 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
124 return;
125 }
126
127 all1_sa.sin6_family = AF_INET6;
128 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
129 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
130 all1_sa.sin6_addr.s6_addr[i] = 0xff;
131
132 /* initialization of the default router list */
133 TAILQ_INIT(&nd_defrouter);
134
135 nd6_init_done = 1;
136
137 /* start timer */
138 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
139 nd6_slowtimo, NULL);
140 }
141
142 struct nd_ifinfo *
143 nd6_ifattach(ifp)
144 struct ifnet *ifp;
145 {
146 struct nd_ifinfo *nd;
147
148 nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
149 bzero(nd, sizeof(*nd));
150
151 nd->initialized = 1;
152
153 nd->chlim = IPV6_DEFHLIM;
154 nd->basereachable = REACHABLE_TIME;
155 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
156 nd->retrans = RETRANS_TIMER;
157 /*
158 * Note that the default value of ip6_accept_rtadv is 0, which means
159 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
160 * here.
161 */
162 nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
163
164 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
165 nd6_setmtu0(ifp, nd);
166
167 return nd;
168 }
169
170 void
171 nd6_ifdetach(nd)
172 struct nd_ifinfo *nd;
173 {
174
175 free(nd, M_IP6NDP);
176 }
177
178 void
179 nd6_setmtu(ifp)
180 struct ifnet *ifp;
181 {
182 nd6_setmtu0(ifp, ND_IFINFO(ifp));
183 }
184
185 void
186 nd6_setmtu0(ifp, ndi)
187 struct ifnet *ifp;
188 struct nd_ifinfo *ndi;
189 {
190 u_int32_t omaxmtu;
191
192 omaxmtu = ndi->maxmtu;
193
194 switch (ifp->if_type) {
195 case IFT_ARCNET:
196 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
197 break;
198 case IFT_FDDI:
199 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
200 break;
201 default:
202 ndi->maxmtu = ifp->if_mtu;
203 break;
204 }
205
206 /*
207 * Decreasing the interface MTU under IPV6 minimum MTU may cause
208 * undesirable situation. We thus notify the operator of the change
209 * explicitly. The check for omaxmtu is necessary to restrict the
210 * log to the case of changing the MTU, not initializing it.
211 */
212 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
213 log(LOG_NOTICE, "nd6_setmtu0: "
214 "new link MTU on %s (%lu) is too small for IPv6\n",
215 if_name(ifp), (unsigned long)ndi->maxmtu);
216 }
217
218 if (ndi->maxmtu > in6_maxmtu)
219 in6_setmaxmtu(); /* check all interfaces just in case */
220 }
221
222 void
223 nd6_option_init(opt, icmp6len, ndopts)
224 void *opt;
225 int icmp6len;
226 union nd_opts *ndopts;
227 {
228
229 bzero(ndopts, sizeof(*ndopts));
230 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
231 ndopts->nd_opts_last
232 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
233
234 if (icmp6len == 0) {
235 ndopts->nd_opts_done = 1;
236 ndopts->nd_opts_search = NULL;
237 }
238 }
239
240 /*
241 * Take one ND option.
242 */
243 struct nd_opt_hdr *
244 nd6_option(ndopts)
245 union nd_opts *ndopts;
246 {
247 struct nd_opt_hdr *nd_opt;
248 int olen;
249
250 if (!ndopts)
251 panic("ndopts == NULL in nd6_option\n");
252 if (!ndopts->nd_opts_last)
253 panic("uninitialized ndopts in nd6_option\n");
254 if (!ndopts->nd_opts_search)
255 return NULL;
256 if (ndopts->nd_opts_done)
257 return NULL;
258
259 nd_opt = ndopts->nd_opts_search;
260
261 /* make sure nd_opt_len is inside the buffer */
262 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
263 bzero(ndopts, sizeof(*ndopts));
264 return NULL;
265 }
266
267 olen = nd_opt->nd_opt_len << 3;
268 if (olen == 0) {
269 /*
270 * Message validation requires that all included
271 * options have a length that is greater than zero.
272 */
273 bzero(ndopts, sizeof(*ndopts));
274 return NULL;
275 }
276
277 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
278 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
279 /* option overruns the end of buffer, invalid */
280 bzero(ndopts, sizeof(*ndopts));
281 return NULL;
282 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
283 /* reached the end of options chain */
284 ndopts->nd_opts_done = 1;
285 ndopts->nd_opts_search = NULL;
286 }
287 return nd_opt;
288 }
289
290 /*
291 * Parse multiple ND options.
292 * This function is much easier to use, for ND routines that do not need
293 * multiple options of the same type.
294 */
295 int
296 nd6_options(ndopts)
297 union nd_opts *ndopts;
298 {
299 struct nd_opt_hdr *nd_opt;
300 int i = 0;
301
302 if (!ndopts)
303 panic("ndopts == NULL in nd6_options\n");
304 if (!ndopts->nd_opts_last)
305 panic("uninitialized ndopts in nd6_options\n");
306 if (!ndopts->nd_opts_search)
307 return 0;
308
309 while (1) {
310 nd_opt = nd6_option(ndopts);
311 if (!nd_opt && !ndopts->nd_opts_last) {
312 /*
313 * Message validation requires that all included
314 * options have a length that is greater than zero.
315 */
316 icmp6stat.icp6s_nd_badopt++;
317 bzero(ndopts, sizeof(*ndopts));
318 return -1;
319 }
320
321 if (!nd_opt)
322 goto skip1;
323
324 switch (nd_opt->nd_opt_type) {
325 case ND_OPT_SOURCE_LINKADDR:
326 case ND_OPT_TARGET_LINKADDR:
327 case ND_OPT_MTU:
328 case ND_OPT_REDIRECTED_HEADER:
329 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
330 nd6log((LOG_INFO,
331 "duplicated ND6 option found (type=%d)\n",
332 nd_opt->nd_opt_type));
333 /* XXX bark? */
334 } else {
335 ndopts->nd_opt_array[nd_opt->nd_opt_type]
336 = nd_opt;
337 }
338 break;
339 case ND_OPT_PREFIX_INFORMATION:
340 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
341 ndopts->nd_opt_array[nd_opt->nd_opt_type]
342 = nd_opt;
343 }
344 ndopts->nd_opts_pi_end =
345 (struct nd_opt_prefix_info *)nd_opt;
346 break;
347 default:
348 /*
349 * Unknown options must be silently ignored,
350 * to accomodate future extension to the protocol.
351 */
352 nd6log((LOG_DEBUG,
353 "nd6_options: unsupported option %d - "
354 "option ignored\n", nd_opt->nd_opt_type));
355 }
356
357 skip1:
358 i++;
359 if (i > nd6_maxndopt) {
360 icmp6stat.icp6s_nd_toomanyopt++;
361 nd6log((LOG_INFO, "too many loop in nd opt\n"));
362 break;
363 }
364
365 if (ndopts->nd_opts_done)
366 break;
367 }
368
369 return 0;
370 }
371
372 /*
373 * ND6 timer routine to expire default route list and prefix list
374 */
375 void
376 nd6_timer(ignored_arg)
377 void *ignored_arg;
378 {
379 int s;
380 struct llinfo_nd6 *ln;
381 struct nd_defrouter *dr;
382 struct nd_prefix *pr;
383 long time_second = time.tv_sec;
384 struct ifnet *ifp;
385 struct in6_ifaddr *ia6, *nia6;
386 struct in6_addrlifetime *lt6;
387
388 s = splsoftnet();
389 callout_reset(&nd6_timer_ch, nd6_prune * hz,
390 nd6_timer, NULL);
391
392 ln = llinfo_nd6.ln_next;
393 while (ln && ln != &llinfo_nd6) {
394 struct rtentry *rt;
395 struct sockaddr_in6 *dst;
396 struct llinfo_nd6 *next = ln->ln_next;
397 /* XXX: used for the DELAY case only: */
398 struct nd_ifinfo *ndi = NULL;
399
400 if ((rt = ln->ln_rt) == NULL) {
401 ln = next;
402 continue;
403 }
404 if ((ifp = rt->rt_ifp) == NULL) {
405 ln = next;
406 continue;
407 }
408 ndi = ND_IFINFO(ifp);
409 dst = (struct sockaddr_in6 *)rt_key(rt);
410
411 if (ln->ln_expire > time_second) {
412 ln = next;
413 continue;
414 }
415
416 /* sanity check */
417 if (!rt)
418 panic("rt=0 in nd6_timer(ln=%p)\n", ln);
419 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
420 panic("rt_llinfo(%p) is not equal to ln(%p)\n",
421 rt->rt_llinfo, ln);
422 if (!dst)
423 panic("dst=0 in nd6_timer(ln=%p)\n", ln);
424
425 switch (ln->ln_state) {
426 case ND6_LLINFO_INCOMPLETE:
427 if (ln->ln_asked < nd6_mmaxtries) {
428 ln->ln_asked++;
429 ln->ln_expire = time_second +
430 ND6_RETRANS_SEC(ND_IFINFO(ifp)->retrans);
431 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
432 ln, 0);
433 } else {
434 struct mbuf *m = ln->ln_hold;
435 if (m) {
436 /*
437 * Fake rcvif to make the ICMP error
438 * more helpful in diagnosing for the
439 * receiver.
440 * XXX: should we consider
441 * older rcvif?
442 */
443 m->m_pkthdr.rcvif = rt->rt_ifp;
444
445 icmp6_error(m, ICMP6_DST_UNREACH,
446 ICMP6_DST_UNREACH_ADDR, 0);
447 ln->ln_hold = NULL;
448 }
449 next = nd6_free(rt, 0);
450 }
451 break;
452 case ND6_LLINFO_REACHABLE:
453 if (ln->ln_expire) {
454 ln->ln_state = ND6_LLINFO_STALE;
455 ln->ln_expire = time_second + nd6_gctimer;
456 }
457 break;
458
459 case ND6_LLINFO_STALE:
460 /* Garbage Collection(RFC 2461 5.3) */
461 if (ln->ln_expire)
462 next = nd6_free(rt, 1);
463 break;
464
465 case ND6_LLINFO_DELAY:
466 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
467 /* We need NUD */
468 ln->ln_asked = 1;
469 ln->ln_state = ND6_LLINFO_PROBE;
470 ln->ln_expire = time_second +
471 ND6_RETRANS_SEC(ndi->retrans);
472 nd6_ns_output(ifp, &dst->sin6_addr,
473 &dst->sin6_addr, ln, 0);
474 } else {
475 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
476 ln->ln_expire = time_second + nd6_gctimer;
477 }
478 break;
479 case ND6_LLINFO_PROBE:
480 if (ln->ln_asked < nd6_umaxtries) {
481 ln->ln_asked++;
482 ln->ln_expire = time_second +
483 ND6_RETRANS_SEC(ND_IFINFO(ifp)->retrans);
484 nd6_ns_output(ifp, &dst->sin6_addr,
485 &dst->sin6_addr, ln, 0);
486 } else {
487 next = nd6_free(rt, 0);
488 }
489 break;
490 }
491 ln = next;
492 }
493
494 /* expire default router list */
495 dr = TAILQ_FIRST(&nd_defrouter);
496 while (dr) {
497 if (dr->expire && dr->expire < time_second) {
498 struct nd_defrouter *t;
499 t = TAILQ_NEXT(dr, dr_entry);
500 defrtrlist_del(dr);
501 dr = t;
502 } else {
503 dr = TAILQ_NEXT(dr, dr_entry);
504 }
505 }
506
507 /*
508 * expire interface addresses.
509 * in the past the loop was inside prefix expiry processing.
510 * However, from a stricter speci-confrmance standpoint, we should
511 * rather separate address lifetimes and prefix lifetimes.
512 */
513 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
514 nia6 = ia6->ia_next;
515 /* check address lifetime */
516 lt6 = &ia6->ia6_lifetime;
517 if (IFA6_IS_INVALID(ia6)) {
518 in6_purgeaddr(&ia6->ia_ifa);
519 }
520 if (IFA6_IS_DEPRECATED(ia6)) {
521 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
522 } else {
523 /*
524 * A new RA might have made a deprecated address
525 * preferred.
526 */
527 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
528 }
529 }
530
531 /* expire prefix list */
532 pr = nd_prefix.lh_first;
533 while (pr) {
534 /*
535 * check prefix lifetime.
536 * since pltime is just for autoconf, pltime processing for
537 * prefix is not necessary.
538 */
539 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
540 time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
541 struct nd_prefix *t;
542 t = pr->ndpr_next;
543
544 /*
545 * address expiration and prefix expiration are
546 * separate. NEVER perform in6_purgeaddr here.
547 */
548
549 prelist_remove(pr);
550 pr = t;
551 } else
552 pr = pr->ndpr_next;
553 }
554 splx(s);
555 }
556
557 /*
558 * Nuke neighbor cache/prefix/default router management table, right before
559 * ifp goes away.
560 */
561 void
562 nd6_purge(ifp)
563 struct ifnet *ifp;
564 {
565 struct llinfo_nd6 *ln, *nln;
566 struct nd_defrouter *dr, *ndr;
567 struct nd_prefix *pr, *npr;
568
569 /*
570 * Nuke default router list entries toward ifp.
571 * We defer removal of default router list entries that is installed
572 * in the routing table, in order to keep additional side effects as
573 * small as possible.
574 */
575 for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
576 ndr = TAILQ_NEXT(dr, dr_entry);
577 if (dr->installed)
578 continue;
579
580 if (dr->ifp == ifp)
581 defrtrlist_del(dr);
582 }
583 for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
584 ndr = TAILQ_NEXT(dr, dr_entry);
585 if (!dr->installed)
586 continue;
587
588 if (dr->ifp == ifp)
589 defrtrlist_del(dr);
590 }
591
592 /* Nuke prefix list entries toward ifp */
593 for (pr = nd_prefix.lh_first; pr; pr = npr) {
594 npr = pr->ndpr_next;
595 if (pr->ndpr_ifp == ifp) {
596 /*
597 * Previously, pr->ndpr_addr is removed as well,
598 * but I strongly believe we don't have to do it.
599 * nd6_purge() is only called from in6_ifdetach(),
600 * which removes all the associated interface addresses
601 * by itself.
602 * (jinmei (at) kame.net 20010129)
603 */
604 prelist_remove(pr);
605 }
606 }
607
608 /* cancel default outgoing interface setting */
609 if (nd6_defifindex == ifp->if_index)
610 nd6_setdefaultiface(0);
611
612 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
613 /* refresh default router list */
614 defrouter_select();
615 }
616
617 /*
618 * Nuke neighbor cache entries for the ifp.
619 * Note that rt->rt_ifp may not be the same as ifp,
620 * due to KAME goto ours hack. See RTM_RESOLVE case in
621 * nd6_rtrequest(), and ip6_input().
622 */
623 ln = llinfo_nd6.ln_next;
624 while (ln && ln != &llinfo_nd6) {
625 struct rtentry *rt;
626 struct sockaddr_dl *sdl;
627
628 nln = ln->ln_next;
629 rt = ln->ln_rt;
630 if (rt && rt->rt_gateway &&
631 rt->rt_gateway->sa_family == AF_LINK) {
632 sdl = (struct sockaddr_dl *)rt->rt_gateway;
633 if (sdl->sdl_index == ifp->if_index)
634 nln = nd6_free(rt, 0);
635 }
636 ln = nln;
637 }
638 }
639
640 struct rtentry *
641 nd6_lookup(addr6, create, ifp)
642 struct in6_addr *addr6;
643 int create;
644 struct ifnet *ifp;
645 {
646 struct rtentry *rt;
647 struct sockaddr_in6 sin6;
648
649 bzero(&sin6, sizeof(sin6));
650 sin6.sin6_len = sizeof(struct sockaddr_in6);
651 sin6.sin6_family = AF_INET6;
652 sin6.sin6_addr = *addr6;
653 rt = rtalloc1((struct sockaddr *)&sin6, create);
654 if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
655 /*
656 * This is the case for the default route.
657 * If we want to create a neighbor cache for the address, we
658 * should free the route for the destination and allocate an
659 * interface route.
660 */
661 if (create) {
662 RTFREE(rt);
663 rt = 0;
664 }
665 }
666 if (!rt) {
667 if (create && ifp) {
668 int e;
669
670 /*
671 * If no route is available and create is set,
672 * we allocate a host route for the destination
673 * and treat it like an interface route.
674 * This hack is necessary for a neighbor which can't
675 * be covered by our own prefix.
676 */
677 struct ifaddr *ifa =
678 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
679 if (ifa == NULL)
680 return(NULL);
681
682 /*
683 * Create a new route. RTF_LLINFO is necessary
684 * to create a Neighbor Cache entry for the
685 * destination in nd6_rtrequest which will be
686 * called in rtrequest via ifa->ifa_rtrequest.
687 */
688 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
689 ifa->ifa_addr, (struct sockaddr *)&all1_sa,
690 (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
691 ~RTF_CLONING, &rt)) != 0) {
692 #if 0
693 log(LOG_ERR,
694 "nd6_lookup: failed to add route for a "
695 "neighbor(%s), errno=%d\n",
696 ip6_sprintf(addr6), e);
697 #endif
698 return(NULL);
699 }
700 if (rt == NULL)
701 return(NULL);
702 if (rt->rt_llinfo) {
703 struct llinfo_nd6 *ln =
704 (struct llinfo_nd6 *)rt->rt_llinfo;
705 ln->ln_state = ND6_LLINFO_NOSTATE;
706 }
707 } else
708 return(NULL);
709 }
710 rt->rt_refcnt--;
711 /*
712 * Validation for the entry.
713 * Note that the check for rt_llinfo is necessary because a cloned
714 * route from a parent route that has the L flag (e.g. the default
715 * route to a p2p interface) may have the flag, too, while the
716 * destination is not actually a neighbor.
717 * XXX: we can't use rt->rt_ifp to check for the interface, since
718 * it might be the loopback interface if the entry is for our
719 * own address on a non-loopback interface. Instead, we should
720 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
721 * interface.
722 */
723 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
724 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
725 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
726 if (create) {
727 log(LOG_DEBUG,
728 "nd6_lookup: failed to lookup %s (if = %s)\n",
729 ip6_sprintf(addr6),
730 ifp ? if_name(ifp) : "unspec");
731 }
732 return(NULL);
733 }
734 return(rt);
735 }
736
737 /*
738 * Detect if a given IPv6 address identifies a neighbor on a given link.
739 * XXX: should take care of the destination of a p2p link?
740 */
741 int
742 nd6_is_addr_neighbor(addr, ifp)
743 struct sockaddr_in6 *addr;
744 struct ifnet *ifp;
745 {
746 struct nd_prefix *pr;
747 struct rtentry *rt;
748
749 /*
750 * A link-local address is always a neighbor.
751 * XXX: we should use the sin6_scope_id field rather than the embedded
752 * interface index.
753 * XXX: a link does not necessarily specify a single interface.
754 */
755 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
756 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
757 return(1);
758
759 /*
760 * If the address matches one of our on-link prefixes, it should be a
761 * neighbor.
762 */
763 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
764 if (pr->ndpr_ifp != ifp)
765 continue;
766
767 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
768 continue;
769
770 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
771 &addr->sin6_addr, &pr->ndpr_mask))
772 return (1);
773 }
774
775 /*
776 * If the default router list is empty, all addresses are regarded
777 * as on-link, and thus, as a neighbor.
778 * XXX: we restrict the condition to hosts, because routers usually do
779 * not have the "default router list".
780 */
781 if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
782 nd6_defifindex == ifp->if_index) {
783 return(1);
784 }
785
786 /*
787 * Even if the address matches none of our addresses, it might be
788 * in the neighbor cache.
789 */
790 if ((rt = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL)
791 return(1);
792
793 return(0);
794 }
795
796 /*
797 * Free an nd6 llinfo entry.
798 * Since the function would cause significant changes in the kernel, DO NOT
799 * make it global, unless you have a strong reason for the change, and are sure
800 * that the change is safe.
801 */
802 static struct llinfo_nd6 *
803 nd6_free(rt, gc)
804 struct rtentry *rt;
805 int gc;
806 {
807 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
808 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
809 struct nd_defrouter *dr;
810
811 /*
812 * we used to have pfctlinput(PRC_HOSTDEAD) here.
813 * even though it is not harmful, it was not really necessary.
814 */
815
816 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
817 int s;
818 s = splsoftnet();
819 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
820 rt->rt_ifp);
821
822 if (dr != NULL && dr->expire &&
823 ln->ln_state == ND6_LLINFO_STALE && gc) {
824 /*
825 * If the reason for the deletion is just garbage
826 * collection, and the neighbor is an active default
827 * router, do not delete it. Instead, reset the GC
828 * timer using the router's lifetime.
829 * Simply deleting the entry would affect default
830 * router selection, which is not necessarily a good
831 * thing, especially when we're using router preference
832 * values.
833 * XXX: the check for ln_state would be redundant,
834 * but we intentionally keep it just in case.
835 */
836 ln->ln_expire = dr->expire;
837 splx(s);
838 return(ln->ln_next);
839 }
840
841 if (ln->ln_router || dr) {
842 /*
843 * rt6_flush must be called whether or not the neighbor
844 * is in the Default Router List.
845 * See a corresponding comment in nd6_na_input().
846 */
847 rt6_flush(&in6, rt->rt_ifp);
848 }
849
850 if (dr) {
851 /*
852 * Unreachablity of a router might affect the default
853 * router selection and on-link detection of advertised
854 * prefixes.
855 */
856
857 /*
858 * Temporarily fake the state to choose a new default
859 * router and to perform on-link determination of
860 * prefixes correctly.
861 * Below the state will be set correctly,
862 * or the entry itself will be deleted.
863 */
864 ln->ln_state = ND6_LLINFO_INCOMPLETE;
865
866 /*
867 * Since defrouter_select() does not affect the
868 * on-link determination and MIP6 needs the check
869 * before the default router selection, we perform
870 * the check now.
871 */
872 pfxlist_onlink_check();
873
874 /*
875 * refresh default router list
876 */
877 defrouter_select();
878 }
879 splx(s);
880 }
881
882 /*
883 * Before deleting the entry, remember the next entry as the
884 * return value. We need this because pfxlist_onlink_check() above
885 * might have freed other entries (particularly the old next entry) as
886 * a side effect (XXX).
887 */
888 next = ln->ln_next;
889
890 /*
891 * Detach the route from the routing tree and the list of neighbor
892 * caches, and disable the route entry not to be used in already
893 * cached routes.
894 */
895 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
896 rt_mask(rt), 0, (struct rtentry **)0);
897
898 return(next);
899 }
900
901 /*
902 * Upper-layer reachability hint for Neighbor Unreachability Detection.
903 *
904 * XXX cost-effective metods?
905 */
906 void
907 nd6_nud_hint(rt, dst6, force)
908 struct rtentry *rt;
909 struct in6_addr *dst6;
910 int force;
911 {
912 struct llinfo_nd6 *ln;
913 long time_second = time.tv_sec;
914
915 /*
916 * If the caller specified "rt", use that. Otherwise, resolve the
917 * routing table by supplied "dst6".
918 */
919 if (!rt) {
920 if (!dst6)
921 return;
922 if (!(rt = nd6_lookup(dst6, 0, NULL)))
923 return;
924 }
925
926 if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
927 (rt->rt_flags & RTF_LLINFO) == 0 ||
928 !rt->rt_llinfo || !rt->rt_gateway ||
929 rt->rt_gateway->sa_family != AF_LINK) {
930 /* This is not a host route. */
931 return;
932 }
933
934 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
935 if (ln->ln_state < ND6_LLINFO_REACHABLE)
936 return;
937
938 /*
939 * if we get upper-layer reachability confirmation many times,
940 * it is possible we have false information.
941 */
942 if (!force) {
943 ln->ln_byhint++;
944 if (ln->ln_byhint > nd6_maxnudhint)
945 return;
946 }
947
948 ln->ln_state = ND6_LLINFO_REACHABLE;
949 if (ln->ln_expire)
950 ln->ln_expire = time_second + ND_IFINFO(rt->rt_ifp)->reachable;
951 }
952
953 void
954 nd6_rtrequest(req, rt, info)
955 int req;
956 struct rtentry *rt;
957 struct rt_addrinfo *info; /* xxx unused */
958 {
959 struct sockaddr *gate = rt->rt_gateway;
960 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
961 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
962 struct ifnet *ifp = rt->rt_ifp;
963 struct ifaddr *ifa;
964 long time_second = time.tv_sec;
965 int mine = 0;
966
967 if ((rt->rt_flags & RTF_GATEWAY) != 0)
968 return;
969
970 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
971 /*
972 * This is probably an interface direct route for a link
973 * which does not need neighbor caches (e.g. fe80::%lo0/64).
974 * We do not need special treatment below for such a route.
975 * Moreover, the RTF_LLINFO flag which would be set below
976 * would annoy the ndp(8) command.
977 */
978 return;
979 }
980
981 if (req == RTM_RESOLVE &&
982 (nd6_need_cache(ifp) == 0 || /* stf case */
983 !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp))) {
984 /*
985 * FreeBSD and BSD/OS often make a cloned host route based
986 * on a less-specific route (e.g. the default route).
987 * If the less specific route does not have a "gateway"
988 * (this is the case when the route just goes to a p2p or an
989 * stf interface), we'll mistakenly make a neighbor cache for
990 * the host route, and will see strange neighbor solicitation
991 * for the corresponding destination. In order to avoid the
992 * confusion, we check if the destination of the route is
993 * a neighbor in terms of neighbor discovery, and stop the
994 * process if not. Additionally, we remove the LLINFO flag
995 * so that ndp(8) will not try to get the neighbor information
996 * of the destination.
997 */
998 rt->rt_flags &= ~RTF_LLINFO;
999 return;
1000 }
1001
1002 switch (req) {
1003 case RTM_ADD:
1004 /*
1005 * There is no backward compatibility :)
1006 *
1007 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1008 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1009 * rt->rt_flags |= RTF_CLONING;
1010 */
1011 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1012 /*
1013 * Case 1: This route should come from
1014 * a route to interface. RTF_LLINFO flag is set
1015 * for a host route whose destination should be
1016 * treated as on-link.
1017 */
1018 rt_setgate(rt, rt_key(rt),
1019 (struct sockaddr *)&null_sdl);
1020 gate = rt->rt_gateway;
1021 SDL(gate)->sdl_type = ifp->if_type;
1022 SDL(gate)->sdl_index = ifp->if_index;
1023 if (ln)
1024 ln->ln_expire = time_second;
1025 #if 1
1026 if (ln && ln->ln_expire == 0) {
1027 /* kludge for desktops */
1028 #if 0
1029 printf("nd6_rtrequest: time.tv_sec is zero; "
1030 "treat it as 1\n");
1031 #endif
1032 ln->ln_expire = 1;
1033 }
1034 #endif
1035 if ((rt->rt_flags & RTF_CLONING) != 0)
1036 break;
1037 }
1038 /*
1039 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1040 * We don't do that here since llinfo is not ready yet.
1041 *
1042 * There are also couple of other things to be discussed:
1043 * - unsolicited NA code needs improvement beforehand
1044 * - RFC2461 says we MAY send multicast unsolicited NA
1045 * (7.2.6 paragraph 4), however, it also says that we
1046 * SHOULD provide a mechanism to prevent multicast NA storm.
1047 * we don't have anything like it right now.
1048 * note that the mechanism needs a mutual agreement
1049 * between proxies, which means that we need to implement
1050 * a new protocol, or a new kludge.
1051 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1052 * we need to check ip6forwarding before sending it.
1053 * (or should we allow proxy ND configuration only for
1054 * routers? there's no mention about proxy ND from hosts)
1055 */
1056 #if 0
1057 /* XXX it does not work */
1058 if (rt->rt_flags & RTF_ANNOUNCE)
1059 nd6_na_output(ifp,
1060 &SIN6(rt_key(rt))->sin6_addr,
1061 &SIN6(rt_key(rt))->sin6_addr,
1062 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1063 1, NULL);
1064 #endif
1065 /* FALLTHROUGH */
1066 case RTM_RESOLVE:
1067 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1068 /*
1069 * Address resolution isn't necessary for a point to
1070 * point link, so we can skip this test for a p2p link.
1071 */
1072 if (gate->sa_family != AF_LINK ||
1073 gate->sa_len < sizeof(null_sdl)) {
1074 log(LOG_DEBUG,
1075 "nd6_rtrequest: bad gateway value: %s\n",
1076 if_name(ifp));
1077 break;
1078 }
1079 SDL(gate)->sdl_type = ifp->if_type;
1080 SDL(gate)->sdl_index = ifp->if_index;
1081 }
1082 if (ln != NULL)
1083 break; /* This happens on a route change */
1084 /*
1085 * Case 2: This route may come from cloning, or a manual route
1086 * add with a LL address.
1087 */
1088 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1089 rt->rt_llinfo = (caddr_t)ln;
1090 if (!ln) {
1091 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1092 break;
1093 }
1094 nd6_inuse++;
1095 nd6_allocated++;
1096 Bzero(ln, sizeof(*ln));
1097 ln->ln_rt = rt;
1098 /* this is required for "ndp" command. - shin */
1099 if (req == RTM_ADD) {
1100 /*
1101 * gate should have some valid AF_LINK entry,
1102 * and ln->ln_expire should have some lifetime
1103 * which is specified by ndp command.
1104 */
1105 ln->ln_state = ND6_LLINFO_REACHABLE;
1106 ln->ln_byhint = 0;
1107 } else {
1108 /*
1109 * When req == RTM_RESOLVE, rt is created and
1110 * initialized in rtrequest(), so rt_expire is 0.
1111 */
1112 ln->ln_state = ND6_LLINFO_NOSTATE;
1113 ln->ln_expire = time_second;
1114 }
1115 rt->rt_flags |= RTF_LLINFO;
1116 ln->ln_next = llinfo_nd6.ln_next;
1117 llinfo_nd6.ln_next = ln;
1118 ln->ln_prev = &llinfo_nd6;
1119 ln->ln_next->ln_prev = ln;
1120
1121 /*
1122 * check if rt_key(rt) is one of my address assigned
1123 * to the interface.
1124 */
1125 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1126 &SIN6(rt_key(rt))->sin6_addr);
1127 if (ifa) {
1128 caddr_t macp = nd6_ifptomac(ifp);
1129 ln->ln_expire = 0;
1130 ln->ln_state = ND6_LLINFO_REACHABLE;
1131 ln->ln_byhint = 0;
1132 mine = 1;
1133 if (macp) {
1134 Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1135 SDL(gate)->sdl_alen = ifp->if_addrlen;
1136 }
1137 if (nd6_useloopback) {
1138 rt->rt_ifp = &loif[0]; /* XXX */
1139 /*
1140 * Make sure rt_ifa be equal to the ifaddr
1141 * corresponding to the address.
1142 * We need this because when we refer
1143 * rt_ifa->ia6_flags in ip6_input, we assume
1144 * that the rt_ifa points to the address instead
1145 * of the loopback address.
1146 */
1147 if (ifa != rt->rt_ifa) {
1148 IFAFREE(rt->rt_ifa);
1149 IFAREF(ifa);
1150 rt->rt_ifa = ifa;
1151 }
1152 }
1153 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1154 ln->ln_expire = 0;
1155 ln->ln_state = ND6_LLINFO_REACHABLE;
1156 ln->ln_byhint = 0;
1157
1158 /* join solicited node multicast for proxy ND */
1159 if (ifp->if_flags & IFF_MULTICAST) {
1160 struct in6_addr llsol;
1161 int error;
1162
1163 llsol = SIN6(rt_key(rt))->sin6_addr;
1164 llsol.s6_addr16[0] = htons(0xff02);
1165 llsol.s6_addr16[1] = htons(ifp->if_index);
1166 llsol.s6_addr32[1] = 0;
1167 llsol.s6_addr32[2] = htonl(1);
1168 llsol.s6_addr8[12] = 0xff;
1169
1170 if (!in6_addmulti(&llsol, ifp, &error)) {
1171 nd6log((LOG_ERR, "%s: failed to join "
1172 "%s (errno=%d)\n", if_name(ifp),
1173 ip6_sprintf(&llsol), error));
1174 }
1175 }
1176 }
1177 break;
1178
1179 case RTM_DELETE:
1180 if (!ln)
1181 break;
1182 /* leave from solicited node multicast for proxy ND */
1183 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1184 (ifp->if_flags & IFF_MULTICAST) != 0) {
1185 struct in6_addr llsol;
1186 struct in6_multi *in6m;
1187
1188 llsol = SIN6(rt_key(rt))->sin6_addr;
1189 llsol.s6_addr16[0] = htons(0xff02);
1190 llsol.s6_addr16[1] = htons(ifp->if_index);
1191 llsol.s6_addr32[1] = 0;
1192 llsol.s6_addr32[2] = htonl(1);
1193 llsol.s6_addr8[12] = 0xff;
1194
1195 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1196 if (in6m)
1197 in6_delmulti(in6m);
1198 }
1199 nd6_inuse--;
1200 ln->ln_next->ln_prev = ln->ln_prev;
1201 ln->ln_prev->ln_next = ln->ln_next;
1202 ln->ln_prev = NULL;
1203 rt->rt_llinfo = 0;
1204 rt->rt_flags &= ~RTF_LLINFO;
1205 if (ln->ln_hold)
1206 m_freem(ln->ln_hold);
1207 Free((caddr_t)ln);
1208 }
1209 }
1210
1211 int
1212 nd6_ioctl(cmd, data, ifp)
1213 u_long cmd;
1214 caddr_t data;
1215 struct ifnet *ifp;
1216 {
1217 struct in6_drlist *drl = (struct in6_drlist *)data;
1218 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1219 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1220 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1221 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1222 struct nd_defrouter *dr;
1223 struct nd_prefix *pr;
1224 struct rtentry *rt;
1225 int i = 0, error = 0;
1226 int s;
1227
1228 switch (cmd) {
1229 case SIOCGDRLST_IN6:
1230 /*
1231 * obsolete API, use sysctl under net.inet6.icmp6
1232 */
1233 bzero(drl, sizeof(*drl));
1234 s = splsoftnet();
1235 dr = TAILQ_FIRST(&nd_defrouter);
1236 while (dr && i < DRLSTSIZ) {
1237 drl->defrouter[i].rtaddr = dr->rtaddr;
1238 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1239 /* XXX: need to this hack for KAME stack */
1240 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1241 } else
1242 log(LOG_ERR,
1243 "default router list contains a "
1244 "non-linklocal address(%s)\n",
1245 ip6_sprintf(&drl->defrouter[i].rtaddr));
1246
1247 drl->defrouter[i].flags = dr->flags;
1248 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1249 drl->defrouter[i].expire = dr->expire;
1250 drl->defrouter[i].if_index = dr->ifp->if_index;
1251 i++;
1252 dr = TAILQ_NEXT(dr, dr_entry);
1253 }
1254 splx(s);
1255 break;
1256 case SIOCGPRLST_IN6:
1257 /*
1258 * obsolete API, use sysctl under net.inet6.icmp6
1259 *
1260 * XXX the structure in6_prlist was changed in backward-
1261 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1262 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1263 */
1264 /*
1265 * XXX meaning of fields, especialy "raflags", is very
1266 * differnet between RA prefix list and RR/static prefix list.
1267 * how about separating ioctls into two?
1268 */
1269 bzero(oprl, sizeof(*oprl));
1270 s = splsoftnet();
1271 pr = nd_prefix.lh_first;
1272 while (pr && i < PRLSTSIZ) {
1273 struct nd_pfxrouter *pfr;
1274 int j;
1275
1276 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1277 oprl->prefix[i].raflags = pr->ndpr_raf;
1278 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1279 oprl->prefix[i].vltime = pr->ndpr_vltime;
1280 oprl->prefix[i].pltime = pr->ndpr_pltime;
1281 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1282 oprl->prefix[i].expire = pr->ndpr_expire;
1283
1284 pfr = pr->ndpr_advrtrs.lh_first;
1285 j = 0;
1286 while (pfr) {
1287 if (j < DRLSTSIZ) {
1288 #define RTRADDR oprl->prefix[i].advrtr[j]
1289 RTRADDR = pfr->router->rtaddr;
1290 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1291 /* XXX: hack for KAME */
1292 RTRADDR.s6_addr16[1] = 0;
1293 } else
1294 log(LOG_ERR,
1295 "a router(%s) advertises "
1296 "a prefix with "
1297 "non-link local address\n",
1298 ip6_sprintf(&RTRADDR));
1299 #undef RTRADDR
1300 }
1301 j++;
1302 pfr = pfr->pfr_next;
1303 }
1304 oprl->prefix[i].advrtrs = j;
1305 oprl->prefix[i].origin = PR_ORIG_RA;
1306
1307 i++;
1308 pr = pr->ndpr_next;
1309 }
1310 splx(s);
1311
1312 break;
1313 case OSIOCGIFINFO_IN6:
1314 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1315 bzero(&ndi->ndi, sizeof(ndi->ndi));
1316 ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1317 ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1318 ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1319 ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1320 ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1321 ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1322 ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1323 ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1324 break;
1325 case SIOCGIFINFO_IN6:
1326 ndi->ndi = *ND_IFINFO(ifp);
1327 break;
1328 case SIOCSIFINFO_FLAGS:
1329 ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1330 break;
1331 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1332 /* sync kernel routing table with the default router list */
1333 defrouter_reset();
1334 defrouter_select();
1335 break;
1336 case SIOCSPFXFLUSH_IN6:
1337 {
1338 /* flush all the prefix advertised by routers */
1339 struct nd_prefix *pr, *next;
1340
1341 s = splsoftnet();
1342 for (pr = nd_prefix.lh_first; pr; pr = next) {
1343 struct in6_ifaddr *ia, *ia_next;
1344
1345 next = pr->ndpr_next;
1346
1347 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1348 continue; /* XXX */
1349
1350 /* do we really have to remove addresses as well? */
1351 for (ia = in6_ifaddr; ia; ia = ia_next) {
1352 /* ia might be removed. keep the next ptr. */
1353 ia_next = ia->ia_next;
1354
1355 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1356 continue;
1357
1358 if (ia->ia6_ndpr == pr)
1359 in6_purgeaddr(&ia->ia_ifa);
1360 }
1361 prelist_remove(pr);
1362 }
1363 splx(s);
1364 break;
1365 }
1366 case SIOCSRTRFLUSH_IN6:
1367 {
1368 /* flush all the default routers */
1369 struct nd_defrouter *dr, *next;
1370
1371 s = splsoftnet();
1372 defrouter_reset();
1373 for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = next) {
1374 next = TAILQ_NEXT(dr, dr_entry);
1375 defrtrlist_del(dr);
1376 }
1377 defrouter_select();
1378 splx(s);
1379 break;
1380 }
1381 case SIOCGNBRINFO_IN6:
1382 {
1383 struct llinfo_nd6 *ln;
1384 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1385
1386 /*
1387 * XXX: KAME specific hack for scoped addresses
1388 * XXXX: for other scopes than link-local?
1389 */
1390 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1391 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1392 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1393
1394 if (*idp == 0)
1395 *idp = htons(ifp->if_index);
1396 }
1397
1398 s = splsoftnet();
1399 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL ||
1400 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
1401 error = EINVAL;
1402 splx(s);
1403 break;
1404 }
1405 nbi->state = ln->ln_state;
1406 nbi->asked = ln->ln_asked;
1407 nbi->isrouter = ln->ln_router;
1408 nbi->expire = ln->ln_expire;
1409 splx(s);
1410
1411 break;
1412 }
1413 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1414 ndif->ifindex = nd6_defifindex;
1415 break;
1416 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1417 return(nd6_setdefaultiface(ndif->ifindex));
1418 break;
1419 }
1420 return(error);
1421 }
1422
1423 /*
1424 * Create neighbor cache entry and cache link-layer address,
1425 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1426 */
1427 struct rtentry *
1428 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1429 struct ifnet *ifp;
1430 struct in6_addr *from;
1431 char *lladdr;
1432 int lladdrlen;
1433 int type; /* ICMP6 type */
1434 int code; /* type dependent information */
1435 {
1436 struct rtentry *rt = NULL;
1437 struct llinfo_nd6 *ln = NULL;
1438 int is_newentry;
1439 struct sockaddr_dl *sdl = NULL;
1440 int do_update;
1441 int olladdr;
1442 int llchange;
1443 int newstate = 0;
1444 long time_second = time.tv_sec;
1445
1446 if (!ifp)
1447 panic("ifp == NULL in nd6_cache_lladdr");
1448 if (!from)
1449 panic("from == NULL in nd6_cache_lladdr");
1450
1451 /* nothing must be updated for unspecified address */
1452 if (IN6_IS_ADDR_UNSPECIFIED(from))
1453 return NULL;
1454
1455 /*
1456 * Validation about ifp->if_addrlen and lladdrlen must be done in
1457 * the caller.
1458 *
1459 * XXX If the link does not have link-layer adderss, what should
1460 * we do? (ifp->if_addrlen == 0)
1461 * Spec says nothing in sections for RA, RS and NA. There's small
1462 * description on it in NS section (RFC 2461 7.2.3).
1463 */
1464
1465 rt = nd6_lookup(from, 0, ifp);
1466 if (!rt) {
1467 #if 0
1468 /* nothing must be done if there's no lladdr */
1469 if (!lladdr || !lladdrlen)
1470 return NULL;
1471 #endif
1472
1473 rt = nd6_lookup(from, 1, ifp);
1474 is_newentry = 1;
1475 } else {
1476 /* do nothing if static ndp is set */
1477 if (rt->rt_flags & RTF_STATIC)
1478 return NULL;
1479 is_newentry = 0;
1480 }
1481
1482 if (!rt)
1483 return NULL;
1484 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1485 fail:
1486 (void)nd6_free(rt, 0);
1487 return NULL;
1488 }
1489 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1490 if (!ln)
1491 goto fail;
1492 if (!rt->rt_gateway)
1493 goto fail;
1494 if (rt->rt_gateway->sa_family != AF_LINK)
1495 goto fail;
1496 sdl = SDL(rt->rt_gateway);
1497
1498 olladdr = (sdl->sdl_alen) ? 1 : 0;
1499 if (olladdr && lladdr) {
1500 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1501 llchange = 1;
1502 else
1503 llchange = 0;
1504 } else
1505 llchange = 0;
1506
1507 /*
1508 * newentry olladdr lladdr llchange (*=record)
1509 * 0 n n -- (1)
1510 * 0 y n -- (2)
1511 * 0 n y -- (3) * STALE
1512 * 0 y y n (4) *
1513 * 0 y y y (5) * STALE
1514 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1515 * 1 -- y -- (7) * STALE
1516 */
1517
1518 if (lladdr) { /* (3-5) and (7) */
1519 /*
1520 * Record source link-layer address
1521 * XXX is it dependent to ifp->if_type?
1522 */
1523 sdl->sdl_alen = ifp->if_addrlen;
1524 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1525 }
1526
1527 if (!is_newentry) {
1528 if ((!olladdr && lladdr) || /* (3) */
1529 (olladdr && lladdr && llchange)) { /* (5) */
1530 do_update = 1;
1531 newstate = ND6_LLINFO_STALE;
1532 } else /* (1-2,4) */
1533 do_update = 0;
1534 } else {
1535 do_update = 1;
1536 if (!lladdr) /* (6) */
1537 newstate = ND6_LLINFO_NOSTATE;
1538 else /* (7) */
1539 newstate = ND6_LLINFO_STALE;
1540 }
1541
1542 if (do_update) {
1543 /*
1544 * Update the state of the neighbor cache.
1545 */
1546 ln->ln_state = newstate;
1547
1548 if (ln->ln_state == ND6_LLINFO_STALE) {
1549 /*
1550 * XXX: since nd6_output() below will cause
1551 * state tansition to DELAY and reset the timer,
1552 * we must set the timer now, although it is actually
1553 * meaningless.
1554 */
1555 ln->ln_expire = time_second + nd6_gctimer;
1556
1557 if (ln->ln_hold) {
1558 /*
1559 * we assume ifp is not a p2p here, so just
1560 * set the 2nd argument as the 1st one.
1561 */
1562 nd6_output(ifp, ifp, ln->ln_hold,
1563 (struct sockaddr_in6 *)rt_key(rt), rt);
1564 ln->ln_hold = NULL;
1565 }
1566 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1567 /* probe right away */
1568 ln->ln_expire = time_second;
1569 }
1570 }
1571
1572 /*
1573 * ICMP6 type dependent behavior.
1574 *
1575 * NS: clear IsRouter if new entry
1576 * RS: clear IsRouter
1577 * RA: set IsRouter if there's lladdr
1578 * redir: clear IsRouter if new entry
1579 *
1580 * RA case, (1):
1581 * The spec says that we must set IsRouter in the following cases:
1582 * - If lladdr exist, set IsRouter. This means (1-5).
1583 * - If it is old entry (!newentry), set IsRouter. This means (7).
1584 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1585 * A quetion arises for (1) case. (1) case has no lladdr in the
1586 * neighbor cache, this is similar to (6).
1587 * This case is rare but we figured that we MUST NOT set IsRouter.
1588 *
1589 * newentry olladdr lladdr llchange NS RS RA redir
1590 * D R
1591 * 0 n n -- (1) c ? s
1592 * 0 y n -- (2) c s s
1593 * 0 n y -- (3) c s s
1594 * 0 y y n (4) c s s
1595 * 0 y y y (5) c s s
1596 * 1 -- n -- (6) c c c s
1597 * 1 -- y -- (7) c c s c s
1598 *
1599 * (c=clear s=set)
1600 */
1601 switch (type & 0xff) {
1602 case ND_NEIGHBOR_SOLICIT:
1603 /*
1604 * New entry must have is_router flag cleared.
1605 */
1606 if (is_newentry) /* (6-7) */
1607 ln->ln_router = 0;
1608 break;
1609 case ND_REDIRECT:
1610 /*
1611 * If the icmp is a redirect to a better router, always set the
1612 * is_router flag. Otherwise, if the entry is newly created,
1613 * clear the flag. [RFC 2461, sec 8.3]
1614 */
1615 if (code == ND_REDIRECT_ROUTER)
1616 ln->ln_router = 1;
1617 else if (is_newentry) /* (6-7) */
1618 ln->ln_router = 0;
1619 break;
1620 case ND_ROUTER_SOLICIT:
1621 /*
1622 * is_router flag must always be cleared.
1623 */
1624 ln->ln_router = 0;
1625 break;
1626 case ND_ROUTER_ADVERT:
1627 /*
1628 * Mark an entry with lladdr as a router.
1629 */
1630 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1631 (is_newentry && lladdr)) { /* (7) */
1632 ln->ln_router = 1;
1633 }
1634 break;
1635 }
1636
1637 /*
1638 * When the link-layer address of a router changes, select the
1639 * best router again. In particular, when the neighbor entry is newly
1640 * created, it might affect the selection policy.
1641 * Question: can we restrict the first condition to the "is_newentry"
1642 * case?
1643 * XXX: when we hear an RA from a new router with the link-layer
1644 * address option, defrouter_select() is called twice, since
1645 * defrtrlist_update called the function as well. However, I believe
1646 * we can compromise the overhead, since it only happens the first
1647 * time.
1648 * XXX: although defrouter_select() should not have a bad effect
1649 * for those are not autoconfigured hosts, we explicitly avoid such
1650 * cases for safety.
1651 */
1652 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1653 defrouter_select();
1654
1655 return rt;
1656 }
1657
1658 static void
1659 nd6_slowtimo(ignored_arg)
1660 void *ignored_arg;
1661 {
1662 int s = splsoftnet();
1663 struct nd_ifinfo *nd6if;
1664 struct ifnet *ifp;
1665
1666 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1667 nd6_slowtimo, NULL);
1668 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1669 {
1670 nd6if = ND_IFINFO(ifp);
1671 if (nd6if->basereachable && /* already initialized */
1672 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1673 /*
1674 * Since reachable time rarely changes by router
1675 * advertisements, we SHOULD insure that a new random
1676 * value gets recomputed at least once every few hours.
1677 * (RFC 2461, 6.3.4)
1678 */
1679 nd6if->recalctm = nd6_recalc_reachtm_interval;
1680 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1681 }
1682 }
1683 splx(s);
1684 }
1685
1686 #define senderr(e) { error = (e); goto bad;}
1687 int
1688 nd6_output(ifp, origifp, m0, dst, rt0)
1689 struct ifnet *ifp;
1690 struct ifnet *origifp;
1691 struct mbuf *m0;
1692 struct sockaddr_in6 *dst;
1693 struct rtentry *rt0;
1694 {
1695 struct mbuf *m = m0;
1696 struct rtentry *rt = rt0;
1697 struct sockaddr_in6 *gw6 = NULL;
1698 struct llinfo_nd6 *ln = NULL;
1699 int error = 0;
1700 long time_second = time.tv_sec;
1701
1702 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1703 goto sendpkt;
1704
1705 if (nd6_need_cache(ifp) == 0)
1706 goto sendpkt;
1707
1708 /*
1709 * next hop determination. This routine is derived from ether_outpout.
1710 */
1711 if (rt) {
1712 if ((rt->rt_flags & RTF_UP) == 0) {
1713 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst,
1714 1)) != NULL)
1715 {
1716 rt->rt_refcnt--;
1717 if (rt->rt_ifp != ifp) {
1718 /* XXX: loop care? */
1719 return nd6_output(ifp, origifp, m0,
1720 dst, rt);
1721 }
1722 } else
1723 senderr(EHOSTUNREACH);
1724 }
1725
1726 if (rt->rt_flags & RTF_GATEWAY) {
1727 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1728
1729 /*
1730 * We skip link-layer address resolution and NUD
1731 * if the gateway is not a neighbor from ND point
1732 * of view, regardless of the value of nd_ifinfo.flags.
1733 * The second condition is a bit tricky; we skip
1734 * if the gateway is our own address, which is
1735 * sometimes used to install a route to a p2p link.
1736 */
1737 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1738 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1739 /*
1740 * We allow this kind of tricky route only
1741 * when the outgoing interface is p2p.
1742 * XXX: we may need a more generic rule here.
1743 */
1744 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1745 senderr(EHOSTUNREACH);
1746
1747 goto sendpkt;
1748 }
1749
1750 if (rt->rt_gwroute == 0)
1751 goto lookup;
1752 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1753 rtfree(rt); rt = rt0;
1754 lookup:
1755 rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
1756 if ((rt = rt->rt_gwroute) == 0)
1757 senderr(EHOSTUNREACH);
1758 /* the "G" test below also prevents rt == rt0 */
1759 if ((rt->rt_flags & RTF_GATEWAY) ||
1760 (rt->rt_ifp != ifp)) {
1761 rt->rt_refcnt--;
1762 rt0->rt_gwroute = 0;
1763 senderr(EHOSTUNREACH);
1764 }
1765 }
1766 }
1767 }
1768
1769 /*
1770 * Address resolution or Neighbor Unreachability Detection
1771 * for the next hop.
1772 * At this point, the destination of the packet must be a unicast
1773 * or an anycast address(i.e. not a multicast).
1774 */
1775
1776 /* Look up the neighbor cache for the nexthop */
1777 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1778 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1779 else {
1780 /*
1781 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1782 * the condition below is not very efficient. But we believe
1783 * it is tolerable, because this should be a rare case.
1784 */
1785 if (nd6_is_addr_neighbor(dst, ifp) &&
1786 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1787 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1788 }
1789 if (!ln || !rt) {
1790 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1791 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1792 log(LOG_DEBUG,
1793 "nd6_output: can't allocate llinfo for %s "
1794 "(ln=%p, rt=%p)\n",
1795 ip6_sprintf(&dst->sin6_addr), ln, rt);
1796 senderr(EIO); /* XXX: good error? */
1797 }
1798
1799 goto sendpkt; /* send anyway */
1800 }
1801
1802 /* We don't have to do link-layer address resolution on a p2p link. */
1803 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1804 ln->ln_state < ND6_LLINFO_REACHABLE) {
1805 ln->ln_state = ND6_LLINFO_STALE;
1806 ln->ln_expire = time_second + nd6_gctimer;
1807 }
1808
1809 /*
1810 * The first time we send a packet to a neighbor whose entry is
1811 * STALE, we have to change the state to DELAY and a sets a timer to
1812 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1813 * neighbor unreachability detection on expiration.
1814 * (RFC 2461 7.3.3)
1815 */
1816 if (ln->ln_state == ND6_LLINFO_STALE) {
1817 ln->ln_asked = 0;
1818 ln->ln_state = ND6_LLINFO_DELAY;
1819 ln->ln_expire = time_second + nd6_delay;
1820 }
1821
1822 /*
1823 * If the neighbor cache entry has a state other than INCOMPLETE
1824 * (i.e. its link-layer address is already resolved), just
1825 * send the packet.
1826 */
1827 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1828 goto sendpkt;
1829
1830 /*
1831 * There is a neighbor cache entry, but no ethernet address
1832 * response yet. Replace the held mbuf (if any) with this
1833 * latest one.
1834 */
1835 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1836 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1837 if (ln->ln_hold)
1838 m_freem(ln->ln_hold);
1839 ln->ln_hold = m;
1840 /*
1841 * If there has been no NS for the neighbor after entering the
1842 * INCOMPLETE state, send the first solicitation.
1843 * Technically this can be against the rate-limiting rule described in
1844 * Section 7.2.2 of RFC 2461 because the interval to the next scheduled
1845 * solicitation issued in nd6_timer() may be less than the specified
1846 * retransmission time. This should not be a problem from a practical
1847 * point of view, because we'll typically see an immediate response
1848 * from the neighbor, which suppresses the succeeding solicitations.
1849 */
1850 if (ln->ln_expire && ln->ln_asked == 0) {
1851 ln->ln_asked++;
1852 ln->ln_expire = time_second +
1853 ND6_RETRANS_SEC(ND_IFINFO(ifp)->retrans);
1854 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1855 }
1856 return(0);
1857
1858 sendpkt:
1859
1860 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1861 return((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
1862 rt));
1863 }
1864 return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1865
1866 bad:
1867 if (m)
1868 m_freem(m);
1869 return (error);
1870 }
1871 #undef senderr
1872
1873 int
1874 nd6_need_cache(ifp)
1875 struct ifnet *ifp;
1876 {
1877 /*
1878 * XXX: we currently do not make neighbor cache on any interface
1879 * other than ARCnet, Ethernet, FDDI and GIF.
1880 *
1881 * RFC2893 says:
1882 * - unidirectional tunnels needs no ND
1883 */
1884 switch (ifp->if_type) {
1885 case IFT_ARCNET:
1886 case IFT_ETHER:
1887 case IFT_FDDI:
1888 case IFT_IEEE1394:
1889 case IFT_GIF: /* XXX need more cases? */
1890 return(1);
1891 default:
1892 return(0);
1893 }
1894 }
1895
1896 int
1897 nd6_storelladdr(ifp, rt, m, dst, desten)
1898 struct ifnet *ifp;
1899 struct rtentry *rt;
1900 struct mbuf *m;
1901 struct sockaddr *dst;
1902 u_char *desten;
1903 {
1904 struct sockaddr_dl *sdl;
1905
1906 if (m->m_flags & M_MCAST) {
1907 switch (ifp->if_type) {
1908 case IFT_ETHER:
1909 case IFT_FDDI:
1910 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
1911 desten);
1912 return(1);
1913 case IFT_IEEE1394:
1914 bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
1915 return(1);
1916 case IFT_ARCNET:
1917 *desten = 0;
1918 return(1);
1919 default:
1920 m_freem(m);
1921 return(0);
1922 }
1923 }
1924
1925 if (rt == NULL) {
1926 /* this could happen, if we could not allocate memory */
1927 m_freem(m);
1928 return(0);
1929 }
1930 if (rt->rt_gateway->sa_family != AF_LINK) {
1931 printf("nd6_storelladdr: something odd happens\n");
1932 m_freem(m);
1933 return(0);
1934 }
1935 sdl = SDL(rt->rt_gateway);
1936 if (sdl->sdl_alen == 0) {
1937 /* this should be impossible, but we bark here for debugging */
1938 printf("nd6_storelladdr: sdl_alen == 0, dst=%s, if=%s\n",
1939 ip6_sprintf(&SIN6(dst)->sin6_addr), if_name(ifp));
1940 m_freem(m);
1941 return(0);
1942 }
1943
1944 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1945 return(1);
1946 }
1947
1948 int
1949 nd6_sysctl(name, oldp, oldlenp, newp, newlen)
1950 int name;
1951 void *oldp; /* syscall arg, need copyout */
1952 size_t *oldlenp;
1953 void *newp; /* syscall arg, need in */
1954 size_t newlen;
1955 {
1956 void *p;
1957 size_t ol, l;
1958 int error;
1959
1960 error = 0;
1961 l = 0;
1962
1963 if (newp)
1964 return EPERM;
1965 if (oldp && !oldlenp)
1966 return EINVAL;
1967 ol = oldlenp ? *oldlenp : 0;
1968
1969 if (oldp) {
1970 p = malloc(*oldlenp, M_TEMP, M_WAITOK);
1971 if (!p)
1972 return ENOMEM;
1973 } else
1974 p = NULL;
1975 switch (name) {
1976 case ICMPV6CTL_ND6_DRLIST:
1977 error = fill_drlist(p, oldlenp, ol);
1978 if (!error && p && oldp)
1979 copyout(p, oldp, *oldlenp);
1980 break;
1981
1982 case ICMPV6CTL_ND6_PRLIST:
1983 error = fill_prlist(p, oldlenp, ol);
1984 if (!error && p && oldp)
1985 copyout(p, oldp, *oldlenp);
1986 break;
1987
1988 default:
1989 error = ENOPROTOOPT;
1990 break;
1991 }
1992 if (p)
1993 free(p, M_TEMP);
1994
1995 return(error);
1996 }
1997
1998 static int
1999 fill_drlist(oldp, oldlenp, ol)
2000 void *oldp;
2001 size_t *oldlenp, ol;
2002 {
2003 int error = 0, s;
2004 struct in6_defrouter *d = NULL, *de = NULL;
2005 struct nd_defrouter *dr;
2006 size_t l;
2007
2008 s = splsoftnet();
2009
2010 if (oldp) {
2011 d = (struct in6_defrouter *)oldp;
2012 de = (struct in6_defrouter *)((caddr_t)oldp + *oldlenp);
2013 }
2014 l = 0;
2015
2016 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2017 dr = TAILQ_NEXT(dr, dr_entry)) {
2018
2019 if (oldp && d + 1 <= de) {
2020 bzero(d, sizeof(*d));
2021 d->rtaddr.sin6_family = AF_INET6;
2022 d->rtaddr.sin6_len = sizeof(struct sockaddr_in6);
2023 d->rtaddr.sin6_addr = dr->rtaddr;
2024 in6_recoverscope(&d->rtaddr, &d->rtaddr.sin6_addr,
2025 dr->ifp);
2026 d->flags = dr->flags;
2027 d->rtlifetime = dr->rtlifetime;
2028 d->expire = dr->expire;
2029 d->if_index = dr->ifp->if_index;
2030 }
2031
2032 l += sizeof(*d);
2033 if (d)
2034 d++;
2035 }
2036
2037 if (oldp) {
2038 *oldlenp = l; /* (caddr_t)d - (caddr_t)oldp */
2039 if (l > ol)
2040 error = ENOMEM;
2041 } else
2042 *oldlenp = l;
2043
2044 splx(s);
2045
2046 return(error);
2047 }
2048
2049 static int
2050 fill_prlist(oldp, oldlenp, ol)
2051 void *oldp;
2052 size_t *oldlenp, ol;
2053 {
2054 int error = 0, s;
2055 struct nd_prefix *pr;
2056 struct in6_prefix *p = NULL;
2057 struct in6_prefix *pe = NULL;
2058 size_t l;
2059
2060 s = splsoftnet();
2061
2062 if (oldp) {
2063 p = (struct in6_prefix *)oldp;
2064 pe = (struct in6_prefix *)((caddr_t)oldp + *oldlenp);
2065 }
2066 l = 0;
2067
2068 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2069 u_short advrtrs;
2070 size_t advance;
2071 struct sockaddr_in6 *sin6;
2072 struct sockaddr_in6 *s6;
2073 struct nd_pfxrouter *pfr;
2074
2075 if (oldp && p + 1 <= pe)
2076 {
2077 bzero(p, sizeof(*p));
2078 sin6 = (struct sockaddr_in6 *)(p + 1);
2079
2080 p->prefix = pr->ndpr_prefix;
2081 if (in6_recoverscope(&p->prefix,
2082 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2083 log(LOG_ERR,
2084 "scope error in prefix list (%s)\n",
2085 ip6_sprintf(&p->prefix.sin6_addr));
2086 p->raflags = pr->ndpr_raf;
2087 p->prefixlen = pr->ndpr_plen;
2088 p->vltime = pr->ndpr_vltime;
2089 p->pltime = pr->ndpr_pltime;
2090 p->if_index = pr->ndpr_ifp->if_index;
2091 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2092 p->expire = 0;
2093 else {
2094 time_t maxexpire;
2095
2096 /* XXX: we assume time_t is signed. */
2097 maxexpire = (-1) &
2098 ~(1 << ((sizeof(maxexpire) * 8) - 1));
2099 if (pr->ndpr_vltime <
2100 maxexpire - pr->ndpr_lastupdate) {
2101 p->expire = pr->ndpr_lastupdate +
2102 pr->ndpr_vltime;
2103 } else
2104 p->expire = maxexpire;
2105 }
2106 p->refcnt = pr->ndpr_refcnt;
2107 p->flags = pr->ndpr_stateflags;
2108 p->origin = PR_ORIG_RA;
2109 advrtrs = 0;
2110 for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2111 pfr = pfr->pfr_next) {
2112 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2113 advrtrs++;
2114 continue;
2115 }
2116 s6 = &sin6[advrtrs];
2117 s6->sin6_family = AF_INET6;
2118 s6->sin6_len = sizeof(struct sockaddr_in6);
2119 s6->sin6_addr = pfr->router->rtaddr;
2120 in6_recoverscope(s6, &s6->sin6_addr,
2121 pfr->router->ifp);
2122 advrtrs++;
2123 }
2124 p->advrtrs = advrtrs;
2125 }
2126 else {
2127 advrtrs = 0;
2128 for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2129 pfr = pfr->pfr_next)
2130 advrtrs++;
2131 }
2132
2133 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2134 l += advance;
2135 if (p)
2136 p = (struct in6_prefix *)((caddr_t)p + advance);
2137 }
2138
2139 if (oldp) {
2140 *oldlenp = l; /* (caddr_t)d - (caddr_t)oldp */
2141 if (l > ol)
2142 error = ENOMEM;
2143 } else
2144 *oldlenp = l;
2145
2146 splx(s);
2147
2148 return(error);
2149 }
2150