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