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