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