in6_ifattach.c revision 187380
11590Srgrimes/*- 21590Srgrimes * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 31590Srgrimes * All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. Neither the name of the project nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes * 291590Srgrimes * $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $ 301590Srgrimes */ 311590Srgrimes 321590Srgrimes#include <sys/cdefs.h> 331590Srgrimes__FBSDID("$FreeBSD: head/sys/netinet6/in6_ifattach.c 187380 2009-01-18 07:20:02Z sam $"); 3487701Smarkm 3587701Smarkm#include <sys/param.h> 3687701Smarkm#include <sys/systm.h> 3787701Smarkm#include <sys/malloc.h> 381590Srgrimes#include <sys/socket.h> 3928370Scharnier#include <sys/sockio.h> 401590Srgrimes#include <sys/kernel.h> 411590Srgrimes#include <sys/syslog.h> 4287701Smarkm#include <sys/md5.h> 431590Srgrimes#include <sys/vimage.h> 441590Srgrimes 4587701Smarkm#include <net/if.h> 4628370Scharnier#include <net/if_dl.h> 471590Srgrimes#include <net/if_types.h> 481590Srgrimes#include <net/route.h> 491590Srgrimes#include <net/vnet.h> 5087701Smarkm 5128370Scharnier#include <netinet/in.h> 5228370Scharnier#include <netinet/in_var.h> 5328370Scharnier#include <netinet/if_ether.h> 541590Srgrimes#include <netinet/in_pcb.h> 551590Srgrimes#include <netinet/vinet.h> 5687701Smarkm 5728370Scharnier#include <netinet/ip6.h> 5887701Smarkm#include <netinet6/ip6_var.h> 591590Srgrimes#include <netinet6/in6_var.h> 601590Srgrimes#include <netinet6/in6_pcb.h> 611590Srgrimes#include <netinet6/in6_ifattach.h> 6287701Smarkm#include <netinet6/ip6_var.h> 631590Srgrimes#include <netinet6/nd6.h> 641590Srgrimes#include <netinet6/scope6_var.h> 651590Srgrimes#include <netinet6/vinet6.h> 661590Srgrimes 6768617Sdg#ifdef VIMAGE_GLOBALS 681590Srgrimesunsigned long in6_maxmtu; 691590Srgrimesint ip6_auto_linklocal; 7068617Sdgstruct callout in6_tmpaddrtimer_ch; 7150638Speterextern struct inpcbinfo ripcbinfo; 722599Sache#endif 731590Srgrimes 741590Srgrimesstatic int get_rand_ifid(struct ifnet *, struct in6_addr *); 751590Srgrimesstatic int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *); 761590Srgrimesstatic int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *); 771590Srgrimesstatic int in6_ifattach_linklocal(struct ifnet *, struct ifnet *); 781590Srgrimesstatic int in6_ifattach_loopback(struct ifnet *); 791590Srgrimesstatic void in6_purgemaddrs(struct ifnet *); 801590Srgrimes 811590Srgrimes#define EUI64_GBIT 0x01 821590Srgrimes#define EUI64_UBIT 0x02 8387701Smarkm#define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) 8487701Smarkm#define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 851590Srgrimes#define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6)) 861590Srgrimes#define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT) 8728370Scharnier#define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6)) 881590Srgrimes 891590Srgrimes#define IFID_LOCAL(in6) (!EUI64_LOCAL(in6)) 902599Sache#define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6)) 913397Sache 9228370Scharnier/* 931590Srgrimes * Generate a last-resort interface identifier, when the machine has no 941590Srgrimes * IEEE802/EUI64 address sources. 951590Srgrimes * The goal here is to get an interface identifier that is 961590Srgrimes * (1) random enough and (2) does not change across reboot. 971590Srgrimes * We currently use MD5(hostname) for it. 981590Srgrimes * 991590Srgrimes * in6 - upper 64bits are preserved 1001590Srgrimes */ 1011590Srgrimesstatic int 1021590Srgrimesget_rand_ifid(struct ifnet *ifp, struct in6_addr *in6) 1031590Srgrimes{ 10424360Simp INIT_VPROCG(TD_TO_VPROCG(curthread)); /* XXX V_hostname needs this */ 1051590Srgrimes MD5_CTX ctxt; 1061590Srgrimes u_int8_t digest[16]; 1071590Srgrimes int hostnamelen; 1081590Srgrimes 1091590Srgrimes mtx_lock(&hostname_mtx); 1101590Srgrimes hostnamelen = strlen(V_hostname); 1111590Srgrimes#if 0 1121590Srgrimes /* we need at least several letters as seed for ifid */ 1131590Srgrimes if (hostnamelen < 3) 1141590Srgrimes return -1; 1151590Srgrimes#endif 11668617Sdg 1171590Srgrimes /* generate 8 bytes of pseudo-random value. */ 1181590Srgrimes bzero(&ctxt, sizeof(ctxt)); 1191590Srgrimes MD5Init(&ctxt); 1201590Srgrimes MD5Update(&ctxt, V_hostname, hostnamelen); 1211590Srgrimes mtx_unlock(&hostname_mtx); 1221590Srgrimes MD5Final(digest, &ctxt); 1231590Srgrimes 1241590Srgrimes /* assumes sizeof(digest) > sizeof(ifid) */ 1251590Srgrimes bcopy(digest, &in6->s6_addr[8], 8); 1261590Srgrimes 1271590Srgrimes /* make sure to set "u" bit to local, and "g" bit to individual. */ 1281590Srgrimes in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 12968617Sdg in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 1301590Srgrimes 1311590Srgrimes /* convert EUI64 into IPv6 interface identifier */ 1321590Srgrimes EUI64_TO_IFID(in6); 1331590Srgrimes 1341590Srgrimes return 0; 1351590Srgrimes} 1361590Srgrimes 1371590Srgrimesstatic int 1381590Srgrimesgenerate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret) 1391590Srgrimes{ 1401590Srgrimes INIT_VNET_INET6(curvnet); 1411590Srgrimes MD5_CTX ctxt; 1421590Srgrimes u_int8_t seed[16], digest[16], nullbuf[8]; 1431590Srgrimes u_int32_t val32; 1441590Srgrimes 1451590Srgrimes /* If there's no history, start with a random seed. */ 1461590Srgrimes bzero(nullbuf, sizeof(nullbuf)); 1471590Srgrimes if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) { 1481590Srgrimes int i; 1491590Srgrimes 1501590Srgrimes for (i = 0; i < 2; i++) { 1511590Srgrimes val32 = arc4random(); 1521590Srgrimes bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32)); 1531590Srgrimes } 1541590Srgrimes } else 1551590Srgrimes bcopy(seed0, seed, 8); 1561590Srgrimes 1571590Srgrimes /* copy the right-most 64-bits of the given address */ 1581590Srgrimes /* XXX assumption on the size of IFID */ 1591590Srgrimes bcopy(seed1, &seed[8], 8); 1601590Srgrimes 1611590Srgrimes if (0) { /* for debugging purposes only */ 1621590Srgrimes int i; 1631590Srgrimes 1641590Srgrimes printf("generate_tmp_ifid: new randomized ID from: "); 1651590Srgrimes for (i = 0; i < 16; i++) 1661590Srgrimes printf("%02x", seed[i]); 16750638Speter printf(" "); 16850638Speter } 1691590Srgrimes 1701590Srgrimes /* generate 16 bytes of pseudo-random value. */ 1711590Srgrimes bzero(&ctxt, sizeof(ctxt)); 1721590Srgrimes MD5Init(&ctxt); 1731590Srgrimes MD5Update(&ctxt, seed, sizeof(seed)); 17450638Speter MD5Final(digest, &ctxt); 17550638Speter 17650638Speter /* 1771590Srgrimes * RFC 3041 3.2.1. (3) 1781590Srgrimes * Take the left-most 64-bits of the MD5 digest and set bit 6 (the 1791590Srgrimes * left-most bit is numbered 0) to zero. 1801590Srgrimes */ 1811590Srgrimes bcopy(digest, ret, 8); 1821590Srgrimes ret[0] &= ~EUI64_UBIT; 1831590Srgrimes 1841590Srgrimes /* 1851590Srgrimes * XXX: we'd like to ensure that the generated value is not zero 1861590Srgrimes * for simplicity. If the caclculated digest happens to be zero, 1871590Srgrimes * use a random non-zero value as the last resort. 1881590Srgrimes */ 1891590Srgrimes if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) { 1901590Srgrimes nd6log((LOG_INFO, 1911590Srgrimes "generate_tmp_ifid: computed MD5 value is zero.\n")); 1921590Srgrimes 1931590Srgrimes val32 = arc4random(); 1941590Srgrimes val32 = 1 + (val32 % (0xffffffff - 1)); 1951590Srgrimes } 1961590Srgrimes 1971590Srgrimes /* 1981590Srgrimes * RFC 3041 3.2.1. (4) 1991590Srgrimes * Take the rightmost 64-bits of the MD5 digest and save them in 2001590Srgrimes * stable storage as the history value to be used in the next 2011590Srgrimes * iteration of the algorithm. 2021590Srgrimes */ 2031590Srgrimes bcopy(&digest[8], seed0, 8); 2041590Srgrimes 2051590Srgrimes if (0) { /* for debugging purposes only */ 2061590Srgrimes int i; 2071590Srgrimes 2081590Srgrimes printf("to: "); 20950638Speter for (i = 0; i < 16; i++) 21050638Speter printf("%02x", digest[i]); 2111590Srgrimes printf("\n"); 2121590Srgrimes } 2131590Srgrimes 2141590Srgrimes return 0; 2151590Srgrimes} 2161590Srgrimes 2171590Srgrimes/* 2181590Srgrimes * Get interface identifier for the specified interface. 2191590Srgrimes * XXX assumes single sockaddr_dl (AF_LINK address) per an interface 22050638Speter * 22150638Speter * in6 - upper 64bits are preserved 22250638Speter */ 22350638Speterint 22450638Speterin6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6) 22550638Speter{ 22650638Speter struct ifaddr *ifa; 2271590Srgrimes struct sockaddr_dl *sdl; 22850638Speter u_int8_t *addr; 22950638Speter size_t addrlen; 23050638Speter static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 23150638Speter static u_int8_t allone[8] = 23250638Speter { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 23350638Speter 23450638Speter for (ifa = ifp->if_addrlist.tqh_first; 2351590Srgrimes ifa; 2361590Srgrimes ifa = ifa->ifa_list.tqe_next) { 2371590Srgrimes if (ifa->ifa_addr->sa_family != AF_LINK) 2381590Srgrimes continue; 2391590Srgrimes sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2401590Srgrimes if (sdl == NULL) 2411590Srgrimes continue; 2421590Srgrimes if (sdl->sdl_alen == 0) 2431590Srgrimes continue; 2441590Srgrimes 2451590Srgrimes goto found; 24687701Smarkm } 2471590Srgrimes 2481590Srgrimes return -1; 2491590Srgrimes 2501590Srgrimesfound: 2511590Srgrimes addr = LLADDR(sdl); 2521590Srgrimes addrlen = sdl->sdl_alen; 2531590Srgrimes 2541590Srgrimes /* get EUI64 */ 2551590Srgrimes switch (ifp->if_type) { 2561590Srgrimes case IFT_ETHER: 2571590Srgrimes case IFT_FDDI: 2581590Srgrimes case IFT_ISO88025: 2591590Srgrimes case IFT_ATM: 2608524Sache case IFT_IEEE1394: 2611590Srgrimes#ifdef IFT_IEEE80211 2621590Srgrimes case IFT_IEEE80211: 2631590Srgrimes#endif 2641590Srgrimes /* IEEE802/EUI64 cases - what others? */ 2651590Srgrimes /* IEEE1394 uses 16byte length address starting with EUI64 */ 2661590Srgrimes if (addrlen > 8) 2671590Srgrimes addrlen = 8; 2681590Srgrimes 2691590Srgrimes /* look at IEEE802/EUI64 only */ 2701590Srgrimes if (addrlen != 8 && addrlen != 6) 2711590Srgrimes return -1; 2721590Srgrimes 2731590Srgrimes /* 2741590Srgrimes * check for invalid MAC address - on bsdi, we see it a lot 2751590Srgrimes * since wildboar configures all-zero MAC on pccard before 2761590Srgrimes * card insertion. 2771590Srgrimes */ 2781590Srgrimes if (bcmp(addr, allzero, addrlen) == 0) 2791590Srgrimes return -1; 28028370Scharnier if (bcmp(addr, allone, addrlen) == 0) 28128370Scharnier return -1; 28228370Scharnier 2831590Srgrimes /* make EUI64 address */ 2841590Srgrimes if (addrlen == 8) 2851590Srgrimes bcopy(addr, &in6->s6_addr[8], 8); 28687701Smarkm else if (addrlen == 6) { 2871590Srgrimes in6->s6_addr[8] = addr[0]; 2881590Srgrimes in6->s6_addr[9] = addr[1]; 28987701Smarkm in6->s6_addr[10] = addr[2]; 2901590Srgrimes in6->s6_addr[11] = 0xff; 2911590Srgrimes in6->s6_addr[12] = 0xfe; 29287701Smarkm in6->s6_addr[13] = addr[3]; 2931590Srgrimes in6->s6_addr[14] = addr[4]; 2941590Srgrimes in6->s6_addr[15] = addr[5]; 2951590Srgrimes } 2961590Srgrimes break; 2971590Srgrimes 2981590Srgrimes case IFT_ARCNET: 2991590Srgrimes if (addrlen != 1) 3001590Srgrimes return -1; 30128370Scharnier if (!addr[0]) 30228370Scharnier return -1; 30328370Scharnier 3041590Srgrimes bzero(&in6->s6_addr[8], 8); 3051590Srgrimes in6->s6_addr[15] = addr[0]; 30628370Scharnier 307 /* 308 * due to insufficient bitwidth, we mark it local. 309 */ 310 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 311 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 312 break; 313 314 case IFT_GIF: 315#ifdef IFT_STF 316 case IFT_STF: 317#endif 318 /* 319 * RFC2893 says: "SHOULD use IPv4 address as ifid source". 320 * however, IPv4 address is not very suitable as unique 321 * identifier source (can be renumbered). 322 * we don't do this. 323 */ 324 return -1; 325 326 default: 327 return -1; 328 } 329 330 /* sanity check: g bit must not indicate "group" */ 331 if (EUI64_GROUP(in6)) 332 return -1; 333 334 /* convert EUI64 into IPv6 interface identifier */ 335 EUI64_TO_IFID(in6); 336 337 /* 338 * sanity check: ifid must not be all zero, avoid conflict with 339 * subnet router anycast 340 */ 341 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && 342 bcmp(&in6->s6_addr[9], allzero, 7) == 0) { 343 return -1; 344 } 345 346 return 0; 347} 348 349/* 350 * Get interface identifier for the specified interface. If it is not 351 * available on ifp0, borrow interface identifier from other information 352 * sources. 353 * 354 * altifp - secondary EUI64 source 355 */ 356static int 357get_ifid(struct ifnet *ifp0, struct ifnet *altifp, 358 struct in6_addr *in6) 359{ 360 INIT_VNET_NET(ifp0->if_vnet); 361 INIT_VNET_INET6(ifp0->if_vnet); 362 struct ifnet *ifp; 363 364 /* first, try to get it from the interface itself */ 365 if (in6_get_hw_ifid(ifp0, in6) == 0) { 366 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 367 if_name(ifp0))); 368 goto success; 369 } 370 371 /* try secondary EUI64 source. this basically is for ATM PVC */ 372 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) { 373 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", 374 if_name(ifp0), if_name(altifp))); 375 goto success; 376 } 377 378 /* next, try to get it from some other hardware interface */ 379 IFNET_RLOCK(); 380 for (ifp = V_ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) { 381 if (ifp == ifp0) 382 continue; 383 if (in6_get_hw_ifid(ifp, in6) != 0) 384 continue; 385 386 /* 387 * to borrow ifid from other interface, ifid needs to be 388 * globally unique 389 */ 390 if (IFID_UNIVERSAL(in6)) { 391 nd6log((LOG_DEBUG, 392 "%s: borrow interface identifier from %s\n", 393 if_name(ifp0), if_name(ifp))); 394 IFNET_RUNLOCK(); 395 goto success; 396 } 397 } 398 IFNET_RUNLOCK(); 399 400 /* last resort: get from random number source */ 401 if (get_rand_ifid(ifp, in6) == 0) { 402 nd6log((LOG_DEBUG, 403 "%s: interface identifier generated by random number\n", 404 if_name(ifp0))); 405 goto success; 406 } 407 408 printf("%s: failed to get interface identifier\n", if_name(ifp0)); 409 return -1; 410 411success: 412 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 413 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 414 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 415 in6->s6_addr[14], in6->s6_addr[15])); 416 return 0; 417} 418 419/* 420 * altifp - secondary EUI64 source 421 */ 422static int 423in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp) 424{ 425 INIT_VNET_INET6(curvnet); 426 struct in6_ifaddr *ia; 427 struct in6_aliasreq ifra; 428 struct nd_prefixctl pr0; 429 int i, error; 430 431 /* 432 * configure link-local address. 433 */ 434 bzero(&ifra, sizeof(ifra)); 435 436 /* 437 * in6_update_ifa() does not use ifra_name, but we accurately set it 438 * for safety. 439 */ 440 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 441 442 ifra.ifra_addr.sin6_family = AF_INET6; 443 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 444 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000); 445 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 446 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 447 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; 448 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); 449 } else { 450 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) { 451 nd6log((LOG_ERR, 452 "%s: no ifid available\n", if_name(ifp))); 453 return (-1); 454 } 455 } 456 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL)) 457 return (-1); 458 459 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 460 ifra.ifra_prefixmask.sin6_family = AF_INET6; 461 ifra.ifra_prefixmask.sin6_addr = in6mask64; 462 /* link-local addresses should NEVER expire. */ 463 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 464 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 465 466 /* 467 * Now call in6_update_ifa() to do a bunch of procedures to configure 468 * a link-local address. We can set the 3rd argument to NULL, because 469 * we know there's no other link-local address on the interface 470 * and therefore we are adding one (instead of updating one). 471 */ 472 if ((error = in6_update_ifa(ifp, &ifra, NULL, 473 IN6_IFAUPDATE_DADDELAY)) != 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 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */ 489#ifdef DIAGNOSTIC 490 if (!ia) { 491 panic("ia == NULL in in6_ifattach_linklocal"); 492 /* NOTREACHED */ 493 } 494#endif 495 496 /* 497 * Make the link-local prefix (fe80::%link/64) as on-link. 498 * Since we'd like to manage prefixes separately from addresses, 499 * we make an ND6 prefix structure for the link-local prefix, 500 * and add it to the prefix list as a never-expire prefix. 501 * XXX: this change might affect some existing code base... 502 */ 503 bzero(&pr0, sizeof(pr0)); 504 pr0.ndpr_ifp = ifp; 505 /* this should be 64 at this moment. */ 506 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); 507 pr0.ndpr_prefix = ifra.ifra_addr; 508 /* apply the mask for safety. (nd6_prelist_add will apply it again) */ 509 for (i = 0; i < 4; i++) { 510 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 511 in6mask64.s6_addr32[i]; 512 } 513 /* 514 * Initialize parameters. The link-local prefix must always be 515 * on-link, and its lifetimes never expire. 516 */ 517 pr0.ndpr_raf_onlink = 1; 518 pr0.ndpr_raf_auto = 1; /* probably meaningless */ 519 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME; 520 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME; 521 /* 522 * Since there is no other link-local addresses, nd6_prefix_lookup() 523 * probably returns NULL. However, we cannot always expect the result. 524 * For example, if we first remove the (only) existing link-local 525 * address, and then reconfigure another one, the prefix is still 526 * valid with referring to the old link-local address. 527 */ 528 if (nd6_prefix_lookup(&pr0) == NULL) { 529 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0) 530 return (error); 531 } 532 533 return 0; 534} 535 536/* 537 * ifp - must be IFT_LOOP 538 */ 539static int 540in6_ifattach_loopback(struct ifnet *ifp) 541{ 542 INIT_VNET_INET6(curvnet); 543 struct in6_aliasreq ifra; 544 int error; 545 546 bzero(&ifra, sizeof(ifra)); 547 548 /* 549 * in6_update_ifa() does not use ifra_name, but we accurately set it 550 * for safety. 551 */ 552 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 553 554 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 555 ifra.ifra_prefixmask.sin6_family = AF_INET6; 556 ifra.ifra_prefixmask.sin6_addr = in6mask128; 557 558 /* 559 * Always initialize ia_dstaddr (= broadcast address) to loopback 560 * address. Follows IPv4 practice - see in_ifinit(). 561 */ 562 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 563 ifra.ifra_dstaddr.sin6_family = AF_INET6; 564 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback; 565 566 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 567 ifra.ifra_addr.sin6_family = AF_INET6; 568 ifra.ifra_addr.sin6_addr = in6addr_loopback; 569 570 /* the loopback address should NEVER expire. */ 571 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 572 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 573 574 /* we don't need to perform DAD on loopback interfaces. */ 575 ifra.ifra_flags |= IN6_IFF_NODAD; 576 577 /* skip registration to the prefix list. XXX should be temporary. */ 578 ifra.ifra_flags |= IN6_IFF_NOPFX; 579 580 /* 581 * We are sure that this is a newly assigned address, so we can set 582 * NULL to the 3rd arg. 583 */ 584 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) { 585 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure " 586 "the loopback address on %s (errno=%d)\n", 587 if_name(ifp), error)); 588 return (-1); 589 } 590 591 return 0; 592} 593 594/* 595 * compute NI group address, based on the current hostname setting. 596 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later). 597 * 598 * when ifp == NULL, the caller is responsible for filling scopeid. 599 */ 600int 601in6_nigroup(struct ifnet *ifp, const char *name, int namelen, 602 struct in6_addr *in6) 603{ 604 const char *p; 605 u_char *q; 606 MD5_CTX ctxt; 607 u_int8_t digest[16]; 608 char l; 609 char n[64]; /* a single label must not exceed 63 chars */ 610 611 if (!namelen || !name) 612 return -1; 613 614 p = name; 615 while (p && *p && *p != '.' && p - name < namelen) 616 p++; 617 if (p - name > sizeof(n) - 1) 618 return -1; /* label too long */ 619 l = p - name; 620 strncpy(n, name, l); 621 n[(int)l] = '\0'; 622 for (q = n; *q; q++) { 623 if ('A' <= *q && *q <= 'Z') 624 *q = *q - 'A' + 'a'; 625 } 626 627 /* generate 8 bytes of pseudo-random value. */ 628 bzero(&ctxt, sizeof(ctxt)); 629 MD5Init(&ctxt); 630 MD5Update(&ctxt, &l, sizeof(l)); 631 MD5Update(&ctxt, n, l); 632 MD5Final(digest, &ctxt); 633 634 bzero(in6, sizeof(*in6)); 635 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL; 636 in6->s6_addr8[11] = 2; 637 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3])); 638 if (in6_setscope(in6, ifp, NULL)) 639 return (-1); /* XXX: should not fail */ 640 641 return 0; 642} 643 644/* 645 * XXX multiple loopback interface needs more care. for instance, 646 * nodelocal address needs to be configured onto only one of them. 647 * XXX multiple link-local address case 648 * 649 * altifp - secondary EUI64 source 650 */ 651void 652in6_ifattach(struct ifnet *ifp, struct ifnet *altifp) 653{ 654 INIT_VNET_INET6(ifp->if_vnet); 655 struct in6_ifaddr *ia; 656 struct in6_addr in6; 657 658 /* some of the interfaces are inherently not IPv6 capable */ 659 switch (ifp->if_type) { 660 case IFT_PFLOG: 661 case IFT_PFSYNC: 662 case IFT_CARP: 663 return; 664 } 665 666 /* 667 * quirks based on interface type 668 */ 669 switch (ifp->if_type) { 670#ifdef IFT_STF 671 case IFT_STF: 672 /* 673 * 6to4 interface is a very special kind of beast. 674 * no multicast, no linklocal. RFC2529 specifies how to make 675 * linklocals for 6to4 interface, but there's no use and 676 * it is rather harmful to have one. 677 */ 678 goto statinit; 679#endif 680 default: 681 break; 682 } 683 684 /* 685 * usually, we require multicast capability to the interface 686 */ 687 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 688 nd6log((LOG_INFO, "in6_ifattach: " 689 "%s is not multicast capable, IPv6 not enabled\n", 690 if_name(ifp))); 691 return; 692 } 693 694 /* 695 * assign loopback address for loopback interface. 696 * XXX multiple loopback interface case. 697 */ 698 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 699 in6 = in6addr_loopback; 700 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) { 701 if (in6_ifattach_loopback(ifp) != 0) 702 return; 703 } 704 } 705 706 /* 707 * assign a link-local address, if there's none. 708 */ 709 if (V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) { 710 ia = in6ifa_ifpforlinklocal(ifp, 0); 711 if (ia == NULL) { 712 if (in6_ifattach_linklocal(ifp, altifp) == 0) { 713 /* linklocal address assigned */ 714 } else { 715 /* failed to assign linklocal address. bark? */ 716 } 717 } 718 } 719 720#ifdef IFT_STF /* XXX */ 721statinit: 722#endif 723 724 /* update dynamically. */ 725 if (V_in6_maxmtu < ifp->if_mtu) 726 V_in6_maxmtu = ifp->if_mtu; 727} 728 729/* 730 * NOTE: in6_ifdetach() does not support loopback if at this moment. 731 * We don't need this function in bsdi, because interfaces are never removed 732 * from the ifnet list in bsdi. 733 */ 734void 735in6_ifdetach(struct ifnet *ifp) 736{ 737 INIT_VNET_NET(ifp->if_vnet); 738 INIT_VNET_INET(ifp->if_vnet); 739 INIT_VNET_INET6(ifp->if_vnet); 740 struct in6_ifaddr *ia, *oia; 741 struct ifaddr *ifa, *next; 742 struct rtentry *rt; 743 short rtflags; 744 struct sockaddr_in6 sin6; 745 struct in6_multi_mship *imm; 746 747 /* remove neighbor management table */ 748 nd6_purge(ifp); 749 750 /* nuke any of IPv6 addresses we have */ 751 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) { 752 next = ifa->ifa_list.tqe_next; 753 if (ifa->ifa_addr->sa_family != AF_INET6) 754 continue; 755 in6_purgeaddr(ifa); 756 } 757 758 /* undo everything done by in6_ifattach(), just in case */ 759 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) { 760 next = ifa->ifa_list.tqe_next; 761 762 if (ifa->ifa_addr->sa_family != AF_INET6 763 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) { 764 continue; 765 } 766 767 ia = (struct in6_ifaddr *)ifa; 768 769 /* 770 * leave from multicast groups we have joined for the interface 771 */ 772 while ((imm = ia->ia6_memberships.lh_first) != NULL) { 773 LIST_REMOVE(imm, i6mm_chain); 774 in6_leavegroup(imm); 775 } 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 = V_in6_ifaddr)) 795 V_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 in6_pcbpurgeif0(&V_udbinfo, ifp); 812 in6_pcbpurgeif0(&V_ripcbinfo, ifp); 813 /* leave from all multicast groups joined */ 814 in6_purgemaddrs(ifp); 815 816 /* 817 * remove neighbor management table. we call it twice just to make 818 * sure we nuke everything. maybe we need just one call. 819 * XXX: since the first call did not release addresses, some prefixes 820 * might remain. We should call nd6_purge() again to release the 821 * prefixes after removing all addresses above. 822 * (Or can we just delay calling nd6_purge until at this point?) 823 */ 824 nd6_purge(ifp); 825 826 /* remove route to link-local allnodes multicast (ff02::1) */ 827 bzero(&sin6, sizeof(sin6)); 828 sin6.sin6_len = sizeof(struct sockaddr_in6); 829 sin6.sin6_family = AF_INET6; 830 sin6.sin6_addr = in6addr_linklocal_allnodes; 831 if (in6_setscope(&sin6.sin6_addr, ifp, NULL)) 832 /* XXX: should not fail */ 833 return; 834 /* XXX grab lock first to avoid LOR */ 835 if (V_rt_tables[0][AF_INET6] != NULL) { 836 RADIX_NODE_HEAD_LOCK(V_rt_tables[0][AF_INET6]); 837 rt = rtalloc1((struct sockaddr *)&sin6, 0, RTF_RNH_LOCKED); 838 if (rt) { 839 if (rt->rt_ifp == ifp) 840 rtexpunge(rt); 841 RTFREE_LOCKED(rt); 842 } 843 RADIX_NODE_HEAD_UNLOCK(V_rt_tables[0][AF_INET6]); 844 } 845} 846 847int 848in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf, 849 const u_int8_t *baseid, int generate) 850{ 851 u_int8_t nullbuf[8]; 852 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 853 854 bzero(nullbuf, sizeof(nullbuf)); 855 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) { 856 /* we've never created a random ID. Create a new one. */ 857 generate = 1; 858 } 859 860 if (generate) { 861 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1)); 862 863 /* generate_tmp_ifid will update seedn and buf */ 864 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1, 865 ndi->randomid); 866 } 867 bcopy(ndi->randomid, retbuf, 8); 868 869 return (0); 870} 871 872void 873in6_tmpaddrtimer(void *ignored_arg) 874{ 875 INIT_VNET_NET(curvnet); 876 INIT_VNET_INET6(curvnet); 877 struct nd_ifinfo *ndi; 878 u_int8_t nullbuf[8]; 879 struct ifnet *ifp; 880 881 callout_reset(&V_in6_tmpaddrtimer_ch, 882 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor - 883 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL); 884 885 bzero(nullbuf, sizeof(nullbuf)); 886 for (ifp = TAILQ_FIRST(&V_ifnet); ifp; 887 ifp = TAILQ_NEXT(ifp, if_list)) { 888 ndi = ND_IFINFO(ifp); 889 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) { 890 /* 891 * We've been generating a random ID on this interface. 892 * Create a new one. 893 */ 894 (void)generate_tmp_ifid(ndi->randomseed0, 895 ndi->randomseed1, ndi->randomid); 896 } 897 } 898 899} 900 901static void 902in6_purgemaddrs(struct ifnet *ifp) 903{ 904 struct in6_multi *in6m; 905 struct in6_multi *oin6m; 906 907 IFF_LOCKGIANT(ifp); 908 LIST_FOREACH_SAFE(in6m, &in6_multihead, in6m_entry, oin6m) { 909 if (in6m->in6m_ifp == ifp) 910 in6_delmulti(in6m); 911 } 912 IFF_UNLOCKGIANT(ifp); 913} 914