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