2 /* $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "opt_inet6.h"
35 #include "opt_ipsec.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
44 #include <sys/kernel.h>
45 #include <sys/errno.h>
46 #include <sys/syslog.h>
47 #include <sys/queue.h>
48 #include <sys/callout.h>
51 #include <net/if_types.h>
52 #include <net/if_dl.h>
53 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet6/in6_var.h>
58 #include <netinet/ip6.h>
59 #include <netinet6/ip6_var.h>
60 #include <netinet6/nd6.h>
61 #include <netinet/icmp6.h>
64 #include <netinet6/ipsec.h>
66 #include <netinet6/ipsec6.h>
70 #include <net/net_osdep.h>
72 #define SDL(s) ((struct sockaddr_dl *)s)
75 static struct dadq *nd6_dad_find __P((struct ifaddr *));
76 static void nd6_dad_starttimer __P((struct dadq *, int));
77 static void nd6_dad_stoptimer __P((struct dadq *));
78 static void nd6_dad_timer __P((struct ifaddr *));
79 static void nd6_dad_ns_output __P((struct dadq *, struct ifaddr *));
80 static void nd6_dad_ns_input __P((struct ifaddr *));
81 static void nd6_dad_na_input __P((struct ifaddr *));
83 static int dad_ignore_ns = 0; /* ignore NS in DAD - specwise incorrect*/
84 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
87 * Input an Neighbor Solicitation Message.
90 * Based on RFC 2462 (duplicated address detection)
93 nd6_ns_input(m, off, icmp6len)
97 struct ifnet *ifp = m->m_pkthdr.rcvif;
98 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
99 struct nd_neighbor_solicit *nd_ns;
100 struct in6_addr saddr6 = ip6->ip6_src;
101 struct in6_addr daddr6 = ip6->ip6_dst;
102 struct in6_addr taddr6;
103 struct in6_addr myaddr6;
107 int anycast = 0, proxy = 0, tentative = 0;
109 union nd_opts ndopts;
110 struct sockaddr_dl *proxydl = NULL;
112 #ifndef PULLDOWN_TEST
113 IP6_EXTHDR_CHECK(m, off, icmp6len,);
114 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
116 IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
118 icmp6stat.icp6s_tooshort++;
122 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
123 taddr6 = nd_ns->nd_ns_target;
125 if (ip6->ip6_hlim != 255) {
127 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
128 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
129 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
133 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
134 /* dst has to be solicited node multicast address. */
135 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL
136 /* don't check ifindex portion */
137 && daddr6.s6_addr32[1] == 0
138 && daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE
139 && daddr6.s6_addr8[12] == 0xff) {
142 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
143 "(wrong ip6 dst)\n"));
148 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
149 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
153 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
154 taddr6.s6_addr16[1] = htons(ifp->if_index);
156 icmp6len -= sizeof(*nd_ns);
157 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
158 if (nd6_options(&ndopts) < 0) {
160 "nd6_ns_input: invalid ND option, ignored\n"));
161 /* nd6_options have incremented stats */
165 if (ndopts.nd_opts_src_lladdr) {
166 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
167 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
170 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
171 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
172 "(link-layer address option)\n"));
177 * Attaching target link-layer address to the NA?
180 * NS IP dst is unicast/anycast MUST NOT add
181 * NS IP dst is solicited-node multicast MUST add
183 * In implementation, we add target link-layer address by default.
184 * We do not add one in MUST NOT cases.
186 #if 0 /* too much! */
187 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
188 if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
192 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
198 * Target address (taddr6) must be either:
199 * (1) Valid unicast/anycast address for my receiving interface,
200 * (2) Unicast address for which I'm offering proxy service, or
201 * (3) "tentative" address on which DAD is being performed.
203 /* (1) and (3) check. */
204 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
209 struct sockaddr_in6 tsin6;
211 bzero(&tsin6, sizeof tsin6);
212 tsin6.sin6_len = sizeof(struct sockaddr_in6);
213 tsin6.sin6_family = AF_INET6;
214 tsin6.sin6_addr = taddr6;
216 rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
217 if (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
218 rt->rt_gateway->sa_family == AF_LINK) {
220 * proxy NDP for single entry
222 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
223 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
226 proxydl = SDL(rt->rt_gateway);
234 * We've got an NS packet, and we don't have that adddress
235 * assigned for us. We MUST silently ignore it.
240 myaddr6 = *IFA_IN6(ifa);
241 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
242 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
243 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
246 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
248 "nd6_ns_input: lladdrlen mismatch for %s "
249 "(if %d, NS packet %d)\n",
250 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
254 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
256 "nd6_ns_input: duplicate IP6 address %s\n",
257 ip6_sprintf(&saddr6)));
262 * We have neighbor solicitation packet, with target address equals to
263 * one of my tentative address.
265 * src addr how to process?
267 * multicast of course, invalid (rejected in ip6_input)
268 * unicast somebody is doing address resolution -> ignore
269 * unspec dup address detection
271 * The processing is defined in RFC 2462.
275 * If source address is unspecified address, it is for
276 * duplicated address detection.
278 * If not, the packet is for addess resolution;
279 * silently ignore it.
281 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
282 nd6_dad_ns_input(ifa);
288 * If the source address is unspecified address, entries must not
289 * be created or updated.
290 * It looks that sender is performing DAD. Output NA toward
291 * all-node multicast address, to tell the sender that I'm using
293 * S bit ("solicited") must be zero.
295 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
296 saddr6 = in6addr_linklocal_allnodes;
297 saddr6.s6_addr16[1] = htons(ifp->if_index);
298 nd6_na_output(ifp, &saddr6, &taddr6,
299 ((anycast || proxy || !tlladdr)
300 ? 0 : ND_NA_FLAG_OVERRIDE)
301 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
302 tlladdr, (struct sockaddr *)proxydl);
306 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
308 nd6_na_output(ifp, &saddr6, &taddr6,
309 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE)
310 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
311 | ND_NA_FLAG_SOLICITED,
312 tlladdr, (struct sockaddr *)proxydl);
318 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)));
319 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)));
320 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)));
321 icmp6stat.icp6s_badns++;
326 * Output an Neighbor Solicitation Message. Caller specifies:
327 * - ICMP6 header source IP6 address
328 * - ND6 header target IP6 address
329 * - ND6 header source datalink address
332 * Based on RFC 2462 (duplicated address detection)
335 nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
337 const struct in6_addr *daddr6, *taddr6;
338 struct llinfo_nd6 *ln; /* for source address determination */
339 int dad; /* duplicated address detection */
343 struct nd_neighbor_solicit *nd_ns;
344 struct in6_ifaddr *ia = NULL;
345 struct ip6_moptions im6o;
349 struct ifnet *outif = NULL;
351 if (IN6_IS_ADDR_MULTICAST(taddr6))
354 /* estimate the size of message */
355 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
356 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
357 if (max_linkhdr + maxlen >= MCLBYTES) {
359 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
360 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
365 MGETHDR(m, M_DONTWAIT, MT_DATA);
366 if (m && max_linkhdr + maxlen >= MHLEN) {
367 MCLGET(m, M_DONTWAIT);
368 if ((m->m_flags & M_EXT) == 0) {
375 m->m_pkthdr.rcvif = NULL;
377 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
378 m->m_flags |= M_MCAST;
379 im6o.im6o_multicast_ifp = ifp;
380 im6o.im6o_multicast_hlim = 255;
381 im6o.im6o_multicast_loop = 0;
384 icmp6len = sizeof(*nd_ns);
385 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
386 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
388 /* fill neighbor solicitation packet */
389 ip6 = mtod(m, struct ip6_hdr *);
391 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
392 ip6->ip6_vfc |= IPV6_VERSION;
393 /* ip6->ip6_plen will be set later */
394 ip6->ip6_nxt = IPPROTO_ICMPV6;
397 ip6->ip6_dst = *daddr6;
399 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
400 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
401 ip6->ip6_dst.s6_addr32[1] = 0;
402 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
403 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
404 ip6->ip6_dst.s6_addr8[12] = 0xff;
407 #if 0 /* KAME way, exact address scope match */
409 * Select a source whose scope is the same as that of the dest.
410 * Typically, the dest is link-local solicitation multicast
411 * (i.e. neighbor discovery) or link-local/global unicast
412 * (i.e. neighbor un-reachability detection).
414 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
419 ip6->ip6_src = ia->ia_addr.sin6_addr;
420 #else /* spec-wise correct */
423 * "If the source address of the packet prompting the
424 * solicitation is the same as one of the addresses assigned
425 * to the outgoing interface, that address SHOULD be placed
426 * in the IP Source Address of the outgoing solicitation.
427 * Otherwise, any one of the addresses assigned to the
428 * interface should be used."
430 * We use the source address for the prompting packet
432 * - saddr6 is given from the caller (by giving "ln"), and
433 * - saddr6 belongs to the outgoing interface.
434 * Otherwise, we perform a scope-wise match.
436 struct ip6_hdr *hip6; /* hold ip6 */
437 struct in6_addr *saddr6;
439 if (ln && ln->ln_hold) {
440 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
442 if (sizeof(*hip6) < ln->ln_hold->m_len)
443 saddr6 = &hip6->ip6_src;
448 if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
449 bcopy(saddr6, &ip6->ip6_src, sizeof(*saddr6));
451 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
456 ip6->ip6_src = ia->ia_addr.sin6_addr;
461 * Source address for DAD packet must always be IPv6
462 * unspecified address. (0::0)
464 bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
466 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
467 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
468 nd_ns->nd_ns_code = 0;
469 nd_ns->nd_ns_reserved = 0;
470 nd_ns->nd_ns_target = *taddr6;
472 if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns->nd_ns_target))
473 nd_ns->nd_ns_target.s6_addr16[1] = 0;
476 * Add source link-layer address option.
478 * spec implementation
480 * DAD packet MUST NOT do not add the option
481 * there's no link layer address:
482 * impossible do not add the option
483 * there's link layer address:
484 * Multicast NS MUST add one add the option
485 * Unicast NS SHOULD add one add the option
487 if (!dad && (mac = nd6_ifptomac(ifp))) {
488 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
489 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
490 /* 8 byte alignments... */
491 optlen = (optlen + 7) & ~7;
493 m->m_pkthdr.len += optlen;
496 bzero((caddr_t)nd_opt, optlen);
497 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
498 nd_opt->nd_opt_len = optlen >> 3;
499 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
502 ip6->ip6_plen = htons((u_short)icmp6len);
503 nd_ns->nd_ns_cksum = 0;
505 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
508 /* Don't lookup socket */
509 (void)ipsec_setsocket(m, NULL);
511 ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif);
513 icmp6_ifstat_inc(outif, ifs6_out_msg);
514 icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
516 icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
520 * Neighbor advertisement input handling.
523 * Based on RFC 2462 (duplicated address detection)
525 * the following items are not implemented yet:
526 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
527 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
530 nd6_na_input(m, off, icmp6len)
534 struct ifnet *ifp = m->m_pkthdr.rcvif;
535 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
536 struct nd_neighbor_advert *nd_na;
538 struct in6_addr saddr6 = ip6->ip6_src;
540 struct in6_addr daddr6 = ip6->ip6_dst;
541 struct in6_addr taddr6;
549 struct llinfo_nd6 *ln;
551 struct sockaddr_dl *sdl;
552 union nd_opts ndopts;
554 if (ip6->ip6_hlim != 255) {
556 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
557 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
558 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
562 #ifndef PULLDOWN_TEST
563 IP6_EXTHDR_CHECK(m, off, icmp6len,);
564 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
566 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
568 icmp6stat.icp6s_tooshort++;
572 taddr6 = nd_na->nd_na_target;
573 flags = nd_na->nd_na_flags_reserved;
574 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
575 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
576 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
578 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
579 taddr6.s6_addr16[1] = htons(ifp->if_index);
581 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
583 "nd6_na_input: invalid target address %s\n",
584 ip6_sprintf(&taddr6)));
587 if (IN6_IS_ADDR_MULTICAST(&daddr6))
590 "nd6_na_input: a solicited adv is multicasted\n"));
594 icmp6len -= sizeof(*nd_na);
595 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
596 if (nd6_options(&ndopts) < 0) {
598 "nd6_na_input: invalid ND option, ignored\n"));
599 /* nd6_options have incremented stats */
603 if (ndopts.nd_opts_tgt_lladdr) {
604 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
605 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
608 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
611 * Target address matches one of my interface address.
613 * If my address is tentative, this means that there's somebody
614 * already using the same address as mine. This indicates DAD failure.
615 * This is defined in RFC 2462.
617 * Otherwise, process as defined in RFC 2461.
620 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
621 nd6_dad_na_input(ifa);
625 /* Just for safety, maybe unnecessary. */
628 "nd6_na_input: duplicate IP6 address %s\n",
629 ip6_sprintf(&taddr6));
633 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
635 "nd6_na_input: lladdrlen mismatch for %s "
636 "(if %d, NA packet %d)\n",
637 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
642 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
644 rt = nd6_lookup(&taddr6, 0, ifp);
646 ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
647 ((sdl = SDL(rt->rt_gateway)) == NULL))
650 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
652 * If the link-layer has address, and no lladdr option came,
653 * discard the packet.
655 if (ifp->if_addrlen && !lladdr)
659 * Record link-layer address, and update the state.
661 sdl->sdl_alen = ifp->if_addrlen;
662 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
664 ln->ln_state = ND6_LLINFO_REACHABLE;
667 ln->ln_expire = time_second +
668 nd_ifinfo[rt->rt_ifp->if_index].reachable;
670 ln->ln_state = ND6_LLINFO_STALE;
671 ln->ln_expire = time_second + nd6_gctimer;
673 if ((ln->ln_router = is_router) != 0) {
675 * This means a router's state has changed from
676 * non-reachable to probably reachable, and might
677 * affect the status of associated prefixes..
679 pfxlist_onlink_check();
685 * Check if the link-layer address has changed or not.
691 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
700 * This is VERY complex. Look at it with care.
702 * override solicit lladdr llchange action
707 * 0 0 y y (1) REACHABLE->STALE
708 * 0 1 n -- (2c) *->REACHABLE
709 * 0 1 y n (2b) L *->REACHABLE
710 * 0 1 y y (1) REACHABLE->STALE
713 * 1 0 y y (2a) L *->STALE
714 * 1 1 n -- (2a) *->REACHABLE
715 * 1 1 y n (2a) L *->REACHABLE
716 * 1 1 y y (2a) L *->REACHABLE
718 if (!is_override && (lladdr && llchange)) { /* (1) */
720 * If state is REACHABLE, make it STALE.
721 * no other updates should be done.
723 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
724 ln->ln_state = ND6_LLINFO_STALE;
725 ln->ln_expire = time_second + nd6_gctimer;
728 } else if (is_override /* (2a) */
729 || (!is_override && (lladdr && !llchange)) /* (2b) */
730 || !lladdr) { /* (2c) */
732 * Update link-local address, if any.
735 sdl->sdl_alen = ifp->if_addrlen;
736 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
740 * If solicited, make the state REACHABLE.
741 * If not solicited and the link-layer address was
742 * changed, make it STALE.
745 ln->ln_state = ND6_LLINFO_REACHABLE;
748 ln->ln_expire = time_second +
749 nd_ifinfo[ifp->if_index].reachable;
752 if (lladdr && llchange) {
753 ln->ln_state = ND6_LLINFO_STALE;
754 ln->ln_expire = time_second + nd6_gctimer;
759 if (ln->ln_router && !is_router) {
761 * The peer dropped the router flag.
762 * Remove the sender from the Default Router List and
763 * update the Destination Cache entries.
765 struct nd_defrouter *dr;
766 struct in6_addr *in6;
769 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
772 * Lock to protect the default router list.
773 * XXX: this might be unnecessary, since this function
774 * is only called under the network software interrupt
775 * context. However, we keep it just for safety.
778 dr = defrouter_lookup(in6, rt->rt_ifp);
781 else if (!ip6_forwarding && ip6_accept_rtadv) {
783 * Even if the neighbor is not in the default
784 * router list, the neighbor may be used
785 * as a next hop for some destinations
786 * (e.g. redirect case). So we must
787 * call rt6_flush explicitly.
789 rt6_flush(&ip6->ip6_src, rt->rt_ifp);
793 ln->ln_router = is_router;
795 rt->rt_flags &= ~RTF_REJECT;
799 * we assume ifp is not a loopback here, so just set the 2nd
800 * argument as the 1st one.
802 nd6_output(ifp, ifp, ln->ln_hold,
803 (struct sockaddr_in6 *)rt_key(rt), rt);
812 icmp6stat.icp6s_badna++;
817 * Neighbor advertisement output handling.
821 * the following items are not implemented yet:
822 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
823 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
826 nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr, sdl0)
828 const struct in6_addr *daddr6, *taddr6;
830 int tlladdr; /* 1 if include target link-layer address */
831 struct sockaddr *sdl0; /* sockaddr_dl (= proxy NA) or NULL */
835 struct nd_neighbor_advert *nd_na;
836 struct in6_ifaddr *ia = NULL;
837 struct ip6_moptions im6o;
841 struct ifnet *outif = NULL;
843 /* estimate the size of message */
844 maxlen = sizeof(*ip6) + sizeof(*nd_na);
845 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
846 if (max_linkhdr + maxlen >= MCLBYTES) {
848 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
849 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
854 MGETHDR(m, M_DONTWAIT, MT_DATA);
855 if (m && max_linkhdr + maxlen >= MHLEN) {
856 MCLGET(m, M_DONTWAIT);
857 if ((m->m_flags & M_EXT) == 0) {
864 m->m_pkthdr.rcvif = NULL;
866 if (IN6_IS_ADDR_MULTICAST(daddr6)) {
867 m->m_flags |= M_MCAST;
868 im6o.im6o_multicast_ifp = ifp;
869 im6o.im6o_multicast_hlim = 255;
870 im6o.im6o_multicast_loop = 0;
873 icmp6len = sizeof(*nd_na);
874 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
875 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
877 /* fill neighbor advertisement packet */
878 ip6 = mtod(m, struct ip6_hdr *);
880 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
881 ip6->ip6_vfc |= IPV6_VERSION;
882 ip6->ip6_nxt = IPPROTO_ICMPV6;
884 if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
886 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
887 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
888 ip6->ip6_dst.s6_addr32[1] = 0;
889 ip6->ip6_dst.s6_addr32[2] = 0;
890 ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
891 flags &= ~ND_NA_FLAG_SOLICITED;
893 ip6->ip6_dst = *daddr6;
896 * Select a source whose scope is the same as that of the dest.
898 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
903 ip6->ip6_src = ia->ia_addr.sin6_addr;
904 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
905 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
906 nd_na->nd_na_code = 0;
907 nd_na->nd_na_target = *taddr6;
908 if (IN6_IS_SCOPE_LINKLOCAL(&nd_na->nd_na_target))
909 nd_na->nd_na_target.s6_addr16[1] = 0;
912 * "tlladdr" indicates NS's condition for adding tlladdr or not.
913 * see nd6_ns_input() for details.
914 * Basically, if NS packet is sent to unicast/anycast addr,
915 * target lladdr option SHOULD NOT be included.
919 * sdl0 != NULL indicates proxy NA. If we do proxy, use
920 * lladdr in sdl0. If we are not proxying (sending NA for
921 * my address) use lladdr configured for the interface.
924 mac = nd6_ifptomac(ifp);
925 else if (sdl0->sa_family == AF_LINK) {
926 struct sockaddr_dl *sdl;
927 sdl = (struct sockaddr_dl *)sdl0;
928 if (sdl->sdl_alen == ifp->if_addrlen)
932 if (tlladdr && mac) {
933 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
934 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
936 /* roundup to 8 bytes alignment! */
937 optlen = (optlen + 7) & ~7;
939 m->m_pkthdr.len += optlen;
942 bzero((caddr_t)nd_opt, optlen);
943 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
944 nd_opt->nd_opt_len = optlen >> 3;
945 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
947 flags &= ~ND_NA_FLAG_OVERRIDE;
949 ip6->ip6_plen = htons((u_short)icmp6len);
950 nd_na->nd_na_flags_reserved = flags;
951 nd_na->nd_na_cksum = 0;
953 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
956 /* Don't lookup socket */
957 (void)ipsec_setsocket(m, NULL);
959 ip6_output(m, NULL, NULL, 0, &im6o, &outif);
961 icmp6_ifstat_inc(outif, ifs6_out_msg);
962 icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
964 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
971 switch (ifp->if_type) {
982 return ((caddr_t)(ifp + 1));
989 TAILQ_HEAD(dadq_head, dadq);
991 TAILQ_ENTRY(dadq) dad_list;
992 struct ifaddr *dad_ifa;
993 int dad_count; /* max NS to send */
994 int dad_ns_tcount; /* # of trials to send NS */
995 int dad_ns_ocount; /* NS sent so far */
998 struct callout dad_timer_ch;
1001 static struct dadq_head dadq;
1002 static int dad_init = 0;
1004 static struct dadq *
1010 for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
1011 if (dp->dad_ifa == ifa)
1018 nd6_dad_starttimer(dp, ticks)
1023 callout_reset(&dp->dad_timer_ch, ticks,
1024 (void (*) __P((void *)))nd6_dad_timer, (void *)dp->dad_ifa);
1028 nd6_dad_stoptimer(dp)
1032 callout_stop(&dp->dad_timer_ch);
1036 * Start Duplicated Address Detection (DAD) for specified interface address.
1039 nd6_dad_start(ifa, tick)
1041 int *tick; /* minimum delay ticks for IFF_UP event */
1043 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1052 * If we don't need DAD, don't do it.
1053 * There are several cases:
1054 * - DAD is disabled (ip6_dad_count == 0)
1055 * - the interface address is anycast
1057 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1059 "nd6_dad_start: called with non-tentative address "
1061 ip6_sprintf(&ia->ia_addr.sin6_addr),
1062 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1065 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1066 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1069 if (!ip6_dad_count) {
1070 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1074 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1075 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1077 if (nd6_dad_find(ifa) != NULL) {
1078 /* DAD already in progress */
1082 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1084 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1086 ip6_sprintf(&ia->ia_addr.sin6_addr),
1087 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1090 bzero(dp, sizeof(*dp));
1091 callout_init(&dp->dad_timer_ch, 0);
1092 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1094 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1095 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1098 * Send NS packet for DAD, ip6_dad_count times.
1099 * Note that we must delay the first transmission, if this is the
1100 * first packet to be sent from the interface after interface
1101 * (re)initialization.
1104 IFAREF(ifa); /* just for safety */
1105 dp->dad_count = ip6_dad_count;
1106 dp->dad_ns_icount = dp->dad_na_icount = 0;
1107 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1109 nd6_dad_ns_output(dp, ifa);
1110 nd6_dad_starttimer(dp,
1111 nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
1116 ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz);
1118 ntick = *tick + random() % (hz / 2);
1120 nd6_dad_starttimer(dp, ntick);
1125 * terminate DAD unconditionally. used for address removals.
1135 dp = nd6_dad_find(ifa);
1137 /* DAD wasn't started yet */
1141 nd6_dad_stoptimer(dp);
1143 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1154 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1157 s = splnet(); /* XXX */
1161 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1164 dp = nd6_dad_find(ifa);
1166 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1169 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1170 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1172 ip6_sprintf(&ia->ia_addr.sin6_addr),
1173 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1176 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1177 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1179 ip6_sprintf(&ia->ia_addr.sin6_addr),
1180 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1184 /* timeouted with IFF_{RUNNING,UP} check */
1185 if (dp->dad_ns_tcount > dad_maxtry) {
1186 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1187 if_name(ifa->ifa_ifp)));
1189 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1196 /* Need more checks? */
1197 if (dp->dad_ns_ocount < dp->dad_count) {
1199 * We have more NS to go. Send NS packet for DAD.
1201 nd6_dad_ns_output(dp, ifa);
1202 nd6_dad_starttimer(dp,
1203 nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
1206 * We have transmitted sufficient number of DAD packets.
1207 * See what we've got.
1213 if (dp->dad_na_icount) {
1215 * the check is in nd6_dad_na_input(),
1221 if (dp->dad_ns_icount) {
1222 #if 0 /* heuristics */
1225 * - we have sent many(?) DAD NS, and
1226 * - the number of NS we sent equals to the
1227 * number of NS we've got, and
1229 * we may have a faulty network card/driver which
1230 * loops back multicasts to myself.
1232 if (3 < dp->dad_count
1233 && dp->dad_ns_icount == dp->dad_count
1234 && dp->dad_na_icount == 0) {
1235 log(LOG_INFO, "DAD questionable for %s(%s): "
1236 "network card loops back multicast?\n",
1237 ip6_sprintf(&ia->ia_addr.sin6_addr),
1238 if_name(ifa->ifa_ifp));
1239 /* XXX consider it a duplicate or not? */
1242 /* We've seen NS, means DAD has failed. */
1246 /* We've seen NS, means DAD has failed. */
1252 /* (*dp) will be freed in nd6_dad_duplicated() */
1254 nd6_dad_duplicated(ifa);
1257 * We are done with DAD. No NA came, no NS came.
1258 * duplicated address found.
1260 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1263 "%s: DAD complete for %s - no duplicates found\n",
1264 if_name(ifa->ifa_ifp),
1265 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1267 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1279 nd6_dad_duplicated(ifa)
1282 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1285 dp = nd6_dad_find(ifa);
1287 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1291 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1292 "NS in/out=%d/%d, NA in=%d\n",
1293 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
1294 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1296 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1297 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1299 /* We are done with DAD, with duplicated address found. (failure) */
1300 nd6_dad_stoptimer(dp);
1302 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1303 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1304 log(LOG_ERR, "%s: manual intervention required\n",
1305 if_name(ifa->ifa_ifp));
1307 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1314 nd6_dad_ns_output(dp, ifa)
1318 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1319 struct ifnet *ifp = ifa->ifa_ifp;
1321 dp->dad_ns_tcount++;
1322 if ((ifp->if_flags & IFF_UP) == 0) {
1324 printf("%s: interface down?\n", if_name(ifp));
1328 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1330 printf("%s: interface not running?\n", if_name(ifp));
1335 dp->dad_ns_ocount++;
1336 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1340 nd6_dad_ns_input(ifa)
1343 struct in6_ifaddr *ia;
1345 const struct in6_addr *taddr6;
1350 panic("ifa == NULL in nd6_dad_ns_input");
1352 ia = (struct in6_ifaddr *)ifa;
1354 taddr6 = &ia->ia_addr.sin6_addr;
1356 dp = nd6_dad_find(ifa);
1358 /* Quickhack - completely ignore DAD NS packets */
1359 if (dad_ignore_ns) {
1361 "nd6_dad_ns_input: ignoring DAD NS packet for "
1362 "address %s(%s)\n", ip6_sprintf(taddr6),
1363 if_name(ifa->ifa_ifp)));
1368 * if I'm yet to start DAD, someone else started using this address
1369 * first. I have a duplicate and you win.
1371 if (!dp || dp->dad_ns_ocount == 0)
1374 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1377 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1378 nd6_dad_duplicated(ifa);
1381 * not sure if I got a duplicate.
1382 * increment ns count and see what happens.
1385 dp->dad_ns_icount++;
1390 nd6_dad_na_input(ifa)
1396 panic("ifa == NULL in nd6_dad_na_input");
1398 dp = nd6_dad_find(ifa);
1400 dp->dad_na_icount++;
1402 /* remove the address. */
1403 nd6_dad_duplicated(ifa);