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