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