2 * Copyright (c) 2003-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/malloc.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/sockio.h>
64 #include <sys/kernel.h>
65 #include <sys/syslog.h>
66 #include <libkern/crypto/sha1.h>
67 #include <libkern/OSAtomic.h>
68 #include <kern/locks.h>
71 #include <net/if_dl.h>
72 #include <net/if_types.h>
73 #include <net/route.h>
74 #include <net/kpi_protocol.h>
76 #include <netinet/in.h>
77 #include <netinet/in_var.h>
78 #include <netinet/if_ether.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/icmp6.h>
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
84 #include <netinet6/in6_var.h>
85 #include <netinet6/in6_pcb.h>
86 #include <netinet6/in6_ifattach.h>
87 #include <netinet6/ip6_var.h>
88 #include <netinet6/nd6.h>
89 #include <netinet6/scope6_var.h>
91 #include <net/net_osdep.h>
92 #include <dev/random/randomdev.h>
94 u_int32_t in6_maxmtu
= 0;
95 extern lck_mtx_t
*nd6_mutex
;
97 #if IP6_AUTO_LINKLOCAL
98 int ip6_auto_linklocal
= IP6_AUTO_LINKLOCAL
;
100 int ip6_auto_linklocal
= 1; /* enable by default */
103 extern struct inpcbinfo udbinfo
;
104 extern struct inpcbinfo ripcbinfo
;
106 static const unsigned int in6_extra_size
= sizeof(struct in6_ifextra
);
107 static const unsigned int in6_extra_bufsize
= in6_extra_size
+
108 sizeof(void *) + sizeof(uint64_t);
110 static int get_rand_iid(struct ifnet
*, struct in6_addr
*);
111 static int in6_generate_tmp_iid(u_int8_t
*, const u_int8_t
*, u_int8_t
*);
112 static int in6_select_iid_from_all_hw(struct ifnet
*, struct ifnet
*,
114 static int in6_ifattach_linklocal(struct ifnet
*, struct in6_aliasreq
*);
115 static int in6_ifattach_loopback(struct ifnet
*);
118 * Generate a last-resort interface identifier, when the machine has no
119 * IEEE802/EUI64 address sources.
120 * The goal here is to get an interface identifier that is
121 * (1) random enough and (2) does not change across reboot.
122 * We currently use SHA1(hostname) for it.
124 * in6 - upper 64bits are preserved
128 __unused
struct ifnet
*ifp
,
129 struct in6_addr
*in6
) /* upper 64bits are preserved */
132 u_int8_t digest
[SHA1_RESULTLEN
];
133 int hostnlen
= strlen(hostname
);
135 /* generate 8 bytes of pseudo-random value. */
136 bzero(&ctxt
, sizeof (ctxt
));
138 SHA1Update(&ctxt
, hostname
, hostnlen
);
139 SHA1Final(digest
, &ctxt
);
141 /* assumes sizeof (digest) > sizeof (iid) */
142 bcopy(digest
, &in6
->s6_addr
[8], 8);
144 /* make sure to set "u" bit to local, and "g" bit to individual. */
145 in6
->s6_addr
[8] &= ~ND6_EUI64_GBIT
; /* g bit to "individual" */
146 in6
->s6_addr
[8] |= ND6_EUI64_UBIT
; /* u bit to "local" */
148 /* convert EUI64 into IPv6 interface identifier */
149 ND6_EUI64_TO_IFID(in6
);
155 in6_generate_tmp_iid(
157 const u_int8_t
*seed1
,
161 u_int8_t seed
[16], nullbuf
[8], digest
[SHA1_RESULTLEN
];
165 /* If there's no history, start with a random seed. */
166 bzero(nullbuf
, sizeof (nullbuf
));
167 if (bcmp(nullbuf
, seed0
, sizeof (nullbuf
)) == 0) {
170 for (i
= 0; i
< 2; i
++) {
172 val32
= RandomULong() ^ tv
.tv_usec
;
173 bcopy(&val32
, seed
+ sizeof (val32
) * i
,
177 bcopy(seed0
, seed
, 8);
180 /* copy the right-most 64-bits of the given address */
181 /* XXX assumption on the size of IFID */
182 bcopy(seed1
, &seed
[8], 8);
184 if ((0)) { /* for debugging purposes only */
187 printf("%s: new randomized ID from: ", __func__
);
188 for (i
= 0; i
< 16; i
++)
189 printf("%02x", seed
[i
]);
193 /* generate 16 bytes of pseudo-random value. */
194 bzero(&ctxt
, sizeof (ctxt
));
196 SHA1Update(&ctxt
, seed
, sizeof (seed
));
197 SHA1Final(digest
, &ctxt
);
200 * RFC 4941 3.2.1. (3)
201 * Take the left-most 64-bits of the SHA1 digest and set bit 6 (the
202 * left-most bit is numbered 0) to zero.
204 bcopy(digest
, ret
, 8);
205 ret
[0] &= ~ND6_EUI64_UBIT
;
208 * XXX: we'd like to ensure that the generated value is not zero
209 * for simplicity. If the caclculated digest happens to be zero,
210 * use a random non-zero value as the last resort.
212 if (bcmp(nullbuf
, ret
, sizeof (nullbuf
)) == 0) {
214 "%s: computed SHA1 value is zero.\n", __func__
));
217 val32
= random() ^ tv
.tv_usec
;
218 val32
= 1 + (val32
% (0xffffffff - 1));
222 * RFC 4941 3.2.1. (4)
223 * Take the next 64-bits of the SHA1 digest and save them in
224 * stable storage as the history value to be used in the next
225 * iteration of the algorithm.
227 bcopy(&digest
[8], seed0
, 8);
229 if ((0)) { /* for debugging purposes only */
233 for (i
= 0; i
< 16; i
++)
234 printf("%02x", digest
[i
]);
242 * Get interface identifier for the specified interface using the method in
243 * Appendix A of RFC 4291.
245 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
247 * in6 - upper 64bits are preserved
250 in6_iid_from_hw(struct ifnet
*ifp
, struct in6_addr
*in6
)
252 struct ifaddr
*ifa
= NULL
;
253 struct sockaddr_dl
*sdl
;
256 static u_int8_t allzero
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
257 static u_int8_t allone
[8] =
258 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
261 /* Why doesn't this code use ifnet_addrs? */
262 ifnet_lock_shared(ifp
);
263 ifa
= ifp
->if_lladdr
;
264 sdl
= (struct sockaddr_dl
*)(void *)ifa
->ifa_addr
;
265 if (sdl
->sdl_alen
== 0) {
266 ifnet_lock_done(ifp
);
269 IFA_ADDREF(ifa
); /* for this routine */
270 ifnet_lock_done(ifp
);
273 addr
= (u_int8_t
*) LLADDR(sdl
);
274 addrlen
= sdl
->sdl_alen
;
277 switch (ifp
->if_type
) {
284 case IFT_IEEE8023ADLAG
:
289 /* IEEE802/EUI64 cases - what others? */
290 /* IEEE1394 uses 16byte length address starting with EUI64 */
294 /* look at IEEE802/EUI64 only */
295 if (addrlen
!= 8 && addrlen
!= 6)
299 * check for invalid MAC address - on bsdi, we see it a lot
300 * since wildboar configures all-zero MAC on pccard before
303 if (bcmp(addr
, allzero
, addrlen
) == 0)
305 if (bcmp(addr
, allone
, addrlen
) == 0)
308 /* make EUI64 address */
310 bcopy(addr
, &in6
->s6_addr
[8], 8);
311 else if (addrlen
== 6) {
312 in6
->s6_addr
[8] = addr
[0];
313 in6
->s6_addr
[9] = addr
[1];
314 in6
->s6_addr
[10] = addr
[2];
315 in6
->s6_addr
[11] = 0xff;
316 in6
->s6_addr
[12] = 0xfe;
317 in6
->s6_addr
[13] = addr
[3];
318 in6
->s6_addr
[14] = addr
[4];
319 in6
->s6_addr
[15] = addr
[5];
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] &= ~ND6_EUI64_GBIT
; /* g to "individual" */
336 in6
->s6_addr
[8] |= ND6_EUI64_UBIT
; /* u to "local" */
344 * RFC2893 says: "SHOULD use IPv4 address as IID source".
345 * however, IPv4 address is not very suitable as unique
346 * identifier source (can be renumbered).
358 /* sanity check: g bit must not indicate "group" */
359 if (ND6_EUI64_GROUP(in6
))
362 /* convert EUI64 into IPv6 interface identifier */
363 ND6_EUI64_TO_IFID(in6
);
366 * sanity check: iid must not be all zero, avoid conflict with
367 * subnet router anycast
369 if ((in6
->s6_addr
[8] & ~(ND6_EUI64_GBIT
| ND6_EUI64_UBIT
)) == 0x00 &&
370 bcmp(&in6
->s6_addr
[9], allzero
, 7) == 0) {
377 /* This must not be the last reference to the lladdr */
378 if (IFA_REMREF_LOCKED(ifa
) == NULL
) {
379 panic("%s: unexpected (missing) refcnt ifa=%p", __func__
, ifa
);
387 * Get interface identifier for the specified interface using the method in
388 * Appendix A of RFC 4291. If it is not available on ifp0, borrow interface
389 * identifier from other information sources.
391 * ifp - primary EUI64 source
392 * altifp - secondary EUI64 source
393 * in6 - IPv6 address to output IID
396 in6_select_iid_from_all_hw(
398 struct ifnet
*altifp
, /* secondary EUI64 source */
399 struct in6_addr
*in6
)
403 /* first, try to get it from the interface itself */
404 if (in6_iid_from_hw(ifp0
, in6
) == 0) {
405 nd6log((LOG_DEBUG
, "%s: IID derived from HW interface.\n",
410 /* try secondary EUI64 source. this basically is for ATM PVC */
411 if (altifp
&& in6_iid_from_hw(altifp
, in6
) == 0) {
412 nd6log((LOG_DEBUG
, "%s: IID from alterate HW interface %s.\n",
413 if_name(ifp0
), if_name(altifp
)));
417 /* next, try to get it from some other hardware interface */
418 ifnet_head_lock_shared();
419 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
422 if (in6_iid_from_hw(ifp
, in6
) != 0)
426 * to borrow IID from other interface, IID needs to be
429 if (ND6_IFID_UNIVERSAL(in6
)) {
430 nd6log((LOG_DEBUG
, "%s: borrowed IID from %s\n",
431 if_name(ifp0
), if_name(ifp
)));
438 /* last resort: get from random number source */
439 if (get_rand_iid(ifp
, in6
) == 0) {
440 nd6log((LOG_DEBUG
, "%s: IID from PRNG.\n", if_name(ifp0
)));
444 printf("%s: failed to get interface identifier\n", if_name(ifp0
));
448 nd6log((LOG_INFO
, "%s: IID: "
449 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
451 in6
->s6_addr
[8], in6
->s6_addr
[9],
452 in6
->s6_addr
[10], in6
->s6_addr
[11],
453 in6
->s6_addr
[12], in6
->s6_addr
[13],
454 in6
->s6_addr
[14], in6
->s6_addr
[15]));
459 in6_ifattach_linklocal(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
)
461 struct in6_ifaddr
*ia
;
462 struct nd_prefix pr0
, *pr
;
465 VERIFY(ifra
!= NULL
);
467 proto_plumb(PF_INET6
, ifp
);
469 error
= in6_update_ifa(ifp
, ifra
, IN6_IFAUPDATE_DADDELAY
, &ia
);
472 * XXX: When the interface does not support IPv6, this call
473 * would fail in the SIOCSIFADDR ioctl. I believe the
474 * notification is rather confusing in this case, so just
475 * suppress it. (jinmei@kame.net 20010130)
477 if (error
!= EAFNOSUPPORT
)
478 nd6log((LOG_NOTICE
, "%s: failed to "
479 "configure a link-local address on %s "
481 __func__
, if_name(ifp
), error
));
482 return (EADDRNOTAVAIL
);
487 * Make the link-local prefix (fe80::%link/64) as on-link.
488 * Since we'd like to manage prefixes separately from addresses,
489 * we make an ND6 prefix structure for the link-local prefix,
490 * and add it to the prefix list as a never-expire prefix.
491 * XXX: this change might affect some existing code base...
493 bzero(&pr0
, sizeof (pr0
));
494 lck_mtx_init(&pr0
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
496 /* this should be 64 at this moment. */
497 pr0
.ndpr_plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
, NULL
);
498 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
499 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
500 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
501 for (i
= 0; i
< 4; i
++) {
502 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
503 in6mask64
.s6_addr32
[i
];
506 * Initialize parameters. The link-local prefix must always be
507 * on-link, and its lifetimes never expire.
509 pr0
.ndpr_raf_onlink
= 1;
510 pr0
.ndpr_raf_auto
= 1; /* probably meaningless */
511 pr0
.ndpr_vltime
= ND6_INFINITE_LIFETIME
;
512 pr0
.ndpr_pltime
= ND6_INFINITE_LIFETIME
;
513 pr0
.ndpr_stateflags
|= NDPRF_STATIC
;
515 * Since there is no other link-local addresses, nd6_prefix_lookup()
516 * probably returns NULL. However, we cannot always expect the result.
517 * For example, if we first remove the (only) existing link-local
518 * address, and then reconfigure another one, the prefix is still
519 * valid with referring to the old link-local address.
521 if ((pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_UNSPEC
)) == NULL
) {
522 if ((error
= nd6_prelist_add(&pr0
, NULL
, &pr
, TRUE
)) != 0) {
523 IFA_REMREF(&ia
->ia_ifa
);
524 lck_mtx_destroy(&pr0
.ndpr_lock
, ifa_mtx_grp
);
529 in6_post_msg(ifp
, KEV_INET6_NEW_LL_ADDR
, ia
, NULL
);
530 IFA_REMREF(&ia
->ia_ifa
);
532 /* Drop use count held above during lookup/add */
536 lck_mtx_destroy(&pr0
.ndpr_lock
, ifa_mtx_grp
);
541 in6_ifattach_loopback(
542 struct ifnet
*ifp
) /* must be IFT_LOOP */
544 struct in6_aliasreq ifra
;
545 struct in6_ifaddr
*ia
;
548 bzero(&ifra
, sizeof (ifra
));
551 * in6_update_ifa() does not use ifra_name, but we accurately set it
554 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
556 ifra
.ifra_prefixmask
.sin6_len
= sizeof (struct sockaddr_in6
);
557 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
558 ifra
.ifra_prefixmask
.sin6_addr
= 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 ifra
.ifra_addr
.sin6_len
= sizeof (struct sockaddr_in6
);
569 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
570 ifra
.ifra_addr
.sin6_addr
= in6addr_loopback
;
572 /* the loopback address should NEVER expire. */
573 ifra
.ifra_lifetime
.ia6t_vltime
= ND6_INFINITE_LIFETIME
;
574 ifra
.ifra_lifetime
.ia6t_pltime
= ND6_INFINITE_LIFETIME
;
576 /* we don't need to perform DAD on loopback interfaces. */
577 ifra
.ifra_flags
|= IN6_IFF_NODAD
;
579 /* add the new interface address */
580 error
= in6_update_ifa(ifp
, &ifra
, 0, &ia
);
583 "%s: failed to configure loopback address %s (error=%d)\n",
584 __func__
, if_name(ifp
), error
));
586 return (EADDRNOTAVAIL
);
590 IFA_REMREF(&ia
->ia_ifa
);
595 * compute NI group address, based on the current hostname setting.
598 * when ifp == NULL, the caller is responsible for filling scopeid.
605 struct in6_addr
*in6
)
610 u_int8_t digest
[SHA1_RESULTLEN
];
612 char n
[64]; /* a single label must not exceed 63 chars */
614 if (!namelen
|| !name
)
618 while (p
&& *p
&& *p
!= '.' && p
- name
< namelen
)
620 if (p
- name
> sizeof (n
) - 1)
621 return (-1); /* label too long */
625 for (q
= (u_char
*) n
; *q
; q
++) {
626 if ('A' <= *q
&& *q
<= 'Z')
630 /* generate 16 bytes of pseudo-random value. */
631 bzero(&ctxt
, sizeof (ctxt
));
633 SHA1Update(&ctxt
, &l
, sizeof (l
));
634 SHA1Update(&ctxt
, n
, l
);
635 SHA1Final(digest
, &ctxt
);
637 bzero(in6
, sizeof (*in6
));
638 in6
->s6_addr16
[0] = IPV6_ADDR_INT16_MLL
;
639 in6
->s6_addr8
[11] = 2;
640 in6
->s6_addr8
[12] = 0xff;
641 /* copy first 3 bytes of prefix into address */
642 bcopy(digest
, &in6
->s6_addr8
[13], 3);
643 if (in6_setscope(in6
, ifp
, NULL
))
644 return (-1); /* XXX: should not fail */
650 in6_domifattach(struct ifnet
*ifp
)
656 error
= proto_plumb(PF_INET6
, ifp
);
659 log(LOG_ERR
, "%s: proto_plumb returned %d if=%s\n",
660 __func__
, error
, if_name(ifp
));
662 error
= in6_ifattach_prelim(ifp
);
667 "%s: in6_ifattach_prelim returned %d if=%s%d\n",
668 __func__
, error
, ifp
->if_name
, ifp
->if_unit
);
670 errorx
= proto_unplumb(PF_INET6
, ifp
);
671 if (errorx
!= 0) /* XXX should not fail */
673 "%s: proto_unplumb returned %d if=%s%d\n",
674 __func__
, errorx
, ifp
->if_name
,
683 in6_ifattach_prelim(struct ifnet
*ifp
)
685 struct in6_ifextra
*ext
;
688 struct in6_ifaddr
*ia6
= NULL
;
692 /* quirks based on interface type */
693 switch (ifp
->if_type
) {
697 * 6to4 interface is a very special kind of beast.
698 * no multicast, no linklocal. RFC2529 specifies how to make
699 * linklocals for 6to4 interface, but there's no use and
700 * it is rather harmful to have one.
709 * IPv6 requires multicast capability at the interface.
710 * (previously, this was a silent error.)
712 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
713 nd6log((LOG_INFO
, "in6_ifattach: ",
714 "%s is not multicast capable, IPv6 not enabled\n",
723 if (ifp
->if_inet6data
== NULL
) {
724 ext
= (struct in6_ifextra
*)_MALLOC(in6_extra_bufsize
, M_IFADDR
,
728 base
= (void *)P2ROUNDUP((intptr_t)ext
+ sizeof(uint64_t),
730 VERIFY(((intptr_t)base
+ in6_extra_size
) <=
731 ((intptr_t)ext
+ in6_extra_bufsize
));
732 pbuf
= (void **)((intptr_t)base
- sizeof(void *));
734 ifp
->if_inet6data
= base
;
735 VERIFY(IS_P2ALIGNED(ifp
->if_inet6data
, sizeof(uint64_t)));
738 * Since the structure is never freed, we need to zero out
739 * some of its members. We avoid zeroing out the scope6
740 * structure on purpose because other threads might be
741 * using its contents.
743 bzero(&IN6_IFEXTRA(ifp
)->icmp6_ifstat
,
744 sizeof(IN6_IFEXTRA(ifp
)->icmp6_ifstat
));
745 bzero(&IN6_IFEXTRA(ifp
)->in6_ifstat
,
746 sizeof(IN6_IFEXTRA(ifp
)->in6_ifstat
));
748 * XXX When recycling, nd_ifinfo gets initialized, other
749 * than the lock, inside nd6_ifattach
754 * XXX Only initialize NDP ifinfo for the interface
755 * if interface has not yet been configured with
756 * link local IPv6 address.
757 * Could possibly be optimized with an interface flag if need
758 * be. For now using in6ifa_ifpforlinklocal.
760 ia6
= in6ifa_ifpforlinklocal(ifp
, 0);
762 /* initialize NDP variables */
765 VERIFY(ND_IFINFO(ifp
)->initialized
);
766 IFA_REMREF(&ia6
->ia_ifa
);
769 scope6_ifattach(ifp
);
771 /* initialize loopback interface address */
772 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
773 error
= in6_ifattach_loopback(ifp
);
775 log(LOG_ERR
, "%s: in6_ifattach_loopback returned %d\n",
776 __func__
, error
, ifp
->if_name
,
782 /* update dynamically. */
783 if (in6_maxmtu
< ifp
->if_mtu
)
784 in6_maxmtu
= ifp
->if_mtu
;
791 in6_ifattach_aliasreq(struct ifnet
*ifp
, struct ifnet
*altifp
,
792 struct in6_aliasreq
*ifra0
)
795 struct in6_ifaddr
*ia6
;
796 struct in6_aliasreq ifra
;
798 error
= in6_ifattach_prelim(ifp
);
802 if (!ip6_auto_linklocal
)
805 /* assign a link-local address, only if there isn't one here already. */
806 ia6
= in6ifa_ifpforlinklocal(ifp
, 0);
808 IFA_REMREF(&ia6
->ia_ifa
);
812 bzero(&ifra
, sizeof (ifra
));
815 * in6_update_ifa() does not use ifra_name, but we accurately set it
818 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
820 /* Initialize the IPv6 interface address in our in6_aliasreq block */
821 if ((ifp
->if_eflags
& IFEF_NOAUTOIPV6LL
) != 0 && ifra0
!= NULL
) {
822 /* interface provided both addresses for us */
823 struct sockaddr_in6
*sin6
= &ifra
.ifra_addr
;
824 struct in6_addr
*in6
= &sin6
->sin6_addr
;
827 bcopy(&ifra0
->ifra_addr
, sin6
, sizeof (struct sockaddr_in6
));
829 if (sin6
->sin6_family
!= AF_INET6
|| sin6
->sin6_port
!= 0)
831 if (ok
&& (in6
->s6_addr16
[0] != htons(0xfe80)))
834 if (sin6
->sin6_scope_id
== 0 && in6
->s6_addr16
[1] == 0)
835 in6
->s6_addr16
[1] = htons(ifp
->if_index
);
836 else if (sin6
->sin6_scope_id
!= 0 &&
837 sin6
->sin6_scope_id
!= ifp
->if_index
)
839 else if (in6
->s6_addr16
[1] != 0 &&
840 ntohs(in6
->s6_addr16
[1]) != ifp
->if_index
)
843 if (ok
&& (in6
->s6_addr32
[1] != 0))
848 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
849 ifra
.ifra_addr
.sin6_len
= sizeof (struct sockaddr_in6
);
850 ifra
.ifra_addr
.sin6_addr
.s6_addr16
[0] = htons(0xfe80);
851 ifra
.ifra_addr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
852 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] = 0;
853 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
854 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] = 0;
855 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] = htonl(1);
857 if (in6_select_iid_from_all_hw(ifp
, altifp
,
858 &ifra
.ifra_addr
.sin6_addr
) != 0) {
859 nd6log((LOG_ERR
, "%s: no IID available\n",
861 return (EADDRNOTAVAIL
);
866 if (in6_setscope(&ifra
.ifra_addr
.sin6_addr
, ifp
, NULL
))
867 return (EADDRNOTAVAIL
);
869 /* Set the prefix mask */
870 ifra
.ifra_prefixmask
.sin6_len
= sizeof (struct sockaddr_in6
);
871 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
872 ifra
.ifra_prefixmask
.sin6_addr
= in6mask64
;
874 /* link-local addresses should NEVER expire. */
875 ifra
.ifra_lifetime
.ia6t_vltime
= ND6_INFINITE_LIFETIME
;
876 ifra
.ifra_lifetime
.ia6t_pltime
= ND6_INFINITE_LIFETIME
;
878 /* Attach the link-local address */
879 if (in6_ifattach_linklocal(ifp
, &ifra
) != 0) {
881 "%s: %s could not attach link-local address.\n",
882 __func__
, if_name(ifp
)));
883 /* NB: not an error */
890 in6_ifattach_llstartreq(struct ifnet
*ifp
, struct in6_llstartreq
*llsr
)
892 struct in6_aliasreq ifra
;
893 struct in6_ifaddr
*ia6
= NULL
;
894 struct nd_ifinfo
*ndi
= NULL
;
897 VERIFY(llsr
!= NULL
);
899 error
= in6_ifattach_prelim(ifp
);
903 if (!ip6_auto_linklocal
|| (ifp
->if_eflags
& IFEF_NOAUTOIPV6LL
) != 0)
906 if (nd6_send_opstate
== ND6_SEND_OPMODE_DISABLED
)
909 ndi
= ND_IFINFO(ifp
);
910 VERIFY(ndi
!= NULL
&& ndi
->initialized
);
911 if ((ndi
->flags
& ND6_IFF_INSECURE
) != 0) {
915 /* assign a link-local address, only if there isn't one here already. */
916 ia6
= in6ifa_ifpforlinklocal(ifp
, 0);
918 IFA_REMREF(&ia6
->ia_ifa
);
922 bzero(&ifra
, sizeof (ifra
));
923 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
925 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
926 ifra
.ifra_addr
.sin6_len
= sizeof (struct sockaddr_in6
);
927 ifra
.ifra_addr
.sin6_addr
.s6_addr16
[0] = htons(0xfe80);
928 ifra
.ifra_addr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
929 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] = 0;
930 ifra
.ifra_flags
= IN6_IFF_SECURED
;
933 if (in6_cga_generate(&llsr
->llsr_cgaprep
, 0,
934 &ifra
.ifra_addr
.sin6_addr
)) {
935 in6_cga_node_unlock();
936 return (EADDRNOTAVAIL
);
938 in6_cga_node_unlock();
940 if (in6_setscope(&ifra
.ifra_addr
.sin6_addr
, ifp
, NULL
))
941 return (EADDRNOTAVAIL
);
943 /* Set the prefix mask */
944 ifra
.ifra_prefixmask
.sin6_len
= sizeof (struct sockaddr_in6
);
945 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
946 ifra
.ifra_prefixmask
.sin6_addr
= in6mask64
;
949 * link-local addresses should NEVER expire, but cryptographic
950 * ones may have finite preferred lifetime [if it's important to
951 * keep them from being used by applications as persistent device
954 ifra
.ifra_lifetime
.ia6t_vltime
= ND6_INFINITE_LIFETIME
;
955 ifra
.ifra_lifetime
.ia6t_pltime
= llsr
->llsr_lifetime
.ia6t_pltime
;
957 /* Attach the link-local address */
958 if (in6_ifattach_linklocal(ifp
, &ifra
) != 0) {
959 /* NB: not an error */
961 "%s: %s could not attach link-local address.\n",
962 __func__
, if_name(ifp
)));
970 * NOTE: in6_ifdetach() does not support loopback if at this moment.
973 in6_ifdetach(struct ifnet
*ifp
)
975 struct in6_ifaddr
*ia
, *oia
;
978 struct sockaddr_in6 sin6
;
979 struct in6_multi_mship
*imm
;
982 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
984 /* remove neighbor management table */
987 /* nuke any of IPv6 addresses we have */
988 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
991 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
995 IFA_ADDREF(&ia
->ia_ifa
); /* for us */
996 lck_rw_done(&in6_ifaddr_rwlock
);
997 in6_purgeaddr(&ia
->ia_ifa
);
998 IFA_REMREF(&ia
->ia_ifa
); /* for us */
999 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1001 * Purging the address caused in6_ifaddr_rwlock
1002 * to be dropped and reacquired;
1003 * therefore search again from the beginning
1004 * of in6_ifaddrs list.
1008 lck_rw_done(&in6_ifaddr_rwlock
);
1010 ifnet_lock_exclusive(ifp
);
1012 /* undo everything done by in6_ifattach(), just in case */
1013 ifa
= TAILQ_FIRST(&ifp
->if_addrlist
);
1014 while (ifa
!= NULL
) {
1016 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
||
1017 !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa
->ifa_addr
)->
1020 ifa
= TAILQ_NEXT(ifa
, ifa_list
);
1024 ia
= (struct in6_ifaddr
*)ifa
;
1026 /* hold a reference for this routine */
1027 IFA_ADDREF_LOCKED(ifa
);
1028 /* remove from the linked list */
1029 if_detach_ifa(ifp
, ifa
);
1033 * Leaving the multicast group(s) may involve freeing the
1034 * link address multicast structure(s) for the interface,
1035 * which is protected by ifnet lock. To avoid violating
1036 * lock ordering, we must drop ifnet lock before doing so.
1037 * The ifa won't go away since we held a refcnt above.
1039 ifnet_lock_done(ifp
);
1042 * We have to do this work manually here instead of calling
1043 * in6_purgeaddr() since in6_purgeaddr() uses the RTM_HOST flag.
1047 * leave from multicast groups we have joined for the interface
1050 while ((imm
= ia
->ia6_memberships
.lh_first
) != NULL
) {
1051 LIST_REMOVE(imm
, i6mm_chain
);
1053 in6_leavegroup(imm
);
1057 /* remove from the routing table */
1058 if (ia
->ia_flags
& IFA_ROUTE
) {
1060 rt
= rtalloc1((struct sockaddr
*)&ia
->ia_addr
, 0, 0);
1062 (void) rtrequest(RTM_DELETE
,
1063 (struct sockaddr
*)&ia
->ia_addr
,
1064 (struct sockaddr
*)&ia
->ia_addr
,
1065 (struct sockaddr
*)&ia
->ia_prefixmask
,
1066 rt
->rt_flags
, (struct rtentry
**)0);
1073 /* also remove from the IPv6 address chain(itojun&jinmei) */
1076 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1077 if (oia
== (ia
= in6_ifaddrs
)) {
1078 in6_ifaddrs
= ia
->ia_next
;
1080 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
1083 ia
->ia_next
= oia
->ia_next
;
1086 "%s: didn't unlink in6ifaddr from "
1087 "list\n", if_name(ifp
)));
1091 lck_rw_done(&in6_ifaddr_rwlock
);
1095 * release another refcnt for the link from in6_ifaddrs.
1096 * Do this only if it's not already unlinked in the event
1097 * that we lost the race, since in6_ifaddr_rwlock was
1098 * momentarily dropped above.
1102 /* release reference held for this routine */
1106 * This is suboptimal, but since we dropped ifnet lock above
1107 * the list might have changed. Repeat the search from the
1108 * beginning until we find the first eligible IPv6 address.
1110 ifnet_lock_exclusive(ifp
);
1111 ifa
= TAILQ_FIRST(&ifp
->if_addrlist
);
1113 ifnet_lock_done(ifp
);
1115 /* invalidate route caches */
1116 routegenid_inet6_update();
1119 * remove neighbor management table. we call it twice just to make
1120 * sure we nuke everything. maybe we need just one call.
1121 * XXX: since the first call did not release addresses, some prefixes
1122 * might remain. We should call nd6_purge() again to release the
1123 * prefixes after removing all addresses above.
1124 * (Or can we just delay calling nd6_purge until at this point?)
1128 /* remove route to link-local allnodes multicast (ff02::1) */
1129 bzero(&sin6
, sizeof (sin6
));
1130 sin6
.sin6_len
= sizeof (struct sockaddr_in6
);
1131 sin6
.sin6_family
= AF_INET6
;
1132 sin6
.sin6_addr
= in6addr_linklocal_allnodes
;
1133 sin6
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
1134 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
1137 if (rt
->rt_ifp
== ifp
) {
1139 * Prevent another thread from modifying rt_key,
1140 * rt_gateway via rt_setgate() after the rt_lock
1141 * is dropped by marking the route as defunct.
1143 rt
->rt_flags
|= RTF_CONDEMNED
;
1145 (void) rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1146 rt_mask(rt
), rt
->rt_flags
, 0);
1155 in6_iid_mktmp(struct ifnet
*ifp
, u_int8_t
*retbuf
, const u_int8_t
*baseid
,
1158 u_int8_t nullbuf
[8];
1159 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
1161 VERIFY(ndi
!= NULL
&& ndi
->initialized
);
1162 lck_mtx_lock(&ndi
->lock
);
1163 bzero(nullbuf
, sizeof (nullbuf
));
1164 if (bcmp(ndi
->randomid
, nullbuf
, sizeof (nullbuf
)) == 0) {
1165 /* we've never created a random ID. Create a new one. */
1170 bcopy(baseid
, ndi
->randomseed1
, sizeof (ndi
->randomseed1
));
1172 /* in6_generate_tmp_iid will update seedn and buf */
1173 (void) in6_generate_tmp_iid(ndi
->randomseed0
, ndi
->randomseed1
,
1177 bcopy(ndi
->randomid
, retbuf
, 8);
1178 lck_mtx_unlock(&ndi
->lock
);
1182 in6_tmpaddrtimer(void *arg
)
1185 struct ifnet
*ifp
= NULL
;
1186 struct nd_ifinfo
*ndi
= NULL
;
1187 u_int8_t nullbuf
[8];
1189 timeout(in6_tmpaddrtimer
, (caddr_t
)0, (ip6_temp_preferred_lifetime
-
1190 ip6_desync_factor
- ip6_temp_regen_advance
) * hz
);
1192 bzero(nullbuf
, sizeof (nullbuf
));
1193 ifnet_head_lock_shared();
1194 for (ifp
= ifnet_head
.tqh_first
; ifp
;
1195 ifp
= ifp
->if_link
.tqe_next
) {
1196 ndi
= ND_IFINFO(ifp
);
1197 if ((NULL
== ndi
) || (FALSE
== ndi
->initialized
)) {
1200 lck_mtx_lock(&ndi
->lock
);
1201 if (bcmp(ndi
->randomid
, nullbuf
, sizeof (nullbuf
)) != 0) {
1203 * We've been generating a random ID on this interface.
1206 (void) in6_generate_tmp_iid(ndi
->randomseed0
,
1207 ndi
->randomseed1
, ndi
->randomid
);
1209 lck_mtx_unlock(&ndi
->lock
);