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