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