1 /* $FreeBSD: src/sys/netinet6/nd6.c,v 1.20 2002/08/02 20:49:14 rwatson Exp $ */
2 /* $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * BSD/OS version heavily modifies this code, related to llinfo.
37 * Since we don't have BSD/OS version of net/route.c in our hand,
38 * I left the code mostly as it was in 970310. -- itojun
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
48 #include <sys/kernel.h>
49 #include <sys/errno.h>
50 #include <sys/syslog.h>
51 #include <sys/protosw.h>
52 #include <kern/queue.h>
54 #define DONT_WARN_OBSOLETE
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/if_atm.h>
59 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/if_ether.h>
64 #include <netinet/if_fddi.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/ip6_var.h>
68 #include <netinet6/nd6.h>
69 #include <netinet6/in6_prefix.h>
70 #include <netinet/icmp6.h>
74 #include <net/net_osdep.h>
76 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
77 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
79 #define SIN6(s) ((struct sockaddr_in6 *)s)
80 #define SDL(s) ((struct sockaddr_dl *)s)
83 int nd6_prune
= 1; /* walk list every 1 seconds */
84 int nd6_delay
= 5; /* delay first probe time 5 second */
85 int nd6_umaxtries
= 3; /* maximum unicast query */
86 int nd6_mmaxtries
= 3; /* maximum multicast query */
87 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
88 int nd6_gctimer
= (60 * 60 * 24); /* 1 day: garbage collection timer */
90 /* preventing too many loops in ND option parsing */
91 int nd6_maxndopt
= 10; /* max # of ND options allowed */
93 int nd6_maxnudhint
= 0; /* max # of subsequent upper layer hints */
102 static int nd6_inuse
, nd6_allocated
;
104 struct llinfo_nd6 llinfo_nd6
= {&llinfo_nd6
, &llinfo_nd6
};
105 size_t nd_ifinfo_indexlim
= 8;
106 struct nd_ifinfo
*nd_ifinfo
= NULL
;
107 struct nd_drhead nd_defrouter
;
108 struct nd_prhead nd_prefix
= { 0 };
110 int nd6_recalc_reachtm_interval
= ND6_RECALC_REACHTM_INTERVAL
;
111 static struct sockaddr_in6 all1_sa
;
113 static void nd6_slowtimo_funneled
__P((void *));
114 static int regen_tmpaddr
__P((struct in6_ifaddr
*));
120 static int nd6_init_done
= 0;
124 log(LOG_NOTICE
, "nd6_init called more than once(ignored)\n");
128 all1_sa
.sin6_family
= AF_INET6
;
129 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
130 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++)
131 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
133 /* initialization of the default router list */
134 TAILQ_INIT(&nd_defrouter
);
139 timeout(nd6_slowtimo_funneled
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
148 * We have some arrays that should be indexed by if_index.
149 * since if_index will grow dynamically, they should grow too.
151 if (nd_ifinfo
== NULL
|| if_index
>= nd_ifinfo_indexlim
) {
155 while (if_index
>= nd_ifinfo_indexlim
)
156 nd_ifinfo_indexlim
<<= 1;
159 n
= nd_ifinfo_indexlim
* sizeof(struct nd_ifinfo
);
160 q
= (caddr_t
)_MALLOC(n
, M_IP6NDP
, M_WAITOK
);
163 bcopy((caddr_t
)nd_ifinfo
, q
, n
/2);
164 FREE((caddr_t
)nd_ifinfo
, M_IP6NDP
);
166 nd_ifinfo
= (struct nd_ifinfo
*)q
;
169 #define ND nd_ifinfo[ifp->if_index]
172 * Don't initialize if called twice.
173 * XXX: to detect this, we should choose a member that is never set
174 * before initialization of the ND structure itself. We formaly used
175 * the linkmtu member, which was not suitable because it could be
176 * initialized via "ifconfig mtu".
178 if (ND
.basereachable
)
181 ND
.linkmtu
= ifindex2ifnet
[ifp
->if_index
]->if_mtu
;
182 ND
.chlim
= IPV6_DEFHLIM
;
183 ND
.basereachable
= REACHABLE_TIME
;
184 ND
.reachable
= ND_COMPUTE_RTIME(ND
.basereachable
);
185 ND
.retrans
= RETRANS_TIMER
;
187 ND
.flags
= ND6_IFF_PERFORMNUD
;
193 * Reset ND level link MTU. This function is called when the physical MTU
194 * changes, which means we might have to adjust the ND level MTU.
201 #define MIN(a,b) ((a) < (b) ? (a) : (b))
204 struct nd_ifinfo
*ndi
;
205 u_long oldmaxmtu
, oldlinkmtu
, dl_tag
;
208 * Make sure IPv6 is enabled for the interface first,
209 * because this can be called directly from SIOCSIFMTU for IPv4
212 if (ifp
->if_index
>= nd_ifinfo_indexlim
) {
213 if (dlil_find_dltag(ifp
->if_family
, ifp
->if_unit
, PF_INET6
, &dl_tag
) != EPROTONOSUPPORT
)
214 nd6log((LOG_INFO
, "setmtu for ifp=% but nd6 is not attached\n", if_name(ifp
)));
215 return; /* we're out of bound for nd_ifinfo */
218 ndi
= &nd_ifinfo
[ifp
->if_index
];
219 oldmaxmtu
= ndi
->maxmtu
;
220 oldlinkmtu
= ndi
->linkmtu
;
222 switch (ifp
->if_type
) {
223 case IFT_ARCNET
: /* XXX MTU handling needs more work */
224 ndi
->maxmtu
= MIN(60480, ifp
->if_mtu
);
227 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
230 ndi
->maxmtu
= MIN(FDDIIPMTU
, ifp
->if_mtu
);
233 ndi
->maxmtu
= MIN(ATMMTU
, ifp
->if_mtu
);
235 case IFT_IEEE1394
: /* XXX should be IEEE1394MTU(1500) */
236 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
239 case IFT_IEEE80211
: /* XXX should be IEEE80211MTU(1500) */
240 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
244 ndi
->maxmtu
= ifp
->if_mtu
;
248 if (oldmaxmtu
!= ndi
->maxmtu
) {
250 * If the ND level MTU is not set yet, or if the maxmtu
251 * is reset to a smaller value than the ND level MTU,
252 * also reset the ND level MTU.
254 if (ndi
->linkmtu
== 0 ||
255 ndi
->maxmtu
< ndi
->linkmtu
) {
256 ndi
->linkmtu
= ndi
->maxmtu
;
257 /* also adjust in6_maxmtu if necessary. */
258 if (oldlinkmtu
== 0) {
260 * XXX: the case analysis is grotty, but
261 * it is not efficient to call in6_setmaxmtu()
262 * here when we are during the initialization
265 if (in6_maxmtu
< ndi
->linkmtu
)
266 in6_maxmtu
= ndi
->linkmtu
;
275 nd6_option_init(opt
, icmp6len
, ndopts
)
278 union nd_opts
*ndopts
;
280 bzero(ndopts
, sizeof(*ndopts
));
281 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
283 = (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
286 ndopts
->nd_opts_done
= 1;
287 ndopts
->nd_opts_search
= NULL
;
292 * Take one ND option.
296 union nd_opts
*ndopts
;
298 struct nd_opt_hdr
*nd_opt
;
302 panic("ndopts == NULL in nd6_option\n");
303 if (!ndopts
->nd_opts_last
)
304 panic("uninitialized ndopts in nd6_option\n");
305 if (!ndopts
->nd_opts_search
)
307 if (ndopts
->nd_opts_done
)
310 nd_opt
= ndopts
->nd_opts_search
;
312 /* make sure nd_opt_len is inside the buffer */
313 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
314 bzero(ndopts
, sizeof(*ndopts
));
318 olen
= nd_opt
->nd_opt_len
<< 3;
321 * Message validation requires that all included
322 * options have a length that is greater than zero.
324 bzero(ndopts
, sizeof(*ndopts
));
328 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
329 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
330 /* option overruns the end of buffer, invalid */
331 bzero(ndopts
, sizeof(*ndopts
));
333 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
334 /* reached the end of options chain */
335 ndopts
->nd_opts_done
= 1;
336 ndopts
->nd_opts_search
= NULL
;
342 * Parse multiple ND options.
343 * This function is much easier to use, for ND routines that do not need
344 * multiple options of the same type.
348 union nd_opts
*ndopts
;
350 struct nd_opt_hdr
*nd_opt
;
354 panic("ndopts == NULL in nd6_options\n");
355 if (!ndopts
->nd_opts_last
)
356 panic("uninitialized ndopts in nd6_options\n");
357 if (!ndopts
->nd_opts_search
)
361 nd_opt
= nd6_option(ndopts
);
362 if (!nd_opt
&& !ndopts
->nd_opts_last
) {
364 * Message validation requires that all included
365 * options have a length that is greater than zero.
367 icmp6stat
.icp6s_nd_badopt
++;
368 bzero(ndopts
, sizeof(*ndopts
));
375 switch (nd_opt
->nd_opt_type
) {
376 case ND_OPT_SOURCE_LINKADDR
:
377 case ND_OPT_TARGET_LINKADDR
:
379 case ND_OPT_REDIRECTED_HEADER
:
380 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
382 "duplicated ND6 option found (type=%d)\n",
383 nd_opt
->nd_opt_type
));
386 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
390 case ND_OPT_PREFIX_INFORMATION
:
391 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
392 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
395 ndopts
->nd_opts_pi_end
=
396 (struct nd_opt_prefix_info
*)nd_opt
;
400 * Unknown options must be silently ignored,
401 * to accomodate future extension to the protocol.
404 "nd6_options: unsupported option %d - "
405 "option ignored\n", nd_opt
->nd_opt_type
));
410 if (i
> nd6_maxndopt
) {
411 icmp6stat
.icp6s_nd_toomanyopt
++;
412 nd6log((LOG_INFO
, "too many loop in nd opt\n"));
416 if (ndopts
->nd_opts_done
)
424 * ND6 timer routine to expire default route list and prefix list
427 nd6_timer_funneled(ignored_arg
)
431 boolean_t funnel_state
;
432 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
434 nd6_timer(ignored_arg
);
436 (void) thread_funnel_set(network_flock
, FALSE
);
440 nd6_timer(ignored_arg
)
444 struct llinfo_nd6
*ln
;
445 struct nd_defrouter
*dr
;
446 struct nd_prefix
*pr
;
448 struct in6_ifaddr
*ia6
, *nia6
;
449 struct in6_addrlifetime
*lt6
;
453 timeout(nd6_timer_funneled
, (caddr_t
)0, nd6_prune
* hz
);
455 ln
= llinfo_nd6
.ln_next
;
456 while (ln
&& ln
!= &llinfo_nd6
) {
458 struct sockaddr_in6
*dst
;
459 struct llinfo_nd6
*next
= ln
->ln_next
;
460 /* XXX: used for the DELAY case only: */
461 struct nd_ifinfo
*ndi
= NULL
;
463 if ((rt
= ln
->ln_rt
) == NULL
) {
467 if ((ifp
= rt
->rt_ifp
) == NULL
) {
471 ndi
= &nd_ifinfo
[ifp
->if_index
];
472 dst
= (struct sockaddr_in6
*)rt_key(rt
);
474 if (ln
->ln_expire
> time_second
) {
481 printf("rt=0 in nd6_timer(ln=%p)\n", ln
);
485 if (rt
->rt_llinfo
&& (struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
) {
486 printf("rt_llinfo(%p) is not equal to ln(%p)\n",
492 printf("dst=0 in nd6_timer(ln=%p)\n", ln
);
497 switch (ln
->ln_state
) {
498 case ND6_LLINFO_INCOMPLETE
:
499 if (ln
->ln_asked
< nd6_mmaxtries
) {
501 ln
->ln_expire
= time_second
+
502 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
503 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
,
506 struct mbuf
*m
= ln
->ln_hold
;
511 * Fake rcvif to make ICMP error
512 * more helpful in diagnosing
514 * XXX: should we consider
517 m
->m_pkthdr
.rcvif
= rt
->rt_ifp
;
519 icmp6_error(m
, ICMP6_DST_UNREACH
,
520 ICMP6_DST_UNREACH_ADDR
, 0);
526 case ND6_LLINFO_REACHABLE
:
528 ln
->ln_state
= ND6_LLINFO_STALE
;
529 ln
->ln_expire
= time_second
+ nd6_gctimer
;
533 case ND6_LLINFO_STALE
:
534 /* Garbage Collection(RFC 2461 5.3) */
539 case ND6_LLINFO_DELAY
:
540 if (ndi
&& (ndi
->flags
& ND6_IFF_PERFORMNUD
) != 0) {
543 ln
->ln_state
= ND6_LLINFO_PROBE
;
544 ln
->ln_expire
= time_second
+
546 nd6_ns_output(ifp
, &dst
->sin6_addr
,
550 ln
->ln_state
= ND6_LLINFO_STALE
; /* XXX */
551 ln
->ln_expire
= time_second
+ nd6_gctimer
;
554 case ND6_LLINFO_PROBE
:
555 if (ln
->ln_asked
< nd6_umaxtries
) {
557 ln
->ln_expire
= time_second
+
558 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
559 nd6_ns_output(ifp
, &dst
->sin6_addr
,
560 &dst
->sin6_addr
, ln
, 0);
569 /* expire default router list */
570 dr
= TAILQ_FIRST(&nd_defrouter
);
572 if (dr
->expire
&& dr
->expire
< time_second
) {
573 struct nd_defrouter
*t
;
574 t
= TAILQ_NEXT(dr
, dr_entry
);
578 dr
= TAILQ_NEXT(dr
, dr_entry
);
583 * expire interface addresses.
584 * in the past the loop was inside prefix expiry processing.
585 * However, from a stricter speci-confrmance standpoint, we should
586 * rather separate address lifetimes and prefix lifetimes.
589 for (ia6
= in6_ifaddr
; ia6
; ia6
= nia6
) {
591 /* check address lifetime */
592 lt6
= &ia6
->ia6_lifetime
;
593 if (IFA6_IS_INVALID(ia6
)) {
597 * If the expiring address is temporary, try
598 * regenerating a new one. This would be useful when
599 * we suspended a laptop PC, then turned it on after a
600 * period that could invalidate all temporary
601 * addresses. Although we may have to restart the
602 * loop (see below), it must be after purging the
603 * address. Otherwise, we'd see an infinite loop of
606 if (ip6_use_tempaddr
&&
607 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
608 if (regen_tmpaddr(ia6
) == 0)
612 in6_purgeaddr(&ia6
->ia_ifa
);
615 goto addrloop
; /* XXX: see below */
617 if (IFA6_IS_DEPRECATED(ia6
)) {
618 int oldflags
= ia6
->ia6_flags
;
620 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
623 * If a temporary address has just become deprecated,
624 * regenerate a new one if possible.
626 if (ip6_use_tempaddr
&&
627 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
628 (oldflags
& IN6_IFF_DEPRECATED
) == 0) {
630 if (regen_tmpaddr(ia6
) == 0) {
632 * A new temporary address is
634 * XXX: this means the address chain
635 * has changed while we are still in
636 * the loop. Although the change
637 * would not cause disaster (because
638 * it's not a deletion, but an
639 * addition,) we'd rather restart the
640 * loop just for safety. Or does this
641 * significantly reduce performance??
648 * A new RA might have made a deprecated address
651 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
655 /* expire prefix list */
656 pr
= nd_prefix
.lh_first
;
659 * check prefix lifetime.
660 * since pltime is just for autoconf, pltime processing for
661 * prefix is not necessary.
663 if (pr
->ndpr_expire
&& pr
->ndpr_expire
< time_second
) {
668 * address expiration and prefix expiration are
669 * separate. NEVER perform in6_purgeaddr here.
682 struct in6_ifaddr
*ia6
; /* deprecated/invalidated temporary address */
686 struct in6_ifaddr
*public_ifa6
= NULL
;
688 ifp
= ia6
->ia_ifa
.ifa_ifp
;
689 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
690 ifa
= ifa
->ifa_list
.tqe_next
)
692 struct in6_ifaddr
*it6
;
694 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
697 it6
= (struct in6_ifaddr
*)ifa
;
699 /* ignore no autoconf addresses. */
700 if ((it6
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
703 /* ignore autoconf addresses with different prefixes. */
704 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
)
708 * Now we are looking at an autoconf address with the same
709 * prefix as ours. If the address is temporary and is still
710 * preferred, do not create another one. It would be rare, but
711 * could happen, for example, when we resume a laptop PC after
714 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
715 !IFA6_IS_DEPRECATED(it6
)) {
721 * This is a public autoconf address that has the same prefix
722 * as ours. If it is preferred, keep it. We can't break the
723 * loop here, because there may be a still-preferred temporary
724 * address with the prefix.
726 if (!IFA6_IS_DEPRECATED(it6
))
730 if (public_ifa6
!= NULL
) {
733 if ((e
= in6_tmpifadd(public_ifa6
, 0)) != 0) {
734 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
735 " tmp addr,errno=%d\n", e
);
745 * Nuke neighbor cache/prefix/default router management table, right before
752 struct llinfo_nd6
*ln
, *nln
;
753 struct nd_defrouter
*dr
, *ndr
, drany
;
754 struct nd_prefix
*pr
, *npr
;
756 /* Nuke default router list entries toward ifp */
757 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
759 * The first entry of the list may be stored in
760 * the routing table, so we'll delete it later.
762 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= ndr
) {
763 ndr
= TAILQ_NEXT(dr
, dr_entry
);
767 dr
= TAILQ_FIRST(&nd_defrouter
);
772 /* Nuke prefix list entries toward ifp */
773 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
775 if (pr
->ndpr_ifp
== ifp
) {
777 * Previously, pr->ndpr_addr is removed as well,
778 * but I strongly believe we don't have to do it.
779 * nd6_purge() is only called from in6_ifdetach(),
780 * which removes all the associated interface addresses
782 * (jinmei@kame.net 20010129)
788 /* cancel default outgoing interface setting */
789 if (nd6_defifindex
== ifp
->if_index
)
790 nd6_setdefaultiface(0);
792 if (!ip6_forwarding
&& (ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
793 /* refresh default router list */
794 bzero(&drany
, sizeof(drany
));
795 defrouter_delreq(&drany
, 0);
800 * Nuke neighbor cache entries for the ifp.
801 * Note that rt->rt_ifp may not be the same as ifp,
802 * due to KAME goto ours hack. See RTM_RESOLVE case in
803 * nd6_rtrequest(), and ip6_input().
805 ln
= llinfo_nd6
.ln_next
;
806 while (ln
&& ln
!= &llinfo_nd6
) {
808 struct sockaddr_dl
*sdl
;
812 if (rt
&& rt
->rt_gateway
&&
813 rt
->rt_gateway
->sa_family
== AF_LINK
) {
814 sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
;
815 if (sdl
->sdl_index
== ifp
->if_index
)
823 nd6_lookup(addr6
, create
, ifp
)
824 struct in6_addr
*addr6
;
829 struct sockaddr_in6 sin6
;
831 bzero(&sin6
, sizeof(sin6
));
832 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
833 sin6
.sin6_family
= AF_INET6
;
834 sin6
.sin6_addr
= *addr6
;
836 sin6
.sin6_scope_id
= in6_addr2scopeid(ifp
, addr6
);
838 rt
= rtalloc1((struct sockaddr
*)&sin6
, create
, 0UL);
839 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) == 0) {
841 * This is the case for the default route.
842 * If we want to create a neighbor cache for the address, we
843 * should free the route for the destination and allocate an
856 * If no route is available and create is set,
857 * we allocate a host route for the destination
858 * and treat it like an interface route.
859 * This hack is necessary for a neighbor which can't
860 * be covered by our own prefix.
863 ifaof_ifpforaddr((struct sockaddr
*)&sin6
, ifp
);
868 * Create a new route. RTF_LLINFO is necessary
869 * to create a Neighbor Cache entry for the
870 * destination in nd6_rtrequest which will be
871 * called in rtrequest via ifa->ifa_rtrequest.
873 if ((e
= rtrequest(RTM_ADD
, (struct sockaddr
*)&sin6
,
875 (struct sockaddr
*)&all1_sa
,
877 RTF_HOST
| RTF_LLINFO
) &
881 "nd6_lookup: failed to add route for a "
882 "neighbor(%s), errno=%d\n",
883 ip6_sprintf(addr6
), e
);
887 struct llinfo_nd6
*ln
=
888 (struct llinfo_nd6
*)rt
->rt_llinfo
;
889 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
896 * Validation for the entry.
897 * Note that the check for rt_llinfo is necessary because a cloned
898 * route from a parent route that has the L flag (e.g. the default
899 * route to a p2p interface) may have the flag, too, while the
900 * destination is not actually a neighbor.
901 * XXX: we can't use rt->rt_ifp to check for the interface, since
902 * it might be the loopback interface if the entry is for our
903 * own address on a non-loopback interface. Instead, we should
904 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
907 if ((ifp
->if_type
!=IFT_PPP
) && ((rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
908 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
910 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
))) {
912 log(LOG_DEBUG
, "nd6_lookup: failed to lookup %s (if = %s)\n",
913 ip6_sprintf(addr6
), ifp
? if_name(ifp
) : "unspec");
914 /* xxx more logs... kazu */
922 * Detect if a given IPv6 address identifies a neighbor on a given link.
923 * XXX: should take care of the destination of a p2p link?
926 nd6_is_addr_neighbor(addr
, ifp
)
927 struct sockaddr_in6
*addr
;
933 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
934 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
937 * A link-local address is always a neighbor.
938 * XXX: we should use the sin6_scope_id field rather than the embedded
941 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
) &&
942 ntohs(*(u_int16_t
*)&addr
->sin6_addr
.s6_addr
[2]) == ifp
->if_index
)
946 * If the address matches one of our addresses,
947 * it should be a neighbor.
949 for (ifa
= ifp
->if_addrlist
.tqh_first
;
951 ifa
= ifa
->ifa_list
.tqe_next
)
953 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
956 for (i
= 0; i
< 4; i
++) {
957 if ((IFADDR6(ifa
).s6_addr32
[i
] ^
958 addr
->sin6_addr
.s6_addr32
[i
]) &
959 IFMASK6(ifa
).s6_addr32
[i
])
966 * Even if the address matches none of our addresses, it might be
967 * in the neighbor cache.
969 if (nd6_lookup(&addr
->sin6_addr
, 0, ifp
) != NULL
)
978 * Free an nd6 llinfo entry.
984 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
, *next
;
985 struct in6_addr in6
= ((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
986 struct nd_defrouter
*dr
;
989 * we used to have pfctlinput(PRC_HOSTDEAD) here.
990 * even though it is not harmful, it was not really necessary.
993 if (!ip6_forwarding
&& (ip6_accept_rtadv
|| (rt
->rt_ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
996 dr
= defrouter_lookup(&((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
,
999 if (ln
&& ln
->ln_router
|| dr
) {
1001 * rt6_flush must be called whether or not the neighbor
1002 * is in the Default Router List.
1003 * See a corresponding comment in nd6_na_input().
1005 rt6_flush(&in6
, rt
->rt_ifp
);
1010 * Unreachablity of a router might affect the default
1011 * router selection and on-link detection of advertised
1016 * Temporarily fake the state to choose a new default
1017 * router and to perform on-link determination of
1018 * prefixes correctly.
1019 * Below the state will be set correctly,
1020 * or the entry itself will be deleted.
1022 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1025 * Since defrouter_select() does not affect the
1026 * on-link determination and MIP6 needs the check
1027 * before the default router selection, we perform
1030 pfxlist_onlink_check();
1032 if (dr
== TAILQ_FIRST(&nd_defrouter
)) {
1034 * It is used as the current default router,
1035 * so we have to move it to the end of the
1036 * list and choose a new one.
1037 * XXX: it is not very efficient if this is
1040 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1041 TAILQ_INSERT_TAIL(&nd_defrouter
, dr
, dr_entry
);
1050 * Before deleting the entry, remember the next entry as the
1051 * return value. We need this because pfxlist_onlink_check() above
1052 * might have freed other entries (particularly the old next entry) as
1053 * a side effect (XXX).
1061 * Detach the route from the routing tree and the list of neighbor
1062 * caches, and disable the route entry not to be used in already
1065 rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
1066 rt_mask(rt
), 0, (struct rtentry
**)0);
1072 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1074 * XXX cost-effective metods?
1077 nd6_nud_hint(rt
, dst6
, force
)
1079 struct in6_addr
*dst6
;
1082 struct llinfo_nd6
*ln
;
1085 * If the caller specified "rt", use that. Otherwise, resolve the
1086 * routing table by supplied "dst6".
1091 if (!(rt
= nd6_lookup(dst6
, 0, NULL
)))
1095 if ((rt
->rt_flags
& RTF_GATEWAY
) != 0 ||
1096 (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1097 !rt
->rt_llinfo
|| !rt
->rt_gateway
||
1098 rt
->rt_gateway
->sa_family
!= AF_LINK
) {
1099 /* This is not a host route. */
1103 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1104 if (ln
->ln_state
< ND6_LLINFO_REACHABLE
)
1108 * if we get upper-layer reachability confirmation many times,
1109 * it is possible we have false information.
1113 if (ln
->ln_byhint
> nd6_maxnudhint
)
1117 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1119 ln
->ln_expire
= time_second
+
1120 nd_ifinfo
[rt
->rt_ifp
->if_index
].reachable
;
1124 nd6_rtrequest(req
, rt
, sa
)
1127 struct sockaddr
*sa
; /* xxx unused */
1129 struct sockaddr
*gate
= rt
->rt_gateway
;
1130 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1131 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
};
1132 struct ifnet
*ifp
= rt
->rt_ifp
;
1135 if ((rt
->rt_flags
& RTF_GATEWAY
))
1138 if (nd6_need_cache(ifp
) == 0 && (rt
->rt_flags
& RTF_HOST
) == 0) {
1140 * This is probably an interface direct route for a link
1141 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1142 * We do not need special treatment below for such a route.
1143 * Moreover, the RTF_LLINFO flag which would be set below
1144 * would annoy the ndp(8) command.
1149 if (req
== RTM_RESOLVE
&&
1150 (nd6_need_cache(ifp
) == 0 || /* stf case */
1151 !nd6_is_addr_neighbor((struct sockaddr_in6
*)rt_key(rt
), ifp
))) {
1153 * FreeBSD and BSD/OS often make a cloned host route based
1154 * on a less-specific route (e.g. the default route).
1155 * If the less specific route does not have a "gateway"
1156 * (this is the case when the route just goes to a p2p or an
1157 * stf interface), we'll mistakenly make a neighbor cache for
1158 * the host route, and will see strange neighbor solicitation
1159 * for the corresponding destination. In order to avoid the
1160 * confusion, we check if the destination of the route is
1161 * a neighbor in terms of neighbor discovery, and stop the
1162 * process if not. Additionally, we remove the LLINFO flag
1163 * so that ndp(8) will not try to get the neighbor information
1164 * of the destination.
1166 rt
->rt_flags
&= ~RTF_LLINFO
;
1173 * There is no backward compatibility :)
1175 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1176 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1177 * rt->rt_flags |= RTF_CLONING;
1179 if (rt
->rt_flags
& (RTF_CLONING
| RTF_LLINFO
)) {
1181 * Case 1: This route should come from
1182 * a route to interface. RTF_LLINFO flag is set
1183 * for a host route whose destination should be
1184 * treated as on-link.
1186 rt_setgate(rt
, rt_key(rt
),
1187 (struct sockaddr
*)&null_sdl
);
1188 gate
= rt
->rt_gateway
;
1189 SDL(gate
)->sdl_type
= ifp
->if_type
;
1190 SDL(gate
)->sdl_index
= ifp
->if_index
;
1192 ln
->ln_expire
= time_second
;
1194 if (ln
&& ln
->ln_expire
== 0) {
1195 /* kludge for desktops */
1197 printf("nd6_rtequest: time.tv_sec is zero; "
1203 if ((rt
->rt_flags
& RTF_CLONING
))
1207 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1208 * We don't do that here since llinfo is not ready yet.
1210 * There are also couple of other things to be discussed:
1211 * - unsolicited NA code needs improvement beforehand
1212 * - RFC2461 says we MAY send multicast unsolicited NA
1213 * (7.2.6 paragraph 4), however, it also says that we
1214 * SHOULD provide a mechanism to prevent multicast NA storm.
1215 * we don't have anything like it right now.
1216 * note that the mechanism needs a mutual agreement
1217 * between proxies, which means that we need to implement
1218 * a new protocol, or a new kludge.
1219 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1220 * we need to check ip6forwarding before sending it.
1221 * (or should we allow proxy ND configuration only for
1222 * routers? there's no mention about proxy ND from hosts)
1225 /* XXX it does not work */
1226 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1228 &SIN6(rt_key(rt
))->sin6_addr
,
1229 &SIN6(rt_key(rt
))->sin6_addr
,
1230 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1235 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) == 0) {
1237 * Address resolution isn't necessary for a point to
1238 * point link, so we can skip this test for a p2p link.
1240 if (gate
->sa_family
!= AF_LINK
||
1241 gate
->sa_len
< sizeof(null_sdl
)) {
1243 "nd6_rtrequest: bad gateway value: %s\n",
1247 SDL(gate
)->sdl_type
= ifp
->if_type
;
1248 SDL(gate
)->sdl_index
= ifp
->if_index
;
1251 break; /* This happens on a route change */
1253 * Case 2: This route may come from cloning, or a manual route
1254 * add with a LL address.
1256 R_Malloc(ln
, struct llinfo_nd6
*, sizeof(*ln
));
1257 rt
->rt_llinfo
= (caddr_t
)ln
;
1259 log(LOG_DEBUG
, "nd6_rtrequest: malloc failed\n");
1264 Bzero(ln
, sizeof(*ln
));
1266 /* this is required for "ndp" command. - shin */
1267 if (req
== RTM_ADD
) {
1269 * gate should have some valid AF_LINK entry,
1270 * and ln->ln_expire should have some lifetime
1271 * which is specified by ndp command.
1273 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1277 * When req == RTM_RESOLVE, rt is created and
1278 * initialized in rtrequest(), so rt_expire is 0.
1280 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1281 ln
->ln_expire
= time_second
;
1283 rt
->rt_flags
|= RTF_LLINFO
;
1284 ln
->ln_next
= llinfo_nd6
.ln_next
;
1285 llinfo_nd6
.ln_next
= ln
;
1286 ln
->ln_prev
= &llinfo_nd6
;
1287 ln
->ln_next
->ln_prev
= ln
;
1290 * check if rt_key(rt) is one of my address assigned
1293 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1294 &SIN6(rt_key(rt
))->sin6_addr
);
1296 caddr_t macp
= nd6_ifptomac(ifp
);
1298 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1301 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
1302 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
1304 if (nd6_useloopback
) {
1305 rt
->rt_ifp
= &loif
[0]; /* XXX */
1307 * Make sure rt_ifa be equal to the ifaddr
1308 * corresponding to the address.
1309 * We need this because when we refer
1310 * rt_ifa->ia6_flags in ip6_input, we assume
1311 * that the rt_ifa points to the address instead
1312 * of the loopback address.
1314 if (ifa
!= rt
->rt_ifa
) {
1318 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
1320 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1323 /* join solicited node multicast for proxy ND */
1324 if (ifp
->if_flags
& IFF_MULTICAST
) {
1325 struct in6_addr llsol
;
1328 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1329 llsol
.s6_addr16
[0] = htons(0xff02);
1330 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1331 llsol
.s6_addr32
[1] = 0;
1332 llsol
.s6_addr32
[2] = htonl(1);
1333 llsol
.s6_addr8
[12] = 0xff;
1335 if (!in6_addmulti(&llsol
, ifp
, &error
)) {
1336 nd6log((LOG_ERR
, "%s: failed to join "
1337 "%s (errno=%d)\n", if_name(ifp
),
1338 ip6_sprintf(&llsol
), error
));
1347 /* leave from solicited node multicast for proxy ND */
1348 if ((rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
1349 (ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1350 struct in6_addr llsol
;
1351 struct in6_multi
*in6m
;
1353 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1354 llsol
.s6_addr16
[0] = htons(0xff02);
1355 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1356 llsol
.s6_addr32
[1] = 0;
1357 llsol
.s6_addr32
[2] = htonl(1);
1358 llsol
.s6_addr8
[12] = 0xff;
1360 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1365 ln
->ln_next
->ln_prev
= ln
->ln_prev
;
1366 ln
->ln_prev
->ln_next
= ln
->ln_next
;
1369 rt
->rt_flags
&= ~RTF_LLINFO
;
1371 m_freem(ln
->ln_hold
);
1378 nd6_ioctl(cmd
, data
, ifp
)
1383 struct in6_drlist
*drl
= (struct in6_drlist
*)data
;
1384 struct in6_prlist
*prl
= (struct in6_prlist
*)data
;
1385 struct in6_ndireq
*ndi
= (struct in6_ndireq
*)data
;
1386 struct in6_nbrinfo
*nbi
= (struct in6_nbrinfo
*)data
;
1387 struct in6_ndifreq
*ndif
= (struct in6_ndifreq
*)data
;
1388 struct nd_defrouter
*dr
, any
;
1389 struct nd_prefix
*pr
;
1391 int i
= 0, error
= 0;
1395 case SIOCGDRLST_IN6
:
1397 * obsolete API, use sysctl under net.inet6.icmp6
1399 bzero(drl
, sizeof(*drl
));
1401 dr
= TAILQ_FIRST(&nd_defrouter
);
1402 while (dr
&& i
< DRLSTSIZ
) {
1403 drl
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1404 if (IN6_IS_ADDR_LINKLOCAL(&drl
->defrouter
[i
].rtaddr
)) {
1405 /* XXX: need to this hack for KAME stack */
1406 drl
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1409 "default router list contains a "
1410 "non-linklocal address(%s)\n",
1411 ip6_sprintf(&drl
->defrouter
[i
].rtaddr
));
1413 drl
->defrouter
[i
].flags
= dr
->flags
;
1414 drl
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1415 drl
->defrouter
[i
].expire
= dr
->expire
;
1416 drl
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1418 dr
= TAILQ_NEXT(dr
, dr_entry
);
1422 case SIOCGPRLST_IN6
:
1424 * obsolete API, use sysctl under net.inet6.icmp6
1427 * XXX meaning of fields, especialy "raflags", is very
1428 * differnet between RA prefix list and RR/static prefix list.
1429 * how about separating ioctls into two?
1431 bzero(prl
, sizeof(*prl
));
1433 pr
= nd_prefix
.lh_first
;
1434 while (pr
&& i
< PRLSTSIZ
) {
1435 struct nd_pfxrouter
*pfr
;
1438 (void)in6_embedscope(&prl
->prefix
[i
].prefix
,
1439 &pr
->ndpr_prefix
, NULL
, NULL
);
1440 prl
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1441 prl
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1442 prl
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1443 prl
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1444 prl
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1445 prl
->prefix
[i
].expire
= pr
->ndpr_expire
;
1447 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1451 #define RTRADDR prl->prefix[i].advrtr[j]
1452 RTRADDR
= pfr
->router
->rtaddr
;
1453 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1454 /* XXX: hack for KAME */
1455 RTRADDR
.s6_addr16
[1] = 0;
1458 "a router(%s) advertises "
1460 "non-link local address\n",
1461 ip6_sprintf(&RTRADDR
));
1465 pfr
= pfr
->pfr_next
;
1467 prl
->prefix
[i
].advrtrs
= j
;
1468 prl
->prefix
[i
].origin
= PR_ORIG_RA
;
1474 struct rr_prefix
*rpp
;
1476 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1477 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1480 (void)in6_embedscope(&prl
->prefix
[i
].prefix
,
1481 &pr
->ndpr_prefix
, NULL
, NULL
);
1482 prl
->prefix
[i
].raflags
= rpp
->rp_raf
;
1483 prl
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1484 prl
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1485 prl
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1486 prl
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1487 prl
->prefix
[i
].expire
= rpp
->rp_expire
;
1488 prl
->prefix
[i
].advrtrs
= 0;
1489 prl
->prefix
[i
].origin
= rpp
->rp_origin
;
1496 case OSIOCGIFINFO_IN6
:
1497 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1501 ndi
->ndi
.linkmtu
= nd_ifinfo
[ifp
->if_index
].linkmtu
;
1502 ndi
->ndi
.maxmtu
= nd_ifinfo
[ifp
->if_index
].maxmtu
;
1503 ndi
->ndi
.basereachable
=
1504 nd_ifinfo
[ifp
->if_index
].basereachable
;
1505 ndi
->ndi
.reachable
= nd_ifinfo
[ifp
->if_index
].reachable
;
1506 ndi
->ndi
.retrans
= nd_ifinfo
[ifp
->if_index
].retrans
;
1507 ndi
->ndi
.flags
= nd_ifinfo
[ifp
->if_index
].flags
;
1508 ndi
->ndi
.recalctm
= nd_ifinfo
[ifp
->if_index
].recalctm
;
1509 ndi
->ndi
.chlim
= nd_ifinfo
[ifp
->if_index
].chlim
;
1510 ndi
->ndi
.receivedra
= nd_ifinfo
[ifp
->if_index
].receivedra
;
1512 case SIOCGIFINFO_IN6
:
1513 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1517 ndi
->ndi
= nd_ifinfo
[ifp
->if_index
];
1519 case SIOCSIFINFO_FLAGS
:
1520 /* XXX: almost all other fields of ndi->ndi is unused */
1521 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
) {
1525 nd_ifinfo
[ifp
->if_index
].flags
= ndi
->ndi
.flags
;
1527 case SIOCSNDFLUSH_IN6
: /* XXX: the ioctl name is confusing... */
1528 /* flush default router list */
1530 * xxx sumikawa: should not delete route if default
1531 * route equals to the top of default router list
1533 bzero(&any
, sizeof(any
));
1534 defrouter_delreq(&any
, 0);
1536 /* xxx sumikawa: flush prefix list */
1538 case SIOCSPFXFLUSH_IN6
:
1540 /* flush all the prefix advertised by routers */
1541 struct nd_prefix
*pr
, *next
;
1544 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
1545 struct in6_ifaddr
*ia
, *ia_next
;
1547 next
= pr
->ndpr_next
;
1549 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
))
1552 /* do we really have to remove addresses as well? */
1553 for (ia
= in6_ifaddr
; ia
; ia
= ia_next
) {
1554 /* ia might be removed. keep the next ptr. */
1555 ia_next
= ia
->ia_next
;
1557 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
1560 if (ia
->ia6_ndpr
== pr
)
1561 in6_purgeaddr(&ia
->ia_ifa
);
1568 case SIOCSRTRFLUSH_IN6
:
1570 /* flush all the default routers */
1571 struct nd_defrouter
*dr
, *next
;
1574 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
1576 * The first entry of the list may be stored in
1577 * the routing table, so we'll delete it later.
1579 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
1580 next
= TAILQ_NEXT(dr
, dr_entry
);
1583 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
));
1588 case SIOCGNBRINFO_IN6
:
1590 struct llinfo_nd6
*ln
;
1591 struct in6_addr nb_addr
= nbi
->addr
; /* make local for safety */
1594 * XXX: KAME specific hack for scoped addresses
1595 * XXXX: for other scopes than link-local?
1597 if (IN6_IS_ADDR_LINKLOCAL(&nbi
->addr
) ||
1598 IN6_IS_ADDR_MC_LINKLOCAL(&nbi
->addr
)) {
1599 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
1602 *idp
= htons(ifp
->if_index
);
1606 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
)) == NULL
) {
1611 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1612 nbi
->state
= ln
->ln_state
;
1613 nbi
->asked
= ln
->ln_asked
;
1614 nbi
->isrouter
= ln
->ln_router
;
1615 nbi
->expire
= ln
->ln_expire
;
1620 case SIOCGDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1621 ndif
->ifindex
= nd6_defifindex
;
1623 case SIOCSDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1624 return(nd6_setdefaultiface(ndif
->ifindex
));
1631 * Create neighbor cache entry and cache link-layer address,
1632 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1635 nd6_cache_lladdr(ifp
, from
, lladdr
, lladdrlen
, type
, code
)
1637 struct in6_addr
*from
;
1640 int type
; /* ICMP6 type */
1641 int code
; /* type dependent information */
1643 struct rtentry
*rt
= NULL
;
1644 struct llinfo_nd6
*ln
= NULL
;
1646 struct sockaddr_dl
*sdl
= NULL
;
1653 panic("ifp == NULL in nd6_cache_lladdr");
1655 panic("from == NULL in nd6_cache_lladdr");
1657 /* nothing must be updated for unspecified address */
1658 if (IN6_IS_ADDR_UNSPECIFIED(from
))
1662 * Validation about ifp->if_addrlen and lladdrlen must be done in
1665 * XXX If the link does not have link-layer adderss, what should
1666 * we do? (ifp->if_addrlen == 0)
1667 * Spec says nothing in sections for RA, RS and NA. There's small
1668 * description on it in NS section (RFC 2461 7.2.3).
1671 rt
= nd6_lookup(from
, 0, ifp
);
1674 /* nothing must be done if there's no lladdr */
1675 if (!lladdr
|| !lladdrlen
)
1679 rt
= nd6_lookup(from
, 1, ifp
);
1682 /* do nothing if static ndp is set */
1683 if (rt
->rt_flags
& RTF_STATIC
)
1690 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
1695 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1698 if (!rt
->rt_gateway
)
1700 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
1702 sdl
= SDL(rt
->rt_gateway
);
1704 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
1705 if (olladdr
&& lladdr
) {
1706 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
1714 * newentry olladdr lladdr llchange (*=record)
1717 * 0 n y -- (3) * STALE
1719 * 0 y y y (5) * STALE
1720 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1721 * 1 -- y -- (7) * STALE
1724 if (lladdr
) { /* (3-5) and (7) */
1726 * Record source link-layer address
1727 * XXX is it dependent to ifp->if_type?
1729 sdl
->sdl_alen
= ifp
->if_addrlen
;
1730 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
1734 if ((!olladdr
&& lladdr
) /* (3) */
1735 || (olladdr
&& lladdr
&& llchange
)) { /* (5) */
1737 newstate
= ND6_LLINFO_STALE
;
1738 } else /* (1-2,4) */
1742 if (!lladdr
) /* (6) */
1743 newstate
= ND6_LLINFO_NOSTATE
;
1745 newstate
= ND6_LLINFO_STALE
;
1750 * Update the state of the neighbor cache.
1752 ln
->ln_state
= newstate
;
1754 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1756 * XXX: since nd6_output() below will cause
1757 * state tansition to DELAY and reset the timer,
1758 * we must set the timer now, although it is actually
1761 ln
->ln_expire
= time_second
+ nd6_gctimer
;
1765 * we assume ifp is not a p2p here, so just
1766 * set the 2nd argument as the 1st one.
1768 nd6_output(ifp
, ifp
, ln
->ln_hold
,
1769 (struct sockaddr_in6
*)rt_key(rt
),
1773 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
1774 /* probe right away */
1775 ln
->ln_expire
= time_second
;
1780 * ICMP6 type dependent behavior.
1782 * NS: clear IsRouter if new entry
1783 * RS: clear IsRouter
1784 * RA: set IsRouter if there's lladdr
1785 * redir: clear IsRouter if new entry
1788 * The spec says that we must set IsRouter in the following cases:
1789 * - If lladdr exist, set IsRouter. This means (1-5).
1790 * - If it is old entry (!newentry), set IsRouter. This means (7).
1791 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1792 * A quetion arises for (1) case. (1) case has no lladdr in the
1793 * neighbor cache, this is similar to (6).
1794 * This case is rare but we figured that we MUST NOT set IsRouter.
1796 * newentry olladdr lladdr llchange NS RS RA redir
1798 * 0 n n -- (1) c ? s
1799 * 0 y n -- (2) c s s
1800 * 0 n y -- (3) c s s
1803 * 1 -- n -- (6) c c c s
1804 * 1 -- y -- (7) c c s c s
1808 switch (type
& 0xff) {
1809 case ND_NEIGHBOR_SOLICIT
:
1811 * New entry must have is_router flag cleared.
1813 if (is_newentry
) /* (6-7) */
1818 * If the icmp is a redirect to a better router, always set the
1819 * is_router flag. Otherwise, if the entry is newly created,
1820 * clear the flag. [RFC 2461, sec 8.3]
1822 if (code
== ND_REDIRECT_ROUTER
)
1824 else if (is_newentry
) /* (6-7) */
1827 case ND_ROUTER_SOLICIT
:
1829 * is_router flag must always be cleared.
1833 case ND_ROUTER_ADVERT
:
1835 * Mark an entry with lladdr as a router.
1837 if ((!is_newentry
&& (olladdr
|| lladdr
)) /* (2-5) */
1838 || (is_newentry
&& lladdr
)) { /* (7) */
1845 * When the link-layer address of a router changes, select the
1846 * best router again. In particular, when the neighbor entry is newly
1847 * created, it might affect the selection policy.
1848 * Question: can we restrict the first condition to the "is_newentry"
1850 * XXX: when we hear an RA from a new router with the link-layer
1851 * address option, defrouter_select() is called twice, since
1852 * defrtrlist_update called the function as well. However, I believe
1853 * we can compromise the overhead, since it only happens the first
1855 * XXX: although defrouter_select() should not have a bad effect
1856 * for those are not autoconfigured hosts, we explicitly avoid such
1859 if (do_update
&& ln
->ln_router
&& !ip6_forwarding
&& (ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
)))
1866 nd6_slowtimo(ignored_arg
)
1871 struct nd_ifinfo
*nd6if
;
1874 timeout(nd6_slowtimo_funneled
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
1875 for (i
= 1; i
< if_index
+ 1; i
++) {
1876 if (!nd_ifinfo
|| i
>= nd_ifinfo_indexlim
)
1878 nd6if
= &nd_ifinfo
[i
];
1879 if (nd6if
->basereachable
&& /* already initialized */
1880 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
1882 * Since reachable time rarely changes by router
1883 * advertisements, we SHOULD insure that a new random
1884 * value gets recomputed at least once every few hours.
1887 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
1888 nd6if
->reachable
= ND_COMPUTE_RTIME(nd6if
->basereachable
);
1895 nd6_slowtimo_funneled(ignored_arg
)
1899 boolean_t funnel_state
;
1900 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
1902 nd6_slowtimo(ignored_arg
);
1904 (void) thread_funnel_set(network_flock
, FALSE
);
1908 #define senderr(e) { error = (e); goto bad;}
1910 nd6_output(ifp
, origifp
, m0
, dst
, rt0
)
1912 struct ifnet
*origifp
;
1914 struct sockaddr_in6
*dst
;
1915 struct rtentry
*rt0
;
1917 struct mbuf
*m
= m0
;
1918 struct rtentry
*rt
= rt0
;
1919 struct sockaddr_in6
*gw6
= NULL
;
1920 struct llinfo_nd6
*ln
= NULL
;
1923 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
))
1926 if (nd6_need_cache(ifp
) == 0)
1930 * next hop determination. This routine is derived from ether_outpout.
1933 if ((rt
->rt_flags
& RTF_UP
) == 0) {
1934 if ((rt0
= rt
= rtalloc1((struct sockaddr
*)dst
, 1, 0UL)) !=
1938 if (rt
->rt_ifp
!= ifp
) {
1939 /* XXX: loop care? */
1940 return nd6_output(ifp
, origifp
, m0
,
1944 senderr(EHOSTUNREACH
);
1947 if (rt
->rt_flags
& RTF_GATEWAY
) {
1948 gw6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
1951 * We skip link-layer address resolution and NUD
1952 * if the gateway is not a neighbor from ND point
1953 * of view, regardless of the value of nd_ifinfo.flags.
1954 * The second condition is a bit tricky; we skip
1955 * if the gateway is our own address, which is
1956 * sometimes used to install a route to a p2p link.
1958 if (!nd6_is_addr_neighbor(gw6
, ifp
) ||
1959 in6ifa_ifpwithaddr(ifp
, &gw6
->sin6_addr
)) {
1961 * We allow this kind of tricky route only
1962 * when the outgoing interface is p2p.
1963 * XXX: we may need a more generic rule here.
1965 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
1966 senderr(EHOSTUNREACH
);
1971 if (rt
->rt_gwroute
== 0)
1973 if (((rt
= rt
->rt_gwroute
)->rt_flags
& RTF_UP
) == 0) {
1974 rtfree(rt
); rt
= rt0
;
1975 lookup
: rt
->rt_gwroute
= rtalloc1(rt
->rt_gateway
, 1, 0UL);
1976 if ((rt
= rt
->rt_gwroute
) == 0)
1977 senderr(EHOSTUNREACH
);
1983 * Address resolution or Neighbor Unreachability Detection
1985 * At this point, the destination of the packet must be a unicast
1986 * or an anycast address(i.e. not a multicast).
1989 /* Look up the neighbor cache for the nexthop */
1990 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0)
1991 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1994 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1995 * the condition below is not very efficient. But we believe
1996 * it is tolerable, because this should be a rare case.
1998 if (nd6_is_addr_neighbor(dst
, ifp
) &&
1999 (rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
)) != NULL
)
2000 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
2003 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0 &&
2004 !(nd_ifinfo
[ifp
->if_index
].flags
& ND6_IFF_PERFORMNUD
)) {
2006 "nd6_output: can't allocate llinfo for %s "
2008 ip6_sprintf(&dst
->sin6_addr
), ln
, rt
);
2009 senderr(EIO
); /* XXX: good error? */
2012 goto sendpkt
; /* send anyway */
2015 /* We don't have to do link-layer address resolution on a p2p link. */
2016 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
2017 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
2018 ln
->ln_state
= ND6_LLINFO_STALE
;
2019 ln
->ln_expire
= time_second
+ nd6_gctimer
;
2023 * The first time we send a packet to a neighbor whose entry is
2024 * STALE, we have to change the state to DELAY and a sets a timer to
2025 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2026 * neighbor unreachability detection on expiration.
2029 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
2031 ln
->ln_state
= ND6_LLINFO_DELAY
;
2032 ln
->ln_expire
= time_second
+ nd6_delay
;
2036 * If the neighbor cache entry has a state other than INCOMPLETE
2037 * (i.e. its link-layer address is already resolved), just
2040 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
)
2044 * There is a neighbor cache entry, but no ethernet address
2045 * response yet. Replace the held mbuf (if any) with this
2048 * This code conforms to the rate-limiting rule described in Section
2049 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
2052 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
)
2053 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
2055 m_freem(ln
->ln_hold
);
2057 if (ln
->ln_expire
) {
2058 if (ln
->ln_asked
< nd6_mmaxtries
&&
2059 ln
->ln_expire
< time_second
) {
2061 ln
->ln_expire
= time_second
+
2062 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
2063 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0);
2071 /* Make sure the HW checksum flags are cleaned before sending the packet */
2073 m
->m_pkthdr
.csum_data
= 0;
2074 m
->m_pkthdr
.csum_flags
= 0;
2076 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
2077 m
->m_pkthdr
.rcvif
= origifp
; /* forwarding rules require the original scope_id */
2078 return (dlil_output(ifptodlt(origifp
, PF_INET6
), m
, (caddr_t
)rt
, (struct sockaddr
*)dst
,0));
2080 /* Do not allow loopback address to wind up on a wire */
2081 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2083 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
2084 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
))) {
2085 ip6stat
.ip6s_badscope
++;
2087 * Simply drop the packet just like a firewall -- we do not want the
2088 * the application to feel the pain, not yet...
2089 * Returning ENETUNREACH like ip6_output does in some similar cases
2090 * could startle the otherwise clueless process that specifies
2091 * loopback as the source address.
2097 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
2098 return (dlil_output(ifptodlt(ifp
, PF_INET6
), m
, (caddr_t
)rt
, (struct sockaddr
*)dst
, 0));
2100 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
2101 return((*ifp
->if_output
)(origifp
, m
, (struct sockaddr
*)dst
,
2104 return((*ifp
->if_output
)(ifp
, m
, (struct sockaddr
*)dst
, rt
));
2119 * XXX: we currently do not make neighbor cache on any interface
2120 * other than ARCnet, Ethernet, FDDI and GIF.
2123 * - unidirectional tunnels needs no ND
2125 switch (ifp
->if_type
) {
2136 case IFT_GIF
: /* XXX need more cases? */
2144 nd6_storelladdr(ifp
, rt
, m
, dst
, desten
)
2148 struct sockaddr
*dst
;
2152 struct sockaddr_dl
*sdl
;
2154 if (m
->m_flags
& M_MCAST
) {
2155 switch (ifp
->if_type
) {
2164 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
2168 for (i
= 0; i
< ifp
->if_addrlen
; i
++)
2175 return(0); /* caller will free mbuf */
2180 /* this could happen, if we could not allocate memory */
2181 return(0); /* caller will free mbuf */
2183 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
2184 printf("nd6_storelladdr: something odd happens\n");
2185 return(0); /* caller will free mbuf */
2187 sdl
= SDL(rt
->rt_gateway
);
2188 if (sdl
->sdl_alen
== 0) {
2189 /* this should be impossible, but we bark here for debugging */
2190 printf("nd6_storelladdr: sdl_alen == 0\n");
2191 return(0); /* caller will free mbuf */
2194 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
2198 static int nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
;
2199 static int nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
;
2200 SYSCTL_DECL(_net_inet6_icmp6
);
2201 SYSCTL_NODE(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
2202 CTLFLAG_RD
, nd6_sysctl_drlist
, "");
2203 SYSCTL_NODE(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
2204 CTLFLAG_RD
, nd6_sysctl_prlist
, "");
2207 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
2211 struct in6_defrouter
*d
, *de
;
2212 struct nd_defrouter
*dr
;
2218 for (dr
= TAILQ_FIRST(&nd_defrouter
);
2220 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2221 d
= (struct in6_defrouter
*)buf
;
2222 de
= (struct in6_defrouter
*)(buf
+ sizeof(buf
));
2225 bzero(d
, sizeof(*d
));
2226 d
->rtaddr
.sin6_family
= AF_INET6
;
2227 d
->rtaddr
.sin6_len
= sizeof(d
->rtaddr
);
2228 if (in6_recoverscope(&d
->rtaddr
, &dr
->rtaddr
,
2232 "default router list (%s)\n",
2233 ip6_sprintf(&dr
->rtaddr
));
2234 d
->flags
= dr
->flags
;
2235 d
->rtlifetime
= dr
->rtlifetime
;
2236 d
->expire
= dr
->expire
;
2237 d
->if_index
= dr
->ifp
->if_index
;
2239 panic("buffer too short");
2241 error
= SYSCTL_OUT(req
, buf
, sizeof(*d
));
2249 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
2253 struct in6_prefix
*p
, *pe
;
2254 struct nd_prefix
*pr
;
2260 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2263 struct sockaddr_in6
*sin6
, *s6
;
2264 struct nd_pfxrouter
*pfr
;
2266 p
= (struct in6_prefix
*)buf
;
2267 pe
= (struct in6_prefix
*)(buf
+ sizeof(buf
));
2270 bzero(p
, sizeof(*p
));
2271 sin6
= (struct sockaddr_in6
*)(p
+ 1);
2273 p
->prefix
= pr
->ndpr_prefix
;
2274 if (in6_recoverscope(&p
->prefix
,
2275 &p
->prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
2277 "scope error in prefix list (%s)\n",
2278 ip6_sprintf(&p
->prefix
.sin6_addr
));
2279 p
->raflags
= pr
->ndpr_raf
;
2280 p
->prefixlen
= pr
->ndpr_plen
;
2281 p
->vltime
= pr
->ndpr_vltime
;
2282 p
->pltime
= pr
->ndpr_pltime
;
2283 p
->if_index
= pr
->ndpr_ifp
->if_index
;
2284 p
->expire
= pr
->ndpr_expire
;
2285 p
->refcnt
= pr
->ndpr_refcnt
;
2286 p
->flags
= pr
->ndpr_stateflags
;
2287 p
->origin
= PR_ORIG_RA
;
2289 for (pfr
= pr
->ndpr_advrtrs
.lh_first
;
2291 pfr
= pfr
->pfr_next
) {
2292 if ((void *)&sin6
[advrtrs
+ 1] >
2297 s6
= &sin6
[advrtrs
];
2298 bzero(s6
, sizeof(*s6
));
2299 s6
->sin6_family
= AF_INET6
;
2300 s6
->sin6_len
= sizeof(*sin6
);
2301 if (in6_recoverscope(s6
,
2302 &pfr
->router
->rtaddr
,
2303 pfr
->router
->ifp
) != 0)
2306 "prefix list (%s)\n",
2307 ip6_sprintf(&pfr
->router
->rtaddr
));
2310 p
->advrtrs
= advrtrs
;
2312 panic("buffer too short");
2314 advance
= sizeof(*p
) + sizeof(*sin6
) * advrtrs
;
2315 error
= SYSCTL_OUT(req
, buf
, advance
);