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