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