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