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