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