nd6_nbr.c revision 151477
1/* $FreeBSD: head/sys/netinet6/nd6_nbr.c 151477 2005-10-19 16:43:57Z suz $ */ 2/* $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $ */ 3 4/*- 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 19 * 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 30 * SUCH DAMAGE. 31 */ 32 33#include "opt_inet.h" 34#include "opt_inet6.h" 35#include "opt_ipsec.h" 36#include "opt_carp.h" 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/malloc.h> 41#include <sys/mbuf.h> 42#include <sys/socket.h> 43#include <sys/sockio.h> 44#include <sys/time.h> 45#include <sys/kernel.h> 46#include <sys/errno.h> 47#include <sys/syslog.h> 48#include <sys/queue.h> 49#include <sys/callout.h> 50 51#include <net/if.h> 52#include <net/if_types.h> 53#include <net/if_dl.h> 54#include <net/if_var.h> 55#include <net/route.h> 56 57#include <netinet/in.h> 58#include <netinet/in_var.h> 59#include <netinet6/in6_var.h> 60#include <netinet6/in6_ifattach.h> 61#include <netinet/ip6.h> 62#include <netinet6/ip6_var.h> 63#include <netinet6/scope6_var.h> 64#include <netinet6/nd6.h> 65#include <netinet/icmp6.h> 66 67#ifdef DEV_CARP 68#include <netinet/ip_carp.h> 69#endif 70 71#include <net/net_osdep.h> 72 73#define SDL(s) ((struct sockaddr_dl *)s) 74 75struct dadq; 76static struct dadq *nd6_dad_find __P((struct ifaddr *)); 77static void nd6_dad_starttimer __P((struct dadq *, int)); 78static void nd6_dad_stoptimer __P((struct dadq *)); 79static void nd6_dad_timer __P((struct ifaddr *)); 80static void nd6_dad_ns_output __P((struct dadq *, struct ifaddr *)); 81static void nd6_dad_ns_input __P((struct ifaddr *)); 82static void nd6_dad_na_input __P((struct ifaddr *)); 83 84static int dad_ignore_ns = 0; /* ignore NS in DAD - specwise incorrect*/ 85static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */ 86 87/* 88 * Input a Neighbor Solicitation Message. 89 * 90 * Based on RFC 2461 91 * Based on RFC 2462 (duplicate address detection) 92 */ 93void 94nd6_ns_input(m, off, icmp6len) 95 struct mbuf *m; 96 int off, icmp6len; 97{ 98 struct ifnet *ifp = m->m_pkthdr.rcvif; 99 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 100 struct nd_neighbor_solicit *nd_ns; 101 struct in6_addr saddr6 = ip6->ip6_src; 102 struct in6_addr daddr6 = ip6->ip6_dst; 103 struct in6_addr taddr6; 104 struct in6_addr myaddr6; 105 char *lladdr = NULL; 106 struct ifaddr *ifa = NULL; 107 int lladdrlen = 0; 108 int anycast = 0, proxy = 0, tentative = 0; 109 int tlladdr; 110 union nd_opts ndopts; 111 struct sockaddr_dl *proxydl = NULL; 112 113#ifndef PULLDOWN_TEST 114 IP6_EXTHDR_CHECK(m, off, icmp6len,); 115 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off); 116#else 117 IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len); 118 if (nd_ns == NULL) { 119 icmp6stat.icp6s_tooshort++; 120 return; 121 } 122#endif 123 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */ 124 taddr6 = nd_ns->nd_ns_target; 125 if (in6_setscope(&taddr6, ifp, NULL) != 0) 126 goto bad; 127 128 if (ip6->ip6_hlim != 255) { 129 nd6log((LOG_ERR, 130 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n", 131 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 132 ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); 133 goto bad; 134 } 135 136 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) { 137 /* dst has to be a solicited node multicast address. */ 138 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL && 139 /* don't check ifindex portion */ 140 daddr6.s6_addr32[1] == 0 && 141 daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE && 142 daddr6.s6_addr8[12] == 0xff) { 143 ; /* good */ 144 } else { 145 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet " 146 "(wrong ip6 dst)\n")); 147 goto bad; 148 } 149 } 150 151 if (IN6_IS_ADDR_MULTICAST(&taddr6)) { 152 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n")); 153 goto bad; 154 } 155 156 icmp6len -= sizeof(*nd_ns); 157 nd6_option_init(nd_ns + 1, icmp6len, &ndopts); 158 if (nd6_options(&ndopts) < 0) { 159 nd6log((LOG_INFO, 160 "nd6_ns_input: invalid ND option, ignored\n")); 161 /* nd6_options have incremented stats */ 162 goto freeit; 163 } 164 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; 168 } 169 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")); 173 goto bad; 174 } 175 176 /* 177 * Attaching target link-layer address to the NA? 178 * (RFC 2461 7.2.4) 179 * 180 * NS IP dst is unicast/anycast MUST NOT add 181 * NS IP dst is solicited-node multicast MUST add 182 * 183 * In implementation, we add target link-layer address by default. 184 * We do not add one in MUST NOT cases. 185 */ 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)) 189 tlladdr = 0; 190 else 191#endif 192 if (!IN6_IS_ADDR_MULTICAST(&daddr6)) 193 tlladdr = 0; 194 else 195 tlladdr = 1; 196 197 /* 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. 202 */ 203 /* (1) and (3) check. */ 204#ifdef DEV_CARP 205 if (ifp->if_carp) 206 ifa = carp_iamatch6(ifp->if_carp, &taddr6); 207 if (ifa == NULL) 208 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); 209#else 210 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); 211#endif 212 213 /* (2) check. */ 214 if (ifa == NULL) { 215 struct rtentry *rt; 216 struct sockaddr_in6 tsin6; 217 int need_proxy; 218 219 bzero(&tsin6, sizeof tsin6); 220 tsin6.sin6_len = sizeof(struct sockaddr_in6); 221 tsin6.sin6_family = AF_INET6; 222 tsin6.sin6_addr = taddr6; 223 224 rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0); 225 need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 && 226 rt->rt_gateway->sa_family == AF_LINK); 227 if (rt) 228 rtfree(rt); 229 if (need_proxy) { 230 /* 231 * proxy NDP for single entry 232 */ 233 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 234 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST); 235 if (ifa) { 236 proxy = 1; 237 proxydl = SDL(rt->rt_gateway); 238 } 239 } 240 } 241 if (!ifa) { 242 /* 243 * We've got an NS packet, and we don't have that adddress 244 * assigned for us. We MUST silently ignore it. 245 * See RFC2461 7.2.3. 246 */ 247 goto freeit; 248 } 249 myaddr6 = *IFA_IN6(ifa); 250 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST; 251 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE; 252 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED) 253 goto freeit; 254 255 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 256 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s " 257 "(if %d, NS packet %d)\n", 258 ip6_sprintf(&taddr6), 259 ifp->if_addrlen, lladdrlen - 2)); 260 goto bad; 261 } 262 263 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) { 264 nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n", 265 ip6_sprintf(&saddr6))); 266 goto freeit; 267 } 268 269 /* 270 * We have neighbor solicitation packet, with target address equals to 271 * one of my tentative address. 272 * 273 * src addr how to process? 274 * --- --- 275 * multicast of course, invalid (rejected in ip6_input) 276 * unicast somebody is doing address resolution -> ignore 277 * unspec dup address detection 278 * 279 * The processing is defined in RFC 2462. 280 */ 281 if (tentative) { 282 /* 283 * If source address is unspecified address, it is for 284 * duplicate address detection. 285 * 286 * If not, the packet is for addess resolution; 287 * silently ignore it. 288 */ 289 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 290 nd6_dad_ns_input(ifa); 291 292 goto freeit; 293 } 294 295 /* 296 * If the source address is unspecified address, entries must not 297 * be created or updated. 298 * It looks that sender is performing DAD. Output NA toward 299 * all-node multicast address, to tell the sender that I'm using 300 * the address. 301 * S bit ("solicited") must be zero. 302 */ 303 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) { 304 struct in6_addr in6_all; 305 306 in6_all = in6addr_linklocal_allnodes; 307 if (in6_setscope(&in6_all, ifp, NULL) != 0) 308 goto bad; 309 nd6_na_output(ifp, &in6_all, &taddr6, 310 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) | 311 (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0), 312 tlladdr, (struct sockaddr *)proxydl); 313 goto freeit; 314 } 315 316 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, 317 ND_NEIGHBOR_SOLICIT, 0); 318 319 nd6_na_output(ifp, &saddr6, &taddr6, 320 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) | 321 (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED, 322 tlladdr, (struct sockaddr *)proxydl); 323 freeit: 324 m_freem(m); 325 return; 326 327 bad: 328 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6))); 329 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6))); 330 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6))); 331 icmp6stat.icp6s_badns++; 332 m_freem(m); 333} 334 335/* 336 * Output a Neighbor Solicitation Message. Caller specifies: 337 * - ICMP6 header source IP6 address 338 * - ND6 header target IP6 address 339 * - ND6 header source datalink address 340 * 341 * Based on RFC 2461 342 * Based on RFC 2462 (duplicate address detection) 343 */ 344void 345nd6_ns_output(ifp, daddr6, taddr6, ln, dad) 346 struct ifnet *ifp; 347 const struct in6_addr *daddr6, *taddr6; 348 struct llinfo_nd6 *ln; /* for source address determination */ 349 int dad; /* duplicate address detection */ 350{ 351 struct mbuf *m; 352 struct ip6_hdr *ip6; 353 struct nd_neighbor_solicit *nd_ns; 354 struct in6_addr *src, src_in; 355 struct ip6_moptions im6o; 356 int icmp6len; 357 int maxlen; 358 caddr_t mac; 359 struct route_in6 ro; 360 361 bzero(&ro, sizeof(ro)); 362 363 if (IN6_IS_ADDR_MULTICAST(taddr6)) 364 return; 365 366 /* estimate the size of message */ 367 maxlen = sizeof(*ip6) + sizeof(*nd_ns); 368 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7; 369 if (max_linkhdr + maxlen >= MCLBYTES) { 370#ifdef DIAGNOSTIC 371 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES " 372 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES); 373#endif 374 return; 375 } 376 377 MGETHDR(m, M_DONTWAIT, MT_DATA); 378 if (m && max_linkhdr + maxlen >= MHLEN) { 379 MCLGET(m, M_DONTWAIT); 380 if ((m->m_flags & M_EXT) == 0) { 381 m_free(m); 382 m = NULL; 383 } 384 } 385 if (m == NULL) 386 return; 387 m->m_pkthdr.rcvif = NULL; 388 389 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) { 390 m->m_flags |= M_MCAST; 391 im6o.im6o_multicast_ifp = ifp; 392 im6o.im6o_multicast_hlim = 255; 393 im6o.im6o_multicast_loop = 0; 394 } 395 396 icmp6len = sizeof(*nd_ns); 397 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len; 398 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ 399 400 /* fill neighbor solicitation packet */ 401 ip6 = mtod(m, struct ip6_hdr *); 402 ip6->ip6_flow = 0; 403 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 404 ip6->ip6_vfc |= IPV6_VERSION; 405 /* ip6->ip6_plen will be set later */ 406 ip6->ip6_nxt = IPPROTO_ICMPV6; 407 ip6->ip6_hlim = 255; 408 if (daddr6) 409 ip6->ip6_dst = *daddr6; 410 else { 411 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL; 412 ip6->ip6_dst.s6_addr16[1] = 0; 413 ip6->ip6_dst.s6_addr32[1] = 0; 414 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE; 415 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3]; 416 ip6->ip6_dst.s6_addr8[12] = 0xff; 417 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 418 goto bad; 419 } 420 if (!dad) { 421 /* 422 * RFC2461 7.2.2: 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." 429 * 430 * We use the source address for the prompting packet 431 * (saddr6), if: 432 * - saddr6 is given from the caller (by giving "ln"), and 433 * - saddr6 belongs to the outgoing interface. 434 * Otherwise, we perform the source address selection as usual. 435 */ 436 struct ip6_hdr *hip6; /* hold ip6 */ 437 struct in6_addr *hsrc = NULL; 438 439 if (ln && ln->ln_hold) { 440 /* 441 * assuming every packet in ln_hold has the same IP 442 * header 443 */ 444 hip6 = mtod(ln->ln_hold, struct ip6_hdr *); 445 /* XXX pullup? */ 446 if (sizeof(*hip6) < ln->ln_hold->m_len) 447 hsrc = &hip6->ip6_src; 448 else 449 hsrc = NULL; 450 } 451 if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc)) 452 src = hsrc; 453 else { 454 int error; 455 struct sockaddr_in6 dst_sa; 456 457 bzero(&dst_sa, sizeof(dst_sa)); 458 dst_sa.sin6_family = AF_INET6; 459 dst_sa.sin6_len = sizeof(dst_sa); 460 dst_sa.sin6_addr = ip6->ip6_dst; 461 462 src = in6_selectsrc(&dst_sa, NULL, 463 NULL, &ro, NULL, NULL, &error); 464 if (src == NULL) { 465 nd6log((LOG_DEBUG, 466 "nd6_ns_output: source can't be " 467 "determined: dst=%s, error=%d\n", 468 ip6_sprintf(&dst_sa.sin6_addr), error)); 469 goto bad; 470 } 471 } 472 } else { 473 /* 474 * Source address for DAD packet must always be IPv6 475 * unspecified address. (0::0) 476 * We actually don't have to 0-clear the address (we did it 477 * above), but we do so here explicitly to make the intention 478 * clearer. 479 */ 480 bzero(&src_in, sizeof(src_in)); 481 src = &src_in; 482 } 483 ip6->ip6_src = *src; 484 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1); 485 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT; 486 nd_ns->nd_ns_code = 0; 487 nd_ns->nd_ns_reserved = 0; 488 nd_ns->nd_ns_target = *taddr6; 489 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */ 490 491 /* 492 * Add source link-layer address option. 493 * 494 * spec implementation 495 * --- --- 496 * DAD packet MUST NOT do not add the option 497 * there's no link layer address: 498 * impossible do not add the option 499 * there's link layer address: 500 * Multicast NS MUST add one add the option 501 * Unicast NS SHOULD add one add the option 502 */ 503 if (!dad && (mac = nd6_ifptomac(ifp))) { 504 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen; 505 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1); 506 /* 8 byte alignments... */ 507 optlen = (optlen + 7) & ~7; 508 509 m->m_pkthdr.len += optlen; 510 m->m_len += optlen; 511 icmp6len += optlen; 512 bzero((caddr_t)nd_opt, optlen); 513 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; 514 nd_opt->nd_opt_len = optlen >> 3; 515 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen); 516 } 517 518 ip6->ip6_plen = htons((u_short)icmp6len); 519 nd_ns->nd_ns_cksum = 0; 520 nd_ns->nd_ns_cksum = 521 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len); 522 523 ip6_output(m, NULL, &ro, dad ? IPV6_DADOUTPUT : 0, &im6o, NULL, NULL); 524 icmp6_ifstat_inc(ifp, ifs6_out_msg); 525 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit); 526 icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++; 527 528 if (ro.ro_rt) { /* we don't cache this route. */ 529 RTFREE(ro.ro_rt); 530 } 531 return; 532 533 bad: 534 if (ro.ro_rt) { 535 RTFREE(ro.ro_rt); 536 } 537 m_freem(m); 538 return; 539} 540 541/* 542 * Neighbor advertisement input handling. 543 * 544 * Based on RFC 2461 545 * Based on RFC 2462 (duplicate address detection) 546 * 547 * the following items are not implemented yet: 548 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD) 549 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD) 550 */ 551void 552nd6_na_input(m, off, icmp6len) 553 struct mbuf *m; 554 int off, icmp6len; 555{ 556 struct ifnet *ifp = m->m_pkthdr.rcvif; 557 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 558 struct nd_neighbor_advert *nd_na; 559 struct in6_addr daddr6 = ip6->ip6_dst; 560 struct in6_addr taddr6; 561 int flags; 562 int is_router; 563 int is_solicited; 564 int is_override; 565 char *lladdr = NULL; 566 int lladdrlen = 0; 567 struct ifaddr *ifa; 568 struct llinfo_nd6 *ln; 569 struct rtentry *rt; 570 struct sockaddr_dl *sdl; 571 union nd_opts ndopts; 572 573 if (ip6->ip6_hlim != 255) { 574 nd6log((LOG_ERR, 575 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n", 576 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 577 ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); 578 goto bad; 579 } 580 581#ifndef PULLDOWN_TEST 582 IP6_EXTHDR_CHECK(m, off, icmp6len,); 583 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off); 584#else 585 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len); 586 if (nd_na == NULL) { 587 icmp6stat.icp6s_tooshort++; 588 return; 589 } 590#endif 591 592 flags = nd_na->nd_na_flags_reserved; 593 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0); 594 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0); 595 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0); 596 597 taddr6 = nd_na->nd_na_target; 598 if (in6_setscope(&taddr6, ifp, NULL)) 599 goto bad; /* XXX: impossible */ 600 601 if (IN6_IS_ADDR_MULTICAST(&taddr6)) { 602 nd6log((LOG_ERR, 603 "nd6_na_input: invalid target address %s\n", 604 ip6_sprintf(&taddr6))); 605 goto bad; 606 } 607 if (IN6_IS_ADDR_MULTICAST(&daddr6)) 608 if (is_solicited) { 609 nd6log((LOG_ERR, 610 "nd6_na_input: a solicited adv is multicasted\n")); 611 goto bad; 612 } 613 614 icmp6len -= sizeof(*nd_na); 615 nd6_option_init(nd_na + 1, icmp6len, &ndopts); 616 if (nd6_options(&ndopts) < 0) { 617 nd6log((LOG_INFO, 618 "nd6_na_input: invalid ND option, ignored\n")); 619 /* nd6_options have incremented stats */ 620 goto freeit; 621 } 622 623 if (ndopts.nd_opts_tgt_lladdr) { 624 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 625 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 626 } 627 628 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); 629 630 /* 631 * Target address matches one of my interface address. 632 * 633 * If my address is tentative, this means that there's somebody 634 * already using the same address as mine. This indicates DAD failure. 635 * This is defined in RFC 2462. 636 * 637 * Otherwise, process as defined in RFC 2461. 638 */ 639 if (ifa 640 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) { 641 nd6_dad_na_input(ifa); 642 goto freeit; 643 } 644 645 /* Just for safety, maybe unnecessary. */ 646 if (ifa) { 647 log(LOG_ERR, 648 "nd6_na_input: duplicate IP6 address %s\n", 649 ip6_sprintf(&taddr6)); 650 goto freeit; 651 } 652 653 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 654 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s " 655 "(if %d, NA packet %d)\n", ip6_sprintf(&taddr6), 656 ifp->if_addrlen, lladdrlen - 2)); 657 goto bad; 658 } 659 660 /* 661 * If no neighbor cache entry is found, NA SHOULD silently be 662 * discarded. 663 */ 664 rt = nd6_lookup(&taddr6, 0, ifp); 665 if ((rt == NULL) || 666 ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) || 667 ((sdl = SDL(rt->rt_gateway)) == NULL)) 668 goto freeit; 669 670 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { 671 /* 672 * If the link-layer has address, and no lladdr option came, 673 * discard the packet. 674 */ 675 if (ifp->if_addrlen && lladdr == NULL) 676 goto freeit; 677 678 /* 679 * Record link-layer address, and update the state. 680 */ 681 sdl->sdl_alen = ifp->if_addrlen; 682 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen); 683 if (is_solicited) { 684 ln->ln_state = ND6_LLINFO_REACHABLE; 685 ln->ln_byhint = 0; 686 if (ln->ln_expire) { 687 ln->ln_expire = time_second + 688 ND_IFINFO(rt->rt_ifp)->reachable; 689 } 690 } else { 691 ln->ln_state = ND6_LLINFO_STALE; 692 ln->ln_expire = time_second + nd6_gctimer; 693 } 694 if ((ln->ln_router = is_router) != 0) { 695 /* 696 * This means a router's state has changed from 697 * non-reachable to probably reachable, and might 698 * affect the status of associated prefixes.. 699 */ 700 pfxlist_onlink_check(); 701 } 702 } else { 703 int llchange; 704 705 /* 706 * Check if the link-layer address has changed or not. 707 */ 708 if (lladdr == NULL) 709 llchange = 0; 710 else { 711 if (sdl->sdl_alen) { 712 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) 713 llchange = 1; 714 else 715 llchange = 0; 716 } else 717 llchange = 1; 718 } 719 720 /* 721 * This is VERY complex. Look at it with care. 722 * 723 * override solicit lladdr llchange action 724 * (L: record lladdr) 725 * 726 * 0 0 n -- (2c) 727 * 0 0 y n (2b) L 728 * 0 0 y y (1) REACHABLE->STALE 729 * 0 1 n -- (2c) *->REACHABLE 730 * 0 1 y n (2b) L *->REACHABLE 731 * 0 1 y y (1) REACHABLE->STALE 732 * 1 0 n -- (2a) 733 * 1 0 y n (2a) L 734 * 1 0 y y (2a) L *->STALE 735 * 1 1 n -- (2a) *->REACHABLE 736 * 1 1 y n (2a) L *->REACHABLE 737 * 1 1 y y (2a) L *->REACHABLE 738 */ 739 if (!is_override && (lladdr != NULL && llchange)) { /* (1) */ 740 /* 741 * If state is REACHABLE, make it STALE. 742 * no other updates should be done. 743 */ 744 if (ln->ln_state == ND6_LLINFO_REACHABLE) { 745 ln->ln_state = ND6_LLINFO_STALE; 746 ln->ln_expire = time_second + nd6_gctimer; 747 } 748 goto freeit; 749 } else if (is_override /* (2a) */ 750 || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */ 751 || lladdr == NULL) { /* (2c) */ 752 /* 753 * Update link-local address, if any. 754 */ 755 if (lladdr != NULL) { 756 sdl->sdl_alen = ifp->if_addrlen; 757 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen); 758 } 759 760 /* 761 * If solicited, make the state REACHABLE. 762 * If not solicited and the link-layer address was 763 * changed, make it STALE. 764 */ 765 if (is_solicited) { 766 ln->ln_state = ND6_LLINFO_REACHABLE; 767 ln->ln_byhint = 0; 768 if (ln->ln_expire) { 769 ln->ln_expire = time_second + 770 ND_IFINFO(ifp)->reachable; 771 } 772 } else { 773 if (lladdr != NULL && llchange) { 774 ln->ln_state = ND6_LLINFO_STALE; 775 ln->ln_expire = time_second + nd6_gctimer; 776 } 777 } 778 } 779 780 if (ln->ln_router && !is_router) { 781 /* 782 * The peer dropped the router flag. 783 * Remove the sender from the Default Router List and 784 * update the Destination Cache entries. 785 */ 786 struct nd_defrouter *dr; 787 struct in6_addr *in6; 788 int s; 789 790 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr; 791 792 /* 793 * Lock to protect the default router list. 794 * XXX: this might be unnecessary, since this function 795 * is only called under the network software interrupt 796 * context. However, we keep it just for safety. 797 */ 798 s = splnet(); 799 dr = defrouter_lookup(in6, ifp); 800 if (dr) 801 defrtrlist_del(dr); 802 else if (!ip6_forwarding && ip6_accept_rtadv) { 803 /* 804 * Even if the neighbor is not in the default 805 * router list, the neighbor may be used 806 * as a next hop for some destinations 807 * (e.g. redirect case). So we must 808 * call rt6_flush explicitly. 809 */ 810 rt6_flush(&ip6->ip6_src, ifp); 811 } 812 splx(s); 813 } 814 ln->ln_router = is_router; 815 } 816 rt->rt_flags &= ~RTF_REJECT; 817 ln->ln_asked = 0; 818 if (ln->ln_hold) { 819 /* 820 * we assume ifp is not a loopback here, so just set the 2nd 821 * argument as the 1st one. 822 */ 823 nd6_output(ifp, ifp, ln->ln_hold, 824 (struct sockaddr_in6 *)rt_key(rt), rt); 825 ln->ln_hold = NULL; 826 } 827 828 freeit: 829 m_freem(m); 830 return; 831 832 bad: 833 icmp6stat.icp6s_badna++; 834 m_freem(m); 835} 836 837/* 838 * Neighbor advertisement output handling. 839 * 840 * Based on RFC 2461 841 * 842 * the following items are not implemented yet: 843 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD) 844 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD) 845 */ 846void 847nd6_na_output(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0) 848 struct ifnet *ifp; 849 const struct in6_addr *daddr6_0, *taddr6; 850 u_long flags; 851 int tlladdr; /* 1 if include target link-layer address */ 852 struct sockaddr *sdl0; /* sockaddr_dl (= proxy NA) or NULL */ 853{ 854 struct mbuf *m; 855 struct ip6_hdr *ip6; 856 struct nd_neighbor_advert *nd_na; 857 struct ip6_moptions im6o; 858 struct in6_addr *src, daddr6; 859 struct sockaddr_in6 dst_sa; 860 int icmp6len, maxlen, error; 861 caddr_t mac = NULL; 862 struct route_in6 ro; 863 864 bzero(&ro, sizeof(ro)); 865 866 daddr6 = *daddr6_0; /* make a local copy for modification */ 867 868 /* estimate the size of message */ 869 maxlen = sizeof(*ip6) + sizeof(*nd_na); 870 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7; 871 if (max_linkhdr + maxlen >= MCLBYTES) { 872#ifdef DIAGNOSTIC 873 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES " 874 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES); 875#endif 876 return; 877 } 878 879 MGETHDR(m, M_DONTWAIT, MT_DATA); 880 if (m && max_linkhdr + maxlen >= MHLEN) { 881 MCLGET(m, M_DONTWAIT); 882 if ((m->m_flags & M_EXT) == 0) { 883 m_free(m); 884 m = NULL; 885 } 886 } 887 if (m == NULL) 888 return; 889 m->m_pkthdr.rcvif = NULL; 890 891 if (IN6_IS_ADDR_MULTICAST(&daddr6)) { 892 m->m_flags |= M_MCAST; 893 im6o.im6o_multicast_ifp = ifp; 894 im6o.im6o_multicast_hlim = 255; 895 im6o.im6o_multicast_loop = 0; 896 } 897 898 icmp6len = sizeof(*nd_na); 899 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len; 900 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ 901 902 /* fill neighbor advertisement packet */ 903 ip6 = mtod(m, struct ip6_hdr *); 904 ip6->ip6_flow = 0; 905 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 906 ip6->ip6_vfc |= IPV6_VERSION; 907 ip6->ip6_nxt = IPPROTO_ICMPV6; 908 ip6->ip6_hlim = 255; 909 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) { 910 /* reply to DAD */ 911 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL; 912 ip6->ip6_dst.s6_addr16[1] = 0; 913 ip6->ip6_dst.s6_addr32[1] = 0; 914 ip6->ip6_dst.s6_addr32[2] = 0; 915 ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE; 916 if (in6_setscope(&daddr6, ifp, NULL)) 917 goto bad; 918 919 flags &= ~ND_NA_FLAG_SOLICITED; 920 } 921 ip6->ip6_dst = daddr6; 922 bzero(&dst_sa, sizeof(struct sockaddr_in6)); 923 dst_sa.sin6_family = AF_INET6; 924 dst_sa.sin6_len = sizeof(struct sockaddr_in6); 925 dst_sa.sin6_addr = daddr6; 926 927 /* 928 * Select a source whose scope is the same as that of the dest. 929 */ 930 bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa)); 931 src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error); 932 if (src == NULL) { 933 nd6log((LOG_DEBUG, "nd6_na_output: source can't be " 934 "determined: dst=%s, error=%d\n", 935 ip6_sprintf(&dst_sa.sin6_addr), error)); 936 goto bad; 937 } 938 ip6->ip6_src = *src; 939 nd_na = (struct nd_neighbor_advert *)(ip6 + 1); 940 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT; 941 nd_na->nd_na_code = 0; 942 nd_na->nd_na_target = *taddr6; 943 in6_clearscope(&nd_na->nd_na_target); /* XXX */ 944 945 /* 946 * "tlladdr" indicates NS's condition for adding tlladdr or not. 947 * see nd6_ns_input() for details. 948 * Basically, if NS packet is sent to unicast/anycast addr, 949 * target lladdr option SHOULD NOT be included. 950 */ 951 if (tlladdr) { 952 /* 953 * sdl0 != NULL indicates proxy NA. If we do proxy, use 954 * lladdr in sdl0. If we are not proxying (sending NA for 955 * my address) use lladdr configured for the interface. 956 */ 957 if (sdl0 == NULL) { 958#ifdef DEV_CARP 959 if (ifp->if_carp) 960 mac = carp_macmatch6(ifp->if_carp, m, taddr6); 961 if (mac == NULL) 962 mac = nd6_ifptomac(ifp); 963#else 964 mac = nd6_ifptomac(ifp); 965#endif 966 } else if (sdl0->sa_family == AF_LINK) { 967 struct sockaddr_dl *sdl; 968 sdl = (struct sockaddr_dl *)sdl0; 969 if (sdl->sdl_alen == ifp->if_addrlen) 970 mac = LLADDR(sdl); 971 } 972 } 973 if (tlladdr && mac) { 974 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen; 975 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1); 976 977 /* roundup to 8 bytes alignment! */ 978 optlen = (optlen + 7) & ~7; 979 980 m->m_pkthdr.len += optlen; 981 m->m_len += optlen; 982 icmp6len += optlen; 983 bzero((caddr_t)nd_opt, optlen); 984 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 985 nd_opt->nd_opt_len = optlen >> 3; 986 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen); 987 } else 988 flags &= ~ND_NA_FLAG_OVERRIDE; 989 990 ip6->ip6_plen = htons((u_short)icmp6len); 991 nd_na->nd_na_flags_reserved = flags; 992 nd_na->nd_na_cksum = 0; 993 nd_na->nd_na_cksum = 994 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len); 995 996 ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL); 997 icmp6_ifstat_inc(ifp, ifs6_out_msg); 998 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert); 999 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++; 1000 1001 if (ro.ro_rt) { /* we don't cache this route. */ 1002 RTFREE(ro.ro_rt); 1003 } 1004 return; 1005 1006 bad: 1007 if (ro.ro_rt) { 1008 RTFREE(ro.ro_rt); 1009 } 1010 m_freem(m); 1011 return; 1012} 1013 1014caddr_t 1015nd6_ifptomac(ifp) 1016 struct ifnet *ifp; 1017{ 1018 switch (ifp->if_type) { 1019 case IFT_ARCNET: 1020 case IFT_ETHER: 1021 case IFT_FDDI: 1022 case IFT_IEEE1394: 1023#ifdef IFT_L2VLAN 1024 case IFT_L2VLAN: 1025#endif 1026#ifdef IFT_IEEE80211 1027 case IFT_IEEE80211: 1028#endif 1029#ifdef IFT_CARP 1030 case IFT_CARP: 1031#endif 1032 case IFT_BRIDGE: 1033 case IFT_ISO88025: 1034 return IF_LLADDR(ifp); 1035 default: 1036 return NULL; 1037 } 1038} 1039 1040TAILQ_HEAD(dadq_head, dadq); 1041struct dadq { 1042 TAILQ_ENTRY(dadq) dad_list; 1043 struct ifaddr *dad_ifa; 1044 int dad_count; /* max NS to send */ 1045 int dad_ns_tcount; /* # of trials to send NS */ 1046 int dad_ns_ocount; /* NS sent so far */ 1047 int dad_ns_icount; 1048 int dad_na_icount; 1049 struct callout dad_timer_ch; 1050}; 1051 1052static struct dadq_head dadq; 1053static int dad_init = 0; 1054 1055static struct dadq * 1056nd6_dad_find(ifa) 1057 struct ifaddr *ifa; 1058{ 1059 struct dadq *dp; 1060 1061 for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) { 1062 if (dp->dad_ifa == ifa) 1063 return dp; 1064 } 1065 return NULL; 1066} 1067 1068static void 1069nd6_dad_starttimer(dp, ticks) 1070 struct dadq *dp; 1071 int ticks; 1072{ 1073 1074 callout_reset(&dp->dad_timer_ch, ticks, 1075 (void (*) __P((void *)))nd6_dad_timer, (void *)dp->dad_ifa); 1076} 1077 1078static void 1079nd6_dad_stoptimer(dp) 1080 struct dadq *dp; 1081{ 1082 1083 callout_stop(&dp->dad_timer_ch); 1084} 1085 1086/* 1087 * Start Duplicate Address Detection (DAD) for specified interface address. 1088 */ 1089void 1090nd6_dad_start(ifa, tick) 1091 struct ifaddr *ifa; 1092 int *tick; /* minimum delay ticks for IFF_UP event */ 1093{ 1094 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 1095 struct dadq *dp; 1096 1097 if (!dad_init) { 1098 TAILQ_INIT(&dadq); 1099 dad_init++; 1100 } 1101 1102 /* 1103 * If we don't need DAD, don't do it. 1104 * There are several cases: 1105 * - DAD is disabled (ip6_dad_count == 0) 1106 * - the interface address is anycast 1107 */ 1108 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) { 1109 log(LOG_DEBUG, 1110 "nd6_dad_start: called with non-tentative address " 1111 "%s(%s)\n", 1112 ip6_sprintf(&ia->ia_addr.sin6_addr), 1113 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1114 return; 1115 } 1116 if (ia->ia6_flags & IN6_IFF_ANYCAST) { 1117 ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 1118 return; 1119 } 1120 if (!ip6_dad_count) { 1121 ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 1122 return; 1123 } 1124 if (ifa->ifa_ifp == NULL) 1125 panic("nd6_dad_start: ifa->ifa_ifp == NULL"); 1126 if (!(ifa->ifa_ifp->if_flags & IFF_UP)) { 1127 return; 1128 } 1129 if (nd6_dad_find(ifa) != NULL) { 1130 /* DAD already in progress */ 1131 return; 1132 } 1133 1134 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT); 1135 if (dp == NULL) { 1136 log(LOG_ERR, "nd6_dad_start: memory allocation failed for " 1137 "%s(%s)\n", 1138 ip6_sprintf(&ia->ia_addr.sin6_addr), 1139 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1140 return; 1141 } 1142 bzero(dp, sizeof(*dp)); 1143 callout_init(&dp->dad_timer_ch, 0); 1144 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list); 1145 1146 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp), 1147 ip6_sprintf(&ia->ia_addr.sin6_addr))); 1148 1149 /* 1150 * Send NS packet for DAD, ip6_dad_count times. 1151 * Note that we must delay the first transmission, if this is the 1152 * first packet to be sent from the interface after interface 1153 * (re)initialization. 1154 */ 1155 dp->dad_ifa = ifa; 1156 IFAREF(ifa); /* just for safety */ 1157 dp->dad_count = ip6_dad_count; 1158 dp->dad_ns_icount = dp->dad_na_icount = 0; 1159 dp->dad_ns_ocount = dp->dad_ns_tcount = 0; 1160 if (tick == NULL) { 1161 nd6_dad_ns_output(dp, ifa); 1162 nd6_dad_starttimer(dp, 1163 ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000); 1164 } else { 1165 int ntick; 1166 1167 if (*tick == 0) 1168 ntick = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz); 1169 else 1170 ntick = *tick + arc4random() % (hz / 2); 1171 *tick = ntick; 1172 nd6_dad_starttimer(dp, ntick); 1173 } 1174} 1175 1176/* 1177 * terminate DAD unconditionally. used for address removals. 1178 */ 1179void 1180nd6_dad_stop(ifa) 1181 struct ifaddr *ifa; 1182{ 1183 struct dadq *dp; 1184 1185 if (!dad_init) 1186 return; 1187 dp = nd6_dad_find(ifa); 1188 if (!dp) { 1189 /* DAD wasn't started yet */ 1190 return; 1191 } 1192 1193 nd6_dad_stoptimer(dp); 1194 1195 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list); 1196 free(dp, M_IP6NDP); 1197 dp = NULL; 1198 IFAFREE(ifa); 1199} 1200 1201static void 1202nd6_dad_timer(ifa) 1203 struct ifaddr *ifa; 1204{ 1205 int s; 1206 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 1207 struct dadq *dp; 1208 1209 s = splnet(); /* XXX */ 1210 1211 /* Sanity check */ 1212 if (ia == NULL) { 1213 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n"); 1214 goto done; 1215 } 1216 dp = nd6_dad_find(ifa); 1217 if (dp == NULL) { 1218 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n"); 1219 goto done; 1220 } 1221 if (ia->ia6_flags & IN6_IFF_DUPLICATED) { 1222 log(LOG_ERR, "nd6_dad_timer: called with duplicated address " 1223 "%s(%s)\n", 1224 ip6_sprintf(&ia->ia_addr.sin6_addr), 1225 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1226 goto done; 1227 } 1228 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) { 1229 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address " 1230 "%s(%s)\n", 1231 ip6_sprintf(&ia->ia_addr.sin6_addr), 1232 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1233 goto done; 1234 } 1235 1236 /* timeouted with IFF_{RUNNING,UP} check */ 1237 if (dp->dad_ns_tcount > dad_maxtry) { 1238 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n", 1239 if_name(ifa->ifa_ifp))); 1240 1241 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list); 1242 free(dp, M_IP6NDP); 1243 dp = NULL; 1244 IFAFREE(ifa); 1245 goto done; 1246 } 1247 1248 /* Need more checks? */ 1249 if (dp->dad_ns_ocount < dp->dad_count) { 1250 /* 1251 * We have more NS to go. Send NS packet for DAD. 1252 */ 1253 nd6_dad_ns_output(dp, ifa); 1254 nd6_dad_starttimer(dp, 1255 ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000); 1256 } else { 1257 /* 1258 * We have transmitted sufficient number of DAD packets. 1259 * See what we've got. 1260 */ 1261 int duplicate; 1262 1263 duplicate = 0; 1264 1265 if (dp->dad_na_icount) { 1266 /* 1267 * the check is in nd6_dad_na_input(), 1268 * but just in case 1269 */ 1270 duplicate++; 1271 } 1272 1273 if (dp->dad_ns_icount) { 1274 /* We've seen NS, means DAD has failed. */ 1275 duplicate++; 1276 } 1277 1278 if (duplicate) { 1279 /* (*dp) will be freed in nd6_dad_duplicated() */ 1280 dp = NULL; 1281 nd6_dad_duplicated(ifa); 1282 } else { 1283 /* 1284 * We are done with DAD. No NA came, no NS came. 1285 * No duplicate address found. 1286 */ 1287 ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 1288 1289 nd6log((LOG_DEBUG, 1290 "%s: DAD complete for %s - no duplicates found\n", 1291 if_name(ifa->ifa_ifp), 1292 ip6_sprintf(&ia->ia_addr.sin6_addr))); 1293 1294 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list); 1295 free(dp, M_IP6NDP); 1296 dp = NULL; 1297 IFAFREE(ifa); 1298 } 1299 } 1300 1301done: 1302 splx(s); 1303} 1304 1305void 1306nd6_dad_duplicated(ifa) 1307 struct ifaddr *ifa; 1308{ 1309 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 1310 struct ifnet *ifp; 1311 struct dadq *dp; 1312 1313 dp = nd6_dad_find(ifa); 1314 if (dp == NULL) { 1315 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n"); 1316 return; 1317 } 1318 1319 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: " 1320 "NS in/out=%d/%d, NA in=%d\n", 1321 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr), 1322 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount); 1323 1324 ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 1325 ia->ia6_flags |= IN6_IFF_DUPLICATED; 1326 1327 /* We are done with DAD, with duplicate address found. (failure) */ 1328 nd6_dad_stoptimer(dp); 1329 1330 ifp = ifa->ifa_ifp; 1331 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n", 1332 if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr)); 1333 log(LOG_ERR, "%s: manual intervention required\n", 1334 if_name(ifp)); 1335 1336 /* 1337 * If the address is a link-local address formed from an interface 1338 * identifier based on the hardware address which is supposed to be 1339 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP 1340 * operation on the interface SHOULD be disabled. 1341 * [rfc2462bis-03 Section 5.4.5] 1342 */ 1343 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) { 1344 struct in6_addr in6; 1345 1346 /* 1347 * To avoid over-reaction, we only apply this logic when we are 1348 * very sure that hardware addresses are supposed to be unique. 1349 */ 1350 switch (ifp->if_type) { 1351 case IFT_ETHER: 1352 case IFT_FDDI: 1353 case IFT_ATM: 1354 case IFT_IEEE1394: 1355#ifdef IFT_IEEE80211 1356 case IFT_IEEE80211: 1357#endif 1358 in6 = ia->ia_addr.sin6_addr; 1359 if (in6_get_hw_ifid(ifp, &in6) == 0 && 1360 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) { 1361 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED; 1362 log(LOG_ERR, "%s: possible hardware address " 1363 "duplication detected, disable IPv6\n", 1364 if_name(ifp)); 1365 } 1366 break; 1367 } 1368 } 1369 1370 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list); 1371 free(dp, M_IP6NDP); 1372 dp = NULL; 1373 IFAFREE(ifa); 1374} 1375 1376static void 1377nd6_dad_ns_output(dp, ifa) 1378 struct dadq *dp; 1379 struct ifaddr *ifa; 1380{ 1381 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 1382 struct ifnet *ifp = ifa->ifa_ifp; 1383 1384 dp->dad_ns_tcount++; 1385 if ((ifp->if_flags & IFF_UP) == 0) { 1386#if 0 1387 printf("%s: interface down?\n", if_name(ifp)); 1388#endif 1389 return; 1390 } 1391 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1392#if 0 1393 printf("%s: interface not running?\n", if_name(ifp)); 1394#endif 1395 return; 1396 } 1397 1398 dp->dad_ns_ocount++; 1399 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1); 1400} 1401 1402static void 1403nd6_dad_ns_input(ifa) 1404 struct ifaddr *ifa; 1405{ 1406 struct in6_ifaddr *ia; 1407 struct ifnet *ifp; 1408 const struct in6_addr *taddr6; 1409 struct dadq *dp; 1410 int duplicate; 1411 1412 if (ifa == NULL) 1413 panic("ifa == NULL in nd6_dad_ns_input"); 1414 1415 ia = (struct in6_ifaddr *)ifa; 1416 ifp = ifa->ifa_ifp; 1417 taddr6 = &ia->ia_addr.sin6_addr; 1418 duplicate = 0; 1419 dp = nd6_dad_find(ifa); 1420 1421 /* Quickhack - completely ignore DAD NS packets */ 1422 if (dad_ignore_ns) { 1423 nd6log((LOG_INFO, 1424 "nd6_dad_ns_input: ignoring DAD NS packet for " 1425 "address %s(%s)\n", ip6_sprintf(taddr6), 1426 if_name(ifa->ifa_ifp))); 1427 return; 1428 } 1429 1430 /* 1431 * if I'm yet to start DAD, someone else started using this address 1432 * first. I have a duplicate and you win. 1433 */ 1434 if (dp == NULL || dp->dad_ns_ocount == 0) 1435 duplicate++; 1436 1437 /* XXX more checks for loopback situation - see nd6_dad_timer too */ 1438 1439 if (duplicate) { 1440 dp = NULL; /* will be freed in nd6_dad_duplicated() */ 1441 nd6_dad_duplicated(ifa); 1442 } else { 1443 /* 1444 * not sure if I got a duplicate. 1445 * increment ns count and see what happens. 1446 */ 1447 if (dp) 1448 dp->dad_ns_icount++; 1449 } 1450} 1451 1452static void 1453nd6_dad_na_input(ifa) 1454 struct ifaddr *ifa; 1455{ 1456 struct dadq *dp; 1457 1458 if (ifa == NULL) 1459 panic("ifa == NULL in nd6_dad_na_input"); 1460 1461 dp = nd6_dad_find(ifa); 1462 if (dp) 1463 dp->dad_na_icount++; 1464 1465 /* remove the address. */ 1466 nd6_dad_duplicated(ifa); 1467} 1468