2 /* $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "opt_inet6.h"
35 #include "opt_ipsec.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
45 #include <sys/kernel.h>
46 #include <sys/errno.h>
47 #include <sys/syslog.h>
48 #include <sys/queue.h>
49 #include <sys/callout.h>
52 #include <net/if_types.h>
53 #include <net/if_dl.h>
54 #include <net/if_var.h>
55 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netinet6/in6_var.h>
60 #include <netinet6/in6_ifattach.h>
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet6/scope6_var.h>
64 #include <netinet6/nd6.h>
65 #include <netinet/icmp6.h>
68 #include <netinet/ip_carp.h>
71 #include <net/net_osdep.h>
73 #define SDL(s) ((struct sockaddr_dl *)s)
76 static struct dadq *nd6_dad_find __P((struct ifaddr *));
77 static void nd6_dad_starttimer __P((struct dadq *, int));
78 static void nd6_dad_stoptimer __P((struct dadq *));
79 static void nd6_dad_timer __P((struct ifaddr *));
80 static void nd6_dad_ns_output __P((struct dadq *, struct ifaddr *));
81 static void nd6_dad_ns_input __P((struct ifaddr *));
82 static void nd6_dad_na_input __P((struct ifaddr *));
84 static int dad_ignore_ns = 0; /* ignore NS in DAD - specwise incorrect*/
85 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
88 * Input a Neighbor Solicitation Message.
91 * Based on RFC 2462 (duplicate address detection)
94 nd6_ns_input(m, off, icmp6len)
98 struct ifnet *ifp = m->m_pkthdr.rcvif;
99 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
100 struct nd_neighbor_solicit *nd_ns;
101 struct in6_addr saddr6 = ip6->ip6_src;
102 struct in6_addr daddr6 = ip6->ip6_dst;
103 struct in6_addr taddr6;
104 struct in6_addr myaddr6;
106 struct ifaddr *ifa = NULL;
108 int anycast = 0, proxy = 0, tentative = 0;
110 union nd_opts ndopts;
111 struct sockaddr_dl *proxydl = NULL;
113 #ifndef PULLDOWN_TEST
114 IP6_EXTHDR_CHECK(m, off, icmp6len,);
115 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
117 IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
119 icmp6stat.icp6s_tooshort++;
123 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
124 taddr6 = nd_ns->nd_ns_target;
125 if (in6_setscope(&taddr6, ifp, NULL) != 0)
128 if (ip6->ip6_hlim != 255) {
130 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
131 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
132 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
136 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
137 /* dst has to be a solicited node multicast address. */
138 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
139 /* don't check ifindex portion */
140 daddr6.s6_addr32[1] == 0 &&
141 daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
142 daddr6.s6_addr8[12] == 0xff) {
145 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
146 "(wrong ip6 dst)\n"));
151 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
152 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
156 icmp6len -= sizeof(*nd_ns);
157 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
158 if (nd6_options(&ndopts) < 0) {
160 "nd6_ns_input: invalid ND option, ignored\n"));
161 /* nd6_options have incremented stats */
165 if (ndopts.nd_opts_src_lladdr) {
166 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
167 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
170 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
171 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
172 "(link-layer address option)\n"));
177 * Attaching target link-layer address to the NA?
180 * NS IP dst is unicast/anycast MUST NOT add
181 * NS IP dst is solicited-node multicast MUST add
183 * In implementation, we add target link-layer address by default.
184 * We do not add one in MUST NOT cases.
186 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
192 * Target address (taddr6) must be either:
193 * (1) Valid unicast/anycast address for my receiving interface,
194 * (2) Unicast address for which I'm offering proxy service, or
195 * (3) "tentative" address on which DAD is being performed.
197 /* (1) and (3) check. */
200 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
202 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
204 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
210 struct sockaddr_in6 tsin6;
213 bzero(&tsin6, sizeof tsin6);
214 tsin6.sin6_len = sizeof(struct sockaddr_in6);
215 tsin6.sin6_family = AF_INET6;
216 tsin6.sin6_addr = taddr6;
218 rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
219 need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
220 rt->rt_gateway->sa_family == AF_LINK);
225 * proxy NDP for single entry
227 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
228 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
231 proxydl = SDL(rt->rt_gateway);
237 * We've got an NS packet, and we don't have that adddress
238 * assigned for us. We MUST silently ignore it.
243 myaddr6 = *IFA_IN6(ifa);
244 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
245 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
246 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
249 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
250 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
251 "(if %d, NS packet %d)\n",
252 ip6_sprintf(&taddr6),
253 ifp->if_addrlen, lladdrlen - 2));
257 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
258 nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
259 ip6_sprintf(&saddr6)));
264 * We have neighbor solicitation packet, with target address equals to
265 * one of my tentative address.
267 * src addr how to process?
269 * multicast of course, invalid (rejected in ip6_input)
270 * unicast somebody is doing address resolution -> ignore
271 * unspec dup address detection
273 * The processing is defined in RFC 2462.
277 * If source address is unspecified address, it is for
278 * duplicate address detection.
280 * If not, the packet is for addess resolution;
281 * silently ignore it.
283 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
284 nd6_dad_ns_input(ifa);
290 * If the source address is unspecified address, entries must not
291 * be created or updated.
292 * It looks that sender is performing DAD. Output NA toward
293 * all-node multicast address, to tell the sender that I'm using
295 * S bit ("solicited") must be zero.
297 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
298 struct in6_addr in6_all;
300 in6_all = in6addr_linklocal_allnodes;
301 if (in6_setscope(&in6_all, ifp, NULL) != 0)
303 nd6_na_output(ifp, &in6_all, &taddr6,
304 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
305 (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
306 tlladdr, (struct sockaddr *)proxydl);
310 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
311 ND_NEIGHBOR_SOLICIT, 0);
313 nd6_na_output(ifp, &saddr6, &taddr6,
314 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
315 (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
316 tlladdr, (struct sockaddr *)proxydl);
322 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)));
323 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)));
324 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)));
325 icmp6stat.icp6s_badns++;
330 * Output a Neighbor Solicitation Message. Caller specifies:
331 * - ICMP6 header source IP6 address
332 * - ND6 header target IP6 address
333 * - ND6 header source datalink address
336 * Based on RFC 2462 (duplicate address detection)
339 nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
341 const struct in6_addr *daddr6, *taddr6;
342 struct llinfo_nd6 *ln; /* for source address determination */
343 int dad; /* duplicate address detection */
347 struct nd_neighbor_solicit *nd_ns;
348 struct in6_addr *src, src_in;
349 struct ip6_moptions im6o;
355 bzero(&ro, sizeof(ro));
357 if (IN6_IS_ADDR_MULTICAST(taddr6))
360 /* estimate the size of message */
361 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
362 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
363 if (max_linkhdr + maxlen >= MCLBYTES) {
365 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
366 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
371 MGETHDR(m, M_DONTWAIT, MT_DATA);
372 if (m && max_linkhdr + maxlen >= MHLEN) {
373 MCLGET(m, M_DONTWAIT);
374 if ((m->m_flags & M_EXT) == 0) {
381 m->m_pkthdr.rcvif = NULL;
383 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
384 m->m_flags |= M_MCAST;
385 im6o.im6o_multicast_ifp = ifp;
386 im6o.im6o_multicast_hlim = 255;
387 im6o.im6o_multicast_loop = 0;
390 icmp6len = sizeof(*nd_ns);
391 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
392 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
394 /* fill neighbor solicitation packet */
395 ip6 = mtod(m, struct ip6_hdr *);
397 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
398 ip6->ip6_vfc |= IPV6_VERSION;
399 /* ip6->ip6_plen will be set later */
400 ip6->ip6_nxt = IPPROTO_ICMPV6;
403 ip6->ip6_dst = *daddr6;
405 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
406 ip6->ip6_dst.s6_addr16[1] = 0;
407 ip6->ip6_dst.s6_addr32[1] = 0;
408 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
409 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
410 ip6->ip6_dst.s6_addr8[12] = 0xff;
411 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
417 * "If the source address of the packet prompting the
418 * solicitation is the same as one of the addresses assigned
419 * to the outgoing interface, that address SHOULD be placed
420 * in the IP Source Address of the outgoing solicitation.
421 * Otherwise, any one of the addresses assigned to the
422 * interface should be used."
424 * We use the source address for the prompting packet
426 * - saddr6 is given from the caller (by giving "ln"), and
427 * - saddr6 belongs to the outgoing interface.
428 * Otherwise, we perform the source address selection as usual.
430 struct ip6_hdr *hip6; /* hold ip6 */
431 struct in6_addr *hsrc = NULL;
433 if (ln && ln->ln_hold) {
435 * assuming every packet in ln_hold has the same IP
438 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
440 if (sizeof(*hip6) < ln->ln_hold->m_len)
441 hsrc = &hip6->ip6_src;
445 if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
449 struct sockaddr_in6 dst_sa;
451 bzero(&dst_sa, sizeof(dst_sa));
452 dst_sa.sin6_family = AF_INET6;
453 dst_sa.sin6_len = sizeof(dst_sa);
454 dst_sa.sin6_addr = ip6->ip6_dst;
456 src = in6_selectsrc(&dst_sa, NULL,
457 NULL, &ro, NULL, NULL, &error);
460 "nd6_ns_output: source can't be "
461 "determined: dst=%s, error=%d\n",
462 ip6_sprintf(&dst_sa.sin6_addr), error));
468 * Source address for DAD packet must always be IPv6
469 * unspecified address. (0::0)
470 * We actually don't have to 0-clear the address (we did it
471 * above), but we do so here explicitly to make the intention
474 bzero(&src_in, sizeof(src_in));
478 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
479 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
480 nd_ns->nd_ns_code = 0;
481 nd_ns->nd_ns_reserved = 0;
482 nd_ns->nd_ns_target = *taddr6;
483 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
486 * Add source link-layer address option.
488 * spec implementation
490 * DAD packet MUST NOT do not add the option
491 * there's no link layer address:
492 * impossible do not add the option
493 * there's link layer address:
494 * Multicast NS MUST add one add the option
495 * Unicast NS SHOULD add one add the option
497 if (!dad && (mac = nd6_ifptomac(ifp))) {
498 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
499 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
500 /* 8 byte alignments... */
501 optlen = (optlen + 7) & ~7;
503 m->m_pkthdr.len += optlen;
506 bzero((caddr_t)nd_opt, optlen);
507 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
508 nd_opt->nd_opt_len = optlen >> 3;
509 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
512 ip6->ip6_plen = htons((u_short)icmp6len);
513 nd_ns->nd_ns_cksum = 0;
515 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
517 ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
518 icmp6_ifstat_inc(ifp, ifs6_out_msg);
519 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
520 icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
522 if (ro.ro_rt) { /* we don't cache this route. */
536 * Neighbor advertisement input handling.
539 * Based on RFC 2462 (duplicate address detection)
541 * the following items are not implemented yet:
542 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
543 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
546 nd6_na_input(m, off, icmp6len)
550 struct ifnet *ifp = m->m_pkthdr.rcvif;
551 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
552 struct nd_neighbor_advert *nd_na;
553 struct in6_addr daddr6 = ip6->ip6_dst;
554 struct in6_addr taddr6;
562 struct llinfo_nd6 *ln;
564 struct sockaddr_dl *sdl;
565 union nd_opts ndopts;
567 if (ip6->ip6_hlim != 255) {
569 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
570 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
571 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
575 #ifndef PULLDOWN_TEST
576 IP6_EXTHDR_CHECK(m, off, icmp6len,);
577 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
579 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
581 icmp6stat.icp6s_tooshort++;
586 flags = nd_na->nd_na_flags_reserved;
587 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
588 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
589 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
591 taddr6 = nd_na->nd_na_target;
592 if (in6_setscope(&taddr6, ifp, NULL))
593 goto bad; /* XXX: impossible */
595 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
597 "nd6_na_input: invalid target address %s\n",
598 ip6_sprintf(&taddr6)));
601 if (IN6_IS_ADDR_MULTICAST(&daddr6))
604 "nd6_na_input: a solicited adv is multicasted\n"));
608 icmp6len -= sizeof(*nd_na);
609 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
610 if (nd6_options(&ndopts) < 0) {
612 "nd6_na_input: invalid ND option, ignored\n"));
613 /* nd6_options have incremented stats */
617 if (ndopts.nd_opts_tgt_lladdr) {
618 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
619 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
622 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
625 * Target address matches one of my interface address.
627 * If my address is tentative, this means that there's somebody
628 * already using the same address as mine. This indicates DAD failure.
629 * This is defined in RFC 2462.
631 * Otherwise, process as defined in RFC 2461.
634 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
635 nd6_dad_na_input(ifa);
639 /* Just for safety, maybe unnecessary. */
642 "nd6_na_input: duplicate IP6 address %s\n",
643 ip6_sprintf(&taddr6));
647 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
648 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
649 "(if %d, NA packet %d)\n", ip6_sprintf(&taddr6),
650 ifp->if_addrlen, lladdrlen - 2));
655 * If no neighbor cache entry is found, NA SHOULD silently be
658 rt = nd6_lookup(&taddr6, 0, ifp);
660 ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
661 ((sdl = SDL(rt->rt_gateway)) == NULL))
664 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
666 * If the link-layer has address, and no lladdr option came,
667 * discard the packet.
669 if (ifp->if_addrlen && lladdr == NULL)
673 * Record link-layer address, and update the state.
675 sdl->sdl_alen = ifp->if_addrlen;
676 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
678 ln->ln_state = ND6_LLINFO_REACHABLE;
680 if (!ND6_LLINFO_PERMANENT(ln)) {
681 nd6_llinfo_settimer(ln,
682 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
685 ln->ln_state = ND6_LLINFO_STALE;
686 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
688 if ((ln->ln_router = is_router) != 0) {
690 * This means a router's state has changed from
691 * non-reachable to probably reachable, and might
692 * affect the status of associated prefixes..
694 pfxlist_onlink_check();
700 * Check if the link-layer address has changed or not.
706 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
715 * This is VERY complex. Look at it with care.
717 * override solicit lladdr llchange action
722 * 0 0 y y (1) REACHABLE->STALE
723 * 0 1 n -- (2c) *->REACHABLE
724 * 0 1 y n (2b) L *->REACHABLE
725 * 0 1 y y (1) REACHABLE->STALE
728 * 1 0 y y (2a) L *->STALE
729 * 1 1 n -- (2a) *->REACHABLE
730 * 1 1 y n (2a) L *->REACHABLE
731 * 1 1 y y (2a) L *->REACHABLE
733 if (!is_override && (lladdr != NULL && llchange)) { /* (1) */
735 * If state is REACHABLE, make it STALE.
736 * no other updates should be done.
738 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
739 ln->ln_state = ND6_LLINFO_STALE;
740 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
743 } else if (is_override /* (2a) */
744 || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
745 || lladdr == NULL) { /* (2c) */
747 * Update link-local address, if any.
749 if (lladdr != NULL) {
750 sdl->sdl_alen = ifp->if_addrlen;
751 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
755 * If solicited, make the state REACHABLE.
756 * If not solicited and the link-layer address was
757 * changed, make it STALE.
760 ln->ln_state = ND6_LLINFO_REACHABLE;
762 if (!ND6_LLINFO_PERMANENT(ln)) {
763 nd6_llinfo_settimer(ln,
764 (long)ND_IFINFO(ifp)->reachable * hz);
767 if (lladdr != NULL && llchange) {
768 ln->ln_state = ND6_LLINFO_STALE;
769 nd6_llinfo_settimer(ln,
770 (long)nd6_gctimer * hz);
775 if (ln->ln_router && !is_router) {
777 * The peer dropped the router flag.
778 * Remove the sender from the Default Router List and
779 * update the Destination Cache entries.
781 struct nd_defrouter *dr;
782 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, ifp);
797 else if (!ip6_forwarding) {
799 * Even if the neighbor is not in the default
800 * router list, the neighbor may be used
801 * as a next hop for some destinations
802 * (e.g. redirect case). So we must
803 * call rt6_flush explicitly.
805 rt6_flush(&ip6->ip6_src, ifp);
809 ln->ln_router = is_router;
811 rt->rt_flags &= ~RTF_REJECT;
814 struct mbuf *m_hold, *m_hold_next;
816 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
817 struct mbuf *mpkt = NULL;
819 m_hold_next = m_hold->m_nextpkt;
820 mpkt = m_copym(m_hold, 0, M_COPYALL, M_DONTWAIT);
825 mpkt->m_nextpkt = NULL;
827 * we assume ifp is not a loopback here, so just set
828 * the 2nd argument as the 1st one.
830 nd6_output(ifp, ifp, mpkt,
831 (struct sockaddr_in6 *)rt_key(rt), rt);
841 icmp6stat.icp6s_badna++;
846 * Neighbor advertisement output handling.
850 * the following items are not implemented yet:
851 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
852 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
855 nd6_na_output(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0)
857 const struct in6_addr *daddr6_0, *taddr6;
859 int tlladdr; /* 1 if include target link-layer address */
860 struct sockaddr *sdl0; /* sockaddr_dl (= proxy NA) or NULL */
864 struct nd_neighbor_advert *nd_na;
865 struct ip6_moptions im6o;
866 struct in6_addr *src, daddr6;
867 struct sockaddr_in6 dst_sa;
868 int icmp6len, maxlen, error;
872 bzero(&ro, sizeof(ro));
874 daddr6 = *daddr6_0; /* make a local copy for modification */
876 /* estimate the size of message */
877 maxlen = sizeof(*ip6) + sizeof(*nd_na);
878 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
879 if (max_linkhdr + maxlen >= MCLBYTES) {
881 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
882 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
887 MGETHDR(m, M_DONTWAIT, MT_DATA);
888 if (m && max_linkhdr + maxlen >= MHLEN) {
889 MCLGET(m, M_DONTWAIT);
890 if ((m->m_flags & M_EXT) == 0) {
897 m->m_pkthdr.rcvif = NULL;
899 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
900 m->m_flags |= M_MCAST;
901 im6o.im6o_multicast_ifp = ifp;
902 im6o.im6o_multicast_hlim = 255;
903 im6o.im6o_multicast_loop = 0;
906 icmp6len = sizeof(*nd_na);
907 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
908 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
910 /* fill neighbor advertisement packet */
911 ip6 = mtod(m, struct ip6_hdr *);
913 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
914 ip6->ip6_vfc |= IPV6_VERSION;
915 ip6->ip6_nxt = IPPROTO_ICMPV6;
917 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
919 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
920 daddr6.s6_addr16[1] = 0;
921 daddr6.s6_addr32[1] = 0;
922 daddr6.s6_addr32[2] = 0;
923 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
924 if (in6_setscope(&daddr6, ifp, NULL))
927 flags &= ~ND_NA_FLAG_SOLICITED;
929 ip6->ip6_dst = daddr6;
930 bzero(&dst_sa, sizeof(struct sockaddr_in6));
931 dst_sa.sin6_family = AF_INET6;
932 dst_sa.sin6_len = sizeof(struct sockaddr_in6);
933 dst_sa.sin6_addr = daddr6;
936 * Select a source whose scope is the same as that of the dest.
938 bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
939 src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error);
941 nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
942 "determined: dst=%s, error=%d\n",
943 ip6_sprintf(&dst_sa.sin6_addr), error));
947 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
948 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
949 nd_na->nd_na_code = 0;
950 nd_na->nd_na_target = *taddr6;
951 in6_clearscope(&nd_na->nd_na_target); /* XXX */
954 * "tlladdr" indicates NS's condition for adding tlladdr or not.
955 * see nd6_ns_input() for details.
956 * Basically, if NS packet is sent to unicast/anycast addr,
957 * target lladdr option SHOULD NOT be included.
961 * sdl0 != NULL indicates proxy NA. If we do proxy, use
962 * lladdr in sdl0. If we are not proxying (sending NA for
963 * my address) use lladdr configured for the interface.
968 mac = carp_macmatch6(ifp->if_carp, m, taddr6);
970 mac = nd6_ifptomac(ifp);
972 mac = nd6_ifptomac(ifp);
974 } else if (sdl0->sa_family == AF_LINK) {
975 struct sockaddr_dl *sdl;
976 sdl = (struct sockaddr_dl *)sdl0;
977 if (sdl->sdl_alen == ifp->if_addrlen)
981 if (tlladdr && mac) {
982 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
983 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
985 /* roundup to 8 bytes alignment! */
986 optlen = (optlen + 7) & ~7;
988 m->m_pkthdr.len += optlen;
991 bzero((caddr_t)nd_opt, optlen);
992 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
993 nd_opt->nd_opt_len = optlen >> 3;
994 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
996 flags &= ~ND_NA_FLAG_OVERRIDE;
998 ip6->ip6_plen = htons((u_short)icmp6len);
999 nd_na->nd_na_flags_reserved = flags;
1000 nd_na->nd_na_cksum = 0;
1001 nd_na->nd_na_cksum =
1002 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1004 ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1005 icmp6_ifstat_inc(ifp, ifs6_out_msg);
1006 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1007 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1009 if (ro.ro_rt) { /* we don't cache this route. */
1026 switch (ifp->if_type) {
1034 #ifdef IFT_IEEE80211
1042 return IF_LLADDR(ifp);
1048 TAILQ_HEAD(dadq_head, dadq);
1050 TAILQ_ENTRY(dadq) dad_list;
1051 struct ifaddr *dad_ifa;
1052 int dad_count; /* max NS to send */
1053 int dad_ns_tcount; /* # of trials to send NS */
1054 int dad_ns_ocount; /* NS sent so far */
1057 struct callout dad_timer_ch;
1060 static struct dadq_head dadq;
1061 static int dad_init = 0;
1063 static struct dadq *
1069 for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
1070 if (dp->dad_ifa == ifa)
1077 nd6_dad_starttimer(dp, ticks)
1082 callout_reset(&dp->dad_timer_ch, ticks,
1083 (void (*) __P((void *)))nd6_dad_timer, (void *)dp->dad_ifa);
1087 nd6_dad_stoptimer(dp)
1091 callout_stop(&dp->dad_timer_ch);
1095 * Start Duplicate Address Detection (DAD) for specified interface address.
1098 nd6_dad_start(ifa, delay)
1102 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1111 * If we don't need DAD, don't do it.
1112 * There are several cases:
1113 * - DAD is disabled (ip6_dad_count == 0)
1114 * - the interface address is anycast
1116 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1118 "nd6_dad_start: called with non-tentative address "
1120 ip6_sprintf(&ia->ia_addr.sin6_addr),
1121 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1124 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1125 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1128 if (!ip6_dad_count) {
1129 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1132 if (ifa->ifa_ifp == NULL)
1133 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1134 if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
1137 if (nd6_dad_find(ifa) != NULL) {
1138 /* DAD already in progress */
1142 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1144 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1146 ip6_sprintf(&ia->ia_addr.sin6_addr),
1147 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1150 bzero(dp, sizeof(*dp));
1151 callout_init(&dp->dad_timer_ch, 0);
1152 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1154 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1155 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1158 * Send NS packet for DAD, ip6_dad_count times.
1159 * Note that we must delay the first transmission, if this is the
1160 * first packet to be sent from the interface after interface
1161 * (re)initialization.
1164 IFAREF(ifa); /* just for safety */
1165 dp->dad_count = ip6_dad_count;
1166 dp->dad_ns_icount = dp->dad_na_icount = 0;
1167 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1169 nd6_dad_ns_output(dp, ifa);
1170 nd6_dad_starttimer(dp,
1171 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1173 nd6_dad_starttimer(dp, delay);
1178 * terminate DAD unconditionally. used for address removals.
1188 dp = nd6_dad_find(ifa);
1190 /* DAD wasn't started yet */
1194 nd6_dad_stoptimer(dp);
1196 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1207 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1210 s = splnet(); /* XXX */
1214 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1217 dp = nd6_dad_find(ifa);
1219 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1222 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1223 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1225 ip6_sprintf(&ia->ia_addr.sin6_addr),
1226 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1229 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1230 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1232 ip6_sprintf(&ia->ia_addr.sin6_addr),
1233 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1237 /* timeouted with IFF_{RUNNING,UP} check */
1238 if (dp->dad_ns_tcount > dad_maxtry) {
1239 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1240 if_name(ifa->ifa_ifp)));
1242 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1249 /* Need more checks? */
1250 if (dp->dad_ns_ocount < dp->dad_count) {
1252 * We have more NS to go. Send NS packet for DAD.
1254 nd6_dad_ns_output(dp, ifa);
1255 nd6_dad_starttimer(dp,
1256 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1259 * We have transmitted sufficient number of DAD packets.
1260 * See what we've got.
1266 if (dp->dad_na_icount) {
1268 * the check is in nd6_dad_na_input(),
1274 if (dp->dad_ns_icount) {
1275 /* We've seen NS, means DAD has failed. */
1280 /* (*dp) will be freed in nd6_dad_duplicated() */
1282 nd6_dad_duplicated(ifa);
1285 * We are done with DAD. No NA came, no NS came.
1286 * No duplicate address found.
1288 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1291 "%s: DAD complete for %s - no duplicates found\n",
1292 if_name(ifa->ifa_ifp),
1293 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1295 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1307 nd6_dad_duplicated(ifa)
1310 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1314 dp = nd6_dad_find(ifa);
1316 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1320 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1321 "NS in/out=%d/%d, NA in=%d\n",
1322 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
1323 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1325 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1326 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1328 /* We are done with DAD, with duplicate address found. (failure) */
1329 nd6_dad_stoptimer(dp);
1332 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1333 if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1334 log(LOG_ERR, "%s: manual intervention required\n",
1338 * If the address is a link-local address formed from an interface
1339 * identifier based on the hardware address which is supposed to be
1340 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1341 * operation on the interface SHOULD be disabled.
1342 * [rfc2462bis-03 Section 5.4.5]
1344 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1345 struct in6_addr in6;
1348 * To avoid over-reaction, we only apply this logic when we are
1349 * very sure that hardware addresses are supposed to be unique.
1351 switch (ifp->if_type) {
1356 #ifdef IFT_IEEE80211
1359 in6 = ia->ia_addr.sin6_addr;
1360 if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1361 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1362 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1363 log(LOG_ERR, "%s: possible hardware address "
1364 "duplication detected, disable IPv6\n",
1371 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1378 nd6_dad_ns_output(dp, ifa)
1382 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1383 struct ifnet *ifp = ifa->ifa_ifp;
1385 dp->dad_ns_tcount++;
1386 if ((ifp->if_flags & IFF_UP) == 0) {
1389 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1393 dp->dad_ns_ocount++;
1394 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1398 nd6_dad_ns_input(ifa)
1401 struct in6_ifaddr *ia;
1403 const struct in6_addr *taddr6;
1408 panic("ifa == NULL in nd6_dad_ns_input");
1410 ia = (struct in6_ifaddr *)ifa;
1412 taddr6 = &ia->ia_addr.sin6_addr;
1414 dp = nd6_dad_find(ifa);
1416 /* Quickhack - completely ignore DAD NS packets */
1417 if (dad_ignore_ns) {
1419 "nd6_dad_ns_input: ignoring DAD NS packet for "
1420 "address %s(%s)\n", ip6_sprintf(taddr6),
1421 if_name(ifa->ifa_ifp)));
1426 * if I'm yet to start DAD, someone else started using this address
1427 * first. I have a duplicate and you win.
1429 if (dp == NULL || dp->dad_ns_ocount == 0)
1432 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1435 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1436 nd6_dad_duplicated(ifa);
1439 * not sure if I got a duplicate.
1440 * increment ns count and see what happens.
1443 dp->dad_ns_icount++;
1448 nd6_dad_na_input(ifa)
1454 panic("ifa == NULL in nd6_dad_na_input");
1456 dp = nd6_dad_find(ifa);
1458 dp->dad_na_icount++;
1460 /* remove the address. */
1461 nd6_dad_duplicated(ifa);