nd6.c revision 1.115 1 /* $NetBSD: nd6.c,v 1.115 2007/05/17 00:53:26 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.115 2007/05/17 00:53:26 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 #define SIN6(s) ((const struct sockaddr_in6 *)s)
79 #define SDL(s) ((struct sockaddr_dl *)s)
80
81 /* timer values */
82 int nd6_prune = 1; /* walk list every 1 seconds */
83 int nd6_delay = 5; /* delay first probe time 5 second */
84 int nd6_umaxtries = 3; /* maximum unicast query */
85 int nd6_mmaxtries = 3; /* maximum multicast query */
86 int nd6_useloopback = 1; /* use loopback interface for local traffic */
87 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
88
89 /* preventing too many loops in ND option parsing */
90 int nd6_maxndopt = 10; /* max # of ND options allowed */
91
92 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
93
94 int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
95
96 #ifdef ND6_DEBUG
97 int nd6_debug = 1;
98 #else
99 int nd6_debug = 0;
100 #endif
101
102 /* for debugging? */
103 static int nd6_inuse, nd6_allocated;
104
105 struct llinfo_nd6 llinfo_nd6 = {
106 .ln_prev = &llinfo_nd6,
107 .ln_next = &llinfo_nd6,
108 };
109 struct nd_drhead nd_defrouter;
110 struct nd_prhead nd_prefix = { 0 };
111
112 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
113 static struct sockaddr_in6 all1_sa;
114
115 static void nd6_setmtu0 __P((struct ifnet *, struct nd_ifinfo *));
116 static void nd6_slowtimo __P((void *));
117 static int regen_tmpaddr __P((struct in6_ifaddr *));
118 static struct llinfo_nd6 *nd6_free __P((struct rtentry *, int));
119 static void nd6_llinfo_timer __P((void *));
120 static void clear_llinfo_pqueue __P((struct llinfo_nd6 *));
121
122 struct callout nd6_slowtimo_ch = CALLOUT_INITIALIZER;
123 struct callout nd6_timer_ch = CALLOUT_INITIALIZER;
124 extern struct callout in6_tmpaddrtimer_ch;
125
126 static int fill_drlist __P((void *, size_t *, size_t));
127 static int fill_prlist __P((void *, size_t *, size_t));
128
129 MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery");
130
131 void
132 nd6_init(void)
133 {
134 static int nd6_init_done = 0;
135 int i;
136
137 if (nd6_init_done) {
138 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
139 return;
140 }
141
142 all1_sa.sin6_family = AF_INET6;
143 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
144 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
145 all1_sa.sin6_addr.s6_addr[i] = 0xff;
146
147 /* initialization of the default router list */
148 TAILQ_INIT(&nd_defrouter);
149
150 nd6_init_done = 1;
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 = (struct sockaddr_in6 *)rt_key(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 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 = (struct sockaddr_dl *)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 = ((struct sockaddr_in6 *)rt_key(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(&((struct sockaddr_in6 *)rt_key(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_key(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 static const struct sockaddr_dl null_sdl = {
1120 .sdl_len = sizeof(null_sdl),
1121 .sdl_family = AF_LINK,
1122 };
1123 struct ifnet *ifp = rt->rt_ifp;
1124 struct ifaddr *ifa;
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 /*
1131 * This is probably an interface direct route for a link
1132 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1133 * We do not need special treatment below for such a route.
1134 * Moreover, the RTF_LLINFO flag which would be set below
1135 * would annoy the ndp(8) command.
1136 */
1137 return;
1138 }
1139
1140 if (req == RTM_RESOLVE &&
1141 (nd6_need_cache(ifp) == 0 || /* stf case */
1142 !nd6_is_addr_neighbor(satocsin6(rt_key(rt)), ifp))) {
1143 /*
1144 * FreeBSD and BSD/OS often make a cloned host route based
1145 * on a less-specific route (e.g. the default route).
1146 * If the less specific route does not have a "gateway"
1147 * (this is the case when the route just goes to a p2p or an
1148 * stf interface), we'll mistakenly make a neighbor cache for
1149 * the host route, and will see strange neighbor solicitation
1150 * for the corresponding destination. In order to avoid the
1151 * confusion, we check if the destination of the route is
1152 * a neighbor in terms of neighbor discovery, and stop the
1153 * process if not. Additionally, we remove the LLINFO flag
1154 * so that ndp(8) will not try to get the neighbor information
1155 * of the destination.
1156 */
1157 rt->rt_flags &= ~RTF_LLINFO;
1158 return;
1159 }
1160
1161 switch (req) {
1162 case RTM_ADD:
1163 /*
1164 * There is no backward compatibility :)
1165 *
1166 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1167 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1168 * rt->rt_flags |= RTF_CLONING;
1169 */
1170 if ((rt->rt_flags & RTF_CLONING) ||
1171 ((rt->rt_flags & RTF_LLINFO) && !ln)) {
1172 /*
1173 * Case 1: This route should come from a route to
1174 * interface (RTF_CLONING case) or the route should be
1175 * treated as on-link but is currently not
1176 * (RTF_LLINFO && !ln case).
1177 */
1178 rt_setgate(rt, rt_key(rt),
1179 (const struct sockaddr *)&null_sdl);
1180 gate = rt->rt_gateway;
1181 SDL(gate)->sdl_type = ifp->if_type;
1182 SDL(gate)->sdl_index = ifp->if_index;
1183 if (ln)
1184 nd6_llinfo_settimer(ln, 0);
1185 if ((rt->rt_flags & RTF_CLONING) != 0)
1186 break;
1187 }
1188 /*
1189 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1190 * We don't do that here since llinfo is not ready yet.
1191 *
1192 * There are also couple of other things to be discussed:
1193 * - unsolicited NA code needs improvement beforehand
1194 * - RFC2461 says we MAY send multicast unsolicited NA
1195 * (7.2.6 paragraph 4), however, it also says that we
1196 * SHOULD provide a mechanism to prevent multicast NA storm.
1197 * we don't have anything like it right now.
1198 * note that the mechanism needs a mutual agreement
1199 * between proxies, which means that we need to implement
1200 * a new protocol, or a new kludge.
1201 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1202 * we need to check ip6forwarding before sending it.
1203 * (or should we allow proxy ND configuration only for
1204 * routers? there's no mention about proxy ND from hosts)
1205 */
1206 #if 0
1207 /* XXX it does not work */
1208 if (rt->rt_flags & RTF_ANNOUNCE)
1209 nd6_na_output(ifp,
1210 &SIN6(rt_key(rt))->sin6_addr,
1211 &SIN6(rt_key(rt))->sin6_addr,
1212 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1213 1, NULL);
1214 #endif
1215 /* FALLTHROUGH */
1216 case RTM_RESOLVE:
1217 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1218 /*
1219 * Address resolution isn't necessary for a point to
1220 * point link, so we can skip this test for a p2p link.
1221 */
1222 if (gate->sa_family != AF_LINK ||
1223 gate->sa_len < sizeof(null_sdl)) {
1224 log(LOG_DEBUG,
1225 "nd6_rtrequest: bad gateway value: %s\n",
1226 if_name(ifp));
1227 break;
1228 }
1229 SDL(gate)->sdl_type = ifp->if_type;
1230 SDL(gate)->sdl_index = ifp->if_index;
1231 }
1232 if (ln != NULL)
1233 break; /* This happens on a route change */
1234 /*
1235 * Case 2: This route may come from cloning, or a manual route
1236 * add with a LL address.
1237 */
1238 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1239 rt->rt_llinfo = (void *)ln;
1240 if (ln == NULL) {
1241 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1242 break;
1243 }
1244 nd6_inuse++;
1245 nd6_allocated++;
1246 bzero(ln, sizeof(*ln));
1247 ln->ln_rt = rt;
1248 callout_init(&ln->ln_timer_ch);
1249 /* this is required for "ndp" command. - shin */
1250 if (req == RTM_ADD) {
1251 /*
1252 * gate should have some valid AF_LINK entry,
1253 * and ln->ln_expire should have some lifetime
1254 * which is specified by ndp command.
1255 */
1256 ln->ln_state = ND6_LLINFO_REACHABLE;
1257 ln->ln_byhint = 0;
1258 } else {
1259 /*
1260 * When req == RTM_RESOLVE, rt is created and
1261 * initialized in rtrequest(), so rt_expire is 0.
1262 */
1263 ln->ln_state = ND6_LLINFO_NOSTATE;
1264 nd6_llinfo_settimer(ln, 0);
1265 }
1266 rt->rt_flags |= RTF_LLINFO;
1267 ln->ln_next = llinfo_nd6.ln_next;
1268 llinfo_nd6.ln_next = ln;
1269 ln->ln_prev = &llinfo_nd6;
1270 ln->ln_next->ln_prev = ln;
1271
1272 /*
1273 * check if rt_key(rt) is one of my address assigned
1274 * to the interface.
1275 */
1276 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1277 &SIN6(rt_key(rt))->sin6_addr);
1278 if (ifa) {
1279 void *macp = nd6_ifptomac(ifp);
1280 nd6_llinfo_settimer(ln, -1);
1281 ln->ln_state = ND6_LLINFO_REACHABLE;
1282 ln->ln_byhint = 0;
1283 if (macp) {
1284 bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1285 SDL(gate)->sdl_alen = ifp->if_addrlen;
1286 }
1287 if (nd6_useloopback) {
1288 rt->rt_ifp = lo0ifp; /* XXX */
1289 /*
1290 * Make sure rt_ifa be equal to the ifaddr
1291 * corresponding to the address.
1292 * We need this because when we refer
1293 * rt_ifa->ia6_flags in ip6_input, we assume
1294 * that the rt_ifa points to the address instead
1295 * of the loopback address.
1296 */
1297 if (ifa != rt->rt_ifa)
1298 rt_replace_ifa(rt, ifa);
1299 rt->rt_flags &= ~RTF_CLONED;
1300 }
1301 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1302 nd6_llinfo_settimer(ln, -1);
1303 ln->ln_state = ND6_LLINFO_REACHABLE;
1304 ln->ln_byhint = 0;
1305
1306 /* join solicited node multicast for proxy ND */
1307 if (ifp->if_flags & IFF_MULTICAST) {
1308 struct in6_addr llsol;
1309 int error;
1310
1311 llsol = SIN6(rt_key(rt))->sin6_addr;
1312 llsol.s6_addr32[0] = htonl(0xff020000);
1313 llsol.s6_addr32[1] = 0;
1314 llsol.s6_addr32[2] = htonl(1);
1315 llsol.s6_addr8[12] = 0xff;
1316 if (in6_setscope(&llsol, ifp, NULL))
1317 break;
1318 if (!in6_addmulti(&llsol, ifp, &error, 0)) {
1319 nd6log((LOG_ERR, "%s: failed to join "
1320 "%s (errno=%d)\n", if_name(ifp),
1321 ip6_sprintf(&llsol), error));
1322 }
1323 }
1324 }
1325 break;
1326
1327 case RTM_DELETE:
1328 if (ln == NULL)
1329 break;
1330 /* leave from solicited node multicast for proxy ND */
1331 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1332 (ifp->if_flags & IFF_MULTICAST) != 0) {
1333 struct in6_addr llsol;
1334 struct in6_multi *in6m;
1335
1336 llsol = SIN6(rt_key(rt))->sin6_addr;
1337 llsol.s6_addr32[0] = htonl(0xff020000);
1338 llsol.s6_addr32[1] = 0;
1339 llsol.s6_addr32[2] = htonl(1);
1340 llsol.s6_addr8[12] = 0xff;
1341 if (in6_setscope(&llsol, ifp, NULL) == 0) {
1342 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1343 if (in6m)
1344 in6_delmulti(in6m);
1345 }
1346 }
1347 nd6_inuse--;
1348 ln->ln_next->ln_prev = ln->ln_prev;
1349 ln->ln_prev->ln_next = ln->ln_next;
1350 ln->ln_prev = NULL;
1351 nd6_llinfo_settimer(ln, -1);
1352 rt->rt_llinfo = 0;
1353 rt->rt_flags &= ~RTF_LLINFO;
1354 clear_llinfo_pqueue(ln);
1355 Free((void *)ln);
1356 }
1357 }
1358
1359 int
1360 nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp)
1361 {
1362 struct in6_drlist *drl = (struct in6_drlist *)data;
1363 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1364 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1365 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1366 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1367 struct nd_defrouter *dr;
1368 struct nd_prefix *pr;
1369 struct rtentry *rt;
1370 int i = 0, error = 0;
1371 int s;
1372
1373 switch (cmd) {
1374 case SIOCGDRLST_IN6:
1375 /*
1376 * obsolete API, use sysctl under net.inet6.icmp6
1377 */
1378 bzero(drl, sizeof(*drl));
1379 s = splsoftnet();
1380 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
1381 if (i >= DRLSTSIZ)
1382 break;
1383 drl->defrouter[i].rtaddr = dr->rtaddr;
1384 in6_clearscope(&drl->defrouter[i].rtaddr);
1385
1386 drl->defrouter[i].flags = dr->flags;
1387 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1388 drl->defrouter[i].expire = dr->expire;
1389 drl->defrouter[i].if_index = dr->ifp->if_index;
1390 i++;
1391 }
1392 splx(s);
1393 break;
1394 case SIOCGPRLST_IN6:
1395 /*
1396 * obsolete API, use sysctl under net.inet6.icmp6
1397 *
1398 * XXX the structure in6_prlist was changed in backward-
1399 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1400 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1401 */
1402 /*
1403 * XXX meaning of fields, especialy "raflags", is very
1404 * differnet between RA prefix list and RR/static prefix list.
1405 * how about separating ioctls into two?
1406 */
1407 bzero(oprl, sizeof(*oprl));
1408 s = splsoftnet();
1409 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1410 struct nd_pfxrouter *pfr;
1411 int j;
1412
1413 if (i >= PRLSTSIZ)
1414 break;
1415 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1416 oprl->prefix[i].raflags = pr->ndpr_raf;
1417 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1418 oprl->prefix[i].vltime = pr->ndpr_vltime;
1419 oprl->prefix[i].pltime = pr->ndpr_pltime;
1420 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1421 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1422 oprl->prefix[i].expire = 0;
1423 else {
1424 time_t maxexpire;
1425
1426 /* XXX: we assume time_t is signed. */
1427 maxexpire = (-1) &
1428 ~((time_t)1 <<
1429 ((sizeof(maxexpire) * 8) - 1));
1430 if (pr->ndpr_vltime <
1431 maxexpire - pr->ndpr_lastupdate) {
1432 oprl->prefix[i].expire =
1433 pr->ndpr_lastupdate +
1434 pr->ndpr_vltime;
1435 } else
1436 oprl->prefix[i].expire = maxexpire;
1437 }
1438
1439 j = 0;
1440 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1441 if (j < DRLSTSIZ) {
1442 #define RTRADDR oprl->prefix[i].advrtr[j]
1443 RTRADDR = pfr->router->rtaddr;
1444 in6_clearscope(&RTRADDR);
1445 #undef RTRADDR
1446 }
1447 j++;
1448 }
1449 oprl->prefix[i].advrtrs = j;
1450 oprl->prefix[i].origin = PR_ORIG_RA;
1451
1452 i++;
1453 }
1454 splx(s);
1455
1456 break;
1457 case OSIOCGIFINFO_IN6:
1458 #define ND ndi->ndi
1459 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1460 memset(&ND, 0, sizeof(ND));
1461 ND.linkmtu = IN6_LINKMTU(ifp);
1462 ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1463 ND.basereachable = ND_IFINFO(ifp)->basereachable;
1464 ND.reachable = ND_IFINFO(ifp)->reachable;
1465 ND.retrans = ND_IFINFO(ifp)->retrans;
1466 ND.flags = ND_IFINFO(ifp)->flags;
1467 ND.recalctm = ND_IFINFO(ifp)->recalctm;
1468 ND.chlim = ND_IFINFO(ifp)->chlim;
1469 break;
1470 case SIOCGIFINFO_IN6:
1471 ND = *ND_IFINFO(ifp);
1472 break;
1473 case SIOCSIFINFO_IN6:
1474 /*
1475 * used to change host variables from userland.
1476 * intented for a use on router to reflect RA configurations.
1477 */
1478 /* 0 means 'unspecified' */
1479 if (ND.linkmtu != 0) {
1480 if (ND.linkmtu < IPV6_MMTU ||
1481 ND.linkmtu > IN6_LINKMTU(ifp)) {
1482 error = EINVAL;
1483 break;
1484 }
1485 ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1486 }
1487
1488 if (ND.basereachable != 0) {
1489 int obasereachable = ND_IFINFO(ifp)->basereachable;
1490
1491 ND_IFINFO(ifp)->basereachable = ND.basereachable;
1492 if (ND.basereachable != obasereachable)
1493 ND_IFINFO(ifp)->reachable =
1494 ND_COMPUTE_RTIME(ND.basereachable);
1495 }
1496 if (ND.retrans != 0)
1497 ND_IFINFO(ifp)->retrans = ND.retrans;
1498 if (ND.chlim != 0)
1499 ND_IFINFO(ifp)->chlim = ND.chlim;
1500 /* FALLTHROUGH */
1501 case SIOCSIFINFO_FLAGS:
1502 ND_IFINFO(ifp)->flags = ND.flags;
1503 break;
1504 #undef ND
1505 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1506 /* sync kernel routing table with the default router list */
1507 defrouter_reset();
1508 defrouter_select();
1509 break;
1510 case SIOCSPFXFLUSH_IN6:
1511 {
1512 /* flush all the prefix advertised by routers */
1513 struct nd_prefix *pfx, *next;
1514
1515 s = splsoftnet();
1516 for (pfx = LIST_FIRST(&nd_prefix); pfx; pfx = next) {
1517 struct in6_ifaddr *ia, *ia_next;
1518
1519 next = LIST_NEXT(pfx, ndpr_entry);
1520
1521 if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr))
1522 continue; /* XXX */
1523
1524 /* do we really have to remove addresses as well? */
1525 for (ia = in6_ifaddr; ia; ia = ia_next) {
1526 /* ia might be removed. keep the next ptr. */
1527 ia_next = ia->ia_next;
1528
1529 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1530 continue;
1531
1532 if (ia->ia6_ndpr == pfx)
1533 in6_purgeaddr(&ia->ia_ifa);
1534 }
1535 prelist_remove(pfx);
1536 }
1537 splx(s);
1538 break;
1539 }
1540 case SIOCSRTRFLUSH_IN6:
1541 {
1542 /* flush all the default routers */
1543 struct nd_defrouter *drtr, *next;
1544
1545 s = splsoftnet();
1546 defrouter_reset();
1547 for (drtr = TAILQ_FIRST(&nd_defrouter); drtr; drtr = next) {
1548 next = TAILQ_NEXT(drtr, dr_entry);
1549 defrtrlist_del(drtr);
1550 }
1551 defrouter_select();
1552 splx(s);
1553 break;
1554 }
1555 case SIOCGNBRINFO_IN6:
1556 {
1557 struct llinfo_nd6 *ln;
1558 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1559
1560 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1561 return error;
1562
1563 s = splsoftnet();
1564 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL ||
1565 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
1566 error = EINVAL;
1567 splx(s);
1568 break;
1569 }
1570 nbi->state = ln->ln_state;
1571 nbi->asked = ln->ln_asked;
1572 nbi->isrouter = ln->ln_router;
1573 nbi->expire = ln->ln_expire;
1574 splx(s);
1575
1576 break;
1577 }
1578 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1579 ndif->ifindex = nd6_defifindex;
1580 break;
1581 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1582 return nd6_setdefaultiface(ndif->ifindex);
1583 }
1584 return error;
1585 }
1586
1587 void
1588 nd6_llinfo_release_pkts(struct llinfo_nd6 *ln, struct ifnet *ifp,
1589 struct rtentry *rt)
1590 {
1591 struct mbuf *m_hold, *m_hold_next;
1592
1593 for (m_hold = ln->ln_hold, ln->ln_hold = NULL;
1594 m_hold != NULL;
1595 m_hold = m_hold_next) {
1596 m_hold_next = m_hold->m_nextpkt;
1597 m_hold->m_nextpkt = NULL;
1598
1599 /*
1600 * we assume ifp is not a p2p here, so
1601 * just set the 2nd argument as the
1602 * 1st one.
1603 */
1604 nd6_output(ifp, ifp, m_hold, satocsin6(rt_key(rt)), rt);
1605 }
1606 }
1607
1608 /*
1609 * Create neighbor cache entry and cache link-layer address,
1610 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1611 */
1612 struct rtentry *
1613 nd6_cache_lladdr(
1614 struct ifnet *ifp,
1615 struct in6_addr *from,
1616 char *lladdr,
1617 int lladdrlen,
1618 int type, /* ICMP6 type */
1619 int code /* type dependent information */
1620 )
1621 {
1622 struct rtentry *rt = NULL;
1623 struct llinfo_nd6 *ln = NULL;
1624 int is_newentry;
1625 struct sockaddr_dl *sdl = NULL;
1626 int do_update;
1627 int olladdr;
1628 int llchange;
1629 int newstate = 0;
1630
1631 if (ifp == NULL)
1632 panic("ifp == NULL in nd6_cache_lladdr");
1633 if (from == NULL)
1634 panic("from == NULL in nd6_cache_lladdr");
1635
1636 /* nothing must be updated for unspecified address */
1637 if (IN6_IS_ADDR_UNSPECIFIED(from))
1638 return NULL;
1639
1640 /*
1641 * Validation about ifp->if_addrlen and lladdrlen must be done in
1642 * the caller.
1643 *
1644 * XXX If the link does not have link-layer adderss, what should
1645 * we do? (ifp->if_addrlen == 0)
1646 * Spec says nothing in sections for RA, RS and NA. There's small
1647 * description on it in NS section (RFC 2461 7.2.3).
1648 */
1649
1650 rt = nd6_lookup(from, 0, ifp);
1651 if (rt == NULL) {
1652 #if 0
1653 /* nothing must be done if there's no lladdr */
1654 if (!lladdr || !lladdrlen)
1655 return NULL;
1656 #endif
1657
1658 rt = nd6_lookup(from, 1, ifp);
1659 is_newentry = 1;
1660 } else {
1661 /* do nothing if static ndp is set */
1662 if (rt->rt_flags & RTF_STATIC)
1663 return NULL;
1664 is_newentry = 0;
1665 }
1666
1667 if (rt == NULL)
1668 return NULL;
1669 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1670 fail:
1671 (void)nd6_free(rt, 0);
1672 return NULL;
1673 }
1674 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1675 if (ln == NULL)
1676 goto fail;
1677 if (rt->rt_gateway == NULL)
1678 goto fail;
1679 if (rt->rt_gateway->sa_family != AF_LINK)
1680 goto fail;
1681 sdl = SDL(rt->rt_gateway);
1682
1683 olladdr = (sdl->sdl_alen) ? 1 : 0;
1684 if (olladdr && lladdr) {
1685 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1686 llchange = 1;
1687 else
1688 llchange = 0;
1689 } else
1690 llchange = 0;
1691
1692 /*
1693 * newentry olladdr lladdr llchange (*=record)
1694 * 0 n n -- (1)
1695 * 0 y n -- (2)
1696 * 0 n y -- (3) * STALE
1697 * 0 y y n (4) *
1698 * 0 y y y (5) * STALE
1699 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1700 * 1 -- y -- (7) * STALE
1701 */
1702
1703 if (lladdr) { /* (3-5) and (7) */
1704 /*
1705 * Record source link-layer address
1706 * XXX is it dependent to ifp->if_type?
1707 */
1708 sdl->sdl_alen = ifp->if_addrlen;
1709 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1710 }
1711
1712 if (!is_newentry) {
1713 if ((!olladdr && lladdr) || /* (3) */
1714 (olladdr && lladdr && llchange)) { /* (5) */
1715 do_update = 1;
1716 newstate = ND6_LLINFO_STALE;
1717 } else /* (1-2,4) */
1718 do_update = 0;
1719 } else {
1720 do_update = 1;
1721 if (lladdr == NULL) /* (6) */
1722 newstate = ND6_LLINFO_NOSTATE;
1723 else /* (7) */
1724 newstate = ND6_LLINFO_STALE;
1725 }
1726
1727 if (do_update) {
1728 /*
1729 * Update the state of the neighbor cache.
1730 */
1731 ln->ln_state = newstate;
1732
1733 if (ln->ln_state == ND6_LLINFO_STALE) {
1734 /*
1735 * XXX: since nd6_output() below will cause
1736 * state tansition to DELAY and reset the timer,
1737 * we must set the timer now, although it is actually
1738 * meaningless.
1739 */
1740 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1741
1742 nd6_llinfo_release_pkts(ln, ifp, rt);
1743 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1744 /* probe right away */
1745 nd6_llinfo_settimer((void *)ln, 0);
1746 }
1747 }
1748
1749 /*
1750 * ICMP6 type dependent behavior.
1751 *
1752 * NS: clear IsRouter if new entry
1753 * RS: clear IsRouter
1754 * RA: set IsRouter if there's lladdr
1755 * redir: clear IsRouter if new entry
1756 *
1757 * RA case, (1):
1758 * The spec says that we must set IsRouter in the following cases:
1759 * - If lladdr exist, set IsRouter. This means (1-5).
1760 * - If it is old entry (!newentry), set IsRouter. This means (7).
1761 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1762 * A quetion arises for (1) case. (1) case has no lladdr in the
1763 * neighbor cache, this is similar to (6).
1764 * This case is rare but we figured that we MUST NOT set IsRouter.
1765 *
1766 * newentry olladdr lladdr llchange NS RS RA redir
1767 * D R
1768 * 0 n n -- (1) c ? s
1769 * 0 y n -- (2) c s s
1770 * 0 n y -- (3) c s s
1771 * 0 y y n (4) c s s
1772 * 0 y y y (5) c s s
1773 * 1 -- n -- (6) c c c s
1774 * 1 -- y -- (7) c c s c s
1775 *
1776 * (c=clear s=set)
1777 */
1778 switch (type & 0xff) {
1779 case ND_NEIGHBOR_SOLICIT:
1780 /*
1781 * New entry must have is_router flag cleared.
1782 */
1783 if (is_newentry) /* (6-7) */
1784 ln->ln_router = 0;
1785 break;
1786 case ND_REDIRECT:
1787 /*
1788 * If the icmp is a redirect to a better router, always set the
1789 * is_router flag. Otherwise, if the entry is newly created,
1790 * clear the flag. [RFC 2461, sec 8.3]
1791 */
1792 if (code == ND_REDIRECT_ROUTER)
1793 ln->ln_router = 1;
1794 else if (is_newentry) /* (6-7) */
1795 ln->ln_router = 0;
1796 break;
1797 case ND_ROUTER_SOLICIT:
1798 /*
1799 * is_router flag must always be cleared.
1800 */
1801 ln->ln_router = 0;
1802 break;
1803 case ND_ROUTER_ADVERT:
1804 /*
1805 * Mark an entry with lladdr as a router.
1806 */
1807 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1808 (is_newentry && lladdr)) { /* (7) */
1809 ln->ln_router = 1;
1810 }
1811 break;
1812 }
1813
1814 /*
1815 * When the link-layer address of a router changes, select the
1816 * best router again. In particular, when the neighbor entry is newly
1817 * created, it might affect the selection policy.
1818 * Question: can we restrict the first condition to the "is_newentry"
1819 * case?
1820 * XXX: when we hear an RA from a new router with the link-layer
1821 * address option, defrouter_select() is called twice, since
1822 * defrtrlist_update called the function as well. However, I believe
1823 * we can compromise the overhead, since it only happens the first
1824 * time.
1825 * XXX: although defrouter_select() should not have a bad effect
1826 * for those are not autoconfigured hosts, we explicitly avoid such
1827 * cases for safety.
1828 */
1829 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1830 defrouter_select();
1831
1832 return rt;
1833 }
1834
1835 static void
1836 nd6_slowtimo(void *ignored_arg)
1837 {
1838 int s = splsoftnet();
1839 struct nd_ifinfo *nd6if;
1840 struct ifnet *ifp;
1841
1842 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1843 nd6_slowtimo, NULL);
1844 TAILQ_FOREACH(ifp, &ifnet, if_list) {
1845 nd6if = ND_IFINFO(ifp);
1846 if (nd6if->basereachable && /* already initialized */
1847 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1848 /*
1849 * Since reachable time rarely changes by router
1850 * advertisements, we SHOULD insure that a new random
1851 * value gets recomputed at least once every few hours.
1852 * (RFC 2461, 6.3.4)
1853 */
1854 nd6if->recalctm = nd6_recalc_reachtm_interval;
1855 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1856 }
1857 }
1858 splx(s);
1859 }
1860
1861 #define senderr(e) { error = (e); goto bad;}
1862 int
1863 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1864 const struct sockaddr_in6 *dst, struct rtentry *rt0)
1865 {
1866 struct mbuf *m = m0;
1867 struct rtentry *rt = rt0;
1868 struct sockaddr_in6 *gw6 = NULL;
1869 struct llinfo_nd6 *ln = NULL;
1870 int error = 0;
1871
1872 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1873 goto sendpkt;
1874
1875 if (nd6_need_cache(ifp) == 0)
1876 goto sendpkt;
1877
1878 /*
1879 * next hop determination. This routine is derived from ether_output.
1880 */
1881 if (rt) {
1882 if ((rt->rt_flags & RTF_UP) == 0) {
1883 if ((rt0 = rt = rtalloc1(sin6tocsa(dst), 1)) != NULL) {
1884 rt->rt_refcnt--;
1885 if (rt->rt_ifp != ifp)
1886 senderr(EHOSTUNREACH);
1887 } else
1888 senderr(EHOSTUNREACH);
1889 }
1890
1891 if (rt->rt_flags & RTF_GATEWAY) {
1892 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1893
1894 /*
1895 * We skip link-layer address resolution and NUD
1896 * if the gateway is not a neighbor from ND point
1897 * of view, regardless of the value of nd_ifinfo.flags.
1898 * The second condition is a bit tricky; we skip
1899 * if the gateway is our own address, which is
1900 * sometimes used to install a route to a p2p link.
1901 */
1902 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1903 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1904 /*
1905 * We allow this kind of tricky route only
1906 * when the outgoing interface is p2p.
1907 * XXX: we may need a more generic rule here.
1908 */
1909 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1910 senderr(EHOSTUNREACH);
1911
1912 goto sendpkt;
1913 }
1914
1915 if (rt->rt_gwroute == 0)
1916 goto lookup;
1917 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1918 rtfree(rt); rt = rt0;
1919 lookup:
1920 rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
1921 if ((rt = rt->rt_gwroute) == 0)
1922 senderr(EHOSTUNREACH);
1923 /* the "G" test below also prevents rt == rt0 */
1924 if ((rt->rt_flags & RTF_GATEWAY) ||
1925 (rt->rt_ifp != ifp)) {
1926 rt->rt_refcnt--;
1927 rt0->rt_gwroute = 0;
1928 senderr(EHOSTUNREACH);
1929 }
1930 }
1931 }
1932 }
1933
1934 /*
1935 * Address resolution or Neighbor Unreachability Detection
1936 * for the next hop.
1937 * At this point, the destination of the packet must be a unicast
1938 * or an anycast address(i.e. not a multicast).
1939 */
1940
1941 /* Look up the neighbor cache for the nexthop */
1942 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1943 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1944 else {
1945 /*
1946 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1947 * the condition below is not very efficient. But we believe
1948 * it is tolerable, because this should be a rare case.
1949 */
1950 if (nd6_is_addr_neighbor(dst, ifp) &&
1951 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1952 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1953 }
1954 if (ln == NULL || rt == NULL) {
1955 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1956 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1957 log(LOG_DEBUG,
1958 "nd6_output: can't allocate llinfo for %s "
1959 "(ln=%p, rt=%p)\n",
1960 ip6_sprintf(&dst->sin6_addr), ln, rt);
1961 senderr(EIO); /* XXX: good error? */
1962 }
1963
1964 goto sendpkt; /* send anyway */
1965 }
1966
1967 /* We don't have to do link-layer address resolution on a p2p link. */
1968 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1969 ln->ln_state < ND6_LLINFO_REACHABLE) {
1970 ln->ln_state = ND6_LLINFO_STALE;
1971 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1972 }
1973
1974 /*
1975 * The first time we send a packet to a neighbor whose entry is
1976 * STALE, we have to change the state to DELAY and a sets a timer to
1977 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1978 * neighbor unreachability detection on expiration.
1979 * (RFC 2461 7.3.3)
1980 */
1981 if (ln->ln_state == ND6_LLINFO_STALE) {
1982 ln->ln_asked = 0;
1983 ln->ln_state = ND6_LLINFO_DELAY;
1984 nd6_llinfo_settimer(ln, (long)nd6_delay * hz);
1985 }
1986
1987 /*
1988 * If the neighbor cache entry has a state other than INCOMPLETE
1989 * (i.e. its link-layer address is already resolved), just
1990 * send the packet.
1991 */
1992 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1993 goto sendpkt;
1994
1995 /*
1996 * There is a neighbor cache entry, but no ethernet address
1997 * response yet. Append this latest packet to the end of the
1998 * packet queue in the mbuf, unless the number of the packet
1999 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen,
2000 * the oldest packet in the queue will be removed.
2001 */
2002 if (ln->ln_state == ND6_LLINFO_NOSTATE)
2003 ln->ln_state = ND6_LLINFO_INCOMPLETE;
2004 if (ln->ln_hold) {
2005 struct mbuf *m_hold;
2006 int i;
2007
2008 i = 0;
2009 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
2010 i++;
2011 if (m_hold->m_nextpkt == NULL) {
2012 m_hold->m_nextpkt = m;
2013 break;
2014 }
2015 }
2016 while (i >= nd6_maxqueuelen) {
2017 m_hold = ln->ln_hold;
2018 ln->ln_hold = ln->ln_hold->m_nextpkt;
2019 m_freem(m_hold);
2020 i--;
2021 }
2022 } else {
2023 ln->ln_hold = m;
2024 }
2025
2026 /*
2027 * If there has been no NS for the neighbor after entering the
2028 * INCOMPLETE state, send the first solicitation.
2029 */
2030 if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
2031 ln->ln_asked++;
2032 nd6_llinfo_settimer(ln,
2033 (long)ND_IFINFO(ifp)->retrans * hz / 1000);
2034 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
2035 }
2036 return 0;
2037
2038 sendpkt:
2039 /* discard the packet if IPv6 operation is disabled on the interface */
2040 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
2041 error = ENETDOWN; /* better error? */
2042 goto bad;
2043 }
2044
2045 #ifdef IPSEC
2046 /* clean ipsec history once it goes out of the node */
2047 ipsec_delaux(m);
2048 #endif
2049 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
2050 return (*ifp->if_output)(origifp, m, sin6tocsa(dst), rt);
2051 }
2052 return (*ifp->if_output)(ifp, m, sin6tocsa(dst), rt);
2053
2054 bad:
2055 if (m != NULL)
2056 m_freem(m);
2057 return error;
2058 }
2059 #undef senderr
2060
2061 int
2062 nd6_need_cache(struct ifnet *ifp)
2063 {
2064 /*
2065 * XXX: we currently do not make neighbor cache on any interface
2066 * other than ARCnet, Ethernet, FDDI and GIF.
2067 *
2068 * RFC2893 says:
2069 * - unidirectional tunnels needs no ND
2070 */
2071 switch (ifp->if_type) {
2072 case IFT_ARCNET:
2073 case IFT_ETHER:
2074 case IFT_FDDI:
2075 case IFT_IEEE1394:
2076 case IFT_CARP:
2077 case IFT_GIF: /* XXX need more cases? */
2078 case IFT_PPP:
2079 case IFT_TUNNEL:
2080 return 1;
2081 default:
2082 return 0;
2083 }
2084 }
2085
2086 int
2087 nd6_storelladdr(const struct ifnet *ifp, const struct rtentry *rt,
2088 struct mbuf *m, const struct sockaddr *dst, u_char *lldst,
2089 size_t dstsize)
2090 {
2091 const struct sockaddr_dl *sdl;
2092
2093 if (m->m_flags & M_MCAST) {
2094 switch (ifp->if_type) {
2095 case IFT_ETHER:
2096 case IFT_FDDI:
2097 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr, lldst);
2098 return 1;
2099 case IFT_IEEE1394:
2100 bcopy(ifp->if_broadcastaddr, lldst, ifp->if_addrlen);
2101 return 1;
2102 case IFT_ARCNET:
2103 *lldst = 0;
2104 return 1;
2105 default:
2106 m_freem(m);
2107 return 0;
2108 }
2109 }
2110
2111 if (rt == NULL) {
2112 /* this could happen, if we could not allocate memory */
2113 m_freem(m);
2114 return 0;
2115 }
2116 if (rt->rt_gateway->sa_family != AF_LINK) {
2117 printf("%s: something odd happens\n", __func__);
2118 m_freem(m);
2119 return 0;
2120 }
2121 sdl = SDL(rt->rt_gateway);
2122 if (sdl->sdl_alen == 0 || sdl->sdl_alen > dstsize) {
2123 /* this should be impossible, but we bark here for debugging */
2124 printf("%s: sdl_alen == 0, dst=%s, if=%s\n", __func__,
2125 ip6_sprintf(&SIN6(dst)->sin6_addr), if_name(ifp));
2126 m_freem(m);
2127 return 0;
2128 }
2129
2130 memcpy(lldst, CLLADDR(sdl), MIN(dstsize, sdl->sdl_alen));
2131 return 1;
2132 }
2133
2134 static void
2135 clear_llinfo_pqueue(struct llinfo_nd6 *ln)
2136 {
2137 struct mbuf *m_hold, *m_hold_next;
2138
2139 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
2140 m_hold_next = m_hold->m_nextpkt;
2141 m_hold->m_nextpkt = NULL;
2142 m_freem(m_hold);
2143 }
2144
2145 ln->ln_hold = NULL;
2146 return;
2147 }
2148
2149 int
2150 nd6_sysctl(
2151 int name,
2152 void *oldp, /* syscall arg, need copyout */
2153 size_t *oldlenp,
2154 void *newp, /* syscall arg, need copyin */
2155 size_t newlen
2156 )
2157 {
2158 void *p;
2159 size_t ol;
2160 int error;
2161
2162 error = 0;
2163
2164 if (newp)
2165 return EPERM;
2166 if (oldp && !oldlenp)
2167 return EINVAL;
2168 ol = oldlenp ? *oldlenp : 0;
2169
2170 if (oldp) {
2171 p = malloc(*oldlenp, M_TEMP, M_WAITOK);
2172 if (p == NULL)
2173 return ENOMEM;
2174 } else
2175 p = NULL;
2176 switch (name) {
2177 case ICMPV6CTL_ND6_DRLIST:
2178 error = fill_drlist(p, oldlenp, ol);
2179 if (!error && p != NULL && oldp != NULL)
2180 error = copyout(p, oldp, *oldlenp);
2181 break;
2182
2183 case ICMPV6CTL_ND6_PRLIST:
2184 error = fill_prlist(p, oldlenp, ol);
2185 if (!error && p != NULL && oldp != NULL)
2186 error = copyout(p, oldp, *oldlenp);
2187 break;
2188
2189 case ICMPV6CTL_ND6_MAXQLEN:
2190 break;
2191
2192 default:
2193 error = ENOPROTOOPT;
2194 break;
2195 }
2196 if (p)
2197 free(p, M_TEMP);
2198
2199 return error;
2200 }
2201
2202 static int
2203 fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
2204 {
2205 int error = 0, s;
2206 struct in6_defrouter *d = NULL, *de = NULL;
2207 struct nd_defrouter *dr;
2208 size_t l;
2209
2210 s = splsoftnet();
2211
2212 if (oldp) {
2213 d = (struct in6_defrouter *)oldp;
2214 de = (struct in6_defrouter *)((char *)oldp + *oldlenp);
2215 }
2216 l = 0;
2217
2218 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
2219
2220 if (oldp && d + 1 <= de) {
2221 bzero(d, sizeof(*d));
2222 d->rtaddr.sin6_family = AF_INET6;
2223 d->rtaddr.sin6_len = sizeof(struct sockaddr_in6);
2224 d->rtaddr.sin6_addr = dr->rtaddr;
2225 if (sa6_recoverscope(&d->rtaddr)) {
2226 log(LOG_ERR,
2227 "scope error in router list (%s)\n",
2228 ip6_sprintf(&d->rtaddr.sin6_addr));
2229 /* XXX: press on... */
2230 }
2231 d->flags = dr->flags;
2232 d->rtlifetime = dr->rtlifetime;
2233 d->expire = dr->expire;
2234 d->if_index = dr->ifp->if_index;
2235 }
2236
2237 l += sizeof(*d);
2238 if (d)
2239 d++;
2240 }
2241
2242 if (oldp) {
2243 if (l > ol)
2244 error = ENOMEM;
2245 }
2246 if (oldlenp)
2247 *oldlenp = l; /* (void *)d - (void *)oldp */
2248
2249 splx(s);
2250
2251 return error;
2252 }
2253
2254 static int
2255 fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
2256 {
2257 int error = 0, s;
2258 struct nd_prefix *pr;
2259 struct in6_prefix *p = NULL;
2260 struct in6_prefix *pe = NULL;
2261 size_t l;
2262
2263 s = splsoftnet();
2264
2265 if (oldp) {
2266 p = (struct in6_prefix *)oldp;
2267 pe = (struct in6_prefix *)((char *)oldp + *oldlenp);
2268 }
2269 l = 0;
2270
2271 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
2272 u_short advrtrs;
2273 size_t advance;
2274 struct sockaddr_in6 *sin6;
2275 struct sockaddr_in6 *s6;
2276 struct nd_pfxrouter *pfr;
2277
2278 if (oldp && p + 1 <= pe)
2279 {
2280 bzero(p, sizeof(*p));
2281 sin6 = (struct sockaddr_in6 *)(p + 1);
2282
2283 p->prefix = pr->ndpr_prefix;
2284 if (sa6_recoverscope(&p->prefix)) {
2285 log(LOG_ERR,
2286 "scope error in prefix list (%s)\n",
2287 ip6_sprintf(&p->prefix.sin6_addr));
2288 /* XXX: press on... */
2289 }
2290 p->raflags = pr->ndpr_raf;
2291 p->prefixlen = pr->ndpr_plen;
2292 p->vltime = pr->ndpr_vltime;
2293 p->pltime = pr->ndpr_pltime;
2294 p->if_index = pr->ndpr_ifp->if_index;
2295 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2296 p->expire = 0;
2297 else {
2298 time_t maxexpire;
2299
2300 /* XXX: we assume time_t is signed. */
2301 maxexpire = (-1) &
2302 ~((time_t)1 <<
2303 ((sizeof(maxexpire) * 8) - 1));
2304 if (pr->ndpr_vltime <
2305 maxexpire - pr->ndpr_lastupdate) {
2306 p->expire = pr->ndpr_lastupdate +
2307 pr->ndpr_vltime;
2308 } else
2309 p->expire = maxexpire;
2310 }
2311 p->refcnt = pr->ndpr_refcnt;
2312 p->flags = pr->ndpr_stateflags;
2313 p->origin = PR_ORIG_RA;
2314 advrtrs = 0;
2315 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2316 if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2317 advrtrs++;
2318 continue;
2319 }
2320 s6 = &sin6[advrtrs];
2321 s6->sin6_family = AF_INET6;
2322 s6->sin6_len = sizeof(struct sockaddr_in6);
2323 s6->sin6_addr = pfr->router->rtaddr;
2324 s6->sin6_scope_id = 0;
2325 if (sa6_recoverscope(s6)) {
2326 log(LOG_ERR,
2327 "scope error in "
2328 "prefix list (%s)\n",
2329 ip6_sprintf(&pfr->router->rtaddr));
2330 }
2331 advrtrs++;
2332 }
2333 p->advrtrs = advrtrs;
2334 }
2335 else {
2336 advrtrs = 0;
2337 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
2338 advrtrs++;
2339 }
2340
2341 advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2342 l += advance;
2343 if (p)
2344 p = (struct in6_prefix *)((char *)p + advance);
2345 }
2346
2347 if (oldp) {
2348 *oldlenp = l; /* (void *)d - (void *)oldp */
2349 if (l > ol)
2350 error = ENOMEM;
2351 } else
2352 *oldlenp = l;
2353
2354 splx(s);
2355
2356 return error;
2357 }
2358