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.26 2008/01/05 14:02:40 swildner 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 <netinet6/in6_var.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/nd6.h>
73 #include <netinet6/in6_prefix.h>
74 #include <netinet/icmp6.h>
78 #include <net/net_osdep.h>
80 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
81 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
83 #define SIN6(s) ((struct sockaddr_in6 *)s)
84 #define SDL(s) ((struct sockaddr_dl *)s)
87 int nd6_prune
= 1; /* walk list every 1 seconds */
88 int nd6_delay
= 5; /* delay first probe time 5 second */
89 int nd6_umaxtries
= 3; /* maximum unicast query */
90 int nd6_mmaxtries
= 3; /* maximum multicast query */
91 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
92 int nd6_gctimer
= (60 * 60 * 24); /* 1 day: garbage collection timer */
94 /* preventing too many loops in ND option parsing */
95 int nd6_maxndopt
= 10; /* max # of ND options allowed */
97 int nd6_maxnudhint
= 0; /* max # of subsequent upper layer hints */
106 static int nd6_inuse
, nd6_allocated
;
108 struct llinfo_nd6 llinfo_nd6
= {&llinfo_nd6
, &llinfo_nd6
};
109 struct nd_drhead nd_defrouter
;
110 struct nd_prhead nd_prefix
= { 0 };
112 int nd6_recalc_reachtm_interval
= ND6_RECALC_REACHTM_INTERVAL
;
113 static struct sockaddr_in6 all1_sa
;
115 static void nd6_setmtu0 (struct ifnet
*, struct nd_ifinfo
*);
116 static void nd6_slowtimo (void *);
117 static int regen_tmpaddr (struct in6_ifaddr
*);
119 struct callout nd6_slowtimo_ch
;
120 struct callout nd6_timer_ch
;
121 extern struct callout in6_tmpaddrtimer_ch
;
126 static int nd6_init_done
= 0;
130 log(LOG_NOTICE
, "nd6_init called more than once(ignored)\n");
134 all1_sa
.sin6_family
= AF_INET6
;
135 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
136 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++)
137 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
139 /* initialization of the default router list */
140 TAILQ_INIT(&nd_defrouter
);
145 callout_init(&nd6_slowtimo_ch
);
146 callout_reset(&nd6_slowtimo_ch
, ND6_SLOWTIMER_INTERVAL
* hz
,
151 nd6_ifattach(struct ifnet
*ifp
)
153 struct nd_ifinfo
*nd
;
155 nd
= (struct nd_ifinfo
*)kmalloc(sizeof(*nd
), M_IP6NDP
,
160 nd
->linkmtu
= ifindex2ifnet
[ifp
->if_index
]->if_mtu
;
161 nd
->chlim
= IPV6_DEFHLIM
;
162 nd
->basereachable
= REACHABLE_TIME
;
163 nd
->reachable
= ND_COMPUTE_RTIME(nd
->basereachable
);
164 nd
->retrans
= RETRANS_TIMER
;
168 * Note that the default value of ip6_accept_rtadv is 0, which means
169 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
172 nd
->flags
= (ND6_IFF_PERFORMNUD
| ND6_IFF_ACCEPT_RTADV
);
174 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
175 nd6_setmtu0(ifp
, nd
);
180 nd6_ifdetach(struct nd_ifinfo
*nd
)
186 * Reset ND level link MTU. This function is called when the physical MTU
187 * changes, which means we might have to adjust the ND level MTU.
190 nd6_setmtu(struct ifnet
*ifp
)
192 nd6_setmtu0(ifp
, ND_IFINFO(ifp
));
195 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
197 nd6_setmtu0(struct ifnet
*ifp
, struct nd_ifinfo
*ndi
)
202 oldmaxmtu
= ndi
->maxmtu
;
203 oldlinkmtu
= ndi
->linkmtu
;
205 switch (ifp
->if_type
) {
207 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
210 ndi
->maxmtu
= MIN(ATMMTU
, ifp
->if_mtu
);
212 case IFT_IEEE1394
: /* XXX should be IEEE1394MTU(1500) */
213 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
216 case IFT_IEEE80211
: /* XXX should be IEEE80211MTU(1500) */
217 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
221 ndi
->maxmtu
= ifp
->if_mtu
;
225 if (oldmaxmtu
!= ndi
->maxmtu
) {
227 * If the ND level MTU is not set yet, or if the maxmtu
228 * is reset to a smaller value than the ND level MTU,
229 * also reset the ND level MTU.
231 if (ndi
->linkmtu
== 0 ||
232 ndi
->maxmtu
< ndi
->linkmtu
) {
233 ndi
->linkmtu
= ndi
->maxmtu
;
234 /* also adjust in6_maxmtu if necessary. */
235 if (oldlinkmtu
== 0) {
237 * XXX: the case analysis is grotty, but
238 * it is not efficient to call in6_setmaxmtu()
239 * here when we are during the initialization
242 if (in6_maxmtu
< ndi
->linkmtu
)
243 in6_maxmtu
= ndi
->linkmtu
;
252 nd6_option_init(void *opt
, int icmp6len
, union nd_opts
*ndopts
)
254 bzero(ndopts
, sizeof(*ndopts
));
255 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
257 = (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
260 ndopts
->nd_opts_done
= 1;
261 ndopts
->nd_opts_search
= NULL
;
266 * Take one ND option.
269 nd6_option(union nd_opts
*ndopts
)
271 struct nd_opt_hdr
*nd_opt
;
275 panic("ndopts == NULL in nd6_option");
276 if (!ndopts
->nd_opts_last
)
277 panic("uninitialized ndopts in nd6_option");
278 if (!ndopts
->nd_opts_search
)
280 if (ndopts
->nd_opts_done
)
283 nd_opt
= ndopts
->nd_opts_search
;
285 /* make sure nd_opt_len is inside the buffer */
286 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
287 bzero(ndopts
, sizeof(*ndopts
));
291 olen
= nd_opt
->nd_opt_len
<< 3;
294 * Message validation requires that all included
295 * options have a length that is greater than zero.
297 bzero(ndopts
, sizeof(*ndopts
));
301 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
302 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
303 /* option overruns the end of buffer, invalid */
304 bzero(ndopts
, sizeof(*ndopts
));
306 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
307 /* reached the end of options chain */
308 ndopts
->nd_opts_done
= 1;
309 ndopts
->nd_opts_search
= NULL
;
315 * Parse multiple ND options.
316 * This function is much easier to use, for ND routines that do not need
317 * multiple options of the same type.
320 nd6_options(union nd_opts
*ndopts
)
322 struct nd_opt_hdr
*nd_opt
;
326 panic("ndopts == NULL in nd6_options");
327 if (!ndopts
->nd_opts_last
)
328 panic("uninitialized ndopts in nd6_options");
329 if (!ndopts
->nd_opts_search
)
333 nd_opt
= nd6_option(ndopts
);
334 if (!nd_opt
&& !ndopts
->nd_opts_last
) {
336 * Message validation requires that all included
337 * options have a length that is greater than zero.
339 icmp6stat
.icp6s_nd_badopt
++;
340 bzero(ndopts
, sizeof(*ndopts
));
347 switch (nd_opt
->nd_opt_type
) {
348 case ND_OPT_SOURCE_LINKADDR
:
349 case ND_OPT_TARGET_LINKADDR
:
351 case ND_OPT_REDIRECTED_HEADER
:
352 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
354 "duplicated ND6 option found (type=%d)\n",
355 nd_opt
->nd_opt_type
));
358 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
362 case ND_OPT_PREFIX_INFORMATION
:
363 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
364 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
367 ndopts
->nd_opts_pi_end
=
368 (struct nd_opt_prefix_info
*)nd_opt
;
372 * Unknown options must be silently ignored,
373 * to accomodate future extension to the protocol.
376 "nd6_options: unsupported option %d - "
377 "option ignored\n", nd_opt
->nd_opt_type
));
382 if (i
> nd6_maxndopt
) {
383 icmp6stat
.icp6s_nd_toomanyopt
++;
384 nd6log((LOG_INFO
, "too many loop in nd opt\n"));
388 if (ndopts
->nd_opts_done
)
396 * ND6 timer routine to expire default route list and prefix list
399 nd6_timer(void *ignored_arg
)
401 struct llinfo_nd6
*ln
;
402 struct nd_defrouter
*dr
;
403 struct nd_prefix
*pr
;
405 struct in6_ifaddr
*ia6
, *nia6
;
406 struct in6_addrlifetime
*lt6
;
409 callout_reset(&nd6_timer_ch
, nd6_prune
* hz
,
412 ln
= llinfo_nd6
.ln_next
;
413 while (ln
&& ln
!= &llinfo_nd6
) {
415 struct sockaddr_in6
*dst
;
416 struct llinfo_nd6
*next
= ln
->ln_next
;
417 /* XXX: used for the DELAY case only: */
418 struct nd_ifinfo
*ndi
= NULL
;
420 if ((rt
= ln
->ln_rt
) == NULL
) {
424 if ((ifp
= rt
->rt_ifp
) == NULL
) {
428 ndi
= ND_IFINFO(ifp
);
429 dst
= (struct sockaddr_in6
*)rt_key(rt
);
431 if (ln
->ln_expire
> time_second
) {
438 panic("rt=0 in nd6_timer(ln=%p)", ln
);
439 if (rt
->rt_llinfo
&& (struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
)
440 panic("rt_llinfo(%p) is not equal to ln(%p)",
443 panic("dst=0 in nd6_timer(ln=%p)", ln
);
445 switch (ln
->ln_state
) {
446 case ND6_LLINFO_INCOMPLETE
:
447 if (ln
->ln_asked
< nd6_mmaxtries
) {
449 ln
->ln_expire
= time_second
+
450 ND_IFINFO(ifp
)->retrans
/ 1000;
451 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
,
454 struct mbuf
*m
= ln
->ln_hold
;
458 * Fake rcvif to make ICMP error
459 * more helpful in diagnosing
461 * XXX: should we consider
464 m
->m_pkthdr
.rcvif
= rt
->rt_ifp
;
466 icmp6_error(m
, ICMP6_DST_UNREACH
,
467 ICMP6_DST_UNREACH_ADDR
, 0);
473 case ND6_LLINFO_REACHABLE
:
475 ln
->ln_state
= ND6_LLINFO_STALE
;
476 ln
->ln_expire
= time_second
+ nd6_gctimer
;
480 case ND6_LLINFO_STALE
:
481 /* Garbage Collection(RFC 2461 5.3) */
486 case ND6_LLINFO_DELAY
:
487 if (ndi
&& (ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
490 ln
->ln_state
= ND6_LLINFO_PROBE
;
491 ln
->ln_expire
= time_second
+
493 nd6_ns_output(ifp
, &dst
->sin6_addr
,
497 ln
->ln_state
= ND6_LLINFO_STALE
; /* XXX */
498 ln
->ln_expire
= time_second
+ nd6_gctimer
;
501 case ND6_LLINFO_PROBE
:
502 if (ln
->ln_asked
< nd6_umaxtries
) {
504 ln
->ln_expire
= time_second
+
505 ND_IFINFO(ifp
)->retrans
/ 1000;
506 nd6_ns_output(ifp
, &dst
->sin6_addr
,
507 &dst
->sin6_addr
, ln
, 0);
516 /* expire default router list */
517 dr
= TAILQ_FIRST(&nd_defrouter
);
519 if (dr
->expire
&& dr
->expire
< time_second
) {
520 struct nd_defrouter
*t
;
521 t
= TAILQ_NEXT(dr
, dr_entry
);
525 dr
= TAILQ_NEXT(dr
, dr_entry
);
530 * expire interface addresses.
531 * in the past the loop was inside prefix expiry processing.
532 * However, from a stricter speci-confrmance standpoint, we should
533 * rather separate address lifetimes and prefix lifetimes.
536 for (ia6
= in6_ifaddr
; ia6
; ia6
= nia6
) {
538 /* check address lifetime */
539 lt6
= &ia6
->ia6_lifetime
;
540 if (IFA6_IS_INVALID(ia6
)) {
544 * If the expiring address is temporary, try
545 * regenerating a new one. This would be useful when
546 * we suspended a laptop PC, then turned it on after a
547 * period that could invalidate all temporary
548 * addresses. Although we may have to restart the
549 * loop (see below), it must be after purging the
550 * address. Otherwise, we'd see an infinite loop of
553 if (ip6_use_tempaddr
&&
554 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
)) {
555 if (regen_tmpaddr(ia6
) == 0)
559 in6_purgeaddr(&ia6
->ia_ifa
);
562 goto addrloop
; /* XXX: see below */
564 if (IFA6_IS_DEPRECATED(ia6
)) {
565 int oldflags
= ia6
->ia6_flags
;
567 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
570 * If a temporary address has just become deprecated,
571 * regenerate a new one if possible.
573 if (ip6_use_tempaddr
&&
574 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) &&
575 !(oldflags
& IN6_IFF_DEPRECATED
)) {
577 if (regen_tmpaddr(ia6
) == 0) {
579 * A new temporary address is
581 * XXX: this means the address chain
582 * has changed while we are still in
583 * the loop. Although the change
584 * would not cause disaster (because
585 * it's not a deletion, but an
586 * addition,) we'd rather restart the
587 * loop just for safety. Or does this
588 * significantly reduce performance??
595 * A new RA might have made a deprecated address
598 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
602 /* expire prefix list */
603 pr
= nd_prefix
.lh_first
;
606 * check prefix lifetime.
607 * since pltime is just for autoconf, pltime processing for
608 * prefix is not necessary.
610 if (pr
->ndpr_expire
&& pr
->ndpr_expire
< time_second
) {
615 * address expiration and prefix expiration are
616 * separate. NEVER perform in6_purgeaddr here.
628 regen_tmpaddr(struct in6_ifaddr
*ia6
) /* deprecated/invalidated temporary
633 struct in6_ifaddr
*public_ifa6
= NULL
;
635 ifp
= ia6
->ia_ifa
.ifa_ifp
;
636 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_list
) {
637 struct in6_ifaddr
*it6
;
639 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
642 it6
= (struct in6_ifaddr
*)ifa
;
644 /* ignore no autoconf addresses. */
645 if (!(it6
->ia6_flags
& IN6_IFF_AUTOCONF
))
648 /* ignore autoconf addresses with different prefixes. */
649 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
)
653 * Now we are looking at an autoconf address with the same
654 * prefix as ours. If the address is temporary and is still
655 * preferred, do not create another one. It would be rare, but
656 * could happen, for example, when we resume a laptop PC after
659 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) &&
660 !IFA6_IS_DEPRECATED(it6
)) {
666 * This is a public autoconf address that has the same prefix
667 * as ours. If it is preferred, keep it. We can't break the
668 * loop here, because there may be a still-preferred temporary
669 * address with the prefix.
671 if (!IFA6_IS_DEPRECATED(it6
))
675 if (public_ifa6
!= NULL
) {
678 if ((e
= in6_tmpifadd(public_ifa6
, 0)) != 0) {
679 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
680 " tmp addr,errno=%d\n", e
);
690 * Nuke neighbor cache/prefix/default router management table, right before
694 nd6_purge(struct ifnet
*ifp
)
696 struct llinfo_nd6
*ln
, *nln
;
697 struct nd_defrouter
*dr
, *ndr
, drany
;
698 struct nd_prefix
*pr
, *npr
;
700 /* Nuke default router list entries toward ifp */
701 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
703 * The first entry of the list may be stored in
704 * the routing table, so we'll delete it later.
706 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= ndr
) {
707 ndr
= TAILQ_NEXT(dr
, dr_entry
);
711 dr
= TAILQ_FIRST(&nd_defrouter
);
716 /* Nuke prefix list entries toward ifp */
717 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
719 if (pr
->ndpr_ifp
== ifp
) {
721 * Previously, pr->ndpr_addr is removed as well,
722 * but I strongly believe we don't have to do it.
723 * nd6_purge() is only called from in6_ifdetach(),
724 * which removes all the associated interface addresses
726 * (jinmei@kame.net 20010129)
732 /* cancel default outgoing interface setting */
733 if (nd6_defifindex
== ifp
->if_index
)
734 nd6_setdefaultiface(0);
736 if (!ip6_forwarding
&& ip6_accept_rtadv
) { /* XXX: too restrictive? */
737 /* refresh default router list */
738 bzero(&drany
, sizeof(drany
));
739 defrouter_delreq(&drany
, 0);
744 * Nuke neighbor cache entries for the ifp.
745 * Note that rt->rt_ifp may not be the same as ifp,
746 * due to KAME goto ours hack. See RTM_RESOLVE case in
747 * nd6_rtrequest(), and ip6_input().
749 ln
= llinfo_nd6
.ln_next
;
750 while (ln
&& ln
!= &llinfo_nd6
) {
752 struct sockaddr_dl
*sdl
;
756 if (rt
&& rt
->rt_gateway
&&
757 rt
->rt_gateway
->sa_family
== AF_LINK
) {
758 sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
;
759 if (sdl
->sdl_index
== ifp
->if_index
)
767 nd6_lookup(struct in6_addr
*addr6
, int create
, struct ifnet
*ifp
)
770 struct sockaddr_in6 sin6
;
772 bzero(&sin6
, sizeof(sin6
));
773 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
774 sin6
.sin6_family
= AF_INET6
;
775 sin6
.sin6_addr
= *addr6
;
778 rt
= rtlookup((struct sockaddr
*)&sin6
);
780 rt
= rtpurelookup((struct sockaddr
*)&sin6
);
781 if (rt
&& !(rt
->rt_flags
& RTF_LLINFO
)) {
783 * This is the case for the default route.
784 * If we want to create a neighbor cache for the address, we
785 * should free the route for the destination and allocate an
798 * If no route is available and create is set,
799 * we allocate a host route for the destination
800 * and treat it like an interface route.
801 * This hack is necessary for a neighbor which can't
802 * be covered by our own prefix.
805 ifaof_ifpforaddr((struct sockaddr
*)&sin6
, ifp
);
810 * Create a new route. RTF_LLINFO is necessary
811 * to create a Neighbor Cache entry for the
812 * destination in nd6_rtrequest which will be
813 * called in rtrequest via ifa->ifa_rtrequest.
815 if ((e
= rtrequest(RTM_ADD
, (struct sockaddr
*)&sin6
,
817 (struct sockaddr
*)&all1_sa
,
819 RTF_HOST
| RTF_LLINFO
) &
823 "nd6_lookup: failed to add route for a "
824 "neighbor(%s), errno=%d\n",
825 ip6_sprintf(addr6
), e
);
829 struct llinfo_nd6
*ln
=
830 (struct llinfo_nd6
*)rt
->rt_llinfo
;
831 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
838 * Validation for the entry.
839 * Note that the check for rt_llinfo is necessary because a cloned
840 * route from a parent route that has the L flag (e.g. the default
841 * route to a p2p interface) may have the flag, too, while the
842 * destination is not actually a neighbor.
843 * XXX: we can't use rt->rt_ifp to check for the interface, since
844 * it might be the loopback interface if the entry is for our
845 * own address on a non-loopback interface. Instead, we should
846 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
849 if ((rt
->rt_flags
& RTF_GATEWAY
) || !(rt
->rt_flags
& RTF_LLINFO
) ||
850 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
851 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
)) {
853 log(LOG_DEBUG
, "nd6_lookup: failed to lookup %s (if = %s)\n",
854 ip6_sprintf(addr6
), ifp
? if_name(ifp
) : "unspec");
855 /* xxx more logs... kazu */
863 * Detect if a given IPv6 address identifies a neighbor on a given link.
864 * XXX: should take care of the destination of a p2p link?
867 nd6_is_addr_neighbor(struct sockaddr_in6
*addr
, struct ifnet
*ifp
)
872 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
873 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
876 * A link-local address is always a neighbor.
877 * XXX: we should use the sin6_scope_id field rather than the embedded
880 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
) &&
881 ntohs(*(u_int16_t
*)&addr
->sin6_addr
.s6_addr
[2]) == ifp
->if_index
)
885 * If the address matches one of our addresses,
886 * it should be a neighbor.
888 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
889 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
892 for (i
= 0; i
< 4; i
++) {
893 if ((IFADDR6(ifa
).s6_addr32
[i
] ^
894 addr
->sin6_addr
.s6_addr32
[i
]) &
895 IFMASK6(ifa
).s6_addr32
[i
])
902 * Even if the address matches none of our addresses, it might be
903 * in the neighbor cache.
905 if (nd6_lookup(&addr
->sin6_addr
, 0, ifp
) != NULL
)
914 * Free an nd6 llinfo entry.
917 nd6_free(struct rtentry
*rt
)
919 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
, *next
;
920 struct in6_addr in6
= ((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
921 struct nd_defrouter
*dr
;
924 * we used to have pfctlinput(PRC_HOSTDEAD) here.
925 * even though it is not harmful, it was not really necessary.
928 if (!ip6_forwarding
&& ip6_accept_rtadv
) { /* XXX: too restrictive? */
930 dr
= defrouter_lookup(&((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
,
933 if (ln
->ln_router
|| dr
) {
935 * rt6_flush must be called whether or not the neighbor
936 * is in the Default Router List.
937 * See a corresponding comment in nd6_na_input().
939 rt6_flush(&in6
, rt
->rt_ifp
);
944 * Unreachablity of a router might affect the default
945 * router selection and on-link detection of advertised
950 * Temporarily fake the state to choose a new default
951 * router and to perform on-link determination of
952 * prefixes correctly.
953 * Below the state will be set correctly,
954 * or the entry itself will be deleted.
956 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
959 * Since defrouter_select() does not affect the
960 * on-link determination and MIP6 needs the check
961 * before the default router selection, we perform
964 pfxlist_onlink_check();
966 if (dr
== TAILQ_FIRST(&nd_defrouter
)) {
968 * It is used as the current default router,
969 * so we have to move it to the end of the
970 * list and choose a new one.
971 * XXX: it is not very efficient if this is
974 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
975 TAILQ_INSERT_TAIL(&nd_defrouter
, dr
, dr_entry
);
984 * Before deleting the entry, remember the next entry as the
985 * return value. We need this because pfxlist_onlink_check() above
986 * might have freed other entries (particularly the old next entry) as
987 * a side effect (XXX).
992 * Detach the route from the routing tree and the list of neighbor
993 * caches, and disable the route entry not to be used in already
996 rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
997 rt_mask(rt
), 0, (struct rtentry
**)0);
1003 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1005 * XXX cost-effective metods?
1008 nd6_nud_hint(struct rtentry
*rt
, struct in6_addr
*dst6
, int force
)
1010 struct llinfo_nd6
*ln
;
1013 * If the caller specified "rt", use that. Otherwise, resolve the
1014 * routing table by supplied "dst6".
1019 if (!(rt
= nd6_lookup(dst6
, 0, NULL
)))
1023 if ((rt
->rt_flags
& RTF_GATEWAY
) ||
1024 !(rt
->rt_flags
& RTF_LLINFO
) ||
1025 rt
->rt_llinfo
== NULL
|| rt
->rt_gateway
== NULL
||
1026 rt
->rt_gateway
->sa_family
!= AF_LINK
) {
1027 /* This is not a host route. */
1031 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1032 if (ln
->ln_state
< ND6_LLINFO_REACHABLE
)
1036 * if we get upper-layer reachability confirmation many times,
1037 * it is possible we have false information.
1041 if (ln
->ln_byhint
> nd6_maxnudhint
)
1045 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1047 ln
->ln_expire
= time_second
+
1048 ND_IFINFO(rt
->rt_ifp
)->reachable
;
1052 nd6_rtrequest(int req
, struct rtentry
*rt
,
1053 struct rt_addrinfo
*info
) /* xxx unused */
1055 struct sockaddr
*gate
= rt
->rt_gateway
;
1056 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1057 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
};
1058 struct ifnet
*ifp
= rt
->rt_ifp
;
1061 if ((rt
->rt_flags
& RTF_GATEWAY
))
1064 if (nd6_need_cache(ifp
) == 0 && !(rt
->rt_flags
& RTF_HOST
)) {
1066 * This is probably an interface direct route for a link
1067 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1068 * We do not need special treatment below for such a route.
1069 * Moreover, the RTF_LLINFO flag which would be set below
1070 * would annoy the ndp(8) command.
1075 if (req
== RTM_RESOLVE
&&
1076 (nd6_need_cache(ifp
) == 0 || /* stf case */
1077 !nd6_is_addr_neighbor((struct sockaddr_in6
*)rt_key(rt
), ifp
))) {
1079 * FreeBSD and BSD/OS often make a cloned host route based
1080 * on a less-specific route (e.g. the default route).
1081 * If the less specific route does not have a "gateway"
1082 * (this is the case when the route just goes to a p2p or an
1083 * stf interface), we'll mistakenly make a neighbor cache for
1084 * the host route, and will see strange neighbor solicitation
1085 * for the corresponding destination. In order to avoid the
1086 * confusion, we check if the destination of the route is
1087 * a neighbor in terms of neighbor discovery, and stop the
1088 * process if not. Additionally, we remove the LLINFO flag
1089 * so that ndp(8) will not try to get the neighbor information
1090 * of the destination.
1092 rt
->rt_flags
&= ~RTF_LLINFO
;
1099 * There is no backward compatibility :)
1101 * if (!(rt->rt_flags & RTF_HOST) &&
1102 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1103 * rt->rt_flags |= RTF_CLONING;
1105 if (rt
->rt_flags
& (RTF_CLONING
| RTF_LLINFO
)) {
1107 * Case 1: This route should come from
1108 * a route to interface. RTF_LLINFO flag is set
1109 * for a host route whose destination should be
1110 * treated as on-link.
1112 rt_setgate(rt
, rt_key(rt
),
1113 (struct sockaddr
*)&null_sdl
);
1114 gate
= rt
->rt_gateway
;
1115 SDL(gate
)->sdl_type
= ifp
->if_type
;
1116 SDL(gate
)->sdl_index
= ifp
->if_index
;
1118 ln
->ln_expire
= time_second
;
1120 if (ln
&& ln
->ln_expire
== 0) {
1121 /* kludge for desktops */
1123 kprintf("nd6_rtequest: time.tv_sec is zero; "
1129 if ((rt
->rt_flags
& RTF_CLONING
))
1133 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1134 * We don't do that here since llinfo is not ready yet.
1136 * There are also couple of other things to be discussed:
1137 * - unsolicited NA code needs improvement beforehand
1138 * - RFC2461 says we MAY send multicast unsolicited NA
1139 * (7.2.6 paragraph 4), however, it also says that we
1140 * SHOULD provide a mechanism to prevent multicast NA storm.
1141 * we don't have anything like it right now.
1142 * note that the mechanism needs a mutual agreement
1143 * between proxies, which means that we need to implement
1144 * a new protocol, or a new kludge.
1145 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1146 * we need to check ip6forwarding before sending it.
1147 * (or should we allow proxy ND configuration only for
1148 * routers? there's no mention about proxy ND from hosts)
1151 /* XXX it does not work */
1152 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1154 &SIN6(rt_key(rt
))->sin6_addr
,
1155 &SIN6(rt_key(rt
))->sin6_addr
,
1156 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1161 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) == 0) {
1163 * Address resolution isn't necessary for a point to
1164 * point link, so we can skip this test for a p2p link.
1166 if (gate
->sa_family
!= AF_LINK
||
1167 gate
->sa_len
< sizeof(null_sdl
)) {
1169 "nd6_rtrequest: bad gateway value: %s\n",
1173 SDL(gate
)->sdl_type
= ifp
->if_type
;
1174 SDL(gate
)->sdl_index
= ifp
->if_index
;
1177 break; /* This happens on a route change */
1179 * Case 2: This route may come from cloning, or a manual route
1180 * add with a LL address.
1182 R_Malloc(ln
, struct llinfo_nd6
*, sizeof(*ln
));
1183 rt
->rt_llinfo
= (caddr_t
)ln
;
1185 log(LOG_DEBUG
, "nd6_rtrequest: malloc failed\n");
1190 bzero(ln
, sizeof(*ln
));
1192 /* this is required for "ndp" command. - shin */
1193 if (req
== RTM_ADD
) {
1195 * gate should have some valid AF_LINK entry,
1196 * and ln->ln_expire should have some lifetime
1197 * which is specified by ndp command.
1199 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1203 * When req == RTM_RESOLVE, rt is created and
1204 * initialized in rtrequest(), so rt_expire is 0.
1206 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1207 ln
->ln_expire
= time_second
;
1209 rt
->rt_flags
|= RTF_LLINFO
;
1210 ln
->ln_next
= llinfo_nd6
.ln_next
;
1211 llinfo_nd6
.ln_next
= ln
;
1212 ln
->ln_prev
= &llinfo_nd6
;
1213 ln
->ln_next
->ln_prev
= ln
;
1216 * check if rt_key(rt) is one of my address assigned
1219 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1220 &SIN6(rt_key(rt
))->sin6_addr
);
1222 caddr_t macp
= nd6_ifptomac(ifp
);
1224 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1227 bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
1228 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
1230 if (nd6_useloopback
) {
1231 rt
->rt_ifp
= &loif
[0]; /* XXX */
1233 * Make sure rt_ifa be equal to the ifaddr
1234 * corresponding to the address.
1235 * We need this because when we refer
1236 * rt_ifa->ia6_flags in ip6_input, we assume
1237 * that the rt_ifa points to the address instead
1238 * of the loopback address.
1240 if (ifa
!= rt
->rt_ifa
) {
1241 IFAFREE(rt
->rt_ifa
);
1246 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
1248 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1251 /* join solicited node multicast for proxy ND */
1252 if (ifp
->if_flags
& IFF_MULTICAST
) {
1253 struct in6_addr llsol
;
1256 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1257 llsol
.s6_addr16
[0] = htons(0xff02);
1258 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1259 llsol
.s6_addr32
[1] = 0;
1260 llsol
.s6_addr32
[2] = htonl(1);
1261 llsol
.s6_addr8
[12] = 0xff;
1263 if (!in6_addmulti(&llsol
, ifp
, &error
)) {
1264 nd6log((LOG_ERR
, "%s: failed to join "
1265 "%s (errno=%d)\n", if_name(ifp
),
1266 ip6_sprintf(&llsol
), error
));
1275 /* leave from solicited node multicast for proxy ND */
1276 if ((rt
->rt_flags
& RTF_ANNOUNCE
) &&
1277 (ifp
->if_flags
& IFF_MULTICAST
)) {
1278 struct in6_addr llsol
;
1279 struct in6_multi
*in6m
;
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 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1293 ln
->ln_next
->ln_prev
= ln
->ln_prev
;
1294 ln
->ln_prev
->ln_next
= ln
->ln_next
;
1297 rt
->rt_flags
&= ~RTF_LLINFO
;
1299 m_freem(ln
->ln_hold
);
1305 nd6_ioctl(u_long cmd
, caddr_t data
, struct ifnet
*ifp
)
1307 struct in6_drlist
*drl
= (struct in6_drlist
*)data
;
1308 struct in6_prlist
*prl
= (struct in6_prlist
*)data
;
1309 struct in6_ndireq
*ndi
= (struct in6_ndireq
*)data
;
1310 struct in6_nbrinfo
*nbi
= (struct in6_nbrinfo
*)data
;
1311 struct in6_ndifreq
*ndif
= (struct in6_ndifreq
*)data
;
1312 struct nd_defrouter
*dr
, any
;
1313 struct nd_prefix
*pr
;
1315 int i
= 0, error
= 0;
1318 case SIOCGDRLST_IN6
:
1320 * obsolete API, use sysctl under net.inet6.icmp6
1322 bzero(drl
, sizeof(*drl
));
1324 dr
= TAILQ_FIRST(&nd_defrouter
);
1325 while (dr
&& i
< DRLSTSIZ
) {
1326 drl
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1327 if (IN6_IS_ADDR_LINKLOCAL(&drl
->defrouter
[i
].rtaddr
)) {
1328 /* XXX: need to this hack for KAME stack */
1329 drl
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1332 "default router list contains a "
1333 "non-linklocal address(%s)\n",
1334 ip6_sprintf(&drl
->defrouter
[i
].rtaddr
));
1336 drl
->defrouter
[i
].flags
= dr
->flags
;
1337 drl
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1338 drl
->defrouter
[i
].expire
= dr
->expire
;
1339 drl
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1341 dr
= TAILQ_NEXT(dr
, dr_entry
);
1345 case SIOCGPRLST_IN6
:
1347 * obsolete API, use sysctl under net.inet6.icmp6
1350 * XXX meaning of fields, especialy "raflags", is very
1351 * differnet between RA prefix list and RR/static prefix list.
1352 * how about separating ioctls into two?
1354 bzero(prl
, sizeof(*prl
));
1356 pr
= nd_prefix
.lh_first
;
1357 while (pr
&& i
< PRLSTSIZ
) {
1358 struct nd_pfxrouter
*pfr
;
1361 in6_embedscope(&prl
->prefix
[i
].prefix
,
1362 &pr
->ndpr_prefix
, NULL
, NULL
);
1363 prl
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1364 prl
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1365 prl
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1366 prl
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1367 prl
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1368 prl
->prefix
[i
].expire
= pr
->ndpr_expire
;
1370 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1374 #define RTRADDR prl->prefix[i].advrtr[j]
1375 RTRADDR
= pfr
->router
->rtaddr
;
1376 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1377 /* XXX: hack for KAME */
1378 RTRADDR
.s6_addr16
[1] = 0;
1381 "a router(%s) advertises "
1383 "non-link local address\n",
1384 ip6_sprintf(&RTRADDR
));
1388 pfr
= pfr
->pfr_next
;
1390 prl
->prefix
[i
].advrtrs
= j
;
1391 prl
->prefix
[i
].origin
= PR_ORIG_RA
;
1397 struct rr_prefix
*rpp
;
1399 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1400 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1403 in6_embedscope(&prl
->prefix
[i
].prefix
,
1404 &pr
->ndpr_prefix
, NULL
, NULL
);
1405 prl
->prefix
[i
].raflags
= rpp
->rp_raf
;
1406 prl
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1407 prl
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1408 prl
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1409 prl
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1410 prl
->prefix
[i
].expire
= rpp
->rp_expire
;
1411 prl
->prefix
[i
].advrtrs
= 0;
1412 prl
->prefix
[i
].origin
= rpp
->rp_origin
;
1419 case OSIOCGIFINFO_IN6
:
1420 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1421 bzero(&ndi
->ndi
, sizeof(ndi
->ndi
));
1422 ndi
->ndi
.linkmtu
= ND_IFINFO(ifp
)->linkmtu
;
1423 ndi
->ndi
.maxmtu
= ND_IFINFO(ifp
)->maxmtu
;
1424 ndi
->ndi
.basereachable
= ND_IFINFO(ifp
)->basereachable
;
1425 ndi
->ndi
.reachable
= ND_IFINFO(ifp
)->reachable
;
1426 ndi
->ndi
.retrans
= ND_IFINFO(ifp
)->retrans
;
1427 ndi
->ndi
.flags
= ND_IFINFO(ifp
)->flags
;
1428 ndi
->ndi
.recalctm
= ND_IFINFO(ifp
)->recalctm
;
1429 ndi
->ndi
.chlim
= ND_IFINFO(ifp
)->chlim
;
1430 ndi
->ndi
.receivedra
= ND_IFINFO(ifp
)->receivedra
;
1432 case SIOCGIFINFO_IN6
:
1433 ndi
->ndi
= *ND_IFINFO(ifp
);
1435 case SIOCSIFINFO_FLAGS
:
1436 ND_IFINFO(ifp
)->flags
= ndi
->ndi
.flags
;
1438 case SIOCSNDFLUSH_IN6
: /* XXX: the ioctl name is confusing... */
1439 /* flush default router list */
1441 * xxx sumikawa: should not delete route if default
1442 * route equals to the top of default router list
1444 bzero(&any
, sizeof(any
));
1445 defrouter_delreq(&any
, 0);
1447 /* xxx sumikawa: flush prefix list */
1449 case SIOCSPFXFLUSH_IN6
:
1451 /* flush all the prefix advertised by routers */
1452 struct nd_prefix
*pr
, *next
;
1455 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
1456 struct in6_ifaddr
*ia
, *ia_next
;
1458 next
= pr
->ndpr_next
;
1460 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
))
1463 /* do we really have to remove addresses as well? */
1464 for (ia
= in6_ifaddr
; ia
; ia
= ia_next
) {
1465 /* ia might be removed. keep the next ptr. */
1466 ia_next
= ia
->ia_next
;
1468 if (!(ia
->ia6_flags
& IN6_IFF_AUTOCONF
))
1471 if (ia
->ia6_ndpr
== pr
)
1472 in6_purgeaddr(&ia
->ia_ifa
);
1479 case SIOCSRTRFLUSH_IN6
:
1481 /* flush all the default routers */
1482 struct nd_defrouter
*dr
, *next
;
1485 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
1487 * The first entry of the list may be stored in
1488 * the routing table, so we'll delete it later.
1490 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
1491 next
= TAILQ_NEXT(dr
, dr_entry
);
1494 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
));
1499 case SIOCGNBRINFO_IN6
:
1501 struct llinfo_nd6
*ln
;
1502 struct in6_addr nb_addr
= nbi
->addr
; /* make local for safety */
1505 * XXX: KAME specific hack for scoped addresses
1506 * XXXX: for other scopes than link-local?
1508 if (IN6_IS_ADDR_LINKLOCAL(&nbi
->addr
) ||
1509 IN6_IS_ADDR_MC_LINKLOCAL(&nbi
->addr
)) {
1510 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
1513 *idp
= htons(ifp
->if_index
);
1517 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
)) == NULL
) {
1522 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1523 nbi
->state
= ln
->ln_state
;
1524 nbi
->asked
= ln
->ln_asked
;
1525 nbi
->isrouter
= ln
->ln_router
;
1526 nbi
->expire
= ln
->ln_expire
;
1531 case SIOCGDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1532 ndif
->ifindex
= nd6_defifindex
;
1534 case SIOCSDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1535 return (nd6_setdefaultiface(ndif
->ifindex
));
1542 * Create neighbor cache entry and cache link-layer address,
1543 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1546 nd6_cache_lladdr(struct ifnet
*ifp
, struct in6_addr
*from
, char *lladdr
,
1548 int type
, /* ICMP6 type */
1549 int code
/* type dependent information */)
1551 struct rtentry
*rt
= NULL
;
1552 struct llinfo_nd6
*ln
= NULL
;
1554 struct sockaddr_dl
*sdl
= NULL
;
1561 panic("ifp == NULL in nd6_cache_lladdr");
1563 panic("from == NULL in nd6_cache_lladdr");
1565 /* nothing must be updated for unspecified address */
1566 if (IN6_IS_ADDR_UNSPECIFIED(from
))
1570 * Validation about ifp->if_addrlen and lladdrlen must be done in
1573 * XXX If the link does not have link-layer adderss, what should
1574 * we do? (ifp->if_addrlen == 0)
1575 * Spec says nothing in sections for RA, RS and NA. There's small
1576 * description on it in NS section (RFC 2461 7.2.3).
1579 rt
= nd6_lookup(from
, 0, ifp
);
1582 /* nothing must be done if there's no lladdr */
1583 if (!lladdr
|| !lladdrlen
)
1587 rt
= nd6_lookup(from
, 1, ifp
);
1590 /* do nothing if static ndp is set */
1591 if (rt
->rt_flags
& RTF_STATIC
)
1598 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
1603 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1606 if (!rt
->rt_gateway
)
1608 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
1610 sdl
= SDL(rt
->rt_gateway
);
1612 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
1613 if (olladdr
&& lladdr
) {
1614 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
1622 * newentry olladdr lladdr llchange (*=record)
1625 * 0 n y -- (3) * STALE
1627 * 0 y y y (5) * STALE
1628 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1629 * 1 -- y -- (7) * STALE
1632 if (lladdr
) { /* (3-5) and (7) */
1634 * Record source link-layer address
1635 * XXX is it dependent to ifp->if_type?
1637 sdl
->sdl_alen
= ifp
->if_addrlen
;
1638 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
1642 if ((!olladdr
&& lladdr
) /* (3) */
1643 || (olladdr
&& lladdr
&& llchange
)) { /* (5) */
1645 newstate
= ND6_LLINFO_STALE
;
1646 } else /* (1-2,4) */
1650 if (!lladdr
) /* (6) */
1651 newstate
= ND6_LLINFO_NOSTATE
;
1653 newstate
= ND6_LLINFO_STALE
;
1658 * Update the state of the neighbor cache.
1660 ln
->ln_state
= newstate
;
1662 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1664 * XXX: since nd6_output() below will cause
1665 * state tansition to DELAY and reset the timer,
1666 * we must set the timer now, although it is actually
1669 ln
->ln_expire
= time_second
+ nd6_gctimer
;
1673 * we assume ifp is not a p2p here, so just
1674 * set the 2nd argument as the 1st one.
1676 nd6_output(ifp
, ifp
, ln
->ln_hold
,
1677 (struct sockaddr_in6
*)rt_key(rt
),
1681 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
1682 /* probe right away */
1683 ln
->ln_expire
= time_second
;
1688 * ICMP6 type dependent behavior.
1690 * NS: clear IsRouter if new entry
1691 * RS: clear IsRouter
1692 * RA: set IsRouter if there's lladdr
1693 * redir: clear IsRouter if new entry
1696 * The spec says that we must set IsRouter in the following cases:
1697 * - If lladdr exist, set IsRouter. This means (1-5).
1698 * - If it is old entry (!newentry), set IsRouter. This means (7).
1699 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1700 * A quetion arises for (1) case. (1) case has no lladdr in the
1701 * neighbor cache, this is similar to (6).
1702 * This case is rare but we figured that we MUST NOT set IsRouter.
1704 * newentry olladdr lladdr llchange NS RS RA redir
1706 * 0 n n -- (1) c ? s
1707 * 0 y n -- (2) c s s
1708 * 0 n y -- (3) c s s
1711 * 1 -- n -- (6) c c c s
1712 * 1 -- y -- (7) c c s c s
1716 switch (type
& 0xff) {
1717 case ND_NEIGHBOR_SOLICIT
:
1719 * New entry must have is_router flag cleared.
1721 if (is_newentry
) /* (6-7) */
1726 * If the icmp is a redirect to a better router, always set the
1727 * is_router flag. Otherwise, if the entry is newly created,
1728 * clear the flag. [RFC 2461, sec 8.3]
1730 if (code
== ND_REDIRECT_ROUTER
)
1732 else if (is_newentry
) /* (6-7) */
1735 case ND_ROUTER_SOLICIT
:
1737 * is_router flag must always be cleared.
1741 case ND_ROUTER_ADVERT
:
1743 * Mark an entry with lladdr as a router.
1745 if ((!is_newentry
&& (olladdr
|| lladdr
)) /* (2-5) */
1746 || (is_newentry
&& lladdr
)) { /* (7) */
1753 * When the link-layer address of a router changes, select the
1754 * best router again. In particular, when the neighbor entry is newly
1755 * created, it might affect the selection policy.
1756 * Question: can we restrict the first condition to the "is_newentry"
1758 * XXX: when we hear an RA from a new router with the link-layer
1759 * address option, defrouter_select() is called twice, since
1760 * defrtrlist_update called the function as well. However, I believe
1761 * we can compromise the overhead, since it only happens the first
1763 * XXX: although defrouter_select() should not have a bad effect
1764 * for those are not autoconfigured hosts, we explicitly avoid such
1767 if (do_update
&& ln
->ln_router
&& !ip6_forwarding
&& ip6_accept_rtadv
)
1774 nd6_slowtimo(void *ignored_arg
)
1776 struct nd_ifinfo
*nd6if
;
1780 callout_reset(&nd6_slowtimo_ch
, ND6_SLOWTIMER_INTERVAL
* hz
,
1781 nd6_slowtimo
, NULL
);
1782 for (ifp
= TAILQ_FIRST(&ifnet
); ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
)) {
1783 nd6if
= ND_IFINFO(ifp
);
1784 if (nd6if
->basereachable
&& /* already initialized */
1785 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
1787 * Since reachable time rarely changes by router
1788 * advertisements, we SHOULD insure that a new random
1789 * value gets recomputed at least once every few hours.
1792 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
1793 nd6if
->reachable
= ND_COMPUTE_RTIME(nd6if
->basereachable
);
1799 #define gotoerr(e) { error = (e); goto bad;}
1802 nd6_output(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m
,
1803 struct sockaddr_in6
*dst
, struct rtentry
*rt
)
1805 struct llinfo_nd6
*ln
= NULL
;
1808 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
))
1811 if (nd6_need_cache(ifp
) == 0)
1815 * next hop determination. This routine is derived from ether_outpout.
1818 if (!(rt
->rt_flags
& RTF_UP
)) {
1819 rt
= rtlookup((struct sockaddr
*)dst
);
1821 gotoerr(EHOSTUNREACH
);
1823 if (rt
->rt_ifp
!= ifp
) {
1824 /* XXX: loop care? */
1825 return nd6_output(ifp
, origifp
, m
, dst
, rt
);
1828 if (rt
->rt_flags
& RTF_GATEWAY
) {
1829 struct sockaddr_in6
*gw6
;
1832 * We skip link-layer address resolution and NUD
1833 * if the gateway is not a neighbor from ND point
1834 * of view, regardless of the value of nd_ifinfo.flags.
1835 * The second condition is a bit tricky; we skip
1836 * if the gateway is our own address, which is
1837 * sometimes used to install a route to a p2p link.
1839 gw6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
1840 if (!nd6_is_addr_neighbor(gw6
, ifp
) ||
1841 in6ifa_ifpwithaddr(ifp
, &gw6
->sin6_addr
)) {
1843 * We allow this kind of tricky route only
1844 * when the outgoing interface is p2p.
1845 * XXX: we may need a more generic rule here.
1847 if (!(ifp
->if_flags
& IFF_POINTOPOINT
))
1848 gotoerr(EHOSTUNREACH
);
1853 if (rt
->rt_gwroute
== NULL
) {
1854 rt
->rt_gwroute
= rtlookup(rt
->rt_gateway
);
1855 if (rt
->rt_gwroute
== NULL
)
1856 gotoerr(EHOSTUNREACH
);
1857 } else if (!(rt
->rt_gwroute
->rt_flags
& RTF_UP
)) {
1858 rtfree(rt
->rt_gwroute
);
1859 rt
->rt_gwroute
= rtlookup(rt
->rt_gateway
);
1860 if (rt
->rt_gwroute
== NULL
)
1861 gotoerr(EHOSTUNREACH
);
1867 * Address resolution or Neighbor Unreachability Detection
1869 * At this point, the destination of the packet must be a unicast
1870 * or an anycast address(i.e. not a multicast).
1873 /* Look up the neighbor cache for the nexthop */
1874 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
))
1875 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1878 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1879 * the condition below is not very efficient. But we believe
1880 * it is tolerable, because this should be a rare case.
1882 if (nd6_is_addr_neighbor(dst
, ifp
) &&
1883 (rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
)) != NULL
)
1884 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1887 if (!(ifp
->if_flags
& IFF_POINTOPOINT
) &&
1888 !(ND_IFINFO(ifp
)->flags
& ND6_IFF_PERFORMNUD
)) {
1890 "nd6_output: can't allocate llinfo for %s "
1892 ip6_sprintf(&dst
->sin6_addr
), ln
, rt
);
1893 gotoerr(EIO
); /* XXX: good error? */
1896 goto sendpkt
; /* send anyway */
1899 /* We don't have to do link-layer address resolution on a p2p link. */
1900 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
1901 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
1902 ln
->ln_state
= ND6_LLINFO_STALE
;
1903 ln
->ln_expire
= time_second
+ nd6_gctimer
;
1907 * The first time we send a packet to a neighbor whose entry is
1908 * STALE, we have to change the state to DELAY and a sets a timer to
1909 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1910 * neighbor unreachability detection on expiration.
1913 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1915 ln
->ln_state
= ND6_LLINFO_DELAY
;
1916 ln
->ln_expire
= time_second
+ nd6_delay
;
1920 * If the neighbor cache entry has a state other than INCOMPLETE
1921 * (i.e. its link-layer address is already resolved), just
1924 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
)
1928 * There is a neighbor cache entry, but no ethernet address
1929 * response yet. Replace the held mbuf (if any) with this
1932 * This code conforms to the rate-limiting rule described in Section
1933 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
1936 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
)
1937 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1939 m_freem(ln
->ln_hold
);
1941 if (ln
->ln_expire
) {
1942 if (ln
->ln_asked
< nd6_mmaxtries
&&
1943 ln
->ln_expire
< time_second
) {
1945 ln
->ln_expire
= time_second
+
1946 ND_IFINFO(ifp
)->retrans
/ 1000;
1947 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0);
1953 lwkt_serialize_enter(ifp
->if_serializer
);
1954 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1955 error
= (*ifp
->if_output
)(origifp
, m
, (struct sockaddr
*)dst
,
1958 error
= (*ifp
->if_output
)(ifp
, m
, (struct sockaddr
*)dst
, rt
);
1960 lwkt_serialize_exit(ifp
->if_serializer
);
1970 nd6_need_cache(struct ifnet
*ifp
)
1973 * XXX: we currently do not make neighbor cache on any interface
1974 * other than Ethernet and GIF.
1977 * - unidirectional tunnels needs no ND
1979 switch (ifp
->if_type
) {
1985 #ifdef IFT_IEEE80211
1991 case IFT_GIF
: /* XXX need more cases? */
1999 nd6_storelladdr(struct ifnet
*ifp
, struct rtentry
*rt0
, struct mbuf
*m
,
2000 struct sockaddr
*dst
, u_char
*desten
)
2002 struct sockaddr_dl
*sdl
;
2006 if (m
->m_flags
& M_MCAST
) {
2007 switch (ifp
->if_type
) {
2012 #ifdef IFT_IEEE80211
2015 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
2019 bcopy(ifp
->if_broadcastaddr
, desten
, ifp
->if_addrlen
);
2027 /* this could happen, if we could not allocate memory */
2031 if (rt_llroute(dst
, rt0
, &rt
) != 0) {
2035 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
2036 kprintf("nd6_storelladdr: something odd happens\n");
2040 sdl
= SDL(rt
->rt_gateway
);
2041 if (sdl
->sdl_alen
== 0) {
2042 /* this should be impossible, but we bark here for debugging */
2043 kprintf("nd6_storelladdr: sdl_alen == 0\n");
2048 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
2052 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS
);
2053 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS
);
2055 SYSCTL_DECL(_net_inet6_icmp6
);
2057 SYSCTL_NODE(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
2058 CTLFLAG_RD
, nd6_sysctl_drlist
, "");
2059 SYSCTL_NODE(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
2060 CTLFLAG_RD
, nd6_sysctl_prlist
, "");
2063 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS
)
2067 struct in6_defrouter
*d
, *de
;
2068 struct nd_defrouter
*dr
;
2074 for (dr
= TAILQ_FIRST(&nd_defrouter
);
2076 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2077 d
= (struct in6_defrouter
*)buf
;
2078 de
= (struct in6_defrouter
*)(buf
+ sizeof(buf
));
2081 bzero(d
, sizeof(*d
));
2082 d
->rtaddr
.sin6_family
= AF_INET6
;
2083 d
->rtaddr
.sin6_len
= sizeof(d
->rtaddr
);
2084 if (in6_recoverscope(&d
->rtaddr
, &dr
->rtaddr
,
2088 "default router list (%s)\n",
2089 ip6_sprintf(&dr
->rtaddr
));
2090 d
->flags
= dr
->flags
;
2091 d
->rtlifetime
= dr
->rtlifetime
;
2092 d
->expire
= dr
->expire
;
2093 d
->if_index
= dr
->ifp
->if_index
;
2095 panic("buffer too short");
2097 error
= SYSCTL_OUT(req
, buf
, sizeof(*d
));
2105 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS
)
2109 struct in6_prefix
*p
, *pe
;
2110 struct nd_prefix
*pr
;
2116 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2119 struct sockaddr_in6
*sin6
, *s6
;
2120 struct nd_pfxrouter
*pfr
;
2122 p
= (struct in6_prefix
*)buf
;
2123 pe
= (struct in6_prefix
*)(buf
+ sizeof(buf
));
2126 bzero(p
, sizeof(*p
));
2127 sin6
= (struct sockaddr_in6
*)(p
+ 1);
2129 p
->prefix
= pr
->ndpr_prefix
;
2130 if (in6_recoverscope(&p
->prefix
,
2131 &p
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
2133 "scope error in prefix list (%s)\n",
2134 ip6_sprintf(&p
->prefix
.sin6_addr
));
2135 p
->raflags
= pr
->ndpr_raf
;
2136 p
->prefixlen
= pr
->ndpr_plen
;
2137 p
->vltime
= pr
->ndpr_vltime
;
2138 p
->pltime
= pr
->ndpr_pltime
;
2139 p
->if_index
= pr
->ndpr_ifp
->if_index
;
2140 p
->expire
= pr
->ndpr_expire
;
2141 p
->refcnt
= pr
->ndpr_refcnt
;
2142 p
->flags
= pr
->ndpr_stateflags
;
2143 p
->origin
= PR_ORIG_RA
;
2145 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
2147 pfr
= pfr
->pfr_next
) {
2148 if ((void *)&sin6
[advrtrs
+ 1] >
2153 s6
= &sin6
[advrtrs
];
2154 bzero(s6
, sizeof(*s6
));
2155 s6
->sin6_family
= AF_INET6
;
2156 s6
->sin6_len
= sizeof(*sin6
);
2157 if (in6_recoverscope(s6
, &pfr
->router
->rtaddr
,
2158 pfr
->router
->ifp
) != 0)
2161 "prefix list (%s)\n",
2162 ip6_sprintf(&pfr
->router
->rtaddr
));
2165 p
->advrtrs
= advrtrs
;
2167 panic("buffer too short");
2169 advance
= sizeof(*p
) + sizeof(*sin6
) * advrtrs
;
2170 error
= SYSCTL_OUT(req
, buf
, advance
);