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