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