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