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