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 "opt_inet.h" 35 #include "opt_inet6.h" 36 #include "opt_ipsec.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/counter.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 #include <machine/atomic.h> 80 81 #define SDL(s) ((struct sockaddr_dl *)s) 82 83 MALLOC_DECLARE(M_IP6NDP); 84 85 struct dadq; 86 static struct dadq *nd6_dad_find(struct ifaddr *, struct nd_opt_nonce *); 87 static void nd6_dad_add(struct dadq *dp); 88 static void nd6_dad_del(struct dadq *dp); 89 static void nd6_dad_rele(struct dadq *); 90 static void nd6_dad_starttimer(struct dadq *, int); 91 static void nd6_dad_stoptimer(struct dadq *); 92 static void nd6_dad_timer(void *); 93 static void nd6_dad_duplicated(struct ifaddr *, struct dadq *); 94 static void nd6_dad_ns_output(struct dadq *); 95 static void nd6_dad_ns_input(struct ifaddr *, struct nd_opt_nonce *); 96 static void nd6_dad_na_input(struct ifaddr *); 97 static void nd6_na_output_fib(struct ifnet *, const struct in6_addr *, 98 const struct in6_addr *, u_long, int, struct sockaddr *, u_int); 99 static void nd6_ns_output_fib(struct ifnet *, const struct in6_addr *, 100 const struct in6_addr *, const struct in6_addr *, uint8_t *, u_int); 101 static void nd6_queue_add(struct ifaddr *, struct in6_addr *, int, uint32_t); 102 103 static struct ifaddr *nd6_proxy_fill_sdl(struct ifnet *, 104 const struct in6_addr *, struct sockaddr_dl *); 105 106 VNET_DEFINE_STATIC(int, dad_enhanced) = 1; 107 #define V_dad_enhanced VNET(dad_enhanced) 108 109 SYSCTL_DECL(_net_inet6_ip6); 110 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, dad_enhanced, CTLFLAG_VNET | CTLFLAG_RW, 111 &VNET_NAME(dad_enhanced), 0, 112 "Enable Enhanced DAD, which adds a random nonce to NS messages for DAD."); 113 114 VNET_DEFINE_STATIC(int, dad_maxtry) = 15; /* max # of *tries* to 115 transmit DAD packet */ 116 #define V_dad_maxtry VNET(dad_maxtry) 117 118 VNET_DEFINE_STATIC(int, nd6_onlink_ns_rfc4861) = 0; 119 #define V_nd6_onlink_ns_rfc4861 VNET(nd6_onlink_ns_rfc4861) 120 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_ONLINKNSRFC4861, 121 nd6_onlink_ns_rfc4861, CTLFLAG_VNET | CTLFLAG_RW, 122 &VNET_NAME(nd6_onlink_ns_rfc4861), 0, 123 "Accept 'on-link' ICMPv6 NS messages in compliance with RFC 4861"); 124 125 struct nd_queue { 126 TAILQ_ENTRY(nd_queue) ndq_list; 127 struct ifaddr *ndq_ifa; 128 struct in6_addr ndq_daddr; 129 uint32_t ndq_flags; 130 struct callout ndq_callout; 131 }; 132 133 /* 134 * Input a Neighbor Solicitation Message. 135 * 136 * Based on RFC 2461 137 * Based on RFC 2462 (duplicate address detection) 138 */ 139 void 140 nd6_ns_input(struct mbuf *m, int off, int icmp6len) 141 { 142 struct ifnet *ifp; 143 struct ip6_hdr *ip6; 144 struct nd_neighbor_solicit *nd_ns; 145 struct in6_addr daddr6, myaddr6, saddr6, taddr6; 146 struct ifaddr *ifa; 147 struct sockaddr_dl proxydl; 148 union nd_opts ndopts; 149 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 150 char *lladdr; 151 int anycast, lladdrlen, proxy, rflag, tentative, tlladdr; 152 153 ifa = NULL; 154 155 /* RFC 6980: Nodes MUST silently ignore fragments */ 156 if(m->m_flags & M_FRAGMENTED) 157 goto freeit; 158 159 ifp = m->m_pkthdr.rcvif; 160 ip6 = mtod(m, struct ip6_hdr *); 161 if (__predict_false(ip6->ip6_hlim != 255)) { 162 ICMP6STAT_INC(icp6s_invlhlim); 163 nd6log((LOG_ERR, 164 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n", 165 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src), 166 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp))); 167 goto bads; 168 } 169 170 if (m->m_len < off + icmp6len) { 171 m = m_pullup(m, off + icmp6len); 172 if (m == NULL) { 173 IP6STAT_INC(ip6s_exthdrtoolong); 174 return; 175 } 176 } 177 ip6 = mtod(m, struct ip6_hdr *); 178 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off); 179 180 saddr6 = ip6->ip6_src; 181 daddr6 = ip6->ip6_dst; 182 taddr6 = nd_ns->nd_ns_target; 183 if (in6_setscope(&taddr6, ifp, NULL) != 0) 184 goto bad; 185 186 rflag = (V_ip6_forwarding) ? ND_NA_FLAG_ROUTER : 0; 187 if (ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV && V_ip6_norbit_raif) 188 rflag = 0; 189 190 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) { 191 /* dst has to be a solicited node multicast address. */ 192 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL && 193 /* don't check ifindex portion */ 194 daddr6.s6_addr32[1] == 0 && 195 daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE && 196 daddr6.s6_addr8[12] == 0xff) { 197 ; /* good */ 198 } else { 199 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet " 200 "(wrong ip6 dst)\n")); 201 goto bad; 202 } 203 } else if (!V_nd6_onlink_ns_rfc4861) { 204 struct sockaddr_in6 src_sa6; 205 206 /* 207 * According to recent IETF discussions, it is not a good idea 208 * to accept a NS from an address which would not be deemed 209 * to be a neighbor otherwise. This point is expected to be 210 * clarified in future revisions of the specification. 211 */ 212 bzero(&src_sa6, sizeof(src_sa6)); 213 src_sa6.sin6_family = AF_INET6; 214 src_sa6.sin6_len = sizeof(src_sa6); 215 src_sa6.sin6_addr = saddr6; 216 if (nd6_is_addr_neighbor(&src_sa6, ifp) == 0) { 217 nd6log((LOG_INFO, "nd6_ns_input: " 218 "NS packet from non-neighbor\n")); 219 goto bad; 220 } 221 } 222 223 if (IN6_IS_ADDR_MULTICAST(&taddr6)) { 224 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n")); 225 goto bad; 226 } 227 228 icmp6len -= sizeof(*nd_ns); 229 nd6_option_init(nd_ns + 1, icmp6len, &ndopts); 230 if (nd6_options(&ndopts) < 0) { 231 nd6log((LOG_INFO, 232 "nd6_ns_input: invalid ND option, ignored\n")); 233 /* nd6_options have incremented stats */ 234 goto freeit; 235 } 236 237 lladdr = NULL; 238 lladdrlen = 0; 239 if (ndopts.nd_opts_src_lladdr) { 240 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 241 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 242 } 243 244 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) { 245 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet " 246 "(link-layer address option)\n")); 247 goto bad; 248 } 249 250 /* 251 * Attaching target link-layer address to the NA? 252 * (RFC 2461 7.2.4) 253 * 254 * NS IP dst is unicast/anycast MUST NOT add 255 * NS IP dst is solicited-node multicast MUST add 256 * 257 * In implementation, we add target link-layer address by default. 258 * We do not add one in MUST NOT cases. 259 */ 260 tlladdr = 0; 261 if (IN6_IS_ADDR_MULTICAST(&daddr6)) 262 tlladdr |= ND6_NA_OPT_LLA; 263 264 /* 265 * Target address (taddr6) must be either: 266 * (1) Valid unicast/anycast address for my receiving interface, 267 * (2) Unicast address for which I'm offering proxy service, or 268 * (3) "tentative" address on which DAD is being performed. 269 */ 270 /* (1) and (3) check. */ 271 if (ifp->if_carp) { 272 ifa = (*carp_iamatch6_p)(ifp, &taddr6); 273 if (ifa != NULL) 274 tlladdr |= ND6_NA_CARP_MASTER; 275 } else 276 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); 277 278 /* (2) check. */ 279 proxy = 0; 280 if (ifa == NULL) { 281 if ((ifa = nd6_proxy_fill_sdl(ifp, &taddr6, &proxydl)) != NULL) 282 proxy = 1; 283 } 284 if (ifa == NULL) { 285 /* 286 * We've got an NS packet, and we don't have that address 287 * assigned for us. We MUST silently ignore it. 288 * See RFC2461 7.2.3. 289 */ 290 goto freeit; 291 } 292 myaddr6 = *IFA_IN6(ifa); 293 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST; 294 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE; 295 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED) 296 goto freeit; 297 298 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 299 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s " 300 "(if %d, NS packet %d)\n", 301 ip6_sprintf(ip6bufs, &taddr6), 302 ifp->if_addrlen, lladdrlen - 2)); 303 goto bad; 304 } 305 306 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) { 307 nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n", 308 ip6_sprintf(ip6bufs, &saddr6))); 309 goto freeit; 310 } 311 312 /* 313 * We have neighbor solicitation packet, with target address equals to 314 * one of my tentative address. 315 * 316 * src addr how to process? 317 * --- --- 318 * multicast of course, invalid (rejected in ip6_input) 319 * unicast somebody is doing address resolution -> ignore 320 * unspec dup address detection 321 * 322 * The processing is defined in RFC 2462. 323 */ 324 if (tentative) { 325 /* 326 * If source address is unspecified address, it is for 327 * duplicate address detection. 328 * 329 * If not, the packet is for addess resolution; 330 * silently ignore it. 331 */ 332 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 333 nd6_dad_ns_input(ifa, ndopts.nd_opts_nonce); 334 335 goto freeit; 336 } 337 338 /* 339 * If the Target Address is either an anycast address or a unicast 340 * address for which the node is providing proxy service, or the Target 341 * Link-Layer Address option is not included, the Override flag SHOULD 342 * be set to zero. Otherwise, the Override flag SHOULD be set to one. 343 */ 344 if (anycast == 0 && proxy == 0 && (tlladdr & ND6_NA_OPT_LLA) != 0) 345 rflag |= ND_NA_FLAG_OVERRIDE; 346 /* 347 * If the source address is unspecified address, entries must not 348 * be created or updated. 349 * It looks that sender is performing DAD. nd6_na_output() will 350 * send NA toward all-node multicast address, to tell the sender 351 * that I'm using the address. 352 * S bit ("solicited") must be zero. 353 */ 354 if (!IN6_IS_ADDR_UNSPECIFIED(&saddr6)) { 355 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, 356 ND_NEIGHBOR_SOLICIT, 0); 357 rflag |= ND_NA_FLAG_SOLICITED; 358 } 359 360 /* 361 * RFC 4861, anycast or proxy NA sent in response to a NS SHOULD 362 * be delayed by a random time between 0 and MAX_ANYCAST_DELAY_TIME 363 * to reduce the probability of network congestion. 364 */ 365 if (anycast == 0) 366 nd6_na_output_fib(ifp, &saddr6, &taddr6, rflag, tlladdr, 367 proxy ? (struct sockaddr *)&proxydl : NULL, M_GETFIB(m)); 368 else 369 nd6_queue_add(ifa, &saddr6, arc4random() % 370 (MAX_ANYCAST_DELAY_TIME * hz), ND6_QUEUE_FLAG_ANYCAST); 371 freeit: 372 if (ifa != NULL) 373 ifa_free(ifa); 374 m_freem(m); 375 return; 376 377 bad: 378 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", 379 ip6_sprintf(ip6bufs, &saddr6))); 380 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", 381 ip6_sprintf(ip6bufs, &daddr6))); 382 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", 383 ip6_sprintf(ip6bufs, &taddr6))); 384 bads: 385 ICMP6STAT_INC(icp6s_badns); 386 if (ifa != NULL) 387 ifa_free(ifa); 388 m_freem(m); 389 } 390 391 static struct ifaddr * 392 nd6_proxy_fill_sdl(struct ifnet *ifp, const struct in6_addr *taddr6, 393 struct sockaddr_dl *sdl) 394 { 395 struct ifaddr *ifa; 396 struct llentry *ln; 397 398 ifa = NULL; 399 ln = nd6_lookup(taddr6, LLE_SF(AF_INET6, 0), ifp); 400 if (ln == NULL) 401 return (ifa); 402 if ((ln->la_flags & (LLE_PUB | LLE_VALID)) == (LLE_PUB | LLE_VALID)) { 403 link_init_sdl(ifp, (struct sockaddr *)sdl, ifp->if_type); 404 sdl->sdl_alen = ifp->if_addrlen; 405 bcopy(ln->ll_addr, &sdl->sdl_data, ifp->if_addrlen); 406 LLE_RUNLOCK(ln); 407 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 408 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST); 409 } else 410 LLE_RUNLOCK(ln); 411 412 return (ifa); 413 } 414 415 /* 416 * Output a Neighbor Solicitation Message. Caller specifies: 417 * - ICMP6 header source IP6 address 418 * - ND6 header target IP6 address 419 * - ND6 header source datalink address 420 * 421 * Based on RFC 2461 422 * Based on RFC 2462 (duplicate address detection) 423 * 424 * ln - for source address determination 425 * nonce - If non-NULL, NS is used for duplicate address detection and 426 * the value (length is ND_OPT_NONCE_LEN) is used as a random nonce. 427 */ 428 static void 429 nd6_ns_output_fib(struct ifnet *ifp, const struct in6_addr *saddr6, 430 const struct in6_addr *daddr6, const struct in6_addr *taddr6, 431 uint8_t *nonce, u_int fibnum) 432 { 433 struct mbuf *m; 434 struct m_tag *mtag; 435 struct ip6_hdr *ip6; 436 struct nd_neighbor_solicit *nd_ns; 437 struct ip6_moptions im6o; 438 int icmp6len; 439 int maxlen; 440 441 NET_EPOCH_ASSERT(); 442 443 if (IN6_IS_ADDR_MULTICAST(taddr6)) 444 return; 445 446 /* estimate the size of message */ 447 maxlen = sizeof(*ip6) + sizeof(*nd_ns); 448 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7; 449 KASSERT(max_linkhdr + maxlen <= MCLBYTES, ( 450 "%s: max_linkhdr + maxlen > MCLBYTES (%d + %d > %d)", 451 __func__, max_linkhdr, maxlen, MCLBYTES)); 452 453 if (max_linkhdr + maxlen > MHLEN) 454 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 455 else 456 m = m_gethdr(M_NOWAIT, MT_DATA); 457 if (m == NULL) 458 return; 459 M_SETFIB(m, fibnum); 460 461 icmp6len = sizeof(*nd_ns); 462 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len; 463 m->m_data += max_linkhdr; /* or M_ALIGN() equivalent? */ 464 465 /* fill neighbor solicitation packet */ 466 ip6 = mtod(m, struct ip6_hdr *); 467 ip6->ip6_flow = 0; 468 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 469 ip6->ip6_vfc |= IPV6_VERSION; 470 /* ip6->ip6_plen will be set later */ 471 ip6->ip6_nxt = IPPROTO_ICMPV6; 472 ip6->ip6_hlim = 255; 473 if (daddr6) 474 ip6->ip6_dst = *daddr6; 475 else { 476 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL; 477 ip6->ip6_dst.s6_addr16[1] = 0; 478 ip6->ip6_dst.s6_addr32[1] = 0; 479 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE; 480 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3]; 481 ip6->ip6_dst.s6_addr8[12] = 0xff; 482 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 483 goto bad; 484 } 485 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 486 m->m_flags |= M_MCAST; 487 im6o.im6o_multicast_ifp = ifp; 488 im6o.im6o_multicast_hlim = 255; 489 im6o.im6o_multicast_loop = 0; 490 } 491 if (nonce == NULL) { 492 char ip6buf[INET6_ADDRSTRLEN]; 493 struct ifaddr *ifa = NULL; 494 495 /* 496 * RFC2461 7.2.2: 497 * "If the source address of the packet prompting the 498 * solicitation is the same as one of the addresses assigned 499 * to the outgoing interface, that address SHOULD be placed 500 * in the IP Source Address of the outgoing solicitation. 501 * Otherwise, any one of the addresses assigned to the 502 * interface should be used." 503 * 504 * We use the source address for the prompting packet 505 * (saddr6), if saddr6 belongs to the outgoing interface. 506 * Otherwise, we perform the source address selection as usual. 507 */ 508 if (saddr6 != NULL) 509 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, saddr6); 510 if (ifa == NULL) { 511 int error; 512 513 error = in6_selectsrc_nbr(fibnum, &ip6->ip6_dst, &im6o, 514 ifp, &ip6->ip6_src); 515 if (error) { 516 nd6log((LOG_DEBUG, "%s: source can't be " 517 "determined: dst=%s, error=%d\n", __func__, 518 ip6_sprintf(ip6buf, &ip6->ip6_dst), 519 error)); 520 goto bad; 521 } 522 } else 523 ip6->ip6_src = *saddr6; 524 525 if (ifp->if_carp != NULL) { 526 /* 527 * Check that selected source address belongs to 528 * CARP addresses. 529 */ 530 if (ifa == NULL) 531 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, 532 &ip6->ip6_src); 533 /* 534 * Do not send NS for CARP address if we are not 535 * the CARP master. 536 */ 537 if (ifa != NULL && ifa->ifa_carp != NULL && 538 !(*carp_master_p)(ifa)) { 539 nd6log((LOG_DEBUG, 540 "nd6_ns_output: NS from BACKUP CARP address %s\n", 541 ip6_sprintf(ip6buf, &ip6->ip6_src))); 542 ifa_free(ifa); 543 goto bad; 544 } 545 } 546 if (ifa != NULL) 547 ifa_free(ifa); 548 } else { 549 /* 550 * Source address for DAD packet must always be IPv6 551 * unspecified address. (0::0) 552 * We actually don't have to 0-clear the address (we did it 553 * above), but we do so here explicitly to make the intention 554 * clearer. 555 */ 556 bzero(&ip6->ip6_src, sizeof(ip6->ip6_src)); 557 } 558 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1); 559 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT; 560 nd_ns->nd_ns_code = 0; 561 nd_ns->nd_ns_reserved = 0; 562 nd_ns->nd_ns_target = *taddr6; 563 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */ 564 565 /* 566 * Add source link-layer address option. 567 * 568 * spec implementation 569 * --- --- 570 * DAD packet MUST NOT do not add the option 571 * there's no link layer address: 572 * impossible do not add the option 573 * there's link layer address: 574 * Multicast NS MUST add one add the option 575 * Unicast NS SHOULD add one add the option 576 */ 577 if (nonce == NULL) { 578 struct nd_opt_hdr *nd_opt; 579 char *mac; 580 int optlen; 581 582 mac = NULL; 583 if (ifp->if_carp) 584 mac = (*carp_macmatch6_p)(ifp, m, &ip6->ip6_src); 585 if (mac == NULL) 586 mac = nd6_ifptomac(ifp); 587 588 if (mac != NULL) { 589 nd_opt = (struct nd_opt_hdr *)(nd_ns + 1); 590 optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen; 591 /* 8 byte alignments... */ 592 optlen = (optlen + 7) & ~7; 593 m->m_pkthdr.len += optlen; 594 m->m_len += optlen; 595 icmp6len += optlen; 596 bzero(nd_opt, optlen); 597 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; 598 nd_opt->nd_opt_len = optlen >> 3; 599 bcopy(mac, nd_opt + 1, ifp->if_addrlen); 600 } 601 } 602 /* 603 * Add a Nonce option (RFC 3971) to detect looped back NS messages. 604 * This behavior is documented as Enhanced Duplicate Address 605 * Detection in RFC 7527. 606 * net.inet6.ip6.dad_enhanced=0 disables this. 607 */ 608 if (V_dad_enhanced != 0 && nonce != NULL) { 609 int optlen = sizeof(struct nd_opt_hdr) + ND_OPT_NONCE_LEN; 610 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1); 611 /* 8-byte alignment is required. */ 612 optlen = (optlen + 7) & ~7; 613 614 m->m_pkthdr.len += optlen; 615 m->m_len += optlen; 616 icmp6len += optlen; 617 bzero((caddr_t)nd_opt, optlen); 618 nd_opt->nd_opt_type = ND_OPT_NONCE; 619 nd_opt->nd_opt_len = optlen >> 3; 620 bcopy(nonce, (caddr_t)(nd_opt + 1), ND_OPT_NONCE_LEN); 621 } 622 ip6->ip6_plen = htons((u_short)icmp6len); 623 nd_ns->nd_ns_cksum = 0; 624 nd_ns->nd_ns_cksum = 625 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len); 626 627 if (send_sendso_input_hook != NULL) { 628 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, 629 sizeof(unsigned short), M_NOWAIT); 630 if (mtag == NULL) 631 goto bad; 632 *(unsigned short *)(mtag + 1) = nd_ns->nd_ns_type; 633 m_tag_prepend(m, mtag); 634 } 635 636 ip6_output(m, NULL, NULL, (nonce != NULL) ? IPV6_UNSPECSRC : 0, 637 &im6o, NULL, NULL); 638 icmp6_ifstat_inc(ifp, ifs6_out_msg); 639 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit); 640 ICMP6STAT_INC2(icp6s_outhist, ND_NEIGHBOR_SOLICIT); 641 642 return; 643 644 bad: 645 m_freem(m); 646 } 647 648 #ifndef BURN_BRIDGES 649 void 650 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *saddr6, 651 const struct in6_addr *daddr6, const struct in6_addr *taddr6,uint8_t *nonce) 652 { 653 654 nd6_ns_output_fib(ifp, saddr6, daddr6, taddr6, nonce, RT_DEFAULT_FIB); 655 } 656 #endif 657 /* 658 * Neighbor advertisement input handling. 659 * 660 * Based on RFC 2461 661 * Based on RFC 2462 (duplicate address detection) 662 */ 663 void 664 nd6_na_input(struct mbuf *m, int off, int icmp6len) 665 { 666 struct ifnet *ifp; 667 struct ip6_hdr *ip6; 668 struct ifaddr *ifa; 669 struct llentry *ln; 670 struct mbuf *chain; 671 struct nd_neighbor_advert *nd_na; 672 struct in6_addr daddr6, taddr6; 673 union nd_opts ndopts; 674 u_char linkhdr[LLE_MAX_LINKHDR]; 675 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 676 char *lladdr; 677 size_t linkhdrsize; 678 int flags, is_override, is_router, is_solicited; 679 int lladdr_off, lladdrlen, checklink; 680 bool flush_holdchain = false; 681 682 NET_EPOCH_ASSERT(); 683 684 chain = NULL; 685 ln = NULL; 686 checklink = 0; 687 688 /* RFC 6980: Nodes MUST silently ignore fragments */ 689 if(m->m_flags & M_FRAGMENTED) 690 goto freeit; 691 692 ifp = m->m_pkthdr.rcvif; 693 ip6 = mtod(m, struct ip6_hdr *); 694 if (__predict_false(ip6->ip6_hlim != 255)) { 695 ICMP6STAT_INC(icp6s_invlhlim); 696 nd6log((LOG_ERR, 697 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n", 698 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src), 699 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp))); 700 goto bad; 701 } 702 703 if (m->m_len < off + icmp6len) { 704 m = m_pullup(m, off + icmp6len); 705 if (m == NULL) { 706 IP6STAT_INC(ip6s_exthdrtoolong); 707 return; 708 } 709 } 710 ip6 = mtod(m, struct ip6_hdr *); 711 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off); 712 713 flags = nd_na->nd_na_flags_reserved; 714 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0); 715 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0); 716 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0); 717 718 taddr6 = nd_na->nd_na_target; 719 if (in6_setscope(&taddr6, ifp, NULL)) 720 goto bad; /* XXX: impossible */ 721 722 if (IN6_IS_ADDR_MULTICAST(&taddr6)) { 723 nd6log((LOG_ERR, 724 "nd6_na_input: invalid target address %s\n", 725 ip6_sprintf(ip6bufs, &taddr6))); 726 goto bad; 727 } 728 729 daddr6 = ip6->ip6_dst; 730 if (IN6_IS_ADDR_MULTICAST(&daddr6)) 731 if (is_solicited) { 732 nd6log((LOG_ERR, 733 "nd6_na_input: a solicited adv is multicasted\n")); 734 goto bad; 735 } 736 737 icmp6len -= sizeof(*nd_na); 738 nd6_option_init(nd_na + 1, icmp6len, &ndopts); 739 if (nd6_options(&ndopts) < 0) { 740 nd6log((LOG_INFO, 741 "nd6_na_input: invalid ND option, ignored\n")); 742 /* nd6_options have incremented stats */ 743 goto freeit; 744 } 745 746 lladdr = NULL; 747 lladdrlen = 0; 748 if (ndopts.nd_opts_tgt_lladdr) { 749 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 750 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 751 } 752 753 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); 754 if (ifa != NULL && ifa->ifa_carp != NULL) { 755 /* 756 * Silently ignore NAs for CARP addresses if we are not 757 * the CARP master. 758 */ 759 if (!(*carp_master_p)(ifa)) { 760 nd6log((LOG_DEBUG, 761 "nd6_na_input: NA for BACKUP CARP address %s\n", 762 ip6_sprintf(ip6bufs, &taddr6))); 763 ifa_free(ifa); 764 goto freeit; 765 } 766 } 767 /* 768 * Target address matches one of my interface address. 769 * 770 * If my address is tentative, this means that there's somebody 771 * already using the same address as mine. This indicates DAD failure. 772 * This is defined in RFC 2462. 773 * 774 * Otherwise, process as defined in RFC 2461. 775 */ 776 if (ifa 777 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) { 778 nd6_dad_na_input(ifa); 779 ifa_free(ifa); 780 goto freeit; 781 } 782 783 /* Just for safety, maybe unnecessary. */ 784 if (ifa) { 785 ifa_free(ifa); 786 log(LOG_ERR, 787 "nd6_na_input: duplicate IP6 address %s\n", 788 ip6_sprintf(ip6bufs, &taddr6)); 789 goto freeit; 790 } 791 792 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 793 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s " 794 "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6), 795 ifp->if_addrlen, lladdrlen - 2)); 796 goto bad; 797 } 798 799 /* 800 * If no neighbor cache entry is found, NA SHOULD silently be 801 * discarded. 802 */ 803 ln = nd6_lookup(&taddr6, LLE_SF(AF_INET6, LLE_EXCLUSIVE), ifp); 804 if (ln == NULL) { 805 goto freeit; 806 } 807 808 /* 809 * Do not try to override static entry. 810 */ 811 if (ln->la_flags & LLE_STATIC) 812 goto freeit; 813 814 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { 815 /* 816 * If the link-layer has address, and no lladdr option came, 817 * discard the packet. 818 */ 819 if (ifp->if_addrlen && lladdr == NULL) { 820 goto freeit; 821 } 822 823 /* 824 * Record link-layer address, and update the state. 825 */ 826 if (!nd6_try_set_entry_addr(ifp, ln, lladdr)) 827 goto freeit; 828 829 flush_holdchain = true; 830 if (is_solicited) 831 nd6_llinfo_setstate(ln, ND6_LLINFO_REACHABLE); 832 else 833 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE); 834 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED); 835 if ((ln->ln_router = is_router) != 0) { 836 /* 837 * This means a router's state has changed from 838 * non-reachable to probably reachable, and might 839 * affect the status of associated prefixes.. 840 */ 841 checklink = 1; 842 } 843 } else { 844 int llchange; 845 846 /* 847 * Check if the link-layer address has changed or not. 848 */ 849 if (lladdr == NULL) 850 llchange = 0; 851 else { 852 if (ln->la_flags & LLE_VALID) { 853 if (bcmp(lladdr, ln->ll_addr, ifp->if_addrlen)) 854 llchange = 1; 855 else 856 llchange = 0; 857 } else 858 llchange = 1; 859 } 860 861 /* 862 * This is VERY complex. Look at it with care. 863 * 864 * override solicit lladdr llchange action 865 * (L: record lladdr) 866 * 867 * 0 0 n -- (2c) 868 * 0 0 y n (2b) L 869 * 0 0 y y (1) REACHABLE->STALE 870 * 0 1 n -- (2c) *->REACHABLE 871 * 0 1 y n (2b) L *->REACHABLE 872 * 0 1 y y (1) REACHABLE->STALE 873 * 1 0 n -- (2a) 874 * 1 0 y n (2a) L 875 * 1 0 y y (2a) L *->STALE 876 * 1 1 n -- (2a) *->REACHABLE 877 * 1 1 y n (2a) L *->REACHABLE 878 * 1 1 y y (2a) L *->REACHABLE 879 */ 880 if (!is_override && (lladdr != NULL && llchange)) { /* (1) */ 881 /* 882 * If state is REACHABLE, make it STALE. 883 * no other updates should be done. 884 */ 885 if (ln->ln_state == ND6_LLINFO_REACHABLE) 886 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE); 887 goto freeit; 888 } else if (is_override /* (2a) */ 889 || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */ 890 || lladdr == NULL) { /* (2c) */ 891 /* 892 * Update link-local address, if any. 893 */ 894 if (lladdr != NULL) { 895 linkhdrsize = sizeof(linkhdr); 896 if (lltable_calc_llheader(ifp, AF_INET6, lladdr, 897 linkhdr, &linkhdrsize, &lladdr_off) != 0) 898 goto freeit; 899 if (lltable_try_set_entry_addr(ifp, ln, linkhdr, 900 linkhdrsize, lladdr_off) == 0) 901 goto freeit; 902 EVENTHANDLER_INVOKE(lle_event, ln, 903 LLENTRY_RESOLVED); 904 } 905 906 /* 907 * If solicited, make the state REACHABLE. 908 * If not solicited and the link-layer address was 909 * changed, make it STALE. 910 */ 911 if (is_solicited) 912 nd6_llinfo_setstate(ln, ND6_LLINFO_REACHABLE); 913 else { 914 if (lladdr != NULL && llchange) 915 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE); 916 } 917 } 918 919 if (ln->ln_router && !is_router) { 920 /* 921 * The peer dropped the router flag. 922 * Remove the sender from the Default Router List and 923 * update the Destination Cache entries. 924 */ 925 struct ifnet *nd6_ifp; 926 927 nd6_ifp = lltable_get_ifp(ln->lle_tbl); 928 if (!defrouter_remove(&ln->r_l3addr.addr6, nd6_ifp) && 929 (nd6_ifp->if_inet6->nd_flags & 930 ND6_IFF_ACCEPT_RTADV) != 0) 931 /* 932 * Even if the neighbor is not in the default 933 * router list, the neighbor may be used as a 934 * next hop for some destinations (e.g. redirect 935 * case). So we must call rt6_flush explicitly. 936 */ 937 rt6_flush(&ip6->ip6_src, ifp); 938 } 939 ln->ln_router = is_router; 940 } 941 /* XXX - QL 942 * Does this matter? 943 * rt->rt_flags &= ~RTF_REJECT; 944 */ 945 ln->la_asked = 0; 946 if (ln->la_hold != NULL) 947 chain = nd6_grab_holdchain(ln); 948 freeit: 949 if (ln != NULL) 950 LLE_WUNLOCK(ln); 951 952 if (chain != NULL) 953 nd6_flush_holdchain(ifp, ln, chain); 954 if (flush_holdchain) 955 nd6_flush_children_holdchain(ifp, ln); 956 957 if (checklink) 958 pfxlist_onlink_check(); 959 960 m_freem(m); 961 return; 962 963 bad: 964 if (ln != NULL) 965 LLE_WUNLOCK(ln); 966 967 ICMP6STAT_INC(icp6s_badna); 968 m_freem(m); 969 } 970 971 /* 972 * Neighbor advertisement output handling. 973 * 974 * Based on RFC 2461 975 * 976 * tlladdr: 977 * - 0x01 if include target link-layer address 978 * - 0x02 if target address is CARP MASTER 979 * sdl0 - sockaddr_dl (= proxy NA) or NULL 980 */ 981 static void 982 nd6_na_output_fib(struct ifnet *ifp, const struct in6_addr *daddr6_0, 983 const struct in6_addr *taddr6, u_long flags, int tlladdr, 984 struct sockaddr *sdl0, u_int fibnum) 985 { 986 struct mbuf *m; 987 struct m_tag *mtag; 988 struct ip6_hdr *ip6; 989 struct nd_neighbor_advert *nd_na; 990 struct ip6_moptions im6o; 991 struct in6_addr daddr6; 992 993 NET_EPOCH_ASSERT(); 994 995 int icmp6len, maxlen, error; 996 caddr_t mac = NULL; 997 998 daddr6 = *daddr6_0; /* make a local copy for modification */ 999 1000 /* estimate the size of message */ 1001 maxlen = sizeof(*ip6) + sizeof(*nd_na); 1002 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7; 1003 KASSERT(max_linkhdr + maxlen <= MCLBYTES, ( 1004 "%s: max_linkhdr + maxlen > MCLBYTES (%d + %d > %d)", 1005 __func__, max_linkhdr, maxlen, MCLBYTES)); 1006 1007 if (max_linkhdr + maxlen > MHLEN) 1008 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1009 else 1010 m = m_gethdr(M_NOWAIT, MT_DATA); 1011 if (m == NULL) 1012 return; 1013 M_SETFIB(m, fibnum); 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 1027 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) { 1028 /* reply to DAD */ 1029 daddr6 = in6addr_linklocal_allnodes; 1030 if (in6_setscope(&daddr6, ifp, NULL)) 1031 goto bad; 1032 1033 flags &= ~ND_NA_FLAG_SOLICITED; 1034 } 1035 if (IN6_IS_ADDR_MULTICAST(&daddr6)) { 1036 m->m_flags |= M_MCAST; 1037 im6o.im6o_multicast_ifp = ifp; 1038 im6o.im6o_multicast_hlim = 255; 1039 im6o.im6o_multicast_loop = 0; 1040 } 1041 1042 ip6->ip6_dst = daddr6; 1043 error = in6_selectsrc_nbr(fibnum, &daddr6, &im6o, ifp, &ip6->ip6_src); 1044 if (error) { 1045 char ip6buf[INET6_ADDRSTRLEN]; 1046 nd6log((LOG_DEBUG, "nd6_na_output: source can't be " 1047 "determined: dst=%s, error=%d\n", 1048 ip6_sprintf(ip6buf, &daddr6), error)); 1049 goto bad; 1050 } 1051 nd_na = (struct nd_neighbor_advert *)(ip6 + 1); 1052 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT; 1053 nd_na->nd_na_code = 0; 1054 nd_na->nd_na_target = *taddr6; 1055 in6_clearscope(&nd_na->nd_na_target); /* XXX */ 1056 1057 /* 1058 * If we respond from CARP address, we need to prepare mac address 1059 * for carp_output(). 1060 */ 1061 if (ifp->if_carp && (tlladdr & ND6_NA_CARP_MASTER)) 1062 mac = (*carp_macmatch6_p)(ifp, m, taddr6); 1063 /* 1064 * "tlladdr" indicates NS's condition for adding tlladdr or not. 1065 * see nd6_ns_input() for details. 1066 * Basically, if NS packet is sent to unicast/anycast addr, 1067 * target lladdr option SHOULD NOT be included. 1068 */ 1069 if (tlladdr & ND6_NA_OPT_LLA) { 1070 /* 1071 * sdl0 != NULL indicates proxy NA. If we do proxy, use 1072 * lladdr in sdl0. If we are not proxying (sending NA for 1073 * my address) use lladdr configured for the interface. 1074 */ 1075 if (sdl0 == NULL) { 1076 if (mac == NULL) 1077 mac = nd6_ifptomac(ifp); 1078 } else if (sdl0->sa_family == AF_LINK) { 1079 struct sockaddr_dl *sdl; 1080 sdl = (struct sockaddr_dl *)sdl0; 1081 if (sdl->sdl_alen == ifp->if_addrlen) 1082 mac = LLADDR(sdl); 1083 } 1084 } 1085 if ((tlladdr & ND6_NA_OPT_LLA) && mac != NULL) { 1086 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen; 1087 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1); 1088 1089 /* roundup to 8 bytes alignment! */ 1090 optlen = (optlen + 7) & ~7; 1091 1092 m->m_pkthdr.len += optlen; 1093 m->m_len += optlen; 1094 icmp6len += optlen; 1095 bzero((caddr_t)nd_opt, optlen); 1096 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 1097 nd_opt->nd_opt_len = optlen >> 3; 1098 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen); 1099 } else 1100 flags &= ~ND_NA_FLAG_OVERRIDE; 1101 1102 ip6->ip6_plen = htons((u_short)icmp6len); 1103 nd_na->nd_na_flags_reserved = flags; 1104 nd_na->nd_na_cksum = 0; 1105 nd_na->nd_na_cksum = 1106 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len); 1107 1108 if (send_sendso_input_hook != NULL) { 1109 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, 1110 sizeof(unsigned short), M_NOWAIT); 1111 if (mtag == NULL) 1112 goto bad; 1113 *(unsigned short *)(mtag + 1) = nd_na->nd_na_type; 1114 m_tag_prepend(m, mtag); 1115 } 1116 1117 ip6_output(m, NULL, NULL, 0, &im6o, NULL, NULL); 1118 icmp6_ifstat_inc(ifp, ifs6_out_msg); 1119 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert); 1120 ICMP6STAT_INC2(icp6s_outhist, ND_NEIGHBOR_ADVERT); 1121 1122 return; 1123 1124 bad: 1125 m_freem(m); 1126 } 1127 1128 #ifndef BURN_BRIDGES 1129 void 1130 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0, 1131 const struct in6_addr *taddr6, u_long flags, int tlladdr, 1132 struct sockaddr *sdl0) 1133 { 1134 1135 nd6_na_output_fib(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0, 1136 RT_DEFAULT_FIB); 1137 } 1138 #endif 1139 1140 caddr_t 1141 nd6_ifptomac(struct ifnet *ifp) 1142 { 1143 switch (ifp->if_type) { 1144 case IFT_ETHER: 1145 case IFT_IEEE1394: 1146 case IFT_L2VLAN: 1147 case IFT_INFINIBAND: 1148 case IFT_BRIDGE: 1149 return IF_LLADDR(ifp); 1150 default: 1151 return NULL; 1152 } 1153 } 1154 1155 struct dadq { 1156 TAILQ_ENTRY(dadq) dad_list; 1157 struct ifaddr *dad_ifa; 1158 int dad_count; /* max NS to send */ 1159 int dad_ns_tcount; /* # of trials to send NS */ 1160 int dad_ns_ocount; /* NS sent so far */ 1161 int dad_ns_icount; 1162 int dad_na_icount; 1163 int dad_ns_lcount; /* looped back NS */ 1164 int dad_loopbackprobe; /* probing state for loopback detection */ 1165 struct callout dad_timer_ch; 1166 struct vnet *dad_vnet; 1167 u_int dad_refcnt; 1168 #define ND_OPT_NONCE_LEN32 \ 1169 ((ND_OPT_NONCE_LEN + sizeof(uint32_t) - 1)/sizeof(uint32_t)) 1170 uint32_t dad_nonce[ND_OPT_NONCE_LEN32]; 1171 bool dad_ondadq; /* on dadq? Protected by DADQ_WLOCK. */ 1172 }; 1173 1174 VNET_DEFINE_STATIC(TAILQ_HEAD(, dadq), dadq); 1175 VNET_DEFINE_STATIC(struct rwlock, dad_rwlock); 1176 #define V_dadq VNET(dadq) 1177 #define V_dad_rwlock VNET(dad_rwlock) 1178 1179 #define DADQ_LOCKPTR() (&V_dad_rwlock) 1180 #define DADQ_LOCK_INIT() rw_init(DADQ_LOCKPTR(), "nd6 DAD queue") 1181 #define DADQ_RLOCK() rw_rlock(DADQ_LOCKPTR()) 1182 #define DADQ_RUNLOCK() rw_runlock(DADQ_LOCKPTR()) 1183 #define DADQ_WLOCK() rw_wlock(DADQ_LOCKPTR()) 1184 #define DADQ_WUNLOCK() rw_wunlock(DADQ_LOCKPTR()) 1185 1186 #define DADQ_LOCK_ASSERT() rw_assert(DADQ_LOCKPTR(), RA_LOCKED); 1187 #define DADQ_RLOCK_ASSERT() rw_assert(DADQ_LOCKPTR(), RA_RLOCKED); 1188 #define DADQ_WLOCK_ASSERT() rw_assert(DADQ_LOCKPTR(), RA_WLOCKED); 1189 1190 static void 1191 nd6_dad_add(struct dadq *dp) 1192 { 1193 DADQ_WLOCK_ASSERT(); 1194 1195 TAILQ_INSERT_TAIL(&V_dadq, dp, dad_list); 1196 dp->dad_ondadq = true; 1197 } 1198 1199 static void 1200 nd6_dad_del(struct dadq *dp) 1201 { 1202 DADQ_WLOCK_ASSERT(); 1203 1204 if (dp->dad_ondadq) { 1205 /* 1206 * Remove dp from the dadq and release the dadq's 1207 * reference. 1208 */ 1209 TAILQ_REMOVE(&V_dadq, dp, dad_list); 1210 dp->dad_ondadq = false; 1211 nd6_dad_rele(dp); 1212 } 1213 } 1214 1215 static struct dadq * 1216 nd6_dad_find(struct ifaddr *ifa, struct nd_opt_nonce *n) 1217 { 1218 struct dadq *dp; 1219 1220 DADQ_LOCK_ASSERT(); 1221 1222 TAILQ_FOREACH(dp, &V_dadq, dad_list) { 1223 if (dp->dad_ifa != ifa) 1224 continue; 1225 1226 /* 1227 * Skip if the nonce matches the received one. 1228 * +2 in the length is required because of type and 1229 * length fields are included in a header. 1230 */ 1231 if (n != NULL && 1232 n->nd_opt_nonce_len == (ND_OPT_NONCE_LEN + 2) / 8 && 1233 memcmp(&n->nd_opt_nonce[0], &dp->dad_nonce[0], 1234 ND_OPT_NONCE_LEN) == 0) { 1235 dp->dad_ns_lcount++; 1236 continue; 1237 } 1238 break; 1239 } 1240 1241 return (dp); 1242 } 1243 1244 static void 1245 nd6_dad_starttimer(struct dadq *dp, int ticks) 1246 { 1247 DADQ_WLOCK_ASSERT(); 1248 1249 callout_reset(&dp->dad_timer_ch, ticks, nd6_dad_timer, dp); 1250 } 1251 1252 static void 1253 nd6_dad_stoptimer(struct dadq *dp) 1254 { 1255 callout_drain(&dp->dad_timer_ch); 1256 } 1257 1258 static void 1259 nd6_dad_rele(struct dadq *dp) 1260 { 1261 if (refcount_release(&dp->dad_refcnt)) { 1262 KASSERT(!dp->dad_ondadq, ("dp %p still on DAD queue", dp)); 1263 ifa_free(dp->dad_ifa); 1264 free(dp, M_IP6NDP); 1265 } 1266 } 1267 1268 void 1269 nd6_dad_init(void) 1270 { 1271 DADQ_LOCK_INIT(); 1272 TAILQ_INIT(&V_dadq); 1273 } 1274 1275 /* 1276 * Start Duplicate Address Detection (DAD) for specified interface address. 1277 */ 1278 void 1279 nd6_dad_start(struct ifaddr *ifa, int delay) 1280 { 1281 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 1282 struct dadq *dp; 1283 char ip6buf[INET6_ADDRSTRLEN]; 1284 1285 KASSERT((ia->ia6_flags & IN6_IFF_TENTATIVE) != 0, 1286 ("starting DAD on non-tentative address %p", ifa)); 1287 1288 /* 1289 * If we don't need DAD, don't do it. 1290 * There are several cases: 1291 * - DAD is disabled globally or on the interface 1292 * - the interface address is anycast 1293 */ 1294 if ((ia->ia6_flags & IN6_IFF_ANYCAST) != 0 || 1295 V_ip6_dad_count == 0 || 1296 (ifa->ifa_ifp->if_inet6->nd_flags & ND6_IFF_NO_DAD) != 0) { 1297 ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 1298 return; 1299 } 1300 if ((ifa->ifa_ifp->if_flags & IFF_UP) == 0 || 1301 (ifa->ifa_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 1302 (ifa->ifa_ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED) != 0) 1303 return; 1304 1305 DADQ_WLOCK(); 1306 if ((dp = nd6_dad_find(ifa, NULL)) != NULL) { 1307 /* 1308 * DAD is already in progress. Let the existing entry 1309 * finish it. 1310 */ 1311 DADQ_WUNLOCK(); 1312 return; 1313 } 1314 1315 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT | M_ZERO); 1316 if (dp == NULL) { 1317 log(LOG_ERR, "nd6_dad_start: memory allocation failed for " 1318 "%s(%s)\n", 1319 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 1320 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1321 return; 1322 } 1323 callout_init_rw(&dp->dad_timer_ch, DADQ_LOCKPTR(), 1324 CALLOUT_RETURNUNLOCKED); 1325 #ifdef VIMAGE 1326 dp->dad_vnet = curvnet; 1327 #endif 1328 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp), 1329 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 1330 1331 /* 1332 * Send NS packet for DAD, ip6_dad_count times. 1333 * Note that we must delay the first transmission, if this is the 1334 * first packet to be sent from the interface after interface 1335 * (re)initialization. 1336 */ 1337 dp->dad_ifa = ifa; 1338 ifa_ref(dp->dad_ifa); 1339 dp->dad_count = V_ip6_dad_count; 1340 dp->dad_ns_icount = dp->dad_na_icount = 0; 1341 dp->dad_ns_ocount = dp->dad_ns_tcount = 0; 1342 dp->dad_ns_lcount = dp->dad_loopbackprobe = 0; 1343 1344 /* Add this to the dadq and add a reference for the dadq. */ 1345 refcount_init(&dp->dad_refcnt, 1); 1346 nd6_dad_add(dp); 1347 nd6_dad_starttimer(dp, delay); 1348 DADQ_WUNLOCK(); 1349 } 1350 1351 /* 1352 * terminate DAD unconditionally. used for address removals. 1353 */ 1354 void 1355 nd6_dad_stop(struct ifaddr *ifa) 1356 { 1357 struct dadq *dp; 1358 1359 DADQ_WLOCK(); 1360 dp = nd6_dad_find(ifa, NULL); 1361 if (dp == NULL) { 1362 DADQ_WUNLOCK(); 1363 /* DAD wasn't started yet */ 1364 return; 1365 } 1366 1367 /* 1368 * Acquire a temporary reference so that we can safely stop the callout. 1369 */ 1370 (void)refcount_acquire(&dp->dad_refcnt); 1371 nd6_dad_del(dp); 1372 DADQ_WUNLOCK(); 1373 1374 nd6_dad_stoptimer(dp); 1375 nd6_dad_rele(dp); 1376 } 1377 1378 static void 1379 nd6_dad_timer(void *arg) 1380 { 1381 struct dadq *dp = arg; 1382 struct ifaddr *ifa = dp->dad_ifa; 1383 struct ifnet *ifp = dp->dad_ifa->ifa_ifp; 1384 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 1385 char ip6buf[INET6_ADDRSTRLEN]; 1386 struct epoch_tracker et; 1387 1388 CURVNET_SET(dp->dad_vnet); 1389 KASSERT(ia != NULL, ("DAD entry %p with no address", dp)); 1390 1391 NET_EPOCH_ENTER(et); 1392 if (ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED) { 1393 /* Do not need DAD for ifdisabled interface. */ 1394 log(LOG_ERR, "nd6_dad_timer: cancel DAD on %s because of " 1395 "ND6_IFF_IFDISABLED.\n", ifp->if_xname); 1396 goto err; 1397 } 1398 if (ia->ia6_flags & IN6_IFF_DUPLICATED) { 1399 log(LOG_ERR, "nd6_dad_timer: called with duplicated address " 1400 "%s(%s)\n", 1401 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 1402 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1403 goto err; 1404 } 1405 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) { 1406 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address " 1407 "%s(%s)\n", 1408 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 1409 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1410 goto err; 1411 } 1412 1413 /* Stop DAD if the interface is down even after dad_maxtry attempts. */ 1414 if ((dp->dad_ns_tcount > V_dad_maxtry) && 1415 (((ifp->if_flags & IFF_UP) == 0) || 1416 ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0))) { 1417 nd6log((LOG_INFO, "%s: could not run DAD " 1418 "because the interface was down or not running.\n", 1419 if_name(ifa->ifa_ifp))); 1420 goto err; 1421 } 1422 1423 /* Need more checks? */ 1424 if (dp->dad_ns_ocount < dp->dad_count) { 1425 /* 1426 * We have more NS to go. Send NS packet for DAD. 1427 */ 1428 nd6_dad_starttimer(dp, 1429 (long)ifa->ifa_ifp->if_inet6->nd_retrans * hz / 1000); 1430 nd6_dad_ns_output(dp); 1431 goto done; 1432 } else { 1433 /* 1434 * We have transmitted sufficient number of DAD packets. 1435 * See what we've got. 1436 */ 1437 if (dp->dad_ns_icount > 0 || dp->dad_na_icount > 0) { 1438 /* We've seen NS or NA, means DAD has failed. */ 1439 nd6_dad_duplicated(ifa, dp); 1440 } else if (V_dad_enhanced != 0 && 1441 dp->dad_ns_lcount > 0 && 1442 dp->dad_ns_lcount > dp->dad_loopbackprobe) { 1443 /* 1444 * Sec. 4.1 in RFC 7527 requires transmission of 1445 * additional probes until the loopback condition 1446 * becomes clear when a looped back probe is detected. 1447 */ 1448 log(LOG_ERR, "%s: a looped back NS message is " 1449 "detected during DAD for %s. " 1450 "Another DAD probes are being sent.\n", 1451 if_name(ifa->ifa_ifp), 1452 ip6_sprintf(ip6buf, IFA_IN6(ifa))); 1453 dp->dad_loopbackprobe = dp->dad_ns_lcount; 1454 /* 1455 * Send an NS immediately and increase dad_count by 1456 * V_nd6_mmaxtries - 1. 1457 */ 1458 dp->dad_count = 1459 dp->dad_ns_ocount + V_nd6_mmaxtries - 1; 1460 nd6_dad_starttimer(dp, 1461 (long)ifa->ifa_ifp->if_inet6->nd_retrans * hz / 1000); 1462 nd6_dad_ns_output(dp); 1463 goto done; 1464 } else { 1465 /* 1466 * We are done with DAD. No NA came, no NS came. 1467 * No duplicate address found. Check IFDISABLED flag 1468 * again in case that it is changed between the 1469 * beginning of this function and here. 1470 * 1471 * Reset DAD failures counter if using stable addresses. 1472 */ 1473 if ((ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED) == 0) { 1474 ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 1475 if ((ifp->if_inet6->nd_flags & ND6_IFF_STABLEADDR) && !(ia->ia6_flags & IN6_IFF_TEMPORARY)) 1476 atomic_store_int(&DAD_FAILURES(ifp), 0); 1477 /* 1478 * RFC 9131 Section 6.1.2: The first advertisement 1479 * SHOULD be sent as soon as an address changes the 1480 * state from tentative to preferred. 1481 */ 1482 nd6_grand_start(ifa, ND6_QUEUE_FLAG_NEWGUA); 1483 } 1484 1485 nd6log((LOG_DEBUG, 1486 "%s: DAD complete for %s - no duplicates found\n", 1487 if_name(ifa->ifa_ifp), 1488 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 1489 if (dp->dad_ns_lcount > 0) 1490 log(LOG_ERR, "%s: DAD completed while " 1491 "a looped back NS message is detected " 1492 "during DAD for %s.\n", 1493 if_name(ifa->ifa_ifp), 1494 ip6_sprintf(ip6buf, IFA_IN6(ifa))); 1495 } 1496 } 1497 err: 1498 nd6_dad_del(dp); 1499 DADQ_WUNLOCK(); 1500 done: 1501 NET_EPOCH_EXIT(et); 1502 CURVNET_RESTORE(); 1503 } 1504 1505 static void 1506 nd6_dad_duplicated(struct ifaddr *ifa, struct dadq *dp) 1507 { 1508 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 1509 struct ifnet *ifp; 1510 char ip6buf[INET6_ADDRSTRLEN]; 1511 1512 ifp = ifa->ifa_ifp; 1513 1514 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: " 1515 "NS in/out/loopback=%d/%d/%d, NA in=%d\n", 1516 if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 1517 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_ns_lcount, 1518 dp->dad_na_icount); 1519 1520 ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 1521 ia->ia6_flags |= IN6_IFF_DUPLICATED; 1522 1523 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n", 1524 if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)); 1525 1526 /* 1527 * For RFC 7217 stable addresses, increment failure counter here if we still have retries. 1528 * More addresses will be generated as long as retries are not exhausted. 1529 */ 1530 if ((ifp->if_inet6->nd_flags & ND6_IFF_STABLEADDR) && !(ia->ia6_flags & IN6_IFF_TEMPORARY)) { 1531 u_int dad_failures = atomic_load_int(&DAD_FAILURES(ifp)); 1532 1533 if (dad_failures <= V_ip6_stableaddr_maxretries) { 1534 atomic_add_int(&DAD_FAILURES(ifp), 1); 1535 /* if retries exhausted, output an informative error message */ 1536 if (dad_failures == V_ip6_stableaddr_maxretries) 1537 log(LOG_ERR, "%s: manual intervention required, consider disabling \"stableaddr\" on the interface" 1538 " or checking hostuuid for uniqueness\n", 1539 if_name(ifp)); 1540 } 1541 } else { 1542 log(LOG_ERR, "%s: manual intervention required\n", 1543 if_name(ifp)); 1544 } 1545 1546 /* 1547 * If the address is a link-local address formed from an interface 1548 * identifier based on the hardware address which is supposed to be 1549 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP 1550 * operation on the interface SHOULD be disabled. 1551 * [RFC 4862, Section 5.4.5] 1552 */ 1553 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) { 1554 struct in6_addr in6; 1555 1556 /* 1557 * To avoid over-reaction, we only apply this logic when we are 1558 * very sure that hardware addresses are supposed to be unique. 1559 */ 1560 switch (ifp->if_type) { 1561 case IFT_ETHER: 1562 case IFT_ATM: 1563 case IFT_IEEE1394: 1564 case IFT_INFINIBAND: 1565 in6 = ia->ia_addr.sin6_addr; 1566 if (in6_get_hw_ifid(ifp, &in6) == 0 && 1567 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) { 1568 ifp->if_inet6->nd_flags |= ND6_IFF_IFDISABLED; 1569 log(LOG_ERR, "%s: possible hardware address " 1570 "duplication detected, disable IPv6\n", 1571 if_name(ifp)); 1572 } 1573 break; 1574 } 1575 } 1576 } 1577 1578 /* 1579 * Transmit a neighbour solicitation for the purpose of DAD. Returns with the 1580 * DAD queue unlocked. 1581 */ 1582 static void 1583 nd6_dad_ns_output(struct dadq *dp) 1584 { 1585 struct in6_ifaddr *ia = (struct in6_ifaddr *)dp->dad_ifa; 1586 struct ifnet *ifp = dp->dad_ifa->ifa_ifp; 1587 int i; 1588 1589 DADQ_WLOCK_ASSERT(); 1590 1591 dp->dad_ns_tcount++; 1592 if ((ifp->if_flags & IFF_UP) == 0) { 1593 DADQ_WUNLOCK(); 1594 return; 1595 } 1596 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1597 DADQ_WUNLOCK(); 1598 return; 1599 } 1600 1601 dp->dad_ns_ocount++; 1602 if (V_dad_enhanced != 0) { 1603 for (i = 0; i < ND_OPT_NONCE_LEN32; i++) 1604 dp->dad_nonce[i] = arc4random(); 1605 /* 1606 * XXXHRS: Note that in the case that 1607 * DupAddrDetectTransmits > 1, multiple NS messages with 1608 * different nonces can be looped back in an unexpected 1609 * order. The current implementation recognizes only 1610 * the latest nonce on the sender side. Practically it 1611 * should work well in almost all cases. 1612 */ 1613 } 1614 DADQ_WUNLOCK(); 1615 nd6_ns_output(ifp, NULL, NULL, &ia->ia_addr.sin6_addr, 1616 (uint8_t *)&dp->dad_nonce[0]); 1617 } 1618 1619 static void 1620 nd6_dad_ns_input(struct ifaddr *ifa, struct nd_opt_nonce *ndopt_nonce) 1621 { 1622 struct dadq *dp; 1623 1624 if (ifa == NULL) 1625 panic("ifa == NULL in nd6_dad_ns_input"); 1626 1627 /* Ignore Nonce option when Enhanced DAD is disabled. */ 1628 if (V_dad_enhanced == 0) 1629 ndopt_nonce = NULL; 1630 DADQ_RLOCK(); 1631 dp = nd6_dad_find(ifa, ndopt_nonce); 1632 if (dp != NULL) 1633 dp->dad_ns_icount++; 1634 DADQ_RUNLOCK(); 1635 } 1636 1637 static void 1638 nd6_dad_na_input(struct ifaddr *ifa) 1639 { 1640 struct dadq *dp; 1641 1642 if (ifa == NULL) 1643 panic("ifa == NULL in nd6_dad_na_input"); 1644 1645 DADQ_RLOCK(); 1646 dp = nd6_dad_find(ifa, NULL); 1647 if (dp != NULL) 1648 dp->dad_na_icount++; 1649 DADQ_RUNLOCK(); 1650 } 1651 1652 static void 1653 nd6_queue_rel(void *arg) 1654 { 1655 struct nd_queue *ndq = arg; 1656 struct ifaddr *ifa; 1657 1658 ifa = ndq->ndq_ifa; 1659 IF_ADDR_WLOCK_ASSERT(ifa->ifa_ifp); 1660 1661 /* Remove ndq from the nd_queue list and free it */ 1662 TAILQ_REMOVE(&ifa->ifa_ifp->if_inet6->nd_queue, ndq, ndq_list); 1663 IF_ADDR_WUNLOCK(ifa->ifa_ifp); 1664 1665 free(ndq, M_IP6NDP); 1666 ifa_free(ifa); 1667 } 1668 1669 static void 1670 nd6_queue_timer(void *arg) 1671 { 1672 struct nd_queue *ndq = arg; 1673 struct ifaddr *ifa = ndq->ndq_ifa; 1674 struct ifnet *ifp; 1675 struct in6_addr daddr; 1676 struct epoch_tracker et; 1677 int delay, tlladdr; 1678 u_long flags; 1679 1680 KASSERT(ifa != NULL, ("ND6 queue entry %p with no address", ndq)); 1681 1682 ifp = ifa->ifa_ifp; 1683 CURVNET_SET(ifp->if_vnet); 1684 NET_EPOCH_ENTER(et); 1685 1686 daddr = ndq->ndq_daddr; 1687 tlladdr = ND6_NA_OPT_LLA; 1688 flags = (V_ip6_forwarding) ? ND_NA_FLAG_ROUTER : 0; 1689 if ((ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV) != 0 && V_ip6_norbit_raif) 1690 flags &= ~ND_NA_FLAG_ROUTER; 1691 1692 /* 1693 * RFC 9131 Section 6.1.2: If the address is preferred, 1694 * then the Override flag SHOULD NOT be set. 1695 */ 1696 if ((ndq->ndq_flags & ND6_QUEUE_FLAG_NEWGUA) != 0) { 1697 /* 1698 * XXX: If the address is in the Optimistic state, 1699 * then the Override flag MUST NOT be set. 1700 * We don't support RFC 4429 yet. 1701 */ 1702 if ((ifp->if_inet6->nd_flags & ND6_IFF_PREFER_SOURCE) == 0) 1703 flags |= ND_NA_FLAG_OVERRIDE; 1704 } 1705 /* 1706 * RFC 4861 Section 7.2.6: if link-layer address changed, 1707 * The Override flag MAY be set to either zero or one. 1708 */ 1709 if ((ndq->ndq_flags & ND6_QUEUE_FLAG_LLADDR) != 0) 1710 flags |= ND_NA_FLAG_OVERRIDE; 1711 /* anycast advertisement delay rule (RFC 4861 7.2.7, SHOULD) */ 1712 if ((ndq->ndq_flags & ND6_QUEUE_FLAG_ANYCAST) != 0) 1713 flags |= ND_NA_FLAG_SOLICITED; 1714 1715 /* 1716 * if it was GRAND, wait at least a RetransTimer 1717 * before removing from queue. 1718 */ 1719 if ((ndq->ndq_flags & ND6_QUEUE_GRAND_MASK) != 0) { 1720 delay = ifp->if_inet6->nd_retrans * hz / 1000; 1721 callout_reset(&ndq->ndq_callout, delay, nd6_queue_rel, ndq); 1722 IF_ADDR_WUNLOCK(ifp); 1723 } else 1724 nd6_queue_rel(ndq); 1725 1726 if (__predict_true(in6_setscope(&daddr, ifp, NULL) == 0)) 1727 nd6_na_output_fib(ifp, &daddr, IFA_IN6(ifa), flags, tlladdr, 1728 NULL, ifp->if_fib); 1729 1730 NET_EPOCH_EXIT(et); 1731 CURVNET_RESTORE(); 1732 } 1733 1734 static void 1735 nd6_queue_add(struct ifaddr *ifa, struct in6_addr *daddr, 1736 int delay, uint32_t flags) 1737 { 1738 struct nd_queue *ndq = NULL; 1739 struct ifnet *ifp; 1740 struct in6_ifextra *ext; 1741 char ip6buf[INET6_ADDRSTRLEN]; 1742 1743 NET_EPOCH_ASSERT(); 1744 1745 ifp = ifa->ifa_ifp; 1746 ext = ifp->if_inet6; 1747 IF_ADDR_WLOCK(ifp); 1748 /* 1749 * if request comes from GRAND, check whether another delayed 1750 * GRAND NA exists in the queue. 1751 * If it exists, cancel previous one and reuse its ndq. 1752 */ 1753 if ((flags & ND6_QUEUE_GRAND_MASK) != 0) { 1754 TAILQ_FOREACH(ndq, &ext->nd_queue, ndq_list) { 1755 if (ndq->ndq_ifa == ifa && 1756 (flags & ND6_QUEUE_GRAND_MASK) != 0) 1757 break; 1758 } 1759 } 1760 if (ndq == NULL) { 1761 ndq = malloc(sizeof(*ndq), M_IP6NDP, M_NOWAIT | M_ZERO); 1762 if (ndq == NULL) { 1763 log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n", 1764 __func__, ip6_sprintf(ip6buf, IFA_IN6(ifa)), 1765 ifp ? if_name(ifp) : "???"); 1766 IF_ADDR_WUNLOCK(ifp); 1767 return; 1768 } 1769 1770 callout_init_mtx(&ndq->ndq_callout, &ifp->if_addr_lock, 1771 CALLOUT_TRYLOCK | CALLOUT_RETURNUNLOCKED); 1772 ifa_ref(ifa); 1773 ndq->ndq_ifa = ifa; 1774 TAILQ_INSERT_TAIL(&ext->nd_queue, ndq, ndq_list); 1775 } 1776 1777 memcpy(&ndq->ndq_daddr, daddr, sizeof(struct in6_addr)); 1778 ndq->ndq_flags = flags; 1779 1780 nd6log((LOG_DEBUG, "%s: delay IPv6 NA for %s\n", if_name(ifp), 1781 ip6_sprintf(ip6buf, IFA_IN6(ifa)))); 1782 callout_reset(&ndq->ndq_callout, delay, nd6_queue_timer, ndq); 1783 IF_ADDR_WUNLOCK(ifp); 1784 } 1785 1786 /* 1787 * Start Gratuitous Neighbor Discovery (GRAND) for specified address. 1788 * Called after DAD completes and by interface link layer change event. 1789 */ 1790 void 1791 nd6_grand_start(struct ifaddr *ifa, uint32_t flags) 1792 { 1793 struct nd_queue *ndq; 1794 struct in6_ifextra *ext = ifa->ifa_ifp->if_inet6; 1795 struct in6_addr daddr = IN6ADDR_ANY_INIT; 1796 int delay, count = 0; 1797 1798 NET_EPOCH_ASSERT(); 1799 /* If we don't need GRAND, don't do it. */ 1800 if (V_ip6_grand_count == 0 || 1801 ifa->ifa_carp != NULL) 1802 return; 1803 1804 /* Check if new address is global */ 1805 if ((flags & ND6_QUEUE_FLAG_NEWGUA) != 0 && 1806 in6_addrscope(IFA_IN6(ifa)) != IPV6_ADDR_SCOPE_GLOBAL) 1807 return; 1808 1809 /* 1810 * RFC 9131 Section 6.1.2: These advertisements MUST be 1811 * separated by at least RetransTimer seconds. 1812 */ 1813 TAILQ_FOREACH(ndq, &ext->nd_queue, ndq_list) { 1814 /* 1815 * RFC 9131 Section 6.1.2: a node SHOULD send 1816 * up to MAX_NEIGHBOR_ADVERTISEMENT Neighbor Advertisement messages. 1817 * Make sure we don't queue GRAND more than V_ip6_grand_count 1818 * per interface. 1819 * Since this limitation only applies to GRAND, don't 1820 * count non-GRAND ndq. 1821 */ 1822 if ((ndq->ndq_flags & ND6_QUEUE_GRAND_MASK) == 0) 1823 continue; 1824 1825 count++; 1826 if (count >= V_ip6_grand_count) 1827 return; 1828 } 1829 1830 /* 1831 * RFC 9131 Section 6.1.2: if new global address added, 1832 * use the all-routers multicast address. 1833 */ 1834 if ((flags & ND6_QUEUE_FLAG_NEWGUA) != 0) 1835 daddr = in6addr_linklocal_allrouters; 1836 1837 /* 1838 * RFC 4861 Section 7.2.6: if link-layer address changed, 1839 * use the all-nodes multicast address. 1840 */ 1841 if ((flags & ND6_QUEUE_FLAG_LLADDR) != 0) 1842 daddr = in6addr_linklocal_allnodes; 1843 1844 delay = ext->nd_retrans * hz / 1000; 1845 nd6_queue_add(ifa, &daddr, count * delay, flags); 1846 } 1847 1848 /* 1849 * drain nd6 queue. used for address removals. 1850 */ 1851 void 1852 nd6_queue_stop(struct ifaddr *ifa) 1853 { 1854 struct nd_queue *ndq, *dndq; 1855 struct ifnet *ifp; 1856 1857 ifp = ifa->ifa_ifp; 1858 IF_ADDR_WLOCK(ifp); 1859 TAILQ_FOREACH_SAFE(ndq, &ifp->if_inet6->nd_queue, ndq_list, dndq) { 1860 if (ndq->ndq_ifa != ifa) 1861 continue; 1862 1863 callout_stop(&ndq->ndq_callout); 1864 1865 /* Remove ndq from the nd_queue list and free it */ 1866 TAILQ_REMOVE(&ifa->ifa_ifp->if_inet6->nd_queue, ndq, ndq_list); 1867 free(ndq, M_IP6NDP); 1868 ifa_free(ifa); 1869 } 1870 IF_ADDR_WUNLOCK(ifp); 1871 } 1872