2 * Copyright (c) 2003-2008 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@
28 /* $FreeBSD: src/sys/netinet6/in6_ifattach.c,v 1.8 2002/04/19 04:46:22 suz Exp $ */
29 /* $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $ */
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/malloc.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sockio.h>
66 #include <sys/kernel.h>
67 #include <sys/syslog.h>
68 #include <libkern/crypto/md5.h>
69 #include <libkern/OSAtomic.h>
70 #include <kern/lock.h>
73 #include <net/if_dl.h>
74 #include <net/if_types.h>
75 #include <net/route.h>
76 #include <net/kpi_protocol.h>
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 #include <netinet/if_ether.h>
81 #include <netinet/in_pcb.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet6/in6_var.h>
86 #include <netinet6/in6_pcb.h>
87 #include <netinet6/in6_ifattach.h>
88 #include <netinet6/ip6_var.h>
89 #include <netinet6/nd6.h>
90 #include <netinet6/scope6_var.h>
92 #include <net/net_osdep.h>
94 struct in6_ifstat
**in6_ifstat
= NULL
;
95 struct icmp6_ifstat
**icmp6_ifstat
= NULL
;
96 size_t in6_ifstatmax
= 0;
97 size_t icmp6_ifstatmax
= 0;
98 u_int32_t in6_maxmtu
= 0;
99 extern lck_mtx_t
*nd6_mutex
;
100 extern lck_mtx_t
*inet6_domain_mutex
;
102 #if IP6_AUTO_LINKLOCAL
103 int ip6_auto_linklocal
= IP6_AUTO_LINKLOCAL
;
105 int ip6_auto_linklocal
= 1; /* enable by default */
108 extern struct inpcbinfo udbinfo
;
109 extern struct inpcbinfo ripcbinfo
;
110 extern lck_mtx_t
*ip6_mutex
;
112 static int get_rand_ifid(struct ifnet
*, struct in6_addr
*);
113 static int generate_tmp_ifid(u_int8_t
*, const u_int8_t
*, u_int8_t
*);
114 static int get_hw_ifid(struct ifnet
*, struct in6_addr
*);
115 static int get_ifid(struct ifnet
*, struct ifnet
*, struct in6_addr
*);
116 static int in6_ifattach_linklocal(struct ifnet
*, struct ifnet
*, struct in6_aliasreq
*);
117 static int in6_ifattach_loopback(struct ifnet
*);
119 #define EUI64_GBIT 0x01
120 #define EUI64_UBIT 0x02
121 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
122 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
123 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
124 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
125 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
127 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
128 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
131 * Generate a last-resort interface identifier, when the machine has no
132 * IEEE802/EUI64 address sources.
133 * The goal here is to get an interface identifier that is
134 * (1) random enough and (2) does not change across reboot.
135 * We currently use MD5(hostname) for it.
139 __unused
struct ifnet
*ifp
,
140 struct in6_addr
*in6
) /* upper 64bits are preserved */
144 int len
= strlen(hostname
);
147 /* we need at least several letters as seed for ifid */
152 /* generate 8 bytes of pseudo-random value. */
153 bzero(&ctxt
, sizeof(ctxt
));
155 MD5Update(&ctxt
, hostname
, len
);
156 MD5Final(digest
, &ctxt
);
158 /* assumes sizeof(digest) > sizeof(ifid) */
159 bcopy(digest
, &in6
->s6_addr
[8], 8);
161 /* make sure to set "u" bit to local, and "g" bit to individual. */
162 in6
->s6_addr
[8] &= ~EUI64_GBIT
; /* g bit to "individual" */
163 in6
->s6_addr
[8] |= EUI64_UBIT
; /* u bit to "local" */
165 /* convert EUI64 into IPv6 interface identifier */
174 const u_int8_t
*seed1
,
178 u_int8_t seed
[16], digest
[16], nullbuf
[8];
182 /* If there's no hisotry, start with a random seed. */
183 bzero(nullbuf
, sizeof(nullbuf
));
184 if (bcmp(nullbuf
, seed0
, sizeof(nullbuf
)) == 0) {
187 for (i
= 0; i
< 2; i
++) {
189 val32
= random() ^ tv
.tv_usec
;
190 bcopy(&val32
, seed
+ sizeof(val32
) * i
, sizeof(val32
));
193 bcopy(seed0
, seed
, 8);
196 /* copy the right-most 64-bits of the given address */
197 /* XXX assumption on the size of IFID */
198 bcopy(seed1
, &seed
[8], 8);
200 if (0) { /* for debugging purposes only */
203 printf("generate_tmp_ifid: new randomized ID from: ");
204 for (i
= 0; i
< 16; i
++)
205 printf("%02x", seed
[i
]);
209 /* generate 16 bytes of pseudo-random value. */
210 bzero(&ctxt
, sizeof(ctxt
));
212 MD5Update(&ctxt
, seed
, sizeof(seed
));
213 MD5Final(digest
, &ctxt
);
216 * RFC 3041 3.2.1. (3)
217 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
218 * left-most bit is numbered 0) to zero.
220 bcopy(digest
, ret
, 8);
221 ret
[0] &= ~EUI64_UBIT
;
224 * XXX: we'd like to ensure that the generated value is not zero
225 * for simplicity. If the caclculated digest happens to be zero,
226 * use a random non-zero value as the last resort.
228 if (bcmp(nullbuf
, ret
, sizeof(nullbuf
)) == 0) {
230 "generate_tmp_ifid: computed MD5 value is zero.\n");
233 val32
= random() ^ tv
.tv_usec
;
234 val32
= 1 + (val32
% (0xffffffff - 1));
238 * RFC 3041 3.2.1. (4)
239 * Take the rightmost 64-bits of the MD5 digest and save them in
240 * stable storage as the history value to be used in the next
241 * iteration of the algorithm.
243 bcopy(&digest
[8], seed0
, 8);
245 if (0) { /* for debugging purposes only */
249 for (i
= 0; i
< 16; i
++)
250 printf("%02x", digest
[i
]);
258 * Get interface identifier for the specified interface.
259 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
264 struct in6_addr
*in6
) /* upper 64bits are preserved */
267 struct sockaddr_dl
*sdl
;
270 static u_int8_t allzero
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
271 static u_int8_t allone
[8] =
272 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
274 /* Why doesn't this code use ifnet_addrs? */
275 ifnet_lock_shared(ifp
);
276 for (ifa
= ifp
->if_addrlist
.tqh_first
;
278 ifa
= ifa
->ifa_list
.tqe_next
)
280 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
282 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
285 if (sdl
->sdl_alen
== 0)
290 ifnet_lock_done(ifp
);
295 ifnet_lock_done(ifp
);
296 addr
= (u_int8_t
*) LLADDR(sdl
);
297 addrlen
= sdl
->sdl_alen
;
300 switch (ifp
->if_type
) {
306 case IFT_IEEE8023ADLAG
:
311 /* IEEE802/EUI64 cases - what others? */
312 /* IEEE1394 uses 16byte length address starting with EUI64 */
316 /* look at IEEE802/EUI64 only */
317 if (addrlen
!= 8 && addrlen
!= 6)
321 * check for invalid MAC address - on bsdi, we see it a lot
322 * since wildboar configures all-zero MAC on pccard before
325 if (bcmp(addr
, allzero
, addrlen
) == 0)
327 if (bcmp(addr
, allone
, addrlen
) == 0)
330 /* make EUI64 address */
332 bcopy(addr
, &in6
->s6_addr
[8], 8);
333 else if (addrlen
== 6) {
334 in6
->s6_addr
[8] = addr
[0];
335 in6
->s6_addr
[9] = addr
[1];
336 in6
->s6_addr
[10] = addr
[2];
337 in6
->s6_addr
[11] = 0xff;
338 in6
->s6_addr
[12] = 0xfe;
339 in6
->s6_addr
[13] = addr
[3];
340 in6
->s6_addr
[14] = addr
[4];
341 in6
->s6_addr
[15] = addr
[5];
351 bzero(&in6
->s6_addr
[8], 8);
352 in6
->s6_addr
[15] = addr
[0];
355 * due to insufficient bitwidth, we mark it local.
357 in6
->s6_addr
[8] &= ~EUI64_GBIT
; /* g bit to "individual" */
358 in6
->s6_addr
[8] |= EUI64_UBIT
; /* u bit to "local" */
366 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
367 * however, IPv4 address is not very suitable as unique
368 * identifier source (can be renumbered).
377 /* sanity check: g bit must not indicate "group" */
378 if (EUI64_GROUP(in6
))
381 /* convert EUI64 into IPv6 interface identifier */
385 * sanity check: ifid must not be all zero, avoid conflict with
386 * subnet router anycast
388 if ((in6
->s6_addr
[8] & ~(EUI64_GBIT
| EUI64_UBIT
)) == 0x00 &&
389 bcmp(&in6
->s6_addr
[9], allzero
, 7) == 0) {
397 * Get interface identifier for the specified interface. If it is not
398 * available on ifp0, borrow interface identifier from other information
404 struct ifnet
*altifp
, /* secondary EUI64 source */
405 struct in6_addr
*in6
)
409 /* first, try to get it from the interface itself */
410 if (get_hw_ifid(ifp0
, in6
) == 0) {
411 nd6log((LOG_DEBUG
, "%s: got interface identifier from itself\n",
416 /* try secondary EUI64 source. this basically is for ATM PVC */
417 if (altifp
&& get_hw_ifid(altifp
, in6
) == 0) {
418 nd6log((LOG_DEBUG
, "%s: got interface identifier from %s\n",
419 if_name(ifp0
), if_name(altifp
)));
423 /* next, try to get it from some other hardware interface */
424 ifnet_head_lock_shared();
425 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
428 if (get_hw_ifid(ifp
, in6
) != 0)
432 * to borrow ifid from other interface, ifid needs to be
435 if (IFID_UNIVERSAL(in6
)) {
437 "%s: borrow interface identifier from %s\n",
438 if_name(ifp0
), if_name(ifp
)));
445 /* last resort: get from random number source */
446 if (get_rand_ifid(ifp
, in6
) == 0) {
448 "%s: interface identifier generated by random number\n",
453 printf("%s: failed to get interface identifier\n", if_name(ifp0
));
457 nd6log((LOG_INFO
, "%s: ifid: "
458 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
460 in6
->s6_addr
[8], in6
->s6_addr
[9],
461 in6
->s6_addr
[10], in6
->s6_addr
[11],
462 in6
->s6_addr
[12], in6
->s6_addr
[13],
463 in6
->s6_addr
[14], in6
->s6_addr
[15]));
468 in6_ifattach_linklocal(
470 struct ifnet
*altifp
, /* secondary EUI64 source */
471 struct in6_aliasreq
*ifra_passed
)
473 struct in6_ifaddr
*ia
;
474 struct in6_aliasreq ifra
;
475 struct nd_prefix pr0
, *pr
;
479 * configure link-local address.
481 bzero(&ifra
, sizeof(ifra
));
483 proto_plumb(PF_INET6
, ifp
);
486 * in6_update_ifa() does not use ifra_name, but we accurately set it
489 strncpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
));
491 if (((ifp
->if_type
== IFT_PPP
) || ((ifp
->if_eflags
& IFEF_NOAUTOIPV6LL
) != 0)) &&
492 ifra_passed
!= NULL
) /* PPP provided both addresses for us */
493 bcopy(&ifra_passed
->ifra_addr
, &(ifra
.ifra_addr
), sizeof(struct sockaddr_in6
));
495 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
496 ifra
.ifra_addr
.sin6_len
= sizeof(struct sockaddr_in6
);
497 ifra
.ifra_addr
.sin6_addr
.s6_addr16
[0] = htons(0xfe80);
499 ifra
.ifra_addr
.sin6_addr
.s6_addr16
[1] = 0
501 ifra
.ifra_addr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
); /* XXX */
503 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] = 0;
504 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
505 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] = 0;
506 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] = htonl(1);
508 if (get_ifid(ifp
, altifp
, &ifra
.ifra_addr
.sin6_addr
) != 0) {
510 " %s: no ifid available\n", if_name(ifp
)));
515 ifra
.ifra_addr
.sin6_scope_id
=
516 in6_addr2scopeid(ifp
, &ifra
.ifra_addr
.sin6_addr
);
519 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
520 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
521 ifra
.ifra_prefixmask
.sin6_addr
= in6mask64
;
523 /* take into accound the sin6_scope_id field for routing */
524 ifra
.ifra_prefixmask
.sin6_scope_id
= 0xffffffff;
526 /* link-local addresses should NEVER expire. */
527 ifra
.ifra_lifetime
.ia6t_vltime
= ND6_INFINITE_LIFETIME
;
528 ifra
.ifra_lifetime
.ia6t_pltime
= ND6_INFINITE_LIFETIME
;
531 * Do not let in6_update_ifa() do DAD, since we need a random delay
532 * before sending an NS at the first time the interface becomes up.
533 * Instead, in6_if_up() will start DAD with a proper random delay.
535 ifra
.ifra_flags
|= IN6_IFF_NODAD
;
538 * Now call in6_update_ifa() to do a bunch of procedures to configure
539 * a link-local address. We can set NULL to the 3rd argument, because
540 * we know there's no other link-local address on the interface
541 * and therefore we are adding one (instead of updating one).
543 if ((error
= in6_update_ifa(ifp
, &ifra
, NULL
, M_WAITOK
)) != 0) {
545 * XXX: When the interface does not support IPv6, this call
546 * would fail in the SIOCSIFADDR ioctl. I believe the
547 * notification is rather confusing in this case, so just
548 * supress it. (jinmei@kame.net 20010130)
550 if (error
!= EAFNOSUPPORT
)
551 log(LOG_NOTICE
, "in6_ifattach_linklocal: failed to "
552 "configure a link-local address on %s "
554 if_name(ifp
), error
);
559 * Adjust ia6_flags so that in6_if_up will perform DAD.
560 * XXX: Some P2P interfaces seem not to send packets just after
561 * becoming up, so we skip p2p interfaces for safety.
563 ia
= in6ifa_ifpforlinklocal(ifp
, 0); /* ia must not be NULL */
566 panic("ia == NULL in in6_ifattach_linklocal");
570 if (in6if_do_dad(ifp
) && (ifp
->if_flags
& IFF_POINTOPOINT
) == 0) {
571 ia
->ia6_flags
&= ~IN6_IFF_NODAD
;
572 ia
->ia6_flags
|= IN6_IFF_TENTATIVE
;
576 * Make the link-local prefix (fe80::/64%link) as on-link.
577 * Since we'd like to manage prefixes separately from addresses,
578 * we make an ND6 prefix structure for the link-local prefix,
579 * and add it to the prefix list as a never-expire prefix.
580 * XXX: this change might affect some existing code base...
582 bzero(&pr0
, sizeof(pr0
));
584 /* this should be 64 at this moment. */
585 pr0
.ndpr_plen
= in6_mask2len(&ifra
.ifra_prefixmask
.sin6_addr
, NULL
);
586 pr0
.ndpr_mask
= ifra
.ifra_prefixmask
.sin6_addr
;
587 pr0
.ndpr_prefix
= ifra
.ifra_addr
;
588 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
589 for (i
= 0; i
< 4; i
++) {
590 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
591 in6mask64
.s6_addr32
[i
];
594 * Initialize parameters. The link-local prefix must always be
595 * on-link, and its lifetimes never expire.
597 pr0
.ndpr_raf_onlink
= 1;
598 pr0
.ndpr_raf_auto
= 1; /* probably meaningless */
599 pr0
.ndpr_vltime
= ND6_INFINITE_LIFETIME
;
600 pr0
.ndpr_pltime
= ND6_INFINITE_LIFETIME
;
602 * Since there is no other link-local addresses, nd6_prefix_lookup()
603 * probably returns NULL. However, we cannot always expect the result.
604 * For example, if we first remove the (only) existing link-local
605 * address, and then reconfigure another one, the prefix is still
606 * valid with referring to the old link-local address.
608 if ((pr
= nd6_prefix_lookup(&pr0
)) == NULL
) {
609 if ((error
= nd6_prelist_add(&pr0
, NULL
, &pr
)) != 0) {
610 printf("in6_ifattach_linklocal: nd6_prelist_add failed %d\n", error
);
611 ifafree(&ia
->ia_ifa
);
617 in6_post_msg(ifp
, KEV_INET6_NEW_LL_ADDR
, ia
);
618 ifafree(&ia
->ia_ifa
);
621 /* Drop use count held above during lookup/add */
623 ndpr_rele(pr
, FALSE
);
629 in6_ifattach_loopback(
630 struct ifnet
*ifp
) /* must be IFT_LOOP */
632 struct in6_aliasreq ifra
;
635 bzero(&ifra
, sizeof(ifra
));
638 * in6_update_ifa() does not use ifra_name, but we accurately set it
641 strncpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
));
643 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
644 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
645 ifra
.ifra_prefixmask
.sin6_addr
= in6mask128
;
648 * Always initialize ia_dstaddr (= broadcast address) to loopback
649 * address. Follows IPv4 practice - see in_ifinit().
651 ifra
.ifra_dstaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
652 ifra
.ifra_dstaddr
.sin6_family
= AF_INET6
;
653 ifra
.ifra_dstaddr
.sin6_addr
= in6addr_loopback
;
655 ifra
.ifra_addr
.sin6_len
= sizeof(struct sockaddr_in6
);
656 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
657 ifra
.ifra_addr
.sin6_addr
= in6addr_loopback
;
659 /* the loopback address should NEVER expire. */
660 ifra
.ifra_lifetime
.ia6t_vltime
= ND6_INFINITE_LIFETIME
;
661 ifra
.ifra_lifetime
.ia6t_pltime
= ND6_INFINITE_LIFETIME
;
663 /* we don't need to perform DAD on loopback interfaces. */
664 ifra
.ifra_flags
|= IN6_IFF_NODAD
;
666 /* skip registration to the prefix list. XXX should be temporary. */
667 ifra
.ifra_flags
|= IN6_IFF_NOPFX
;
670 * We are sure that this is a newly assigned address, so we can set
671 * NULL to the 3rd arg.
673 if ((error
= in6_update_ifa(ifp
, &ifra
, NULL
, M_WAITOK
)) != 0) {
674 log(LOG_ERR
, "in6_ifattach_loopback: failed to configure "
675 "the loopback address on %s (errno=%d)\n",
676 if_name(ifp
), error
);
684 * compute NI group address, based on the current hostname setting.
685 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
687 * when ifp == NULL, the caller is responsible for filling scopeid.
694 struct in6_addr
*in6
)
701 char n
[64]; /* a single label must not exceed 63 chars */
703 if (!namelen
|| !name
)
707 while (p
&& *p
&& *p
!= '.' && p
- name
< namelen
)
709 if (p
- name
> sizeof(n
) - 1)
710 return -1; /* label too long */
714 for (q
= (u_char
*) n
; *q
; q
++) {
715 if ('A' <= *q
&& *q
<= 'Z')
719 /* generate 8 bytes of pseudo-random value. */
720 bzero(&ctxt
, sizeof(ctxt
));
722 MD5Update(&ctxt
, &l
, sizeof(l
));
723 MD5Update(&ctxt
, n
, l
);
724 MD5Final(digest
, &ctxt
);
726 bzero(in6
, sizeof(*in6
));
727 in6
->s6_addr16
[0] = htons(0xff02);
729 in6
->s6_addr16
[1] = htons(ifp
->if_index
);
730 in6
->s6_addr8
[11] = 2;
731 bcopy(digest
, &in6
->s6_addr32
[3], sizeof(in6
->s6_addr32
[3]));
742 struct sockaddr_in6 mltaddr
;
743 struct in6_multi
*in6m
;
746 bzero(&mltaddr
, sizeof(mltaddr
));
747 mltaddr
.sin6_family
= AF_INET6
;
748 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
749 if (in6_nigroup(NULL
, name
, namelen
, &mltaddr
.sin6_addr
) != 0)
752 ifnet_head_lock_shared();
753 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
754 mltaddr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
755 ifnet_lock_shared(ifp
);
756 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
757 ifnet_lock_done(ifp
);
759 if (!in6_addmulti(&mltaddr
.sin6_addr
, ifp
, &error
, 0)) {
760 nd6log((LOG_ERR
, "%s: failed to join %s "
761 "(errno=%d)\n", if_name(ifp
),
762 ip6_sprintf(&mltaddr
.sin6_addr
),
776 struct sockaddr_in6 mltaddr
;
777 struct in6_multi
*in6m
;
779 bzero(&mltaddr
, sizeof(mltaddr
));
780 mltaddr
.sin6_family
= AF_INET6
;
781 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
782 if (in6_nigroup(NULL
, name
, namelen
, &mltaddr
.sin6_addr
) != 0)
785 ifnet_head_lock_shared();
786 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
787 mltaddr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
788 ifnet_lock_shared(ifp
);
789 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
790 ifnet_lock_done(ifp
);
792 in6_delmulti(in6m
, 0);
798 * XXX multiple loopback interface needs more care. for instance,
799 * nodelocal address needs to be configured onto only one of them.
800 * XXX multiple link-local address case
805 struct ifnet
*altifp
, /* secondary EUI64 source */
806 struct in6_aliasreq
*ifra
)
808 static size_t if_indexlim
= 8;
809 struct in6_ifaddr
*ia
;
813 * We have some arrays that should be indexed by if_index.
814 * since if_index will grow dynamically, they should grow too.
815 * struct in6_ifstat **in6_ifstat
816 * struct icmp6_ifstat **icmp6_ifstat
818 if (in6_ifstat
== NULL
|| icmp6_ifstat
== NULL
||
819 if_index
>= if_indexlim
) {
820 while (if_index
>= if_indexlim
)
824 lck_mtx_lock(ip6_mutex
);
825 /* grow in6_ifstat */
826 if (in6_ifstatmax
< if_indexlim
) {
830 n
= if_indexlim
* sizeof(struct in6_ifstat
*);
831 q
= (caddr_t
)_MALLOC(n
, M_IFADDR
, M_WAITOK
);
833 lck_mtx_unlock(ip6_mutex
);
838 bcopy((caddr_t
)in6_ifstat
, q
,
839 in6_ifstatmax
* sizeof(struct in6_ifstat
*));
840 FREE((caddr_t
)in6_ifstat
, M_IFADDR
);
842 in6_ifstat
= (struct in6_ifstat
**)q
;
843 in6_ifstatmax
= if_indexlim
;
846 if (in6_ifstat
[ifp
->if_index
] == NULL
) {
847 in6_ifstat
[ifp
->if_index
] = (struct in6_ifstat
*)
848 _MALLOC(sizeof(struct in6_ifstat
), M_IFADDR
, M_WAITOK
);
849 if (in6_ifstat
[ifp
->if_index
] == NULL
) {
850 lck_mtx_unlock(ip6_mutex
);
853 bzero(in6_ifstat
[ifp
->if_index
], sizeof(struct in6_ifstat
));
855 lck_mtx_unlock(ip6_mutex
);
857 /* grow icmp6_ifstat, use inet6_domain_mutex as that is used in
860 lck_mtx_lock(inet6_domain_mutex
);
861 if (icmp6_ifstatmax
< if_indexlim
) {
865 n
= if_indexlim
* sizeof(struct icmp6_ifstat
*);
866 q
= (caddr_t
)_MALLOC(n
, M_IFADDR
, M_WAITOK
);
868 lck_mtx_unlock(inet6_domain_mutex
);
873 bcopy((caddr_t
)icmp6_ifstat
, q
,
874 icmp6_ifstatmax
* sizeof(struct icmp6_ifstat
*));
875 FREE((caddr_t
)icmp6_ifstat
, M_IFADDR
);
877 icmp6_ifstat
= (struct icmp6_ifstat
**)q
;
878 icmp6_ifstatmax
= if_indexlim
;
881 if (icmp6_ifstat
[ifp
->if_index
] == NULL
) {
882 icmp6_ifstat
[ifp
->if_index
] = (struct icmp6_ifstat
*)
883 _MALLOC(sizeof(struct icmp6_ifstat
), M_IFADDR
, M_WAITOK
);
884 if (icmp6_ifstat
[ifp
->if_index
] == NULL
) {
885 lck_mtx_unlock(inet6_domain_mutex
);
888 bzero(icmp6_ifstat
[ifp
->if_index
], sizeof(struct icmp6_ifstat
));
890 lck_mtx_unlock(inet6_domain_mutex
);
892 /* initialize NDP variables */
893 if ((error
= nd6_ifattach(ifp
)) != 0)
896 /* initialize scope identifiers */
897 if ((error
= scope6_ifattach(ifp
)) != 0)
901 * quirks based on interface type
903 switch (ifp
->if_type
) {
907 * 6to4 interface is a very special kind of beast.
908 * no multicast, no linklocal. RFC2529 specifies how to make
909 * linklocals for 6to4 interface, but there's no use and
910 * it is rather harmful to have one.
919 * usually, we require multicast capability to the interface
921 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
922 log(LOG_INFO
, "in6_ifattach: "
923 "%s is not multicast capable, IPv6 not enabled\n",
929 * assign loopback address for loopback interface.
930 * XXX multiple loopback interface case.
932 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
933 if (in6_ifattach_loopback(ifp
) != 0)
934 printf("in6_ifattach: in6_ifattach_loopback failed\n");
938 * assign a link-local address, if there's none.
940 if (ip6_auto_linklocal
) {
941 ia
= in6ifa_ifpforlinklocal(ifp
, 0);
943 if (in6_ifattach_linklocal(ifp
, altifp
, ifra
) == 0) {
944 /* linklocal address assigned */
946 log(LOG_INFO
, "in6_ifattach: %s failed to "
947 "attach a linklocal address.\n",
949 /* failed to assign linklocal address. bark? */
952 ifafree(&ia
->ia_ifa
);
956 #if IFT_STF /* XXX */
960 /* update dynamically. */
961 if (in6_maxmtu
< ifp
->if_mtu
)
962 in6_maxmtu
= ifp
->if_mtu
;
968 * NOTE: in6_ifdetach() does not support loopback if at this moment.
969 * We don't need this function in bsdi, because interfaces are never removed
970 * from the ifnet list in bsdi.
976 struct in6_ifaddr
*ia
, *oia
, *nia
;
977 struct ifaddr
*ifa
, *next
;
979 struct sockaddr_in6 sin6
;
981 /* nuke prefix list. this may try to remove some of ifaddrs as well */
982 in6_purgeprefix(ifp
);
984 /* remove neighbor management table */
987 /* nuke any of IPv6 addresses we have */
989 lck_mtx_lock(nd6_mutex
);
990 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= nia
) {
992 if (ia
->ia_ifa
.ifa_ifp
!= ifp
)
994 in6_purgeaddr(&ia
->ia_ifa
, 1);
996 lck_mtx_unlock(nd6_mutex
);
998 ifnet_lock_exclusive(ifp
);
1000 /* undo everything done by in6_ifattach(), just in case */
1001 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
; ifa
= next
)
1003 next
= ifa
->ifa_list
.tqe_next
;
1006 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
1007 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa
->ifa_addr
)->sin6_addr
)) {
1011 ia
= (struct in6_ifaddr
*)ifa
;
1013 /* remove from the routing table */
1014 if ((ia
->ia_flags
& IFA_ROUTE
) &&
1015 (rt
= rtalloc1((struct sockaddr
*)&ia
->ia_addr
, 0, 0))) {
1016 (void) rtrequest(RTM_DELETE
,
1017 (struct sockaddr
*)&ia
->ia_addr
,
1018 (struct sockaddr
*)&ia
->ia_addr
,
1019 (struct sockaddr
*)&ia
->ia_prefixmask
,
1020 rt
->rt_flags
, (struct rtentry
**)0);
1024 /* remove from the linked list */
1025 if_detach_ifa(ifp
, &ia
->ia_ifa
);
1027 /* also remove from the IPv6 address chain(itojun&jinmei) */
1029 lck_mtx_lock(nd6_mutex
);
1030 if (oia
== (ia
= in6_ifaddrs
))
1031 in6_ifaddrs
= ia
->ia_next
;
1033 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
1036 ia
->ia_next
= oia
->ia_next
;
1039 "%s: didn't unlink in6ifaddr from "
1040 "list\n", if_name(ifp
)));
1043 lck_mtx_unlock(nd6_mutex
);
1045 ifafree(&oia
->ia_ifa
);
1047 ifnet_lock_done(ifp
);
1050 * remove neighbor management table. we call it twice just to make
1051 * sure we nuke everything. maybe we need just one call.
1052 * XXX: since the first call did not release addresses, some prefixes
1053 * might remain. We should call nd6_purge() again to release the
1054 * prefixes after removing all addresses above.
1055 * (Or can we just delay calling nd6_purge until at this point?)
1059 /* remove route to link-local allnodes multicast (ff02::1) */
1060 bzero(&sin6
, sizeof(sin6
));
1061 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1062 sin6
.sin6_family
= AF_INET6
;
1063 sin6
.sin6_addr
= in6addr_linklocal_allnodes
;
1064 sin6
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
1065 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
1068 if (rt
->rt_ifp
== ifp
) {
1070 * Prevent another thread from modifying rt_key,
1071 * rt_gateway via rt_setgate() after the rt_lock
1072 * is dropped by marking the route as defunct.
1074 rt
->rt_flags
|= RTF_CONDEMNED
;
1076 (void) rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1077 rt_mask(rt
), rt
->rt_flags
, 0);
1089 const u_int8_t
*baseid
,
1092 u_int8_t nullbuf
[8];
1093 struct nd_ifinfo
*ndi
;
1095 lck_rw_lock_shared(nd_if_rwlock
);
1096 ndi
= &nd_ifinfo
[ifp
->if_index
];
1097 bzero(nullbuf
, sizeof(nullbuf
));
1098 if (bcmp(ndi
->randomid
, nullbuf
, sizeof(nullbuf
)) == 0) {
1099 /* we've never created a random ID. Create a new one. */
1104 bcopy(baseid
, ndi
->randomseed1
, sizeof(ndi
->randomseed1
));
1106 /* generate_tmp_ifid will update seedn and buf */
1107 (void)generate_tmp_ifid(ndi
->randomseed0
, ndi
->randomseed1
,
1110 bcopy(ndi
->randomid
, retbuf
, 8);
1111 lck_rw_done(nd_if_rwlock
);
1116 __unused
void *ignored_arg
)
1119 struct nd_ifinfo
*ndi
;
1120 u_int8_t nullbuf
[8];
1122 timeout(in6_tmpaddrtimer
, (caddr_t
)0,
1123 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
1124 ip6_temp_regen_advance
) * hz
);
1126 if (ip6_use_tempaddr
) {
1127 lck_rw_lock_shared(nd_if_rwlock
);
1128 bzero(nullbuf
, sizeof(nullbuf
));
1129 for (i
= 1; i
< nd_ifinfo_indexlim
+ 1; i
++) {
1130 ndi
= &nd_ifinfo
[i
];
1131 if (ndi
->flags
!= ND6_IFF_PERFORMNUD
)
1133 if (bcmp(ndi
->randomid
, nullbuf
, sizeof(nullbuf
)) != 0) {
1135 * We've been generating a random ID on this interface.
1138 (void)generate_tmp_ifid(ndi
->randomseed0
,
1143 lck_rw_done(nd_if_rwlock
);