nd6.c revision 1.165 1 /* $NetBSD: nd6.c,v 1.165 2015/07/17 02:21:08 ozaki-r 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.165 2015/07/17 02:21:08 ozaki-r Exp $");
35
36 #include "opt_net_mpsafe.h"
37
38 #include "bridge.h"
39 #include "carp.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sockio.h>
49 #include <sys/time.h>
50 #include <sys/kernel.h>
51 #include <sys/protosw.h>
52 #include <sys/errno.h>
53 #include <sys/ioctl.h>
54 #include <sys/syslog.h>
55 #include <sys/queue.h>
56 #include <sys/cprng.h>
57
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/route.h>
62 #include <net/if_ether.h>
63 #include <net/if_fddi.h>
64 #include <net/if_arc.h>
65
66 #include <netinet/in.h>
67 #include <netinet6/in6_var.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/scope6_var.h>
71 #include <netinet6/nd6.h>
72 #include <netinet6/in6_ifattach.h>
73 #include <netinet/icmp6.h>
74 #include <netinet6/icmp6_private.h>
75
76 #include <net/net_osdep.h>
77
78 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
79 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
80
81 /* timer values */
82 int nd6_prune = 1; /* walk list every 1 seconds */
83 int nd6_delay = 5; /* delay first probe time 5 second */
84 int nd6_umaxtries = 3; /* maximum unicast query */
85 int nd6_mmaxtries = 3; /* maximum multicast query */
86 int nd6_useloopback = 1; /* use loopback interface for local traffic */
87 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
88
89 /* preventing too many loops in ND option parsing */
90 int nd6_maxndopt = 10; /* max # of ND options allowed */
91
92 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
93
94 int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
95
96 #ifdef ND6_DEBUG
97 int nd6_debug = 1;
98 #else
99 int nd6_debug = 0;
100 #endif
101
102 /* for debugging? */
103 static int nd6_inuse, nd6_allocated;
104
105 struct llinfo_nd6 llinfo_nd6 = {
106 .ln_prev = &llinfo_nd6,
107 .ln_next = &llinfo_nd6,
108 };
109 struct nd_drhead nd_defrouter;
110 struct nd_prhead nd_prefix = { 0 };
111
112 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
113 static const struct sockaddr_in6 all1_sa = {
114 .sin6_family = AF_INET6
115 , .sin6_len = sizeof(struct sockaddr_in6)
116 , .sin6_addr = {.s6_addr = {0xff, 0xff, 0xff, 0xff,
117 0xff, 0xff, 0xff, 0xff,
118 0xff, 0xff, 0xff, 0xff,
119 0xff, 0xff, 0xff, 0xff}}
120 };
121
122 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
123 static void nd6_slowtimo(void *);
124 static int regen_tmpaddr(struct in6_ifaddr *);
125 static struct llinfo_nd6 *nd6_free(struct rtentry *, int);
126 static void nd6_llinfo_timer(void *);
127 static void clear_llinfo_pqueue(struct llinfo_nd6 *);
128
129 callout_t nd6_slowtimo_ch;
130 callout_t nd6_timer_ch;
131 extern callout_t in6_tmpaddrtimer_ch;
132
133 static int fill_drlist(void *, size_t *, size_t);
134 static int fill_prlist(void *, size_t *, size_t);
135
136 MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery");
137
138 #define LN_DEQUEUE(ln) do { \
139 (ln)->ln_next->ln_prev = (ln)->ln_prev; \
140 (ln)->ln_prev->ln_next = (ln)->ln_next; \
141 } while (/*CONSTCOND*/0)
142 #define LN_INSERTHEAD(ln) do { \
143 (ln)->ln_next = llinfo_nd6.ln_next; \
144 llinfo_nd6.ln_next = (ln); \
145 (ln)->ln_prev = &llinfo_nd6; \
146 (ln)->ln_next->ln_prev = (ln); \
147 } while (/*CONSTCOND*/0)
148 void
149 nd6_init(void)
150 {
151 static int nd6_init_done = 0;
152
153 if (nd6_init_done) {
154 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
155 return;
156 }
157
158 /* initialization of the default router list */
159 TAILQ_INIT(&nd_defrouter);
160
161 nd6_init_done = 1;
162
163 callout_init(&nd6_slowtimo_ch, CALLOUT_MPSAFE);
164 callout_init(&nd6_timer_ch, CALLOUT_MPSAFE);
165
166 /* start timer */
167 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
168 nd6_slowtimo, NULL);
169 }
170
171 struct nd_ifinfo *
172 nd6_ifattach(struct ifnet *ifp)
173 {
174 struct nd_ifinfo *nd;
175
176 nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK|M_ZERO);
177
178 nd->initialized = 1;
179
180 nd->chlim = IPV6_DEFHLIM;
181 nd->basereachable = REACHABLE_TIME;
182 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
183 nd->retrans = RETRANS_TIMER;
184
185 nd->flags = ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV;
186
187 /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
188 * A bridge interface should not have ND6_IFF_AUTO_LINKLOCAL
189 * because one of its members should. */
190 if ((ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
191 (ifp->if_flags & IFF_LOOPBACK))
192 nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
193
194 /* A loopback interface does not need to accept RTADV.
195 * A bridge interface should not accept RTADV
196 * because one of its members should. */
197 if (ip6_accept_rtadv &&
198 !(ifp->if_flags & IFF_LOOPBACK) &&
199 !(ifp->if_type != IFT_BRIDGE))
200 nd->flags |= ND6_IFF_ACCEPT_RTADV;
201
202 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
203 nd6_setmtu0(ifp, nd);
204
205 return nd;
206 }
207
208 void
209 nd6_ifdetach(struct ifnet *ifp, struct in6_ifextra *ext)
210 {
211
212 nd6_purge(ifp, ext);
213 free(ext->nd_ifinfo, M_IP6NDP);
214 }
215
216 void
217 nd6_setmtu(struct ifnet *ifp)
218 {
219 nd6_setmtu0(ifp, ND_IFINFO(ifp));
220 }
221
222 void
223 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
224 {
225 u_int32_t omaxmtu;
226
227 omaxmtu = ndi->maxmtu;
228
229 switch (ifp->if_type) {
230 case IFT_ARCNET:
231 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
232 break;
233 case IFT_FDDI:
234 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
235 break;
236 default:
237 ndi->maxmtu = ifp->if_mtu;
238 break;
239 }
240
241 /*
242 * Decreasing the interface MTU under IPV6 minimum MTU may cause
243 * undesirable situation. We thus notify the operator of the change
244 * explicitly. The check for omaxmtu is necessary to restrict the
245 * log to the case of changing the MTU, not initializing it.
246 */
247 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
248 log(LOG_NOTICE, "nd6_setmtu0: new link MTU on %s (%lu) is too"
249 " small for IPv6 which needs %lu\n",
250 if_name(ifp), (unsigned long)ndi->maxmtu, (unsigned long)
251 IPV6_MMTU);
252 }
253
254 if (ndi->maxmtu > in6_maxmtu)
255 in6_setmaxmtu(); /* check all interfaces just in case */
256 }
257
258 void
259 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
260 {
261
262 memset(ndopts, 0, sizeof(*ndopts));
263 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
264 ndopts->nd_opts_last
265 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
266
267 if (icmp6len == 0) {
268 ndopts->nd_opts_done = 1;
269 ndopts->nd_opts_search = NULL;
270 }
271 }
272
273 /*
274 * Take one ND option.
275 */
276 struct nd_opt_hdr *
277 nd6_option(union nd_opts *ndopts)
278 {
279 struct nd_opt_hdr *nd_opt;
280 int olen;
281
282 KASSERT(ndopts != NULL);
283 KASSERT(ndopts->nd_opts_last != NULL);
284
285 if (ndopts->nd_opts_search == NULL)
286 return NULL;
287 if (ndopts->nd_opts_done)
288 return NULL;
289
290 nd_opt = ndopts->nd_opts_search;
291
292 /* make sure nd_opt_len is inside the buffer */
293 if ((void *)&nd_opt->nd_opt_len >= (void *)ndopts->nd_opts_last) {
294 memset(ndopts, 0, sizeof(*ndopts));
295 return NULL;
296 }
297
298 olen = nd_opt->nd_opt_len << 3;
299 if (olen == 0) {
300 /*
301 * Message validation requires that all included
302 * options have a length that is greater than zero.
303 */
304 memset(ndopts, 0, sizeof(*ndopts));
305 return NULL;
306 }
307
308 ndopts->nd_opts_search = (struct nd_opt_hdr *)((char *)nd_opt + olen);
309 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
310 /* option overruns the end of buffer, invalid */
311 memset(ndopts, 0, sizeof(*ndopts));
312 return NULL;
313 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
314 /* reached the end of options chain */
315 ndopts->nd_opts_done = 1;
316 ndopts->nd_opts_search = NULL;
317 }
318 return nd_opt;
319 }
320
321 /*
322 * Parse multiple ND options.
323 * This function is much easier to use, for ND routines that do not need
324 * multiple options of the same type.
325 */
326 int
327 nd6_options(union nd_opts *ndopts)
328 {
329 struct nd_opt_hdr *nd_opt;
330 int i = 0;
331
332 KASSERT(ndopts != NULL);
333 KASSERT(ndopts->nd_opts_last != NULL);
334
335 if (ndopts->nd_opts_search == NULL)
336 return 0;
337
338 while (1) {
339 nd_opt = nd6_option(ndopts);
340 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
341 /*
342 * Message validation requires that all included
343 * options have a length that is greater than zero.
344 */
345 ICMP6_STATINC(ICMP6_STAT_ND_BADOPT);
346 memset(ndopts, 0, sizeof(*ndopts));
347 return -1;
348 }
349
350 if (nd_opt == NULL)
351 goto skip1;
352
353 switch (nd_opt->nd_opt_type) {
354 case ND_OPT_SOURCE_LINKADDR:
355 case ND_OPT_TARGET_LINKADDR:
356 case ND_OPT_MTU:
357 case ND_OPT_REDIRECTED_HEADER:
358 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
359 nd6log((LOG_INFO,
360 "duplicated ND6 option found (type=%d)\n",
361 nd_opt->nd_opt_type));
362 /* XXX bark? */
363 } else {
364 ndopts->nd_opt_array[nd_opt->nd_opt_type]
365 = nd_opt;
366 }
367 break;
368 case ND_OPT_PREFIX_INFORMATION:
369 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
370 ndopts->nd_opt_array[nd_opt->nd_opt_type]
371 = nd_opt;
372 }
373 ndopts->nd_opts_pi_end =
374 (struct nd_opt_prefix_info *)nd_opt;
375 break;
376 default:
377 /*
378 * Unknown options must be silently ignored,
379 * to accommodate future extension to the protocol.
380 */
381 nd6log((LOG_DEBUG,
382 "nd6_options: unsupported option %d - "
383 "option ignored\n", nd_opt->nd_opt_type));
384 }
385
386 skip1:
387 i++;
388 if (i > nd6_maxndopt) {
389 ICMP6_STATINC(ICMP6_STAT_ND_TOOMANYOPT);
390 nd6log((LOG_INFO, "too many loop in nd opt\n"));
391 break;
392 }
393
394 if (ndopts->nd_opts_done)
395 break;
396 }
397
398 return 0;
399 }
400
401 /*
402 * ND6 timer routine to handle ND6 entries
403 */
404 void
405 nd6_llinfo_settimer(struct llinfo_nd6 *ln, long xtick)
406 {
407 int s;
408
409 s = splsoftnet();
410
411 if (xtick < 0) {
412 ln->ln_expire = 0;
413 ln->ln_ntick = 0;
414 callout_stop(&ln->ln_timer_ch);
415 } else {
416 ln->ln_expire = time_second + xtick / hz;
417 if (xtick > INT_MAX) {
418 ln->ln_ntick = xtick - INT_MAX;
419 callout_reset(&ln->ln_timer_ch, INT_MAX,
420 nd6_llinfo_timer, ln);
421 } else {
422 ln->ln_ntick = 0;
423 callout_reset(&ln->ln_timer_ch, xtick,
424 nd6_llinfo_timer, ln);
425 }
426 }
427
428 splx(s);
429 }
430
431 static void
432 nd6_llinfo_timer(void *arg)
433 {
434 struct llinfo_nd6 *ln;
435 struct rtentry *rt;
436 const struct sockaddr_in6 *dst;
437 struct ifnet *ifp;
438 struct nd_ifinfo *ndi = NULL;
439
440 mutex_enter(softnet_lock);
441 KERNEL_LOCK(1, NULL);
442
443 ln = (struct llinfo_nd6 *)arg;
444
445 if (ln->ln_ntick > 0) {
446 nd6_llinfo_settimer(ln, ln->ln_ntick);
447 KERNEL_UNLOCK_ONE(NULL);
448 mutex_exit(softnet_lock);
449 return;
450 }
451
452 if ((rt = ln->ln_rt) == NULL)
453 panic("ln->ln_rt == NULL");
454 if ((ifp = rt->rt_ifp) == NULL)
455 panic("ln->ln_rt->rt_ifp == NULL");
456 ndi = ND_IFINFO(ifp);
457 dst = satocsin6(rt_getkey(rt));
458
459 /* sanity check */
460 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
461 panic("rt_llinfo(%p) is not equal to ln(%p)",
462 rt->rt_llinfo, ln);
463 if (!dst)
464 panic("dst=0 in nd6_timer(ln=%p)", ln);
465
466 switch (ln->ln_state) {
467 case ND6_LLINFO_INCOMPLETE:
468 if (ln->ln_asked < nd6_mmaxtries) {
469 ln->ln_asked++;
470 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
471 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
472 } else {
473 struct mbuf *m = ln->ln_hold;
474 if (m) {
475 struct mbuf *m0;
476
477 /*
478 * assuming every packet in ln_hold has
479 * the same IP header
480 */
481 m0 = m->m_nextpkt;
482 m->m_nextpkt = NULL;
483 icmp6_error2(m, ICMP6_DST_UNREACH,
484 ICMP6_DST_UNREACH_ADDR, 0, rt->rt_ifp);
485
486 ln->ln_hold = m0;
487 clear_llinfo_pqueue(ln);
488 }
489 (void)nd6_free(rt, 0);
490 ln = NULL;
491 }
492 break;
493 case ND6_LLINFO_REACHABLE:
494 if (!ND6_LLINFO_PERMANENT(ln)) {
495 ln->ln_state = ND6_LLINFO_STALE;
496 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
497 }
498 break;
499
500 case ND6_LLINFO_PURGE:
501 case ND6_LLINFO_STALE:
502 /* Garbage Collection(RFC 2461 5.3) */
503 if (!ND6_LLINFO_PERMANENT(ln)) {
504 (void)nd6_free(rt, 1);
505 ln = NULL;
506 }
507 break;
508
509 case ND6_LLINFO_DELAY:
510 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
511 /* We need NUD */
512 ln->ln_asked = 1;
513 ln->ln_state = ND6_LLINFO_PROBE;
514 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
515 nd6_ns_output(ifp, &dst->sin6_addr,
516 &dst->sin6_addr, ln, 0);
517 } else {
518 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
519 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
520 }
521 break;
522 case ND6_LLINFO_PROBE:
523 if (ln->ln_asked < nd6_umaxtries) {
524 ln->ln_asked++;
525 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
526 nd6_ns_output(ifp, &dst->sin6_addr,
527 &dst->sin6_addr, ln, 0);
528 } else {
529 (void)nd6_free(rt, 0);
530 ln = NULL;
531 }
532 break;
533 }
534
535 KERNEL_UNLOCK_ONE(NULL);
536 mutex_exit(softnet_lock);
537 }
538
539 /*
540 * ND6 timer routine to expire default route list and prefix list
541 */
542 void
543 nd6_timer(void *ignored_arg)
544 {
545 struct nd_defrouter *next_dr, *dr;
546 struct nd_prefix *next_pr, *pr;
547 struct in6_ifaddr *ia6, *nia6;
548
549 callout_reset(&nd6_timer_ch, nd6_prune * hz,
550 nd6_timer, NULL);
551
552 mutex_enter(softnet_lock);
553 KERNEL_LOCK(1, NULL);
554
555 /* expire default router list */
556
557 TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, next_dr) {
558 if (dr->expire && dr->expire < time_second) {
559 defrtrlist_del(dr, NULL);
560 }
561 }
562
563 /*
564 * expire interface addresses.
565 * in the past the loop was inside prefix expiry processing.
566 * However, from a stricter speci-confrmance standpoint, we should
567 * rather separate address lifetimes and prefix lifetimes.
568 */
569 addrloop:
570 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
571 nia6 = ia6->ia_next;
572 /* check address lifetime */
573 if (IFA6_IS_INVALID(ia6)) {
574 int regen = 0;
575
576 /*
577 * If the expiring address is temporary, try
578 * regenerating a new one. This would be useful when
579 * we suspended a laptop PC, then turned it on after a
580 * period that could invalidate all temporary
581 * addresses. Although we may have to restart the
582 * loop (see below), it must be after purging the
583 * address. Otherwise, we'd see an infinite loop of
584 * regeneration.
585 */
586 if (ip6_use_tempaddr &&
587 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
588 if (regen_tmpaddr(ia6) == 0)
589 regen = 1;
590 }
591
592 in6_purgeaddr(&ia6->ia_ifa);
593
594 if (regen)
595 goto addrloop; /* XXX: see below */
596 } else if (IFA6_IS_DEPRECATED(ia6)) {
597 int oldflags = ia6->ia6_flags;
598
599 if ((oldflags & IN6_IFF_DEPRECATED) == 0) {
600 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
601 rt_newaddrmsg(RTM_NEWADDR,
602 (struct ifaddr *)ia6, 0, NULL);
603 }
604
605 /*
606 * If a temporary address has just become deprecated,
607 * regenerate a new one if possible.
608 */
609 if (ip6_use_tempaddr &&
610 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
611 (oldflags & IN6_IFF_DEPRECATED) == 0) {
612
613 if (regen_tmpaddr(ia6) == 0) {
614 /*
615 * A new temporary address is
616 * generated.
617 * XXX: this means the address chain
618 * has changed while we are still in
619 * the loop. Although the change
620 * would not cause disaster (because
621 * it's not a deletion, but an
622 * addition,) we'd rather restart the
623 * loop just for safety. Or does this
624 * significantly reduce performance??
625 */
626 goto addrloop;
627 }
628 }
629 } else {
630 /*
631 * A new RA might have made a deprecated address
632 * preferred.
633 */
634 if (ia6->ia6_flags & IN6_IFF_DEPRECATED) {
635 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
636 rt_newaddrmsg(RTM_NEWADDR,
637 (struct ifaddr *)ia6, 0, NULL);
638 }
639 }
640 }
641
642 /* expire prefix list */
643 LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, next_pr) {
644 /*
645 * check prefix lifetime.
646 * since pltime is just for autoconf, pltime processing for
647 * prefix is not necessary.
648 */
649 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
650 time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
651
652 /*
653 * address expiration and prefix expiration are
654 * separate. NEVER perform in6_purgeaddr here.
655 */
656
657 prelist_remove(pr);
658 }
659 }
660
661 KERNEL_UNLOCK_ONE(NULL);
662 mutex_exit(softnet_lock);
663 }
664
665 /* ia6: deprecated/invalidated temporary address */
666 static int
667 regen_tmpaddr(struct in6_ifaddr *ia6)
668 {
669 struct ifaddr *ifa;
670 struct ifnet *ifp;
671 struct in6_ifaddr *public_ifa6 = NULL;
672
673 ifp = ia6->ia_ifa.ifa_ifp;
674 IFADDR_FOREACH(ifa, ifp) {
675 struct in6_ifaddr *it6;
676
677 if (ifa->ifa_addr->sa_family != AF_INET6)
678 continue;
679
680 it6 = (struct in6_ifaddr *)ifa;
681
682 /* ignore no autoconf addresses. */
683 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
684 continue;
685
686 /* ignore autoconf addresses with different prefixes. */
687 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
688 continue;
689
690 /*
691 * Now we are looking at an autoconf address with the same
692 * prefix as ours. If the address is temporary and is still
693 * preferred, do not create another one. It would be rare, but
694 * could happen, for example, when we resume a laptop PC after
695 * a long period.
696 */
697 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
698 !IFA6_IS_DEPRECATED(it6)) {
699 public_ifa6 = NULL;
700 break;
701 }
702
703 /*
704 * This is a public autoconf address that has the same prefix
705 * as ours. If it is preferred, keep it. We can't break the
706 * loop here, because there may be a still-preferred temporary
707 * address with the prefix.
708 */
709 if (!IFA6_IS_DEPRECATED(it6))
710 public_ifa6 = it6;
711 }
712
713 if (public_ifa6 != NULL) {
714 int e;
715
716 /*
717 * Random factor is introduced in the preferred lifetime, so
718 * we do not need additional delay (3rd arg to in6_tmpifadd).
719 */
720 if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
721 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
722 " tmp addr, errno=%d\n", e);
723 return -1;
724 }
725 return 0;
726 }
727
728 return -1;
729 }
730
731 bool
732 nd6_accepts_rtadv(const struct nd_ifinfo *ndi)
733 {
734 switch (ndi->flags & (ND6_IFF_ACCEPT_RTADV|ND6_IFF_OVERRIDE_RTADV)) {
735 case ND6_IFF_OVERRIDE_RTADV|ND6_IFF_ACCEPT_RTADV:
736 return true;
737 case ND6_IFF_ACCEPT_RTADV:
738 return ip6_accept_rtadv != 0;
739 case ND6_IFF_OVERRIDE_RTADV:
740 case 0:
741 default:
742 return false;
743 }
744 }
745
746 /*
747 * Nuke neighbor cache/prefix/default router management table, right before
748 * ifp goes away.
749 */
750 void
751 nd6_purge(struct ifnet *ifp, struct in6_ifextra *ext)
752 {
753 struct llinfo_nd6 *ln, *nln;
754 struct nd_defrouter *dr, *ndr;
755 struct nd_prefix *pr, *npr;
756
757 /*
758 * During detach, the ND info might be already removed, but
759 * then is explitly passed as argument.
760 * Otherwise get it from ifp->if_afdata.
761 */
762 if (ext == NULL)
763 ext = ifp->if_afdata[AF_INET6];
764 if (ext == NULL)
765 return;
766
767 /*
768 * Nuke default router list entries toward ifp.
769 * We defer removal of default router list entries that is installed
770 * in the routing table, in order to keep additional side effects as
771 * small as possible.
772 */
773 TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
774 if (dr->installed)
775 continue;
776
777 if (dr->ifp == ifp) {
778 KASSERT(ext != NULL);
779 defrtrlist_del(dr, ext);
780 }
781 }
782
783 TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
784 if (!dr->installed)
785 continue;
786
787 if (dr->ifp == ifp) {
788 KASSERT(ext != NULL);
789 defrtrlist_del(dr, ext);
790 }
791 }
792
793 /* Nuke prefix list entries toward ifp */
794 LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
795 if (pr->ndpr_ifp == ifp) {
796 /*
797 * Because if_detach() does *not* release prefixes
798 * while purging addresses the reference count will
799 * still be above zero. We therefore reset it to
800 * make sure that the prefix really gets purged.
801 */
802 pr->ndpr_refcnt = 0;
803 /*
804 * Previously, pr->ndpr_addr is removed as well,
805 * but I strongly believe we don't have to do it.
806 * nd6_purge() is only called from in6_ifdetach(),
807 * which removes all the associated interface addresses
808 * by itself.
809 * (jinmei (at) kame.net 20010129)
810 */
811 prelist_remove(pr);
812 }
813 }
814
815 /* cancel default outgoing interface setting */
816 if (nd6_defifindex == ifp->if_index)
817 nd6_setdefaultiface(0);
818
819 /* XXX: too restrictive? */
820 if (!ip6_forwarding && ifp->if_afdata[AF_INET6]) {
821 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
822 if (ndi && nd6_accepts_rtadv(ndi)) {
823 /* refresh default router list */
824 defrouter_select();
825 }
826 }
827
828 /*
829 * Nuke neighbor cache entries for the ifp.
830 * Note that rt->rt_ifp may not be the same as ifp,
831 * due to KAME goto ours hack. See RTM_RESOLVE case in
832 * nd6_rtrequest(), and ip6_input().
833 */
834 ln = llinfo_nd6.ln_next;
835 while (ln != NULL && ln != &llinfo_nd6) {
836 struct rtentry *rt;
837 const struct sockaddr_dl *sdl;
838
839 nln = ln->ln_next;
840 rt = ln->ln_rt;
841 if (rt && rt->rt_gateway &&
842 rt->rt_gateway->sa_family == AF_LINK) {
843 sdl = satocsdl(rt->rt_gateway);
844 if (sdl->sdl_index == ifp->if_index)
845 nln = nd6_free(rt, 0);
846 }
847 ln = nln;
848 }
849 }
850
851 static struct rtentry *
852 nd6_lookup1(const struct in6_addr *addr6, int create, struct ifnet *ifp,
853 int cloning)
854 {
855 struct rtentry *rt;
856 struct sockaddr_in6 sin6;
857
858 sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
859 rt = rtalloc1((struct sockaddr *)&sin6, create);
860 if (rt != NULL && (rt->rt_flags & RTF_LLINFO) == 0) {
861 /*
862 * This is the case for the default route.
863 * If we want to create a neighbor cache for the address, we
864 * should free the route for the destination and allocate an
865 * interface route.
866 */
867 if (create) {
868 rtfree(rt);
869 rt = NULL;
870 }
871 }
872 if (rt != NULL)
873 ;
874 else if (create && ifp) {
875 int e;
876
877 /*
878 * If no route is available and create is set,
879 * we allocate a host route for the destination
880 * and treat it like an interface route.
881 * This hack is necessary for a neighbor which can't
882 * be covered by our own prefix.
883 */
884 struct ifaddr *ifa =
885 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
886 if (ifa == NULL)
887 return NULL;
888
889 /*
890 * Create a new route. RTF_LLINFO is necessary
891 * to create a Neighbor Cache entry for the
892 * destination in nd6_rtrequest which will be
893 * called in rtrequest via ifa->ifa_rtrequest.
894 */
895 if ((e = rtrequest(RTM_ADD, (const struct sockaddr *)&sin6,
896 ifa->ifa_addr, (const struct sockaddr *)&all1_sa,
897 (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
898 ~RTF_CLONING, &rt)) != 0) {
899 #if 0
900 log(LOG_ERR,
901 "nd6_lookup: failed to add route for a "
902 "neighbor(%s), errno=%d\n",
903 ip6_sprintf(addr6), e);
904 #endif
905 return NULL;
906 }
907 if (rt == NULL)
908 return NULL;
909 if (rt->rt_llinfo) {
910 struct llinfo_nd6 *ln =
911 (struct llinfo_nd6 *)rt->rt_llinfo;
912 ln->ln_state = ND6_LLINFO_NOSTATE;
913 }
914 } else
915 return NULL;
916
917 /*
918 * Check for a cloning route to match the address.
919 * This should only be set from in6_is_addr_neighbor so we avoid
920 * a potentially expensive second call to rtalloc1.
921 */
922 if (cloning &&
923 rt->rt_flags & (RTF_CLONING | RTF_CLONED) &&
924 (rt->rt_ifp == ifp
925 #if NBRIDGE > 0
926 || rt->rt_ifp->if_bridge == ifp->if_bridge
927 #endif
928 #if NCARP > 0
929 || (ifp->if_type == IFT_CARP && rt->rt_ifp == ifp->if_carpdev) ||
930 (rt->rt_ifp->if_type == IFT_CARP && rt->rt_ifp->if_carpdev == ifp)||
931 (ifp->if_type == IFT_CARP && rt->rt_ifp->if_type == IFT_CARP &&
932 rt->rt_ifp->if_carpdev == ifp->if_carpdev)
933 #endif
934 ))
935 return rt;
936
937 /*
938 * Validation for the entry.
939 * Note that the check for rt_llinfo is necessary because a cloned
940 * route from a parent route that has the L flag (e.g. the default
941 * route to a p2p interface) may have the flag, too, while the
942 * destination is not actually a neighbor.
943 * XXX: we can't use rt->rt_ifp to check for the interface, since
944 * it might be the loopback interface if the entry is for our
945 * own address on a non-loopback interface. Instead, we should
946 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
947 * interface.
948 * Note also that ifa_ifp and ifp may differ when we connect two
949 * interfaces to a same link, install a link prefix to an interface,
950 * and try to install a neighbor cache on an interface that does not
951 * have a route to the prefix.
952 */
953 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
954 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
955 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
956 if (create) {
957 nd6log((LOG_DEBUG,
958 "nd6_lookup: failed to lookup %s (if = %s)\n",
959 ip6_sprintf(addr6),
960 ifp ? if_name(ifp) : "unspec"));
961 }
962 return NULL;
963 }
964 return rt;
965 }
966
967 struct rtentry *
968 nd6_lookup(const struct in6_addr *addr6, int create, struct ifnet *ifp)
969 {
970
971 return nd6_lookup1(addr6, create, ifp, 0);
972 }
973
974 /*
975 * Detect if a given IPv6 address identifies a neighbor on a given link.
976 * XXX: should take care of the destination of a p2p link?
977 */
978 int
979 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
980 {
981 struct nd_prefix *pr;
982 struct rtentry *rt;
983
984 /*
985 * A link-local address is always a neighbor.
986 * XXX: a link does not necessarily specify a single interface.
987 */
988 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
989 struct sockaddr_in6 sin6_copy;
990 u_int32_t zone;
991
992 /*
993 * We need sin6_copy since sa6_recoverscope() may modify the
994 * content (XXX).
995 */
996 sin6_copy = *addr;
997 if (sa6_recoverscope(&sin6_copy))
998 return 0; /* XXX: should be impossible */
999 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
1000 return 0;
1001 if (sin6_copy.sin6_scope_id == zone)
1002 return 1;
1003 else
1004 return 0;
1005 }
1006
1007 /*
1008 * If the address matches one of our on-link prefixes, it should be a
1009 * neighbor.
1010 */
1011 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1012 if (pr->ndpr_ifp != ifp)
1013 continue;
1014
1015 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
1016 continue;
1017
1018 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1019 &addr->sin6_addr, &pr->ndpr_mask))
1020 return 1;
1021 }
1022
1023 /*
1024 * If the default router list is empty, all addresses are regarded
1025 * as on-link, and thus, as a neighbor.
1026 * XXX: we restrict the condition to hosts, because routers usually do
1027 * not have the "default router list".
1028 */
1029 if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
1030 nd6_defifindex == ifp->if_index) {
1031 return 1;
1032 }
1033
1034 /*
1035 * Even if the address matches none of our addresses, it might match
1036 * a cloning route or be in the neighbor cache.
1037 */
1038 rt = nd6_lookup1(&addr->sin6_addr, 0, ifp, 1);
1039 if (rt != NULL) {
1040 rtfree(rt);
1041 return 1;
1042 }
1043
1044 return 0;
1045 }
1046
1047 /*
1048 * Free an nd6 llinfo entry.
1049 * Since the function would cause significant changes in the kernel, DO NOT
1050 * make it global, unless you have a strong reason for the change, and are sure
1051 * that the change is safe.
1052 */
1053 static struct llinfo_nd6 *
1054 nd6_free(struct rtentry *rt, int gc)
1055 {
1056 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
1057 struct in6_addr in6 = satocsin6(rt_getkey(rt))->sin6_addr;
1058 struct nd_defrouter *dr;
1059 int error;
1060
1061 /*
1062 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1063 * even though it is not harmful, it was not really necessary.
1064 */
1065
1066 /* cancel timer */
1067 nd6_llinfo_settimer(ln, -1);
1068
1069 if (!ip6_forwarding) {
1070 int s;
1071 s = splsoftnet();
1072 dr = defrouter_lookup(&satocsin6(rt_getkey(rt))->sin6_addr,
1073 rt->rt_ifp);
1074
1075 if (dr != NULL && dr->expire &&
1076 ln->ln_state == ND6_LLINFO_STALE && gc) {
1077 /*
1078 * If the reason for the deletion is just garbage
1079 * collection, and the neighbor is an active default
1080 * router, do not delete it. Instead, reset the GC
1081 * timer using the router's lifetime.
1082 * Simply deleting the entry would affect default
1083 * router selection, which is not necessarily a good
1084 * thing, especially when we're using router preference
1085 * values.
1086 * XXX: the check for ln_state would be redundant,
1087 * but we intentionally keep it just in case.
1088 */
1089 if (dr->expire > time_second)
1090 nd6_llinfo_settimer(ln,
1091 (dr->expire - time_second) * hz);
1092 else
1093 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1094 splx(s);
1095 return ln->ln_next;
1096 }
1097
1098 if (ln->ln_router || dr) {
1099 /*
1100 * rt6_flush must be called whether or not the neighbor
1101 * is in the Default Router List.
1102 * See a corresponding comment in nd6_na_input().
1103 */
1104 rt6_flush(&in6, rt->rt_ifp);
1105 }
1106
1107 if (dr) {
1108 /*
1109 * Unreachablity of a router might affect the default
1110 * router selection and on-link detection of advertised
1111 * prefixes.
1112 */
1113
1114 /*
1115 * Temporarily fake the state to choose a new default
1116 * router and to perform on-link determination of
1117 * prefixes correctly.
1118 * Below the state will be set correctly,
1119 * or the entry itself will be deleted.
1120 */
1121 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1122
1123 /*
1124 * Since defrouter_select() does not affect the
1125 * on-link determination and MIP6 needs the check
1126 * before the default router selection, we perform
1127 * the check now.
1128 */
1129 pfxlist_onlink_check();
1130
1131 /*
1132 * refresh default router list
1133 */
1134 defrouter_select();
1135 }
1136 splx(s);
1137 }
1138
1139 /*
1140 * Before deleting the entry, remember the next entry as the
1141 * return value. We need this because pfxlist_onlink_check() above
1142 * might have freed other entries (particularly the old next entry) as
1143 * a side effect (XXX).
1144 */
1145 next = ln->ln_next;
1146
1147 /*
1148 * Detach the route from the routing tree and the list of neighbor
1149 * caches, and disable the route entry not to be used in already
1150 * cached routes.
1151 */
1152 error = rtrequest_newmsg(RTM_DELETE, rt_getkey(rt), NULL,
1153 rt_mask(rt), 0);
1154 if (error != 0) {
1155 /* XXX need error message? */;
1156 }
1157
1158 return next;
1159 }
1160
1161 /*
1162 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1163 *
1164 * XXX cost-effective methods?
1165 */
1166 void
1167 nd6_nud_hint(struct rtentry *rt0)
1168 {
1169 struct llinfo_nd6 *ln;
1170 struct rtentry *rt = rt0;
1171
1172 if (rt == NULL)
1173 return;
1174
1175 if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
1176 (rt->rt_flags & RTF_LLINFO) == 0 ||
1177 !rt->rt_llinfo || !rt->rt_gateway ||
1178 rt->rt_gateway->sa_family != AF_LINK) {
1179 /* This is not a host route. */
1180 goto exit;
1181 }
1182
1183 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1184 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1185 goto exit;
1186
1187 /*
1188 * if we get upper-layer reachability confirmation many times,
1189 * it is possible we have false information.
1190 */
1191 ln->ln_byhint++;
1192 if (ln->ln_byhint > nd6_maxnudhint)
1193 goto exit;
1194
1195 ln->ln_state = ND6_LLINFO_REACHABLE;
1196 if (!ND6_LLINFO_PERMANENT(ln)) {
1197 nd6_llinfo_settimer(ln,
1198 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1199 }
1200 exit:
1201 if (rt != rt0)
1202 rtfree(rt);
1203 return;
1204 }
1205
1206 void
1207 nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
1208 {
1209 struct sockaddr *gate = rt->rt_gateway;
1210 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1211 struct ifnet *ifp = rt->rt_ifp;
1212 uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen;
1213 struct ifaddr *ifa;
1214
1215 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1216
1217 if (req == RTM_LLINFO_UPD) {
1218 int rc;
1219 struct in6_addr *in6;
1220 struct in6_addr in6_all;
1221 int anycast;
1222
1223 if ((ifa = info->rti_ifa) == NULL)
1224 return;
1225
1226 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1227 anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST;
1228
1229 in6_all = in6addr_linklocal_allnodes;
1230 if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) {
1231 log(LOG_ERR, "%s: failed to set scope %s "
1232 "(errno=%d)\n", __func__, if_name(ifp), rc);
1233 return;
1234 }
1235
1236 /* XXX don't set Override for proxy addresses */
1237 nd6_na_output(ifa->ifa_ifp, &in6_all, in6,
1238 (anycast ? 0 : ND_NA_FLAG_OVERRIDE)
1239 #if 0
1240 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
1241 #endif
1242 , 1, NULL);
1243 return;
1244 }
1245
1246 if ((rt->rt_flags & RTF_GATEWAY) != 0)
1247 return;
1248
1249 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1250 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1251 /*
1252 * This is probably an interface direct route for a link
1253 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1254 * We do not need special treatment below for such a route.
1255 * Moreover, the RTF_LLINFO flag which would be set below
1256 * would annoy the ndp(8) command.
1257 */
1258 return;
1259 }
1260
1261 if (req == RTM_RESOLVE &&
1262 (nd6_need_cache(ifp) == 0 || /* stf case */
1263 !nd6_is_addr_neighbor(satocsin6(rt_getkey(rt)), ifp))) {
1264 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1265 /*
1266 * FreeBSD and BSD/OS often make a cloned host route based
1267 * on a less-specific route (e.g. the default route).
1268 * If the less specific route does not have a "gateway"
1269 * (this is the case when the route just goes to a p2p or an
1270 * stf interface), we'll mistakenly make a neighbor cache for
1271 * the host route, and will see strange neighbor solicitation
1272 * for the corresponding destination. In order to avoid the
1273 * confusion, we check if the destination of the route is
1274 * a neighbor in terms of neighbor discovery, and stop the
1275 * process if not. Additionally, we remove the LLINFO flag
1276 * so that ndp(8) will not try to get the neighbor information
1277 * of the destination.
1278 */
1279 rt->rt_flags &= ~RTF_LLINFO;
1280 return;
1281 }
1282
1283 switch (req) {
1284 case RTM_ADD:
1285 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1286 /*
1287 * There is no backward compatibility :)
1288 *
1289 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1290 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1291 * rt->rt_flags |= RTF_CLONING;
1292 */
1293 if ((rt->rt_flags & RTF_CLONING) ||
1294 ((rt->rt_flags & RTF_LLINFO) && ln == NULL)) {
1295 union {
1296 struct sockaddr sa;
1297 struct sockaddr_dl sdl;
1298 struct sockaddr_storage ss;
1299 } u;
1300 /*
1301 * Case 1: This route should come from a route to
1302 * interface (RTF_CLONING case) or the route should be
1303 * treated as on-link but is currently not
1304 * (RTF_LLINFO && ln == NULL case).
1305 */
1306 if (sockaddr_dl_init(&u.sdl, sizeof(u.ss),
1307 ifp->if_index, ifp->if_type,
1308 NULL, namelen, NULL, addrlen) == NULL) {
1309 printf("%s.%d: sockaddr_dl_init(, %zu, ) "
1310 "failed on %s\n", __func__, __LINE__,
1311 sizeof(u.ss), if_name(ifp));
1312 }
1313 rt_setgate(rt, &u.sa);
1314 gate = rt->rt_gateway;
1315 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1316 if (ln != NULL)
1317 nd6_llinfo_settimer(ln, 0);
1318 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1319 if ((rt->rt_flags & RTF_CLONING) != 0)
1320 break;
1321 }
1322 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1323 /*
1324 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1325 * We don't do that here since llinfo is not ready yet.
1326 *
1327 * There are also couple of other things to be discussed:
1328 * - unsolicited NA code needs improvement beforehand
1329 * - RFC2461 says we MAY send multicast unsolicited NA
1330 * (7.2.6 paragraph 4), however, it also says that we
1331 * SHOULD provide a mechanism to prevent multicast NA storm.
1332 * we don't have anything like it right now.
1333 * note that the mechanism needs a mutual agreement
1334 * between proxies, which means that we need to implement
1335 * a new protocol, or a new kludge.
1336 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1337 * we need to check ip6forwarding before sending it.
1338 * (or should we allow proxy ND configuration only for
1339 * routers? there's no mention about proxy ND from hosts)
1340 */
1341 #if 0
1342 /* XXX it does not work */
1343 if (rt->rt_flags & RTF_ANNOUNCE)
1344 nd6_na_output(ifp,
1345 &satocsin6(rt_getkey(rt))->sin6_addr,
1346 &satocsin6(rt_getkey(rt))->sin6_addr,
1347 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1348 1, NULL);
1349 #endif
1350 /* FALLTHROUGH */
1351 case RTM_RESOLVE:
1352 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1353 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1354 /*
1355 * Address resolution isn't necessary for a point to
1356 * point link, so we can skip this test for a p2p link.
1357 */
1358 if (gate->sa_family != AF_LINK ||
1359 gate->sa_len <
1360 sockaddr_dl_measure(namelen, addrlen)) {
1361 log(LOG_DEBUG,
1362 "nd6_rtrequest: bad gateway value: %s\n",
1363 if_name(ifp));
1364 break;
1365 }
1366 satosdl(gate)->sdl_type = ifp->if_type;
1367 satosdl(gate)->sdl_index = ifp->if_index;
1368 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1369 }
1370 if (ln != NULL)
1371 break; /* This happens on a route change */
1372 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1373 /*
1374 * Case 2: This route may come from cloning, or a manual route
1375 * add with a LL address.
1376 */
1377 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1378 rt->rt_llinfo = ln;
1379 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1380 if (ln == NULL) {
1381 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1382 break;
1383 }
1384 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1385 nd6_inuse++;
1386 nd6_allocated++;
1387 memset(ln, 0, sizeof(*ln));
1388 ln->ln_rt = rt;
1389 callout_init(&ln->ln_timer_ch, CALLOUT_MPSAFE);
1390 /* this is required for "ndp" command. - shin */
1391 if (req == RTM_ADD) {
1392 /*
1393 * gate should have some valid AF_LINK entry,
1394 * and ln->ln_expire should have some lifetime
1395 * which is specified by ndp command.
1396 */
1397 ln->ln_state = ND6_LLINFO_REACHABLE;
1398 ln->ln_byhint = 0;
1399 } else {
1400 /*
1401 * When req == RTM_RESOLVE, rt is created and
1402 * initialized in rtrequest(), so rt_expire is 0.
1403 */
1404 ln->ln_state = ND6_LLINFO_NOSTATE;
1405 nd6_llinfo_settimer(ln, 0);
1406 }
1407 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1408 rt->rt_flags |= RTF_LLINFO;
1409 ln->ln_next = llinfo_nd6.ln_next;
1410 llinfo_nd6.ln_next = ln;
1411 ln->ln_prev = &llinfo_nd6;
1412 ln->ln_next->ln_prev = ln;
1413
1414 /*
1415 * If we have too many cache entries, initiate immediate
1416 * purging for some "less recently used" entries. Note that
1417 * we cannot directly call nd6_free() here because it would
1418 * cause re-entering rtable related routines triggering an LOR
1419 * problem for FreeBSD.
1420 */
1421 if (ip6_neighborgcthresh >= 0 &&
1422 nd6_inuse >= ip6_neighborgcthresh) {
1423 int i;
1424
1425 for (i = 0; i < 10 && llinfo_nd6.ln_prev != ln; i++) {
1426 struct llinfo_nd6 *ln_end = llinfo_nd6.ln_prev;
1427
1428 /* Move this entry to the head */
1429 LN_DEQUEUE(ln_end);
1430 LN_INSERTHEAD(ln_end);
1431
1432 if (ND6_LLINFO_PERMANENT(ln_end))
1433 continue;
1434
1435 if (ln_end->ln_state > ND6_LLINFO_INCOMPLETE)
1436 ln_end->ln_state = ND6_LLINFO_STALE;
1437 else
1438 ln_end->ln_state = ND6_LLINFO_PURGE;
1439 nd6_llinfo_settimer(ln_end, 0);
1440 }
1441 }
1442
1443 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1444 /*
1445 * check if rt_getkey(rt) is an address assigned
1446 * to the interface.
1447 */
1448 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1449 &satocsin6(rt_getkey(rt))->sin6_addr);
1450 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1451 if (ifa != NULL) {
1452 const void *mac;
1453 nd6_llinfo_settimer(ln, -1);
1454 ln->ln_state = ND6_LLINFO_REACHABLE;
1455 ln->ln_byhint = 0;
1456 if ((mac = nd6_ifptomac(ifp)) != NULL) {
1457 /* XXX check for error */
1458 if (sockaddr_dl_setaddr(satosdl(gate),
1459 gate->sa_len, mac,
1460 ifp->if_addrlen) == NULL) {
1461 printf("%s.%d: "
1462 "sockaddr_dl_setaddr(, %d, ) "
1463 "failed on %s\n", __func__,
1464 __LINE__, gate->sa_len,
1465 if_name(ifp));
1466 }
1467 }
1468 if (nd6_useloopback) {
1469 ifp = rt->rt_ifp = lo0ifp; /* XXX */
1470 /*
1471 * Make sure rt_ifa be equal to the ifaddr
1472 * corresponding to the address.
1473 * We need this because when we refer
1474 * rt_ifa->ia6_flags in ip6_input, we assume
1475 * that the rt_ifa points to the address instead
1476 * of the loopback address.
1477 */
1478 if (ifa != rt->rt_ifa)
1479 rt_replace_ifa(rt, ifa);
1480 rt->rt_flags &= ~RTF_CLONED;
1481 }
1482 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1483 nd6_llinfo_settimer(ln, -1);
1484 ln->ln_state = ND6_LLINFO_REACHABLE;
1485 ln->ln_byhint = 0;
1486
1487 /* join solicited node multicast for proxy ND */
1488 if (ifp->if_flags & IFF_MULTICAST) {
1489 struct in6_addr llsol;
1490 int error;
1491
1492 llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1493 llsol.s6_addr32[0] = htonl(0xff020000);
1494 llsol.s6_addr32[1] = 0;
1495 llsol.s6_addr32[2] = htonl(1);
1496 llsol.s6_addr8[12] = 0xff;
1497 if (in6_setscope(&llsol, ifp, NULL))
1498 break;
1499 if (!in6_addmulti(&llsol, ifp, &error, 0)) {
1500 nd6log((LOG_ERR, "%s: failed to join "
1501 "%s (errno=%d)\n", if_name(ifp),
1502 ip6_sprintf(&llsol), error));
1503 }
1504 }
1505 }
1506 break;
1507
1508 case RTM_DELETE:
1509 if (ln == NULL)
1510 break;
1511 /* leave from solicited node multicast for proxy ND */
1512 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1513 (ifp->if_flags & IFF_MULTICAST) != 0) {
1514 struct in6_addr llsol;
1515 struct in6_multi *in6m;
1516
1517 llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1518 llsol.s6_addr32[0] = htonl(0xff020000);
1519 llsol.s6_addr32[1] = 0;
1520 llsol.s6_addr32[2] = htonl(1);
1521 llsol.s6_addr8[12] = 0xff;
1522 if (in6_setscope(&llsol, ifp, NULL) == 0) {
1523 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1524 if (in6m)
1525 in6_delmulti(in6m);
1526 }
1527 }
1528 nd6_inuse--;
1529 ln->ln_next->ln_prev = ln->ln_prev;
1530 ln->ln_prev->ln_next = ln->ln_next;
1531 ln->ln_prev = NULL;
1532 nd6_llinfo_settimer(ln, -1);
1533 rt->rt_llinfo = 0;
1534 rt->rt_flags &= ~RTF_LLINFO;
1535 clear_llinfo_pqueue(ln);
1536 Free(ln);
1537 }
1538 }
1539
1540 int
1541 nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp)
1542 {
1543 struct in6_drlist *drl = (struct in6_drlist *)data;
1544 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1545 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1546 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1547 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1548 struct nd_defrouter *dr;
1549 struct nd_prefix *pr;
1550 struct rtentry *rt;
1551 int i = 0, error = 0;
1552 int s;
1553
1554 switch (cmd) {
1555 case SIOCGDRLST_IN6:
1556 /*
1557 * obsolete API, use sysctl under net.inet6.icmp6
1558 */
1559 memset(drl, 0, sizeof(*drl));
1560 s = splsoftnet();
1561 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
1562 if (i >= DRLSTSIZ)
1563 break;
1564 drl->defrouter[i].rtaddr = dr->rtaddr;
1565 in6_clearscope(&drl->defrouter[i].rtaddr);
1566
1567 drl->defrouter[i].flags = dr->flags;
1568 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1569 drl->defrouter[i].expire = dr->expire;
1570 drl->defrouter[i].if_index = dr->ifp->if_index;
1571 i++;
1572 }
1573 splx(s);
1574 break;
1575 case SIOCGPRLST_IN6:
1576 /*
1577 * obsolete API, use sysctl under net.inet6.icmp6
1578 *
1579 * XXX the structure in6_prlist was changed in backward-
1580 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1581 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1582 */
1583 /*
1584 * XXX meaning of fields, especialy "raflags", is very
1585 * differnet between RA prefix list and RR/static prefix list.
1586 * how about separating ioctls into two?
1587 */
1588 memset(oprl, 0, sizeof(*oprl));
1589 s = splsoftnet();
1590 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1591 struct nd_pfxrouter *pfr;
1592 int j;
1593
1594 if (i >= PRLSTSIZ)
1595 break;
1596 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1597 oprl->prefix[i].raflags = pr->ndpr_raf;
1598 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1599 oprl->prefix[i].vltime = pr->ndpr_vltime;
1600 oprl->prefix[i].pltime = pr->ndpr_pltime;
1601 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1602 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1603 oprl->prefix[i].expire = 0;
1604 else {
1605 time_t maxexpire;
1606
1607 /* XXX: we assume time_t is signed. */
1608 maxexpire = (-1) &
1609 ~((time_t)1 <<
1610 ((sizeof(maxexpire) * 8) - 1));
1611 if (pr->ndpr_vltime <
1612 maxexpire - pr->ndpr_lastupdate) {
1613 oprl->prefix[i].expire =
1614 pr->ndpr_lastupdate +
1615 pr->ndpr_vltime;
1616 } else
1617 oprl->prefix[i].expire = maxexpire;
1618 }
1619
1620 j = 0;
1621 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1622 if (j < DRLSTSIZ) {
1623 #define RTRADDR oprl->prefix[i].advrtr[j]
1624 RTRADDR = pfr->router->rtaddr;
1625 in6_clearscope(&RTRADDR);
1626 #undef RTRADDR
1627 }
1628 j++;
1629 }
1630 oprl->prefix[i].advrtrs = j;
1631 oprl->prefix[i].origin = PR_ORIG_RA;
1632
1633 i++;
1634 }
1635 splx(s);
1636
1637 break;
1638 case OSIOCGIFINFO_IN6:
1639 #define ND ndi->ndi
1640 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1641 memset(&ND, 0, sizeof(ND));
1642 ND.linkmtu = IN6_LINKMTU(ifp);
1643 ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1644 ND.basereachable = ND_IFINFO(ifp)->basereachable;
1645 ND.reachable = ND_IFINFO(ifp)->reachable;
1646 ND.retrans = ND_IFINFO(ifp)->retrans;
1647 ND.flags = ND_IFINFO(ifp)->flags;
1648 ND.recalctm = ND_IFINFO(ifp)->recalctm;
1649 ND.chlim = ND_IFINFO(ifp)->chlim;
1650 break;
1651 case SIOCGIFINFO_IN6:
1652 ND = *ND_IFINFO(ifp);
1653 break;
1654 case SIOCSIFINFO_IN6:
1655 /*
1656 * used to change host variables from userland.
1657 * intented for a use on router to reflect RA configurations.
1658 */
1659 /* 0 means 'unspecified' */
1660 if (ND.linkmtu != 0) {
1661 if (ND.linkmtu < IPV6_MMTU ||
1662 ND.linkmtu > IN6_LINKMTU(ifp)) {
1663 error = EINVAL;
1664 break;
1665 }
1666 ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1667 }
1668
1669 if (ND.basereachable != 0) {
1670 int obasereachable = ND_IFINFO(ifp)->basereachable;
1671
1672 ND_IFINFO(ifp)->basereachable = ND.basereachable;
1673 if (ND.basereachable != obasereachable)
1674 ND_IFINFO(ifp)->reachable =
1675 ND_COMPUTE_RTIME(ND.basereachable);
1676 }
1677 if (ND.retrans != 0)
1678 ND_IFINFO(ifp)->retrans = ND.retrans;
1679 if (ND.chlim != 0)
1680 ND_IFINFO(ifp)->chlim = ND.chlim;
1681 /* FALLTHROUGH */
1682 case SIOCSIFINFO_FLAGS:
1683 {
1684 struct ifaddr *ifa;
1685 struct in6_ifaddr *ia;
1686
1687 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1688 !(ND.flags & ND6_IFF_IFDISABLED))
1689 {
1690 /*
1691 * If the interface is marked as ND6_IFF_IFDISABLED and
1692 * has a link-local address with IN6_IFF_DUPLICATED,
1693 * do not clear ND6_IFF_IFDISABLED.
1694 * See RFC 4862, section 5.4.5.
1695 */
1696 int duplicated_linklocal = 0;
1697
1698 IFADDR_FOREACH(ifa, ifp) {
1699 if (ifa->ifa_addr->sa_family != AF_INET6)
1700 continue;
1701 ia = (struct in6_ifaddr *)ifa;
1702 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1703 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1704 {
1705 duplicated_linklocal = 1;
1706 break;
1707 }
1708 }
1709
1710 if (duplicated_linklocal) {
1711 ND.flags |= ND6_IFF_IFDISABLED;
1712 log(LOG_ERR, "Cannot enable an interface"
1713 " with a link-local address marked"
1714 " duplicate.\n");
1715 } else {
1716 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
1717 if (ifp->if_flags & IFF_UP)
1718 in6_if_up(ifp);
1719 }
1720 } else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1721 (ND.flags & ND6_IFF_IFDISABLED))
1722 {
1723 /* Mark all IPv6 addresses as tentative. */
1724
1725 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1726 IFADDR_FOREACH(ifa, ifp) {
1727 if (ifa->ifa_addr->sa_family != AF_INET6)
1728 continue;
1729 nd6_dad_stop(ifa);
1730 ia = (struct in6_ifaddr *)ifa;
1731 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1732 }
1733 }
1734
1735 if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
1736 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1737 /* auto_linklocal 0->1 transition */
1738
1739 ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
1740 in6_ifattach(ifp, NULL);
1741 } else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
1742 ifp->if_flags & IFF_UP)
1743 {
1744 /*
1745 * When the IF already has
1746 * ND6_IFF_AUTO_LINKLOCAL, no link-local
1747 * address is assigned, and IFF_UP, try to
1748 * assign one.
1749 */
1750 int haslinklocal = 0;
1751
1752 IFADDR_FOREACH(ifa, ifp) {
1753 if (ifa->ifa_addr->sa_family !=AF_INET6)
1754 continue;
1755 ia = (struct in6_ifaddr *)ifa;
1756 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))){
1757 haslinklocal = 1;
1758 break;
1759 }
1760 }
1761 if (!haslinklocal)
1762 in6_ifattach(ifp, NULL);
1763 }
1764 }
1765 }
1766 ND_IFINFO(ifp)->flags = ND.flags;
1767 break;
1768 #undef ND
1769 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1770 /* sync kernel routing table with the default router list */
1771 defrouter_reset();
1772 defrouter_select();
1773 break;
1774 case SIOCSPFXFLUSH_IN6:
1775 {
1776 /* flush all the prefix advertised by routers */
1777 struct nd_prefix *pfx, *next;
1778
1779 s = splsoftnet();
1780 LIST_FOREACH_SAFE(pfx, &nd_prefix, ndpr_entry, next) {
1781 struct in6_ifaddr *ia, *ia_next;
1782
1783 if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr))
1784 continue; /* XXX */
1785
1786 /* do we really have to remove addresses as well? */
1787 for (ia = in6_ifaddr; ia; ia = ia_next) {
1788 /* ia might be removed. keep the next ptr. */
1789 ia_next = ia->ia_next;
1790
1791 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1792 continue;
1793
1794 if (ia->ia6_ndpr == pfx)
1795 in6_purgeaddr(&ia->ia_ifa);
1796 }
1797 prelist_remove(pfx);
1798 }
1799 splx(s);
1800 break;
1801 }
1802 case SIOCSRTRFLUSH_IN6:
1803 {
1804 /* flush all the default routers */
1805 struct nd_defrouter *drtr, *next;
1806
1807 s = splsoftnet();
1808 defrouter_reset();
1809 TAILQ_FOREACH_SAFE(drtr, &nd_defrouter, dr_entry, next) {
1810 defrtrlist_del(drtr, NULL);
1811 }
1812 defrouter_select();
1813 splx(s);
1814 break;
1815 }
1816 case SIOCGNBRINFO_IN6:
1817 {
1818 struct llinfo_nd6 *ln;
1819 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1820
1821 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1822 return error;
1823
1824 s = splsoftnet();
1825 rt = nd6_lookup(&nb_addr, 0, ifp);
1826 if (rt == NULL) {
1827 error = EINVAL;
1828 splx(s);
1829 break;
1830 }
1831
1832 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1833 rtfree(rt);
1834 if (ln == NULL) {
1835 error = EINVAL;
1836 splx(s);
1837 break;
1838 }
1839 nbi->state = ln->ln_state;
1840 nbi->asked = ln->ln_asked;
1841 nbi->isrouter = ln->ln_router;
1842 nbi->expire = ln->ln_expire;
1843 splx(s);
1844
1845 break;
1846 }
1847 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1848 ndif->ifindex = nd6_defifindex;
1849 break;
1850 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1851 return nd6_setdefaultiface(ndif->ifindex);
1852 }
1853 return error;
1854 }
1855
1856 void
1857 nd6_llinfo_release_pkts(struct llinfo_nd6 *ln, struct ifnet *ifp,
1858 struct rtentry *rt)
1859 {
1860 struct mbuf *m_hold, *m_hold_next;
1861
1862 for (m_hold = ln->ln_hold, ln->ln_hold = NULL;
1863 m_hold != NULL;
1864 m_hold = m_hold_next) {
1865 m_hold_next = m_hold->m_nextpkt;
1866 m_hold->m_nextpkt = NULL;
1867
1868 /*
1869 * we assume ifp is not a p2p here, so
1870 * just set the 2nd argument as the
1871 * 1st one.
1872 */
1873 nd6_output(ifp, ifp, m_hold, satocsin6(rt_getkey(rt)), rt);
1874 }
1875 }
1876
1877 /*
1878 * Create neighbor cache entry and cache link-layer address,
1879 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1880 */
1881 void
1882 nd6_cache_lladdr(
1883 struct ifnet *ifp,
1884 struct in6_addr *from,
1885 char *lladdr,
1886 int lladdrlen,
1887 int type, /* ICMP6 type */
1888 int code /* type dependent information */
1889 )
1890 {
1891 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
1892 struct rtentry *rt = NULL;
1893 struct llinfo_nd6 *ln = NULL;
1894 int is_newentry;
1895 struct sockaddr_dl *sdl = NULL;
1896 int do_update;
1897 int olladdr;
1898 int llchange;
1899 int newstate = 0;
1900
1901 KASSERT(ifp != NULL);
1902 KASSERT(from != NULL);
1903
1904 /* nothing must be updated for unspecified address */
1905 if (IN6_IS_ADDR_UNSPECIFIED(from))
1906 return;
1907
1908 /*
1909 * Validation about ifp->if_addrlen and lladdrlen must be done in
1910 * the caller.
1911 *
1912 * XXX If the link does not have link-layer adderss, what should
1913 * we do? (ifp->if_addrlen == 0)
1914 * Spec says nothing in sections for RA, RS and NA. There's small
1915 * description on it in NS section (RFC 2461 7.2.3).
1916 */
1917
1918 rt = nd6_lookup(from, 0, ifp);
1919 if (rt == NULL) {
1920 #if 0
1921 /* nothing must be done if there's no lladdr */
1922 if (!lladdr || !lladdrlen)
1923 return NULL;
1924 #endif
1925
1926 rt = nd6_lookup(from, 1, ifp);
1927 is_newentry = 1;
1928 } else {
1929 /* do nothing if static ndp is set */
1930 if (rt->rt_flags & RTF_STATIC) {
1931 rtfree(rt);
1932 return;
1933 }
1934 is_newentry = 0;
1935 }
1936
1937 if (rt == NULL)
1938 return;
1939 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1940 fail:
1941 (void)nd6_free(rt, 0);
1942 rtfree(rt);
1943 return;
1944 }
1945 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1946 if (ln == NULL)
1947 goto fail;
1948 if (rt->rt_gateway == NULL)
1949 goto fail;
1950 if (rt->rt_gateway->sa_family != AF_LINK)
1951 goto fail;
1952 sdl = satosdl(rt->rt_gateway);
1953
1954 olladdr = (sdl->sdl_alen) ? 1 : 0;
1955 if (olladdr && lladdr) {
1956 if (memcmp(lladdr, CLLADDR(sdl), ifp->if_addrlen))
1957 llchange = 1;
1958 else
1959 llchange = 0;
1960 } else
1961 llchange = 0;
1962
1963 /*
1964 * newentry olladdr lladdr llchange (*=record)
1965 * 0 n n -- (1)
1966 * 0 y n -- (2)
1967 * 0 n y -- (3) * STALE
1968 * 0 y y n (4) *
1969 * 0 y y y (5) * STALE
1970 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1971 * 1 -- y -- (7) * STALE
1972 */
1973
1974 if (lladdr) { /* (3-5) and (7) */
1975 /*
1976 * Record source link-layer address
1977 * XXX is it dependent to ifp->if_type?
1978 */
1979 /* XXX check for error */
1980 if (sockaddr_dl_setaddr(sdl, sdl->sdl_len, lladdr,
1981 ifp->if_addrlen) == NULL) {
1982 printf("%s.%d: sockaddr_dl_setaddr(, %d, ) "
1983 "failed on %s\n", __func__, __LINE__,
1984 sdl->sdl_len, if_name(ifp));
1985 }
1986 }
1987
1988 if (!is_newentry) {
1989 if ((!olladdr && lladdr) || /* (3) */
1990 (olladdr && lladdr && llchange)) { /* (5) */
1991 do_update = 1;
1992 newstate = ND6_LLINFO_STALE;
1993 } else /* (1-2,4) */
1994 do_update = 0;
1995 } else {
1996 do_update = 1;
1997 if (lladdr == NULL) /* (6) */
1998 newstate = ND6_LLINFO_NOSTATE;
1999 else /* (7) */
2000 newstate = ND6_LLINFO_STALE;
2001 }
2002
2003 if (do_update) {
2004 /*
2005 * Update the state of the neighbor cache.
2006 */
2007 ln->ln_state = newstate;
2008
2009 if (ln->ln_state == ND6_LLINFO_STALE) {
2010 /*
2011 * XXX: since nd6_output() below will cause
2012 * state tansition to DELAY and reset the timer,
2013 * we must set the timer now, although it is actually
2014 * meaningless.
2015 */
2016 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
2017
2018 nd6_llinfo_release_pkts(ln, ifp, rt);
2019 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
2020 /* probe right away */
2021 nd6_llinfo_settimer((void *)ln, 0);
2022 }
2023 }
2024
2025 /*
2026 * ICMP6 type dependent behavior.
2027 *
2028 * NS: clear IsRouter if new entry
2029 * RS: clear IsRouter
2030 * RA: set IsRouter if there's lladdr
2031 * redir: clear IsRouter if new entry
2032 *
2033 * RA case, (1):
2034 * The spec says that we must set IsRouter in the following cases:
2035 * - If lladdr exist, set IsRouter. This means (1-5).
2036 * - If it is old entry (!newentry), set IsRouter. This means (7).
2037 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
2038 * A quetion arises for (1) case. (1) case has no lladdr in the
2039 * neighbor cache, this is similar to (6).
2040 * This case is rare but we figured that we MUST NOT set IsRouter.
2041 *
2042 * newentry olladdr lladdr llchange NS RS RA redir
2043 * D R
2044 * 0 n n -- (1) c ? s
2045 * 0 y n -- (2) c s s
2046 * 0 n y -- (3) c s s
2047 * 0 y y n (4) c s s
2048 * 0 y y y (5) c s s
2049 * 1 -- n -- (6) c c c s
2050 * 1 -- y -- (7) c c s c s
2051 *
2052 * (c=clear s=set)
2053 */
2054 switch (type & 0xff) {
2055 case ND_NEIGHBOR_SOLICIT:
2056 /*
2057 * New entry must have is_router flag cleared.
2058 */
2059 if (is_newentry) /* (6-7) */
2060 ln->ln_router = 0;
2061 break;
2062 case ND_REDIRECT:
2063 /*
2064 * If the icmp is a redirect to a better router, always set the
2065 * is_router flag. Otherwise, if the entry is newly created,
2066 * clear the flag. [RFC 2461, sec 8.3]
2067 */
2068 if (code == ND_REDIRECT_ROUTER)
2069 ln->ln_router = 1;
2070 else if (is_newentry) /* (6-7) */
2071 ln->ln_router = 0;
2072 break;
2073 case ND_ROUTER_SOLICIT:
2074 /*
2075 * is_router flag must always be cleared.
2076 */
2077 ln->ln_router = 0;
2078 break;
2079 case ND_ROUTER_ADVERT:
2080 /*
2081 * Mark an entry with lladdr as a router.
2082 */
2083 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
2084 (is_newentry && lladdr)) { /* (7) */
2085 ln->ln_router = 1;
2086 }
2087 break;
2088 }
2089
2090 if (do_update)
2091 rt_newmsg(RTM_CHANGE, rt); /* tell user process */
2092
2093 /*
2094 * When the link-layer address of a router changes, select the
2095 * best router again. In particular, when the neighbor entry is newly
2096 * created, it might affect the selection policy.
2097 * Question: can we restrict the first condition to the "is_newentry"
2098 * case?
2099 * XXX: when we hear an RA from a new router with the link-layer
2100 * address option, defrouter_select() is called twice, since
2101 * defrtrlist_update called the function as well. However, I believe
2102 * we can compromise the overhead, since it only happens the first
2103 * time.
2104 * XXX: although defrouter_select() should not have a bad effect
2105 * for those are not autoconfigured hosts, we explicitly avoid such
2106 * cases for safety.
2107 */
2108 if (do_update && ln->ln_router && !ip6_forwarding &&
2109 nd6_accepts_rtadv(ndi))
2110 defrouter_select();
2111
2112 rtfree(rt);
2113 }
2114
2115 static void
2116 nd6_slowtimo(void *ignored_arg)
2117 {
2118 struct nd_ifinfo *nd6if;
2119 struct ifnet *ifp;
2120
2121 mutex_enter(softnet_lock);
2122 KERNEL_LOCK(1, NULL);
2123 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
2124 nd6_slowtimo, NULL);
2125 IFNET_FOREACH(ifp) {
2126 nd6if = ND_IFINFO(ifp);
2127 if (nd6if->basereachable && /* already initialized */
2128 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
2129 /*
2130 * Since reachable time rarely changes by router
2131 * advertisements, we SHOULD insure that a new random
2132 * value gets recomputed at least once every few hours.
2133 * (RFC 2461, 6.3.4)
2134 */
2135 nd6if->recalctm = nd6_recalc_reachtm_interval;
2136 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
2137 }
2138 }
2139 KERNEL_UNLOCK_ONE(NULL);
2140 mutex_exit(softnet_lock);
2141 }
2142
2143 #define senderr(e) { error = (e); goto bad;}
2144 int
2145 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
2146 const struct sockaddr_in6 *dst, struct rtentry *rt00)
2147 {
2148 struct mbuf *m = m0;
2149 struct rtentry *rt, *rt0;
2150 struct sockaddr_in6 *gw6 = NULL;
2151 struct llinfo_nd6 *ln = NULL;
2152 int error = 0;
2153
2154 #define RTFREE_IF_NEEDED(_rt) \
2155 if ((_rt) != NULL && (_rt) != rt00) \
2156 rtfree((_rt));
2157
2158 rt = rt0 = rt00;
2159
2160 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
2161 goto sendpkt;
2162
2163 if (nd6_need_cache(ifp) == 0)
2164 goto sendpkt;
2165
2166 /*
2167 * next hop determination. This routine is derived from ether_output.
2168 */
2169 if (rt) {
2170 if ((rt->rt_flags & RTF_UP) == 0) {
2171 if ((rt0 = rt = rtalloc1(sin6tocsa(dst), 1)) != NULL) {
2172 if (rt->rt_ifp != ifp)
2173 senderr(EHOSTUNREACH);
2174 } else
2175 senderr(EHOSTUNREACH);
2176 }
2177
2178 if (rt->rt_flags & RTF_GATEWAY) {
2179 struct rtentry *gwrt;
2180 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
2181
2182 /*
2183 * We skip link-layer address resolution and NUD
2184 * if the gateway is not a neighbor from ND point
2185 * of view, regardless of the value of nd_ifinfo.flags.
2186 * The second condition is a bit tricky; we skip
2187 * if the gateway is our own address, which is
2188 * sometimes used to install a route to a p2p link.
2189 */
2190 if (!nd6_is_addr_neighbor(gw6, ifp) ||
2191 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
2192 /*
2193 * We allow this kind of tricky route only
2194 * when the outgoing interface is p2p.
2195 * XXX: we may need a more generic rule here.
2196 */
2197 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2198 senderr(EHOSTUNREACH);
2199
2200 goto sendpkt;
2201 }
2202
2203 gwrt = rt_get_gwroute(rt);
2204 if (gwrt == NULL)
2205 goto lookup;
2206
2207 RTFREE_IF_NEEDED(rt);
2208 rt = gwrt;
2209 if ((rt->rt_flags & RTF_UP) == 0) {
2210 rtfree(rt);
2211 rt = rt0;
2212 lookup:
2213 gwrt = rt->rt_gwroute =
2214 rtalloc1(rt->rt_gateway, 1);
2215 rtfree(rt);
2216 rt = gwrt;
2217 if (rt == NULL)
2218 senderr(EHOSTUNREACH);
2219 /* the "G" test below also prevents rt == rt0 */
2220 if ((rt->rt_flags & RTF_GATEWAY) ||
2221 (rt->rt_ifp != ifp)) {
2222 rt0->rt_gwroute = NULL;
2223 senderr(EHOSTUNREACH);
2224 }
2225 }
2226 }
2227 }
2228
2229 /*
2230 * Address resolution or Neighbor Unreachability Detection
2231 * for the next hop.
2232 * At this point, the destination of the packet must be a unicast
2233 * or an anycast address(i.e. not a multicast).
2234 */
2235
2236 /* Look up the neighbor cache for the nexthop */
2237 if (rt != NULL && (rt->rt_flags & RTF_LLINFO) != 0)
2238 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2239 else {
2240 /*
2241 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2242 * the condition below is not very efficient. But we believe
2243 * it is tolerable, because this should be a rare case.
2244 */
2245 if (nd6_is_addr_neighbor(dst, ifp)) {
2246 if (rt != NULL && rt != rt00)
2247 rtfree(rt);
2248
2249 RTFREE_IF_NEEDED(rt);
2250 rt = nd6_lookup(&dst->sin6_addr, 1, ifp);
2251 if (rt != NULL)
2252 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2253 }
2254 }
2255 if (ln == NULL || rt == NULL) {
2256 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
2257 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
2258 log(LOG_DEBUG,
2259 "nd6_output: can't allocate llinfo for %s "
2260 "(ln=%p, rt=%p)\n",
2261 ip6_sprintf(&dst->sin6_addr), ln, rt);
2262 senderr(EIO); /* XXX: good error? */
2263 }
2264
2265 goto sendpkt; /* send anyway */
2266 }
2267
2268 /*
2269 * Move this entry to the head of the queue so that it is less likely
2270 * for this entry to be a target of forced garbage collection (see
2271 * nd6_rtrequest()).
2272 */
2273 LN_DEQUEUE(ln);
2274 LN_INSERTHEAD(ln);
2275
2276 /* We don't have to do link-layer address resolution on a p2p link. */
2277 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2278 ln->ln_state < ND6_LLINFO_REACHABLE) {
2279 ln->ln_state = ND6_LLINFO_STALE;
2280 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
2281 }
2282
2283 /*
2284 * The first time we send a packet to a neighbor whose entry is
2285 * STALE, we have to change the state to DELAY and a sets a timer to
2286 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2287 * neighbor unreachability detection on expiration.
2288 * (RFC 2461 7.3.3)
2289 */
2290 if (ln->ln_state == ND6_LLINFO_STALE) {
2291 ln->ln_asked = 0;
2292 ln->ln_state = ND6_LLINFO_DELAY;
2293 nd6_llinfo_settimer(ln, (long)nd6_delay * hz);
2294 }
2295
2296 /*
2297 * If the neighbor cache entry has a state other than INCOMPLETE
2298 * (i.e. its link-layer address is already resolved), just
2299 * send the packet.
2300 */
2301 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
2302 goto sendpkt;
2303
2304 /*
2305 * There is a neighbor cache entry, but no ethernet address
2306 * response yet. Append this latest packet to the end of the
2307 * packet queue in the mbuf, unless the number of the packet
2308 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen,
2309 * the oldest packet in the queue will be removed.
2310 */
2311 if (ln->ln_state == ND6_LLINFO_NOSTATE)
2312 ln->ln_state = ND6_LLINFO_INCOMPLETE;
2313 if (ln->ln_hold) {
2314 struct mbuf *m_hold;
2315 int i;
2316
2317 i = 0;
2318 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
2319 i++;
2320 if (m_hold->m_nextpkt == NULL) {
2321 m_hold->m_nextpkt = m;
2322 break;
2323 }
2324 }
2325 while (i >= nd6_maxqueuelen) {
2326 m_hold = ln->ln_hold;
2327 ln->ln_hold = ln->ln_hold->m_nextpkt;
2328 m_freem(m_hold);
2329 i--;
2330 }
2331 } else {
2332 ln->ln_hold = m;
2333 }
2334
2335 /*
2336 * If there has been no NS for the neighbor after entering the
2337 * INCOMPLETE state, send the first solicitation.
2338 */
2339 if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
2340 ln->ln_asked++;
2341 nd6_llinfo_settimer(ln,
2342 (long)ND_IFINFO(ifp)->retrans * hz / 1000);
2343 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
2344 }
2345 error = 0;
2346 goto exit;
2347
2348 sendpkt:
2349 /* discard the packet if IPv6 operation is disabled on the interface */
2350 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
2351 error = ENETDOWN; /* better error? */
2352 goto bad;
2353 }
2354
2355 #ifndef NET_MPSAFE
2356 KERNEL_LOCK(1, NULL);
2357 #endif
2358 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2359 error = (*ifp->if_output)(origifp, m, sin6tocsa(dst), rt);
2360 else
2361 error = (*ifp->if_output)(ifp, m, sin6tocsa(dst), rt);
2362 #ifndef NET_MPSAFE
2363 KERNEL_UNLOCK_ONE(NULL);
2364 #endif
2365 goto exit;
2366
2367 bad:
2368 if (m != NULL)
2369 m_freem(m);
2370 exit:
2371 RTFREE_IF_NEEDED(rt);
2372
2373 return error;
2374 #undef RTFREE_IF_NEEDED
2375 }
2376 #undef senderr
2377
2378 int
2379 nd6_need_cache(struct ifnet *ifp)
2380 {
2381 /*
2382 * XXX: we currently do not make neighbor cache on any interface
2383 * other than ARCnet, Ethernet, FDDI and GIF.
2384 *
2385 * RFC2893 says:
2386 * - unidirectional tunnels needs no ND
2387 */
2388 switch (ifp->if_type) {
2389 case IFT_ARCNET:
2390 case IFT_ETHER:
2391 case IFT_FDDI:
2392 case IFT_IEEE1394:
2393 case IFT_CARP:
2394 case IFT_GIF: /* XXX need more cases? */
2395 case IFT_PPP:
2396 case IFT_TUNNEL:
2397 return 1;
2398 default:
2399 return 0;
2400 }
2401 }
2402
2403 int
2404 nd6_storelladdr(const struct ifnet *ifp, const struct rtentry *rt,
2405 struct mbuf *m, const struct sockaddr *dst, uint8_t *lldst,
2406 size_t dstsize)
2407 {
2408 const struct sockaddr_dl *sdl;
2409
2410 if (m->m_flags & M_MCAST) {
2411 switch (ifp->if_type) {
2412 case IFT_ETHER:
2413 case IFT_FDDI:
2414 ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
2415 lldst);
2416 return 1;
2417 case IFT_IEEE1394:
2418 memcpy(lldst, ifp->if_broadcastaddr,
2419 MIN(dstsize, ifp->if_addrlen));
2420 return 1;
2421 case IFT_ARCNET:
2422 *lldst = 0;
2423 return 1;
2424 default:
2425 m_freem(m);
2426 return 0;
2427 }
2428 }
2429
2430 if (rt == NULL) {
2431 /* this could happen, if we could not allocate memory */
2432 m_freem(m);
2433 return 0;
2434 }
2435 if (rt->rt_gateway->sa_family != AF_LINK) {
2436 char gbuf[256];
2437 char dbuf[LINK_ADDRSTRLEN];
2438 sockaddr_format(rt->rt_gateway, gbuf, sizeof(gbuf));
2439 printf("%s: bad gateway address type %s for dst %s"
2440 " through interface %s\n", __func__, gbuf,
2441 IN6_PRINT(dbuf, &satocsin6(dst)->sin6_addr),
2442 if_name(ifp));
2443 m_freem(m);
2444 return 0;
2445 }
2446 sdl = satocsdl(rt->rt_gateway);
2447 if (sdl->sdl_alen == 0 || sdl->sdl_alen > dstsize) {
2448 char sbuf[INET6_ADDRSTRLEN];
2449 char dbuf[LINK_ADDRSTRLEN];
2450 /* this should be impossible, but we bark here for debugging */
2451 printf("%s: sdl_alen == %" PRIu8 ", if=%s, dst=%s, sdl=%s\n",
2452 __func__, sdl->sdl_alen, if_name(ifp),
2453 IN6_PRINT(sbuf, &satocsin6(dst)->sin6_addr),
2454 DL_PRINT(dbuf, &sdl->sdl_addr));
2455 m_freem(m);
2456 return 0;
2457 }
2458
2459 memcpy(lldst, CLLADDR(sdl), MIN(dstsize, sdl->sdl_alen));
2460 return 1;
2461 }
2462
2463 static void
2464 clear_llinfo_pqueue(struct llinfo_nd6 *ln)
2465 {
2466 struct mbuf *m_hold, *m_hold_next;
2467
2468 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
2469 m_hold_next = m_hold->m_nextpkt;
2470 m_hold->m_nextpkt = NULL;
2471 m_freem(m_hold);
2472 }
2473
2474 ln->ln_hold = NULL;
2475 return;
2476 }
2477
2478 int
2479 nd6_sysctl(
2480 int name,
2481 void *oldp, /* syscall arg, need copyout */
2482 size_t *oldlenp,
2483 void *newp, /* syscall arg, need copyin */
2484 size_t newlen
2485 )
2486 {
2487 void *p;
2488 size_t ol;
2489 int error;
2490
2491 error = 0;
2492
2493 if (newp)
2494 return EPERM;
2495 if (oldp && !oldlenp)
2496 return EINVAL;
2497 ol = oldlenp ? *oldlenp : 0;
2498
2499 if (oldp) {
2500 p = malloc(*oldlenp, M_TEMP, M_WAITOK);
2501 if (p == NULL)
2502 return ENOMEM;
2503 } else
2504 p = NULL;
2505 switch (name) {
2506 case ICMPV6CTL_ND6_DRLIST:
2507 error = fill_drlist(p, oldlenp, ol);
2508 if (!error && p != NULL && oldp != NULL)
2509 error = copyout(p, oldp, *oldlenp);
2510 break;
2511
2512 case ICMPV6CTL_ND6_PRLIST:
2513 error = fill_prlist(p, oldlenp, ol);
2514 if (!error && p != NULL && oldp != NULL)
2515 error = copyout(p, oldp, *oldlenp);
2516 break;
2517
2518 case ICMPV6CTL_ND6_MAXQLEN:
2519 break;
2520
2521 default:
2522 error = ENOPROTOOPT;
2523 break;
2524 }
2525 if (p)
2526 free(p, M_TEMP);
2527
2528 return error;
2529 }
2530
2531 static int
2532 fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
2533 {
2534 int error = 0, s;
2535 struct in6_defrouter *d = NULL, *de = NULL;
2536 struct nd_defrouter *dr;
2537 size_t l;
2538
2539 s = splsoftnet();
2540
2541 if (oldp) {
2542 d = (struct in6_defrouter *)oldp;
2543 de = (struct in6_defrouter *)((char *)oldp + *oldlenp);
2544 }
2545 l = 0;
2546
2547 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
2548
2549 if (oldp && d + 1 <= de) {
2550 memset(d, 0, sizeof(*d));
2551 sockaddr_in6_init(&d->rtaddr, &dr->rtaddr, 0, 0, 0);
2552 if (sa6_recoverscope(&d->rtaddr)) {
2553 log(LOG_ERR,
2554 "scope error in router list (%s)\n",
2555 ip6_sprintf(&d->rtaddr.sin6_addr));
2556 /* XXX: press on... */
2557 }
2558 d->flags = dr->flags;
2559 d->rtlifetime = dr->rtlifetime;
2560 d->expire = dr->expire;
2561 d->if_index = dr->ifp->if_index;
2562 }
2563
2564 l += sizeof(*d);
2565 if (d)
2566 d++;
2567 }
2568
2569 if (oldp) {
2570 if (l > ol)
2571 error = ENOMEM;
2572 }
2573 if (oldlenp)
2574 *oldlenp = l; /* (void *)d - (void *)oldp */
2575
2576 splx(s);
2577
2578 return error;
2579 }
2580
2581 static int
2582 fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
2583 {
2584 int error = 0, s;
2585 struct nd_prefix *pr;
2586 uint8_t *p = NULL, *ps = NULL;
2587 uint8_t *pe = NULL;
2588 size_t l;
2589
2590 s = splsoftnet();
2591
2592 if (oldp) {
2593 ps = p = (uint8_t*)oldp;
2594 pe = (uint8_t*)oldp + *oldlenp;
2595 }
2596 l = 0;
2597
2598 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
2599 u_short advrtrs;
2600 struct sockaddr_in6 sin6;
2601 struct nd_pfxrouter *pfr;
2602 struct in6_prefix pfx;
2603
2604 if (oldp && p + sizeof(struct in6_prefix) <= pe)
2605 {
2606 memset(&pfx, 0, sizeof(pfx));
2607 ps = p;
2608 pfx.prefix = pr->ndpr_prefix;
2609
2610 if (sa6_recoverscope(&pfx.prefix)) {
2611 log(LOG_ERR,
2612 "scope error in prefix list (%s)\n",
2613 ip6_sprintf(&pfx.prefix.sin6_addr));
2614 /* XXX: press on... */
2615 }
2616 pfx.raflags = pr->ndpr_raf;
2617 pfx.prefixlen = pr->ndpr_plen;
2618 pfx.vltime = pr->ndpr_vltime;
2619 pfx.pltime = pr->ndpr_pltime;
2620 pfx.if_index = pr->ndpr_ifp->if_index;
2621 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2622 pfx.expire = 0;
2623 else {
2624 time_t maxexpire;
2625
2626 /* XXX: we assume time_t is signed. */
2627 maxexpire = (-1) &
2628 ~((time_t)1 <<
2629 ((sizeof(maxexpire) * 8) - 1));
2630 if (pr->ndpr_vltime <
2631 maxexpire - pr->ndpr_lastupdate) {
2632 pfx.expire = pr->ndpr_lastupdate +
2633 pr->ndpr_vltime;
2634 } else
2635 pfx.expire = maxexpire;
2636 }
2637 pfx.refcnt = pr->ndpr_refcnt;
2638 pfx.flags = pr->ndpr_stateflags;
2639 pfx.origin = PR_ORIG_RA;
2640
2641 p += sizeof(pfx); l += sizeof(pfx);
2642
2643 advrtrs = 0;
2644 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2645 if (p + sizeof(sin6) > pe) {
2646 advrtrs++;
2647 continue;
2648 }
2649
2650 sockaddr_in6_init(&sin6, &pfr->router->rtaddr,
2651 0, 0, 0);
2652 if (sa6_recoverscope(&sin6)) {
2653 log(LOG_ERR,
2654 "scope error in "
2655 "prefix list (%s)\n",
2656 ip6_sprintf(&pfr->router->rtaddr));
2657 }
2658 advrtrs++;
2659 memcpy(p, &sin6, sizeof(sin6));
2660 p += sizeof(sin6);
2661 l += sizeof(sin6);
2662 }
2663 pfx.advrtrs = advrtrs;
2664 memcpy(ps, &pfx, sizeof(pfx));
2665 }
2666 else {
2667 l += sizeof(pfx);
2668 advrtrs = 0;
2669 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2670 advrtrs++;
2671 l += sizeof(sin6);
2672 }
2673 }
2674 }
2675
2676 if (oldp) {
2677 *oldlenp = l; /* (void *)d - (void *)oldp */
2678 if (l > ol)
2679 error = ENOMEM;
2680 } else
2681 *oldlenp = l;
2682
2683 splx(s);
2684
2685 return error;
2686 }
2687