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;
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);
252 * Make a copy while we can be sure that rt_gateway
253 * is still stable before unlocking to avoid lock
254 * order problems. proxydl will only be used if
255 * proxy will be set in the next block.
258 proxydl = *SDL(rt->rt_gateway);
263 * proxy NDP for single entry
265 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
266 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
273 * We've got an NS packet, and we don't have that adddress
274 * assigned for us. We MUST silently ignore it.
279 myaddr6 = *IFA_IN6(ifa);
280 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
281 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
282 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
285 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
286 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
287 "(if %d, NS packet %d)\n",
288 ip6_sprintf(ip6bufs, &taddr6),
289 ifp->if_addrlen, lladdrlen - 2));
293 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
294 nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
295 ip6_sprintf(ip6bufs, &saddr6)));
300 * We have neighbor solicitation packet, with target address equals to
301 * one of my tentative address.
303 * src addr how to process?
305 * multicast of course, invalid (rejected in ip6_input)
306 * unicast somebody is doing address resolution -> ignore
307 * unspec dup address detection
309 * The processing is defined in RFC 2462.
313 * If source address is unspecified address, it is for
314 * duplicate address detection.
316 * If not, the packet is for addess resolution;
317 * silently ignore it.
319 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
320 nd6_dad_ns_input(ifa);
326 * If the source address is unspecified address, entries must not
327 * be created or updated.
328 * It looks that sender is performing DAD. Output NA toward
329 * all-node multicast address, to tell the sender that I'm using
331 * S bit ("solicited") must be zero.
333 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
334 struct in6_addr in6_all;
336 in6_all = in6addr_linklocal_allnodes;
337 if (in6_setscope(&in6_all, ifp, NULL) != 0)
339 nd6_na_output(ifp, &in6_all, &taddr6,
340 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
341 (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
342 tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL);
346 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
347 ND_NEIGHBOR_SOLICIT, 0);
349 nd6_na_output(ifp, &saddr6, &taddr6,
350 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
351 (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
352 tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL);
360 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
361 ip6_sprintf(ip6bufs, &saddr6)));
362 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
363 ip6_sprintf(ip6bufs, &daddr6)));
364 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
365 ip6_sprintf(ip6bufs, &taddr6)));
366 ICMP6STAT_INC(icp6s_badns);
373 * Output a Neighbor Solicitation Message. Caller specifies:
374 * - ICMP6 header source IP6 address
375 * - ND6 header target IP6 address
376 * - ND6 header source datalink address
379 * Based on RFC 2462 (duplicate address detection)
381 * ln - for source address determination
382 * dad - duplicate address detection
385 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
386 const struct in6_addr *taddr6, struct llentry *ln, int dad)
390 struct nd_neighbor_solicit *nd_ns;
391 struct ip6_moptions im6o;
397 if (IN6_IS_ADDR_MULTICAST(taddr6))
400 /* estimate the size of message */
401 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
402 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
403 if (max_linkhdr + maxlen >= MCLBYTES) {
405 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
406 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
411 MGETHDR(m, M_DONTWAIT, MT_DATA);
412 if (m && max_linkhdr + maxlen >= MHLEN) {
413 MCLGET(m, M_DONTWAIT);
414 if ((m->m_flags & M_EXT) == 0) {
421 m->m_pkthdr.rcvif = NULL;
423 bzero(&ro, sizeof(ro));
425 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
426 m->m_flags |= M_MCAST;
427 im6o.im6o_multicast_ifp = ifp;
428 im6o.im6o_multicast_hlim = 255;
429 im6o.im6o_multicast_loop = 0;
432 icmp6len = sizeof(*nd_ns);
433 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
434 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
436 /* fill neighbor solicitation packet */
437 ip6 = mtod(m, struct ip6_hdr *);
439 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
440 ip6->ip6_vfc |= IPV6_VERSION;
441 /* ip6->ip6_plen will be set later */
442 ip6->ip6_nxt = IPPROTO_ICMPV6;
445 ip6->ip6_dst = *daddr6;
447 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
448 ip6->ip6_dst.s6_addr16[1] = 0;
449 ip6->ip6_dst.s6_addr32[1] = 0;
450 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
451 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
452 ip6->ip6_dst.s6_addr8[12] = 0xff;
453 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
461 * "If the source address of the packet prompting the
462 * solicitation is the same as one of the addresses assigned
463 * to the outgoing interface, that address SHOULD be placed
464 * in the IP Source Address of the outgoing solicitation.
465 * Otherwise, any one of the addresses assigned to the
466 * interface should be used."
468 * We use the source address for the prompting packet
470 * - saddr6 is given from the caller (by giving "ln"), and
471 * - saddr6 belongs to the outgoing interface.
472 * Otherwise, we perform the source address selection as usual.
474 struct in6_addr *hsrc;
479 if (ln->la_hold != NULL) {
480 struct ip6_hdr *hip6; /* hold ip6 */
483 * assuming every packet in la_hold has the same IP
486 hip6 = mtod(ln->la_hold, struct ip6_hdr *);
488 if (sizeof(*hip6) < ln->la_hold->m_len) {
489 ip6->ip6_src = hip6->ip6_src;
490 hsrc = &hip6->ip6_src;
495 if (hsrc && (ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
497 /* ip6_src set already. */
501 struct sockaddr_in6 dst_sa;
502 struct in6_addr src_in;
504 bzero(&dst_sa, sizeof(dst_sa));
505 dst_sa.sin6_family = AF_INET6;
506 dst_sa.sin6_len = sizeof(dst_sa);
507 dst_sa.sin6_addr = ip6->ip6_dst;
509 error = in6_selectsrc(&dst_sa, NULL,
510 NULL, &ro, NULL, NULL, &src_in);
512 char ip6buf[INET6_ADDRSTRLEN];
514 "nd6_ns_output: source can't be "
515 "determined: dst=%s, error=%d\n",
516 ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
520 ip6->ip6_src = src_in;
524 * Source address for DAD packet must always be IPv6
525 * unspecified address. (0::0)
526 * We actually don't have to 0-clear the address (we did it
527 * above), but we do so here explicitly to make the intention
530 bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
532 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
533 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
534 nd_ns->nd_ns_code = 0;
535 nd_ns->nd_ns_reserved = 0;
536 nd_ns->nd_ns_target = *taddr6;
537 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
540 * Add source link-layer address option.
542 * spec implementation
544 * DAD packet MUST NOT do not add the option
545 * there's no link layer address:
546 * impossible do not add the option
547 * there's link layer address:
548 * Multicast NS MUST add one add the option
549 * Unicast NS SHOULD add one add the option
551 if (!dad && (mac = nd6_ifptomac(ifp))) {
552 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
553 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
554 /* 8 byte alignments... */
555 optlen = (optlen + 7) & ~7;
557 m->m_pkthdr.len += optlen;
560 bzero((caddr_t)nd_opt, optlen);
561 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
562 nd_opt->nd_opt_len = optlen >> 3;
563 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
566 ip6->ip6_plen = htons((u_short)icmp6len);
567 nd_ns->nd_ns_cksum = 0;
569 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
571 ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
572 icmp6_ifstat_inc(ifp, ifs6_out_msg);
573 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
574 ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_SOLICIT]);
576 if (ro.ro_rt) { /* we don't cache this route. */
590 * Neighbor advertisement input handling.
593 * Based on RFC 2462 (duplicate address detection)
595 * the following items are not implemented yet:
596 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
597 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
600 nd6_na_input(struct mbuf *m, int off, int icmp6len)
602 struct ifnet *ifp = m->m_pkthdr.rcvif;
603 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
604 struct nd_neighbor_advert *nd_na;
605 struct in6_addr daddr6 = ip6->ip6_dst;
606 struct in6_addr taddr6;
615 struct llentry *ln = NULL;
616 union nd_opts ndopts;
617 struct mbuf *chain = NULL;
618 struct sockaddr_in6 sin6;
619 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
621 if (ip6->ip6_hlim != 255) {
623 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
624 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
625 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
629 #ifndef PULLDOWN_TEST
630 IP6_EXTHDR_CHECK(m, off, icmp6len,);
631 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
633 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
635 ICMP6STAT_INC(icp6s_tooshort);
640 flags = nd_na->nd_na_flags_reserved;
641 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
642 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
643 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
645 taddr6 = nd_na->nd_na_target;
646 if (in6_setscope(&taddr6, ifp, NULL))
647 goto bad; /* XXX: impossible */
649 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
651 "nd6_na_input: invalid target address %s\n",
652 ip6_sprintf(ip6bufs, &taddr6)));
655 if (IN6_IS_ADDR_MULTICAST(&daddr6))
658 "nd6_na_input: a solicited adv is multicasted\n"));
662 icmp6len -= sizeof(*nd_na);
663 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
664 if (nd6_options(&ndopts) < 0) {
666 "nd6_na_input: invalid ND option, ignored\n"));
667 /* nd6_options have incremented stats */
671 if (ndopts.nd_opts_tgt_lladdr) {
672 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
673 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
676 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
679 * Target address matches one of my interface address.
681 * If my address is tentative, this means that there's somebody
682 * already using the same address as mine. This indicates DAD failure.
683 * This is defined in RFC 2462.
685 * Otherwise, process as defined in RFC 2461.
688 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
690 nd6_dad_na_input(ifa);
694 /* Just for safety, maybe unnecessary. */
698 "nd6_na_input: duplicate IP6 address %s\n",
699 ip6_sprintf(ip6bufs, &taddr6));
703 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
704 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
705 "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
706 ifp->if_addrlen, lladdrlen - 2));
711 * If no neighbor cache entry is found, NA SHOULD silently be
715 ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp);
716 IF_AFDATA_UNLOCK(ifp);
721 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
723 * If the link-layer has address, and no lladdr option came,
724 * discard the packet.
726 if (ifp->if_addrlen && lladdr == NULL) {
731 * Record link-layer address, and update the state.
733 bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
734 ln->la_flags |= LLE_VALID;
736 ln->ln_state = ND6_LLINFO_REACHABLE;
738 if (!ND6_LLINFO_PERMANENT(ln)) {
739 nd6_llinfo_settimer_locked(ln,
740 (long)ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
743 ln->ln_state = ND6_LLINFO_STALE;
744 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
746 if ((ln->ln_router = is_router) != 0) {
748 * This means a router's state has changed from
749 * non-reachable to probably reachable, and might
750 * affect the status of associated prefixes..
758 * Check if the link-layer address has changed or not.
763 if (ln->la_flags & LLE_VALID) {
764 if (bcmp(lladdr, &ln->ll_addr, ifp->if_addrlen))
773 * This is VERY complex. Look at it with care.
775 * override solicit lladdr llchange action
780 * 0 0 y y (1) REACHABLE->STALE
781 * 0 1 n -- (2c) *->REACHABLE
782 * 0 1 y n (2b) L *->REACHABLE
783 * 0 1 y y (1) REACHABLE->STALE
786 * 1 0 y y (2a) L *->STALE
787 * 1 1 n -- (2a) *->REACHABLE
788 * 1 1 y n (2a) L *->REACHABLE
789 * 1 1 y y (2a) L *->REACHABLE
791 if (!is_override && (lladdr != NULL && llchange)) { /* (1) */
793 * If state is REACHABLE, make it STALE.
794 * no other updates should be done.
796 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
797 ln->ln_state = ND6_LLINFO_STALE;
798 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
801 } else if (is_override /* (2a) */
802 || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
803 || lladdr == NULL) { /* (2c) */
805 * Update link-local address, if any.
807 if (lladdr != NULL) {
808 bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
809 ln->la_flags |= LLE_VALID;
813 * If solicited, make the state REACHABLE.
814 * If not solicited and the link-layer address was
815 * changed, make it STALE.
818 ln->ln_state = ND6_LLINFO_REACHABLE;
820 if (!ND6_LLINFO_PERMANENT(ln)) {
821 nd6_llinfo_settimer_locked(ln,
822 (long)ND_IFINFO(ifp)->reachable * hz);
825 if (lladdr != NULL && llchange) {
826 ln->ln_state = ND6_LLINFO_STALE;
827 nd6_llinfo_settimer_locked(ln,
828 (long)V_nd6_gctimer * hz);
833 if (ln->ln_router && !is_router) {
835 * The peer dropped the router flag.
836 * Remove the sender from the Default Router List and
837 * update the Destination Cache entries.
839 struct nd_defrouter *dr;
840 struct in6_addr *in6;
842 in6 = &L3_ADDR_SIN6(ln)->sin6_addr;
845 * Lock to protect the default router list.
846 * XXX: this might be unnecessary, since this function
847 * is only called under the network software interrupt
848 * context. However, we keep it just for safety.
850 dr = defrouter_lookup(in6, ln->lle_tbl->llt_ifp);
853 else if (!V_ip6_forwarding) {
855 * Even if the neighbor is not in the default
856 * router list, the neighbor may be used
857 * as a next hop for some destinations
858 * (e.g. redirect case). So we must
859 * call rt6_flush explicitly.
861 rt6_flush(&ip6->ip6_src, ifp);
864 ln->ln_router = is_router;
868 * rt->rt_flags &= ~RTF_REJECT;
872 struct mbuf *m_hold, *m_hold_next;
875 * reset the la_hold in advance, to explicitly
876 * prevent a la_hold lookup in nd6_output()
877 * (wouldn't happen, though...)
879 for (m_hold = ln->la_hold, ln->la_hold = NULL;
880 m_hold; m_hold = m_hold_next) {
881 m_hold_next = m_hold->m_nextpkt;
882 m_hold->m_nextpkt = NULL;
884 * we assume ifp is not a loopback here, so just set
885 * the 2nd argument as the 1st one.
887 nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
893 memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
897 nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
900 pfxlist_onlink_check();
909 ICMP6STAT_INC(icp6s_badna);
914 * Neighbor advertisement output handling.
918 * the following items are not implemented yet:
919 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
920 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
922 * tlladdr - 1 if include target link-layer address
923 * sdl0 - sockaddr_dl (= proxy NA) or NULL
926 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
927 const struct in6_addr *taddr6, u_long flags, int tlladdr,
928 struct sockaddr *sdl0)
932 struct nd_neighbor_advert *nd_na;
933 struct ip6_moptions im6o;
934 struct in6_addr src, daddr6;
935 struct sockaddr_in6 dst_sa;
936 int icmp6len, maxlen, error;
940 bzero(&ro, sizeof(ro));
942 daddr6 = *daddr6_0; /* make a local copy for modification */
944 /* estimate the size of message */
945 maxlen = sizeof(*ip6) + sizeof(*nd_na);
946 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
947 if (max_linkhdr + maxlen >= MCLBYTES) {
949 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
950 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
955 MGETHDR(m, M_DONTWAIT, MT_DATA);
956 if (m && max_linkhdr + maxlen >= MHLEN) {
957 MCLGET(m, M_DONTWAIT);
958 if ((m->m_flags & M_EXT) == 0) {
965 m->m_pkthdr.rcvif = NULL;
967 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
968 m->m_flags |= M_MCAST;
969 im6o.im6o_multicast_ifp = ifp;
970 im6o.im6o_multicast_hlim = 255;
971 im6o.im6o_multicast_loop = 0;
974 icmp6len = sizeof(*nd_na);
975 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
976 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
978 /* fill neighbor advertisement packet */
979 ip6 = mtod(m, struct ip6_hdr *);
981 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
982 ip6->ip6_vfc |= IPV6_VERSION;
983 ip6->ip6_nxt = IPPROTO_ICMPV6;
985 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
987 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
988 daddr6.s6_addr16[1] = 0;
989 daddr6.s6_addr32[1] = 0;
990 daddr6.s6_addr32[2] = 0;
991 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
992 if (in6_setscope(&daddr6, ifp, NULL))
995 flags &= ~ND_NA_FLAG_SOLICITED;
997 ip6->ip6_dst = daddr6;
998 bzero(&dst_sa, sizeof(struct sockaddr_in6));
999 dst_sa.sin6_family = AF_INET6;
1000 dst_sa.sin6_len = sizeof(struct sockaddr_in6);
1001 dst_sa.sin6_addr = daddr6;
1004 * Select a source whose scope is the same as that of the dest.
1006 bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
1007 error = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &src);
1009 char ip6buf[INET6_ADDRSTRLEN];
1010 nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
1011 "determined: dst=%s, error=%d\n",
1012 ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
1016 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
1017 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
1018 nd_na->nd_na_code = 0;
1019 nd_na->nd_na_target = *taddr6;
1020 in6_clearscope(&nd_na->nd_na_target); /* XXX */
1023 * "tlladdr" indicates NS's condition for adding tlladdr or not.
1024 * see nd6_ns_input() for details.
1025 * Basically, if NS packet is sent to unicast/anycast addr,
1026 * target lladdr option SHOULD NOT be included.
1030 * sdl0 != NULL indicates proxy NA. If we do proxy, use
1031 * lladdr in sdl0. If we are not proxying (sending NA for
1032 * my address) use lladdr configured for the interface.
1036 mac = (*carp_macmatch6_p)(ifp, m, taddr6);
1038 mac = nd6_ifptomac(ifp);
1039 } else if (sdl0->sa_family == AF_LINK) {
1040 struct sockaddr_dl *sdl;
1041 sdl = (struct sockaddr_dl *)sdl0;
1042 if (sdl->sdl_alen == ifp->if_addrlen)
1046 if (tlladdr && mac) {
1047 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1048 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1050 /* roundup to 8 bytes alignment! */
1051 optlen = (optlen + 7) & ~7;
1053 m->m_pkthdr.len += optlen;
1056 bzero((caddr_t)nd_opt, optlen);
1057 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1058 nd_opt->nd_opt_len = optlen >> 3;
1059 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1061 flags &= ~ND_NA_FLAG_OVERRIDE;
1063 ip6->ip6_plen = htons((u_short)icmp6len);
1064 nd_na->nd_na_flags_reserved = flags;
1065 nd_na->nd_na_cksum = 0;
1066 nd_na->nd_na_cksum =
1067 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1069 ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1070 icmp6_ifstat_inc(ifp, ifs6_out_msg);
1071 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1072 ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_ADVERT]);
1074 if (ro.ro_rt) { /* we don't cache this route. */
1088 nd6_ifptomac(struct ifnet *ifp)
1090 switch (ifp->if_type) {
1098 #ifdef IFT_IEEE80211
1106 return IF_LLADDR(ifp);
1113 TAILQ_ENTRY(dadq) dad_list;
1114 struct ifaddr *dad_ifa;
1115 int dad_count; /* max NS to send */
1116 int dad_ns_tcount; /* # of trials to send NS */
1117 int dad_ns_ocount; /* NS sent so far */
1120 struct callout dad_timer_ch;
1121 struct vnet *dad_vnet;
1124 static VNET_DEFINE(TAILQ_HEAD(, dadq), dadq);
1125 VNET_DEFINE(int, dad_init) = 0;
1126 #define V_dadq VNET(dadq)
1127 #define V_dad_init VNET(dad_init)
1129 static struct dadq *
1130 nd6_dad_find(struct ifaddr *ifa)
1134 for (dp = V_dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
1135 if (dp->dad_ifa == ifa)
1142 nd6_dad_starttimer(struct dadq *dp, int ticks)
1145 callout_reset(&dp->dad_timer_ch, ticks,
1146 (void (*)(void *))nd6_dad_timer, (void *)dp);
1150 nd6_dad_stoptimer(struct dadq *dp)
1153 callout_stop(&dp->dad_timer_ch);
1157 * Start Duplicate Address Detection (DAD) for specified interface address.
1160 nd6_dad_start(struct ifaddr *ifa, int delay)
1162 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1164 char ip6buf[INET6_ADDRSTRLEN];
1167 TAILQ_INIT(&V_dadq);
1172 * If we don't need DAD, don't do it.
1173 * There are several cases:
1174 * - DAD is disabled (ip6_dad_count == 0)
1175 * - the interface address is anycast
1177 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1179 "nd6_dad_start: called with non-tentative address "
1181 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1182 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1185 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1186 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1189 if (!V_ip6_dad_count) {
1190 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1193 if (ifa->ifa_ifp == NULL)
1194 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1195 if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
1198 if (nd6_dad_find(ifa) != NULL) {
1199 /* DAD already in progress */
1203 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1205 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1207 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1208 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1211 bzero(dp, sizeof(*dp));
1212 callout_init(&dp->dad_timer_ch, 0);
1214 dp->dad_vnet = curvnet;
1216 TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list);
1218 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1219 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1222 * Send NS packet for DAD, ip6_dad_count times.
1223 * Note that we must delay the first transmission, if this is the
1224 * first packet to be sent from the interface after interface
1225 * (re)initialization.
1228 ifa_ref(ifa); /* just for safety */
1229 dp->dad_count = V_ip6_dad_count;
1230 dp->dad_ns_icount = dp->dad_na_icount = 0;
1231 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1233 nd6_dad_ns_output(dp, ifa);
1234 nd6_dad_starttimer(dp,
1235 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1237 nd6_dad_starttimer(dp, delay);
1242 * terminate DAD unconditionally. used for address removals.
1245 nd6_dad_stop(struct ifaddr *ifa)
1251 dp = nd6_dad_find(ifa);
1253 /* DAD wasn't started yet */
1257 nd6_dad_stoptimer(dp);
1259 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1266 nd6_dad_timer(struct dadq *dp)
1268 CURVNET_SET(dp->dad_vnet);
1270 struct ifaddr *ifa = dp->dad_ifa;
1271 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1272 char ip6buf[INET6_ADDRSTRLEN];
1274 s = splnet(); /* XXX */
1278 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1281 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1282 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1284 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1285 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1288 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1289 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1291 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1292 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1296 /* timeouted with IFF_{RUNNING,UP} check */
1297 if (dp->dad_ns_tcount > V_dad_maxtry) {
1298 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1299 if_name(ifa->ifa_ifp)));
1301 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1308 /* Need more checks? */
1309 if (dp->dad_ns_ocount < dp->dad_count) {
1311 * We have more NS to go. Send NS packet for DAD.
1313 nd6_dad_ns_output(dp, ifa);
1314 nd6_dad_starttimer(dp,
1315 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1318 * We have transmitted sufficient number of DAD packets.
1319 * See what we've got.
1325 if (dp->dad_na_icount) {
1327 * the check is in nd6_dad_na_input(),
1333 if (dp->dad_ns_icount) {
1334 /* We've seen NS, means DAD has failed. */
1339 /* (*dp) will be freed in nd6_dad_duplicated() */
1341 nd6_dad_duplicated(ifa);
1344 * We are done with DAD. No NA came, no NS came.
1345 * No duplicate address found.
1347 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1350 "%s: DAD complete for %s - no duplicates found\n",
1351 if_name(ifa->ifa_ifp),
1352 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1354 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1367 nd6_dad_duplicated(struct ifaddr *ifa)
1369 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1372 char ip6buf[INET6_ADDRSTRLEN];
1374 dp = nd6_dad_find(ifa);
1376 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1380 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1381 "NS in/out=%d/%d, NA in=%d\n",
1382 if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1383 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1385 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1386 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1388 /* We are done with DAD, with duplicate address found. (failure) */
1389 nd6_dad_stoptimer(dp);
1392 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1393 if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
1394 log(LOG_ERR, "%s: manual intervention required\n",
1398 * If the address is a link-local address formed from an interface
1399 * identifier based on the hardware address which is supposed to be
1400 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1401 * operation on the interface SHOULD be disabled.
1402 * [rfc2462bis-03 Section 5.4.5]
1404 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1405 struct in6_addr in6;
1408 * To avoid over-reaction, we only apply this logic when we are
1409 * very sure that hardware addresses are supposed to be unique.
1411 switch (ifp->if_type) {
1416 #ifdef IFT_IEEE80211
1419 in6 = ia->ia_addr.sin6_addr;
1420 if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1421 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1422 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1423 log(LOG_ERR, "%s: possible hardware address "
1424 "duplication detected, disable IPv6\n",
1431 TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1438 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1440 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1441 struct ifnet *ifp = ifa->ifa_ifp;
1443 dp->dad_ns_tcount++;
1444 if ((ifp->if_flags & IFF_UP) == 0) {
1447 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1451 dp->dad_ns_ocount++;
1452 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1456 nd6_dad_ns_input(struct ifaddr *ifa)
1458 struct in6_ifaddr *ia;
1460 const struct in6_addr *taddr6;
1465 panic("ifa == NULL in nd6_dad_ns_input");
1467 ia = (struct in6_ifaddr *)ifa;
1469 taddr6 = &ia->ia_addr.sin6_addr;
1471 dp = nd6_dad_find(ifa);
1473 /* Quickhack - completely ignore DAD NS packets */
1474 if (V_dad_ignore_ns) {
1475 char ip6buf[INET6_ADDRSTRLEN];
1477 "nd6_dad_ns_input: ignoring DAD NS packet for "
1478 "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
1479 if_name(ifa->ifa_ifp)));
1484 * if I'm yet to start DAD, someone else started using this address
1485 * first. I have a duplicate and you win.
1487 if (dp == NULL || dp->dad_ns_ocount == 0)
1490 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1493 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1494 nd6_dad_duplicated(ifa);
1497 * not sure if I got a duplicate.
1498 * increment ns count and see what happens.
1501 dp->dad_ns_icount++;
1506 nd6_dad_na_input(struct ifaddr *ifa)
1511 panic("ifa == NULL in nd6_dad_na_input");
1513 dp = nd6_dad_find(ifa);
1515 dp->dad_na_icount++;
1517 /* remove the address. */
1518 nd6_dad_duplicated(ifa);