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