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