1 /* $FreeBSD: src/sys/netinet6/nd6_nbr.c,v 1.4.2.6 2003/01/23 21:06:47 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/nd6_nbr.c,v 1.24 2008/10/03 07:59:20 hasso Exp $ */
3 /* $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
46 #include <sys/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/syslog.h>
49 #include <sys/queue.h>
50 #include <sys/callout.h>
51 #include <sys/mutex.h>
53 #include <sys/thread2.h>
54 #include <sys/mutex2.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet6/in6_var.h>
64 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/nd6.h>
67 #include <netinet/icmp6.h>
70 #include <netinet6/ipsec.h>
72 #include <netinet6/ipsec6.h>
76 #include <net/net_osdep.h>
79 #include <netinet/ip_carp.h>
83 #define SDL(s) ((struct sockaddr_dl *)s)
86 static struct dadq *nd6_dad_find (struct ifaddr *);
87 static void nd6_dad_starttimer (struct dadq *, int);
88 static void nd6_dad_stoptimer (struct dadq *);
89 static void nd6_dad_timer (struct ifaddr *);
90 static void nd6_dad_ns_output (struct dadq *, struct ifaddr *);
91 static void nd6_dad_ns_input (struct ifaddr *);
92 static void nd6_dad_na_input (struct ifaddr *);
94 static int dad_ignore_ns = 0; /* ignore NS in DAD - specwise incorrect*/
95 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
98 * Input an Neighbor Solicitation Message.
101 * Based on RFC 2462 (duplicated address detection)
104 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
106 struct ifnet *ifp = m->m_pkthdr.rcvif;
107 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
108 struct nd_neighbor_solicit *nd_ns;
109 struct in6_addr saddr6 = ip6->ip6_src;
110 struct in6_addr daddr6 = ip6->ip6_dst;
111 struct in6_addr taddr6;
112 struct in6_addr myaddr6;
114 struct ifaddr *ifa = NULL;
116 int anycast = 0, proxy = 0, tentative = 0;
118 union nd_opts ndopts;
119 struct sockaddr_dl *proxydl = NULL;
121 #ifndef PULLDOWN_TEST
122 IP6_EXTHDR_CHECK(m, off, icmp6len,);
123 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
125 IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
127 icmp6stat.icp6s_tooshort++;
131 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
132 taddr6 = nd_ns->nd_ns_target;
134 if (ip6->ip6_hlim != 255) {
136 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
137 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
138 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
142 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
143 /* dst has to be solicited node multicast address. */
144 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL
145 /* don't check ifindex portion */
146 && daddr6.s6_addr32[1] == 0
147 && daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE
148 && daddr6.s6_addr8[12] == 0xff) {
151 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
152 "(wrong ip6 dst)\n"));
157 * Make sure the source address is from a neighbor's address.
159 if (in6ifa_ifplocaladdr(ifp, &saddr6) == NULL) {
160 nd6log((LOG_INFO, "nd6_ns_input: "
161 "NS packet from non-neighbor\n"));
166 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
167 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
171 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
172 taddr6.s6_addr16[1] = htons(ifp->if_index);
174 icmp6len -= sizeof(*nd_ns);
175 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
176 if (nd6_options(&ndopts) < 0) {
178 "nd6_ns_input: invalid ND option, ignored\n"));
179 /* nd6_options have incremented stats */
183 if (ndopts.nd_opts_src_lladdr) {
184 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
185 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
188 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
189 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
190 "(link-layer address option)\n"));
195 * Attaching target link-layer address to the NA?
198 * NS IP dst is unicast/anycast MUST NOT add
199 * NS IP dst is solicited-node multicast MUST add
201 * In implementation, we add target link-layer address by default.
202 * We do not add one in MUST NOT cases.
204 #if 0 /* too much! */
205 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
206 if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
210 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
216 * Target address (taddr6) must be either:
217 * (1) Valid unicast/anycast address for my receiving interface,
218 * (2) Unicast address for which I'm offering proxy service, or
219 * (3) "tentative" address on which DAD is being performed.
221 /* (1) and (3) check. */
224 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
226 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
228 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
234 struct sockaddr_in6 tsin6;
236 bzero(&tsin6, sizeof tsin6);
237 tsin6.sin6_len = sizeof(struct sockaddr_in6);
238 tsin6.sin6_family = AF_INET6;
239 tsin6.sin6_addr = taddr6;
241 rt = rtpurelookup((struct sockaddr *)&tsin6);
242 if (rt != NULL && (rt->rt_flags & RTF_ANNOUNCE) &&
243 rt->rt_gateway->sa_family == AF_LINK) {
245 * proxy NDP for single entry
247 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
248 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
251 proxydl = SDL(rt->rt_gateway);
259 * We've got an NS packet, and we don't have that adddress
260 * assigned for us. We MUST silently ignore it.
265 myaddr6 = *IFA_IN6(ifa);
266 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
267 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
268 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
271 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
273 "nd6_ns_input: lladdrlen mismatch for %s "
274 "(if %d, NS packet %d)\n",
275 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
279 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
281 "nd6_ns_input: duplicate IP6 address %s\n",
282 ip6_sprintf(&saddr6)));
287 * We have neighbor solicitation packet, with target address equals to
288 * one of my tentative address.
290 * src addr how to process?
292 * multicast of course, invalid (rejected in ip6_input)
293 * unicast somebody is doing address resolution -> ignore
294 * unspec dup address detection
296 * The processing is defined in RFC 2462.
300 * If source address is unspecified address, it is for
301 * duplicated address detection.
303 * If not, the packet is for addess resolution;
304 * silently ignore it.
306 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
307 nd6_dad_ns_input(ifa);
313 * If the source address is unspecified address, entries must not
314 * be created or updated.
315 * It looks that sender is performing DAD. Output NA toward
316 * all-node multicast address, to tell the sender that I'm using
318 * S bit ("solicited") must be zero.
320 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
321 saddr6 = kin6addr_linklocal_allnodes;
322 saddr6.s6_addr16[1] = htons(ifp->if_index);
323 nd6_na_output(ifp, &saddr6, &taddr6,
324 ((anycast || proxy || !tlladdr)
325 ? 0 : ND_NA_FLAG_OVERRIDE)
326 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
327 tlladdr, (struct sockaddr *)proxydl);
331 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
333 nd6_na_output(ifp, &saddr6, &taddr6,
334 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE)
335 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
336 | ND_NA_FLAG_SOLICITED,
337 tlladdr, (struct sockaddr *)proxydl);
343 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)));
344 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)));
345 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)));
346 icmp6stat.icp6s_badns++;
351 * Output an Neighbor Solicitation Message. Caller specifies:
352 * - ICMP6 header source IP6 address
353 * - ND6 header target IP6 address
354 * - ND6 header source datalink address
357 * Based on RFC 2462 (duplicated address detection)
360 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
361 const struct in6_addr *taddr6,
362 struct llinfo_nd6 *ln, /* for source address determination */
363 int dad) /* duplicated address detection */
367 struct nd_neighbor_solicit *nd_ns;
368 struct in6_ifaddr *ia = NULL;
369 struct ip6_moptions im6o;
373 struct ifnet *outif = NULL;
375 if (IN6_IS_ADDR_MULTICAST(taddr6))
378 /* estimate the size of message */
379 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
380 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
381 if (max_linkhdr + maxlen >= MCLBYTES) {
383 kprintf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
384 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
389 m = m_getb(max_linkhdr + maxlen, MB_DONTWAIT, MT_DATA, M_PKTHDR);
393 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
394 m->m_flags |= M_MCAST;
395 im6o.im6o_multicast_ifp = ifp;
396 im6o.im6o_multicast_hlim = 255;
397 im6o.im6o_multicast_loop = 0;
400 icmp6len = sizeof(*nd_ns);
401 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
402 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
404 /* fill neighbor solicitation packet */
405 ip6 = mtod(m, struct ip6_hdr *);
407 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
408 ip6->ip6_vfc |= IPV6_VERSION;
409 /* ip6->ip6_plen will be set later */
410 ip6->ip6_nxt = IPPROTO_ICMPV6;
413 ip6->ip6_dst = *daddr6;
415 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
416 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
417 ip6->ip6_dst.s6_addr32[1] = 0;
418 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
419 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
420 ip6->ip6_dst.s6_addr8[12] = 0xff;
423 #if 0 /* KAME way, exact address scope match */
425 * Select a source whose scope is the same as that of the dest.
426 * Typically, the dest is link-local solicitation multicast
427 * (i.e. neighbor discovery) or link-local/global unicast
428 * (i.e. neighbor un-reachability detection).
430 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
435 ip6->ip6_src = ia->ia_addr.sin6_addr;
436 #else /* spec-wise correct */
439 * "If the source address of the packet prompting the
440 * solicitation is the same as one of the addresses assigned
441 * to the outgoing interface, that address SHOULD be placed
442 * in the IP Source Address of the outgoing solicitation.
443 * Otherwise, any one of the addresses assigned to the
444 * interface should be used."
446 * We use the source address for the prompting packet
448 * - saddr6 is given from the caller (by giving "ln"), and
449 * - saddr6 belongs to the outgoing interface.
450 * Otherwise, we perform a scope-wise match.
452 struct ip6_hdr *hip6; /* hold ip6 */
453 struct in6_addr *saddr6;
455 if (ln && ln->ln_hold) {
456 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
458 if (sizeof(*hip6) < ln->ln_hold->m_len)
459 saddr6 = &hip6->ip6_src;
464 if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
465 bcopy(saddr6, &ip6->ip6_src, sizeof(*saddr6));
467 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
472 ip6->ip6_src = ia->ia_addr.sin6_addr;
477 * Source address for DAD packet must always be IPv6
478 * unspecified address. (0::0)
480 bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
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;
488 if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns->nd_ns_target))
489 nd_ns->nd_ns_target.s6_addr16[1] = 0;
492 * Add source link-layer address option.
494 * spec implementation
496 * DAD packet MUST NOT do not add the option
497 * there's no link layer address:
498 * impossible do not add the option
499 * there's link layer address:
500 * Multicast NS MUST add one add the option
501 * Unicast NS SHOULD add one add the option
503 if (!dad && (mac = nd6_ifptomac(ifp))) {
504 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
505 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
506 /* 8 byte alignments... */
507 optlen = (optlen + 7) & ~7;
509 m->m_pkthdr.len += optlen;
512 bzero((caddr_t)nd_opt, optlen);
513 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
514 nd_opt->nd_opt_len = optlen >> 3;
515 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
518 ip6->ip6_plen = htons((u_short)icmp6len);
519 nd_ns->nd_ns_cksum = 0;
521 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
523 ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif, NULL);
525 icmp6_ifstat_inc(outif, ifs6_out_msg);
526 icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
528 icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
532 * Neighbor advertisement input handling.
535 * Based on RFC 2462 (duplicated address detection)
537 * the following items are not implemented yet:
538 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
539 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
542 nd6_na_input(struct mbuf *m, int off, int icmp6len)
544 struct ifnet *ifp = m->m_pkthdr.rcvif;
545 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
546 struct nd_neighbor_advert *nd_na;
547 struct in6_addr saddr6 = ip6->ip6_src;
548 struct in6_addr daddr6 = ip6->ip6_dst;
549 struct in6_addr taddr6;
557 struct llinfo_nd6 *ln;
559 struct sockaddr_dl *sdl;
560 union nd_opts ndopts;
562 if (ip6->ip6_hlim != 255) {
564 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
565 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
566 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
570 #ifndef PULLDOWN_TEST
571 IP6_EXTHDR_CHECK(m, off, icmp6len,);
572 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
574 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
576 icmp6stat.icp6s_tooshort++;
580 taddr6 = nd_na->nd_na_target;
581 flags = nd_na->nd_na_flags_reserved;
582 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
583 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
584 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
586 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
587 taddr6.s6_addr16[1] = htons(ifp->if_index);
589 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
591 "nd6_na_input: invalid target address %s\n",
592 ip6_sprintf(&taddr6)));
595 if (IN6_IS_ADDR_MULTICAST(&daddr6))
598 "nd6_na_input: a solicited adv is multicasted\n"));
602 icmp6len -= sizeof(*nd_na);
603 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
604 if (nd6_options(&ndopts) < 0) {
606 "nd6_na_input: invalid ND option, ignored\n"));
607 /* nd6_options have incremented stats */
611 if (ndopts.nd_opts_tgt_lladdr) {
612 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
613 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
616 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
619 * Target address matches one of my interface address.
621 * If my address is tentative, this means that there's somebody
622 * already using the same address as mine. This indicates DAD failure.
623 * This is defined in RFC 2462.
625 * Otherwise, process as defined in RFC 2461.
628 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
629 nd6_dad_na_input(ifa);
633 /* Just for safety, maybe unnecessary. */
636 "nd6_na_input: duplicate IP6 address %s\n",
637 ip6_sprintf(&taddr6));
642 * Make sure the source address is from a neighbor's address.
644 if (in6ifa_ifplocaladdr(ifp, &saddr6) == NULL) {
645 nd6log((LOG_INFO, "nd6_na_input: "
646 "NA packet from non-neighbor\n"));
650 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
652 "nd6_na_input: lladdrlen mismatch for %s "
653 "(if %d, NA packet %d)\n",
654 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
659 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
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)
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;
684 ln->ln_expire = time_second +
685 ND_IFINFO(rt->rt_ifp)->reachable;
687 ln->ln_state = ND6_LLINFO_STALE;
688 ln->ln_expire = time_second + nd6_gctimer;
690 if ((ln->ln_router = is_router) != 0) {
692 * This means a router's state has changed from
693 * non-reachable to probably reachable, and might
694 * affect the status of associated prefixes..
696 pfxlist_onlink_check();
702 * Check if the link-layer address has changed or not.
708 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
717 * This is VERY complex. Look at it with care.
719 * override solicit lladdr llchange action
724 * 0 0 y y (1) REACHABLE->STALE
725 * 0 1 n -- (2c) *->REACHABLE
726 * 0 1 y n (2b) L *->REACHABLE
727 * 0 1 y y (1) REACHABLE->STALE
730 * 1 0 y y (2a) L *->STALE
731 * 1 1 n -- (2a) *->REACHABLE
732 * 1 1 y n (2a) L *->REACHABLE
733 * 1 1 y y (2a) L *->REACHABLE
735 if (!is_override && (lladdr && llchange)) { /* (1) */
737 * If state is REACHABLE, make it STALE.
738 * no other updates should be done.
740 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
741 ln->ln_state = ND6_LLINFO_STALE;
742 ln->ln_expire = time_second + nd6_gctimer;
745 } else if (is_override /* (2a) */
746 || (!is_override && (lladdr && !llchange)) /* (2b) */
747 || !lladdr) { /* (2c) */
749 * Update link-local address, if any.
752 sdl->sdl_alen = ifp->if_addrlen;
753 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
757 * If solicited, make the state REACHABLE.
758 * If not solicited and the link-layer address was
759 * changed, make it STALE.
762 ln->ln_state = ND6_LLINFO_REACHABLE;
765 ln->ln_expire = time_second +
766 ND_IFINFO(ifp)->reachable;
769 if (lladdr && llchange) {
770 ln->ln_state = ND6_LLINFO_STALE;
771 ln->ln_expire = time_second + nd6_gctimer;
776 if (ln->ln_router && !is_router) {
778 * The peer dropped the router flag.
779 * Remove the sender from the Default Router List and
780 * update the Destination Cache entries.
782 struct nd_defrouter *dr;
783 struct in6_addr *in6;
785 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
788 * Lock to protect the default router list.
789 * XXX: this might be unnecessary, since this function
790 * is only called under the network software interrupt
791 * context. However, we keep it just for safety.
794 dr = defrouter_lookup(in6, rt->rt_ifp);
797 mtx_unlock(&nd6_mtx);
799 if (dr == NULL && !ip6_forwarding && ip6_accept_rtadv) {
801 * Even if the neighbor is not in the default
802 * router list, the neighbor may be used
803 * as a next hop for some destinations
804 * (e.g. redirect case). So we must
805 * call rt6_flush explicitly.
807 rt6_flush(&ip6->ip6_src, rt->rt_ifp);
810 ln->ln_router = is_router;
812 rt->rt_flags &= ~RTF_REJECT;
816 * we assume ifp is not a loopback here, so just set the 2nd
817 * argument as the 1st one.
819 nd6_output(ifp, ifp, ln->ln_hold,
820 (struct sockaddr_in6 *)rt_key(rt), rt);
829 icmp6stat.icp6s_badna++;
834 * Neighbor advertisement output handling.
838 * the following items are not implemented yet:
839 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
840 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
843 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6,
844 const struct in6_addr *taddr6, u_long flags,
845 int tlladdr, /* 1 if include target link-layer address */
846 struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */
850 struct nd_neighbor_advert *nd_na;
851 struct in6_ifaddr *ia = NULL;
852 struct ip6_moptions im6o;
856 struct ifnet *outif = NULL;
858 /* estimate the size of message */
859 maxlen = sizeof(*ip6) + sizeof(*nd_na);
860 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
861 if (max_linkhdr + maxlen >= MCLBYTES) {
863 kprintf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
864 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
869 m = m_getb(max_linkhdr + maxlen, MB_DONTWAIT, MT_DATA, M_PKTHDR);
873 if (IN6_IS_ADDR_MULTICAST(daddr6)) {
874 m->m_flags |= M_MCAST;
875 im6o.im6o_multicast_ifp = ifp;
876 im6o.im6o_multicast_hlim = 255;
877 im6o.im6o_multicast_loop = 0;
880 icmp6len = sizeof(*nd_na);
881 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
882 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
884 /* fill neighbor advertisement packet */
885 ip6 = mtod(m, struct ip6_hdr *);
887 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
888 ip6->ip6_vfc |= IPV6_VERSION;
889 ip6->ip6_nxt = IPPROTO_ICMPV6;
891 if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
893 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
894 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
895 ip6->ip6_dst.s6_addr32[1] = 0;
896 ip6->ip6_dst.s6_addr32[2] = 0;
897 ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
898 flags &= ~ND_NA_FLAG_SOLICITED;
900 ip6->ip6_dst = *daddr6;
903 * Select a source whose scope is the same as that of the dest.
905 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
910 ip6->ip6_src = ia->ia_addr.sin6_addr;
911 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
912 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
913 nd_na->nd_na_code = 0;
914 nd_na->nd_na_target = *taddr6;
915 if (IN6_IS_SCOPE_LINKLOCAL(&nd_na->nd_na_target))
916 nd_na->nd_na_target.s6_addr16[1] = 0;
919 * "tlladdr" indicates NS's condition for adding tlladdr or not.
920 * see nd6_ns_input() for details.
921 * Basically, if NS packet is sent to unicast/anycast addr,
922 * target lladdr option SHOULD NOT be included.
927 * sdl0 != NULL indicates proxy NA. If we do proxy, use
928 * lladdr in sdl0. If we are not proxying (sending NA for
929 * my address) use lladdr configured for the interface.
934 mac = carp_macmatch6(ifp->if_carp, m, taddr6);
936 mac = nd6_ifptomac(ifp);
938 mac = nd6_ifptomac(ifp);
940 } else if (sdl0->sa_family == AF_LINK) {
941 struct sockaddr_dl *sdl;
942 sdl = (struct sockaddr_dl *)sdl0;
943 if (sdl->sdl_alen == ifp->if_addrlen)
948 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
949 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
951 /* roundup to 8 bytes alignment! */
952 optlen = (optlen + 7) & ~7;
954 m->m_pkthdr.len += optlen;
957 bzero((caddr_t)nd_opt, optlen);
958 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
959 nd_opt->nd_opt_len = optlen >> 3;
960 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
962 flags &= ~ND_NA_FLAG_OVERRIDE;
964 ip6->ip6_plen = htons((u_short)icmp6len);
965 nd_na->nd_na_flags_reserved = flags;
966 nd_na->nd_na_cksum = 0;
968 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
970 ip6_output(m, NULL, NULL, 0, &im6o, &outif, NULL);
972 icmp6_ifstat_inc(outif, ifs6_out_msg);
973 icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
975 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
979 nd6_ifptomac(struct ifnet *ifp)
981 switch (ifp->if_type) {
993 return ((caddr_t)(ifp + 1));
1000 TAILQ_HEAD(dadq_head, dadq);
1002 TAILQ_ENTRY(dadq) dad_list;
1003 struct ifaddr *dad_ifa;
1004 int dad_count; /* max NS to send */
1005 int dad_ns_tcount; /* # of trials to send NS */
1006 int dad_ns_ocount; /* NS sent so far */
1009 struct callout dad_timer_ch;
1012 static struct dadq_head dadq;
1013 static int dad_init = 0;
1015 static struct dadq *
1016 nd6_dad_find(struct ifaddr *ifa)
1020 TAILQ_FOREACH(dp, &dadq, dad_list) {
1021 if (dp->dad_ifa == ifa)
1028 nd6_dad_starttimer(struct dadq *dp, int ticks)
1031 callout_reset(&dp->dad_timer_ch, ticks,
1032 (void (*) (void *))nd6_dad_timer, (void *)dp->dad_ifa);
1036 nd6_dad_stoptimer(struct dadq *dp)
1039 callout_stop(&dp->dad_timer_ch);
1043 * Start Duplicated Address Detection (DAD) for specified interface address.
1046 nd6_dad_start(struct ifaddr *ifa,
1047 int *tick) /* minimum delay ticks for IFF_UP event */
1049 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1058 * If we don't need DAD, don't do it.
1059 * There are several cases:
1060 * - DAD is disabled (ip6_dad_count == 0)
1061 * - the interface address is anycast
1063 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1065 "nd6_dad_start: called with non-tentative address "
1067 ip6_sprintf(&ia->ia_addr.sin6_addr),
1068 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1071 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1072 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1075 if (!ip6_dad_count) {
1076 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1080 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1081 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1083 if (nd6_dad_find(ifa) != NULL) {
1084 /* DAD already in progress */
1088 dp = kmalloc(sizeof(*dp), M_IP6NDP, M_NOWAIT | M_ZERO);
1090 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1092 ip6_sprintf(&ia->ia_addr.sin6_addr),
1093 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1096 callout_init(&dp->dad_timer_ch);
1097 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1099 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1100 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1103 * Send NS packet for DAD, ip6_dad_count times.
1104 * Note that we must delay the first transmission, if this is the
1105 * first packet to be sent from the interface after interface
1106 * (re)initialization.
1109 _IFAREF(ifa, 0); /* just for safety */
1110 dp->dad_count = ip6_dad_count;
1111 dp->dad_ns_icount = dp->dad_na_icount = 0;
1112 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1114 nd6_dad_ns_output(dp, ifa);
1115 nd6_dad_starttimer(dp,
1116 ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1121 ntick = krandom() % (MAX_RTR_SOLICITATION_DELAY * hz);
1123 ntick = *tick + krandom() % (hz / 2);
1125 nd6_dad_starttimer(dp, ntick);
1130 * terminate DAD unconditionally. used for address removals.
1133 nd6_dad_stop(struct ifaddr *ifa)
1139 dp = nd6_dad_find(ifa);
1141 /* DAD wasn't started yet */
1145 nd6_dad_stoptimer(dp);
1147 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1148 kfree(dp, M_IP6NDP);
1154 nd6_dad_timer(struct ifaddr *ifa)
1156 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1163 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1166 dp = nd6_dad_find(ifa);
1168 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1171 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1172 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1174 ip6_sprintf(&ia->ia_addr.sin6_addr),
1175 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1178 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1179 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1181 ip6_sprintf(&ia->ia_addr.sin6_addr),
1182 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1186 /* timeouted with IFF_{RUNNING,UP} check */
1187 if (dp->dad_ns_tcount > dad_maxtry) {
1188 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1189 if_name(ifa->ifa_ifp)));
1191 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1192 kfree(dp, M_IP6NDP);
1198 /* Need more checks? */
1199 if (dp->dad_ns_ocount < dp->dad_count) {
1201 * We have more NS to go. Send NS packet for DAD.
1203 nd6_dad_ns_output(dp, ifa);
1204 nd6_dad_starttimer(dp,
1205 ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1208 * We have transmitted sufficient number of DAD packets.
1209 * See what we've got.
1215 if (dp->dad_na_icount) {
1217 * the check is in nd6_dad_na_input(),
1223 if (dp->dad_ns_icount) {
1224 #if 0 /* heuristics */
1227 * - we have sent many(?) DAD NS, and
1228 * - the number of NS we sent equals to the
1229 * number of NS we've got, and
1231 * we may have a faulty network card/driver which
1232 * loops back multicasts to myself.
1234 if (3 < dp->dad_count
1235 && dp->dad_ns_icount == dp->dad_count
1236 && dp->dad_na_icount == 0) {
1237 log(LOG_INFO, "DAD questionable for %s(%s): "
1238 "network card loops back multicast?\n",
1239 ip6_sprintf(&ia->ia_addr.sin6_addr),
1240 if_name(ifa->ifa_ifp));
1241 /* XXX consider it a duplicate or not? */
1244 /* We've seen NS, means DAD has failed. */
1248 /* We've seen NS, means DAD has failed. */
1254 /* (*dp) will be freed in nd6_dad_duplicated() */
1256 nd6_dad_duplicated(ifa);
1259 * We are done with DAD. No NA came, no NS came.
1260 * duplicated address found.
1262 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1265 "%s: DAD complete for %s - no duplicates found\n",
1266 if_name(ifa->ifa_ifp),
1267 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1269 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1270 kfree(dp, M_IP6NDP);
1277 mtx_unlock(&nd6_mtx);
1281 nd6_dad_duplicated(struct ifaddr *ifa)
1283 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1286 dp = nd6_dad_find(ifa);
1288 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1292 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1293 "NS in/out=%d/%d, NA in=%d\n",
1294 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
1295 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1297 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1298 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1300 /* We are done with DAD, with duplicated address found. (failure) */
1301 nd6_dad_stoptimer(dp);
1303 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1304 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1305 log(LOG_ERR, "%s: manual intervention required\n",
1306 if_name(ifa->ifa_ifp));
1308 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1309 kfree(dp, M_IP6NDP);
1315 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1317 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1318 struct ifnet *ifp = ifa->ifa_ifp;
1320 dp->dad_ns_tcount++;
1321 if (!(ifp->if_flags & IFF_UP)) {
1323 kprintf("%s: interface down?\n", if_name(ifp));
1327 if (!(ifp->if_flags & IFF_RUNNING)) {
1329 kprintf("%s: interface not running?\n", if_name(ifp));
1334 dp->dad_ns_ocount++;
1335 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1339 nd6_dad_ns_input(struct ifaddr *ifa)
1341 struct in6_ifaddr *ia;
1343 const struct in6_addr *taddr6;
1348 panic("ifa == NULL in nd6_dad_ns_input");
1350 ia = (struct in6_ifaddr *)ifa;
1352 taddr6 = &ia->ia_addr.sin6_addr;
1354 dp = nd6_dad_find(ifa);
1356 /* Quickhack - completely ignore DAD NS packets */
1357 if (dad_ignore_ns) {
1359 "nd6_dad_ns_input: ignoring DAD NS packet for "
1360 "address %s(%s)\n", ip6_sprintf(taddr6),
1361 if_name(ifa->ifa_ifp)));
1366 * if I'm yet to start DAD, someone else started using this address
1367 * first. I have a duplicate and you win.
1369 if (!dp || dp->dad_ns_ocount == 0)
1372 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1375 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1376 nd6_dad_duplicated(ifa);
1379 * not sure if I got a duplicate.
1380 * increment ns count and see what happens.
1383 dp->dad_ns_icount++;
1388 nd6_dad_na_input(struct ifaddr *ifa)
1393 panic("ifa == NULL in nd6_dad_na_input");
1395 dp = nd6_dad_find(ifa);
1397 dp->dad_na_icount++;
1399 /* remove the address. */
1400 nd6_dad_duplicated(ifa);