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