1 /* $FreeBSD: src/sys/netinet6/nd6.c,v 1.2.2.15 2003/05/06 06:46:58 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/nd6.c,v 1.24 2007/08/27 13:15:14 hasso Exp $ */
3 /* $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * BSD/OS version heavily modifies this code, related to llinfo.
38 * Since we don't have BSD/OS version of net/route.c in our hand,
39 * I left the code mostly as it was in 970310. -- itojun
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
53 #include <sys/kernel.h>
54 #include <sys/protosw.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
57 #include <sys/queue.h>
58 #include <sys/sysctl.h>
59 #include <sys/thread2.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 #include <net/if_atm.h>
65 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
69 #include <netinet/if_fddi.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet6/nd6.h>
74 #include <netinet6/in6_prefix.h>
75 #include <netinet/icmp6.h>
79 #include <net/net_osdep.h>
81 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
82 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
84 #define SIN6(s) ((struct sockaddr_in6 *)s)
85 #define SDL(s) ((struct sockaddr_dl *)s)
88 int nd6_prune
= 1; /* walk list every 1 seconds */
89 int nd6_delay
= 5; /* delay first probe time 5 second */
90 int nd6_umaxtries
= 3; /* maximum unicast query */
91 int nd6_mmaxtries
= 3; /* maximum multicast query */
92 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
93 int nd6_gctimer
= (60 * 60 * 24); /* 1 day: garbage collection timer */
95 /* preventing too many loops in ND option parsing */
96 int nd6_maxndopt
= 10; /* max # of ND options allowed */
98 int nd6_maxnudhint
= 0; /* max # of subsequent upper layer hints */
107 static int nd6_inuse
, nd6_allocated
;
109 struct llinfo_nd6 llinfo_nd6
= {&llinfo_nd6
, &llinfo_nd6
};
110 struct nd_drhead nd_defrouter
;
111 struct nd_prhead nd_prefix
= { 0 };
113 int nd6_recalc_reachtm_interval
= ND6_RECALC_REACHTM_INTERVAL
;
114 static struct sockaddr_in6 all1_sa
;
116 static void nd6_setmtu0 (struct ifnet
*, struct nd_ifinfo
*);
117 static void nd6_slowtimo (void *);
118 static int regen_tmpaddr (struct in6_ifaddr
*);
120 struct callout nd6_slowtimo_ch
;
121 struct callout nd6_timer_ch
;
122 extern struct callout in6_tmpaddrtimer_ch
;
127 static int nd6_init_done
= 0;
131 log(LOG_NOTICE
, "nd6_init called more than once(ignored)\n");
135 all1_sa
.sin6_family
= AF_INET6
;
136 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
137 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++)
138 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
140 /* initialization of the default router list */
141 TAILQ_INIT(&nd_defrouter
);
146 callout_init(&nd6_slowtimo_ch
);
147 callout_reset(&nd6_slowtimo_ch
, ND6_SLOWTIMER_INTERVAL
* hz
,
152 nd6_ifattach(struct ifnet
*ifp
)
154 struct nd_ifinfo
*nd
;
156 nd
= (struct nd_ifinfo
*)kmalloc(sizeof(*nd
), M_IP6NDP
, M_WAITOK
);
157 bzero(nd
, sizeof(*nd
));
161 nd
->linkmtu
= ifindex2ifnet
[ifp
->if_index
]->if_mtu
;
162 nd
->chlim
= IPV6_DEFHLIM
;
163 nd
->basereachable
= REACHABLE_TIME
;
164 nd
->reachable
= ND_COMPUTE_RTIME(nd
->basereachable
);
165 nd
->retrans
= RETRANS_TIMER
;
169 * Note that the default value of ip6_accept_rtadv is 0, which means
170 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
173 nd
->flags
= (ND6_IFF_PERFORMNUD
| ND6_IFF_ACCEPT_RTADV
);
175 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
176 nd6_setmtu0(ifp
, nd
);
181 nd6_ifdetach(struct nd_ifinfo
*nd
)
187 * Reset ND level link MTU. This function is called when the physical MTU
188 * changes, which means we might have to adjust the ND level MTU.
191 nd6_setmtu(struct ifnet
*ifp
)
193 nd6_setmtu0(ifp
, ND_IFINFO(ifp
));
196 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
198 nd6_setmtu0(struct ifnet
*ifp
, struct nd_ifinfo
*ndi
)
203 oldmaxmtu
= ndi
->maxmtu
;
204 oldlinkmtu
= ndi
->linkmtu
;
206 switch (ifp
->if_type
) {
208 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
211 ndi
->maxmtu
= MIN(FDDIIPMTU
, ifp
->if_mtu
);
214 ndi
->maxmtu
= MIN(ATMMTU
, ifp
->if_mtu
);
216 case IFT_IEEE1394
: /* XXX should be IEEE1394MTU(1500) */
217 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
220 case IFT_IEEE80211
: /* XXX should be IEEE80211MTU(1500) */
221 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
225 ndi
->maxmtu
= ifp
->if_mtu
;
229 if (oldmaxmtu
!= ndi
->maxmtu
) {
231 * If the ND level MTU is not set yet, or if the maxmtu
232 * is reset to a smaller value than the ND level MTU,
233 * also reset the ND level MTU.
235 if (ndi
->linkmtu
== 0 ||
236 ndi
->maxmtu
< ndi
->linkmtu
) {
237 ndi
->linkmtu
= ndi
->maxmtu
;
238 /* also adjust in6_maxmtu if necessary. */
239 if (oldlinkmtu
== 0) {
241 * XXX: the case analysis is grotty, but
242 * it is not efficient to call in6_setmaxmtu()
243 * here when we are during the initialization
246 if (in6_maxmtu
< ndi
->linkmtu
)
247 in6_maxmtu
= ndi
->linkmtu
;
256 nd6_option_init(void *opt
, int icmp6len
, union nd_opts
*ndopts
)
258 bzero(ndopts
, sizeof(*ndopts
));
259 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
261 = (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
264 ndopts
->nd_opts_done
= 1;
265 ndopts
->nd_opts_search
= NULL
;
270 * Take one ND option.
273 nd6_option(union nd_opts
*ndopts
)
275 struct nd_opt_hdr
*nd_opt
;
279 panic("ndopts == NULL in nd6_option");
280 if (!ndopts
->nd_opts_last
)
281 panic("uninitialized ndopts in nd6_option");
282 if (!ndopts
->nd_opts_search
)
284 if (ndopts
->nd_opts_done
)
287 nd_opt
= ndopts
->nd_opts_search
;
289 /* make sure nd_opt_len is inside the buffer */
290 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
291 bzero(ndopts
, sizeof(*ndopts
));
295 olen
= nd_opt
->nd_opt_len
<< 3;
298 * Message validation requires that all included
299 * options have a length that is greater than zero.
301 bzero(ndopts
, sizeof(*ndopts
));
305 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
306 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
307 /* option overruns the end of buffer, invalid */
308 bzero(ndopts
, sizeof(*ndopts
));
310 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
311 /* reached the end of options chain */
312 ndopts
->nd_opts_done
= 1;
313 ndopts
->nd_opts_search
= NULL
;
319 * Parse multiple ND options.
320 * This function is much easier to use, for ND routines that do not need
321 * multiple options of the same type.
324 nd6_options(union nd_opts
*ndopts
)
326 struct nd_opt_hdr
*nd_opt
;
330 panic("ndopts == NULL in nd6_options");
331 if (!ndopts
->nd_opts_last
)
332 panic("uninitialized ndopts in nd6_options");
333 if (!ndopts
->nd_opts_search
)
337 nd_opt
= nd6_option(ndopts
);
338 if (!nd_opt
&& !ndopts
->nd_opts_last
) {
340 * Message validation requires that all included
341 * options have a length that is greater than zero.
343 icmp6stat
.icp6s_nd_badopt
++;
344 bzero(ndopts
, sizeof(*ndopts
));
351 switch (nd_opt
->nd_opt_type
) {
352 case ND_OPT_SOURCE_LINKADDR
:
353 case ND_OPT_TARGET_LINKADDR
:
355 case ND_OPT_REDIRECTED_HEADER
:
356 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
358 "duplicated ND6 option found (type=%d)\n",
359 nd_opt
->nd_opt_type
));
362 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
366 case ND_OPT_PREFIX_INFORMATION
:
367 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
368 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
371 ndopts
->nd_opts_pi_end
=
372 (struct nd_opt_prefix_info
*)nd_opt
;
376 * Unknown options must be silently ignored,
377 * to accomodate future extension to the protocol.
380 "nd6_options: unsupported option %d - "
381 "option ignored\n", nd_opt
->nd_opt_type
));
386 if (i
> nd6_maxndopt
) {
387 icmp6stat
.icp6s_nd_toomanyopt
++;
388 nd6log((LOG_INFO
, "too many loop in nd opt\n"));
392 if (ndopts
->nd_opts_done
)
400 * ND6 timer routine to expire default route list and prefix list
403 nd6_timer(void *ignored_arg
)
405 struct llinfo_nd6
*ln
;
406 struct nd_defrouter
*dr
;
407 struct nd_prefix
*pr
;
409 struct in6_ifaddr
*ia6
, *nia6
;
410 struct in6_addrlifetime
*lt6
;
413 callout_reset(&nd6_timer_ch
, nd6_prune
* hz
,
416 ln
= llinfo_nd6
.ln_next
;
417 while (ln
&& ln
!= &llinfo_nd6
) {
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
;
424 if ((rt
= ln
->ln_rt
) == NULL
) {
428 if ((ifp
= rt
->rt_ifp
) == NULL
) {
432 ndi
= ND_IFINFO(ifp
);
433 dst
= (struct sockaddr_in6
*)rt_key(rt
);
435 if (ln
->ln_expire
> time_second
) {
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)",
447 panic("dst=0 in nd6_timer(ln=%p)", ln
);
449 switch (ln
->ln_state
) {
450 case ND6_LLINFO_INCOMPLETE
:
451 if (ln
->ln_asked
< nd6_mmaxtries
) {
453 ln
->ln_expire
= time_second
+
454 ND_IFINFO(ifp
)->retrans
/ 1000;
455 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
,
458 struct mbuf
*m
= ln
->ln_hold
;
462 * Fake rcvif to make ICMP error
463 * more helpful in diagnosing
465 * XXX: should we consider
468 m
->m_pkthdr
.rcvif
= rt
->rt_ifp
;
470 icmp6_error(m
, ICMP6_DST_UNREACH
,
471 ICMP6_DST_UNREACH_ADDR
, 0);
477 case ND6_LLINFO_REACHABLE
:
479 ln
->ln_state
= ND6_LLINFO_STALE
;
480 ln
->ln_expire
= time_second
+ nd6_gctimer
;
484 case ND6_LLINFO_STALE
:
485 /* Garbage Collection(RFC 2461 5.3) */
490 case ND6_LLINFO_DELAY
:
491 if (ndi
&& (ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
494 ln
->ln_state
= ND6_LLINFO_PROBE
;
495 ln
->ln_expire
= time_second
+
497 nd6_ns_output(ifp
, &dst
->sin6_addr
,
501 ln
->ln_state
= ND6_LLINFO_STALE
; /* XXX */
502 ln
->ln_expire
= time_second
+ nd6_gctimer
;
505 case ND6_LLINFO_PROBE
:
506 if (ln
->ln_asked
< nd6_umaxtries
) {
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);
520 /* expire default router list */
521 dr
= TAILQ_FIRST(&nd_defrouter
);
523 if (dr
->expire
&& dr
->expire
< time_second
) {
524 struct nd_defrouter
*t
;
525 t
= TAILQ_NEXT(dr
, dr_entry
);
529 dr
= TAILQ_NEXT(dr
, dr_entry
);
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.
540 for (ia6
= in6_ifaddr
; ia6
; ia6
= nia6
) {
542 /* check address lifetime */
543 lt6
= &ia6
->ia6_lifetime
;
544 if (IFA6_IS_INVALID(ia6
)) {
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
557 if (ip6_use_tempaddr
&&
558 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
)) {
559 if (regen_tmpaddr(ia6
) == 0)
563 in6_purgeaddr(&ia6
->ia_ifa
);
566 goto addrloop
; /* XXX: see below */
568 if (IFA6_IS_DEPRECATED(ia6
)) {
569 int oldflags
= ia6
->ia6_flags
;
571 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
574 * If a temporary address has just become deprecated,
575 * regenerate a new one if possible.
577 if (ip6_use_tempaddr
&&
578 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) &&
579 !(oldflags
& IN6_IFF_DEPRECATED
)) {
581 if (regen_tmpaddr(ia6
) == 0) {
583 * A new temporary address is
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??
599 * A new RA might have made a deprecated address
602 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
606 /* expire prefix list */
607 pr
= nd_prefix
.lh_first
;
610 * check prefix lifetime.
611 * since pltime is just for autoconf, pltime processing for
612 * prefix is not necessary.
614 if (pr
->ndpr_expire
&& pr
->ndpr_expire
< time_second
) {
619 * address expiration and prefix expiration are
620 * separate. NEVER perform in6_purgeaddr here.
632 regen_tmpaddr(struct in6_ifaddr
*ia6
) /* deprecated/invalidated temporary
637 struct in6_ifaddr
*public_ifa6
= NULL
;
639 ifp
= ia6
->ia_ifa
.ifa_ifp
;
640 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_list
) {
641 struct in6_ifaddr
*it6
;
643 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
646 it6
= (struct in6_ifaddr
*)ifa
;
648 /* ignore no autoconf addresses. */
649 if (!(it6
->ia6_flags
& IN6_IFF_AUTOCONF
))
652 /* ignore autoconf addresses with different prefixes. */
653 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
)
657 * Now we are looking at an autoconf address with the same
658 * prefix as ours. If the address is temporary and is still
659 * preferred, do not create another one. It would be rare, but
660 * could happen, for example, when we resume a laptop PC after
663 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) &&
664 !IFA6_IS_DEPRECATED(it6
)) {
670 * This is a public autoconf address that has the same prefix
671 * as ours. If it is preferred, keep it. We can't break the
672 * loop here, because there may be a still-preferred temporary
673 * address with the prefix.
675 if (!IFA6_IS_DEPRECATED(it6
))
679 if (public_ifa6
!= NULL
) {
682 if ((e
= in6_tmpifadd(public_ifa6
, 0)) != 0) {
683 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
684 " tmp addr,errno=%d\n", e
);
694 * Nuke neighbor cache/prefix/default router management table, right before
698 nd6_purge(struct ifnet
*ifp
)
700 struct llinfo_nd6
*ln
, *nln
;
701 struct nd_defrouter
*dr
, *ndr
, drany
;
702 struct nd_prefix
*pr
, *npr
;
704 /* Nuke default router list entries toward ifp */
705 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
707 * The first entry of the list may be stored in
708 * the routing table, so we'll delete it later.
710 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= ndr
) {
711 ndr
= TAILQ_NEXT(dr
, dr_entry
);
715 dr
= TAILQ_FIRST(&nd_defrouter
);
720 /* Nuke prefix list entries toward ifp */
721 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
723 if (pr
->ndpr_ifp
== ifp
) {
725 * Previously, pr->ndpr_addr is removed as well,
726 * but I strongly believe we don't have to do it.
727 * nd6_purge() is only called from in6_ifdetach(),
728 * which removes all the associated interface addresses
730 * (jinmei@kame.net 20010129)
736 /* cancel default outgoing interface setting */
737 if (nd6_defifindex
== ifp
->if_index
)
738 nd6_setdefaultiface(0);
740 if (!ip6_forwarding
&& ip6_accept_rtadv
) { /* XXX: too restrictive? */
741 /* refresh default router list */
742 bzero(&drany
, sizeof(drany
));
743 defrouter_delreq(&drany
, 0);
748 * Nuke neighbor cache entries for the ifp.
749 * Note that rt->rt_ifp may not be the same as ifp,
750 * due to KAME goto ours hack. See RTM_RESOLVE case in
751 * nd6_rtrequest(), and ip6_input().
753 ln
= llinfo_nd6
.ln_next
;
754 while (ln
&& ln
!= &llinfo_nd6
) {
756 struct sockaddr_dl
*sdl
;
760 if (rt
&& rt
->rt_gateway
&&
761 rt
->rt_gateway
->sa_family
== AF_LINK
) {
762 sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
;
763 if (sdl
->sdl_index
== ifp
->if_index
)
771 nd6_lookup(struct in6_addr
*addr6
, int create
, struct ifnet
*ifp
)
774 struct sockaddr_in6 sin6
;
776 bzero(&sin6
, sizeof(sin6
));
777 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
778 sin6
.sin6_family
= AF_INET6
;
779 sin6
.sin6_addr
= *addr6
;
782 rt
= rtlookup((struct sockaddr
*)&sin6
);
784 rt
= rtpurelookup((struct sockaddr
*)&sin6
);
785 if (rt
&& !(rt
->rt_flags
& RTF_LLINFO
)) {
787 * This is the case for the default route.
788 * If we want to create a neighbor cache for the address, we
789 * should free the route for the destination and allocate an
802 * If no route is available and create is set,
803 * we allocate a host route for the destination
804 * and treat it like an interface route.
805 * This hack is necessary for a neighbor which can't
806 * be covered by our own prefix.
809 ifaof_ifpforaddr((struct sockaddr
*)&sin6
, ifp
);
814 * Create a new route. RTF_LLINFO is necessary
815 * to create a Neighbor Cache entry for the
816 * destination in nd6_rtrequest which will be
817 * called in rtrequest via ifa->ifa_rtrequest.
819 if ((e
= rtrequest(RTM_ADD
, (struct sockaddr
*)&sin6
,
821 (struct sockaddr
*)&all1_sa
,
823 RTF_HOST
| RTF_LLINFO
) &
827 "nd6_lookup: failed to add route for a "
828 "neighbor(%s), errno=%d\n",
829 ip6_sprintf(addr6
), e
);
833 struct llinfo_nd6
*ln
=
834 (struct llinfo_nd6
*)rt
->rt_llinfo
;
835 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
842 * Validation for the entry.
843 * Note that the check for rt_llinfo is necessary because a cloned
844 * route from a parent route that has the L flag (e.g. the default
845 * route to a p2p interface) may have the flag, too, while the
846 * destination is not actually a neighbor.
847 * XXX: we can't use rt->rt_ifp to check for the interface, since
848 * it might be the loopback interface if the entry is for our
849 * own address on a non-loopback interface. Instead, we should
850 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
853 if ((rt
->rt_flags
& RTF_GATEWAY
) || !(rt
->rt_flags
& RTF_LLINFO
) ||
854 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
855 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
)) {
857 log(LOG_DEBUG
, "nd6_lookup: failed to lookup %s (if = %s)\n",
858 ip6_sprintf(addr6
), ifp
? if_name(ifp
) : "unspec");
859 /* xxx more logs... kazu */
867 * Detect if a given IPv6 address identifies a neighbor on a given link.
868 * XXX: should take care of the destination of a p2p link?
871 nd6_is_addr_neighbor(struct sockaddr_in6
*addr
, struct ifnet
*ifp
)
876 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
877 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
880 * A link-local address is always a neighbor.
881 * XXX: we should use the sin6_scope_id field rather than the embedded
884 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
) &&
885 ntohs(*(u_int16_t
*)&addr
->sin6_addr
.s6_addr
[2]) == ifp
->if_index
)
889 * If the address matches one of our addresses,
890 * it should be a neighbor.
892 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
893 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
896 for (i
= 0; i
< 4; i
++) {
897 if ((IFADDR6(ifa
).s6_addr32
[i
] ^
898 addr
->sin6_addr
.s6_addr32
[i
]) &
899 IFMASK6(ifa
).s6_addr32
[i
])
906 * Even if the address matches none of our addresses, it might be
907 * in the neighbor cache.
909 if (nd6_lookup(&addr
->sin6_addr
, 0, ifp
) != NULL
)
918 * Free an nd6 llinfo entry.
921 nd6_free(struct rtentry
*rt
)
923 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
, *next
;
924 struct in6_addr in6
= ((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
925 struct nd_defrouter
*dr
;
928 * we used to have pfctlinput(PRC_HOSTDEAD) here.
929 * even though it is not harmful, it was not really necessary.
932 if (!ip6_forwarding
&& ip6_accept_rtadv
) { /* XXX: too restrictive? */
934 dr
= defrouter_lookup(&((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
,
937 if (ln
->ln_router
|| dr
) {
939 * rt6_flush must be called whether or not the neighbor
940 * is in the Default Router List.
941 * See a corresponding comment in nd6_na_input().
943 rt6_flush(&in6
, rt
->rt_ifp
);
948 * Unreachablity of a router might affect the default
949 * router selection and on-link detection of advertised
954 * Temporarily fake the state to choose a new default
955 * router and to perform on-link determination of
956 * prefixes correctly.
957 * Below the state will be set correctly,
958 * or the entry itself will be deleted.
960 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
963 * Since defrouter_select() does not affect the
964 * on-link determination and MIP6 needs the check
965 * before the default router selection, we perform
968 pfxlist_onlink_check();
970 if (dr
== TAILQ_FIRST(&nd_defrouter
)) {
972 * It is used as the current default router,
973 * so we have to move it to the end of the
974 * list and choose a new one.
975 * XXX: it is not very efficient if this is
978 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
979 TAILQ_INSERT_TAIL(&nd_defrouter
, dr
, dr_entry
);
988 * Before deleting the entry, remember the next entry as the
989 * return value. We need this because pfxlist_onlink_check() above
990 * might have freed other entries (particularly the old next entry) as
991 * a side effect (XXX).
996 * Detach the route from the routing tree and the list of neighbor
997 * caches, and disable the route entry not to be used in already
1000 rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
1001 rt_mask(rt
), 0, (struct rtentry
**)0);
1007 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1009 * XXX cost-effective metods?
1012 nd6_nud_hint(struct rtentry
*rt
, struct in6_addr
*dst6
, int force
)
1014 struct llinfo_nd6
*ln
;
1017 * If the caller specified "rt", use that. Otherwise, resolve the
1018 * routing table by supplied "dst6".
1023 if (!(rt
= nd6_lookup(dst6
, 0, NULL
)))
1027 if ((rt
->rt_flags
& RTF_GATEWAY
) ||
1028 !(rt
->rt_flags
& RTF_LLINFO
) ||
1029 rt
->rt_llinfo
== NULL
|| rt
->rt_gateway
== NULL
||
1030 rt
->rt_gateway
->sa_family
!= AF_LINK
) {
1031 /* This is not a host route. */
1035 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1036 if (ln
->ln_state
< ND6_LLINFO_REACHABLE
)
1040 * if we get upper-layer reachability confirmation many times,
1041 * it is possible we have false information.
1045 if (ln
->ln_byhint
> nd6_maxnudhint
)
1049 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1051 ln
->ln_expire
= time_second
+
1052 ND_IFINFO(rt
->rt_ifp
)->reachable
;
1056 nd6_rtrequest(int req
, struct rtentry
*rt
,
1057 struct rt_addrinfo
*info
) /* xxx unused */
1059 struct sockaddr
*gate
= rt
->rt_gateway
;
1060 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1061 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
};
1062 struct ifnet
*ifp
= rt
->rt_ifp
;
1065 if ((rt
->rt_flags
& RTF_GATEWAY
))
1068 if (nd6_need_cache(ifp
) == 0 && !(rt
->rt_flags
& RTF_HOST
)) {
1070 * This is probably an interface direct route for a link
1071 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1072 * We do not need special treatment below for such a route.
1073 * Moreover, the RTF_LLINFO flag which would be set below
1074 * would annoy the ndp(8) command.
1079 if (req
== RTM_RESOLVE
&&
1080 (nd6_need_cache(ifp
) == 0 || /* stf case */
1081 !nd6_is_addr_neighbor((struct sockaddr_in6
*)rt_key(rt
), ifp
))) {
1083 * FreeBSD and BSD/OS often make a cloned host route based
1084 * on a less-specific route (e.g. the default route).
1085 * If the less specific route does not have a "gateway"
1086 * (this is the case when the route just goes to a p2p or an
1087 * stf interface), we'll mistakenly make a neighbor cache for
1088 * the host route, and will see strange neighbor solicitation
1089 * for the corresponding destination. In order to avoid the
1090 * confusion, we check if the destination of the route is
1091 * a neighbor in terms of neighbor discovery, and stop the
1092 * process if not. Additionally, we remove the LLINFO flag
1093 * so that ndp(8) will not try to get the neighbor information
1094 * of the destination.
1096 rt
->rt_flags
&= ~RTF_LLINFO
;
1103 * There is no backward compatibility :)
1105 * if (!(rt->rt_flags & RTF_HOST) &&
1106 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1107 * rt->rt_flags |= RTF_CLONING;
1109 if (rt
->rt_flags
& (RTF_CLONING
| RTF_LLINFO
)) {
1111 * Case 1: This route should come from
1112 * a route to interface. RTF_LLINFO flag is set
1113 * for a host route whose destination should be
1114 * treated as on-link.
1116 rt_setgate(rt
, rt_key(rt
),
1117 (struct sockaddr
*)&null_sdl
);
1118 gate
= rt
->rt_gateway
;
1119 SDL(gate
)->sdl_type
= ifp
->if_type
;
1120 SDL(gate
)->sdl_index
= ifp
->if_index
;
1122 ln
->ln_expire
= time_second
;
1124 if (ln
&& ln
->ln_expire
== 0) {
1125 /* kludge for desktops */
1127 kprintf("nd6_rtequest: time.tv_sec is zero; "
1133 if ((rt
->rt_flags
& RTF_CLONING
))
1137 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1138 * We don't do that here since llinfo is not ready yet.
1140 * There are also couple of other things to be discussed:
1141 * - unsolicited NA code needs improvement beforehand
1142 * - RFC2461 says we MAY send multicast unsolicited NA
1143 * (7.2.6 paragraph 4), however, it also says that we
1144 * SHOULD provide a mechanism to prevent multicast NA storm.
1145 * we don't have anything like it right now.
1146 * note that the mechanism needs a mutual agreement
1147 * between proxies, which means that we need to implement
1148 * a new protocol, or a new kludge.
1149 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1150 * we need to check ip6forwarding before sending it.
1151 * (or should we allow proxy ND configuration only for
1152 * routers? there's no mention about proxy ND from hosts)
1155 /* XXX it does not work */
1156 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1158 &SIN6(rt_key(rt
))->sin6_addr
,
1159 &SIN6(rt_key(rt
))->sin6_addr
,
1160 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1165 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) == 0) {
1167 * Address resolution isn't necessary for a point to
1168 * point link, so we can skip this test for a p2p link.
1170 if (gate
->sa_family
!= AF_LINK
||
1171 gate
->sa_len
< sizeof(null_sdl
)) {
1173 "nd6_rtrequest: bad gateway value: %s\n",
1177 SDL(gate
)->sdl_type
= ifp
->if_type
;
1178 SDL(gate
)->sdl_index
= ifp
->if_index
;
1181 break; /* This happens on a route change */
1183 * Case 2: This route may come from cloning, or a manual route
1184 * add with a LL address.
1186 R_Malloc(ln
, struct llinfo_nd6
*, sizeof(*ln
));
1187 rt
->rt_llinfo
= (caddr_t
)ln
;
1189 log(LOG_DEBUG
, "nd6_rtrequest: malloc failed\n");
1194 bzero(ln
, sizeof(*ln
));
1196 /* this is required for "ndp" command. - shin */
1197 if (req
== RTM_ADD
) {
1199 * gate should have some valid AF_LINK entry,
1200 * and ln->ln_expire should have some lifetime
1201 * which is specified by ndp command.
1203 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1207 * When req == RTM_RESOLVE, rt is created and
1208 * initialized in rtrequest(), so rt_expire is 0.
1210 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1211 ln
->ln_expire
= time_second
;
1213 rt
->rt_flags
|= RTF_LLINFO
;
1214 ln
->ln_next
= llinfo_nd6
.ln_next
;
1215 llinfo_nd6
.ln_next
= ln
;
1216 ln
->ln_prev
= &llinfo_nd6
;
1217 ln
->ln_next
->ln_prev
= ln
;
1220 * check if rt_key(rt) is one of my address assigned
1223 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1224 &SIN6(rt_key(rt
))->sin6_addr
);
1226 caddr_t macp
= nd6_ifptomac(ifp
);
1228 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1231 bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
1232 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
1234 if (nd6_useloopback
) {
1235 rt
->rt_ifp
= &loif
[0]; /* XXX */
1237 * Make sure rt_ifa be equal to the ifaddr
1238 * corresponding to the address.
1239 * We need this because when we refer
1240 * rt_ifa->ia6_flags in ip6_input, we assume
1241 * that the rt_ifa points to the address instead
1242 * of the loopback address.
1244 if (ifa
!= rt
->rt_ifa
) {
1245 IFAFREE(rt
->rt_ifa
);
1250 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
1252 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1255 /* join solicited node multicast for proxy ND */
1256 if (ifp
->if_flags
& IFF_MULTICAST
) {
1257 struct in6_addr llsol
;
1260 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1261 llsol
.s6_addr16
[0] = htons(0xff02);
1262 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1263 llsol
.s6_addr32
[1] = 0;
1264 llsol
.s6_addr32
[2] = htonl(1);
1265 llsol
.s6_addr8
[12] = 0xff;
1267 if (!in6_addmulti(&llsol
, ifp
, &error
)) {
1268 nd6log((LOG_ERR
, "%s: failed to join "
1269 "%s (errno=%d)\n", if_name(ifp
),
1270 ip6_sprintf(&llsol
), error
));
1279 /* leave from solicited node multicast for proxy ND */
1280 if ((rt
->rt_flags
& RTF_ANNOUNCE
) &&
1281 (ifp
->if_flags
& IFF_MULTICAST
)) {
1282 struct in6_addr llsol
;
1283 struct in6_multi
*in6m
;
1285 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1286 llsol
.s6_addr16
[0] = htons(0xff02);
1287 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1288 llsol
.s6_addr32
[1] = 0;
1289 llsol
.s6_addr32
[2] = htonl(1);
1290 llsol
.s6_addr8
[12] = 0xff;
1292 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1297 ln
->ln_next
->ln_prev
= ln
->ln_prev
;
1298 ln
->ln_prev
->ln_next
= ln
->ln_next
;
1301 rt
->rt_flags
&= ~RTF_LLINFO
;
1303 m_freem(ln
->ln_hold
);
1309 nd6_ioctl(u_long cmd
, caddr_t data
, struct ifnet
*ifp
)
1311 struct in6_drlist
*drl
= (struct in6_drlist
*)data
;
1312 struct in6_prlist
*prl
= (struct in6_prlist
*)data
;
1313 struct in6_ndireq
*ndi
= (struct in6_ndireq
*)data
;
1314 struct in6_nbrinfo
*nbi
= (struct in6_nbrinfo
*)data
;
1315 struct in6_ndifreq
*ndif
= (struct in6_ndifreq
*)data
;
1316 struct nd_defrouter
*dr
, any
;
1317 struct nd_prefix
*pr
;
1319 int i
= 0, error
= 0;
1322 case SIOCGDRLST_IN6
:
1324 * obsolete API, use sysctl under net.inet6.icmp6
1326 bzero(drl
, sizeof(*drl
));
1328 dr
= TAILQ_FIRST(&nd_defrouter
);
1329 while (dr
&& i
< DRLSTSIZ
) {
1330 drl
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1331 if (IN6_IS_ADDR_LINKLOCAL(&drl
->defrouter
[i
].rtaddr
)) {
1332 /* XXX: need to this hack for KAME stack */
1333 drl
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1336 "default router list contains a "
1337 "non-linklocal address(%s)\n",
1338 ip6_sprintf(&drl
->defrouter
[i
].rtaddr
));
1340 drl
->defrouter
[i
].flags
= dr
->flags
;
1341 drl
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1342 drl
->defrouter
[i
].expire
= dr
->expire
;
1343 drl
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1345 dr
= TAILQ_NEXT(dr
, dr_entry
);
1349 case SIOCGPRLST_IN6
:
1351 * obsolete API, use sysctl under net.inet6.icmp6
1354 * XXX meaning of fields, especialy "raflags", is very
1355 * differnet between RA prefix list and RR/static prefix list.
1356 * how about separating ioctls into two?
1358 bzero(prl
, sizeof(*prl
));
1360 pr
= nd_prefix
.lh_first
;
1361 while (pr
&& i
< PRLSTSIZ
) {
1362 struct nd_pfxrouter
*pfr
;
1365 in6_embedscope(&prl
->prefix
[i
].prefix
,
1366 &pr
->ndpr_prefix
, NULL
, NULL
);
1367 prl
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1368 prl
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1369 prl
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1370 prl
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1371 prl
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1372 prl
->prefix
[i
].expire
= pr
->ndpr_expire
;
1374 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1378 #define RTRADDR prl->prefix[i].advrtr[j]
1379 RTRADDR
= pfr
->router
->rtaddr
;
1380 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1381 /* XXX: hack for KAME */
1382 RTRADDR
.s6_addr16
[1] = 0;
1385 "a router(%s) advertises "
1387 "non-link local address\n",
1388 ip6_sprintf(&RTRADDR
));
1392 pfr
= pfr
->pfr_next
;
1394 prl
->prefix
[i
].advrtrs
= j
;
1395 prl
->prefix
[i
].origin
= PR_ORIG_RA
;
1401 struct rr_prefix
*rpp
;
1403 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1404 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1407 in6_embedscope(&prl
->prefix
[i
].prefix
,
1408 &pr
->ndpr_prefix
, NULL
, NULL
);
1409 prl
->prefix
[i
].raflags
= rpp
->rp_raf
;
1410 prl
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1411 prl
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1412 prl
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1413 prl
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1414 prl
->prefix
[i
].expire
= rpp
->rp_expire
;
1415 prl
->prefix
[i
].advrtrs
= 0;
1416 prl
->prefix
[i
].origin
= rpp
->rp_origin
;
1423 case OSIOCGIFINFO_IN6
:
1424 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1425 bzero(&ndi
->ndi
, sizeof(ndi
->ndi
));
1426 ndi
->ndi
.linkmtu
= ND_IFINFO(ifp
)->linkmtu
;
1427 ndi
->ndi
.maxmtu
= ND_IFINFO(ifp
)->maxmtu
;
1428 ndi
->ndi
.basereachable
= ND_IFINFO(ifp
)->basereachable
;
1429 ndi
->ndi
.reachable
= ND_IFINFO(ifp
)->reachable
;
1430 ndi
->ndi
.retrans
= ND_IFINFO(ifp
)->retrans
;
1431 ndi
->ndi
.flags
= ND_IFINFO(ifp
)->flags
;
1432 ndi
->ndi
.recalctm
= ND_IFINFO(ifp
)->recalctm
;
1433 ndi
->ndi
.chlim
= ND_IFINFO(ifp
)->chlim
;
1434 ndi
->ndi
.receivedra
= ND_IFINFO(ifp
)->receivedra
;
1436 case SIOCGIFINFO_IN6
:
1437 ndi
->ndi
= *ND_IFINFO(ifp
);
1439 case SIOCSIFINFO_FLAGS
:
1440 ND_IFINFO(ifp
)->flags
= ndi
->ndi
.flags
;
1442 case SIOCSNDFLUSH_IN6
: /* XXX: the ioctl name is confusing... */
1443 /* flush default router list */
1445 * xxx sumikawa: should not delete route if default
1446 * route equals to the top of default router list
1448 bzero(&any
, sizeof(any
));
1449 defrouter_delreq(&any
, 0);
1451 /* xxx sumikawa: flush prefix list */
1453 case SIOCSPFXFLUSH_IN6
:
1455 /* flush all the prefix advertised by routers */
1456 struct nd_prefix
*pr
, *next
;
1459 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
1460 struct in6_ifaddr
*ia
, *ia_next
;
1462 next
= pr
->ndpr_next
;
1464 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
))
1467 /* do we really have to remove addresses as well? */
1468 for (ia
= in6_ifaddr
; ia
; ia
= ia_next
) {
1469 /* ia might be removed. keep the next ptr. */
1470 ia_next
= ia
->ia_next
;
1472 if (!(ia
->ia6_flags
& IN6_IFF_AUTOCONF
))
1475 if (ia
->ia6_ndpr
== pr
)
1476 in6_purgeaddr(&ia
->ia_ifa
);
1483 case SIOCSRTRFLUSH_IN6
:
1485 /* flush all the default routers */
1486 struct nd_defrouter
*dr
, *next
;
1489 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
1491 * The first entry of the list may be stored in
1492 * the routing table, so we'll delete it later.
1494 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
1495 next
= TAILQ_NEXT(dr
, dr_entry
);
1498 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
));
1503 case SIOCGNBRINFO_IN6
:
1505 struct llinfo_nd6
*ln
;
1506 struct in6_addr nb_addr
= nbi
->addr
; /* make local for safety */
1509 * XXX: KAME specific hack for scoped addresses
1510 * XXXX: for other scopes than link-local?
1512 if (IN6_IS_ADDR_LINKLOCAL(&nbi
->addr
) ||
1513 IN6_IS_ADDR_MC_LINKLOCAL(&nbi
->addr
)) {
1514 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
1517 *idp
= htons(ifp
->if_index
);
1521 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
)) == NULL
) {
1526 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1527 nbi
->state
= ln
->ln_state
;
1528 nbi
->asked
= ln
->ln_asked
;
1529 nbi
->isrouter
= ln
->ln_router
;
1530 nbi
->expire
= ln
->ln_expire
;
1535 case SIOCGDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1536 ndif
->ifindex
= nd6_defifindex
;
1538 case SIOCSDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1539 return (nd6_setdefaultiface(ndif
->ifindex
));
1546 * Create neighbor cache entry and cache link-layer address,
1547 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1550 nd6_cache_lladdr(struct ifnet
*ifp
, struct in6_addr
*from
, char *lladdr
,
1552 int type
, /* ICMP6 type */
1553 int code
/* type dependent information */)
1555 struct rtentry
*rt
= NULL
;
1556 struct llinfo_nd6
*ln
= NULL
;
1558 struct sockaddr_dl
*sdl
= NULL
;
1565 panic("ifp == NULL in nd6_cache_lladdr");
1567 panic("from == NULL in nd6_cache_lladdr");
1569 /* nothing must be updated for unspecified address */
1570 if (IN6_IS_ADDR_UNSPECIFIED(from
))
1574 * Validation about ifp->if_addrlen and lladdrlen must be done in
1577 * XXX If the link does not have link-layer adderss, what should
1578 * we do? (ifp->if_addrlen == 0)
1579 * Spec says nothing in sections for RA, RS and NA. There's small
1580 * description on it in NS section (RFC 2461 7.2.3).
1583 rt
= nd6_lookup(from
, 0, ifp
);
1586 /* nothing must be done if there's no lladdr */
1587 if (!lladdr
|| !lladdrlen
)
1591 rt
= nd6_lookup(from
, 1, ifp
);
1594 /* do nothing if static ndp is set */
1595 if (rt
->rt_flags
& RTF_STATIC
)
1602 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
1607 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1610 if (!rt
->rt_gateway
)
1612 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
1614 sdl
= SDL(rt
->rt_gateway
);
1616 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
1617 if (olladdr
&& lladdr
) {
1618 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
1626 * newentry olladdr lladdr llchange (*=record)
1629 * 0 n y -- (3) * STALE
1631 * 0 y y y (5) * STALE
1632 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1633 * 1 -- y -- (7) * STALE
1636 if (lladdr
) { /* (3-5) and (7) */
1638 * Record source link-layer address
1639 * XXX is it dependent to ifp->if_type?
1641 sdl
->sdl_alen
= ifp
->if_addrlen
;
1642 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
1646 if ((!olladdr
&& lladdr
) /* (3) */
1647 || (olladdr
&& lladdr
&& llchange
)) { /* (5) */
1649 newstate
= ND6_LLINFO_STALE
;
1650 } else /* (1-2,4) */
1654 if (!lladdr
) /* (6) */
1655 newstate
= ND6_LLINFO_NOSTATE
;
1657 newstate
= ND6_LLINFO_STALE
;
1662 * Update the state of the neighbor cache.
1664 ln
->ln_state
= newstate
;
1666 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1668 * XXX: since nd6_output() below will cause
1669 * state tansition to DELAY and reset the timer,
1670 * we must set the timer now, although it is actually
1673 ln
->ln_expire
= time_second
+ nd6_gctimer
;
1677 * we assume ifp is not a p2p here, so just
1678 * set the 2nd argument as the 1st one.
1680 nd6_output(ifp
, ifp
, ln
->ln_hold
,
1681 (struct sockaddr_in6
*)rt_key(rt
),
1685 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
1686 /* probe right away */
1687 ln
->ln_expire
= time_second
;
1692 * ICMP6 type dependent behavior.
1694 * NS: clear IsRouter if new entry
1695 * RS: clear IsRouter
1696 * RA: set IsRouter if there's lladdr
1697 * redir: clear IsRouter if new entry
1700 * The spec says that we must set IsRouter in the following cases:
1701 * - If lladdr exist, set IsRouter. This means (1-5).
1702 * - If it is old entry (!newentry), set IsRouter. This means (7).
1703 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1704 * A quetion arises for (1) case. (1) case has no lladdr in the
1705 * neighbor cache, this is similar to (6).
1706 * This case is rare but we figured that we MUST NOT set IsRouter.
1708 * newentry olladdr lladdr llchange NS RS RA redir
1710 * 0 n n -- (1) c ? s
1711 * 0 y n -- (2) c s s
1712 * 0 n y -- (3) c s s
1715 * 1 -- n -- (6) c c c s
1716 * 1 -- y -- (7) c c s c s
1720 switch (type
& 0xff) {
1721 case ND_NEIGHBOR_SOLICIT
:
1723 * New entry must have is_router flag cleared.
1725 if (is_newentry
) /* (6-7) */
1730 * If the icmp is a redirect to a better router, always set the
1731 * is_router flag. Otherwise, if the entry is newly created,
1732 * clear the flag. [RFC 2461, sec 8.3]
1734 if (code
== ND_REDIRECT_ROUTER
)
1736 else if (is_newentry
) /* (6-7) */
1739 case ND_ROUTER_SOLICIT
:
1741 * is_router flag must always be cleared.
1745 case ND_ROUTER_ADVERT
:
1747 * Mark an entry with lladdr as a router.
1749 if ((!is_newentry
&& (olladdr
|| lladdr
)) /* (2-5) */
1750 || (is_newentry
&& lladdr
)) { /* (7) */
1757 * When the link-layer address of a router changes, select the
1758 * best router again. In particular, when the neighbor entry is newly
1759 * created, it might affect the selection policy.
1760 * Question: can we restrict the first condition to the "is_newentry"
1762 * XXX: when we hear an RA from a new router with the link-layer
1763 * address option, defrouter_select() is called twice, since
1764 * defrtrlist_update called the function as well. However, I believe
1765 * we can compromise the overhead, since it only happens the first
1767 * XXX: although defrouter_select() should not have a bad effect
1768 * for those are not autoconfigured hosts, we explicitly avoid such
1771 if (do_update
&& ln
->ln_router
&& !ip6_forwarding
&& ip6_accept_rtadv
)
1778 nd6_slowtimo(void *ignored_arg
)
1780 struct nd_ifinfo
*nd6if
;
1784 callout_reset(&nd6_slowtimo_ch
, ND6_SLOWTIMER_INTERVAL
* hz
,
1785 nd6_slowtimo
, NULL
);
1786 for (ifp
= TAILQ_FIRST(&ifnet
); ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
)) {
1787 nd6if
= ND_IFINFO(ifp
);
1788 if (nd6if
->basereachable
&& /* already initialized */
1789 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
1791 * Since reachable time rarely changes by router
1792 * advertisements, we SHOULD insure that a new random
1793 * value gets recomputed at least once every few hours.
1796 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
1797 nd6if
->reachable
= ND_COMPUTE_RTIME(nd6if
->basereachable
);
1803 #define gotoerr(e) { error = (e); goto bad;}
1806 nd6_output(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m
,
1807 struct sockaddr_in6
*dst
, struct rtentry
*rt
)
1809 struct llinfo_nd6
*ln
= NULL
;
1812 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
))
1815 if (nd6_need_cache(ifp
) == 0)
1819 * next hop determination. This routine is derived from ether_outpout.
1822 if (!(rt
->rt_flags
& RTF_UP
)) {
1823 rt
= rtlookup((struct sockaddr
*)dst
);
1825 gotoerr(EHOSTUNREACH
);
1827 if (rt
->rt_ifp
!= ifp
) {
1828 /* XXX: loop care? */
1829 return nd6_output(ifp
, origifp
, m
, dst
, rt
);
1832 if (rt
->rt_flags
& RTF_GATEWAY
) {
1833 struct sockaddr_in6
*gw6
;
1836 * We skip link-layer address resolution and NUD
1837 * if the gateway is not a neighbor from ND point
1838 * of view, regardless of the value of nd_ifinfo.flags.
1839 * The second condition is a bit tricky; we skip
1840 * if the gateway is our own address, which is
1841 * sometimes used to install a route to a p2p link.
1843 gw6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
1844 if (!nd6_is_addr_neighbor(gw6
, ifp
) ||
1845 in6ifa_ifpwithaddr(ifp
, &gw6
->sin6_addr
)) {
1847 * We allow this kind of tricky route only
1848 * when the outgoing interface is p2p.
1849 * XXX: we may need a more generic rule here.
1851 if (!(ifp
->if_flags
& IFF_POINTOPOINT
))
1852 gotoerr(EHOSTUNREACH
);
1857 if (rt
->rt_gwroute
== NULL
) {
1858 rt
->rt_gwroute
= rtlookup(rt
->rt_gateway
);
1859 if (rt
->rt_gwroute
== NULL
)
1860 gotoerr(EHOSTUNREACH
);
1861 } else if (!(rt
->rt_gwroute
->rt_flags
& RTF_UP
)) {
1862 rtfree(rt
->rt_gwroute
);
1863 rt
->rt_gwroute
= rtlookup(rt
->rt_gateway
);
1864 if (rt
->rt_gwroute
== NULL
)
1865 gotoerr(EHOSTUNREACH
);
1871 * Address resolution or Neighbor Unreachability Detection
1873 * At this point, the destination of the packet must be a unicast
1874 * or an anycast address(i.e. not a multicast).
1877 /* Look up the neighbor cache for the nexthop */
1878 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
))
1879 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1882 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1883 * the condition below is not very efficient. But we believe
1884 * it is tolerable, because this should be a rare case.
1886 if (nd6_is_addr_neighbor(dst
, ifp
) &&
1887 (rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
)) != NULL
)
1888 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1891 if (!(ifp
->if_flags
& IFF_POINTOPOINT
) &&
1892 !(ND_IFINFO(ifp
)->flags
& ND6_IFF_PERFORMNUD
)) {
1894 "nd6_output: can't allocate llinfo for %s "
1896 ip6_sprintf(&dst
->sin6_addr
), ln
, rt
);
1897 gotoerr(EIO
); /* XXX: good error? */
1900 goto sendpkt
; /* send anyway */
1903 /* We don't have to do link-layer address resolution on a p2p link. */
1904 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
1905 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
1906 ln
->ln_state
= ND6_LLINFO_STALE
;
1907 ln
->ln_expire
= time_second
+ nd6_gctimer
;
1911 * The first time we send a packet to a neighbor whose entry is
1912 * STALE, we have to change the state to DELAY and a sets a timer to
1913 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1914 * neighbor unreachability detection on expiration.
1917 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1919 ln
->ln_state
= ND6_LLINFO_DELAY
;
1920 ln
->ln_expire
= time_second
+ nd6_delay
;
1924 * If the neighbor cache entry has a state other than INCOMPLETE
1925 * (i.e. its link-layer address is already resolved), just
1928 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
)
1932 * There is a neighbor cache entry, but no ethernet address
1933 * response yet. Replace the held mbuf (if any) with this
1936 * This code conforms to the rate-limiting rule described in Section
1937 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
1940 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
)
1941 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1943 m_freem(ln
->ln_hold
);
1945 if (ln
->ln_expire
) {
1946 if (ln
->ln_asked
< nd6_mmaxtries
&&
1947 ln
->ln_expire
< time_second
) {
1949 ln
->ln_expire
= time_second
+
1950 ND_IFINFO(ifp
)->retrans
/ 1000;
1951 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0);
1957 lwkt_serialize_enter(ifp
->if_serializer
);
1958 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1959 error
= (*ifp
->if_output
)(origifp
, m
, (struct sockaddr
*)dst
,
1962 error
= (*ifp
->if_output
)(ifp
, m
, (struct sockaddr
*)dst
, rt
);
1964 lwkt_serialize_exit(ifp
->if_serializer
);
1974 nd6_need_cache(struct ifnet
*ifp
)
1977 * XXX: we currently do not make neighbor cache on any interface
1978 * other than Ethernet, FDDI and GIF.
1981 * - unidirectional tunnels needs no ND
1983 switch (ifp
->if_type
) {
1990 #ifdef IFT_IEEE80211
1996 case IFT_GIF
: /* XXX need more cases? */
2004 nd6_storelladdr(struct ifnet
*ifp
, struct rtentry
*rt0
, struct mbuf
*m
,
2005 struct sockaddr
*dst
, u_char
*desten
)
2007 struct sockaddr_dl
*sdl
;
2011 if (m
->m_flags
& M_MCAST
) {
2012 switch (ifp
->if_type
) {
2018 #ifdef IFT_IEEE80211
2021 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
2025 bcopy(ifp
->if_broadcastaddr
, desten
, ifp
->if_addrlen
);
2033 /* this could happen, if we could not allocate memory */
2037 if (rt_llroute(dst
, rt0
, &rt
) != 0) {
2041 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
2042 kprintf("nd6_storelladdr: something odd happens\n");
2046 sdl
= SDL(rt
->rt_gateway
);
2047 if (sdl
->sdl_alen
== 0) {
2048 /* this should be impossible, but we bark here for debugging */
2049 kprintf("nd6_storelladdr: sdl_alen == 0\n");
2054 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
2058 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS
);
2059 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS
);
2061 SYSCTL_DECL(_net_inet6_icmp6
);
2063 SYSCTL_NODE(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
2064 CTLFLAG_RD
, nd6_sysctl_drlist
, "");
2065 SYSCTL_NODE(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
2066 CTLFLAG_RD
, nd6_sysctl_prlist
, "");
2069 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS
)
2073 struct in6_defrouter
*d
, *de
;
2074 struct nd_defrouter
*dr
;
2080 for (dr
= TAILQ_FIRST(&nd_defrouter
);
2082 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2083 d
= (struct in6_defrouter
*)buf
;
2084 de
= (struct in6_defrouter
*)(buf
+ sizeof(buf
));
2087 bzero(d
, sizeof(*d
));
2088 d
->rtaddr
.sin6_family
= AF_INET6
;
2089 d
->rtaddr
.sin6_len
= sizeof(d
->rtaddr
);
2090 if (in6_recoverscope(&d
->rtaddr
, &dr
->rtaddr
,
2094 "default router list (%s)\n",
2095 ip6_sprintf(&dr
->rtaddr
));
2096 d
->flags
= dr
->flags
;
2097 d
->rtlifetime
= dr
->rtlifetime
;
2098 d
->expire
= dr
->expire
;
2099 d
->if_index
= dr
->ifp
->if_index
;
2101 panic("buffer too short");
2103 error
= SYSCTL_OUT(req
, buf
, sizeof(*d
));
2111 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS
)
2115 struct in6_prefix
*p
, *pe
;
2116 struct nd_prefix
*pr
;
2122 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2125 struct sockaddr_in6
*sin6
, *s6
;
2126 struct nd_pfxrouter
*pfr
;
2128 p
= (struct in6_prefix
*)buf
;
2129 pe
= (struct in6_prefix
*)(buf
+ sizeof(buf
));
2132 bzero(p
, sizeof(*p
));
2133 sin6
= (struct sockaddr_in6
*)(p
+ 1);
2135 p
->prefix
= pr
->ndpr_prefix
;
2136 if (in6_recoverscope(&p
->prefix
,
2137 &p
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
2139 "scope error in prefix list (%s)\n",
2140 ip6_sprintf(&p
->prefix
.sin6_addr
));
2141 p
->raflags
= pr
->ndpr_raf
;
2142 p
->prefixlen
= pr
->ndpr_plen
;
2143 p
->vltime
= pr
->ndpr_vltime
;
2144 p
->pltime
= pr
->ndpr_pltime
;
2145 p
->if_index
= pr
->ndpr_ifp
->if_index
;
2146 p
->expire
= pr
->ndpr_expire
;
2147 p
->refcnt
= pr
->ndpr_refcnt
;
2148 p
->flags
= pr
->ndpr_stateflags
;
2149 p
->origin
= PR_ORIG_RA
;
2151 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
2153 pfr
= pfr
->pfr_next
) {
2154 if ((void *)&sin6
[advrtrs
+ 1] >
2159 s6
= &sin6
[advrtrs
];
2160 bzero(s6
, sizeof(*s6
));
2161 s6
->sin6_family
= AF_INET6
;
2162 s6
->sin6_len
= sizeof(*sin6
);
2163 if (in6_recoverscope(s6
, &pfr
->router
->rtaddr
,
2164 pfr
->router
->ifp
) != 0)
2167 "prefix list (%s)\n",
2168 ip6_sprintf(&pfr
->router
->rtaddr
));
2171 p
->advrtrs
= advrtrs
;
2173 panic("buffer too short");
2175 advance
= sizeof(*p
) + sizeof(*sin6
) * advrtrs
;
2176 error
= SYSCTL_OUT(req
, buf
, advance
);