1 /* $FreeBSD: src/sys/netinet6/nd6.c,v 1.2.2.9 2001/07/11 09:39:04 ume Exp $ */
2 /* $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * BSD/OS version heavily modifies this code, related to llinfo.
37 * Since we don't have BSD/OS version of net/route.c in our hand,
38 * I left the code mostly as it was in 970310. -- itojun
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
48 #include <sys/kernel.h>
49 #include <sys/errno.h>
50 #include <sys/syslog.h>
51 #include <sys/protosw.h>
52 #include <kern/queue.h>
54 #define DONT_WARN_OBSOLETE
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/if_atm.h>
59 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/if_ether.h>
64 #include <netinet/if_fddi.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/ip6_var.h>
68 #include <netinet6/nd6.h>
69 #include <netinet6/in6_prefix.h>
70 #include <netinet/icmp6.h>
74 #include <net/net_osdep.h>
76 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
77 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
79 #define SIN6(s) ((struct sockaddr_in6 *)s)
80 #define SDL(s) ((struct sockaddr_dl *)s)
83 int nd6_prune
= 1; /* walk list every 1 seconds */
84 int nd6_delay
= 5; /* delay first probe time 5 second */
85 int nd6_umaxtries
= 3; /* maximum unicast query */
86 int nd6_mmaxtries
= 3; /* maximum multicast query */
87 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
88 int nd6_gctimer
= (60 * 60 * 24); /* 1 day: garbage collection timer */
90 /* preventing too many loops in ND option parsing */
91 int nd6_maxndopt
= 10; /* max # of ND options allowed */
93 int nd6_maxnudhint
= 0; /* max # of subsequent upper layer hints */
102 static int nd6_inuse
, nd6_allocated
;
104 struct llinfo_nd6 llinfo_nd6
= {&llinfo_nd6
, &llinfo_nd6
};
105 static size_t nd_ifinfo_indexlim
= 8;
106 struct nd_ifinfo
*nd_ifinfo
= NULL
;
107 struct nd_drhead nd_defrouter
;
108 struct nd_prhead nd_prefix
= { 0 };
110 int nd6_recalc_reachtm_interval
= ND6_RECALC_REACHTM_INTERVAL
;
111 static struct sockaddr_in6 all1_sa
;
113 static void nd6_slowtimo_funneled
__P((void *));
114 static int regen_tmpaddr
__P((struct in6_ifaddr
*));
120 static int nd6_init_done
= 0;
124 log(LOG_NOTICE
, "nd6_init called more than once(ignored)\n");
128 all1_sa
.sin6_family
= AF_INET6
;
129 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
130 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++)
131 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
133 /* initialization of the default router list */
134 TAILQ_INIT(&nd_defrouter
);
139 timeout(nd6_slowtimo_funneled
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
148 * We have some arrays that should be indexed by if_index.
149 * since if_index will grow dynamically, they should grow too.
151 if (nd_ifinfo
== NULL
|| if_index
>= nd_ifinfo_indexlim
) {
155 while (if_index
>= nd_ifinfo_indexlim
)
156 nd_ifinfo_indexlim
<<= 1;
159 n
= nd_ifinfo_indexlim
* sizeof(struct nd_ifinfo
);
160 q
= (caddr_t
)_MALLOC(n
, M_IP6NDP
, M_WAITOK
);
163 bcopy((caddr_t
)nd_ifinfo
, q
, n
/2);
164 FREE((caddr_t
)nd_ifinfo
, M_IP6NDP
);
166 nd_ifinfo
= (struct nd_ifinfo
*)q
;
169 #define ND nd_ifinfo[ifp->if_index]
172 * Don't initialize if called twice.
173 * XXX: to detect this, we should choose a member that is never set
174 * before initialization of the ND structure itself. We formaly used
175 * the linkmtu member, which was not suitable because it could be
176 * initialized via "ifconfig mtu".
178 if (ND
.basereachable
)
181 ND
.linkmtu
= ifindex2ifnet
[ifp
->if_index
]->if_mtu
;
182 ND
.chlim
= IPV6_DEFHLIM
;
183 ND
.basereachable
= REACHABLE_TIME
;
184 ND
.reachable
= ND_COMPUTE_RTIME(ND
.basereachable
);
185 ND
.retrans
= RETRANS_TIMER
;
187 ND
.flags
= ND6_IFF_PERFORMNUD
;
193 * Reset ND level link MTU. This function is called when the physical MTU
194 * changes, which means we might have to adjust the ND level MTU.
201 #define MIN(a,b) ((a) < (b) ? (a) : (b))
203 struct nd_ifinfo
*ndi
= &nd_ifinfo
[ifp
->if_index
];
204 u_long oldmaxmtu
= ndi
->maxmtu
;
205 u_long oldlinkmtu
= ndi
->linkmtu
;
207 switch (ifp
->if_type
) {
208 case IFT_ARCNET
: /* XXX MTU handling needs more work */
209 ndi
->maxmtu
= MIN(60480, ifp
->if_mtu
);
212 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
215 ndi
->maxmtu
= MIN(FDDIIPMTU
, ifp
->if_mtu
);
218 ndi
->maxmtu
= MIN(ATMMTU
, ifp
->if_mtu
);
220 case IFT_IEEE1394
: /* XXX should be IEEE1394MTU(1500) */
221 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
224 case IFT_IEEE80211
: /* XXX should be IEEE80211MTU(1500) */
225 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
229 ndi
->maxmtu
= ifp
->if_mtu
;
233 if (oldmaxmtu
!= ndi
->maxmtu
) {
235 * If the ND level MTU is not set yet, or if the maxmtu
236 * is reset to a smaller value than the ND level MTU,
237 * also reset the ND level MTU.
239 if (ndi
->linkmtu
== 0 ||
240 ndi
->maxmtu
< ndi
->linkmtu
) {
241 ndi
->linkmtu
= ndi
->maxmtu
;
242 /* also adjust in6_maxmtu if necessary. */
243 if (oldlinkmtu
== 0) {
245 * XXX: the case analysis is grotty, but
246 * it is not efficient to call in6_setmaxmtu()
247 * here when we are during the initialization
250 if (in6_maxmtu
< ndi
->linkmtu
)
251 in6_maxmtu
= ndi
->linkmtu
;
260 nd6_option_init(opt
, icmp6len
, ndopts
)
263 union nd_opts
*ndopts
;
265 bzero(ndopts
, sizeof(*ndopts
));
266 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
268 = (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
271 ndopts
->nd_opts_done
= 1;
272 ndopts
->nd_opts_search
= NULL
;
277 * Take one ND option.
281 union nd_opts
*ndopts
;
283 struct nd_opt_hdr
*nd_opt
;
287 panic("ndopts == NULL in nd6_option\n");
288 if (!ndopts
->nd_opts_last
)
289 panic("uninitialized ndopts in nd6_option\n");
290 if (!ndopts
->nd_opts_search
)
292 if (ndopts
->nd_opts_done
)
295 nd_opt
= ndopts
->nd_opts_search
;
297 /* make sure nd_opt_len is inside the buffer */
298 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
299 bzero(ndopts
, sizeof(*ndopts
));
303 olen
= nd_opt
->nd_opt_len
<< 3;
306 * Message validation requires that all included
307 * options have a length that is greater than zero.
309 bzero(ndopts
, sizeof(*ndopts
));
313 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
314 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
315 /* option overruns the end of buffer, invalid */
316 bzero(ndopts
, sizeof(*ndopts
));
318 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
319 /* reached the end of options chain */
320 ndopts
->nd_opts_done
= 1;
321 ndopts
->nd_opts_search
= NULL
;
327 * Parse multiple ND options.
328 * This function is much easier to use, for ND routines that do not need
329 * multiple options of the same type.
333 union nd_opts
*ndopts
;
335 struct nd_opt_hdr
*nd_opt
;
339 panic("ndopts == NULL in nd6_options\n");
340 if (!ndopts
->nd_opts_last
)
341 panic("uninitialized ndopts in nd6_options\n");
342 if (!ndopts
->nd_opts_search
)
346 nd_opt
= nd6_option(ndopts
);
347 if (!nd_opt
&& !ndopts
->nd_opts_last
) {
349 * Message validation requires that all included
350 * options have a length that is greater than zero.
352 icmp6stat
.icp6s_nd_badopt
++;
353 bzero(ndopts
, sizeof(*ndopts
));
360 switch (nd_opt
->nd_opt_type
) {
361 case ND_OPT_SOURCE_LINKADDR
:
362 case ND_OPT_TARGET_LINKADDR
:
364 case ND_OPT_REDIRECTED_HEADER
:
365 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
367 "duplicated ND6 option found (type=%d)\n",
368 nd_opt
->nd_opt_type
));
371 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
375 case ND_OPT_PREFIX_INFORMATION
:
376 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
377 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
380 ndopts
->nd_opts_pi_end
=
381 (struct nd_opt_prefix_info
*)nd_opt
;
385 * Unknown options must be silently ignored,
386 * to accomodate future extension to the protocol.
389 "nd6_options: unsupported option %d - "
390 "option ignored\n", nd_opt
->nd_opt_type
));
395 if (i
> nd6_maxndopt
) {
396 icmp6stat
.icp6s_nd_toomanyopt
++;
397 nd6log((LOG_INFO
, "too many loop in nd opt\n"));
401 if (ndopts
->nd_opts_done
)
409 * ND6 timer routine to expire default route list and prefix list
412 nd6_timer_funneled(ignored_arg
)
416 boolean_t funnel_state
;
417 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
419 nd6_timer(ignored_arg
);
421 (void) thread_funnel_set(network_flock
, FALSE
);
425 nd6_timer(ignored_arg
)
429 struct llinfo_nd6
*ln
;
430 struct nd_defrouter
*dr
;
431 struct nd_prefix
*pr
;
433 struct in6_ifaddr
*ia6
, *nia6
;
434 struct in6_addrlifetime
*lt6
;
438 timeout(nd6_timer_funneled
, (caddr_t
)0, nd6_prune
* hz
);
440 ln
= llinfo_nd6
.ln_next
;
441 /* XXX BSD/OS separates this code -- itojun */
442 while (ln
&& ln
!= &llinfo_nd6
) {
444 struct sockaddr_in6
*dst
;
445 struct llinfo_nd6
*next
= ln
->ln_next
;
446 /* XXX: used for the DELAY case only: */
447 struct nd_ifinfo
*ndi
= NULL
;
449 if ((rt
= ln
->ln_rt
) == NULL
) {
453 if ((ifp
= rt
->rt_ifp
) == NULL
) {
457 ndi
= &nd_ifinfo
[ifp
->if_index
];
458 dst
= (struct sockaddr_in6
*)rt_key(rt
);
460 if (ln
->ln_expire
> time_second
) {
467 panic("rt=0 in nd6_timer(ln=%p)\n", ln
);
468 if (rt
->rt_llinfo
&& (struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
)
469 panic("rt_llinfo(%p) is not equal to ln(%p)\n",
472 panic("dst=0 in nd6_timer(ln=%p)\n", ln
);
474 switch (ln
->ln_state
) {
475 case ND6_LLINFO_INCOMPLETE
:
476 if (ln
->ln_asked
< nd6_mmaxtries
) {
478 ln
->ln_expire
= time_second
+
479 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
480 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
,
483 struct mbuf
*m
= ln
->ln_hold
;
487 * Fake rcvif to make ICMP error
488 * more helpful in diagnosing
490 * XXX: should we consider
493 m
->m_pkthdr
.rcvif
= rt
->rt_ifp
;
495 icmp6_error(m
, ICMP6_DST_UNREACH
,
496 ICMP6_DST_UNREACH_ADDR
, 0);
502 case ND6_LLINFO_REACHABLE
:
504 ln
->ln_state
= ND6_LLINFO_STALE
;
505 ln
->ln_expire
= time_second
+ nd6_gctimer
;
509 case ND6_LLINFO_STALE
:
510 /* Garbage Collection(RFC 2461 5.3) */
515 case ND6_LLINFO_DELAY
:
516 if (ndi
&& (ndi
->flags
& ND6_IFF_PERFORMNUD
) != 0) {
519 ln
->ln_state
= ND6_LLINFO_PROBE
;
520 ln
->ln_expire
= time_second
+
522 nd6_ns_output(ifp
, &dst
->sin6_addr
,
526 ln
->ln_state
= ND6_LLINFO_STALE
; /* XXX */
527 ln
->ln_expire
= time_second
+ nd6_gctimer
;
530 case ND6_LLINFO_PROBE
:
531 if (ln
->ln_asked
< nd6_umaxtries
) {
533 ln
->ln_expire
= time_second
+
534 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
535 nd6_ns_output(ifp
, &dst
->sin6_addr
,
536 &dst
->sin6_addr
, ln
, 0);
545 /* expire default router list */
546 dr
= TAILQ_FIRST(&nd_defrouter
);
548 if (dr
->expire
&& dr
->expire
< time_second
) {
549 struct nd_defrouter
*t
;
550 t
= TAILQ_NEXT(dr
, dr_entry
);
554 dr
= TAILQ_NEXT(dr
, dr_entry
);
559 * expire interface addresses.
560 * in the past the loop was inside prefix expiry processing.
561 * However, from a stricter speci-confrmance standpoint, we should
562 * rather separate address lifetimes and prefix lifetimes.
565 for (ia6
= in6_ifaddr
; ia6
; ia6
= nia6
) {
567 /* check address lifetime */
568 lt6
= &ia6
->ia6_lifetime
;
569 if (IFA6_IS_INVALID(ia6
)) {
573 * If the expiring address is temporary, try
574 * regenerating a new one. This would be useful when
575 * we suspended a laptop PC, then turned on after a
576 * period that could invalidate all temporary
577 * addresses. Although we may have to restart the
578 * loop (see below), it must be after purging the
579 * address. Otherwise, we'd see an infinite loop of
582 if (ip6_use_tempaddr
&&
583 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
584 if (regen_tmpaddr(ia6
) == 0)
588 in6_purgeaddr(&ia6
->ia_ifa
);
591 goto addrloop
; /* XXX: see below */
592 } else if (IFA6_IS_DEPRECATED(ia6
)) {
593 int oldflags
= ia6
->ia6_flags
;
595 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
598 * If a temporary address has just become deprecated,
599 * regenerate a new one if possible.
601 if (ip6_use_tempaddr
&&
602 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
603 (oldflags
& IN6_IFF_DEPRECATED
) == 0) {
605 if (regen_tmpaddr(ia6
) == 0) {
607 * A new temporary address is
609 * XXX: this means the address chain
610 * has changed while we are still in
611 * the loop. Although the change
612 * would not cause disaster (because
613 * it's not an addition, but a
614 * deletion,) we'd rather restart the
615 * loop just for safety. Or does this
616 * significantly reduce performance??
621 } else if (IFA6_IS_DEPRECATED(ia6
)) {
623 * A new RA might have made a deprecated address
626 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
630 /* expire prefix list */
631 pr
= nd_prefix
.lh_first
;
634 * check prefix lifetime.
635 * since pltime is just for autoconf, pltime processing for
636 * prefix is not necessary.
638 * we offset expire time by NDPR_KEEP_EXPIRE, so that we
639 * can use the old prefix information to validate the
640 * next prefix information to come. See prelist_update()
641 * for actual validation.
643 * I don't think such an offset is necessary.
644 * (jinmei@kame.net, 20010130).
646 if (pr
->ndpr_expire
&& pr
->ndpr_expire
< time_second
) {
651 * address expiration and prefix expiration are
652 * separate. NEVER perform in6_purgeaddr here.
665 struct in6_ifaddr
*ia6
; /* deprecated/invalidated temporary address */
669 struct in6_ifaddr
*public_ifa6
= NULL
;
671 ifp
= ia6
->ia_ifa
.ifa_ifp
;
672 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
673 ifa
= ifa
->ifa_list
.tqe_next
)
675 struct in6_ifaddr
*it6
;
677 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
680 it6
= (struct in6_ifaddr
*)ifa
;
682 /* ignore no autoconf addresses. */
683 if ((it6
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
686 /* ignore autoconf addresses with different prefixes. */
687 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
)
691 * Now we are looking at an autoconf address with the same
692 * prefix as ours. If the address is temporary and is still
693 * preferred, do not create another one. It would be rare, but
694 * could happen, for example, when we resume a laptop PC after
697 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
698 !IFA6_IS_DEPRECATED(it6
)) {
704 * This is a public autoconf address that has the same prefix
705 * as ours. If it is preferred, keep it. We can't break the
706 * loop here, because there may be a still-preferred temporary
707 * address with the prefix.
709 if (!IFA6_IS_DEPRECATED(it6
))
713 if (public_ifa6
!= NULL
) {
716 if ((e
= in6_tmpifadd(public_ifa6
, 0)) != 0) {
717 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
718 " tmp addr,errno=%d\n", e
);
728 * Nuke neighbor cache/prefix/default router management table, right before
735 struct llinfo_nd6
*ln
, *nln
;
736 struct nd_defrouter
*dr
, *ndr
, drany
;
737 struct nd_prefix
*pr
, *npr
;
739 /* Nuke default router list entries toward ifp */
740 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
742 * The first entry of the list may be stored in
743 * the routing table, so we'll delete it later.
745 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= ndr
) {
746 ndr
= TAILQ_NEXT(dr
, dr_entry
);
750 dr
= TAILQ_FIRST(&nd_defrouter
);
755 /* Nuke prefix list entries toward ifp */
756 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
758 if (pr
->ndpr_ifp
== ifp
) {
760 * Previously, pr->ndpr_addr is removed as well,
761 * but I strongly believe we don't have to do it.
762 * nd6_purge() is only called from in6_ifdetach(),
763 * which removes all the associated interface addresses
765 * (jinmei@kame.net 20010129)
771 /* cancel default outgoing interface setting */
772 if (nd6_defifindex
== ifp
->if_index
)
773 nd6_setdefaultiface(0);
775 if (!ip6_forwarding
&& ip6_accept_rtadv
) { /* XXX: too restrictive? */
776 /* refresh default router list */
777 bzero(&drany
, sizeof(drany
));
778 defrouter_delreq(&drany
, 0);
783 * Nuke neighbor cache entries for the ifp.
784 * Note that rt->rt_ifp may not be the same as ifp,
785 * due to KAME goto ours hack. See RTM_RESOLVE case in
786 * nd6_rtrequest(), and ip6_input().
788 ln
= llinfo_nd6
.ln_next
;
789 while (ln
&& ln
!= &llinfo_nd6
) {
791 struct sockaddr_dl
*sdl
;
795 if (rt
&& rt
->rt_gateway
&&
796 rt
->rt_gateway
->sa_family
== AF_LINK
) {
797 sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
;
798 if (sdl
->sdl_index
== ifp
->if_index
)
806 nd6_lookup(addr6
, create
, ifp
)
807 struct in6_addr
*addr6
;
812 struct sockaddr_in6 sin6
;
814 bzero(&sin6
, sizeof(sin6
));
815 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
816 sin6
.sin6_family
= AF_INET6
;
817 sin6
.sin6_addr
= *addr6
;
819 sin6
.sin6_scope_id
= in6_addr2scopeid(ifp
, addr6
);
821 rt
= rtalloc1((struct sockaddr
*)&sin6
, create
, 0UL);
822 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) == 0) {
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
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.
846 ifaof_ifpforaddr((struct sockaddr
*)&sin6
, ifp
);
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 rtequest via ifa->ifa_rtrequest.
856 if ((e
= rtrequest(RTM_ADD
, (struct sockaddr
*)&sin6
,
858 (struct sockaddr
*)&all1_sa
,
860 RTF_HOST
| RTF_LLINFO
) &
864 "nd6_lookup: failed to add route for a "
865 "neighbor(%s), errno=%d\n",
866 ip6_sprintf(addr6
), e
);
870 struct llinfo_nd6
*ln
=
871 (struct llinfo_nd6
*)rt
->rt_llinfo
;
872 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
879 * Validation for the entry.
880 * XXX: we can't use rt->rt_ifp to check for the interface, since
881 * it might be the loopback interface if the entry is for our
882 * own address on a non-loopback interface. Instead, we should
883 * use rt->rt_ifa->ifa_ifp, which would specify the REAL interface.
885 if ((rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
886 rt
->rt_gateway
->sa_family
!= AF_LINK
||
887 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
)) {
889 log(LOG_DEBUG
, "nd6_lookup: failed to lookup %s (if = %s)\n",
890 ip6_sprintf(addr6
), ifp
? if_name(ifp
) : "unspec");
891 /* xxx more logs... kazu */
899 * Detect if a given IPv6 address identifies a neighbor on a given link.
900 * XXX: should take care of the destination of a p2p link?
903 nd6_is_addr_neighbor(addr
, ifp
)
904 struct sockaddr_in6
*addr
;
910 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
911 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
914 * A link-local address is always a neighbor.
915 * XXX: we should use the sin6_scope_id field rather than the embedded
918 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
) &&
919 ntohs(*(u_int16_t
*)&addr
->sin6_addr
.s6_addr
[2]) == ifp
->if_index
)
923 * If the address matches one of our addresses,
924 * it should be a neighbor.
926 for (ifa
= ifp
->if_addrlist
.tqh_first
;
928 ifa
= ifa
->ifa_list
.tqe_next
)
930 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
933 for (i
= 0; i
< 4; i
++) {
934 if ((IFADDR6(ifa
).s6_addr32
[i
] ^
935 addr
->sin6_addr
.s6_addr32
[i
]) &
936 IFMASK6(ifa
).s6_addr32
[i
])
943 * Even if the address matches none of our addresses, it might be
944 * in the neighbor cache.
946 if (nd6_lookup(&addr
->sin6_addr
, 0, ifp
))
955 * Free an nd6 llinfo entry.
961 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
, *next
;
962 struct in6_addr in6
= ((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
963 struct nd_defrouter
*dr
;
966 * we used to have pfctlinput(PRC_HOSTDEAD) here.
967 * even though it is not harmful, it was not really necessary.
970 if (!ip6_forwarding
&& ip6_accept_rtadv
) { /* XXX: too restrictive? */
973 dr
= defrouter_lookup(&((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
,
976 if (ln
->ln_router
|| dr
) {
978 * rt6_flush must be called whether or not the neighbor
979 * is in the Default Router List.
980 * See a corresponding comment in nd6_na_input().
982 rt6_flush(&in6
, rt
->rt_ifp
);
987 * Unreachablity of a router might affect the default
988 * router selection and on-link detection of advertised
993 * Temporarily fake the state to choose a new default
994 * router and to perform on-link determination of
995 * prefixes coreectly.
996 * Below the state will be set correctly,
997 * or the entry itself will be deleted.
999 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1002 * Since defrouter_select() does not affect the
1003 * on-link determination and MIP6 needs the check
1004 * before the default router selection, we perform
1007 pfxlist_onlink_check();
1009 if (dr
== TAILQ_FIRST(&nd_defrouter
)) {
1011 * It is used as the current default router,
1012 * so we have to move it to the end of the
1013 * list and choose a new one.
1014 * XXX: it is not very efficient if this is
1017 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1018 TAILQ_INSERT_TAIL(&nd_defrouter
, dr
, dr_entry
);
1027 * Before deleting the entry, remember the next entry as the
1028 * return value. We need this because pfxlist_onlink_check() above
1029 * might have freed other entries (particularly the old next entry) as
1030 * a side effect (XXX).
1035 * Detach the route from the routing tree and the list of neighbor
1036 * caches, and disable the route entry not to be used in already
1039 rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
1040 rt_mask(rt
), 0, (struct rtentry
**)0);
1046 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1048 * XXX cost-effective metods?
1051 nd6_nud_hint(rt
, dst6
, force
)
1053 struct in6_addr
*dst6
;
1056 struct llinfo_nd6
*ln
;
1059 * If the caller specified "rt", use that. Otherwise, resolve the
1060 * routing table by supplied "dst6".
1065 if (!(rt
= nd6_lookup(dst6
, 0, NULL
)))
1069 if ((rt
->rt_flags
& RTF_GATEWAY
) != 0 ||
1070 (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1071 !rt
->rt_llinfo
|| !rt
->rt_gateway
||
1072 rt
->rt_gateway
->sa_family
!= AF_LINK
) {
1073 /* This is not a host route. */
1077 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1078 if (ln
->ln_state
< ND6_LLINFO_REACHABLE
)
1082 * if we get upper-layer reachability confirmation many times,
1083 * it is possible we have false information.
1087 if (ln
->ln_byhint
> nd6_maxnudhint
)
1091 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1093 ln
->ln_expire
= time_second
+
1094 nd_ifinfo
[rt
->rt_ifp
->if_index
].reachable
;
1098 nd6_rtrequest(req
, rt
, sa
)
1101 struct sockaddr
*sa
; /* xxx unused */
1103 struct sockaddr
*gate
= rt
->rt_gateway
;
1104 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1105 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
};
1106 struct ifnet
*ifp
= rt
->rt_ifp
;
1109 if (rt
->rt_flags
& RTF_GATEWAY
)
1112 if (nd6_need_cache(ifp
) == 0 && (rt
->rt_flags
& RTF_HOST
) == 0) {
1114 * This is probably an interface direct route for a link
1115 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1116 * We do not need special treatment below for such a route.
1117 * Moreover, the RTF_LLINFO flag which would be set below
1118 * would annoy the ndp(8) command.
1126 * There is no backward compatibility :)
1128 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1129 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1130 * rt->rt_flags |= RTF_CLONING;
1132 if (rt
->rt_flags
& (RTF_CLONING
| RTF_LLINFO
)) {
1134 * Case 1: This route should come from
1135 * a route to interface. RTF_LLINFO flag is set
1136 * for a host route whose destination should be
1137 * treated as on-link.
1139 rt_setgate(rt
, rt_key(rt
),
1140 (struct sockaddr
*)&null_sdl
);
1141 gate
= rt
->rt_gateway
;
1142 SDL(gate
)->sdl_type
= ifp
->if_type
;
1143 SDL(gate
)->sdl_index
= ifp
->if_index
;
1145 ln
->ln_expire
= time_second
;
1147 if (ln
&& ln
->ln_expire
== 0) {
1148 /* kludge for desktops */
1150 printf("nd6_request: time.tv_sec is zero; "
1156 if (rt
->rt_flags
& RTF_CLONING
)
1160 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1161 * We don't do that here since llinfo is not ready yet.
1163 * There are also couple of other things to be discussed:
1164 * - unsolicited NA code needs improvement beforehand
1165 * - RFC2461 says we MAY send multicast unsolicited NA
1166 * (7.2.6 paragraph 4), however, it also says that we
1167 * SHOULD provide a mechanism to prevent multicast NA storm.
1168 * we don't have anything like it right now.
1169 * note that the mechanism needs a mutual agreement
1170 * between proxies, which means that we need to implement
1171 * a new protocol, or a new kludge.
1172 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1173 * we need to check ip6forwarding before sending it.
1174 * (or should we allow proxy ND configuration only for
1175 * routers? there's no mention about proxy ND from hosts)
1178 /* XXX it does not work */
1179 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1181 &SIN6(rt_key(rt
))->sin6_addr
,
1182 &SIN6(rt_key(rt
))->sin6_addr
,
1183 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1188 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) == 0) {
1190 * Address resolution isn't necessary for a point to
1191 * point link, so we can skip this test for a p2p link.
1193 if (gate
->sa_family
!= AF_LINK
||
1194 gate
->sa_len
< sizeof(null_sdl
)) {
1196 "nd6_rtrequest: bad gateway value: %s\n",
1200 SDL(gate
)->sdl_type
= ifp
->if_type
;
1201 SDL(gate
)->sdl_index
= ifp
->if_index
;
1204 break; /* This happens on a route change */
1206 * Case 2: This route may come from cloning, or a manual route
1207 * add with a LL address.
1209 R_Malloc(ln
, struct llinfo_nd6
*, sizeof(*ln
));
1210 rt
->rt_llinfo
= (caddr_t
)ln
;
1212 log(LOG_DEBUG
, "nd6_rtrequest: malloc failed\n");
1217 Bzero(ln
, sizeof(*ln
));
1219 /* this is required for "ndp" command. - shin */
1220 if (req
== RTM_ADD
) {
1222 * gate should have some valid AF_LINK entry,
1223 * and ln->ln_expire should have some lifetime
1224 * which is specified by ndp command.
1226 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1230 * When req == RTM_RESOLVE, rt is created and
1231 * initialized in rtrequest(), so rt_expire is 0.
1233 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1234 ln
->ln_expire
= time_second
;
1236 rt
->rt_flags
|= RTF_LLINFO
;
1237 ln
->ln_next
= llinfo_nd6
.ln_next
;
1238 llinfo_nd6
.ln_next
= ln
;
1239 ln
->ln_prev
= &llinfo_nd6
;
1240 ln
->ln_next
->ln_prev
= ln
;
1243 * check if rt_key(rt) is one of my address assigned
1246 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1247 &SIN6(rt_key(rt
))->sin6_addr
);
1249 caddr_t macp
= nd6_ifptomac(ifp
);
1251 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1254 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
1255 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
1257 if (nd6_useloopback
) {
1258 rt
->rt_ifp
= &loif
[0]; /*XXX*/
1260 * Make sure rt_ifa be equal to the ifaddr
1261 * corresponding to the address.
1262 * We need this because when we refer
1263 * rt_ifa->ia6_flags in ip6_input, we assume
1264 * that the rt_ifa points to the address instead
1265 * of the loopback address.
1267 if (ifa
!= rt
->rt_ifa
) {
1271 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
1273 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1276 /* join solicited node multicast for proxy ND */
1277 if (ifp
->if_flags
& IFF_MULTICAST
) {
1278 struct in6_addr llsol
;
1281 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1282 llsol
.s6_addr16
[0] = htons(0xff02);
1283 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1284 llsol
.s6_addr32
[1] = 0;
1285 llsol
.s6_addr32
[2] = htonl(1);
1286 llsol
.s6_addr8
[12] = 0xff;
1288 if (!in6_addmulti(&llsol
, ifp
, &error
)) {
1289 nd6log((LOG_ERR
, "%s: failed to join "
1290 "%s (errno=%d)\n", if_name(ifp
),
1291 ip6_sprintf(&llsol
), error
));
1300 /* leave from solicited node multicast for proxy ND */
1301 if ((rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
1302 (ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1303 struct in6_addr llsol
;
1304 struct in6_multi
*in6m
;
1306 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1307 llsol
.s6_addr16
[0] = htons(0xff02);
1308 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1309 llsol
.s6_addr32
[1] = 0;
1310 llsol
.s6_addr32
[2] = htonl(1);
1311 llsol
.s6_addr8
[12] = 0xff;
1313 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1318 ln
->ln_next
->ln_prev
= ln
->ln_prev
;
1319 ln
->ln_prev
->ln_next
= ln
->ln_next
;
1322 rt
->rt_flags
&= ~RTF_LLINFO
;
1324 m_freem(ln
->ln_hold
);
1330 nd6_ioctl(cmd
, data
, ifp
)
1335 struct in6_drlist
*drl
= (struct in6_drlist
*)data
;
1336 struct in6_prlist
*prl
= (struct in6_prlist
*)data
;
1337 struct in6_ndireq
*ndi
= (struct in6_ndireq
*)data
;
1338 struct in6_nbrinfo
*nbi
= (struct in6_nbrinfo
*)data
;
1339 struct in6_ndifreq
*ndif
= (struct in6_ndifreq
*)data
;
1340 struct nd_defrouter
*dr
, any
;
1341 struct nd_prefix
*pr
;
1343 int i
= 0, error
= 0;
1347 case SIOCGDRLST_IN6
:
1349 * obsolete API, use sysctl under net.inet6.icmp6
1351 bzero(drl
, sizeof(*drl
));
1353 dr
= TAILQ_FIRST(&nd_defrouter
);
1354 while (dr
&& i
< DRLSTSIZ
) {
1355 drl
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1356 if (IN6_IS_ADDR_LINKLOCAL(&drl
->defrouter
[i
].rtaddr
)) {
1357 /* XXX: need to this hack for KAME stack */
1358 drl
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1361 "default router list contains a "
1362 "non-linklocal address(%s)\n",
1363 ip6_sprintf(&drl
->defrouter
[i
].rtaddr
));
1365 drl
->defrouter
[i
].flags
= dr
->flags
;
1366 drl
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1367 drl
->defrouter
[i
].expire
= dr
->expire
;
1368 drl
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1370 dr
= TAILQ_NEXT(dr
, dr_entry
);
1374 case SIOCGPRLST_IN6
:
1376 * obsolete API, use sysctl under net.inet6.icmp6
1379 * XXX meaning of fields, especialy "raflags", is very
1380 * differnet between RA prefix list and RR/static prefix list.
1381 * how about separating ioctls into two?
1383 bzero(prl
, sizeof(*prl
));
1385 pr
= nd_prefix
.lh_first
;
1386 while (pr
&& i
< PRLSTSIZ
) {
1387 struct nd_pfxrouter
*pfr
;
1390 (void)in6_embedscope(&prl
->prefix
[i
].prefix
,
1391 &pr
->ndpr_prefix
, NULL
, NULL
);
1392 prl
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1393 prl
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1394 prl
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1395 prl
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1396 prl
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1397 prl
->prefix
[i
].expire
= pr
->ndpr_expire
;
1399 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1403 #define RTRADDR prl->prefix[i].advrtr[j]
1404 RTRADDR
= pfr
->router
->rtaddr
;
1405 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1406 /* XXX: hack for KAME */
1407 RTRADDR
.s6_addr16
[1] = 0;
1410 "a router(%s) advertises "
1412 "non-link local address\n",
1413 ip6_sprintf(&RTRADDR
));
1417 pfr
= pfr
->pfr_next
;
1419 prl
->prefix
[i
].advrtrs
= j
;
1420 prl
->prefix
[i
].origin
= PR_ORIG_RA
;
1426 struct rr_prefix
*rpp
;
1428 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1429 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1432 (void)in6_embedscope(&prl
->prefix
[i
].prefix
,
1433 &pr
->ndpr_prefix
, NULL
, NULL
);
1434 prl
->prefix
[i
].raflags
= rpp
->rp_raf
;
1435 prl
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1436 prl
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1437 prl
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1438 prl
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1439 prl
->prefix
[i
].expire
= rpp
->rp_expire
;
1440 prl
->prefix
[i
].advrtrs
= 0;
1441 prl
->prefix
[i
].origin
= rpp
->rp_origin
;
1448 case OSIOCGIFINFO_IN6
:
1449 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1453 ndi
->ndi
.linkmtu
= nd_ifinfo
[ifp
->if_index
].linkmtu
;
1454 ndi
->ndi
.maxmtu
= nd_ifinfo
[ifp
->if_index
].maxmtu
;
1455 ndi
->ndi
.basereachable
=
1456 nd_ifinfo
[ifp
->if_index
].basereachable
;
1457 ndi
->ndi
.reachable
= nd_ifinfo
[ifp
->if_index
].reachable
;
1458 ndi
->ndi
.retrans
= nd_ifinfo
[ifp
->if_index
].retrans
;
1459 ndi
->ndi
.flags
= nd_ifinfo
[ifp
->if_index
].flags
;
1460 ndi
->ndi
.recalctm
= nd_ifinfo
[ifp
->if_index
].recalctm
;
1461 ndi
->ndi
.chlim
= nd_ifinfo
[ifp
->if_index
].chlim
;
1462 ndi
->ndi
.receivedra
= nd_ifinfo
[ifp
->if_index
].receivedra
;
1464 case SIOCGIFINFO_IN6
:
1465 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1469 ndi
->ndi
= nd_ifinfo
[ifp
->if_index
];
1471 case SIOCSIFINFO_FLAGS
:
1472 /* XXX: almost all other fields of ndi->ndi is unused */
1473 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1477 nd_ifinfo
[ifp
->if_index
].flags
= ndi
->ndi
.flags
;
1479 case SIOCSNDFLUSH_IN6
: /* XXX: the ioctl name is confusing... */
1480 /* flush default router list */
1482 * xxx sumikawa: should not delete route if default
1483 * route equals to the top of default router list
1485 bzero(&any
, sizeof(any
));
1486 defrouter_delreq(&any
, 0);
1488 /* xxx sumikawa: flush prefix list */
1490 case SIOCSPFXFLUSH_IN6
:
1492 /* flush all the prefix advertised by routers */
1493 struct nd_prefix
*pr
, *next
;
1496 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
1497 struct in6_ifaddr
*ia
, *ia_next
;
1499 next
= pr
->ndpr_next
;
1501 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
))
1504 /* do we really have to remove addresses as well? */
1505 for (ia
= in6_ifaddr
; ia
; ia
= ia_next
) {
1506 /* ia might be removed. keep the next ptr. */
1507 ia_next
= ia
->ia_next
;
1509 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
1512 if (ia
->ia6_ndpr
== pr
)
1513 in6_purgeaddr(&ia
->ia_ifa
);
1520 case SIOCSRTRFLUSH_IN6
:
1522 /* flush all the default routers */
1523 struct nd_defrouter
*dr
, *next
;
1526 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
1528 * The first entry of the list may be stored in
1529 * the routing table, so we'll delete it later.
1531 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
1532 next
= TAILQ_NEXT(dr
, dr_entry
);
1535 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
));
1540 case SIOCGNBRINFO_IN6
:
1542 struct llinfo_nd6
*ln
;
1543 struct in6_addr nb_addr
= nbi
->addr
; /* make local for safety */
1546 * XXX: KAME specific hack for scoped addresses
1547 * XXXX: for other scopes than link-local?
1549 if (IN6_IS_ADDR_LINKLOCAL(&nbi
->addr
) ||
1550 IN6_IS_ADDR_MC_LINKLOCAL(&nbi
->addr
)) {
1551 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
1554 *idp
= htons(ifp
->if_index
);
1558 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
)) == NULL
) {
1563 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1564 nbi
->state
= ln
->ln_state
;
1565 nbi
->asked
= ln
->ln_asked
;
1566 nbi
->isrouter
= ln
->ln_router
;
1567 nbi
->expire
= ln
->ln_expire
;
1572 case SIOCGDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1573 ndif
->ifindex
= nd6_defifindex
;
1575 case SIOCSDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1576 return(nd6_setdefaultiface(ndif
->ifindex
));
1583 * Create neighbor cache entry and cache link-layer address,
1584 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1587 nd6_cache_lladdr(ifp
, from
, lladdr
, lladdrlen
, type
, code
)
1589 struct in6_addr
*from
;
1592 int type
; /* ICMP6 type */
1593 int code
; /* type dependent information */
1595 struct rtentry
*rt
= NULL
;
1596 struct llinfo_nd6
*ln
= NULL
;
1598 struct sockaddr_dl
*sdl
= NULL
;
1605 panic("ifp == NULL in nd6_cache_lladdr");
1607 panic("from == NULL in nd6_cache_lladdr");
1609 /* nothing must be updated for unspecified address */
1610 if (IN6_IS_ADDR_UNSPECIFIED(from
))
1614 * Validation about ifp->if_addrlen and lladdrlen must be done in
1617 * XXX If the link does not have link-layer adderss, what should
1618 * we do? (ifp->if_addrlen == 0)
1619 * Spec says nothing in sections for RA, RS and NA. There's small
1620 * description on it in NS section (RFC 2461 7.2.3).
1623 rt
= nd6_lookup(from
, 0, ifp
);
1626 /* nothing must be done if there's no lladdr */
1627 if (!lladdr
|| !lladdrlen
)
1631 rt
= nd6_lookup(from
, 1, ifp
);
1634 /* do nothing if static ndp is set */
1635 if (rt
->rt_flags
& RTF_STATIC
)
1642 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
1647 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1650 if (!rt
->rt_gateway
)
1652 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
1654 sdl
= SDL(rt
->rt_gateway
);
1656 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
1657 if (olladdr
&& lladdr
) {
1658 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
1666 * newentry olladdr lladdr llchange (*=record)
1669 * 0 n y -- (3) * STALE
1671 * 0 y y y (5) * STALE
1672 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1673 * 1 -- y -- (7) * STALE
1676 if (lladdr
) { /*(3-5) and (7)*/
1678 * Record source link-layer address
1679 * XXX is it dependent to ifp->if_type?
1681 sdl
->sdl_alen
= ifp
->if_addrlen
;
1682 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
1686 if ((!olladdr
&& lladdr
) /*(3)*/
1687 || (olladdr
&& lladdr
&& llchange
)) { /*(5)*/
1689 newstate
= ND6_LLINFO_STALE
;
1694 if (!lladdr
) /*(6)*/
1695 newstate
= ND6_LLINFO_NOSTATE
;
1697 newstate
= ND6_LLINFO_STALE
;
1702 * Update the state of the neighbor cache.
1704 ln
->ln_state
= newstate
;
1706 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1708 * XXX: since nd6_output() below will cause
1709 * state tansition to DELAY and reset the timer,
1710 * we must set the timer now, although it is actually
1713 ln
->ln_expire
= time_second
+ nd6_gctimer
;
1717 * we assume ifp is not a p2p here, so just
1718 * set the 2nd argument as the 1st one.
1720 nd6_output(ifp
, ifp
, ln
->ln_hold
,
1721 (struct sockaddr_in6
*)rt_key(rt
),
1725 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
1726 /* probe right away */
1727 ln
->ln_expire
= time_second
;
1732 * ICMP6 type dependent behavior.
1734 * NS: clear IsRouter if new entry
1735 * RS: clear IsRouter
1736 * RA: set IsRouter if there's lladdr
1737 * redir: clear IsRouter if new entry
1740 * The spec says that we must set IsRouter in the following cases:
1741 * - If lladdr exist, set IsRouter. This means (1-5).
1742 * - If it is old entry (!newentry), set IsRouter. This means (7).
1743 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1744 * A quetion arises for (1) case. (1) case has no lladdr in the
1745 * neighbor cache, this is similar to (6).
1746 * This case is rare but we figured that we MUST NOT set IsRouter.
1748 * newentry olladdr lladdr llchange NS RS RA redir
1750 * 0 n n -- (1) c ? s
1751 * 0 y n -- (2) c s s
1752 * 0 n y -- (3) c s s
1755 * 1 -- n -- (6) c c c s
1756 * 1 -- y -- (7) c c s c s
1760 switch (type
& 0xff) {
1761 case ND_NEIGHBOR_SOLICIT
:
1763 * New entry must have is_router flag cleared.
1765 if (is_newentry
) /*(6-7)*/
1770 * If the icmp is a redirect to a better router, always set the
1771 * is_router flag. Otherwise, if the entry is newly created,
1772 * clear the flag. [RFC 2461, sec 8.3]
1774 if (code
== ND_REDIRECT_ROUTER
)
1776 else if (is_newentry
) /*(6-7)*/
1779 case ND_ROUTER_SOLICIT
:
1781 * is_router flag must always be cleared.
1785 case ND_ROUTER_ADVERT
:
1787 * Mark an entry with lladdr as a router.
1789 if ((!is_newentry
&& (olladdr
|| lladdr
)) /*(2-5)*/
1790 || (is_newentry
&& lladdr
)) { /*(7)*/
1797 * When the link-layer address of a router changes, select the
1798 * best router again. In particular, when the neighbor entry is newly
1799 * created, it might affect the selection policy.
1800 * Question: can we restrict the first condition to the "is_newentry"
1802 * XXX: when we hear an RA from a new router with the link-layer
1803 * address option, defrouter_select() is called twice, since
1804 * defrtrlist_update called the function as well. However, I believe
1805 * we can compromise the overhead, since it only happens the first
1807 * XXX: although defrouter_select() should not have a bad effect
1808 * for those are not autoconfigured hosts, we explicitly avoid such
1811 if (do_update
&& ln
->ln_router
&& !ip6_forwarding
&& ip6_accept_rtadv
)
1819 nd6_slowtimo(ignored_arg
)
1824 struct nd_ifinfo
*nd6if
;
1827 timeout(nd6_slowtimo_funneled
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
1828 for (i
= 1; i
< if_index
+ 1; i
++) {
1829 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
)
1831 nd6if
= &nd_ifinfo
[i
];
1832 if (nd6if
->basereachable
&& /* already initialized */
1833 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
1835 * Since reachable time rarely changes by router
1836 * advertisements, we SHOULD insure that a new random
1837 * value gets recomputed at least once every few hours.
1840 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
1841 nd6if
->reachable
= ND_COMPUTE_RTIME(nd6if
->basereachable
);
1848 nd6_slowtimo_funneled(ignored_arg
)
1852 boolean_t funnel_state
;
1853 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
1855 nd6_slowtimo(ignored_arg
);
1857 (void) thread_funnel_set(network_flock
, FALSE
);
1861 #define senderr(e) { error = (e); goto bad;}
1863 nd6_output(ifp
, origifp
, m0
, dst
, rt0
)
1865 struct ifnet
*origifp
;
1867 struct sockaddr_in6
*dst
;
1868 struct rtentry
*rt0
;
1870 struct mbuf
*m
= m0
;
1871 struct rtentry
*rt
= rt0
;
1872 struct sockaddr_in6
*gw6
= NULL
;
1873 struct llinfo_nd6
*ln
= NULL
;
1876 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
))
1879 if (nd6_need_cache(ifp
) == 0)
1883 * next hop determination. This routine is derived from ether_outpout.
1886 if ((rt
->rt_flags
& RTF_UP
) == 0) {
1887 if ((rt0
= rt
= rtalloc1((struct sockaddr
*)dst
, 1, 0UL)) !=
1891 if (rt
->rt_ifp
!= ifp
) {
1892 /* XXX: loop care? */
1893 return nd6_output(ifp
, origifp
, m0
,
1897 senderr(EHOSTUNREACH
);
1900 if (rt
->rt_flags
& RTF_GATEWAY
) {
1901 gw6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
1904 * We skip link-layer address resolution and NUD
1905 * if the gateway is not a neighbor from ND point
1906 * of view, regardless the value of the
1908 * The second condition is a bit tricky: we skip
1909 * if the gateway is our own address, which is
1910 * sometimes used to install a route to a p2p link.
1912 if (!nd6_is_addr_neighbor(gw6
, ifp
) ||
1913 in6ifa_ifpwithaddr(ifp
, &gw6
->sin6_addr
)) {
1915 * We allow this kind of tricky route only
1916 * when the outgoing interface is p2p.
1917 * XXX: we may need a more generic rule here.
1919 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
1920 senderr(EHOSTUNREACH
);
1925 if (rt
->rt_gwroute
== 0)
1927 if (((rt
= rt
->rt_gwroute
)->rt_flags
& RTF_UP
) == 0) {
1928 rtfree(rt
); rt
= rt0
;
1929 lookup
: rt
->rt_gwroute
= rtalloc1(rt
->rt_gateway
, 1, 0UL);
1930 if ((rt
= rt
->rt_gwroute
) == 0)
1931 senderr(EHOSTUNREACH
);
1937 * Address resolution or Neighbor Unreachability Detection
1939 * At this point, the destination of the packet must be a unicast
1940 * or an anycast address(i.e. not a multicast).
1943 /* Look up the neighbor cache for the nexthop */
1944 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0)
1945 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1948 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1949 * the condition below is not very efficient. But we believe
1950 * it is tolerable, because this should be a rare case.
1952 if (nd6_is_addr_neighbor(dst
, ifp
) &&
1953 (rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
)) != NULL
)
1954 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1957 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0 &&
1958 !(nd_ifinfo
[ifp
->if_index
].flags
& ND6_IFF_PERFORMNUD
)) {
1960 "nd6_output: can't allocate llinfo for %s "
1962 ip6_sprintf(&dst
->sin6_addr
), ln
, rt
);
1963 senderr(EIO
); /* XXX: good error? */
1966 goto sendpkt
; /* send anyway */
1969 /* We don't have to do link-layer address resolution on a p2p link. */
1970 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
1971 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
1972 ln
->ln_state
= ND6_LLINFO_STALE
;
1973 ln
->ln_expire
= time_second
+ nd6_gctimer
;
1977 * The first time we send a packet to a neighbor whose entry is
1978 * STALE, we have to change the state to DELAY and a sets a timer to
1979 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1980 * neighbor unreachability detection on expiration.
1983 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1985 ln
->ln_state
= ND6_LLINFO_DELAY
;
1986 ln
->ln_expire
= time_second
+ nd6_delay
;
1990 * If the neighbor cache entry has a state other than INCOMPLETE
1991 * (i.e. its link-layer address is already reloved), just
1994 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
)
1998 * There is a neighbor cache entry, but no ethernet address
1999 * response yet. Replace the held mbuf (if any) with this
2002 * XXX Does the code conform to rate-limiting rule?
2005 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
)
2006 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
2008 m_freem(ln
->ln_hold
);
2010 if (ln
->ln_expire
) {
2011 if (ln
->ln_asked
< nd6_mmaxtries
&&
2012 ln
->ln_expire
< time_second
) {
2014 ln
->ln_expire
= time_second
+
2015 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
2016 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0);
2024 /* Make sure the HW checksum flags are cleaned before sending the packet */
2026 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
2027 m
->m_pkthdr
.csum_data
= 0;
2028 m
->m_pkthdr
.csum_flags
= 0;
2030 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
2031 return (dlil_output(ifptodlt(origifp
, PF_INET6
), m
, (caddr_t
)rt
, (struct sockaddr
*)dst
,0));
2034 return (dlil_output(ifptodlt(ifp
, PF_INET6
), m
, (caddr_t
)rt
, (struct sockaddr
*)dst
, 0));
2036 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
2037 return((*ifp
->if_output
)(origifp
, m
, (struct sockaddr
*)dst
,
2040 return((*ifp
->if_output
)(ifp
, m
, (struct sockaddr
*)dst
, rt
));
2055 * XXX: we currently do not make neighbor cache on any interface
2056 * other than ARCnet, Ethernet, FDDI and GIF.
2059 * - unidirectional tunnels needs no ND
2061 switch (ifp
->if_type
) {
2072 case IFT_GIF
: /* XXX need more cases? */
2080 nd6_storelladdr(ifp
, rt
, m
, dst
, desten
)
2084 struct sockaddr
*dst
;
2088 struct sockaddr_dl
*sdl
;
2090 if (m
->m_flags
& M_MCAST
) {
2091 switch (ifp
->if_type
) {
2100 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
2104 for (i
= 0; i
< ifp
->if_addrlen
; i
++)
2117 /* this could happen, if we could not allocate memory */
2121 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
2122 printf("nd6_storelladdr: something odd happens\n");
2126 sdl
= SDL(rt
->rt_gateway
);
2127 if (sdl
->sdl_alen
== 0) {
2128 /* this should be impossible, but we bark here for debugging */
2129 printf("nd6_storelladdr: sdl_alen == 0\n");
2134 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
2138 static int nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
;
2139 static int nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
;
2140 SYSCTL_DECL(_net_inet6_icmp6
);
2141 SYSCTL_NODE(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
2142 CTLFLAG_RD
, nd6_sysctl_drlist
, "");
2143 SYSCTL_NODE(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
2144 CTLFLAG_RD
, nd6_sysctl_prlist
, "");
2147 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
2151 struct in6_defrouter
*d
, *de
;
2152 struct nd_defrouter
*dr
;
2158 for (dr
= TAILQ_FIRST(&nd_defrouter
);
2160 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2161 d
= (struct in6_defrouter
*)buf
;
2162 de
= (struct in6_defrouter
*)(buf
+ sizeof(buf
));
2165 bzero(d
, sizeof(*d
));
2166 d
->rtaddr
.sin6_family
= AF_INET6
;
2167 d
->rtaddr
.sin6_len
= sizeof(d
->rtaddr
);
2168 if (in6_recoverscope(&d
->rtaddr
, &dr
->rtaddr
,
2172 "default router list (%s)\n",
2173 ip6_sprintf(&dr
->rtaddr
));
2174 d
->flags
= dr
->flags
;
2175 d
->rtlifetime
= dr
->rtlifetime
;
2176 d
->expire
= dr
->expire
;
2177 d
->if_index
= dr
->ifp
->if_index
;
2179 panic("buffer too short");
2181 error
= SYSCTL_OUT(req
, buf
, sizeof(*d
));
2189 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
2193 struct in6_prefix
*p
, *pe
;
2194 struct nd_prefix
*pr
;
2200 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2203 struct sockaddr_in6
*sin6
, *s6
;
2204 struct nd_pfxrouter
*pfr
;
2206 p
= (struct in6_prefix
*)buf
;
2207 pe
= (struct in6_prefix
*)(buf
+ sizeof(buf
));
2210 bzero(p
, sizeof(*p
));
2211 sin6
= (struct sockaddr_in6
*)(p
+ 1);
2213 p
->prefix
= pr
->ndpr_prefix
;
2214 if (in6_recoverscope(&p
->prefix
,
2215 &p
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
2217 "scope error in prefix list (%s)\n",
2218 ip6_sprintf(&p
->prefix
.sin6_addr
));
2219 p
->raflags
= pr
->ndpr_raf
;
2220 p
->prefixlen
= pr
->ndpr_plen
;
2221 p
->vltime
= pr
->ndpr_vltime
;
2222 p
->pltime
= pr
->ndpr_pltime
;
2223 p
->if_index
= pr
->ndpr_ifp
->if_index
;
2224 p
->expire
= pr
->ndpr_expire
;
2225 p
->refcnt
= pr
->ndpr_refcnt
;
2226 p
->flags
= pr
->ndpr_stateflags
;
2227 p
->origin
= PR_ORIG_RA
;
2229 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
2231 pfr
= pfr
->pfr_next
) {
2232 if ((void *)&sin6
[advrtrs
+ 1] >
2237 s6
= &sin6
[advrtrs
];
2238 bzero(s6
, sizeof(*s6
));
2239 s6
->sin6_family
= AF_INET6
;
2240 s6
->sin6_len
= sizeof(*sin6
);
2241 if (in6_recoverscope(s6
,
2242 &pfr
->router
->rtaddr
,
2243 pfr
->router
->ifp
) != 0)
2246 "prefix list (%s)\n",
2247 ip6_sprintf(&pfr
->router
->rtaddr
));
2250 p
->advrtrs
= advrtrs
;
2252 panic("buffer too short");
2254 advance
= sizeof(*p
) + sizeof(*sin6
) * advrtrs
;
2255 error
= SYSCTL_OUT(req
, buf
, advance
);