in6_ifattach.c revision 1.26
1/* $OpenBSD: in6_ifattach.c,v 1.26 2002/06/07 04:34:45 itojun Exp $ */ 2/* $KAME: in6_ifattach.c,v 1.124 2001/07/18 08:32:51 jinmei 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/md5k.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 51#include <netinet/ip6.h> 52#include <netinet6/ip6_var.h> 53#include <netinet6/in6_ifattach.h> 54#include <netinet6/ip6_var.h> 55#include <netinet6/nd6.h> 56 57unsigned long in6_maxmtu = 0; 58 59static int get_rand_ifid(struct ifnet *, struct in6_addr *); 60static int get_hw_ifid(struct ifnet *, struct in6_addr *); 61static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *); 62static int in6_ifattach_addaddr(struct ifnet *, struct in6_ifaddr *); 63static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *); 64static int in6_ifattach_loopback(struct ifnet *); 65 66#define EUI64_GBIT 0x01 67#define EUI64_UBIT 0x02 68#define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) 69#define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 70#define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6)) 71#define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT) 72#define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6)) 73 74#define IFID_LOCAL(in6) (!EUI64_LOCAL(in6)) 75#define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6)) 76 77/* 78 * Generate a last-resort interface identifier, when the machine has no 79 * IEEE802/EUI64 address sources. 80 * The goal here is to get an interface identifier that is 81 * (1) random enough and (2) does not change across reboot. 82 * We currently use MD5(hostname) for it. 83 */ 84static int 85get_rand_ifid(ifp, in6) 86 struct ifnet *ifp; 87 struct in6_addr *in6; /* upper 64bits are preserved */ 88{ 89 MD5_CTX ctxt; 90 u_int8_t digest[16]; 91 92#if 0 93 /* we need at least several letters as seed for ifid */ 94 if (hostnamelen < 3) 95 return -1; 96#endif 97 98 /* generate 8 bytes of pseudo-random value. */ 99 bzero(&ctxt, sizeof(ctxt)); 100 MD5Init(&ctxt); 101 MD5Update(&ctxt, hostname, hostnamelen); 102 MD5Final(digest, &ctxt); 103 104 /* assumes sizeof(digest) > sizeof(ifid) */ 105 bcopy(digest, &in6->s6_addr[8], 8); 106 107 /* make sure to set "u" bit to local, and "g" bit to individual. */ 108 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 109 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 110 111 /* convert EUI64 into IPv6 interface identifier */ 112 EUI64_TO_IFID(in6); 113 114 return 0; 115} 116 117/* 118 * Get interface identifier for the specified interface. 119 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface 120 */ 121static int 122get_hw_ifid(ifp, in6) 123 struct ifnet *ifp; 124 struct in6_addr *in6; /* upper 64bits are preserved */ 125{ 126 struct ifaddr *ifa; 127 struct sockaddr_dl *sdl; 128 u_int8_t *addr; 129 size_t addrlen; 130 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 131 static u_int8_t allone[8] = 132 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 133 134 for (ifa = ifp->if_addrlist.tqh_first; 135 ifa; 136 ifa = ifa->ifa_list.tqe_next) 137 { 138 if (ifa->ifa_addr->sa_family != AF_LINK) 139 continue; 140 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 141 if (sdl == NULL) 142 continue; 143 if (sdl->sdl_alen == 0) 144 continue; 145 146 goto found; 147 } 148 149 return -1; 150 151found: 152 addr = LLADDR(sdl); 153 addrlen = sdl->sdl_alen; 154 155 /* get EUI64 */ 156 switch (ifp->if_type) { 157 case IFT_ETHER: 158 case IFT_FDDI: 159 case IFT_ATM: 160 /* IEEE802/EUI64 cases - what others? */ 161 162 /* look at IEEE802/EUI64 only */ 163 if (addrlen != 8 && addrlen != 6) 164 return -1; 165 166 /* 167 * check for invalid MAC address - on bsdi, we see it a lot 168 * since wildboar configures all-zero MAC on pccard before 169 * card insertion. 170 */ 171 if (bcmp(addr, allzero, addrlen) == 0) 172 return -1; 173 if (bcmp(addr, allone, addrlen) == 0) 174 return -1; 175 176 /* make EUI64 address */ 177 if (addrlen == 8) 178 bcopy(addr, &in6->s6_addr[8], 8); 179 else if (addrlen == 6) { 180 in6->s6_addr[8] = addr[0]; 181 in6->s6_addr[9] = addr[1]; 182 in6->s6_addr[10] = addr[2]; 183 in6->s6_addr[11] = 0xff; 184 in6->s6_addr[12] = 0xfe; 185 in6->s6_addr[13] = addr[3]; 186 in6->s6_addr[14] = addr[4]; 187 in6->s6_addr[15] = addr[5]; 188 } 189 break; 190 191 case IFT_ARCNET: 192 if (addrlen != 1) 193 return -1; 194 if (!addr[0]) 195 return -1; 196 197 bzero(&in6->s6_addr[8], 8); 198 in6->s6_addr[15] = addr[0]; 199 200 /* 201 * due to insufficient bitwidth, we mark it local. 202 */ 203 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 204 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 205 break; 206 207 case IFT_GIF: 208#ifdef IFT_STF 209 case IFT_STF: 210#endif 211 /* 212 * RFC2893 says: "SHOULD use IPv4 address as ifid source". 213 * however, IPv4 address is not very suitable as unique 214 * identifier source (can be renumbered). 215 * we don't do this. 216 */ 217 return -1; 218 219 default: 220 return -1; 221 } 222 223 /* sanity check: g bit must not indicate "group" */ 224 if (EUI64_GROUP(in6)) 225 return -1; 226 227 /* convert EUI64 into IPv6 interface identifier */ 228 EUI64_TO_IFID(in6); 229 230 /* 231 * sanity check: ifid must not be all zero, avoid conflict with 232 * subnet router anycast 233 */ 234 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && 235 bcmp(&in6->s6_addr[9], allzero, 7) == 0) { 236 return -1; 237 } 238 239 return 0; 240} 241 242/* 243 * Get interface identifier for the specified interface. If it is not 244 * available on ifp0, borrow interface identifier from other information 245 * sources. 246 */ 247static int 248get_ifid(ifp0, altifp, in6) 249 struct ifnet *ifp0; 250 struct ifnet *altifp; /* secondary EUI64 source */ 251 struct in6_addr *in6; 252{ 253 struct ifnet *ifp; 254 255 /* first, try to get it from the interface itself */ 256 if (get_hw_ifid(ifp0, in6) == 0) { 257 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 258 ifp0->if_xname)); 259 goto success; 260 } 261 262 /* try secondary EUI64 source. this basically is for ATM PVC */ 263 if (altifp && get_hw_ifid(altifp, in6) == 0) { 264 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", 265 ifp0->if_xname, altifp->if_xname)); 266 goto success; 267 } 268 269 /* next, try to get it from some other hardware interface */ 270 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) 271 { 272 if (ifp == ifp0) 273 continue; 274 if (get_hw_ifid(ifp, in6) != 0) 275 continue; 276 277 /* 278 * to borrow ifid from other interface, ifid needs to be 279 * globally unique 280 */ 281 if (IFID_UNIVERSAL(in6)) { 282 nd6log((LOG_DEBUG, 283 "%s: borrow interface identifier from %s\n", 284 ifp0->if_xname, ifp->if_xname)); 285 goto success; 286 } 287 } 288 289 /* last resort: get from random number source */ 290 if (get_rand_ifid(ifp, in6) == 0) { 291 nd6log((LOG_DEBUG, 292 "%s: interface identifier generated by random number\n", 293 ifp0->if_xname)); 294 goto success; 295 } 296 297 printf("%s: failed to get interface identifier\n", ifp0->if_xname); 298 return -1; 299 300success: 301 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 302 ifp0->if_xname, in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 303 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 304 in6->s6_addr[14], in6->s6_addr[15])); 305 return 0; 306} 307 308/* 309 * configure IPv6 interface address. XXX code duplicated with in.c 310 */ 311static int 312in6_ifattach_addaddr(ifp, ia) 313 struct ifnet *ifp; 314 struct in6_ifaddr *ia; 315{ 316 struct in6_ifaddr *oia; 317 struct ifaddr *ifa; 318 int error; 319 int rtflag; 320 struct in6_addr llsol; 321 322 /* 323 * initialize if_addrlist, if we are the very first one 324 */ 325 ifa = TAILQ_FIRST(&ifp->if_addrlist); 326 if (ifa == NULL) { 327 TAILQ_INIT(&ifp->if_addrlist); 328 } 329 330 /* 331 * link the interface address to global list 332 */ 333 TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list); 334 ia->ia_ifa.ifa_refcnt++; 335 336 /* 337 * Also link into the IPv6 address chain beginning with in6_ifaddr. 338 * kazu opposed it, but itojun & jinmei wanted. 339 */ 340 if ((oia = in6_ifaddr) != NULL) { 341 for (; oia->ia_next; oia = oia->ia_next) 342 continue; 343 oia->ia_next = ia; 344 } else 345 in6_ifaddr = ia; 346 ia->ia_ifa.ifa_refcnt++; 347 348 /* 349 * give the interface a chance to initialize, in case this 350 * is the first address to be added. 351 */ 352 if (ifp->if_ioctl != NULL) { 353 int s; 354 s = splimp(); 355 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 356 splx(s); 357 } else 358 error = 0; 359 if (error) { 360 switch (error) { 361 case EAFNOSUPPORT: 362 printf("%s: IPv6 not supported\n", ifp->if_xname); 363 break; 364 default: 365 printf("%s: SIOCSIFADDR error %d\n", ifp->if_xname, 366 error); 367 break; 368 } 369 370 /* undo changes */ 371 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list); 372 IFAFREE(&ia->ia_ifa); 373 if (oia) 374 oia->ia_next = ia->ia_next; 375 else 376 in6_ifaddr = ia->ia_next; 377 IFAFREE(&ia->ia_ifa); 378 return -1; 379 } 380 381 /* configure link-layer address resolution */ 382 rtflag = 0; 383 if (IN6_ARE_ADDR_EQUAL(&ia->ia_prefixmask.sin6_addr, &in6mask128)) 384 rtflag = RTF_HOST; 385 else { 386 switch (ifp->if_type) { 387 case IFT_LOOP: 388#ifdef IFT_STF 389 case IFT_STF: 390#endif 391 rtflag = 0; 392 break; 393 default: 394 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 395 ia->ia_ifa.ifa_flags |= RTF_CLONING; 396 rtflag = RTF_CLONING; 397 break; 398 } 399 } 400 401 /* add route to the interface. */ 402 rtrequest(RTM_ADD, 403 (struct sockaddr *)&ia->ia_addr, 404 (struct sockaddr *)&ia->ia_addr, 405 (struct sockaddr *)&ia->ia_prefixmask, 406 RTF_UP | rtflag, 407 (struct rtentry **)0); 408 ia->ia_flags |= IFA_ROUTE; 409 410 if ((rtflag & RTF_CLONING) != 0 && 411 (ifp->if_flags & IFF_MULTICAST) != 0) { 412 /* Restore saved multicast addresses (if any). */ 413 in6_restoremkludge(ia, ifp); 414 415 /* 416 * join solicited multicast address 417 */ 418 bzero(&llsol, sizeof(llsol)); 419 llsol.s6_addr16[0] = htons(0xff02); 420 llsol.s6_addr16[1] = htons(ifp->if_index); 421 llsol.s6_addr32[1] = 0; 422 llsol.s6_addr32[2] = htonl(1); 423 llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3]; 424 llsol.s6_addr8[12] = 0xff; 425 (void)in6_addmulti(&llsol, ifp, &error); 426 427 /* XXX should we run DAD on other interface types? */ 428 if (in6if_do_dad(ifp)) { 429 /* mark the address TENTATIVE, if needed. */ 430 ia->ia6_flags |= IN6_IFF_TENTATIVE; 431 /* nd6_dad_start() will be called in in6_if_up */ 432 } 433 } 434 435 return 0; 436} 437 438static int 439in6_ifattach_linklocal(ifp, altifp) 440 struct ifnet *ifp; 441 struct ifnet *altifp; /*secondary EUI64 source*/ 442{ 443 struct in6_ifaddr *ia; 444 445 /* 446 * configure link-local address 447 */ 448 ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK); 449 bzero((caddr_t)ia, sizeof(*ia)); 450 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 451 if (ifp->if_flags & IFF_POINTOPOINT) 452 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 453 else 454 ia->ia_ifa.ifa_dstaddr = NULL; 455 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 456 ia->ia_ifp = ifp; 457 458 bzero(&ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 459 ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 460 ia->ia_prefixmask.sin6_family = AF_INET6; 461 ia->ia_prefixmask.sin6_addr = in6mask64; 462 463 /* just in case */ 464 bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr)); 465 ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 466 ia->ia_dstaddr.sin6_family = AF_INET6; 467 468 bzero(&ia->ia_addr, sizeof(ia->ia_addr)); 469 ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6); 470 ia->ia_addr.sin6_family = AF_INET6; 471 ia->ia_addr.sin6_addr.s6_addr16[0] = htons(0xfe80); 472 ia->ia_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 473 ia->ia_addr.sin6_addr.s6_addr32[1] = 0; 474 if (ifp->if_flags & IFF_LOOPBACK) { 475 ia->ia_addr.sin6_addr.s6_addr32[2] = 0; 476 ia->ia_addr.sin6_addr.s6_addr32[3] = htonl(1); 477 } else { 478 if (get_ifid(ifp, altifp, &ia->ia_addr.sin6_addr) != 0) { 479 nd6log((LOG_ERR, 480 "%s: no ifid available\n", ifp->if_xname)); 481 free(ia, M_IFADDR); 482 return -1; 483 } 484 } 485 486 ia->ia_ifa.ifa_metric = ifp->if_metric; 487 488 if (in6_ifattach_addaddr(ifp, ia) != 0) { 489 /* ia will be freed on failure */ 490 return -1; 491 } 492 493 return 0; 494} 495 496static int 497in6_ifattach_loopback(ifp) 498 struct ifnet *ifp; /* must be IFT_LOOP */ 499{ 500 struct in6_ifaddr *ia; 501 502 /* 503 * configure link-local address 504 */ 505 ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK); 506 bzero((caddr_t)ia, sizeof(*ia)); 507 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 508 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 509 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 510 ia->ia_ifp = ifp; 511 512 bzero(&ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 513 ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 514 ia->ia_prefixmask.sin6_family = AF_INET6; 515 ia->ia_prefixmask.sin6_addr = in6mask128; 516 517 /* 518 * Always initialize ia_dstaddr (= broadcast address) to loopback 519 * address, to make getifaddr happier. 520 * 521 * For BSDI, it is mandatory. The BSDI version of 522 * ifa_ifwithroute() rejects to add a route to the loopback 523 * interface. Even for other systems, loopback looks somewhat 524 * special. 525 */ 526 bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr)); 527 ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 528 ia->ia_dstaddr.sin6_family = AF_INET6; 529 ia->ia_dstaddr.sin6_addr = in6addr_loopback; 530 531 bzero(&ia->ia_addr, sizeof(ia->ia_addr)); 532 ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6); 533 ia->ia_addr.sin6_family = AF_INET6; 534 ia->ia_addr.sin6_addr = in6addr_loopback; 535 536 ia->ia_ifa.ifa_metric = ifp->if_metric; 537 538 if (in6_ifattach_addaddr(ifp, ia) != 0) { 539 /* ia will be freed on failure */ 540 return -1; 541 } 542 543 return 0; 544} 545 546/* 547 * XXX multiple loopback interface needs more care. for instance, 548 * nodelocal address needs to be configured onto only one of them. 549 * XXX multiple link-local address case 550 */ 551void 552in6_ifattach(ifp, altifp) 553 struct ifnet *ifp; 554 struct ifnet *altifp; /* secondary EUI64 source */ 555{ 556 struct sockaddr_in6 mltaddr; 557 struct sockaddr_in6 mltmask; 558 struct sockaddr_in6 gate; 559 struct sockaddr_in6 mask; 560 struct in6_ifaddr *ia; 561 struct in6_addr in6; 562 563 /* some of the interfaces are inherently not IPv6 capable */ 564 switch (ifp->if_type) { 565 case IFT_BRIDGE: 566 case IFT_ENC: 567 case IFT_PFLOG: 568 return; 569 case IFT_PROPVIRTUAL: 570 if (strncmp("bridge", ifp->if_xname, sizeof("bridge")) == 0 && 571 '0' <= ifp->if_xname[sizeof("bridge")] && 572 ifp->if_xname[sizeof("bridge")] <= '9') 573 return; 574 break; 575 } 576 577 /* 578 * if link mtu is too small, don't try to configure IPv6. 579 * remember there could be some link-layer that has special 580 * fragmentation logic. 581 */ 582 if (ifp->if_mtu < IPV6_MMTU) 583 return; 584 585 /* create a multicast kludge storage (if we have not had one) */ 586 in6_createmkludge(ifp); 587 588 /* 589 * quirks based on interface type 590 */ 591 switch (ifp->if_type) { 592#ifdef IFT_STF 593 case IFT_STF: 594 /* 595 * 6to4 interface is a very special kind of beast. 596 * no multicast, no linklocal. RFC2529 specifies how to make 597 * linklocals for 6to4 interface, but there's no use and 598 * it is rather harmful to have one. 599 */ 600 return; 601#endif 602 default: 603 break; 604 } 605 606 /* 607 * usually, we require multicast capability to the interface 608 */ 609 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 610 log(LOG_INFO, "in6_ifattach: " 611 "%s is not multicast capable, IPv6 not enabled\n", 612 ifp->if_xname); 613 return; 614 } 615 616 /* 617 * assign link-local address, if there's none 618 */ 619 ia = in6ifa_ifpforlinklocal(ifp, 0); 620 if (ia == NULL) { 621 if (in6_ifattach_linklocal(ifp, altifp) != 0) 622 return; 623 ia = in6ifa_ifpforlinklocal(ifp, 0); 624 625 if (ia == NULL) { 626 printf("%s: failed to add link-local address\n", 627 ifp->if_xname); 628 629 /* we can't initialize multicasts without link-local */ 630 return; 631 } 632 } 633 634 if (ifp->if_flags & IFF_POINTOPOINT) { 635 /* 636 * route local address to loopback 637 */ 638 bzero(&gate, sizeof(gate)); 639 gate.sin6_len = sizeof(struct sockaddr_in6); 640 gate.sin6_family = AF_INET6; 641 gate.sin6_addr = in6addr_loopback; 642 bzero(&mask, sizeof(mask)); 643 mask.sin6_len = sizeof(struct sockaddr_in6); 644 mask.sin6_family = AF_INET6; 645 mask.sin6_addr = in6mask64; 646 rtrequest(RTM_ADD, 647 (struct sockaddr *)&ia->ia_addr, 648 (struct sockaddr *)&gate, 649 (struct sockaddr *)&mask, 650 RTF_UP|RTF_HOST, 651 (struct rtentry **)0); 652 } 653 654 /* 655 * assign loopback address for loopback interface 656 * XXX multiple loopback interface case 657 */ 658 in6 = in6addr_loopback; 659 if (ifp->if_flags & IFF_LOOPBACK) { 660 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) { 661 if (in6_ifattach_loopback(ifp) != 0) 662 return; 663 } 664 } 665 666#ifdef DIAGNOSTIC 667 if (!ia) { 668 panic("ia == NULL in in6_ifattach"); 669 /*NOTREACHED*/ 670 } 671#endif 672 673 /* 674 * join multicast 675 */ 676 if (ifp->if_flags & IFF_MULTICAST) { 677 int error; /* not used */ 678 struct in6_multi *in6m; 679 680 /* Restore saved multicast addresses(if any). */ 681 in6_restoremkludge(ia, ifp); 682 683 bzero(&mltmask, sizeof(mltmask)); 684 mltmask.sin6_len = sizeof(struct sockaddr_in6); 685 mltmask.sin6_family = AF_INET6; 686 mltmask.sin6_addr = in6mask32; 687 688 /* 689 * join link-local all-nodes address 690 */ 691 bzero(&mltaddr, sizeof(mltaddr)); 692 mltaddr.sin6_len = sizeof(struct sockaddr_in6); 693 mltaddr.sin6_family = AF_INET6; 694 mltaddr.sin6_addr = in6addr_linklocal_allnodes; 695 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 696 697 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m); 698 if (in6m == NULL) { 699 rtrequest(RTM_ADD, 700 (struct sockaddr *)&mltaddr, 701 (struct sockaddr *)&ia->ia_addr, 702 (struct sockaddr *)&mltmask, 703 RTF_UP|RTF_CLONING, /* xxx */ 704 (struct rtentry **)0); 705 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error); 706 } 707 708 if (ifp->if_flags & IFF_LOOPBACK) { 709 in6 = in6addr_loopback; 710 ia = in6ifa_ifpwithaddr(ifp, &in6); 711 /* 712 * join node-local all-nodes address, on loopback 713 */ 714 mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 715 716 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m); 717 if (in6m == NULL && ia != NULL) { 718 rtrequest(RTM_ADD, 719 (struct sockaddr *)&mltaddr, 720 (struct sockaddr *)&ia->ia_addr, 721 (struct sockaddr *)&mltmask, 722 RTF_UP, 723 (struct rtentry **)0); 724 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error); 725 } 726 } 727 } 728} 729 730/* 731 * NOTE: in6_ifdetach() does not support loopback if at this moment. 732 */ 733void 734in6_ifdetach(ifp) 735 struct ifnet *ifp; 736{ 737 struct in6_ifaddr *ia, *oia; 738 struct ifaddr *ifa, *next; 739 struct rtentry *rt; 740 short rtflags; 741 struct sockaddr_in6 sin6; 742 struct in6_multi *in6m; 743 744 /* nuke prefix list. this may try to remove some of ifaddrs as well */ 745 in6_purgeprefix(ifp); 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 { 753 next = ifa->ifa_list.tqe_next; 754 if (ifa->ifa_addr->sa_family != AF_INET6) 755 continue; 756 in6_purgeaddr(ifa, ifp); 757 } 758 759 /* undo everything done by in6_ifattach(), just in case */ 760 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = 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 /* leave from all multicast groups joined */ 770 while ((in6m = LIST_FIRST(&ia->ia6_multiaddrs)) != NULL) 771 in6_delmulti(in6m); 772 773 /* remove from the routing table */ 774 if ((ia->ia_flags & IFA_ROUTE) 775 && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) { 776 rtflags = rt->rt_flags; 777 rtfree(rt); 778 rtrequest(RTM_DELETE, 779 (struct sockaddr *)&ia->ia_addr, 780 (struct sockaddr *)&ia->ia_addr, 781 (struct sockaddr *)&ia->ia_prefixmask, 782 rtflags, (struct rtentry **)0); 783 } 784 785 /* remove from the linked list */ 786 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list); 787 IFAFREE(&ia->ia_ifa); 788 789 /* also remove from the IPv6 address chain(itojun&jinmei) */ 790 oia = ia; 791 if (oia == (ia = in6_ifaddr)) 792 in6_ifaddr = ia->ia_next; 793 else { 794 while (ia->ia_next && (ia->ia_next != oia)) 795 ia = ia->ia_next; 796 if (ia->ia_next) 797 ia->ia_next = oia->ia_next; 798 else { 799 nd6log((LOG_ERR, 800 "%s: didn't unlink in6ifaddr from list\n", 801 ifp->if_xname)); 802 } 803 } 804 805 IFAFREE(&oia->ia_ifa); 806 } 807 808 /* cleanup multicast address kludge table, if there is any */ 809 in6_purgemkludge(ifp); 810 811 /* 812 * remove neighbor management table. we call it twice just to make 813 * sure we nuke everything. maybe we need just one call. 814 * XXX: since the first call did not release addresses, some prefixes 815 * might remain. We should call nd6_purge() again to release the 816 * prefixes after removing all addresses above. 817 * (Or can we just delay calling nd6_purge until at this point?) 818 */ 819 nd6_purge(ifp); 820 821 /* remove route to link-local allnodes multicast (ff02::1) */ 822 bzero(&sin6, sizeof(sin6)); 823 sin6.sin6_len = sizeof(struct sockaddr_in6); 824 sin6.sin6_family = AF_INET6; 825 sin6.sin6_addr = in6addr_linklocal_allnodes; 826 sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 827 rt = rtalloc1((struct sockaddr *)&sin6, 0); 828 if (rt && rt->rt_ifp == ifp) { 829 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt), 830 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); 831 rtfree(rt); 832 } 833} 834