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