1 /* $FreeBSD: src/sys/netinet6/nd6_nbr.c,v 1.4.2.6 2003/01/23 21:06:47 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/nd6_nbr.c,v 1.24 2008/10/03 07:59:20 hasso Exp $ */
3 /* $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "opt_inet6.h"
36 #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/sockio.h>
46 #include <sys/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/syslog.h>
49 #include <sys/queue.h>
50 #include <sys/callout.h>
51 #include <sys/thread2.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet6/in6_var.h>
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet6/nd6.h>
64 #include <netinet/icmp6.h>
67 #include <netinet6/ipsec.h>
69 #include <netinet6/ipsec6.h>
73 #include <net/net_osdep.h>
76 #include <netinet/ip_carp.h>
80 #define SDL(s) ((struct sockaddr_dl *)s)
83 static struct dadq
*nd6_dad_find (struct ifaddr
*);
84 static void nd6_dad_starttimer (struct dadq
*, int);
85 static void nd6_dad_stoptimer (struct dadq
*);
86 static void nd6_dad_timer (struct ifaddr
*);
87 static void nd6_dad_ns_output (struct dadq
*, struct ifaddr
*);
88 static void nd6_dad_ns_input (struct ifaddr
*);
89 static void nd6_dad_na_input (struct ifaddr
*);
91 static int dad_ignore_ns
= 0; /* ignore NS in DAD - specwise incorrect*/
92 static int dad_maxtry
= 15; /* max # of *tries* to transmit DAD packet */
95 * Input an Neighbor Solicitation Message.
98 * Based on RFC 2462 (duplicated address detection)
101 nd6_ns_input(struct mbuf
*m
, int off
, int icmp6len
)
103 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
104 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
105 struct nd_neighbor_solicit
*nd_ns
;
106 struct in6_addr saddr6
= ip6
->ip6_src
;
107 struct in6_addr daddr6
= ip6
->ip6_dst
;
108 struct in6_addr taddr6
;
109 struct in6_addr myaddr6
;
111 struct ifaddr
*ifa
= NULL
;
113 int anycast
= 0, proxy
= 0, tentative
= 0;
115 union nd_opts ndopts
;
116 struct sockaddr_dl
*proxydl
= NULL
;
118 #ifndef PULLDOWN_TEST
119 IP6_EXTHDR_CHECK(m
, off
, icmp6len
,);
120 nd_ns
= (struct nd_neighbor_solicit
*)((caddr_t
)ip6
+ off
);
122 IP6_EXTHDR_GET(nd_ns
, struct nd_neighbor_solicit
*, m
, off
, icmp6len
);
124 icmp6stat
.icp6s_tooshort
++;
128 ip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer for safety */
129 taddr6
= nd_ns
->nd_ns_target
;
131 if (ip6
->ip6_hlim
!= 255) {
133 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
134 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
135 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
139 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
)) {
140 /* dst has to be solicited node multicast address. */
141 if (daddr6
.s6_addr16
[0] == IPV6_ADDR_INT16_MLL
142 /* don't check ifindex portion */
143 && daddr6
.s6_addr32
[1] == 0
144 && daddr6
.s6_addr32
[2] == IPV6_ADDR_INT32_ONE
145 && daddr6
.s6_addr8
[12] == 0xff) {
148 nd6log((LOG_INFO
, "nd6_ns_input: bad DAD packet "
149 "(wrong ip6 dst)\n"));
154 * Make sure the source address is from a neighbor's address.
156 if (in6ifa_ifplocaladdr(ifp
, &saddr6
) == NULL
) {
157 nd6log((LOG_INFO
, "nd6_ns_input: "
158 "NS packet from non-neighbor\n"));
163 if (IN6_IS_ADDR_MULTICAST(&taddr6
)) {
164 nd6log((LOG_INFO
, "nd6_ns_input: bad NS target (multicast)\n"));
168 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6
))
169 taddr6
.s6_addr16
[1] = htons(ifp
->if_index
);
171 icmp6len
-= sizeof(*nd_ns
);
172 nd6_option_init(nd_ns
+ 1, icmp6len
, &ndopts
);
173 if (nd6_options(&ndopts
) < 0) {
175 "nd6_ns_input: invalid ND option, ignored\n"));
176 /* nd6_options have incremented stats */
180 if (ndopts
.nd_opts_src_lladdr
) {
181 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
182 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
185 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) && lladdr
) {
186 nd6log((LOG_INFO
, "nd6_ns_input: bad DAD packet "
187 "(link-layer address option)\n"));
192 * Attaching target link-layer address to the NA?
195 * NS IP dst is unicast/anycast MUST NOT add
196 * NS IP dst is solicited-node multicast MUST add
198 * In implementation, we add target link-layer address by default.
199 * We do not add one in MUST NOT cases.
201 #if 0 /* too much! */
202 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &daddr6
);
203 if (ifa
&& (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
))
207 if (!IN6_IS_ADDR_MULTICAST(&daddr6
))
213 * Target address (taddr6) must be either:
214 * (1) Valid unicast/anycast address for my receiving interface,
215 * (2) Unicast address for which I'm offering proxy service, or
216 * (3) "tentative" address on which DAD is being performed.
218 /* (1) and (3) check. */
221 ifa
= carp_iamatch6(ifp
->if_carp
, &taddr6
);
223 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
225 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
231 struct sockaddr_in6 tsin6
;
233 bzero(&tsin6
, sizeof tsin6
);
234 tsin6
.sin6_len
= sizeof(struct sockaddr_in6
);
235 tsin6
.sin6_family
= AF_INET6
;
236 tsin6
.sin6_addr
= taddr6
;
238 rt
= rtpurelookup((struct sockaddr
*)&tsin6
);
239 if (rt
!= NULL
&& (rt
->rt_flags
& RTF_ANNOUNCE
) &&
240 rt
->rt_gateway
->sa_family
== AF_LINK
) {
242 * proxy NDP for single entry
244 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
245 IN6_IFF_NOTREADY
|IN6_IFF_ANYCAST
);
248 proxydl
= SDL(rt
->rt_gateway
);
256 * We've got an NS packet, and we don't have that adddress
257 * assigned for us. We MUST silently ignore it.
262 myaddr6
= *IFA_IN6(ifa
);
263 anycast
= ((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
;
264 tentative
= ((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_TENTATIVE
;
265 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DUPLICATED
)
268 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
270 "nd6_ns_input: lladdrlen mismatch for %s "
271 "(if %d, NS packet %d)\n",
272 ip6_sprintf(&taddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
276 if (IN6_ARE_ADDR_EQUAL(&myaddr6
, &saddr6
)) {
278 "nd6_ns_input: duplicate IP6 address %s\n",
279 ip6_sprintf(&saddr6
)));
284 * We have neighbor solicitation packet, with target address equals to
285 * one of my tentative address.
287 * src addr how to process?
289 * multicast of course, invalid (rejected in ip6_input)
290 * unicast somebody is doing address resolution -> ignore
291 * unspec dup address detection
293 * The processing is defined in RFC 2462.
297 * If source address is unspecified address, it is for
298 * duplicated address detection.
300 * If not, the packet is for addess resolution;
301 * silently ignore it.
303 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
))
304 nd6_dad_ns_input(ifa
);
310 * If the source address is unspecified address, entries must not
311 * be created or updated.
312 * It looks that sender is performing DAD. Output NA toward
313 * all-node multicast address, to tell the sender that I'm using
315 * S bit ("solicited") must be zero.
317 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
)) {
318 saddr6
= kin6addr_linklocal_allnodes
;
319 saddr6
.s6_addr16
[1] = htons(ifp
->if_index
);
320 nd6_na_output(ifp
, &saddr6
, &taddr6
,
321 ((anycast
|| proxy
|| !tlladdr
)
322 ? 0 : ND_NA_FLAG_OVERRIDE
)
323 | (ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0),
324 tlladdr
, (struct sockaddr
*)proxydl
);
328 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_NEIGHBOR_SOLICIT
, 0);
330 nd6_na_output(ifp
, &saddr6
, &taddr6
,
331 ((anycast
|| proxy
|| !tlladdr
) ? 0 : ND_NA_FLAG_OVERRIDE
)
332 | (ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0)
333 | ND_NA_FLAG_SOLICITED
,
334 tlladdr
, (struct sockaddr
*)proxydl
);
340 nd6log((LOG_ERR
, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6
)));
341 nd6log((LOG_ERR
, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6
)));
342 nd6log((LOG_ERR
, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6
)));
343 icmp6stat
.icp6s_badns
++;
348 * Output an Neighbor Solicitation Message. Caller specifies:
349 * - ICMP6 header source IP6 address
350 * - ND6 header target IP6 address
351 * - ND6 header source datalink address
354 * Based on RFC 2462 (duplicated address detection)
357 nd6_ns_output(struct ifnet
*ifp
, const struct in6_addr
*daddr6
,
358 const struct in6_addr
*taddr6
,
359 struct llinfo_nd6
*ln
, /* for source address determination */
360 int dad
) /* duplicated address detection */
364 struct nd_neighbor_solicit
*nd_ns
;
365 struct in6_ifaddr
*ia
= NULL
;
366 struct ip6_moptions im6o
;
370 struct ifnet
*outif
= NULL
;
372 if (IN6_IS_ADDR_MULTICAST(taddr6
))
375 /* estimate the size of message */
376 maxlen
= sizeof(*ip6
) + sizeof(*nd_ns
);
377 maxlen
+= (sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7;
378 if (max_linkhdr
+ maxlen
>= MCLBYTES
) {
380 kprintf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
381 "(%d + %d > %d)\n", max_linkhdr
, maxlen
, MCLBYTES
);
386 m
= m_getb(max_linkhdr
+ maxlen
, MB_DONTWAIT
, MT_DATA
, M_PKTHDR
);
390 if (daddr6
== NULL
|| IN6_IS_ADDR_MULTICAST(daddr6
)) {
391 m
->m_flags
|= M_MCAST
;
392 im6o
.im6o_multicast_ifp
= ifp
;
393 im6o
.im6o_multicast_hlim
= 255;
394 im6o
.im6o_multicast_loop
= 0;
397 icmp6len
= sizeof(*nd_ns
);
398 m
->m_pkthdr
.len
= m
->m_len
= sizeof(*ip6
) + icmp6len
;
399 m
->m_data
+= max_linkhdr
; /* or MH_ALIGN() equivalent? */
401 /* fill neighbor solicitation packet */
402 ip6
= mtod(m
, struct ip6_hdr
*);
404 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
405 ip6
->ip6_vfc
|= IPV6_VERSION
;
406 /* ip6->ip6_plen will be set later */
407 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
410 ip6
->ip6_dst
= *daddr6
;
412 ip6
->ip6_dst
.s6_addr16
[0] = IPV6_ADDR_INT16_MLL
;
413 ip6
->ip6_dst
.s6_addr16
[1] = htons(ifp
->if_index
);
414 ip6
->ip6_dst
.s6_addr32
[1] = 0;
415 ip6
->ip6_dst
.s6_addr32
[2] = IPV6_ADDR_INT32_ONE
;
416 ip6
->ip6_dst
.s6_addr32
[3] = taddr6
->s6_addr32
[3];
417 ip6
->ip6_dst
.s6_addr8
[12] = 0xff;
420 #if 0 /* KAME way, exact address scope match */
422 * Select a source whose scope is the same as that of the dest.
423 * Typically, the dest is link-local solicitation multicast
424 * (i.e. neighbor discovery) or link-local/global unicast
425 * (i.e. neighbor un-reachability detection).
427 ia
= in6_ifawithifp(ifp
, &ip6
->ip6_dst
);
432 ip6
->ip6_src
= ia
->ia_addr
.sin6_addr
;
433 #else /* spec-wise correct */
436 * "If the source address of the packet prompting the
437 * solicitation is the same as one of the addresses assigned
438 * to the outgoing interface, that address SHOULD be placed
439 * in the IP Source Address of the outgoing solicitation.
440 * Otherwise, any one of the addresses assigned to the
441 * interface should be used."
443 * We use the source address for the prompting packet
445 * - saddr6 is given from the caller (by giving "ln"), and
446 * - saddr6 belongs to the outgoing interface.
447 * Otherwise, we perform a scope-wise match.
449 struct ip6_hdr
*hip6
; /* hold ip6 */
450 struct in6_addr
*saddr6
;
452 if (ln
&& ln
->ln_hold
) {
453 hip6
= mtod(ln
->ln_hold
, struct ip6_hdr
*);
455 if (sizeof(*hip6
) < ln
->ln_hold
->m_len
)
456 saddr6
= &hip6
->ip6_src
;
461 if (saddr6
&& in6ifa_ifpwithaddr(ifp
, saddr6
))
462 bcopy(saddr6
, &ip6
->ip6_src
, sizeof(*saddr6
));
464 ia
= in6_ifawithifp(ifp
, &ip6
->ip6_dst
);
469 ip6
->ip6_src
= ia
->ia_addr
.sin6_addr
;
474 * Source address for DAD packet must always be IPv6
475 * unspecified address. (0::0)
477 bzero(&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
479 nd_ns
= (struct nd_neighbor_solicit
*)(ip6
+ 1);
480 nd_ns
->nd_ns_type
= ND_NEIGHBOR_SOLICIT
;
481 nd_ns
->nd_ns_code
= 0;
482 nd_ns
->nd_ns_reserved
= 0;
483 nd_ns
->nd_ns_target
= *taddr6
;
485 if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns
->nd_ns_target
))
486 nd_ns
->nd_ns_target
.s6_addr16
[1] = 0;
489 * Add source link-layer address option.
491 * spec implementation
493 * DAD packet MUST NOT do not add the option
494 * there's no link layer address:
495 * impossible do not add the option
496 * there's link layer address:
497 * Multicast NS MUST add one add the option
498 * Unicast NS SHOULD add one add the option
500 if (!dad
&& (mac
= nd6_ifptomac(ifp
))) {
501 int optlen
= sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
;
502 struct nd_opt_hdr
*nd_opt
= (struct nd_opt_hdr
*)(nd_ns
+ 1);
503 /* 8 byte alignments... */
504 optlen
= (optlen
+ 7) & ~7;
506 m
->m_pkthdr
.len
+= optlen
;
509 bzero((caddr_t
)nd_opt
, optlen
);
510 nd_opt
->nd_opt_type
= ND_OPT_SOURCE_LINKADDR
;
511 nd_opt
->nd_opt_len
= optlen
>> 3;
512 bcopy(mac
, (caddr_t
)(nd_opt
+ 1), ifp
->if_addrlen
);
515 ip6
->ip6_plen
= htons((u_short
)icmp6len
);
516 nd_ns
->nd_ns_cksum
= 0;
518 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), icmp6len
);
520 ip6_output(m
, NULL
, NULL
, dad
? IPV6_DADOUTPUT
: 0, &im6o
, &outif
, NULL
);
522 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
523 icmp6_ifstat_inc(outif
, ifs6_out_neighborsolicit
);
525 icmp6stat
.icp6s_outhist
[ND_NEIGHBOR_SOLICIT
]++;
529 * Neighbor advertisement input handling.
532 * Based on RFC 2462 (duplicated address detection)
534 * the following items are not implemented yet:
535 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
536 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
539 nd6_na_input(struct mbuf
*m
, int off
, int icmp6len
)
541 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
542 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
543 struct nd_neighbor_advert
*nd_na
;
544 struct in6_addr saddr6
= ip6
->ip6_src
;
545 struct in6_addr daddr6
= ip6
->ip6_dst
;
546 struct in6_addr taddr6
;
554 struct llinfo_nd6
*ln
;
556 struct sockaddr_dl
*sdl
;
557 union nd_opts ndopts
;
559 if (ip6
->ip6_hlim
!= 255) {
561 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
562 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
563 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
567 #ifndef PULLDOWN_TEST
568 IP6_EXTHDR_CHECK(m
, off
, icmp6len
,);
569 nd_na
= (struct nd_neighbor_advert
*)((caddr_t
)ip6
+ off
);
571 IP6_EXTHDR_GET(nd_na
, struct nd_neighbor_advert
*, m
, off
, icmp6len
);
573 icmp6stat
.icp6s_tooshort
++;
577 taddr6
= nd_na
->nd_na_target
;
578 flags
= nd_na
->nd_na_flags_reserved
;
579 is_router
= ((flags
& ND_NA_FLAG_ROUTER
) != 0);
580 is_solicited
= ((flags
& ND_NA_FLAG_SOLICITED
) != 0);
581 is_override
= ((flags
& ND_NA_FLAG_OVERRIDE
) != 0);
583 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6
))
584 taddr6
.s6_addr16
[1] = htons(ifp
->if_index
);
586 if (IN6_IS_ADDR_MULTICAST(&taddr6
)) {
588 "nd6_na_input: invalid target address %s\n",
589 ip6_sprintf(&taddr6
)));
592 if (IN6_IS_ADDR_MULTICAST(&daddr6
))
595 "nd6_na_input: a solicited adv is multicasted\n"));
599 icmp6len
-= sizeof(*nd_na
);
600 nd6_option_init(nd_na
+ 1, icmp6len
, &ndopts
);
601 if (nd6_options(&ndopts
) < 0) {
603 "nd6_na_input: invalid ND option, ignored\n"));
604 /* nd6_options have incremented stats */
608 if (ndopts
.nd_opts_tgt_lladdr
) {
609 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
610 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
613 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
616 * Target address matches one of my interface address.
618 * If my address is tentative, this means that there's somebody
619 * already using the same address as mine. This indicates DAD failure.
620 * This is defined in RFC 2462.
622 * Otherwise, process as defined in RFC 2461.
625 && (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_TENTATIVE
)) {
626 nd6_dad_na_input(ifa
);
630 /* Just for safety, maybe unnecessary. */
633 "nd6_na_input: duplicate IP6 address %s\n",
634 ip6_sprintf(&taddr6
));
639 * Make sure the source address is from a neighbor's address.
641 if (in6ifa_ifplocaladdr(ifp
, &saddr6
) == NULL
) {
642 nd6log((LOG_INFO
, "nd6_na_input: "
643 "NA packet from non-neighbor\n"));
647 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
649 "nd6_na_input: lladdrlen mismatch for %s "
650 "(if %d, NA packet %d)\n",
651 ip6_sprintf(&taddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
656 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
658 rt
= nd6_lookup(&taddr6
, 0, ifp
);
660 ((ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
) == NULL
) ||
661 ((sdl
= SDL(rt
->rt_gateway
)) == NULL
))
664 if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
666 * If the link-layer has address, and no lladdr option came,
667 * discard the packet.
669 if (ifp
->if_addrlen
&& !lladdr
)
673 * Record link-layer address, and update the state.
675 sdl
->sdl_alen
= ifp
->if_addrlen
;
676 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
678 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
681 ln
->ln_expire
= time_second
+
682 ND_IFINFO(rt
->rt_ifp
)->reachable
;
684 ln
->ln_state
= ND6_LLINFO_STALE
;
685 ln
->ln_expire
= time_second
+ nd6_gctimer
;
687 if ((ln
->ln_router
= is_router
) != 0) {
689 * This means a router's state has changed from
690 * non-reachable to probably reachable, and might
691 * affect the status of associated prefixes..
693 pfxlist_onlink_check();
699 * Check if the link-layer address has changed or not.
705 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
714 * This is VERY complex. Look at it with care.
716 * override solicit lladdr llchange action
721 * 0 0 y y (1) REACHABLE->STALE
722 * 0 1 n -- (2c) *->REACHABLE
723 * 0 1 y n (2b) L *->REACHABLE
724 * 0 1 y y (1) REACHABLE->STALE
727 * 1 0 y y (2a) L *->STALE
728 * 1 1 n -- (2a) *->REACHABLE
729 * 1 1 y n (2a) L *->REACHABLE
730 * 1 1 y y (2a) L *->REACHABLE
732 if (!is_override
&& (lladdr
&& llchange
)) { /* (1) */
734 * If state is REACHABLE, make it STALE.
735 * no other updates should be done.
737 if (ln
->ln_state
== ND6_LLINFO_REACHABLE
) {
738 ln
->ln_state
= ND6_LLINFO_STALE
;
739 ln
->ln_expire
= time_second
+ nd6_gctimer
;
742 } else if (is_override
/* (2a) */
743 || (!is_override
&& (lladdr
&& !llchange
)) /* (2b) */
744 || !lladdr
) { /* (2c) */
746 * Update link-local address, if any.
749 sdl
->sdl_alen
= ifp
->if_addrlen
;
750 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
754 * If solicited, make the state REACHABLE.
755 * If not solicited and the link-layer address was
756 * changed, make it STALE.
759 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
762 ln
->ln_expire
= time_second
+
763 ND_IFINFO(ifp
)->reachable
;
766 if (lladdr
&& llchange
) {
767 ln
->ln_state
= ND6_LLINFO_STALE
;
768 ln
->ln_expire
= time_second
+ nd6_gctimer
;
773 if (ln
->ln_router
&& !is_router
) {
775 * The peer dropped the router flag.
776 * Remove the sender from the Default Router List and
777 * update the Destination Cache entries.
779 struct nd_defrouter
*dr
;
780 struct in6_addr
*in6
;
782 in6
= &((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
785 * Lock to protect the default router list.
786 * XXX: this might be unnecessary, since this function
787 * is only called under the network software interrupt
788 * context. However, we keep it just for safety.
791 dr
= defrouter_lookup(in6
, rt
->rt_ifp
);
794 else if (!ip6_forwarding
&& ip6_accept_rtadv
) {
796 * Even if the neighbor is not in the default
797 * router list, the neighbor may be used
798 * as a next hop for some destinations
799 * (e.g. redirect case). So we must
800 * call rt6_flush explicitly.
802 rt6_flush(&ip6
->ip6_src
, rt
->rt_ifp
);
806 ln
->ln_router
= is_router
;
808 rt
->rt_flags
&= ~RTF_REJECT
;
812 * we assume ifp is not a loopback here, so just set the 2nd
813 * argument as the 1st one.
815 nd6_output(ifp
, ifp
, ln
->ln_hold
,
816 (struct sockaddr_in6
*)rt_key(rt
), rt
);
825 icmp6stat
.icp6s_badna
++;
830 * Neighbor advertisement output handling.
834 * the following items are not implemented yet:
835 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
836 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
839 nd6_na_output(struct ifnet
*ifp
, const struct in6_addr
*daddr6
,
840 const struct in6_addr
*taddr6
, u_long flags
,
841 int tlladdr
, /* 1 if include target link-layer address */
842 struct sockaddr
*sdl0
) /* sockaddr_dl (= proxy NA) or NULL */
846 struct nd_neighbor_advert
*nd_na
;
847 struct in6_ifaddr
*ia
= NULL
;
848 struct ip6_moptions im6o
;
852 struct ifnet
*outif
= NULL
;
854 /* estimate the size of message */
855 maxlen
= sizeof(*ip6
) + sizeof(*nd_na
);
856 maxlen
+= (sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7;
857 if (max_linkhdr
+ maxlen
>= MCLBYTES
) {
859 kprintf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
860 "(%d + %d > %d)\n", max_linkhdr
, maxlen
, MCLBYTES
);
865 m
= m_getb(max_linkhdr
+ maxlen
, MB_DONTWAIT
, MT_DATA
, M_PKTHDR
);
869 if (IN6_IS_ADDR_MULTICAST(daddr6
)) {
870 m
->m_flags
|= M_MCAST
;
871 im6o
.im6o_multicast_ifp
= ifp
;
872 im6o
.im6o_multicast_hlim
= 255;
873 im6o
.im6o_multicast_loop
= 0;
876 icmp6len
= sizeof(*nd_na
);
877 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct ip6_hdr
) + icmp6len
;
878 m
->m_data
+= max_linkhdr
; /* or MH_ALIGN() equivalent? */
880 /* fill neighbor advertisement packet */
881 ip6
= mtod(m
, struct ip6_hdr
*);
883 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
884 ip6
->ip6_vfc
|= IPV6_VERSION
;
885 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
887 if (IN6_IS_ADDR_UNSPECIFIED(daddr6
)) {
889 ip6
->ip6_dst
.s6_addr16
[0] = IPV6_ADDR_INT16_MLL
;
890 ip6
->ip6_dst
.s6_addr16
[1] = htons(ifp
->if_index
);
891 ip6
->ip6_dst
.s6_addr32
[1] = 0;
892 ip6
->ip6_dst
.s6_addr32
[2] = 0;
893 ip6
->ip6_dst
.s6_addr32
[3] = IPV6_ADDR_INT32_ONE
;
894 flags
&= ~ND_NA_FLAG_SOLICITED
;
896 ip6
->ip6_dst
= *daddr6
;
899 * Select a source whose scope is the same as that of the dest.
901 ia
= in6_ifawithifp(ifp
, &ip6
->ip6_dst
);
906 ip6
->ip6_src
= ia
->ia_addr
.sin6_addr
;
907 nd_na
= (struct nd_neighbor_advert
*)(ip6
+ 1);
908 nd_na
->nd_na_type
= ND_NEIGHBOR_ADVERT
;
909 nd_na
->nd_na_code
= 0;
910 nd_na
->nd_na_target
= *taddr6
;
911 if (IN6_IS_SCOPE_LINKLOCAL(&nd_na
->nd_na_target
))
912 nd_na
->nd_na_target
.s6_addr16
[1] = 0;
915 * "tlladdr" indicates NS's condition for adding tlladdr or not.
916 * see nd6_ns_input() for details.
917 * Basically, if NS packet is sent to unicast/anycast addr,
918 * target lladdr option SHOULD NOT be included.
923 * sdl0 != NULL indicates proxy NA. If we do proxy, use
924 * lladdr in sdl0. If we are not proxying (sending NA for
925 * my address) use lladdr configured for the interface.
930 mac
= carp_macmatch6(ifp
->if_carp
, m
, taddr6
);
932 mac
= nd6_ifptomac(ifp
);
934 mac
= nd6_ifptomac(ifp
);
936 } else if (sdl0
->sa_family
== AF_LINK
) {
937 struct sockaddr_dl
*sdl
;
938 sdl
= (struct sockaddr_dl
*)sdl0
;
939 if (sdl
->sdl_alen
== ifp
->if_addrlen
)
944 int optlen
= sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
;
945 struct nd_opt_hdr
*nd_opt
= (struct nd_opt_hdr
*)(nd_na
+ 1);
947 /* roundup to 8 bytes alignment! */
948 optlen
= (optlen
+ 7) & ~7;
950 m
->m_pkthdr
.len
+= optlen
;
953 bzero((caddr_t
)nd_opt
, optlen
);
954 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
955 nd_opt
->nd_opt_len
= optlen
>> 3;
956 bcopy(mac
, (caddr_t
)(nd_opt
+ 1), ifp
->if_addrlen
);
958 flags
&= ~ND_NA_FLAG_OVERRIDE
;
960 ip6
->ip6_plen
= htons((u_short
)icmp6len
);
961 nd_na
->nd_na_flags_reserved
= flags
;
962 nd_na
->nd_na_cksum
= 0;
964 in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(struct ip6_hdr
), icmp6len
);
966 ip6_output(m
, NULL
, NULL
, 0, &im6o
, &outif
, NULL
);
968 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
969 icmp6_ifstat_inc(outif
, ifs6_out_neighboradvert
);
971 icmp6stat
.icp6s_outhist
[ND_NEIGHBOR_ADVERT
]++;
975 nd6_ifptomac(struct ifnet
*ifp
)
977 switch (ifp
->if_type
) {
989 return ((caddr_t
)(ifp
+ 1));
996 TAILQ_HEAD(dadq_head
, dadq
);
998 TAILQ_ENTRY(dadq
) dad_list
;
999 struct ifaddr
*dad_ifa
;
1000 int dad_count
; /* max NS to send */
1001 int dad_ns_tcount
; /* # of trials to send NS */
1002 int dad_ns_ocount
; /* NS sent so far */
1005 struct callout dad_timer_ch
;
1008 static struct dadq_head dadq
;
1009 static int dad_init
= 0;
1011 static struct dadq
*
1012 nd6_dad_find(struct ifaddr
*ifa
)
1016 TAILQ_FOREACH(dp
, &dadq
, dad_list
) {
1017 if (dp
->dad_ifa
== ifa
)
1024 nd6_dad_starttimer(struct dadq
*dp
, int ticks
)
1027 callout_reset(&dp
->dad_timer_ch
, ticks
,
1028 (void (*) (void *))nd6_dad_timer
, (void *)dp
->dad_ifa
);
1032 nd6_dad_stoptimer(struct dadq
*dp
)
1035 callout_stop(&dp
->dad_timer_ch
);
1039 * Start Duplicated Address Detection (DAD) for specified interface address.
1042 nd6_dad_start(struct ifaddr
*ifa
,
1043 int *tick
) /* minimum delay ticks for IFF_UP event */
1045 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1054 * If we don't need DAD, don't do it.
1055 * There are several cases:
1056 * - DAD is disabled (ip6_dad_count == 0)
1057 * - the interface address is anycast
1059 if (!(ia
->ia6_flags
& IN6_IFF_TENTATIVE
)) {
1061 "nd6_dad_start: called with non-tentative address "
1063 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1064 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1067 if (ia
->ia6_flags
& IN6_IFF_ANYCAST
) {
1068 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1071 if (!ip6_dad_count
) {
1072 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1076 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1077 if (!(ifa
->ifa_ifp
->if_flags
& IFF_UP
))
1079 if (nd6_dad_find(ifa
) != NULL
) {
1080 /* DAD already in progress */
1084 dp
= kmalloc(sizeof(*dp
), M_IP6NDP
, M_NOWAIT
| M_ZERO
);
1086 log(LOG_ERR
, "nd6_dad_start: memory allocation failed for "
1088 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1089 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1092 callout_init(&dp
->dad_timer_ch
);
1093 TAILQ_INSERT_TAIL(&dadq
, (struct dadq
*)dp
, dad_list
);
1095 nd6log((LOG_DEBUG
, "%s: starting DAD for %s\n", if_name(ifa
->ifa_ifp
),
1096 ip6_sprintf(&ia
->ia_addr
.sin6_addr
)));
1099 * Send NS packet for DAD, ip6_dad_count times.
1100 * Note that we must delay the first transmission, if this is the
1101 * first packet to be sent from the interface after interface
1102 * (re)initialization.
1105 crit_enter(); /* XXX MP not MP safe */
1106 _IFAREF(ifa
, 0); /* just for safety */
1108 dp
->dad_count
= ip6_dad_count
;
1109 dp
->dad_ns_icount
= dp
->dad_na_icount
= 0;
1110 dp
->dad_ns_ocount
= dp
->dad_ns_tcount
= 0;
1112 nd6_dad_ns_output(dp
, ifa
);
1113 nd6_dad_starttimer(dp
,
1114 ND_IFINFO(ifa
->ifa_ifp
)->retrans
* hz
/ 1000);
1119 ntick
= krandom() % (MAX_RTR_SOLICITATION_DELAY
* hz
);
1121 ntick
= *tick
+ krandom() % (hz
/ 2);
1123 nd6_dad_starttimer(dp
, ntick
);
1128 * terminate DAD unconditionally. used for address removals.
1131 nd6_dad_stop(struct ifaddr
*ifa
)
1137 dp
= nd6_dad_find(ifa
);
1139 /* DAD wasn't started yet */
1143 nd6_dad_stoptimer(dp
);
1145 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1146 kfree(dp
, M_IP6NDP
);
1148 crit_enter(); /* XXX MP not MP safe */
1154 nd6_dad_timer(struct ifaddr
*ifa
)
1156 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1159 crit_enter(); /* XXX */
1163 log(LOG_ERR
, "nd6_dad_timer: called with null parameter\n");
1166 dp
= nd6_dad_find(ifa
);
1168 log(LOG_ERR
, "nd6_dad_timer: DAD structure not found\n");
1171 if (ia
->ia6_flags
& IN6_IFF_DUPLICATED
) {
1172 log(LOG_ERR
, "nd6_dad_timer: called with duplicated address "
1174 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1175 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1178 if (!(ia
->ia6_flags
& IN6_IFF_TENTATIVE
)) {
1179 log(LOG_ERR
, "nd6_dad_timer: called with non-tentative address "
1181 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1182 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1186 /* timeouted with IFF_{RUNNING,UP} check */
1187 if (dp
->dad_ns_tcount
> dad_maxtry
) {
1188 nd6log((LOG_INFO
, "%s: could not run DAD, driver problem?\n",
1189 if_name(ifa
->ifa_ifp
)));
1191 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1192 kfree(dp
, M_IP6NDP
);
1194 crit_enter(); /* XXX MP not MP safe */
1200 /* Need more checks? */
1201 if (dp
->dad_ns_ocount
< dp
->dad_count
) {
1203 * We have more NS to go. Send NS packet for DAD.
1205 nd6_dad_ns_output(dp
, ifa
);
1206 nd6_dad_starttimer(dp
,
1207 ND_IFINFO(ifa
->ifa_ifp
)->retrans
* hz
/ 1000);
1210 * We have transmitted sufficient number of DAD packets.
1211 * See what we've got.
1217 if (dp
->dad_na_icount
) {
1219 * the check is in nd6_dad_na_input(),
1225 if (dp
->dad_ns_icount
) {
1226 #if 0 /* heuristics */
1229 * - we have sent many(?) DAD NS, and
1230 * - the number of NS we sent equals to the
1231 * number of NS we've got, and
1233 * we may have a faulty network card/driver which
1234 * loops back multicasts to myself.
1236 if (3 < dp
->dad_count
1237 && dp
->dad_ns_icount
== dp
->dad_count
1238 && dp
->dad_na_icount
== 0) {
1239 log(LOG_INFO
, "DAD questionable for %s(%s): "
1240 "network card loops back multicast?\n",
1241 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1242 if_name(ifa
->ifa_ifp
));
1243 /* XXX consider it a duplicate or not? */
1246 /* We've seen NS, means DAD has failed. */
1250 /* We've seen NS, means DAD has failed. */
1256 /* (*dp) will be freed in nd6_dad_duplicated() */
1258 nd6_dad_duplicated(ifa
);
1261 * We are done with DAD. No NA came, no NS came.
1262 * duplicated address found.
1264 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1267 "%s: DAD complete for %s - no duplicates found\n",
1268 if_name(ifa
->ifa_ifp
),
1269 ip6_sprintf(&ia
->ia_addr
.sin6_addr
)));
1271 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1272 kfree(dp
, M_IP6NDP
);
1283 nd6_dad_duplicated(struct ifaddr
*ifa
)
1285 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1288 dp
= nd6_dad_find(ifa
);
1290 log(LOG_ERR
, "nd6_dad_duplicated: DAD structure not found\n");
1294 log(LOG_ERR
, "%s: DAD detected duplicate IPv6 address %s: "
1295 "NS in/out=%d/%d, NA in=%d\n",
1296 if_name(ifa
->ifa_ifp
), ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1297 dp
->dad_ns_icount
, dp
->dad_ns_ocount
, dp
->dad_na_icount
);
1299 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1300 ia
->ia6_flags
|= IN6_IFF_DUPLICATED
;
1302 /* We are done with DAD, with duplicated address found. (failure) */
1303 nd6_dad_stoptimer(dp
);
1305 log(LOG_ERR
, "%s: DAD complete for %s - duplicate found\n",
1306 if_name(ifa
->ifa_ifp
), ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1307 log(LOG_ERR
, "%s: manual intervention required\n",
1308 if_name(ifa
->ifa_ifp
));
1310 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1311 kfree(dp
, M_IP6NDP
);
1313 crit_enter(); /* XXX MP not MP safe */
1319 nd6_dad_ns_output(struct dadq
*dp
, struct ifaddr
*ifa
)
1321 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1322 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1324 dp
->dad_ns_tcount
++;
1325 if (!(ifp
->if_flags
& IFF_UP
)) {
1327 kprintf("%s: interface down?\n", if_name(ifp
));
1331 if (!(ifp
->if_flags
& IFF_RUNNING
)) {
1333 kprintf("%s: interface not running?\n", if_name(ifp
));
1338 dp
->dad_ns_ocount
++;
1339 nd6_ns_output(ifp
, NULL
, &ia
->ia_addr
.sin6_addr
, NULL
, 1);
1343 nd6_dad_ns_input(struct ifaddr
*ifa
)
1345 struct in6_ifaddr
*ia
;
1347 const struct in6_addr
*taddr6
;
1352 panic("ifa == NULL in nd6_dad_ns_input");
1354 ia
= (struct in6_ifaddr
*)ifa
;
1356 taddr6
= &ia
->ia_addr
.sin6_addr
;
1358 dp
= nd6_dad_find(ifa
);
1360 /* Quickhack - completely ignore DAD NS packets */
1361 if (dad_ignore_ns
) {
1363 "nd6_dad_ns_input: ignoring DAD NS packet for "
1364 "address %s(%s)\n", ip6_sprintf(taddr6
),
1365 if_name(ifa
->ifa_ifp
)));
1370 * if I'm yet to start DAD, someone else started using this address
1371 * first. I have a duplicate and you win.
1373 if (!dp
|| dp
->dad_ns_ocount
== 0)
1376 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1379 dp
= NULL
; /* will be freed in nd6_dad_duplicated() */
1380 nd6_dad_duplicated(ifa
);
1383 * not sure if I got a duplicate.
1384 * increment ns count and see what happens.
1387 dp
->dad_ns_icount
++;
1392 nd6_dad_na_input(struct ifaddr
*ifa
)
1397 panic("ifa == NULL in nd6_dad_na_input");
1399 dp
= nd6_dad_find(ifa
);
1401 dp
->dad_na_icount
++;
1403 /* remove the address. */
1404 nd6_dad_duplicated(ifa
);