in6_ifattach.c revision 1.33
1/* $OpenBSD: in6_ifattach.c,v 1.33 2003/05/14 05:23:37 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 59int ip6_auto_linklocal = 1; /* enable by default */ 60 61static int get_rand_ifid(struct ifnet *, struct in6_addr *); 62static int get_hw_ifid(struct ifnet *, struct in6_addr *); 63static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *); 64static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *); 65static int in6_ifattach_loopback(struct ifnet *); 66 67#define EUI64_GBIT 0x01 68#define EUI64_UBIT 0x02 69#define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) 70#define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 71#define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6)) 72#define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT) 73#define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6)) 74 75#define IFID_LOCAL(in6) (!EUI64_LOCAL(in6)) 76#define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6)) 77 78/* 79 * Generate a last-resort interface identifier, when the machine has no 80 * IEEE802/EUI64 address sources. 81 * The goal here is to get an interface identifier that is 82 * (1) random enough and (2) does not change across reboot. 83 * We currently use MD5(hostname) for it. 84 */ 85static int 86get_rand_ifid(ifp, in6) 87 struct ifnet *ifp; 88 struct in6_addr *in6; /* upper 64bits are preserved */ 89{ 90 MD5_CTX ctxt; 91 u_int8_t digest[16]; 92 93#if 0 94 /* we need at least several letters as seed for ifid */ 95 if (hostnamelen < 3) 96 return -1; 97#endif 98 99 /* generate 8 bytes of pseudo-random value. */ 100 bzero(&ctxt, sizeof(ctxt)); 101 MD5Init(&ctxt); 102 MD5Update(&ctxt, hostname, hostnamelen); 103 MD5Final(digest, &ctxt); 104 105 /* assumes sizeof(digest) > sizeof(ifid) */ 106 bcopy(digest, &in6->s6_addr[8], 8); 107 108 /* make sure to set "u" bit to local, and "g" bit to individual. */ 109 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 110 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 111 112 /* convert EUI64 into IPv6 interface identifier */ 113 EUI64_TO_IFID(in6); 114 115 return 0; 116} 117 118/* 119 * Get interface identifier for the specified interface. 120 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface 121 */ 122static int 123get_hw_ifid(ifp, in6) 124 struct ifnet *ifp; 125 struct in6_addr *in6; /* upper 64bits are preserved */ 126{ 127 struct ifaddr *ifa; 128 struct sockaddr_dl *sdl; 129 char *addr; 130 size_t addrlen; 131 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 132 static u_int8_t allone[8] = 133 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 134 135 for (ifa = ifp->if_addrlist.tqh_first; 136 ifa; 137 ifa = ifa->ifa_list.tqe_next) 138 { 139 if (ifa->ifa_addr->sa_family != AF_LINK) 140 continue; 141 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 142 if (sdl == NULL) 143 continue; 144 if (sdl->sdl_alen == 0) 145 continue; 146 147 goto found; 148 } 149 150 return -1; 151 152found: 153 addr = LLADDR(sdl); 154 addrlen = sdl->sdl_alen; 155 156 switch (ifp->if_type) { 157 case IFT_IEEE1394: 158 case IFT_IEEE80211: 159 /* IEEE1394 uses 16byte length address starting with EUI64 */ 160 if (addrlen > 8) 161 addrlen = 8; 162 break; 163 default: 164 break; 165 } 166 167 /* get EUI64 */ 168 switch (ifp->if_type) { 169 /* IEEE802/EUI64 cases - what others? */ 170 case IFT_ETHER: 171 case IFT_FDDI: 172 case IFT_ATM: 173 case IFT_IEEE1394: 174 case IFT_IEEE80211: 175 /* look at IEEE802/EUI64 only */ 176 if (addrlen != 8 && addrlen != 6) 177 return -1; 178 179 /* 180 * check for invalid MAC address - on bsdi, we see it a lot 181 * since wildboar configures all-zero MAC on pccard before 182 * card insertion. 183 */ 184 if (bcmp(addr, allzero, addrlen) == 0) 185 return -1; 186 if (bcmp(addr, allone, addrlen) == 0) 187 return -1; 188 189 /* make EUI64 address */ 190 if (addrlen == 8) 191 bcopy(addr, &in6->s6_addr[8], 8); 192 else if (addrlen == 6) { 193 in6->s6_addr[8] = addr[0]; 194 in6->s6_addr[9] = addr[1]; 195 in6->s6_addr[10] = addr[2]; 196 in6->s6_addr[11] = 0xff; 197 in6->s6_addr[12] = 0xfe; 198 in6->s6_addr[13] = addr[3]; 199 in6->s6_addr[14] = addr[4]; 200 in6->s6_addr[15] = addr[5]; 201 } 202 break; 203 204 case IFT_ARCNET: 205 if (addrlen != 1) 206 return -1; 207 if (!addr[0]) 208 return -1; 209 210 bzero(&in6->s6_addr[8], 8); 211 in6->s6_addr[15] = addr[0]; 212 213 /* 214 * due to insufficient bitwidth, we mark it local. 215 */ 216 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 217 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 218 break; 219 220 case IFT_GIF: 221 /* 222 * RFC2893 says: "SHOULD use IPv4 address as ifid source". 223 * however, IPv4 address is not very suitable as unique 224 * identifier source (can be renumbered). 225 * we don't do this. 226 */ 227 return -1; 228 229 default: 230 return -1; 231 } 232 233 /* sanity check: g bit must not indicate "group" */ 234 if (EUI64_GROUP(in6)) 235 return -1; 236 237 /* convert EUI64 into IPv6 interface identifier */ 238 EUI64_TO_IFID(in6); 239 240 /* 241 * sanity check: ifid must not be all zero, avoid conflict with 242 * subnet router anycast 243 */ 244 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && 245 bcmp(&in6->s6_addr[9], allzero, 7) == 0) { 246 return -1; 247 } 248 249 return 0; 250} 251 252/* 253 * Get interface identifier for the specified interface. If it is not 254 * available on ifp0, borrow interface identifier from other information 255 * sources. 256 */ 257static int 258get_ifid(ifp0, altifp, in6) 259 struct ifnet *ifp0; 260 struct ifnet *altifp; /* secondary EUI64 source */ 261 struct in6_addr *in6; 262{ 263 struct ifnet *ifp; 264 265 /* first, try to get it from the interface itself */ 266 if (get_hw_ifid(ifp0, in6) == 0) { 267 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 268 ifp0->if_xname)); 269 goto success; 270 } 271 272 /* try secondary EUI64 source. this basically is for ATM PVC */ 273 if (altifp && get_hw_ifid(altifp, in6) == 0) { 274 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", 275 ifp0->if_xname, altifp->if_xname)); 276 goto success; 277 } 278 279 /* next, try to get it from some other hardware interface */ 280 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) 281 { 282 if (ifp == ifp0) 283 continue; 284 if (get_hw_ifid(ifp, in6) != 0) 285 continue; 286 287 /* 288 * to borrow ifid from other interface, ifid needs to be 289 * globally unique 290 */ 291 if (IFID_UNIVERSAL(in6)) { 292 nd6log((LOG_DEBUG, 293 "%s: borrow interface identifier from %s\n", 294 ifp0->if_xname, ifp->if_xname)); 295 goto success; 296 } 297 } 298 299 /* last resort: get from random number source */ 300 if (get_rand_ifid(ifp, in6) == 0) { 301 nd6log((LOG_DEBUG, 302 "%s: interface identifier generated by random number\n", 303 ifp0->if_xname)); 304 goto success; 305 } 306 307 printf("%s: failed to get interface identifier\n", ifp0->if_xname); 308 return -1; 309 310success: 311 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 312 ifp0->if_xname, in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 313 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 314 in6->s6_addr[14], in6->s6_addr[15])); 315 return 0; 316} 317 318static int 319in6_ifattach_linklocal(ifp, altifp) 320 struct ifnet *ifp; 321 struct ifnet *altifp; /*secondary EUI64 source*/ 322{ 323 struct in6_ifaddr *ia; 324 struct in6_aliasreq ifra; 325 struct nd_prefix pr0; 326 int i, error; 327 328 /* 329 * configure link-local address. 330 */ 331 bzero(&ifra, sizeof(ifra)); 332 333 /* 334 * in6_update_ifa() does not use ifra_name, but we accurately set it 335 * for safety. 336 */ 337 strncpy(ifra.ifra_name, ifp->if_xname, sizeof(ifra.ifra_name)); 338 339 ifra.ifra_addr.sin6_family = AF_INET6; 340 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 341 ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80); 342 ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 343 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 344 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 345 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; 346 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); 347 } else { 348 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) { 349 nd6log((LOG_ERR, 350 "%s: no ifid available\n", ifp->if_xname)); 351 return (-1); 352 } 353 } 354 355 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 356 ifra.ifra_prefixmask.sin6_family = AF_INET6; 357 ifra.ifra_prefixmask.sin6_addr = in6mask64; 358#ifdef SCOPEDROUTING 359 /* take into account the sin6_scope_id field for routing */ 360 ifra.ifra_prefixmask.sin6_scope_id = 0xffffffff; 361#endif 362 /* link-local addresses should NEVER expire. */ 363 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 364 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 365 366 /* 367 * Do not let in6_update_ifa() do DAD, since we need a random delay 368 * before sending an NS at the first time the interface becomes up. 369 * Instead, in6_if_up() will start DAD with a proper random delay. 370 */ 371 ifra.ifra_flags |= IN6_IFF_NODAD; 372 373 /* 374 * Now call in6_update_ifa() to do a bunch of procedures to configure 375 * a link-local address. We can set NULL to the 3rd argument, because 376 * we know there's no other link-local address on the interface 377 * and therefore we are adding one (instead of updating one). 378 */ 379 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) { 380 /* 381 * XXX: When the interface does not support IPv6, this call 382 * would fail in the SIOCSIFADDR ioctl. I believe the 383 * notification is rather confusing in this case, so just 384 * suppress it. (jinmei@kame.net 20010130) 385 */ 386 if (error != EAFNOSUPPORT) 387 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to " 388 "configure a link-local address on %s " 389 "(errno=%d)\n", 390 ifp->if_xname, error)); 391 return (-1); 392 } 393 394 /* 395 * Adjust ia6_flags so that in6_if_up will perform DAD. 396 * XXX: Some P2P interfaces seem not to send packets just after 397 * becoming up, so we skip p2p interfaces for safety. 398 */ 399 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */ 400#ifdef DIAGNOSTIC 401 if (!ia) { 402 panic("ia == NULL in in6_ifattach_linklocal"); 403 /* NOTREACHED */ 404 } 405#endif 406 if (in6if_do_dad(ifp) && (ifp->if_flags & IFF_POINTOPOINT) == 0) { 407 ia->ia6_flags &= ~IN6_IFF_NODAD; 408 ia->ia6_flags |= IN6_IFF_TENTATIVE; 409 } 410 411 /* 412 * Make the link-local prefix (fe80::/64%link) as on-link. 413 * Since we'd like to manage prefixes separately from addresses, 414 * we make an ND6 prefix structure for the link-local prefix, 415 * and add it to the prefix list as a never-expire prefix. 416 * XXX: this change might affect some existing code base... 417 */ 418 bzero(&pr0, sizeof(pr0)); 419 pr0.ndpr_ifp = ifp; 420 /* this should be 64 at this moment. */ 421 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); 422 pr0.ndpr_mask = ifra.ifra_prefixmask.sin6_addr; 423 pr0.ndpr_prefix = ifra.ifra_addr; 424 /* apply the mask for safety. (nd6_prelist_add will apply it again) */ 425 for (i = 0; i < 4; i++) { 426 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 427 in6mask64.s6_addr32[i]; 428 } 429 /* 430 * Initialize parameters. The link-local prefix must always be 431 * on-link, and its lifetimes never expire. 432 */ 433 pr0.ndpr_raf_onlink = 1; 434 pr0.ndpr_raf_auto = 1; /* probably meaningless */ 435 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME; 436 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME; 437 /* 438 * Since there is no other link-local addresses, nd6_prefix_lookup() 439 * probably returns NULL. However, we cannot always expect the result. 440 * For example, if we first remove the (only) existing link-local 441 * address, and then reconfigure another one, the prefix is still 442 * valid with referring to the old link-local address. 443 */ 444 if (nd6_prefix_lookup(&pr0) == NULL) { 445 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0) 446 return (error); 447 } 448 449 return 0; 450} 451 452static int 453in6_ifattach_loopback(ifp) 454 struct ifnet *ifp; /* must be IFT_LOOP */ 455{ 456 struct in6_aliasreq ifra; 457 int error; 458 459 bzero(&ifra, sizeof(ifra)); 460 461 /* 462 * in6_update_ifa() does not use ifra_name, but we accurately set it 463 * for safety. 464 */ 465 strncpy(ifra.ifra_name, ifp->if_xname, sizeof(ifra.ifra_name)); 466 467 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 468 ifra.ifra_prefixmask.sin6_family = AF_INET6; 469 ifra.ifra_prefixmask.sin6_addr = in6mask128; 470 471 /* 472 * Always initialize ia_dstaddr (= broadcast address) to loopback 473 * address. Follows IPv4 practice - see in_ifinit(). 474 */ 475 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 476 ifra.ifra_dstaddr.sin6_family = AF_INET6; 477 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback; 478 479 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 480 ifra.ifra_addr.sin6_family = AF_INET6; 481 ifra.ifra_addr.sin6_addr = in6addr_loopback; 482 483 /* the loopback address should NEVER expire. */ 484 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 485 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 486 487 /* we don't need to perform DAD on loopback interfaces. */ 488 ifra.ifra_flags |= IN6_IFF_NODAD; 489 490 /* 491 * We are sure that this is a newly assigned address, so we can set 492 * NULL to the 3rd arg. 493 */ 494 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) { 495 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure " 496 "the loopback address on %s (errno=%d)\n", 497 ifp->if_xname, error)); 498 return (-1); 499 } 500 501 return 0; 502} 503 504/* 505 * compute NI group address, based on the current hostname setting. 506 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later). 507 * 508 * when ifp == NULL, the caller is responsible for filling scopeid. 509 */ 510int 511in6_nigroup(ifp, name, namelen, sa6) 512 struct ifnet *ifp; 513 const char *name; 514 int namelen; 515 struct sockaddr_in6 *sa6; 516{ 517 const char *p; 518 u_int8_t *q; 519 MD5_CTX ctxt; 520 u_int8_t digest[16]; 521 u_int8_t l; 522 u_int8_t n[64]; /* a single label must not exceed 63 chars */ 523 524 if (!namelen || !name) 525 return -1; 526 527 p = name; 528 while (p && *p && *p != '.' && p - name < namelen) 529 p++; 530 if (p - name > sizeof(n) - 1) 531 return -1; /* label too long */ 532 l = p - name; 533 strncpy((char *)n, name, l); 534 n[(int)l] = '\0'; 535 for (q = n; *q; q++) { 536 if ('A' <= *q && *q <= 'Z') 537 *q = *q - 'A' + 'a'; 538 } 539 540 /* generate 8 bytes of pseudo-random value. */ 541 bzero(&ctxt, sizeof(ctxt)); 542 MD5Init(&ctxt); 543 MD5Update(&ctxt, &l, sizeof(l)); 544 MD5Update(&ctxt, n, l); 545 MD5Final(digest, &ctxt); 546 547 bzero(sa6, sizeof(*sa6)); 548 sa6->sin6_family = AF_INET6; 549 sa6->sin6_len = sizeof(*sa6); 550 sa6->sin6_addr.s6_addr16[0] = htons(0xff02); 551 sa6->sin6_addr.s6_addr16[1] = htons(ifp->if_index); 552 sa6->sin6_addr.s6_addr8[11] = 2; 553 bcopy(digest, &sa6->sin6_addr.s6_addr32[3], 554 sizeof(sa6->sin6_addr.s6_addr32[3])); 555 556 return 0; 557} 558 559/* 560 * XXX multiple loopback interface needs more care. for instance, 561 * nodelocal address needs to be configured onto only one of them. 562 * XXX multiple link-local address case 563 */ 564void 565in6_ifattach(ifp, altifp) 566 struct ifnet *ifp; 567 struct ifnet *altifp; /* secondary EUI64 source */ 568{ 569 struct in6_ifaddr *ia; 570 struct in6_addr in6; 571 572 /* some of the interfaces are inherently not IPv6 capable */ 573 switch (ifp->if_type) { 574 case IFT_BRIDGE: 575 case IFT_ENC: 576 case IFT_PFLOG: 577 return; 578 case IFT_PROPVIRTUAL: 579 if (strncmp("bridge", ifp->if_xname, sizeof("bridge")) == 0 && 580 '0' <= ifp->if_xname[sizeof("bridge")] && 581 ifp->if_xname[sizeof("bridge")] <= '9') 582 return; 583 break; 584 } 585 586 /* 587 * if link mtu is too small, don't try to configure IPv6. 588 * remember there could be some link-layer that has special 589 * fragmentation logic. 590 */ 591 if (ifp->if_mtu < IPV6_MMTU) { 592 nd6log((LOG_INFO, "in6_ifattach: " 593 "%s has too small MTU, IPv6 not enabled\n", 594 ifp->if_xname)); 595 return; 596 } 597 598 /* create a multicast kludge storage (if we have not had one) */ 599 in6_createmkludge(ifp); 600 601 /* 602 * quirks based on interface type 603 */ 604 switch (ifp->if_type) { 605 default: 606 break; 607 } 608 609 /* 610 * usually, we require multicast capability to the interface 611 */ 612 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 613 nd6log((LOG_INFO, "in6_ifattach: " 614 "%s is not multicast capable, IPv6 not enabled\n", 615 ifp->if_xname)); 616 return; 617 } 618 619 /* 620 * assign loopback address for loopback interface. 621 * XXX multiple loopback interface case. 622 */ 623 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 624 in6 = in6addr_loopback; 625 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) { 626 if (in6_ifattach_loopback(ifp) != 0) 627 return; 628 } 629 } 630 631 /* 632 * assign a link-local address, if there's none. 633 */ 634 if (ip6_auto_linklocal) { 635 ia = in6ifa_ifpforlinklocal(ifp, 0); 636 if (ia == NULL) { 637 if (in6_ifattach_linklocal(ifp, altifp) == 0) { 638 /* linklocal address assigned */ 639 } else { 640 /* failed to assign linklocal address. bark? */ 641 } 642 } 643 } 644} 645 646/* 647 * NOTE: in6_ifdetach() does not support loopback if at this moment. 648 */ 649void 650in6_ifdetach(ifp) 651 struct ifnet *ifp; 652{ 653 struct in6_ifaddr *ia, *oia; 654 struct ifaddr *ifa, *next; 655 struct rtentry *rt; 656 short rtflags; 657 struct sockaddr_in6 sin6; 658 struct in6_multi_mship *imm; 659 660 /* remove neighbor management table */ 661 nd6_purge(ifp); 662 663 /* nuke any of IPv6 addresses we have */ 664 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) 665 { 666 next = ifa->ifa_list.tqe_next; 667 if (ifa->ifa_addr->sa_family != AF_INET6) 668 continue; 669 in6_purgeaddr(ifa); 670 } 671 672 /* undo everything done by in6_ifattach(), just in case */ 673 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) 674 { 675 next = ifa->ifa_list.tqe_next; 676 677 if (ifa->ifa_addr->sa_family != AF_INET6 678 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) { 679 continue; 680 } 681 682 ia = (struct in6_ifaddr *)ifa; 683 684 /* 685 * leave from multicast groups we have joined for the interface 686 */ 687 while ((imm = ia->ia6_memberships.lh_first) != NULL) { 688 LIST_REMOVE(imm, i6mm_chain); 689 in6_leavegroup(imm); 690 } 691 692 /* remove from the routing table */ 693 if ((ia->ia_flags & IFA_ROUTE) && 694 (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) { 695 rtflags = rt->rt_flags; 696 rtfree(rt); 697 rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr, 698 (struct sockaddr *)&ia->ia_addr, 699 (struct sockaddr *)&ia->ia_prefixmask, 700 rtflags, (struct rtentry **)0); 701 } 702 703 /* remove from the linked list */ 704 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list); 705 IFAFREE(&ia->ia_ifa); 706 707 /* also remove from the IPv6 address chain(itojun&jinmei) */ 708 oia = ia; 709 if (oia == (ia = in6_ifaddr)) 710 in6_ifaddr = ia->ia_next; 711 else { 712 while (ia->ia_next && (ia->ia_next != oia)) 713 ia = ia->ia_next; 714 if (ia->ia_next) 715 ia->ia_next = oia->ia_next; 716 else { 717 nd6log((LOG_ERR, 718 "%s: didn't unlink in6ifaddr from list\n", 719 ifp->if_xname)); 720 } 721 } 722 723 IFAFREE(&oia->ia_ifa); 724 } 725 726 /* cleanup multicast address kludge table, if there is any */ 727 in6_purgemkludge(ifp); 728 729 /* 730 * remove neighbor management table. we call it twice just to make 731 * sure we nuke everything. maybe we need just one call. 732 * XXX: since the first call did not release addresses, some prefixes 733 * might remain. We should call nd6_purge() again to release the 734 * prefixes after removing all addresses above. 735 * (Or can we just delay calling nd6_purge until at this point?) 736 */ 737 nd6_purge(ifp); 738 739 /* remove route to link-local allnodes multicast (ff02::1) */ 740 bzero(&sin6, sizeof(sin6)); 741 sin6.sin6_len = sizeof(struct sockaddr_in6); 742 sin6.sin6_family = AF_INET6; 743 sin6.sin6_addr = in6addr_linklocal_allnodes; 744 sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 745 rt = rtalloc1((struct sockaddr *)&sin6, 0); 746 if (rt && rt->rt_ifp == ifp) { 747 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt), 748 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); 749 rtfree(rt); 750 } 751} 752