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