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