2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
37 #include "opt_ipsec.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
47 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/queue.h>
51 #include <sys/callout.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/if_var.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet6/in6_var.h>
62 #include <netinet6/in6_ifattach.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/scope6_var.h>
66 #include <netinet6/nd6.h>
67 #include <netinet/icmp6.h>
70 #include <netinet/ip_carp.h>
73 #define SDL(s) ((struct sockaddr_dl *)s)
76 static struct dadq *nd6_dad_find(struct ifaddr *);
77 static void nd6_dad_starttimer(struct dadq *, int);
78 static void nd6_dad_stoptimer(struct dadq *);
79 static void nd6_dad_timer(struct ifaddr *);
80 static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
81 static void nd6_dad_ns_input(struct ifaddr *);
82 static void nd6_dad_na_input(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 a Neighbor Solicitation Message.
91 * Based on RFC 2462 (duplicate address detection)
94 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
96 struct ifnet *ifp = m->m_pkthdr.rcvif;
97 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
98 struct nd_neighbor_solicit *nd_ns;
99 struct in6_addr saddr6 = ip6->ip6_src;
100 struct in6_addr daddr6 = ip6->ip6_dst;
101 struct in6_addr taddr6;
102 struct in6_addr myaddr6;
104 struct ifaddr *ifa = NULL;
106 int anycast = 0, proxy = 0, tentative = 0;
108 union nd_opts ndopts;
109 struct sockaddr_dl *proxydl = NULL;
110 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
112 #ifndef PULLDOWN_TEST
113 IP6_EXTHDR_CHECK(m, off, icmp6len,);
114 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
116 IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
118 icmp6stat.icp6s_tooshort++;
122 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
123 taddr6 = nd_ns->nd_ns_target;
124 if (in6_setscope(&taddr6, ifp, NULL) != 0)
127 if (ip6->ip6_hlim != 255) {
129 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
130 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
131 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
135 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
136 /* dst has to be a solicited node multicast address. */
137 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
138 /* don't check ifindex portion */
139 daddr6.s6_addr32[1] == 0 &&
140 daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
141 daddr6.s6_addr8[12] == 0xff) {
144 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
145 "(wrong ip6 dst)\n"));
150 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
151 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
155 icmp6len -= sizeof(*nd_ns);
156 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
157 if (nd6_options(&ndopts) < 0) {
159 "nd6_ns_input: invalid ND option, ignored\n"));
160 /* nd6_options have incremented stats */
164 if (ndopts.nd_opts_src_lladdr) {
165 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
166 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
169 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
170 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
171 "(link-layer address option)\n"));
176 * Attaching target link-layer address to the NA?
179 * NS IP dst is unicast/anycast MUST NOT add
180 * NS IP dst is solicited-node multicast MUST add
182 * In implementation, we add target link-layer address by default.
183 * We do not add one in MUST NOT cases.
185 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
191 * Target address (taddr6) must be either:
192 * (1) Valid unicast/anycast address for my receiving interface,
193 * (2) Unicast address for which I'm offering proxy service, or
194 * (3) "tentative" address on which DAD is being performed.
196 /* (1) and (3) check. */
199 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
201 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
203 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
209 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 need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
219 rt->rt_gateway->sa_family == AF_LINK);
224 * proxy NDP for single entry
226 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
227 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
230 proxydl = SDL(rt->rt_gateway);
236 * We've got an NS packet, and we don't have that adddress
237 * assigned for us. We MUST silently ignore it.
242 myaddr6 = *IFA_IN6(ifa);
243 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
244 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
245 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
248 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
249 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
250 "(if %d, NS packet %d)\n",
251 ip6_sprintf(ip6bufs, &taddr6),
252 ifp->if_addrlen, lladdrlen - 2));
256 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
257 nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
258 ip6_sprintf(ip6bufs, &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 * duplicate 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 struct in6_addr in6_all;
299 in6_all = in6addr_linklocal_allnodes;
300 if (in6_setscope(&in6_all, ifp, NULL) != 0)
302 nd6_na_output(ifp, &in6_all, &taddr6,
303 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
304 (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
305 tlladdr, (struct sockaddr *)proxydl);
309 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
310 ND_NEIGHBOR_SOLICIT, 0);
312 nd6_na_output(ifp, &saddr6, &taddr6,
313 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
314 (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
315 tlladdr, (struct sockaddr *)proxydl);
321 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
322 ip6_sprintf(ip6bufs, &saddr6)));
323 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
324 ip6_sprintf(ip6bufs, &daddr6)));
325 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
326 ip6_sprintf(ip6bufs, &taddr6)));
327 icmp6stat.icp6s_badns++;
332 * Output a Neighbor Solicitation Message. Caller specifies:
333 * - ICMP6 header source IP6 address
334 * - ND6 header target IP6 address
335 * - ND6 header source datalink address
338 * Based on RFC 2462 (duplicate address detection)
340 * ln - for source address determination
341 * dad - duplicate address detection
344 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
345 const struct in6_addr *taddr6, struct llinfo_nd6 *ln, int dad)
349 struct nd_neighbor_solicit *nd_ns;
350 struct in6_addr *src, src_in;
351 struct ip6_moptions im6o;
357 bzero(&ro, sizeof(ro));
359 if (IN6_IS_ADDR_MULTICAST(taddr6))
362 /* estimate the size of message */
363 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
364 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
365 if (max_linkhdr + maxlen >= MCLBYTES) {
367 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
368 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
373 MGETHDR(m, M_DONTWAIT, MT_DATA);
374 if (m && max_linkhdr + maxlen >= MHLEN) {
375 MCLGET(m, M_DONTWAIT);
376 if ((m->m_flags & M_EXT) == 0) {
383 m->m_pkthdr.rcvif = NULL;
385 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
386 m->m_flags |= M_MCAST;
387 im6o.im6o_multicast_ifp = ifp;
388 im6o.im6o_multicast_hlim = 255;
389 im6o.im6o_multicast_loop = 0;
392 icmp6len = sizeof(*nd_ns);
393 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
394 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
396 /* fill neighbor solicitation packet */
397 ip6 = mtod(m, struct ip6_hdr *);
399 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
400 ip6->ip6_vfc |= IPV6_VERSION;
401 /* ip6->ip6_plen will be set later */
402 ip6->ip6_nxt = IPPROTO_ICMPV6;
405 ip6->ip6_dst = *daddr6;
407 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
408 ip6->ip6_dst.s6_addr16[1] = 0;
409 ip6->ip6_dst.s6_addr32[1] = 0;
410 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
411 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
412 ip6->ip6_dst.s6_addr8[12] = 0xff;
413 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
419 * "If the source address of the packet prompting the
420 * solicitation is the same as one of the addresses assigned
421 * to the outgoing interface, that address SHOULD be placed
422 * in the IP Source Address of the outgoing solicitation.
423 * Otherwise, any one of the addresses assigned to the
424 * interface should be used."
426 * We use the source address for the prompting packet
428 * - saddr6 is given from the caller (by giving "ln"), and
429 * - saddr6 belongs to the outgoing interface.
430 * Otherwise, we perform the source address selection as usual.
432 struct ip6_hdr *hip6; /* hold ip6 */
433 struct in6_addr *hsrc = NULL;
435 if (ln && ln->ln_hold) {
437 * assuming every packet in ln_hold has the same IP
440 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
442 if (sizeof(*hip6) < ln->ln_hold->m_len)
443 hsrc = &hip6->ip6_src;
447 if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
451 struct sockaddr_in6 dst_sa;
453 bzero(&dst_sa, sizeof(dst_sa));
454 dst_sa.sin6_family = AF_INET6;
455 dst_sa.sin6_len = sizeof(dst_sa);
456 dst_sa.sin6_addr = ip6->ip6_dst;
458 src = in6_selectsrc(&dst_sa, NULL,
459 NULL, &ro, NULL, NULL, &error);
461 char ip6buf[INET6_ADDRSTRLEN];
463 "nd6_ns_output: source can't be "
464 "determined: dst=%s, error=%d\n",
465 ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
472 * Source address for DAD packet must always be IPv6
473 * unspecified address. (0::0)
474 * We actually don't have to 0-clear the address (we did it
475 * above), but we do so here explicitly to make the intention
478 bzero(&src_in, sizeof(src_in));
482 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
483 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
484 nd_ns->nd_ns_code = 0;
485 nd_ns->nd_ns_reserved = 0;
486 nd_ns->nd_ns_target = *taddr6;
487 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
490 * Add source link-layer address option.
492 * spec implementation
494 * DAD packet MUST NOT do not add the option
495 * there's no link layer address:
496 * impossible do not add the option
497 * there's link layer address:
498 * Multicast NS MUST add one add the option
499 * Unicast NS SHOULD add one add the option
501 if (!dad && (mac = nd6_ifptomac(ifp))) {
502 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
503 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
504 /* 8 byte alignments... */
505 optlen = (optlen + 7) & ~7;
507 m->m_pkthdr.len += optlen;
510 bzero((caddr_t)nd_opt, optlen);
511 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
512 nd_opt->nd_opt_len = optlen >> 3;
513 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
516 ip6->ip6_plen = htons((u_short)icmp6len);
517 nd_ns->nd_ns_cksum = 0;
519 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
521 ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
522 icmp6_ifstat_inc(ifp, ifs6_out_msg);
523 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
524 icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
526 if (ro.ro_rt) { /* we don't cache this route. */
540 * Neighbor advertisement input handling.
543 * Based on RFC 2462 (duplicate address detection)
545 * the following items are not implemented yet:
546 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
547 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
550 nd6_na_input(struct mbuf *m, int off, int icmp6len)
552 struct ifnet *ifp = m->m_pkthdr.rcvif;
553 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
554 struct nd_neighbor_advert *nd_na;
555 struct in6_addr daddr6 = ip6->ip6_dst;
556 struct in6_addr taddr6;
564 struct llinfo_nd6 *ln;
566 struct sockaddr_dl *sdl;
567 union nd_opts ndopts;
568 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
570 if (ip6->ip6_hlim != 255) {
572 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
573 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
574 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
578 #ifndef PULLDOWN_TEST
579 IP6_EXTHDR_CHECK(m, off, icmp6len,);
580 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
582 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
584 icmp6stat.icp6s_tooshort++;
589 flags = nd_na->nd_na_flags_reserved;
590 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
591 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
592 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
594 taddr6 = nd_na->nd_na_target;
595 if (in6_setscope(&taddr6, ifp, NULL))
596 goto bad; /* XXX: impossible */
598 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
600 "nd6_na_input: invalid target address %s\n",
601 ip6_sprintf(ip6bufs, &taddr6)));
604 if (IN6_IS_ADDR_MULTICAST(&daddr6))
607 "nd6_na_input: a solicited adv is multicasted\n"));
611 icmp6len -= sizeof(*nd_na);
612 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
613 if (nd6_options(&ndopts) < 0) {
615 "nd6_na_input: invalid ND option, ignored\n"));
616 /* nd6_options have incremented stats */
620 if (ndopts.nd_opts_tgt_lladdr) {
621 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
622 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
625 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
628 * Target address matches one of my interface address.
630 * If my address is tentative, this means that there's somebody
631 * already using the same address as mine. This indicates DAD failure.
632 * This is defined in RFC 2462.
634 * Otherwise, process as defined in RFC 2461.
637 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
638 nd6_dad_na_input(ifa);
642 /* Just for safety, maybe unnecessary. */
645 "nd6_na_input: duplicate IP6 address %s\n",
646 ip6_sprintf(ip6bufs, &taddr6));
650 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
651 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
652 "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
653 ifp->if_addrlen, lladdrlen - 2));
658 * If no neighbor cache entry is found, NA SHOULD silently be
661 rt = nd6_lookup(&taddr6, 0, ifp);
663 ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
664 ((sdl = SDL(rt->rt_gateway)) == NULL))
667 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
669 * If the link-layer has address, and no lladdr option came,
670 * discard the packet.
672 if (ifp->if_addrlen && lladdr == NULL)
676 * Record link-layer address, and update the state.
678 sdl->sdl_alen = ifp->if_addrlen;
679 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
681 ln->ln_state = ND6_LLINFO_REACHABLE;
683 if (!ND6_LLINFO_PERMANENT(ln)) {
684 nd6_llinfo_settimer(ln,
685 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
688 ln->ln_state = ND6_LLINFO_STALE;
689 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
691 if ((ln->ln_router = is_router) != 0) {
693 * This means a router's state has changed from
694 * non-reachable to probably reachable, and might
695 * affect the status of associated prefixes..
697 pfxlist_onlink_check();
703 * Check if the link-layer address has changed or not.
709 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
718 * This is VERY complex. Look at it with care.
720 * override solicit lladdr llchange action
725 * 0 0 y y (1) REACHABLE->STALE
726 * 0 1 n -- (2c) *->REACHABLE
727 * 0 1 y n (2b) L *->REACHABLE
728 * 0 1 y y (1) REACHABLE->STALE
731 * 1 0 y y (2a) L *->STALE
732 * 1 1 n -- (2a) *->REACHABLE
733 * 1 1 y n (2a) L *->REACHABLE
734 * 1 1 y y (2a) L *->REACHABLE
736 if (!is_override && (lladdr != NULL && llchange)) { /* (1) */
738 * If state is REACHABLE, make it STALE.
739 * no other updates should be done.
741 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
742 ln->ln_state = ND6_LLINFO_STALE;
743 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
746 } else if (is_override /* (2a) */
747 || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
748 || lladdr == NULL) { /* (2c) */
750 * Update link-local address, if any.
752 if (lladdr != NULL) {
753 sdl->sdl_alen = ifp->if_addrlen;
754 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
758 * If solicited, make the state REACHABLE.
759 * If not solicited and the link-layer address was
760 * changed, make it STALE.
763 ln->ln_state = ND6_LLINFO_REACHABLE;
765 if (!ND6_LLINFO_PERMANENT(ln)) {
766 nd6_llinfo_settimer(ln,
767 (long)ND_IFINFO(ifp)->reachable * hz);
770 if (lladdr != NULL && llchange) {
771 ln->ln_state = ND6_LLINFO_STALE;
772 nd6_llinfo_settimer(ln,
773 (long)nd6_gctimer * hz);
778 if (ln->ln_router && !is_router) {
780 * The peer dropped the router flag.
781 * Remove the sender from the Default Router List and
782 * update the Destination Cache entries.
784 struct nd_defrouter *dr;
785 struct in6_addr *in6;
788 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
791 * Lock to protect the default router list.
792 * XXX: this might be unnecessary, since this function
793 * is only called under the network software interrupt
794 * context. However, we keep it just for safety.
797 dr = defrouter_lookup(in6, ifp);
800 else if (!ip6_forwarding) {
802 * Even if the neighbor is not in the default
803 * router list, the neighbor may be used
804 * as a next hop for some destinations
805 * (e.g. redirect case). So we must
806 * call rt6_flush explicitly.
808 rt6_flush(&ip6->ip6_src, ifp);
812 ln->ln_router = is_router;
814 rt->rt_flags &= ~RTF_REJECT;
817 struct mbuf *m_hold, *m_hold_next;
820 * reset the ln_hold in advance, to explicitly
821 * prevent a ln_hold lookup in nd6_output()
822 * (wouldn't happen, though...)
824 for (m_hold = ln->ln_hold;
825 m_hold; m_hold = m_hold_next) {
826 m_hold_next = m_hold->m_nextpkt;
827 m_hold->m_nextpkt = NULL;
829 * we assume ifp is not a loopback here, so just set
830 * the 2nd argument as the 1st one.
832 nd6_output(ifp, ifp, m_hold,
833 (struct sockaddr_in6 *)rt_key(rt), rt);
843 icmp6stat.icp6s_badna++;
848 * Neighbor advertisement output handling.
852 * the following items are not implemented yet:
853 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
854 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
856 * tlladdr - 1 if include target link-layer address
857 * sdl0 - sockaddr_dl (= proxy NA) or NULL
860 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
861 const struct in6_addr *taddr6, u_long flags, int tlladdr,
862 struct sockaddr *sdl0)
866 struct nd_neighbor_advert *nd_na;
867 struct ip6_moptions im6o;
868 struct in6_addr *src, daddr6;
869 struct sockaddr_in6 dst_sa;
870 int icmp6len, maxlen, error;
874 bzero(&ro, sizeof(ro));
876 daddr6 = *daddr6_0; /* make a local copy for modification */
878 /* estimate the size of message */
879 maxlen = sizeof(*ip6) + sizeof(*nd_na);
880 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
881 if (max_linkhdr + maxlen >= MCLBYTES) {
883 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
884 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
889 MGETHDR(m, M_DONTWAIT, MT_DATA);
890 if (m && max_linkhdr + maxlen >= MHLEN) {
891 MCLGET(m, M_DONTWAIT);
892 if ((m->m_flags & M_EXT) == 0) {
899 m->m_pkthdr.rcvif = NULL;
901 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
902 m->m_flags |= M_MCAST;
903 im6o.im6o_multicast_ifp = ifp;
904 im6o.im6o_multicast_hlim = 255;
905 im6o.im6o_multicast_loop = 0;
908 icmp6len = sizeof(*nd_na);
909 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
910 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
912 /* fill neighbor advertisement packet */
913 ip6 = mtod(m, struct ip6_hdr *);
915 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
916 ip6->ip6_vfc |= IPV6_VERSION;
917 ip6->ip6_nxt = IPPROTO_ICMPV6;
919 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
921 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
922 daddr6.s6_addr16[1] = 0;
923 daddr6.s6_addr32[1] = 0;
924 daddr6.s6_addr32[2] = 0;
925 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
926 if (in6_setscope(&daddr6, ifp, NULL))
929 flags &= ~ND_NA_FLAG_SOLICITED;
931 ip6->ip6_dst = daddr6;
932 bzero(&dst_sa, sizeof(struct sockaddr_in6));
933 dst_sa.sin6_family = AF_INET6;
934 dst_sa.sin6_len = sizeof(struct sockaddr_in6);
935 dst_sa.sin6_addr = daddr6;
938 * Select a source whose scope is the same as that of the dest.
940 bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
941 src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error);
943 char ip6buf[INET6_ADDRSTRLEN];
944 nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
945 "determined: dst=%s, error=%d\n",
946 ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
950 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
951 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
952 nd_na->nd_na_code = 0;
953 nd_na->nd_na_target = *taddr6;
954 in6_clearscope(&nd_na->nd_na_target); /* XXX */
957 * "tlladdr" indicates NS's condition for adding tlladdr or not.
958 * see nd6_ns_input() for details.
959 * Basically, if NS packet is sent to unicast/anycast addr,
960 * target lladdr option SHOULD NOT be included.
964 * sdl0 != NULL indicates proxy NA. If we do proxy, use
965 * lladdr in sdl0. If we are not proxying (sending NA for
966 * my address) use lladdr configured for the interface.
971 mac = carp_macmatch6(ifp->if_carp, m, taddr6);
973 mac = nd6_ifptomac(ifp);
975 mac = nd6_ifptomac(ifp);
977 } else if (sdl0->sa_family == AF_LINK) {
978 struct sockaddr_dl *sdl;
979 sdl = (struct sockaddr_dl *)sdl0;
980 if (sdl->sdl_alen == ifp->if_addrlen)
984 if (tlladdr && mac) {
985 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
986 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
988 /* roundup to 8 bytes alignment! */
989 optlen = (optlen + 7) & ~7;
991 m->m_pkthdr.len += optlen;
994 bzero((caddr_t)nd_opt, optlen);
995 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
996 nd_opt->nd_opt_len = optlen >> 3;
997 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
999 flags &= ~ND_NA_FLAG_OVERRIDE;
1001 ip6->ip6_plen = htons((u_short)icmp6len);
1002 nd_na->nd_na_flags_reserved = flags;
1003 nd_na->nd_na_cksum = 0;
1004 nd_na->nd_na_cksum =
1005 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1007 ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1008 icmp6_ifstat_inc(ifp, ifs6_out_msg);
1009 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1010 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1012 if (ro.ro_rt) { /* we don't cache this route. */
1026 nd6_ifptomac(struct ifnet *ifp)
1028 switch (ifp->if_type) {
1036 #ifdef IFT_IEEE80211
1044 return IF_LLADDR(ifp);
1050 TAILQ_HEAD(dadq_head, dadq);
1052 TAILQ_ENTRY(dadq) dad_list;
1053 struct ifaddr *dad_ifa;
1054 int dad_count; /* max NS to send */
1055 int dad_ns_tcount; /* # of trials to send NS */
1056 int dad_ns_ocount; /* NS sent so far */
1059 struct callout dad_timer_ch;
1062 static struct dadq_head dadq;
1063 static int dad_init = 0;
1065 static struct dadq *
1066 nd6_dad_find(struct ifaddr *ifa)
1070 for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
1071 if (dp->dad_ifa == ifa)
1078 nd6_dad_starttimer(struct dadq *dp, int ticks)
1081 callout_reset(&dp->dad_timer_ch, ticks,
1082 (void (*)(void *))nd6_dad_timer, (void *)dp->dad_ifa);
1086 nd6_dad_stoptimer(struct dadq *dp)
1089 callout_stop(&dp->dad_timer_ch);
1093 * Start Duplicate Address Detection (DAD) for specified interface address.
1096 nd6_dad_start(struct ifaddr *ifa, int delay)
1098 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1100 char ip6buf[INET6_ADDRSTRLEN];
1108 * If we don't need DAD, don't do it.
1109 * There are several cases:
1110 * - DAD is disabled (ip6_dad_count == 0)
1111 * - the interface address is anycast
1113 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1115 "nd6_dad_start: called with non-tentative address "
1117 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1118 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1121 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1122 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1125 if (!ip6_dad_count) {
1126 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1129 if (ifa->ifa_ifp == NULL)
1130 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1131 if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
1134 if (nd6_dad_find(ifa) != NULL) {
1135 /* DAD already in progress */
1139 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1141 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1143 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1144 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1147 bzero(dp, sizeof(*dp));
1148 callout_init(&dp->dad_timer_ch, 0);
1149 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1151 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1152 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1155 * Send NS packet for DAD, ip6_dad_count times.
1156 * Note that we must delay the first transmission, if this is the
1157 * first packet to be sent from the interface after interface
1158 * (re)initialization.
1161 IFAREF(ifa); /* just for safety */
1162 dp->dad_count = ip6_dad_count;
1163 dp->dad_ns_icount = dp->dad_na_icount = 0;
1164 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1166 nd6_dad_ns_output(dp, ifa);
1167 nd6_dad_starttimer(dp,
1168 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1170 nd6_dad_starttimer(dp, delay);
1175 * terminate DAD unconditionally. used for address removals.
1178 nd6_dad_stop(struct ifaddr *ifa)
1184 dp = nd6_dad_find(ifa);
1186 /* DAD wasn't started yet */
1190 nd6_dad_stoptimer(dp);
1192 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1199 nd6_dad_timer(struct ifaddr *ifa)
1202 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1204 char ip6buf[INET6_ADDRSTRLEN];
1206 s = splnet(); /* XXX */
1210 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1213 dp = nd6_dad_find(ifa);
1215 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1218 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1219 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1221 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1222 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1225 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1226 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1228 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1229 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1233 /* timeouted with IFF_{RUNNING,UP} check */
1234 if (dp->dad_ns_tcount > dad_maxtry) {
1235 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1236 if_name(ifa->ifa_ifp)));
1238 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1245 /* Need more checks? */
1246 if (dp->dad_ns_ocount < dp->dad_count) {
1248 * We have more NS to go. Send NS packet for DAD.
1250 nd6_dad_ns_output(dp, ifa);
1251 nd6_dad_starttimer(dp,
1252 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1255 * We have transmitted sufficient number of DAD packets.
1256 * See what we've got.
1262 if (dp->dad_na_icount) {
1264 * the check is in nd6_dad_na_input(),
1270 if (dp->dad_ns_icount) {
1271 /* We've seen NS, means DAD has failed. */
1276 /* (*dp) will be freed in nd6_dad_duplicated() */
1278 nd6_dad_duplicated(ifa);
1281 * We are done with DAD. No NA came, no NS came.
1282 * No duplicate address found.
1284 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1287 "%s: DAD complete for %s - no duplicates found\n",
1288 if_name(ifa->ifa_ifp),
1289 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1291 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1303 nd6_dad_duplicated(struct ifaddr *ifa)
1305 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1308 char ip6buf[INET6_ADDRSTRLEN];
1310 dp = nd6_dad_find(ifa);
1312 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1316 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1317 "NS in/out=%d/%d, NA in=%d\n",
1318 if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1319 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1321 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1322 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1324 /* We are done with DAD, with duplicate address found. (failure) */
1325 nd6_dad_stoptimer(dp);
1328 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1329 if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
1330 log(LOG_ERR, "%s: manual intervention required\n",
1334 * If the address is a link-local address formed from an interface
1335 * identifier based on the hardware address which is supposed to be
1336 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1337 * operation on the interface SHOULD be disabled.
1338 * [rfc2462bis-03 Section 5.4.5]
1340 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1341 struct in6_addr in6;
1344 * To avoid over-reaction, we only apply this logic when we are
1345 * very sure that hardware addresses are supposed to be unique.
1347 switch (ifp->if_type) {
1352 #ifdef IFT_IEEE80211
1355 in6 = ia->ia_addr.sin6_addr;
1356 if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1357 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1358 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1359 log(LOG_ERR, "%s: possible hardware address "
1360 "duplication detected, disable IPv6\n",
1367 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1374 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1376 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1377 struct ifnet *ifp = ifa->ifa_ifp;
1379 dp->dad_ns_tcount++;
1380 if ((ifp->if_flags & IFF_UP) == 0) {
1383 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1387 dp->dad_ns_ocount++;
1388 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1392 nd6_dad_ns_input(struct ifaddr *ifa)
1394 struct in6_ifaddr *ia;
1396 const struct in6_addr *taddr6;
1401 panic("ifa == NULL in nd6_dad_ns_input");
1403 ia = (struct in6_ifaddr *)ifa;
1405 taddr6 = &ia->ia_addr.sin6_addr;
1407 dp = nd6_dad_find(ifa);
1409 /* Quickhack - completely ignore DAD NS packets */
1410 if (dad_ignore_ns) {
1411 char ip6buf[INET6_ADDRSTRLEN];
1413 "nd6_dad_ns_input: ignoring DAD NS packet for "
1414 "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
1415 if_name(ifa->ifa_ifp)));
1420 * if I'm yet to start DAD, someone else started using this address
1421 * first. I have a duplicate and you win.
1423 if (dp == NULL || dp->dad_ns_ocount == 0)
1426 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1429 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1430 nd6_dad_duplicated(ifa);
1433 * not sure if I got a duplicate.
1434 * increment ns count and see what happens.
1437 dp->dad_ns_icount++;
1442 nd6_dad_na_input(struct ifaddr *ifa)
1447 panic("ifa == NULL in nd6_dad_na_input");
1449 dp = nd6_dad_find(ifa);
1451 dp->dad_na_icount++;
1453 /* remove the address. */
1454 nd6_dad_duplicated(ifa);