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