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