34 35#include "opt_route.h" 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/syslog.h> 45#include <sys/md5.h> 46#include <sys/vimage.h> 47 48#include <net/if.h> 49#include <net/if_dl.h> 50#include <net/if_types.h> 51#include <net/route.h> 52#include <net/vnet.h> 53 54#include <netinet/in.h> 55#include <netinet/in_var.h> 56#include <netinet/if_ether.h> 57#include <netinet/in_pcb.h> 58#include <netinet/vinet.h> 59 60#include <netinet/ip6.h> 61#include <netinet6/ip6_var.h> 62#include <netinet6/in6_var.h> 63#include <netinet6/in6_pcb.h> 64#include <netinet6/in6_ifattach.h> 65#include <netinet6/ip6_var.h> 66#include <netinet6/nd6.h> 67#include <netinet6/mld6_var.h> 68#include <netinet6/scope6_var.h> 69#include <netinet6/vinet6.h> 70 71#ifdef VIMAGE_GLOBALS 72unsigned long in6_maxmtu; 73int ip6_auto_linklocal; 74struct callout in6_tmpaddrtimer_ch; 75extern struct inpcbinfo ripcbinfo; 76#endif 77 78static int get_rand_ifid(struct ifnet *, struct in6_addr *); 79static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *); 80static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *); 81static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *); 82static int in6_ifattach_loopback(struct ifnet *); 83static void in6_purgemaddrs(struct ifnet *); 84 85#define EUI64_GBIT 0x01 86#define EUI64_UBIT 0x02 87#define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) 88#define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 89#define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6)) 90#define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT) 91#define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6)) 92 93#define IFID_LOCAL(in6) (!EUI64_LOCAL(in6)) 94#define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6)) 95 96/* 97 * Generate a last-resort interface identifier, when the machine has no 98 * IEEE802/EUI64 address sources. 99 * The goal here is to get an interface identifier that is 100 * (1) random enough and (2) does not change across reboot. 101 * We currently use MD5(hostname) for it. 102 * 103 * in6 - upper 64bits are preserved 104 */ 105static int 106get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6) 107{ 108 MD5_CTX ctxt; 109 struct prison *pr; 110 u_int8_t digest[16]; 111 int hostnamelen; 112 113 pr = curthread->td_ucred->cr_prison; 114 mtx_lock(&pr->pr_mtx); 115 hostnamelen = strlen(pr->pr_host); 116#if 0 117 /* we need at least several letters as seed for ifid */ 118 if (hostnamelen < 3) { 119 mtx_unlock(&pr->pr_mtx); 120 return -1; 121 } 122#endif 123 124 /* generate 8 bytes of pseudo-random value. */ 125 bzero(&ctxt, sizeof(ctxt)); 126 MD5Init(&ctxt); 127 MD5Update(&ctxt, pr->pr_host, hostnamelen); 128 mtx_unlock(&pr->pr_mtx); 129 MD5Final(digest, &ctxt); 130 131 /* assumes sizeof(digest) > sizeof(ifid) */ 132 bcopy(digest, &in6->s6_addr[8], 8); 133 134 /* make sure to set "u" bit to local, and "g" bit to individual. */ 135 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 136 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 137 138 /* convert EUI64 into IPv6 interface identifier */ 139 EUI64_TO_IFID(in6); 140 141 return 0; 142} 143 144static int 145generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret) 146{ 147 INIT_VNET_INET6(curvnet); 148 MD5_CTX ctxt; 149 u_int8_t seed[16], digest[16], nullbuf[8]; 150 u_int32_t val32; 151 152 /* If there's no history, start with a random seed. */ 153 bzero(nullbuf, sizeof(nullbuf)); 154 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) { 155 int i; 156 157 for (i = 0; i < 2; i++) { 158 val32 = arc4random(); 159 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32)); 160 } 161 } else 162 bcopy(seed0, seed, 8); 163 164 /* copy the right-most 64-bits of the given address */ 165 /* XXX assumption on the size of IFID */ 166 bcopy(seed1, &seed[8], 8); 167 168 if (0) { /* for debugging purposes only */ 169 int i; 170 171 printf("generate_tmp_ifid: new randomized ID from: "); 172 for (i = 0; i < 16; i++) 173 printf("%02x", seed[i]); 174 printf(" "); 175 } 176 177 /* generate 16 bytes of pseudo-random value. */ 178 bzero(&ctxt, sizeof(ctxt)); 179 MD5Init(&ctxt); 180 MD5Update(&ctxt, seed, sizeof(seed)); 181 MD5Final(digest, &ctxt); 182 183 /* 184 * RFC 3041 3.2.1. (3) 185 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the 186 * left-most bit is numbered 0) to zero. 187 */ 188 bcopy(digest, ret, 8); 189 ret[0] &= ~EUI64_UBIT; 190 191 /* 192 * XXX: we'd like to ensure that the generated value is not zero 193 * for simplicity. If the caclculated digest happens to be zero, 194 * use a random non-zero value as the last resort. 195 */ 196 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) { 197 nd6log((LOG_INFO, 198 "generate_tmp_ifid: computed MD5 value is zero.\n")); 199 200 val32 = arc4random(); 201 val32 = 1 + (val32 % (0xffffffff - 1)); 202 } 203 204 /* 205 * RFC 3041 3.2.1. (4) 206 * Take the rightmost 64-bits of the MD5 digest and save them in 207 * stable storage as the history value to be used in the next 208 * iteration of the algorithm. 209 */ 210 bcopy(&digest[8], seed0, 8); 211 212 if (0) { /* for debugging purposes only */ 213 int i; 214 215 printf("to: "); 216 for (i = 0; i < 16; i++) 217 printf("%02x", digest[i]); 218 printf("\n"); 219 } 220 221 return 0; 222} 223 224/* 225 * Get interface identifier for the specified interface. 226 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface 227 * 228 * in6 - upper 64bits are preserved 229 */ 230int 231in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6) 232{ 233 struct ifaddr *ifa; 234 struct sockaddr_dl *sdl; 235 u_int8_t *addr; 236 size_t addrlen; 237 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 238 static u_int8_t allone[8] = 239 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 240 241 IF_ADDR_LOCK(ifp); 242 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 243 if (ifa->ifa_addr->sa_family != AF_LINK) 244 continue; 245 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 246 if (sdl == NULL) 247 continue; 248 if (sdl->sdl_alen == 0) 249 continue; 250 251 goto found; 252 } 253 IF_ADDR_UNLOCK(ifp); 254 255 return -1; 256 257found: 258 addr = LLADDR(sdl); 259 addrlen = sdl->sdl_alen; 260 261 /* get EUI64 */ 262 switch (ifp->if_type) { 263 case IFT_ETHER: 264 case IFT_FDDI: 265 case IFT_ISO88025: 266 case IFT_ATM: 267 case IFT_IEEE1394: 268#ifdef IFT_IEEE80211 269 case IFT_IEEE80211: 270#endif 271 /* IEEE802/EUI64 cases - what others? */ 272 /* IEEE1394 uses 16byte length address starting with EUI64 */ 273 if (addrlen > 8) 274 addrlen = 8; 275 276 /* look at IEEE802/EUI64 only */ 277 if (addrlen != 8 && addrlen != 6) { 278 IF_ADDR_UNLOCK(ifp); 279 return -1; 280 } 281 282 /* 283 * check for invalid MAC address - on bsdi, we see it a lot 284 * since wildboar configures all-zero MAC on pccard before 285 * card insertion. 286 */ 287 if (bcmp(addr, allzero, addrlen) == 0) { 288 IF_ADDR_UNLOCK(ifp); 289 return -1; 290 } 291 if (bcmp(addr, allone, addrlen) == 0) { 292 IF_ADDR_UNLOCK(ifp); 293 return -1; 294 } 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_ARCNET: 312 if (addrlen != 1) { 313 IF_ADDR_UNLOCK(ifp); 314 return -1; 315 } 316 if (!addr[0]) { 317 IF_ADDR_UNLOCK(ifp); 318 return -1; 319 } 320 321 bzero(&in6->s6_addr[8], 8); 322 in6->s6_addr[15] = addr[0]; 323 324 /* 325 * due to insufficient bitwidth, we mark it local. 326 */ 327 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 328 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 329 break; 330 331 case IFT_GIF: 332#ifdef IFT_STF 333 case IFT_STF: 334#endif 335 /* 336 * RFC2893 says: "SHOULD use IPv4 address as ifid source". 337 * however, IPv4 address is not very suitable as unique 338 * identifier source (can be renumbered). 339 * we don't do this. 340 */ 341 IF_ADDR_UNLOCK(ifp); 342 return -1; 343 344 default: 345 IF_ADDR_UNLOCK(ifp); 346 return -1; 347 } 348 349 /* sanity check: g bit must not indicate "group" */ 350 if (EUI64_GROUP(in6)) { 351 IF_ADDR_UNLOCK(ifp); 352 return -1; 353 } 354 355 /* convert EUI64 into IPv6 interface identifier */ 356 EUI64_TO_IFID(in6); 357 358 /* 359 * sanity check: ifid must not be all zero, avoid conflict with 360 * subnet router anycast 361 */ 362 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && 363 bcmp(&in6->s6_addr[9], allzero, 7) == 0) { 364 IF_ADDR_UNLOCK(ifp); 365 return -1; 366 } 367 368 IF_ADDR_UNLOCK(ifp); 369 return 0; 370} 371 372/* 373 * Get interface identifier for the specified interface. If it is not 374 * available on ifp0, borrow interface identifier from other information 375 * sources. 376 * 377 * altifp - secondary EUI64 source 378 */ 379static int 380get_ifid(struct ifnet *ifp0, struct ifnet *altifp, 381 struct in6_addr *in6) 382{ 383 INIT_VNET_NET(ifp0->if_vnet); 384 INIT_VNET_INET6(ifp0->if_vnet); 385 struct ifnet *ifp; 386 387 /* first, try to get it from the interface itself */ 388 if (in6_get_hw_ifid(ifp0, in6) == 0) { 389 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 390 if_name(ifp0))); 391 goto success; 392 } 393 394 /* try secondary EUI64 source. this basically is for ATM PVC */ 395 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) { 396 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", 397 if_name(ifp0), if_name(altifp))); 398 goto success; 399 } 400 401 /* next, try to get it from some other hardware interface */ 402 IFNET_RLOCK(); 403 for (ifp = V_ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) { 404 if (ifp == ifp0) 405 continue; 406 if (in6_get_hw_ifid(ifp, in6) != 0) 407 continue; 408 409 /* 410 * to borrow ifid from other interface, ifid needs to be 411 * globally unique 412 */ 413 if (IFID_UNIVERSAL(in6)) { 414 nd6log((LOG_DEBUG, 415 "%s: borrow interface identifier from %s\n", 416 if_name(ifp0), if_name(ifp))); 417 IFNET_RUNLOCK(); 418 goto success; 419 } 420 } 421 IFNET_RUNLOCK(); 422 423 /* last resort: get from random number source */ 424 if (get_rand_ifid(ifp, in6) == 0) { 425 nd6log((LOG_DEBUG, 426 "%s: interface identifier generated by random number\n", 427 if_name(ifp0))); 428 goto success; 429 } 430 431 printf("%s: failed to get interface identifier\n", if_name(ifp0)); 432 return -1; 433 434success: 435 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 436 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 437 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 438 in6->s6_addr[14], in6->s6_addr[15])); 439 return 0; 440} 441 442/* 443 * altifp - secondary EUI64 source 444 */ 445static int 446in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp) 447{ 448 INIT_VNET_INET6(curvnet); 449 struct in6_ifaddr *ia; 450 struct in6_aliasreq ifra; 451 struct nd_prefixctl pr0; 452 int i, error; 453 454 /* 455 * configure link-local address. 456 */ 457 bzero(&ifra, sizeof(ifra)); 458 459 /* 460 * in6_update_ifa() does not use ifra_name, but we accurately set it 461 * for safety. 462 */ 463 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 464 465 ifra.ifra_addr.sin6_family = AF_INET6; 466 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 467 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000); 468 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 469 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 470 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; 471 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); 472 } else { 473 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) { 474 nd6log((LOG_ERR, 475 "%s: no ifid available\n", if_name(ifp))); 476 return (-1); 477 } 478 } 479 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL)) 480 return (-1); 481 482 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 483 ifra.ifra_prefixmask.sin6_family = AF_INET6; 484 ifra.ifra_prefixmask.sin6_addr = in6mask64; 485 /* link-local addresses should NEVER expire. */ 486 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 487 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 488 489 /* 490 * Now call in6_update_ifa() to do a bunch of procedures to configure 491 * a link-local address. We can set the 3rd argument to NULL, because 492 * we know there's no other link-local address on the interface 493 * and therefore we are adding one (instead of updating one). 494 */ 495 if ((error = in6_update_ifa(ifp, &ifra, NULL, 496 IN6_IFAUPDATE_DADDELAY)) != 0) { 497 /* 498 * XXX: When the interface does not support IPv6, this call 499 * would fail in the SIOCSIFADDR ioctl. I believe the 500 * notification is rather confusing in this case, so just 501 * suppress it. (jinmei@kame.net 20010130) 502 */ 503 if (error != EAFNOSUPPORT) 504 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to " 505 "configure a link-local address on %s " 506 "(errno=%d)\n", 507 if_name(ifp), error)); 508 return (-1); 509 } 510 511 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */ 512#ifdef DIAGNOSTIC 513 if (!ia) { 514 panic("ia == NULL in in6_ifattach_linklocal"); 515 /* NOTREACHED */ 516 } 517#endif 518 519 /* 520 * Make the link-local prefix (fe80::%link/64) as on-link. 521 * Since we'd like to manage prefixes separately from addresses, 522 * we make an ND6 prefix structure for the link-local prefix, 523 * and add it to the prefix list as a never-expire prefix. 524 * XXX: this change might affect some existing code base... 525 */ 526 bzero(&pr0, sizeof(pr0)); 527 pr0.ndpr_ifp = ifp; 528 /* this should be 64 at this moment. */ 529 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); 530 pr0.ndpr_prefix = ifra.ifra_addr; 531 /* apply the mask for safety. (nd6_prelist_add will apply it again) */ 532 for (i = 0; i < 4; i++) { 533 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 534 in6mask64.s6_addr32[i]; 535 } 536 /* 537 * Initialize parameters. The link-local prefix must always be 538 * on-link, and its lifetimes never expire. 539 */ 540 pr0.ndpr_raf_onlink = 1; 541 pr0.ndpr_raf_auto = 1; /* probably meaningless */ 542 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME; 543 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME; 544 /* 545 * Since there is no other link-local addresses, nd6_prefix_lookup() 546 * probably returns NULL. However, we cannot always expect the result. 547 * For example, if we first remove the (only) existing link-local 548 * address, and then reconfigure another one, the prefix is still 549 * valid with referring to the old link-local address. 550 */ 551 if (nd6_prefix_lookup(&pr0) == NULL) { 552 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0) 553 return (error); 554 } 555 556 return 0; 557} 558 559/* 560 * ifp - must be IFT_LOOP 561 */ 562static int 563in6_ifattach_loopback(struct ifnet *ifp) 564{ 565 INIT_VNET_INET6(curvnet); 566 struct in6_aliasreq ifra; 567 int error; 568 569 bzero(&ifra, sizeof(ifra)); 570 571 /* 572 * in6_update_ifa() does not use ifra_name, but we accurately set it 573 * for safety. 574 */ 575 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 576 577 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 578 ifra.ifra_prefixmask.sin6_family = AF_INET6; 579 ifra.ifra_prefixmask.sin6_addr = in6mask128; 580 581 /* 582 * Always initialize ia_dstaddr (= broadcast address) to loopback 583 * address. Follows IPv4 practice - see in_ifinit(). 584 */ 585 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 586 ifra.ifra_dstaddr.sin6_family = AF_INET6; 587 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback; 588 589 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 590 ifra.ifra_addr.sin6_family = AF_INET6; 591 ifra.ifra_addr.sin6_addr = in6addr_loopback; 592 593 /* the loopback address should NEVER expire. */ 594 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 595 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 596 597 /* we don't need to perform DAD on loopback interfaces. */ 598 ifra.ifra_flags |= IN6_IFF_NODAD; 599 600 /* skip registration to the prefix list. XXX should be temporary. */ 601 ifra.ifra_flags |= IN6_IFF_NOPFX; 602 603 /* 604 * We are sure that this is a newly assigned address, so we can set 605 * NULL to the 3rd arg. 606 */ 607 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) { 608 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure " 609 "the loopback address on %s (errno=%d)\n", 610 if_name(ifp), error)); 611 return (-1); 612 } 613 614 return 0; 615} 616 617/* 618 * compute NI group address, based on the current hostname setting. 619 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later). 620 * 621 * when ifp == NULL, the caller is responsible for filling scopeid. 622 */ 623int 624in6_nigroup(struct ifnet *ifp, const char *name, int namelen, 625 struct in6_addr *in6) 626{ 627 struct prison *pr; 628 const char *p; 629 u_char *q; 630 MD5_CTX ctxt; 631 u_int8_t digest[16]; 632 char l; 633 char n[64]; /* a single label must not exceed 63 chars */ 634 635 /* 636 * If no name is given and namelen is -1, 637 * we try to do the hostname lookup ourselves. 638 */ 639 if (!name && namelen == -1) { 640 pr = curthread->td_ucred->cr_prison; 641 mtx_lock(&pr->pr_mtx); 642 name = pr->pr_host; 643 namelen = strlen(name); 644 } else 645 pr = NULL; 646 if (!name || !namelen) { 647 if (pr != NULL) 648 mtx_unlock(&pr->pr_mtx); 649 return -1; 650 } 651 652 p = name; 653 while (p && *p && *p != '.' && p - name < namelen) 654 p++; 655 if (p == name || p - name > sizeof(n) - 1) { 656 if (pr != NULL) 657 mtx_unlock(&pr->pr_mtx); 658 return -1; /* label too long */ 659 } 660 l = p - name; 661 strncpy(n, name, l); 662 if (pr != NULL) 663 mtx_unlock(&pr->pr_mtx); 664 n[(int)l] = '\0'; 665 for (q = n; *q; q++) { 666 if ('A' <= *q && *q <= 'Z') 667 *q = *q - 'A' + 'a'; 668 } 669 670 /* generate 8 bytes of pseudo-random value. */ 671 bzero(&ctxt, sizeof(ctxt)); 672 MD5Init(&ctxt); 673 MD5Update(&ctxt, &l, sizeof(l)); 674 MD5Update(&ctxt, n, l); 675 MD5Final(digest, &ctxt); 676 677 bzero(in6, sizeof(*in6)); 678 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL; 679 in6->s6_addr8[11] = 2; 680 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3])); 681 if (in6_setscope(in6, ifp, NULL)) 682 return (-1); /* XXX: should not fail */ 683 684 return 0; 685} 686 687/* 688 * XXX multiple loopback interface needs more care. for instance, 689 * nodelocal address needs to be configured onto only one of them. 690 * XXX multiple link-local address case 691 * 692 * altifp - secondary EUI64 source 693 */ 694void 695in6_ifattach(struct ifnet *ifp, struct ifnet *altifp) 696{ 697 INIT_VNET_INET6(ifp->if_vnet); 698 struct in6_ifaddr *ia; 699 struct in6_addr in6; 700 701 /* some of the interfaces are inherently not IPv6 capable */ 702 switch (ifp->if_type) { 703 case IFT_PFLOG: 704 case IFT_PFSYNC: 705 case IFT_CARP: 706 return; 707 } 708 709 /* 710 * quirks based on interface type 711 */ 712 switch (ifp->if_type) { 713#ifdef IFT_STF 714 case IFT_STF: 715 /* 716 * 6to4 interface is a very special kind of beast. 717 * no multicast, no linklocal. RFC2529 specifies how to make 718 * linklocals for 6to4 interface, but there's no use and 719 * it is rather harmful to have one. 720 */ 721 goto statinit; 722#endif 723 default: 724 break; 725 } 726 727 /* 728 * usually, we require multicast capability to the interface 729 */ 730 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 731 nd6log((LOG_INFO, "in6_ifattach: " 732 "%s is not multicast capable, IPv6 not enabled\n", 733 if_name(ifp))); 734 return; 735 } 736 737 /* 738 * assign loopback address for loopback interface. 739 * XXX multiple loopback interface case. 740 */ 741 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 742 in6 = in6addr_loopback; 743 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) { 744 if (in6_ifattach_loopback(ifp) != 0) 745 return; 746 } 747 } 748 749 /* 750 * assign a link-local address, if there's none. 751 */ 752 if (V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) { 753 ia = in6ifa_ifpforlinklocal(ifp, 0); 754 if (ia == NULL) { 755 if (in6_ifattach_linklocal(ifp, altifp) == 0) { 756 /* linklocal address assigned */ 757 } else { 758 /* failed to assign linklocal address. bark? */ 759 } 760 } 761 } 762 763#ifdef IFT_STF /* XXX */ 764statinit: 765#endif 766 767 /* update dynamically. */ 768 if (V_in6_maxmtu < ifp->if_mtu) 769 V_in6_maxmtu = ifp->if_mtu; 770} 771 772/* 773 * NOTE: in6_ifdetach() does not support loopback if at this moment. 774 * We don't need this function in bsdi, because interfaces are never removed 775 * from the ifnet list in bsdi. 776 */ 777void 778in6_ifdetach(struct ifnet *ifp) 779{
| 34 35#include "opt_route.h" 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/syslog.h> 45#include <sys/md5.h> 46#include <sys/vimage.h> 47 48#include <net/if.h> 49#include <net/if_dl.h> 50#include <net/if_types.h> 51#include <net/route.h> 52#include <net/vnet.h> 53 54#include <netinet/in.h> 55#include <netinet/in_var.h> 56#include <netinet/if_ether.h> 57#include <netinet/in_pcb.h> 58#include <netinet/vinet.h> 59 60#include <netinet/ip6.h> 61#include <netinet6/ip6_var.h> 62#include <netinet6/in6_var.h> 63#include <netinet6/in6_pcb.h> 64#include <netinet6/in6_ifattach.h> 65#include <netinet6/ip6_var.h> 66#include <netinet6/nd6.h> 67#include <netinet6/mld6_var.h> 68#include <netinet6/scope6_var.h> 69#include <netinet6/vinet6.h> 70 71#ifdef VIMAGE_GLOBALS 72unsigned long in6_maxmtu; 73int ip6_auto_linklocal; 74struct callout in6_tmpaddrtimer_ch; 75extern struct inpcbinfo ripcbinfo; 76#endif 77 78static int get_rand_ifid(struct ifnet *, struct in6_addr *); 79static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *); 80static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *); 81static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *); 82static int in6_ifattach_loopback(struct ifnet *); 83static void in6_purgemaddrs(struct ifnet *); 84 85#define EUI64_GBIT 0x01 86#define EUI64_UBIT 0x02 87#define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) 88#define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 89#define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6)) 90#define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT) 91#define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6)) 92 93#define IFID_LOCAL(in6) (!EUI64_LOCAL(in6)) 94#define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6)) 95 96/* 97 * Generate a last-resort interface identifier, when the machine has no 98 * IEEE802/EUI64 address sources. 99 * The goal here is to get an interface identifier that is 100 * (1) random enough and (2) does not change across reboot. 101 * We currently use MD5(hostname) for it. 102 * 103 * in6 - upper 64bits are preserved 104 */ 105static int 106get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6) 107{ 108 MD5_CTX ctxt; 109 struct prison *pr; 110 u_int8_t digest[16]; 111 int hostnamelen; 112 113 pr = curthread->td_ucred->cr_prison; 114 mtx_lock(&pr->pr_mtx); 115 hostnamelen = strlen(pr->pr_host); 116#if 0 117 /* we need at least several letters as seed for ifid */ 118 if (hostnamelen < 3) { 119 mtx_unlock(&pr->pr_mtx); 120 return -1; 121 } 122#endif 123 124 /* generate 8 bytes of pseudo-random value. */ 125 bzero(&ctxt, sizeof(ctxt)); 126 MD5Init(&ctxt); 127 MD5Update(&ctxt, pr->pr_host, hostnamelen); 128 mtx_unlock(&pr->pr_mtx); 129 MD5Final(digest, &ctxt); 130 131 /* assumes sizeof(digest) > sizeof(ifid) */ 132 bcopy(digest, &in6->s6_addr[8], 8); 133 134 /* make sure to set "u" bit to local, and "g" bit to individual. */ 135 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 136 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 137 138 /* convert EUI64 into IPv6 interface identifier */ 139 EUI64_TO_IFID(in6); 140 141 return 0; 142} 143 144static int 145generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret) 146{ 147 INIT_VNET_INET6(curvnet); 148 MD5_CTX ctxt; 149 u_int8_t seed[16], digest[16], nullbuf[8]; 150 u_int32_t val32; 151 152 /* If there's no history, start with a random seed. */ 153 bzero(nullbuf, sizeof(nullbuf)); 154 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) { 155 int i; 156 157 for (i = 0; i < 2; i++) { 158 val32 = arc4random(); 159 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32)); 160 } 161 } else 162 bcopy(seed0, seed, 8); 163 164 /* copy the right-most 64-bits of the given address */ 165 /* XXX assumption on the size of IFID */ 166 bcopy(seed1, &seed[8], 8); 167 168 if (0) { /* for debugging purposes only */ 169 int i; 170 171 printf("generate_tmp_ifid: new randomized ID from: "); 172 for (i = 0; i < 16; i++) 173 printf("%02x", seed[i]); 174 printf(" "); 175 } 176 177 /* generate 16 bytes of pseudo-random value. */ 178 bzero(&ctxt, sizeof(ctxt)); 179 MD5Init(&ctxt); 180 MD5Update(&ctxt, seed, sizeof(seed)); 181 MD5Final(digest, &ctxt); 182 183 /* 184 * RFC 3041 3.2.1. (3) 185 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the 186 * left-most bit is numbered 0) to zero. 187 */ 188 bcopy(digest, ret, 8); 189 ret[0] &= ~EUI64_UBIT; 190 191 /* 192 * XXX: we'd like to ensure that the generated value is not zero 193 * for simplicity. If the caclculated digest happens to be zero, 194 * use a random non-zero value as the last resort. 195 */ 196 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) { 197 nd6log((LOG_INFO, 198 "generate_tmp_ifid: computed MD5 value is zero.\n")); 199 200 val32 = arc4random(); 201 val32 = 1 + (val32 % (0xffffffff - 1)); 202 } 203 204 /* 205 * RFC 3041 3.2.1. (4) 206 * Take the rightmost 64-bits of the MD5 digest and save them in 207 * stable storage as the history value to be used in the next 208 * iteration of the algorithm. 209 */ 210 bcopy(&digest[8], seed0, 8); 211 212 if (0) { /* for debugging purposes only */ 213 int i; 214 215 printf("to: "); 216 for (i = 0; i < 16; i++) 217 printf("%02x", digest[i]); 218 printf("\n"); 219 } 220 221 return 0; 222} 223 224/* 225 * Get interface identifier for the specified interface. 226 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface 227 * 228 * in6 - upper 64bits are preserved 229 */ 230int 231in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6) 232{ 233 struct ifaddr *ifa; 234 struct sockaddr_dl *sdl; 235 u_int8_t *addr; 236 size_t addrlen; 237 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 238 static u_int8_t allone[8] = 239 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 240 241 IF_ADDR_LOCK(ifp); 242 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 243 if (ifa->ifa_addr->sa_family != AF_LINK) 244 continue; 245 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 246 if (sdl == NULL) 247 continue; 248 if (sdl->sdl_alen == 0) 249 continue; 250 251 goto found; 252 } 253 IF_ADDR_UNLOCK(ifp); 254 255 return -1; 256 257found: 258 addr = LLADDR(sdl); 259 addrlen = sdl->sdl_alen; 260 261 /* get EUI64 */ 262 switch (ifp->if_type) { 263 case IFT_ETHER: 264 case IFT_FDDI: 265 case IFT_ISO88025: 266 case IFT_ATM: 267 case IFT_IEEE1394: 268#ifdef IFT_IEEE80211 269 case IFT_IEEE80211: 270#endif 271 /* IEEE802/EUI64 cases - what others? */ 272 /* IEEE1394 uses 16byte length address starting with EUI64 */ 273 if (addrlen > 8) 274 addrlen = 8; 275 276 /* look at IEEE802/EUI64 only */ 277 if (addrlen != 8 && addrlen != 6) { 278 IF_ADDR_UNLOCK(ifp); 279 return -1; 280 } 281 282 /* 283 * check for invalid MAC address - on bsdi, we see it a lot 284 * since wildboar configures all-zero MAC on pccard before 285 * card insertion. 286 */ 287 if (bcmp(addr, allzero, addrlen) == 0) { 288 IF_ADDR_UNLOCK(ifp); 289 return -1; 290 } 291 if (bcmp(addr, allone, addrlen) == 0) { 292 IF_ADDR_UNLOCK(ifp); 293 return -1; 294 } 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_ARCNET: 312 if (addrlen != 1) { 313 IF_ADDR_UNLOCK(ifp); 314 return -1; 315 } 316 if (!addr[0]) { 317 IF_ADDR_UNLOCK(ifp); 318 return -1; 319 } 320 321 bzero(&in6->s6_addr[8], 8); 322 in6->s6_addr[15] = addr[0]; 323 324 /* 325 * due to insufficient bitwidth, we mark it local. 326 */ 327 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 328 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 329 break; 330 331 case IFT_GIF: 332#ifdef IFT_STF 333 case IFT_STF: 334#endif 335 /* 336 * RFC2893 says: "SHOULD use IPv4 address as ifid source". 337 * however, IPv4 address is not very suitable as unique 338 * identifier source (can be renumbered). 339 * we don't do this. 340 */ 341 IF_ADDR_UNLOCK(ifp); 342 return -1; 343 344 default: 345 IF_ADDR_UNLOCK(ifp); 346 return -1; 347 } 348 349 /* sanity check: g bit must not indicate "group" */ 350 if (EUI64_GROUP(in6)) { 351 IF_ADDR_UNLOCK(ifp); 352 return -1; 353 } 354 355 /* convert EUI64 into IPv6 interface identifier */ 356 EUI64_TO_IFID(in6); 357 358 /* 359 * sanity check: ifid must not be all zero, avoid conflict with 360 * subnet router anycast 361 */ 362 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && 363 bcmp(&in6->s6_addr[9], allzero, 7) == 0) { 364 IF_ADDR_UNLOCK(ifp); 365 return -1; 366 } 367 368 IF_ADDR_UNLOCK(ifp); 369 return 0; 370} 371 372/* 373 * Get interface identifier for the specified interface. If it is not 374 * available on ifp0, borrow interface identifier from other information 375 * sources. 376 * 377 * altifp - secondary EUI64 source 378 */ 379static int 380get_ifid(struct ifnet *ifp0, struct ifnet *altifp, 381 struct in6_addr *in6) 382{ 383 INIT_VNET_NET(ifp0->if_vnet); 384 INIT_VNET_INET6(ifp0->if_vnet); 385 struct ifnet *ifp; 386 387 /* first, try to get it from the interface itself */ 388 if (in6_get_hw_ifid(ifp0, in6) == 0) { 389 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 390 if_name(ifp0))); 391 goto success; 392 } 393 394 /* try secondary EUI64 source. this basically is for ATM PVC */ 395 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) { 396 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", 397 if_name(ifp0), if_name(altifp))); 398 goto success; 399 } 400 401 /* next, try to get it from some other hardware interface */ 402 IFNET_RLOCK(); 403 for (ifp = V_ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) { 404 if (ifp == ifp0) 405 continue; 406 if (in6_get_hw_ifid(ifp, in6) != 0) 407 continue; 408 409 /* 410 * to borrow ifid from other interface, ifid needs to be 411 * globally unique 412 */ 413 if (IFID_UNIVERSAL(in6)) { 414 nd6log((LOG_DEBUG, 415 "%s: borrow interface identifier from %s\n", 416 if_name(ifp0), if_name(ifp))); 417 IFNET_RUNLOCK(); 418 goto success; 419 } 420 } 421 IFNET_RUNLOCK(); 422 423 /* last resort: get from random number source */ 424 if (get_rand_ifid(ifp, in6) == 0) { 425 nd6log((LOG_DEBUG, 426 "%s: interface identifier generated by random number\n", 427 if_name(ifp0))); 428 goto success; 429 } 430 431 printf("%s: failed to get interface identifier\n", if_name(ifp0)); 432 return -1; 433 434success: 435 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 436 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 437 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 438 in6->s6_addr[14], in6->s6_addr[15])); 439 return 0; 440} 441 442/* 443 * altifp - secondary EUI64 source 444 */ 445static int 446in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp) 447{ 448 INIT_VNET_INET6(curvnet); 449 struct in6_ifaddr *ia; 450 struct in6_aliasreq ifra; 451 struct nd_prefixctl pr0; 452 int i, error; 453 454 /* 455 * configure link-local address. 456 */ 457 bzero(&ifra, sizeof(ifra)); 458 459 /* 460 * in6_update_ifa() does not use ifra_name, but we accurately set it 461 * for safety. 462 */ 463 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 464 465 ifra.ifra_addr.sin6_family = AF_INET6; 466 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 467 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000); 468 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 469 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 470 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; 471 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); 472 } else { 473 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) { 474 nd6log((LOG_ERR, 475 "%s: no ifid available\n", if_name(ifp))); 476 return (-1); 477 } 478 } 479 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL)) 480 return (-1); 481 482 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 483 ifra.ifra_prefixmask.sin6_family = AF_INET6; 484 ifra.ifra_prefixmask.sin6_addr = in6mask64; 485 /* link-local addresses should NEVER expire. */ 486 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 487 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 488 489 /* 490 * Now call in6_update_ifa() to do a bunch of procedures to configure 491 * a link-local address. We can set the 3rd argument to NULL, because 492 * we know there's no other link-local address on the interface 493 * and therefore we are adding one (instead of updating one). 494 */ 495 if ((error = in6_update_ifa(ifp, &ifra, NULL, 496 IN6_IFAUPDATE_DADDELAY)) != 0) { 497 /* 498 * XXX: When the interface does not support IPv6, this call 499 * would fail in the SIOCSIFADDR ioctl. I believe the 500 * notification is rather confusing in this case, so just 501 * suppress it. (jinmei@kame.net 20010130) 502 */ 503 if (error != EAFNOSUPPORT) 504 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to " 505 "configure a link-local address on %s " 506 "(errno=%d)\n", 507 if_name(ifp), error)); 508 return (-1); 509 } 510 511 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */ 512#ifdef DIAGNOSTIC 513 if (!ia) { 514 panic("ia == NULL in in6_ifattach_linklocal"); 515 /* NOTREACHED */ 516 } 517#endif 518 519 /* 520 * Make the link-local prefix (fe80::%link/64) as on-link. 521 * Since we'd like to manage prefixes separately from addresses, 522 * we make an ND6 prefix structure for the link-local prefix, 523 * and add it to the prefix list as a never-expire prefix. 524 * XXX: this change might affect some existing code base... 525 */ 526 bzero(&pr0, sizeof(pr0)); 527 pr0.ndpr_ifp = ifp; 528 /* this should be 64 at this moment. */ 529 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); 530 pr0.ndpr_prefix = ifra.ifra_addr; 531 /* apply the mask for safety. (nd6_prelist_add will apply it again) */ 532 for (i = 0; i < 4; i++) { 533 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 534 in6mask64.s6_addr32[i]; 535 } 536 /* 537 * Initialize parameters. The link-local prefix must always be 538 * on-link, and its lifetimes never expire. 539 */ 540 pr0.ndpr_raf_onlink = 1; 541 pr0.ndpr_raf_auto = 1; /* probably meaningless */ 542 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME; 543 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME; 544 /* 545 * Since there is no other link-local addresses, nd6_prefix_lookup() 546 * probably returns NULL. However, we cannot always expect the result. 547 * For example, if we first remove the (only) existing link-local 548 * address, and then reconfigure another one, the prefix is still 549 * valid with referring to the old link-local address. 550 */ 551 if (nd6_prefix_lookup(&pr0) == NULL) { 552 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0) 553 return (error); 554 } 555 556 return 0; 557} 558 559/* 560 * ifp - must be IFT_LOOP 561 */ 562static int 563in6_ifattach_loopback(struct ifnet *ifp) 564{ 565 INIT_VNET_INET6(curvnet); 566 struct in6_aliasreq ifra; 567 int error; 568 569 bzero(&ifra, sizeof(ifra)); 570 571 /* 572 * in6_update_ifa() does not use ifra_name, but we accurately set it 573 * for safety. 574 */ 575 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 576 577 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 578 ifra.ifra_prefixmask.sin6_family = AF_INET6; 579 ifra.ifra_prefixmask.sin6_addr = in6mask128; 580 581 /* 582 * Always initialize ia_dstaddr (= broadcast address) to loopback 583 * address. Follows IPv4 practice - see in_ifinit(). 584 */ 585 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 586 ifra.ifra_dstaddr.sin6_family = AF_INET6; 587 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback; 588 589 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 590 ifra.ifra_addr.sin6_family = AF_INET6; 591 ifra.ifra_addr.sin6_addr = in6addr_loopback; 592 593 /* the loopback address should NEVER expire. */ 594 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 595 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 596 597 /* we don't need to perform DAD on loopback interfaces. */ 598 ifra.ifra_flags |= IN6_IFF_NODAD; 599 600 /* skip registration to the prefix list. XXX should be temporary. */ 601 ifra.ifra_flags |= IN6_IFF_NOPFX; 602 603 /* 604 * We are sure that this is a newly assigned address, so we can set 605 * NULL to the 3rd arg. 606 */ 607 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) { 608 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure " 609 "the loopback address on %s (errno=%d)\n", 610 if_name(ifp), error)); 611 return (-1); 612 } 613 614 return 0; 615} 616 617/* 618 * compute NI group address, based on the current hostname setting. 619 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later). 620 * 621 * when ifp == NULL, the caller is responsible for filling scopeid. 622 */ 623int 624in6_nigroup(struct ifnet *ifp, const char *name, int namelen, 625 struct in6_addr *in6) 626{ 627 struct prison *pr; 628 const char *p; 629 u_char *q; 630 MD5_CTX ctxt; 631 u_int8_t digest[16]; 632 char l; 633 char n[64]; /* a single label must not exceed 63 chars */ 634 635 /* 636 * If no name is given and namelen is -1, 637 * we try to do the hostname lookup ourselves. 638 */ 639 if (!name && namelen == -1) { 640 pr = curthread->td_ucred->cr_prison; 641 mtx_lock(&pr->pr_mtx); 642 name = pr->pr_host; 643 namelen = strlen(name); 644 } else 645 pr = NULL; 646 if (!name || !namelen) { 647 if (pr != NULL) 648 mtx_unlock(&pr->pr_mtx); 649 return -1; 650 } 651 652 p = name; 653 while (p && *p && *p != '.' && p - name < namelen) 654 p++; 655 if (p == name || p - name > sizeof(n) - 1) { 656 if (pr != NULL) 657 mtx_unlock(&pr->pr_mtx); 658 return -1; /* label too long */ 659 } 660 l = p - name; 661 strncpy(n, name, l); 662 if (pr != NULL) 663 mtx_unlock(&pr->pr_mtx); 664 n[(int)l] = '\0'; 665 for (q = n; *q; q++) { 666 if ('A' <= *q && *q <= 'Z') 667 *q = *q - 'A' + 'a'; 668 } 669 670 /* generate 8 bytes of pseudo-random value. */ 671 bzero(&ctxt, sizeof(ctxt)); 672 MD5Init(&ctxt); 673 MD5Update(&ctxt, &l, sizeof(l)); 674 MD5Update(&ctxt, n, l); 675 MD5Final(digest, &ctxt); 676 677 bzero(in6, sizeof(*in6)); 678 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL; 679 in6->s6_addr8[11] = 2; 680 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3])); 681 if (in6_setscope(in6, ifp, NULL)) 682 return (-1); /* XXX: should not fail */ 683 684 return 0; 685} 686 687/* 688 * XXX multiple loopback interface needs more care. for instance, 689 * nodelocal address needs to be configured onto only one of them. 690 * XXX multiple link-local address case 691 * 692 * altifp - secondary EUI64 source 693 */ 694void 695in6_ifattach(struct ifnet *ifp, struct ifnet *altifp) 696{ 697 INIT_VNET_INET6(ifp->if_vnet); 698 struct in6_ifaddr *ia; 699 struct in6_addr in6; 700 701 /* some of the interfaces are inherently not IPv6 capable */ 702 switch (ifp->if_type) { 703 case IFT_PFLOG: 704 case IFT_PFSYNC: 705 case IFT_CARP: 706 return; 707 } 708 709 /* 710 * quirks based on interface type 711 */ 712 switch (ifp->if_type) { 713#ifdef IFT_STF 714 case IFT_STF: 715 /* 716 * 6to4 interface is a very special kind of beast. 717 * no multicast, no linklocal. RFC2529 specifies how to make 718 * linklocals for 6to4 interface, but there's no use and 719 * it is rather harmful to have one. 720 */ 721 goto statinit; 722#endif 723 default: 724 break; 725 } 726 727 /* 728 * usually, we require multicast capability to the interface 729 */ 730 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 731 nd6log((LOG_INFO, "in6_ifattach: " 732 "%s is not multicast capable, IPv6 not enabled\n", 733 if_name(ifp))); 734 return; 735 } 736 737 /* 738 * assign loopback address for loopback interface. 739 * XXX multiple loopback interface case. 740 */ 741 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 742 in6 = in6addr_loopback; 743 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) { 744 if (in6_ifattach_loopback(ifp) != 0) 745 return; 746 } 747 } 748 749 /* 750 * assign a link-local address, if there's none. 751 */ 752 if (V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) { 753 ia = in6ifa_ifpforlinklocal(ifp, 0); 754 if (ia == NULL) { 755 if (in6_ifattach_linklocal(ifp, altifp) == 0) { 756 /* linklocal address assigned */ 757 } else { 758 /* failed to assign linklocal address. bark? */ 759 } 760 } 761 } 762 763#ifdef IFT_STF /* XXX */ 764statinit: 765#endif 766 767 /* update dynamically. */ 768 if (V_in6_maxmtu < ifp->if_mtu) 769 V_in6_maxmtu = ifp->if_mtu; 770} 771 772/* 773 * NOTE: in6_ifdetach() does not support loopback if at this moment. 774 * We don't need this function in bsdi, because interfaces are never removed 775 * from the ifnet list in bsdi. 776 */ 777void 778in6_ifdetach(struct ifnet *ifp) 779{
|