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