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