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