xref: /xnu-8796.101.5/bsd/netinet6/in6_ifattach.c (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
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/sha1.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 #include <net/if_llatbl.h>
76 
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
79 #include <netinet/if_ether.h>
80 #include <netinet/in_pcb.h>
81 #include <netinet/icmp6.h>
82 
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet6/in6_var.h>
86 #include <netinet6/in6_pcb.h>
87 #include <netinet6/in6_ifattach.h>
88 #include <netinet6/ip6_var.h>
89 #include <netinet6/nd6.h>
90 #include <netinet6/scope6_var.h>
91 
92 #include <net/net_osdep.h>
93 #include <dev/random/randomdev.h>
94 
95 u_int32_t in6_maxmtu = 0;
96 
97 #if IP6_AUTO_LINKLOCAL
98 int ip6_auto_linklocal = IP6_AUTO_LINKLOCAL;
99 #else
100 int ip6_auto_linklocal = 1;     /* enable by default */
101 #endif
102 
103 extern struct inpcbinfo udbinfo;
104 extern struct inpcbinfo ripcbinfo;
105 
106 static int get_rand_iid(struct ifnet *, struct in6_addr *);
107 static int in6_generate_tmp_iid(u_int8_t *, const u_int8_t *, u_int8_t *);
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 SHA1(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 	SHA1_CTX ctxt;
128 	u_int8_t digest[SHA1_RESULTLEN];
129 	size_t hostnlen;
130 
131 	/* generate 8 bytes of pseudo-random value. */
132 	bzero(&ctxt, sizeof(ctxt));
133 	SHA1Init(&ctxt);
134 	lck_mtx_lock(&hostname_lock);
135 	hostnlen = strlen(hostname);
136 	SHA1Update(&ctxt, hostname, hostnlen);
137 	lck_mtx_unlock(&hostname_lock);
138 	SHA1Final(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
in6_generate_tmp_iid(u_int8_t * seed0,const u_int8_t * seed1,u_int8_t * ret)154 in6_generate_tmp_iid(
155 	u_int8_t *seed0,
156 	const u_int8_t *seed1,
157 	u_int8_t *ret)
158 {
159 	SHA1_CTX ctxt;
160 	u_int8_t seed[16], nullbuf[8], digest[SHA1_RESULTLEN];
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 ((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 
193 	/* generate 16 bytes of pseudo-random value. */
194 	bzero(&ctxt, sizeof(ctxt));
195 	SHA1Init(&ctxt);
196 	SHA1Update(&ctxt, seed, sizeof(seed));
197 	SHA1Final(digest, &ctxt);
198 
199 	/*
200 	 * RFC 4941 3.2.1. (3)
201 	 * Take the left-most 64-bits of the SHA1 digest and set bit 6 (the
202 	 * left-most bit is numbered 0) to zero.
203 	 */
204 	bcopy(digest, ret, 8);
205 	ret[0] &= ~ND6_EUI64_UBIT;
206 
207 	/*
208 	 * XXX: we'd like to ensure that the generated value is not zero
209 	 * for simplicity.  If the caclculated digest happens to be zero,
210 	 * use a random non-zero value as the last resort.
211 	 */
212 	if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
213 		nd6log(info,
214 		    "%s: computed SHA1 value is zero.\n", __func__);
215 
216 		getmicrotime(&tv);
217 		val32 = random() ^ tv.tv_usec;
218 		val32 = 1 + (val32 % (0xffffffff - 1));
219 	}
220 
221 	/*
222 	 * RFC 4941 3.2.1. (4)
223 	 * Take the next 64-bits of the SHA1 digest and save them in
224 	 * stable storage as the history value to be used in the next
225 	 * iteration of the algorithm.
226 	 */
227 	bcopy(&digest[8], seed0, 8);
228 
229 	if ((0)) {              /* for debugging purposes only */
230 		int i;
231 
232 		printf("to: ");
233 		for (i = 0; i < 16; i++) {
234 			printf("%02x", digest[i]);
235 		}
236 		printf("\n");
237 	}
238 
239 	return 0;
240 }
241 
242 /*
243  * Get interface identifier for the specified interface using the method in
244  * Appendix A of RFC 4291.
245  *
246  * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
247  *
248  * in6 - upper 64bits are preserved
249  */
250 int
in6_iid_from_hw(struct ifnet * ifp,struct in6_addr * in6)251 in6_iid_from_hw(struct ifnet *ifp, struct in6_addr *in6)
252 {
253 	struct ifaddr *ifa = NULL;
254 	struct sockaddr_dl *sdl;
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 };
260 	int err = -1;
261 
262 	/* Why doesn't this code use ifnet_addrs? */
263 	ifnet_lock_shared(ifp);
264 	ifa = ifp->if_lladdr;
265 	sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr;
266 	if (sdl->sdl_alen == 0) {
267 		ifnet_lock_done(ifp);
268 		return -1;
269 	}
270 	IFA_ADDREF(ifa);        /* for this routine */
271 	ifnet_lock_done(ifp);
272 
273 	IFA_LOCK(ifa);
274 	addr = (u_int8_t *) LLADDR(sdl);
275 	addrlen = sdl->sdl_alen;
276 
277 	/* get EUI64 */
278 	switch (ifp->if_type) {
279 	case IFT_ETHER:
280 	case IFT_FDDI:
281 	case IFT_ISO88025:
282 	case IFT_ATM:
283 	case IFT_IEEE1394:
284 	case IFT_L2VLAN:
285 	case IFT_IEEE8023ADLAG:
286 #if IFT_IEEE80211
287 	case IFT_IEEE80211:
288 #endif
289 	case IFT_BRIDGE:
290 		/* IEEE802/EUI64 cases - what others? */
291 		/* IEEE1394 uses 16byte length address starting with EUI64 */
292 		if (addrlen > 8) {
293 			addrlen = 8;
294 		}
295 
296 		/* look at IEEE802/EUI64 only */
297 		if (addrlen != 8 && addrlen != 6) {
298 			goto done;
299 		}
300 
301 		/*
302 		 * check for invalid MAC address - on bsdi, we see it a lot
303 		 * since wildboar configures all-zero MAC on pccard before
304 		 * card insertion.
305 		 */
306 		if (bcmp(addr, allzero, addrlen) == 0) {
307 			goto done;
308 		}
309 		if (bcmp(addr, allone, addrlen) == 0) {
310 			goto done;
311 		}
312 
313 		/* make EUI64 address */
314 		if (addrlen == 8) {
315 			bcopy(addr, &in6->s6_addr[8], 8);
316 		} else if (addrlen == 6) {
317 			in6->s6_addr[8] = addr[0];
318 			in6->s6_addr[9] = addr[1];
319 			in6->s6_addr[10] = addr[2];
320 			in6->s6_addr[11] = 0xff;
321 			in6->s6_addr[12] = 0xfe;
322 			in6->s6_addr[13] = addr[3];
323 			in6->s6_addr[14] = addr[4];
324 			in6->s6_addr[15] = addr[5];
325 		}
326 		break;
327 
328 	case IFT_ARCNET:
329 		if (addrlen != 1) {
330 			goto done;
331 		}
332 		if (!addr[0]) {
333 			goto done;
334 		}
335 
336 		bzero(&in6->s6_addr[8], 8);
337 		in6->s6_addr[15] = addr[0];
338 
339 		/*
340 		 * due to insufficient bitwidth, we mark it local.
341 		 */
342 		in6->s6_addr[8] &= ~ND6_EUI64_GBIT;     /* g to "individual" */
343 		in6->s6_addr[8] |= ND6_EUI64_UBIT;      /* u to "local" */
344 		break;
345 
346 	case IFT_GIF:
347 #if IFT_STF
348 	case IFT_STF:
349 #endif
350 		/*
351 		 * RFC2893 says: "SHOULD use IPv4 address as IID source".
352 		 * however, IPv4 address is not very suitable as unique
353 		 * identifier source (can be renumbered).
354 		 * we don't do this.
355 		 */
356 		goto done;
357 
358 	case IFT_CELLULAR:
359 		goto done;
360 
361 	default:
362 		goto done;
363 	}
364 
365 	/* sanity check: g bit must not indicate "group" */
366 	if (ND6_EUI64_GROUP(in6)) {
367 		goto done;
368 	}
369 
370 	/* convert EUI64 into IPv6 interface identifier */
371 	ND6_EUI64_TO_IFID(in6);
372 
373 	/*
374 	 * sanity check: iid must not be all zero, avoid conflict with
375 	 * subnet router anycast
376 	 */
377 	if ((in6->s6_addr[8] & ~(ND6_EUI64_GBIT | ND6_EUI64_UBIT)) == 0x00 &&
378 	    bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
379 		goto done;
380 	}
381 
382 	err = 0;        /* found */
383 
384 done:
385 	/* This must not be the last reference to the lladdr */
386 	if (IFA_REMREF_LOCKED(ifa) == NULL) {
387 		panic("%s: unexpected (missing) refcnt ifa=%p", __func__, ifa);
388 		/* NOTREACHED */
389 	}
390 	IFA_UNLOCK(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 *ia;
472 	struct nd_prefix pr0, *pr;
473 	int i, error;
474 
475 	VERIFY(ifra != NULL);
476 
477 	proto_plumb(PF_INET6, ifp);
478 
479 	error = in6_update_ifa(ifp, ifra, IN6_IFAUPDATE_DADDELAY, &ia);
480 	if (error != 0) {
481 		/*
482 		 * XXX: When the interface does not support IPv6, this call
483 		 * would fail in the SIOCSIFADDR ioctl.  I believe the
484 		 * notification is rather confusing in this case, so just
485 		 * suppress it.  ([email protected] 20010130)
486 		 */
487 		if (error != EAFNOSUPPORT) {
488 			nd6log(info, "%s: failed to "
489 			    "configure a link-local address on %s "
490 			    "(errno=%d)\n",
491 			    __func__, if_name(ifp), error);
492 		}
493 		return EADDRNOTAVAIL;
494 	}
495 	VERIFY(ia != NULL);
496 
497 	/*
498 	 * Make the link-local prefix (fe80::%link/64) as on-link.
499 	 * Since we'd like to manage prefixes separately from addresses,
500 	 * we make an ND6 prefix structure for the link-local prefix,
501 	 * and add it to the prefix list as a never-expire prefix.
502 	 * XXX: this change might affect some existing code base...
503 	 */
504 	bzero(&pr0, sizeof(pr0));
505 	lck_mtx_init(&pr0.ndpr_lock, &ifa_mtx_grp, &ifa_mtx_attr);
506 	pr0.ndpr_ifp = ifp;
507 	/* this should be 64 at this moment. */
508 	pr0.ndpr_plen = (u_char)in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, NULL);
509 	pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
510 	pr0.ndpr_prefix = ifra->ifra_addr;
511 	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
512 	for (i = 0; i < 4; i++) {
513 		pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
514 		    in6mask64.s6_addr32[i];
515 	}
516 	/*
517 	 * Initialize parameters.  The link-local prefix must always be
518 	 * on-link, and its lifetimes never expire.
519 	 */
520 	pr0.ndpr_raf_onlink = 1;
521 	pr0.ndpr_raf_auto = 1;  /* probably meaningless */
522 	pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
523 	pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
524 	pr0.ndpr_stateflags |= NDPRF_STATIC;
525 	/*
526 	 * Since there is no other link-local addresses, nd6_prefix_lookup()
527 	 * probably returns NULL.  However, we cannot always expect the result.
528 	 * For example, if we first remove the (only) existing link-local
529 	 * address, and then reconfigure another one, the prefix is still
530 	 * valid with referring to the old link-local address.
531 	 */
532 	if ((pr = nd6_prefix_lookup(&pr0, ND6_PREFIX_EXPIRY_UNSPEC)) == NULL) {
533 		if ((error = nd6_prelist_add(&pr0, NULL, &pr, TRUE)) != 0) {
534 			IFA_REMREF(&ia->ia_ifa);
535 			lck_mtx_destroy(&pr0.ndpr_lock, &ifa_mtx_grp);
536 			return error;
537 		}
538 	}
539 
540 	in6_post_msg(ifp, KEV_INET6_NEW_LL_ADDR, ia, NULL);
541 	IFA_REMREF(&ia->ia_ifa);
542 
543 	/* Drop use count held above during lookup/add */
544 	if (pr != NULL) {
545 		NDPR_REMREF(pr);
546 	}
547 
548 	lck_mtx_destroy(&pr0.ndpr_lock, &ifa_mtx_grp);
549 	return 0;
550 }
551 
552 static int
in6_ifattach_loopback(struct ifnet * ifp)553 in6_ifattach_loopback(
554 	struct ifnet *ifp)      /* must be IFT_LOOP */
555 {
556 	struct in6_aliasreq ifra;
557 	struct in6_ifaddr *ia;
558 	int error;
559 
560 	bzero(&ifra, sizeof(ifra));
561 
562 	/*
563 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
564 	 * for safety.
565 	 */
566 	strlcpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
567 
568 	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
569 	ifra.ifra_prefixmask.sin6_family = AF_INET6;
570 	ifra.ifra_prefixmask.sin6_addr = in6mask128;
571 
572 	/*
573 	 * Always initialize ia_dstaddr (= broadcast address) to loopback
574 	 * address.  Follows IPv4 practice - see in_ifinit().
575 	 */
576 	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
577 	ifra.ifra_dstaddr.sin6_family = AF_INET6;
578 	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
579 
580 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
581 	ifra.ifra_addr.sin6_family = AF_INET6;
582 	ifra.ifra_addr.sin6_addr = in6addr_loopback;
583 
584 	/* the loopback  address should NEVER expire. */
585 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
586 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
587 
588 	/* we don't need to perform DAD on loopback interfaces. */
589 	ifra.ifra_flags |= IN6_IFF_NODAD;
590 
591 	/* add the new interface address */
592 	error = in6_update_ifa(ifp, &ifra, 0, &ia);
593 	if (error != 0) {
594 		nd6log(error,
595 		    "%s: failed to configure loopback address %s (error=%d)\n",
596 		    __func__, if_name(ifp), error);
597 		VERIFY(ia == NULL);
598 		return EADDRNOTAVAIL;
599 	}
600 
601 	VERIFY(ia != NULL);
602 	IFA_REMREF(&ia->ia_ifa);
603 	return 0;
604 }
605 
606 /*
607  * compute NI group address, based on the current hostname setting.
608  * see RFC 4620.
609  *
610  * when ifp == NULL, the caller is responsible for filling scopeid.
611  */
612 int
in6_nigroup(struct ifnet * ifp,const char * name,size_t namelen,struct in6_addr * in6,uint32_t * ifscopep)613 in6_nigroup(
614 	struct ifnet *ifp,
615 	const char *name,
616 	size_t namelen,
617 	struct in6_addr *in6,
618 	uint32_t *ifscopep)
619 {
620 	const char *p;
621 	u_char *q;
622 	SHA1_CTX ctxt;
623 	u_int8_t digest[SHA1_RESULTLEN];
624 	size_t l;
625 	char n[64];     /* a single label must not exceed 63 chars */
626 
627 	if (!namelen || !name) {
628 		return -1;
629 	}
630 
631 	p = name;
632 	while (p && *p && *p != '.' && p - name < namelen) {
633 		p++;
634 	}
635 	if (p - name > sizeof(n) - 1) {
636 		return -1;    /* label too long */
637 	}
638 	l = p - name;
639 	strlcpy(n, name, l);
640 	n[(int)l] = '\0';
641 	for (q = (u_char *) n; *q; q++) {
642 		if ('A' <= *q && *q <= 'Z') {
643 			*q = *q - 'A' + 'a';
644 		}
645 	}
646 
647 	/* generate 16 bytes of pseudo-random value. */
648 	bzero(&ctxt, sizeof(ctxt));
649 	SHA1Init(&ctxt);
650 	SHA1Update(&ctxt, &l, sizeof(l));
651 	SHA1Update(&ctxt, n, l);
652 	SHA1Final(digest, &ctxt);
653 
654 	bzero(in6, sizeof(*in6));
655 	in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
656 	in6->s6_addr8[11] = 2;
657 	in6->s6_addr8[12] = 0xff;
658 	/* copy first 3 bytes of prefix into address */
659 	bcopy(digest, &in6->s6_addr8[13], 3);
660 	if (in6_setscope(in6, ifp, ifscopep)) {
661 		return -1; /* XXX: should not fail */
662 	}
663 	return 0;
664 }
665 
666 int
in6_domifattach(struct ifnet * ifp)667 in6_domifattach(struct ifnet *ifp)
668 {
669 	int error;
670 
671 	VERIFY(ifp != NULL);
672 
673 	error = proto_plumb(PF_INET6, ifp);
674 	if (error != 0) {
675 		if (error != EEXIST) {
676 			log(LOG_ERR, "%s: proto_plumb returned %d if=%s\n",
677 			    __func__, error, if_name(ifp));
678 		}
679 	} else {
680 		error = in6_ifattach_prelim(ifp);
681 		if (error != 0) {
682 			int errorx;
683 
684 			log(LOG_ERR,
685 			    "%s: in6_ifattach_prelim returned %d if=%s%d\n",
686 			    __func__, error, ifp->if_name, ifp->if_unit);
687 
688 			errorx = proto_unplumb(PF_INET6, ifp);
689 			if (errorx != 0) { /* XXX should not fail */
690 				log(LOG_ERR,
691 				    "%s: proto_unplumb returned %d if=%s%d\n",
692 				    __func__, errorx, ifp->if_name,
693 				    ifp->if_unit);
694 			}
695 		}
696 	}
697 
698 	return error;
699 }
700 
701 int
in6_ifattach_prelim(struct ifnet * ifp)702 in6_ifattach_prelim(struct ifnet *ifp)
703 {
704 	int error = 0;
705 	struct in6_ifaddr *ia6 = NULL;
706 
707 	VERIFY(ifp != NULL);
708 
709 	/* quirks based on interface type */
710 	switch (ifp->if_type) {
711 #if IFT_STF
712 	case IFT_STF:
713 		/*
714 		 * 6to4 interface is a very special kind of beast.
715 		 * no multicast, no linklocal.  RFC2529 specifies how to make
716 		 * linklocals for 6to4 interface, but there's no use and
717 		 * it is rather harmful to have one.
718 		 */
719 		goto skipmcast;
720 #endif
721 	default:
722 		break;
723 	}
724 
725 	/*
726 	 * IPv6 requires multicast capability at the interface.
727 	 *   (previously, this was a silent error.)
728 	 */
729 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
730 		nd6log0(info, "in6_ifattach: %s is not multicast capable, IPv6 not enabled\n",
731 		    if_name(ifp));
732 		return EINVAL;
733 	}
734 
735 #if IFT_STF
736 skipmcast:
737 #endif
738 
739 	if (ifp->if_inet6data == NULL) {
740 		ifp->if_inet6data = zalloc_permanent_type(struct in6_ifextra);
741 	} else {
742 		/*
743 		 * Since the structure is never freed, we need to zero out
744 		 * some of its members. We avoid zeroing out the scope6
745 		 * structure on purpose because other threads might be
746 		 * using its contents.
747 		 */
748 		bzero(&IN6_IFEXTRA(ifp)->icmp6_ifstat,
749 		    sizeof(IN6_IFEXTRA(ifp)->icmp6_ifstat));
750 		bzero(&IN6_IFEXTRA(ifp)->in6_ifstat,
751 		    sizeof(IN6_IFEXTRA(ifp)->in6_ifstat));
752 		/* XXX TBD Purge the layer two table */
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 *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 		bcopy(&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 *ia6 = NULL;
936 	struct nd_ifinfo *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 	if (in6_embedded_scope) {
980 		ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
981 	} else {
982 		ifra.ifra_addr.sin6_addr.s6_addr16[1] = 0;
983 	}
984 	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
985 	ifra.ifra_flags = IN6_IFF_SECURED;
986 
987 	in6_cga_node_lock();
988 	if (in6_cga_generate(&llcgasr->cgar_cgaprep, llcgasr->cgar_collision_count,
989 	    &ifra.ifra_addr.sin6_addr, ifp)) {
990 		in6_cga_node_unlock();
991 		return EADDRNOTAVAIL;
992 	}
993 	in6_cga_node_unlock();
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 *ia, *nia;
1032 	struct ifaddr *ifa;
1033 	struct rtentry *rt;
1034 	struct sockaddr_in6 sin6;
1035 	struct in6_multi_mship *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 	if (LLTABLE6(ifp)) {
1044 		lltable_free(LLTABLE6(ifp));
1045 	}
1046 
1047 	/* nuke any of IPv6 addresses we have */
1048 	lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1049 	boolean_t from_begining = TRUE;
1050 	while (from_begining) {
1051 		from_begining = FALSE;
1052 		TAILQ_FOREACH(ia, &in6_ifaddrhead, ia6_link) {
1053 			if (ia->ia_ifa.ifa_ifp != ifp) {
1054 				continue;
1055 			}
1056 			IFA_ADDREF(&ia->ia_ifa);        /* for us */
1057 			lck_rw_done(&in6_ifaddr_rwlock);
1058 			in6_purgeaddr(&ia->ia_ifa);
1059 			IFA_REMREF(&ia->ia_ifa);        /* for us */
1060 			lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1061 			/*
1062 			 * Purging the address caused in6_ifaddr_rwlock
1063 			 * to be dropped and reacquired;
1064 			 * therefore search again from the beginning
1065 			 * of in6_ifaddrs list.
1066 			 */
1067 			from_begining = TRUE;
1068 			break;
1069 		}
1070 	}
1071 	lck_rw_done(&in6_ifaddr_rwlock);
1072 
1073 	ifnet_lock_exclusive(ifp);
1074 
1075 	/* undo everything done by in6_ifattach(), just in case */
1076 	ifa = TAILQ_FIRST(&ifp->if_addrlist);
1077 	while (ifa != NULL) {
1078 		IFA_LOCK(ifa);
1079 		if (ifa->ifa_addr->sa_family != AF_INET6 ||
1080 		    !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->
1081 		    sin6_addr)) {
1082 			IFA_UNLOCK(ifa);
1083 			ifa = TAILQ_NEXT(ifa, ifa_list);
1084 			continue;
1085 		}
1086 
1087 		ia = (struct in6_ifaddr *)ifa;
1088 
1089 		/* hold a reference for this routine */
1090 		IFA_ADDREF_LOCKED(ifa);
1091 		/* remove from the linked list */
1092 		if_detach_ifa(ifp, ifa);
1093 		IFA_UNLOCK(ifa);
1094 
1095 		/*
1096 		 * Leaving the multicast group(s) may involve freeing the
1097 		 * link address multicast structure(s) for the interface,
1098 		 * which is protected by ifnet lock.  To avoid violating
1099 		 * lock ordering, we must drop ifnet lock before doing so.
1100 		 * The ifa won't go away since we held a refcnt above.
1101 		 */
1102 		ifnet_lock_done(ifp);
1103 
1104 		/*
1105 		 * We have to do this work manually here instead of calling
1106 		 * in6_purgeaddr() since in6_purgeaddr() uses the RTM_HOST flag.
1107 		 */
1108 
1109 		/*
1110 		 * leave from multicast groups we have joined for the interface
1111 		 */
1112 		IFA_LOCK(ifa);
1113 		while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1114 			LIST_REMOVE(imm, i6mm_chain);
1115 			IFA_UNLOCK(ifa);
1116 			in6_leavegroup(imm);
1117 			IFA_LOCK(ifa);
1118 		}
1119 
1120 		/* remove from the routing table */
1121 		if (ia->ia_flags & IFA_ROUTE) {
1122 			IFA_UNLOCK(ifa);
1123 			rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0);
1124 			if (rt != NULL) {
1125 				(void) rtrequest(RTM_DELETE,
1126 				    (struct sockaddr *)&ia->ia_addr,
1127 				    (struct sockaddr *)&ia->ia_addr,
1128 				    (struct sockaddr *)&ia->ia_prefixmask,
1129 				    rt->rt_flags, (struct rtentry **)0);
1130 				rtfree(rt);
1131 			}
1132 		} else {
1133 			IFA_UNLOCK(ifa);
1134 		}
1135 
1136 		/* also remove from the IPv6 address chain(itojun&jinmei) */
1137 		unlinked = 0;
1138 		lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1139 		TAILQ_FOREACH(nia, &in6_ifaddrhead, ia6_link) {
1140 			if (ia == nia) {
1141 				TAILQ_REMOVE(&in6_ifaddrhead, ia, ia6_link);
1142 				os_atomic_inc(&in6_ifaddrlist_genid, relaxed);
1143 				unlinked = 1;
1144 				break;
1145 			}
1146 		}
1147 		lck_rw_done(&in6_ifaddr_rwlock);
1148 
1149 		/*
1150 		 * release another refcnt for the link from in6_ifaddrs.
1151 		 * Do this only if it's not already unlinked in the event
1152 		 * that we lost the race, since in6_ifaddr_rwlock was
1153 		 * momentarily dropped above.
1154 		 */
1155 		if (unlinked) {
1156 			IFA_REMREF(ifa);
1157 		}
1158 		/* release reference held for this routine */
1159 		IFA_REMREF(ifa);
1160 
1161 		/*
1162 		 * This is suboptimal, but since we dropped ifnet lock above
1163 		 * the list might have changed.  Repeat the search from the
1164 		 * beginning until we find the first eligible IPv6 address.
1165 		 */
1166 		ifnet_lock_exclusive(ifp);
1167 		ifa = TAILQ_FIRST(&ifp->if_addrlist);
1168 	}
1169 	ifnet_lock_done(ifp);
1170 
1171 	/* invalidate route caches */
1172 	routegenid_inet6_update();
1173 
1174 	/*
1175 	 * remove neighbor management table.  we call it twice just to make
1176 	 * sure we nuke everything.  maybe we need just one call.
1177 	 * XXX: since the first call did not release addresses, some prefixes
1178 	 * might remain.  We should call nd6_purge() again to release the
1179 	 * prefixes after removing all addresses above.
1180 	 * (Or can we just delay calling nd6_purge until at this point?)
1181 	 */
1182 	nd6_purge(ifp);
1183 
1184 	/* remove route to link-local allnodes multicast (ff02::1) */
1185 	bzero(&sin6, sizeof(sin6));
1186 	sin6.sin6_len = sizeof(struct sockaddr_in6);
1187 	sin6.sin6_family = AF_INET6;
1188 	sin6.sin6_addr = in6addr_linklocal_allnodes;
1189 	if (in6_embedded_scope) {
1190 		sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1191 	} else {
1192 		sin6.sin6_scope_id = ifp->if_index;
1193 	}
1194 	rt = rtalloc1((struct sockaddr *)&sin6, 0, 0);
1195 	if (rt != NULL) {
1196 		RT_LOCK(rt);
1197 		if (rt->rt_ifp == ifp) {
1198 			/*
1199 			 * Prevent another thread from modifying rt_key,
1200 			 * rt_gateway via rt_setgate() after the rt_lock
1201 			 * is dropped by marking the route as defunct.
1202 			 */
1203 			rt->rt_flags |= RTF_CONDEMNED;
1204 			RT_UNLOCK(rt);
1205 			(void) rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1206 			    rt_mask(rt), rt->rt_flags, 0);
1207 		} else {
1208 			RT_UNLOCK(rt);
1209 		}
1210 		rtfree(rt);
1211 	}
1212 }
1213 
1214 void
in6_iid_mktmp(struct ifnet * ifp,u_int8_t * retbuf,const u_int8_t * baseid,int generate)1215 in6_iid_mktmp(struct ifnet *ifp, u_int8_t *retbuf, const u_int8_t *baseid,
1216     int generate)
1217 {
1218 	u_int8_t nullbuf[8];
1219 	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
1220 
1221 	VERIFY(ndi != NULL && ndi->initialized);
1222 	lck_mtx_lock(&ndi->lock);
1223 	bzero(nullbuf, sizeof(nullbuf));
1224 	if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
1225 		/* we've never created a random ID.  Create a new one. */
1226 		generate = 1;
1227 	}
1228 
1229 	if (generate) {
1230 		bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
1231 
1232 		/* in6_generate_tmp_iid will update seedn and buf */
1233 		(void) in6_generate_tmp_iid(ndi->randomseed0, ndi->randomseed1,
1234 		    ndi->randomid);
1235 	}
1236 
1237 	bcopy(ndi->randomid, retbuf, 8);
1238 	lck_mtx_unlock(&ndi->lock);
1239 }
1240 
1241 void
in6_tmpaddrtimer(void * arg)1242 in6_tmpaddrtimer(void *arg)
1243 {
1244 #pragma unused(arg)
1245 	struct ifnet *ifp = NULL;
1246 	struct nd_ifinfo *ndi = NULL;
1247 	u_int8_t nullbuf[8];
1248 
1249 	timeout(in6_tmpaddrtimer, (caddr_t)0, (ip6_temp_preferred_lifetime -
1250 	    ip6_desync_factor - ip6_temp_regen_advance) * hz);
1251 
1252 	bzero(nullbuf, sizeof(nullbuf));
1253 	ifnet_head_lock_shared();
1254 	for (ifp = ifnet_head.tqh_first; ifp;
1255 	    ifp = ifp->if_link.tqe_next) {
1256 		ndi = ND_IFINFO(ifp);
1257 		if ((NULL == ndi) || (FALSE == ndi->initialized)) {
1258 			continue;
1259 		}
1260 		lck_mtx_lock(&ndi->lock);
1261 		if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
1262 			/*
1263 			 * We've been generating a random ID on this interface.
1264 			 * Create a new one.
1265 			 */
1266 			(void) in6_generate_tmp_iid(ndi->randomseed0,
1267 			    ndi->randomseed1, ndi->randomid);
1268 		}
1269 		lck_mtx_unlock(&ndi->lock);
1270 	}
1271 	ifnet_head_done();
1272 }
1273