1 /* $NetBSD: in6_ifattach.c,v 1.82 2009/07/30 17:28:36 dyoung Exp $ */
2 /* $KAME: in6_ifattach.c,v 1.124 2001/07/18 08:32:51 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: in6_ifattach.c,v 1.84 2009/08/13 09:04:03 cegger Exp $");
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/kernel.h>
43 #include <sys/syslog.h>
45 #include <sys/socketvar.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
55 #include <netinet/ip6.h>
56 #include <netinet6/in6_ifattach.h>
57 #include <netinet6/ip6_var.h>
58 #include <netinet6/nd6.h>
59 #include <netinet6/ip6_mroute.h>
60 #include <netinet6/scope6_var.h>
62 #include <net/net_osdep.h>
64 unsigned long in6_maxmtu
= 0;
66 int ip6_auto_linklocal
= 1; /* enable by default */
68 callout_t in6_tmpaddrtimer_ch
;
72 static int get_hostid_ifid(struct ifnet
*, struct in6_addr
*);
74 static int get_rand_ifid(struct ifnet
*, struct in6_addr
*);
75 static int generate_tmp_ifid(u_int8_t
*, const u_int8_t
*, u_int8_t
*);
76 static int get_ifid(struct ifnet
*, struct ifnet
*, struct in6_addr
*);
77 static int in6_ifattach_linklocal(struct ifnet
*, struct ifnet
*);
78 static int in6_ifattach_loopback(struct ifnet
*);
80 #define EUI64_GBIT 0x01
81 #define EUI64_UBIT 0x02
82 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (/*CONSTCOND*/ 0)
83 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
84 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
85 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
86 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
88 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
89 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
91 #define GEN_TEMPID_RETRY_MAX 5
95 * Generate a last-resort interface identifier from hostid.
96 * works only for certain architectures (like sparc).
97 * also, using hostid itself may constitute a privacy threat, much worse
98 * than MAC addresses (hostids are used for software licensing).
99 * maybe we should use MD5(hostid) instead.
101 * in6 - upper 64bits are preserved
104 get_hostid_ifid(struct ifnet
*ifp
, struct in6_addr
*in6
)
107 static const uint8_t allzero
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
108 static const uint8_t allone
[8] =
109 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
114 /* get up to 8 bytes from the hostid field - should we get */
115 len
= (sizeof(hostid
) > 8) ? 8 : sizeof(hostid
);
116 off
= sizeof(*in6
) - len
;
117 memcpy(&in6
->s6_addr
[off
], &hostid
, len
);
119 /* make sure we do not return anything bogus */
120 if (memcmp(&in6
->s6_addr
[8], allzero
, sizeof(allzero
)))
122 if (memcmp(&in6
->s6_addr
[8], allone
, sizeof(allone
)))
125 /* make sure to set "u" bit to local, and "g" bit to individual. */
126 in6
->s6_addr
[8] &= ~EUI64_GBIT
; /* g bit to "individual" */
127 in6
->s6_addr
[8] |= EUI64_UBIT
; /* u bit to "local" */
129 /* convert EUI64 into IPv6 interface identifier */
137 * Generate a last-resort interface identifier, when the machine has no
138 * IEEE802/EUI64 address sources.
139 * The goal here is to get an interface identifier that is
140 * (1) random enough and (2) does not change across reboot.
141 * We currently use MD5(hostname) for it.
144 get_rand_ifid(struct ifnet
*ifp
,
145 struct in6_addr
*in6
) /* upper 64bits are preserved */
151 /* we need at least several letters as seed for ifid */
156 /* generate 8 bytes of pseudo-random value. */
157 memset(&ctxt
, 0, sizeof(ctxt
));
159 MD5Update(&ctxt
, (u_char
*)hostname
, hostnamelen
);
160 MD5Final(digest
, &ctxt
);
162 /* assumes sizeof(digest) > sizeof(ifid) */
163 memcpy(&in6
->s6_addr
[8], digest
, 8);
165 /* make sure to set "u" bit to local, and "g" bit to individual. */
166 in6
->s6_addr
[8] &= ~EUI64_GBIT
; /* g bit to "individual" */
167 in6
->s6_addr
[8] |= EUI64_UBIT
; /* u bit to "local" */
169 /* convert EUI64 into IPv6 interface identifier */
176 generate_tmp_ifid(u_int8_t
*seed0
, const u_int8_t
*seed1
, u_int8_t
*ret
)
179 u_int8_t seed
[16], digest
[16], nullbuf
[8];
182 * interface ID for subnet anycast addresses.
183 * XXX: we assume the unicast address range that requires IDs
186 static const uint8_t anycast_id
[8] = { 0xfd, 0xff, 0xff, 0xff,
187 0xff, 0xff, 0xff, 0x80 };
188 static const uint8_t isatap_id
[4] = { 0x00, 0x00, 0x5e, 0xfe };
189 int badid
, retry
= 0;
191 /* If there's no hisotry, start with a random seed. */
192 memset(nullbuf
, 0, sizeof(nullbuf
));
193 if (memcmp(nullbuf
, seed0
, sizeof(nullbuf
)) == 0) {
196 for (i
= 0; i
< 2; i
++) {
197 val32
= arc4random();
198 memcpy(seed
+ sizeof(val32
) * i
, &val32
, sizeof(val32
));
201 memcpy(seed
, seed0
, 8);
203 /* copy the right-most 64-bits of the given address */
204 /* XXX assumption on the size of IFID */
205 memcpy(&seed
[8], seed1
, 8);
208 /* for debugging purposes only */
213 printf("generate_tmp_ifid: new randomized ID from: ");
214 for (i
= 0; i
< 16; i
++)
215 printf("%02x", seed
[i
]);
220 /* generate 16 bytes of pseudo-random value. */
221 memset(&ctxt
, 0, sizeof(ctxt
));
223 MD5Update(&ctxt
, seed
, sizeof(seed
));
224 MD5Final(digest
, &ctxt
);
227 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (3)
228 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
229 * left-most bit is numbered 0) to zero.
231 memcpy(ret
, digest
, 8);
232 ret
[0] &= ~EUI64_UBIT
;
235 * Reject inappropriate identifiers according to
236 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (4)
237 * At this moment, we reject following cases:
239 * - identifiers that conflict with reserved subnet anycast addresses,
240 * which are defined in RFC 2526.
241 * - identifiers that conflict with ISATAP addresses
242 * - identifiers used in our own addresses
245 if (memcmp(nullbuf
, ret
, sizeof(nullbuf
)) == 0)
247 else if (memcmp(anycast_id
, ret
, 7) == 0 &&
248 (anycast_id
[7] & ret
[7]) == anycast_id
[7]) {
250 } else if (memcmp(isatap_id
, ret
, sizeof(isatap_id
)) == 0)
253 struct in6_ifaddr
*ia
;
255 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
256 if (!memcmp(&ia
->ia_addr
.sin6_addr
.s6_addr
[8],
265 * In the event that an unacceptable identifier has been generated,
266 * restart the process, using the right-most 64 bits of the MD5 digest
267 * obtained in place of the history value.
270 /* for debugging purposes only */
275 printf("unacceptable random ID: ");
276 for (i
= 0; i
< 16; i
++)
277 printf("%02x", digest
[i
]);
282 if (++retry
< GEN_TEMPID_RETRY_MAX
) {
283 memcpy(seed
, &digest
[8], 8);
287 * We're so unlucky. Give up for now, and return
288 * all 0 IDs to tell the caller not to make a
292 "generate_tmp_ifid: never found a good ID\n"));
298 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (6)
299 * Take the rightmost 64-bits of the MD5 digest and save them in
300 * stable storage as the history value to be used in the next
301 * iteration of the algorithm.
303 memcpy(seed0
, &digest
[8], 8);
305 /* for debugging purposes only */
311 for (i
= 0; i
< 16; i
++)
312 printf("%02x", digest
[i
]);
321 * Get interface identifier for the specified interface.
323 * in6 - upper 64bits are preserved
326 in6_get_hw_ifid(struct ifnet
*ifp
, struct in6_addr
*in6
)
329 const struct sockaddr_dl
*sdl
= NULL
, *tsdl
;
332 static u_int8_t allzero
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
333 static u_int8_t allone
[8] =
334 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
336 IFADDR_FOREACH(ifa
, ifp
) {
337 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
339 tsdl
= satocsdl(ifa
->ifa_addr
);
340 if (tsdl
== NULL
|| tsdl
->sdl_alen
== 0)
342 if (sdl
== NULL
|| ifa
== ifp
->if_dl
|| ifa
== ifp
->if_hwdl
)
344 if (ifa
== ifp
->if_hwdl
)
352 addrlen
= sdl
->sdl_alen
;
354 switch (ifp
->if_type
) {
357 /* IEEE1394 uses 16byte length address starting with EUI64 */
366 switch (ifp
->if_type
) {
367 /* IEEE802/EUI64 cases - what others? */
373 /* look at IEEE802/EUI64 only */
374 if (addrlen
!= 8 && addrlen
!= 6)
378 * check for invalid MAC address - on bsdi, we see it a lot
379 * since wildboar configures all-zero MAC on pccard before
382 if (memcmp(addr
, allzero
, addrlen
) == 0)
384 if (memcmp(addr
, allone
, addrlen
) == 0)
387 /* make EUI64 address */
389 memcpy(&in6
->s6_addr
[8], addr
, 8);
390 else if (addrlen
== 6) {
391 in6
->s6_addr
[8] = addr
[0];
392 in6
->s6_addr
[9] = addr
[1];
393 in6
->s6_addr
[10] = addr
[2];
394 in6
->s6_addr
[11] = 0xff;
395 in6
->s6_addr
[12] = 0xfe;
396 in6
->s6_addr
[13] = addr
[3];
397 in6
->s6_addr
[14] = addr
[4];
398 in6
->s6_addr
[15] = addr
[5];
408 memset(&in6
->s6_addr
[8], 0, 8);
409 in6
->s6_addr
[15] = addr
[0];
412 * due to insufficient bitwidth, we mark it local.
414 in6
->s6_addr
[8] &= ~EUI64_GBIT
; /* g bit to "individual" */
415 in6
->s6_addr
[8] |= EUI64_UBIT
; /* u bit to "local" */
423 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
424 * however, IPv4 address is not very suitable as unique
425 * identifier source (can be renumbered).
434 /* sanity check: g bit must not indicate "group" */
435 if (EUI64_GROUP(in6
))
438 /* convert EUI64 into IPv6 interface identifier */
442 * sanity check: ifid must not be all zero, avoid conflict with
443 * subnet router anycast
445 if ((in6
->s6_addr
[8] & ~(EUI64_GBIT
| EUI64_UBIT
)) == 0x00 &&
446 memcmp(&in6
->s6_addr
[9], allzero
, 7) == 0) {
454 * Get interface identifier for the specified interface. If it is not
455 * available on ifp0, borrow interface identifier from other information
458 * altifp - secondary EUI64 source
461 get_ifid(struct ifnet
*ifp0
, struct ifnet
*altifp
,
462 struct in6_addr
*in6
)
466 /* first, try to get it from the interface itself */
467 if (in6_get_hw_ifid(ifp0
, in6
) == 0) {
468 nd6log((LOG_DEBUG
, "%s: got interface identifier from itself\n",
473 /* try secondary EUI64 source. this basically is for ATM PVC */
474 if (altifp
&& in6_get_hw_ifid(altifp
, in6
) == 0) {
475 nd6log((LOG_DEBUG
, "%s: got interface identifier from %s\n",
476 if_name(ifp0
), if_name(altifp
)));
480 /* next, try to get it from some other hardware interface */
481 TAILQ_FOREACH(ifp
, &ifnet
, if_list
) {
484 if (in6_get_hw_ifid(ifp
, in6
) != 0)
488 * to borrow ifid from other interface, ifid needs to be
491 if (IFID_UNIVERSAL(in6
)) {
493 "%s: borrow interface identifier from %s\n",
494 if_name(ifp0
), if_name(ifp
)));
500 /* get from hostid - only for certain architectures */
501 if (get_hostid_ifid(ifp
, in6
) == 0) {
503 "%s: interface identifier generated by hostid\n",
509 /* last resort: get from random number source */
510 if (get_rand_ifid(ifp
, in6
) == 0) {
512 "%s: interface identifier generated by random number\n",
517 printf("%s: failed to get interface identifier\n", if_name(ifp0
));
521 nd6log((LOG_INFO
, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
522 if_name(ifp0
), in6
->s6_addr
[8], in6
->s6_addr
[9], in6
->s6_addr
[10],
523 in6
->s6_addr
[11], in6
->s6_addr
[12], in6
->s6_addr
[13],
524 in6
->s6_addr
[14], in6
->s6_addr
[15]));
529 * altifp - secondary EUI64 source
533 in6_ifattach_linklocal(struct ifnet
*ifp
, struct ifnet
*altifp
)
535 struct in6_ifaddr
*ia
;
536 struct in6_aliasreq ifra
;
537 struct nd_prefixctl pr0
;
541 * configure link-local address.
543 memset(&ifra
, 0, sizeof(ifra
));
546 * in6_update_ifa() does not use ifra_name, but we accurately set it
549 strncpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
));
551 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
552 ifra
.ifra_addr
.sin6_len
= sizeof(struct sockaddr_in6
);
553 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] = htonl(0xfe800000);
554 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] = 0;
555 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
556 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] = 0;
557 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] = htonl(1);
559 if (get_ifid(ifp
, altifp
, &ifra
.ifra_addr
.sin6_addr
) != 0) {
561 "%s: no ifid available\n", if_name(ifp
)));
565 if (in6_setscope(&ifra
.ifra_addr
.sin6_addr
, ifp
, NULL
))
568 sockaddr_in6_init(&ifra
.ifra_prefixmask
, &in6mask64
, 0, 0, 0);
569 /* link-local addresses should NEVER expire. */
570 ifra
.ifra_lifetime
.ia6t_vltime
= ND6_INFINITE_LIFETIME
;
571 ifra
.ifra_lifetime
.ia6t_pltime
= ND6_INFINITE_LIFETIME
;
574 * Now call in6_update_ifa() to do a bunch of procedures to configure
575 * a link-local address. We can set the 3rd argument to NULL, because
576 * we know there's no other link-local address on the interface
577 * and therefore we are adding one (instead of updating one).
579 if ((error
= in6_update_ifa(ifp
, &ifra
, NULL
,
580 IN6_IFAUPDATE_DADDELAY
)) != 0) {
582 * XXX: When the interface does not support IPv6, this call
583 * would fail in the SIOCINITIFADDR ioctl. I believe the
584 * notification is rather confusing in this case, so just
585 * suppress it. (jinmei@kame.net 20010130)
587 if (error
!= EAFNOSUPPORT
)
588 nd6log((LOG_NOTICE
, "in6_ifattach_linklocal: failed to "
589 "configure a link-local address on %s "
591 if_name(ifp
), error
));
595 ia
= in6ifa_ifpforlinklocal(ifp
, 0); /* ia must not be NULL */
598 panic("ia == NULL in in6_ifattach_linklocal");
604 * Make the link-local prefix (fe80::/64%link) as on-link.
605 * Since we'd like to manage prefixes separately from addresses,
606 * we make an ND6 prefix structure for the link-local prefix,
607 * and add it to the prefix list as a never-expire prefix.
608 * XXX: this change might affect some existing code base...
610 memset(&pr0
, 0, sizeof(pr0
));
612 /* this should be 64 at this moment. */
613 pr0
.ndpr_plen
= in6_mask2len(&ifra
.ifra_prefixmask
.sin6_addr
, NULL
);
614 pr0
.ndpr_prefix
= ifra
.ifra_addr
;
615 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
616 for (i
= 0; i
< 4; i
++) {
617 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
618 in6mask64
.s6_addr32
[i
];
621 * Initialize parameters. The link-local prefix must always be
622 * on-link, and its lifetimes never expire.
624 pr0
.ndpr_raf_onlink
= 1;
625 pr0
.ndpr_raf_auto
= 1; /* probably meaningless */
626 pr0
.ndpr_vltime
= ND6_INFINITE_LIFETIME
;
627 pr0
.ndpr_pltime
= ND6_INFINITE_LIFETIME
;
629 * Since there is no other link-local addresses, nd6_prefix_lookup()
630 * probably returns NULL. However, we cannot always expect the result.
631 * For example, if we first remove the (only) existing link-local
632 * address, and then reconfigure another one, the prefix is still
633 * valid with referring to the old link-local address.
635 if (nd6_prefix_lookup(&pr0
) == NULL
) {
636 if ((error
= nd6_prelist_add(&pr0
, NULL
, NULL
)) != 0)
644 * ifp - mut be IFT_LOOP
648 in6_ifattach_loopback(struct ifnet
*ifp
)
650 struct in6_aliasreq ifra
;
653 memset(&ifra
, 0, sizeof(ifra
));
656 * in6_update_ifa() does not use ifra_name, but we accurately set it
659 strncpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
));
661 sockaddr_in6_init(&ifra
.ifra_prefixmask
, &in6mask128
, 0, 0, 0);
664 * Always initialize ia_dstaddr (= broadcast address) to loopback
665 * address. Follows IPv4 practice - see in_ifinit().
667 sockaddr_in6_init(&ifra
.ifra_dstaddr
, &in6addr_loopback
, 0, 0, 0);
669 sockaddr_in6_init(&ifra
.ifra_addr
, &in6addr_loopback
, 0, 0, 0);
671 /* the loopback address should NEVER expire. */
672 ifra
.ifra_lifetime
.ia6t_vltime
= ND6_INFINITE_LIFETIME
;
673 ifra
.ifra_lifetime
.ia6t_pltime
= ND6_INFINITE_LIFETIME
;
675 /* we don't need to perform DAD on loopback interfaces. */
676 ifra
.ifra_flags
|= IN6_IFF_NODAD
;
679 * We are sure that this is a newly assigned address, so we can set
680 * NULL to the 3rd arg.
682 if ((error
= in6_update_ifa(ifp
, &ifra
, NULL
, 0)) != 0) {
683 nd6log((LOG_ERR
, "in6_ifattach_loopback: failed to configure "
684 "the loopback address on %s (errno=%d)\n",
685 if_name(ifp
), error
));
693 * compute NI group address, based on the current hostname setting.
694 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
696 * when ifp == NULL, the caller is responsible for filling scopeid.
699 in6_nigroup(struct ifnet
*ifp
, const char *name
, int namelen
,
700 struct sockaddr_in6
*sa6
)
707 u_int8_t n
[64]; /* a single label must not exceed 63 chars */
709 if (!namelen
|| !name
)
713 while (p
&& *p
&& *p
!= '.' && p
- name
< namelen
)
715 if (p
- name
> sizeof(n
) - 1)
716 return -1; /* label too long */
718 strncpy((char *)n
, name
, l
);
720 for (q
= n
; *q
; q
++) {
721 if ('A' <= *q
&& *q
<= 'Z')
725 /* generate 8 bytes of pseudo-random value. */
726 memset(&ctxt
, 0, sizeof(ctxt
));
728 MD5Update(&ctxt
, &l
, sizeof(l
));
729 MD5Update(&ctxt
, n
, l
);
730 MD5Final(digest
, &ctxt
);
732 memset(sa6
, 0, sizeof(*sa6
));
733 sa6
->sin6_family
= AF_INET6
;
734 sa6
->sin6_len
= sizeof(*sa6
);
735 sa6
->sin6_addr
.s6_addr16
[0] = htons(0xff02);
736 sa6
->sin6_addr
.s6_addr8
[11] = 2;
737 memcpy(&sa6
->sin6_addr
.s6_addr32
[3], digest
,
738 sizeof(sa6
->sin6_addr
.s6_addr32
[3]));
739 if (in6_setscope(&sa6
->sin6_addr
, ifp
, NULL
))
740 return -1; /* XXX: should not fail */
746 * XXX multiple loopback interface needs more care. for instance,
747 * nodelocal address needs to be configured onto only one of them.
748 * XXX multiple link-local address case
750 * altifp - secondary EUI64 source
753 in6_ifattach(struct ifnet
*ifp
, struct ifnet
*altifp
)
755 struct in6_ifaddr
*ia
;
758 /* some of the interfaces are inherently not IPv6 capable */
759 switch (ifp
->if_type
) {
771 * if link mtu is too small, don't try to configure IPv6.
772 * remember there could be some link-layer that has special
773 * fragmentation logic.
775 if (ifp
->if_mtu
< IPV6_MMTU
) {
776 nd6log((LOG_INFO
, "in6_ifattach: "
777 "%s has too small MTU, IPv6 not enabled\n",
782 /* create a multicast kludge storage (if we have not had one) */
783 in6_createmkludge(ifp
);
786 * quirks based on interface type
788 switch (ifp
->if_type
) {
792 * 6to4 interface is a very special kind of beast.
793 * no multicast, no linklocal. RFC2529 specifies how to make
794 * linklocals for 6to4 interface, but there's no use and
795 * it is rather harmful to have one.
806 * usually, we require multicast capability to the interface
808 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
809 nd6log((LOG_INFO
, "in6_ifattach: "
810 "%s is not multicast capable, IPv6 not enabled\n",
816 * assign loopback address for loopback interface.
817 * XXX multiple loopback interface case.
819 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
820 in6
= in6addr_loopback
;
821 if (in6ifa_ifpwithaddr(ifp
, &in6
) == NULL
) {
822 if (in6_ifattach_loopback(ifp
) != 0)
828 * assign a link-local address, if there's none.
830 if (ip6_auto_linklocal
) {
831 ia
= in6ifa_ifpforlinklocal(ifp
, 0);
832 if (ia
== NULL
&& in6_ifattach_linklocal(ifp
, altifp
) != 0) {
833 printf("%s: cannot assign link-local address\n",
840 * NOTE: in6_ifdetach() does not support loopback if at this moment.
841 * We don't need this function in bsdi, because interfaces are never removed
842 * from the ifnet list in bsdi.
845 in6_ifdetach(struct ifnet
*ifp
)
847 struct in6_ifaddr
*ia
, *oia
;
848 struct ifaddr
*ifa
, *next
;
851 struct in6_multi_mship
*imm
;
853 /* remove ip6_mrouter stuff */
854 ip6_mrouter_detach(ifp
);
856 /* remove neighbor management table */
859 /* XXX this code is duplicated in in6_purgeif() --dyoung */
860 /* nuke any of IPv6 addresses we have */
861 if_purgeaddrs(ifp
, AF_INET6
, in6_purgeaddr
);
863 /* XXX isn't this code is redundant, given the above? --dyoung */
864 /* XXX doesn't this code replicate code in in6_purgeaddr() ? --dyoung */
865 /* undo everything done by in6_ifattach(), just in case */
866 for (ifa
= IFADDR_FIRST(ifp
); ifa
!= NULL
; ifa
= next
) {
867 next
= IFADDR_NEXT(ifa
);
869 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
870 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa
->ifa_addr
)->sin6_addr
)) {
874 ia
= (struct in6_ifaddr
*)ifa
;
877 * leave from multicast groups we have joined for the interface
879 while ((imm
= LIST_FIRST(&ia
->ia6_memberships
)) != NULL
) {
880 LIST_REMOVE(imm
, i6mm_chain
);
884 /* remove from the routing table */
885 if ((ia
->ia_flags
& IFA_ROUTE
) &&
886 (rt
= rtalloc1((struct sockaddr
*)&ia
->ia_addr
, 0))) {
887 rtflags
= rt
->rt_flags
;
889 rtrequest(RTM_DELETE
, (struct sockaddr
*)&ia
->ia_addr
,
890 (struct sockaddr
*)&ia
->ia_addr
,
891 (struct sockaddr
*)&ia
->ia_prefixmask
,
892 rtflags
, (struct rtentry
**)0);
895 /* remove from the linked list */
896 ifa_remove(ifp
, &ia
->ia_ifa
);
898 /* also remove from the IPv6 address chain(itojun&jinmei) */
900 if (oia
== (ia
= in6_ifaddr
))
901 in6_ifaddr
= ia
->ia_next
;
903 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
906 ia
->ia_next
= oia
->ia_next
;
909 "%s: didn't unlink in6ifaddr from list\n",
914 IFAFREE(&oia
->ia_ifa
);
917 /* cleanup multicast address kludge table, if there is any */
918 in6_purgemkludge(ifp
);
921 * remove neighbor management table. we call it twice just to make
922 * sure we nuke everything. maybe we need just one call.
923 * XXX: since the first call did not release addresses, some prefixes
924 * might remain. We should call nd6_purge() again to release the
925 * prefixes after removing all addresses above.
926 * (Or can we just delay calling nd6_purge until at this point?)
932 in6_get_tmpifid(struct ifnet
*ifp
, u_int8_t
*retbuf
,
933 const u_int8_t
*baseid
, int generate
)
936 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
938 memset(nullbuf
, 0, sizeof(nullbuf
));
939 if (memcmp(ndi
->randomid
, nullbuf
, sizeof(nullbuf
)) == 0) {
940 /* we've never created a random ID. Create a new one. */
945 memcpy(ndi
->randomseed1
, baseid
, sizeof(ndi
->randomseed1
));
947 /* generate_tmp_ifid will update seedn and buf */
948 (void)generate_tmp_ifid(ndi
->randomseed0
, ndi
->randomseed1
,
951 memcpy(retbuf
, ndi
->randomid
, 8);
952 if (generate
&& memcmp(retbuf
, nullbuf
, sizeof(nullbuf
)) == 0) {
953 /* generate_tmp_ifid could not found a good ID. */
961 in6_tmpaddrtimer(void *ignored_arg
)
963 struct nd_ifinfo
*ndi
;
967 mutex_enter(softnet_lock
);
968 KERNEL_LOCK(1, NULL
);
970 callout_reset(&in6_tmpaddrtimer_ch
,
971 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
972 ip6_temp_regen_advance
) * hz
, in6_tmpaddrtimer
, NULL
);
974 memset(nullbuf
, 0, sizeof(nullbuf
));
975 TAILQ_FOREACH(ifp
, &ifnet
, if_list
) {
976 ndi
= ND_IFINFO(ifp
);
977 if (memcmp(ndi
->randomid
, nullbuf
, sizeof(nullbuf
)) != 0) {
979 * We've been generating a random ID on this interface.
982 (void)generate_tmp_ifid(ndi
->randomseed0
,
983 ndi
->randomseed1
, ndi
->randomid
);
987 KERNEL_UNLOCK_ONE(NULL
);
988 mutex_exit(softnet_lock
);