xref: /xnu-8020.140.41/bsd/netinet6/in6_ifattach.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
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 	case IFT_6LOWPAN:
291 		/* IEEE802/EUI64 cases - what others? */
292 		/* IEEE1394 uses 16byte length address starting with EUI64 */
293 		if (addrlen > 8) {
294 			addrlen = 8;
295 		}
296 
297 		/* look at IEEE802/EUI64 only */
298 		if (addrlen != 8 && addrlen != 6) {
299 			goto done;
300 		}
301 
302 		/*
303 		 * check for invalid MAC address - on bsdi, we see it a lot
304 		 * since wildboar configures all-zero MAC on pccard before
305 		 * card insertion.
306 		 */
307 		if (bcmp(addr, allzero, addrlen) == 0) {
308 			goto done;
309 		}
310 		if (bcmp(addr, allone, addrlen) == 0) {
311 			goto done;
312 		}
313 
314 		/* make EUI64 address */
315 		if (addrlen == 8) {
316 			bcopy(addr, &in6->s6_addr[8], 8);
317 		} else if (addrlen == 6) {
318 			in6->s6_addr[8] = addr[0];
319 			in6->s6_addr[9] = addr[1];
320 			in6->s6_addr[10] = addr[2];
321 			in6->s6_addr[11] = 0xff;
322 			in6->s6_addr[12] = 0xfe;
323 			in6->s6_addr[13] = addr[3];
324 			in6->s6_addr[14] = addr[4];
325 			in6->s6_addr[15] = addr[5];
326 		}
327 		break;
328 
329 	case IFT_ARCNET:
330 		if (addrlen != 1) {
331 			goto done;
332 		}
333 		if (!addr[0]) {
334 			goto done;
335 		}
336 
337 		bzero(&in6->s6_addr[8], 8);
338 		in6->s6_addr[15] = addr[0];
339 
340 		/*
341 		 * due to insufficient bitwidth, we mark it local.
342 		 */
343 		in6->s6_addr[8] &= ~ND6_EUI64_GBIT;     /* g to "individual" */
344 		in6->s6_addr[8] |= ND6_EUI64_UBIT;      /* u to "local" */
345 		break;
346 
347 	case IFT_GIF:
348 #if IFT_STF
349 	case IFT_STF:
350 #endif
351 		/*
352 		 * RFC2893 says: "SHOULD use IPv4 address as IID source".
353 		 * however, IPv4 address is not very suitable as unique
354 		 * identifier source (can be renumbered).
355 		 * we don't do this.
356 		 */
357 		goto done;
358 
359 	case IFT_CELLULAR:
360 		goto done;
361 
362 	default:
363 		goto done;
364 	}
365 
366 	/* sanity check: g bit must not indicate "group" */
367 	if (ND6_EUI64_GROUP(in6)) {
368 		goto done;
369 	}
370 
371 	/* convert EUI64 into IPv6 interface identifier */
372 	ND6_EUI64_TO_IFID(in6);
373 
374 	/*
375 	 * sanity check: iid must not be all zero, avoid conflict with
376 	 * subnet router anycast
377 	 */
378 	if ((in6->s6_addr[8] & ~(ND6_EUI64_GBIT | ND6_EUI64_UBIT)) == 0x00 &&
379 	    bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
380 		goto done;
381 	}
382 
383 	err = 0;        /* found */
384 
385 done:
386 	/* This must not be the last reference to the lladdr */
387 	if (IFA_REMREF_LOCKED(ifa) == NULL) {
388 		panic("%s: unexpected (missing) refcnt ifa=%p", __func__, ifa);
389 		/* NOTREACHED */
390 	}
391 	IFA_UNLOCK(ifa);
392 	return err;
393 }
394 
395 /*
396  * Get interface identifier for the specified interface using the method in
397  * Appendix A of RFC 4291.  If it is not available on ifp0, borrow interface
398  * identifier from other information sources.
399  *
400  * ifp     - primary EUI64 source
401  * altifp  - secondary EUI64 source
402  * in6     - IPv6 address to output IID
403  */
404 static int
in6_select_iid_from_all_hw(struct ifnet * ifp0,struct ifnet * altifp,struct in6_addr * in6)405 in6_select_iid_from_all_hw(
406 	struct ifnet *ifp0,
407 	struct ifnet *altifp,   /* secondary EUI64 source */
408 	struct in6_addr *in6)
409 {
410 	struct ifnet *ifp;
411 
412 	/* first, try to get it from the interface itself */
413 	if (in6_iid_from_hw(ifp0, in6) == 0) {
414 		nd6log(debug, "%s: IID derived from HW interface.\n",
415 		    if_name(ifp0));
416 		goto success;
417 	}
418 
419 	/* try secondary EUI64 source. this basically is for ATM PVC */
420 	if (altifp && in6_iid_from_hw(altifp, in6) == 0) {
421 		nd6log(debug, "%s: IID from alterate HW interface %s.\n",
422 		    if_name(ifp0), if_name(altifp));
423 		goto success;
424 	}
425 
426 	/* next, try to get it from some other hardware interface */
427 	ifnet_head_lock_shared();
428 	TAILQ_FOREACH(ifp, &ifnet_head, if_list) {
429 		if (ifp == ifp0) {
430 			continue;
431 		}
432 		if (in6_iid_from_hw(ifp, in6) != 0) {
433 			continue;
434 		}
435 
436 		/*
437 		 * to borrow IID from other interface, IID needs to be
438 		 * globally unique
439 		 */
440 		if (ND6_IFID_UNIVERSAL(in6)) {
441 			nd6log(debug, "%s: borrowed IID from %s\n",
442 			    if_name(ifp0), if_name(ifp));
443 			ifnet_head_done();
444 			goto success;
445 		}
446 	}
447 	ifnet_head_done();
448 
449 	/* last resort: get from random number source */
450 	if (get_rand_iid(ifp, in6) == 0) {
451 		nd6log(debug, "%s: IID from PRNG.\n", if_name(ifp0));
452 		goto success;
453 	}
454 
455 	printf("%s: failed to get interface identifier\n", if_name(ifp0));
456 	return -1;
457 
458 success:
459 	nd6log(info, "%s: IID: "
460 	    "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
461 	    if_name(ifp0),
462 	    in6->s6_addr[8], in6->s6_addr[9],
463 	    in6->s6_addr[10], in6->s6_addr[11],
464 	    in6->s6_addr[12], in6->s6_addr[13],
465 	    in6->s6_addr[14], in6->s6_addr[15]);
466 	return 0;
467 }
468 
469 static int
in6_ifattach_linklocal(struct ifnet * ifp,struct in6_aliasreq * ifra)470 in6_ifattach_linklocal(struct ifnet *ifp, struct in6_aliasreq *ifra)
471 {
472 	struct in6_ifaddr *ia;
473 	struct nd_prefix pr0, *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);
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 *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 * name,size_t namelen,struct in6_addr * in6,uint32_t * ifscopep)614 in6_nigroup(
615 	struct ifnet *ifp,
616 	const char *name,
617 	size_t namelen,
618 	struct in6_addr *in6,
619 	uint32_t *ifscopep)
620 {
621 	const char *p;
622 	u_char *q;
623 	SHA1_CTX ctxt;
624 	u_int8_t digest[SHA1_RESULTLEN];
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 && *p && *p != '.' && p - name < namelen) {
634 		p++;
635 	}
636 	if (p - name > sizeof(n) - 1) {
637 		return -1;    /* label too long */
638 	}
639 	l = p - name;
640 	strlcpy(n, name, l);
641 	n[(int)l] = '\0';
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 	SHA1Init(&ctxt);
651 	SHA1Update(&ctxt, &l, sizeof(l));
652 	SHA1Update(&ctxt, n, l);
653 	SHA1Final(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 *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 		/* XXX TBD Purge the layer two table */
754 		/*
755 		 * XXX When recycling, nd_ifinfo gets initialized, other
756 		 * than the lock, inside nd6_ifattach
757 		 */
758 	}
759 
760 	/*
761 	 * XXX Only initialize IPv6 configuration for the interface
762 	 * if interface has not yet been configured with
763 	 * link local IPv6 address.
764 	 * Could possibly be optimized with an interface flag if need
765 	 * be. For now using in6ifa_ifpforlinklocal.
766 	 */
767 	ia6 = in6ifa_ifpforlinklocal(ifp, 0);
768 	if (ia6 == NULL) {
769 		IN6_IFEXTRA(ifp)->netsig_len = 0;
770 		bzero(&IN6_IFEXTRA(ifp)->netsig,
771 		    sizeof(IN6_IFEXTRA(ifp)->netsig));
772 		bzero(IN6_IFEXTRA(ifp)->nat64_prefixes,
773 		    sizeof(IN6_IFEXTRA(ifp)->nat64_prefixes));
774 		/* initialize NDP variables */
775 		nd6_ifattach(ifp);
776 	} else {
777 		VERIFY(ND_IFINFO(ifp)->initialized);
778 		IFA_REMREF(&ia6->ia_ifa);
779 		ia6 = NULL;
780 	}
781 	scope6_ifattach(ifp);
782 
783 	/* initialize loopback interface address */
784 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
785 		error = in6_ifattach_loopback(ifp);
786 		if (error != 0) {
787 			log(LOG_ERR, "%s: in6_ifattach_loopback returned %d\n",
788 			    __func__, error);
789 			return error;
790 		}
791 	}
792 
793 	/* update dynamically. */
794 	if (in6_maxmtu < ifp->if_mtu) {
795 		in6_maxmtu = ifp->if_mtu;
796 	}
797 
798 	VERIFY(error == 0);
799 	return 0;
800 }
801 
802 /*
803  * This routine is only meant to configure IPv6 Link Local
804  * addresses.
805  */
806 int
in6_ifattach_aliasreq(struct ifnet * ifp,struct ifnet * altifp,struct in6_aliasreq * ifra0)807 in6_ifattach_aliasreq(struct ifnet *ifp, struct ifnet *altifp,
808     struct in6_aliasreq *ifra0)
809 {
810 	int error;
811 	struct in6_ifaddr *ia6;
812 	struct in6_aliasreq ifra;
813 
814 	error = in6_ifattach_prelim(ifp);
815 	if (error != 0) {
816 		return error;
817 	}
818 
819 	if (!ip6_auto_linklocal) {
820 		return 0;
821 	}
822 
823 	/*
824 	 * Assign a link-local address, only if there isn't one here already.
825 	 * XXX If we ever allow more than one LLA on the interface
826 	 * make sure that the corresponding prefix on the prefixlist
827 	 * is reference counted and the address's prefix pointer
828 	 * points to the prefix.
829 	 */
830 	ia6 = in6ifa_ifpforlinklocal(ifp, 0);
831 	if (ia6 != NULL) {
832 		IFA_REMREF(&ia6->ia_ifa);
833 		return 0;
834 	}
835 
836 	bzero(&ifra, sizeof(ifra));
837 
838 	/*
839 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
840 	 * for safety.
841 	 */
842 	strlcpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
843 
844 	/* Initialize the IPv6 interface address in our in6_aliasreq block */
845 	if (ifra0 != NULL) {
846 		/* interface provided both addresses for us */
847 		struct sockaddr_in6 *sin6 = &ifra.ifra_addr;
848 		struct in6_addr *in6 = &sin6->sin6_addr;
849 		boolean_t ok = TRUE;
850 
851 		bcopy(&ifra0->ifra_addr, sin6, sizeof(struct sockaddr_in6));
852 
853 		if (sin6->sin6_family != AF_INET6 || sin6->sin6_port != 0) {
854 			ok = FALSE;
855 		}
856 		if (ok && (in6->s6_addr16[0] != htons(0xfe80))) {
857 			ok = FALSE;
858 		}
859 
860 		if (ok) {
861 			if (sin6->sin6_scope_id == 0 && in6->s6_addr16[1] == 0) {
862 				if (in6_embedded_scope) {
863 					in6->s6_addr16[1] = htons(ifp->if_index);
864 				} else {
865 					sin6->sin6_scope_id = ifp->if_index;
866 				}
867 			} else if (sin6->sin6_scope_id != 0 &&
868 			    sin6->sin6_scope_id != ifp->if_index) {
869 				ok = FALSE;
870 			} else if (in6_embedded_scope && in6->s6_addr16[1] != 0 &&
871 			    ntohs(in6->s6_addr16[1]) != ifp->if_index) {
872 				ok = FALSE;
873 			}
874 		}
875 		if (ok && (in6->s6_addr32[1] != 0)) {
876 			ok = FALSE;
877 		}
878 		if (!ok) {
879 			return EINVAL;
880 		}
881 	} else {
882 		ifra.ifra_addr.sin6_family = AF_INET6;
883 		ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
884 		ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
885 		if (in6_embedded_scope) {
886 			ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
887 		} else {
888 			ifra.ifra_addr.sin6_addr.s6_addr16[1] = 0;
889 			ifra.ifra_addr.sin6_scope_id = ifp->if_index;
890 		}
891 		ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
892 		if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
893 			ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
894 			ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
895 			if (!in6_embedded_scope) {
896 				ifra.ifra_addr.sin6_scope_id = ifp->if_index;
897 			}
898 		} else {
899 			if (in6_select_iid_from_all_hw(ifp, altifp,
900 			    &ifra.ifra_addr.sin6_addr) != 0) {
901 				nd6log(error, "%s: no IID available\n",
902 				    if_name(ifp));
903 				return EADDRNOTAVAIL;
904 			}
905 		}
906 	}
907 
908 	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, IN6_NULL_IF_EMBEDDED_SCOPE(&ifra.ifra_addr.sin6_scope_id))) {
909 		return EADDRNOTAVAIL;
910 	}
911 
912 	/* Set the prefix mask */
913 	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
914 	ifra.ifra_prefixmask.sin6_family = AF_INET6;
915 	ifra.ifra_prefixmask.sin6_addr = in6mask64;
916 
917 	/* link-local addresses should NEVER expire. */
918 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
919 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
920 
921 	/* Attach the link-local address */
922 	if (in6_ifattach_linklocal(ifp, &ifra) != 0) {
923 		nd6log(info,
924 		    "%s: %s could not attach link-local address.\n",
925 		    __func__, if_name(ifp));
926 		/* NB: not an error */
927 	}
928 
929 	return 0;
930 }
931 
932 int
in6_ifattach_llcgareq(struct ifnet * ifp,struct in6_cgareq * llcgasr)933 in6_ifattach_llcgareq(struct ifnet *ifp, struct in6_cgareq *llcgasr)
934 {
935 	struct in6_aliasreq ifra;
936 	struct in6_ifaddr *ia6 = NULL;
937 	struct nd_ifinfo *ndi = NULL;
938 	int error;
939 
940 	VERIFY(llcgasr != NULL);
941 
942 	error = in6_ifattach_prelim(ifp);
943 	if (error != 0) {
944 		return error;
945 	}
946 
947 	if (!ip6_auto_linklocal) {
948 		return 0;
949 	}
950 
951 	if (nd6_send_opstate == ND6_SEND_OPMODE_DISABLED) {
952 		return ENXIO;
953 	}
954 
955 	ndi = ND_IFINFO(ifp);
956 	VERIFY(ndi != NULL && ndi->initialized);
957 	if ((ndi->flags & ND6_IFF_INSECURE) != 0) {
958 		return ENXIO;
959 	}
960 
961 	/*
962 	 * Assign a link-local address, only if there isn't one here already.
963 	 * XXX If we ever allow more than one LLA on the interface
964 	 * make sure that the corresponding prefix on the prefixlist
965 	 * is reference counted and the address's prefix pointer
966 	 * points to the prefix.
967 	 */
968 	ia6 = in6ifa_ifpforlinklocal(ifp, 0);
969 	if (ia6 != NULL) {
970 		IFA_REMREF(&ia6->ia_ifa);
971 		return 0;
972 	}
973 
974 	bzero(&ifra, sizeof(ifra));
975 	strlcpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
976 
977 	ifra.ifra_addr.sin6_family = AF_INET6;
978 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
979 	ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
980 	if (in6_embedded_scope) {
981 		ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
982 	} else {
983 		ifra.ifra_addr.sin6_addr.s6_addr16[1] = 0;
984 	}
985 	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
986 	ifra.ifra_flags = IN6_IFF_SECURED;
987 
988 	in6_cga_node_lock();
989 	if (in6_cga_generate(&llcgasr->cgar_cgaprep, llcgasr->cgar_collision_count,
990 	    &ifra.ifra_addr.sin6_addr, ifp)) {
991 		in6_cga_node_unlock();
992 		return EADDRNOTAVAIL;
993 	}
994 	in6_cga_node_unlock();
995 
996 	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, IN6_NULL_IF_EMBEDDED_SCOPE(&ifra.ifra_addr.sin6_scope_id))) {
997 		return EADDRNOTAVAIL;
998 	}
999 
1000 	/* Set the prefix mask */
1001 	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1002 	ifra.ifra_prefixmask.sin6_family = AF_INET6;
1003 	ifra.ifra_prefixmask.sin6_addr = in6mask64;
1004 
1005 	/*
1006 	 * link-local addresses should NEVER expire, but cryptographic
1007 	 * ones may have finite preferred lifetime [if it's important to
1008 	 * keep them from being used by applications as persistent device
1009 	 * identifiers].
1010 	 */
1011 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
1012 	ifra.ifra_lifetime.ia6t_pltime = llcgasr->cgar_lifetime.ia6t_pltime;
1013 
1014 	/* Attach the link-local address */
1015 	if (in6_ifattach_linklocal(ifp, &ifra) != 0) {
1016 		/* NB: not an error */
1017 		nd6log(info,
1018 		    "%s: %s could not attach link-local address.\n",
1019 		    __func__, if_name(ifp));
1020 	}
1021 
1022 	VERIFY(error == 0);
1023 	return error;
1024 }
1025 
1026 /*
1027  * NOTE: in6_ifdetach() does not support loopback if at this moment.
1028  */
1029 void
in6_ifdetach(struct ifnet * ifp)1030 in6_ifdetach(struct ifnet *ifp)
1031 {
1032 	struct in6_ifaddr *ia, *nia;
1033 	struct ifaddr *ifa;
1034 	struct rtentry *rt;
1035 	struct sockaddr_in6 sin6;
1036 	struct in6_multi_mship *imm;
1037 	int unlinked;
1038 
1039 	LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
1040 
1041 	/* remove neighbor management table */
1042 	nd6_purge(ifp);
1043 
1044 	if (LLTABLE6(ifp)) {
1045 		lltable_free(LLTABLE6(ifp));
1046 	}
1047 
1048 	/* nuke any of IPv6 addresses we have */
1049 	lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1050 	boolean_t from_begining = TRUE;
1051 	while (from_begining) {
1052 		from_begining = FALSE;
1053 		TAILQ_FOREACH(ia, &in6_ifaddrhead, ia6_link) {
1054 			if (ia->ia_ifa.ifa_ifp != ifp) {
1055 				continue;
1056 			}
1057 			IFA_ADDREF(&ia->ia_ifa);        /* for us */
1058 			lck_rw_done(&in6_ifaddr_rwlock);
1059 			in6_purgeaddr(&ia->ia_ifa);
1060 			IFA_REMREF(&ia->ia_ifa);        /* for us */
1061 			lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1062 			/*
1063 			 * Purging the address caused in6_ifaddr_rwlock
1064 			 * to be dropped and reacquired;
1065 			 * therefore search again from the beginning
1066 			 * of in6_ifaddrs list.
1067 			 */
1068 			from_begining = TRUE;
1069 			break;
1070 		}
1071 	}
1072 	lck_rw_done(&in6_ifaddr_rwlock);
1073 
1074 	ifnet_lock_exclusive(ifp);
1075 
1076 	/* undo everything done by in6_ifattach(), just in case */
1077 	ifa = TAILQ_FIRST(&ifp->if_addrlist);
1078 	while (ifa != NULL) {
1079 		IFA_LOCK(ifa);
1080 		if (ifa->ifa_addr->sa_family != AF_INET6 ||
1081 		    !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->
1082 		    sin6_addr)) {
1083 			IFA_UNLOCK(ifa);
1084 			ifa = TAILQ_NEXT(ifa, ifa_list);
1085 			continue;
1086 		}
1087 
1088 		ia = (struct in6_ifaddr *)ifa;
1089 
1090 		/* hold a reference for this routine */
1091 		IFA_ADDREF_LOCKED(ifa);
1092 		/* remove from the linked list */
1093 		if_detach_ifa(ifp, ifa);
1094 		IFA_UNLOCK(ifa);
1095 
1096 		/*
1097 		 * Leaving the multicast group(s) may involve freeing the
1098 		 * link address multicast structure(s) for the interface,
1099 		 * which is protected by ifnet lock.  To avoid violating
1100 		 * lock ordering, we must drop ifnet lock before doing so.
1101 		 * The ifa won't go away since we held a refcnt above.
1102 		 */
1103 		ifnet_lock_done(ifp);
1104 
1105 		/*
1106 		 * We have to do this work manually here instead of calling
1107 		 * in6_purgeaddr() since in6_purgeaddr() uses the RTM_HOST flag.
1108 		 */
1109 
1110 		/*
1111 		 * leave from multicast groups we have joined for the interface
1112 		 */
1113 		IFA_LOCK(ifa);
1114 		while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1115 			LIST_REMOVE(imm, i6mm_chain);
1116 			IFA_UNLOCK(ifa);
1117 			in6_leavegroup(imm);
1118 			IFA_LOCK(ifa);
1119 		}
1120 
1121 		/* remove from the routing table */
1122 		if (ia->ia_flags & IFA_ROUTE) {
1123 			IFA_UNLOCK(ifa);
1124 			rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0);
1125 			if (rt != NULL) {
1126 				(void) rtrequest(RTM_DELETE,
1127 				    (struct sockaddr *)&ia->ia_addr,
1128 				    (struct sockaddr *)&ia->ia_addr,
1129 				    (struct sockaddr *)&ia->ia_prefixmask,
1130 				    rt->rt_flags, (struct rtentry **)0);
1131 				rtfree(rt);
1132 			}
1133 		} else {
1134 			IFA_UNLOCK(ifa);
1135 		}
1136 
1137 		/* also remove from the IPv6 address chain(itojun&jinmei) */
1138 		unlinked = 0;
1139 		lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1140 		TAILQ_FOREACH(nia, &in6_ifaddrhead, ia6_link) {
1141 			if (ia == nia) {
1142 				TAILQ_REMOVE(&in6_ifaddrhead, ia, ia6_link);
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