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>
44 #include <sys/rmlock.h>
45 #include <sys/syslog.h>
49 #include <net/if_var.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/if_ether.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/udp.h>
61 #include <netinet/udp_var.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet6/in6_pcb.h>
67 #include <netinet6/in6_ifattach.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/nd6.h>
70 #include <netinet6/mld6_var.h>
71 #include <netinet6/scope6_var.h>
73 VNET_DEFINE(unsigned long, in6_maxmtu) = 0;
75 #ifdef IP6_AUTO_LINKLOCAL
76 VNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL;
78 VNET_DEFINE(int, ip6_auto_linklocal) = 1; /* enabled by default */
81 VNET_DEFINE(struct callout, in6_tmpaddrtimer_ch);
82 #define V_in6_tmpaddrtimer_ch VNET(in6_tmpaddrtimer_ch)
84 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
85 #define V_ripcbinfo VNET(ripcbinfo)
87 static int get_rand_ifid(struct ifnet *, struct in6_addr *);
88 static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
89 static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
90 static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
91 static int in6_ifattach_loopback(struct ifnet *);
92 static void in6_purgemaddrs(struct ifnet *);
94 #define EUI64_GBIT 0x01
95 #define EUI64_UBIT 0x02
96 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
97 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
98 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
99 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
100 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
102 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
103 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
106 * Generate a last-resort interface identifier, when the machine has no
107 * IEEE802/EUI64 address sources.
108 * The goal here is to get an interface identifier that is
109 * (1) random enough and (2) does not change across reboot.
110 * We currently use MD5(hostname) for it.
112 * in6 - upper 64bits are preserved
115 get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
122 pr = curthread->td_ucred->cr_prison;
123 mtx_lock(&pr->pr_mtx);
124 hostnamelen = strlen(pr->pr_hostname);
126 /* we need at least several letters as seed for ifid */
127 if (hostnamelen < 3) {
128 mtx_unlock(&pr->pr_mtx);
133 /* generate 8 bytes of pseudo-random value. */
134 bzero(&ctxt, sizeof(ctxt));
136 MD5Update(&ctxt, pr->pr_hostname, hostnamelen);
137 mtx_unlock(&pr->pr_mtx);
138 MD5Final(digest, &ctxt);
140 /* assumes sizeof(digest) > sizeof(ifid) */
141 bcopy(digest, &in6->s6_addr[8], 8);
143 /* make sure to set "u" bit to local, and "g" bit to individual. */
144 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
145 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
147 /* convert EUI64 into IPv6 interface identifier */
154 generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
157 u_int8_t seed[16], digest[16], nullbuf[8];
160 /* If there's no history, start with a random seed. */
161 bzero(nullbuf, sizeof(nullbuf));
162 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
165 for (i = 0; i < 2; i++) {
166 val32 = arc4random();
167 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
170 bcopy(seed0, seed, 8);
172 /* copy the right-most 64-bits of the given address */
173 /* XXX assumption on the size of IFID */
174 bcopy(seed1, &seed[8], 8);
176 if (0) { /* for debugging purposes only */
179 printf("generate_tmp_ifid: new randomized ID from: ");
180 for (i = 0; i < 16; i++)
181 printf("%02x", seed[i]);
185 /* generate 16 bytes of pseudo-random value. */
186 bzero(&ctxt, sizeof(ctxt));
188 MD5Update(&ctxt, seed, sizeof(seed));
189 MD5Final(digest, &ctxt);
192 * RFC 3041 3.2.1. (3)
193 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
194 * left-most bit is numbered 0) to zero.
196 bcopy(digest, ret, 8);
197 ret[0] &= ~EUI64_UBIT;
200 * XXX: we'd like to ensure that the generated value is not zero
201 * for simplicity. If the caclculated digest happens to be zero,
202 * use a random non-zero value as the last resort.
204 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
206 "generate_tmp_ifid: computed MD5 value is zero.\n"));
208 val32 = arc4random();
209 val32 = 1 + (val32 % (0xffffffff - 1));
213 * RFC 3041 3.2.1. (4)
214 * Take the rightmost 64-bits of the MD5 digest and save them in
215 * stable storage as the history value to be used in the next
216 * iteration of the algorithm.
218 bcopy(&digest[8], seed0, 8);
220 if (0) { /* for debugging purposes only */
224 for (i = 0; i < 16; i++)
225 printf("%02x", digest[i]);
233 * Get interface identifier for the specified interface.
234 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
236 * in6 - upper 64bits are preserved
239 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
242 struct sockaddr_dl *sdl;
245 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
246 static u_int8_t allone[8] =
247 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
250 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
251 if (ifa->ifa_addr->sa_family != AF_LINK)
253 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
256 if (sdl->sdl_alen == 0)
261 IF_ADDR_RUNLOCK(ifp);
266 IF_ADDR_LOCK_ASSERT(ifp);
268 addrlen = sdl->sdl_alen;
271 switch (ifp->if_type) {
279 /* IEEE802/EUI64 cases - what others? */
280 /* IEEE1394 uses 16byte length address starting with EUI64 */
284 /* look at IEEE802/EUI64 only */
285 if (addrlen != 8 && addrlen != 6) {
286 IF_ADDR_RUNLOCK(ifp);
291 * check for invalid MAC address - on bsdi, we see it a lot
292 * since wildboar configures all-zero MAC on pccard before
295 if (bcmp(addr, allzero, addrlen) == 0) {
296 IF_ADDR_RUNLOCK(ifp);
299 if (bcmp(addr, allone, addrlen) == 0) {
300 IF_ADDR_RUNLOCK(ifp);
304 /* make EUI64 address */
306 bcopy(addr, &in6->s6_addr[8], 8);
307 else if (addrlen == 6) {
308 in6->s6_addr[8] = addr[0];
309 in6->s6_addr[9] = addr[1];
310 in6->s6_addr[10] = addr[2];
311 in6->s6_addr[11] = 0xff;
312 in6->s6_addr[12] = 0xfe;
313 in6->s6_addr[13] = addr[3];
314 in6->s6_addr[14] = addr[4];
315 in6->s6_addr[15] = addr[5];
321 IF_ADDR_RUNLOCK(ifp);
325 IF_ADDR_RUNLOCK(ifp);
329 bzero(&in6->s6_addr[8], 8);
330 in6->s6_addr[15] = addr[0];
333 * due to insufficient bitwidth, we mark it local.
335 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
336 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
342 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
343 * however, IPv4 address is not very suitable as unique
344 * identifier source (can be renumbered).
347 IF_ADDR_RUNLOCK(ifp);
351 IF_ADDR_RUNLOCK(ifp);
355 /* sanity check: g bit must not indicate "group" */
356 if (EUI64_GROUP(in6)) {
357 IF_ADDR_RUNLOCK(ifp);
361 /* convert EUI64 into IPv6 interface identifier */
365 * sanity check: ifid must not be all zero, avoid conflict with
366 * subnet router anycast
368 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
369 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
370 IF_ADDR_RUNLOCK(ifp);
374 IF_ADDR_RUNLOCK(ifp);
379 * Get interface identifier for the specified interface. If it is not
380 * available on ifp0, borrow interface identifier from other information
383 * altifp - secondary EUI64 source
386 get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
387 struct in6_addr *in6)
391 /* first, try to get it from the interface itself */
392 if (in6_get_hw_ifid(ifp0, in6) == 0) {
393 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
398 /* try secondary EUI64 source. this basically is for ATM PVC */
399 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
400 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
401 if_name(ifp0), if_name(altifp)));
405 /* next, try to get it from some other hardware interface */
406 IFNET_RLOCK_NOSLEEP();
407 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
410 if (in6_get_hw_ifid(ifp, in6) != 0)
414 * to borrow ifid from other interface, ifid needs to be
417 if (IFID_UNIVERSAL(in6)) {
419 "%s: borrow interface identifier from %s\n",
420 if_name(ifp0), if_name(ifp)));
421 IFNET_RUNLOCK_NOSLEEP();
425 IFNET_RUNLOCK_NOSLEEP();
427 /* last resort: get from random number source */
428 if (get_rand_ifid(ifp, in6) == 0) {
430 "%s: interface identifier generated by random number\n",
435 printf("%s: failed to get interface identifier\n", if_name(ifp0));
439 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
440 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
441 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
442 in6->s6_addr[14], in6->s6_addr[15]));
447 * altifp - secondary EUI64 source
450 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
452 struct in6_ifaddr *ia;
453 struct in6_aliasreq ifra;
454 struct nd_prefixctl pr0;
455 struct nd_prefix *pr;
459 * configure link-local address.
461 in6_prepare_ifra(&ifra, NULL, &in6mask64);
463 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
464 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
465 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
466 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
467 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
469 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
471 "%s: no ifid available\n", if_name(ifp)));
475 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
478 /* link-local addresses should NEVER expire. */
479 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
480 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
483 * Now call in6_update_ifa() to do a bunch of procedures to configure
484 * a link-local address. We can set the 3rd argument to NULL, because
485 * we know there's no other link-local address on the interface
486 * and therefore we are adding one (instead of updating one).
488 if ((error = in6_update_ifa(ifp, &ifra, NULL,
489 IN6_IFAUPDATE_DADDELAY)) != 0) {
491 * XXX: When the interface does not support IPv6, this call
492 * would fail in the SIOCSIFADDR ioctl. I believe the
493 * notification is rather confusing in this case, so just
494 * suppress it. (jinmei@kame.net 20010130)
496 if (error != EAFNOSUPPORT)
497 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
498 "configure a link-local address on %s "
500 if_name(ifp), error));
504 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
505 KASSERT(ia != NULL, ("%s: ia == NULL, ifp=%p", __func__, ifp));
507 ifa_free(&ia->ia_ifa);
510 * Make the link-local prefix (fe80::%link/64) as on-link.
511 * Since we'd like to manage prefixes separately from addresses,
512 * we make an ND6 prefix structure for the link-local prefix,
513 * and add it to the prefix list as a never-expire prefix.
514 * XXX: this change might affect some existing code base...
516 bzero(&pr0, sizeof(pr0));
518 /* this should be 64 at this moment. */
519 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
520 pr0.ndpr_prefix = ifra.ifra_addr;
521 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
522 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, &in6mask64);
524 * Initialize parameters. The link-local prefix must always be
525 * on-link, and its lifetimes never expire.
527 pr0.ndpr_raf_onlink = 1;
528 pr0.ndpr_raf_auto = 1; /* probably meaningless */
529 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
530 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
532 * Since there is no other link-local addresses, nd6_prefix_lookup()
533 * probably returns NULL. However, we cannot always expect the result.
534 * For example, if we first remove the (only) existing link-local
535 * address, and then reconfigure another one, the prefix is still
536 * valid with referring to the old link-local address.
538 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
539 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
548 * ifp - must be IFT_LOOP
551 in6_ifattach_loopback(struct ifnet *ifp)
553 struct in6_aliasreq ifra;
556 in6_prepare_ifra(&ifra, &in6addr_loopback, &in6mask128);
559 * Always initialize ia_dstaddr (= broadcast address) to loopback
560 * address. Follows IPv4 practice - see in_ifinit().
562 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
563 ifra.ifra_dstaddr.sin6_family = AF_INET6;
564 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
566 /* the loopback address should NEVER expire. */
567 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
568 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
571 * We are sure that this is a newly assigned address, so we can set
572 * NULL to the 3rd arg.
574 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
575 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
576 "the loopback address on %s (errno=%d)\n",
577 if_name(ifp), error));
585 * compute NI group address, based on the current hostname setting.
588 * when ifp == NULL, the caller is responsible for filling scopeid.
590 * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address
591 * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620.
594 in6_nigroup0(struct ifnet *ifp, const char *name, int namelen,
595 struct in6_addr *in6, int oldmcprefix)
603 char n[64]; /* a single label must not exceed 63 chars */
606 * If no name is given and namelen is -1,
607 * we try to do the hostname lookup ourselves.
609 if (!name && namelen == -1) {
610 pr = curthread->td_ucred->cr_prison;
611 mtx_lock(&pr->pr_mtx);
612 name = pr->pr_hostname;
613 namelen = strlen(name);
616 if (!name || !namelen) {
618 mtx_unlock(&pr->pr_mtx);
623 while (p && *p && *p != '.' && p - name < namelen)
625 if (p == name || p - name > sizeof(n) - 1) {
627 mtx_unlock(&pr->pr_mtx);
628 return -1; /* label too long */
633 mtx_unlock(&pr->pr_mtx);
635 for (q = n; *q; q++) {
636 if ('A' <= *q && *q <= 'Z')
640 /* generate 16 bytes of pseudo-random value. */
641 bzero(&ctxt, sizeof(ctxt));
643 MD5Update(&ctxt, &l, sizeof(l));
644 MD5Update(&ctxt, n, l);
645 MD5Final(digest, &ctxt);
647 bzero(in6, sizeof(*in6));
648 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
649 in6->s6_addr8[11] = 2;
650 if (oldmcprefix == 0) {
651 in6->s6_addr8[12] = 0xff;
652 /* Copy the first 24 bits of 128-bit hash into the address. */
653 bcopy(digest, &in6->s6_addr8[13], 3);
655 /* Copy the first 32 bits of 128-bit hash into the address. */
656 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
658 if (in6_setscope(in6, ifp, NULL))
659 return (-1); /* XXX: should not fail */
665 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
666 struct in6_addr *in6)
669 return (in6_nigroup0(ifp, name, namelen, in6, 0));
673 in6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen,
674 struct in6_addr *in6)
677 return (in6_nigroup0(ifp, name, namelen, in6, 1));
681 * XXX multiple loopback interface needs more care. for instance,
682 * nodelocal address needs to be configured onto only one of them.
683 * XXX multiple link-local address case
685 * altifp - secondary EUI64 source
688 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
690 struct in6_ifaddr *ia;
693 if (ifp->if_afdata[AF_INET6] == NULL)
696 * quirks based on interface type
698 switch (ifp->if_type) {
701 * 6to4 interface is a very special kind of beast.
702 * no multicast, no linklocal. RFC2529 specifies how to make
703 * linklocals for 6to4 interface, but there's no use and
704 * it is rather harmful to have one.
706 ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
713 * usually, we require multicast capability to the interface
715 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
716 nd6log((LOG_INFO, "in6_ifattach: "
717 "%s is not multicast capable, IPv6 not enabled\n",
723 * assign loopback address for loopback interface.
724 * XXX multiple loopback interface case.
726 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
729 in6 = in6addr_loopback;
730 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &in6);
732 if (in6_ifattach_loopback(ifp) != 0)
739 * assign a link-local address, if there's none.
741 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
742 ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
745 ia = in6ifa_ifpforlinklocal(ifp, 0);
747 error = in6_ifattach_linklocal(ifp, altifp);
750 log(LOG_NOTICE, "in6_ifattach_linklocal: "
751 "failed to add a link-local addr to %s\n",
755 ifa_free(&ia->ia_ifa);
758 /* update dynamically. */
759 if (V_in6_maxmtu < ifp->if_mtu)
760 V_in6_maxmtu = ifp->if_mtu;
764 * NOTE: in6_ifdetach() does not support loopback if at this moment.
766 * When shutting down a VNET we clean up layers top-down. In that case
767 * upper layer protocols (ulp) are cleaned up already and locks are destroyed
768 * and we must not call into these cleanup functions anymore, thus purgeulp
769 * is set to 0 in that case by in6_ifdetach_destroy().
770 * The normal case of destroying a (cloned) interface still needs to cleanup
771 * everything related to the interface and will have purgeulp set to 1.
774 _in6_ifdetach(struct ifnet *ifp, int purgeulp)
776 struct ifaddr *ifa, *next;
778 if (ifp->if_afdata[AF_INET6] == NULL)
782 * nuke any of IPv6 addresses we have
783 * XXX: all addresses should be already removed
785 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
786 if (ifa->ifa_addr->sa_family != AF_INET6)
791 in6_pcbpurgeif0(&V_udbinfo, ifp);
792 in6_pcbpurgeif0(&V_ulitecbinfo, ifp);
793 in6_pcbpurgeif0(&V_ripcbinfo, ifp);
795 /* leave from all multicast groups joined */
796 in6_purgemaddrs(ifp);
799 * Remove neighbor management table.
800 * Enabling the nd6_purge will panic on vmove for interfaces on VNET
801 * teardown as the IPv6 layer is cleaned up already and the locks
809 in6_ifdetach(struct ifnet *ifp)
812 _in6_ifdetach(ifp, 1);
816 in6_ifdetach_destroy(struct ifnet *ifp)
819 _in6_ifdetach(ifp, 0);
823 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
824 const u_int8_t *baseid, int generate)
827 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
829 bzero(nullbuf, sizeof(nullbuf));
830 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
831 /* we've never created a random ID. Create a new one. */
836 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
838 /* generate_tmp_ifid will update seedn and buf */
839 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
842 bcopy(ndi->randomid, retbuf, 8);
848 in6_tmpaddrtimer(void *arg)
850 CURVNET_SET((struct vnet *) arg);
851 struct nd_ifinfo *ndi;
855 callout_reset(&V_in6_tmpaddrtimer_ch,
856 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
857 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
859 bzero(nullbuf, sizeof(nullbuf));
860 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
861 if (ifp->if_afdata[AF_INET6] == NULL)
863 ndi = ND_IFINFO(ifp);
864 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
866 * We've been generating a random ID on this interface.
869 (void)generate_tmp_ifid(ndi->randomseed0,
870 ndi->randomseed1, ndi->randomid);
878 in6_purgemaddrs(struct ifnet *ifp)
880 LIST_HEAD(,in6_multi) purgeinms;
881 struct in6_multi *inm, *tinm;
882 struct ifmultiaddr *ifma;
884 LIST_INIT(&purgeinms);
888 * Extract list of in6_multi associated with the detaching ifp
889 * which the PF_INET6 layer is about to release.
890 * We need to do this as IF_ADDR_LOCK() may be re-acquired
891 * by code further down.
894 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
895 if (ifma->ifma_addr->sa_family != AF_INET6 ||
896 ifma->ifma_protospec == NULL)
898 inm = (struct in6_multi *)ifma->ifma_protospec;
899 LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry);
901 IF_ADDR_RUNLOCK(ifp);
903 LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) {
904 LIST_REMOVE(inm, in6m_entry);
905 in6m_release_locked(inm);
913 in6_ifattach_destroy(void)
916 callout_drain(&V_in6_tmpaddrtimer_ch);
920 in6_ifattach_init(void *dummy)
923 /* Timer for regeneranation of temporary addresses randomize ID. */
924 callout_init(&V_in6_tmpaddrtimer_ch, 0);
925 callout_reset(&V_in6_tmpaddrtimer_ch,
926 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
927 V_ip6_temp_regen_advance) * hz,
928 in6_tmpaddrtimer, curvnet);
933 * This must be after route_init(), which is now SI_ORDER_THIRD.
935 SYSINIT(in6_ifattach_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
936 in6_ifattach_init, NULL);