in6_ifattach.c revision 1.69
1/* $OpenBSD: in6_ifattach.c,v 1.69 2014/05/20 10:29:01 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 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 * 145 * in6 - upper 64bits are preserved 146 */ 147int 148get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6) 149{ 150 struct sockaddr_dl *sdl; 151 char *addr; 152 size_t addrlen; 153 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 154 static u_int8_t allone[8] = 155 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 156 157 sdl = (struct sockaddr_dl *)ifp->if_sadl; 158 if (sdl == NULL || sdl->sdl_alen == 0) 159 return -1; 160 161 addr = LLADDR(sdl); 162 addrlen = sdl->sdl_alen; 163 164 switch (ifp->if_type) { 165 case IFT_IEEE1394: 166 case IFT_IEEE80211: 167 /* IEEE1394 uses 16byte length address starting with EUI64 */ 168 if (addrlen > 8) 169 addrlen = 8; 170 break; 171 default: 172 break; 173 } 174 175 /* get EUI64 */ 176 switch (ifp->if_type) { 177 /* IEEE802/EUI64 cases - what others? */ 178 case IFT_ETHER: 179 case IFT_CARP: 180 case IFT_IEEE1394: 181 case IFT_IEEE80211: 182 /* look at IEEE802/EUI64 only */ 183 if (addrlen != 8 && addrlen != 6) 184 return -1; 185 186 /* 187 * check for invalid MAC address - on bsdi, we see it a lot 188 * since wildboar configures all-zero MAC on pccard before 189 * card insertion. 190 */ 191 if (bcmp(addr, allzero, addrlen) == 0) 192 return -1; 193 if (bcmp(addr, allone, addrlen) == 0) 194 return -1; 195 196 /* make EUI64 address */ 197 if (addrlen == 8) 198 bcopy(addr, &in6->s6_addr[8], 8); 199 else if (addrlen == 6) { 200 in6->s6_addr[8] = addr[0]; 201 in6->s6_addr[9] = addr[1]; 202 in6->s6_addr[10] = addr[2]; 203 in6->s6_addr[11] = 0xff; 204 in6->s6_addr[12] = 0xfe; 205 in6->s6_addr[13] = addr[3]; 206 in6->s6_addr[14] = addr[4]; 207 in6->s6_addr[15] = addr[5]; 208 } 209 break; 210 211 case IFT_GIF: 212 /* 213 * RFC2893 says: "SHOULD use IPv4 address as ifid source". 214 * however, IPv4 address is not very suitable as unique 215 * identifier source (can be renumbered). 216 * we don't do this. 217 */ 218 return -1; 219 220 default: 221 return -1; 222 } 223 224 /* sanity check: g bit must not indicate "group" */ 225 if (EUI64_GROUP(in6)) 226 return -1; 227 228 /* convert EUI64 into IPv6 interface identifier */ 229 EUI64_TO_IFID(in6); 230 231 /* 232 * sanity check: ifid must not be all zero, avoid conflict with 233 * subnet router anycast 234 */ 235 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && 236 bcmp(&in6->s6_addr[9], allzero, 7) == 0) { 237 return -1; 238 } 239 240 return 0; 241} 242 243/* 244 * Get interface identifier for the specified interface. If it is not 245 * available on ifp0, borrow interface identifier from other information 246 * sources. 247 */ 248int 249get_ifid(struct ifnet *ifp0, struct in6_addr *in6) 250{ 251 struct ifnet *ifp; 252 253 /* first, try to get it from the interface itself */ 254 if (get_hw_ifid(ifp0, in6) == 0) { 255 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 256 ifp0->if_xname)); 257 goto success; 258 } 259 260 /* next, try to get it from some other hardware interface */ 261 TAILQ_FOREACH(ifp, &ifnet, if_list) { 262 if (ifp == ifp0) 263 continue; 264 if (get_hw_ifid(ifp, in6) != 0) 265 continue; 266 267 /* 268 * to borrow ifid from other interface, ifid needs to be 269 * globally unique 270 */ 271 if (IFID_UNIVERSAL(in6)) { 272 nd6log((LOG_DEBUG, 273 "%s: borrow interface identifier from %s\n", 274 ifp0->if_xname, ifp->if_xname)); 275 goto success; 276 } 277 } 278 279 /* last resort: get from random number source */ 280 if (get_last_resort_ifid(ifp, in6) == 0) { 281 nd6log((LOG_DEBUG, 282 "%s: interface identifier generated by random number\n", 283 ifp0->if_xname)); 284 goto success; 285 } 286 287 printf("%s: failed to get interface identifier\n", ifp0->if_xname); 288 return -1; 289 290success: 291 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 292 ifp0->if_xname, in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 293 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 294 in6->s6_addr[14], in6->s6_addr[15])); 295 return 0; 296} 297 298/* 299 * ifid - used as EUI64 if not NULL, overrides other EUI64 sources 300 */ 301 302int 303in6_ifattach_linklocal(struct ifnet *ifp, struct in6_addr *ifid) 304{ 305 struct in6_ifaddr *ia6; 306 struct in6_aliasreq ifra; 307 struct nd_prefix pr0; 308 int i, s, error; 309 310 /* 311 * configure link-local address. 312 */ 313 bzero(&ifra, sizeof(ifra)); 314 315 /* 316 * in6_update_ifa() does not use ifra_name, but we accurately set it 317 * for safety. 318 */ 319 strncpy(ifra.ifra_name, ifp->if_xname, sizeof(ifra.ifra_name)); 320 321 ifra.ifra_addr.sin6_family = AF_INET6; 322 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 323 ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80); 324 ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 325 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 326 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 327 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; 328 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); 329 } else if (ifid) { 330 ifra.ifra_addr.sin6_addr = *ifid; 331 ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80); 332 ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 333 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 334 ifra.ifra_addr.sin6_addr.s6_addr[8] &= ~EUI64_GBIT; 335 ifra.ifra_addr.sin6_addr.s6_addr[8] |= EUI64_UBIT; 336 } else { 337 if (get_ifid(ifp, &ifra.ifra_addr.sin6_addr) != 0) { 338 nd6log((LOG_ERR, 339 "%s: no ifid available\n", ifp->if_xname)); 340 return (-1); 341 } 342 } 343 344 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 345 ifra.ifra_prefixmask.sin6_family = AF_INET6; 346 ifra.ifra_prefixmask.sin6_addr = in6mask64; 347 /* link-local addresses should NEVER expire. */ 348 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 349 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 350 351 /* 352 * Do not let in6_update_ifa() do DAD, since we need a random delay 353 * before sending an NS at the first time the interface becomes up. 354 * Instead, in6_if_up() will start DAD with a proper random delay. 355 */ 356 ifra.ifra_flags |= IN6_IFF_NODAD; 357 358 /* 359 * Now call in6_update_ifa() to do a bunch of procedures to configure 360 * a link-local address. In the case of CARP, we may be called after 361 * one has already been configured, so check if it's already there 362 * with in6ifa_ifpforlinklocal() and clobber it if it exists. 363 */ 364 s = splsoftnet(); 365 error = in6_update_ifa(ifp, &ifra, in6ifa_ifpforlinklocal(ifp, 0)); 366 splx(s); 367 368 if (error != 0) { 369 /* 370 * XXX: When the interface does not support IPv6, this call 371 * would fail in the SIOCSIFADDR ioctl. I believe the 372 * notification is rather confusing in this case, so just 373 * suppress it. (jinmei@kame.net 20010130) 374 */ 375 if (error != EAFNOSUPPORT) 376 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to " 377 "configure a link-local address on %s " 378 "(errno=%d)\n", 379 ifp->if_xname, error)); 380 return (-1); 381 } 382 383 /* 384 * Adjust ia6_flags so that in6_if_up will perform DAD. 385 * XXX: Some P2P interfaces seem not to send packets just after 386 * becoming up, so we skip p2p interfaces for safety. 387 */ 388 ia6 = in6ifa_ifpforlinklocal(ifp, 0); /* ia6 must not be NULL */ 389#ifdef DIAGNOSTIC 390 if (!ia6) { 391 panic("ia6 == NULL in in6_ifattach_linklocal"); 392 /* NOTREACHED */ 393 } 394#endif 395 if (in6if_do_dad(ifp) && ((ifp->if_flags & IFF_POINTOPOINT) || 396 (ifp->if_type == IFT_CARP)) == 0) { 397 ia6->ia6_flags &= ~IN6_IFF_NODAD; 398 ia6->ia6_flags |= IN6_IFF_TENTATIVE; 399 } 400 401 /* 402 * Make the link-local prefix (fe80::/64%link) as on-link. 403 * Since we'd like to manage prefixes separately from addresses, 404 * we make an ND6 prefix structure for the link-local prefix, 405 * and add it to the prefix list as a never-expire prefix. 406 * XXX: this change might affect some existing code base... 407 */ 408 bzero(&pr0, sizeof(pr0)); 409 pr0.ndpr_ifp = ifp; 410 /* this should be 64 at this moment. */ 411 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); 412 pr0.ndpr_mask = ifra.ifra_prefixmask.sin6_addr; 413 pr0.ndpr_prefix = ifra.ifra_addr; 414 /* apply the mask for safety. (nd6_prelist_add will apply it again) */ 415 for (i = 0; i < 4; i++) { 416 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 417 in6mask64.s6_addr32[i]; 418 } 419 /* 420 * Initialize parameters. The link-local prefix must always be 421 * on-link, and its lifetimes never expire. 422 */ 423 pr0.ndpr_raf_onlink = 1; 424 pr0.ndpr_raf_auto = 1; /* probably meaningless */ 425 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME; 426 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME; 427 /* 428 * Since there is no other link-local addresses, nd6_prefix_lookup() 429 * probably returns NULL. However, we cannot always expect the result. 430 * For example, if we first remove the (only) existing link-local 431 * address, and then reconfigure another one, the prefix is still 432 * valid with referring to the old link-local address. 433 */ 434 if (nd6_prefix_lookup(&pr0) == NULL) { 435 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0) 436 return (error); 437 } 438 439 return 0; 440} 441 442/* 443 * ifp - must be IFT_LOOP 444 */ 445 446int 447in6_ifattach_loopback(struct ifnet *ifp) 448{ 449 struct in6_aliasreq ifra; 450 int error; 451 452 bzero(&ifra, sizeof(ifra)); 453 454 /* 455 * in6_update_ifa() does not use ifra_name, but we accurately set it 456 * for safety. 457 */ 458 strncpy(ifra.ifra_name, ifp->if_xname, sizeof(ifra.ifra_name)); 459 460 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 461 ifra.ifra_prefixmask.sin6_family = AF_INET6; 462 ifra.ifra_prefixmask.sin6_addr = in6mask128; 463 464 /* 465 * Always initialize ia_dstaddr (= broadcast address) to loopback 466 * address. Follows IPv4 practice - see in_ifinit(). 467 */ 468 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 469 ifra.ifra_dstaddr.sin6_family = AF_INET6; 470 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback; 471 472 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 473 ifra.ifra_addr.sin6_family = AF_INET6; 474 ifra.ifra_addr.sin6_addr = in6addr_loopback; 475 476 /* the loopback address should NEVER expire. */ 477 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 478 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 479 480 /* we don't need to perform DAD on loopback interfaces. */ 481 ifra.ifra_flags |= IN6_IFF_NODAD; 482 483 /* 484 * We are sure that this is a newly assigned address, so we can set 485 * NULL to the 3rd arg. 486 */ 487 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) { 488 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure " 489 "the loopback address on %s (errno=%d)\n", 490 ifp->if_xname, error)); 491 return (-1); 492 } 493 494 return 0; 495} 496 497/* 498 * compute NI group address, based on the current hostname setting. 499 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later). 500 * 501 * when ifp == NULL, the caller is responsible for filling scopeid. 502 */ 503int 504in6_nigroup(struct ifnet *ifp, const char *name, int namelen, 505 struct sockaddr_in6 *sa6) 506{ 507 const char *p; 508 u_int8_t *q; 509 MD5_CTX ctxt; 510 u_int8_t digest[16]; 511 u_int8_t l; 512 u_int8_t n[64]; /* a single label must not exceed 63 chars */ 513 514 if (!namelen || !name) 515 return -1; 516 517 p = name; 518 while (p && *p && *p != '.' && p - name < namelen) 519 p++; 520 if (p - name > sizeof(n) - 1) 521 return -1; /* label too long */ 522 l = p - name; 523 strncpy((char *)n, name, l); 524 n[(int)l] = '\0'; 525 for (q = n; *q; q++) { 526 if ('A' <= *q && *q <= 'Z') 527 *q = *q - 'A' + 'a'; 528 } 529 530 /* generate 8 bytes of pseudo-random value. */ 531 bzero(&ctxt, sizeof(ctxt)); 532 MD5Init(&ctxt); 533 MD5Update(&ctxt, &l, sizeof(l)); 534 MD5Update(&ctxt, n, l); 535 MD5Final(digest, &ctxt); 536 537 bzero(sa6, sizeof(*sa6)); 538 sa6->sin6_family = AF_INET6; 539 sa6->sin6_len = sizeof(*sa6); 540 sa6->sin6_addr.s6_addr16[0] = htons(0xff02); 541 sa6->sin6_addr.s6_addr16[1] = htons(ifp->if_index); 542 sa6->sin6_addr.s6_addr8[11] = 2; 543 bcopy(digest, &sa6->sin6_addr.s6_addr32[3], 544 sizeof(sa6->sin6_addr.s6_addr32[3])); 545 546 return 0; 547} 548 549/* 550 * XXX multiple loopback interface needs more care. for instance, 551 * nodelocal address needs to be configured onto only one of them. 552 * XXX multiple link-local address case 553 */ 554void 555in6_ifattach(struct ifnet *ifp) 556{ 557 struct in6_ifaddr *ia6; 558 struct in6_addr in6; 559 560 /* some of the interfaces are inherently not IPv6 capable */ 561 switch (ifp->if_type) { 562 case IFT_BRIDGE: 563 case IFT_ENC: 564 case IFT_PFLOG: 565 case IFT_PFSYNC: 566 return; 567 } 568 569 /* 570 * if link mtu is too small, don't try to configure IPv6. 571 * remember there could be some link-layer that has special 572 * fragmentation logic. 573 */ 574 if (ifp->if_mtu < IPV6_MMTU) { 575 nd6log((LOG_INFO, "in6_ifattach: " 576 "%s has too small MTU, IPv6 not enabled\n", 577 ifp->if_xname)); 578 return; 579 } 580 581 /* 582 * quirks based on interface type 583 */ 584 switch (ifp->if_type) { 585 /* we attach a link-local address when a vhid is assigned */ 586 case IFT_CARP: 587 return; 588 default: 589 break; 590 } 591 592 /* 593 * usually, we require multicast capability to the interface 594 */ 595 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 596 nd6log((LOG_INFO, "in6_ifattach: " 597 "%s is not multicast capable, IPv6 not enabled\n", 598 ifp->if_xname)); 599 return; 600 } 601 602 /* 603 * assign loopback address for loopback interface. 604 * XXX multiple loopback interface case. 605 */ 606 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 607 in6 = in6addr_loopback; 608 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) { 609 if (in6_ifattach_loopback(ifp) != 0) 610 return; 611 } 612 } 613 614 /* 615 * assign a link-local address, if there's none. 616 */ 617 if (ip6_auto_linklocal) { 618 ia6 = in6ifa_ifpforlinklocal(ifp, 0); 619 if (ia6 == NULL) { 620 if (in6_ifattach_linklocal(ifp, NULL) == 0) { 621 /* linklocal address assigned */ 622 } else { 623 /* failed to assign linklocal address. bark? */ 624 } 625 } 626 } 627} 628 629/* 630 * NOTE: in6_ifdetach() does not support loopback if at this moment. 631 */ 632void 633in6_ifdetach(struct ifnet *ifp) 634{ 635 struct ifaddr *ifa, *next; 636 struct rtentry *rt; 637 struct sockaddr_in6 sin6; 638 639#ifdef MROUTING 640 /* remove ip6_mrouter stuff */ 641 ip6_mrouter_detach(ifp); 642#endif 643 644 /* remove neighbor management table */ 645 nd6_purge(ifp); 646 647 /* nuke any of IPv6 addresses we have */ 648 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrlist, ifa_list, next) { 649 if (ifa->ifa_addr->sa_family != AF_INET6) 650 continue; 651 in6_purgeaddr(ifa); 652 } 653 654 /* 655 * remove neighbor management table. we call it twice just to make 656 * sure we nuke everything. maybe we need just one call. 657 * XXX: since the first call did not release addresses, some prefixes 658 * might remain. We should call nd6_purge() again to release the 659 * prefixes after removing all addresses above. 660 * (Or can we just delay calling nd6_purge until at this point?) 661 */ 662 nd6_purge(ifp); 663 664 /* remove route to interface local allnodes multicast (ff01::1) */ 665 bzero(&sin6, sizeof(sin6)); 666 sin6.sin6_len = sizeof(struct sockaddr_in6); 667 sin6.sin6_family = AF_INET6; 668 sin6.sin6_addr = in6addr_intfacelocal_allnodes; 669 sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 670 rt = rtalloc1(sin6tosa(&sin6), 0, ifp->if_rdomain); 671 if (rt && rt->rt_ifp == ifp) { 672 struct rt_addrinfo info; 673 674 bzero(&info, sizeof(info)); 675 info.rti_flags = rt->rt_flags; 676 info.rti_info[RTAX_DST] = rt_key(rt); 677 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 678 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 679 rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL, 680 ifp->if_rdomain); 681 rtfree(rt); 682 } 683 684 /* remove route to link-local allnodes multicast (ff02::1) */ 685 bzero(&sin6, sizeof(sin6)); 686 sin6.sin6_len = sizeof(struct sockaddr_in6); 687 sin6.sin6_family = AF_INET6; 688 sin6.sin6_addr = in6addr_linklocal_allnodes; 689 sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 690 rt = rtalloc1(sin6tosa(&sin6), 0, ifp->if_rdomain); 691 if (rt && rt->rt_ifp == ifp) { 692 struct rt_addrinfo info; 693 694 bzero(&info, sizeof(info)); 695 info.rti_flags = rt->rt_flags; 696 info.rti_info[RTAX_DST] = rt_key(rt); 697 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 698 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 699 rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL, 700 ifp->if_rdomain); 701 rtfree(rt); 702 } 703} 704