2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/sockio.h>
41 #include <sys/kernel.h>
43 #include <sys/syslog.h>
47 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/if_ether.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/udp.h>
58 #include <netinet/udp_var.h>
60 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet6/in6_var.h>
63 #include <netinet6/in6_pcb.h>
64 #include <netinet6/in6_ifattach.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/nd6.h>
67 #include <netinet6/mld6_var.h>
68 #include <netinet6/scope6_var.h>
70 VNET_DEFINE(unsigned long, in6_maxmtu);
71 VNET_DEFINE(int, ip6_auto_linklocal);
72 VNET_DEFINE(struct callout, in6_tmpaddrtimer_ch);
74 #define V_in6_tmpaddrtimer_ch VNET(in6_tmpaddrtimer_ch)
76 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
77 #define V_ripcbinfo VNET(ripcbinfo)
79 static int get_rand_ifid(struct ifnet *, struct in6_addr *);
80 static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
81 static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
82 static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
83 static int in6_ifattach_loopback(struct ifnet *);
84 static void in6_purgemaddrs(struct ifnet *);
86 #define EUI64_GBIT 0x01
87 #define EUI64_UBIT 0x02
88 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
89 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
90 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
91 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
92 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
94 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
95 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
98 * Generate a last-resort interface identifier, when the machine has no
99 * IEEE802/EUI64 address sources.
100 * The goal here is to get an interface identifier that is
101 * (1) random enough and (2) does not change across reboot.
102 * We currently use MD5(hostname) for it.
104 * in6 - upper 64bits are preserved
107 get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
114 pr = curthread->td_ucred->cr_prison;
115 mtx_lock(&pr->pr_mtx);
116 hostnamelen = strlen(pr->pr_hostname);
118 /* we need at least several letters as seed for ifid */
119 if (hostnamelen < 3) {
120 mtx_unlock(&pr->pr_mtx);
125 /* generate 8 bytes of pseudo-random value. */
126 bzero(&ctxt, sizeof(ctxt));
128 MD5Update(&ctxt, pr->pr_hostname, hostnamelen);
129 mtx_unlock(&pr->pr_mtx);
130 MD5Final(digest, &ctxt);
132 /* assumes sizeof(digest) > sizeof(ifid) */
133 bcopy(digest, &in6->s6_addr[8], 8);
135 /* make sure to set "u" bit to local, and "g" bit to individual. */
136 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
137 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
139 /* convert EUI64 into IPv6 interface identifier */
146 generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
149 u_int8_t seed[16], digest[16], nullbuf[8];
152 /* If there's no history, start with a random seed. */
153 bzero(nullbuf, sizeof(nullbuf));
154 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
157 for (i = 0; i < 2; i++) {
158 val32 = arc4random();
159 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
162 bcopy(seed0, seed, 8);
164 /* copy the right-most 64-bits of the given address */
165 /* XXX assumption on the size of IFID */
166 bcopy(seed1, &seed[8], 8);
168 if (0) { /* for debugging purposes only */
171 printf("generate_tmp_ifid: new randomized ID from: ");
172 for (i = 0; i < 16; i++)
173 printf("%02x", seed[i]);
177 /* generate 16 bytes of pseudo-random value. */
178 bzero(&ctxt, sizeof(ctxt));
180 MD5Update(&ctxt, seed, sizeof(seed));
181 MD5Final(digest, &ctxt);
184 * RFC 3041 3.2.1. (3)
185 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
186 * left-most bit is numbered 0) to zero.
188 bcopy(digest, ret, 8);
189 ret[0] &= ~EUI64_UBIT;
192 * XXX: we'd like to ensure that the generated value is not zero
193 * for simplicity. If the caclculated digest happens to be zero,
194 * use a random non-zero value as the last resort.
196 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
198 "generate_tmp_ifid: computed MD5 value is zero.\n"));
200 val32 = arc4random();
201 val32 = 1 + (val32 % (0xffffffff - 1));
205 * RFC 3041 3.2.1. (4)
206 * Take the rightmost 64-bits of the MD5 digest and save them in
207 * stable storage as the history value to be used in the next
208 * iteration of the algorithm.
210 bcopy(&digest[8], seed0, 8);
212 if (0) { /* for debugging purposes only */
216 for (i = 0; i < 16; i++)
217 printf("%02x", digest[i]);
225 * Get interface identifier for the specified interface.
226 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
228 * in6 - upper 64bits are preserved
231 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
234 struct sockaddr_dl *sdl;
237 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
238 static u_int8_t allone[8] =
239 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
242 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
243 if (ifa->ifa_addr->sa_family != AF_LINK)
245 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
248 if (sdl->sdl_alen == 0)
258 IF_ADDR_LOCK_ASSERT(ifp);
260 addrlen = sdl->sdl_alen;
263 switch (ifp->if_type) {
272 /* IEEE802/EUI64 cases - what others? */
273 /* IEEE1394 uses 16byte length address starting with EUI64 */
277 /* look at IEEE802/EUI64 only */
278 if (addrlen != 8 && addrlen != 6) {
284 * check for invalid MAC address - on bsdi, we see it a lot
285 * since wildboar configures all-zero MAC on pccard before
288 if (bcmp(addr, allzero, addrlen) == 0) {
292 if (bcmp(addr, allone, addrlen) == 0) {
297 /* make EUI64 address */
299 bcopy(addr, &in6->s6_addr[8], 8);
300 else if (addrlen == 6) {
301 in6->s6_addr[8] = addr[0];
302 in6->s6_addr[9] = addr[1];
303 in6->s6_addr[10] = addr[2];
304 in6->s6_addr[11] = 0xff;
305 in6->s6_addr[12] = 0xfe;
306 in6->s6_addr[13] = addr[3];
307 in6->s6_addr[14] = addr[4];
308 in6->s6_addr[15] = addr[5];
322 bzero(&in6->s6_addr[8], 8);
323 in6->s6_addr[15] = addr[0];
326 * due to insufficient bitwidth, we mark it local.
328 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
329 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
337 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
338 * however, IPv4 address is not very suitable as unique
339 * identifier source (can be renumbered).
350 /* sanity check: g bit must not indicate "group" */
351 if (EUI64_GROUP(in6)) {
356 /* convert EUI64 into IPv6 interface identifier */
360 * sanity check: ifid must not be all zero, avoid conflict with
361 * subnet router anycast
363 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
364 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
374 * Get interface identifier for the specified interface. If it is not
375 * available on ifp0, borrow interface identifier from other information
378 * altifp - secondary EUI64 source
381 get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
382 struct in6_addr *in6)
386 /* first, try to get it from the interface itself */
387 if (in6_get_hw_ifid(ifp0, in6) == 0) {
388 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
393 /* try secondary EUI64 source. this basically is for ATM PVC */
394 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
395 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
396 if_name(ifp0), if_name(altifp)));
400 /* next, try to get it from some other hardware interface */
401 IFNET_RLOCK_NOSLEEP();
402 for (ifp = V_ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
405 if (in6_get_hw_ifid(ifp, in6) != 0)
409 * to borrow ifid from other interface, ifid needs to be
412 if (IFID_UNIVERSAL(in6)) {
414 "%s: borrow interface identifier from %s\n",
415 if_name(ifp0), if_name(ifp)));
416 IFNET_RUNLOCK_NOSLEEP();
420 IFNET_RUNLOCK_NOSLEEP();
422 /* last resort: get from random number source */
423 if (get_rand_ifid(ifp, in6) == 0) {
425 "%s: interface identifier generated by random number\n",
430 printf("%s: failed to get interface identifier\n", if_name(ifp0));
434 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
435 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
436 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
437 in6->s6_addr[14], in6->s6_addr[15]));
442 * altifp - secondary EUI64 source
445 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
447 struct in6_ifaddr *ia;
448 struct in6_aliasreq ifra;
449 struct nd_prefixctl pr0;
453 * configure link-local address.
455 bzero(&ifra, sizeof(ifra));
458 * in6_update_ifa() does not use ifra_name, but we accurately set it
461 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
463 ifra.ifra_addr.sin6_family = AF_INET6;
464 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
465 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
466 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
467 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
468 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
469 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
471 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
473 "%s: no ifid available\n", if_name(ifp)));
477 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
480 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
481 ifra.ifra_prefixmask.sin6_family = AF_INET6;
482 ifra.ifra_prefixmask.sin6_addr = in6mask64;
483 /* link-local addresses should NEVER expire. */
484 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
485 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
488 * Now call in6_update_ifa() to do a bunch of procedures to configure
489 * a link-local address. We can set the 3rd argument to NULL, because
490 * we know there's no other link-local address on the interface
491 * and therefore we are adding one (instead of updating one).
493 if ((error = in6_update_ifa(ifp, &ifra, NULL,
494 IN6_IFAUPDATE_DADDELAY)) != 0) {
496 * XXX: When the interface does not support IPv6, this call
497 * would fail in the SIOCSIFADDR ioctl. I believe the
498 * notification is rather confusing in this case, so just
499 * suppress it. (jinmei@kame.net 20010130)
501 if (error != EAFNOSUPPORT)
502 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
503 "configure a link-local address on %s "
505 if_name(ifp), error));
509 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
512 panic("ia == NULL in in6_ifattach_linklocal");
516 ifa_free(&ia->ia_ifa);
519 * Make the link-local prefix (fe80::%link/64) as on-link.
520 * Since we'd like to manage prefixes separately from addresses,
521 * we make an ND6 prefix structure for the link-local prefix,
522 * and add it to the prefix list as a never-expire prefix.
523 * XXX: this change might affect some existing code base...
525 bzero(&pr0, sizeof(pr0));
527 /* this should be 64 at this moment. */
528 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
529 pr0.ndpr_prefix = ifra.ifra_addr;
530 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
531 for (i = 0; i < 4; i++) {
532 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
533 in6mask64.s6_addr32[i];
536 * Initialize parameters. The link-local prefix must always be
537 * on-link, and its lifetimes never expire.
539 pr0.ndpr_raf_onlink = 1;
540 pr0.ndpr_raf_auto = 1; /* probably meaningless */
541 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
542 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
544 * Since there is no other link-local addresses, nd6_prefix_lookup()
545 * probably returns NULL. However, we cannot always expect the result.
546 * For example, if we first remove the (only) existing link-local
547 * address, and then reconfigure another one, the prefix is still
548 * valid with referring to the old link-local address.
550 if (nd6_prefix_lookup(&pr0) == NULL) {
551 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
559 * ifp - must be IFT_LOOP
562 in6_ifattach_loopback(struct ifnet *ifp)
564 struct in6_aliasreq ifra;
567 bzero(&ifra, sizeof(ifra));
570 * in6_update_ifa() does not use ifra_name, but we accurately set it
573 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
575 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
576 ifra.ifra_prefixmask.sin6_family = AF_INET6;
577 ifra.ifra_prefixmask.sin6_addr = in6mask128;
580 * Always initialize ia_dstaddr (= broadcast address) to loopback
581 * address. Follows IPv4 practice - see in_ifinit().
583 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
584 ifra.ifra_dstaddr.sin6_family = AF_INET6;
585 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
587 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
588 ifra.ifra_addr.sin6_family = AF_INET6;
589 ifra.ifra_addr.sin6_addr = in6addr_loopback;
591 /* the loopback address should NEVER expire. */
592 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
593 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
595 /* we don't need to perform DAD on loopback interfaces. */
596 ifra.ifra_flags |= IN6_IFF_NODAD;
598 /* skip registration to the prefix list. XXX should be temporary. */
599 ifra.ifra_flags |= IN6_IFF_NOPFX;
602 * We are sure that this is a newly assigned address, so we can set
603 * NULL to the 3rd arg.
605 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
606 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
607 "the loopback address on %s (errno=%d)\n",
608 if_name(ifp), error));
616 * compute NI group address, based on the current hostname setting.
617 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
619 * when ifp == NULL, the caller is responsible for filling scopeid.
622 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
623 struct in6_addr *in6)
631 char n[64]; /* a single label must not exceed 63 chars */
634 * If no name is given and namelen is -1,
635 * we try to do the hostname lookup ourselves.
637 if (!name && namelen == -1) {
638 pr = curthread->td_ucred->cr_prison;
639 mtx_lock(&pr->pr_mtx);
640 name = pr->pr_hostname;
641 namelen = strlen(name);
644 if (!name || !namelen) {
646 mtx_unlock(&pr->pr_mtx);
651 while (p && *p && *p != '.' && p - name < namelen)
653 if (p == name || p - name > sizeof(n) - 1) {
655 mtx_unlock(&pr->pr_mtx);
656 return -1; /* label too long */
661 mtx_unlock(&pr->pr_mtx);
663 for (q = n; *q; q++) {
664 if ('A' <= *q && *q <= 'Z')
668 /* generate 8 bytes of pseudo-random value. */
669 bzero(&ctxt, sizeof(ctxt));
671 MD5Update(&ctxt, &l, sizeof(l));
672 MD5Update(&ctxt, n, l);
673 MD5Final(digest, &ctxt);
675 bzero(in6, sizeof(*in6));
676 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
677 in6->s6_addr8[11] = 2;
678 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
679 if (in6_setscope(in6, ifp, NULL))
680 return (-1); /* XXX: should not fail */
686 * XXX multiple loopback interface needs more care. for instance,
687 * nodelocal address needs to be configured onto only one of them.
688 * XXX multiple link-local address case
690 * altifp - secondary EUI64 source
693 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
695 struct in6_ifaddr *ia;
698 /* some of the interfaces are inherently not IPv6 capable */
699 switch (ifp->if_type) {
707 * quirks based on interface type
709 switch (ifp->if_type) {
713 * 6to4 interface is a very special kind of beast.
714 * no multicast, no linklocal. RFC2529 specifies how to make
715 * linklocals for 6to4 interface, but there's no use and
716 * it is rather harmful to have one.
725 * usually, we require multicast capability to the interface
727 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
728 nd6log((LOG_INFO, "in6_ifattach: "
729 "%s is not multicast capable, IPv6 not enabled\n",
735 * assign loopback address for loopback interface.
736 * XXX multiple loopback interface case.
738 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
741 in6 = in6addr_loopback;
742 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &in6);
744 if (in6_ifattach_loopback(ifp) != 0)
751 * assign a link-local address, if there's none.
753 if (V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) {
754 ia = in6ifa_ifpforlinklocal(ifp, 0);
756 if (in6_ifattach_linklocal(ifp, altifp) == 0) {
757 /* linklocal address assigned */
759 /* failed to assign linklocal address. bark? */
762 ifa_free(&ia->ia_ifa);
765 #ifdef IFT_STF /* XXX */
769 /* update dynamically. */
770 if (V_in6_maxmtu < ifp->if_mtu)
771 V_in6_maxmtu = ifp->if_mtu;
775 * NOTE: in6_ifdetach() does not support loopback if at this moment.
776 * We don't need this function in bsdi, because interfaces are never removed
777 * from the ifnet list in bsdi.
780 in6_ifdetach(struct ifnet *ifp)
782 struct in6_ifaddr *ia;
783 struct ifaddr *ifa, *next;
784 struct radix_node_head *rnh;
787 struct sockaddr_in6 sin6;
788 struct in6_multi_mship *imm;
790 /* remove neighbor management table */
793 /* nuke any of IPv6 addresses we have */
794 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
795 if (ifa->ifa_addr->sa_family != AF_INET6)
800 /* undo everything done by in6_ifattach(), just in case */
801 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
802 if (ifa->ifa_addr->sa_family != AF_INET6
803 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
807 ia = (struct in6_ifaddr *)ifa;
810 * leave from multicast groups we have joined for the interface
812 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
813 LIST_REMOVE(imm, i6mm_chain);
817 /* remove from the routing table */
818 if ((ia->ia_flags & IFA_ROUTE) &&
819 (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
820 rtflags = rt->rt_flags;
822 rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
823 (struct sockaddr *)&ia->ia_addr,
824 (struct sockaddr *)&ia->ia_prefixmask,
825 rtflags, (struct rtentry **)0);
828 /* remove from the linked list */
830 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
832 ifa_free(ifa); /* if_addrhead */
835 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
836 IN6_IFADDR_WUNLOCK();
840 in6_pcbpurgeif0(&V_udbinfo, ifp);
841 in6_pcbpurgeif0(&V_ripcbinfo, ifp);
842 /* leave from all multicast groups joined */
843 in6_purgemaddrs(ifp);
846 * remove neighbor management table. we call it twice just to make
847 * sure we nuke everything. maybe we need just one call.
848 * XXX: since the first call did not release addresses, some prefixes
849 * might remain. We should call nd6_purge() again to release the
850 * prefixes after removing all addresses above.
851 * (Or can we just delay calling nd6_purge until at this point?)
855 /* remove route to link-local allnodes multicast (ff02::1) */
856 bzero(&sin6, sizeof(sin6));
857 sin6.sin6_len = sizeof(struct sockaddr_in6);
858 sin6.sin6_family = AF_INET6;
859 sin6.sin6_addr = in6addr_linklocal_allnodes;
860 if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
861 /* XXX: should not fail */
863 /* XXX grab lock first to avoid LOR */
864 rnh = rt_tables_get_rnh(0, AF_INET6);
866 RADIX_NODE_HEAD_LOCK(rnh);
867 rt = rtalloc1((struct sockaddr *)&sin6, 0, RTF_RNH_LOCKED);
869 if (rt->rt_ifp == ifp)
873 RADIX_NODE_HEAD_UNLOCK(rnh);
878 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
879 const u_int8_t *baseid, int generate)
882 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
884 bzero(nullbuf, sizeof(nullbuf));
885 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
886 /* we've never created a random ID. Create a new one. */
891 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
893 /* generate_tmp_ifid will update seedn and buf */
894 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
897 bcopy(ndi->randomid, retbuf, 8);
903 in6_tmpaddrtimer(void *arg)
905 CURVNET_SET((struct vnet *) arg);
906 struct nd_ifinfo *ndi;
910 callout_reset(&V_in6_tmpaddrtimer_ch,
911 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
912 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
914 bzero(nullbuf, sizeof(nullbuf));
915 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
916 ifp = TAILQ_NEXT(ifp, if_list)) {
917 ndi = ND_IFINFO(ifp);
918 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
920 * We've been generating a random ID on this interface.
923 (void)generate_tmp_ifid(ndi->randomseed0,
924 ndi->randomseed1, ndi->randomid);
932 in6_purgemaddrs(struct ifnet *ifp)
934 LIST_HEAD(,in6_multi) purgeinms;
935 struct in6_multi *inm, *tinm;
936 struct ifmultiaddr *ifma;
938 LIST_INIT(&purgeinms);
942 * Extract list of in6_multi associated with the detaching ifp
943 * which the PF_INET6 layer is about to release.
944 * We need to do this as IF_ADDR_LOCK() may be re-acquired
945 * by code further down.
948 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
949 if (ifma->ifma_addr->sa_family != AF_INET6 ||
950 ifma->ifma_protospec == NULL)
952 inm = (struct in6_multi *)ifma->ifma_protospec;
953 LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry);
957 LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) {
958 LIST_REMOVE(inm, in6m_entry);
959 in6m_release_locked(inm);