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