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: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $ 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/malloc.h> 40 #include <sys/socket.h> 41 #include <sys/sockio.h> 42 #include <sys/jail.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/proc.h> 46 #include <sys/rmlock.h> 47 #include <sys/syslog.h> 48 #include <sys/md5.h> 49 50 #include <net/if.h> 51 #include <net/if_var.h> 52 #include <net/if_dl.h> 53 #include <net/if_private.h> 54 #include <net/if_types.h> 55 #include <net/route.h> 56 #include <net/vnet.h> 57 58 #include <netinet/in.h> 59 #include <netinet/in_var.h> 60 #include <netinet/if_ether.h> 61 #include <netinet/in_pcb.h> 62 #include <netinet/ip_var.h> 63 #include <netinet/udp.h> 64 #include <netinet/udp_var.h> 65 66 #include <netinet/ip6.h> 67 #include <netinet6/ip6_var.h> 68 #include <netinet6/in6_var.h> 69 #include <netinet6/in6_pcb.h> 70 #include <netinet6/in6_ifattach.h> 71 #include <netinet6/ip6_var.h> 72 #include <netinet6/nd6.h> 73 #include <netinet6/mld6_var.h> 74 #include <netinet6/scope6_var.h> 75 76 VNET_DEFINE(unsigned long, in6_maxmtu) = 0; 77 78 #ifdef IP6_AUTO_LINKLOCAL 79 VNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL; 80 #else 81 VNET_DEFINE(int, ip6_auto_linklocal) = 1; /* enabled by default */ 82 #endif 83 84 VNET_DEFINE(struct callout, in6_tmpaddrtimer_ch); 85 #define V_in6_tmpaddrtimer_ch VNET(in6_tmpaddrtimer_ch) 86 87 VNET_DECLARE(struct inpcbinfo, ripcbinfo); 88 #define V_ripcbinfo VNET(ripcbinfo) 89 90 static int get_rand_ifid(struct ifnet *, struct in6_addr *); 91 static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *); 92 static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *); 93 static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *); 94 static int in6_ifattach_loopback(struct ifnet *); 95 static void in6_purgemaddrs(struct ifnet *); 96 97 #define EUI64_GBIT 0x01 98 #define EUI64_UBIT 0x02 99 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) 100 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 101 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6)) 102 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT) 103 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6)) 104 105 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6)) 106 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6)) 107 108 /* 109 * Generate a last-resort interface identifier, when the machine has no 110 * IEEE802/EUI64 address sources. 111 * The goal here is to get an interface identifier that is 112 * (1) random enough and (2) does not change across reboot. 113 * We currently use MD5(hostname) for it. 114 * 115 * in6 - upper 64bits are preserved 116 */ 117 static int 118 get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6) 119 { 120 MD5_CTX ctxt; 121 struct prison *pr; 122 u_int8_t digest[16]; 123 int hostnamelen; 124 125 pr = curthread->td_ucred->cr_prison; 126 mtx_lock(&pr->pr_mtx); 127 hostnamelen = strlen(pr->pr_hostname); 128 #if 0 129 /* we need at least several letters as seed for ifid */ 130 if (hostnamelen < 3) { 131 mtx_unlock(&pr->pr_mtx); 132 return -1; 133 } 134 #endif 135 136 /* generate 8 bytes of pseudo-random value. */ 137 bzero(&ctxt, sizeof(ctxt)); 138 MD5Init(&ctxt); 139 MD5Update(&ctxt, pr->pr_hostname, hostnamelen); 140 mtx_unlock(&pr->pr_mtx); 141 MD5Final(digest, &ctxt); 142 143 /* assumes sizeof(digest) > sizeof(ifid) */ 144 bcopy(digest, &in6->s6_addr[8], 8); 145 146 /* make sure to set "u" bit to local, and "g" bit to individual. */ 147 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 148 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 149 150 /* convert EUI64 into IPv6 interface identifier */ 151 EUI64_TO_IFID(in6); 152 153 return 0; 154 } 155 156 static int 157 generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret) 158 { 159 MD5_CTX ctxt; 160 u_int8_t seed[16], digest[16], nullbuf[8]; 161 u_int32_t val32; 162 163 /* If there's no history, start with a random seed. */ 164 bzero(nullbuf, sizeof(nullbuf)); 165 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) { 166 int i; 167 168 for (i = 0; i < 2; i++) { 169 val32 = arc4random(); 170 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32)); 171 } 172 } else 173 bcopy(seed0, seed, 8); 174 175 /* copy the right-most 64-bits of the given address */ 176 /* XXX assumption on the size of IFID */ 177 bcopy(seed1, &seed[8], 8); 178 179 if (0) { /* for debugging purposes only */ 180 int i; 181 182 printf("generate_tmp_ifid: new randomized ID from: "); 183 for (i = 0; i < 16; i++) 184 printf("%02x", seed[i]); 185 printf(" "); 186 } 187 188 /* generate 16 bytes of pseudo-random value. */ 189 bzero(&ctxt, sizeof(ctxt)); 190 MD5Init(&ctxt); 191 MD5Update(&ctxt, seed, sizeof(seed)); 192 MD5Final(digest, &ctxt); 193 194 /* 195 * RFC 3041 3.2.1. (3) 196 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the 197 * left-most bit is numbered 0) to zero. 198 */ 199 bcopy(digest, ret, 8); 200 ret[0] &= ~EUI64_UBIT; 201 202 /* 203 * XXX: we'd like to ensure that the generated value is not zero 204 * for simplicity. If the caclculated digest happens to be zero, 205 * use a random non-zero value as the last resort. 206 */ 207 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) { 208 nd6log((LOG_INFO, 209 "generate_tmp_ifid: computed MD5 value is zero.\n")); 210 211 val32 = arc4random(); 212 val32 = 1 + (val32 % (0xffffffff - 1)); 213 } 214 215 /* 216 * RFC 3041 3.2.1. (4) 217 * Take the rightmost 64-bits of the MD5 digest and save them in 218 * stable storage as the history value to be used in the next 219 * iteration of the algorithm. 220 */ 221 bcopy(&digest[8], seed0, 8); 222 223 if (0) { /* for debugging purposes only */ 224 int i; 225 226 printf("to: "); 227 for (i = 0; i < 16; i++) 228 printf("%02x", digest[i]); 229 printf("\n"); 230 } 231 232 return 0; 233 } 234 235 /* 236 * Get interface identifier for the specified interface. 237 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface 238 * 239 * in6 - upper 64bits are preserved 240 */ 241 int 242 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6) 243 { 244 struct ifaddr *ifa; 245 struct sockaddr_dl *sdl; 246 u_int8_t *addr; 247 size_t addrlen; 248 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 249 static u_int8_t allone[8] = 250 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 251 252 NET_EPOCH_ASSERT(); 253 254 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 255 if (ifa->ifa_addr->sa_family != AF_LINK) 256 continue; 257 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 258 if (sdl == NULL) 259 continue; 260 if (sdl->sdl_alen == 0) 261 continue; 262 263 goto found; 264 } 265 266 return -1; 267 268 found: 269 addr = LLADDR(sdl); 270 addrlen = sdl->sdl_alen; 271 272 /* get EUI64 */ 273 switch (ifp->if_type) { 274 case IFT_BRIDGE: 275 case IFT_ETHER: 276 case IFT_L2VLAN: 277 case IFT_ATM: 278 case IFT_IEEE1394: 279 /* IEEE802/EUI64 cases - what others? */ 280 /* IEEE1394 uses 16byte length address starting with EUI64 */ 281 if (addrlen > 8) 282 addrlen = 8; 283 284 /* look at IEEE802/EUI64 only */ 285 if (addrlen != 8 && addrlen != 6) 286 return -1; 287 288 /* 289 * check for invalid MAC address - on bsdi, we see it a lot 290 * since wildboar configures all-zero MAC on pccard before 291 * card insertion. 292 */ 293 if (bcmp(addr, allzero, addrlen) == 0) 294 return -1; 295 if (bcmp(addr, allone, addrlen) == 0) 296 return -1; 297 298 /* make EUI64 address */ 299 if (addrlen == 8) 300 bcopy(addr, &in6->s6_addr[8], 8); 301 else if (addrlen == 6) { 302 in6->s6_addr[8] = addr[0]; 303 in6->s6_addr[9] = addr[1]; 304 in6->s6_addr[10] = addr[2]; 305 in6->s6_addr[11] = 0xff; 306 in6->s6_addr[12] = 0xfe; 307 in6->s6_addr[13] = addr[3]; 308 in6->s6_addr[14] = addr[4]; 309 in6->s6_addr[15] = addr[5]; 310 } 311 break; 312 313 case IFT_GIF: 314 case IFT_STF: 315 /* 316 * RFC2893 says: "SHOULD use IPv4 address as ifid source". 317 * however, IPv4 address is not very suitable as unique 318 * identifier source (can be renumbered). 319 * we don't do this. 320 */ 321 return -1; 322 323 case IFT_INFINIBAND: 324 if (addrlen != 20) 325 return -1; 326 bcopy(addr + 12, &in6->s6_addr[8], 8); 327 break; 328 329 default: 330 return -1; 331 } 332 333 /* sanity check: g bit must not indicate "group" */ 334 if (EUI64_GROUP(in6)) 335 return -1; 336 337 /* convert EUI64 into IPv6 interface identifier */ 338 EUI64_TO_IFID(in6); 339 340 /* 341 * sanity check: ifid must not be all zero, avoid conflict with 342 * subnet router anycast 343 */ 344 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && 345 bcmp(&in6->s6_addr[9], allzero, 7) == 0) 346 return -1; 347 348 return 0; 349 } 350 351 /* 352 * Get interface identifier for the specified interface. If it is not 353 * available on ifp0, borrow interface identifier from other information 354 * sources. 355 * 356 * altifp - secondary EUI64 source 357 */ 358 static int 359 get_ifid(struct ifnet *ifp0, struct ifnet *altifp, 360 struct in6_addr *in6) 361 { 362 struct ifnet *ifp; 363 364 NET_EPOCH_ASSERT(); 365 366 /* first, try to get it from the interface itself */ 367 if (in6_get_hw_ifid(ifp0, in6) == 0) { 368 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 369 if_name(ifp0))); 370 goto success; 371 } 372 373 /* try secondary EUI64 source. this basically is for ATM PVC */ 374 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) { 375 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", 376 if_name(ifp0), if_name(altifp))); 377 goto success; 378 } 379 380 /* next, try to get it from some other hardware interface */ 381 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 382 if (ifp == ifp0) 383 continue; 384 if (in6_get_hw_ifid(ifp, in6) != 0) 385 continue; 386 387 /* 388 * to borrow ifid from other interface, ifid needs to be 389 * globally unique 390 */ 391 if (IFID_UNIVERSAL(in6)) { 392 nd6log((LOG_DEBUG, 393 "%s: borrow interface identifier from %s\n", 394 if_name(ifp0), if_name(ifp))); 395 goto success; 396 } 397 } 398 399 /* last resort: get from random number source */ 400 if (get_rand_ifid(ifp, in6) == 0) { 401 nd6log((LOG_DEBUG, 402 "%s: interface identifier generated by random number\n", 403 if_name(ifp0))); 404 goto success; 405 } 406 407 printf("%s: failed to get interface identifier\n", if_name(ifp0)); 408 return -1; 409 410 success: 411 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 412 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 413 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 414 in6->s6_addr[14], in6->s6_addr[15])); 415 return 0; 416 } 417 418 /* 419 * altifp - secondary EUI64 source 420 */ 421 static int 422 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp) 423 { 424 struct in6_ifaddr *ia; 425 struct in6_aliasreq ifra; 426 struct nd_prefixctl pr0; 427 struct epoch_tracker et; 428 struct nd_prefix *pr; 429 int error; 430 431 /* 432 * configure link-local address. 433 */ 434 in6_prepare_ifra(&ifra, NULL, &in6mask64); 435 436 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000); 437 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 438 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 439 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; 440 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); 441 } else { 442 NET_EPOCH_ENTER(et); 443 error = get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr); 444 NET_EPOCH_EXIT(et); 445 if (error != 0) { 446 nd6log((LOG_ERR, 447 "%s: no ifid available\n", if_name(ifp))); 448 return (-1); 449 } 450 } 451 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL)) 452 return (-1); 453 454 /* link-local addresses should NEVER expire. */ 455 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 456 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 457 458 /* 459 * Now call in6_update_ifa() to do a bunch of procedures to configure 460 * a link-local address. We can set the 3rd argument to NULL, because 461 * we know there's no other link-local address on the interface 462 * and therefore we are adding one (instead of updating one). 463 */ 464 if ((error = in6_update_ifa(ifp, &ifra, NULL, 465 IN6_IFAUPDATE_DADDELAY)) != 0) { 466 /* 467 * XXX: When the interface does not support IPv6, this call 468 * would fail in the SIOCSIFADDR ioctl. I believe the 469 * notification is rather confusing in this case, so just 470 * suppress it. (jinmei@kame.net 20010130) 471 */ 472 if (error != EAFNOSUPPORT) 473 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to " 474 "configure a link-local address on %s " 475 "(errno=%d)\n", 476 if_name(ifp), error)); 477 return (-1); 478 } 479 480 NET_EPOCH_ENTER(et); 481 ia = in6ifa_ifpforlinklocal(ifp, 0); 482 NET_EPOCH_EXIT(et); 483 if (ia == NULL) { 484 /* 485 * Another thread removed the address that we just added. 486 * This should be rare, but it happens. 487 */ 488 nd6log((LOG_NOTICE, "%s: %s: new link-local address " 489 "disappeared\n", __func__, if_name(ifp))); 490 return (-1); 491 } 492 ifa_free(&ia->ia_ifa); 493 494 /* 495 * Make the link-local prefix (fe80::%link/64) as on-link. 496 * Since we'd like to manage prefixes separately from addresses, 497 * we make an ND6 prefix structure for the link-local prefix, 498 * and add it to the prefix list as a never-expire prefix. 499 * XXX: this change might affect some existing code base... 500 */ 501 bzero(&pr0, sizeof(pr0)); 502 pr0.ndpr_ifp = ifp; 503 /* this should be 64 at this moment. */ 504 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); 505 pr0.ndpr_prefix = ifra.ifra_addr; 506 /* apply the mask for safety. (nd6_prelist_add will apply it again) */ 507 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, &in6mask64); 508 /* 509 * Initialize parameters. The link-local prefix must always be 510 * on-link, and its lifetimes never expire. 511 */ 512 pr0.ndpr_raf_onlink = 1; 513 pr0.ndpr_raf_auto = 1; /* probably meaningless */ 514 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME; 515 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME; 516 /* 517 * Since there is no other link-local addresses, nd6_prefix_lookup() 518 * probably returns NULL. However, we cannot always expect the result. 519 * For example, if we first remove the (only) existing link-local 520 * address, and then reconfigure another one, the prefix is still 521 * valid with referring to the old link-local address. 522 */ 523 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 524 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) 525 return (error); 526 /* Reference prefix */ 527 ia->ia6_ndpr = pr; 528 pr->ndpr_addrcnt++; 529 } else 530 nd6_prefix_rele(pr); 531 532 return 0; 533 } 534 535 /* 536 * ifp - must be IFT_LOOP 537 */ 538 static int 539 in6_ifattach_loopback(struct ifnet *ifp) 540 { 541 struct in6_aliasreq ifra; 542 int error; 543 544 in6_prepare_ifra(&ifra, &in6addr_loopback, &in6mask128); 545 546 /* 547 * Always initialize ia_dstaddr (= broadcast address) to loopback 548 * address. Follows IPv4 practice - see in_ifinit(). 549 */ 550 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 551 ifra.ifra_dstaddr.sin6_family = AF_INET6; 552 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback; 553 554 /* the loopback address should NEVER expire. */ 555 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 556 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 557 558 /* 559 * We are sure that this is a newly assigned address, so we can set 560 * NULL to the 3rd arg. 561 */ 562 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) { 563 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure " 564 "the loopback address on %s (errno=%d)\n", 565 if_name(ifp), error)); 566 return (-1); 567 } 568 569 return 0; 570 } 571 572 /* 573 * compute NI group address, based on the current hostname setting. 574 * see RFC 4620. 575 * 576 * when ifp == NULL, the caller is responsible for filling scopeid. 577 * 578 * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address 579 * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620. 580 */ 581 static int 582 in6_nigroup0(struct ifnet *ifp, const char *name, int namelen, 583 struct in6_addr *in6, int oldmcprefix) 584 { 585 struct prison *pr; 586 const char *p; 587 u_char *q; 588 MD5_CTX ctxt; 589 u_int8_t digest[16]; 590 char l; 591 char n[64]; /* a single label must not exceed 63 chars */ 592 593 /* 594 * If no name is given and namelen is -1, 595 * we try to do the hostname lookup ourselves. 596 */ 597 if (!name && namelen == -1) { 598 pr = curthread->td_ucred->cr_prison; 599 mtx_lock(&pr->pr_mtx); 600 name = pr->pr_hostname; 601 namelen = strlen(name); 602 } else 603 pr = NULL; 604 if (!name || !namelen) { 605 if (pr != NULL) 606 mtx_unlock(&pr->pr_mtx); 607 return -1; 608 } 609 610 p = name; 611 while (p && *p && *p != '.' && p - name < namelen) 612 p++; 613 if (p == name || p - name > sizeof(n) - 1) { 614 if (pr != NULL) 615 mtx_unlock(&pr->pr_mtx); 616 return -1; /* label too long */ 617 } 618 l = p - name; 619 strncpy(n, name, l); 620 if (pr != NULL) 621 mtx_unlock(&pr->pr_mtx); 622 n[(int)l] = '\0'; 623 for (q = n; *q; q++) { 624 if ('A' <= *q && *q <= 'Z') 625 *q = *q - 'A' + 'a'; 626 } 627 628 /* generate 16 bytes of pseudo-random value. */ 629 bzero(&ctxt, sizeof(ctxt)); 630 MD5Init(&ctxt); 631 MD5Update(&ctxt, &l, sizeof(l)); 632 MD5Update(&ctxt, n, l); 633 MD5Final(digest, &ctxt); 634 635 bzero(in6, sizeof(*in6)); 636 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL; 637 in6->s6_addr8[11] = 2; 638 if (oldmcprefix == 0) { 639 in6->s6_addr8[12] = 0xff; 640 /* Copy the first 24 bits of 128-bit hash into the address. */ 641 bcopy(digest, &in6->s6_addr8[13], 3); 642 } else { 643 /* Copy the first 32 bits of 128-bit hash into the address. */ 644 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3])); 645 } 646 if (in6_setscope(in6, ifp, NULL)) 647 return (-1); /* XXX: should not fail */ 648 649 return 0; 650 } 651 652 int 653 in6_nigroup(struct ifnet *ifp, const char *name, int namelen, 654 struct in6_addr *in6) 655 { 656 657 return (in6_nigroup0(ifp, name, namelen, in6, 0)); 658 } 659 660 int 661 in6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen, 662 struct in6_addr *in6) 663 { 664 665 return (in6_nigroup0(ifp, name, namelen, in6, 1)); 666 } 667 668 /* 669 * XXX multiple loopback interface needs more care. for instance, 670 * nodelocal address needs to be configured onto only one of them. 671 * XXX multiple link-local address case 672 * 673 * altifp - secondary EUI64 source 674 */ 675 void 676 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp) 677 { 678 struct in6_ifaddr *ia; 679 680 if (ifp->if_afdata[AF_INET6] == NULL) 681 return; 682 /* 683 * quirks based on interface type 684 */ 685 switch (ifp->if_type) { 686 case IFT_STF: 687 /* 688 * 6to4 interface is a very special kind of beast. 689 * no multicast, no linklocal. RFC2529 specifies how to make 690 * linklocals for 6to4 interface, but there's no use and 691 * it is rather harmful to have one. 692 */ 693 ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL; 694 ND_IFINFO(ifp)->flags |= ND6_IFF_NO_DAD; 695 break; 696 default: 697 break; 698 } 699 700 /* 701 * usually, we require multicast capability to the interface 702 */ 703 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 704 nd6log((LOG_INFO, "in6_ifattach: " 705 "%s is not multicast capable, IPv6 not enabled\n", 706 if_name(ifp))); 707 return; 708 } 709 710 /* 711 * assign loopback address for loopback interface. 712 */ 713 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 714 /* 715 * check that loopback address doesn't exist yet. 716 */ 717 ia = in6ifa_ifwithaddr(&in6addr_loopback, 0, false); 718 if (ia == NULL) 719 in6_ifattach_loopback(ifp); 720 } 721 722 /* 723 * assign a link-local address, if there's none. 724 */ 725 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 726 ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) { 727 struct epoch_tracker et; 728 729 NET_EPOCH_ENTER(et); 730 ia = in6ifa_ifpforlinklocal(ifp, 0); 731 NET_EPOCH_EXIT(et); 732 if (ia == NULL) 733 in6_ifattach_linklocal(ifp, altifp); 734 else 735 ifa_free(&ia->ia_ifa); 736 } 737 738 /* update dynamically. */ 739 if (V_in6_maxmtu < ifp->if_mtu) 740 V_in6_maxmtu = ifp->if_mtu; 741 } 742 743 /* 744 * NOTE: in6_ifdetach() does not support loopback if at this moment. 745 * 746 * When shutting down a VNET we clean up layers top-down. In that case 747 * upper layer protocols (ulp) are cleaned up already and locks are destroyed 748 * and we must not call into these cleanup functions anymore, thus purgeulp 749 * is set to 0 in that case by in6_ifdetach_destroy(). 750 * The normal case of destroying a (cloned) interface still needs to cleanup 751 * everything related to the interface and will have purgeulp set to 1. 752 */ 753 static void 754 _in6_ifdetach(struct ifnet *ifp, int purgeulp) 755 { 756 struct ifaddr *ifa, *next; 757 758 if (ifp->if_afdata[AF_INET6] == NULL) 759 return; 760 761 /* 762 * nuke any of IPv6 addresses we have 763 */ 764 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 765 if (ifa->ifa_addr->sa_family != AF_INET6) 766 continue; 767 in6_purgeaddr(ifa); 768 } 769 if (purgeulp) { 770 IN6_MULTI_LOCK(); 771 in6_pcbpurgeif0(&V_udbinfo, ifp); 772 in6_pcbpurgeif0(&V_ulitecbinfo, ifp); 773 in6_pcbpurgeif0(&V_ripcbinfo, ifp); 774 IN6_MULTI_UNLOCK(); 775 } 776 /* leave from all multicast groups joined */ 777 in6_purgemaddrs(ifp); 778 779 /* 780 * Remove neighbor management table. 781 * Enabling the nd6_purge will panic on vmove for interfaces on VNET 782 * teardown as the IPv6 layer is cleaned up already and the locks 783 * are destroyed. 784 */ 785 if (purgeulp) 786 nd6_purge(ifp); 787 } 788 789 void 790 in6_ifdetach(struct ifnet *ifp) 791 { 792 793 _in6_ifdetach(ifp, 1); 794 } 795 796 void 797 in6_ifdetach_destroy(struct ifnet *ifp) 798 { 799 800 _in6_ifdetach(ifp, 0); 801 } 802 803 int 804 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf, 805 const u_int8_t *baseid, int generate) 806 { 807 u_int8_t nullbuf[8]; 808 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 809 810 bzero(nullbuf, sizeof(nullbuf)); 811 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) { 812 /* we've never created a random ID. Create a new one. */ 813 generate = 1; 814 } 815 816 if (generate) { 817 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1)); 818 819 /* generate_tmp_ifid will update seedn and buf */ 820 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1, 821 ndi->randomid); 822 } 823 bcopy(ndi->randomid, retbuf, 8); 824 825 return (0); 826 } 827 828 void 829 in6_tmpaddrtimer(void *arg) 830 { 831 CURVNET_SET((struct vnet *) arg); 832 struct epoch_tracker et; 833 struct nd_ifinfo *ndi; 834 u_int8_t nullbuf[8]; 835 struct ifnet *ifp; 836 837 callout_reset(&V_in6_tmpaddrtimer_ch, 838 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor - 839 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet); 840 841 bzero(nullbuf, sizeof(nullbuf)); 842 NET_EPOCH_ENTER(et); 843 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 844 if (ifp->if_afdata[AF_INET6] == NULL) 845 continue; 846 ndi = ND_IFINFO(ifp); 847 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) { 848 /* 849 * We've been generating a random ID on this interface. 850 * Create a new one. 851 */ 852 (void)generate_tmp_ifid(ndi->randomseed0, 853 ndi->randomseed1, ndi->randomid); 854 } 855 } 856 NET_EPOCH_EXIT(et); 857 CURVNET_RESTORE(); 858 } 859 860 static void 861 in6_purgemaddrs(struct ifnet *ifp) 862 { 863 struct in6_multi_head inmh; 864 865 SLIST_INIT(&inmh); 866 IN6_MULTI_LOCK(); 867 IN6_MULTI_LIST_LOCK(); 868 mld_ifdetach(ifp, &inmh); 869 IN6_MULTI_LIST_UNLOCK(); 870 IN6_MULTI_UNLOCK(); 871 in6m_release_list_deferred(&inmh); 872 873 /* 874 * Make sure all multicast deletions invoking if_ioctl() are 875 * completed before returning. Else we risk accessing a freed 876 * ifnet structure pointer. 877 */ 878 in6m_release_wait(NULL); 879 } 880 881 void 882 in6_ifattach_destroy(void) 883 { 884 885 callout_drain(&V_in6_tmpaddrtimer_ch); 886 } 887 888 static void 889 in6_ifattach_init(void *dummy) 890 { 891 892 /* Timer for regeneranation of temporary addresses randomize ID. */ 893 callout_init(&V_in6_tmpaddrtimer_ch, 1); 894 callout_reset(&V_in6_tmpaddrtimer_ch, 895 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor - 896 V_ip6_temp_regen_advance) * hz, 897 in6_tmpaddrtimer, curvnet); 898 } 899 900 /* 901 * Cheat. 902 * This must be after route_init(), which is now SI_ORDER_THIRD. 903 */ 904 SYSINIT(in6_ifattach_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 905 in6_ifattach_init, NULL); 906