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.23 2008/03/07 11:34:21 sephe 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 if (IN6_IS_ADDR_MULTICAST(&taddr6
)) {
155 nd6log((LOG_INFO
, "nd6_ns_input: bad NS target (multicast)\n"));
159 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6
))
160 taddr6
.s6_addr16
[1] = htons(ifp
->if_index
);
162 icmp6len
-= sizeof(*nd_ns
);
163 nd6_option_init(nd_ns
+ 1, icmp6len
, &ndopts
);
164 if (nd6_options(&ndopts
) < 0) {
166 "nd6_ns_input: invalid ND option, ignored\n"));
167 /* nd6_options have incremented stats */
171 if (ndopts
.nd_opts_src_lladdr
) {
172 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
173 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
176 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) && lladdr
) {
177 nd6log((LOG_INFO
, "nd6_ns_input: bad DAD packet "
178 "(link-layer address option)\n"));
183 * Attaching target link-layer address to the NA?
186 * NS IP dst is unicast/anycast MUST NOT add
187 * NS IP dst is solicited-node multicast MUST add
189 * In implementation, we add target link-layer address by default.
190 * We do not add one in MUST NOT cases.
192 #if 0 /* too much! */
193 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &daddr6
);
194 if (ifa
&& (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
))
198 if (!IN6_IS_ADDR_MULTICAST(&daddr6
))
204 * Target address (taddr6) must be either:
205 * (1) Valid unicast/anycast address for my receiving interface,
206 * (2) Unicast address for which I'm offering proxy service, or
207 * (3) "tentative" address on which DAD is being performed.
209 /* (1) and (3) check. */
212 ifa
= carp_iamatch6(ifp
->if_carp
, &taddr6
);
214 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
216 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
222 struct sockaddr_in6 tsin6
;
224 bzero(&tsin6
, sizeof tsin6
);
225 tsin6
.sin6_len
= sizeof(struct sockaddr_in6
);
226 tsin6
.sin6_family
= AF_INET6
;
227 tsin6
.sin6_addr
= taddr6
;
229 rt
= rtpurelookup((struct sockaddr
*)&tsin6
);
230 if (rt
!= NULL
&& (rt
->rt_flags
& RTF_ANNOUNCE
) &&
231 rt
->rt_gateway
->sa_family
== AF_LINK
) {
233 * proxy NDP for single entry
235 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
236 IN6_IFF_NOTREADY
|IN6_IFF_ANYCAST
);
239 proxydl
= SDL(rt
->rt_gateway
);
247 * We've got an NS packet, and we don't have that adddress
248 * assigned for us. We MUST silently ignore it.
253 myaddr6
= *IFA_IN6(ifa
);
254 anycast
= ((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
;
255 tentative
= ((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_TENTATIVE
;
256 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DUPLICATED
)
259 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
261 "nd6_ns_input: lladdrlen mismatch for %s "
262 "(if %d, NS packet %d)\n",
263 ip6_sprintf(&taddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
267 if (IN6_ARE_ADDR_EQUAL(&myaddr6
, &saddr6
)) {
269 "nd6_ns_input: duplicate IP6 address %s\n",
270 ip6_sprintf(&saddr6
)));
275 * We have neighbor solicitation packet, with target address equals to
276 * one of my tentative address.
278 * src addr how to process?
280 * multicast of course, invalid (rejected in ip6_input)
281 * unicast somebody is doing address resolution -> ignore
282 * unspec dup address detection
284 * The processing is defined in RFC 2462.
288 * If source address is unspecified address, it is for
289 * duplicated address detection.
291 * If not, the packet is for addess resolution;
292 * silently ignore it.
294 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
))
295 nd6_dad_ns_input(ifa
);
301 * If the source address is unspecified address, entries must not
302 * be created or updated.
303 * It looks that sender is performing DAD. Output NA toward
304 * all-node multicast address, to tell the sender that I'm using
306 * S bit ("solicited") must be zero.
308 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
)) {
309 saddr6
= kin6addr_linklocal_allnodes
;
310 saddr6
.s6_addr16
[1] = htons(ifp
->if_index
);
311 nd6_na_output(ifp
, &saddr6
, &taddr6
,
312 ((anycast
|| proxy
|| !tlladdr
)
313 ? 0 : ND_NA_FLAG_OVERRIDE
)
314 | (ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0),
315 tlladdr
, (struct sockaddr
*)proxydl
);
319 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_NEIGHBOR_SOLICIT
, 0);
321 nd6_na_output(ifp
, &saddr6
, &taddr6
,
322 ((anycast
|| proxy
|| !tlladdr
) ? 0 : ND_NA_FLAG_OVERRIDE
)
323 | (ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0)
324 | ND_NA_FLAG_SOLICITED
,
325 tlladdr
, (struct sockaddr
*)proxydl
);
331 nd6log((LOG_ERR
, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6
)));
332 nd6log((LOG_ERR
, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6
)));
333 nd6log((LOG_ERR
, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6
)));
334 icmp6stat
.icp6s_badns
++;
339 * Output an Neighbor Solicitation Message. Caller specifies:
340 * - ICMP6 header source IP6 address
341 * - ND6 header target IP6 address
342 * - ND6 header source datalink address
345 * Based on RFC 2462 (duplicated address detection)
348 nd6_ns_output(struct ifnet
*ifp
, const struct in6_addr
*daddr6
,
349 const struct in6_addr
*taddr6
,
350 struct llinfo_nd6
*ln
, /* for source address determination */
351 int dad
) /* duplicated address detection */
355 struct nd_neighbor_solicit
*nd_ns
;
356 struct in6_ifaddr
*ia
= NULL
;
357 struct ip6_moptions im6o
;
361 struct ifnet
*outif
= NULL
;
363 if (IN6_IS_ADDR_MULTICAST(taddr6
))
366 /* estimate the size of message */
367 maxlen
= sizeof(*ip6
) + sizeof(*nd_ns
);
368 maxlen
+= (sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7;
369 if (max_linkhdr
+ maxlen
>= MCLBYTES
) {
371 kprintf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
372 "(%d + %d > %d)\n", max_linkhdr
, maxlen
, MCLBYTES
);
377 m
= m_getb(max_linkhdr
+ maxlen
, MB_DONTWAIT
, MT_DATA
, M_PKTHDR
);
381 if (daddr6
== NULL
|| IN6_IS_ADDR_MULTICAST(daddr6
)) {
382 m
->m_flags
|= M_MCAST
;
383 im6o
.im6o_multicast_ifp
= ifp
;
384 im6o
.im6o_multicast_hlim
= 255;
385 im6o
.im6o_multicast_loop
= 0;
388 icmp6len
= sizeof(*nd_ns
);
389 m
->m_pkthdr
.len
= m
->m_len
= sizeof(*ip6
) + icmp6len
;
390 m
->m_data
+= max_linkhdr
; /* or MH_ALIGN() equivalent? */
392 /* fill neighbor solicitation packet */
393 ip6
= mtod(m
, struct ip6_hdr
*);
395 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
396 ip6
->ip6_vfc
|= IPV6_VERSION
;
397 /* ip6->ip6_plen will be set later */
398 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
401 ip6
->ip6_dst
= *daddr6
;
403 ip6
->ip6_dst
.s6_addr16
[0] = IPV6_ADDR_INT16_MLL
;
404 ip6
->ip6_dst
.s6_addr16
[1] = htons(ifp
->if_index
);
405 ip6
->ip6_dst
.s6_addr32
[1] = 0;
406 ip6
->ip6_dst
.s6_addr32
[2] = IPV6_ADDR_INT32_ONE
;
407 ip6
->ip6_dst
.s6_addr32
[3] = taddr6
->s6_addr32
[3];
408 ip6
->ip6_dst
.s6_addr8
[12] = 0xff;
411 #if 0 /* KAME way, exact address scope match */
413 * Select a source whose scope is the same as that of the dest.
414 * Typically, the dest is link-local solicitation multicast
415 * (i.e. neighbor discovery) or link-local/global unicast
416 * (i.e. neighbor un-reachability detection).
418 ia
= in6_ifawithifp(ifp
, &ip6
->ip6_dst
);
423 ip6
->ip6_src
= ia
->ia_addr
.sin6_addr
;
424 #else /* spec-wise correct */
427 * "If the source address of the packet prompting the
428 * solicitation is the same as one of the addresses assigned
429 * to the outgoing interface, that address SHOULD be placed
430 * in the IP Source Address of the outgoing solicitation.
431 * Otherwise, any one of the addresses assigned to the
432 * interface should be used."
434 * We use the source address for the prompting packet
436 * - saddr6 is given from the caller (by giving "ln"), and
437 * - saddr6 belongs to the outgoing interface.
438 * Otherwise, we perform a scope-wise match.
440 struct ip6_hdr
*hip6
; /* hold ip6 */
441 struct in6_addr
*saddr6
;
443 if (ln
&& ln
->ln_hold
) {
444 hip6
= mtod(ln
->ln_hold
, struct ip6_hdr
*);
446 if (sizeof(*hip6
) < ln
->ln_hold
->m_len
)
447 saddr6
= &hip6
->ip6_src
;
452 if (saddr6
&& in6ifa_ifpwithaddr(ifp
, saddr6
))
453 bcopy(saddr6
, &ip6
->ip6_src
, sizeof(*saddr6
));
455 ia
= in6_ifawithifp(ifp
, &ip6
->ip6_dst
);
460 ip6
->ip6_src
= ia
->ia_addr
.sin6_addr
;
465 * Source address for DAD packet must always be IPv6
466 * unspecified address. (0::0)
468 bzero(&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
470 nd_ns
= (struct nd_neighbor_solicit
*)(ip6
+ 1);
471 nd_ns
->nd_ns_type
= ND_NEIGHBOR_SOLICIT
;
472 nd_ns
->nd_ns_code
= 0;
473 nd_ns
->nd_ns_reserved
= 0;
474 nd_ns
->nd_ns_target
= *taddr6
;
476 if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns
->nd_ns_target
))
477 nd_ns
->nd_ns_target
.s6_addr16
[1] = 0;
480 * Add source link-layer address option.
482 * spec implementation
484 * DAD packet MUST NOT do not add the option
485 * there's no link layer address:
486 * impossible do not add the option
487 * there's link layer address:
488 * Multicast NS MUST add one add the option
489 * Unicast NS SHOULD add one add the option
491 if (!dad
&& (mac
= nd6_ifptomac(ifp
))) {
492 int optlen
= sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
;
493 struct nd_opt_hdr
*nd_opt
= (struct nd_opt_hdr
*)(nd_ns
+ 1);
494 /* 8 byte alignments... */
495 optlen
= (optlen
+ 7) & ~7;
497 m
->m_pkthdr
.len
+= optlen
;
500 bzero((caddr_t
)nd_opt
, optlen
);
501 nd_opt
->nd_opt_type
= ND_OPT_SOURCE_LINKADDR
;
502 nd_opt
->nd_opt_len
= optlen
>> 3;
503 bcopy(mac
, (caddr_t
)(nd_opt
+ 1), ifp
->if_addrlen
);
506 ip6
->ip6_plen
= htons((u_short
)icmp6len
);
507 nd_ns
->nd_ns_cksum
= 0;
509 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), icmp6len
);
511 ip6_output(m
, NULL
, NULL
, dad
? IPV6_DADOUTPUT
: 0, &im6o
, &outif
, NULL
);
513 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
514 icmp6_ifstat_inc(outif
, ifs6_out_neighborsolicit
);
516 icmp6stat
.icp6s_outhist
[ND_NEIGHBOR_SOLICIT
]++;
520 * Neighbor advertisement input handling.
523 * Based on RFC 2462 (duplicated address detection)
525 * the following items are not implemented yet:
526 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
527 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
530 nd6_na_input(struct mbuf
*m
, int off
, int icmp6len
)
532 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
533 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
534 struct nd_neighbor_advert
*nd_na
;
536 struct in6_addr saddr6
= ip6
->ip6_src
;
538 struct in6_addr daddr6
= ip6
->ip6_dst
;
539 struct in6_addr taddr6
;
547 struct llinfo_nd6
*ln
;
549 struct sockaddr_dl
*sdl
;
550 union nd_opts ndopts
;
552 if (ip6
->ip6_hlim
!= 255) {
554 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
555 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
556 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
560 #ifndef PULLDOWN_TEST
561 IP6_EXTHDR_CHECK(m
, off
, icmp6len
,);
562 nd_na
= (struct nd_neighbor_advert
*)((caddr_t
)ip6
+ off
);
564 IP6_EXTHDR_GET(nd_na
, struct nd_neighbor_advert
*, m
, off
, icmp6len
);
566 icmp6stat
.icp6s_tooshort
++;
570 taddr6
= nd_na
->nd_na_target
;
571 flags
= nd_na
->nd_na_flags_reserved
;
572 is_router
= ((flags
& ND_NA_FLAG_ROUTER
) != 0);
573 is_solicited
= ((flags
& ND_NA_FLAG_SOLICITED
) != 0);
574 is_override
= ((flags
& ND_NA_FLAG_OVERRIDE
) != 0);
576 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6
))
577 taddr6
.s6_addr16
[1] = htons(ifp
->if_index
);
579 if (IN6_IS_ADDR_MULTICAST(&taddr6
)) {
581 "nd6_na_input: invalid target address %s\n",
582 ip6_sprintf(&taddr6
)));
585 if (IN6_IS_ADDR_MULTICAST(&daddr6
))
588 "nd6_na_input: a solicited adv is multicasted\n"));
592 icmp6len
-= sizeof(*nd_na
);
593 nd6_option_init(nd_na
+ 1, icmp6len
, &ndopts
);
594 if (nd6_options(&ndopts
) < 0) {
596 "nd6_na_input: invalid ND option, ignored\n"));
597 /* nd6_options have incremented stats */
601 if (ndopts
.nd_opts_tgt_lladdr
) {
602 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
603 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
606 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
609 * Target address matches one of my interface address.
611 * If my address is tentative, this means that there's somebody
612 * already using the same address as mine. This indicates DAD failure.
613 * This is defined in RFC 2462.
615 * Otherwise, process as defined in RFC 2461.
618 && (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_TENTATIVE
)) {
619 nd6_dad_na_input(ifa
);
623 /* Just for safety, maybe unnecessary. */
626 "nd6_na_input: duplicate IP6 address %s\n",
627 ip6_sprintf(&taddr6
));
631 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
633 "nd6_na_input: lladdrlen mismatch for %s "
634 "(if %d, NA packet %d)\n",
635 ip6_sprintf(&taddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
640 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
642 rt
= nd6_lookup(&taddr6
, 0, ifp
);
644 ((ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
) == NULL
) ||
645 ((sdl
= SDL(rt
->rt_gateway
)) == NULL
))
648 if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
650 * If the link-layer has address, and no lladdr option came,
651 * discard the packet.
653 if (ifp
->if_addrlen
&& !lladdr
)
657 * Record link-layer address, and update the state.
659 sdl
->sdl_alen
= ifp
->if_addrlen
;
660 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
662 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
665 ln
->ln_expire
= time_second
+
666 ND_IFINFO(rt
->rt_ifp
)->reachable
;
668 ln
->ln_state
= ND6_LLINFO_STALE
;
669 ln
->ln_expire
= time_second
+ nd6_gctimer
;
671 if ((ln
->ln_router
= is_router
) != 0) {
673 * This means a router's state has changed from
674 * non-reachable to probably reachable, and might
675 * affect the status of associated prefixes..
677 pfxlist_onlink_check();
683 * Check if the link-layer address has changed or not.
689 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
698 * This is VERY complex. Look at it with care.
700 * override solicit lladdr llchange action
705 * 0 0 y y (1) REACHABLE->STALE
706 * 0 1 n -- (2c) *->REACHABLE
707 * 0 1 y n (2b) L *->REACHABLE
708 * 0 1 y y (1) REACHABLE->STALE
711 * 1 0 y y (2a) L *->STALE
712 * 1 1 n -- (2a) *->REACHABLE
713 * 1 1 y n (2a) L *->REACHABLE
714 * 1 1 y y (2a) L *->REACHABLE
716 if (!is_override
&& (lladdr
&& llchange
)) { /* (1) */
718 * If state is REACHABLE, make it STALE.
719 * no other updates should be done.
721 if (ln
->ln_state
== ND6_LLINFO_REACHABLE
) {
722 ln
->ln_state
= ND6_LLINFO_STALE
;
723 ln
->ln_expire
= time_second
+ nd6_gctimer
;
726 } else if (is_override
/* (2a) */
727 || (!is_override
&& (lladdr
&& !llchange
)) /* (2b) */
728 || !lladdr
) { /* (2c) */
730 * Update link-local address, if any.
733 sdl
->sdl_alen
= ifp
->if_addrlen
;
734 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
738 * If solicited, make the state REACHABLE.
739 * If not solicited and the link-layer address was
740 * changed, make it STALE.
743 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
746 ln
->ln_expire
= time_second
+
747 ND_IFINFO(ifp
)->reachable
;
750 if (lladdr
&& llchange
) {
751 ln
->ln_state
= ND6_LLINFO_STALE
;
752 ln
->ln_expire
= time_second
+ nd6_gctimer
;
757 if (ln
->ln_router
&& !is_router
) {
759 * The peer dropped the router flag.
760 * Remove the sender from the Default Router List and
761 * update the Destination Cache entries.
763 struct nd_defrouter
*dr
;
764 struct in6_addr
*in6
;
766 in6
= &((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
769 * Lock to protect the default router list.
770 * XXX: this might be unnecessary, since this function
771 * is only called under the network software interrupt
772 * context. However, we keep it just for safety.
775 dr
= defrouter_lookup(in6
, rt
->rt_ifp
);
778 else if (!ip6_forwarding
&& ip6_accept_rtadv
) {
780 * Even if the neighbor is not in the default
781 * router list, the neighbor may be used
782 * as a next hop for some destinations
783 * (e.g. redirect case). So we must
784 * call rt6_flush explicitly.
786 rt6_flush(&ip6
->ip6_src
, rt
->rt_ifp
);
790 ln
->ln_router
= is_router
;
792 rt
->rt_flags
&= ~RTF_REJECT
;
796 * we assume ifp is not a loopback here, so just set the 2nd
797 * argument as the 1st one.
799 nd6_output(ifp
, ifp
, ln
->ln_hold
,
800 (struct sockaddr_in6
*)rt_key(rt
), rt
);
809 icmp6stat
.icp6s_badna
++;
814 * Neighbor advertisement output handling.
818 * the following items are not implemented yet:
819 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
820 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
823 nd6_na_output(struct ifnet
*ifp
, const struct in6_addr
*daddr6
,
824 const struct in6_addr
*taddr6
, u_long flags
,
825 int tlladdr
, /* 1 if include target link-layer address */
826 struct sockaddr
*sdl0
) /* sockaddr_dl (= proxy NA) or NULL */
830 struct nd_neighbor_advert
*nd_na
;
831 struct in6_ifaddr
*ia
= NULL
;
832 struct ip6_moptions im6o
;
836 struct ifnet
*outif
= NULL
;
838 /* estimate the size of message */
839 maxlen
= sizeof(*ip6
) + sizeof(*nd_na
);
840 maxlen
+= (sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7;
841 if (max_linkhdr
+ maxlen
>= MCLBYTES
) {
843 kprintf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
844 "(%d + %d > %d)\n", max_linkhdr
, maxlen
, MCLBYTES
);
849 m
= m_getb(max_linkhdr
+ maxlen
, MB_DONTWAIT
, MT_DATA
, M_PKTHDR
);
853 if (IN6_IS_ADDR_MULTICAST(daddr6
)) {
854 m
->m_flags
|= M_MCAST
;
855 im6o
.im6o_multicast_ifp
= ifp
;
856 im6o
.im6o_multicast_hlim
= 255;
857 im6o
.im6o_multicast_loop
= 0;
860 icmp6len
= sizeof(*nd_na
);
861 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct ip6_hdr
) + icmp6len
;
862 m
->m_data
+= max_linkhdr
; /* or MH_ALIGN() equivalent? */
864 /* fill neighbor advertisement packet */
865 ip6
= mtod(m
, struct ip6_hdr
*);
867 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
868 ip6
->ip6_vfc
|= IPV6_VERSION
;
869 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
871 if (IN6_IS_ADDR_UNSPECIFIED(daddr6
)) {
873 ip6
->ip6_dst
.s6_addr16
[0] = IPV6_ADDR_INT16_MLL
;
874 ip6
->ip6_dst
.s6_addr16
[1] = htons(ifp
->if_index
);
875 ip6
->ip6_dst
.s6_addr32
[1] = 0;
876 ip6
->ip6_dst
.s6_addr32
[2] = 0;
877 ip6
->ip6_dst
.s6_addr32
[3] = IPV6_ADDR_INT32_ONE
;
878 flags
&= ~ND_NA_FLAG_SOLICITED
;
880 ip6
->ip6_dst
= *daddr6
;
883 * Select a source whose scope is the same as that of the dest.
885 ia
= in6_ifawithifp(ifp
, &ip6
->ip6_dst
);
890 ip6
->ip6_src
= ia
->ia_addr
.sin6_addr
;
891 nd_na
= (struct nd_neighbor_advert
*)(ip6
+ 1);
892 nd_na
->nd_na_type
= ND_NEIGHBOR_ADVERT
;
893 nd_na
->nd_na_code
= 0;
894 nd_na
->nd_na_target
= *taddr6
;
895 if (IN6_IS_SCOPE_LINKLOCAL(&nd_na
->nd_na_target
))
896 nd_na
->nd_na_target
.s6_addr16
[1] = 0;
899 * "tlladdr" indicates NS's condition for adding tlladdr or not.
900 * see nd6_ns_input() for details.
901 * Basically, if NS packet is sent to unicast/anycast addr,
902 * target lladdr option SHOULD NOT be included.
907 * sdl0 != NULL indicates proxy NA. If we do proxy, use
908 * lladdr in sdl0. If we are not proxying (sending NA for
909 * my address) use lladdr configured for the interface.
914 mac
= carp_macmatch6(ifp
->if_carp
, m
, taddr6
);
916 mac
= nd6_ifptomac(ifp
);
918 mac
= nd6_ifptomac(ifp
);
920 } else if (sdl0
->sa_family
== AF_LINK
) {
921 struct sockaddr_dl
*sdl
;
922 sdl
= (struct sockaddr_dl
*)sdl0
;
923 if (sdl
->sdl_alen
== ifp
->if_addrlen
)
928 int optlen
= sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
;
929 struct nd_opt_hdr
*nd_opt
= (struct nd_opt_hdr
*)(nd_na
+ 1);
931 /* roundup to 8 bytes alignment! */
932 optlen
= (optlen
+ 7) & ~7;
934 m
->m_pkthdr
.len
+= optlen
;
937 bzero((caddr_t
)nd_opt
, optlen
);
938 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
939 nd_opt
->nd_opt_len
= optlen
>> 3;
940 bcopy(mac
, (caddr_t
)(nd_opt
+ 1), ifp
->if_addrlen
);
942 flags
&= ~ND_NA_FLAG_OVERRIDE
;
944 ip6
->ip6_plen
= htons((u_short
)icmp6len
);
945 nd_na
->nd_na_flags_reserved
= flags
;
946 nd_na
->nd_na_cksum
= 0;
948 in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(struct ip6_hdr
), icmp6len
);
950 ip6_output(m
, NULL
, NULL
, 0, &im6o
, &outif
, NULL
);
952 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
953 icmp6_ifstat_inc(outif
, ifs6_out_neighboradvert
);
955 icmp6stat
.icp6s_outhist
[ND_NEIGHBOR_ADVERT
]++;
959 nd6_ifptomac(struct ifnet
*ifp
)
961 switch (ifp
->if_type
) {
973 return ((caddr_t
)(ifp
+ 1));
980 TAILQ_HEAD(dadq_head
, dadq
);
982 TAILQ_ENTRY(dadq
) dad_list
;
983 struct ifaddr
*dad_ifa
;
984 int dad_count
; /* max NS to send */
985 int dad_ns_tcount
; /* # of trials to send NS */
986 int dad_ns_ocount
; /* NS sent so far */
989 struct callout dad_timer_ch
;
992 static struct dadq_head dadq
;
993 static int dad_init
= 0;
996 nd6_dad_find(struct ifaddr
*ifa
)
1000 TAILQ_FOREACH(dp
, &dadq
, dad_list
) {
1001 if (dp
->dad_ifa
== ifa
)
1008 nd6_dad_starttimer(struct dadq
*dp
, int ticks
)
1011 callout_reset(&dp
->dad_timer_ch
, ticks
,
1012 (void (*) (void *))nd6_dad_timer
, (void *)dp
->dad_ifa
);
1016 nd6_dad_stoptimer(struct dadq
*dp
)
1019 callout_stop(&dp
->dad_timer_ch
);
1023 * Start Duplicated Address Detection (DAD) for specified interface address.
1026 nd6_dad_start(struct ifaddr
*ifa
,
1027 int *tick
) /* minimum delay ticks for IFF_UP event */
1029 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1038 * If we don't need DAD, don't do it.
1039 * There are several cases:
1040 * - DAD is disabled (ip6_dad_count == 0)
1041 * - the interface address is anycast
1043 if (!(ia
->ia6_flags
& IN6_IFF_TENTATIVE
)) {
1045 "nd6_dad_start: called with non-tentative address "
1047 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1048 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1051 if (ia
->ia6_flags
& IN6_IFF_ANYCAST
) {
1052 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1055 if (!ip6_dad_count
) {
1056 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1060 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1061 if (!(ifa
->ifa_ifp
->if_flags
& IFF_UP
))
1063 if (nd6_dad_find(ifa
) != NULL
) {
1064 /* DAD already in progress */
1068 dp
= kmalloc(sizeof(*dp
), M_IP6NDP
, M_NOWAIT
| M_ZERO
);
1070 log(LOG_ERR
, "nd6_dad_start: memory allocation failed for "
1072 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1073 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1076 callout_init(&dp
->dad_timer_ch
);
1077 TAILQ_INSERT_TAIL(&dadq
, (struct dadq
*)dp
, dad_list
);
1079 nd6log((LOG_DEBUG
, "%s: starting DAD for %s\n", if_name(ifa
->ifa_ifp
),
1080 ip6_sprintf(&ia
->ia_addr
.sin6_addr
)));
1083 * Send NS packet for DAD, ip6_dad_count times.
1084 * Note that we must delay the first transmission, if this is the
1085 * first packet to be sent from the interface after interface
1086 * (re)initialization.
1089 crit_enter(); /* XXX MP not MP safe */
1090 _IFAREF(ifa
, 0); /* just for safety */
1092 dp
->dad_count
= ip6_dad_count
;
1093 dp
->dad_ns_icount
= dp
->dad_na_icount
= 0;
1094 dp
->dad_ns_ocount
= dp
->dad_ns_tcount
= 0;
1096 nd6_dad_ns_output(dp
, ifa
);
1097 nd6_dad_starttimer(dp
,
1098 ND_IFINFO(ifa
->ifa_ifp
)->retrans
* hz
/ 1000);
1103 ntick
= krandom() % (MAX_RTR_SOLICITATION_DELAY
* hz
);
1105 ntick
= *tick
+ krandom() % (hz
/ 2);
1107 nd6_dad_starttimer(dp
, ntick
);
1112 * terminate DAD unconditionally. used for address removals.
1115 nd6_dad_stop(struct ifaddr
*ifa
)
1121 dp
= nd6_dad_find(ifa
);
1123 /* DAD wasn't started yet */
1127 nd6_dad_stoptimer(dp
);
1129 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1130 kfree(dp
, M_IP6NDP
);
1132 crit_enter(); /* XXX MP not MP safe */
1138 nd6_dad_timer(struct ifaddr
*ifa
)
1140 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1143 crit_enter(); /* XXX */
1147 log(LOG_ERR
, "nd6_dad_timer: called with null parameter\n");
1150 dp
= nd6_dad_find(ifa
);
1152 log(LOG_ERR
, "nd6_dad_timer: DAD structure not found\n");
1155 if (ia
->ia6_flags
& IN6_IFF_DUPLICATED
) {
1156 log(LOG_ERR
, "nd6_dad_timer: called with duplicated address "
1158 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1159 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1162 if (!(ia
->ia6_flags
& IN6_IFF_TENTATIVE
)) {
1163 log(LOG_ERR
, "nd6_dad_timer: called with non-tentative address "
1165 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1166 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1170 /* timeouted with IFF_{RUNNING,UP} check */
1171 if (dp
->dad_ns_tcount
> dad_maxtry
) {
1172 nd6log((LOG_INFO
, "%s: could not run DAD, driver problem?\n",
1173 if_name(ifa
->ifa_ifp
)));
1175 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1176 kfree(dp
, M_IP6NDP
);
1178 crit_enter(); /* XXX MP not MP safe */
1184 /* Need more checks? */
1185 if (dp
->dad_ns_ocount
< dp
->dad_count
) {
1187 * We have more NS to go. Send NS packet for DAD.
1189 nd6_dad_ns_output(dp
, ifa
);
1190 nd6_dad_starttimer(dp
,
1191 ND_IFINFO(ifa
->ifa_ifp
)->retrans
* hz
/ 1000);
1194 * We have transmitted sufficient number of DAD packets.
1195 * See what we've got.
1201 if (dp
->dad_na_icount
) {
1203 * the check is in nd6_dad_na_input(),
1209 if (dp
->dad_ns_icount
) {
1210 #if 0 /* heuristics */
1213 * - we have sent many(?) DAD NS, and
1214 * - the number of NS we sent equals to the
1215 * number of NS we've got, and
1217 * we may have a faulty network card/driver which
1218 * loops back multicasts to myself.
1220 if (3 < dp
->dad_count
1221 && dp
->dad_ns_icount
== dp
->dad_count
1222 && dp
->dad_na_icount
== 0) {
1223 log(LOG_INFO
, "DAD questionable for %s(%s): "
1224 "network card loops back multicast?\n",
1225 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1226 if_name(ifa
->ifa_ifp
));
1227 /* XXX consider it a duplicate or not? */
1230 /* We've seen NS, means DAD has failed. */
1234 /* We've seen NS, means DAD has failed. */
1240 /* (*dp) will be freed in nd6_dad_duplicated() */
1242 nd6_dad_duplicated(ifa
);
1245 * We are done with DAD. No NA came, no NS came.
1246 * duplicated address found.
1248 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1251 "%s: DAD complete for %s - no duplicates found\n",
1252 if_name(ifa
->ifa_ifp
),
1253 ip6_sprintf(&ia
->ia_addr
.sin6_addr
)));
1255 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1256 kfree(dp
, M_IP6NDP
);
1267 nd6_dad_duplicated(struct ifaddr
*ifa
)
1269 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1272 dp
= nd6_dad_find(ifa
);
1274 log(LOG_ERR
, "nd6_dad_duplicated: DAD structure not found\n");
1278 log(LOG_ERR
, "%s: DAD detected duplicate IPv6 address %s: "
1279 "NS in/out=%d/%d, NA in=%d\n",
1280 if_name(ifa
->ifa_ifp
), ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1281 dp
->dad_ns_icount
, dp
->dad_ns_ocount
, dp
->dad_na_icount
);
1283 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1284 ia
->ia6_flags
|= IN6_IFF_DUPLICATED
;
1286 /* We are done with DAD, with duplicated address found. (failure) */
1287 nd6_dad_stoptimer(dp
);
1289 log(LOG_ERR
, "%s: DAD complete for %s - duplicate found\n",
1290 if_name(ifa
->ifa_ifp
), ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1291 log(LOG_ERR
, "%s: manual intervention required\n",
1292 if_name(ifa
->ifa_ifp
));
1294 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1295 kfree(dp
, M_IP6NDP
);
1297 crit_enter(); /* XXX MP not MP safe */
1303 nd6_dad_ns_output(struct dadq
*dp
, struct ifaddr
*ifa
)
1305 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1306 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1308 dp
->dad_ns_tcount
++;
1309 if (!(ifp
->if_flags
& IFF_UP
)) {
1311 kprintf("%s: interface down?\n", if_name(ifp
));
1315 if (!(ifp
->if_flags
& IFF_RUNNING
)) {
1317 kprintf("%s: interface not running?\n", if_name(ifp
));
1322 dp
->dad_ns_ocount
++;
1323 nd6_ns_output(ifp
, NULL
, &ia
->ia_addr
.sin6_addr
, NULL
, 1);
1327 nd6_dad_ns_input(struct ifaddr
*ifa
)
1329 struct in6_ifaddr
*ia
;
1331 const struct in6_addr
*taddr6
;
1336 panic("ifa == NULL in nd6_dad_ns_input");
1338 ia
= (struct in6_ifaddr
*)ifa
;
1340 taddr6
= &ia
->ia_addr
.sin6_addr
;
1342 dp
= nd6_dad_find(ifa
);
1344 /* Quickhack - completely ignore DAD NS packets */
1345 if (dad_ignore_ns
) {
1347 "nd6_dad_ns_input: ignoring DAD NS packet for "
1348 "address %s(%s)\n", ip6_sprintf(taddr6
),
1349 if_name(ifa
->ifa_ifp
)));
1354 * if I'm yet to start DAD, someone else started using this address
1355 * first. I have a duplicate and you win.
1357 if (!dp
|| dp
->dad_ns_ocount
== 0)
1360 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1363 dp
= NULL
; /* will be freed in nd6_dad_duplicated() */
1364 nd6_dad_duplicated(ifa
);
1367 * not sure if I got a duplicate.
1368 * increment ns count and see what happens.
1371 dp
->dad_ns_icount
++;
1376 nd6_dad_na_input(struct ifaddr
*ifa
)
1381 panic("ifa == NULL in nd6_dad_na_input");
1383 dp
= nd6_dad_find(ifa
);
1385 dp
->dad_na_icount
++;
1387 /* remove the address. */
1388 nd6_dad_duplicated(ifa
);