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