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