1 /* $FreeBSD: src/sys/netinet6/in6_ifattach.c,v 1.8 2002/04/19 04:46:22 suz Exp $ */
2 /* $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun 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/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
38 #include <sys/sockio.h>
39 #include <sys/kernel.h>
40 #include <sys/syslog.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/if_ether.h>
51 #include <netinet/in_pcb.h>
53 #include <netinet/ip6.h>
54 #include <netinet6/ip6_var.h>
55 #include <netinet6/in6_var.h>
56 #include <netinet6/in6_pcb.h>
57 #include <netinet6/in6_ifattach.h>
58 #include <netinet6/ip6_var.h>
59 #include <netinet6/nd6.h>
60 #include <netinet6/scope6_var.h>
62 #include <net/net_osdep.h>
64 struct in6_ifstat
**in6_ifstat
= NULL
;
65 struct icmp6_ifstat
**icmp6_ifstat
= NULL
;
66 size_t in6_ifstatmax
= 0;
67 size_t icmp6_ifstatmax
= 0;
68 unsigned long in6_maxmtu
= 0;
70 #if IP6_AUTO_LINKLOCAL
71 int ip6_auto_linklocal
= IP6_AUTO_LINKLOCAL
;
73 int ip6_auto_linklocal
= 1; /* enable by default */
77 extern struct inpcbinfo udbinfo
;
78 extern struct inpcbinfo ripcbinfo
;
80 static int get_rand_ifid
__P((struct ifnet
*, struct in6_addr
*));
81 static int generate_tmp_ifid
__P((u_int8_t
*, const u_int8_t
*, u_int8_t
*));
82 static int get_hw_ifid
__P((struct ifnet
*, struct in6_addr
*));
83 static int get_ifid
__P((struct ifnet
*, struct ifnet
*, struct in6_addr
*));
84 static int in6_ifattach_linklocal
__P((struct ifnet
*, struct ifnet
*, struct in6_aliasreq
*));
85 static int in6_ifattach_loopback
__P((struct ifnet
*));
87 #define EUI64_GBIT 0x01
88 #define EUI64_UBIT 0x02
89 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
90 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
91 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
92 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
93 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
95 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
96 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
99 * Generate a last-resort interface identifier, when the machine has no
100 * IEEE802/EUI64 address sources.
101 * The goal here is to get an interface identifier that is
102 * (1) random enough and (2) does not change across reboot.
103 * We currently use MD5(hostname) for it.
106 get_rand_ifid(ifp
, in6
)
108 struct in6_addr
*in6
; /* upper 64bits are preserved */
112 int hostnamelen
= strlen(hostname
);
115 /* we need at least several letters as seed for ifid */
120 /* generate 8 bytes of pseudo-random value. */
121 bzero(&ctxt
, sizeof(ctxt
));
123 MD5Update(&ctxt
, hostname
, hostnamelen
);
124 MD5Final(digest
, &ctxt
);
126 /* assumes sizeof(digest) > sizeof(ifid) */
127 bcopy(digest
, &in6
->s6_addr
[8], 8);
129 /* make sure to set "u" bit to local, and "g" bit to individual. */
130 in6
->s6_addr
[8] &= ~EUI64_GBIT
; /* g bit to "individual" */
131 in6
->s6_addr
[8] |= EUI64_UBIT
; /* u bit to "local" */
133 /* convert EUI64 into IPv6 interface identifier */
140 generate_tmp_ifid(seed0
, seed1
, ret
)
141 u_int8_t
*seed0
, *ret
;
142 const u_int8_t
*seed1
;
145 u_int8_t seed
[16], digest
[16], nullbuf
[8];
149 /* If there's no hisotry, start with a random seed. */
150 bzero(nullbuf
, sizeof(nullbuf
));
151 if (bcmp(nullbuf
, seed0
, sizeof(nullbuf
)) == 0) {
154 for (i
= 0; i
< 2; i
++) {
156 val32
= random() ^ tv
.tv_usec
;
157 bcopy(&val32
, seed
+ sizeof(val32
) * i
, sizeof(val32
));
160 bcopy(seed0
, seed
, 8);
163 /* copy the right-most 64-bits of the given address */
164 /* XXX assumption on the size of IFID */
165 bcopy(seed1
, &seed
[8], 8);
167 if (0) { /* for debugging purposes only */
170 printf("generate_tmp_ifid: new randomized ID from: ");
171 for (i
= 0; i
< 16; i
++)
172 printf("%02x", seed
[i
]);
176 /* generate 16 bytes of pseudo-random value. */
177 bzero(&ctxt
, sizeof(ctxt
));
179 MD5Update(&ctxt
, seed
, sizeof(seed
));
180 MD5Final(digest
, &ctxt
);
183 * RFC 3041 3.2.1. (3)
184 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
185 * left-most bit is numbered 0) to zero.
187 bcopy(digest
, ret
, 8);
188 ret
[0] &= ~EUI64_UBIT
;
191 * XXX: we'd like to ensure that the generated value is not zero
192 * for simplicity. If the caclculated digest happens to be zero,
193 * use a random non-zero value as the last resort.
195 if (bcmp(nullbuf
, ret
, sizeof(nullbuf
)) == 0) {
197 "generate_tmp_ifid: computed MD5 value is zero.\n");
200 val32
= random() ^ tv
.tv_usec
;
201 val32
= 1 + (val32
% (0xffffffff - 1));
205 * RFC 3041 3.2.1. (4)
206 * Take the rightmost 64-bits of the MD5 digest and save them in
207 * stable storage as the history value to be used in the next
208 * iteration of the algorithm.
210 bcopy(&digest
[8], seed0
, 8);
212 if (0) { /* for debugging purposes only */
216 for (i
= 0; i
< 16; i
++)
217 printf("%02x", digest
[i
]);
225 * Get interface identifier for the specified interface.
226 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
229 get_hw_ifid(ifp
, in6
)
231 struct in6_addr
*in6
; /* upper 64bits are preserved */
234 struct sockaddr_dl
*sdl
;
237 static u_int8_t allzero
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
238 static u_int8_t allone
[8] =
239 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
241 for (ifa
= ifp
->if_addrlist
.tqh_first
;
243 ifa
= ifa
->ifa_list
.tqe_next
)
245 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
247 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
250 if (sdl
->sdl_alen
== 0)
260 addrlen
= sdl
->sdl_alen
;
263 switch (ifp
->if_type
) {
271 /* IEEE802/EUI64 cases - what others? */
272 /* IEEE1394 uses 16byte length address starting with EUI64 */
276 /* look at IEEE802/EUI64 only */
277 if (addrlen
!= 8 && addrlen
!= 6)
281 * check for invalid MAC address - on bsdi, we see it a lot
282 * since wildboar configures all-zero MAC on pccard before
285 if (bcmp(addr
, allzero
, addrlen
) == 0)
287 if (bcmp(addr
, allone
, addrlen
) == 0)
290 /* make EUI64 address */
292 bcopy(addr
, &in6
->s6_addr
[8], 8);
293 else if (addrlen
== 6) {
294 in6
->s6_addr
[8] = addr
[0];
295 in6
->s6_addr
[9] = addr
[1];
296 in6
->s6_addr
[10] = addr
[2];
297 in6
->s6_addr
[11] = 0xff;
298 in6
->s6_addr
[12] = 0xfe;
299 in6
->s6_addr
[13] = addr
[3];
300 in6
->s6_addr
[14] = addr
[4];
301 in6
->s6_addr
[15] = addr
[5];
311 bzero(&in6
->s6_addr
[8], 8);
312 in6
->s6_addr
[15] = addr
[0];
315 * due to insufficient bitwidth, we mark it local.
317 in6
->s6_addr
[8] &= ~EUI64_GBIT
; /* g bit to "individual" */
318 in6
->s6_addr
[8] |= EUI64_UBIT
; /* u bit to "local" */
326 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
327 * however, IPv4 address is not very suitable as unique
328 * identifier source (can be renumbered).
337 /* sanity check: g bit must not indicate "group" */
338 if (EUI64_GROUP(in6
))
341 /* convert EUI64 into IPv6 interface identifier */
345 * sanity check: ifid must not be all zero, avoid conflict with
346 * subnet router anycast
348 if ((in6
->s6_addr
[8] & ~(EUI64_GBIT
| EUI64_UBIT
)) == 0x00 &&
349 bcmp(&in6
->s6_addr
[9], allzero
, 7) == 0) {
357 * Get interface identifier for the specified interface. If it is not
358 * available on ifp0, borrow interface identifier from other information
362 get_ifid(ifp0
, altifp
, in6
)
364 struct ifnet
*altifp
; /* secondary EUI64 source */
365 struct in6_addr
*in6
;
369 /* first, try to get it from the interface itself */
370 if (get_hw_ifid(ifp0
, in6
) == 0) {
371 nd6log((LOG_DEBUG
, "%s: got interface identifier from itself\n",
376 /* try secondary EUI64 source. this basically is for ATM PVC */
377 if (altifp
&& get_hw_ifid(altifp
, in6
) == 0) {
378 nd6log((LOG_DEBUG
, "%s: got interface identifier from %s\n",
379 if_name(ifp0
), if_name(altifp
)));
383 /* next, try to get it from some other hardware interface */
384 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_list
.tqe_next
)
388 if (get_hw_ifid(ifp
, in6
) != 0)
392 * to borrow ifid from other interface, ifid needs to be
395 if (IFID_UNIVERSAL(in6
)) {
397 "%s: borrow interface identifier from %s\n",
398 if_name(ifp0
), if_name(ifp
)));
403 /* last resort: get from random number source */
404 if (get_rand_ifid(ifp
, in6
) == 0) {
406 "%s: interface identifier generated by random number\n",
411 printf("%s: failed to get interface identifier\n", if_name(ifp0
));
415 nd6log((LOG_INFO
, "%s: ifid: "
416 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
418 in6
->s6_addr
[8], in6
->s6_addr
[9],
419 in6
->s6_addr
[10], in6
->s6_addr
[11],
420 in6
->s6_addr
[12], in6
->s6_addr
[13],
421 in6
->s6_addr
[14], in6
->s6_addr
[15]));
426 in6_ifattach_linklocal(ifp
, altifp
, ifra_passed
)
428 struct ifnet
*altifp
; /* secondary EUI64 source */
429 struct in6_aliasreq
*ifra_passed
;
431 struct in6_ifaddr
*ia
;
432 struct in6_aliasreq ifra
;
433 struct nd_prefix pr0
;
434 int i
, dl_tag
, error
;
437 * configure link-local address.
439 bzero(&ifra
, sizeof(ifra
));
441 dlil_plumb_protocol(PF_INET6
, ifp
, &dl_tag
);
444 * in6_update_ifa() does not use ifra_name, but we accurately set it
447 strncpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
));
449 if (ifp
->if_type
== IFT_PPP
&& ifra_passed
!= NULL
) /* PPP provided both addresses for us */
450 bcopy(&ifra_passed
->ifra_addr
, &(ifra
.ifra_addr
), sizeof(struct sockaddr_in6
));
452 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
453 ifra
.ifra_addr
.sin6_len
= sizeof(struct sockaddr_in6
);
454 ifra
.ifra_addr
.sin6_addr
.s6_addr16
[0] = htons(0xfe80);
456 ifra
.ifra_addr
.sin6_addr
.s6_addr16
[1] = 0
458 ifra
.ifra_addr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
); /* XXX */
460 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] = 0;
461 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
462 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] = 0;
463 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] = htonl(1);
465 if (get_ifid(ifp
, altifp
, &ifra
.ifra_addr
.sin6_addr
) != 0) {
467 " %s: no ifid available\n", if_name(ifp
)));
472 ifra
.ifra_addr
.sin6_scope_id
=
473 in6_addr2scopeid(ifp
, &ifra
.ifra_addr
.sin6_addr
);
476 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
477 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
478 ifra
.ifra_prefixmask
.sin6_addr
= in6mask64
;
480 /* take into accound the sin6_scope_id field for routing */
481 ifra
.ifra_prefixmask
.sin6_scope_id
= 0xffffffff;
483 /* link-local addresses should NEVER expire. */
484 ifra
.ifra_lifetime
.ia6t_vltime
= ND6_INFINITE_LIFETIME
;
485 ifra
.ifra_lifetime
.ia6t_pltime
= ND6_INFINITE_LIFETIME
;
488 * Do not let in6_update_ifa() do DAD, since we need a random delay
489 * before sending an NS at the first time the interface becomes up.
490 * Instead, in6_if_up() will start DAD with a proper random delay.
492 ifra
.ifra_flags
|= IN6_IFF_NODAD
;
495 * Now call in6_update_ifa() to do a bunch of procedures to configure
496 * a link-local address. We can set NULL to the 3rd argument, because
497 * we know there's no other link-local address on the interface
498 * and therefore we are adding one (instead of updating one).
500 if ((error
= in6_update_ifa(ifp
, &ifra
, NULL
)) != 0) {
502 * XXX: When the interface does not support IPv6, this call
503 * would fail in the SIOCSIFADDR ioctl. I believe the
504 * notification is rather confusing in this case, so just
505 * supress it. (jinmei@kame.net 20010130)
507 if (error
!= EAFNOSUPPORT
)
508 log(LOG_NOTICE
, "in6_ifattach_linklocal: failed to "
509 "configure a link-local address on %s "
511 if_name(ifp
), error
);
516 * Adjust ia6_flags so that in6_if_up will perform DAD.
517 * XXX: Some P2P interfaces seem not to send packets just after
518 * becoming up, so we skip p2p interfaces for safety.
520 ia
= in6ifa_ifpforlinklocal(ifp
, 0); /* ia must not be NULL */
523 panic("ia == NULL in in6_ifattach_linklocal");
527 if (in6if_do_dad(ifp
) && (ifp
->if_flags
& IFF_POINTOPOINT
) == 0) {
528 ia
->ia6_flags
&= ~IN6_IFF_NODAD
;
529 ia
->ia6_flags
|= IN6_IFF_TENTATIVE
;
533 * Make the link-local prefix (fe80::/64%link) as on-link.
534 * Since we'd like to manage prefixes separately from addresses,
535 * we make an ND6 prefix structure for the link-local prefix,
536 * and add it to the prefix list as a never-expire prefix.
537 * XXX: this change might affect some existing code base...
539 bzero(&pr0
, sizeof(pr0
));
541 /* this should be 64 at this moment. */
542 pr0
.ndpr_plen
= in6_mask2len(&ifra
.ifra_prefixmask
.sin6_addr
, NULL
);
543 pr0
.ndpr_mask
= ifra
.ifra_prefixmask
.sin6_addr
;
544 pr0
.ndpr_prefix
= ifra
.ifra_addr
;
545 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
546 for (i
= 0; i
< 4; i
++) {
547 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
548 in6mask64
.s6_addr32
[i
];
551 * Initialize parameters. The link-local prefix must always be
552 * on-link, and its lifetimes never expire.
554 pr0
.ndpr_raf_onlink
= 1;
555 pr0
.ndpr_raf_auto
= 1; /* probably meaningless */
556 pr0
.ndpr_vltime
= ND6_INFINITE_LIFETIME
;
557 pr0
.ndpr_pltime
= ND6_INFINITE_LIFETIME
;
559 * Since there is no other link-local addresses, nd6_prefix_lookup()
560 * probably returns NULL. However, we cannot always expect the result.
561 * For example, if we first remove the (only) existing link-local
562 * address, and then reconfigure another one, the prefix is still
563 * valid with referring to the old link-local address.
565 if (nd6_prefix_lookup(&pr0
) == NULL
) {
566 if ((error
= nd6_prelist_add(&pr0
, NULL
, NULL
)) != 0)
570 in6_post_msg(ifp
, KEV_INET6_NEW_LL_ADDR
, ia
);
575 in6_ifattach_loopback(ifp
)
576 struct ifnet
*ifp
; /* must be IFT_LOOP */
578 struct in6_aliasreq ifra
;
581 bzero(&ifra
, sizeof(ifra
));
584 * in6_update_ifa() does not use ifra_name, but we accurately set it
587 strncpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
));
589 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
590 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
591 ifra
.ifra_prefixmask
.sin6_addr
= in6mask128
;
594 * Always initialize ia_dstaddr (= broadcast address) to loopback
595 * address. Follows IPv4 practice - see in_ifinit().
597 ifra
.ifra_dstaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
598 ifra
.ifra_dstaddr
.sin6_family
= AF_INET6
;
599 ifra
.ifra_dstaddr
.sin6_addr
= in6addr_loopback
;
601 ifra
.ifra_addr
.sin6_len
= sizeof(struct sockaddr_in6
);
602 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
603 ifra
.ifra_addr
.sin6_addr
= in6addr_loopback
;
605 /* the loopback address should NEVER expire. */
606 ifra
.ifra_lifetime
.ia6t_vltime
= ND6_INFINITE_LIFETIME
;
607 ifra
.ifra_lifetime
.ia6t_pltime
= ND6_INFINITE_LIFETIME
;
609 /* we don't need to perform DAD on loopback interfaces. */
610 ifra
.ifra_flags
|= IN6_IFF_NODAD
;
612 /* skip registration to the prefix list. XXX should be temporary. */
613 ifra
.ifra_flags
|= IN6_IFF_NOPFX
;
616 * We are sure that this is a newly assigned address, so we can set
617 * NULL to the 3rd arg.
619 if ((error
= in6_update_ifa(ifp
, &ifra
, NULL
)) != 0) {
620 log(LOG_ERR
, "in6_ifattach_loopback: failed to configure "
621 "the loopback address on %s (errno=%d)\n",
622 if_name(ifp
), error
);
630 * compute NI group address, based on the current hostname setting.
631 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
633 * when ifp == NULL, the caller is responsible for filling scopeid.
636 in6_nigroup(ifp
, name
, namelen
, in6
)
640 struct in6_addr
*in6
;
647 char n
[64]; /* a single label must not exceed 63 chars */
649 if (!namelen
|| !name
)
653 while (p
&& *p
&& *p
!= '.' && p
- name
< namelen
)
655 if (p
- name
> sizeof(n
) - 1)
656 return -1; /* label too long */
660 for (q
= n
; *q
; q
++) {
661 if ('A' <= *q
&& *q
<= 'Z')
665 /* generate 8 bytes of pseudo-random value. */
666 bzero(&ctxt
, sizeof(ctxt
));
668 MD5Update(&ctxt
, &l
, sizeof(l
));
669 MD5Update(&ctxt
, n
, l
);
670 MD5Final(digest
, &ctxt
);
672 bzero(in6
, sizeof(*in6
));
673 in6
->s6_addr16
[0] = htons(0xff02);
675 in6
->s6_addr16
[1] = htons(ifp
->if_index
);
676 in6
->s6_addr8
[11] = 2;
677 bcopy(digest
, &in6
->s6_addr32
[3], sizeof(in6
->s6_addr32
[3]));
683 in6_nigroup_attach(name
, namelen
)
688 struct sockaddr_in6 mltaddr
;
689 struct in6_multi
*in6m
;
692 bzero(&mltaddr
, sizeof(mltaddr
));
693 mltaddr
.sin6_family
= AF_INET6
;
694 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
695 if (in6_nigroup(NULL
, name
, namelen
, &mltaddr
.sin6_addr
) != 0)
698 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_list
.tqe_next
)
700 mltaddr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
701 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
703 if (!in6_addmulti(&mltaddr
.sin6_addr
, ifp
, &error
)) {
704 nd6log((LOG_ERR
, "%s: failed to join %s "
705 "(errno=%d)\n", if_name(ifp
),
706 ip6_sprintf(&mltaddr
.sin6_addr
),
714 in6_nigroup_detach(name
, namelen
)
719 struct sockaddr_in6 mltaddr
;
720 struct in6_multi
*in6m
;
722 bzero(&mltaddr
, sizeof(mltaddr
));
723 mltaddr
.sin6_family
= AF_INET6
;
724 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
725 if (in6_nigroup(NULL
, name
, namelen
, &mltaddr
.sin6_addr
) != 0)
728 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_list
.tqe_next
)
730 mltaddr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
731 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
738 * XXX multiple loopback interface needs more care. for instance,
739 * nodelocal address needs to be configured onto only one of them.
740 * XXX multiple link-local address case
743 in6_ifattach(ifp
, altifp
, ifra
)
745 struct ifnet
*altifp
; /* secondary EUI64 source */
746 struct in6_aliasreq
*ifra
;
748 static size_t if_indexlim
= 8;
749 struct in6_ifaddr
*ia
;
754 * We have some arrays that should be indexed by if_index.
755 * since if_index will grow dynamically, they should grow too.
756 * struct in6_ifstat **in6_ifstat
757 * struct icmp6_ifstat **icmp6_ifstat
759 if (in6_ifstat
== NULL
|| icmp6_ifstat
== NULL
||
760 if_index
>= if_indexlim
) {
766 while (if_index
>= if_indexlim
)
769 /* grow in6_ifstat */
770 n
= if_indexlim
* sizeof(struct in6_ifstat
*);
771 q
= (caddr_t
)_MALLOC(n
, M_IFADDR
, M_WAITOK
);
774 bcopy((caddr_t
)in6_ifstat
, q
,
775 olim
* sizeof(struct in6_ifstat
*));
776 FREE((caddr_t
)in6_ifstat
, M_IFADDR
);
778 in6_ifstat
= (struct in6_ifstat
**)q
;
779 in6_ifstatmax
= if_indexlim
;
781 /* grow icmp6_ifstat */
782 n
= if_indexlim
* sizeof(struct icmp6_ifstat
*);
783 q
= (caddr_t
)_MALLOC(n
, M_IFADDR
, M_WAITOK
);
786 bcopy((caddr_t
)icmp6_ifstat
, q
,
787 olim
* sizeof(struct icmp6_ifstat
*));
788 FREE((caddr_t
)icmp6_ifstat
, M_IFADDR
);
790 icmp6_ifstat
= (struct icmp6_ifstat
**)q
;
791 icmp6_ifstatmax
= if_indexlim
;
794 /* initialize NDP variables */
797 /* initialize scope identifiers */
798 scope6_ifattach(ifp
);
801 * quirks based on interface type
803 switch (ifp
->if_type
) {
807 * 6to4 interface is a very special kind of beast.
808 * no multicast, no linklocal. RFC2529 specifies how to make
809 * linklocals for 6to4 interface, but there's no use and
810 * it is rather harmful to have one.
819 * usually, we require multicast capability to the interface
821 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
822 log(LOG_INFO
, "in6_ifattach: "
823 "%s is not multicast capable, IPv6 not enabled\n",
829 * assign loopback address for loopback interface.
830 * XXX multiple loopback interface case.
832 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
833 in6
= in6addr_loopback
;
834 if (in6ifa_ifpwithaddr(ifp
, &in6
) == NULL
) {
835 if (in6_ifattach_loopback(ifp
) != 0)
841 * assign a link-local address, if there's none.
843 if (ip6_auto_linklocal
) {
844 ia
= in6ifa_ifpforlinklocal(ifp
, 0);
846 if (in6_ifattach_linklocal(ifp
, altifp
, ifra
) == 0) {
847 /* linklocal address assigned */
849 log(LOG_INFO
, "in6_ifattach: "
850 "%s failed to attach a linklocal address.\n",
852 /* failed to assign linklocal address. bark? */
857 #if IFT_STF /* XXX */
861 /* update dynamically. */
862 if (in6_maxmtu
< ifp
->if_mtu
)
863 in6_maxmtu
= ifp
->if_mtu
;
865 if (in6_ifstat
[ifp
->if_index
] == NULL
) {
866 in6_ifstat
[ifp
->if_index
] = (struct in6_ifstat
*)
867 _MALLOC(sizeof(struct in6_ifstat
), M_IFADDR
, M_WAITOK
);
868 bzero(in6_ifstat
[ifp
->if_index
], sizeof(struct in6_ifstat
));
870 if (icmp6_ifstat
[ifp
->if_index
] == NULL
) {
871 icmp6_ifstat
[ifp
->if_index
] = (struct icmp6_ifstat
*)
872 _MALLOC(sizeof(struct icmp6_ifstat
), M_IFADDR
, M_WAITOK
);
873 bzero(icmp6_ifstat
[ifp
->if_index
], sizeof(struct icmp6_ifstat
));
878 * NOTE: in6_ifdetach() does not support loopback if at this moment.
879 * We don't need this function in bsdi, because interfaces are never removed
880 * from the ifnet list in bsdi.
886 struct in6_ifaddr
*ia
, *oia
;
887 struct ifaddr
*ifa
, *next
;
890 struct sockaddr_in6 sin6
;
891 struct in6_multi
*in6m
;
892 struct in6_multi
*in6m_next
;
894 /* nuke prefix list. this may try to remove some of ifaddrs as well */
895 in6_purgeprefix(ifp
);
897 /* remove neighbor management table */
900 /* nuke any of IPv6 addresses we have */
901 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
; ifa
= next
)
903 next
= ifa
->ifa_list
.tqe_next
;
904 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
909 /* undo everything done by in6_ifattach(), just in case */
910 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
; ifa
= next
)
912 next
= ifa
->ifa_list
.tqe_next
;
915 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
916 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa
->ifa_addr
)->sin6_addr
)) {
920 ia
= (struct in6_ifaddr
*)ifa
;
922 /* remove from the routing table */
923 if ((ia
->ia_flags
& IFA_ROUTE
)
924 && (rt
= rtalloc1((struct sockaddr
*)&ia
->ia_addr
, 0, 0UL))) {
925 rtflags
= rt
->rt_flags
;
927 rtrequest(RTM_DELETE
,
928 (struct sockaddr
*)&ia
->ia_addr
,
929 (struct sockaddr
*)&ia
->ia_addr
,
930 (struct sockaddr
*)&ia
->ia_prefixmask
,
931 rtflags
, (struct rtentry
**)0);
934 /* remove from the linked list */
935 TAILQ_REMOVE(&ifp
->if_addrlist
, (struct ifaddr
*)ia
, ifa_list
);
936 ifafree(&ia
->ia_ifa
);
938 /* also remove from the IPv6 address chain(itojun&jinmei) */
940 if (oia
== (ia
= in6_ifaddr
))
941 in6_ifaddr
= ia
->ia_next
;
943 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
946 ia
->ia_next
= oia
->ia_next
;
949 "%s: didn't unlink in6ifaddr from "
950 "list\n", if_name(ifp
)));
954 IFAFREE(&oia
->ia_ifa
);
959 /* This is a cause for reentrency, as those multicast addresses are
960 * freed both from the interface detaching and triggered by the closing of the socket
961 * Let the socket do the cleanup and not force it from the interface level
963 /* leave from all multicast groups joined */
964 in6_pcbpurgeif0(LIST_FIRST(udbinfo
.listhead
), ifp
);
965 in6_pcbpurgeif0(LIST_FIRST(ripcbinfo
.listhead
), ifp
);
966 for (in6m
= LIST_FIRST(&in6_multihead
); in6m
; in6m
= in6m_next
) {
967 in6m_next
= LIST_NEXT(in6m
, in6m_entry
);
968 if (in6m
->in6m_ifp
!= ifp
)
973 #endif /* __APPLE__ */
976 * remove neighbor management table. we call it twice just to make
977 * sure we nuke everything. maybe we need just one call.
978 * XXX: since the first call did not release addresses, some prefixes
979 * might remain. We should call nd6_purge() again to release the
980 * prefixes after removing all addresses above.
981 * (Or can we just delay calling nd6_purge until at this point?)
985 /* remove route to link-local allnodes multicast (ff02::1) */
986 bzero(&sin6
, sizeof(sin6
));
987 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
988 sin6
.sin6_family
= AF_INET6
;
989 sin6
.sin6_addr
= in6addr_linklocal_allnodes
;
990 sin6
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
991 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0UL);
992 if (rt
&& rt
->rt_ifp
== ifp
) {
993 rtrequest(RTM_DELETE
, (struct sockaddr
*)rt_key(rt
),
994 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, 0);
1000 in6_get_tmpifid(ifp
, retbuf
, baseid
, generate
)
1003 const u_int8_t
*baseid
;
1006 u_int8_t nullbuf
[8];
1007 struct nd_ifinfo
*ndi
= &nd_ifinfo
[ifp
->if_index
];
1009 bzero(nullbuf
, sizeof(nullbuf
));
1010 if (bcmp(ndi
->randomid
, nullbuf
, sizeof(nullbuf
)) == 0) {
1011 /* we've never created a random ID. Create a new one. */
1016 bcopy(baseid
, ndi
->randomseed1
, sizeof(ndi
->randomseed1
));
1018 /* generate_tmp_ifid will update seedn and buf */
1019 (void)generate_tmp_ifid(ndi
->randomseed0
, ndi
->randomseed1
,
1022 bcopy(ndi
->randomid
, retbuf
, 8);
1026 in6_tmpaddrtimer_funneled(void *ignored_arg
)
1029 boolean_t funnel_state
;
1030 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
1032 in6_tmpaddrtimer(ignored_arg
);
1034 (void) thread_funnel_set(network_flock
, FALSE
);
1039 in6_tmpaddrtimer(ignored_arg
)
1043 struct nd_ifinfo
*ndi
;
1044 u_int8_t nullbuf
[8];
1047 timeout(in6_tmpaddrtimer_funneled
, (caddr_t
)0,
1048 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
1049 ip6_temp_regen_advance
) * hz
);
1051 bzero(nullbuf
, sizeof(nullbuf
));
1052 for (i
= 1; i
< if_index
+ 1; i
++) {
1053 ndi
= &nd_ifinfo
[i
];
1054 if (bcmp(ndi
->randomid
, nullbuf
, sizeof(nullbuf
)) != 0) {
1056 * We've been generating a random ID on this interface.
1059 (void)generate_tmp_ifid(ndi
->randomseed0
,