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