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/rwlock.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
52 #include <sys/syslog.h>
53 #include <sys/queue.h>
54 #include <sys/callout.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/if_var.h>
60 #include <net/route.h>
62 #include <net/radix_mpath.h>
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <net/if_llatbl.h>
68 #define L3_ADDR_SIN6(le) ((struct sockaddr_in6 *) L3_ADDR(le))
69 #include <netinet6/in6_var.h>
70 #include <netinet6/in6_ifattach.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet6/scope6_var.h>
74 #include <netinet6/nd6.h>
75 #include <netinet/icmp6.h>
78 #include <netinet/ip_carp.h>
81 #define SDL(s) ((struct sockaddr_dl *)s)
84 static struct dadq *nd6_dad_find(struct ifaddr *);
85 static void nd6_dad_starttimer(struct dadq *, int);
86 static void nd6_dad_stoptimer(struct dadq *);
87 static void nd6_dad_timer(struct dadq *);
88 static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
89 static void nd6_dad_ns_input(struct ifaddr *);
90 static void nd6_dad_na_input(struct ifaddr *);
92 VNET_DEFINE(int, dad_ignore_ns);
93 VNET_DEFINE(int, dad_maxtry);
95 #define V_dad_ignore_ns VNET(dad_ignore_ns)
96 #define V_dad_maxtry VNET(dad_maxtry)
99 * Input a Neighbor Solicitation Message.
102 * Based on RFC 2462 (duplicate address detection)
105 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
107 struct ifnet *ifp = m->m_pkthdr.rcvif;
108 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
109 struct nd_neighbor_solicit *nd_ns;
110 struct in6_addr saddr6 = ip6->ip6_src;
111 struct in6_addr daddr6 = ip6->ip6_dst;
112 struct in6_addr taddr6;
113 struct in6_addr myaddr6;
115 struct ifaddr *ifa = NULL;
117 int anycast = 0, proxy = 0, tentative = 0;
119 union nd_opts ndopts;
120 struct sockaddr_dl *proxydl = NULL;
121 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
123 #ifndef PULLDOWN_TEST
124 IP6_EXTHDR_CHECK(m, off, icmp6len,);
125 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
127 IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
129 ICMP6STAT_INC(icp6s_tooshort);
133 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
134 taddr6 = nd_ns->nd_ns_target;
135 if (in6_setscope(&taddr6, ifp, NULL) != 0)
138 if (ip6->ip6_hlim != 255) {
140 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
141 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
142 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
146 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
147 /* dst has to be a solicited node multicast address. */
148 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
149 /* don't check ifindex portion */
150 daddr6.s6_addr32[1] == 0 &&
151 daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
152 daddr6.s6_addr8[12] == 0xff) {
155 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
156 "(wrong ip6 dst)\n"));
159 } else if (!V_nd6_onlink_ns_rfc4861) {
160 struct sockaddr_in6 src_sa6;
163 * According to recent IETF discussions, it is not a good idea
164 * to accept a NS from an address which would not be deemed
165 * to be a neighbor otherwise. This point is expected to be
166 * clarified in future revisions of the specification.
168 bzero(&src_sa6, sizeof(src_sa6));
169 src_sa6.sin6_family = AF_INET6;
170 src_sa6.sin6_len = sizeof(src_sa6);
171 src_sa6.sin6_addr = saddr6;
172 if (nd6_is_addr_neighbor(&src_sa6, ifp) == 0) {
173 nd6log((LOG_INFO, "nd6_ns_input: "
174 "NS packet from non-neighbor\n"));
179 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
180 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
184 icmp6len -= sizeof(*nd_ns);
185 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
186 if (nd6_options(&ndopts) < 0) {
188 "nd6_ns_input: invalid ND option, ignored\n"));
189 /* nd6_options have incremented stats */
193 if (ndopts.nd_opts_src_lladdr) {
194 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
195 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
198 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
199 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
200 "(link-layer address option)\n"));
205 * Attaching target link-layer address to the NA?
208 * NS IP dst is unicast/anycast MUST NOT add
209 * NS IP dst is solicited-node multicast MUST add
211 * In implementation, we add target link-layer address by default.
212 * We do not add one in MUST NOT cases.
214 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
220 * Target address (taddr6) must be either:
221 * (1) Valid unicast/anycast address for my receiving interface,
222 * (2) Unicast address for which I'm offering proxy service, or
223 * (3) "tentative" address on which DAD is being performed.
225 /* (1) and (3) check. */
228 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
230 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
232 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
238 struct sockaddr_in6 tsin6;
244 bzero(&tsin6, sizeof tsin6);
245 tsin6.sin6_len = sizeof(struct sockaddr_in6);
246 tsin6.sin6_family = AF_INET6;
247 tsin6.sin6_addr = taddr6;
250 bzero(&ro, sizeof(ro));
252 rtalloc_mpath((struct route *)&ro, RTF_ANNOUNCE);
255 rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
257 need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
258 rt->rt_gateway->sa_family == AF_LINK);
263 * proxy NDP for single entry
265 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
266 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
269 proxydl = SDL(rt->rt_gateway);
275 * We've got an NS packet, and we don't have that adddress
276 * assigned for us. We MUST silently ignore it.
281 myaddr6 = *IFA_IN6(ifa);
282 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
283 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
284 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
287 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
288 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
289 "(if %d, NS packet %d)\n",
290 ip6_sprintf(ip6bufs, &taddr6),
291 ifp->if_addrlen, lladdrlen - 2));
295 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
296 nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
297 ip6_sprintf(ip6bufs, &saddr6)));
302 * We have neighbor solicitation packet, with target address equals to
303 * one of my tentative address.
305 * src addr how to process?
307 * multicast of course, invalid (rejected in ip6_input)
308 * unicast somebody is doing address resolution -> ignore
309 * unspec dup address detection
311 * The processing is defined in RFC 2462.
315 * If source address is unspecified address, it is for
316 * duplicate address detection.
318 * If not, the packet is for addess resolution;
319 * silently ignore it.
321 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
322 nd6_dad_ns_input(ifa);
328 * If the source address is unspecified address, entries must not
329 * be created or updated.
330 * It looks that sender is performing DAD. Output NA toward
331 * all-node multicast address, to tell the sender that I'm using
333 * S bit ("solicited") must be zero.
335 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
336 struct in6_addr in6_all;
338 in6_all = in6addr_linklocal_allnodes;
339 if (in6_setscope(&in6_all, ifp, NULL) != 0)
341 nd6_na_output(ifp, &in6_all, &taddr6,
342 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
343 (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
344 tlladdr, (struct sockaddr *)proxydl);
348 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
349 ND_NEIGHBOR_SOLICIT, 0);
351 nd6_na_output(ifp, &saddr6, &taddr6,
352 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
353 (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
354 tlladdr, (struct sockaddr *)proxydl);
362 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
363 ip6_sprintf(ip6bufs, &saddr6)));
364 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
365 ip6_sprintf(ip6bufs, &daddr6)));
366 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
367 ip6_sprintf(ip6bufs, &taddr6)));
368 ICMP6STAT_INC(icp6s_badns);
375 * Output a Neighbor Solicitation Message. Caller specifies:
376 * - ICMP6 header source IP6 address
377 * - ND6 header target IP6 address
378 * - ND6 header source datalink address
381 * Based on RFC 2462 (duplicate address detection)
383 * ln - for source address determination
384 * dad - duplicate address detection
387 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
388 const struct in6_addr *taddr6, struct llentry *ln, int dad)
392 struct nd_neighbor_solicit *nd_ns;
393 struct in6_addr *src, src_in;
394 struct ip6_moptions im6o;
400 bzero(&ro, sizeof(ro));
402 if (IN6_IS_ADDR_MULTICAST(taddr6))
405 /* estimate the size of message */
406 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
407 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
408 if (max_linkhdr + maxlen >= MCLBYTES) {
410 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
411 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
416 MGETHDR(m, M_DONTWAIT, MT_DATA);
417 if (m && max_linkhdr + maxlen >= MHLEN) {
418 MCLGET(m, M_DONTWAIT);
419 if ((m->m_flags & M_EXT) == 0) {
426 m->m_pkthdr.rcvif = NULL;
428 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
429 m->m_flags |= M_MCAST;
430 im6o.im6o_multicast_ifp = ifp;
431 im6o.im6o_multicast_hlim = 255;
432 im6o.im6o_multicast_loop = 0;
435 icmp6len = sizeof(*nd_ns);
436 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
437 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
439 /* fill neighbor solicitation packet */
440 ip6 = mtod(m, struct ip6_hdr *);
442 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
443 ip6->ip6_vfc |= IPV6_VERSION;
444 /* ip6->ip6_plen will be set later */
445 ip6->ip6_nxt = IPPROTO_ICMPV6;
448 ip6->ip6_dst = *daddr6;
450 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
451 ip6->ip6_dst.s6_addr16[1] = 0;
452 ip6->ip6_dst.s6_addr32[1] = 0;
453 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
454 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
455 ip6->ip6_dst.s6_addr8[12] = 0xff;
456 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
464 * "If the source address of the packet prompting the
465 * solicitation is the same as one of the addresses assigned
466 * to the outgoing interface, that address SHOULD be placed
467 * in the IP Source Address of the outgoing solicitation.
468 * Otherwise, any one of the addresses assigned to the
469 * interface should be used."
471 * We use the source address for the prompting packet
473 * - saddr6 is given from the caller (by giving "ln"), and
474 * - saddr6 belongs to the outgoing interface.
475 * Otherwise, we perform the source address selection as usual.
477 struct ip6_hdr *hip6; /* hold ip6 */
478 struct in6_addr *hsrc = NULL;
480 if ((ln != NULL) && ln->la_hold) {
482 * assuming every packet in la_hold has the same IP
485 hip6 = mtod(ln->la_hold, struct ip6_hdr *);
487 if (sizeof(*hip6) < ln->la_hold->m_len)
488 hsrc = &hip6->ip6_src;
492 if (hsrc && (ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
498 struct sockaddr_in6 dst_sa;
500 bzero(&dst_sa, sizeof(dst_sa));
501 dst_sa.sin6_family = AF_INET6;
502 dst_sa.sin6_len = sizeof(dst_sa);
503 dst_sa.sin6_addr = ip6->ip6_dst;
505 error = in6_selectsrc(&dst_sa, NULL,
506 NULL, &ro, NULL, NULL, &src_in);
508 char ip6buf[INET6_ADDRSTRLEN];
510 "nd6_ns_output: source can't be "
511 "determined: dst=%s, error=%d\n",
512 ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
520 * Source address for DAD packet must always be IPv6
521 * unspecified address. (0::0)
522 * We actually don't have to 0-clear the address (we did it
523 * above), but we do so here explicitly to make the intention
526 bzero(&src_in, sizeof(src_in));
530 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
531 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
532 nd_ns->nd_ns_code = 0;
533 nd_ns->nd_ns_reserved = 0;
534 nd_ns->nd_ns_target = *taddr6;
535 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
538 * Add source link-layer address option.
540 * spec implementation
542 * DAD packet MUST NOT do not add the option
543 * there's no link layer address:
544 * impossible do not add the option
545 * there's link layer address:
546 * Multicast NS MUST add one add the option
547 * Unicast NS SHOULD add one add the option
549 if (!dad && (mac = nd6_ifptomac(ifp))) {
550 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
551 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
552 /* 8 byte alignments... */
553 optlen = (optlen + 7) & ~7;
555 m->m_pkthdr.len += optlen;
558 bzero((caddr_t)nd_opt, optlen);
559 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
560 nd_opt->nd_opt_len = optlen >> 3;
561 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
564 ip6->ip6_plen = htons((u_short)icmp6len);
565 nd_ns->nd_ns_cksum = 0;
567 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
569 ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
570 icmp6_ifstat_inc(ifp, ifs6_out_msg);
571 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
572 ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_SOLICIT]);
574 if (ro.ro_rt) { /* we don't cache this route. */
588 * Neighbor advertisement input handling.
591 * Based on RFC 2462 (duplicate address detection)
593 * the following items are not implemented yet:
594 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
595 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
598 nd6_na_input(struct mbuf *m, int off, int icmp6len)
600 struct ifnet *ifp = m->m_pkthdr.rcvif;
601 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
602 struct nd_neighbor_advert *nd_na;
603 struct in6_addr daddr6 = ip6->ip6_dst;
604 struct in6_addr taddr6;
613 struct llentry *ln = NULL;
614 union nd_opts ndopts;
615 struct mbuf *chain = NULL;
616 struct sockaddr_in6 sin6;
617 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
619 if (ip6->ip6_hlim != 255) {
621 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
622 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
623 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
627 #ifndef PULLDOWN_TEST
628 IP6_EXTHDR_CHECK(m, off, icmp6len,);
629 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
631 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
633 ICMP6STAT_INC(icp6s_tooshort);
638 flags = nd_na->nd_na_flags_reserved;
639 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
640 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
641 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
643 taddr6 = nd_na->nd_na_target;
644 if (in6_setscope(&taddr6, ifp, NULL))
645 goto bad; /* XXX: impossible */
647 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
649 "nd6_na_input: invalid target address %s\n",
650 ip6_sprintf(ip6bufs, &taddr6)));
653 if (IN6_IS_ADDR_MULTICAST(&daddr6))
656 "nd6_na_input: a solicited adv is multicasted\n"));
660 icmp6len -= sizeof(*nd_na);
661 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
662 if (nd6_options(&ndopts) < 0) {
664 "nd6_na_input: invalid ND option, ignored\n"));
665 /* nd6_options have incremented stats */
669 if (ndopts.nd_opts_tgt_lladdr) {
670 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
671 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
674 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
677 * Target address matches one of my interface address.
679 * If my address is tentative, this means that there's somebody
680 * already using the same address as mine. This indicates DAD failure.
681 * This is defined in RFC 2462.
683 * Otherwise, process as defined in RFC 2461.
686 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
688 nd6_dad_na_input(ifa);
692 /* Just for safety, maybe unnecessary. */
696 "nd6_na_input: duplicate IP6 address %s\n",
697 ip6_sprintf(ip6bufs, &taddr6));
701 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
702 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
703 "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
704 ifp->if_addrlen, lladdrlen - 2));
709 * If no neighbor cache entry is found, NA SHOULD silently be
713 ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp);
714 IF_AFDATA_UNLOCK(ifp);
719 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
721 * If the link-layer has address, and no lladdr option came,
722 * discard the packet.
724 if (ifp->if_addrlen && lladdr == NULL) {
729 * Record link-layer address, and update the state.
731 bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
732 ln->la_flags |= LLE_VALID;
734 ln->ln_state = ND6_LLINFO_REACHABLE;
736 if (!ND6_LLINFO_PERMANENT(ln)) {
737 nd6_llinfo_settimer_locked(ln,
738 (long)ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
741 ln->ln_state = ND6_LLINFO_STALE;
742 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
744 if ((ln->ln_router = is_router) != 0) {
746 * This means a router's state has changed from
747 * non-reachable to probably reachable, and might
748 * affect the status of associated prefixes..
756 * Check if the link-layer address has changed or not.
761 if (ln->la_flags & LLE_VALID) {
762 if (bcmp(lladdr, &ln->ll_addr, ifp->if_addrlen))
771 * This is VERY complex. Look at it with care.
773 * override solicit lladdr llchange action
778 * 0 0 y y (1) REACHABLE->STALE
779 * 0 1 n -- (2c) *->REACHABLE
780 * 0 1 y n (2b) L *->REACHABLE
781 * 0 1 y y (1) REACHABLE->STALE
784 * 1 0 y y (2a) L *->STALE
785 * 1 1 n -- (2a) *->REACHABLE
786 * 1 1 y n (2a) L *->REACHABLE
787 * 1 1 y y (2a) L *->REACHABLE
789 if (!is_override && (lladdr != NULL && llchange)) { /* (1) */
791 * If state is REACHABLE, make it STALE.
792 * no other updates should be done.
794 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
795 ln->ln_state = ND6_LLINFO_STALE;
796 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
799 } else if (is_override /* (2a) */
800 || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
801 || lladdr == NULL) { /* (2c) */
803 * Update link-local address, if any.
805 if (lladdr != NULL) {
806 bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
807 ln->la_flags |= LLE_VALID;
811 * If solicited, make the state REACHABLE.
812 * If not solicited and the link-layer address was
813 * changed, make it STALE.
816 ln->ln_state = ND6_LLINFO_REACHABLE;
818 if (!ND6_LLINFO_PERMANENT(ln)) {
819 nd6_llinfo_settimer_locked(ln,
820 (long)ND_IFINFO(ifp)->reachable * hz);
823 if (lladdr != NULL && llchange) {
824 ln->ln_state = ND6_LLINFO_STALE;
825 nd6_llinfo_settimer_locked(ln,
826 (long)V_nd6_gctimer * hz);
831 if (ln->ln_router && !is_router) {
833 * The peer dropped the router flag.
834 * Remove the sender from the Default Router List and
835 * update the Destination Cache entries.
837 struct nd_defrouter *dr;
838 struct in6_addr *in6;
840 in6 = &L3_ADDR_SIN6(ln)->sin6_addr;
843 * Lock to protect the default router list.
844 * XXX: this might be unnecessary, since this function
845 * is only called under the network software interrupt
846 * context. However, we keep it just for safety.
848 dr = defrouter_lookup(in6, ln->lle_tbl->llt_ifp);
851 else if (!V_ip6_forwarding) {
853 * Even if the neighbor is not in the default
854 * router list, the neighbor may be used
855 * as a next hop for some destinations
856 * (e.g. redirect case). So we must
857 * call rt6_flush explicitly.
859 rt6_flush(&ip6->ip6_src, ifp);
862 ln->ln_router = is_router;
866 * rt->rt_flags &= ~RTF_REJECT;
870 struct mbuf *m_hold, *m_hold_next;
873 * reset the la_hold in advance, to explicitly
874 * prevent a la_hold lookup in nd6_output()
875 * (wouldn't happen, though...)
877 for (m_hold = ln->la_hold, ln->la_hold = NULL;
878 m_hold; m_hold = m_hold_next) {
879 m_hold_next = m_hold->m_nextpkt;
880 m_hold->m_nextpkt = NULL;
882 * we assume ifp is not a loopback here, so just set
883 * the 2nd argument as the 1st one.
885 nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
891 memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
895 nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
898 pfxlist_onlink_check();
907 ICMP6STAT_INC(icp6s_badna);
912 * Neighbor advertisement output handling.
916 * the following items are not implemented yet:
917 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
918 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
920 * tlladdr - 1 if include target link-layer address
921 * sdl0 - sockaddr_dl (= proxy NA) or NULL
924 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
925 const struct in6_addr *taddr6, u_long flags, int tlladdr,
926 struct sockaddr *sdl0)
930 struct nd_neighbor_advert *nd_na;
931 struct ip6_moptions im6o;
932 struct in6_addr src, daddr6;
933 struct sockaddr_in6 dst_sa;
934 int icmp6len, maxlen, error;
938 bzero(&ro, sizeof(ro));
940 daddr6 = *daddr6_0; /* make a local copy for modification */
942 /* estimate the size of message */
943 maxlen = sizeof(*ip6) + sizeof(*nd_na);
944 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
945 if (max_linkhdr + maxlen >= MCLBYTES) {
947 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
948 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
953 MGETHDR(m, M_DONTWAIT, MT_DATA);
954 if (m && max_linkhdr + maxlen >= MHLEN) {
955 MCLGET(m, M_DONTWAIT);
956 if ((m->m_flags & M_EXT) == 0) {
963 m->m_pkthdr.rcvif = NULL;
965 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
966 m->m_flags |= M_MCAST;
967 im6o.im6o_multicast_ifp = ifp;
968 im6o.im6o_multicast_hlim = 255;
969 im6o.im6o_multicast_loop = 0;
972 icmp6len = sizeof(*nd_na);
973 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
974 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
976 /* fill neighbor advertisement packet */
977 ip6 = mtod(m, struct ip6_hdr *);
979 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
980 ip6->ip6_vfc |= IPV6_VERSION;
981 ip6->ip6_nxt = IPPROTO_ICMPV6;
983 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
985 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
986 daddr6.s6_addr16[1] = 0;
987 daddr6.s6_addr32[1] = 0;
988 daddr6.s6_addr32[2] = 0;
989 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
990 if (in6_setscope(&daddr6, ifp, NULL))
993 flags &= ~ND_NA_FLAG_SOLICITED;
995 ip6->ip6_dst = daddr6;
996 bzero(&dst_sa, sizeof(struct sockaddr_in6));
997 dst_sa.sin6_family = AF_INET6;
998 dst_sa.sin6_len = sizeof(struct sockaddr_in6);
999 dst_sa.sin6_addr = daddr6;
1002 * Select a source whose scope is the same as that of the dest.
1004 bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
1005 error = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &src);
1007 char ip6buf[INET6_ADDRSTRLEN];
1008 nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
1009 "determined: dst=%s, error=%d\n",
1010 ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
1014 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
1015 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
1016 nd_na->nd_na_code = 0;
1017 nd_na->nd_na_target = *taddr6;
1018 in6_clearscope(&nd_na->nd_na_target); /* XXX */
1021 * "tlladdr" indicates NS's condition for adding tlladdr or not.
1022 * see nd6_ns_input() for details.
1023 * Basically, if NS packet is sent to unicast/anycast addr,
1024 * target lladdr option SHOULD NOT be included.
1028 * sdl0 != NULL indicates proxy NA. If we do proxy, use
1029 * lladdr in sdl0. If we are not proxying (sending NA for
1030 * my address) use lladdr configured for the interface.
1035 mac = carp_macmatch6(ifp->if_carp, m, taddr6);
1037 mac = nd6_ifptomac(ifp);
1039 mac = nd6_ifptomac(ifp);
1041 } else if (sdl0->sa_family == AF_LINK) {
1042 struct sockaddr_dl *sdl;
1043 sdl = (struct sockaddr_dl *)sdl0;
1044 if (sdl->sdl_alen == ifp->if_addrlen)
1048 if (tlladdr && mac) {
1049 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1050 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1052 /* roundup to 8 bytes alignment! */
1053 optlen = (optlen + 7) & ~7;
1055 m->m_pkthdr.len += optlen;
1058 bzero((caddr_t)nd_opt, optlen);
1059 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1060 nd_opt->nd_opt_len = optlen >> 3;
1061 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1063 flags &= ~ND_NA_FLAG_OVERRIDE;
1065 ip6->ip6_plen = htons((u_short)icmp6len);
1066 nd_na->nd_na_flags_reserved = flags;
1067 nd_na->nd_na_cksum = 0;
1068 nd_na->nd_na_cksum =
1069 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1071 ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1072 icmp6_ifstat_inc(ifp, ifs6_out_msg);
1073 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1074 ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_ADVERT]);
1076 if (ro.ro_rt) { /* we don't cache this route. */
1090 nd6_ifptomac(struct ifnet *ifp)
1092 switch (ifp->if_type) {
1100 #ifdef IFT_IEEE80211
1108 return IF_LLADDR(ifp);
1115 TAILQ_ENTRY(dadq) dad_list;
1116 struct ifaddr *dad_ifa;
1117 int dad_count; /* max NS to send */
1118 int dad_ns_tcount; /* # of trials to send NS */
1119 int dad_ns_ocount; /* NS sent so far */
1122 struct callout dad_timer_ch;
1123 struct vnet *dad_vnet;
1126 static VNET_DEFINE(TAILQ_HEAD(, dadq), dadq);
1127 #define V_dadq VNET(dadq)
1129 VNET_DEFINE(int, dad_init);
1130 #define V_dad_init VNET(dad_init)
1132 static struct dadq *
1133 nd6_dad_find(struct ifaddr *ifa)
1137 for (dp = V_dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
1138 if (dp->dad_ifa == ifa)
1145 nd6_dad_starttimer(struct dadq *dp, int ticks)
1148 callout_reset(&dp->dad_timer_ch, ticks,
1149 (void (*)(void *))nd6_dad_timer, (void *)dp);
1153 nd6_dad_stoptimer(struct dadq *dp)
1156 callout_stop(&dp->dad_timer_ch);
1160 * Start Duplicate Address Detection (DAD) for specified interface address.
1163 nd6_dad_start(struct ifaddr *ifa, int delay)
1165 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1167 char ip6buf[INET6_ADDRSTRLEN];
1170 TAILQ_INIT(&V_dadq);
1175 * If we don't need DAD, don't do it.
1176 * There are several cases:
1177 * - DAD is disabled (ip6_dad_count == 0)
1178 * - the interface address is anycast
1180 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1182 "nd6_dad_start: called with non-tentative address "
1184 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1185 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1188 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1189 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1192 if (!V_ip6_dad_count) {
1193 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1196 if (ifa->ifa_ifp == NULL)
1197 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1198 if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
1201 if (nd6_dad_find(ifa) != NULL) {
1202 /* DAD already in progress */
1206 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1208 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1210 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1211 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1214 bzero(dp, sizeof(*dp));
1215 callout_init(&dp->dad_timer_ch, 0);
1217 dp->dad_vnet = curvnet;
1219 TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list);
1221 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1222 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1225 * Send NS packet for DAD, ip6_dad_count times.
1226 * Note that we must delay the first transmission, if this is the
1227 * first packet to be sent from the interface after interface
1228 * (re)initialization.
1231 ifa_ref(ifa); /* just for safety */
1232 dp->dad_count = V_ip6_dad_count;
1233 dp->dad_ns_icount = dp->dad_na_icount = 0;
1234 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1236 nd6_dad_ns_output(dp, ifa);
1237 nd6_dad_starttimer(dp,
1238 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1240 nd6_dad_starttimer(dp, delay);
1245 * terminate DAD unconditionally. used for address removals.
1248 nd6_dad_stop(struct ifaddr *ifa)
1254 dp = nd6_dad_find(ifa);
1256 /* DAD wasn't started yet */
1260 nd6_dad_stoptimer(dp);
1262 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1269 nd6_dad_timer(struct dadq *dp)
1271 CURVNET_SET(dp->dad_vnet);
1273 struct ifaddr *ifa = dp->dad_ifa;
1274 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1275 char ip6buf[INET6_ADDRSTRLEN];
1277 s = splnet(); /* XXX */
1281 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1284 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1285 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1287 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1288 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1291 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1292 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1294 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1295 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1299 /* timeouted with IFF_{RUNNING,UP} check */
1300 if (dp->dad_ns_tcount > V_dad_maxtry) {
1301 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1302 if_name(ifa->ifa_ifp)));
1304 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1311 /* Need more checks? */
1312 if (dp->dad_ns_ocount < dp->dad_count) {
1314 * We have more NS to go. Send NS packet for DAD.
1316 nd6_dad_ns_output(dp, ifa);
1317 nd6_dad_starttimer(dp,
1318 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1321 * We have transmitted sufficient number of DAD packets.
1322 * See what we've got.
1328 if (dp->dad_na_icount) {
1330 * the check is in nd6_dad_na_input(),
1336 if (dp->dad_ns_icount) {
1337 /* We've seen NS, means DAD has failed. */
1342 /* (*dp) will be freed in nd6_dad_duplicated() */
1344 nd6_dad_duplicated(ifa);
1347 * We are done with DAD. No NA came, no NS came.
1348 * No duplicate address found.
1350 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1353 "%s: DAD complete for %s - no duplicates found\n",
1354 if_name(ifa->ifa_ifp),
1355 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1357 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1370 nd6_dad_duplicated(struct ifaddr *ifa)
1372 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1375 char ip6buf[INET6_ADDRSTRLEN];
1377 dp = nd6_dad_find(ifa);
1379 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1383 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1384 "NS in/out=%d/%d, NA in=%d\n",
1385 if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1386 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1388 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1389 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1391 /* We are done with DAD, with duplicate address found. (failure) */
1392 nd6_dad_stoptimer(dp);
1395 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1396 if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
1397 log(LOG_ERR, "%s: manual intervention required\n",
1401 * If the address is a link-local address formed from an interface
1402 * identifier based on the hardware address which is supposed to be
1403 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1404 * operation on the interface SHOULD be disabled.
1405 * [rfc2462bis-03 Section 5.4.5]
1407 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1408 struct in6_addr in6;
1411 * To avoid over-reaction, we only apply this logic when we are
1412 * very sure that hardware addresses are supposed to be unique.
1414 switch (ifp->if_type) {
1419 #ifdef IFT_IEEE80211
1422 in6 = ia->ia_addr.sin6_addr;
1423 if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1424 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1425 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1426 log(LOG_ERR, "%s: possible hardware address "
1427 "duplication detected, disable IPv6\n",
1434 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1441 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1443 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1444 struct ifnet *ifp = ifa->ifa_ifp;
1446 dp->dad_ns_tcount++;
1447 if ((ifp->if_flags & IFF_UP) == 0) {
1450 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1454 dp->dad_ns_ocount++;
1455 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1459 nd6_dad_ns_input(struct ifaddr *ifa)
1461 struct in6_ifaddr *ia;
1463 const struct in6_addr *taddr6;
1468 panic("ifa == NULL in nd6_dad_ns_input");
1470 ia = (struct in6_ifaddr *)ifa;
1472 taddr6 = &ia->ia_addr.sin6_addr;
1474 dp = nd6_dad_find(ifa);
1476 /* Quickhack - completely ignore DAD NS packets */
1477 if (V_dad_ignore_ns) {
1478 char ip6buf[INET6_ADDRSTRLEN];
1480 "nd6_dad_ns_input: ignoring DAD NS packet for "
1481 "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
1482 if_name(ifa->ifa_ifp)));
1487 * if I'm yet to start DAD, someone else started using this address
1488 * first. I have a duplicate and you win.
1490 if (dp == NULL || dp->dad_ns_ocount == 0)
1493 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1496 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1497 nd6_dad_duplicated(ifa);
1500 * not sure if I got a duplicate.
1501 * increment ns count and see what happens.
1504 dp->dad_ns_icount++;
1509 nd6_dad_na_input(struct ifaddr *ifa)
1514 panic("ifa == NULL in nd6_dad_na_input");
1516 dp = nd6_dad_find(ifa);
1518 dp->dad_na_icount++;
1520 /* remove the address. */
1521 nd6_dad_duplicated(ifa);