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