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