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