1 /* $NetBSD: nd6_nbr.c,v 1.92 2009/03/18 16:00:23 cegger Exp $ */
2 /* $KAME: nd6_nbr.c,v 1.61 2001/02/10 16:06:14 jinmei 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
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: nd6_nbr.c,v 1.92 2009/03/18 16:00:23 cegger Exp $");
37 #include "opt_ipsec.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sockio.h>
47 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #include <sys/ioctl.h>
50 #include <sys/syslog.h>
51 #include <sys/queue.h>
52 #include <sys/callout.h>
55 #include <net/if_types.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet6/in6_var.h>
62 #include <netinet6/in6_ifattach.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/scope6_var.h>
66 #include <netinet6/nd6.h>
67 #include <netinet/icmp6.h>
68 #include <netinet6/icmp6_private.h>
71 #include <netinet6/ipsec.h>
76 #include <netinet/ip_carp.h>
79 #include <net/net_osdep.h>
82 static struct dadq
*nd6_dad_find(struct ifaddr
*);
83 static void nd6_dad_starttimer(struct dadq
*, int);
84 static void nd6_dad_stoptimer(struct dadq
*);
85 static void nd6_dad_timer(struct ifaddr
*);
86 static void nd6_dad_ns_output(struct dadq
*, struct ifaddr
*);
87 static void nd6_dad_ns_input(struct ifaddr
*);
88 static void nd6_dad_na_input(struct ifaddr
*);
90 static int dad_ignore_ns
= 0; /* ignore NS in DAD - specwise incorrect*/
91 static int dad_maxtry
= 15; /* max # of *tries* to transmit DAD packet */
94 * Input a Neighbor Solicitation Message.
97 * Based on RFC 2462 (duplicate address detection)
100 nd6_ns_input(struct mbuf
*m
, int off
, int icmp6len
)
102 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
103 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
104 struct nd_neighbor_solicit
*nd_ns
;
105 struct in6_addr saddr6
= ip6
->ip6_src
;
106 struct in6_addr daddr6
= ip6
->ip6_dst
;
107 struct in6_addr taddr6
;
108 struct in6_addr myaddr6
;
112 int anycast
= 0, proxy
= 0, tentative
= 0;
113 int router
= ip6_forwarding
;
115 union nd_opts ndopts
;
116 const struct sockaddr_dl
*proxydl
= NULL
;
118 IP6_EXTHDR_GET(nd_ns
, struct nd_neighbor_solicit
*, m
, off
, icmp6len
);
120 ICMP6_STATINC(ICMP6_STAT_TOOSHORT
);
123 ip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer for safety */
124 taddr6
= nd_ns
->nd_ns_target
;
125 if (in6_setscope(&taddr6
, ifp
, NULL
) != 0)
128 if (ip6
->ip6_hlim
!= 255) {
130 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
131 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
132 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
136 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
)) {
137 /* dst has to be a solicited node multicast address. */
138 /* don't check ifindex portion */
139 if (daddr6
.s6_addr16
[0] == IPV6_ADDR_INT16_MLL
&&
140 daddr6
.s6_addr32
[1] == 0 &&
141 daddr6
.s6_addr32
[2] == IPV6_ADDR_INT32_ONE
&&
142 daddr6
.s6_addr8
[12] == 0xff) {
145 nd6log((LOG_INFO
, "nd6_ns_input: bad DAD packet "
146 "(wrong ip6 dst)\n"));
151 * Make sure the source address is from a neighbor's address.
153 if (in6ifa_ifplocaladdr(ifp
, &saddr6
) == NULL
) {
154 nd6log((LOG_INFO
, "nd6_ns_input: "
155 "NS packet from non-neighbor\n"));
161 if (IN6_IS_ADDR_MULTICAST(&taddr6
)) {
162 nd6log((LOG_INFO
, "nd6_ns_input: bad NS target (multicast)\n"));
166 icmp6len
-= sizeof(*nd_ns
);
167 nd6_option_init(nd_ns
+ 1, icmp6len
, &ndopts
);
168 if (nd6_options(&ndopts
) < 0) {
170 "nd6_ns_input: invalid ND option, ignored\n"));
171 /* nd6_options have incremented stats */
175 if (ndopts
.nd_opts_src_lladdr
) {
176 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
177 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
180 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) && lladdr
) {
181 nd6log((LOG_INFO
, "nd6_ns_input: bad DAD packet "
182 "(link-layer address option)\n"));
187 * Attaching target link-layer address to the NA?
190 * NS IP dst is multicast MUST add
191 * Otherwise MAY be omitted
193 * In this implementation, we omit the target link-layer address
196 #if 0 /* too much! */
197 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &daddr6
);
198 if (ifa
&& (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
))
202 if (!IN6_IS_ADDR_MULTICAST(&daddr6
))
208 * Target address (taddr6) must be either:
209 * (1) Valid unicast/anycast address for my receiving interface,
210 * (2) Unicast address for which I'm offering proxy service, or
211 * (3) "tentative" address on which DAD is being performed.
213 /* (1) and (3) check. */
215 if (ifp
->if_carp
&& ifp
->if_type
!= IFT_CARP
)
216 ifa
= carp_iamatch6(ifp
->if_carp
, &taddr6
);
220 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
222 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
228 struct sockaddr_in6 tsin6
;
230 sockaddr_in6_init(&tsin6
, &taddr6
, 0, 0, 0);
232 rt
= rtalloc1((struct sockaddr
*)&tsin6
, 0);
233 if (rt
&& (rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
234 rt
->rt_gateway
->sa_family
== AF_LINK
) {
236 * proxy NDP for single entry
238 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
239 IN6_IFF_NOTREADY
|IN6_IFF_ANYCAST
);
242 proxydl
= satocsdl(rt
->rt_gateway
);
243 router
= 0; /* XXX */
251 * We've got an NS packet, and we don't have that address
252 * assigned for us. We MUST silently ignore it.
257 myaddr6
= *IFA_IN6(ifa
);
258 anycast
= ((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
;
259 tentative
= ((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_TENTATIVE
;
260 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DUPLICATED
)
263 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
264 nd6log((LOG_INFO
, "nd6_ns_input: lladdrlen mismatch for %s "
265 "(if %d, NS packet %d)\n",
266 ip6_sprintf(&taddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
270 if (IN6_ARE_ADDR_EQUAL(&myaddr6
, &saddr6
)) {
271 nd6log((LOG_INFO
, "nd6_ns_input: duplicate IP6 address %s\n",
272 ip6_sprintf(&saddr6
)));
277 * We have neighbor solicitation packet, with target address equals to
278 * one of my tentative address.
280 * src addr how to process?
282 * multicast of course, invalid (rejected in ip6_input)
283 * unicast somebody is doing address resolution -> ignore
284 * unspec dup address detection
286 * The processing is defined in RFC 2462.
290 * If source address is unspecified address, it is for
291 * duplicate address detection.
293 * If not, the packet is for addess resolution;
294 * silently ignore it.
296 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
))
297 nd6_dad_ns_input(ifa
);
303 * If the source address is unspecified address, entries must not
304 * be created or updated.
305 * It looks that sender is performing DAD. Output NA toward
306 * all-node multicast address, to tell the sender that I'm using
308 * S bit ("solicited") must be zero.
310 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
)) {
311 struct in6_addr in6_all
;
313 in6_all
= in6addr_linklocal_allnodes
;
314 if (in6_setscope(&in6_all
, ifp
, NULL
) != 0)
316 nd6_na_output(ifp
, &in6_all
, &taddr6
,
317 ((anycast
|| proxy
|| !tlladdr
) ? 0 : ND_NA_FLAG_OVERRIDE
) |
318 (ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0),
319 tlladdr
, (const struct sockaddr
*)proxydl
);
323 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_NEIGHBOR_SOLICIT
, 0);
325 nd6_na_output(ifp
, &saddr6
, &taddr6
,
326 ((anycast
|| proxy
|| !tlladdr
) ? 0 : ND_NA_FLAG_OVERRIDE
) |
327 (router
? ND_NA_FLAG_ROUTER
: 0) | ND_NA_FLAG_SOLICITED
,
328 tlladdr
, (const struct sockaddr
*)proxydl
);
334 nd6log((LOG_ERR
, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6
)));
335 nd6log((LOG_ERR
, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6
)));
336 nd6log((LOG_ERR
, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6
)));
337 ICMP6_STATINC(ICMP6_STAT_BADNS
);
342 * Output a Neighbor Solicitation Message. Caller specifies:
343 * - ICMP6 header source IP6 address
344 * - ND6 header target IP6 address
345 * - ND6 header source datalink address
348 * Based on RFC 2462 (duplicate address detection)
351 nd6_ns_output(struct ifnet
*ifp
, const struct in6_addr
*daddr6
,
352 const struct in6_addr
*taddr6
,
353 struct llinfo_nd6
*ln
, /* for source address determination */
354 int dad
/* duplicate address detection */)
358 struct nd_neighbor_solicit
*nd_ns
;
359 struct in6_addr
*src
, src_in
;
360 struct ip6_moptions im6o
;
366 if (IN6_IS_ADDR_MULTICAST(taddr6
))
369 memset(&ro
, 0, sizeof(ro
));
371 /* estimate the size of message */
372 maxlen
= sizeof(*ip6
) + sizeof(*nd_ns
);
373 maxlen
+= (sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7;
375 if (max_linkhdr
+ maxlen
>= MCLBYTES
) {
376 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
377 "(%d + %d > %d)\n", max_linkhdr
, maxlen
, MCLBYTES
);
378 panic("nd6_ns_output: insufficient MCLBYTES");
383 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
384 if (m
&& max_linkhdr
+ maxlen
>= MHLEN
) {
385 MCLGET(m
, M_DONTWAIT
);
386 if ((m
->m_flags
& M_EXT
) == 0) {
393 m
->m_pkthdr
.rcvif
= NULL
;
395 if (daddr6
== NULL
|| IN6_IS_ADDR_MULTICAST(daddr6
)) {
396 m
->m_flags
|= M_MCAST
;
397 im6o
.im6o_multicast_ifp
= ifp
;
398 im6o
.im6o_multicast_hlim
= 255;
399 im6o
.im6o_multicast_loop
= 0;
402 icmp6len
= sizeof(*nd_ns
);
403 m
->m_pkthdr
.len
= m
->m_len
= sizeof(*ip6
) + icmp6len
;
404 m
->m_data
+= max_linkhdr
; /* or MH_ALIGN() equivalent? */
406 /* fill neighbor solicitation packet */
407 ip6
= mtod(m
, struct ip6_hdr
*);
409 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
410 ip6
->ip6_vfc
|= IPV6_VERSION
;
411 /* ip6->ip6_plen will be set later */
412 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
415 ip6
->ip6_dst
= *daddr6
;
417 ip6
->ip6_dst
.s6_addr16
[0] = IPV6_ADDR_INT16_MLL
;
418 ip6
->ip6_dst
.s6_addr16
[1] = 0;
419 ip6
->ip6_dst
.s6_addr32
[1] = 0;
420 ip6
->ip6_dst
.s6_addr32
[2] = IPV6_ADDR_INT32_ONE
;
421 ip6
->ip6_dst
.s6_addr32
[3] = taddr6
->s6_addr32
[3];
422 ip6
->ip6_dst
.s6_addr8
[12] = 0xff;
423 if (in6_setscope(&ip6
->ip6_dst
, ifp
, NULL
) != 0)
429 * "If the source address of the packet prompting the
430 * solicitation is the same as one of the addresses assigned
431 * to the outgoing interface, that address SHOULD be placed
432 * in the IP Source Address of the outgoing solicitation.
433 * Otherwise, any one of the addresses assigned to the
434 * interface should be used."
436 * We use the source address for the prompting packet
438 * - hsrc is given from the caller (by giving "ln"), and
439 * - hsrc belongs to the outgoing interface.
440 * Otherwise, we perform the source address selection as usual.
442 struct ip6_hdr
*hip6
; /* hold ip6 */
443 struct in6_addr
*hsrc
= NULL
;
445 if (ln
&& ln
->ln_hold
) {
447 * assuming every packet in ln_hold has the same IP
450 hip6
= mtod(ln
->ln_hold
, struct ip6_hdr
*);
452 if (sizeof(*hip6
) < ln
->ln_hold
->m_len
)
453 hsrc
= &hip6
->ip6_src
;
457 if (hsrc
&& in6ifa_ifpwithaddr(ifp
, hsrc
))
461 struct sockaddr_in6 dst_sa
;
463 sockaddr_in6_init(&dst_sa
, &ip6
->ip6_dst
, 0, 0, 0);
465 src
= in6_selectsrc(&dst_sa
, NULL
,
466 NULL
, &ro
, NULL
, NULL
, &error
);
469 "nd6_ns_output: source can't be "
470 "determined: dst=%s, error=%d\n",
471 ip6_sprintf(&dst_sa
.sin6_addr
), error
));
477 * Source address for DAD packet must always be IPv6
478 * unspecified address. (0::0)
479 * We actually don't have to 0-clear the address (we did it
480 * above), but we do so here explicitly to make the intention
483 memset(&src_in
, 0, sizeof(src_in
));
487 nd_ns
= (struct nd_neighbor_solicit
*)(ip6
+ 1);
488 nd_ns
->nd_ns_type
= ND_NEIGHBOR_SOLICIT
;
489 nd_ns
->nd_ns_code
= 0;
490 nd_ns
->nd_ns_reserved
= 0;
491 nd_ns
->nd_ns_target
= *taddr6
;
492 in6_clearscope(&nd_ns
->nd_ns_target
); /* XXX */
495 * Add source link-layer address option.
497 * spec implementation
499 * DAD packet MUST NOT do not add the option
500 * there's no link layer address:
501 * impossible do not add the option
502 * there's link layer address:
503 * Multicast NS MUST add one add the option
504 * Unicast NS SHOULD add one add the option
506 if (!dad
&& (mac
= nd6_ifptomac(ifp
))) {
507 int optlen
= sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
;
508 struct nd_opt_hdr
*nd_opt
= (struct nd_opt_hdr
*)(nd_ns
+ 1);
509 /* 8 byte alignments... */
510 optlen
= (optlen
+ 7) & ~7;
512 m
->m_pkthdr
.len
+= optlen
;
515 memset((void *)nd_opt
, 0, optlen
);
516 nd_opt
->nd_opt_type
= ND_OPT_SOURCE_LINKADDR
;
517 nd_opt
->nd_opt_len
= optlen
>> 3;
518 memcpy( (void *)(nd_opt
+ 1), mac
, ifp
->if_addrlen
);
521 ip6
->ip6_plen
= htons((u_int16_t
)icmp6len
);
522 nd_ns
->nd_ns_cksum
= 0;
524 in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), icmp6len
);
526 ip6_output(m
, NULL
, &ro
, dad
? IPV6_UNSPECSRC
: 0, &im6o
, NULL
, NULL
);
527 icmp6_ifstat_inc(ifp
, ifs6_out_msg
);
528 icmp6_ifstat_inc(ifp
, ifs6_out_neighborsolicit
);
529 ICMP6_STATINC(ICMP6_STAT_OUTHIST
+ ND_NEIGHBOR_SOLICIT
);
541 * Neighbor advertisement input handling.
544 * Based on RFC 2462 (duplicate address detection)
546 * the following items are not implemented yet:
547 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
548 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
551 nd6_na_input(struct mbuf
*m
, int off
, int icmp6len
)
553 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
554 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
555 struct nd_neighbor_advert
*nd_na
;
556 struct in6_addr saddr6
= ip6
->ip6_src
;
557 struct in6_addr daddr6
= ip6
->ip6_dst
;
558 struct in6_addr taddr6
;
566 struct llinfo_nd6
*ln
;
568 struct sockaddr_dl
*sdl
;
569 union nd_opts ndopts
;
571 if (ip6
->ip6_hlim
!= 255) {
573 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
574 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
575 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
579 IP6_EXTHDR_GET(nd_na
, struct nd_neighbor_advert
*, m
, off
, icmp6len
);
581 ICMP6_STATINC(ICMP6_STAT_TOOSHORT
);
585 flags
= nd_na
->nd_na_flags_reserved
;
586 is_router
= ((flags
& ND_NA_FLAG_ROUTER
) != 0);
587 is_solicited
= ((flags
& ND_NA_FLAG_SOLICITED
) != 0);
588 is_override
= ((flags
& ND_NA_FLAG_OVERRIDE
) != 0);
590 taddr6
= nd_na
->nd_na_target
;
591 if (in6_setscope(&taddr6
, ifp
, NULL
))
592 return; /* XXX: impossible */
594 if (IN6_IS_ADDR_MULTICAST(&taddr6
)) {
596 "nd6_na_input: invalid target address %s\n",
597 ip6_sprintf(&taddr6
)));
600 if (is_solicited
&& IN6_IS_ADDR_MULTICAST(&daddr6
)) {
602 "nd6_na_input: a solicited adv is multicasted\n"));
606 icmp6len
-= sizeof(*nd_na
);
607 nd6_option_init(nd_na
+ 1, icmp6len
, &ndopts
);
608 if (nd6_options(&ndopts
) < 0) {
610 "nd6_na_input: invalid ND option, ignored\n"));
611 /* nd6_options have incremented stats */
615 if (ndopts
.nd_opts_tgt_lladdr
) {
616 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
617 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
620 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
623 * Target address matches one of my interface address.
625 * If my address is tentative, this means that there's somebody
626 * already using the same address as mine. This indicates DAD failure.
627 * This is defined in RFC 2462.
629 * Otherwise, process as defined in RFC 2461.
632 && (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_TENTATIVE
)) {
633 nd6_dad_na_input(ifa
);
637 /* Just for safety, maybe unnecessary. */
640 "nd6_na_input: duplicate IP6 address %s\n",
641 ip6_sprintf(&taddr6
));
645 * Make sure the source address is from a neighbor's address.
647 if (in6ifa_ifplocaladdr(ifp
, &saddr6
) == NULL
) {
648 nd6log((LOG_INFO
, "nd6_ns_input: "
649 "ND packet from non-neighbor\n"));
653 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
654 nd6log((LOG_INFO
, "nd6_na_input: lladdrlen mismatch for %s "
655 "(if %d, NA packet %d)\n", ip6_sprintf(&taddr6
),
656 ifp
->if_addrlen
, lladdrlen
- 2));
661 * If no neighbor cache entry is found, NA SHOULD silently be
664 rt
= nd6_lookup(&taddr6
, 0, ifp
);
666 ((ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
) == NULL
) ||
667 ((sdl
= satosdl(rt
->rt_gateway
)) == NULL
))
670 if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
672 * If the link-layer has address, and no lladdr option came,
673 * discard the packet.
675 if (ifp
->if_addrlen
&& !lladdr
)
679 * Record link-layer address, and update the state.
681 (void)sockaddr_dl_setaddr(sdl
, sdl
->sdl_len
, lladdr
,
684 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
686 if (!ND6_LLINFO_PERMANENT(ln
)) {
687 nd6_llinfo_settimer(ln
,
688 (long)ND_IFINFO(rt
->rt_ifp
)->reachable
* hz
);
691 ln
->ln_state
= ND6_LLINFO_STALE
;
692 nd6_llinfo_settimer(ln
, (long)nd6_gctimer
* hz
);
694 if ((ln
->ln_router
= is_router
) != 0) {
696 * This means a router's state has changed from
697 * non-reachable to probably reachable, and might
698 * affect the status of associated prefixes..
700 pfxlist_onlink_check();
706 * Check if the link-layer address has changed or not.
712 if (memcmp(lladdr
, CLLADDR(sdl
), ifp
->if_addrlen
))
721 * This is VERY complex. Look at it with care.
723 * override solicit lladdr llchange action
728 * 0 0 y y (1) REACHABLE->STALE
729 * 0 1 n -- (2c) *->REACHABLE
730 * 0 1 y n (2b) L *->REACHABLE
731 * 0 1 y y (1) REACHABLE->STALE
734 * 1 0 y y (2a) L *->STALE
735 * 1 1 n -- (2a) *->REACHABLE
736 * 1 1 y n (2a) L *->REACHABLE
737 * 1 1 y y (2a) L *->REACHABLE
739 if (!is_override
&& lladdr
!= NULL
&& llchange
) { /* (1) */
741 * If state is REACHABLE, make it STALE.
742 * no other updates should be done.
744 if (ln
->ln_state
== ND6_LLINFO_REACHABLE
) {
745 ln
->ln_state
= ND6_LLINFO_STALE
;
746 nd6_llinfo_settimer(ln
, (long)nd6_gctimer
* hz
);
749 } else if (is_override
/* (2a) */
750 || (!is_override
&& lladdr
!= NULL
&& !llchange
) /* (2b) */
751 || lladdr
== NULL
) { /* (2c) */
753 * Update link-local address, if any.
755 if (lladdr
!= NULL
) {
756 (void)sockaddr_dl_setaddr(sdl
, sdl
->sdl_len
,
757 lladdr
, ifp
->if_addrlen
);
761 * If solicited, make the state REACHABLE.
762 * If not solicited and the link-layer address was
763 * changed, make it STALE.
766 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
768 if (!ND6_LLINFO_PERMANENT(ln
)) {
769 nd6_llinfo_settimer(ln
,
770 (long)ND_IFINFO(ifp
)->reachable
* hz
);
773 if (lladdr
&& llchange
) {
774 ln
->ln_state
= ND6_LLINFO_STALE
;
775 nd6_llinfo_settimer(ln
,
776 (long)nd6_gctimer
* hz
);
781 if (ln
->ln_router
&& !is_router
) {
783 * The peer dropped the router flag.
784 * Remove the sender from the Default Router List and
785 * update the Destination Cache entries.
787 struct nd_defrouter
*dr
;
788 const struct in6_addr
*in6
;
791 in6
= &satocsin6(rt_getkey(rt
))->sin6_addr
;
794 * Lock to protect the default router list.
795 * XXX: this might be unnecessary, since this function
796 * is only called under the network software interrupt
797 * context. However, we keep it just for safety.
800 dr
= defrouter_lookup(in6
, rt
->rt_ifp
);
803 else if (!ip6_forwarding
) {
805 * Even if the neighbor is not in the default
806 * router list, the neighbor may be used
807 * as a next hop for some destinations
808 * (e.g. redirect case). So we must
809 * call rt6_flush explicitly.
811 rt6_flush(&ip6
->ip6_src
, rt
->rt_ifp
);
815 ln
->ln_router
= is_router
;
817 rt
->rt_flags
&= ~RTF_REJECT
;
819 nd6_llinfo_release_pkts(ln
, ifp
, rt
);
826 ICMP6_STATINC(ICMP6_STAT_BADNA
);
831 * Neighbor advertisement output handling.
835 * the following items are not implemented yet:
836 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
837 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
842 const struct in6_addr
*daddr6_0
,
843 const struct in6_addr
*taddr6
,
845 int tlladdr
, /* 1 if include target link-layer address */
846 const struct sockaddr
*sdl0
) /* sockaddr_dl (= proxy NA) or NULL */
850 struct nd_neighbor_advert
*nd_na
;
851 struct ip6_moptions im6o
;
852 struct sockaddr
*dst
;
855 struct sockaddr_in6 dst6
;
857 struct in6_addr
*src
, daddr6
;
858 int icmp6len
, maxlen
, error
;
863 memset(&ro
, 0, sizeof(ro
));
865 daddr6
= *daddr6_0
; /* make a local copy for modification */
867 /* estimate the size of message */
868 maxlen
= sizeof(*ip6
) + sizeof(*nd_na
);
869 maxlen
+= (sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7;
871 if (max_linkhdr
+ maxlen
>= MCLBYTES
) {
872 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
873 "(%d + %d > %d)\n", max_linkhdr
, maxlen
, MCLBYTES
);
874 panic("nd6_na_output: insufficient MCLBYTES");
879 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
880 if (m
&& max_linkhdr
+ maxlen
>= MHLEN
) {
881 MCLGET(m
, M_DONTWAIT
);
882 if ((m
->m_flags
& M_EXT
) == 0) {
889 m
->m_pkthdr
.rcvif
= NULL
;
891 if (IN6_IS_ADDR_MULTICAST(&daddr6
)) {
892 m
->m_flags
|= M_MCAST
;
893 im6o
.im6o_multicast_ifp
= ifp
;
894 im6o
.im6o_multicast_hlim
= 255;
895 im6o
.im6o_multicast_loop
= 0;
898 icmp6len
= sizeof(*nd_na
);
899 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct ip6_hdr
) + icmp6len
;
900 m
->m_data
+= max_linkhdr
; /* or MH_ALIGN() equivalent? */
902 /* fill neighbor advertisement packet */
903 ip6
= mtod(m
, struct ip6_hdr
*);
905 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
906 ip6
->ip6_vfc
|= IPV6_VERSION
;
907 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
909 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6
)) {
911 daddr6
.s6_addr16
[0] = IPV6_ADDR_INT16_MLL
;
912 daddr6
.s6_addr16
[1] = 0;
913 daddr6
.s6_addr32
[1] = 0;
914 daddr6
.s6_addr32
[2] = 0;
915 daddr6
.s6_addr32
[3] = IPV6_ADDR_INT32_ONE
;
916 if (in6_setscope(&daddr6
, ifp
, NULL
))
919 flags
&= ~ND_NA_FLAG_SOLICITED
;
921 ip6
->ip6_dst
= daddr6
;
922 sockaddr_in6_init(&u
.dst6
, &daddr6
, 0, 0, 0);
924 rtcache_setdst(&ro
, dst
);
927 * Select a source whose scope is the same as that of the dest.
929 src
= in6_selectsrc(satosin6(dst
), NULL
, NULL
, &ro
, NULL
, NULL
, &error
);
931 nd6log((LOG_DEBUG
, "nd6_na_output: source can't be "
932 "determined: dst=%s, error=%d\n",
933 ip6_sprintf(&satocsin6(dst
)->sin6_addr
), error
));
937 nd_na
= (struct nd_neighbor_advert
*)(ip6
+ 1);
938 nd_na
->nd_na_type
= ND_NEIGHBOR_ADVERT
;
939 nd_na
->nd_na_code
= 0;
940 nd_na
->nd_na_target
= *taddr6
;
941 in6_clearscope(&nd_na
->nd_na_target
); /* XXX */
944 * "tlladdr" indicates NS's condition for adding tlladdr or not.
945 * see nd6_ns_input() for details.
946 * Basically, if NS packet is sent to unicast/anycast addr,
947 * target lladdr option SHOULD NOT be included.
951 * sdl0 != NULL indicates proxy NA. If we do proxy, use
952 * lladdr in sdl0. If we are not proxying (sending NA for
953 * my address) use lladdr configured for the interface.
956 mac
= nd6_ifptomac(ifp
);
957 else if (sdl0
->sa_family
== AF_LINK
) {
958 const struct sockaddr_dl
*sdl
;
959 sdl
= satocsdl(sdl0
);
960 if (sdl
->sdl_alen
== ifp
->if_addrlen
)
964 if (tlladdr
&& mac
) {
965 int optlen
= sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
;
966 struct nd_opt_hdr
*nd_opt
= (struct nd_opt_hdr
*)(nd_na
+ 1);
968 /* roundup to 8 bytes alignment! */
969 optlen
= (optlen
+ 7) & ~7;
971 m
->m_pkthdr
.len
+= optlen
;
974 memset((void *)nd_opt
, 0, optlen
);
975 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
976 nd_opt
->nd_opt_len
= optlen
>> 3;
977 memcpy( (void *)(nd_opt
+ 1), mac
, ifp
->if_addrlen
);
979 flags
&= ~ND_NA_FLAG_OVERRIDE
;
981 ip6
->ip6_plen
= htons((u_int16_t
)icmp6len
);
982 nd_na
->nd_na_flags_reserved
= flags
;
983 nd_na
->nd_na_cksum
= 0;
985 in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(struct ip6_hdr
), icmp6len
);
987 ip6_output(m
, NULL
, NULL
, 0, &im6o
, NULL
, NULL
);
989 icmp6_ifstat_inc(ifp
, ifs6_out_msg
);
990 icmp6_ifstat_inc(ifp
, ifs6_out_neighboradvert
);
991 ICMP6_STATINC(ICMP6_STAT_OUTHIST
+ ND_NEIGHBOR_ADVERT
);
1003 nd6_ifptomac(const struct ifnet
*ifp
)
1005 switch (ifp
->if_type
) {
1010 case IFT_PROPVIRTUAL
:
1014 return CLLADDR(ifp
->if_sadl
);
1020 TAILQ_HEAD(dadq_head
, dadq
);
1022 TAILQ_ENTRY(dadq
) dad_list
;
1023 struct ifaddr
*dad_ifa
;
1024 int dad_count
; /* max NS to send */
1025 int dad_ns_tcount
; /* # of trials to send NS */
1026 int dad_ns_ocount
; /* NS sent so far */
1029 struct callout dad_timer_ch
;
1032 static struct dadq_head dadq
;
1033 static int dad_init
= 0;
1035 static struct dadq
*
1036 nd6_dad_find(struct ifaddr
*ifa
)
1040 TAILQ_FOREACH(dp
, &dadq
, dad_list
) {
1041 if (dp
->dad_ifa
== ifa
)
1048 nd6_dad_starttimer(struct dadq
*dp
, int ticks
)
1051 callout_reset(&dp
->dad_timer_ch
, ticks
,
1052 (void (*)(void *))nd6_dad_timer
, (void *)dp
->dad_ifa
);
1056 nd6_dad_stoptimer(struct dadq
*dp
)
1059 callout_stop(&dp
->dad_timer_ch
);
1063 * Start Duplicate Address Detection (DAD) for specified interface address.
1065 * xtick: minimum delay ticks for IFF_UP event
1068 nd6_dad_start(struct ifaddr
*ifa
, int xtick
)
1070 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1079 * If we don't need DAD, don't do it.
1080 * There are several cases:
1081 * - DAD is disabled (ip6_dad_count == 0)
1082 * - the interface address is anycast
1084 if (!(ia
->ia6_flags
& IN6_IFF_TENTATIVE
)) {
1086 "nd6_dad_start: called with non-tentative address "
1088 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1089 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1092 if (ia
->ia6_flags
& IN6_IFF_ANYCAST
) {
1093 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1096 if (!ip6_dad_count
) {
1097 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1100 if (ifa
->ifa_ifp
== NULL
)
1101 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1102 if (!(ifa
->ifa_ifp
->if_flags
& IFF_UP
))
1104 if (nd6_dad_find(ifa
) != NULL
) {
1105 /* DAD already in progress */
1109 dp
= malloc(sizeof(*dp
), M_IP6NDP
, M_NOWAIT
);
1111 log(LOG_ERR
, "nd6_dad_start: memory allocation failed for "
1113 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1114 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1117 memset(dp
, 0, sizeof(*dp
));
1118 callout_init(&dp
->dad_timer_ch
, CALLOUT_MPSAFE
);
1119 TAILQ_INSERT_TAIL(&dadq
, (struct dadq
*)dp
, dad_list
);
1121 nd6log((LOG_DEBUG
, "%s: starting DAD for %s\n", if_name(ifa
->ifa_ifp
),
1122 ip6_sprintf(&ia
->ia_addr
.sin6_addr
)));
1125 * Send NS packet for DAD, ip6_dad_count times.
1126 * Note that we must delay the first transmission, if this is the
1127 * first packet to be sent from the interface after interface
1128 * (re)initialization.
1131 IFAREF(ifa
); /* just for safety */
1132 dp
->dad_count
= ip6_dad_count
;
1133 dp
->dad_ns_icount
= dp
->dad_na_icount
= 0;
1134 dp
->dad_ns_ocount
= dp
->dad_ns_tcount
= 0;
1136 nd6_dad_ns_output(dp
, ifa
);
1137 nd6_dad_starttimer(dp
,
1138 (long)ND_IFINFO(ifa
->ifa_ifp
)->retrans
* hz
/ 1000);
1140 nd6_dad_starttimer(dp
, xtick
);
1144 * terminate DAD unconditionally. used for address removals.
1147 nd6_dad_stop(struct ifaddr
*ifa
)
1153 dp
= nd6_dad_find(ifa
);
1155 /* DAD wasn't started yet */
1159 nd6_dad_stoptimer(dp
);
1161 TAILQ_REMOVE(&dadq
, dp
, dad_list
);
1168 nd6_dad_timer(struct ifaddr
*ifa
)
1170 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1173 mutex_enter(softnet_lock
);
1174 KERNEL_LOCK(1, NULL
);
1178 log(LOG_ERR
, "nd6_dad_timer: called with null parameter\n");
1181 dp
= nd6_dad_find(ifa
);
1183 log(LOG_ERR
, "nd6_dad_timer: DAD structure not found\n");
1186 if (ia
->ia6_flags
& IN6_IFF_DUPLICATED
) {
1187 log(LOG_ERR
, "nd6_dad_timer: called with duplicate address "
1189 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1190 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1193 if ((ia
->ia6_flags
& IN6_IFF_TENTATIVE
) == 0) {
1194 log(LOG_ERR
, "nd6_dad_timer: called with non-tentative address "
1196 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1197 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1201 /* timeouted with IFF_{RUNNING,UP} check */
1202 if (dp
->dad_ns_tcount
> dad_maxtry
) {
1203 nd6log((LOG_INFO
, "%s: could not run DAD, driver problem?\n",
1204 if_name(ifa
->ifa_ifp
)));
1206 TAILQ_REMOVE(&dadq
, dp
, dad_list
);
1213 /* Need more checks? */
1214 if (dp
->dad_ns_ocount
< dp
->dad_count
) {
1216 * We have more NS to go. Send NS packet for DAD.
1218 nd6_dad_ns_output(dp
, ifa
);
1219 nd6_dad_starttimer(dp
,
1220 (long)ND_IFINFO(ifa
->ifa_ifp
)->retrans
* hz
/ 1000);
1223 * We have transmitted sufficient number of DAD packets.
1224 * See what we've got.
1230 if (dp
->dad_na_icount
) {
1232 * the check is in nd6_dad_na_input(),
1238 if (dp
->dad_ns_icount
) {
1239 /* We've seen NS, means DAD has failed. */
1244 /* (*dp) will be freed in nd6_dad_duplicated() */
1246 nd6_dad_duplicated(ifa
);
1249 * We are done with DAD. No NA came, no NS came.
1250 * No duplicate address found.
1252 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1255 "%s: DAD complete for %s - no duplicates found\n",
1256 if_name(ifa
->ifa_ifp
),
1257 ip6_sprintf(&ia
->ia_addr
.sin6_addr
)));
1259 TAILQ_REMOVE(&dadq
, dp
, dad_list
);
1267 KERNEL_UNLOCK_ONE(NULL
);
1268 mutex_exit(softnet_lock
);
1272 nd6_dad_duplicated(struct ifaddr
*ifa
)
1274 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1278 dp
= nd6_dad_find(ifa
);
1280 log(LOG_ERR
, "nd6_dad_duplicated: DAD structure not found\n");
1285 log(LOG_ERR
, "%s: DAD detected duplicate IPv6 address %s: "
1286 "NS in/out=%d/%d, NA in=%d\n",
1287 if_name(ifp
), ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1288 dp
->dad_ns_icount
, dp
->dad_ns_ocount
, dp
->dad_na_icount
);
1290 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1291 ia
->ia6_flags
|= IN6_IFF_DUPLICATED
;
1293 /* We are done with DAD, with duplicated address found. (failure) */
1294 nd6_dad_stoptimer(dp
);
1296 log(LOG_ERR
, "%s: DAD complete for %s - duplicate found\n",
1297 if_name(ifp
), ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1298 log(LOG_ERR
, "%s: manual intervention required\n",
1302 * If the address is a link-local address formed from an interface
1303 * identifier based on the hardware address which is supposed to be
1304 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1305 * operation on the interface SHOULD be disabled.
1306 * [rfc2462bis-03 Section 5.4.5]
1308 if (IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
1309 struct in6_addr in6
;
1312 * To avoid over-reaction, we only apply this logic when we are
1313 * very sure that hardware addresses are supposed to be unique.
1315 switch (ifp
->if_type
) {
1320 #ifdef IFT_IEEE80211
1323 in6
= ia
->ia_addr
.sin6_addr
;
1324 if (in6_get_hw_ifid(ifp
, &in6
) == 0 &&
1325 IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, &in6
)) {
1326 ND_IFINFO(ifp
)->flags
|= ND6_IFF_IFDISABLED
;
1327 log(LOG_ERR
, "%s: possible hardware address "
1328 "duplication detected, disable IPv6\n",
1335 TAILQ_REMOVE(&dadq
, dp
, dad_list
);
1342 nd6_dad_ns_output(struct dadq
*dp
, struct ifaddr
*ifa
)
1344 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1345 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1347 dp
->dad_ns_tcount
++;
1348 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1350 printf("%s: interface down?\n", if_name(ifp
));
1354 if ((ifp
->if_flags
& IFF_RUNNING
) == 0) {
1356 printf("%s: interface not running?\n", if_name(ifp
));
1361 dp
->dad_ns_tcount
= 0;
1362 dp
->dad_ns_ocount
++;
1363 nd6_ns_output(ifp
, NULL
, &ia
->ia_addr
.sin6_addr
, NULL
, 1);
1367 nd6_dad_ns_input(struct ifaddr
*ifa
)
1369 struct in6_ifaddr
*ia
;
1370 const struct in6_addr
*taddr6
;
1375 panic("ifa == NULL in nd6_dad_ns_input");
1377 ia
= (struct in6_ifaddr
*)ifa
;
1378 taddr6
= &ia
->ia_addr
.sin6_addr
;
1380 dp
= nd6_dad_find(ifa
);
1382 /* Quickhack - completely ignore DAD NS packets */
1383 if (dad_ignore_ns
) {
1385 "nd6_dad_ns_input: ignoring DAD NS packet for "
1386 "address %s(%s)\n", ip6_sprintf(taddr6
),
1387 if_name(ifa
->ifa_ifp
)));
1392 * if I'm yet to start DAD, someone else started using this address
1393 * first. I have a duplicate and you win.
1395 if (dp
== NULL
|| dp
->dad_ns_ocount
== 0)
1398 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1401 dp
= NULL
; /* will be freed in nd6_dad_duplicated() */
1402 nd6_dad_duplicated(ifa
);
1405 * not sure if I got a duplicate.
1406 * increment ns count and see what happens.
1409 dp
->dad_ns_icount
++;
1414 nd6_dad_na_input(struct ifaddr
*ifa
)
1419 panic("ifa == NULL in nd6_dad_na_input");
1421 dp
= nd6_dad_find(ifa
);
1423 dp
->dad_na_icount
++;
1425 /* remove the address. */
1426 nd6_dad_duplicated(ifa
);