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