]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/in6_ifattach.c
xnu-4570.71.2.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_ifattach.c
CommitLineData
2d21ac55 1/*
39037602 2 * Copyright (c) 2003-2016 Apple Inc. All rights reserved.
2d21ac55
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
2d21ac55
A
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.
39236c6e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
2d21ac55
A
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.
39236c6e 25 *
2d21ac55
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
1c79356b
A
28
29/*
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
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.
44 *
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
55 * SUCH DAMAGE.
56 */
57
58#include <sys/param.h>
59#include <sys/systm.h>
60#include <sys/malloc.h>
61#include <sys/socket.h>
9bccf70c 62#include <sys/socketvar.h>
1c79356b
A
63#include <sys/sockio.h>
64#include <sys/kernel.h>
9bccf70c 65#include <sys/syslog.h>
316670eb 66#include <libkern/crypto/sha1.h>
2d21ac55 67#include <libkern/OSAtomic.h>
fe8ab488 68#include <kern/locks.h>
1c79356b
A
69
70#include <net/if.h>
71#include <net/if_dl.h>
72#include <net/if_types.h>
73#include <net/route.h>
2d21ac55 74#include <net/kpi_protocol.h>
5ba3f43e 75#include <net/if_llatbl.h>
1c79356b
A
76
77#include <netinet/in.h>
78#include <netinet/in_var.h>
1c79356b 79#include <netinet/if_ether.h>
9bccf70c 80#include <netinet/in_pcb.h>
6d2010ae 81#include <netinet/icmp6.h>
1c79356b
A
82
83#include <netinet/ip6.h>
84#include <netinet6/ip6_var.h>
9bccf70c
A
85#include <netinet6/in6_var.h>
86#include <netinet6/in6_pcb.h>
1c79356b
A
87#include <netinet6/in6_ifattach.h>
88#include <netinet6/ip6_var.h>
89#include <netinet6/nd6.h>
9bccf70c 90#include <netinet6/scope6_var.h>
1c79356b
A
91
92#include <net/net_osdep.h>
39236c6e 93#include <dev/random/randomdev.h>
1c79356b 94
b0d623f7 95u_int32_t in6_maxmtu = 0;
91447636 96extern lck_mtx_t *nd6_mutex;
1c79356b 97
9bccf70c
A
98#if IP6_AUTO_LINKLOCAL
99int ip6_auto_linklocal = IP6_AUTO_LINKLOCAL;
100#else
101int ip6_auto_linklocal = 1; /* enable by default */
102#endif
1c79356b 103
9bccf70c
A
104extern struct inpcbinfo udbinfo;
105extern struct inpcbinfo ripcbinfo;
1c79356b 106
39236c6e
A
107static const unsigned int in6_extra_size = sizeof(struct in6_ifextra);
108static const unsigned int in6_extra_bufsize = in6_extra_size +
109 sizeof(void *) + sizeof(uint64_t);
110
111static int get_rand_iid(struct ifnet *, struct in6_addr *);
112static int in6_generate_tmp_iid(u_int8_t *, const u_int8_t *, u_int8_t *);
113static int in6_select_iid_from_all_hw(struct ifnet *, struct ifnet *,
114 struct in6_addr *);
115static int in6_ifattach_linklocal(struct ifnet *, struct in6_aliasreq *);
91447636 116static int in6_ifattach_loopback(struct ifnet *);
1c79356b 117
1c79356b
A
118/*
119 * Generate a last-resort interface identifier, when the machine has no
120 * IEEE802/EUI64 address sources.
9bccf70c
A
121 * The goal here is to get an interface identifier that is
122 * (1) random enough and (2) does not change across reboot.
316670eb 123 * We currently use SHA1(hostname) for it.
6d2010ae
A
124 *
125 * in6 - upper 64bits are preserved
1c79356b
A
126 */
127static int
39236c6e 128get_rand_iid(
2d21ac55 129 __unused struct ifnet *ifp,
91447636 130 struct in6_addr *in6) /* upper 64bits are preserved */
1c79356b 131{
316670eb
A
132 SHA1_CTX ctxt;
133 u_int8_t digest[SHA1_RESULTLEN];
6d2010ae 134 int hostnlen = strlen(hostname);
9bccf70c 135
9bccf70c 136 /* generate 8 bytes of pseudo-random value. */
39236c6e 137 bzero(&ctxt, sizeof (ctxt));
316670eb
A
138 SHA1Init(&ctxt);
139 SHA1Update(&ctxt, hostname, hostnlen);
140 SHA1Final(digest, &ctxt);
1c79356b 141
39236c6e 142 /* assumes sizeof (digest) > sizeof (iid) */
9bccf70c 143 bcopy(digest, &in6->s6_addr[8], 8);
1c79356b
A
144
145 /* make sure to set "u" bit to local, and "g" bit to individual. */
316670eb
A
146 in6->s6_addr[8] &= ~ND6_EUI64_GBIT; /* g bit to "individual" */
147 in6->s6_addr[8] |= ND6_EUI64_UBIT; /* u bit to "local" */
9bccf70c
A
148
149 /* convert EUI64 into IPv6 interface identifier */
316670eb 150 ND6_EUI64_TO_IFID(in6);
9bccf70c 151
39236c6e 152 return (0);
9bccf70c
A
153}
154
155static int
39236c6e 156in6_generate_tmp_iid(
91447636
A
157 u_int8_t *seed0,
158 const u_int8_t *seed1,
159 u_int8_t *ret)
9bccf70c 160{
316670eb
A
161 SHA1_CTX ctxt;
162 u_int8_t seed[16], nullbuf[8], digest[SHA1_RESULTLEN];
9bccf70c
A
163 u_int32_t val32;
164 struct timeval tv;
165
6d2010ae 166 /* If there's no history, start with a random seed. */
39236c6e
A
167 bzero(nullbuf, sizeof (nullbuf));
168 if (bcmp(nullbuf, seed0, sizeof (nullbuf)) == 0) {
9bccf70c
A
169 int i;
170
171 for (i = 0; i < 2; i++) {
39236c6e
A
172 getmicrotime(&tv);
173 val32 = RandomULong() ^ tv.tv_usec;
174 bcopy(&val32, seed + sizeof (val32) * i,
175 sizeof (val32));
9bccf70c 176 }
55e303ae 177 } else {
9bccf70c 178 bcopy(seed0, seed, 8);
55e303ae 179 }
9bccf70c
A
180
181 /* copy the right-most 64-bits of the given address */
182 /* XXX assumption on the size of IFID */
183 bcopy(seed1, &seed[8], 8);
184
fe8ab488 185 if ((0)) { /* for debugging purposes only */
9bccf70c
A
186 int i;
187
39236c6e 188 printf("%s: new randomized ID from: ", __func__);
9bccf70c
A
189 for (i = 0; i < 16; i++)
190 printf("%02x", seed[i]);
191 printf(" ");
192 }
193
194 /* generate 16 bytes of pseudo-random value. */
39236c6e 195 bzero(&ctxt, sizeof (ctxt));
316670eb 196 SHA1Init(&ctxt);
39236c6e 197 SHA1Update(&ctxt, seed, sizeof (seed));
316670eb 198 SHA1Final(digest, &ctxt);
9bccf70c
A
199
200 /*
6d2010ae 201 * RFC 4941 3.2.1. (3)
316670eb 202 * Take the left-most 64-bits of the SHA1 digest and set bit 6 (the
9bccf70c
A
203 * left-most bit is numbered 0) to zero.
204 */
205 bcopy(digest, ret, 8);
316670eb 206 ret[0] &= ~ND6_EUI64_UBIT;
9bccf70c
A
207
208 /*
209 * XXX: we'd like to ensure that the generated value is not zero
210 * for simplicity. If the caclculated digest happens to be zero,
211 * use a random non-zero value as the last resort.
212 */
39236c6e 213 if (bcmp(nullbuf, ret, sizeof (nullbuf)) == 0) {
6d2010ae 214 nd6log((LOG_INFO,
39236c6e 215 "%s: computed SHA1 value is zero.\n", __func__));
9bccf70c 216
39236c6e 217 getmicrotime(&tv);
9bccf70c
A
218 val32 = random() ^ tv.tv_usec;
219 val32 = 1 + (val32 % (0xffffffff - 1));
220 }
221
222 /*
6d2010ae 223 * RFC 4941 3.2.1. (4)
316670eb 224 * Take the next 64-bits of the SHA1 digest and save them in
9bccf70c 225 * stable storage as the history value to be used in the next
6d2010ae 226 * iteration of the algorithm.
9bccf70c
A
227 */
228 bcopy(&digest[8], seed0, 8);
229
fe8ab488 230 if ((0)) { /* for debugging purposes only */
9bccf70c
A
231 int i;
232
233 printf("to: ");
234 for (i = 0; i < 16; i++)
235 printf("%02x", digest[i]);
236 printf("\n");
237 }
1c79356b 238
39236c6e 239 return (0);
1c79356b
A
240}
241
242/*
39236c6e
A
243 * Get interface identifier for the specified interface using the method in
244 * Appendix A of RFC 4291.
245 *
9bccf70c 246 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
6d2010ae
A
247 *
248 * in6 - upper 64bits are preserved
1c79356b 249 */
6d2010ae 250int
39236c6e 251in6_iid_from_hw(struct ifnet *ifp, struct in6_addr *in6)
9bccf70c 252{
6d2010ae 253 struct ifaddr *ifa = NULL;
1c79356b 254 struct sockaddr_dl *sdl;
9bccf70c
A
255 u_int8_t *addr;
256 size_t addrlen;
257 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
258 static u_int8_t allone[8] =
259 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6d2010ae 260 int err = -1;
9bccf70c 261
91447636
A
262 /* Why doesn't this code use ifnet_addrs? */
263 ifnet_lock_shared(ifp);
6d2010ae 264 ifa = ifp->if_lladdr;
316670eb 265 sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr;
6d2010ae
A
266 if (sdl->sdl_alen == 0) {
267 ifnet_lock_done(ifp);
268 return (-1);
9bccf70c 269 }
6d2010ae 270 IFA_ADDREF(ifa); /* for this routine */
91447636 271 ifnet_lock_done(ifp);
1c79356b 272
6d2010ae 273 IFA_LOCK(ifa);
2d21ac55 274 addr = (u_int8_t *) LLADDR(sdl);
9bccf70c
A
275 addrlen = sdl->sdl_alen;
276
277 /* get EUI64 */
278 switch (ifp->if_type) {
279 case IFT_ETHER:
280 case IFT_FDDI:
6d2010ae 281 case IFT_ISO88025:
9bccf70c
A
282 case IFT_ATM:
283 case IFT_IEEE1394:
91447636
A
284 case IFT_L2VLAN:
285 case IFT_IEEE8023ADLAG:
9bccf70c
A
286#if IFT_IEEE80211
287 case IFT_IEEE80211:
1c79356b 288#endif
b7266188 289 case IFT_BRIDGE:
9bccf70c
A
290 /* IEEE802/EUI64 cases - what others? */
291 /* IEEE1394 uses 16byte length address starting with EUI64 */
292 if (addrlen > 8)
293 addrlen = 8;
294
295 /* look at IEEE802/EUI64 only */
296 if (addrlen != 8 && addrlen != 6)
6d2010ae 297 goto done;
9bccf70c
A
298
299 /*
300 * check for invalid MAC address - on bsdi, we see it a lot
301 * since wildboar configures all-zero MAC on pccard before
302 * card insertion.
303 */
304 if (bcmp(addr, allzero, addrlen) == 0)
6d2010ae 305 goto done;
9bccf70c 306 if (bcmp(addr, allone, addrlen) == 0)
6d2010ae 307 goto done;
9bccf70c
A
308
309 /* make EUI64 address */
310 if (addrlen == 8)
311 bcopy(addr, &in6->s6_addr[8], 8);
312 else if (addrlen == 6) {
313 in6->s6_addr[8] = addr[0];
314 in6->s6_addr[9] = addr[1];
315 in6->s6_addr[10] = addr[2];
316 in6->s6_addr[11] = 0xff;
317 in6->s6_addr[12] = 0xfe;
318 in6->s6_addr[13] = addr[3];
319 in6->s6_addr[14] = addr[4];
320 in6->s6_addr[15] = addr[5];
321 }
322 break;
323
324 case IFT_ARCNET:
325 if (addrlen != 1)
6d2010ae 326 goto done;
9bccf70c 327 if (!addr[0])
6d2010ae 328 goto done;
9bccf70c
A
329
330 bzero(&in6->s6_addr[8], 8);
331 in6->s6_addr[15] = addr[0];
332
333 /*
334 * due to insufficient bitwidth, we mark it local.
335 */
39236c6e
A
336 in6->s6_addr[8] &= ~ND6_EUI64_GBIT; /* g to "individual" */
337 in6->s6_addr[8] |= ND6_EUI64_UBIT; /* u to "local" */
9bccf70c
A
338 break;
339
340 case IFT_GIF:
341#if IFT_STF
342 case IFT_STF:
343#endif
344 /*
39236c6e 345 * RFC2893 says: "SHOULD use IPv4 address as IID source".
9bccf70c
A
346 * however, IPv4 address is not very suitable as unique
347 * identifier source (can be renumbered).
348 * we don't do this.
349 */
6d2010ae
A
350 goto done;
351
352 case IFT_CELLULAR:
353 goto done;
9bccf70c
A
354
355 default:
6d2010ae 356 goto done;
9bccf70c
A
357 }
358
359 /* sanity check: g bit must not indicate "group" */
316670eb 360 if (ND6_EUI64_GROUP(in6))
6d2010ae 361 goto done;
9bccf70c
A
362
363 /* convert EUI64 into IPv6 interface identifier */
316670eb 364 ND6_EUI64_TO_IFID(in6);
9bccf70c
A
365
366 /*
39236c6e 367 * sanity check: iid must not be all zero, avoid conflict with
9bccf70c
A
368 * subnet router anycast
369 */
316670eb 370 if ((in6->s6_addr[8] & ~(ND6_EUI64_GBIT | ND6_EUI64_UBIT)) == 0x00 &&
9bccf70c 371 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
6d2010ae 372 goto done;
9bccf70c
A
373 }
374
6d2010ae
A
375 err = 0; /* found */
376
377done:
378 /* This must not be the last reference to the lladdr */
379 if (IFA_REMREF_LOCKED(ifa) == NULL) {
380 panic("%s: unexpected (missing) refcnt ifa=%p", __func__, ifa);
381 /* NOTREACHED */
382 }
383 IFA_UNLOCK(ifa);
384 return (err);
9bccf70c
A
385}
386
387/*
39236c6e
A
388 * Get interface identifier for the specified interface using the method in
389 * Appendix A of RFC 4291. If it is not available on ifp0, borrow interface
390 * identifier from other information sources.
6d2010ae 391 *
39236c6e
A
392 * ifp - primary EUI64 source
393 * altifp - secondary EUI64 source
394 * in6 - IPv6 address to output IID
9bccf70c
A
395 */
396static int
39236c6e 397in6_select_iid_from_all_hw(
91447636
A
398 struct ifnet *ifp0,
399 struct ifnet *altifp, /* secondary EUI64 source */
400 struct in6_addr *in6)
9bccf70c
A
401{
402 struct ifnet *ifp;
403
404 /* first, try to get it from the interface itself */
39236c6e
A
405 if (in6_iid_from_hw(ifp0, in6) == 0) {
406 nd6log((LOG_DEBUG, "%s: IID derived from HW interface.\n",
9bccf70c
A
407 if_name(ifp0)));
408 goto success;
409 }
410
411 /* try secondary EUI64 source. this basically is for ATM PVC */
39236c6e
A
412 if (altifp && in6_iid_from_hw(altifp, in6) == 0) {
413 nd6log((LOG_DEBUG, "%s: IID from alterate HW interface %s.\n",
9bccf70c
A
414 if_name(ifp0), if_name(altifp)));
415 goto success;
416 }
417
418 /* next, try to get it from some other hardware interface */
91447636
A
419 ifnet_head_lock_shared();
420 TAILQ_FOREACH(ifp, &ifnet_head, if_list) {
9bccf70c 421 if (ifp == ifp0)
1c79356b 422 continue;
39236c6e 423 if (in6_iid_from_hw(ifp, in6) != 0)
9bccf70c
A
424 continue;
425
426 /*
39236c6e 427 * to borrow IID from other interface, IID needs to be
9bccf70c
A
428 * globally unique
429 */
316670eb 430 if (ND6_IFID_UNIVERSAL(in6)) {
39236c6e 431 nd6log((LOG_DEBUG, "%s: borrowed IID from %s\n",
9bccf70c 432 if_name(ifp0), if_name(ifp)));
91447636 433 ifnet_head_done();
9bccf70c
A
434 goto success;
435 }
436 }
91447636 437 ifnet_head_done();
9bccf70c
A
438
439 /* last resort: get from random number source */
39236c6e
A
440 if (get_rand_iid(ifp, in6) == 0) {
441 nd6log((LOG_DEBUG, "%s: IID from PRNG.\n", if_name(ifp0)));
9bccf70c
A
442 goto success;
443 }
444
445 printf("%s: failed to get interface identifier\n", if_name(ifp0));
39236c6e 446 return (-1);
9bccf70c
A
447
448success:
39236c6e 449 nd6log((LOG_INFO, "%s: IID: "
9bccf70c
A
450 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
451 if_name(ifp0),
452 in6->s6_addr[8], in6->s6_addr[9],
453 in6->s6_addr[10], in6->s6_addr[11],
454 in6->s6_addr[12], in6->s6_addr[13],
455 in6->s6_addr[14], in6->s6_addr[15]));
39236c6e 456 return (0);
9bccf70c
A
457}
458
459static int
39236c6e 460in6_ifattach_linklocal(struct ifnet *ifp, struct in6_aliasreq *ifra)
9bccf70c
A
461{
462 struct in6_ifaddr *ia;
2d21ac55 463 struct nd_prefix pr0, *pr;
91447636 464 int i, error;
9bccf70c 465
39236c6e 466 VERIFY(ifra != NULL);
9bccf70c 467
2d21ac55 468 proto_plumb(PF_INET6, ifp);
55e303ae 469
39236c6e
A
470 error = in6_update_ifa(ifp, ifra, IN6_IFAUPDATE_DADDELAY, &ia);
471 if (error != 0) {
9bccf70c
A
472 /*
473 * XXX: When the interface does not support IPv6, this call
474 * would fail in the SIOCSIFADDR ioctl. I believe the
475 * notification is rather confusing in this case, so just
6d2010ae 476 * suppress it. (jinmei@kame.net 20010130)
9bccf70c
A
477 */
478 if (error != EAFNOSUPPORT)
39236c6e 479 nd6log((LOG_NOTICE, "%s: failed to "
9bccf70c
A
480 "configure a link-local address on %s "
481 "(errno=%d)\n",
39236c6e 482 __func__, if_name(ifp), error));
6d2010ae 483 return (EADDRNOTAVAIL);
9bccf70c 484 }
39236c6e 485 VERIFY(ia != NULL);
9bccf70c 486
9bccf70c 487 /*
6d2010ae 488 * Make the link-local prefix (fe80::%link/64) as on-link.
9bccf70c
A
489 * Since we'd like to manage prefixes separately from addresses,
490 * we make an ND6 prefix structure for the link-local prefix,
491 * and add it to the prefix list as a never-expire prefix.
492 * XXX: this change might affect some existing code base...
493 */
39236c6e 494 bzero(&pr0, sizeof (pr0));
6d2010ae 495 lck_mtx_init(&pr0.ndpr_lock, ifa_mtx_grp, ifa_mtx_attr);
9bccf70c
A
496 pr0.ndpr_ifp = ifp;
497 /* this should be 64 at this moment. */
39236c6e
A
498 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, NULL);
499 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
500 pr0.ndpr_prefix = ifra->ifra_addr;
9bccf70c
A
501 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
502 for (i = 0; i < 4; i++) {
503 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
504 in6mask64.s6_addr32[i];
505 }
506 /*
507 * Initialize parameters. The link-local prefix must always be
508 * on-link, and its lifetimes never expire.
509 */
510 pr0.ndpr_raf_onlink = 1;
511 pr0.ndpr_raf_auto = 1; /* probably meaningless */
512 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
513 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
6d2010ae 514 pr0.ndpr_stateflags |= NDPRF_STATIC;
9bccf70c
A
515 /*
516 * Since there is no other link-local addresses, nd6_prefix_lookup()
517 * probably returns NULL. However, we cannot always expect the result.
518 * For example, if we first remove the (only) existing link-local
519 * address, and then reconfigure another one, the prefix is still
520 * valid with referring to the old link-local address.
521 */
3e170ce0 522 if ((pr = nd6_prefix_lookup(&pr0, ND6_PREFIX_EXPIRY_UNSPEC)) == NULL) {
6d2010ae
A
523 if ((error = nd6_prelist_add(&pr0, NULL, &pr, TRUE)) != 0) {
524 IFA_REMREF(&ia->ia_ifa);
525 lck_mtx_destroy(&pr0.ndpr_lock, ifa_mtx_grp);
39236c6e 526 return (error);
b0d623f7 527 }
9bccf70c
A
528 }
529
fe8ab488 530 in6_post_msg(ifp, KEV_INET6_NEW_LL_ADDR, ia, NULL);
39236c6e 531 IFA_REMREF(&ia->ia_ifa);
2d21ac55
A
532
533 /* Drop use count held above during lookup/add */
534 if (pr != NULL)
6d2010ae
A
535 NDPR_REMREF(pr);
536
537 lck_mtx_destroy(&pr0.ndpr_lock, ifa_mtx_grp);
39236c6e 538 return (0);
9bccf70c
A
539}
540
541static int
91447636
A
542in6_ifattach_loopback(
543 struct ifnet *ifp) /* must be IFT_LOOP */
9bccf70c
A
544{
545 struct in6_aliasreq ifra;
39236c6e 546 struct in6_ifaddr *ia;
9bccf70c
A
547 int error;
548
39236c6e 549 bzero(&ifra, sizeof (ifra));
9bccf70c
A
550
551 /*
552 * in6_update_ifa() does not use ifra_name, but we accurately set it
553 * for safety.
554 */
fe8ab488 555 strlcpy(ifra.ifra_name, if_name(ifp), sizeof (ifra.ifra_name));
9bccf70c 556
39236c6e 557 ifra.ifra_prefixmask.sin6_len = sizeof (struct sockaddr_in6);
9bccf70c
A
558 ifra.ifra_prefixmask.sin6_family = AF_INET6;
559 ifra.ifra_prefixmask.sin6_addr = in6mask128;
560
561 /*
562 * Always initialize ia_dstaddr (= broadcast address) to loopback
563 * address. Follows IPv4 practice - see in_ifinit().
564 */
39236c6e 565 ifra.ifra_dstaddr.sin6_len = sizeof (struct sockaddr_in6);
9bccf70c
A
566 ifra.ifra_dstaddr.sin6_family = AF_INET6;
567 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
568
39236c6e 569 ifra.ifra_addr.sin6_len = sizeof (struct sockaddr_in6);
9bccf70c
A
570 ifra.ifra_addr.sin6_family = AF_INET6;
571 ifra.ifra_addr.sin6_addr = in6addr_loopback;
572
573 /* the loopback address should NEVER expire. */
574 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
575 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
576
55e303ae 577 /* we don't need to perform DAD on loopback interfaces. */
9bccf70c
A
578 ifra.ifra_flags |= IN6_IFF_NODAD;
579
39236c6e
A
580 /* add the new interface address */
581 error = in6_update_ifa(ifp, &ifra, 0, &ia);
582 if (error != 0) {
583 nd6log((LOG_ERR,
584 "%s: failed to configure loopback address %s (error=%d)\n",
585 __func__, if_name(ifp), error));
586 VERIFY(ia == NULL);
6d2010ae 587 return (EADDRNOTAVAIL);
9bccf70c
A
588 }
589
39236c6e
A
590 VERIFY(ia != NULL);
591 IFA_REMREF(&ia->ia_ifa);
592 return (0);
1c79356b
A
593}
594
595/*
9bccf70c 596 * compute NI group address, based on the current hostname setting.
39236c6e 597 * see RFC 4620.
9bccf70c
A
598 *
599 * when ifp == NULL, the caller is responsible for filling scopeid.
1c79356b 600 */
9bccf70c 601int
91447636
A
602in6_nigroup(
603 struct ifnet *ifp,
604 const char *name,
605 int namelen,
606 struct in6_addr *in6)
1c79356b 607{
9bccf70c
A
608 const char *p;
609 u_char *q;
316670eb
A
610 SHA1_CTX ctxt;
611 u_int8_t digest[SHA1_RESULTLEN];
9bccf70c
A
612 char l;
613 char n[64]; /* a single label must not exceed 63 chars */
614
615 if (!namelen || !name)
39236c6e 616 return (-1);
9bccf70c
A
617
618 p = name;
619 while (p && *p && *p != '.' && p - name < namelen)
620 p++;
39236c6e
A
621 if (p - name > sizeof (n) - 1)
622 return (-1); /* label too long */
9bccf70c 623 l = p - name;
fe8ab488 624 strlcpy(n, name, l);
9bccf70c 625 n[(int)l] = '\0';
2d21ac55 626 for (q = (u_char *) n; *q; q++) {
9bccf70c
A
627 if ('A' <= *q && *q <= 'Z')
628 *q = *q - 'A' + 'a';
629 }
1c79356b 630
39236c6e
A
631 /* generate 16 bytes of pseudo-random value. */
632 bzero(&ctxt, sizeof (ctxt));
316670eb 633 SHA1Init(&ctxt);
39236c6e 634 SHA1Update(&ctxt, &l, sizeof (l));
316670eb
A
635 SHA1Update(&ctxt, n, l);
636 SHA1Final(digest, &ctxt);
9bccf70c 637
39236c6e 638 bzero(in6, sizeof (*in6));
6d2010ae 639 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
9bccf70c 640 in6->s6_addr8[11] = 2;
39236c6e
A
641 in6->s6_addr8[12] = 0xff;
642 /* copy first 3 bytes of prefix into address */
643 bcopy(digest, &in6->s6_addr8[13], 3);
6d2010ae
A
644 if (in6_setscope(in6, ifp, NULL))
645 return (-1); /* XXX: should not fail */
9bccf70c 646
39236c6e 647 return (0);
9bccf70c
A
648}
649
6d2010ae
A
650int
651in6_domifattach(struct ifnet *ifp)
9bccf70c 652{
39236c6e
A
653 int error;
654
655 VERIFY(ifp != NULL);
1c79356b 656
39236c6e
A
657 error = proto_plumb(PF_INET6, ifp);
658 if (error != 0) {
6d2010ae 659 if (error != EEXIST)
39236c6e
A
660 log(LOG_ERR, "%s: proto_plumb returned %d if=%s\n",
661 __func__, error, if_name(ifp));
6d2010ae 662 } else {
39236c6e
A
663 error = in6_ifattach_prelim(ifp);
664 if (error != 0) {
665 int errorx;
666
667 log(LOG_ERR,
668 "%s: in6_ifattach_prelim returned %d if=%s%d\n",
669 __func__, error, ifp->if_name, ifp->if_unit);
670
671 errorx = proto_unplumb(PF_INET6, ifp);
672 if (errorx != 0) /* XXX should not fail */
673 log(LOG_ERR,
674 "%s: proto_unplumb returned %d if=%s%d\n",
675 __func__, errorx, ifp->if_name,
676 ifp->if_unit);
677 }
1c79356b 678 }
9bccf70c 679
6d2010ae 680 return (error);
9bccf70c
A
681}
682
b0d623f7 683int
39236c6e 684in6_ifattach_prelim(struct ifnet *ifp)
9bccf70c 685{
39236c6e
A
686 struct in6_ifextra *ext;
687 void **pbuf, *base;
688 int error = 0;
3e170ce0 689 struct in6_ifaddr *ia6 = NULL;
55e303ae 690
39236c6e 691 VERIFY(ifp != NULL);
1c79356b 692
39236c6e 693 /* quirks based on interface type */
9bccf70c
A
694 switch (ifp->if_type) {
695#if IFT_STF
696 case IFT_STF:
697 /*
55e303ae
A
698 * 6to4 interface is a very special kind of beast.
699 * no multicast, no linklocal. RFC2529 specifies how to make
700 * linklocals for 6to4 interface, but there's no use and
701 * it is rather harmful to have one.
9bccf70c 702 */
39236c6e 703 goto skipmcast;
0b4e3aa0 704#endif
9bccf70c 705 default:
1c79356b 706 break;
1c79356b
A
707 }
708
1c79356b 709 /*
39236c6e
A
710 * IPv6 requires multicast capability at the interface.
711 * (previously, this was a silent error.)
1c79356b 712 */
9bccf70c 713 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
5ba3f43e 714 nd6log0((LOG_INFO, "in6_ifattach: ",
39236c6e 715 "%s is not multicast capable, IPv6 not enabled\n",
6d2010ae 716 if_name(ifp)));
39236c6e 717 return (EINVAL);
1c79356b
A
718 }
719
39236c6e
A
720#if IFT_STF
721skipmcast:
722#endif
723
724 if (ifp->if_inet6data == NULL) {
3e170ce0 725 ext = (struct in6_ifextra *)_MALLOC(in6_extra_bufsize, M_IFADDR,
39236c6e
A
726 M_WAITOK|M_ZERO);
727 if (!ext)
728 return (ENOMEM);
729 base = (void *)P2ROUNDUP((intptr_t)ext + sizeof(uint64_t),
730 sizeof(uint64_t));
3e170ce0 731 VERIFY(((intptr_t)base + in6_extra_size) <=
39236c6e
A
732 ((intptr_t)ext + in6_extra_bufsize));
733 pbuf = (void **)((intptr_t)base - sizeof(void *));
734 *pbuf = ext;
735 ifp->if_inet6data = base;
5ba3f43e 736 IN6_IFEXTRA(ifp)->ii_llt = in6_lltattach(ifp);
39236c6e
A
737 VERIFY(IS_P2ALIGNED(ifp->if_inet6data, sizeof(uint64_t)));
738 } else {
739 /*
3e170ce0 740 * Since the structure is never freed, we need to zero out
39236c6e
A
741 * some of its members. We avoid zeroing out the scope6
742 * structure on purpose because other threads might be
743 * using its contents.
744 */
745 bzero(&IN6_IFEXTRA(ifp)->icmp6_ifstat,
746 sizeof(IN6_IFEXTRA(ifp)->icmp6_ifstat));
747 bzero(&IN6_IFEXTRA(ifp)->in6_ifstat,
748 sizeof(IN6_IFEXTRA(ifp)->in6_ifstat));
5ba3f43e
A
749 IN6_IFEXTRA(ifp)->netsig_len = 0;
750 bzero(&IN6_IFEXTRA(ifp)->netsig,
751 sizeof(IN6_IFEXTRA(ifp)->netsig));
752 bzero(IN6_IFEXTRA(ifp)->nat64_prefixes, sizeof(IN6_IFEXTRA(ifp)->nat64_prefixes));
753 /* XXX TBD Purge the layer two table */
3e170ce0
A
754 /*
755 * XXX When recycling, nd_ifinfo gets initialized, other
756 * than the lock, inside nd6_ifattach
757 */
39236c6e
A
758 }
759
3e170ce0
A
760 /*
761 * XXX Only initialize NDP ifinfo for the interface
762 * if interface has not yet been configured with
763 * link local IPv6 address.
764 * Could possibly be optimized with an interface flag if need
765 * be. For now using in6ifa_ifpforlinklocal.
766 */
767 ia6 = in6ifa_ifpforlinklocal(ifp, 0);
768 if (ia6 == NULL) {
769 /* initialize NDP variables */
770 nd6_ifattach(ifp);
771 } else {
772 VERIFY(ND_IFINFO(ifp)->initialized);
773 IFA_REMREF(&ia6->ia_ifa);
774 ia6 = NULL;
775 }
39236c6e
A
776 scope6_ifattach(ifp);
777
778 /* initialize loopback interface address */
9bccf70c 779 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
39236c6e
A
780 error = in6_ifattach_loopback(ifp);
781 if (error != 0) {
782 log(LOG_ERR, "%s: in6_ifattach_loopback returned %d\n",
783 __func__, error, ifp->if_name,
784 ifp->if_unit);
785 return (error);
6d2010ae 786 }
1c79356b
A
787 }
788
39236c6e
A
789 /* update dynamically. */
790 if (in6_maxmtu < ifp->if_mtu)
791 in6_maxmtu = ifp->if_mtu;
792
793 VERIFY(error == 0);
794 return (0);
795}
796
39037602
A
797/*
798 * This routine is only meant to configure IPv6 Link Local
799 * addresses.
800 */
39236c6e
A
801int
802in6_ifattach_aliasreq(struct ifnet *ifp, struct ifnet *altifp,
803 struct in6_aliasreq *ifra0)
804{
805 int error;
806 struct in6_ifaddr *ia6;
807 struct in6_aliasreq ifra;
808
809 error = in6_ifattach_prelim(ifp);
810 if (error != 0)
811 return (error);
812
813 if (!ip6_auto_linklocal)
814 return (0);
815
39037602
A
816 /*
817 * Assign a link-local address, only if there isn't one here already.
818 * XXX If we ever allow more than one LLA on the interface
819 * make sure that the corresponding prefix on the prefixlist
820 * is reference counted and the address's prefix pointer
821 * points to the prefix.
822 */
39236c6e
A
823 ia6 = in6ifa_ifpforlinklocal(ifp, 0);
824 if (ia6 != NULL) {
825 IFA_REMREF(&ia6->ia_ifa);
826 return (0);
827 }
828
829 bzero(&ifra, sizeof (ifra));
830
1c79356b 831 /*
39236c6e
A
832 * in6_update_ifa() does not use ifra_name, but we accurately set it
833 * for safety.
1c79356b 834 */
fe8ab488 835 strlcpy(ifra.ifra_name, if_name(ifp), sizeof (ifra.ifra_name));
39236c6e
A
836
837 /* Initialize the IPv6 interface address in our in6_aliasreq block */
39037602 838 if (ifra0 != NULL) {
39236c6e
A
839 /* interface provided both addresses for us */
840 struct sockaddr_in6 *sin6 = &ifra.ifra_addr;
841 struct in6_addr *in6 = &sin6->sin6_addr;
842 boolean_t ok = TRUE;
843
844 bcopy(&ifra0->ifra_addr, sin6, sizeof (struct sockaddr_in6));
845
846 if (sin6->sin6_family != AF_INET6 || sin6->sin6_port != 0)
847 ok = FALSE;
848 if (ok && (in6->s6_addr16[0] != htons(0xfe80)))
849 ok = FALSE;
850 if (ok) {
851 if (sin6->sin6_scope_id == 0 && in6->s6_addr16[1] == 0)
852 in6->s6_addr16[1] = htons(ifp->if_index);
853 else if (sin6->sin6_scope_id != 0 &&
854 sin6->sin6_scope_id != ifp->if_index)
855 ok = FALSE;
856 else if (in6->s6_addr16[1] != 0 &&
857 ntohs(in6->s6_addr16[1]) != ifp->if_index)
858 ok = FALSE;
859 }
860 if (ok && (in6->s6_addr32[1] != 0))
861 ok = FALSE;
862 if (!ok)
863 return (EINVAL);
864 } else {
865 ifra.ifra_addr.sin6_family = AF_INET6;
866 ifra.ifra_addr.sin6_len = sizeof (struct sockaddr_in6);
867 ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
868 ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
869 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
870 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
871 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
872 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
873 } else {
874 if (in6_select_iid_from_all_hw(ifp, altifp,
875 &ifra.ifra_addr.sin6_addr) != 0) {
876 nd6log((LOG_ERR, "%s: no IID available\n",
6d2010ae 877 if_name(ifp)));
39236c6e 878 return (EADDRNOTAVAIL);
9bccf70c 879 }
1c79356b
A
880 }
881 }
882
39236c6e
A
883 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
884 return (EADDRNOTAVAIL);
9bccf70c 885
39236c6e
A
886 /* Set the prefix mask */
887 ifra.ifra_prefixmask.sin6_len = sizeof (struct sockaddr_in6);
888 ifra.ifra_prefixmask.sin6_family = AF_INET6;
889 ifra.ifra_prefixmask.sin6_addr = in6mask64;
1c79356b 890
39236c6e
A
891 /* link-local addresses should NEVER expire. */
892 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
893 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
894
895 /* Attach the link-local address */
896 if (in6_ifattach_linklocal(ifp, &ifra) != 0) {
897 nd6log((LOG_INFO,
898 "%s: %s could not attach link-local address.\n",
899 __func__, if_name(ifp)));
900 /* NB: not an error */
901 }
902
903 return (0);
904}
905
906int
39037602 907in6_ifattach_llcgareq(struct ifnet *ifp, struct in6_cgareq *llcgasr)
39236c6e
A
908{
909 struct in6_aliasreq ifra;
3e170ce0
A
910 struct in6_ifaddr *ia6 = NULL;
911 struct nd_ifinfo *ndi = NULL;
39236c6e
A
912 int error;
913
39037602 914 VERIFY(llcgasr != NULL);
39236c6e
A
915
916 error = in6_ifattach_prelim(ifp);
917 if (error != 0)
918 return (error);
919
39037602 920 if (!ip6_auto_linklocal)
39236c6e
A
921 return (0);
922
923 if (nd6_send_opstate == ND6_SEND_OPMODE_DISABLED)
924 return (ENXIO);
925
39236c6e
A
926 ndi = ND_IFINFO(ifp);
927 VERIFY(ndi != NULL && ndi->initialized);
928 if ((ndi->flags & ND6_IFF_INSECURE) != 0) {
39236c6e
A
929 return (ENXIO);
930 }
39236c6e 931
39037602
A
932 /*
933 * Assign a link-local address, only if there isn't one here already.
934 * XXX If we ever allow more than one LLA on the interface
935 * make sure that the corresponding prefix on the prefixlist
936 * is reference counted and the address's prefix pointer
937 * points to the prefix.
938 */
39236c6e
A
939 ia6 = in6ifa_ifpforlinklocal(ifp, 0);
940 if (ia6 != NULL) {
941 IFA_REMREF(&ia6->ia_ifa);
942 return (0);
943 }
944
945 bzero(&ifra, sizeof (ifra));
fe8ab488 946 strlcpy(ifra.ifra_name, if_name(ifp), sizeof (ifra.ifra_name));
39236c6e
A
947
948 ifra.ifra_addr.sin6_family = AF_INET6;
949 ifra.ifra_addr.sin6_len = sizeof (struct sockaddr_in6);
950 ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
951 ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
952 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
953 ifra.ifra_flags = IN6_IFF_SECURED;
954
955 in6_cga_node_lock();
39037602 956 if (in6_cga_generate(&llcgasr->cgar_cgaprep, 0,
39236c6e
A
957 &ifra.ifra_addr.sin6_addr)) {
958 in6_cga_node_unlock();
959 return (EADDRNOTAVAIL);
960 }
961 in6_cga_node_unlock();
962
963 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
964 return (EADDRNOTAVAIL);
965
966 /* Set the prefix mask */
967 ifra.ifra_prefixmask.sin6_len = sizeof (struct sockaddr_in6);
968 ifra.ifra_prefixmask.sin6_family = AF_INET6;
969 ifra.ifra_prefixmask.sin6_addr = in6mask64;
970
971 /*
972 * link-local addresses should NEVER expire, but cryptographic
973 * ones may have finite preferred lifetime [if it's important to
974 * keep them from being used by applications as persistent device
975 * identifiers].
976 */
977 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
39037602 978 ifra.ifra_lifetime.ia6t_pltime = llcgasr->cgar_lifetime.ia6t_pltime;
39236c6e
A
979
980 /* Attach the link-local address */
981 if (in6_ifattach_linklocal(ifp, &ifra) != 0) {
982 /* NB: not an error */
983 nd6log((LOG_INFO,
984 "%s: %s could not attach link-local address.\n",
985 __func__, if_name(ifp)));
986 }
987
988 VERIFY(error == 0);
989 return (error);
1c79356b
A
990}
991
992/*
993 * NOTE: in6_ifdetach() does not support loopback if at this moment.
994 */
995void
6d2010ae 996in6_ifdetach(struct ifnet *ifp)
1c79356b 997{
6d2010ae
A
998 struct in6_ifaddr *ia, *oia;
999 struct ifaddr *ifa;
1c79356b 1000 struct rtentry *rt;
1c79356b 1001 struct sockaddr_in6 sin6;
6d2010ae
A
1002 struct in6_multi_mship *imm;
1003 int unlinked;
1c79356b 1004
5ba3f43e 1005 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
1c79356b
A
1006
1007 /* remove neighbor management table */
1008 nd6_purge(ifp);
1009
5ba3f43e
A
1010 if (LLTABLE6(ifp))
1011 lltable_free(LLTABLE6(ifp));
1012
9bccf70c 1013 /* nuke any of IPv6 addresses we have */
6d2010ae
A
1014 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1015 ia = in6_ifaddrs;
1016 while (ia != NULL) {
1017 if (ia->ia_ifa.ifa_ifp != ifp) {
1018 ia = ia->ia_next;
9bccf70c 1019 continue;
6d2010ae
A
1020 }
1021 IFA_ADDREF(&ia->ia_ifa); /* for us */
1022 lck_rw_done(&in6_ifaddr_rwlock);
1023 in6_purgeaddr(&ia->ia_ifa);
1024 IFA_REMREF(&ia->ia_ifa); /* for us */
1025 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1026 /*
1027 * Purging the address caused in6_ifaddr_rwlock
1028 * to be dropped and reacquired;
1029 * therefore search again from the beginning
1030 * of in6_ifaddrs list.
1031 */
1032 ia = in6_ifaddrs;
9bccf70c 1033 }
6d2010ae 1034 lck_rw_done(&in6_ifaddr_rwlock);
91447636
A
1035
1036 ifnet_lock_exclusive(ifp);
9bccf70c
A
1037
1038 /* undo everything done by in6_ifattach(), just in case */
6d2010ae
A
1039 ifa = TAILQ_FIRST(&ifp->if_addrlist);
1040 while (ifa != NULL) {
1041 IFA_LOCK(ifa);
1042 if (ifa->ifa_addr->sa_family != AF_INET6 ||
1043 !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->
1044 sin6_addr)) {
1045 IFA_UNLOCK(ifa);
1046 ifa = TAILQ_NEXT(ifa, ifa_list);
1c79356b
A
1047 continue;
1048 }
1049
1050 ia = (struct in6_ifaddr *)ifa;
1051
6d2010ae
A
1052 /* hold a reference for this routine */
1053 IFA_ADDREF_LOCKED(ifa);
1054 /* remove from the linked list */
1055 if_detach_ifa(ifp, ifa);
1056 IFA_UNLOCK(ifa);
1057
1058 /*
1059 * Leaving the multicast group(s) may involve freeing the
1060 * link address multicast structure(s) for the interface,
1061 * which is protected by ifnet lock. To avoid violating
1062 * lock ordering, we must drop ifnet lock before doing so.
1063 * The ifa won't go away since we held a refcnt above.
1064 */
1065 ifnet_lock_done(ifp);
1066
1067 /*
1068 * We have to do this work manually here instead of calling
1069 * in6_purgeaddr() since in6_purgeaddr() uses the RTM_HOST flag.
1070 */
1071
1072 /*
1073 * leave from multicast groups we have joined for the interface
1074 */
1075 IFA_LOCK(ifa);
1076 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1077 LIST_REMOVE(imm, i6mm_chain);
1078 IFA_UNLOCK(ifa);
1079 in6_leavegroup(imm);
1080 IFA_LOCK(ifa);
1c79356b
A
1081 }
1082
6d2010ae
A
1083 /* remove from the routing table */
1084 if (ia->ia_flags & IFA_ROUTE) {
1085 IFA_UNLOCK(ifa);
1086 rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0);
1087 if (rt != NULL) {
1088 (void) rtrequest(RTM_DELETE,
1089 (struct sockaddr *)&ia->ia_addr,
1090 (struct sockaddr *)&ia->ia_addr,
1091 (struct sockaddr *)&ia->ia_prefixmask,
1092 rt->rt_flags, (struct rtentry **)0);
1093 rtfree(rt);
1094 }
1095 } else {
1096 IFA_UNLOCK(ifa);
1097 }
1c79356b
A
1098
1099 /* also remove from the IPv6 address chain(itojun&jinmei) */
6d2010ae 1100 unlinked = 1;
1c79356b 1101 oia = ia;
6d2010ae
A
1102 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1103 if (oia == (ia = in6_ifaddrs)) {
91447636 1104 in6_ifaddrs = ia->ia_next;
6d2010ae 1105 } else {
1c79356b
A
1106 while (ia->ia_next && (ia->ia_next != oia))
1107 ia = ia->ia_next;
6d2010ae 1108 if (ia->ia_next) {
1c79356b 1109 ia->ia_next = oia->ia_next;
6d2010ae
A
1110 } else {
1111 nd6log((LOG_ERR,
9bccf70c
A
1112 "%s: didn't unlink in6ifaddr from "
1113 "list\n", if_name(ifp)));
6d2010ae 1114 unlinked = 0;
9bccf70c 1115 }
1c79356b 1116 }
6d2010ae
A
1117 lck_rw_done(&in6_ifaddr_rwlock);
1118
1119 ifa = &oia->ia_ifa;
1120 /*
1121 * release another refcnt for the link from in6_ifaddrs.
1122 * Do this only if it's not already unlinked in the event
39236c6e
A
1123 * that we lost the race, since in6_ifaddr_rwlock was
1124 * momentarily dropped above.
6d2010ae
A
1125 */
1126 if (unlinked)
1127 IFA_REMREF(ifa);
1128 /* release reference held for this routine */
1129 IFA_REMREF(ifa);
1c79356b 1130
6d2010ae
A
1131 /*
1132 * This is suboptimal, but since we dropped ifnet lock above
1133 * the list might have changed. Repeat the search from the
1134 * beginning until we find the first eligible IPv6 address.
1135 */
1136 ifnet_lock_exclusive(ifp);
1137 ifa = TAILQ_FIRST(&ifp->if_addrlist);
1c79356b 1138 }
91447636 1139 ifnet_lock_done(ifp);
1c79356b 1140
39236c6e
A
1141 /* invalidate route caches */
1142 routegenid_inet6_update();
1143
9bccf70c
A
1144 /*
1145 * remove neighbor management table. we call it twice just to make
1146 * sure we nuke everything. maybe we need just one call.
1147 * XXX: since the first call did not release addresses, some prefixes
1148 * might remain. We should call nd6_purge() again to release the
1149 * prefixes after removing all addresses above.
1150 * (Or can we just delay calling nd6_purge until at this point?)
1151 */
1c79356b
A
1152 nd6_purge(ifp);
1153
1154 /* remove route to link-local allnodes multicast (ff02::1) */
39236c6e
A
1155 bzero(&sin6, sizeof (sin6));
1156 sin6.sin6_len = sizeof (struct sockaddr_in6);
1c79356b
A
1157 sin6.sin6_family = AF_INET6;
1158 sin6.sin6_addr = in6addr_linklocal_allnodes;
1159 sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
b0d623f7 1160 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0);
2d21ac55 1161 if (rt != NULL) {
b0d623f7 1162 RT_LOCK(rt);
39236c6e 1163 if (rt->rt_ifp == ifp) {
b0d623f7
A
1164 /*
1165 * Prevent another thread from modifying rt_key,
1166 * rt_gateway via rt_setgate() after the rt_lock
1167 * is dropped by marking the route as defunct.
1168 */
1169 rt->rt_flags |= RTF_CONDEMNED;
1170 RT_UNLOCK(rt);
1171 (void) rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1172 rt_mask(rt), rt->rt_flags, 0);
1173 } else {
1174 RT_UNLOCK(rt);
2d21ac55 1175 }
b0d623f7 1176 rtfree(rt);
1c79356b
A
1177 }
1178}
9bccf70c
A
1179
1180void
39236c6e
A
1181in6_iid_mktmp(struct ifnet *ifp, u_int8_t *retbuf, const u_int8_t *baseid,
1182 int generate)
9bccf70c
A
1183{
1184 u_int8_t nullbuf[8];
3e170ce0 1185 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
9bccf70c 1186
316670eb
A
1187 VERIFY(ndi != NULL && ndi->initialized);
1188 lck_mtx_lock(&ndi->lock);
39236c6e
A
1189 bzero(nullbuf, sizeof (nullbuf));
1190 if (bcmp(ndi->randomid, nullbuf, sizeof (nullbuf)) == 0) {
9bccf70c
A
1191 /* we've never created a random ID. Create a new one. */
1192 generate = 1;
1193 }
1194
1195 if (generate) {
39236c6e 1196 bcopy(baseid, ndi->randomseed1, sizeof (ndi->randomseed1));
9bccf70c 1197
39236c6e
A
1198 /* in6_generate_tmp_iid will update seedn and buf */
1199 (void) in6_generate_tmp_iid(ndi->randomseed0, ndi->randomseed1,
1200 ndi->randomid);
9bccf70c 1201 }
39236c6e 1202
9bccf70c 1203 bcopy(ndi->randomid, retbuf, 8);
316670eb 1204 lck_mtx_unlock(&ndi->lock);
9bccf70c
A
1205}
1206
9bccf70c 1207void
316670eb 1208in6_tmpaddrtimer(void *arg)
9bccf70c 1209{
316670eb 1210#pragma unused(arg)
3e170ce0
A
1211 struct ifnet *ifp = NULL;
1212 struct nd_ifinfo *ndi = NULL;
9bccf70c 1213 u_int8_t nullbuf[8];
9bccf70c 1214
39236c6e
A
1215 timeout(in6_tmpaddrtimer, (caddr_t)0, (ip6_temp_preferred_lifetime -
1216 ip6_desync_factor - ip6_temp_regen_advance) * hz);
9bccf70c 1217
39236c6e 1218 bzero(nullbuf, sizeof (nullbuf));
3e170ce0
A
1219 ifnet_head_lock_shared();
1220 for (ifp = ifnet_head.tqh_first; ifp;
1221 ifp = ifp->if_link.tqe_next) {
1222 ndi = ND_IFINFO(ifp);
1223 if ((NULL == ndi) || (FALSE == ndi->initialized)) {
316670eb 1224 continue;
3e170ce0 1225 }
316670eb 1226 lck_mtx_lock(&ndi->lock);
39236c6e 1227 if (bcmp(ndi->randomid, nullbuf, sizeof (nullbuf)) != 0) {
316670eb
A
1228 /*
1229 * We've been generating a random ID on this interface.
1230 * Create a new one.
1231 */
39236c6e
A
1232 (void) in6_generate_tmp_iid(ndi->randomseed0,
1233 ndi->randomseed1, ndi->randomid);
9bccf70c 1234 }
316670eb 1235 lck_mtx_unlock(&ndi->lock);
9bccf70c 1236 }
3e170ce0 1237 ifnet_head_done();
9bccf70c 1238}