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