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