2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
43 #include <sys/kernel.h>
46 #include <sys/rmlock.h>
47 #include <sys/syslog.h>
51 #include <net/if_var.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netinet/if_ether.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/udp.h>
63 #include <netinet/udp_var.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #include <netinet6/in6_var.h>
68 #include <netinet6/in6_pcb.h>
69 #include <netinet6/in6_ifattach.h>
70 #include <netinet6/ip6_var.h>
71 #include <netinet6/nd6.h>
72 #include <netinet6/mld6_var.h>
73 #include <netinet6/scope6_var.h>
75 VNET_DEFINE(unsigned long, in6_maxmtu) = 0;
77 #ifdef IP6_AUTO_LINKLOCAL
78 VNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL;
80 VNET_DEFINE(int, ip6_auto_linklocal) = 1; /* enabled by default */
83 VNET_DEFINE(struct callout, in6_tmpaddrtimer_ch);
84 #define V_in6_tmpaddrtimer_ch VNET(in6_tmpaddrtimer_ch)
86 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
87 #define V_ripcbinfo VNET(ripcbinfo)
89 static int get_rand_ifid(struct ifnet *, struct in6_addr *);
90 static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
91 static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
92 static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
93 static int in6_ifattach_loopback(struct ifnet *);
94 static void in6_purgemaddrs(struct ifnet *);
96 #define EUI64_GBIT 0x01
97 #define EUI64_UBIT 0x02
98 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
99 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
100 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
101 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
102 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
104 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
105 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
108 * Generate a last-resort interface identifier, when the machine has no
109 * IEEE802/EUI64 address sources.
110 * The goal here is to get an interface identifier that is
111 * (1) random enough and (2) does not change across reboot.
112 * We currently use MD5(hostname) for it.
114 * in6 - upper 64bits are preserved
117 get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
124 pr = curthread->td_ucred->cr_prison;
125 mtx_lock(&pr->pr_mtx);
126 hostnamelen = strlen(pr->pr_hostname);
128 /* we need at least several letters as seed for ifid */
129 if (hostnamelen < 3) {
130 mtx_unlock(&pr->pr_mtx);
135 /* generate 8 bytes of pseudo-random value. */
136 bzero(&ctxt, sizeof(ctxt));
138 MD5Update(&ctxt, pr->pr_hostname, hostnamelen);
139 mtx_unlock(&pr->pr_mtx);
140 MD5Final(digest, &ctxt);
142 /* assumes sizeof(digest) > sizeof(ifid) */
143 bcopy(digest, &in6->s6_addr[8], 8);
145 /* make sure to set "u" bit to local, and "g" bit to individual. */
146 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
147 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
149 /* convert EUI64 into IPv6 interface identifier */
156 generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
159 u_int8_t seed[16], digest[16], nullbuf[8];
162 /* If there's no history, start with a random seed. */
163 bzero(nullbuf, sizeof(nullbuf));
164 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
167 for (i = 0; i < 2; i++) {
168 val32 = arc4random();
169 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
172 bcopy(seed0, seed, 8);
174 /* copy the right-most 64-bits of the given address */
175 /* XXX assumption on the size of IFID */
176 bcopy(seed1, &seed[8], 8);
178 if (0) { /* for debugging purposes only */
181 printf("generate_tmp_ifid: new randomized ID from: ");
182 for (i = 0; i < 16; i++)
183 printf("%02x", seed[i]);
187 /* generate 16 bytes of pseudo-random value. */
188 bzero(&ctxt, sizeof(ctxt));
190 MD5Update(&ctxt, seed, sizeof(seed));
191 MD5Final(digest, &ctxt);
194 * RFC 3041 3.2.1. (3)
195 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
196 * left-most bit is numbered 0) to zero.
198 bcopy(digest, ret, 8);
199 ret[0] &= ~EUI64_UBIT;
202 * XXX: we'd like to ensure that the generated value is not zero
203 * for simplicity. If the caclculated digest happens to be zero,
204 * use a random non-zero value as the last resort.
206 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
208 "generate_tmp_ifid: computed MD5 value is zero.\n"));
210 val32 = arc4random();
211 val32 = 1 + (val32 % (0xffffffff - 1));
215 * RFC 3041 3.2.1. (4)
216 * Take the rightmost 64-bits of the MD5 digest and save them in
217 * stable storage as the history value to be used in the next
218 * iteration of the algorithm.
220 bcopy(&digest[8], seed0, 8);
222 if (0) { /* for debugging purposes only */
226 for (i = 0; i < 16; i++)
227 printf("%02x", digest[i]);
235 * Get interface identifier for the specified interface.
236 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
238 * in6 - upper 64bits are preserved
241 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
244 struct sockaddr_dl *sdl;
247 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
248 static u_int8_t allone[8] =
249 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
252 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
253 if (ifa->ifa_addr->sa_family != AF_LINK)
255 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
258 if (sdl->sdl_alen == 0)
263 IF_ADDR_RUNLOCK(ifp);
268 IF_ADDR_LOCK_ASSERT(ifp);
270 addrlen = sdl->sdl_alen;
273 switch (ifp->if_type) {
281 /* IEEE802/EUI64 cases - what others? */
282 /* IEEE1394 uses 16byte length address starting with EUI64 */
286 /* look at IEEE802/EUI64 only */
287 if (addrlen != 8 && addrlen != 6) {
288 IF_ADDR_RUNLOCK(ifp);
293 * check for invalid MAC address - on bsdi, we see it a lot
294 * since wildboar configures all-zero MAC on pccard before
297 if (bcmp(addr, allzero, addrlen) == 0) {
298 IF_ADDR_RUNLOCK(ifp);
301 if (bcmp(addr, allone, addrlen) == 0) {
302 IF_ADDR_RUNLOCK(ifp);
306 /* make EUI64 address */
308 bcopy(addr, &in6->s6_addr[8], 8);
309 else if (addrlen == 6) {
310 in6->s6_addr[8] = addr[0];
311 in6->s6_addr[9] = addr[1];
312 in6->s6_addr[10] = addr[2];
313 in6->s6_addr[11] = 0xff;
314 in6->s6_addr[12] = 0xfe;
315 in6->s6_addr[13] = addr[3];
316 in6->s6_addr[14] = addr[4];
317 in6->s6_addr[15] = addr[5];
323 IF_ADDR_RUNLOCK(ifp);
327 IF_ADDR_RUNLOCK(ifp);
331 bzero(&in6->s6_addr[8], 8);
332 in6->s6_addr[15] = addr[0];
335 * due to insufficient bitwidth, we mark it local.
337 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
338 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
344 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
345 * however, IPv4 address is not very suitable as unique
346 * identifier source (can be renumbered).
349 IF_ADDR_RUNLOCK(ifp);
353 IF_ADDR_RUNLOCK(ifp);
357 /* sanity check: g bit must not indicate "group" */
358 if (EUI64_GROUP(in6)) {
359 IF_ADDR_RUNLOCK(ifp);
363 /* convert EUI64 into IPv6 interface identifier */
367 * sanity check: ifid must not be all zero, avoid conflict with
368 * subnet router anycast
370 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
371 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
372 IF_ADDR_RUNLOCK(ifp);
376 IF_ADDR_RUNLOCK(ifp);
381 * Get interface identifier for the specified interface. If it is not
382 * available on ifp0, borrow interface identifier from other information
385 * altifp - secondary EUI64 source
388 get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
389 struct in6_addr *in6)
393 /* first, try to get it from the interface itself */
394 if (in6_get_hw_ifid(ifp0, in6) == 0) {
395 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
400 /* try secondary EUI64 source. this basically is for ATM PVC */
401 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
402 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
403 if_name(ifp0), if_name(altifp)));
407 /* next, try to get it from some other hardware interface */
408 IFNET_RLOCK_NOSLEEP();
409 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
412 if (in6_get_hw_ifid(ifp, in6) != 0)
416 * to borrow ifid from other interface, ifid needs to be
419 if (IFID_UNIVERSAL(in6)) {
421 "%s: borrow interface identifier from %s\n",
422 if_name(ifp0), if_name(ifp)));
423 IFNET_RUNLOCK_NOSLEEP();
427 IFNET_RUNLOCK_NOSLEEP();
429 /* last resort: get from random number source */
430 if (get_rand_ifid(ifp, in6) == 0) {
432 "%s: interface identifier generated by random number\n",
437 printf("%s: failed to get interface identifier\n", if_name(ifp0));
441 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
442 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
443 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
444 in6->s6_addr[14], in6->s6_addr[15]));
449 * altifp - secondary EUI64 source
452 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
454 struct in6_ifaddr *ia;
455 struct in6_aliasreq ifra;
456 struct nd_prefixctl pr0;
457 struct nd_prefix *pr;
461 * configure link-local address.
463 in6_prepare_ifra(&ifra, NULL, &in6mask64);
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 /* link-local addresses should NEVER expire. */
481 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
482 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
485 * Now call in6_update_ifa() to do a bunch of procedures to configure
486 * a link-local address. We can set the 3rd argument to NULL, because
487 * we know there's no other link-local address on the interface
488 * and therefore we are adding one (instead of updating one).
490 if ((error = in6_update_ifa(ifp, &ifra, NULL,
491 IN6_IFAUPDATE_DADDELAY)) != 0) {
493 * XXX: When the interface does not support IPv6, this call
494 * would fail in the SIOCSIFADDR ioctl. I believe the
495 * notification is rather confusing in this case, so just
496 * suppress it. (jinmei@kame.net 20010130)
498 if (error != EAFNOSUPPORT)
499 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
500 "configure a link-local address on %s "
502 if_name(ifp), error));
506 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
507 KASSERT(ia != NULL, ("%s: ia == NULL, ifp=%p", __func__, ifp));
509 ifa_free(&ia->ia_ifa);
512 * Make the link-local prefix (fe80::%link/64) as on-link.
513 * Since we'd like to manage prefixes separately from addresses,
514 * we make an ND6 prefix structure for the link-local prefix,
515 * and add it to the prefix list as a never-expire prefix.
516 * XXX: this change might affect some existing code base...
518 bzero(&pr0, sizeof(pr0));
520 /* this should be 64 at this moment. */
521 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
522 pr0.ndpr_prefix = ifra.ifra_addr;
523 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
524 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, &in6mask64);
526 * Initialize parameters. The link-local prefix must always be
527 * on-link, and its lifetimes never expire.
529 pr0.ndpr_raf_onlink = 1;
530 pr0.ndpr_raf_auto = 1; /* probably meaningless */
531 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
532 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
534 * Since there is no other link-local addresses, nd6_prefix_lookup()
535 * probably returns NULL. However, we cannot always expect the result.
536 * For example, if we first remove the (only) existing link-local
537 * address, and then reconfigure another one, the prefix is still
538 * valid with referring to the old link-local address.
540 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
541 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
550 * ifp - must be IFT_LOOP
553 in6_ifattach_loopback(struct ifnet *ifp)
555 struct in6_aliasreq ifra;
558 in6_prepare_ifra(&ifra, &in6addr_loopback, &in6mask128);
561 * Always initialize ia_dstaddr (= broadcast address) to loopback
562 * address. Follows IPv4 practice - see in_ifinit().
564 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
565 ifra.ifra_dstaddr.sin6_family = AF_INET6;
566 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
568 /* the loopback address should NEVER expire. */
569 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
570 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
573 * We are sure that this is a newly assigned address, so we can set
574 * NULL to the 3rd arg.
576 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
577 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
578 "the loopback address on %s (errno=%d)\n",
579 if_name(ifp), error));
587 * compute NI group address, based on the current hostname setting.
590 * when ifp == NULL, the caller is responsible for filling scopeid.
592 * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address
593 * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620.
596 in6_nigroup0(struct ifnet *ifp, const char *name, int namelen,
597 struct in6_addr *in6, int oldmcprefix)
605 char n[64]; /* a single label must not exceed 63 chars */
608 * If no name is given and namelen is -1,
609 * we try to do the hostname lookup ourselves.
611 if (!name && namelen == -1) {
612 pr = curthread->td_ucred->cr_prison;
613 mtx_lock(&pr->pr_mtx);
614 name = pr->pr_hostname;
615 namelen = strlen(name);
618 if (!name || !namelen) {
620 mtx_unlock(&pr->pr_mtx);
625 while (p && *p && *p != '.' && p - name < namelen)
627 if (p == name || p - name > sizeof(n) - 1) {
629 mtx_unlock(&pr->pr_mtx);
630 return -1; /* label too long */
635 mtx_unlock(&pr->pr_mtx);
637 for (q = n; *q; q++) {
638 if ('A' <= *q && *q <= 'Z')
642 /* generate 16 bytes of pseudo-random value. */
643 bzero(&ctxt, sizeof(ctxt));
645 MD5Update(&ctxt, &l, sizeof(l));
646 MD5Update(&ctxt, n, l);
647 MD5Final(digest, &ctxt);
649 bzero(in6, sizeof(*in6));
650 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
651 in6->s6_addr8[11] = 2;
652 if (oldmcprefix == 0) {
653 in6->s6_addr8[12] = 0xff;
654 /* Copy the first 24 bits of 128-bit hash into the address. */
655 bcopy(digest, &in6->s6_addr8[13], 3);
657 /* Copy the first 32 bits of 128-bit hash into the address. */
658 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
660 if (in6_setscope(in6, ifp, NULL))
661 return (-1); /* XXX: should not fail */
667 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
668 struct in6_addr *in6)
671 return (in6_nigroup0(ifp, name, namelen, in6, 0));
675 in6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen,
676 struct in6_addr *in6)
679 return (in6_nigroup0(ifp, name, namelen, in6, 1));
683 * XXX multiple loopback interface needs more care. for instance,
684 * nodelocal address needs to be configured onto only one of them.
685 * XXX multiple link-local address case
687 * altifp - secondary EUI64 source
690 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
692 struct in6_ifaddr *ia;
695 if (ifp->if_afdata[AF_INET6] == NULL)
698 * quirks based on interface type
700 switch (ifp->if_type) {
703 * 6to4 interface is a very special kind of beast.
704 * no multicast, no linklocal. RFC2529 specifies how to make
705 * linklocals for 6to4 interface, but there's no use and
706 * it is rather harmful to have one.
708 ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
715 * usually, we require multicast capability to the interface
717 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
718 nd6log((LOG_INFO, "in6_ifattach: "
719 "%s is not multicast capable, IPv6 not enabled\n",
725 * assign loopback address for loopback interface.
726 * XXX multiple loopback interface case.
728 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
731 in6 = in6addr_loopback;
732 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &in6);
734 if (in6_ifattach_loopback(ifp) != 0)
741 * assign a link-local address, if there's none.
743 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
744 ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
747 ia = in6ifa_ifpforlinklocal(ifp, 0);
749 error = in6_ifattach_linklocal(ifp, altifp);
752 log(LOG_NOTICE, "in6_ifattach_linklocal: "
753 "failed to add a link-local addr to %s\n",
757 ifa_free(&ia->ia_ifa);
760 /* update dynamically. */
761 if (V_in6_maxmtu < ifp->if_mtu)
762 V_in6_maxmtu = ifp->if_mtu;
766 * NOTE: in6_ifdetach() does not support loopback if at this moment.
768 * When shutting down a VNET we clean up layers top-down. In that case
769 * upper layer protocols (ulp) are cleaned up already and locks are destroyed
770 * and we must not call into these cleanup functions anymore, thus purgeulp
771 * is set to 0 in that case by in6_ifdetach_destroy().
772 * The normal case of destroying a (cloned) interface still needs to cleanup
773 * everything related to the interface and will have purgeulp set to 1.
776 _in6_ifdetach(struct ifnet *ifp, int purgeulp)
778 struct ifaddr *ifa, *next;
780 if (ifp->if_afdata[AF_INET6] == NULL)
784 * nuke any of IPv6 addresses we have
785 * XXX: all addresses should be already removed
787 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
788 if (ifa->ifa_addr->sa_family != AF_INET6)
793 in6_pcbpurgeif0(&V_udbinfo, ifp);
794 in6_pcbpurgeif0(&V_ulitecbinfo, ifp);
795 in6_pcbpurgeif0(&V_ripcbinfo, ifp);
797 /* leave from all multicast groups joined */
798 in6_purgemaddrs(ifp);
801 * Remove neighbor management table.
802 * Enabling the nd6_purge will panic on vmove for interfaces on VNET
803 * teardown as the IPv6 layer is cleaned up already and the locks
811 in6_ifdetach(struct ifnet *ifp)
814 _in6_ifdetach(ifp, 1);
818 in6_ifdetach_destroy(struct ifnet *ifp)
821 _in6_ifdetach(ifp, 0);
825 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
826 const u_int8_t *baseid, int generate)
829 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
831 bzero(nullbuf, sizeof(nullbuf));
832 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
833 /* we've never created a random ID. Create a new one. */
838 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
840 /* generate_tmp_ifid will update seedn and buf */
841 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
844 bcopy(ndi->randomid, retbuf, 8);
850 in6_tmpaddrtimer(void *arg)
852 CURVNET_SET((struct vnet *) arg);
853 struct nd_ifinfo *ndi;
857 callout_reset(&V_in6_tmpaddrtimer_ch,
858 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
859 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
861 bzero(nullbuf, sizeof(nullbuf));
862 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
863 if (ifp->if_afdata[AF_INET6] == NULL)
865 ndi = ND_IFINFO(ifp);
866 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
868 * We've been generating a random ID on this interface.
871 (void)generate_tmp_ifid(ndi->randomseed0,
872 ndi->randomseed1, ndi->randomid);
880 in6_purgemaddrs(struct ifnet *ifp)
882 LIST_HEAD(,in6_multi) purgeinms;
883 struct in6_multi *inm, *tinm;
884 struct ifmultiaddr *ifma;
886 LIST_INIT(&purgeinms);
890 * Extract list of in6_multi associated with the detaching ifp
891 * which the PF_INET6 layer is about to release.
892 * We need to do this as IF_ADDR_LOCK() may be re-acquired
893 * by code further down.
896 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
897 if (ifma->ifma_addr->sa_family != AF_INET6 ||
898 ifma->ifma_protospec == NULL)
900 inm = (struct in6_multi *)ifma->ifma_protospec;
901 LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry);
903 IF_ADDR_RUNLOCK(ifp);
905 LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) {
906 LIST_REMOVE(inm, in6m_entry);
907 in6m_release_locked(inm);
915 in6_ifattach_destroy(void)
918 callout_drain(&V_in6_tmpaddrtimer_ch);
922 in6_ifattach_init(void *dummy)
925 /* Timer for regeneranation of temporary addresses randomize ID. */
926 callout_init(&V_in6_tmpaddrtimer_ch, 0);
927 callout_reset(&V_in6_tmpaddrtimer_ch,
928 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
929 V_ip6_temp_regen_advance) * hz,
930 in6_tmpaddrtimer, curvnet);
935 * This must be after route_init(), which is now SI_ORDER_THIRD.
937 SYSINIT(in6_ifattach_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
938 in6_ifattach_init, NULL);