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 *));
125 void (*mip6_expired_defrouter_hook
)(struct nd_defrouter
*dr
) = 0;
131 static int nd6_init_done
= 0;
135 log(LOG_NOTICE
, "nd6_init called more than once(ignored)\n");
139 all1_sa
.sin6_family
= AF_INET6
;
140 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
141 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++)
142 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
144 /* initialization of the default router list */
145 TAILQ_INIT(&nd_defrouter
);
150 timeout(nd6_slowtimo
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
157 static size_t if_indexlim
= 8;
160 * We have some arrays that should be indexed by if_index.
161 * since if_index will grow dynamically, they should grow too.
163 if (nd_ifinfo
== NULL
|| if_index
>= if_indexlim
) {
167 while (if_index
>= if_indexlim
)
171 n
= if_indexlim
* sizeof(struct nd_ifinfo
);
172 q
= (caddr_t
)_MALLOC(n
, M_IP6NDP
, M_WAITOK
);
175 bcopy((caddr_t
)nd_ifinfo
, q
, n
/2);
176 _FREE((caddr_t
)nd_ifinfo
, M_IP6NDP
);
178 nd_ifinfo
= (struct nd_ifinfo
*)q
;
181 #define ND nd_ifinfo[ifp->if_index]
182 ND
.linkmtu
= ifindex2ifnet
[ifp
->if_index
]->if_mtu
;
183 ND
.chlim
= IPV6_DEFHLIM
;
184 ND
.basereachable
= REACHABLE_TIME
;
185 ND
.reachable
= ND_COMPUTE_RTIME(ND
.basereachable
);
186 ND
.retrans
= RETRANS_TIMER
;
188 ND
.flags
= ND6_IFF_PERFORMNUD
;
194 * Reset ND level link MTU. This function is called when the physical MTU
195 * changes, which means we might have to adjust the ND level MTU.
201 struct nd_ifinfo
*ndi
= &nd_ifinfo
[ifp
->if_index
];
202 u_long oldmaxmtu
= ndi
->maxmtu
;
203 u_long oldlinkmtu
= ndi
->linkmtu
;
205 switch(ifp
->if_type
) {
206 case IFT_ARCNET
: /* XXX MTU handling needs more work */
207 ndi
->maxmtu
= MIN(60480, ifp
->if_mtu
);
210 ndi
->maxmtu
= MIN(ETHERMTU
, ifp
->if_mtu
);
212 #if defined(__FreeBSD__) || defined(__bsdi__)
214 ndi
->maxmtu
= MIN(FDDIIPMTU
, ifp
->if_mtu
);
217 #if !(defined(__bsdi__) && _BSDI_VERSION >= 199802) && !defined (__APPLE__)
219 ndi
->maxmtu
= MIN(ATMMTU
, ifp
->if_mtu
);
223 ndi
->maxmtu
= ifp
->if_mtu
;
227 if (oldmaxmtu
!= ndi
->maxmtu
) {
229 * If the ND level MTU is not set yet, or if the maxmtu
230 * is reset to a smaller value than the ND level MTU,
231 * also reset the ND level MTU.
233 if (ndi
->linkmtu
== 0 ||
234 ndi
->maxmtu
< ndi
->linkmtu
) {
235 ndi
->linkmtu
= ndi
->maxmtu
;
236 /* also adjust in6_maxmtu if necessary. */
237 if (oldlinkmtu
== 0) {
239 * XXX: the case analysis is grotty, but
240 * it is not efficient to call in6_setmaxmtu()
241 * here when we are during the initialization
244 if (in6_maxmtu
< ndi
->linkmtu
)
245 in6_maxmtu
= ndi
->linkmtu
;
255 nd6_option_init(opt
, icmp6len
, ndopts
)
258 union nd_opts
*ndopts
;
260 bzero(ndopts
, sizeof(*ndopts
));
261 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
263 = (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
266 ndopts
->nd_opts_done
= 1;
267 ndopts
->nd_opts_search
= NULL
;
272 * Take one ND option.
276 union nd_opts
*ndopts
;
278 struct nd_opt_hdr
*nd_opt
;
282 panic("ndopts == NULL in nd6_option\n");
283 if (!ndopts
->nd_opts_last
)
284 panic("uninitialized ndopts in nd6_option\n");
285 if (!ndopts
->nd_opts_search
)
287 if (ndopts
->nd_opts_done
)
290 nd_opt
= ndopts
->nd_opts_search
;
292 olen
= nd_opt
->nd_opt_len
<< 3;
295 * Message validation requires that all included
296 * options have a length that is greater than zero.
298 bzero(ndopts
, sizeof(*ndopts
));
302 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
303 if (!(ndopts
->nd_opts_search
< ndopts
->nd_opts_last
)) {
304 ndopts
->nd_opts_done
= 1;
305 ndopts
->nd_opts_search
= NULL
;
311 * Parse multiple ND options.
312 * This function is much easier to use, for ND routines that do not need
313 * multiple options of the same type.
317 union nd_opts
*ndopts
;
319 struct nd_opt_hdr
*nd_opt
;
323 panic("ndopts == NULL in nd6_options\n");
324 if (!ndopts
->nd_opts_last
)
325 panic("uninitialized ndopts in nd6_options\n");
326 if (!ndopts
->nd_opts_search
)
330 nd_opt
= nd6_option(ndopts
);
331 if (!nd_opt
&& !ndopts
->nd_opts_last
) {
333 * Message validation requires that all included
334 * options have a length that is greater than zero.
336 bzero(ndopts
, sizeof(*ndopts
));
343 switch (nd_opt
->nd_opt_type
) {
344 case ND_OPT_SOURCE_LINKADDR
:
345 case ND_OPT_TARGET_LINKADDR
:
347 case ND_OPT_REDIRECTED_HEADER
:
348 case ND_OPT_ADV_INTERVAL
:
349 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
350 printf("duplicated ND6 option found "
351 "(type=%d)\n", nd_opt
->nd_opt_type
);
354 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
358 case ND_OPT_PREFIX_INFORMATION
:
359 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
360 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
363 ndopts
->nd_opts_pi_end
=
364 (struct nd_opt_prefix_info
*)nd_opt
;
366 case ND_OPT_HA_INFORMATION
:
370 * Unknown options must be silently ignored,
371 * to accomodate future extension to the protocol.
374 "nd6_options: unsupported option %d - "
375 "option ignored\n", nd_opt
->nd_opt_type
);
380 if (i
> nd6_maxndopt
) {
381 icmp6stat
.icp6s_nd_toomanyopt
++;
382 printf("too many loop in nd opt\n");
386 if (ndopts
->nd_opts_done
)
394 * ND6 timer routine to expire default route list and prefix list
397 nd6_timer(ignored_arg
)
401 register struct llinfo_nd6
*ln
;
402 register struct nd_defrouter
*dr
;
403 register struct nd_prefix
*pr
;
404 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
405 long time_second
= time
.tv_sec
;
408 boolean_t funnel_state
;
409 funnel_state
= thread_set_funneled(TRUE
);
417 timeout(nd6_timer
, (caddr_t
)0, nd6_prune
* hz
);
419 ln
= llinfo_nd6
.ln_next
;
420 /* XXX BSD/OS separates this code -- itojun */
421 while (ln
&& ln
!= &llinfo_nd6
) {
424 struct sockaddr_in6
*dst
;
425 struct llinfo_nd6
*next
= ln
->ln_next
;
426 /* XXX: used for the DELAY case only: */
427 struct nd_ifinfo
*ndi
= NULL
;
429 if ((rt
= ln
->ln_rt
) == NULL
) {
433 if ((ifp
= rt
->rt_ifp
) == NULL
) {
437 ndi
= &nd_ifinfo
[ifp
->if_index
];
438 dst
= (struct sockaddr_in6
*)rt_key(rt
);
440 if (ln
->ln_expire
> time_second
) {
447 panic("rt=0 in nd6_timer(ln=%p)\n", ln
);
448 if (rt
->rt_llinfo
&& (struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
)
449 panic("rt_llinfo(%p) is not equal to ln(%p)\n",
452 panic("dst=0 in nd6_timer(ln=%p)\n", ln
);
454 switch (ln
->ln_state
) {
455 case ND6_LLINFO_INCOMPLETE
:
456 if (ln
->ln_asked
< nd6_mmaxtries
) {
458 ln
->ln_expire
= time_second
+
459 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
460 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
,
463 struct mbuf
*m
= ln
->ln_hold
;
467 * Fake rcvif to make ICMP error
468 * more helpful in diagnosing
470 * XXX: should we consider
473 m
->m_pkthdr
.rcvif
= rt
->rt_ifp
;
475 icmp6_error(m
, ICMP6_DST_UNREACH
,
476 ICMP6_DST_UNREACH_ADDR
, 0);
482 case ND6_LLINFO_REACHABLE
:
484 ln
->ln_state
= ND6_LLINFO_STALE
;
487 * ND6_LLINFO_STALE state requires nothing for timer
490 case ND6_LLINFO_DELAY
:
491 if (ndi
&& (ndi
->flags
& ND6_IFF_PERFORMNUD
) != 0) {
494 ln
->ln_state
= ND6_LLINFO_PROBE
;
495 ln
->ln_expire
= time_second
+
497 nd6_ns_output(ifp
, &dst
->sin6_addr
,
502 ln
->ln_state
= ND6_LLINFO_STALE
; /* XXX */
504 case ND6_LLINFO_PROBE
:
505 if (ln
->ln_asked
< nd6_umaxtries
) {
507 ln
->ln_expire
= time_second
+
508 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
509 nd6_ns_output(ifp
, &dst
->sin6_addr
,
510 &dst
->sin6_addr
, ln
, 0);
515 case ND6_LLINFO_WAITDELETE
:
523 dr
= TAILQ_FIRST(&nd_defrouter
);
525 if (dr
->expire
&& dr
->expire
< time_second
) {
526 struct nd_defrouter
*t
;
527 t
= TAILQ_NEXT(dr
, dr_entry
);
532 if (mip6_expired_defrouter_hook
)
533 (*mip6_expired_defrouter_hook
)(dr
);
535 dr
= TAILQ_NEXT(dr
, dr_entry
);
538 pr
= nd_prefix
.lh_first
;
540 struct in6_ifaddr
*ia6
;
541 struct in6_addrlifetime
*lt6
;
543 if (IN6_IS_ADDR_UNSPECIFIED(&pr
->ndpr_addr
))
546 ia6
= in6ifa_ifpwithaddr(pr
->ndpr_ifp
, &pr
->ndpr_addr
);
549 /* check address lifetime */
550 lt6
= &ia6
->ia6_lifetime
;
551 if (lt6
->ia6t_preferred
&& lt6
->ia6t_preferred
< time_second
)
552 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
553 if (lt6
->ia6t_expire
&& lt6
->ia6t_expire
< time_second
) {
554 if (!IN6_IS_ADDR_UNSPECIFIED(&pr
->ndpr_addr
))
555 in6_ifdel(pr
->ndpr_ifp
, &pr
->ndpr_addr
);
556 /* xxx ND_OPT_PI_FLAG_ONLINK processing */
561 * check prefix lifetime.
562 * since pltime is just for autoconf, pltime processing for
563 * prefix is not necessary.
565 * we offset expire time by NDPR_KEEP_EXPIRE, so that we
566 * can use the old prefix information to validate the
567 * next prefix information to come. See prelist_update()
568 * for actual validation.
571 && pr
->ndpr_expire
+ NDPR_KEEP_EXPIRED
< time_second
) {
576 * address expiration and prefix expiration are
577 * separate. NEVER perform in6_ifdel here.
587 (void) thread_set_funneled(funnel_state
);
592 * Nuke neighbor cache/prefix/default router management table, right before
599 struct llinfo_nd6
*ln
, *nln
;
600 struct nd_defrouter
*dr
, *ndr
, drany
;
601 struct nd_prefix
*pr
, *npr
;
603 /* Nuke default router list entries toward ifp */
604 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
606 * The first entry of the list may be stored in
607 * the routing table, so we'll delete it later.
609 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= ndr
) {
610 ndr
= TAILQ_NEXT(dr
, dr_entry
);
614 dr
= TAILQ_FIRST(&nd_defrouter
);
619 /* Nuke prefix list entries toward ifp */
620 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
622 if (pr
->ndpr_ifp
== ifp
) {
623 if (!IN6_IS_ADDR_UNSPECIFIED(&pr
->ndpr_addr
))
624 in6_ifdel(pr
->ndpr_ifp
, &pr
->ndpr_addr
);
629 /* cancel default outgoing interface setting */
630 if (nd6_defifindex
== ifp
->if_index
)
631 nd6_setdefaultiface(0);
633 /* refresh default router list */
634 bzero(&drany
, sizeof(drany
));
635 defrouter_delreq(&drany
, 0);
639 * Nuke neighbor cache entries for the ifp.
640 * Note that rt->rt_ifp may not be the same as ifp,
641 * due to KAME goto ours hack. See RTM_RESOLVE case in
642 * nd6_rtrequest(), and ip6_input().
644 ln
= llinfo_nd6
.ln_next
;
645 while (ln
&& ln
!= &llinfo_nd6
) {
647 struct sockaddr_dl
*sdl
;
651 if (rt
&& rt
->rt_gateway
&&
652 rt
->rt_gateway
->sa_family
== AF_LINK
) {
653 sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
;
654 if (sdl
->sdl_index
== ifp
->if_index
)
661 * Neighbor cache entry for interface route will be retained
662 * with ND6_LLINFO_WAITDELETE state, by nd6_free(). Nuke it.
664 ln
= llinfo_nd6
.ln_next
;
665 while (ln
&& ln
!= &llinfo_nd6
) {
667 struct sockaddr_dl
*sdl
;
671 if (rt
&& rt
->rt_gateway
&&
672 rt
->rt_gateway
->sa_family
== AF_LINK
) {
673 sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
;
674 if (sdl
->sdl_index
== ifp
->if_index
) {
675 rtrequest(RTM_DELETE
, rt_key(rt
),
676 (struct sockaddr
*)0, rt_mask(rt
), 0,
677 (struct rtentry
**)0);
685 nd6_lookup(addr6
, create
, ifp
)
686 struct in6_addr
*addr6
;
691 struct sockaddr_in6 sin6
;
693 bzero(&sin6
, sizeof(sin6
));
694 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
695 sin6
.sin6_family
= AF_INET6
;
696 sin6
.sin6_addr
= *addr6
;
697 rt
= rtalloc1((struct sockaddr
*)&sin6
, create
698 #if __FreeBSD__ || defined (__APPLE__)
700 #endif /*__FreeBSD__*/
702 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) == 0) {
704 * This is the case for the default route.
705 * If we want to create a neighbor cache for the address, we
706 * should free the route for the destination and allocate an
719 * If no route is available and create is set,
720 * we allocate a host route for the destination
721 * and treat it like an interface route.
722 * This hack is necessary for a neighbor which can't
723 * be covered by our own prefix.
726 ifaof_ifpforaddr((struct sockaddr
*)&sin6
, ifp
);
731 * Create a new route. RTF_LLINFO is necessary
732 * to create a Neighbor Cache entry for the
733 * destination in nd6_rtrequest which will be
734 * called in rtequest via ifa->ifa_rtrequest.
736 if ((e
= rtrequest(RTM_ADD
, (struct sockaddr
*)&sin6
,
738 (struct sockaddr
*)&all1_sa
,
740 RTF_HOST
| RTF_LLINFO
) &
744 "nd6_lookup: failed to add route for a "
745 "neighbor(%s), errno=%d\n",
746 ip6_sprintf(addr6
), e
);
750 struct llinfo_nd6
*ln
=
751 (struct llinfo_nd6
*)rt
->rt_llinfo
;
752 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
760 * Validation for the entry.
761 * XXX: we can't use rt->rt_ifp to check for the interface, since
762 * it might be the loopback interface if the entry is for our
763 * own address on a non-loopback interface. Instead, we should
764 * use rt->rt_ifa->ifa_ifp, which would specify the REAL interface.
766 if ((rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
767 rt
->rt_gateway
->sa_family
!= AF_LINK
||
768 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
)) {
770 log(LOG_DEBUG
, "nd6_lookup: failed to lookup %s (if = %s)\n",
771 ip6_sprintf(addr6
), ifp
? if_name(ifp
) : "unspec");
772 /* xxx more logs... kazu */
780 * Detect if a given IPv6 address identifies a neighbor on a given link.
781 * XXX: should take care of the destination of a p2p link?
784 nd6_is_addr_neighbor(addr
, ifp
)
785 struct in6_addr
*addr
;
788 register struct ifaddr
*ifa
;
791 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
792 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
794 /* A link-local address is always a neighbor. */
795 if (IN6_IS_ADDR_LINKLOCAL(addr
))
799 * If the address matches one of our addresses,
800 * it should be a neighbor.
802 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
803 for (ifa
= ifp
->if_addrlist
; ifa
; ifa
= ifa
->ifa_next
)
805 for (ifa
= ifp
->if_addrlist
.tqh_first
;
807 ifa
= ifa
->ifa_list
.tqe_next
)
810 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
813 for (i
= 0; i
< 4; i
++) {
814 if ((IFADDR6(ifa
).s6_addr32
[i
] ^ addr
->s6_addr32
[i
]) &
815 IFMASK6(ifa
).s6_addr32
[i
])
822 * Even if the address matches none of our addresses, it might be
823 * in the neighbor cache.
825 if (nd6_lookup(addr
, 0, ifp
))
834 * Free an nd6 llinfo entry.
840 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
841 struct sockaddr_dl
*sdl
;
842 struct in6_addr in6
= ((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
843 struct nd_defrouter
*dr
;
846 * Clear all destination cache entries for the neighbor.
847 * XXX: is it better to restrict this to hosts?
849 pfctlinput(PRC_HOSTDEAD
, rt_key(rt
));
851 if (!ip6_forwarding
&& ip6_accept_rtadv
) { /* XXX: too restrictive? */
858 dr
= defrouter_lookup(&((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
,
860 if (ln
->ln_router
|| dr
) {
862 * rt6_flush must be called whether or not the neighbor
863 * is in the Default Router List.
864 * See a corresponding comment in nd6_na_input().
866 rt6_flush(&in6
, rt
->rt_ifp
);
871 * Unreachablity of a router might affect the default
872 * router selection and on-link detection of advertised
877 * Temporarily fake the state to choose a new default
878 * router and to perform on-link determination of
879 * prefixes coreectly.
880 * Below the state will be set correctly,
881 * or the entry itself will be deleted.
883 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
885 if (dr
== TAILQ_FIRST(&nd_defrouter
)) {
887 * It is used as the current default router,
888 * so we have to move it to the end of the
889 * list and choose a new one.
890 * XXX: it is not very efficient if this is
893 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
894 TAILQ_INSERT_TAIL(&nd_defrouter
, dr
, dr_entry
);
898 pfxlist_onlink_check();
903 if (rt
->rt_refcnt
> 0 && (sdl
= SDL(rt
->rt_gateway
)) &&
904 sdl
->sdl_family
== AF_LINK
) {
906 ln
->ln_state
= ND6_LLINFO_WAITDELETE
;
908 rt
->rt_flags
&= ~RTF_REJECT
;
912 rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
913 rt_mask(rt
), 0, (struct rtentry
**)0);
917 * Upper-layer reachability hint for Neighbor Unreachability Detection.
919 * XXX cost-effective metods?
922 nd6_nud_hint(rt
, dst6
)
924 struct in6_addr
*dst6
;
926 struct llinfo_nd6
*ln
;
927 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
928 long time_second
= time
.tv_sec
;
932 * If the caller specified "rt", use that. Otherwise, resolve the
933 * routing table by supplied "dst6".
938 if (!(rt
= nd6_lookup(dst6
, 0, NULL
)))
942 if ((rt
->rt_flags
& RTF_GATEWAY
)
943 || (rt
->rt_flags
& RTF_LLINFO
) == 0
946 || rt
->rt_gateway
->sa_family
!= AF_LINK
) {
947 /* This is not a host route. */
951 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
952 if (ln
->ln_state
< ND6_LLINFO_REACHABLE
)
955 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
956 if (ln
->ln_expire
&& nd_ifinfo
)
957 ln
->ln_expire
= time_second
+
958 (rt
->rt_ifp
? nd_ifinfo
[rt
->rt_ifp
->if_index
].reachable
: 0) ;
963 * Resolve an IP6 address into an ethernet address. If success,
964 * desten is filled in. If there is no entry in ndptab,
965 * set one up and multicast a solicitation for the IP6 address.
966 * Hold onto this mbuf and resend it once the address
967 * is finally resolved. A return value of 1 indicates
968 * that desten has been filled in and the packet should be sent
969 * normally; a 0 return indicates that the packet has been
970 * taken over here, either now or for later transmission.
973 nd6_resolve(ifp
, rt
, m
, dst
, desten
)
977 struct sockaddr
*dst
;
980 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)NULL
;
981 struct sockaddr_dl
*sdl
;
982 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
983 long time_second
= time
.tv_sec
;
986 if (m
->m_flags
& M_MCAST
) {
987 switch (ifp
->if_type
) {
990 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
1002 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0)
1003 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1005 if ((rt
= nd6_lookup(&(SIN6(dst
)->sin6_addr
), 1, ifp
)) != NULL
)
1006 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1009 log(LOG_DEBUG
, "nd6_resolve: can't allocate llinfo for %s\n",
1010 ip6_sprintf(&(SIN6(dst
)->sin6_addr
)));
1014 sdl
= SDL(rt
->rt_gateway
);
1016 * Ckeck the address family and length is valid, the address
1017 * is resolved; otherwise, try to resolve.
1019 if (ln
->ln_state
>= ND6_LLINFO_REACHABLE
1020 && sdl
->sdl_family
== AF_LINK
1021 && sdl
->sdl_alen
!= 0) {
1022 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
1023 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1025 ln
->ln_state
= ND6_LLINFO_DELAY
;
1026 ln
->ln_expire
= time_second
+ nd6_delay
;
1031 * There is an ndp entry, but no ethernet address
1032 * response yet. Replace the held mbuf with this
1035 * XXX Does the code conform to rate-limiting rule?
1038 if (ln
->ln_state
== ND6_LLINFO_WAITDELETE
||
1039 ln
->ln_state
== ND6_LLINFO_NOSTATE
)
1040 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1042 m_freem(ln
->ln_hold
);
1044 if (ln
->ln_expire
) {
1045 rt
->rt_flags
&= ~RTF_REJECT
;
1046 if (ln
->ln_asked
< nd6_mmaxtries
&&
1047 ln
->ln_expire
< time_second
) {
1049 ln
->ln_expire
= time_second
+
1050 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
1051 nd6_ns_output(ifp
, NULL
, &(SIN6(dst
)->sin6_addr
),
1057 #endif /* OLDIP6OUTPUT */
1060 #if defined(__bsdi__) && _BSDI_VERSION >= 199802
1061 nd6_rtrequest(req
, rt
, info
)
1064 struct rt_addrinfo
*info
; /* xxx unused */
1066 nd6_rtrequest(req
, rt
, sa
)
1069 struct sockaddr
*sa
; /* xxx unused */
1072 struct sockaddr
*gate
= rt
->rt_gateway
;
1073 struct llinfo_nd6
*ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1074 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
};
1075 struct ifnet
*ifp
= rt
->rt_ifp
;
1077 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
1078 long time_second
= time
.tv_sec
;
1081 if (rt
->rt_flags
& RTF_GATEWAY
)
1087 * There is no backward compatibility :)
1089 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1090 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1091 * rt->rt_flags |= RTF_CLONING;
1093 if (rt
->rt_flags
& (RTF_CLONING
| RTF_LLINFO
)) {
1095 * Case 1: This route should come from
1096 * a route to interface. RTF_LLINFO flag is set
1097 * for a host route whose destination should be
1098 * treated as on-link.
1100 rt_setgate(rt
, rt_key(rt
),
1101 (struct sockaddr
*)&null_sdl
);
1102 gate
= rt
->rt_gateway
;
1103 SDL(gate
)->sdl_type
= ifp
->if_type
;
1104 SDL(gate
)->sdl_index
= ifp
->if_index
;
1106 ln
->ln_expire
= time_second
;
1108 if (ln
&& ln
->ln_expire
== 0) {
1109 /* cludge for desktops */
1111 printf("nd6_request: time.tv_sec is zero; "
1117 if (rt
->rt_flags
& RTF_CLONING
)
1121 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1122 * We don't do that here since llinfo is not ready yet.
1124 * There are also couple of other things to be discussed:
1125 * - unsolicited NA code needs improvement beforehand
1126 * - RFC2461 says we MAY send multicast unsolicited NA
1127 * (7.2.6 paragraph 4), however, it also says that we
1128 * SHOULD provide a mechanism to prevent multicast NA storm.
1129 * we don't have anything like it right now.
1130 * note that the mechanism need a mutual agreement
1131 * between proxies, which means that we need to implement
1132 * a new protocol, or new kludge.
1133 * - from RFC2461 6.2.4, host MUST NOT send unsolicited NA.
1134 * we need to check ip6forwarding before sending it.
1135 * (or should we allow proxy ND configuration only for
1136 * routers? there's no mention about proxy ND from hosts)
1139 /* XXX it does not work */
1140 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1142 &SIN6(rt_key(rt
))->sin6_addr
,
1143 &SIN6(rt_key(rt
))->sin6_addr
,
1144 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1149 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0) {
1151 * Address resolution isn't necessary for a point to
1152 * point link, so we can skip this test for a p2p link.
1154 if (gate
->sa_family
!= AF_LINK
||
1155 gate
->sa_len
< sizeof(null_sdl
)) {
1157 "nd6_rtrequest: bad gateway value\n");
1160 SDL(gate
)->sdl_type
= ifp
->if_type
;
1161 SDL(gate
)->sdl_index
= ifp
->if_index
;
1164 break; /* This happens on a route change */
1166 * Case 2: This route may come from cloning, or a manual route
1167 * add with a LL address.
1169 R_Malloc(ln
, struct llinfo_nd6
*, sizeof(*ln
));
1170 rt
->rt_llinfo
= (caddr_t
)ln
;
1172 log(LOG_DEBUG
, "nd6_rtrequest: malloc failed\n");
1177 Bzero(ln
, sizeof(*ln
));
1179 /* this is required for "ndp" command. - shin */
1180 if (req
== RTM_ADD
) {
1182 * gate should have some valid AF_LINK entry,
1183 * and ln->ln_expire should have some lifetime
1184 * which is specified by ndp command.
1186 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1189 * When req == RTM_RESOLVE, rt is created and
1190 * initialized in rtrequest(), so rt_expire is 0.
1192 ln
->ln_state
= ND6_LLINFO_NOSTATE
;
1193 ln
->ln_expire
= time_second
;
1195 rt
->rt_flags
|= RTF_LLINFO
;
1196 ln
->ln_next
= llinfo_nd6
.ln_next
;
1197 llinfo_nd6
.ln_next
= ln
;
1198 ln
->ln_prev
= &llinfo_nd6
;
1199 ln
->ln_next
->ln_prev
= ln
;
1202 * check if rt_key(rt) is one of my address assigned
1205 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1206 &SIN6(rt_key(rt
))->sin6_addr
);
1208 caddr_t macp
= nd6_ifptomac(ifp
);
1210 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1212 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
1213 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
1215 if (nd6_useloopback
) {
1217 #if _BSDI_VERSION >= 199802
1218 extern struct ifnet
*loifp
;
1219 rt
->rt_ifp
= loifp
; /*XXX*/
1221 extern struct ifnet loif
;
1222 rt
->rt_ifp
= &loif
; /*XXX*/
1224 #else /* non-bsdi */
1225 rt
->rt_ifp
= &loif
[0]; /*XXX*/
1228 * Make sure rt_ifa be equal to the ifaddr
1229 * corresponding to the address.
1230 * We need this because when we refer
1231 * rt_ifa->ia6_flags in ip6_input, we assume
1232 * that the rt_ifa points to the address instead
1233 * of the loopback address.
1235 if (ifa
!= rt
->rt_ifa
) {
1236 rt
->rt_ifa
->ifa_refcnt
--;
1241 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
1243 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1245 /* join solicited node multicast for proxy ND */
1246 if (ifp
->if_flags
& IFF_MULTICAST
) {
1247 struct in6_addr llsol
;
1250 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1251 llsol
.s6_addr16
[0] = htons(0xff02);
1252 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1253 llsol
.s6_addr32
[1] = 0;
1254 llsol
.s6_addr32
[2] = htonl(1);
1255 llsol
.s6_addr8
[12] = 0xff;
1257 (void)in6_addmulti(&llsol
, ifp
, &error
);
1260 "nd6_rtrequest: could not join solicited node multicast (errno=%d)\n", error
);
1268 /* leave from solicited node multicast for proxy ND */
1269 if ((rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
1270 (ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1271 struct in6_addr llsol
;
1272 struct in6_multi
*in6m
;
1274 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
1275 llsol
.s6_addr16
[0] = htons(0xff02);
1276 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1277 llsol
.s6_addr32
[1] = 0;
1278 llsol
.s6_addr32
[2] = htonl(1);
1279 llsol
.s6_addr8
[12] = 0xff;
1281 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1286 ln
->ln_next
->ln_prev
= ln
->ln_prev
;
1287 ln
->ln_prev
->ln_next
= ln
->ln_next
;
1290 rt
->rt_flags
&= ~RTF_LLINFO
;
1292 m_freem(ln
->ln_hold
);
1298 #if defined(__bsdi__) && _BSDI_VERSION >= 199802
1299 nd6_p2p_rtrequest(req
, rt
, info
)
1302 struct rt_addrinfo
*info
; /* xxx unused */
1304 nd6_p2p_rtrequest(req
, rt
, sa
)
1307 struct sockaddr
*sa
; /* xxx unused */
1310 struct sockaddr
*gate
= rt
->rt_gateway
;
1311 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
};
1312 struct ifnet
*ifp
= rt
->rt_ifp
;
1315 if (rt
->rt_flags
& RTF_GATEWAY
)
1321 * There is no backward compatibility :)
1323 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1324 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1325 * rt->rt_flags |= RTF_CLONING;
1327 if (rt
->rt_flags
& RTF_CLONING
) {
1329 * Case 1: This route should come from
1330 * a route to interface.
1332 rt_setgate(rt
, rt_key(rt
),
1333 (struct sockaddr
*)&null_sdl
);
1334 gate
= rt
->rt_gateway
;
1335 SDL(gate
)->sdl_type
= ifp
->if_type
;
1336 SDL(gate
)->sdl_index
= ifp
->if_index
;
1339 /* Announce a new entry if requested. */
1340 if (rt
->rt_flags
& RTF_ANNOUNCE
)
1342 &SIN6(rt_key(rt
))->sin6_addr
,
1343 &SIN6(rt_key(rt
))->sin6_addr
,
1344 ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0,
1349 * check if rt_key(rt) is one of my address assigned
1352 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
1353 &SIN6(rt_key(rt
))->sin6_addr
);
1355 if (nd6_useloopback
) {
1357 #if _BSDI_VERSION >= 199802
1358 extern struct ifnet
*loifp
;
1359 rt
->rt_ifp
= loifp
; /*XXX*/
1361 extern struct ifnet loif
;
1362 rt
->rt_ifp
= &loif
; /*XXX*/
1365 rt
->rt_ifp
= &loif
[0]; /*XXX*/
1374 nd6_ioctl(cmd
, data
, ifp
)
1379 struct in6_drlist
*drl
= (struct in6_drlist
*)data
;
1380 struct in6_prlist
*prl
= (struct in6_prlist
*)data
;
1381 struct in6_ndireq
*ndi
= (struct in6_ndireq
*)data
;
1382 struct in6_nbrinfo
*nbi
= (struct in6_nbrinfo
*)data
;
1383 struct in6_ndifreq
*ndif
= (struct in6_ndifreq
*)data
;
1384 struct nd_defrouter
*dr
, any
;
1385 struct nd_prefix
*pr
;
1387 int i
= 0, error
= 0;
1391 case SIOCGDRLST_IN6
:
1392 bzero(drl
, sizeof(*drl
));
1398 dr
= TAILQ_FIRST(&nd_defrouter
);
1399 while (dr
&& i
< DRLSTSIZ
) {
1400 drl
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
1401 if (IN6_IS_ADDR_LINKLOCAL(&drl
->defrouter
[i
].rtaddr
)) {
1402 /* XXX: need to this hack for KAME stack */
1403 drl
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
1407 "default router list contains a "
1408 "non-linklocal address(%s)\n",
1409 ip6_sprintf(&drl
->defrouter
[i
].rtaddr
));
1411 drl
->defrouter
[i
].flags
= dr
->flags
;
1412 drl
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
1413 drl
->defrouter
[i
].expire
= dr
->expire
;
1414 drl
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
1416 dr
= TAILQ_NEXT(dr
, dr_entry
);
1420 case SIOCGPRLST_IN6
:
1422 * XXX meaning of fields, especialy "raflags", is very
1423 * differnet between RA prefix list and RR/static prefix list.
1424 * how about separating ioctls into two?
1426 bzero(prl
, sizeof(*prl
));
1432 pr
= nd_prefix
.lh_first
;
1433 while (pr
&& i
< PRLSTSIZ
) {
1434 struct nd_pfxrouter
*pfr
;
1437 prl
->prefix
[i
].prefix
= pr
->ndpr_prefix
.sin6_addr
;
1438 prl
->prefix
[i
].raflags
= pr
->ndpr_raf
;
1439 prl
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
1440 prl
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
1441 prl
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
1442 prl
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
1443 prl
->prefix
[i
].expire
= pr
->ndpr_expire
;
1445 pfr
= pr
->ndpr_advrtrs
.lh_first
;
1449 #define RTRADDR prl->prefix[i].advrtr[j]
1450 RTRADDR
= pfr
->router
->rtaddr
;
1451 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
1452 /* XXX: hack for KAME */
1453 RTRADDR
.s6_addr16
[1] = 0;
1457 "a router(%s) advertises "
1459 "non-link local address\n",
1460 ip6_sprintf(&RTRADDR
));
1464 pfr
= pfr
->pfr_next
;
1466 prl
->prefix
[i
].advrtrs
= j
;
1467 prl
->prefix
[i
].origin
= PR_ORIG_RA
;
1473 struct rr_prefix
*rpp
;
1475 for (rpp
= LIST_FIRST(&rr_prefix
); rpp
;
1476 rpp
= LIST_NEXT(rpp
, rp_entry
)) {
1479 prl
->prefix
[i
].prefix
= rpp
->rp_prefix
.sin6_addr
;
1480 prl
->prefix
[i
].raflags
= rpp
->rp_raf
;
1481 prl
->prefix
[i
].prefixlen
= rpp
->rp_plen
;
1482 prl
->prefix
[i
].vltime
= rpp
->rp_vltime
;
1483 prl
->prefix
[i
].pltime
= rpp
->rp_pltime
;
1484 prl
->prefix
[i
].if_index
= rpp
->rp_ifp
->if_index
;
1485 prl
->prefix
[i
].expire
= rpp
->rp_expire
;
1486 prl
->prefix
[i
].advrtrs
= 0;
1487 prl
->prefix
[i
].origin
= rpp
->rp_origin
;
1494 case SIOCGIFINFO_IN6
:
1495 ndi
->ndi
= nd_ifinfo
[ifp
->if_index
];
1497 case SIOCSIFINFO_FLAGS
:
1498 /* XXX: almost all other fields of ndi->ndi is unused */
1499 nd_ifinfo
[ifp
->if_index
].flags
= ndi
->ndi
.flags
;
1501 case SIOCSNDFLUSH_IN6
: /* XXX: the ioctl name is confusing... */
1502 /* flush default router list */
1504 * xxx sumikawa: should not delete route if default
1505 * route equals to the top of default router list
1507 bzero(&any
, sizeof(any
));
1508 defrouter_delreq(&any
, 0);
1510 /* xxx sumikawa: flush prefix list */
1512 case SIOCSPFXFLUSH_IN6
:
1514 /* flush all the prefix advertised by routers */
1515 struct nd_prefix
*pr
, *next
;
1522 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
1523 next
= pr
->ndpr_next
;
1524 if (!IN6_IS_ADDR_UNSPECIFIED(&pr
->ndpr_addr
))
1525 in6_ifdel(pr
->ndpr_ifp
, &pr
->ndpr_addr
);
1531 case SIOCSRTRFLUSH_IN6
:
1533 /* flush all the default routers */
1534 struct nd_defrouter
*dr
, *next
;
1541 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
1543 * The first entry of the list may be stored in
1544 * the routing table, so we'll delete it later.
1546 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
1547 next
= TAILQ_NEXT(dr
, dr_entry
);
1550 defrtrlist_del(TAILQ_FIRST(&nd_defrouter
));
1555 case SIOCGNBRINFO_IN6
:
1557 struct llinfo_nd6
*ln
;
1558 struct in6_addr nb_addr
= nbi
->addr
; /* make local for safety */
1561 * XXX: KAME specific hack for scoped addresses
1562 * XXXX: for other scopes than link-local?
1564 if (IN6_IS_ADDR_LINKLOCAL(&nbi
->addr
) ||
1565 IN6_IS_ADDR_MC_LINKLOCAL(&nbi
->addr
)) {
1566 u_int16_t
*idp
= (u_int16_t
*)&nb_addr
.s6_addr
[2];
1569 *idp
= htons(ifp
->if_index
);
1577 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
)) == NULL
) {
1582 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1583 nbi
->state
= ln
->ln_state
;
1584 nbi
->asked
= ln
->ln_asked
;
1585 nbi
->isrouter
= ln
->ln_router
;
1586 nbi
->expire
= ln
->ln_expire
;
1591 case SIOCGDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1592 ndif
->ifindex
= nd6_defifindex
;
1594 case SIOCSDEFIFACE_IN6
: /* XXX: should be implemented as a sysctl? */
1595 return(nd6_setdefaultiface(ndif
->ifindex
));
1602 * Create neighbor cache entry and cache link-layer address,
1603 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1606 nd6_cache_lladdr(ifp
, from
, lladdr
, lladdrlen
, type
, code
)
1608 struct in6_addr
*from
;
1611 int type
; /* ICMP6 type */
1612 int code
; /* type dependent information */
1614 struct rtentry
*rt
= NULL
;
1615 struct llinfo_nd6
*ln
= NULL
;
1617 struct sockaddr_dl
*sdl
= NULL
;
1622 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
1623 long time_second
= time
.tv_sec
;
1627 panic("ifp == NULL in nd6_cache_lladdr");
1629 panic("from == NULL in nd6_cache_lladdr");
1631 /* nothing must be updated for unspecified address */
1632 if (IN6_IS_ADDR_UNSPECIFIED(from
))
1636 * Validation about ifp->if_addrlen and lladdrlen must be done in
1639 * XXX If the link does not have link-layer adderss, what should
1640 * we do? (ifp->if_addrlen == 0)
1641 * Spec says nothing in sections for RA, RS and NA. There's small
1642 * description on it in NS section (RFC 2461 7.2.3).
1645 rt
= nd6_lookup(from
, 0, ifp
);
1648 /* nothing must be done if there's no lladdr */
1649 if (!lladdr
|| !lladdrlen
)
1653 rt
= nd6_lookup(from
, 1, ifp
);
1660 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
1665 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1668 if (!rt
->rt_gateway
)
1670 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
1672 sdl
= SDL(rt
->rt_gateway
);
1674 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
1675 if (olladdr
&& lladdr
) {
1676 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
1684 * newentry olladdr lladdr llchange (*=record)
1687 * 0 n y -- (3) * STALE
1689 * 0 y y y (5) * STALE
1690 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1691 * 1 -- y -- (7) * STALE
1694 if (lladdr
) { /*(3-5) and (7)*/
1696 * Record source link-layer address
1697 * XXX is it dependent to ifp->if_type?
1699 sdl
->sdl_alen
= ifp
->if_addrlen
;
1700 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
1704 if ((!olladdr
&& lladdr
) /*(3)*/
1705 || (olladdr
&& lladdr
&& llchange
)) { /*(5)*/
1707 newstate
= ND6_LLINFO_STALE
;
1712 if (!lladdr
) /*(6)*/
1713 newstate
= ND6_LLINFO_NOSTATE
;
1715 newstate
= ND6_LLINFO_STALE
;
1720 * Update the state of the neighbor cache.
1722 ln
->ln_state
= newstate
;
1724 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1725 rt
->rt_flags
&= ~RTF_REJECT
;
1728 (*ifp
->if_output
)(ifp
, ln
->ln_hold
,
1731 nd6_output(ifp
, ln
->ln_hold
,
1732 (struct sockaddr_in6
*)rt_key(rt
),
1737 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
1738 /* probe right away */
1739 ln
->ln_expire
= time_second
;
1744 * ICMP6 type dependent behavior.
1746 * NS: clear IsRouter if new entry
1747 * RS: clear IsRouter
1748 * RA: set IsRouter if there's lladdr
1749 * redir: clear IsRouter if new entry
1752 * The spec says that we must set IsRouter in the following cases:
1753 * - If lladdr exist, set IsRouter. This means (1-5).
1754 * - If it is old entry (!newentry), set IsRouter. This means (7).
1755 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1756 * A quetion arises for (1) case. (1) case has no lladdr in the
1757 * neighbor cache, this is similar to (6).
1758 * This case is rare but we figured that we MUST NOT set IsRouter.
1760 * newentry olladdr lladdr llchange NS RS RA redir
1762 * 0 n n -- (1) c ? s
1763 * 0 y n -- (2) c s s
1764 * 0 n y -- (3) c s s
1767 * 1 -- n -- (6) c c c s
1768 * 1 -- y -- (7) c c s c s
1772 switch (type
& 0xff) {
1773 case ND_NEIGHBOR_SOLICIT
:
1775 * New entry must have is_router flag cleared.
1777 if (is_newentry
) /*(6-7)*/
1782 * If the icmp is a redirect to a better router, always set the
1783 * is_router flag. Otherwise, if the entry is newly created,
1784 * clear the flag. [RFC 2461, sec 8.3]
1787 if (code
== ND_REDIRECT_ROUTER
)
1789 else if (is_newentry
) /*(6-7)*/
1792 case ND_ROUTER_SOLICIT
:
1794 * is_router flag must always be cleared.
1798 case ND_ROUTER_ADVERT
:
1800 * Mark an entry with lladdr as a router.
1802 if ((!is_newentry
&& (olladdr
|| lladdr
)) /*(2-5)*/
1803 || (is_newentry
&& lladdr
)) { /*(7)*/
1813 nd6_slowtimo(ignored_arg
)
1818 register struct nd_ifinfo
*nd6if
;
1820 boolean_t funnel_state
;
1821 funnel_state
= thread_set_funneled(TRUE
);
1830 timeout(nd6_slowtimo
, (caddr_t
)0, ND6_SLOWTIMER_INTERVAL
* hz
);
1831 for (i
= 1; i
< if_index
+ 1; i
++) {
1832 nd6if
= &nd_ifinfo
[i
];
1833 if (nd6if
->basereachable
&& /* already initialized */
1834 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
1836 * Since reachable time rarely changes by router
1837 * advertisements, we SHOULD insure that a new random
1838 * value gets recomputed at least once every few hours.
1841 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
1842 nd6if
->reachable
= ND_COMPUTE_RTIME(nd6if
->basereachable
);
1847 (void) thread_set_funneled(funnel_state
);
1852 #define senderr(e) { error = (e); goto bad;}
1854 nd6_output(ifp
, m0
, dst
, rt0
)
1855 register struct ifnet
*ifp
;
1857 struct sockaddr_in6
*dst
;
1858 struct rtentry
*rt0
;
1860 register struct mbuf
*m
= m0
;
1861 register struct rtentry
*rt
= rt0
;
1862 struct llinfo_nd6
*ln
= NULL
;
1864 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined (__APPLE__)
1865 long time_second
= time
.tv_sec
;
1868 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
))
1872 * XXX: we currently do not make neighbor cache on any interface
1873 * other than ARCnet, Ethernet, FDDI and GIF.
1875 * draft-ietf-ngtrans-mech-04.txt says:
1876 * - unidirectional tunnels needs no ND
1878 switch (ifp
->if_type
) {
1882 case IFT_GIF
: /* XXX need more cases? */
1888 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
1889 (nd_ifinfo
[ifp
->if_index
].flags
& ND6_IFF_PERFORMNUD
) == 0)
1893 * next hop determination. This routine is derived from ether_outpout.
1896 if ((rt
->rt_flags
& RTF_UP
) == 0) {
1897 #if __FreeBSD__ || defined (__APPLE__)
1898 if ((rt0
= rt
= rtalloc1((struct sockaddr
*)dst
, 1, 0UL)) !=
1901 if ((rt0
= rt
= rtalloc1((struct sockaddr
*)dst
, 1)) !=
1906 if (rt
->rt_ifp
!= ifp
)
1907 return nd6_output(ifp
, m0
, dst
, rt
); /* XXX: loop care? */
1909 senderr(EHOSTUNREACH
);
1911 if (rt
->rt_flags
& RTF_GATEWAY
) {
1912 if (rt
->rt_gwroute
== 0)
1914 if (((rt
= rt
->rt_gwroute
)->rt_flags
& RTF_UP
) == 0) {
1915 rtfree(rt
); rt
= rt0
;
1916 #if __FreeBSD__ || defined (__APPLE__)
1917 lookup
: rt
->rt_gwroute
= rtalloc1(rt
->rt_gateway
, 1, 0UL);
1919 lookup
: rt
->rt_gwroute
= rtalloc1(rt
->rt_gateway
, 1);
1921 if ((rt
= rt
->rt_gwroute
) == 0)
1922 senderr(EHOSTUNREACH
);
1924 /* the "G" test below also prevents rt == rt0 */
1925 if ((rt
->rt_flags
& RTF_GATEWAY
) ||
1926 (rt
->rt_ifp
!= ifp
)) {
1928 rt0
->rt_gwroute
= 0;
1929 senderr(EHOSTUNREACH
);
1934 if (rt
->rt_flags
& RTF_REJECT
)
1935 senderr(rt
== rt0
? EHOSTDOWN
: EHOSTUNREACH
);
1939 * Address resolution or Neighbor Unreachability Detection
1941 * At this point, the destination of the packet must be a unicast
1942 * or an anycast address(i.e. not a multicast).
1945 /* Look up the neighbor cache for the nexthop */
1946 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0)
1947 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1949 if ((rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
)) != NULL
)
1950 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
1953 log(LOG_DEBUG
, "nd6_output: can't allocate llinfo for %s "
1955 ip6_sprintf(&dst
->sin6_addr
), ln
, rt
);
1956 senderr(EIO
); /* XXX: good error? */
1959 /* We don't have to do link-layer address resolution on a p2p link. */
1960 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
1961 ln
->ln_state
< ND6_LLINFO_REACHABLE
)
1962 ln
->ln_state
= ND6_LLINFO_STALE
;
1965 * The first time we send a packet to a neighbor whose entry is
1966 * STALE, we have to change the state to DELAY and a sets a timer to
1967 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1968 * neighbor unreachability detection on expiration.
1971 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
1973 ln
->ln_state
= ND6_LLINFO_DELAY
;
1974 ln
->ln_expire
= time_second
+ nd6_delay
;
1978 * If the neighbor cache entry has a state other than INCOMPLETE
1979 * (i.e. its link-layer address is already reloved), just
1982 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
)
1986 * There is a neighbor cache entry, but no ethernet address
1987 * response yet. Replace the held mbuf (if any) with this
1990 * XXX Does the code conform to rate-limiting rule?
1993 if (ln
->ln_state
== ND6_LLINFO_WAITDELETE
||
1994 ln
->ln_state
== ND6_LLINFO_NOSTATE
)
1995 ln
->ln_state
= ND6_LLINFO_INCOMPLETE
;
1997 m_freem(ln
->ln_hold
);
1999 if (ln
->ln_expire
) {
2000 rt
->rt_flags
&= ~RTF_REJECT
;
2001 if (ln
->ln_asked
< nd6_mmaxtries
&&
2002 ln
->ln_expire
< time_second
) {
2004 ln
->ln_expire
= time_second
+
2005 nd_ifinfo
[ifp
->if_index
].retrans
/ 1000;
2006 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, 0);
2013 return (dlil_output(ifptodlt(ifp
, PF_INET6
), m
, rt
, (struct sockaddr
*)dst
, 0));
2015 return((*ifp
->if_output
)(ifp
, m
, (struct sockaddr
*)dst
, rt
));
2026 nd6_storelladdr(ifp
, rt
, m
, dst
, desten
)
2030 struct sockaddr
*dst
;
2033 struct sockaddr_dl
*sdl
;
2035 if (m
->m_flags
& M_MCAST
) {
2036 switch (ifp
->if_type
) {
2039 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
,
2052 rt
->rt_gateway
->sa_family
!= AF_LINK
) {
2053 printf("nd6_storelladdr: something odd happens\n");
2056 sdl
= SDL(rt
->rt_gateway
);
2057 if (sdl
->sdl_alen
== 0) {
2058 /* this should be impossible, but we bark here for debugging */
2059 printf("nd6_storelladdr: sdl_alen == 0\n");
2063 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);