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