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