1 /* $KAME: nd6.c,v 1.51.2.1 2000/04/13 11:59:29 jinmei Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * BSD/OS version heavily modifies this code, related to llinfo.
36 * Since we don't have BSD/OS version of net/route.c in our hand,
37 * I left the code mostly as it was in 970310. -- itojun
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
47 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
50 #include <sys/ioctl.h>
52 #include <sys/syslog.h>
53 #include <sys/protosw.h>
54 #include <kern/queue.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #if !(defined(__bsdi__) && _BSDI_VERSION >= 199802) && !defined(__APPLE__)
60 #include <net/if_atm.h>
62 #include <net/route.h>
65 #include <netinet/in.h>
67 #include <netinet/if_ether.h>
69 #include <netinet/if_fddi.h>
72 #include <net/if_fddi.h>
74 #else /* __NetBSD__ */
75 #include <net/if_ether.h>
76 #include <netinet/if_inarp.h>
77 #include <net/if_fddi.h>
78 #endif /* __NetBSD__ */
79 #include <netinet6/in6_var.h>
80 #include <netinet/ip6.h>
81 #include <netinet6/ip6_var.h>
82 #include <netinet6/nd6.h>
83 #include <netinet6/in6_prefix.h>
84 #include <netinet/icmp6.h>
89 #if defined(__NetBSD__) || defined(__OpenBSD__)
90 extern struct ifnet loif
[NLOOP
];
93 #include <net/net_osdep.h>
95 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
96 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
98 #define SIN6(s) ((struct sockaddr_in6 *)s)
99 #define SDL(s) ((struct sockaddr_dl *)s)
102 int nd6_prune
= 1; /* walk list every 1 seconds */
103 int nd6_delay
= 5; /* delay first probe time 5 second */
104 int nd6_umaxtries
= 3; /* maximum unicast query */
105 int nd6_mmaxtries
= 3; /* maximum multicast query */
106 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
108 /* preventing too many loops in ND option parsing */
109 int nd6_maxndopt
= 10; /* max # of ND options allowed */
112 static int nd6_inuse
, nd6_allocated
;
114 struct llinfo_nd6 llinfo_nd6
= {&llinfo_nd6
, &llinfo_nd6
};
115 struct nd_ifinfo
*nd_ifinfo
= NULL
;
116 struct nd_drhead nd_defrouter
;
117 struct nd_prhead nd_prefix
= { 0 };
119 int nd6_recalc_reachtm_interval
= ND6_RECALC_REACHTM_INTERVAL
;
120 static struct sockaddr_in6 all1_sa
;
122 static void nd6_slowtimo
__P((void *));
123 static void nd6_slowtimo_funneled
__P((void *));
126 void (*mip6_expired_defrouter_hook
)(struct nd_defrouter
*dr
) = 0;
132 static int nd6_init_done
= 0;
136 log(LOG_NOTICE
, "nd6_init called more than once(ignored)\n");
140 all1_sa
.sin6_family
= AF_INET6
;
141 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
142 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++)
143 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
145 /* initialization of the default router list */
146 TAILQ_INIT(&nd_defrouter
);
151 timeout(nd6_slowtimo_funneled
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
158 static size_t if_indexlim
= 8;
161 * We have some arrays that should be indexed by if_index.
162 * since if_index will grow dynamically, they should grow too.
164 if (nd_ifinfo
== NULL
|| if_index
>= if_indexlim
) {
168 while (if_index
>= if_indexlim
)
172 n
= if_indexlim
* sizeof(struct nd_ifinfo
);
173 q
= (caddr_t
)_MALLOC(n
, M_IP6NDP
, M_WAITOK
);
176 bcopy((caddr_t
)nd_ifinfo
, q
, n
/2);
177 _FREE((caddr_t
)nd_ifinfo
, M_IP6NDP
);
179 nd_ifinfo
= (struct nd_ifinfo
*)q
;
182 #define ND nd_ifinfo[ifp->if_index]
183 ND
.linkmtu
= ifindex2ifnet
[ifp
->if_index
]->if_mtu
;
184 ND
.chlim
= IPV6_DEFHLIM
;
185 ND
.basereachable
= REACHABLE_TIME
;
186 ND
.reachable
= ND_COMPUTE_RTIME(ND
.basereachable
);
187 ND
.retrans
= RETRANS_TIMER
;
189 ND
.flags
= ND6_IFF_PERFORMNUD
;
195 * Reset ND level link MTU. This function is called when the physical MTU
196 * changes, which means we might have to adjust the ND level MTU.
202 struct nd_ifinfo
*ndi
= &nd_ifinfo
[ifp
->if_index
];
203 u_long oldmaxmtu
= ndi
->maxmtu
;
204 u_long oldlinkmtu
= ndi
->linkmtu
;
206 switch(ifp
->if_type
) {
207 case IFT_ARCNET
: /* XXX MTU handling needs more work */
208 ndi
->maxmtu
= MIN(60480, ifp
->if_mtu
);
211 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
213 #if defined(__FreeBSD__) || defined(__bsdi__)
215 ndi
->maxmtu
= MIN(FDDIIPMTU
, ifp
->if_mtu
);
218 #if !(defined(__bsdi__) && _BSDI_VERSION >= 199802) && !defined (__APPLE__)
220 ndi
->maxmtu
= MIN(ATMMTU
, ifp
->if_mtu
);
224 ndi
->maxmtu
= ifp
->if_mtu
;
228 if (oldmaxmtu
!= ndi
->maxmtu
) {
230 * If the ND level MTU is not set yet, or if the maxmtu
231 * is reset to a smaller value than the ND level MTU,
232 * also reset the ND level MTU.
234 if (ndi
->linkmtu
== 0 ||
235 ndi
->maxmtu
< ndi
->linkmtu
) {
236 ndi
->linkmtu
= ndi
->maxmtu
;
237 /* also adjust in6_maxmtu if necessary. */
238 if (oldlinkmtu
== 0) {
240 * XXX: the case analysis is grotty, but
241 * it is not efficient to call in6_setmaxmtu()
242 * here when we are during the initialization
245 if (in6_maxmtu
< ndi
->linkmtu
)
246 in6_maxmtu
= ndi
->linkmtu
;
256 nd6_option_init(opt
, icmp6len
, ndopts
)
259 union nd_opts
*ndopts
;
261 bzero(ndopts
, sizeof(*ndopts
));
262 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
264 = (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
267 ndopts
->nd_opts_done
= 1;
268 ndopts
->nd_opts_search
= NULL
;
273 * Take one ND option.
277 union nd_opts
*ndopts
;
279 struct nd_opt_hdr
*nd_opt
;
283 panic("ndopts == NULL in nd6_option\n");
284 if (!ndopts
->nd_opts_last
)
285 panic("uninitialized ndopts in nd6_option\n");
286 if (!ndopts
->nd_opts_search
)
288 if (ndopts
->nd_opts_done
)
291 nd_opt
= ndopts
->nd_opts_search
;
293 olen
= nd_opt
->nd_opt_len
<< 3;
296 * Message validation requires that all included
297 * options have a length that is greater than zero.
299 bzero(ndopts
, sizeof(*ndopts
));
303 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
304 if (!(ndopts
->nd_opts_search
< ndopts
->nd_opts_last
)) {
305 ndopts
->nd_opts_done
= 1;
306 ndopts
->nd_opts_search
= NULL
;
312 * Parse multiple ND options.
313 * This function is much easier to use, for ND routines that do not need
314 * multiple options of the same type.
318 union nd_opts
*ndopts
;
320 struct nd_opt_hdr
*nd_opt
;
324 panic("ndopts == NULL in nd6_options\n");
325 if (!ndopts
->nd_opts_last
)
326 panic("uninitialized ndopts in nd6_options\n");
327 if (!ndopts
->nd_opts_search
)
331 nd_opt
= nd6_option(ndopts
);
332 if (!nd_opt
&& !ndopts
->nd_opts_last
) {
334 * Message validation requires that all included
335 * options have a length that is greater than zero.
337 bzero(ndopts
, sizeof(*ndopts
));
344 switch (nd_opt
->nd_opt_type
) {
345 case ND_OPT_SOURCE_LINKADDR
:
346 case ND_OPT_TARGET_LINKADDR
:
348 case ND_OPT_REDIRECTED_HEADER
:
349 case ND_OPT_ADV_INTERVAL
:
350 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
351 printf("duplicated ND6 option found "
352 "(type=%d)\n", nd_opt
->nd_opt_type
);
355 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
359 case ND_OPT_PREFIX_INFORMATION
:
360 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
361 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
364 ndopts
->nd_opts_pi_end
=
365 (struct nd_opt_prefix_info
*)nd_opt
;
367 case ND_OPT_HA_INFORMATION
:
371 * Unknown options must be silently ignored,
372 * to accomodate future extension to the protocol.
375 "nd6_options: unsupported option %d - "
376 "option ignored\n", nd_opt
->nd_opt_type
);
381 if (i
> nd6_maxndopt
) {
382 icmp6stat
.icp6s_nd_toomanyopt
++;
383 printf("too many loop in nd opt\n");
387 if (ndopts
->nd_opts_done
)
395 * ND6 timer routine to expire default route list and prefix list
398 nd6_timer_funneled(ignored_arg
)
402 boolean_t funnel_state
;
403 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
405 nd6_timer(ignored_arg
);
407 (void) thread_funnel_set(network_flock
, FALSE
);
411 nd6_timer(ignored_arg
)
415 register struct llinfo_nd6
*ln
;
416 register struct nd_defrouter
*dr
;
417 register struct nd_prefix
*pr
;
418 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
419 long time_second
= time
.tv_sec
;
427 timeout(nd6_timer_funneled
, (caddr_t
)0, nd6_prune
* hz
);
429 ln
= llinfo_nd6
.ln_next
;
430 /* XXX BSD/OS separates this code -- itojun */
431 while (ln
&& ln
!= &llinfo_nd6
) {
434 struct sockaddr_in6
*dst
;
435 struct llinfo_nd6
*next
= ln
->ln_next
;
436 /* XXX: used for the DELAY case only: */
437 struct nd_ifinfo
*ndi
= NULL
;
439 if ((rt
= ln
->ln_rt
) == NULL
) {
443 if ((ifp
= rt
->rt_ifp
) == NULL
) {
447 ndi
= &nd_ifinfo
[ifp
->if_index
];
448 dst
= (struct sockaddr_in6
*)rt_key(rt
);
450 if (ln
->ln_expire
> time_second
) {
457 panic("rt=0 in nd6_timer(ln=%p)\n", ln
);
458 if (rt
->rt_llinfo
&& (struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
)
459 panic("rt_llinfo(%p) is not equal to ln(%p)\n",
462 panic("dst=0 in nd6_timer(ln=%p)\n", ln
);
464 switch (ln
->ln_state
) {
465 case ND6_LLINFO_INCOMPLETE
:
466 if (ln
->ln_asked
< nd6_mmaxtries
) {
468 ln
->ln_expire
= time_second
+
469 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
470 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
,
473 struct mbuf
*m
= ln
->ln_hold
;
477 * Fake rcvif to make ICMP error
478 * more helpful in diagnosing
480 * XXX: should we consider
483 m
->m_pkthdr
.rcvif
= rt
->rt_ifp
;
485 icmp6_error(m
, ICMP6_DST_UNREACH
,
486 ICMP6_DST_UNREACH_ADDR
, 0);
492 case ND6_LLINFO_REACHABLE
:
494 ln
->ln_state
= ND6_LLINFO_STALE
;
497 * ND6_LLINFO_STALE state requires nothing for timer
500 case ND6_LLINFO_DELAY
:
501 if (ndi
&& (ndi
->flags
& ND6_IFF_PERFORMNUD
) != 0) {
504 ln
->ln_state
= ND6_LLINFO_PROBE
;
505 ln
->ln_expire
= time_second
+
507 nd6_ns_output(ifp
, &dst
->sin6_addr
,
512 ln
->ln_state
= ND6_LLINFO_STALE
; /* XXX */
514 case ND6_LLINFO_PROBE
:
515 if (ln
->ln_asked
< nd6_umaxtries
) {
517 ln
->ln_expire
= time_second
+
518 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
519 nd6_ns_output(ifp
, &dst
->sin6_addr
,
520 &dst
->sin6_addr
, ln
, 0);
525 case ND6_LLINFO_WAITDELETE
:
533 dr
= TAILQ_FIRST(&nd_defrouter
);
535 if (dr
->expire
&& dr
->expire
< time_second
) {
536 struct nd_defrouter
*t
;
537 t
= TAILQ_NEXT(dr
, dr_entry
);
542 if (mip6_expired_defrouter_hook
)
543 (*mip6_expired_defrouter_hook
)(dr
);
545 dr
= TAILQ_NEXT(dr
, dr_entry
);
548 pr
= nd_prefix
.lh_first
;
550 struct in6_ifaddr
*ia6
;
551 struct in6_addrlifetime
*lt6
;
553 if (IN6_IS_ADDR_UNSPECIFIED(&pr
->ndpr_addr
))
556 ia6
= in6ifa_ifpwithaddr(pr
->ndpr_ifp
, &pr
->ndpr_addr
);
559 /* check address lifetime */
560 lt6
= &ia6
->ia6_lifetime
;
561 if (lt6
->ia6t_preferred
&& lt6
->ia6t_preferred
< time_second
)
562 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
563 if (lt6
->ia6t_expire
&& lt6
->ia6t_expire
< time_second
) {
564 if (!IN6_IS_ADDR_UNSPECIFIED(&pr
->ndpr_addr
))
565 in6_ifdel(pr
->ndpr_ifp
, &pr
->ndpr_addr
);
566 /* xxx ND_OPT_PI_FLAG_ONLINK processing */
571 * check prefix lifetime.
572 * since pltime is just for autoconf, pltime processing for
573 * prefix is not necessary.
575 * we offset expire time by NDPR_KEEP_EXPIRE, so that we
576 * can use the old prefix information to validate the
577 * next prefix information to come. See prelist_update()
578 * for actual validation.
581 && pr
->ndpr_expire
+ NDPR_KEEP_EXPIRED
< time_second
) {
586 * address expiration and prefix expiration are
587 * separate. NEVER perform in6_ifdel here.
599 * Nuke neighbor cache/prefix/default router management table, right before
606 struct llinfo_nd6
*ln
, *nln
;
607 struct nd_defrouter
*dr
, *ndr
, drany
;
608 struct nd_prefix
*pr
, *npr
;
610 /* Nuke default router list entries toward ifp */
611 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
613 * The first entry of the list may be stored in
614 * the routing table, so we'll delete it later.
616 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= ndr
) {
617 ndr
= TAILQ_NEXT(dr
, dr_entry
);
621 dr
= TAILQ_FIRST(&nd_defrouter
);
626 /* Nuke prefix list entries toward ifp */
627 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
629 if (pr
->ndpr_ifp
== ifp
) {
630 if (!IN6_IS_ADDR_UNSPECIFIED(&pr
->ndpr_addr
))
631 in6_ifdel(pr
->ndpr_ifp
, &pr
->ndpr_addr
);
636 /* cancel default outgoing interface setting */
637 if (nd6_defifindex
== ifp
->if_index
)
638 nd6_setdefaultiface(0);
640 /* refresh default router list */
641 bzero(&drany
, sizeof(drany
));
642 defrouter_delreq(&drany
, 0);
646 * Nuke neighbor cache entries for the ifp.
647 * Note that rt->rt_ifp may not be the same as ifp,
648 * due to KAME goto ours hack. See RTM_RESOLVE case in
649 * nd6_rtrequest(), and ip6_input().
651 ln
= llinfo_nd6
.ln_next
;
652 while (ln
&& ln
!= &llinfo_nd6
) {
654 struct sockaddr_dl
*sdl
;
658 if (rt
&& rt
->rt_gateway
&&
659 rt
->rt_gateway
->sa_family
== AF_LINK
) {
660 sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
;
661 if (sdl
->sdl_index
== ifp
->if_index
)
668 * Neighbor cache entry for interface route will be retained
669 * with ND6_LLINFO_WAITDELETE state, by nd6_free(). Nuke it.
671 ln
= llinfo_nd6
.ln_next
;
672 while (ln
&& ln
!= &llinfo_nd6
) {
674 struct sockaddr_dl
*sdl
;
678 if (rt
&& rt
->rt_gateway
&&
679 rt
->rt_gateway
->sa_family
== AF_LINK
) {
680 sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
;
681 if (sdl
->sdl_index
== ifp
->if_index
) {
682 rtrequest(RTM_DELETE
, rt_key(rt
),
683 (struct sockaddr
*)0, rt_mask(rt
), 0,
684 (struct rtentry
**)0);
692 nd6_lookup(addr6
, create
, ifp
)
693 struct in6_addr
*addr6
;
698 struct sockaddr_in6 sin6
;
700 bzero(&sin6
, sizeof(sin6
));
701 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
702 sin6
.sin6_family
= AF_INET6
;
703 sin6
.sin6_addr
= *addr6
;
704 rt
= rtalloc1((struct sockaddr
*)&sin6
, create
705 #if __FreeBSD__ || defined (__APPLE__)
707 #endif /*__FreeBSD__*/
709 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) == 0) {
711 * This is the case for the default route.
712 * If we want to create a neighbor cache for the address, we
713 * should free the route for the destination and allocate an
726 * If no route is available and create is set,
727 * we allocate a host route for the destination
728 * and treat it like an interface route.
729 * This hack is necessary for a neighbor which can't
730 * be covered by our own prefix.
733 ifaof_ifpforaddr((struct sockaddr
*)&sin6
, ifp
);
738 * Create a new route. RTF_LLINFO is necessary
739 * to create a Neighbor Cache entry for the
740 * destination in nd6_rtrequest which will be
741 * called in rtequest via ifa->ifa_rtrequest.
743 if ((e
= rtrequest(RTM_ADD
, (struct sockaddr
*)&sin6
,
745 (struct sockaddr
*)&all1_sa
,
747 RTF_HOST
| RTF_LLINFO
) &
751 "nd6_lookup: failed to add route for a "
752 "neighbor(%s), errno=%d\n",
753 ip6_sprintf(addr6
), e
);
757 struct llinfo_nd6
*ln
=
758 (struct llinfo_nd6
*)rt
->rt_llinfo
;
759 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
767 * Validation for the entry.
768 * XXX: we can't use rt->rt_ifp to check for the interface, since
769 * it might be the loopback interface if the entry is for our
770 * own address on a non-loopback interface. Instead, we should
771 * use rt->rt_ifa->ifa_ifp, which would specify the REAL interface.
773 if ((rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
774 rt
->rt_gateway
->sa_family
!= AF_LINK
||
775 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
)) {
777 log(LOG_DEBUG
, "nd6_lookup: failed to lookup %s (if = %s)\n",
778 ip6_sprintf(addr6
), ifp
? if_name(ifp
) : "unspec");
779 /* xxx more logs... kazu */
787 * Detect if a given IPv6 address identifies a neighbor on a given link.
788 * XXX: should take care of the destination of a p2p link?
791 nd6_is_addr_neighbor(addr
, ifp
)
792 struct in6_addr
*addr
;
795 register struct ifaddr
*ifa
;
798 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
799 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
801 /* A link-local address is always a neighbor. */
802 if (IN6_IS_ADDR_LINKLOCAL(addr
))
806 * If the address matches one of our addresses,
807 * it should be a neighbor.
809 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
810 for (ifa
= ifp
->if_addrlist
; ifa
; ifa
= ifa
->ifa_next
)
812 for (ifa
= ifp
->if_addrlist
.tqh_first
;
814 ifa
= ifa
->ifa_list
.tqe_next
)
817 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
820 for (i
= 0; i
< 4; i
++) {
821 if ((IFADDR6(ifa
).s6_addr32
[i
] ^ addr
->s6_addr32
[i
]) &
822 IFMASK6(ifa
).s6_addr32
[i
])
829 * Even if the address matches none of our addresses, it might be
830 * in the neighbor cache.
832 if (nd6_lookup(addr
, 0, ifp
))
841 * Free an nd6 llinfo entry.
847 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
848 struct sockaddr_dl
*sdl
;
849 struct in6_addr in6
= ((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
850 struct nd_defrouter
*dr
;
853 * Clear all destination cache entries for the neighbor.
854 * XXX: is it better to restrict this to hosts?
856 pfctlinput(PRC_HOSTDEAD
, rt_key(rt
));
858 if (!ip6_forwarding
&& ip6_accept_rtadv
) { /* XXX: too restrictive? */
865 dr
= defrouter_lookup(&((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
,
867 if (ln
->ln_router
|| dr
) {
869 * rt6_flush must be called whether or not the neighbor
870 * is in the Default Router List.
871 * See a corresponding comment in nd6_na_input().
873 rt6_flush(&in6
, rt
->rt_ifp
);
878 * Unreachablity of a router might affect the default
879 * router selection and on-link detection of advertised
884 * Temporarily fake the state to choose a new default
885 * router and to perform on-link determination of
886 * prefixes coreectly.
887 * Below the state will be set correctly,
888 * or the entry itself will be deleted.
890 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
892 if (dr
== TAILQ_FIRST(&nd_defrouter
)) {
894 * It is used as the current default router,
895 * so we have to move it to the end of the
896 * list and choose a new one.
897 * XXX: it is not very efficient if this is
900 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
901 TAILQ_INSERT_TAIL(&nd_defrouter
, dr
, dr_entry
);
905 pfxlist_onlink_check();
910 if (rt
->rt_refcnt
> 0 && (sdl
= SDL(rt
->rt_gateway
)) &&
911 sdl
->sdl_family
== AF_LINK
) {
913 ln
->ln_state
= ND6_LLINFO_WAITDELETE
;
915 rt
->rt_flags
&= ~RTF_REJECT
;
919 rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
920 rt_mask(rt
), 0, (struct rtentry
**)0);
924 * Upper-layer reachability hint for Neighbor Unreachability Detection.
926 * XXX cost-effective metods?
929 nd6_nud_hint(rt
, dst6
)
931 struct in6_addr
*dst6
;
933 struct llinfo_nd6
*ln
;
934 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
935 long time_second
= time
.tv_sec
;
939 * If the caller specified "rt", use that. Otherwise, resolve the
940 * routing table by supplied "dst6".
945 if (!(rt
= nd6_lookup(dst6
, 0, NULL
)))
949 if ((rt
->rt_flags
& RTF_GATEWAY
)
950 || (rt
->rt_flags
& RTF_LLINFO
) == 0
953 || rt
->rt_gateway
->sa_family
!= AF_LINK
) {
954 /* This is not a host route. */
958 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
959 if (ln
->ln_state
< ND6_LLINFO_REACHABLE
)
962 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
963 if (ln
->ln_expire
&& nd_ifinfo
)
964 ln
->ln_expire
= time_second
+
965 (rt
->rt_ifp
? nd_ifinfo
[rt
->rt_ifp
->if_index
].reachable
: 0) ;
970 * Resolve an IP6 address into an ethernet address. If success,
971 * desten is filled in. If there is no entry in ndptab,
972 * set one up and multicast a solicitation for the IP6 address.
973 * Hold onto this mbuf and resend it once the address
974 * is finally resolved. A return value of 1 indicates
975 * that desten has been filled in and the packet should be sent
976 * normally; a 0 return indicates that the packet has been
977 * taken over here, either now or for later transmission.
980 nd6_resolve(ifp
, rt
, m
, dst
, desten
)
984 struct sockaddr
*dst
;
987 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)NULL
;
988 struct sockaddr_dl
*sdl
;
989 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
990 long time_second
= time
.tv_sec
;
993 if (m
->m_flags
& M_MCAST
) {
994 switch (ifp
->if_type
) {
997 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
1009 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0)
1010 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1012 if ((rt
= nd6_lookup(&(SIN6(dst
)->sin6_addr
), 1, ifp
)) != NULL
)
1013 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1016 log(LOG_DEBUG
, "nd6_resolve: can't allocate llinfo for %s\n",
1017 ip6_sprintf(&(SIN6(dst
)->sin6_addr
)));
1021 sdl
= SDL(rt
->rt_gateway
);
1023 * Ckeck the address family and length is valid, the address
1024 * is resolved; otherwise, try to resolve.
1026 if (ln
->ln_state
>= ND6_LLINFO_REACHABLE
1027 && sdl
->sdl_family
== AF_LINK
1028 && sdl
->sdl_alen
!= 0) {
1029 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
1030 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1032 ln
->ln_state
= ND6_LLINFO_DELAY
;
1033 ln
->ln_expire
= time_second
+ nd6_delay
;
1038 * There is an ndp entry, but no ethernet address
1039 * response yet. Replace the held mbuf with this
1042 * XXX Does the code conform to rate-limiting rule?
1045 if (ln
->ln_state
== ND6_LLINFO_WAITDELETE
||
1046 ln
->ln_state
== ND6_LLINFO_NOSTATE
)
1047 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1049 m_freem(ln
->ln_hold
);
1051 if (ln
->ln_expire
) {
1052 rt
->rt_flags
&= ~RTF_REJECT
;
1053 if (ln
->ln_asked
< nd6_mmaxtries
&&
1054 ln
->ln_expire
< time_second
) {
1056 ln
->ln_expire
= time_second
+
1057 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
1058 nd6_ns_output(ifp
, NULL
, &(SIN6(dst
)->sin6_addr
),
1064 #endif /* OLDIP6OUTPUT */
1067 #if defined(__bsdi__) && _BSDI_VERSION >= 199802
1068 nd6_rtrequest(req
, rt
, info
)
1071 struct rt_addrinfo
*info
; /* xxx unused */
1073 nd6_rtrequest(req
, rt
, sa
)
1076 struct sockaddr
*sa
; /* xxx unused */
1079 struct sockaddr
*gate
= rt
->rt_gateway
;
1080 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1081 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
};
1082 struct ifnet
*ifp
= rt
->rt_ifp
;
1084 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
1085 long time_second
= time
.tv_sec
;
1088 if (rt
->rt_flags
& RTF_GATEWAY
)
1094 * There is no backward compatibility :)
1096 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1097 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1098 * rt->rt_flags |= RTF_CLONING;
1100 if (rt
->rt_flags
& (RTF_CLONING
| RTF_LLINFO
)) {
1102 * Case 1: This route should come from
1103 * a route to interface. RTF_LLINFO flag is set
1104 * for a host route whose destination should be
1105 * treated as on-link.
1107 rt_setgate(rt
, rt_key(rt
),
1108 (struct sockaddr
*)&null_sdl
);
1109 gate
= rt
->rt_gateway
;
1110 SDL(gate
)->sdl_type
= ifp
->if_type
;
1111 SDL(gate
)->sdl_index
= ifp
->if_index
;
1113 ln
->ln_expire
= time_second
;
1115 if (ln
&& ln
->ln_expire
== 0) {
1116 /* cludge for desktops */
1118 printf("nd6_request: time.tv_sec is zero; "
1124 if (rt
->rt_flags
& RTF_CLONING
)
1128 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1129 * We don't do that here since llinfo is not ready yet.
1131 * There are also couple of other things to be discussed:
1132 * - unsolicited NA code needs improvement beforehand
1133 * - RFC2461 says we MAY send multicast unsolicited NA
1134 * (7.2.6 paragraph 4), however, it also says that we
1135 * SHOULD provide a mechanism to prevent multicast NA storm.
1136 * we don't have anything like it right now.
1137 * note that the mechanism need a mutual agreement
1138 * between proxies, which means that we need to implement
1139 * a new protocol, or new kludge.
1140 * - from RFC2461 6.2.4, host MUST NOT send unsolicited NA.
1141 * we need to check ip6forwarding before sending it.
1142 * (or should we allow proxy ND configuration only for
1143 * routers? there's no mention about proxy ND from hosts)
1146 /* XXX it does not work */
1147 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1149 &SIN6(rt_key(rt
))->sin6_addr
,
1150 &SIN6(rt_key(rt
))->sin6_addr
,
1151 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1156 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0) {
1158 * Address resolution isn't necessary for a point to
1159 * point link, so we can skip this test for a p2p link.
1161 if (gate
->sa_family
!= AF_LINK
||
1162 gate
->sa_len
< sizeof(null_sdl
)) {
1164 "nd6_rtrequest: bad gateway value\n");
1167 SDL(gate
)->sdl_type
= ifp
->if_type
;
1168 SDL(gate
)->sdl_index
= ifp
->if_index
;
1171 break; /* This happens on a route change */
1173 * Case 2: This route may come from cloning, or a manual route
1174 * add with a LL address.
1176 R_Malloc(ln
, struct llinfo_nd6
*, sizeof(*ln
));
1177 rt
->rt_llinfo
= (caddr_t
)ln
;
1179 log(LOG_DEBUG
, "nd6_rtrequest: malloc failed\n");
1184 Bzero(ln
, sizeof(*ln
));
1186 /* this is required for "ndp" command. - shin */
1187 if (req
== RTM_ADD
) {
1189 * gate should have some valid AF_LINK entry,
1190 * and ln->ln_expire should have some lifetime
1191 * which is specified by ndp command.
1193 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1196 * When req == RTM_RESOLVE, rt is created and
1197 * initialized in rtrequest(), so rt_expire is 0.
1199 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1200 ln
->ln_expire
= time_second
;
1202 rt
->rt_flags
|= RTF_LLINFO
;
1203 ln
->ln_next
= llinfo_nd6
.ln_next
;
1204 llinfo_nd6
.ln_next
= ln
;
1205 ln
->ln_prev
= &llinfo_nd6
;
1206 ln
->ln_next
->ln_prev
= ln
;
1209 * check if rt_key(rt) is one of my address assigned
1212 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1213 &SIN6(rt_key(rt
))->sin6_addr
);
1215 caddr_t macp
= nd6_ifptomac(ifp
);
1217 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1219 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
1220 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
1222 if (nd6_useloopback
) {
1224 #if _BSDI_VERSION >= 199802
1225 extern struct ifnet
*loifp
;
1226 rt
->rt_ifp
= loifp
; /*XXX*/
1228 extern struct ifnet loif
;
1229 rt
->rt_ifp
= &loif
; /*XXX*/
1231 #else /* non-bsdi */
1232 rt
->rt_ifp
= &loif
[0]; /*XXX*/
1235 * Make sure rt_ifa be equal to the ifaddr
1236 * corresponding to the address.
1237 * We need this because when we refer
1238 * rt_ifa->ia6_flags in ip6_input, we assume
1239 * that the rt_ifa points to the address instead
1240 * of the loopback address.
1242 if (ifa
!= rt
->rt_ifa
) {
1243 rt
->rt_ifa
->ifa_refcnt
--;
1248 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
1250 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1252 /* join solicited node multicast for proxy ND */
1253 if (ifp
->if_flags
& IFF_MULTICAST
) {
1254 struct in6_addr llsol
;
1257 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1258 llsol
.s6_addr16
[0] = htons(0xff02);
1259 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1260 llsol
.s6_addr32
[1] = 0;
1261 llsol
.s6_addr32
[2] = htonl(1);
1262 llsol
.s6_addr8
[12] = 0xff;
1264 (void)in6_addmulti(&llsol
, ifp
, &error
);
1267 "nd6_rtrequest: could not join solicited node multicast (errno=%d)\n", error
);
1275 /* leave from solicited node multicast for proxy ND */
1276 if ((rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
1277 (ifp
->if_flags
& IFF_MULTICAST
) != 0) {
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 #if defined(__bsdi__) && _BSDI_VERSION >= 199802
1306 nd6_p2p_rtrequest(req
, rt
, info
)
1309 struct rt_addrinfo
*info
; /* xxx unused */
1311 nd6_p2p_rtrequest(req
, rt
, sa
)
1314 struct sockaddr
*sa
; /* xxx unused */
1317 struct sockaddr
*gate
= rt
->rt_gateway
;
1318 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
};
1319 struct ifnet
*ifp
= rt
->rt_ifp
;
1322 if (rt
->rt_flags
& RTF_GATEWAY
)
1328 * There is no backward compatibility :)
1330 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1331 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1332 * rt->rt_flags |= RTF_CLONING;
1334 if (rt
->rt_flags
& RTF_CLONING
) {
1336 * Case 1: This route should come from
1337 * a route to interface.
1339 rt_setgate(rt
, rt_key(rt
),
1340 (struct sockaddr
*)&null_sdl
);
1341 gate
= rt
->rt_gateway
;
1342 SDL(gate
)->sdl_type
= ifp
->if_type
;
1343 SDL(gate
)->sdl_index
= ifp
->if_index
;
1346 /* Announce a new entry if requested. */
1347 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1349 &SIN6(rt_key(rt
))->sin6_addr
,
1350 &SIN6(rt_key(rt
))->sin6_addr
,
1351 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1356 * check if rt_key(rt) is one of my address assigned
1359 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1360 &SIN6(rt_key(rt
))->sin6_addr
);
1362 if (nd6_useloopback
) {
1364 #if _BSDI_VERSION >= 199802
1365 extern struct ifnet
*loifp
;
1366 rt
->rt_ifp
= loifp
; /*XXX*/
1368 extern struct ifnet loif
;
1369 rt
->rt_ifp
= &loif
; /*XXX*/
1372 rt
->rt_ifp
= &loif
[0]; /*XXX*/
1381 nd6_ioctl(cmd
, data
, ifp
)
1386 struct in6_drlist
*drl
= (struct in6_drlist
*)data
;
1387 struct in6_prlist
*prl
= (struct in6_prlist
*)data
;
1388 struct in6_ndireq
*ndi
= (struct in6_ndireq
*)data
;
1389 struct in6_nbrinfo
*nbi
= (struct in6_nbrinfo
*)data
;
1390 struct in6_ndifreq
*ndif
= (struct in6_ndifreq
*)data
;
1391 struct nd_defrouter
*dr
, any
;
1392 struct nd_prefix
*pr
;
1394 int i
= 0, error
= 0;
1398 case SIOCGDRLST_IN6
:
1399 bzero(drl
, sizeof(*drl
));
1405 dr
= TAILQ_FIRST(&nd_defrouter
);
1406 while (dr
&& i
< DRLSTSIZ
) {
1407 drl
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1408 if (IN6_IS_ADDR_LINKLOCAL(&drl
->defrouter
[i
].rtaddr
)) {
1409 /* XXX: need to this hack for KAME stack */
1410 drl
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1414 "default router list contains a "
1415 "non-linklocal address(%s)\n",
1416 ip6_sprintf(&drl
->defrouter
[i
].rtaddr
));
1418 drl
->defrouter
[i
].flags
= dr
->flags
;
1419 drl
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1420 drl
->defrouter
[i
].expire
= dr
->expire
;
1421 drl
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1423 dr
= TAILQ_NEXT(dr
, dr_entry
);
1427 case SIOCGPRLST_IN6
:
1429 * XXX meaning of fields, especialy "raflags", is very
1430 * differnet between RA prefix list and RR/static prefix list.
1431 * how about separating ioctls into two?
1433 bzero(prl
, sizeof(*prl
));
1439 pr
= nd_prefix
.lh_first
;
1440 while (pr
&& i
< PRLSTSIZ
) {
1441 struct nd_pfxrouter
*pfr
;
1444 prl
->prefix
[i
].prefix
= pr
->ndpr_prefix
.sin6_addr
;
1445 prl
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1446 prl
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1447 prl
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1448 prl
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1449 prl
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1450 prl
->prefix
[i
].expire
= pr
->ndpr_expire
;
1452 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1456 #define RTRADDR prl->prefix[i].advrtr[j]
1457 RTRADDR
= pfr
->router
->rtaddr
;
1458 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1459 /* XXX: hack for KAME */
1460 RTRADDR
.s6_addr16
[1] = 0;
1464 "a router(%s) advertises "
1466 "non-link local address\n",
1467 ip6_sprintf(&RTRADDR
));
1471 pfr
= pfr
->pfr_next
;
1473 prl
->prefix
[i
].advrtrs
= j
;
1474 prl
->prefix
[i
].origin
= PR_ORIG_RA
;
1480 struct rr_prefix
*rpp
;
1482 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1483 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1486 prl
->prefix
[i
].prefix
= rpp
->rp_prefix
.sin6_addr
;
1487 prl
->prefix
[i
].raflags
= rpp
->rp_raf
;
1488 prl
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1489 prl
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1490 prl
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1491 prl
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1492 prl
->prefix
[i
].expire
= rpp
->rp_expire
;
1493 prl
->prefix
[i
].advrtrs
= 0;
1494 prl
->prefix
[i
].origin
= rpp
->rp_origin
;
1501 case SIOCGIFINFO_IN6
:
1502 ndi
->ndi
= nd_ifinfo
[ifp
->if_index
];
1504 case SIOCSIFINFO_FLAGS
:
1505 /* XXX: almost all other fields of ndi->ndi is unused */
1506 nd_ifinfo
[ifp
->if_index
].flags
= ndi
->ndi
.flags
;
1508 case SIOCSNDFLUSH_IN6
: /* XXX: the ioctl name is confusing... */
1509 /* flush default router list */
1511 * xxx sumikawa: should not delete route if default
1512 * route equals to the top of default router list
1514 bzero(&any
, sizeof(any
));
1515 defrouter_delreq(&any
, 0);
1517 /* xxx sumikawa: flush prefix list */
1519 case SIOCSPFXFLUSH_IN6
:
1521 /* flush all the prefix advertised by routers */
1522 struct nd_prefix
*pr
, *next
;
1529 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
1530 next
= pr
->ndpr_next
;
1531 if (!IN6_IS_ADDR_UNSPECIFIED(&pr
->ndpr_addr
))
1532 in6_ifdel(pr
->ndpr_ifp
, &pr
->ndpr_addr
);
1538 case SIOCSRTRFLUSH_IN6
:
1540 /* flush all the default routers */
1541 struct nd_defrouter
*dr
, *next
;
1548 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
1550 * The first entry of the list may be stored in
1551 * the routing table, so we'll delete it later.
1553 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
1554 next
= TAILQ_NEXT(dr
, dr_entry
);
1557 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
));
1562 case SIOCGNBRINFO_IN6
:
1564 struct llinfo_nd6
*ln
;
1565 struct in6_addr nb_addr
= nbi
->addr
; /* make local for safety */
1568 * XXX: KAME specific hack for scoped addresses
1569 * XXXX: for other scopes than link-local?
1571 if (IN6_IS_ADDR_LINKLOCAL(&nbi
->addr
) ||
1572 IN6_IS_ADDR_MC_LINKLOCAL(&nbi
->addr
)) {
1573 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
1576 *idp
= htons(ifp
->if_index
);
1584 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
)) == NULL
) {
1589 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1590 nbi
->state
= ln
->ln_state
;
1591 nbi
->asked
= ln
->ln_asked
;
1592 nbi
->isrouter
= ln
->ln_router
;
1593 nbi
->expire
= ln
->ln_expire
;
1598 case SIOCGDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1599 ndif
->ifindex
= nd6_defifindex
;
1601 case SIOCSDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1602 return(nd6_setdefaultiface(ndif
->ifindex
));
1609 * Create neighbor cache entry and cache link-layer address,
1610 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1613 nd6_cache_lladdr(ifp
, from
, lladdr
, lladdrlen
, type
, code
)
1615 struct in6_addr
*from
;
1618 int type
; /* ICMP6 type */
1619 int code
; /* type dependent information */
1621 struct rtentry
*rt
= NULL
;
1622 struct llinfo_nd6
*ln
= NULL
;
1624 struct sockaddr_dl
*sdl
= NULL
;
1629 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
1630 long time_second
= time
.tv_sec
;
1634 panic("ifp == NULL in nd6_cache_lladdr");
1636 panic("from == NULL in nd6_cache_lladdr");
1638 /* nothing must be updated for unspecified address */
1639 if (IN6_IS_ADDR_UNSPECIFIED(from
))
1643 * Validation about ifp->if_addrlen and lladdrlen must be done in
1646 * XXX If the link does not have link-layer adderss, what should
1647 * we do? (ifp->if_addrlen == 0)
1648 * Spec says nothing in sections for RA, RS and NA. There's small
1649 * description on it in NS section (RFC 2461 7.2.3).
1652 rt
= nd6_lookup(from
, 0, ifp
);
1655 /* nothing must be done if there's no lladdr */
1656 if (!lladdr
|| !lladdrlen
)
1660 rt
= nd6_lookup(from
, 1, ifp
);
1667 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
1672 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1675 if (!rt
->rt_gateway
)
1677 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
1679 sdl
= SDL(rt
->rt_gateway
);
1681 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
1682 if (olladdr
&& lladdr
) {
1683 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
1691 * newentry olladdr lladdr llchange (*=record)
1694 * 0 n y -- (3) * STALE
1696 * 0 y y y (5) * STALE
1697 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1698 * 1 -- y -- (7) * STALE
1701 if (lladdr
) { /*(3-5) and (7)*/
1703 * Record source link-layer address
1704 * XXX is it dependent to ifp->if_type?
1706 sdl
->sdl_alen
= ifp
->if_addrlen
;
1707 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
1711 if ((!olladdr
&& lladdr
) /*(3)*/
1712 || (olladdr
&& lladdr
&& llchange
)) { /*(5)*/
1714 newstate
= ND6_LLINFO_STALE
;
1719 if (!lladdr
) /*(6)*/
1720 newstate
= ND6_LLINFO_NOSTATE
;
1722 newstate
= ND6_LLINFO_STALE
;
1727 * Update the state of the neighbor cache.
1729 ln
->ln_state
= newstate
;
1731 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1732 rt
->rt_flags
&= ~RTF_REJECT
;
1735 (*ifp
->if_output
)(ifp
, ln
->ln_hold
,
1738 nd6_output(ifp
, ln
->ln_hold
,
1739 (struct sockaddr_in6
*)rt_key(rt
),
1744 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
1745 /* probe right away */
1746 ln
->ln_expire
= time_second
;
1751 * ICMP6 type dependent behavior.
1753 * NS: clear IsRouter if new entry
1754 * RS: clear IsRouter
1755 * RA: set IsRouter if there's lladdr
1756 * redir: clear IsRouter if new entry
1759 * The spec says that we must set IsRouter in the following cases:
1760 * - If lladdr exist, set IsRouter. This means (1-5).
1761 * - If it is old entry (!newentry), set IsRouter. This means (7).
1762 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1763 * A quetion arises for (1) case. (1) case has no lladdr in the
1764 * neighbor cache, this is similar to (6).
1765 * This case is rare but we figured that we MUST NOT set IsRouter.
1767 * newentry olladdr lladdr llchange NS RS RA redir
1769 * 0 n n -- (1) c ? s
1770 * 0 y n -- (2) c s s
1771 * 0 n y -- (3) c s s
1774 * 1 -- n -- (6) c c c s
1775 * 1 -- y -- (7) c c s c s
1779 switch (type
& 0xff) {
1780 case ND_NEIGHBOR_SOLICIT
:
1782 * New entry must have is_router flag cleared.
1784 if (is_newentry
) /*(6-7)*/
1789 * If the icmp is a redirect to a better router, always set the
1790 * is_router flag. Otherwise, if the entry is newly created,
1791 * clear the flag. [RFC 2461, sec 8.3]
1794 if (code
== ND_REDIRECT_ROUTER
)
1796 else if (is_newentry
) /*(6-7)*/
1799 case ND_ROUTER_SOLICIT
:
1801 * is_router flag must always be cleared.
1805 case ND_ROUTER_ADVERT
:
1807 * Mark an entry with lladdr as a router.
1809 if ((!is_newentry
&& (olladdr
|| lladdr
)) /*(2-5)*/
1810 || (is_newentry
&& lladdr
)) { /*(7)*/
1820 nd6_slowtimo_funneled(ignored_arg
)
1824 boolean_t funnel_state
;
1825 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
1827 nd6_slowtimo(ignored_arg
);
1829 (void) thread_funnel_set(network_flock
, FALSE
);
1834 nd6_slowtimo(ignored_arg
)
1839 register struct nd_ifinfo
*nd6if
;
1847 timeout(nd6_slowtimo_funneled
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
1848 for (i
= 1; i
< if_index
+ 1; i
++) {
1849 nd6if
= &nd_ifinfo
[i
];
1850 if (nd6if
->basereachable
&& /* already initialized */
1851 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
1853 * Since reachable time rarely changes by router
1854 * advertisements, we SHOULD insure that a new random
1855 * value gets recomputed at least once every few hours.
1858 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
1859 nd6if
->reachable
= ND_COMPUTE_RTIME(nd6if
->basereachable
);
1866 #define senderr(e) { error = (e); goto bad;}
1868 nd6_output(ifp
, m0
, dst
, rt0
)
1869 register struct ifnet
*ifp
;
1871 struct sockaddr_in6
*dst
;
1872 struct rtentry
*rt0
;
1874 register struct mbuf
*m
= m0
;
1875 register struct rtentry
*rt
= rt0
;
1876 struct llinfo_nd6
*ln
= NULL
;
1878 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
1879 long time_second
= time
.tv_sec
;
1882 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
))
1886 * XXX: we currently do not make neighbor cache on any interface
1887 * other than ARCnet, Ethernet, FDDI and GIF.
1889 * draft-ietf-ngtrans-mech-04.txt says:
1890 * - unidirectional tunnels needs no ND
1892 switch (ifp
->if_type
) {
1896 case IFT_GIF
: /* XXX need more cases? */
1902 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
1903 (nd_ifinfo
[ifp
->if_index
].flags
& ND6_IFF_PERFORMNUD
) == 0)
1907 * next hop determination. This routine is derived from ether_outpout.
1910 if ((rt
->rt_flags
& RTF_UP
) == 0) {
1911 #if __FreeBSD__ || defined (__APPLE__)
1912 if ((rt0
= rt
= rtalloc1((struct sockaddr
*)dst
, 1, 0UL)) !=
1915 if ((rt0
= rt
= rtalloc1((struct sockaddr
*)dst
, 1)) !=
1920 if (rt
->rt_ifp
!= ifp
)
1921 return nd6_output(ifp
, m0
, dst
, rt
); /* XXX: loop care? */
1923 senderr(EHOSTUNREACH
);
1925 if (rt
->rt_flags
& RTF_GATEWAY
) {
1926 if (rt
->rt_gwroute
== 0)
1928 if (((rt
= rt
->rt_gwroute
)->rt_flags
& RTF_UP
) == 0) {
1929 rtfree(rt
); rt
= rt0
;
1930 #if __FreeBSD__ || defined (__APPLE__)
1931 lookup
: rt
->rt_gwroute
= rtalloc1(rt
->rt_gateway
, 1, 0UL);
1933 lookup
: rt
->rt_gwroute
= rtalloc1(rt
->rt_gateway
, 1);
1935 if ((rt
= rt
->rt_gwroute
) == 0)
1936 senderr(EHOSTUNREACH
);
1938 /* the "G" test below also prevents rt == rt0 */
1939 if ((rt
->rt_flags
& RTF_GATEWAY
) ||
1940 (rt
->rt_ifp
!= ifp
)) {
1942 rt0
->rt_gwroute
= 0;
1943 senderr(EHOSTUNREACH
);
1948 if (rt
->rt_flags
& RTF_REJECT
)
1949 senderr(rt
== rt0
? EHOSTDOWN
: EHOSTUNREACH
);
1953 * Address resolution or Neighbor Unreachability Detection
1955 * At this point, the destination of the packet must be a unicast
1956 * or an anycast address(i.e. not a multicast).
1959 /* Look up the neighbor cache for the nexthop */
1960 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0)
1961 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1963 if ((rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
)) != NULL
)
1964 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1967 log(LOG_DEBUG
, "nd6_output: can't allocate llinfo for %s "
1969 ip6_sprintf(&dst
->sin6_addr
), ln
, rt
);
1970 senderr(EIO
); /* XXX: good error? */
1973 /* We don't have to do link-layer address resolution on a p2p link. */
1974 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
1975 ln
->ln_state
< ND6_LLINFO_REACHABLE
)
1976 ln
->ln_state
= ND6_LLINFO_STALE
;
1979 * The first time we send a packet to a neighbor whose entry is
1980 * STALE, we have to change the state to DELAY and a sets a timer to
1981 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1982 * neighbor unreachability detection on expiration.
1985 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1987 ln
->ln_state
= ND6_LLINFO_DELAY
;
1988 ln
->ln_expire
= time_second
+ nd6_delay
;
1992 * If the neighbor cache entry has a state other than INCOMPLETE
1993 * (i.e. its link-layer address is already reloved), just
1996 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
)
2000 * There is a neighbor cache entry, but no ethernet address
2001 * response yet. Replace the held mbuf (if any) with this
2004 * XXX Does the code conform to rate-limiting rule?
2007 if (ln
->ln_state
== ND6_LLINFO_WAITDELETE
||
2008 ln
->ln_state
== ND6_LLINFO_NOSTATE
)
2009 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
2011 m_freem(ln
->ln_hold
);
2013 if (ln
->ln_expire
) {
2014 rt
->rt_flags
&= ~RTF_REJECT
;
2015 if (ln
->ln_asked
< nd6_mmaxtries
&&
2016 ln
->ln_expire
< time_second
) {
2018 ln
->ln_expire
= time_second
+
2019 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
2020 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0);
2027 return (dlil_output(ifptodlt(ifp
, PF_INET6
), m
, rt
, (struct sockaddr
*)dst
, 0));
2029 return((*ifp
->if_output
)(ifp
, m
, (struct sockaddr
*)dst
, rt
));
2040 nd6_storelladdr(ifp
, rt
, m
, dst
, desten
)
2044 struct sockaddr
*dst
;
2047 struct sockaddr_dl
*sdl
;
2049 if (m
->m_flags
& M_MCAST
) {
2050 switch (ifp
->if_type
) {
2053 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
2066 rt
->rt_gateway
->sa_family
!= AF_LINK
) {
2067 printf("nd6_storelladdr: something odd happens\n");
2070 sdl
= SDL(rt
->rt_gateway
);
2071 if (sdl
->sdl_alen
== 0) {
2072 /* this should be impossible, but we bark here for debugging */
2073 printf("nd6_storelladdr: sdl_alen == 0\n");
2077 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);