in6_ifattach.c revision 120971
1/* $FreeBSD: head/sys/netinet6/in6_ifattach.c 120971 2003-10-10 16:04:00Z ume $ */ 2/* $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $ */ 3 4/* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/malloc.h> 36#include <sys/socket.h> 37#include <sys/sockio.h> 38#include <sys/kernel.h> 39#include <sys/syslog.h> 40#include <sys/md5.h> 41 42#include <net/if.h> 43#include <net/if_dl.h> 44#include <net/if_types.h> 45#include <net/route.h> 46 47#include <netinet/in.h> 48#include <netinet/in_var.h> 49#include <netinet/if_ether.h> 50#include <netinet/in_pcb.h> 51 52#include <netinet/ip6.h> 53#include <netinet6/ip6_var.h> 54#include <netinet6/in6_var.h> 55#include <netinet6/in6_pcb.h> 56#include <netinet6/in6_ifattach.h> 57#include <netinet6/ip6_var.h> 58#include <netinet6/nd6.h> 59#include <netinet6/scope6_var.h> 60 61#include <net/net_osdep.h> 62 63struct in6_ifstat **in6_ifstat = NULL; 64struct icmp6_ifstat **icmp6_ifstat = NULL; 65size_t in6_ifstatmax = 0; 66size_t icmp6_ifstatmax = 0; 67unsigned long in6_maxmtu = 0; 68 69#ifdef IP6_AUTO_LINKLOCAL 70int ip6_auto_linklocal = IP6_AUTO_LINKLOCAL; 71#else 72int ip6_auto_linklocal = 1; /* enable by default */ 73#endif 74 75struct callout in6_tmpaddrtimer_ch; 76 77extern struct inpcbinfo udbinfo; 78extern struct inpcbinfo ripcbinfo; 79 80static int get_rand_ifid __P((struct ifnet *, struct in6_addr *)); 81static int generate_tmp_ifid __P((u_int8_t *, const u_int8_t *, u_int8_t *)); 82static int get_hw_ifid __P((struct ifnet *, struct in6_addr *)); 83static int get_ifid __P((struct ifnet *, struct ifnet *, struct in6_addr *)); 84static int in6_ifattach_linklocal __P((struct ifnet *, struct ifnet *)); 85static int in6_ifattach_loopback __P((struct ifnet *)); 86 87#define EUI64_GBIT 0x01 88#define EUI64_UBIT 0x02 89#define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) 90#define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 91#define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6)) 92#define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT) 93#define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6)) 94 95#define IFID_LOCAL(in6) (!EUI64_LOCAL(in6)) 96#define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6)) 97 98/* 99 * Generate a last-resort interface identifier, when the machine has no 100 * IEEE802/EUI64 address sources. 101 * The goal here is to get an interface identifier that is 102 * (1) random enough and (2) does not change across reboot. 103 * We currently use MD5(hostname) for it. 104 */ 105static int 106get_rand_ifid(ifp, in6) 107 struct ifnet *ifp; 108 struct in6_addr *in6; /* upper 64bits are preserved */ 109{ 110 MD5_CTX ctxt; 111 u_int8_t digest[16]; 112 int hostnamelen = strlen(hostname); 113 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, hostname, hostnamelen); 124 MD5Final(digest, &ctxt); 125 126 /* assumes sizeof(digest) > sizeof(ifid) */ 127 bcopy(digest, &in6->s6_addr[8], 8); 128 129 /* make sure to set "u" bit to local, and "g" bit to individual. */ 130 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 131 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 132 133 /* convert EUI64 into IPv6 interface identifier */ 134 EUI64_TO_IFID(in6); 135 136 return 0; 137} 138 139static int 140generate_tmp_ifid(seed0, seed1, ret) 141 u_int8_t *seed0, *ret; 142 const u_int8_t *seed1; 143{ 144 MD5_CTX ctxt; 145 u_int8_t seed[16], digest[16], nullbuf[8]; 146 u_int32_t val32; 147 struct timeval tv; 148 149 /* If there's no hisotry, start with a random seed. */ 150 bzero(nullbuf, sizeof(nullbuf)); 151 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) { 152 int i; 153 154 for (i = 0; i < 2; i++) { 155 microtime(&tv); 156 val32 = random() ^ tv.tv_usec; 157 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32)); 158 } 159 } else 160 bcopy(seed0, seed, 8); 161 162 /* copy the right-most 64-bits of the given address */ 163 /* XXX assumption on the size of IFID */ 164 bcopy(seed1, &seed[8], 8); 165 166 if (0) { /* for debugging purposes only */ 167 int i; 168 169 printf("generate_tmp_ifid: new randomized ID from: "); 170 for (i = 0; i < 16; i++) 171 printf("%02x", seed[i]); 172 printf(" "); 173 } 174 175 /* generate 16 bytes of pseudo-random value. */ 176 bzero(&ctxt, sizeof(ctxt)); 177 MD5Init(&ctxt); 178 MD5Update(&ctxt, seed, sizeof(seed)); 179 MD5Final(digest, &ctxt); 180 181 /* 182 * RFC 3041 3.2.1. (3) 183 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the 184 * left-most bit is numbered 0) to zero. 185 */ 186 bcopy(digest, ret, 8); 187 ret[0] &= ~EUI64_UBIT; 188 189 /* 190 * XXX: we'd like to ensure that the generated value is not zero 191 * for simplicity. If the caclculated digest happens to be zero, 192 * use a random non-zero value as the last resort. 193 */ 194 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) { 195 log(LOG_INFO, 196 "generate_tmp_ifid: computed MD5 value is zero.\n"); 197 198 microtime(&tv); 199 val32 = random() ^ tv.tv_usec; 200 val32 = 1 + (val32 % (0xffffffff - 1)); 201 } 202 203 /* 204 * RFC 3041 3.2.1. (4) 205 * Take the rightmost 64-bits of the MD5 digest and save them in 206 * stable storage as the history value to be used in the next 207 * iteration of the algorithm. 208 */ 209 bcopy(&digest[8], seed0, 8); 210 211 if (0) { /* for debugging purposes only */ 212 int i; 213 214 printf("to: "); 215 for (i = 0; i < 16; i++) 216 printf("%02x", digest[i]); 217 printf("\n"); 218 } 219 220 return 0; 221} 222 223/* 224 * Get interface identifier for the specified interface. 225 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface 226 */ 227static int 228get_hw_ifid(ifp, in6) 229 struct ifnet *ifp; 230 struct in6_addr *in6; /* upper 64bits are preserved */ 231{ 232 struct ifaddr *ifa; 233 struct sockaddr_dl *sdl; 234 u_int8_t *addr; 235 size_t addrlen; 236 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 237 static u_int8_t allone[8] = 238 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 239 240 for (ifa = ifp->if_addrlist.tqh_first; 241 ifa; 242 ifa = ifa->ifa_list.tqe_next) { 243 if (ifa->ifa_addr->sa_family != AF_LINK) 244 continue; 245 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 246 if (sdl == NULL) 247 continue; 248 if (sdl->sdl_alen == 0) 249 continue; 250 251 goto found; 252 } 253 254 return -1; 255 256found: 257 addr = LLADDR(sdl); 258 addrlen = sdl->sdl_alen; 259 260 /* get EUI64 */ 261 switch (ifp->if_type) { 262 case IFT_ETHER: 263 case IFT_FDDI: 264 case IFT_ISO88025: 265 case IFT_ATM: 266 case IFT_IEEE1394: 267#ifdef IFT_IEEE80211 268 case IFT_IEEE80211: 269#endif 270 /* IEEE802/EUI64 cases - what others? */ 271 /* IEEE1394 uses 16byte length address starting with EUI64 */ 272 if (addrlen > 8) 273 addrlen = 8; 274 275 /* look at IEEE802/EUI64 only */ 276 if (addrlen != 8 && addrlen != 6) 277 return -1; 278 279 /* 280 * check for invalid MAC address - on bsdi, we see it a lot 281 * since wildboar configures all-zero MAC on pccard before 282 * card insertion. 283 */ 284 if (bcmp(addr, allzero, addrlen) == 0) 285 return -1; 286 if (bcmp(addr, allone, addrlen) == 0) 287 return -1; 288 289 /* make EUI64 address */ 290 if (addrlen == 8) 291 bcopy(addr, &in6->s6_addr[8], 8); 292 else if (addrlen == 6) { 293 in6->s6_addr[8] = addr[0]; 294 in6->s6_addr[9] = addr[1]; 295 in6->s6_addr[10] = addr[2]; 296 in6->s6_addr[11] = 0xff; 297 in6->s6_addr[12] = 0xfe; 298 in6->s6_addr[13] = addr[3]; 299 in6->s6_addr[14] = addr[4]; 300 in6->s6_addr[15] = addr[5]; 301 } 302 break; 303 304 case IFT_ARCNET: 305 if (addrlen != 1) 306 return -1; 307 if (!addr[0]) 308 return -1; 309 310 bzero(&in6->s6_addr[8], 8); 311 in6->s6_addr[15] = addr[0]; 312 313 /* 314 * due to insufficient bitwidth, we mark it local. 315 */ 316 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 317 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 318 break; 319 320 case IFT_GIF: 321#ifdef IFT_STF 322 case IFT_STF: 323#endif 324 /* 325 * RFC2893 says: "SHOULD use IPv4 address as ifid source". 326 * however, IPv4 address is not very suitable as unique 327 * identifier source (can be renumbered). 328 * we don't do this. 329 */ 330 return -1; 331 332 default: 333 return -1; 334 } 335 336 /* sanity check: g bit must not indicate "group" */ 337 if (EUI64_GROUP(in6)) 338 return -1; 339 340 /* convert EUI64 into IPv6 interface identifier */ 341 EUI64_TO_IFID(in6); 342 343 /* 344 * sanity check: ifid must not be all zero, avoid conflict with 345 * subnet router anycast 346 */ 347 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && 348 bcmp(&in6->s6_addr[9], allzero, 7) == 0) { 349 return -1; 350 } 351 352 return 0; 353} 354 355/* 356 * Get interface identifier for the specified interface. If it is not 357 * available on ifp0, borrow interface identifier from other information 358 * sources. 359 */ 360static int 361get_ifid(ifp0, altifp, in6) 362 struct ifnet *ifp0; 363 struct ifnet *altifp; /* secondary EUI64 source */ 364 struct in6_addr *in6; 365{ 366 struct ifnet *ifp; 367 368 /* first, try to get it from the interface itself */ 369 if (get_hw_ifid(ifp0, in6) == 0) { 370 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 371 if_name(ifp0))); 372 goto success; 373 } 374 375 /* try secondary EUI64 source. this basically is for ATM PVC */ 376 if (altifp && get_hw_ifid(altifp, in6) == 0) { 377 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", 378 if_name(ifp0), if_name(altifp))); 379 goto success; 380 } 381 382 /* next, try to get it from some other hardware interface */ 383 IFNET_RLOCK(); 384 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) { 385 if (ifp == ifp0) 386 continue; 387 if (get_hw_ifid(ifp, in6) != 0) 388 continue; 389 390 /* 391 * to borrow ifid from other interface, ifid needs to be 392 * globally unique 393 */ 394 if (IFID_UNIVERSAL(in6)) { 395 nd6log((LOG_DEBUG, 396 "%s: borrow interface identifier from %s\n", 397 if_name(ifp0), if_name(ifp))); 398 IFNET_RUNLOCK(); 399 goto success; 400 } 401 } 402 IFNET_RUNLOCK(); 403 404 /* last resort: get from random number source */ 405 if (get_rand_ifid(ifp, in6) == 0) { 406 nd6log((LOG_DEBUG, 407 "%s: interface identifier generated by random number\n", 408 if_name(ifp0))); 409 goto success; 410 } 411 412 printf("%s: failed to get interface identifier\n", if_name(ifp0)); 413 return -1; 414 415success: 416 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 417 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 418 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 419 in6->s6_addr[14], in6->s6_addr[15])); 420 return 0; 421} 422 423static int 424in6_ifattach_linklocal(ifp, altifp) 425 struct ifnet *ifp; 426 struct ifnet *altifp; /* secondary EUI64 source */ 427{ 428 struct in6_ifaddr *ia; 429 struct in6_aliasreq ifra; 430 struct nd_prefix pr0; 431 int i, error; 432 433 /* 434 * configure link-local address. 435 */ 436 bzero(&ifra, sizeof(ifra)); 437 438 /* 439 * in6_update_ifa() does not use ifra_name, but we accurately set it 440 * for safety. 441 */ 442 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 443 444 ifra.ifra_addr.sin6_family = AF_INET6; 445 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 446 ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80); 447 ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); /* XXX */ 448 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 449 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 450 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; 451 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); 452 } else { 453 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) { 454 nd6log((LOG_ERR, 455 "%s: no ifid available\n", if_name(ifp))); 456 return (-1); 457 } 458 } 459 460 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 461 ifra.ifra_prefixmask.sin6_family = AF_INET6; 462 ifra.ifra_prefixmask.sin6_addr = in6mask64; 463 /* link-local addresses should NEVER expire. */ 464 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 465 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 466 467 /* 468 * Do not let in6_update_ifa() do DAD, since we need a random delay 469 * before sending an NS at the first time the interface becomes up. 470 * Instead, in6_if_up() will start DAD with a proper random delay. 471 */ 472 ifra.ifra_flags |= IN6_IFF_NODAD; 473 474 /* 475 * Now call in6_update_ifa() to do a bunch of procedures to configure 476 * a link-local address. We can set NULL to the 3rd argument, because 477 * we know there's no other link-local address on the interface 478 * and therefore we are adding one (instead of updating one). 479 */ 480 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) { 481 /* 482 * XXX: When the interface does not support IPv6, this call 483 * would fail in the SIOCSIFADDR ioctl. I believe the 484 * notification is rather confusing in this case, so just 485 * suppress it. (jinmei@kame.net 20010130) 486 */ 487 if (error != EAFNOSUPPORT) 488 log(LOG_NOTICE, "in6_ifattach_linklocal: failed to " 489 "configure a link-local address on %s " 490 "(errno=%d)\n", 491 if_name(ifp), error); 492 return (-1); 493 } 494 495 /* 496 * Adjust ia6_flags so that in6_if_up will perform DAD. 497 * XXX: Some P2P interfaces seem not to send packets just after 498 * becoming up, so we skip p2p interfaces for safety. 499 */ 500 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */ 501#ifdef DIAGNOSTIC 502 if (!ia) { 503 panic("ia == NULL in in6_ifattach_linklocal"); 504 /* NOTREACHED */ 505 } 506#endif 507 if (in6if_do_dad(ifp) && (ifp->if_flags & IFF_POINTOPOINT) == 0) { 508 ia->ia6_flags &= ~IN6_IFF_NODAD; 509 ia->ia6_flags |= IN6_IFF_TENTATIVE; 510 } 511 512 /* 513 * Make the link-local prefix (fe80::%link/64) as on-link. 514 * Since we'd like to manage prefixes separately from addresses, 515 * we make an ND6 prefix structure for the link-local prefix, 516 * and add it to the prefix list as a never-expire prefix. 517 * XXX: this change might affect some existing code base... 518 */ 519 bzero(&pr0, sizeof(pr0)); 520 pr0.ndpr_ifp = ifp; 521 /* this should be 64 at this moment. */ 522 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); 523 pr0.ndpr_mask = ifra.ifra_prefixmask.sin6_addr; 524 pr0.ndpr_prefix = ifra.ifra_addr; 525 /* apply the mask for safety. (nd6_prelist_add will apply it again) */ 526 for (i = 0; i < 4; i++) { 527 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 528 in6mask64.s6_addr32[i]; 529 } 530 /* 531 * Initialize parameters. The link-local prefix must always be 532 * on-link, and its lifetimes never expire. 533 */ 534 pr0.ndpr_raf_onlink = 1; 535 pr0.ndpr_raf_auto = 1; /* probably meaningless */ 536 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME; 537 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME; 538 /* 539 * Since there is no other link-local addresses, nd6_prefix_lookup() 540 * probably returns NULL. However, we cannot always expect the result. 541 * For example, if we first remove the (only) existing link-local 542 * address, and then reconfigure another one, the prefix is still 543 * valid with referring to the old link-local address. 544 */ 545 if (nd6_prefix_lookup(&pr0) == NULL) { 546 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0) 547 return (error); 548 } 549 550 return 0; 551} 552 553static int 554in6_ifattach_loopback(ifp) 555 struct ifnet *ifp; /* must be IFT_LOOP */ 556{ 557 struct in6_aliasreq ifra; 558 int error; 559 560 bzero(&ifra, sizeof(ifra)); 561 562 /* 563 * in6_update_ifa() does not use ifra_name, but we accurately set it 564 * for safety. 565 */ 566 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 567 568 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 569 ifra.ifra_prefixmask.sin6_family = AF_INET6; 570 ifra.ifra_prefixmask.sin6_addr = in6mask128; 571 572 /* 573 * Always initialize ia_dstaddr (= broadcast address) to loopback 574 * address. Follows IPv4 practice - see in_ifinit(). 575 */ 576 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 577 ifra.ifra_dstaddr.sin6_family = AF_INET6; 578 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback; 579 580 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 581 ifra.ifra_addr.sin6_family = AF_INET6; 582 ifra.ifra_addr.sin6_addr = in6addr_loopback; 583 584 /* the loopback address should NEVER expire. */ 585 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 586 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 587 588 /* we don't need to perform DAD on loopback interfaces. */ 589 ifra.ifra_flags |= IN6_IFF_NODAD; 590 591 /* skip registration to the prefix list. XXX should be temporary. */ 592 ifra.ifra_flags |= IN6_IFF_NOPFX; 593 594 /* 595 * We are sure that this is a newly assigned address, so we can set 596 * NULL to the 3rd arg. 597 */ 598 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) { 599 log(LOG_ERR, "in6_ifattach_loopback: failed to configure " 600 "the loopback address on %s (errno=%d)\n", 601 if_name(ifp), error); 602 return (-1); 603 } 604 605 return 0; 606} 607 608/* 609 * compute NI group address, based on the current hostname setting. 610 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later). 611 * 612 * when ifp == NULL, the caller is responsible for filling scopeid. 613 */ 614int 615in6_nigroup(ifp, name, namelen, in6) 616 struct ifnet *ifp; 617 const char *name; 618 int namelen; 619 struct in6_addr *in6; 620{ 621 const char *p; 622 u_char *q; 623 MD5_CTX ctxt; 624 u_int8_t digest[16]; 625 char l; 626 char n[64]; /* a single label must not exceed 63 chars */ 627 628 if (!namelen || !name) 629 return -1; 630 631 p = name; 632 while (p && *p && *p != '.' && p - name < namelen) 633 p++; 634 if (p - name > sizeof(n) - 1) 635 return -1; /* label too long */ 636 l = p - name; 637 strncpy(n, name, l); 638 n[(int)l] = '\0'; 639 for (q = n; *q; q++) { 640 if ('A' <= *q && *q <= 'Z') 641 *q = *q - 'A' + 'a'; 642 } 643 644 /* generate 8 bytes of pseudo-random value. */ 645 bzero(&ctxt, sizeof(ctxt)); 646 MD5Init(&ctxt); 647 MD5Update(&ctxt, &l, sizeof(l)); 648 MD5Update(&ctxt, n, l); 649 MD5Final(digest, &ctxt); 650 651 bzero(in6, sizeof(*in6)); 652 in6->s6_addr16[0] = htons(0xff02); 653 if (ifp) 654 in6->s6_addr16[1] = htons(ifp->if_index); 655 in6->s6_addr8[11] = 2; 656 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3])); 657 658 return 0; 659} 660 661void 662in6_nigroup_attach(name, namelen) 663 const char *name; 664 int namelen; 665{ 666 struct ifnet *ifp; 667 struct sockaddr_in6 mltaddr; 668 struct in6_multi *in6m; 669 int error; 670 671 bzero(&mltaddr, sizeof(mltaddr)); 672 mltaddr.sin6_family = AF_INET6; 673 mltaddr.sin6_len = sizeof(struct sockaddr_in6); 674 if (in6_nigroup(NULL, name, namelen, &mltaddr.sin6_addr) != 0) 675 return; 676 677 IFNET_RLOCK(); 678 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) 679 { 680 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 681 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m); 682 if (!in6m) { 683 if (!in6_addmulti(&mltaddr.sin6_addr, ifp, &error)) { 684 nd6log((LOG_ERR, "%s: failed to join %s " 685 "(errno=%d)\n", if_name(ifp), 686 ip6_sprintf(&mltaddr.sin6_addr), 687 error)); 688 } 689 } 690 } 691 IFNET_RUNLOCK(); 692} 693 694void 695in6_nigroup_detach(name, namelen) 696 const char *name; 697 int namelen; 698{ 699 struct ifnet *ifp; 700 struct sockaddr_in6 mltaddr; 701 struct in6_multi *in6m; 702 703 bzero(&mltaddr, sizeof(mltaddr)); 704 mltaddr.sin6_family = AF_INET6; 705 mltaddr.sin6_len = sizeof(struct sockaddr_in6); 706 if (in6_nigroup(NULL, name, namelen, &mltaddr.sin6_addr) != 0) 707 return; 708 709 IFNET_RLOCK(); 710 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) 711 { 712 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 713 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m); 714 if (in6m) 715 in6_delmulti(in6m); 716 } 717 IFNET_RUNLOCK(); 718} 719 720/* 721 * XXX multiple loopback interface needs more care. for instance, 722 * nodelocal address needs to be configured onto only one of them. 723 * XXX multiple link-local address case 724 */ 725void 726in6_ifattach(ifp, altifp) 727 struct ifnet *ifp; 728 struct ifnet *altifp; /* secondary EUI64 source */ 729{ 730 static size_t if_indexlim = 8; 731 struct in6_ifaddr *ia; 732 struct in6_addr in6; 733 734 /* some of the interfaces are inherently not IPv6 capable */ 735 switch (ifp->if_type) { 736#ifdef IFT_BRIDGE /* OpenBSD 2.8, NetBSD 1.6 */ 737 case IFT_BRIDGE: 738 return; 739#endif 740 } 741 742 /* 743 * We have some arrays that should be indexed by if_index. 744 * since if_index will grow dynamically, they should grow too. 745 * struct in6_ifstat **in6_ifstat 746 * struct icmp6_ifstat **icmp6_ifstat 747 */ 748 if (in6_ifstat == NULL || icmp6_ifstat == NULL || 749 if_index >= if_indexlim) { 750 size_t n; 751 caddr_t q; 752 size_t olim; 753 754 olim = if_indexlim; 755 while (if_index >= if_indexlim) 756 if_indexlim <<= 1; 757 758 /* grow in6_ifstat */ 759 n = if_indexlim * sizeof(struct in6_ifstat *); 760 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK); 761 bzero(q, n); 762 if (in6_ifstat) { 763 bcopy((caddr_t)in6_ifstat, q, 764 olim * sizeof(struct in6_ifstat *)); 765 free((caddr_t)in6_ifstat, M_IFADDR); 766 } 767 in6_ifstat = (struct in6_ifstat **)q; 768 in6_ifstatmax = if_indexlim; 769 770 /* grow icmp6_ifstat */ 771 n = if_indexlim * sizeof(struct icmp6_ifstat *); 772 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK); 773 bzero(q, n); 774 if (icmp6_ifstat) { 775 bcopy((caddr_t)icmp6_ifstat, q, 776 olim * sizeof(struct icmp6_ifstat *)); 777 free((caddr_t)icmp6_ifstat, M_IFADDR); 778 } 779 icmp6_ifstat = (struct icmp6_ifstat **)q; 780 icmp6_ifstatmax = if_indexlim; 781 } 782 783 /* initialize scope identifiers */ 784 scope6_ifattach(ifp); 785 786 /* 787 * quirks based on interface type 788 */ 789 switch (ifp->if_type) { 790#ifdef IFT_STF 791 case IFT_STF: 792 /* 793 * 6to4 interface is a very special kind of beast. 794 * no multicast, no linklocal. RFC2529 specifies how to make 795 * linklocals for 6to4 interface, but there's no use and 796 * it is rather harmful to have one. 797 */ 798 goto statinit; 799#endif 800 default: 801 break; 802 } 803 804 /* 805 * usually, we require multicast capability to the interface 806 */ 807 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 808 log(LOG_INFO, "in6_ifattach: " 809 "%s is not multicast capable, IPv6 not enabled\n", 810 if_name(ifp)); 811 return; 812 } 813 814 /* 815 * assign loopback address for loopback interface. 816 * XXX multiple loopback interface case. 817 */ 818 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 819 in6 = in6addr_loopback; 820 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) { 821 if (in6_ifattach_loopback(ifp) != 0) 822 return; 823 } 824 } 825 826 /* 827 * assign a link-local address, if there's none. 828 */ 829 if (ip6_auto_linklocal) { 830 ia = in6ifa_ifpforlinklocal(ifp, 0); 831 if (ia == NULL) { 832 if (in6_ifattach_linklocal(ifp, altifp) == 0) { 833 /* linklocal address assigned */ 834 } else { 835 /* failed to assign linklocal address. bark? */ 836 } 837 } 838 } 839 840#ifdef IFT_STF /* XXX */ 841statinit: 842#endif 843 844 /* update dynamically. */ 845 if (in6_maxmtu < ifp->if_mtu) 846 in6_maxmtu = ifp->if_mtu; 847 848 if (in6_ifstat[ifp->if_index] == NULL) { 849 in6_ifstat[ifp->if_index] = (struct in6_ifstat *) 850 malloc(sizeof(struct in6_ifstat), M_IFADDR, M_WAITOK); 851 bzero(in6_ifstat[ifp->if_index], sizeof(struct in6_ifstat)); 852 } 853 if (icmp6_ifstat[ifp->if_index] == NULL) { 854 icmp6_ifstat[ifp->if_index] = (struct icmp6_ifstat *) 855 malloc(sizeof(struct icmp6_ifstat), M_IFADDR, M_WAITOK); 856 bzero(icmp6_ifstat[ifp->if_index], sizeof(struct icmp6_ifstat)); 857 } 858 859 /* initialize NDP variables */ 860 nd6_ifattach(ifp); 861} 862 863/* 864 * NOTE: in6_ifdetach() does not support loopback if at this moment. 865 * We don't need this function in bsdi, because interfaces are never removed 866 * from the ifnet list in bsdi. 867 */ 868void 869in6_ifdetach(ifp) 870 struct ifnet *ifp; 871{ 872 struct in6_ifaddr *ia, *oia; 873 struct ifaddr *ifa, *next; 874 struct rtentry *rt; 875 short rtflags; 876 struct sockaddr_in6 sin6; 877 struct in6_multi *in6m; 878 struct in6_multi *in6m_next; 879 880 /* nuke prefix list. this may try to remove some of ifaddrs as well */ 881 in6_purgeprefix(ifp); 882 883 /* remove neighbor management table */ 884 nd6_purge(ifp); 885 886 /* nuke any of IPv6 addresses we have */ 887 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) { 888 next = ifa->ifa_list.tqe_next; 889 if (ifa->ifa_addr->sa_family != AF_INET6) 890 continue; 891 in6_purgeaddr(ifa); 892 } 893 894 /* undo everything done by in6_ifattach(), just in case */ 895 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) { 896 next = ifa->ifa_list.tqe_next; 897 898 if (ifa->ifa_addr->sa_family != AF_INET6 899 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) { 900 continue; 901 } 902 903 ia = (struct in6_ifaddr *)ifa; 904 905 /* remove from the routing table */ 906 if ((ia->ia_flags & IFA_ROUTE) && 907 (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) { 908 rtflags = rt->rt_flags; 909 rtfree(rt); 910 rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr, 911 (struct sockaddr *)&ia->ia_addr, 912 (struct sockaddr *)&ia->ia_prefixmask, 913 rtflags, (struct rtentry **)0); 914 } 915 916 /* remove from the linked list */ 917 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list); 918 IFAFREE(&ia->ia_ifa); 919 920 /* also remove from the IPv6 address chain(itojun&jinmei) */ 921 oia = ia; 922 if (oia == (ia = in6_ifaddr)) 923 in6_ifaddr = ia->ia_next; 924 else { 925 while (ia->ia_next && (ia->ia_next != oia)) 926 ia = ia->ia_next; 927 if (ia->ia_next) 928 ia->ia_next = oia->ia_next; 929 else { 930 nd6log((LOG_ERR, 931 "%s: didn't unlink in6ifaddr from list\n", 932 if_name(ifp))); 933 } 934 } 935 936 IFAFREE(&oia->ia_ifa); 937 } 938 939 /* leave from all multicast groups joined */ 940 941 if (udbinfo.listhead != NULL) 942 in6_pcbpurgeif0(LIST_FIRST(udbinfo.listhead), ifp); 943 if (ripcbinfo.listhead != NULL) 944 in6_pcbpurgeif0(LIST_FIRST(ripcbinfo.listhead), ifp); 945 946 for (in6m = LIST_FIRST(&in6_multihead); in6m; in6m = in6m_next) { 947 in6m_next = LIST_NEXT(in6m, in6m_entry); 948 if (in6m->in6m_ifp != ifp) 949 continue; 950 in6_delmulti(in6m); 951 in6m = NULL; 952 } 953 954 /* 955 * remove neighbor management table. we call it twice just to make 956 * sure we nuke everything. maybe we need just one call. 957 * XXX: since the first call did not release addresses, some prefixes 958 * might remain. We should call nd6_purge() again to release the 959 * prefixes after removing all addresses above. 960 * (Or can we just delay calling nd6_purge until at this point?) 961 */ 962 nd6_purge(ifp); 963 964 /* remove route to link-local allnodes multicast (ff02::1) */ 965 bzero(&sin6, sizeof(sin6)); 966 sin6.sin6_len = sizeof(struct sockaddr_in6); 967 sin6.sin6_family = AF_INET6; 968 sin6.sin6_addr = in6addr_linklocal_allnodes; 969 sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 970 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 971 if (rt) { 972 if (rt->rt_ifp == ifp) { 973 RT_UNLOCK(rt); 974 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt), 975 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); 976 RTFREE(rt); 977 } else 978 rtfree(rt); 979 } 980} 981 982void 983in6_get_tmpifid(ifp, retbuf, baseid, generate) 984 struct ifnet *ifp; 985 u_int8_t *retbuf; 986 const u_int8_t *baseid; 987 int generate; 988{ 989 u_int8_t nullbuf[8]; 990 struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index]; 991 992 bzero(nullbuf, sizeof(nullbuf)); 993 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) { 994 /* we've never created a random ID. Create a new one. */ 995 generate = 1; 996 } 997 998 if (generate) { 999 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1)); 1000 1001 /* generate_tmp_ifid will update seedn and buf */ 1002 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1, 1003 ndi->randomid); 1004 } 1005 bcopy(ndi->randomid, retbuf, 8); 1006} 1007 1008void 1009in6_tmpaddrtimer(ignored_arg) 1010 void *ignored_arg; 1011{ 1012 int i; 1013 struct nd_ifinfo *ndi; 1014 u_int8_t nullbuf[8]; 1015 int s = splnet(); 1016 1017 callout_reset(&in6_tmpaddrtimer_ch, 1018 (ip6_temp_preferred_lifetime - ip6_desync_factor - 1019 ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL); 1020 1021 bzero(nullbuf, sizeof(nullbuf)); 1022 for (i = 1; i < if_index + 1; i++) { 1023 ndi = &nd_ifinfo[i]; 1024 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) { 1025 /* 1026 * We've been generating a random ID on this interface. 1027 * Create a new one. 1028 */ 1029 (void)generate_tmp_ifid(ndi->randomseed0, 1030 ndi->randomseed1, ndi->randomid); 1031 } 1032 } 1033 1034 splx(s); 1035} 1036