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