in6_ifattach.c revision 185348
1/*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $ 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/sys/netinet6/in6_ifattach.c 185348 2008-11-26 22:32:07Z zec $"); 34 35#include <sys/param.h> 36#include <sys/systm.h> 37#include <sys/malloc.h> 38#include <sys/socket.h> 39#include <sys/sockio.h> 40#include <sys/kernel.h> 41#include <sys/syslog.h> 42#include <sys/md5.h> 43#include <sys/vimage.h> 44 45#include <net/if.h> 46#include <net/if_dl.h> 47#include <net/if_types.h> 48#include <net/route.h> 49 50#include <netinet/in.h> 51#include <netinet/in_var.h> 52#include <netinet/if_ether.h> 53#include <netinet/in_pcb.h> 54 55#include <netinet/ip6.h> 56#include <netinet6/ip6_var.h> 57#include <netinet6/in6_var.h> 58#include <netinet6/in6_pcb.h> 59#include <netinet6/in6_ifattach.h> 60#include <netinet6/ip6_var.h> 61#include <netinet6/nd6.h> 62#include <netinet6/scope6_var.h> 63 64#ifdef VIMAGE_GLOBALS 65unsigned long in6_maxmtu; 66int ip6_auto_linklocal; 67struct callout in6_tmpaddrtimer_ch; 68extern struct inpcbinfo udbinfo; 69extern struct inpcbinfo ripcbinfo; 70#endif 71 72static int get_rand_ifid(struct ifnet *, struct in6_addr *); 73static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *); 74static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *); 75static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *); 76static int in6_ifattach_loopback(struct ifnet *); 77static void in6_purgemaddrs(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 (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/* 91 * Generate a last-resort interface identifier, when the machine has no 92 * IEEE802/EUI64 address sources. 93 * The goal here is to get an interface identifier that is 94 * (1) random enough and (2) does not change across reboot. 95 * We currently use MD5(hostname) for it. 96 * 97 * in6 - upper 64bits are preserved 98 */ 99static int 100get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6) 101{ 102 INIT_VPROCG(TD_TO_VPROCG(curthread)); /* XXX V_hostname needs this */ 103 MD5_CTX ctxt; 104 u_int8_t digest[16]; 105 int hostnamelen; 106 107 mtx_lock(&hostname_mtx); 108 hostnamelen = strlen(V_hostname); 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, V_hostname, hostnamelen); 119 mtx_unlock(&hostname_mtx); 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(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret) 137{ 138 INIT_VNET_INET6(curvnet); 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 history, 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 * 219 * in6 - upper 64bits are preserved 220 */ 221int 222in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6) 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 * 352 * altifp - secondary EUI64 source 353 */ 354static int 355get_ifid(struct ifnet *ifp0, struct ifnet *altifp, 356 struct in6_addr *in6) 357{ 358 INIT_VNET_NET(ifp0->if_vnet); 359 INIT_VNET_INET6(ifp0->if_vnet); 360 struct ifnet *ifp; 361 362 /* first, try to get it from the interface itself */ 363 if (in6_get_hw_ifid(ifp0, in6) == 0) { 364 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 365 if_name(ifp0))); 366 goto success; 367 } 368 369 /* try secondary EUI64 source. this basically is for ATM PVC */ 370 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) { 371 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", 372 if_name(ifp0), if_name(altifp))); 373 goto success; 374 } 375 376 /* next, try to get it from some other hardware interface */ 377 IFNET_RLOCK(); 378 for (ifp = V_ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) { 379 if (ifp == ifp0) 380 continue; 381 if (in6_get_hw_ifid(ifp, in6) != 0) 382 continue; 383 384 /* 385 * to borrow ifid from other interface, ifid needs to be 386 * globally unique 387 */ 388 if (IFID_UNIVERSAL(in6)) { 389 nd6log((LOG_DEBUG, 390 "%s: borrow interface identifier from %s\n", 391 if_name(ifp0), if_name(ifp))); 392 IFNET_RUNLOCK(); 393 goto success; 394 } 395 } 396 IFNET_RUNLOCK(); 397 398 /* last resort: get from random number source */ 399 if (get_rand_ifid(ifp, in6) == 0) { 400 nd6log((LOG_DEBUG, 401 "%s: interface identifier generated by random number\n", 402 if_name(ifp0))); 403 goto success; 404 } 405 406 printf("%s: failed to get interface identifier\n", if_name(ifp0)); 407 return -1; 408 409success: 410 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 411 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 412 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 413 in6->s6_addr[14], in6->s6_addr[15])); 414 return 0; 415} 416 417/* 418 * altifp - secondary EUI64 source 419 */ 420static int 421in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp) 422{ 423 INIT_VNET_INET6(curvnet); 424 struct in6_ifaddr *ia; 425 struct in6_aliasreq ifra; 426 struct nd_prefixctl pr0; 427 int i, error; 428 429 /* 430 * configure link-local address. 431 */ 432 bzero(&ifra, sizeof(ifra)); 433 434 /* 435 * in6_update_ifa() does not use ifra_name, but we accurately set it 436 * for safety. 437 */ 438 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 439 440 ifra.ifra_addr.sin6_family = AF_INET6; 441 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 442 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000); 443 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 444 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 445 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; 446 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); 447 } else { 448 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) { 449 nd6log((LOG_ERR, 450 "%s: no ifid available\n", if_name(ifp))); 451 return (-1); 452 } 453 } 454 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL)) 455 return (-1); 456 457 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 458 ifra.ifra_prefixmask.sin6_family = AF_INET6; 459 ifra.ifra_prefixmask.sin6_addr = in6mask64; 460 /* link-local addresses should NEVER expire. */ 461 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 462 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 463 464 /* 465 * Now call in6_update_ifa() to do a bunch of procedures to configure 466 * a link-local address. We can set the 3rd argument to NULL, because 467 * we know there's no other link-local address on the interface 468 * and therefore we are adding one (instead of updating one). 469 */ 470 if ((error = in6_update_ifa(ifp, &ifra, NULL, 471 IN6_IFAUPDATE_DADDELAY)) != 0) { 472 /* 473 * XXX: When the interface does not support IPv6, this call 474 * would fail in the SIOCSIFADDR ioctl. I believe the 475 * notification is rather confusing in this case, so just 476 * suppress it. (jinmei@kame.net 20010130) 477 */ 478 if (error != EAFNOSUPPORT) 479 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to " 480 "configure a link-local address on %s " 481 "(errno=%d)\n", 482 if_name(ifp), error)); 483 return (-1); 484 } 485 486 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */ 487#ifdef DIAGNOSTIC 488 if (!ia) { 489 panic("ia == NULL in in6_ifattach_linklocal"); 490 /* NOTREACHED */ 491 } 492#endif 493 494 /* 495 * Make the link-local prefix (fe80::%link/64) as on-link. 496 * Since we'd like to manage prefixes separately from addresses, 497 * we make an ND6 prefix structure for the link-local prefix, 498 * and add it to the prefix list as a never-expire prefix. 499 * XXX: this change might affect some existing code base... 500 */ 501 bzero(&pr0, sizeof(pr0)); 502 pr0.ndpr_ifp = ifp; 503 /* this should be 64 at this moment. */ 504 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); 505 pr0.ndpr_prefix = ifra.ifra_addr; 506 /* apply the mask for safety. (nd6_prelist_add will apply it again) */ 507 for (i = 0; i < 4; i++) { 508 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 509 in6mask64.s6_addr32[i]; 510 } 511 /* 512 * Initialize parameters. The link-local prefix must always be 513 * on-link, and its lifetimes never expire. 514 */ 515 pr0.ndpr_raf_onlink = 1; 516 pr0.ndpr_raf_auto = 1; /* probably meaningless */ 517 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME; 518 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME; 519 /* 520 * Since there is no other link-local addresses, nd6_prefix_lookup() 521 * probably returns NULL. However, we cannot always expect the result. 522 * For example, if we first remove the (only) existing link-local 523 * address, and then reconfigure another one, the prefix is still 524 * valid with referring to the old link-local address. 525 */ 526 if (nd6_prefix_lookup(&pr0) == NULL) { 527 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0) 528 return (error); 529 } 530 531 return 0; 532} 533 534/* 535 * ifp - must be IFT_LOOP 536 */ 537static int 538in6_ifattach_loopback(struct ifnet *ifp) 539{ 540 INIT_VNET_INET6(curvnet); 541 struct in6_aliasreq ifra; 542 int error; 543 544 bzero(&ifra, sizeof(ifra)); 545 546 /* 547 * in6_update_ifa() does not use ifra_name, but we accurately set it 548 * for safety. 549 */ 550 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 551 552 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 553 ifra.ifra_prefixmask.sin6_family = AF_INET6; 554 ifra.ifra_prefixmask.sin6_addr = in6mask128; 555 556 /* 557 * Always initialize ia_dstaddr (= broadcast address) to loopback 558 * address. Follows IPv4 practice - see in_ifinit(). 559 */ 560 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 561 ifra.ifra_dstaddr.sin6_family = AF_INET6; 562 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback; 563 564 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 565 ifra.ifra_addr.sin6_family = AF_INET6; 566 ifra.ifra_addr.sin6_addr = in6addr_loopback; 567 568 /* the loopback address should NEVER expire. */ 569 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 570 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 571 572 /* we don't need to perform DAD on loopback interfaces. */ 573 ifra.ifra_flags |= IN6_IFF_NODAD; 574 575 /* skip registration to the prefix list. XXX should be temporary. */ 576 ifra.ifra_flags |= IN6_IFF_NOPFX; 577 578 /* 579 * We are sure that this is a newly assigned address, so we can set 580 * NULL to the 3rd arg. 581 */ 582 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) { 583 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure " 584 "the loopback address on %s (errno=%d)\n", 585 if_name(ifp), error)); 586 return (-1); 587 } 588 589 return 0; 590} 591 592/* 593 * compute NI group address, based on the current hostname setting. 594 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later). 595 * 596 * when ifp == NULL, the caller is responsible for filling scopeid. 597 */ 598int 599in6_nigroup(struct ifnet *ifp, const char *name, int namelen, 600 struct in6_addr *in6) 601{ 602 const char *p; 603 u_char *q; 604 MD5_CTX ctxt; 605 u_int8_t digest[16]; 606 char l; 607 char n[64]; /* a single label must not exceed 63 chars */ 608 609 if (!namelen || !name) 610 return -1; 611 612 p = name; 613 while (p && *p && *p != '.' && p - name < namelen) 614 p++; 615 if (p - name > sizeof(n) - 1) 616 return -1; /* label too long */ 617 l = p - name; 618 strncpy(n, name, l); 619 n[(int)l] = '\0'; 620 for (q = n; *q; q++) { 621 if ('A' <= *q && *q <= 'Z') 622 *q = *q - 'A' + 'a'; 623 } 624 625 /* generate 8 bytes of pseudo-random value. */ 626 bzero(&ctxt, sizeof(ctxt)); 627 MD5Init(&ctxt); 628 MD5Update(&ctxt, &l, sizeof(l)); 629 MD5Update(&ctxt, n, l); 630 MD5Final(digest, &ctxt); 631 632 bzero(in6, sizeof(*in6)); 633 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL; 634 in6->s6_addr8[11] = 2; 635 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3])); 636 if (in6_setscope(in6, ifp, NULL)) 637 return (-1); /* XXX: should not fail */ 638 639 return 0; 640} 641 642/* 643 * XXX multiple loopback interface needs more care. for instance, 644 * nodelocal address needs to be configured onto only one of them. 645 * XXX multiple link-local address case 646 * 647 * altifp - secondary EUI64 source 648 */ 649void 650in6_ifattach(struct ifnet *ifp, struct ifnet *altifp) 651{ 652 INIT_VNET_INET6(ifp->if_vnet); 653 struct in6_ifaddr *ia; 654 struct in6_addr in6; 655 656 /* some of the interfaces are inherently not IPv6 capable */ 657 switch (ifp->if_type) { 658 case IFT_PFLOG: 659 case IFT_PFSYNC: 660 case IFT_CARP: 661 return; 662 } 663 664 /* 665 * quirks based on interface type 666 */ 667 switch (ifp->if_type) { 668#ifdef IFT_STF 669 case IFT_STF: 670 /* 671 * 6to4 interface is a very special kind of beast. 672 * no multicast, no linklocal. RFC2529 specifies how to make 673 * linklocals for 6to4 interface, but there's no use and 674 * it is rather harmful to have one. 675 */ 676 goto statinit; 677#endif 678 default: 679 break; 680 } 681 682 /* 683 * usually, we require multicast capability to the interface 684 */ 685 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 686 nd6log((LOG_INFO, "in6_ifattach: " 687 "%s is not multicast capable, IPv6 not enabled\n", 688 if_name(ifp))); 689 return; 690 } 691 692 /* 693 * assign loopback address for loopback interface. 694 * XXX multiple loopback interface case. 695 */ 696 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 697 in6 = in6addr_loopback; 698 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) { 699 if (in6_ifattach_loopback(ifp) != 0) 700 return; 701 } 702 } 703 704 /* 705 * assign a link-local address, if there's none. 706 */ 707 if (V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) { 708 ia = in6ifa_ifpforlinklocal(ifp, 0); 709 if (ia == NULL) { 710 if (in6_ifattach_linklocal(ifp, altifp) == 0) { 711 /* linklocal address assigned */ 712 } else { 713 /* failed to assign linklocal address. bark? */ 714 } 715 } 716 } 717 718#ifdef IFT_STF /* XXX */ 719statinit: 720#endif 721 722 /* update dynamically. */ 723 if (V_in6_maxmtu < ifp->if_mtu) 724 V_in6_maxmtu = ifp->if_mtu; 725} 726 727/* 728 * NOTE: in6_ifdetach() does not support loopback if at this moment. 729 * We don't need this function in bsdi, because interfaces are never removed 730 * from the ifnet list in bsdi. 731 */ 732void 733in6_ifdetach(struct ifnet *ifp) 734{ 735 INIT_VNET_NET(ifp->if_vnet); 736 INIT_VNET_INET(ifp->if_vnet); 737 INIT_VNET_INET6(ifp->if_vnet); 738 struct in6_ifaddr *ia, *oia; 739 struct ifaddr *ifa, *next; 740 struct rtentry *rt; 741 short rtflags; 742 struct sockaddr_in6 sin6; 743 struct in6_multi_mship *imm; 744 745 /* remove neighbor management table */ 746 nd6_purge(ifp); 747 748 /* nuke any of IPv6 addresses we have */ 749 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) { 750 next = ifa->ifa_list.tqe_next; 751 if (ifa->ifa_addr->sa_family != AF_INET6) 752 continue; 753 in6_purgeaddr(ifa); 754 } 755 756 /* undo everything done by in6_ifattach(), just in case */ 757 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) { 758 next = ifa->ifa_list.tqe_next; 759 760 if (ifa->ifa_addr->sa_family != AF_INET6 761 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) { 762 continue; 763 } 764 765 ia = (struct in6_ifaddr *)ifa; 766 767 /* 768 * leave from multicast groups we have joined for the interface 769 */ 770 while ((imm = ia->ia6_memberships.lh_first) != NULL) { 771 LIST_REMOVE(imm, i6mm_chain); 772 in6_leavegroup(imm); 773 } 774 775 /* remove from the routing table */ 776 if ((ia->ia_flags & IFA_ROUTE) && 777 (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) { 778 rtflags = rt->rt_flags; 779 rtfree(rt); 780 rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr, 781 (struct sockaddr *)&ia->ia_addr, 782 (struct sockaddr *)&ia->ia_prefixmask, 783 rtflags, (struct rtentry **)0); 784 } 785 786 /* remove from the linked list */ 787 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list); 788 IFAFREE(&ia->ia_ifa); 789 790 /* also remove from the IPv6 address chain(itojun&jinmei) */ 791 oia = ia; 792 if (oia == (ia = V_in6_ifaddr)) 793 V_in6_ifaddr = ia->ia_next; 794 else { 795 while (ia->ia_next && (ia->ia_next != oia)) 796 ia = ia->ia_next; 797 if (ia->ia_next) 798 ia->ia_next = oia->ia_next; 799 else { 800 nd6log((LOG_ERR, 801 "%s: didn't unlink in6ifaddr from list\n", 802 if_name(ifp))); 803 } 804 } 805 806 IFAFREE(&oia->ia_ifa); 807 } 808 809 in6_pcbpurgeif0(&V_udbinfo, ifp); 810 in6_pcbpurgeif0(&V_ripcbinfo, ifp); 811 /* leave from all multicast groups joined */ 812 in6_purgemaddrs(ifp); 813 814 /* 815 * remove neighbor management table. we call it twice just to make 816 * sure we nuke everything. maybe we need just one call. 817 * XXX: since the first call did not release addresses, some prefixes 818 * might remain. We should call nd6_purge() again to release the 819 * prefixes after removing all addresses above. 820 * (Or can we just delay calling nd6_purge until at this point?) 821 */ 822 nd6_purge(ifp); 823 824 /* remove route to link-local allnodes multicast (ff02::1) */ 825 bzero(&sin6, sizeof(sin6)); 826 sin6.sin6_len = sizeof(struct sockaddr_in6); 827 sin6.sin6_family = AF_INET6; 828 sin6.sin6_addr = in6addr_linklocal_allnodes; 829 if (in6_setscope(&sin6.sin6_addr, ifp, NULL)) 830 /* XXX: should not fail */ 831 return; 832 /* XXX grab lock first to avoid LOR */ 833 if (V_rt_tables[0][AF_INET6] != NULL) { 834 RADIX_NODE_HEAD_LOCK(V_rt_tables[0][AF_INET6]); 835 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 836 if (rt) { 837 if (rt->rt_ifp == ifp) 838 rtexpunge(rt); 839 RTFREE_LOCKED(rt); 840 } 841 RADIX_NODE_HEAD_UNLOCK(V_rt_tables[0][AF_INET6]); 842 } 843} 844 845int 846in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf, 847 const u_int8_t *baseid, int generate) 848{ 849 u_int8_t nullbuf[8]; 850 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 851 852 bzero(nullbuf, sizeof(nullbuf)); 853 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) { 854 /* we've never created a random ID. Create a new one. */ 855 generate = 1; 856 } 857 858 if (generate) { 859 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1)); 860 861 /* generate_tmp_ifid will update seedn and buf */ 862 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1, 863 ndi->randomid); 864 } 865 bcopy(ndi->randomid, retbuf, 8); 866 867 return (0); 868} 869 870void 871in6_tmpaddrtimer(void *ignored_arg) 872{ 873 INIT_VNET_NET(curvnet); 874 INIT_VNET_INET6(curvnet); 875 struct nd_ifinfo *ndi; 876 u_int8_t nullbuf[8]; 877 struct ifnet *ifp; 878 879 callout_reset(&V_in6_tmpaddrtimer_ch, 880 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor - 881 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL); 882 883 bzero(nullbuf, sizeof(nullbuf)); 884 for (ifp = TAILQ_FIRST(&V_ifnet); ifp; 885 ifp = TAILQ_NEXT(ifp, if_list)) { 886 ndi = ND_IFINFO(ifp); 887 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) { 888 /* 889 * We've been generating a random ID on this interface. 890 * Create a new one. 891 */ 892 (void)generate_tmp_ifid(ndi->randomseed0, 893 ndi->randomseed1, ndi->randomid); 894 } 895 } 896 897} 898 899static void 900in6_purgemaddrs(struct ifnet *ifp) 901{ 902 struct in6_multi *in6m; 903 struct in6_multi *oin6m; 904 905#ifdef DIAGNOSTIC 906 printf("%s: purging ifp %p\n", __func__, ifp); 907#endif 908 909 IFF_LOCKGIANT(ifp); 910 LIST_FOREACH_SAFE(in6m, &in6_multihead, in6m_entry, oin6m) { 911 if (in6m->in6m_ifp == ifp) 912 in6_delmulti(in6m); 913 } 914 IFF_UNLOCKGIANT(ifp); 915} 916