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"
38 #include "opt_mpath.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
44 #include <sys/rwlock.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
49 #include <sys/kernel.h>
50 #include <sys/errno.h>
51 #include <sys/syslog.h>
52 #include <sys/queue.h>
53 #include <sys/callout.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/if_var.h>
59 #include <net/route.h>
61 #include <net/radix_mpath.h>
64 #include <netinet/in.h>
65 #include <netinet/in_var.h>
66 #include <net/if_llatbl.h>
67 #define L3_ADDR_SIN6(le) ((struct sockaddr_in6 *) L3_ADDR(le))
68 #include <netinet6/in6_var.h>
69 #include <netinet6/in6_ifattach.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/scope6_var.h>
73 #include <netinet6/nd6.h>
74 #include <netinet/icmp6.h>
75 #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 dadq *);
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 VNET_DEFINE(int, dad_ignore_ns) = 0; /* ignore NS in DAD - specwise incorrect*/
89 VNET_DEFINE(int, dad_maxtry) = 15; /* max # of *tries* to transmit DAD packet */
90 #define V_dad_ignore_ns VNET(dad_ignore_ns)
91 #define V_dad_maxtry VNET(dad_maxtry)
94 * Input a Neighbor Solicitation Message.
97 * Based on RFC 2462 (duplicate address detection)
100 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
102 struct ifnet *ifp = m->m_pkthdr.rcvif;
103 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
104 struct nd_neighbor_solicit *nd_ns;
105 struct in6_addr saddr6 = ip6->ip6_src;
106 struct in6_addr daddr6 = ip6->ip6_dst;
107 struct in6_addr taddr6;
108 struct in6_addr myaddr6;
110 struct ifaddr *ifa = NULL;
112 int anycast = 0, proxy = 0, tentative = 0;
114 union nd_opts ndopts;
115 struct sockaddr_dl *proxydl = NULL;
116 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
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_INC(icp6s_tooshort);
128 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
129 taddr6 = nd_ns->nd_ns_target;
130 if (in6_setscope(&taddr6, ifp, NULL) != 0)
133 if (ip6->ip6_hlim != 255) {
135 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
136 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
137 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
141 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
142 /* dst has to be a solicited node multicast address. */
143 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
144 /* don't check ifindex portion */
145 daddr6.s6_addr32[1] == 0 &&
146 daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
147 daddr6.s6_addr8[12] == 0xff) {
150 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
151 "(wrong ip6 dst)\n"));
154 } else if (!V_nd6_onlink_ns_rfc4861) {
155 struct sockaddr_in6 src_sa6;
158 * According to recent IETF discussions, it is not a good idea
159 * to accept a NS from an address which would not be deemed
160 * to be a neighbor otherwise. This point is expected to be
161 * clarified in future revisions of the specification.
163 bzero(&src_sa6, sizeof(src_sa6));
164 src_sa6.sin6_family = AF_INET6;
165 src_sa6.sin6_len = sizeof(src_sa6);
166 src_sa6.sin6_addr = saddr6;
167 if (nd6_is_addr_neighbor(&src_sa6, ifp) == 0) {
168 nd6log((LOG_INFO, "nd6_ns_input: "
169 "NS packet from non-neighbor\n"));
174 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
175 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
179 icmp6len -= sizeof(*nd_ns);
180 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
181 if (nd6_options(&ndopts) < 0) {
183 "nd6_ns_input: invalid ND option, ignored\n"));
184 /* nd6_options have incremented stats */
188 if (ndopts.nd_opts_src_lladdr) {
189 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
190 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
193 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
194 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
195 "(link-layer address option)\n"));
200 * Attaching target link-layer address to the NA?
203 * NS IP dst is unicast/anycast MUST NOT add
204 * NS IP dst is solicited-node multicast MUST add
206 * In implementation, we add target link-layer address by default.
207 * We do not add one in MUST NOT cases.
209 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
215 * Target address (taddr6) must be either:
216 * (1) Valid unicast/anycast address for my receiving interface,
217 * (2) Unicast address for which I'm offering proxy service, or
218 * (3) "tentative" address on which DAD is being performed.
220 /* (1) and (3) check. */
222 ifa = (*carp_iamatch6_p)(ifp, &taddr6);
224 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
229 struct sockaddr_in6 tsin6;
235 bzero(&tsin6, sizeof tsin6);
236 tsin6.sin6_len = sizeof(struct sockaddr_in6);
237 tsin6.sin6_family = AF_INET6;
238 tsin6.sin6_addr = taddr6;
241 bzero(&ro, sizeof(ro));
243 rtalloc_mpath((struct route *)&ro, RTF_ANNOUNCE);
246 rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
248 need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
249 rt->rt_gateway->sa_family == AF_LINK);
254 * proxy NDP for single entry
256 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
257 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
260 proxydl = SDL(rt->rt_gateway);
266 * We've got an NS packet, and we don't have that adddress
267 * assigned for us. We MUST silently ignore it.
272 myaddr6 = *IFA_IN6(ifa);
273 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
274 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
275 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
278 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
279 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
280 "(if %d, NS packet %d)\n",
281 ip6_sprintf(ip6bufs, &taddr6),
282 ifp->if_addrlen, lladdrlen - 2));
286 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
287 nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
288 ip6_sprintf(ip6bufs, &saddr6)));
293 * We have neighbor solicitation packet, with target address equals to
294 * one of my tentative address.
296 * src addr how to process?
298 * multicast of course, invalid (rejected in ip6_input)
299 * unicast somebody is doing address resolution -> ignore
300 * unspec dup address detection
302 * The processing is defined in RFC 2462.
306 * If source address is unspecified address, it is for
307 * duplicate address detection.
309 * If not, the packet is for addess resolution;
310 * silently ignore it.
312 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
313 nd6_dad_ns_input(ifa);
319 * If the source address is unspecified address, entries must not
320 * be created or updated.
321 * It looks that sender is performing DAD. Output NA toward
322 * all-node multicast address, to tell the sender that I'm using
324 * S bit ("solicited") must be zero.
326 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
327 struct in6_addr in6_all;
329 in6_all = in6addr_linklocal_allnodes;
330 if (in6_setscope(&in6_all, ifp, NULL) != 0)
332 nd6_na_output(ifp, &in6_all, &taddr6,
333 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
334 (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
335 tlladdr, (struct sockaddr *)proxydl);
339 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
340 ND_NEIGHBOR_SOLICIT, 0);
342 nd6_na_output(ifp, &saddr6, &taddr6,
343 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
344 (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
345 tlladdr, (struct sockaddr *)proxydl);
353 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
354 ip6_sprintf(ip6bufs, &saddr6)));
355 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
356 ip6_sprintf(ip6bufs, &daddr6)));
357 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
358 ip6_sprintf(ip6bufs, &taddr6)));
359 ICMP6STAT_INC(icp6s_badns);
366 * Output a Neighbor Solicitation Message. Caller specifies:
367 * - ICMP6 header source IP6 address
368 * - ND6 header target IP6 address
369 * - ND6 header source datalink address
372 * Based on RFC 2462 (duplicate address detection)
374 * ln - for source address determination
375 * dad - duplicate address detection
378 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
379 const struct in6_addr *taddr6, struct llentry *ln, int dad)
383 struct nd_neighbor_solicit *nd_ns;
384 struct ip6_moptions im6o;
390 if (IN6_IS_ADDR_MULTICAST(taddr6))
393 /* estimate the size of message */
394 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
395 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
396 if (max_linkhdr + maxlen >= MCLBYTES) {
398 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
399 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
404 MGETHDR(m, M_DONTWAIT, MT_DATA);
405 if (m && max_linkhdr + maxlen >= MHLEN) {
406 MCLGET(m, M_DONTWAIT);
407 if ((m->m_flags & M_EXT) == 0) {
414 m->m_pkthdr.rcvif = NULL;
416 bzero(&ro, sizeof(ro));
418 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
419 m->m_flags |= M_MCAST;
420 im6o.im6o_multicast_ifp = ifp;
421 im6o.im6o_multicast_hlim = 255;
422 im6o.im6o_multicast_loop = 0;
425 icmp6len = sizeof(*nd_ns);
426 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
427 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
429 /* fill neighbor solicitation packet */
430 ip6 = mtod(m, struct ip6_hdr *);
432 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
433 ip6->ip6_vfc |= IPV6_VERSION;
434 /* ip6->ip6_plen will be set later */
435 ip6->ip6_nxt = IPPROTO_ICMPV6;
438 ip6->ip6_dst = *daddr6;
440 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
441 ip6->ip6_dst.s6_addr16[1] = 0;
442 ip6->ip6_dst.s6_addr32[1] = 0;
443 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
444 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
445 ip6->ip6_dst.s6_addr8[12] = 0xff;
446 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
454 * "If the source address of the packet prompting the
455 * solicitation is the same as one of the addresses assigned
456 * to the outgoing interface, that address SHOULD be placed
457 * in the IP Source Address of the outgoing solicitation.
458 * Otherwise, any one of the addresses assigned to the
459 * interface should be used."
461 * We use the source address for the prompting packet
463 * - saddr6 is given from the caller (by giving "ln"), and
464 * - saddr6 belongs to the outgoing interface.
465 * Otherwise, we perform the source address selection as usual.
467 struct in6_addr *hsrc;
472 if (ln->la_hold != NULL) {
473 struct ip6_hdr *hip6; /* hold ip6 */
476 * assuming every packet in la_hold has the same IP
479 hip6 = mtod(ln->la_hold, struct ip6_hdr *);
481 if (sizeof(*hip6) < ln->la_hold->m_len) {
482 ip6->ip6_src = hip6->ip6_src;
483 hsrc = &hip6->ip6_src;
488 if (hsrc && (ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
490 /* ip6_src set already. */
494 struct sockaddr_in6 dst_sa;
495 struct in6_addr src_in;
497 bzero(&dst_sa, sizeof(dst_sa));
498 dst_sa.sin6_family = AF_INET6;
499 dst_sa.sin6_len = sizeof(dst_sa);
500 dst_sa.sin6_addr = ip6->ip6_dst;
502 error = in6_selectsrc(&dst_sa, NULL,
503 NULL, &ro, NULL, NULL, &src_in);
505 char ip6buf[INET6_ADDRSTRLEN];
507 "nd6_ns_output: source can't be "
508 "determined: dst=%s, error=%d\n",
509 ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
513 ip6->ip6_src = src_in;
517 * Source address for DAD packet must always be IPv6
518 * unspecified address. (0::0)
519 * We actually don't have to 0-clear the address (we did it
520 * above), but we do so here explicitly to make the intention
523 bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
525 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
526 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
527 nd_ns->nd_ns_code = 0;
528 nd_ns->nd_ns_reserved = 0;
529 nd_ns->nd_ns_target = *taddr6;
530 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
533 * Add source link-layer address option.
535 * spec implementation
537 * DAD packet MUST NOT do not add the option
538 * there's no link layer address:
539 * impossible do not add the option
540 * there's link layer address:
541 * Multicast NS MUST add one add the option
542 * Unicast NS SHOULD add one add the option
544 if (!dad && (mac = nd6_ifptomac(ifp))) {
545 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
546 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
547 /* 8 byte alignments... */
548 optlen = (optlen + 7) & ~7;
550 m->m_pkthdr.len += optlen;
553 bzero((caddr_t)nd_opt, optlen);
554 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
555 nd_opt->nd_opt_len = optlen >> 3;
556 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
559 ip6->ip6_plen = htons((u_short)icmp6len);
560 nd_ns->nd_ns_cksum = 0;
562 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
564 ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
565 icmp6_ifstat_inc(ifp, ifs6_out_msg);
566 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
567 ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_SOLICIT]);
569 if (ro.ro_rt) { /* we don't cache this route. */
583 * Neighbor advertisement input handling.
586 * Based on RFC 2462 (duplicate address detection)
588 * the following items are not implemented yet:
589 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
590 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
593 nd6_na_input(struct mbuf *m, int off, int icmp6len)
595 struct ifnet *ifp = m->m_pkthdr.rcvif;
596 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
597 struct nd_neighbor_advert *nd_na;
598 struct in6_addr daddr6 = ip6->ip6_dst;
599 struct in6_addr taddr6;
608 struct llentry *ln = NULL;
609 union nd_opts ndopts;
610 struct mbuf *chain = NULL;
611 struct sockaddr_in6 sin6;
612 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
614 if (ip6->ip6_hlim != 255) {
616 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
617 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
618 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
622 #ifndef PULLDOWN_TEST
623 IP6_EXTHDR_CHECK(m, off, icmp6len,);
624 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
626 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
628 ICMP6STAT_INC(icp6s_tooshort);
633 flags = nd_na->nd_na_flags_reserved;
634 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
635 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
636 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
638 taddr6 = nd_na->nd_na_target;
639 if (in6_setscope(&taddr6, ifp, NULL))
640 goto bad; /* XXX: impossible */
642 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
644 "nd6_na_input: invalid target address %s\n",
645 ip6_sprintf(ip6bufs, &taddr6)));
648 if (IN6_IS_ADDR_MULTICAST(&daddr6))
651 "nd6_na_input: a solicited adv is multicasted\n"));
655 icmp6len -= sizeof(*nd_na);
656 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
657 if (nd6_options(&ndopts) < 0) {
659 "nd6_na_input: invalid ND option, ignored\n"));
660 /* nd6_options have incremented stats */
664 if (ndopts.nd_opts_tgt_lladdr) {
665 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
666 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
669 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
672 * Target address matches one of my interface address.
674 * If my address is tentative, this means that there's somebody
675 * already using the same address as mine. This indicates DAD failure.
676 * This is defined in RFC 2462.
678 * Otherwise, process as defined in RFC 2461.
681 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
683 nd6_dad_na_input(ifa);
687 /* Just for safety, maybe unnecessary. */
691 "nd6_na_input: duplicate IP6 address %s\n",
692 ip6_sprintf(ip6bufs, &taddr6));
696 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
697 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
698 "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
699 ifp->if_addrlen, lladdrlen - 2));
704 * If no neighbor cache entry is found, NA SHOULD silently be
708 ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp);
709 IF_AFDATA_UNLOCK(ifp);
714 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
716 * If the link-layer has address, and no lladdr option came,
717 * discard the packet.
719 if (ifp->if_addrlen && lladdr == NULL) {
724 * Record link-layer address, and update the state.
726 bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
727 ln->la_flags |= LLE_VALID;
729 ln->ln_state = ND6_LLINFO_REACHABLE;
731 if (!ND6_LLINFO_PERMANENT(ln)) {
732 nd6_llinfo_settimer_locked(ln,
733 (long)ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
736 ln->ln_state = ND6_LLINFO_STALE;
737 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
739 if ((ln->ln_router = is_router) != 0) {
741 * This means a router's state has changed from
742 * non-reachable to probably reachable, and might
743 * affect the status of associated prefixes..
751 * Check if the link-layer address has changed or not.
756 if (ln->la_flags & LLE_VALID) {
757 if (bcmp(lladdr, &ln->ll_addr, ifp->if_addrlen))
766 * This is VERY complex. Look at it with care.
768 * override solicit lladdr llchange action
773 * 0 0 y y (1) REACHABLE->STALE
774 * 0 1 n -- (2c) *->REACHABLE
775 * 0 1 y n (2b) L *->REACHABLE
776 * 0 1 y y (1) REACHABLE->STALE
779 * 1 0 y y (2a) L *->STALE
780 * 1 1 n -- (2a) *->REACHABLE
781 * 1 1 y n (2a) L *->REACHABLE
782 * 1 1 y y (2a) L *->REACHABLE
784 if (!is_override && (lladdr != NULL && llchange)) { /* (1) */
786 * If state is REACHABLE, make it STALE.
787 * no other updates should be done.
789 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
790 ln->ln_state = ND6_LLINFO_STALE;
791 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
794 } else if (is_override /* (2a) */
795 || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
796 || lladdr == NULL) { /* (2c) */
798 * Update link-local address, if any.
800 if (lladdr != NULL) {
801 bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
802 ln->la_flags |= LLE_VALID;
806 * If solicited, make the state REACHABLE.
807 * If not solicited and the link-layer address was
808 * changed, make it STALE.
811 ln->ln_state = ND6_LLINFO_REACHABLE;
813 if (!ND6_LLINFO_PERMANENT(ln)) {
814 nd6_llinfo_settimer_locked(ln,
815 (long)ND_IFINFO(ifp)->reachable * hz);
818 if (lladdr != NULL && llchange) {
819 ln->ln_state = ND6_LLINFO_STALE;
820 nd6_llinfo_settimer_locked(ln,
821 (long)V_nd6_gctimer * hz);
826 if (ln->ln_router && !is_router) {
828 * The peer dropped the router flag.
829 * Remove the sender from the Default Router List and
830 * update the Destination Cache entries.
832 struct nd_defrouter *dr;
833 struct in6_addr *in6;
835 in6 = &L3_ADDR_SIN6(ln)->sin6_addr;
838 * Lock to protect the default router list.
839 * XXX: this might be unnecessary, since this function
840 * is only called under the network software interrupt
841 * context. However, we keep it just for safety.
843 dr = defrouter_lookup(in6, ln->lle_tbl->llt_ifp);
846 else if (!V_ip6_forwarding) {
848 * Even if the neighbor is not in the default
849 * router list, the neighbor may be used
850 * as a next hop for some destinations
851 * (e.g. redirect case). So we must
852 * call rt6_flush explicitly.
854 rt6_flush(&ip6->ip6_src, ifp);
857 ln->ln_router = is_router;
861 * rt->rt_flags &= ~RTF_REJECT;
865 struct mbuf *m_hold, *m_hold_next;
868 * reset the la_hold in advance, to explicitly
869 * prevent a la_hold lookup in nd6_output()
870 * (wouldn't happen, though...)
872 for (m_hold = ln->la_hold, ln->la_hold = NULL;
873 m_hold; m_hold = m_hold_next) {
874 m_hold_next = m_hold->m_nextpkt;
875 m_hold->m_nextpkt = NULL;
877 * we assume ifp is not a loopback here, so just set
878 * the 2nd argument as the 1st one.
880 nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
886 memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
890 nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
893 pfxlist_onlink_check();
902 ICMP6STAT_INC(icp6s_badna);
907 * Neighbor advertisement output handling.
911 * the following items are not implemented yet:
912 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
913 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
915 * tlladdr - 1 if include target link-layer address
916 * sdl0 - sockaddr_dl (= proxy NA) or NULL
919 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
920 const struct in6_addr *taddr6, u_long flags, int tlladdr,
921 struct sockaddr *sdl0)
925 struct nd_neighbor_advert *nd_na;
926 struct ip6_moptions im6o;
927 struct in6_addr src, daddr6;
928 struct sockaddr_in6 dst_sa;
929 int icmp6len, maxlen, error;
933 bzero(&ro, sizeof(ro));
935 daddr6 = *daddr6_0; /* make a local copy for modification */
937 /* estimate the size of message */
938 maxlen = sizeof(*ip6) + sizeof(*nd_na);
939 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
940 if (max_linkhdr + maxlen >= MCLBYTES) {
942 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
943 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
948 MGETHDR(m, M_DONTWAIT, MT_DATA);
949 if (m && max_linkhdr + maxlen >= MHLEN) {
950 MCLGET(m, M_DONTWAIT);
951 if ((m->m_flags & M_EXT) == 0) {
958 m->m_pkthdr.rcvif = NULL;
960 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
961 m->m_flags |= M_MCAST;
962 im6o.im6o_multicast_ifp = ifp;
963 im6o.im6o_multicast_hlim = 255;
964 im6o.im6o_multicast_loop = 0;
967 icmp6len = sizeof(*nd_na);
968 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
969 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
971 /* fill neighbor advertisement packet */
972 ip6 = mtod(m, struct ip6_hdr *);
974 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
975 ip6->ip6_vfc |= IPV6_VERSION;
976 ip6->ip6_nxt = IPPROTO_ICMPV6;
978 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
980 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
981 daddr6.s6_addr16[1] = 0;
982 daddr6.s6_addr32[1] = 0;
983 daddr6.s6_addr32[2] = 0;
984 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
985 if (in6_setscope(&daddr6, ifp, NULL))
988 flags &= ~ND_NA_FLAG_SOLICITED;
990 ip6->ip6_dst = daddr6;
991 bzero(&dst_sa, sizeof(struct sockaddr_in6));
992 dst_sa.sin6_family = AF_INET6;
993 dst_sa.sin6_len = sizeof(struct sockaddr_in6);
994 dst_sa.sin6_addr = daddr6;
997 * Select a source whose scope is the same as that of the dest.
999 bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
1000 error = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &src);
1002 char ip6buf[INET6_ADDRSTRLEN];
1003 nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
1004 "determined: dst=%s, error=%d\n",
1005 ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
1009 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
1010 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
1011 nd_na->nd_na_code = 0;
1012 nd_na->nd_na_target = *taddr6;
1013 in6_clearscope(&nd_na->nd_na_target); /* XXX */
1016 * "tlladdr" indicates NS's condition for adding tlladdr or not.
1017 * see nd6_ns_input() for details.
1018 * Basically, if NS packet is sent to unicast/anycast addr,
1019 * target lladdr option SHOULD NOT be included.
1023 * sdl0 != NULL indicates proxy NA. If we do proxy, use
1024 * lladdr in sdl0. If we are not proxying (sending NA for
1025 * my address) use lladdr configured for the interface.
1029 mac = (*carp_macmatch6_p)(ifp, m, taddr6);
1031 mac = nd6_ifptomac(ifp);
1032 } else if (sdl0->sa_family == AF_LINK) {
1033 struct sockaddr_dl *sdl;
1034 sdl = (struct sockaddr_dl *)sdl0;
1035 if (sdl->sdl_alen == ifp->if_addrlen)
1039 if (tlladdr && mac) {
1040 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1041 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1043 /* roundup to 8 bytes alignment! */
1044 optlen = (optlen + 7) & ~7;
1046 m->m_pkthdr.len += optlen;
1049 bzero((caddr_t)nd_opt, optlen);
1050 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1051 nd_opt->nd_opt_len = optlen >> 3;
1052 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1054 flags &= ~ND_NA_FLAG_OVERRIDE;
1056 ip6->ip6_plen = htons((u_short)icmp6len);
1057 nd_na->nd_na_flags_reserved = flags;
1058 nd_na->nd_na_cksum = 0;
1059 nd_na->nd_na_cksum =
1060 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1062 ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1063 icmp6_ifstat_inc(ifp, ifs6_out_msg);
1064 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1065 ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_ADVERT]);
1067 if (ro.ro_rt) { /* we don't cache this route. */
1081 nd6_ifptomac(struct ifnet *ifp)
1083 switch (ifp->if_type) {
1091 #ifdef IFT_IEEE80211
1099 return IF_LLADDR(ifp);
1106 TAILQ_ENTRY(dadq) dad_list;
1107 struct ifaddr *dad_ifa;
1108 int dad_count; /* max NS to send */
1109 int dad_ns_tcount; /* # of trials to send NS */
1110 int dad_ns_ocount; /* NS sent so far */
1113 struct callout dad_timer_ch;
1114 struct vnet *dad_vnet;
1117 static VNET_DEFINE(TAILQ_HEAD(, dadq), dadq);
1118 VNET_DEFINE(int, dad_init) = 0;
1119 #define V_dadq VNET(dadq)
1120 #define V_dad_init VNET(dad_init)
1122 static struct dadq *
1123 nd6_dad_find(struct ifaddr *ifa)
1127 for (dp = V_dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
1128 if (dp->dad_ifa == ifa)
1135 nd6_dad_starttimer(struct dadq *dp, int ticks)
1138 callout_reset(&dp->dad_timer_ch, ticks,
1139 (void (*)(void *))nd6_dad_timer, (void *)dp);
1143 nd6_dad_stoptimer(struct dadq *dp)
1146 callout_stop(&dp->dad_timer_ch);
1150 * Start Duplicate Address Detection (DAD) for specified interface address.
1153 nd6_dad_start(struct ifaddr *ifa, int delay)
1155 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1157 char ip6buf[INET6_ADDRSTRLEN];
1160 TAILQ_INIT(&V_dadq);
1165 * If we don't need DAD, don't do it.
1166 * There are several cases:
1167 * - DAD is disabled (ip6_dad_count == 0)
1168 * - the interface address is anycast
1170 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1172 "nd6_dad_start: called with non-tentative address "
1174 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1175 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1178 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1179 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1182 if (!V_ip6_dad_count) {
1183 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1186 if (ifa->ifa_ifp == NULL)
1187 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1188 if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
1191 if (nd6_dad_find(ifa) != NULL) {
1192 /* DAD already in progress */
1196 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1198 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1200 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1201 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1204 bzero(dp, sizeof(*dp));
1205 callout_init(&dp->dad_timer_ch, 0);
1207 dp->dad_vnet = curvnet;
1209 TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list);
1211 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1212 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1215 * Send NS packet for DAD, ip6_dad_count times.
1216 * Note that we must delay the first transmission, if this is the
1217 * first packet to be sent from the interface after interface
1218 * (re)initialization.
1221 ifa_ref(ifa); /* just for safety */
1222 dp->dad_count = V_ip6_dad_count;
1223 dp->dad_ns_icount = dp->dad_na_icount = 0;
1224 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1226 nd6_dad_ns_output(dp, ifa);
1227 nd6_dad_starttimer(dp,
1228 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1230 nd6_dad_starttimer(dp, delay);
1235 * terminate DAD unconditionally. used for address removals.
1238 nd6_dad_stop(struct ifaddr *ifa)
1244 dp = nd6_dad_find(ifa);
1246 /* DAD wasn't started yet */
1250 nd6_dad_stoptimer(dp);
1252 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1259 nd6_dad_timer(struct dadq *dp)
1261 CURVNET_SET(dp->dad_vnet);
1263 struct ifaddr *ifa = dp->dad_ifa;
1264 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1265 char ip6buf[INET6_ADDRSTRLEN];
1267 s = splnet(); /* XXX */
1271 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1274 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1275 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1277 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1278 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1281 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1282 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1284 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1285 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1289 /* timeouted with IFF_{RUNNING,UP} check */
1290 if (dp->dad_ns_tcount > V_dad_maxtry) {
1291 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1292 if_name(ifa->ifa_ifp)));
1294 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1301 /* Need more checks? */
1302 if (dp->dad_ns_ocount < dp->dad_count) {
1304 * We have more NS to go. Send NS packet for DAD.
1306 nd6_dad_ns_output(dp, ifa);
1307 nd6_dad_starttimer(dp,
1308 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1311 * We have transmitted sufficient number of DAD packets.
1312 * See what we've got.
1318 if (dp->dad_na_icount) {
1320 * the check is in nd6_dad_na_input(),
1326 if (dp->dad_ns_icount) {
1327 /* We've seen NS, means DAD has failed. */
1332 /* (*dp) will be freed in nd6_dad_duplicated() */
1334 nd6_dad_duplicated(ifa);
1337 * We are done with DAD. No NA came, no NS came.
1338 * No duplicate address found.
1340 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1343 "%s: DAD complete for %s - no duplicates found\n",
1344 if_name(ifa->ifa_ifp),
1345 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1347 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1360 nd6_dad_duplicated(struct ifaddr *ifa)
1362 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1365 char ip6buf[INET6_ADDRSTRLEN];
1367 dp = nd6_dad_find(ifa);
1369 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1373 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1374 "NS in/out=%d/%d, NA in=%d\n",
1375 if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1376 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1378 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1379 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1381 /* We are done with DAD, with duplicate address found. (failure) */
1382 nd6_dad_stoptimer(dp);
1385 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1386 if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
1387 log(LOG_ERR, "%s: manual intervention required\n",
1391 * If the address is a link-local address formed from an interface
1392 * identifier based on the hardware address which is supposed to be
1393 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1394 * operation on the interface SHOULD be disabled.
1395 * [rfc2462bis-03 Section 5.4.5]
1397 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1398 struct in6_addr in6;
1401 * To avoid over-reaction, we only apply this logic when we are
1402 * very sure that hardware addresses are supposed to be unique.
1404 switch (ifp->if_type) {
1409 #ifdef IFT_IEEE80211
1412 in6 = ia->ia_addr.sin6_addr;
1413 if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1414 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1415 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1416 log(LOG_ERR, "%s: possible hardware address "
1417 "duplication detected, disable IPv6\n",
1424 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1431 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1433 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1434 struct ifnet *ifp = ifa->ifa_ifp;
1436 dp->dad_ns_tcount++;
1437 if ((ifp->if_flags & IFF_UP) == 0) {
1440 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1444 dp->dad_ns_ocount++;
1445 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1449 nd6_dad_ns_input(struct ifaddr *ifa)
1451 struct in6_ifaddr *ia;
1453 const struct in6_addr *taddr6;
1458 panic("ifa == NULL in nd6_dad_ns_input");
1460 ia = (struct in6_ifaddr *)ifa;
1462 taddr6 = &ia->ia_addr.sin6_addr;
1464 dp = nd6_dad_find(ifa);
1466 /* Quickhack - completely ignore DAD NS packets */
1467 if (V_dad_ignore_ns) {
1468 char ip6buf[INET6_ADDRSTRLEN];
1470 "nd6_dad_ns_input: ignoring DAD NS packet for "
1471 "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
1472 if_name(ifa->ifa_ifp)));
1477 * if I'm yet to start DAD, someone else started using this address
1478 * first. I have a duplicate and you win.
1480 if (dp == NULL || dp->dad_ns_ocount == 0)
1483 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1486 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1487 nd6_dad_duplicated(ifa);
1490 * not sure if I got a duplicate.
1491 * increment ns count and see what happens.
1494 dp->dad_ns_icount++;
1499 nd6_dad_na_input(struct ifaddr *ifa)
1504 panic("ifa == NULL in nd6_dad_na_input");
1506 dp = nd6_dad_find(ifa);
1508 dp->dad_na_icount++;
1510 /* remove the address. */
1511 nd6_dad_duplicated(ifa);