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