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