1 /* $NetBSD: in6_ifattach.c,v 1.95 2015/02/23 19:15:59 martin Exp $ */ 2 /* $KAME: in6_ifattach.c,v 1.124 2001/07/18 08:32:51 jinmei Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: in6_ifattach.c,v 1.95 2015/02/23 19:15:59 martin Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kmem.h> 39 #include <sys/socket.h> 40 #include <sys/sockio.h> 41 #include <sys/kernel.h> 42 #include <sys/syslog.h> 43 #include <sys/md5.h> 44 #include <sys/socketvar.h> 45 #include <sys/cprng.h> 46 47 #include <net/if.h> 48 #include <net/if_dl.h> 49 #include <net/if_types.h> 50 #include <net/route.h> 51 52 #include <netinet/in.h> 53 #include <netinet/in_var.h> 54 55 #include <netinet/ip6.h> 56 #include <netinet6/in6_ifattach.h> 57 #include <netinet6/ip6_var.h> 58 #include <netinet6/nd6.h> 59 #include <netinet6/ip6_mroute.h> 60 #include <netinet6/scope6_var.h> 61 62 #include <net/net_osdep.h> 63 64 unsigned long in6_maxmtu = 0; 65 66 int ip6_auto_linklocal = 1; /* enable by default */ 67 68 callout_t in6_tmpaddrtimer_ch; 69 70 71 #if 0 72 static int get_hostid_ifid(struct ifnet *, struct in6_addr *); 73 #endif 74 static int get_rand_ifid(struct ifnet *, struct in6_addr *); 75 static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *); 76 static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *); 77 static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *); 78 static int in6_ifattach_loopback(struct ifnet *); 79 80 #define EUI64_GBIT 0x01 81 #define EUI64_UBIT 0x02 82 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (/*CONSTCOND*/ 0) 83 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 84 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6)) 85 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT) 86 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6)) 87 88 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6)) 89 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6)) 90 91 #define GEN_TEMPID_RETRY_MAX 5 92 93 #if 0 94 /* 95 * Generate a last-resort interface identifier from hostid. 96 * works only for certain architectures (like sparc). 97 * also, using hostid itself may constitute a privacy threat, much worse 98 * than MAC addresses (hostids are used for software licensing). 99 * maybe we should use MD5(hostid) instead. 100 * 101 * in6 - upper 64bits are preserved 102 */ 103 static int 104 get_hostid_ifid(struct ifnet *ifp, struct in6_addr *in6) 105 { 106 int off, len; 107 static const uint8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 108 static const uint8_t allone[8] = 109 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 110 111 if (!hostid) 112 return -1; 113 114 /* get up to 8 bytes from the hostid field - should we get */ 115 len = (sizeof(hostid) > 8) ? 8 : sizeof(hostid); 116 off = sizeof(*in6) - len; 117 memcpy(&in6->s6_addr[off], &hostid, len); 118 119 /* make sure we do not return anything bogus */ 120 if (memcmp(&in6->s6_addr[8], allzero, sizeof(allzero))) 121 return -1; 122 if (memcmp(&in6->s6_addr[8], allone, sizeof(allone))) 123 return -1; 124 125 /* make sure to set "u" bit to local, and "g" bit to individual. */ 126 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 127 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 128 129 /* convert EUI64 into IPv6 interface identifier */ 130 EUI64_TO_IFID(in6); 131 132 return 0; 133 } 134 #endif 135 136 /* 137 * Generate a last-resort interface identifier, when the machine has no 138 * IEEE802/EUI64 address sources. 139 * The goal here is to get an interface identifier that is 140 * (1) random enough and (2) does not change across reboot. 141 * We currently use MD5(hostname) for it. 142 */ 143 static int 144 get_rand_ifid(struct ifnet *ifp, 145 struct in6_addr *in6) /* upper 64bits are preserved */ 146 { 147 MD5_CTX ctxt; 148 u_int8_t digest[16]; 149 150 #if 0 151 /* we need at least several letters as seed for ifid */ 152 if (hostnamelen < 3) 153 return -1; 154 #endif 155 156 /* generate 8 bytes of pseudo-random value. */ 157 memset(&ctxt, 0, sizeof(ctxt)); 158 MD5Init(&ctxt); 159 MD5Update(&ctxt, (u_char *)hostname, hostnamelen); 160 MD5Final(digest, &ctxt); 161 162 /* assumes sizeof(digest) > sizeof(ifid) */ 163 memcpy(&in6->s6_addr[8], digest, 8); 164 165 /* make sure to set "u" bit to local, and "g" bit to individual. */ 166 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 167 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 168 169 /* convert EUI64 into IPv6 interface identifier */ 170 EUI64_TO_IFID(in6); 171 172 return 0; 173 } 174 175 static int 176 generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret) 177 { 178 MD5_CTX ctxt; 179 u_int8_t seed[16], digest[16], nullbuf[8]; 180 /* 181 * interface ID for subnet anycast addresses. 182 * XXX: we assume the unicast address range that requires IDs 183 * in EUI-64 format. 184 */ 185 static const uint8_t anycast_id[8] = { 0xfd, 0xff, 0xff, 0xff, 186 0xff, 0xff, 0xff, 0x80 }; 187 static const uint8_t isatap_id[4] = { 0x00, 0x00, 0x5e, 0xfe }; 188 int badid, retry = 0; 189 190 /* If there's no hisotry, start with a random seed. */ 191 memset(nullbuf, 0, sizeof(nullbuf)); 192 if (memcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) { 193 cprng_fast(seed, sizeof(seed)); 194 } else 195 memcpy(seed, seed0, 8); 196 197 /* copy the right-most 64-bits of the given address */ 198 /* XXX assumption on the size of IFID */ 199 memcpy(&seed[8], seed1, 8); 200 201 again: 202 /* for debugging purposes only */ 203 #if 0 204 { 205 int i; 206 207 printf("generate_tmp_ifid: new randomized ID from: "); 208 for (i = 0; i < 16; i++) 209 printf("%02x", seed[i]); 210 printf(" "); 211 } 212 #endif 213 214 /* generate 16 bytes of pseudo-random value. */ 215 memset(&ctxt, 0, sizeof(ctxt)); 216 MD5Init(&ctxt); 217 MD5Update(&ctxt, seed, sizeof(seed)); 218 MD5Final(digest, &ctxt); 219 220 /* 221 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (3) 222 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the 223 * left-most bit is numbered 0) to zero. 224 */ 225 memcpy(ret, digest, 8); 226 ret[0] &= ~EUI64_UBIT; 227 228 /* 229 * Reject inappropriate identifiers according to 230 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (4) 231 * At this moment, we reject following cases: 232 * - all 0 identifier 233 * - identifiers that conflict with reserved subnet anycast addresses, 234 * which are defined in RFC 2526. 235 * - identifiers that conflict with ISATAP addresses 236 * - identifiers used in our own addresses 237 */ 238 badid = 0; 239 if (memcmp(nullbuf, ret, sizeof(nullbuf)) == 0) 240 badid = 1; 241 else if (memcmp(anycast_id, ret, 7) == 0 && 242 (anycast_id[7] & ret[7]) == anycast_id[7]) { 243 badid = 1; 244 } else if (memcmp(isatap_id, ret, sizeof(isatap_id)) == 0) 245 badid = 1; 246 else { 247 struct in6_ifaddr *ia; 248 249 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 250 if (!memcmp(&ia->ia_addr.sin6_addr.s6_addr[8], 251 ret, 8)) { 252 badid = 1; 253 break; 254 } 255 } 256 } 257 258 /* 259 * In the event that an unacceptable identifier has been generated, 260 * restart the process, using the right-most 64 bits of the MD5 digest 261 * obtained in place of the history value. 262 */ 263 if (badid) { 264 /* for debugging purposes only */ 265 #if 0 266 { 267 int i; 268 269 printf("unacceptable random ID: "); 270 for (i = 0; i < 16; i++) 271 printf("%02x", digest[i]); 272 printf("\n"); 273 } 274 #endif 275 276 if (++retry < GEN_TEMPID_RETRY_MAX) { 277 memcpy(seed, &digest[8], 8); 278 goto again; 279 } else { 280 /* 281 * We're so unlucky. Give up for now, and return 282 * all 0 IDs to tell the caller not to make a 283 * temporary address. 284 */ 285 nd6log((LOG_NOTICE, 286 "generate_tmp_ifid: never found a good ID\n")); 287 memset(ret, 0, 8); 288 } 289 } 290 291 /* 292 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (6) 293 * Take the rightmost 64-bits of the MD5 digest and save them in 294 * stable storage as the history value to be used in the next 295 * iteration of the algorithm. 296 */ 297 memcpy(seed0, &digest[8], 8); 298 299 /* for debugging purposes only */ 300 #if 0 301 { 302 int i; 303 304 printf("to: "); 305 for (i = 0; i < 16; i++) 306 printf("%02x", digest[i]); 307 printf("\n"); 308 } 309 #endif 310 311 return 0; 312 } 313 314 /* 315 * Get interface identifier for the specified interface. 316 * 317 * in6 - upper 64bits are preserved 318 */ 319 int 320 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6) 321 { 322 struct ifaddr *ifa; 323 const struct sockaddr_dl *sdl = NULL, *tsdl; 324 const char *addr; 325 size_t addrlen; 326 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 327 static u_int8_t allone[8] = 328 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 329 330 IFADDR_FOREACH(ifa, ifp) { 331 if (ifa->ifa_addr->sa_family != AF_LINK) 332 continue; 333 tsdl = satocsdl(ifa->ifa_addr); 334 if (tsdl == NULL || tsdl->sdl_alen == 0) 335 continue; 336 if (sdl == NULL || ifa == ifp->if_dl || ifa == ifp->if_hwdl) 337 sdl = tsdl; 338 if (ifa == ifp->if_hwdl) 339 break; 340 } 341 342 if (sdl == NULL) 343 return -1; 344 345 addr = CLLADDR(sdl); 346 addrlen = sdl->sdl_alen; 347 348 switch (ifp->if_type) { 349 case IFT_IEEE1394: 350 case IFT_IEEE80211: 351 /* IEEE1394 uses 16byte length address starting with EUI64 */ 352 if (addrlen > 8) 353 addrlen = 8; 354 break; 355 default: 356 break; 357 } 358 359 /* get EUI64 */ 360 switch (ifp->if_type) { 361 /* IEEE802/EUI64 cases - what others? */ 362 case IFT_ETHER: 363 case IFT_FDDI: 364 case IFT_ATM: 365 case IFT_IEEE1394: 366 case IFT_IEEE80211: 367 /* look at IEEE802/EUI64 only */ 368 if (addrlen != 8 && addrlen != 6) 369 return -1; 370 371 /* 372 * check for invalid MAC address - on bsdi, we see it a lot 373 * since wildboar configures all-zero MAC on pccard before 374 * card insertion. 375 */ 376 if (memcmp(addr, allzero, addrlen) == 0) 377 return -1; 378 if (memcmp(addr, allone, addrlen) == 0) 379 return -1; 380 381 /* make EUI64 address */ 382 if (addrlen == 8) 383 memcpy(&in6->s6_addr[8], addr, 8); 384 else if (addrlen == 6) { 385 in6->s6_addr[8] = addr[0]; 386 in6->s6_addr[9] = addr[1]; 387 in6->s6_addr[10] = addr[2]; 388 in6->s6_addr[11] = 0xff; 389 in6->s6_addr[12] = 0xfe; 390 in6->s6_addr[13] = addr[3]; 391 in6->s6_addr[14] = addr[4]; 392 in6->s6_addr[15] = addr[5]; 393 } 394 break; 395 396 case IFT_ARCNET: 397 if (addrlen != 1) 398 return -1; 399 if (!addr[0]) 400 return -1; 401 402 memset(&in6->s6_addr[8], 0, 8); 403 in6->s6_addr[15] = addr[0]; 404 405 /* 406 * due to insufficient bitwidth, we mark it local. 407 */ 408 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 409 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 410 break; 411 412 case IFT_GIF: 413 #ifdef IFT_STF 414 case IFT_STF: 415 #endif 416 /* 417 * RFC2893 says: "SHOULD use IPv4 address as ifid source". 418 * however, IPv4 address is not very suitable as unique 419 * identifier source (can be renumbered). 420 * we don't do this. 421 */ 422 return -1; 423 424 default: 425 return -1; 426 } 427 428 /* sanity check: g bit must not indicate "group" */ 429 if (EUI64_GROUP(in6)) 430 return -1; 431 432 /* convert EUI64 into IPv6 interface identifier */ 433 EUI64_TO_IFID(in6); 434 435 /* 436 * sanity check: ifid must not be all zero, avoid conflict with 437 * subnet router anycast 438 */ 439 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && 440 memcmp(&in6->s6_addr[9], allzero, 7) == 0) { 441 return -1; 442 } 443 444 return 0; 445 } 446 447 /* 448 * Get interface identifier for the specified interface. If it is not 449 * available on ifp0, borrow interface identifier from other information 450 * sources. 451 * 452 * altifp - secondary EUI64 source 453 */ 454 static int 455 get_ifid(struct ifnet *ifp0, struct ifnet *altifp, 456 struct in6_addr *in6) 457 { 458 struct ifnet *ifp; 459 460 /* first, try to get it from the interface itself */ 461 if (in6_get_hw_ifid(ifp0, in6) == 0) { 462 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 463 if_name(ifp0))); 464 goto success; 465 } 466 467 /* try secondary EUI64 source. this basically is for ATM PVC */ 468 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) { 469 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", 470 if_name(ifp0), if_name(altifp))); 471 goto success; 472 } 473 474 /* next, try to get it from some other hardware interface */ 475 IFNET_FOREACH(ifp) { 476 if (ifp == ifp0) 477 continue; 478 if (in6_get_hw_ifid(ifp, in6) != 0) 479 continue; 480 481 /* 482 * to borrow ifid from other interface, ifid needs to be 483 * globally unique 484 */ 485 if (IFID_UNIVERSAL(in6)) { 486 nd6log((LOG_DEBUG, 487 "%s: borrow interface identifier from %s\n", 488 if_name(ifp0), if_name(ifp))); 489 goto success; 490 } 491 } 492 493 #if 0 494 /* get from hostid - only for certain architectures */ 495 if (get_hostid_ifid(ifp, in6) == 0) { 496 nd6log((LOG_DEBUG, 497 "%s: interface identifier generated by hostid\n", 498 if_name(ifp0))); 499 goto success; 500 } 501 #endif 502 503 /* last resort: get from random number source */ 504 if (get_rand_ifid(ifp, in6) == 0) { 505 nd6log((LOG_DEBUG, 506 "%s: interface identifier generated by random number\n", 507 if_name(ifp0))); 508 goto success; 509 } 510 511 printf("%s: failed to get interface identifier\n", if_name(ifp0)); 512 return -1; 513 514 success: 515 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 516 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 517 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 518 in6->s6_addr[14], in6->s6_addr[15])); 519 return 0; 520 } 521 522 /* 523 * altifp - secondary EUI64 source 524 */ 525 526 static int 527 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp) 528 { 529 struct in6_ifaddr *ia __diagused; 530 struct in6_aliasreq ifra; 531 struct nd_prefixctl prc0; 532 int i, error; 533 534 /* 535 * configure link-local address. 536 */ 537 memset(&ifra, 0, sizeof(ifra)); 538 539 /* 540 * in6_update_ifa() does not use ifra_name, but we accurately set it 541 * for safety. 542 */ 543 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 544 545 ifra.ifra_addr.sin6_family = AF_INET6; 546 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 547 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000); 548 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 549 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 550 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; 551 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); 552 } else { 553 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) { 554 nd6log((LOG_ERR, 555 "%s: no ifid available\n", if_name(ifp))); 556 return -1; 557 } 558 } 559 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL)) 560 return -1; 561 562 sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask64, 0, 0, 0); 563 /* link-local addresses should NEVER expire. */ 564 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 565 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 566 567 /* 568 * Now call in6_update_ifa() to do a bunch of procedures to configure 569 * a link-local address. We can set the 3rd argument to NULL, because 570 * we know there's no other link-local address on the interface 571 * and therefore we are adding one (instead of updating one). 572 */ 573 if ((error = in6_update_ifa(ifp, &ifra, NULL, 574 IN6_IFAUPDATE_DADDELAY)) != 0) { 575 /* 576 * XXX: When the interface does not support IPv6, this call 577 * would fail in the SIOCINITIFADDR ioctl. I believe the 578 * notification is rather confusing in this case, so just 579 * suppress it. (jinmei@kame.net 20010130) 580 */ 581 if (error != EAFNOSUPPORT) 582 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to " 583 "configure a link-local address on %s " 584 "(errno=%d)\n", 585 if_name(ifp), error)); 586 return -1; 587 } 588 589 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */ 590 KASSERTMSG(ia, "ia == NULL in in6_ifattach_linklocal"); 591 592 /* 593 * Make the link-local prefix (fe80::/64%link) as on-link. 594 * Since we'd like to manage prefixes separately from addresses, 595 * we make an ND6 prefix structure for the link-local prefix, 596 * and add it to the prefix list as a never-expire prefix. 597 * XXX: this change might affect some existing code base... 598 */ 599 memset(&prc0, 0, sizeof(prc0)); 600 prc0.ndprc_ifp = ifp; 601 /* this should be 64 at this moment. */ 602 prc0.ndprc_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); 603 prc0.ndprc_prefix = ifra.ifra_addr; 604 /* apply the mask for safety. (nd6_prelist_add will apply it again) */ 605 for (i = 0; i < 4; i++) { 606 prc0.ndprc_prefix.sin6_addr.s6_addr32[i] &= 607 in6mask64.s6_addr32[i]; 608 } 609 /* 610 * Initialize parameters. The link-local prefix must always be 611 * on-link, and its lifetimes never expire. 612 */ 613 prc0.ndprc_raf_onlink = 1; 614 prc0.ndprc_raf_auto = 1; /* probably meaningless */ 615 prc0.ndprc_vltime = ND6_INFINITE_LIFETIME; 616 prc0.ndprc_pltime = ND6_INFINITE_LIFETIME; 617 /* 618 * Since there is no other link-local addresses, nd6_prefix_lookup() 619 * probably returns NULL. However, we cannot always expect the result. 620 * For example, if we first remove the (only) existing link-local 621 * address, and then reconfigure another one, the prefix is still 622 * valid with referring to the old link-local address. 623 */ 624 if (nd6_prefix_lookup(&prc0) == NULL) { 625 if ((error = nd6_prelist_add(&prc0, NULL, NULL)) != 0) 626 return error; 627 } 628 629 return 0; 630 } 631 632 /* 633 * ifp - mut be IFT_LOOP 634 */ 635 636 static int 637 in6_ifattach_loopback(struct ifnet *ifp) 638 { 639 struct in6_aliasreq ifra; 640 int error; 641 642 memset(&ifra, 0, sizeof(ifra)); 643 644 /* 645 * in6_update_ifa() does not use ifra_name, but we accurately set it 646 * for safety. 647 */ 648 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 649 650 sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask128, 0, 0, 0); 651 652 /* 653 * Always initialize ia_dstaddr (= broadcast address) to loopback 654 * address. Follows IPv4 practice - see in_ifinit(). 655 */ 656 sockaddr_in6_init(&ifra.ifra_dstaddr, &in6addr_loopback, 0, 0, 0); 657 658 sockaddr_in6_init(&ifra.ifra_addr, &in6addr_loopback, 0, 0, 0); 659 660 /* the loopback address should NEVER expire. */ 661 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 662 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 663 664 /* we don't need to perform DAD on loopback interfaces. */ 665 ifra.ifra_flags |= IN6_IFF_NODAD; 666 667 /* 668 * We are sure that this is a newly assigned address, so we can set 669 * NULL to the 3rd arg. 670 */ 671 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) { 672 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure " 673 "the loopback address on %s (errno=%d)\n", 674 if_name(ifp), error)); 675 return -1; 676 } 677 678 return 0; 679 } 680 681 /* 682 * compute NI group address, based on the current hostname setting. 683 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later). 684 * 685 * when ifp == NULL, the caller is responsible for filling scopeid. 686 */ 687 int 688 in6_nigroup(struct ifnet *ifp, const char *name, int namelen, 689 struct sockaddr_in6 *sa6) 690 { 691 const char *p; 692 u_int8_t *q; 693 MD5_CTX ctxt; 694 u_int8_t digest[16]; 695 u_int8_t l; 696 u_int8_t n[64]; /* a single label must not exceed 63 chars */ 697 698 if (!namelen || !name) 699 return -1; 700 701 p = name; 702 while (p && *p && *p != '.' && p - name < namelen) 703 p++; 704 if (p - name > sizeof(n) - 1) 705 return -1; /* label too long */ 706 l = p - name; 707 strncpy((char *)n, name, l); 708 n[(int)l] = '\0'; 709 for (q = n; *q; q++) { 710 if ('A' <= *q && *q <= 'Z') 711 *q = *q - 'A' + 'a'; 712 } 713 714 /* generate 8 bytes of pseudo-random value. */ 715 memset(&ctxt, 0, sizeof(ctxt)); 716 MD5Init(&ctxt); 717 MD5Update(&ctxt, &l, sizeof(l)); 718 MD5Update(&ctxt, n, l); 719 MD5Final(digest, &ctxt); 720 721 memset(sa6, 0, sizeof(*sa6)); 722 sa6->sin6_family = AF_INET6; 723 sa6->sin6_len = sizeof(*sa6); 724 sa6->sin6_addr.s6_addr16[0] = htons(0xff02); 725 sa6->sin6_addr.s6_addr8[11] = 2; 726 memcpy(&sa6->sin6_addr.s6_addr32[3], digest, 727 sizeof(sa6->sin6_addr.s6_addr32[3])); 728 if (in6_setscope(&sa6->sin6_addr, ifp, NULL)) 729 return -1; /* XXX: should not fail */ 730 731 return 0; 732 } 733 734 /* 735 * XXX multiple loopback interface needs more care. for instance, 736 * nodelocal address needs to be configured onto only one of them. 737 * XXX multiple link-local address case 738 * 739 * altifp - secondary EUI64 source 740 */ 741 void 742 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp) 743 { 744 struct in6_ifaddr *ia; 745 struct in6_addr in6; 746 747 /* some of the interfaces are inherently not IPv6 capable */ 748 switch (ifp->if_type) { 749 case IFT_BRIDGE: 750 #ifdef IFT_PFLOG 751 case IFT_PFLOG: 752 #endif 753 #ifdef IFT_PFSYNC 754 case IFT_PFSYNC: 755 #endif 756 ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL; 757 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED; 758 return; 759 } 760 761 /* 762 * if link mtu is too small, don't try to configure IPv6. 763 * remember there could be some link-layer that has special 764 * fragmentation logic. 765 */ 766 if (ifp->if_mtu < IPV6_MMTU) { 767 nd6log((LOG_INFO, "in6_ifattach: " 768 "%s has too small MTU, IPv6 not enabled\n", 769 if_name(ifp))); 770 return; 771 } 772 773 /* create a multicast kludge storage (if we have not had one) */ 774 in6_createmkludge(ifp); 775 776 /* 777 * quirks based on interface type 778 */ 779 switch (ifp->if_type) { 780 #ifdef IFT_STF 781 case IFT_STF: 782 /* 783 * 6to4 interface is a very special kind of beast. 784 * no multicast, no linklocal. RFC2529 specifies how to make 785 * linklocals for 6to4 interface, but there's no use and 786 * it is rather harmful to have one. 787 */ 788 ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL; 789 return; 790 #endif 791 case IFT_CARP: 792 return; 793 default: 794 break; 795 } 796 797 /* 798 * usually, we require multicast capability to the interface 799 */ 800 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 801 nd6log((LOG_INFO, "in6_ifattach: " 802 "%s is not multicast capable, IPv6 not enabled\n", 803 if_name(ifp))); 804 return; 805 } 806 807 /* 808 * assign loopback address for loopback interface. 809 * XXX multiple loopback interface case. 810 */ 811 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 812 in6 = in6addr_loopback; 813 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) { 814 if (in6_ifattach_loopback(ifp) != 0) 815 return; 816 } 817 } 818 819 /* 820 * assign a link-local address, if there's none. 821 */ 822 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 823 ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) 824 { 825 ia = in6ifa_ifpforlinklocal(ifp, 0); 826 if (ia == NULL && in6_ifattach_linklocal(ifp, altifp) != 0) { 827 printf("%s: cannot assign link-local address\n", 828 ifp->if_xname); 829 } 830 } 831 } 832 833 /* 834 * NOTE: in6_ifdetach() does not support loopback if at this moment. 835 * We don't need this function in bsdi, because interfaces are never removed 836 * from the ifnet list in bsdi. 837 */ 838 void 839 in6_ifdetach(struct ifnet *ifp) 840 { 841 struct in6_ifaddr *ia, *oia; 842 struct ifaddr *ifa, *next; 843 struct rtentry *rt; 844 short rtflags; 845 struct in6_multi_mship *imm; 846 847 /* remove ip6_mrouter stuff */ 848 ip6_mrouter_detach(ifp); 849 850 /* remove neighbor management table */ 851 nd6_purge(ifp, NULL); 852 853 /* XXX this code is duplicated in in6_purgeif() --dyoung */ 854 /* nuke any of IPv6 addresses we have */ 855 if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr); 856 857 /* XXX isn't this code is redundant, given the above? --dyoung */ 858 /* XXX doesn't this code replicate code in in6_purgeaddr() ? --dyoung */ 859 /* undo everything done by in6_ifattach(), just in case */ 860 for (ifa = IFADDR_FIRST(ifp); ifa != NULL; ifa = next) { 861 next = IFADDR_NEXT(ifa); 862 863 if (ifa->ifa_addr->sa_family != AF_INET6 864 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) { 865 continue; 866 } 867 868 ia = (struct in6_ifaddr *)ifa; 869 870 /* 871 * leave from multicast groups we have joined for the interface 872 */ 873 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) { 874 LIST_REMOVE(imm, i6mm_chain); 875 in6_leavegroup(imm); 876 } 877 878 /* remove from the routing table */ 879 if ((ia->ia_flags & IFA_ROUTE) && 880 (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) { 881 rtflags = rt->rt_flags; 882 rtfree(rt); 883 rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr, 884 (struct sockaddr *)&ia->ia_addr, 885 (struct sockaddr *)&ia->ia_prefixmask, 886 rtflags, NULL); 887 } 888 889 /* remove from the linked list */ 890 ifa_remove(ifp, &ia->ia_ifa); 891 892 /* also remove from the IPv6 address chain(itojun&jinmei) */ 893 oia = ia; 894 if (oia == (ia = in6_ifaddr)) 895 in6_ifaddr = ia->ia_next; 896 else { 897 while (ia->ia_next && (ia->ia_next != oia)) 898 ia = ia->ia_next; 899 if (ia->ia_next) 900 ia->ia_next = oia->ia_next; 901 else { 902 nd6log((LOG_ERR, 903 "%s: didn't unlink in6ifaddr from list\n", 904 if_name(ifp))); 905 } 906 } 907 908 ifafree(&oia->ia_ifa); 909 } 910 911 /* cleanup multicast address kludge table, if there is any */ 912 in6_purgemkludge(ifp); 913 914 /* 915 * remove neighbor management table. we call it twice just to make 916 * sure we nuke everything. maybe we need just one call. 917 * XXX: since the first call did not release addresses, some prefixes 918 * might remain. We should call nd6_purge() again to release the 919 * prefixes after removing all addresses above. 920 * (Or can we just delay calling nd6_purge until at this point?) 921 */ 922 nd6_purge(ifp, NULL); 923 } 924 925 int 926 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf, 927 const u_int8_t *baseid, int generate) 928 { 929 u_int8_t nullbuf[8]; 930 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 931 932 memset(nullbuf, 0, sizeof(nullbuf)); 933 if (memcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) { 934 /* we've never created a random ID. Create a new one. */ 935 generate = 1; 936 } 937 938 if (generate) { 939 memcpy(ndi->randomseed1, baseid, sizeof(ndi->randomseed1)); 940 941 /* generate_tmp_ifid will update seedn and buf */ 942 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1, 943 ndi->randomid); 944 } 945 memcpy(retbuf, ndi->randomid, 8); 946 if (generate && memcmp(retbuf, nullbuf, sizeof(nullbuf)) == 0) { 947 /* generate_tmp_ifid could not found a good ID. */ 948 return -1; 949 } 950 951 return 0; 952 } 953 954 void 955 in6_tmpaddrtimer(void *ignored_arg) 956 { 957 struct nd_ifinfo *ndi; 958 u_int8_t nullbuf[8]; 959 struct ifnet *ifp; 960 961 mutex_enter(softnet_lock); 962 KERNEL_LOCK(1, NULL); 963 964 callout_reset(&in6_tmpaddrtimer_ch, 965 (ip6_temp_preferred_lifetime - ip6_desync_factor - 966 ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL); 967 968 memset(nullbuf, 0, sizeof(nullbuf)); 969 IFNET_FOREACH(ifp) { 970 ndi = ND_IFINFO(ifp); 971 if (memcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) { 972 /* 973 * We've been generating a random ID on this interface. 974 * Create a new one. 975 */ 976 (void)generate_tmp_ifid(ndi->randomseed0, 977 ndi->randomseed1, ndi->randomid); 978 } 979 } 980 981 KERNEL_UNLOCK_ONE(NULL); 982 mutex_exit(softnet_lock); 983 } 984