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