1 /* $FreeBSD: src/sys/netinet6/nd6_nbr.c,v 1.4.2.4 2001/07/06 05:32:25 sumikawa Exp $ */
2 /* $KAME: nd6_nbr.c,v 1.64 2001/05/17 03:48:30 itojun 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/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
40 #include <sys/kernel.h>
41 #include <sys/errno.h>
42 #include <sys/syslog.h>
43 #include <kern/queue.h>
46 #include <net/if_types.h>
47 #include <net/if_dl.h>
48 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <netinet6/in6_var.h>
53 #include <netinet/ip6.h>
54 #include <netinet6/ip6_var.h>
55 #include <netinet6/nd6.h>
56 #include <netinet/icmp6.h>
59 #include <netinet6/ipsec.h>
61 #include <netinet6/ipsec6.h>
63 extern int ipsec_bypass
;
66 #include <net/net_osdep.h>
68 #define SDL(s) ((struct sockaddr_dl *)s)
71 static struct dadq
*nd6_dad_find
__P((struct ifaddr
*));
73 static void nd6_dad_starttimer
__P((struct dadq
*, int));
74 static void nd6_dad_stoptimer
__P((struct dadq
*));
76 void nd6_dad_stoptimer
__P((struct ifaddr
*));
78 static void nd6_dad_timer
__P((struct ifaddr
*));
79 static void nd6_dad_timer_funnel
__P((struct ifaddr
*));
80 static void nd6_dad_ns_output
__P((struct dadq
*, struct ifaddr
*));
81 static void nd6_dad_ns_input
__P((struct ifaddr
*));
82 static void nd6_dad_na_input
__P((struct ifaddr
*));
84 static int dad_ignore_ns
= 0; /* ignore NS in DAD - specwise incorrect*/
85 static int dad_maxtry
= 15; /* max # of *tries* to transmit DAD packet */
88 * Input an Neighbor Solicitation Message.
91 * Based on RFC 2462 (duplicated address detection)
94 nd6_ns_input(m
, off
, icmp6len
)
98 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
99 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
100 struct nd_neighbor_solicit
*nd_ns
;
101 struct in6_addr saddr6
= ip6
->ip6_src
;
102 struct in6_addr daddr6
= ip6
->ip6_dst
;
103 struct in6_addr taddr6
;
104 struct in6_addr myaddr6
;
108 int anycast
= 0, proxy
= 0, tentative
= 0;
110 union nd_opts ndopts
;
111 struct sockaddr_dl
*proxydl
= NULL
;
113 #ifndef PULLDOWN_TEST
114 IP6_EXTHDR_CHECK(m
, off
, icmp6len
,);
115 nd_ns
= (struct nd_neighbor_solicit
*)((caddr_t
)ip6
+ off
);
117 IP6_EXTHDR_GET(nd_ns
, struct nd_neighbor_solicit
*, m
, off
, icmp6len
);
119 icmp6stat
.icp6s_tooshort
++;
123 ip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer for safety */
124 taddr6
= nd_ns
->nd_ns_target
;
126 if (ip6
->ip6_hlim
!= 255) {
128 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
129 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
130 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
134 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
)) {
135 /* dst has to be solicited node multicast address. */
136 if (daddr6
.s6_addr16
[0] == IPV6_ADDR_INT16_MLL
137 /* don't check ifindex portion */
138 && daddr6
.s6_addr32
[1] == 0
139 && daddr6
.s6_addr32
[2] == IPV6_ADDR_INT32_ONE
140 && daddr6
.s6_addr8
[12] == 0xff) {
143 nd6log((LOG_INFO
, "nd6_ns_input: bad DAD packet "
144 "(wrong ip6 dst)\n"));
149 if (IN6_IS_ADDR_MULTICAST(&taddr6
)) {
150 nd6log((LOG_INFO
, "nd6_ns_input: bad NS target (multicast)\n"));
154 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6
))
155 taddr6
.s6_addr16
[1] = htons(ifp
->if_index
);
157 icmp6len
-= sizeof(*nd_ns
);
158 nd6_option_init(nd_ns
+ 1, icmp6len
, &ndopts
);
159 if (nd6_options(&ndopts
) < 0) {
161 "nd6_ns_input: invalid ND option, ignored\n"));
162 /* nd6_options have incremented stats */
166 if (ndopts
.nd_opts_src_lladdr
) {
167 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
168 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
171 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) && lladdr
) {
172 nd6log((LOG_INFO
, "nd6_ns_input: bad DAD packet "
173 "(link-layer address option)\n"));
178 * Attaching target link-layer address to the NA?
181 * NS IP dst is unicast/anycast MUST NOT add
182 * NS IP dst is solicited-node multicast MUST add
184 * In implementation, we add target link-layer address by default.
185 * We do not add one in MUST NOT cases.
187 #if 0 /* too much! */
188 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &daddr6
);
189 if (ifa
&& (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
))
193 if (!IN6_IS_ADDR_MULTICAST(&daddr6
))
199 * Target address (taddr6) must be either:
200 * (1) Valid unicast/anycast address for my receiving interface,
201 * (2) Unicast address for which I'm offering proxy service, or
202 * (3) "tentative" address on which DAD is being performed.
204 /* (1) and (3) check. */
205 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
210 struct sockaddr_in6 tsin6
;
212 bzero(&tsin6
, sizeof tsin6
);
213 tsin6
.sin6_len
= sizeof(struct sockaddr_in6
);
214 tsin6
.sin6_family
= AF_INET6
;
215 tsin6
.sin6_addr
= taddr6
;
217 rt
= rtalloc1((struct sockaddr
*)&tsin6
, 0, 0);
218 if (rt
&& (rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
219 rt
->rt_gateway
->sa_family
== AF_LINK
) {
221 * proxy NDP for single entry
223 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
224 IN6_IFF_NOTREADY
|IN6_IFF_ANYCAST
);
227 proxydl
= SDL(rt
->rt_gateway
);
235 * We've got an NS packet, and we don't have that adddress
236 * assigned for us. We MUST silently ignore it.
241 myaddr6
= *IFA_IN6(ifa
);
242 anycast
= ((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
;
243 tentative
= ((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_TENTATIVE
;
244 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DUPLICATED
)
247 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
249 "nd6_ns_input: lladdrlen mismatch for %s "
250 "(if %d, NS packet %d)\n",
251 ip6_sprintf(&taddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
255 if (IN6_ARE_ADDR_EQUAL(&myaddr6
, &saddr6
)) {
257 "nd6_ns_input: duplicate IP6 address %s\n",
258 ip6_sprintf(&saddr6
)));
263 * We have neighbor solicitation packet, with target address equals to
264 * one of my tentative address.
266 * src addr how to process?
268 * multicast of course, invalid (rejected in ip6_input)
269 * unicast somebody is doing address resolution -> ignore
270 * unspec dup address detection
272 * The processing is defined in RFC 2462.
276 * If source address is unspecified address, it is for
277 * duplicated address detection.
279 * If not, the packet is for addess resolution;
280 * silently ignore it.
282 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
))
283 nd6_dad_ns_input(ifa
);
289 * If the source address is unspecified address, entries must not
290 * be created or updated.
291 * It looks that sender is performing DAD. Output NA toward
292 * all-node multicast address, to tell the sender that I'm using
294 * S bit ("solicited") must be zero.
296 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
)) {
297 saddr6
= in6addr_linklocal_allnodes
;
298 saddr6
.s6_addr16
[1] = htons(ifp
->if_index
);
299 nd6_na_output(ifp
, &saddr6
, &taddr6
,
300 ((anycast
|| proxy
|| !tlladdr
)
301 ? 0 : ND_NA_FLAG_OVERRIDE
)
302 | (ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0),
303 tlladdr
, (struct sockaddr
*)proxydl
);
307 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_NEIGHBOR_SOLICIT
, 0);
309 nd6_na_output(ifp
, &saddr6
, &taddr6
,
310 ((anycast
|| proxy
|| !tlladdr
) ? 0 : ND_NA_FLAG_OVERRIDE
)
311 | (ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0)
312 | ND_NA_FLAG_SOLICITED
,
313 tlladdr
, (struct sockaddr
*)proxydl
);
319 nd6log((LOG_ERR
, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6
)));
320 nd6log((LOG_ERR
, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6
)));
321 nd6log((LOG_ERR
, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6
)));
322 icmp6stat
.icp6s_badns
++;
327 * Output an Neighbor Solicitation Message. Caller specifies:
328 * - ICMP6 header source IP6 address
329 * - ND6 header target IP6 address
330 * - ND6 header source datalink address
333 * Based on RFC 2462 (duplicated address detection)
336 nd6_ns_output(ifp
, daddr6
, taddr6
, ln
, dad
)
338 const struct in6_addr
*daddr6
, *taddr6
;
339 struct llinfo_nd6
*ln
; /* for source address determination */
340 int dad
; /* duplicated address detection */
344 struct nd_neighbor_solicit
*nd_ns
;
345 struct in6_ifaddr
*ia
= NULL
;
346 struct ip6_moptions im6o
;
350 struct ifnet
*outif
= NULL
;
352 if (IN6_IS_ADDR_MULTICAST(taddr6
))
355 /* estimate the size of message */
356 maxlen
= sizeof(*ip6
) + sizeof(*nd_ns
);
357 maxlen
+= (sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7;
358 if (max_linkhdr
+ maxlen
>= MCLBYTES
) {
360 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
361 "(%d + %d > %d)\n", max_linkhdr
, maxlen
, MCLBYTES
);
366 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
367 if (m
&& max_linkhdr
+ maxlen
>= MHLEN
) {
368 MCLGET(m
, M_DONTWAIT
);
369 if ((m
->m_flags
& M_EXT
) == 0) {
376 m
->m_pkthdr
.rcvif
= NULL
;
378 if (daddr6
== NULL
|| IN6_IS_ADDR_MULTICAST(daddr6
)) {
379 m
->m_flags
|= M_MCAST
;
380 im6o
.im6o_multicast_ifp
= ifp
;
381 im6o
.im6o_multicast_hlim
= 255;
382 im6o
.im6o_multicast_loop
= 0;
385 icmp6len
= sizeof(*nd_ns
);
386 m
->m_pkthdr
.len
= m
->m_len
= sizeof(*ip6
) + icmp6len
;
387 m
->m_data
+= max_linkhdr
; /* or MH_ALIGN() equivalent? */
389 /* fill neighbor solicitation packet */
390 ip6
= mtod(m
, struct ip6_hdr
*);
392 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
393 ip6
->ip6_vfc
|= IPV6_VERSION
;
394 /* ip6->ip6_plen will be set later */
395 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
398 ip6
->ip6_dst
= *daddr6
;
400 ip6
->ip6_dst
.s6_addr16
[0] = IPV6_ADDR_INT16_MLL
;
401 ip6
->ip6_dst
.s6_addr16
[1] = htons(ifp
->if_index
);
402 ip6
->ip6_dst
.s6_addr32
[1] = 0;
403 ip6
->ip6_dst
.s6_addr32
[2] = IPV6_ADDR_INT32_ONE
;
404 ip6
->ip6_dst
.s6_addr32
[3] = taddr6
->s6_addr32
[3];
405 ip6
->ip6_dst
.s6_addr8
[12] = 0xff;
408 #if 0 /* KAME way, exact address scope match */
410 * Select a source whose scope is the same as that of the dest.
411 * Typically, the dest is link-local solicitation multicast
412 * (i.e. neighbor discovery) or link-local/global unicast
413 * (i.e. neighbor un-reachability detection).
415 ia
= in6_ifawithifp(ifp
, &ip6
->ip6_dst
);
420 ip6
->ip6_src
= ia
->ia_addr
.sin6_addr
;
421 #else /* spec-wise correct */
424 * "If the source address of the packet prompting the
425 * solicitation is the same as one of the addresses assigned
426 * to the outgoing interface, that address SHOULD be placed
427 * in the IP Source Address of the outgoing solicitation.
428 * Otherwise, any one of the addresses assigned to the
429 * interface should be used."
431 * We use the source address for the prompting packet
433 * - saddr6 is given from the caller (by giving "ln"), and
434 * - saddr6 belongs to the outgoing interface.
435 * Otherwise, we perform a scope-wise match.
437 struct ip6_hdr
*hip6
; /* hold ip6 */
438 struct in6_addr
*saddr6
;
440 if (ln
&& ln
->ln_hold
) {
441 hip6
= mtod(ln
->ln_hold
, struct ip6_hdr
*);
443 if (sizeof(*hip6
) < ln
->ln_hold
->m_len
)
444 saddr6
= &hip6
->ip6_src
;
449 if (saddr6
&& in6ifa_ifpwithaddr(ifp
, saddr6
))
450 bcopy(saddr6
, &ip6
->ip6_src
, sizeof(*saddr6
));
452 ia
= in6_ifawithifp(ifp
, &ip6
->ip6_dst
);
454 if (ln
&& ln
->ln_hold
)
455 m_freem(ln
->ln_hold
);
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
);
512 /* Don't lookup socket */
513 if (ipsec_bypass
== 0)
514 (void)ipsec_setsocket(m
, NULL
);
516 ip6_output(m
, NULL
, NULL
, dad
? IPV6_DADOUTPUT
: 0, &im6o
, &outif
);
518 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
519 icmp6_ifstat_inc(outif
, ifs6_out_neighborsolicit
);
521 icmp6stat
.icp6s_outhist
[ND_NEIGHBOR_SOLICIT
]++;
525 * Neighbor advertisement input handling.
528 * Based on RFC 2462 (duplicated address detection)
530 * the following items are not implemented yet:
531 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
532 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
535 nd6_na_input(m
, off
, icmp6len
)
539 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
540 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
541 struct nd_neighbor_advert
*nd_na
;
543 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
));
638 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
640 "nd6_na_input: lladdrlen mismatch for %s "
641 "(if %d, NA packet %d)\n",
642 ip6_sprintf(&taddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
647 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
649 rt
= nd6_lookup(&taddr6
, 0, ifp
);
651 ((ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
) == NULL
) ||
652 ((sdl
= SDL(rt
->rt_gateway
)) == NULL
))
655 if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
657 * If the link-layer has address, and no lladdr option came,
658 * discard the packet.
660 if (ifp
->if_addrlen
&& !lladdr
)
664 * Record link-layer address, and update the state.
666 sdl
->sdl_alen
= ifp
->if_addrlen
;
667 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
669 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
672 ln
->ln_expire
= time_second
+
673 nd_ifinfo
[rt
->rt_ifp
->if_index
].reachable
;
675 ln
->ln_state
= ND6_LLINFO_STALE
;
676 ln
->ln_expire
= time_second
+ nd6_gctimer
;
678 if ((ln
->ln_router
= is_router
) != 0) {
680 * This means a router's state has changed from
681 * non-reachable to probably reachable, and might
682 * affect the status of associated prefixes..
684 pfxlist_onlink_check();
690 * Check if the link-layer address has changed or not.
696 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
705 * This is VERY complex. Look at it with care.
707 * override solicit lladdr llchange action
712 * 0 0 y y (1) REACHABLE->STALE
713 * 0 1 n -- (2c) *->REACHABLE
714 * 0 1 y n (2b) L *->REACHABLE
715 * 0 1 y y (1) REACHABLE->STALE
718 * 1 0 y y (2a) L *->STALE
719 * 1 1 n -- (2a) *->REACHABLE
720 * 1 1 y n (2a) L *->REACHABLE
721 * 1 1 y y (2a) L *->REACHABLE
723 if (!is_override
&& (lladdr
&& llchange
)) { /* (1) */
725 * If state is REACHABLE, make it STALE.
726 * no other updates should be done.
728 if (ln
->ln_state
== ND6_LLINFO_REACHABLE
) {
729 ln
->ln_state
= ND6_LLINFO_STALE
;
730 ln
->ln_expire
= time_second
+ nd6_gctimer
;
733 } else if (is_override
/* (2a) */
734 || (!is_override
&& (lladdr
&& !llchange
)) /* (2b) */
735 || !lladdr
) { /* (2c) */
737 * Update link-local address, if any.
740 sdl
->sdl_alen
= ifp
->if_addrlen
;
741 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
745 * If solicited, make the state REACHABLE.
746 * If not solicited and the link-layer address was
747 * changed, make it STALE.
750 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
753 ln
->ln_expire
= time_second
+
754 nd_ifinfo
[ifp
->if_index
].reachable
;
757 if (lladdr
&& llchange
) {
758 ln
->ln_state
= ND6_LLINFO_STALE
;
759 ln
->ln_expire
= time_second
+ nd6_gctimer
;
764 if (ln
->ln_router
&& !is_router
) {
766 * The peer dropped the router flag.
767 * Remove the sender from the Default Router List and
768 * update the Destination Cache entries.
770 struct nd_defrouter
*dr
;
771 struct in6_addr
*in6
;
774 in6
= &((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
777 * Lock to protect the default router list.
778 * XXX: this might be unnecessary, since this function
779 * is only called under the network software interrupt
780 * context. However, we keep it just for safety.
783 dr
= defrouter_lookup(in6
, rt
->rt_ifp
);
786 else if (!ip6_forwarding
&& (ip6_accept_rtadv
|| (rt
->rt_ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
788 * Even if the neighbor is not in the default
789 * router list, the neighbor may be used
790 * as a next hop for some destinations
791 * (e.g. redirect case). So we must
792 * call rt6_flush explicitly.
794 rt6_flush(&ip6
->ip6_src
, rt
->rt_ifp
);
798 ln
->ln_router
= is_router
;
800 rt
->rt_flags
&= ~RTF_REJECT
;
804 * we assume ifp is not a loopback here, so just set the 2nd
805 * argument as the 1st one.
807 nd6_output(ifp
, ifp
, ln
->ln_hold
,
808 (struct sockaddr_in6
*)rt_key(rt
), rt
);
817 icmp6stat
.icp6s_badna
++;
822 * Neighbor advertisement output handling.
826 * the following items are not implemented yet:
827 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
828 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
831 nd6_na_output(ifp
, daddr6
, taddr6
, flags
, tlladdr
, sdl0
)
833 const struct in6_addr
*daddr6
, *taddr6
;
835 int tlladdr
; /* 1 if include target link-layer address */
836 struct sockaddr
*sdl0
; /* sockaddr_dl (= proxy NA) or NULL */
840 struct nd_neighbor_advert
*nd_na
;
841 struct in6_ifaddr
*ia
= NULL
;
842 struct ip6_moptions im6o
;
846 struct ifnet
*outif
= NULL
;
848 /* estimate the size of message */
849 maxlen
= sizeof(*ip6
) + sizeof(*nd_na
);
850 maxlen
+= (sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7;
851 if (max_linkhdr
+ maxlen
>= MCLBYTES
) {
853 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
854 "(%d + %d > %d)\n", max_linkhdr
, maxlen
, MCLBYTES
);
859 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
860 if (m
&& max_linkhdr
+ maxlen
>= MHLEN
) {
861 MCLGET(m
, M_DONTWAIT
);
862 if ((m
->m_flags
& M_EXT
) == 0) {
869 m
->m_pkthdr
.rcvif
= NULL
;
871 if (IN6_IS_ADDR_MULTICAST(daddr6
)) {
872 m
->m_flags
|= M_MCAST
;
873 im6o
.im6o_multicast_ifp
= ifp
;
874 im6o
.im6o_multicast_hlim
= 255;
875 im6o
.im6o_multicast_loop
= 0;
878 icmp6len
= sizeof(*nd_na
);
879 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct ip6_hdr
) + icmp6len
;
880 m
->m_data
+= max_linkhdr
; /* or MH_ALIGN() equivalent? */
882 /* fill neighbor advertisement packet */
883 ip6
= mtod(m
, struct ip6_hdr
*);
885 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
886 ip6
->ip6_vfc
|= IPV6_VERSION
;
887 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
889 if (IN6_IS_ADDR_UNSPECIFIED(daddr6
)) {
891 ip6
->ip6_dst
.s6_addr16
[0] = IPV6_ADDR_INT16_MLL
;
892 ip6
->ip6_dst
.s6_addr16
[1] = htons(ifp
->if_index
);
893 ip6
->ip6_dst
.s6_addr32
[1] = 0;
894 ip6
->ip6_dst
.s6_addr32
[2] = 0;
895 ip6
->ip6_dst
.s6_addr32
[3] = IPV6_ADDR_INT32_ONE
;
896 flags
&= ~ND_NA_FLAG_SOLICITED
;
898 ip6
->ip6_dst
= *daddr6
;
901 * Select a source whose scope is the same as that of the dest.
903 ia
= in6_ifawithifp(ifp
, &ip6
->ip6_dst
);
908 ip6
->ip6_src
= ia
->ia_addr
.sin6_addr
;
909 nd_na
= (struct nd_neighbor_advert
*)(ip6
+ 1);
910 nd_na
->nd_na_type
= ND_NEIGHBOR_ADVERT
;
911 nd_na
->nd_na_code
= 0;
912 nd_na
->nd_na_target
= *taddr6
;
913 if (IN6_IS_SCOPE_LINKLOCAL(&nd_na
->nd_na_target
))
914 nd_na
->nd_na_target
.s6_addr16
[1] = 0;
917 * "tlladdr" indicates NS's condition for adding tlladdr or not.
918 * see nd6_ns_input() for details.
919 * Basically, if NS packet is sent to unicast/anycast addr,
920 * target lladdr option SHOULD NOT be included.
924 * sdl0 != NULL indicates proxy NA. If we do proxy, use
925 * lladdr in sdl0. If we are not proxying (sending NA for
926 * my address) use lladdr configured for the interface.
929 mac
= nd6_ifptomac(ifp
);
930 else if (sdl0
->sa_family
== AF_LINK
) {
931 struct sockaddr_dl
*sdl
;
932 sdl
= (struct sockaddr_dl
*)sdl0
;
933 if (sdl
->sdl_alen
== ifp
->if_addrlen
)
937 if (tlladdr
&& mac
) {
938 int optlen
= sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
;
939 struct nd_opt_hdr
*nd_opt
= (struct nd_opt_hdr
*)(nd_na
+ 1);
941 /* roundup to 8 bytes alignment! */
942 optlen
= (optlen
+ 7) & ~7;
944 m
->m_pkthdr
.len
+= optlen
;
947 bzero((caddr_t
)nd_opt
, optlen
);
948 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
949 nd_opt
->nd_opt_len
= optlen
>> 3;
950 bcopy(mac
, (caddr_t
)(nd_opt
+ 1), ifp
->if_addrlen
);
952 flags
&= ~ND_NA_FLAG_OVERRIDE
;
954 ip6
->ip6_plen
= htons((u_short
)icmp6len
);
955 nd_na
->nd_na_flags_reserved
= flags
;
956 nd_na
->nd_na_cksum
= 0;
958 in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(struct ip6_hdr
), icmp6len
);
961 /* Don't lookup socket */
962 if (ipsec_bypass
== 0)
963 (void)ipsec_setsocket(m
, NULL
);
965 ip6_output(m
, NULL
, NULL
, 0, &im6o
, &outif
);
967 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
968 icmp6_ifstat_inc(outif
, ifs6_out_neighboradvert
);
970 icmp6stat
.icp6s_outhist
[ND_NEIGHBOR_ADVERT
]++;
977 switch (ifp
->if_type
) {
988 return ((caddr_t
)(ifp
+ 1));
995 TAILQ_HEAD(dadq_head
, dadq
);
997 TAILQ_ENTRY(dadq
) dad_list
;
998 struct ifaddr
*dad_ifa
;
999 int dad_count
; /* max NS to send */
1000 int dad_ns_tcount
; /* # of trials to send NS */
1001 int dad_ns_ocount
; /* NS sent so far */
1006 static struct dadq_head dadq
;
1007 static int dad_init
= 0;
1009 static struct dadq
*
1015 for (dp
= dadq
.tqh_first
; dp
; dp
= dp
->dad_list
.tqe_next
) {
1016 if (dp
->dad_ifa
== ifa
)
1024 nd6_dad_stoptimer(ifa
)
1028 untimeout((void (*) __P((void *)))nd6_dad_timer_funnel
, (void *)ifa
);
1032 nd6_dad_starttimer(dp
, ticks
)
1037 callout_reset(&dp
->dad_timer_ch
, ticks
,
1038 (void (*) __P((void *)))nd6_dad_timer
, (void *)dp
->dad_ifa
);
1042 nd6_dad_stoptimer(dp
)
1046 callout_stop(&dp
->dad_timer_ch
);
1051 * Start Duplicated Address Detection (DAD) for specified interface address.
1054 nd6_dad_start(ifa
, tick
)
1056 int *tick
; /* minimum delay ticks for IFF_UP event */
1058 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1067 * If we don't need DAD, don't do it.
1068 * There are several cases:
1069 * - DAD is disabled (ip6_dad_count == 0)
1070 * - the interface address is anycast
1072 if (!(ia
->ia6_flags
& IN6_IFF_TENTATIVE
)) {
1074 "nd6_dad_start: called with non-tentative address "
1076 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1077 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1080 if (ia
->ia6_flags
& IN6_IFF_ANYCAST
) {
1081 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1084 if (!ip6_dad_count
) {
1085 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1089 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1090 if (!(ifa
->ifa_ifp
->if_flags
& IFF_UP
))
1092 if (nd6_dad_find(ifa
) != NULL
) {
1093 /* DAD already in progress */
1097 dp
= _MALLOC(sizeof(*dp
), M_IP6NDP
, M_NOWAIT
);
1099 log(LOG_ERR
, "nd6_dad_start: memory allocation failed for "
1101 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1102 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1105 bzero(dp
, sizeof(*dp
));
1106 TAILQ_INSERT_TAIL(&dadq
, (struct dadq
*)dp
, dad_list
);
1108 nd6log((LOG_DEBUG
, "%s: starting DAD for %s\n", if_name(ifa
->ifa_ifp
),
1109 ip6_sprintf(&ia
->ia_addr
.sin6_addr
)));
1112 * Send NS packet for DAD, ip6_dad_count times.
1113 * Note that we must delay the first transmission, if this is the
1114 * first packet to be sent from the interface after interface
1115 * (re)initialization.
1118 ifaref(ifa
); /*just for safety*/
1119 dp
->dad_count
= ip6_dad_count
;
1120 dp
->dad_ns_icount
= dp
->dad_na_icount
= 0;
1121 dp
->dad_ns_ocount
= dp
->dad_ns_tcount
= 0;
1123 nd6_dad_ns_output(dp
, ifa
);
1124 timeout((void (*) __P((void *)))nd6_dad_timer_funnel
, (void *)ifa
,
1125 nd_ifinfo
[ifa
->ifa_ifp
->if_index
].retrans
* hz
/ 1000);
1130 ntick
= random() % (MAX_RTR_SOLICITATION_DELAY
* hz
);
1132 ntick
= *tick
+ random() % (hz
/ 2);
1134 timeout((void (*) __P((void *)))nd6_dad_timer_funnel
, (void *)ifa
,
1140 * terminate DAD unconditionally. used for address removals.
1150 dp
= nd6_dad_find(ifa
);
1152 /* DAD wasn't started yet */
1156 untimeout((void (*) __P((void *)))nd6_dad_timer_funnel
, (void *)ifa
);
1158 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1166 nd6_dad_timer_funnel(ifa
)
1171 boolean_t funnel_state
;
1172 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
1176 (void) thread_funnel_set(network_flock
, FALSE
);
1186 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1189 s
= splnet(); /* XXX */
1193 log(LOG_ERR
, "nd6_dad_timer: called with null parameter\n");
1196 dp
= nd6_dad_find(ifa
);
1198 log(LOG_ERR
, "nd6_dad_timer: DAD structure not found\n");
1201 if (ia
->ia6_flags
& IN6_IFF_DUPLICATED
) {
1202 log(LOG_ERR
, "nd6_dad_timer: called with duplicated address "
1204 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1205 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1208 if ((ia
->ia6_flags
& IN6_IFF_TENTATIVE
) == 0) {
1209 log(LOG_ERR
, "nd6_dad_timer: called with non-tentative address "
1211 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1212 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1216 /* timeouted with IFF_{RUNNING,UP} check */
1217 if (dp
->dad_ns_tcount
> dad_maxtry
) {
1218 nd6log((LOG_INFO
, "%s: could not run DAD, driver problem?\n",
1219 if_name(ifa
->ifa_ifp
)));
1221 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1228 /* Need more checks? */
1229 if (dp
->dad_ns_ocount
< dp
->dad_count
) {
1231 * We have more NS to go. Send NS packet for DAD.
1233 nd6_dad_ns_output(dp
, ifa
);
1234 timeout((void (*) __P((void *)))nd6_dad_timer_funnel
, (void *)ifa
,
1235 nd_ifinfo
[ifa
->ifa_ifp
->if_index
].retrans
* hz
/ 1000);
1238 * We have transmitted sufficient number of DAD packets.
1239 * See what we've got.
1245 if (dp
->dad_na_icount
) {
1247 * the check is in nd6_dad_na_input(),
1253 if (dp
->dad_ns_icount
) {
1254 #if 0 /* heuristics */
1257 * - we have sent many(?) DAD NS, and
1258 * - the number of NS we sent equals to the
1259 * number of NS we've got, and
1261 * we may have a faulty network card/driver which
1262 * loops back multicasts to myself.
1264 if (3 < dp
->dad_count
1265 && dp
->dad_ns_icount
== dp
->dad_count
1266 && dp
->dad_na_icount
== 0) {
1267 log(LOG_INFO
, "DAD questionable for %s(%s): "
1268 "network card loops back multicast?\n",
1269 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1270 if_name(ifa
->ifa_ifp
));
1271 /* XXX consider it a duplicate or not? */
1274 /* We've seen NS, means DAD has failed. */
1278 /* We've seen NS, means DAD has failed. */
1284 /* (*dp) will be freed in nd6_dad_duplicated() */
1286 nd6_dad_duplicated(ifa
);
1289 * We are done with DAD. No NA came, no NS came.
1290 * duplicated address found.
1292 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1295 "%s: DAD complete for %s - no duplicates found\n",
1296 if_name(ifa
->ifa_ifp
),
1297 ip6_sprintf(&ia
->ia_addr
.sin6_addr
)));
1299 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1311 nd6_dad_duplicated(ifa
)
1314 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1317 dp
= nd6_dad_find(ifa
);
1319 log(LOG_ERR
, "nd6_dad_duplicated: DAD structure not found\n");
1323 log(LOG_ERR
, "%s: DAD detected duplicate IPv6 address %s: "
1324 "NS in/out=%d/%d, NA in=%d\n",
1325 if_name(ifa
->ifa_ifp
), ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1326 dp
->dad_ns_icount
, dp
->dad_ns_ocount
, dp
->dad_na_icount
);
1328 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1329 ia
->ia6_flags
|= IN6_IFF_DUPLICATED
;
1331 /* We are done with DAD, with duplicated address found. (failure) */
1332 untimeout((void (*) __P((void *)))nd6_dad_timer_funnel
, (void *)ifa
);
1335 log(LOG_ERR
, "%s: DAD complete for %s - duplicate found\n",
1336 if_name(ifa
->ifa_ifp
), ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1337 log(LOG_ERR
, "%s: manual intervention required\n",
1338 if_name(ifa
->ifa_ifp
));
1340 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1347 nd6_dad_ns_output(dp
, ifa
)
1351 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1352 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1354 dp
->dad_ns_tcount
++;
1355 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1357 printf("%s: interface down?\n", if_name(ifp
));
1361 if ((ifp
->if_flags
& IFF_RUNNING
) == 0) {
1363 printf("%s: interface not running?\n", if_name(ifp
));
1368 dp
->dad_ns_ocount
++;
1369 nd6_ns_output(ifp
, NULL
, &ia
->ia_addr
.sin6_addr
, NULL
, 1);
1373 nd6_dad_ns_input(ifa
)
1376 struct in6_ifaddr
*ia
;
1378 const struct in6_addr
*taddr6
;
1383 panic("ifa == NULL in nd6_dad_ns_input");
1385 ia
= (struct in6_ifaddr
*)ifa
;
1387 taddr6
= &ia
->ia_addr
.sin6_addr
;
1389 dp
= nd6_dad_find(ifa
);
1391 /* Quickhack - completely ignore DAD NS packets */
1392 if (dad_ignore_ns
) {
1394 "nd6_dad_ns_input: ignoring DAD NS packet for "
1395 "address %s(%s)\n", ip6_sprintf(taddr6
),
1396 if_name(ifa
->ifa_ifp
)));
1401 * if I'm yet to start DAD, someone else started using this address
1402 * first. I have a duplicate and you win.
1404 if (!dp
|| dp
->dad_ns_ocount
== 0)
1407 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1410 dp
= NULL
; /* will be freed in nd6_dad_duplicated() */
1411 nd6_dad_duplicated(ifa
);
1414 * not sure if I got a duplicate.
1415 * increment ns count and see what happens.
1418 dp
->dad_ns_icount
++;
1423 nd6_dad_na_input(ifa
)
1429 panic("ifa == NULL in nd6_dad_na_input");
1431 dp
= nd6_dad_find(ifa
);
1433 dp
->dad_na_icount
++;
1435 /* remove the address. */
1436 nd6_dad_duplicated(ifa
);