2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
37 #include "opt_ipsec.h"
39 #include "opt_mpath.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
48 #include <sys/kernel.h>
49 #include <sys/errno.h>
50 #include <sys/syslog.h>
51 #include <sys/queue.h>
52 #include <sys/callout.h>
55 #include <net/if_types.h>
56 #include <net/if_dl.h>
57 #include <net/if_var.h>
58 #include <net/route.h>
60 #include <net/radix_mpath.h>
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet6/in6_ifattach.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/scope6_var.h>
70 #include <netinet6/nd6.h>
71 #include <netinet/icmp6.h>
74 #include <netinet/ip_carp.h>
77 #define SDL(s) ((struct sockaddr_dl *)s)
80 static struct dadq *nd6_dad_find(struct ifaddr *);
81 static void nd6_dad_starttimer(struct dadq *, int);
82 static void nd6_dad_stoptimer(struct dadq *);
83 static void nd6_dad_timer(struct ifaddr *);
84 static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
85 static void nd6_dad_ns_input(struct ifaddr *);
86 static void nd6_dad_na_input(struct ifaddr *);
88 static int dad_ignore_ns = 0; /* ignore NS in DAD - specwise incorrect*/
89 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
92 * Input a Neighbor Solicitation Message.
95 * Based on RFC 2462 (duplicate address detection)
98 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
100 struct ifnet *ifp = m->m_pkthdr.rcvif;
101 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
102 struct nd_neighbor_solicit *nd_ns;
103 struct in6_addr saddr6 = ip6->ip6_src;
104 struct in6_addr daddr6 = ip6->ip6_dst;
105 struct in6_addr taddr6;
106 struct in6_addr myaddr6;
108 struct ifaddr *ifa = NULL;
110 int anycast = 0, proxy = 0, tentative = 0;
112 union nd_opts ndopts;
113 struct sockaddr_dl *proxydl = NULL;
114 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
116 #ifndef PULLDOWN_TEST
117 IP6_EXTHDR_CHECK(m, off, icmp6len,);
118 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
120 IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
122 icmp6stat.icp6s_tooshort++;
126 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
127 taddr6 = nd_ns->nd_ns_target;
128 if (in6_setscope(&taddr6, ifp, NULL) != 0)
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(ip6bufs, &ip6->ip6_src),
135 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
139 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
140 /* dst has to be a 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 icmp6len -= sizeof(*nd_ns);
160 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
161 if (nd6_options(&ndopts) < 0) {
163 "nd6_ns_input: invalid ND option, ignored\n"));
164 /* nd6_options have incremented stats */
168 if (ndopts.nd_opts_src_lladdr) {
169 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
170 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
173 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
174 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
175 "(link-layer address option)\n"));
180 * Attaching target link-layer address to the NA?
183 * NS IP dst is unicast/anycast MUST NOT add
184 * NS IP dst is solicited-node multicast MUST add
186 * In implementation, we add target link-layer address by default.
187 * We do not add one in MUST NOT cases.
189 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
195 * Target address (taddr6) must be either:
196 * (1) Valid unicast/anycast address for my receiving interface,
197 * (2) Unicast address for which I'm offering proxy service, or
198 * (3) "tentative" address on which DAD is being performed.
200 /* (1) and (3) check. */
203 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
205 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
207 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
213 struct sockaddr_in6 tsin6;
219 bzero(&tsin6, sizeof tsin6);
220 tsin6.sin6_len = sizeof(struct sockaddr_in6);
221 tsin6.sin6_family = AF_INET6;
222 tsin6.sin6_addr = taddr6;
225 bzero(&ro, sizeof(ro));
227 rtalloc_mpath((struct route *)&ro, RTF_ANNOUNCE);
230 rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
232 need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
233 rt->rt_gateway->sa_family == AF_LINK);
238 * proxy NDP for single entry
240 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
241 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
244 proxydl = SDL(rt->rt_gateway);
250 * We've got an NS packet, and we don't have that adddress
251 * assigned for us. We MUST silently ignore it.
256 myaddr6 = *IFA_IN6(ifa);
257 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
258 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
259 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
262 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
263 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
264 "(if %d, NS packet %d)\n",
265 ip6_sprintf(ip6bufs, &taddr6),
266 ifp->if_addrlen, lladdrlen - 2));
270 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
271 nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
272 ip6_sprintf(ip6bufs, &saddr6)));
277 * We have neighbor solicitation packet, with target address equals to
278 * one of my tentative address.
280 * src addr how to process?
282 * multicast of course, invalid (rejected in ip6_input)
283 * unicast somebody is doing address resolution -> ignore
284 * unspec dup address detection
286 * The processing is defined in RFC 2462.
290 * If source address is unspecified address, it is for
291 * duplicate address detection.
293 * If not, the packet is for addess resolution;
294 * silently ignore it.
296 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
297 nd6_dad_ns_input(ifa);
303 * If the source address is unspecified address, entries must not
304 * be created or updated.
305 * It looks that sender is performing DAD. Output NA toward
306 * all-node multicast address, to tell the sender that I'm using
308 * S bit ("solicited") must be zero.
310 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
311 struct in6_addr in6_all;
313 in6_all = in6addr_linklocal_allnodes;
314 if (in6_setscope(&in6_all, ifp, NULL) != 0)
316 nd6_na_output(ifp, &in6_all, &taddr6,
317 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
318 (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
319 tlladdr, (struct sockaddr *)proxydl);
323 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
324 ND_NEIGHBOR_SOLICIT, 0);
326 nd6_na_output(ifp, &saddr6, &taddr6,
327 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
328 (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
329 tlladdr, (struct sockaddr *)proxydl);
335 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
336 ip6_sprintf(ip6bufs, &saddr6)));
337 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
338 ip6_sprintf(ip6bufs, &daddr6)));
339 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
340 ip6_sprintf(ip6bufs, &taddr6)));
341 icmp6stat.icp6s_badns++;
346 * Output a Neighbor Solicitation Message. Caller specifies:
347 * - ICMP6 header source IP6 address
348 * - ND6 header target IP6 address
349 * - ND6 header source datalink address
352 * Based on RFC 2462 (duplicate address detection)
354 * ln - for source address determination
355 * dad - duplicate address detection
358 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
359 const struct in6_addr *taddr6, struct llinfo_nd6 *ln, int dad)
363 struct nd_neighbor_solicit *nd_ns;
364 struct in6_addr *src, src_in;
365 struct ip6_moptions im6o;
371 bzero(&ro, sizeof(ro));
373 if (IN6_IS_ADDR_MULTICAST(taddr6))
376 /* estimate the size of message */
377 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
378 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
379 if (max_linkhdr + maxlen >= MCLBYTES) {
381 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
382 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
387 MGETHDR(m, M_DONTWAIT, MT_DATA);
388 if (m && max_linkhdr + maxlen >= MHLEN) {
389 MCLGET(m, M_DONTWAIT);
390 if ((m->m_flags & M_EXT) == 0) {
397 m->m_pkthdr.rcvif = NULL;
399 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
400 m->m_flags |= M_MCAST;
401 im6o.im6o_multicast_ifp = ifp;
402 im6o.im6o_multicast_hlim = 255;
403 im6o.im6o_multicast_loop = 0;
406 icmp6len = sizeof(*nd_ns);
407 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
408 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
410 /* fill neighbor solicitation packet */
411 ip6 = mtod(m, struct ip6_hdr *);
413 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
414 ip6->ip6_vfc |= IPV6_VERSION;
415 /* ip6->ip6_plen will be set later */
416 ip6->ip6_nxt = IPPROTO_ICMPV6;
419 ip6->ip6_dst = *daddr6;
421 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
422 ip6->ip6_dst.s6_addr16[1] = 0;
423 ip6->ip6_dst.s6_addr32[1] = 0;
424 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
425 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
426 ip6->ip6_dst.s6_addr8[12] = 0xff;
427 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
433 * "If the source address of the packet prompting the
434 * solicitation is the same as one of the addresses assigned
435 * to the outgoing interface, that address SHOULD be placed
436 * in the IP Source Address of the outgoing solicitation.
437 * Otherwise, any one of the addresses assigned to the
438 * interface should be used."
440 * We use the source address for the prompting packet
442 * - saddr6 is given from the caller (by giving "ln"), and
443 * - saddr6 belongs to the outgoing interface.
444 * Otherwise, we perform the source address selection as usual.
446 struct ip6_hdr *hip6; /* hold ip6 */
447 struct in6_addr *hsrc = NULL;
449 if (ln && ln->ln_hold) {
451 * assuming every packet in ln_hold has the same IP
454 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
456 if (sizeof(*hip6) < ln->ln_hold->m_len)
457 hsrc = &hip6->ip6_src;
461 if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
465 struct sockaddr_in6 dst_sa;
467 bzero(&dst_sa, sizeof(dst_sa));
468 dst_sa.sin6_family = AF_INET6;
469 dst_sa.sin6_len = sizeof(dst_sa);
470 dst_sa.sin6_addr = ip6->ip6_dst;
472 src = in6_selectsrc(&dst_sa, NULL,
473 NULL, &ro, NULL, NULL, &error);
475 char ip6buf[INET6_ADDRSTRLEN];
477 "nd6_ns_output: source can't be "
478 "determined: dst=%s, error=%d\n",
479 ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
486 * Source address for DAD packet must always be IPv6
487 * unspecified address. (0::0)
488 * We actually don't have to 0-clear the address (we did it
489 * above), but we do so here explicitly to make the intention
492 bzero(&src_in, sizeof(src_in));
496 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
497 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
498 nd_ns->nd_ns_code = 0;
499 nd_ns->nd_ns_reserved = 0;
500 nd_ns->nd_ns_target = *taddr6;
501 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
504 * Add source link-layer address option.
506 * spec implementation
508 * DAD packet MUST NOT do not add the option
509 * there's no link layer address:
510 * impossible do not add the option
511 * there's link layer address:
512 * Multicast NS MUST add one add the option
513 * Unicast NS SHOULD add one add the option
515 if (!dad && (mac = nd6_ifptomac(ifp))) {
516 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
517 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
518 /* 8 byte alignments... */
519 optlen = (optlen + 7) & ~7;
521 m->m_pkthdr.len += optlen;
524 bzero((caddr_t)nd_opt, optlen);
525 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
526 nd_opt->nd_opt_len = optlen >> 3;
527 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
530 ip6->ip6_plen = htons((u_short)icmp6len);
531 nd_ns->nd_ns_cksum = 0;
533 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
535 ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
536 icmp6_ifstat_inc(ifp, ifs6_out_msg);
537 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
538 icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
540 if (ro.ro_rt) { /* we don't cache this route. */
554 * Neighbor advertisement input handling.
557 * Based on RFC 2462 (duplicate address detection)
559 * the following items are not implemented yet:
560 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
561 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
564 nd6_na_input(struct mbuf *m, int off, int icmp6len)
566 struct ifnet *ifp = m->m_pkthdr.rcvif;
567 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
568 struct nd_neighbor_advert *nd_na;
569 struct in6_addr daddr6 = ip6->ip6_dst;
570 struct in6_addr taddr6;
578 struct llinfo_nd6 *ln;
580 struct sockaddr_dl *sdl;
581 union nd_opts ndopts;
582 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
584 if (ip6->ip6_hlim != 255) {
586 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
587 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
588 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
592 #ifndef PULLDOWN_TEST
593 IP6_EXTHDR_CHECK(m, off, icmp6len,);
594 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
596 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
598 icmp6stat.icp6s_tooshort++;
603 flags = nd_na->nd_na_flags_reserved;
604 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
605 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
606 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
608 taddr6 = nd_na->nd_na_target;
609 if (in6_setscope(&taddr6, ifp, NULL))
610 goto bad; /* XXX: impossible */
612 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
614 "nd6_na_input: invalid target address %s\n",
615 ip6_sprintf(ip6bufs, &taddr6)));
618 if (IN6_IS_ADDR_MULTICAST(&daddr6))
621 "nd6_na_input: a solicited adv is multicasted\n"));
625 icmp6len -= sizeof(*nd_na);
626 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
627 if (nd6_options(&ndopts) < 0) {
629 "nd6_na_input: invalid ND option, ignored\n"));
630 /* nd6_options have incremented stats */
634 if (ndopts.nd_opts_tgt_lladdr) {
635 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
636 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
639 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
642 * Target address matches one of my interface address.
644 * If my address is tentative, this means that there's somebody
645 * already using the same address as mine. This indicates DAD failure.
646 * This is defined in RFC 2462.
648 * Otherwise, process as defined in RFC 2461.
651 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
652 nd6_dad_na_input(ifa);
656 /* Just for safety, maybe unnecessary. */
659 "nd6_na_input: duplicate IP6 address %s\n",
660 ip6_sprintf(ip6bufs, &taddr6));
664 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
665 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
666 "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
667 ifp->if_addrlen, lladdrlen - 2));
672 * If no neighbor cache entry is found, NA SHOULD silently be
675 rt = nd6_lookup(&taddr6, 0, ifp);
677 ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
678 ((sdl = SDL(rt->rt_gateway)) == NULL))
681 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
683 * If the link-layer has address, and no lladdr option came,
684 * discard the packet.
686 if (ifp->if_addrlen && lladdr == NULL)
690 * Record link-layer address, and update the state.
692 sdl->sdl_alen = ifp->if_addrlen;
693 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
695 ln->ln_state = ND6_LLINFO_REACHABLE;
697 if (!ND6_LLINFO_PERMANENT(ln)) {
698 nd6_llinfo_settimer(ln,
699 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
702 ln->ln_state = ND6_LLINFO_STALE;
703 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
705 if ((ln->ln_router = is_router) != 0) {
707 * This means a router's state has changed from
708 * non-reachable to probably reachable, and might
709 * affect the status of associated prefixes..
711 pfxlist_onlink_check();
717 * Check if the link-layer address has changed or not.
723 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
732 * This is VERY complex. Look at it with care.
734 * override solicit lladdr llchange action
739 * 0 0 y y (1) REACHABLE->STALE
740 * 0 1 n -- (2c) *->REACHABLE
741 * 0 1 y n (2b) L *->REACHABLE
742 * 0 1 y y (1) REACHABLE->STALE
745 * 1 0 y y (2a) L *->STALE
746 * 1 1 n -- (2a) *->REACHABLE
747 * 1 1 y n (2a) L *->REACHABLE
748 * 1 1 y y (2a) L *->REACHABLE
750 if (!is_override && (lladdr != NULL && llchange)) { /* (1) */
752 * If state is REACHABLE, make it STALE.
753 * no other updates should be done.
755 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
756 ln->ln_state = ND6_LLINFO_STALE;
757 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
760 } else if (is_override /* (2a) */
761 || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
762 || lladdr == NULL) { /* (2c) */
764 * Update link-local address, if any.
766 if (lladdr != NULL) {
767 sdl->sdl_alen = ifp->if_addrlen;
768 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
772 * If solicited, make the state REACHABLE.
773 * If not solicited and the link-layer address was
774 * changed, make it STALE.
777 ln->ln_state = ND6_LLINFO_REACHABLE;
779 if (!ND6_LLINFO_PERMANENT(ln)) {
780 nd6_llinfo_settimer(ln,
781 (long)ND_IFINFO(ifp)->reachable * hz);
784 if (lladdr != NULL && llchange) {
785 ln->ln_state = ND6_LLINFO_STALE;
786 nd6_llinfo_settimer(ln,
787 (long)nd6_gctimer * hz);
792 if (ln->ln_router && !is_router) {
794 * The peer dropped the router flag.
795 * Remove the sender from the Default Router List and
796 * update the Destination Cache entries.
798 struct nd_defrouter *dr;
799 struct in6_addr *in6;
802 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
805 * Lock to protect the default router list.
806 * XXX: this might be unnecessary, since this function
807 * is only called under the network software interrupt
808 * context. However, we keep it just for safety.
811 dr = defrouter_lookup(in6, ifp);
814 else if (!ip6_forwarding) {
816 * Even if the neighbor is not in the default
817 * router list, the neighbor may be used
818 * as a next hop for some destinations
819 * (e.g. redirect case). So we must
820 * call rt6_flush explicitly.
822 rt6_flush(&ip6->ip6_src, ifp);
826 ln->ln_router = is_router;
828 rt->rt_flags &= ~RTF_REJECT;
831 struct mbuf *m_hold, *m_hold_next;
834 * reset the ln_hold in advance, to explicitly
835 * prevent a ln_hold lookup in nd6_output()
836 * (wouldn't happen, though...)
838 for (m_hold = ln->ln_hold;
839 m_hold; m_hold = m_hold_next) {
840 m_hold_next = m_hold->m_nextpkt;
841 m_hold->m_nextpkt = NULL;
843 * we assume ifp is not a loopback here, so just set
844 * the 2nd argument as the 1st one.
846 nd6_output(ifp, ifp, m_hold,
847 (struct sockaddr_in6 *)rt_key(rt), rt);
857 icmp6stat.icp6s_badna++;
862 * Neighbor advertisement output handling.
866 * the following items are not implemented yet:
867 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
868 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
870 * tlladdr - 1 if include target link-layer address
871 * sdl0 - sockaddr_dl (= proxy NA) or NULL
874 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
875 const struct in6_addr *taddr6, u_long flags, int tlladdr,
876 struct sockaddr *sdl0)
880 struct nd_neighbor_advert *nd_na;
881 struct ip6_moptions im6o;
882 struct in6_addr *src, daddr6;
883 struct sockaddr_in6 dst_sa;
884 int icmp6len, maxlen, error;
888 bzero(&ro, sizeof(ro));
890 daddr6 = *daddr6_0; /* make a local copy for modification */
892 /* estimate the size of message */
893 maxlen = sizeof(*ip6) + sizeof(*nd_na);
894 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
895 if (max_linkhdr + maxlen >= MCLBYTES) {
897 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
898 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
903 MGETHDR(m, M_DONTWAIT, MT_DATA);
904 if (m && max_linkhdr + maxlen >= MHLEN) {
905 MCLGET(m, M_DONTWAIT);
906 if ((m->m_flags & M_EXT) == 0) {
913 m->m_pkthdr.rcvif = NULL;
915 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
916 m->m_flags |= M_MCAST;
917 im6o.im6o_multicast_ifp = ifp;
918 im6o.im6o_multicast_hlim = 255;
919 im6o.im6o_multicast_loop = 0;
922 icmp6len = sizeof(*nd_na);
923 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
924 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
926 /* fill neighbor advertisement packet */
927 ip6 = mtod(m, struct ip6_hdr *);
929 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
930 ip6->ip6_vfc |= IPV6_VERSION;
931 ip6->ip6_nxt = IPPROTO_ICMPV6;
933 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
935 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
936 daddr6.s6_addr16[1] = 0;
937 daddr6.s6_addr32[1] = 0;
938 daddr6.s6_addr32[2] = 0;
939 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
940 if (in6_setscope(&daddr6, ifp, NULL))
943 flags &= ~ND_NA_FLAG_SOLICITED;
945 ip6->ip6_dst = daddr6;
946 bzero(&dst_sa, sizeof(struct sockaddr_in6));
947 dst_sa.sin6_family = AF_INET6;
948 dst_sa.sin6_len = sizeof(struct sockaddr_in6);
949 dst_sa.sin6_addr = daddr6;
952 * Select a source whose scope is the same as that of the dest.
954 bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
955 src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error);
957 char ip6buf[INET6_ADDRSTRLEN];
958 nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
959 "determined: dst=%s, error=%d\n",
960 ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
964 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
965 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
966 nd_na->nd_na_code = 0;
967 nd_na->nd_na_target = *taddr6;
968 in6_clearscope(&nd_na->nd_na_target); /* XXX */
971 * "tlladdr" indicates NS's condition for adding tlladdr or not.
972 * see nd6_ns_input() for details.
973 * Basically, if NS packet is sent to unicast/anycast addr,
974 * target lladdr option SHOULD NOT be included.
978 * sdl0 != NULL indicates proxy NA. If we do proxy, use
979 * lladdr in sdl0. If we are not proxying (sending NA for
980 * my address) use lladdr configured for the interface.
985 mac = carp_macmatch6(ifp->if_carp, m, taddr6);
987 mac = nd6_ifptomac(ifp);
989 mac = nd6_ifptomac(ifp);
991 } else if (sdl0->sa_family == AF_LINK) {
992 struct sockaddr_dl *sdl;
993 sdl = (struct sockaddr_dl *)sdl0;
994 if (sdl->sdl_alen == ifp->if_addrlen)
998 if (tlladdr && mac) {
999 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1000 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1002 /* roundup to 8 bytes alignment! */
1003 optlen = (optlen + 7) & ~7;
1005 m->m_pkthdr.len += optlen;
1008 bzero((caddr_t)nd_opt, optlen);
1009 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1010 nd_opt->nd_opt_len = optlen >> 3;
1011 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1013 flags &= ~ND_NA_FLAG_OVERRIDE;
1015 ip6->ip6_plen = htons((u_short)icmp6len);
1016 nd_na->nd_na_flags_reserved = flags;
1017 nd_na->nd_na_cksum = 0;
1018 nd_na->nd_na_cksum =
1019 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1021 ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1022 icmp6_ifstat_inc(ifp, ifs6_out_msg);
1023 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1024 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1026 if (ro.ro_rt) { /* we don't cache this route. */
1040 nd6_ifptomac(struct ifnet *ifp)
1042 switch (ifp->if_type) {
1050 #ifdef IFT_IEEE80211
1058 return IF_LLADDR(ifp);
1064 TAILQ_HEAD(dadq_head, dadq);
1066 TAILQ_ENTRY(dadq) dad_list;
1067 struct ifaddr *dad_ifa;
1068 int dad_count; /* max NS to send */
1069 int dad_ns_tcount; /* # of trials to send NS */
1070 int dad_ns_ocount; /* NS sent so far */
1073 struct callout dad_timer_ch;
1076 static struct dadq_head dadq;
1077 static int dad_init = 0;
1079 static struct dadq *
1080 nd6_dad_find(struct ifaddr *ifa)
1084 for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
1085 if (dp->dad_ifa == ifa)
1092 nd6_dad_starttimer(struct dadq *dp, int ticks)
1095 callout_reset(&dp->dad_timer_ch, ticks,
1096 (void (*)(void *))nd6_dad_timer, (void *)dp->dad_ifa);
1100 nd6_dad_stoptimer(struct dadq *dp)
1103 callout_stop(&dp->dad_timer_ch);
1107 * Start Duplicate Address Detection (DAD) for specified interface address.
1110 nd6_dad_start(struct ifaddr *ifa, int delay)
1112 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1114 char ip6buf[INET6_ADDRSTRLEN];
1122 * If we don't need DAD, don't do it.
1123 * There are several cases:
1124 * - DAD is disabled (ip6_dad_count == 0)
1125 * - the interface address is anycast
1127 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1129 "nd6_dad_start: called with non-tentative address "
1131 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1132 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1135 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1136 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1139 if (!ip6_dad_count) {
1140 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1143 if (ifa->ifa_ifp == NULL)
1144 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1145 if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
1148 if (nd6_dad_find(ifa) != NULL) {
1149 /* DAD already in progress */
1153 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1155 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1157 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1158 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1161 bzero(dp, sizeof(*dp));
1162 callout_init(&dp->dad_timer_ch, 0);
1163 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1165 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1166 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1169 * Send NS packet for DAD, ip6_dad_count times.
1170 * Note that we must delay the first transmission, if this is the
1171 * first packet to be sent from the interface after interface
1172 * (re)initialization.
1175 IFAREF(ifa); /* just for safety */
1176 dp->dad_count = ip6_dad_count;
1177 dp->dad_ns_icount = dp->dad_na_icount = 0;
1178 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1180 nd6_dad_ns_output(dp, ifa);
1181 nd6_dad_starttimer(dp,
1182 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1184 nd6_dad_starttimer(dp, delay);
1189 * terminate DAD unconditionally. used for address removals.
1192 nd6_dad_stop(struct ifaddr *ifa)
1198 dp = nd6_dad_find(ifa);
1200 /* DAD wasn't started yet */
1204 nd6_dad_stoptimer(dp);
1206 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1213 nd6_dad_timer(struct ifaddr *ifa)
1216 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1218 char ip6buf[INET6_ADDRSTRLEN];
1220 s = splnet(); /* XXX */
1224 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1227 dp = nd6_dad_find(ifa);
1229 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1232 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1233 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1235 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1236 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1239 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1240 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1242 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1243 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1247 /* timeouted with IFF_{RUNNING,UP} check */
1248 if (dp->dad_ns_tcount > dad_maxtry) {
1249 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1250 if_name(ifa->ifa_ifp)));
1252 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1259 /* Need more checks? */
1260 if (dp->dad_ns_ocount < dp->dad_count) {
1262 * We have more NS to go. Send NS packet for DAD.
1264 nd6_dad_ns_output(dp, ifa);
1265 nd6_dad_starttimer(dp,
1266 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1269 * We have transmitted sufficient number of DAD packets.
1270 * See what we've got.
1276 if (dp->dad_na_icount) {
1278 * the check is in nd6_dad_na_input(),
1284 if (dp->dad_ns_icount) {
1285 /* We've seen NS, means DAD has failed. */
1290 /* (*dp) will be freed in nd6_dad_duplicated() */
1292 nd6_dad_duplicated(ifa);
1295 * We are done with DAD. No NA came, no NS came.
1296 * No duplicate address found.
1298 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1301 "%s: DAD complete for %s - no duplicates found\n",
1302 if_name(ifa->ifa_ifp),
1303 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1305 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1317 nd6_dad_duplicated(struct ifaddr *ifa)
1319 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1322 char ip6buf[INET6_ADDRSTRLEN];
1324 dp = nd6_dad_find(ifa);
1326 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1330 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1331 "NS in/out=%d/%d, NA in=%d\n",
1332 if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1333 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1335 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1336 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1338 /* We are done with DAD, with duplicate address found. (failure) */
1339 nd6_dad_stoptimer(dp);
1342 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1343 if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
1344 log(LOG_ERR, "%s: manual intervention required\n",
1348 * If the address is a link-local address formed from an interface
1349 * identifier based on the hardware address which is supposed to be
1350 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1351 * operation on the interface SHOULD be disabled.
1352 * [rfc2462bis-03 Section 5.4.5]
1354 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1355 struct in6_addr in6;
1358 * To avoid over-reaction, we only apply this logic when we are
1359 * very sure that hardware addresses are supposed to be unique.
1361 switch (ifp->if_type) {
1366 #ifdef IFT_IEEE80211
1369 in6 = ia->ia_addr.sin6_addr;
1370 if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1371 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1372 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1373 log(LOG_ERR, "%s: possible hardware address "
1374 "duplication detected, disable IPv6\n",
1381 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1388 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1390 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1391 struct ifnet *ifp = ifa->ifa_ifp;
1393 dp->dad_ns_tcount++;
1394 if ((ifp->if_flags & IFF_UP) == 0) {
1397 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1401 dp->dad_ns_ocount++;
1402 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1406 nd6_dad_ns_input(struct ifaddr *ifa)
1408 struct in6_ifaddr *ia;
1410 const struct in6_addr *taddr6;
1415 panic("ifa == NULL in nd6_dad_ns_input");
1417 ia = (struct in6_ifaddr *)ifa;
1419 taddr6 = &ia->ia_addr.sin6_addr;
1421 dp = nd6_dad_find(ifa);
1423 /* Quickhack - completely ignore DAD NS packets */
1424 if (dad_ignore_ns) {
1425 char ip6buf[INET6_ADDRSTRLEN];
1427 "nd6_dad_ns_input: ignoring DAD NS packet for "
1428 "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
1429 if_name(ifa->ifa_ifp)));
1434 * if I'm yet to start DAD, someone else started using this address
1435 * first. I have a duplicate and you win.
1437 if (dp == NULL || dp->dad_ns_ocount == 0)
1440 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1443 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1444 nd6_dad_duplicated(ifa);
1447 * not sure if I got a duplicate.
1448 * increment ns count and see what happens.
1451 dp->dad_ns_icount++;
1456 nd6_dad_na_input(struct ifaddr *ifa)
1461 panic("ifa == NULL in nd6_dad_na_input");
1463 dp = nd6_dad_find(ifa);
1465 dp->dad_na_icount++;
1467 /* remove the address. */
1468 nd6_dad_duplicated(ifa);