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.19 2007/08/16 20:03:58 dillon Exp $ */
3 /* $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
46 #include <sys/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/syslog.h>
49 #include <sys/queue.h>
50 #include <sys/callout.h>
51 #include <sys/thread2.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet6/in6_var.h>
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet6/nd6.h>
64 #include <netinet/icmp6.h>
67 #include <netinet6/ipsec.h>
69 #include <netinet6/ipsec6.h>
73 #include <net/net_osdep.h>
76 #include <netinet/ip_carp.h>
80 #define SDL(s) ((struct sockaddr_dl *)s)
83 static struct dadq *nd6_dad_find (struct ifaddr *);
84 static void nd6_dad_starttimer (struct dadq *, int);
85 static void nd6_dad_stoptimer (struct dadq *);
86 static void nd6_dad_timer (struct ifaddr *);
87 static void nd6_dad_ns_output (struct dadq *, struct ifaddr *);
88 static void nd6_dad_ns_input (struct ifaddr *);
89 static void nd6_dad_na_input (struct ifaddr *);
91 static int dad_ignore_ns = 0; /* ignore NS in DAD - specwise incorrect*/
92 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
95 * Input an Neighbor Solicitation Message.
98 * Based on RFC 2462 (duplicated address detection)
101 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
103 struct ifnet *ifp = m->m_pkthdr.rcvif;
104 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
105 struct nd_neighbor_solicit *nd_ns;
106 struct in6_addr saddr6 = ip6->ip6_src;
107 struct in6_addr daddr6 = ip6->ip6_dst;
108 struct in6_addr taddr6;
109 struct in6_addr myaddr6;
111 struct ifaddr *ifa = NULL;
113 int anycast = 0, proxy = 0, tentative = 0;
115 union nd_opts ndopts;
116 struct sockaddr_dl *proxydl = NULL;
118 #ifndef PULLDOWN_TEST
119 IP6_EXTHDR_CHECK(m, off, icmp6len,);
120 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
122 IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
124 icmp6stat.icp6s_tooshort++;
128 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
129 taddr6 = nd_ns->nd_ns_target;
131 if (ip6->ip6_hlim != 255) {
133 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
134 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
135 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
139 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
140 /* dst has to be solicited node multicast address. */
141 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL
142 /* don't check ifindex portion */
143 && daddr6.s6_addr32[1] == 0
144 && daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE
145 && daddr6.s6_addr8[12] == 0xff) {
148 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
149 "(wrong ip6 dst)\n"));
154 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
155 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
159 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
160 taddr6.s6_addr16[1] = htons(ifp->if_index);
162 icmp6len -= sizeof(*nd_ns);
163 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
164 if (nd6_options(&ndopts) < 0) {
166 "nd6_ns_input: invalid ND option, ignored\n"));
167 /* nd6_options have incremented stats */
171 if (ndopts.nd_opts_src_lladdr) {
172 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
173 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
176 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
177 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
178 "(link-layer address option)\n"));
183 * Attaching target link-layer address to the NA?
186 * NS IP dst is unicast/anycast MUST NOT add
187 * NS IP dst is solicited-node multicast MUST add
189 * In implementation, we add target link-layer address by default.
190 * We do not add one in MUST NOT cases.
192 #if 0 /* too much! */
193 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
194 if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
198 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
204 * Target address (taddr6) must be either:
205 * (1) Valid unicast/anycast address for my receiving interface,
206 * (2) Unicast address for which I'm offering proxy service, or
207 * (3) "tentative" address on which DAD is being performed.
209 /* (1) and (3) check. */
212 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
214 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
216 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
222 struct sockaddr_in6 tsin6;
224 bzero(&tsin6, sizeof tsin6);
225 tsin6.sin6_len = sizeof(struct sockaddr_in6);
226 tsin6.sin6_family = AF_INET6;
227 tsin6.sin6_addr = taddr6;
229 rt = rtpurelookup((struct sockaddr *)&tsin6);
230 if (rt != NULL && (rt->rt_flags & RTF_ANNOUNCE) &&
231 rt->rt_gateway->sa_family == AF_LINK) {
233 * proxy NDP for single entry
235 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
236 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
239 proxydl = SDL(rt->rt_gateway);
247 * We've got an NS packet, and we don't have that adddress
248 * assigned for us. We MUST silently ignore it.
253 myaddr6 = *IFA_IN6(ifa);
254 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
255 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
256 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
259 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
261 "nd6_ns_input: lladdrlen mismatch for %s "
262 "(if %d, NS packet %d)\n",
263 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
267 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
269 "nd6_ns_input: duplicate IP6 address %s\n",
270 ip6_sprintf(&saddr6)));
275 * We have neighbor solicitation packet, with target address equals to
276 * one of my tentative address.
278 * src addr how to process?
280 * multicast of course, invalid (rejected in ip6_input)
281 * unicast somebody is doing address resolution -> ignore
282 * unspec dup address detection
284 * The processing is defined in RFC 2462.
288 * If source address is unspecified address, it is for
289 * duplicated address detection.
291 * If not, the packet is for addess resolution;
292 * silently ignore it.
294 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
295 nd6_dad_ns_input(ifa);
301 * If the source address is unspecified address, entries must not
302 * be created or updated.
303 * It looks that sender is performing DAD. Output NA toward
304 * all-node multicast address, to tell the sender that I'm using
306 * S bit ("solicited") must be zero.
308 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
309 saddr6 = kin6addr_linklocal_allnodes;
310 saddr6.s6_addr16[1] = htons(ifp->if_index);
311 nd6_na_output(ifp, &saddr6, &taddr6,
312 ((anycast || proxy || !tlladdr)
313 ? 0 : ND_NA_FLAG_OVERRIDE)
314 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
315 tlladdr, (struct sockaddr *)proxydl);
319 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
321 nd6_na_output(ifp, &saddr6, &taddr6,
322 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE)
323 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
324 | ND_NA_FLAG_SOLICITED,
325 tlladdr, (struct sockaddr *)proxydl);
331 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)));
332 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)));
333 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)));
334 icmp6stat.icp6s_badns++;
339 * Output an Neighbor Solicitation Message. Caller specifies:
340 * - ICMP6 header source IP6 address
341 * - ND6 header target IP6 address
342 * - ND6 header source datalink address
345 * Based on RFC 2462 (duplicated address detection)
348 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
349 const struct in6_addr *taddr6,
350 struct llinfo_nd6 *ln, /* for source address determination */
351 int dad) /* duplicated address detection */
355 struct nd_neighbor_solicit *nd_ns;
356 struct in6_ifaddr *ia = NULL;
357 struct ip6_moptions im6o;
361 struct ifnet *outif = NULL;
363 if (IN6_IS_ADDR_MULTICAST(taddr6))
366 /* estimate the size of message */
367 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
368 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
369 if (max_linkhdr + maxlen >= MCLBYTES) {
371 kprintf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
372 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
377 m = m_getb(max_linkhdr + maxlen, MB_DONTWAIT, MT_DATA, M_PKTHDR);
381 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
382 m->m_flags |= M_MCAST;
383 im6o.im6o_multicast_ifp = ifp;
384 im6o.im6o_multicast_hlim = 255;
385 im6o.im6o_multicast_loop = 0;
388 icmp6len = sizeof(*nd_ns);
389 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
390 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
392 /* fill neighbor solicitation packet */
393 ip6 = mtod(m, struct ip6_hdr *);
395 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
396 ip6->ip6_vfc |= IPV6_VERSION;
397 /* ip6->ip6_plen will be set later */
398 ip6->ip6_nxt = IPPROTO_ICMPV6;
401 ip6->ip6_dst = *daddr6;
403 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
404 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
405 ip6->ip6_dst.s6_addr32[1] = 0;
406 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
407 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
408 ip6->ip6_dst.s6_addr8[12] = 0xff;
411 #if 0 /* KAME way, exact address scope match */
413 * Select a source whose scope is the same as that of the dest.
414 * Typically, the dest is link-local solicitation multicast
415 * (i.e. neighbor discovery) or link-local/global unicast
416 * (i.e. neighbor un-reachability detection).
418 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
423 ip6->ip6_src = ia->ia_addr.sin6_addr;
424 #else /* spec-wise correct */
427 * "If the source address of the packet prompting the
428 * solicitation is the same as one of the addresses assigned
429 * to the outgoing interface, that address SHOULD be placed
430 * in the IP Source Address of the outgoing solicitation.
431 * Otherwise, any one of the addresses assigned to the
432 * interface should be used."
434 * We use the source address for the prompting packet
436 * - saddr6 is given from the caller (by giving "ln"), and
437 * - saddr6 belongs to the outgoing interface.
438 * Otherwise, we perform a scope-wise match.
440 struct ip6_hdr *hip6; /* hold ip6 */
441 struct in6_addr *saddr6;
443 if (ln && ln->ln_hold) {
444 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
446 if (sizeof(*hip6) < ln->ln_hold->m_len)
447 saddr6 = &hip6->ip6_src;
452 if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
453 bcopy(saddr6, &ip6->ip6_src, sizeof(*saddr6));
455 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
460 ip6->ip6_src = ia->ia_addr.sin6_addr;
465 * Source address for DAD packet must always be IPv6
466 * unspecified address. (0::0)
468 bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
470 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
471 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
472 nd_ns->nd_ns_code = 0;
473 nd_ns->nd_ns_reserved = 0;
474 nd_ns->nd_ns_target = *taddr6;
476 if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns->nd_ns_target))
477 nd_ns->nd_ns_target.s6_addr16[1] = 0;
480 * Add source link-layer address option.
482 * spec implementation
484 * DAD packet MUST NOT do not add the option
485 * there's no link layer address:
486 * impossible do not add the option
487 * there's link layer address:
488 * Multicast NS MUST add one add the option
489 * Unicast NS SHOULD add one add the option
491 if (!dad && (mac = nd6_ifptomac(ifp))) {
492 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
493 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
494 /* 8 byte alignments... */
495 optlen = (optlen + 7) & ~7;
497 m->m_pkthdr.len += optlen;
500 bzero((caddr_t)nd_opt, optlen);
501 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
502 nd_opt->nd_opt_len = optlen >> 3;
503 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
506 ip6->ip6_plen = htons((u_short)icmp6len);
507 nd_ns->nd_ns_cksum = 0;
509 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
511 ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif, NULL);
513 icmp6_ifstat_inc(outif, ifs6_out_msg);
514 icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
516 icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
520 * Neighbor advertisement input handling.
523 * Based on RFC 2462 (duplicated address detection)
525 * the following items are not implemented yet:
526 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
527 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
530 nd6_na_input(struct mbuf *m, int off, int icmp6len)
532 struct ifnet *ifp = m->m_pkthdr.rcvif;
533 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
534 struct nd_neighbor_advert *nd_na;
536 struct in6_addr saddr6 = ip6->ip6_src;
538 struct in6_addr daddr6 = ip6->ip6_dst;
539 struct in6_addr taddr6;
547 struct llinfo_nd6 *ln;
549 struct sockaddr_dl *sdl;
550 union nd_opts ndopts;
552 if (ip6->ip6_hlim != 255) {
554 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
555 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
556 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
560 #ifndef PULLDOWN_TEST
561 IP6_EXTHDR_CHECK(m, off, icmp6len,);
562 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
564 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
566 icmp6stat.icp6s_tooshort++;
570 taddr6 = nd_na->nd_na_target;
571 flags = nd_na->nd_na_flags_reserved;
572 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
573 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
574 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
576 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
577 taddr6.s6_addr16[1] = htons(ifp->if_index);
579 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
581 "nd6_na_input: invalid target address %s\n",
582 ip6_sprintf(&taddr6)));
585 if (IN6_IS_ADDR_MULTICAST(&daddr6))
588 "nd6_na_input: a solicited adv is multicasted\n"));
592 icmp6len -= sizeof(*nd_na);
593 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
594 if (nd6_options(&ndopts) < 0) {
596 "nd6_na_input: invalid ND option, ignored\n"));
597 /* nd6_options have incremented stats */
601 if (ndopts.nd_opts_tgt_lladdr) {
602 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
603 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
606 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
609 * Target address matches one of my interface address.
611 * If my address is tentative, this means that there's somebody
612 * already using the same address as mine. This indicates DAD failure.
613 * This is defined in RFC 2462.
615 * Otherwise, process as defined in RFC 2461.
618 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
619 nd6_dad_na_input(ifa);
623 /* Just for safety, maybe unnecessary. */
626 "nd6_na_input: duplicate IP6 address %s\n",
627 ip6_sprintf(&taddr6));
631 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
633 "nd6_na_input: lladdrlen mismatch for %s "
634 "(if %d, NA packet %d)\n",
635 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
640 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
642 rt = nd6_lookup(&taddr6, 0, ifp);
644 ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
645 ((sdl = SDL(rt->rt_gateway)) == NULL))
648 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
650 * If the link-layer has address, and no lladdr option came,
651 * discard the packet.
653 if (ifp->if_addrlen && !lladdr)
657 * Record link-layer address, and update the state.
659 sdl->sdl_alen = ifp->if_addrlen;
660 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
662 ln->ln_state = ND6_LLINFO_REACHABLE;
665 ln->ln_expire = time_second +
666 ND_IFINFO(rt->rt_ifp)->reachable;
668 ln->ln_state = ND6_LLINFO_STALE;
669 ln->ln_expire = time_second + nd6_gctimer;
671 if ((ln->ln_router = is_router) != 0) {
673 * This means a router's state has changed from
674 * non-reachable to probably reachable, and might
675 * affect the status of associated prefixes..
677 pfxlist_onlink_check();
683 * Check if the link-layer address has changed or not.
689 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
698 * This is VERY complex. Look at it with care.
700 * override solicit lladdr llchange action
705 * 0 0 y y (1) REACHABLE->STALE
706 * 0 1 n -- (2c) *->REACHABLE
707 * 0 1 y n (2b) L *->REACHABLE
708 * 0 1 y y (1) REACHABLE->STALE
711 * 1 0 y y (2a) L *->STALE
712 * 1 1 n -- (2a) *->REACHABLE
713 * 1 1 y n (2a) L *->REACHABLE
714 * 1 1 y y (2a) L *->REACHABLE
716 if (!is_override && (lladdr && llchange)) { /* (1) */
718 * If state is REACHABLE, make it STALE.
719 * no other updates should be done.
721 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
722 ln->ln_state = ND6_LLINFO_STALE;
723 ln->ln_expire = time_second + nd6_gctimer;
726 } else if (is_override /* (2a) */
727 || (!is_override && (lladdr && !llchange)) /* (2b) */
728 || !lladdr) { /* (2c) */
730 * Update link-local address, if any.
733 sdl->sdl_alen = ifp->if_addrlen;
734 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
738 * If solicited, make the state REACHABLE.
739 * If not solicited and the link-layer address was
740 * changed, make it STALE.
743 ln->ln_state = ND6_LLINFO_REACHABLE;
746 ln->ln_expire = time_second +
747 ND_IFINFO(ifp)->reachable;
750 if (lladdr && llchange) {
751 ln->ln_state = ND6_LLINFO_STALE;
752 ln->ln_expire = time_second + nd6_gctimer;
757 if (ln->ln_router && !is_router) {
759 * The peer dropped the router flag.
760 * Remove the sender from the Default Router List and
761 * update the Destination Cache entries.
763 struct nd_defrouter *dr;
764 struct in6_addr *in6;
766 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
769 * Lock to protect the default router list.
770 * XXX: this might be unnecessary, since this function
771 * is only called under the network software interrupt
772 * context. However, we keep it just for safety.
775 dr = defrouter_lookup(in6, rt->rt_ifp);
778 else if (!ip6_forwarding && ip6_accept_rtadv) {
780 * Even if the neighbor is not in the default
781 * router list, the neighbor may be used
782 * as a next hop for some destinations
783 * (e.g. redirect case). So we must
784 * call rt6_flush explicitly.
786 rt6_flush(&ip6->ip6_src, rt->rt_ifp);
790 ln->ln_router = is_router;
792 rt->rt_flags &= ~RTF_REJECT;
796 * we assume ifp is not a loopback here, so just set the 2nd
797 * argument as the 1st one.
799 nd6_output(ifp, ifp, ln->ln_hold,
800 (struct sockaddr_in6 *)rt_key(rt), rt);
809 icmp6stat.icp6s_badna++;
814 * Neighbor advertisement output handling.
818 * the following items are not implemented yet:
819 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
820 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
823 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6,
824 const struct in6_addr *taddr6, u_long flags,
825 int tlladdr, /* 1 if include target link-layer address */
826 struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */
830 struct nd_neighbor_advert *nd_na;
831 struct in6_ifaddr *ia = NULL;
832 struct ip6_moptions im6o;
836 struct ifnet *outif = NULL;
838 /* estimate the size of message */
839 maxlen = sizeof(*ip6) + sizeof(*nd_na);
840 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
841 if (max_linkhdr + maxlen >= MCLBYTES) {
843 kprintf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
844 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
849 m = m_getb(max_linkhdr + maxlen, MB_DONTWAIT, MT_DATA, M_PKTHDR);
853 if (IN6_IS_ADDR_MULTICAST(daddr6)) {
854 m->m_flags |= M_MCAST;
855 im6o.im6o_multicast_ifp = ifp;
856 im6o.im6o_multicast_hlim = 255;
857 im6o.im6o_multicast_loop = 0;
860 icmp6len = sizeof(*nd_na);
861 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
862 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
864 /* fill neighbor advertisement packet */
865 ip6 = mtod(m, struct ip6_hdr *);
867 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
868 ip6->ip6_vfc |= IPV6_VERSION;
869 ip6->ip6_nxt = IPPROTO_ICMPV6;
871 if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
873 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
874 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
875 ip6->ip6_dst.s6_addr32[1] = 0;
876 ip6->ip6_dst.s6_addr32[2] = 0;
877 ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
878 flags &= ~ND_NA_FLAG_SOLICITED;
880 ip6->ip6_dst = *daddr6;
883 * Select a source whose scope is the same as that of the dest.
885 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
890 ip6->ip6_src = ia->ia_addr.sin6_addr;
891 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
892 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
893 nd_na->nd_na_code = 0;
894 nd_na->nd_na_target = *taddr6;
895 if (IN6_IS_SCOPE_LINKLOCAL(&nd_na->nd_na_target))
896 nd_na->nd_na_target.s6_addr16[1] = 0;
899 * "tlladdr" indicates NS's condition for adding tlladdr or not.
900 * see nd6_ns_input() for details.
901 * Basically, if NS packet is sent to unicast/anycast addr,
902 * target lladdr option SHOULD NOT be included.
907 * sdl0 != NULL indicates proxy NA. If we do proxy, use
908 * lladdr in sdl0. If we are not proxying (sending NA for
909 * my address) use lladdr configured for the interface.
914 mac = carp_macmatch6(ifp->if_carp, m, taddr6);
916 mac = nd6_ifptomac(ifp);
918 mac = nd6_ifptomac(ifp);
920 } else if (sdl0->sa_family == AF_LINK) {
921 struct sockaddr_dl *sdl;
922 sdl = (struct sockaddr_dl *)sdl0;
923 if (sdl->sdl_alen == ifp->if_addrlen)
928 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
929 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
931 /* roundup to 8 bytes alignment! */
932 optlen = (optlen + 7) & ~7;
934 m->m_pkthdr.len += optlen;
937 bzero((caddr_t)nd_opt, optlen);
938 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
939 nd_opt->nd_opt_len = optlen >> 3;
940 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
942 flags &= ~ND_NA_FLAG_OVERRIDE;
944 ip6->ip6_plen = htons((u_short)icmp6len);
945 nd_na->nd_na_flags_reserved = flags;
946 nd_na->nd_na_cksum = 0;
948 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
950 ip6_output(m, NULL, NULL, 0, &im6o, &outif, NULL);
952 icmp6_ifstat_inc(outif, ifs6_out_msg);
953 icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
955 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
959 nd6_ifptomac(struct ifnet *ifp)
961 switch (ifp->if_type) {
975 return ((caddr_t)(ifp + 1));
982 TAILQ_HEAD(dadq_head, dadq);
984 TAILQ_ENTRY(dadq) dad_list;
985 struct ifaddr *dad_ifa;
986 int dad_count; /* max NS to send */
987 int dad_ns_tcount; /* # of trials to send NS */
988 int dad_ns_ocount; /* NS sent so far */
991 struct callout dad_timer_ch;
994 static struct dadq_head dadq;
995 static int dad_init = 0;
998 nd6_dad_find(struct ifaddr *ifa)
1002 TAILQ_FOREACH(dp, &dadq, dad_list) {
1003 if (dp->dad_ifa == ifa)
1010 nd6_dad_starttimer(struct dadq *dp, int ticks)
1013 callout_reset(&dp->dad_timer_ch, ticks,
1014 (void (*) (void *))nd6_dad_timer, (void *)dp->dad_ifa);
1018 nd6_dad_stoptimer(struct dadq *dp)
1021 callout_stop(&dp->dad_timer_ch);
1025 * Start Duplicated Address Detection (DAD) for specified interface address.
1028 nd6_dad_start(struct ifaddr *ifa,
1029 int *tick) /* minimum delay ticks for IFF_UP event */
1031 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1040 * If we don't need DAD, don't do it.
1041 * There are several cases:
1042 * - DAD is disabled (ip6_dad_count == 0)
1043 * - the interface address is anycast
1045 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1047 "nd6_dad_start: called with non-tentative address "
1049 ip6_sprintf(&ia->ia_addr.sin6_addr),
1050 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1053 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1054 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1057 if (!ip6_dad_count) {
1058 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1062 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1063 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1065 if (nd6_dad_find(ifa) != NULL) {
1066 /* DAD already in progress */
1070 dp = kmalloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1072 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1074 ip6_sprintf(&ia->ia_addr.sin6_addr),
1075 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1078 bzero(dp, sizeof(*dp));
1079 callout_init(&dp->dad_timer_ch);
1080 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1082 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1083 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1086 * Send NS packet for DAD, ip6_dad_count times.
1087 * Note that we must delay the first transmission, if this is the
1088 * first packet to be sent from the interface after interface
1089 * (re)initialization.
1092 IFAREF(ifa); /* just for safety */
1093 dp->dad_count = ip6_dad_count;
1094 dp->dad_ns_icount = dp->dad_na_icount = 0;
1095 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1097 nd6_dad_ns_output(dp, ifa);
1098 nd6_dad_starttimer(dp,
1099 ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1104 ntick = krandom() % (MAX_RTR_SOLICITATION_DELAY * hz);
1106 ntick = *tick + krandom() % (hz / 2);
1108 nd6_dad_starttimer(dp, ntick);
1113 * terminate DAD unconditionally. used for address removals.
1116 nd6_dad_stop(struct ifaddr *ifa)
1122 dp = nd6_dad_find(ifa);
1124 /* DAD wasn't started yet */
1128 nd6_dad_stoptimer(dp);
1130 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1131 kfree(dp, M_IP6NDP);
1137 nd6_dad_timer(struct ifaddr *ifa)
1139 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1142 crit_enter(); /* XXX */
1146 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1149 dp = nd6_dad_find(ifa);
1151 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1154 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1155 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1157 ip6_sprintf(&ia->ia_addr.sin6_addr),
1158 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1161 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1162 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1164 ip6_sprintf(&ia->ia_addr.sin6_addr),
1165 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1169 /* timeouted with IFF_{RUNNING,UP} check */
1170 if (dp->dad_ns_tcount > dad_maxtry) {
1171 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1172 if_name(ifa->ifa_ifp)));
1174 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1175 kfree(dp, M_IP6NDP);
1181 /* Need more checks? */
1182 if (dp->dad_ns_ocount < dp->dad_count) {
1184 * We have more NS to go. Send NS packet for DAD.
1186 nd6_dad_ns_output(dp, ifa);
1187 nd6_dad_starttimer(dp,
1188 ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1191 * We have transmitted sufficient number of DAD packets.
1192 * See what we've got.
1198 if (dp->dad_na_icount) {
1200 * the check is in nd6_dad_na_input(),
1206 if (dp->dad_ns_icount) {
1207 #if 0 /* heuristics */
1210 * - we have sent many(?) DAD NS, and
1211 * - the number of NS we sent equals to the
1212 * number of NS we've got, and
1214 * we may have a faulty network card/driver which
1215 * loops back multicasts to myself.
1217 if (3 < dp->dad_count
1218 && dp->dad_ns_icount == dp->dad_count
1219 && dp->dad_na_icount == 0) {
1220 log(LOG_INFO, "DAD questionable for %s(%s): "
1221 "network card loops back multicast?\n",
1222 ip6_sprintf(&ia->ia_addr.sin6_addr),
1223 if_name(ifa->ifa_ifp));
1224 /* XXX consider it a duplicate or not? */
1227 /* We've seen NS, means DAD has failed. */
1231 /* We've seen NS, means DAD has failed. */
1237 /* (*dp) will be freed in nd6_dad_duplicated() */
1239 nd6_dad_duplicated(ifa);
1242 * We are done with DAD. No NA came, no NS came.
1243 * duplicated address found.
1245 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1248 "%s: DAD complete for %s - no duplicates found\n",
1249 if_name(ifa->ifa_ifp),
1250 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1252 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1253 kfree(dp, M_IP6NDP);
1264 nd6_dad_duplicated(struct ifaddr *ifa)
1266 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1269 dp = nd6_dad_find(ifa);
1271 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1275 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1276 "NS in/out=%d/%d, NA in=%d\n",
1277 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
1278 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1280 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1281 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1283 /* We are done with DAD, with duplicated address found. (failure) */
1284 nd6_dad_stoptimer(dp);
1286 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1287 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1288 log(LOG_ERR, "%s: manual intervention required\n",
1289 if_name(ifa->ifa_ifp));
1291 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1292 kfree(dp, M_IP6NDP);
1298 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1300 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1301 struct ifnet *ifp = ifa->ifa_ifp;
1303 dp->dad_ns_tcount++;
1304 if (!(ifp->if_flags & IFF_UP)) {
1306 kprintf("%s: interface down?\n", if_name(ifp));
1310 if (!(ifp->if_flags & IFF_RUNNING)) {
1312 kprintf("%s: interface not running?\n", if_name(ifp));
1317 dp->dad_ns_ocount++;
1318 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1322 nd6_dad_ns_input(struct ifaddr *ifa)
1324 struct in6_ifaddr *ia;
1326 const struct in6_addr *taddr6;
1331 panic("ifa == NULL in nd6_dad_ns_input");
1333 ia = (struct in6_ifaddr *)ifa;
1335 taddr6 = &ia->ia_addr.sin6_addr;
1337 dp = nd6_dad_find(ifa);
1339 /* Quickhack - completely ignore DAD NS packets */
1340 if (dad_ignore_ns) {
1342 "nd6_dad_ns_input: ignoring DAD NS packet for "
1343 "address %s(%s)\n", ip6_sprintf(taddr6),
1344 if_name(ifa->ifa_ifp)));
1349 * if I'm yet to start DAD, someone else started using this address
1350 * first. I have a duplicate and you win.
1352 if (!dp || dp->dad_ns_ocount == 0)
1355 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1358 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1359 nd6_dad_duplicated(ifa);
1362 * not sure if I got a duplicate.
1363 * increment ns count and see what happens.
1366 dp->dad_ns_icount++;
1371 nd6_dad_na_input(struct ifaddr *ifa)
1376 panic("ifa == NULL in nd6_dad_na_input");
1378 dp = nd6_dad_find(ifa);
1380 dp->dad_na_icount++;
1382 /* remove the address. */
1383 nd6_dad_duplicated(ifa);