in6_ifattach.c revision 1.64
1/* $OpenBSD: in6_ifattach.c,v 1.64 2013/11/28 10:16:44 mpi 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 41#include <crypto/md5.h> 42 43#include <net/if.h> 44#include <net/if_var.h> 45#include <net/if_dl.h> 46#include <net/if_types.h> 47#include <net/route.h> 48 49#include <netinet/in.h> 50#include <netinet/if_ether.h> 51 52#include <netinet6/in6_var.h> 53#include <netinet/ip6.h> 54#include <netinet6/ip6_var.h> 55#include <netinet6/in6_ifattach.h> 56#include <netinet6/ip6_var.h> 57#include <netinet6/nd6.h> 58#ifdef MROUTING 59#include <netinet6/ip6_mroute.h> 60#endif 61 62#include <dev/rndvar.h> 63 64unsigned long in6_maxmtu = 0; 65 66int ip6_auto_linklocal = 1; /* enable by default */ 67 68int get_last_resort_ifid(struct ifnet *, struct in6_addr *); 69int get_hw_ifid(struct ifnet *, struct in6_addr *); 70int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *); 71int in6_ifattach_loopback(struct ifnet *); 72 73#define EUI64_GBIT 0x01 74#define EUI64_UBIT 0x02 75#define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) 76#define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 77#define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6)) 78#define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT) 79#define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6)) 80 81#define IFID_LOCAL(in6) (!EUI64_LOCAL(in6)) 82#define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6)) 83 84/* 85 * Generate a last-resort interface identifier, when the machine has no 86 * IEEE802/EUI64 address sources. 87 * The goal here is to get an interface identifier that is 88 * (1) random enough and (2) does not change across reboot. 89 * We currently use MD5(hostname) for it. 90 * 91 * in6 - upper 64bits are preserved 92 */ 93int 94get_last_resort_ifid(struct ifnet *ifp, struct in6_addr *in6) 95{ 96 MD5_CTX ctxt; 97 u_int8_t digest[16]; 98 99#if 0 100 /* we need at least several letters as seed for ifid */ 101 if (hostnamelen < 3) 102 return -1; 103#endif 104 105 /* generate 8 bytes of pseudo-random value. */ 106 bzero(&ctxt, sizeof(ctxt)); 107 MD5Init(&ctxt); 108 MD5Update(&ctxt, hostname, hostnamelen); 109 MD5Final(digest, &ctxt); 110 111 /* assumes sizeof(digest) > sizeof(ifid) */ 112 bcopy(digest, &in6->s6_addr[8], 8); 113 114 /* make sure to set "u" bit to local, and "g" bit to individual. */ 115 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 116 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 117 118 /* convert EUI64 into IPv6 interface identifier */ 119 EUI64_TO_IFID(in6); 120 121 return 0; 122} 123 124/* 125 * Generate a random interface identifier. 126 * 127 * in6 - upper 64bits are preserved 128 */ 129void 130in6_get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6) 131{ 132 arc4random_buf(&in6->s6_addr32[2], 8); 133 134 /* make sure to set "u" bit to local, and "g" bit to individual. */ 135 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 136 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 137 138 /* convert EUI64 into IPv6 interface identifier */ 139 EUI64_TO_IFID(in6); 140} 141 142/* 143 * Get interface identifier for the specified interface. 144 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface 145 * 146 * in6 - upper 64bits are preserved 147 */ 148int 149get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6) 150{ 151 struct ifaddr *ifa; 152 struct sockaddr_dl *sdl; 153 char *addr; 154 size_t addrlen; 155 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 156 static u_int8_t allone[8] = 157 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 158 159 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 160 if (ifa->ifa_addr->sa_family != AF_LINK) 161 continue; 162 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 163 if (sdl == NULL) 164 continue; 165 if (sdl->sdl_alen == 0) 166 continue; 167 168 goto found; 169 } 170 171 return -1; 172 173found: 174 addr = LLADDR(sdl); 175 addrlen = sdl->sdl_alen; 176 177 switch (ifp->if_type) { 178 case IFT_IEEE1394: 179 case IFT_IEEE80211: 180 /* IEEE1394 uses 16byte length address starting with EUI64 */ 181 if (addrlen > 8) 182 addrlen = 8; 183 break; 184 default: 185 break; 186 } 187 188 /* get EUI64 */ 189 switch (ifp->if_type) { 190 /* IEEE802/EUI64 cases - what others? */ 191 case IFT_ETHER: 192 case IFT_CARP: 193 case IFT_IEEE1394: 194 case IFT_IEEE80211: 195 /* look at IEEE802/EUI64 only */ 196 if (addrlen != 8 && addrlen != 6) 197 return -1; 198 199 /* 200 * check for invalid MAC address - on bsdi, we see it a lot 201 * since wildboar configures all-zero MAC on pccard before 202 * card insertion. 203 */ 204 if (bcmp(addr, allzero, addrlen) == 0) 205 return -1; 206 if (bcmp(addr, allone, addrlen) == 0) 207 return -1; 208 209 /* make EUI64 address */ 210 if (addrlen == 8) 211 bcopy(addr, &in6->s6_addr[8], 8); 212 else if (addrlen == 6) { 213 in6->s6_addr[8] = addr[0]; 214 in6->s6_addr[9] = addr[1]; 215 in6->s6_addr[10] = addr[2]; 216 in6->s6_addr[11] = 0xff; 217 in6->s6_addr[12] = 0xfe; 218 in6->s6_addr[13] = addr[3]; 219 in6->s6_addr[14] = addr[4]; 220 in6->s6_addr[15] = addr[5]; 221 } 222 break; 223 224 case IFT_GIF: 225 /* 226 * RFC2893 says: "SHOULD use IPv4 address as ifid source". 227 * however, IPv4 address is not very suitable as unique 228 * identifier source (can be renumbered). 229 * we don't do this. 230 */ 231 return -1; 232 233 default: 234 return -1; 235 } 236 237 /* sanity check: g bit must not indicate "group" */ 238 if (EUI64_GROUP(in6)) 239 return -1; 240 241 /* convert EUI64 into IPv6 interface identifier */ 242 EUI64_TO_IFID(in6); 243 244 /* 245 * sanity check: ifid must not be all zero, avoid conflict with 246 * subnet router anycast 247 */ 248 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && 249 bcmp(&in6->s6_addr[9], allzero, 7) == 0) { 250 return -1; 251 } 252 253 return 0; 254} 255 256/* 257 * Get interface identifier for the specified interface. If it is not 258 * available on ifp0, borrow interface identifier from other information 259 * sources. 260 * 261 * altifp - secondary EUI64 source 262 */ 263int 264get_ifid(struct ifnet *ifp0, struct ifnet *altifp, struct in6_addr *in6) 265{ 266 struct ifnet *ifp; 267 268 /* first, try to get it from the interface itself */ 269 if (get_hw_ifid(ifp0, in6) == 0) { 270 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 271 ifp0->if_xname)); 272 goto success; 273 } 274 275 /* try secondary EUI64 source. this basically is for ATM PVC */ 276 if (altifp && get_hw_ifid(altifp, in6) == 0) { 277 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", 278 ifp0->if_xname, altifp->if_xname)); 279 goto success; 280 } 281 282 /* next, try to get it from some other hardware interface */ 283 TAILQ_FOREACH(ifp, &ifnet, if_list) { 284 if (ifp == ifp0) 285 continue; 286 if (get_hw_ifid(ifp, in6) != 0) 287 continue; 288 289 /* 290 * to borrow ifid from other interface, ifid needs to be 291 * globally unique 292 */ 293 if (IFID_UNIVERSAL(in6)) { 294 nd6log((LOG_DEBUG, 295 "%s: borrow interface identifier from %s\n", 296 ifp0->if_xname, ifp->if_xname)); 297 goto success; 298 } 299 } 300 301 /* last resort: get from random number source */ 302 if (get_last_resort_ifid(ifp, in6) == 0) { 303 nd6log((LOG_DEBUG, 304 "%s: interface identifier generated by random number\n", 305 ifp0->if_xname)); 306 goto success; 307 } 308 309 printf("%s: failed to get interface identifier\n", ifp0->if_xname); 310 return -1; 311 312success: 313 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 314 ifp0->if_xname, in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 315 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 316 in6->s6_addr[14], in6->s6_addr[15])); 317 return 0; 318} 319 320/* 321 * altifp - secondary EUI64 source 322 */ 323 324int 325in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp) 326{ 327 struct in6_ifaddr *ia; 328 struct in6_aliasreq ifra; 329 struct nd_prefix pr0; 330 int i, s, error; 331 332 /* 333 * configure link-local address. 334 */ 335 bzero(&ifra, sizeof(ifra)); 336 337 /* 338 * in6_update_ifa() does not use ifra_name, but we accurately set it 339 * for safety. 340 */ 341 strncpy(ifra.ifra_name, ifp->if_xname, sizeof(ifra.ifra_name)); 342 343 ifra.ifra_addr.sin6_family = AF_INET6; 344 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 345 ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80); 346 ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 347 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 348 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 349 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; 350 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); 351 } else { 352 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) { 353 nd6log((LOG_ERR, 354 "%s: no ifid available\n", ifp->if_xname)); 355 return (-1); 356 } 357 } 358 359 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 360 ifra.ifra_prefixmask.sin6_family = AF_INET6; 361 ifra.ifra_prefixmask.sin6_addr = in6mask64; 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. In the case of CARP, we may be called after 376 * one has already been configured, so check if it's already there 377 * with in6ifa_ifpforlinklocal() and clobber it if it exists. 378 */ 379 s = splsoftnet(); 380 error = in6_update_ifa(ifp, &ifra, in6ifa_ifpforlinklocal(ifp, 0)); 381 splx(s); 382 383 if (error != 0) { 384 /* 385 * XXX: When the interface does not support IPv6, this call 386 * would fail in the SIOCSIFADDR ioctl. I believe the 387 * notification is rather confusing in this case, so just 388 * suppress it. (jinmei@kame.net 20010130) 389 */ 390 if (error != EAFNOSUPPORT) 391 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to " 392 "configure a link-local address on %s " 393 "(errno=%d)\n", 394 ifp->if_xname, error)); 395 return (-1); 396 } 397 398 /* 399 * Adjust ia6_flags so that in6_if_up will perform DAD. 400 * XXX: Some P2P interfaces seem not to send packets just after 401 * becoming up, so we skip p2p interfaces for safety. 402 */ 403 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */ 404#ifdef DIAGNOSTIC 405 if (!ia) { 406 panic("ia == NULL in in6_ifattach_linklocal"); 407 /* NOTREACHED */ 408 } 409#endif 410 if (in6if_do_dad(ifp) && ((ifp->if_flags & IFF_POINTOPOINT) || 411 (ifp->if_type == IFT_CARP)) == 0) { 412 ia->ia6_flags &= ~IN6_IFF_NODAD; 413 ia->ia6_flags |= IN6_IFF_TENTATIVE; 414 } 415 416 /* 417 * Make the link-local prefix (fe80::/64%link) as on-link. 418 * Since we'd like to manage prefixes separately from addresses, 419 * we make an ND6 prefix structure for the link-local prefix, 420 * and add it to the prefix list as a never-expire prefix. 421 * XXX: this change might affect some existing code base... 422 */ 423 bzero(&pr0, sizeof(pr0)); 424 pr0.ndpr_ifp = ifp; 425 /* this should be 64 at this moment. */ 426 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); 427 pr0.ndpr_mask = ifra.ifra_prefixmask.sin6_addr; 428 pr0.ndpr_prefix = ifra.ifra_addr; 429 /* apply the mask for safety. (nd6_prelist_add will apply it again) */ 430 for (i = 0; i < 4; i++) { 431 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 432 in6mask64.s6_addr32[i]; 433 } 434 /* 435 * Initialize parameters. The link-local prefix must always be 436 * on-link, and its lifetimes never expire. 437 */ 438 pr0.ndpr_raf_onlink = 1; 439 pr0.ndpr_raf_auto = 1; /* probably meaningless */ 440 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME; 441 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME; 442 /* 443 * Since there is no other link-local addresses, nd6_prefix_lookup() 444 * probably returns NULL. However, we cannot always expect the result. 445 * For example, if we first remove the (only) existing link-local 446 * address, and then reconfigure another one, the prefix is still 447 * valid with referring to the old link-local address. 448 */ 449 if (nd6_prefix_lookup(&pr0) == NULL) { 450 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0) 451 return (error); 452 } 453 454 return 0; 455} 456 457/* 458 * ifp - must be IFT_LOOP 459 */ 460 461int 462in6_ifattach_loopback(struct ifnet *ifp) 463{ 464 struct in6_aliasreq ifra; 465 int error; 466 467 bzero(&ifra, sizeof(ifra)); 468 469 /* 470 * in6_update_ifa() does not use ifra_name, but we accurately set it 471 * for safety. 472 */ 473 strncpy(ifra.ifra_name, ifp->if_xname, sizeof(ifra.ifra_name)); 474 475 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 476 ifra.ifra_prefixmask.sin6_family = AF_INET6; 477 ifra.ifra_prefixmask.sin6_addr = in6mask128; 478 479 /* 480 * Always initialize ia_dstaddr (= broadcast address) to loopback 481 * address. Follows IPv4 practice - see in_ifinit(). 482 */ 483 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 484 ifra.ifra_dstaddr.sin6_family = AF_INET6; 485 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback; 486 487 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 488 ifra.ifra_addr.sin6_family = AF_INET6; 489 ifra.ifra_addr.sin6_addr = in6addr_loopback; 490 491 /* the loopback address should NEVER expire. */ 492 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 493 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 494 495 /* we don't need to perform DAD on loopback interfaces. */ 496 ifra.ifra_flags |= IN6_IFF_NODAD; 497 498 /* 499 * We are sure that this is a newly assigned address, so we can set 500 * NULL to the 3rd arg. 501 */ 502 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) { 503 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure " 504 "the loopback address on %s (errno=%d)\n", 505 ifp->if_xname, error)); 506 return (-1); 507 } 508 509 return 0; 510} 511 512/* 513 * compute NI group address, based on the current hostname setting. 514 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later). 515 * 516 * when ifp == NULL, the caller is responsible for filling scopeid. 517 */ 518int 519in6_nigroup(struct ifnet *ifp, const char *name, int namelen, 520 struct sockaddr_in6 *sa6) 521{ 522 const char *p; 523 u_int8_t *q; 524 MD5_CTX ctxt; 525 u_int8_t digest[16]; 526 u_int8_t l; 527 u_int8_t n[64]; /* a single label must not exceed 63 chars */ 528 529 if (!namelen || !name) 530 return -1; 531 532 p = name; 533 while (p && *p && *p != '.' && p - name < namelen) 534 p++; 535 if (p - name > sizeof(n) - 1) 536 return -1; /* label too long */ 537 l = p - name; 538 strncpy((char *)n, name, l); 539 n[(int)l] = '\0'; 540 for (q = n; *q; q++) { 541 if ('A' <= *q && *q <= 'Z') 542 *q = *q - 'A' + 'a'; 543 } 544 545 /* generate 8 bytes of pseudo-random value. */ 546 bzero(&ctxt, sizeof(ctxt)); 547 MD5Init(&ctxt); 548 MD5Update(&ctxt, &l, sizeof(l)); 549 MD5Update(&ctxt, n, l); 550 MD5Final(digest, &ctxt); 551 552 bzero(sa6, sizeof(*sa6)); 553 sa6->sin6_family = AF_INET6; 554 sa6->sin6_len = sizeof(*sa6); 555 sa6->sin6_addr.s6_addr16[0] = htons(0xff02); 556 sa6->sin6_addr.s6_addr16[1] = htons(ifp->if_index); 557 sa6->sin6_addr.s6_addr8[11] = 2; 558 bcopy(digest, &sa6->sin6_addr.s6_addr32[3], 559 sizeof(sa6->sin6_addr.s6_addr32[3])); 560 561 return 0; 562} 563 564/* 565 * XXX multiple loopback interface needs more care. for instance, 566 * nodelocal address needs to be configured onto only one of them. 567 * XXX multiple link-local address case 568 * 569 * altifp - secondary EUI64 source 570 */ 571void 572in6_ifattach(struct ifnet *ifp, struct ifnet *altifp) 573{ 574 struct in6_ifaddr *ia; 575 struct in6_addr in6; 576 577 /* some of the interfaces are inherently not IPv6 capable */ 578 switch (ifp->if_type) { 579 case IFT_BRIDGE: 580 case IFT_ENC: 581 case IFT_PFLOG: 582 case IFT_PFSYNC: 583 return; 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 /* 599 * quirks based on interface type 600 */ 601 switch (ifp->if_type) { 602 /* we attach a link-local address when a vhid is assigned */ 603 case IFT_CARP: 604 return; 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(struct ifnet *ifp) 651{ 652 struct ifaddr *ifa, *next; 653 struct ifmaddr *ifma, *mnext; 654 struct rtentry *rt; 655 struct sockaddr_in6 sin6; 656 657#ifdef MROUTING 658 /* remove ip6_mrouter stuff */ 659 ip6_mrouter_detach(ifp); 660#endif 661 662 /* remove neighbor management table */ 663 nd6_purge(ifp); 664 665 /* nuke any of IPv6 addresses we have */ 666 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrlist, ifa_list, next) { 667 if (ifa->ifa_addr->sa_family != AF_INET6) 668 continue; 669 in6_purgeaddr(ifa); 670 } 671 672 673 TAILQ_FOREACH_SAFE(ifma, &ifp->if_maddrlist, ifma_list, mnext) { 674 if (ifma->ifma_addr->sa_family != AF_INET6) 675 continue; 676 677 ifma->ifma_refcnt = 1; 678 in6_delmulti(ifmatoin6m(ifma)); 679 } 680 681 /* 682 * remove neighbor management table. we call it twice just to make 683 * sure we nuke everything. maybe we need just one call. 684 * XXX: since the first call did not release addresses, some prefixes 685 * might remain. We should call nd6_purge() again to release the 686 * prefixes after removing all addresses above. 687 * (Or can we just delay calling nd6_purge until at this point?) 688 */ 689 nd6_purge(ifp); 690 691 /* remove route to link-local allnodes multicast (ff02::1) */ 692 bzero(&sin6, sizeof(sin6)); 693 sin6.sin6_len = sizeof(struct sockaddr_in6); 694 sin6.sin6_family = AF_INET6; 695 sin6.sin6_addr = in6addr_linklocal_allnodes; 696 sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 697 rt = rtalloc1(sin6tosa(&sin6), 0, ifp->if_rdomain); 698 if (rt && rt->rt_ifp == ifp) { 699 struct rt_addrinfo info; 700 701 bzero(&info, sizeof(info)); 702 info.rti_flags = rt->rt_flags; 703 info.rti_info[RTAX_DST] = rt_key(rt); 704 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 705 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 706 rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL, 707 ifp->if_rdomain); 708 rtfree(rt); 709 } 710} 711