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 | |
76 | #include <netinet/in.h> |
77 | #include <netinet/in_var.h> |
78 | #include <netinet/if_ether.h> |
79 | #include <netinet/in_pcb.h> |
80 | #include <netinet/icmp6.h> |
81 | |
82 | #include <netinet/ip6.h> |
83 | #include <netinet6/ip6_var.h> |
84 | #include <netinet6/in6_var.h> |
85 | #include <netinet6/in6_pcb.h> |
86 | #include <netinet6/in6_ifattach.h> |
87 | #include <netinet6/ip6_var.h> |
88 | #include <netinet6/nd6.h> |
89 | #include <netinet6/scope6_var.h> |
90 | |
91 | #include <net/net_osdep.h> |
92 | #include <dev/random/randomdev.h> |
93 | |
94 | #include <net/sockaddr_utils.h> |
95 | |
96 | u_int32_t in6_maxmtu = 0; |
97 | |
98 | #if IP6_AUTO_LINKLOCAL |
99 | int ip6_auto_linklocal = IP6_AUTO_LINKLOCAL; |
100 | #else |
101 | int ip6_auto_linklocal = 1; /* enable by default */ |
102 | #endif |
103 | |
104 | extern struct inpcbinfo udbinfo; |
105 | extern struct inpcbinfo ripcbinfo; |
106 | |
107 | static int get_rand_iid(struct ifnet *, struct in6_addr *); |
108 | static int in6_generate_tmp_iid(u_int8_t *, const u_int8_t *, u_int8_t *); |
109 | static int in6_select_iid_from_all_hw(struct ifnet *, struct ifnet *, |
110 | struct in6_addr *); |
111 | static int in6_ifattach_linklocal(struct ifnet *, struct in6_aliasreq *); |
112 | static int in6_ifattach_loopback(struct ifnet *); |
113 | |
114 | /* |
115 | * Generate a last-resort interface identifier, when the machine has no |
116 | * IEEE802/EUI64 address sources. |
117 | * The goal here is to get an interface identifier that is |
118 | * (1) random enough and (2) does not change across reboot. |
119 | * We currently use SHA256(hostname) for it. |
120 | * |
121 | * in6 - upper 64bits are preserved |
122 | */ |
123 | static int |
124 | get_rand_iid( |
125 | __unused struct ifnet *ifp, |
126 | struct in6_addr *in6) /* upper 64bits are preserved */ |
127 | { |
128 | SHA256_CTX ctxt; |
129 | u_int8_t digest[SHA256_DIGEST_LENGTH]; |
130 | size_t hostnlen; |
131 | |
132 | /* generate 8 bytes of pseudo-random value. */ |
133 | bzero(s: &ctxt, n: sizeof(ctxt)); |
134 | SHA256_Init(ctx: &ctxt); |
135 | lck_mtx_lock(lck: &hostname_lock); |
136 | hostnlen = strlen(s: hostname); |
137 | SHA256_Update(ctx: &ctxt, data: hostname, len: hostnlen); |
138 | lck_mtx_unlock(lck: &hostname_lock); |
139 | SHA256_Final(digest, ctx: &ctxt); |
140 | |
141 | /* assumes sizeof (digest) > sizeof (iid) */ |
142 | bcopy(src: digest, dst: &in6->s6_addr[8], n: 8); |
143 | |
144 | /* make sure to set "u" bit to local, and "g" bit to individual. */ |
145 | in6->s6_addr[8] &= ~ND6_EUI64_GBIT; /* g bit to "individual" */ |
146 | in6->s6_addr[8] |= ND6_EUI64_UBIT; /* u bit to "local" */ |
147 | |
148 | /* convert EUI64 into IPv6 interface identifier */ |
149 | ND6_EUI64_TO_IFID(in6); |
150 | |
151 | return 0; |
152 | } |
153 | |
154 | static int |
155 | in6_generate_tmp_iid( |
156 | u_int8_t *seed0, |
157 | const u_int8_t *seed1, |
158 | u_int8_t *ret) |
159 | { |
160 | SHA256_CTX ctxt; |
161 | u_int8_t seed[16], nullbuf[8], digest[SHA256_DIGEST_LENGTH]; |
162 | u_int32_t val32; |
163 | struct timeval tv; |
164 | |
165 | /* If there's no history, start with a random seed. */ |
166 | bzero(s: nullbuf, n: sizeof(nullbuf)); |
167 | if (bcmp(s1: nullbuf, s2: seed0, n: sizeof(nullbuf)) == 0) { |
168 | int i; |
169 | |
170 | for (i = 0; i < 2; i++) { |
171 | getmicrotime(&tv); |
172 | val32 = RandomULong() ^ tv.tv_usec; |
173 | bcopy(src: &val32, dst: seed + sizeof(val32) * i, |
174 | n: sizeof(val32)); |
175 | } |
176 | } else { |
177 | bcopy(src: seed0, dst: seed, n: 8); |
178 | } |
179 | |
180 | /* copy the right-most 64-bits of the given address */ |
181 | /* XXX assumption on the size of IFID */ |
182 | bcopy(src: seed1, dst: &seed[8], n: 8); |
183 | |
184 | #if DEVELOPMENT || DEBUG |
185 | if ((0)) { /* for debugging purposes only */ |
186 | int i; |
187 | |
188 | printf("%s: new randomized ID from: " , __func__); |
189 | for (i = 0; i < 16; i++) { |
190 | printf("%02x" , seed[i]); |
191 | } |
192 | printf(" " ); |
193 | } |
194 | #endif /* DEVELOPMENT || DEBUG */ |
195 | |
196 | /* generate 16 bytes of pseudo-random value. */ |
197 | bzero(s: &ctxt, n: sizeof(ctxt)); |
198 | SHA256_Init(ctx: &ctxt); |
199 | SHA256_Update(ctx: &ctxt, data: seed, len: sizeof(seed)); |
200 | SHA256_Final(digest, ctx: &ctxt); |
201 | |
202 | /* |
203 | * RFC 4941 3.2.1. (3) |
204 | * Take the left-most 64-bits of the SHA256 digest and set bit 6 (the |
205 | * left-most bit is numbered 0) to zero. |
206 | */ |
207 | bcopy(src: digest, dst: ret, n: 8); |
208 | ret[0] &= ~ND6_EUI64_UBIT; |
209 | |
210 | /* |
211 | * XXX: we'd like to ensure that the generated value is not zero |
212 | * for simplicity. If the caclculated digest happens to be zero, |
213 | * use a random non-zero value as the last resort. |
214 | */ |
215 | if (bcmp(s1: nullbuf, s2: ret, n: sizeof(nullbuf)) == 0) { |
216 | nd6log(info, |
217 | "%s: computed SHA256 value is zero.\n" , __func__); |
218 | |
219 | getmicrotime(&tv); |
220 | val32 = random() ^ tv.tv_usec; |
221 | val32 = 1 + (val32 % (0xffffffff - 1)); |
222 | } |
223 | |
224 | /* |
225 | * RFC 4941 3.2.1. (4) |
226 | * Take the next 64-bits of the SHA256 digest and save them in |
227 | * stable storage as the history value to be used in the next |
228 | * iteration of the algorithm. |
229 | */ |
230 | bcopy(src: &digest[8], dst: seed0, n: 8); |
231 | |
232 | #if DEVELOPMENT || DEBUG |
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 | #endif |
243 | |
244 | return 0; |
245 | } |
246 | |
247 | /* |
248 | * Get interface identifier for the specified interface using the method in |
249 | * Appendix A of RFC 4291. |
250 | * |
251 | * XXX assumes single sockaddr_dl (AF_LINK address) per an interface |
252 | * |
253 | * in6 - upper 64bits are preserved |
254 | */ |
255 | int |
256 | in6_iid_from_hw(struct ifnet *ifp, struct in6_addr *in6) |
257 | { |
258 | struct ifaddr *ifa = NULL; |
259 | struct sockaddr_dl *sdl; |
260 | u_int8_t *addr; |
261 | size_t addrlen; |
262 | static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
263 | static u_int8_t allone[8] = |
264 | { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
265 | int err = -1; |
266 | |
267 | /* Why doesn't this code use ifnet_addrs? */ |
268 | ifnet_lock_shared(ifp); |
269 | ifa = ifp->if_lladdr; |
270 | sdl = SDL(ifa->ifa_addr); |
271 | if (sdl->sdl_alen == 0) { |
272 | ifnet_lock_done(ifp); |
273 | return -1; |
274 | } |
275 | ifa_addref(ifa); /* for this routine */ |
276 | ifnet_lock_done(ifp); |
277 | |
278 | IFA_LOCK(ifa); |
279 | addr = (u_int8_t *) LLADDR(sdl); |
280 | addrlen = sdl->sdl_alen; |
281 | |
282 | /* get EUI64 */ |
283 | switch (ifp->if_type) { |
284 | case IFT_ETHER: |
285 | case IFT_FDDI: |
286 | case IFT_ISO88025: |
287 | case IFT_ATM: |
288 | case IFT_IEEE1394: |
289 | case IFT_L2VLAN: |
290 | case IFT_IEEE8023ADLAG: |
291 | #if IFT_IEEE80211 |
292 | case IFT_IEEE80211: |
293 | #endif |
294 | case IFT_BRIDGE: |
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(s1: addr, s2: allzero, n: addrlen) == 0) { |
312 | goto done; |
313 | } |
314 | if (bcmp(s1: addr, s2: allone, n: addrlen) == 0) { |
315 | goto done; |
316 | } |
317 | |
318 | /* make EUI64 address */ |
319 | if (addrlen == 8) { |
320 | bcopy(src: addr, dst: &in6->s6_addr[8], n: 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(s: &in6->s6_addr[8], n: 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(s1: &in6->s6_addr[9], s2: allzero, n: 7) == 0) { |
384 | goto done; |
385 | } |
386 | |
387 | err = 0; /* found */ |
388 | |
389 | done: |
390 | IFA_UNLOCK(ifa); |
391 | ifa_remref(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 |
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(ifp: 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(ifp: 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 |
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. (jinmei@kame.net 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(s: &pr0, n: sizeof(pr0)); |
506 | lck_mtx_init(lck: &pr0.ndpr_lock, grp: &ifa_mtx_grp, attr: &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(ifa: &ia->ia_ifa); |
536 | lck_mtx_destroy(lck: &pr0.ndpr_lock, grp: &ifa_mtx_grp); |
537 | return error; |
538 | } |
539 | } |
540 | |
541 | in6_post_msg(ifp, KEV_INET6_NEW_LL_ADDR, ia, NULL); |
542 | ifa_remref(ifa: &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(lck: &pr0.ndpr_lock, grp: &ifa_mtx_grp); |
550 | return 0; |
551 | } |
552 | |
553 | static int |
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(s: &ifra, n: sizeof(ifra)); |
562 | |
563 | /* |
564 | * in6_update_ifa() does not use ifra_name, but we accurately set it |
565 | * for safety. |
566 | */ |
567 | strlcpy(dst: ifra.ifra_name, if_name(ifp), n: 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(ifa: &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 |
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 | SHA256_CTX ctxt; |
624 | u_int8_t digest[SHA256_DIGEST_LENGTH]; |
625 | size_t l; |
626 | char n[64]; /* a single label must not exceed 63 chars */ |
627 | |
628 | if (!namelen || !name) { |
629 | return -1; |
630 | } |
631 | |
632 | p = name; |
633 | while (p && *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(dst: n, src: name, n: 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(s: &ctxt, n: sizeof(ctxt)); |
650 | SHA256_Init(ctx: &ctxt); |
651 | SHA256_Update(ctx: &ctxt, data: &l, len: sizeof(l)); |
652 | SHA256_Update(ctx: &ctxt, data: n, len: l); |
653 | SHA256_Final(digest, ctx: &ctxt); |
654 | |
655 | bzero(s: in6, n: 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(src: digest, dst: &in6->s6_addr8[13], n: 3); |
661 | if (in6_setscope(in6, ifp, ifscopep)) { |
662 | return -1; /* XXX: should not fail */ |
663 | } |
664 | return 0; |
665 | } |
666 | |
667 | int |
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 |
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(s: &IN6_IFEXTRA(ifp)->icmp6_ifstat, |
750 | n: sizeof(IN6_IFEXTRA(ifp)->icmp6_ifstat)); |
751 | bzero(s: &IN6_IFEXTRA(ifp)->in6_ifstat, |
752 | n: sizeof(IN6_IFEXTRA(ifp)->in6_ifstat)); |
753 | /* |
754 | * XXX When recycling, nd_ifinfo gets initialized, other |
755 | * than the lock, inside nd6_ifattach |
756 | */ |
757 | } |
758 | |
759 | /* |
760 | * XXX Only initialize IPv6 configuration for the interface |
761 | * if interface has not yet been configured with |
762 | * link local IPv6 address. |
763 | * Could possibly be optimized with an interface flag if need |
764 | * be. For now using in6ifa_ifpforlinklocal. |
765 | */ |
766 | ia6 = in6ifa_ifpforlinklocal(ifp, 0); |
767 | if (ia6 == NULL) { |
768 | IN6_IFEXTRA(ifp)->netsig_len = 0; |
769 | bzero(s: &IN6_IFEXTRA(ifp)->netsig, |
770 | n: sizeof(IN6_IFEXTRA(ifp)->netsig)); |
771 | bzero(IN6_IFEXTRA(ifp)->nat64_prefixes, |
772 | n: sizeof(IN6_IFEXTRA(ifp)->nat64_prefixes)); |
773 | /* initialize NDP variables */ |
774 | nd6_ifattach(ifp); |
775 | } else { |
776 | VERIFY(ND_IFINFO(ifp)->initialized); |
777 | ifa_remref(ifa: &ia6->ia_ifa); |
778 | ia6 = NULL; |
779 | } |
780 | scope6_ifattach(ifp); |
781 | |
782 | /* initialize loopback interface address */ |
783 | if ((ifp->if_flags & IFF_LOOPBACK) != 0) { |
784 | error = in6_ifattach_loopback(ifp); |
785 | if (error != 0) { |
786 | log(LOG_ERR, "%s: in6_ifattach_loopback returned %d\n" , |
787 | __func__, error); |
788 | return error; |
789 | } |
790 | } |
791 | |
792 | /* update dynamically. */ |
793 | if (in6_maxmtu < ifp->if_mtu) { |
794 | in6_maxmtu = ifp->if_mtu; |
795 | } |
796 | |
797 | VERIFY(error == 0); |
798 | return 0; |
799 | } |
800 | |
801 | /* |
802 | * This routine is only meant to configure IPv6 Link Local |
803 | * addresses. |
804 | */ |
805 | int |
806 | in6_ifattach_aliasreq(struct ifnet *ifp, struct ifnet *altifp, |
807 | struct in6_aliasreq *ifra0) |
808 | { |
809 | int error; |
810 | struct in6_ifaddr *ia6; |
811 | struct in6_aliasreq ifra; |
812 | |
813 | error = in6_ifattach_prelim(ifp); |
814 | if (error != 0) { |
815 | return error; |
816 | } |
817 | |
818 | if (!ip6_auto_linklocal) { |
819 | return 0; |
820 | } |
821 | |
822 | /* |
823 | * Assign a link-local address, only if there isn't one here already. |
824 | * XXX If we ever allow more than one LLA on the interface |
825 | * make sure that the corresponding prefix on the prefixlist |
826 | * is reference counted and the address's prefix pointer |
827 | * points to the prefix. |
828 | */ |
829 | ia6 = in6ifa_ifpforlinklocal(ifp, 0); |
830 | if (ia6 != NULL) { |
831 | ifa_remref(ifa: &ia6->ia_ifa); |
832 | return 0; |
833 | } |
834 | |
835 | bzero(s: &ifra, n: sizeof(ifra)); |
836 | |
837 | /* |
838 | * in6_update_ifa() does not use ifra_name, but we accurately set it |
839 | * for safety. |
840 | */ |
841 | strlcpy(dst: ifra.ifra_name, if_name(ifp), n: sizeof(ifra.ifra_name)); |
842 | |
843 | /* Initialize the IPv6 interface address in our in6_aliasreq block */ |
844 | if (ifra0 != NULL) { |
845 | /* interface provided both addresses for us */ |
846 | struct sockaddr_in6 *sin6 = &ifra.ifra_addr; |
847 | struct in6_addr *in6 = &sin6->sin6_addr; |
848 | boolean_t ok = TRUE; |
849 | |
850 | SOCKADDR_COPY(&ifra0->ifra_addr, sin6, sizeof(struct sockaddr_in6)); |
851 | |
852 | if (sin6->sin6_family != AF_INET6 || sin6->sin6_port != 0) { |
853 | ok = FALSE; |
854 | } |
855 | if (ok && (in6->s6_addr16[0] != htons(0xfe80))) { |
856 | ok = FALSE; |
857 | } |
858 | |
859 | if (ok) { |
860 | if (sin6->sin6_scope_id == 0 && in6->s6_addr16[1] == 0) { |
861 | if (in6_embedded_scope) { |
862 | in6->s6_addr16[1] = htons(ifp->if_index); |
863 | } else { |
864 | sin6->sin6_scope_id = ifp->if_index; |
865 | } |
866 | } else if (sin6->sin6_scope_id != 0 && |
867 | sin6->sin6_scope_id != ifp->if_index) { |
868 | ok = FALSE; |
869 | } else if (in6_embedded_scope && in6->s6_addr16[1] != 0 && |
870 | ntohs(in6->s6_addr16[1]) != ifp->if_index) { |
871 | ok = FALSE; |
872 | } |
873 | } |
874 | if (ok && (in6->s6_addr32[1] != 0)) { |
875 | ok = FALSE; |
876 | } |
877 | if (!ok) { |
878 | return EINVAL; |
879 | } |
880 | } else { |
881 | ifra.ifra_addr.sin6_family = AF_INET6; |
882 | ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); |
883 | ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80); |
884 | if (in6_embedded_scope) { |
885 | ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); |
886 | } else { |
887 | ifra.ifra_addr.sin6_addr.s6_addr16[1] = 0; |
888 | ifra.ifra_addr.sin6_scope_id = ifp->if_index; |
889 | } |
890 | ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; |
891 | if ((ifp->if_flags & IFF_LOOPBACK) != 0) { |
892 | ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; |
893 | ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); |
894 | if (!in6_embedded_scope) { |
895 | ifra.ifra_addr.sin6_scope_id = ifp->if_index; |
896 | } |
897 | } else { |
898 | if (in6_select_iid_from_all_hw(ifp0: ifp, altifp, |
899 | in6: &ifra.ifra_addr.sin6_addr) != 0) { |
900 | nd6log(error, "%s: no IID available\n" , |
901 | if_name(ifp)); |
902 | return EADDRNOTAVAIL; |
903 | } |
904 | } |
905 | } |
906 | |
907 | if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, IN6_NULL_IF_EMBEDDED_SCOPE(&ifra.ifra_addr.sin6_scope_id))) { |
908 | return EADDRNOTAVAIL; |
909 | } |
910 | |
911 | /* Set the prefix mask */ |
912 | ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); |
913 | ifra.ifra_prefixmask.sin6_family = AF_INET6; |
914 | ifra.ifra_prefixmask.sin6_addr = in6mask64; |
915 | |
916 | /* link-local addresses should NEVER expire. */ |
917 | ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; |
918 | ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; |
919 | |
920 | /* Attach the link-local address */ |
921 | if (in6_ifattach_linklocal(ifp, ifra: &ifra) != 0) { |
922 | nd6log(info, |
923 | "%s: %s could not attach link-local address.\n" , |
924 | __func__, if_name(ifp)); |
925 | /* NB: not an error */ |
926 | } |
927 | |
928 | return 0; |
929 | } |
930 | |
931 | int |
932 | in6_ifattach_llcgareq(struct ifnet *ifp, struct in6_cgareq *llcgasr) |
933 | { |
934 | struct in6_aliasreq ifra; |
935 | struct in6_ifaddr *ia6 = NULL; |
936 | struct nd_ifinfo *ndi = NULL; |
937 | int error; |
938 | |
939 | VERIFY(llcgasr != NULL); |
940 | |
941 | error = in6_ifattach_prelim(ifp); |
942 | if (error != 0) { |
943 | return error; |
944 | } |
945 | |
946 | if (!ip6_auto_linklocal) { |
947 | return 0; |
948 | } |
949 | |
950 | if (nd6_send_opstate == ND6_SEND_OPMODE_DISABLED) { |
951 | return ENXIO; |
952 | } |
953 | |
954 | ndi = ND_IFINFO(ifp); |
955 | VERIFY(ndi != NULL && ndi->initialized); |
956 | if ((ndi->flags & ND6_IFF_INSECURE) != 0) { |
957 | return ENXIO; |
958 | } |
959 | |
960 | /* |
961 | * Assign a link-local address, only if there isn't one here already. |
962 | * XXX If we ever allow more than one LLA on the interface |
963 | * make sure that the corresponding prefix on the prefixlist |
964 | * is reference counted and the address's prefix pointer |
965 | * points to the prefix. |
966 | */ |
967 | ia6 = in6ifa_ifpforlinklocal(ifp, 0); |
968 | if (ia6 != NULL) { |
969 | ifa_remref(ifa: &ia6->ia_ifa); |
970 | return 0; |
971 | } |
972 | |
973 | bzero(s: &ifra, n: sizeof(ifra)); |
974 | strlcpy(dst: ifra.ifra_name, if_name(ifp), n: sizeof(ifra.ifra_name)); |
975 | |
976 | ifra.ifra_addr.sin6_family = AF_INET6; |
977 | ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); |
978 | ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80); |
979 | ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; |
980 | ifra.ifra_flags = IN6_IFF_SECURED; |
981 | |
982 | in6_cga_node_lock(); |
983 | if (in6_cga_generate(&llcgasr->cgar_cgaprep, llcgasr->cgar_collision_count, |
984 | &ifra.ifra_addr.sin6_addr, ifp)) { |
985 | in6_cga_node_unlock(); |
986 | return EADDRNOTAVAIL; |
987 | } |
988 | in6_cga_node_unlock(); |
989 | if (in6_embedded_scope) { |
990 | ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); |
991 | } else { |
992 | ifra.ifra_addr.sin6_addr.s6_addr16[1] = 0; |
993 | } |
994 | |
995 | if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, IN6_NULL_IF_EMBEDDED_SCOPE(&ifra.ifra_addr.sin6_scope_id))) { |
996 | return EADDRNOTAVAIL; |
997 | } |
998 | |
999 | /* Set the prefix mask */ |
1000 | ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); |
1001 | ifra.ifra_prefixmask.sin6_family = AF_INET6; |
1002 | ifra.ifra_prefixmask.sin6_addr = in6mask64; |
1003 | |
1004 | /* |
1005 | * link-local addresses should NEVER expire, but cryptographic |
1006 | * ones may have finite preferred lifetime [if it's important to |
1007 | * keep them from being used by applications as persistent device |
1008 | * identifiers]. |
1009 | */ |
1010 | ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; |
1011 | ifra.ifra_lifetime.ia6t_pltime = llcgasr->cgar_lifetime.ia6t_pltime; |
1012 | |
1013 | /* Attach the link-local address */ |
1014 | if (in6_ifattach_linklocal(ifp, ifra: &ifra) != 0) { |
1015 | /* NB: not an error */ |
1016 | nd6log(info, |
1017 | "%s: %s could not attach link-local address.\n" , |
1018 | __func__, if_name(ifp)); |
1019 | } |
1020 | |
1021 | VERIFY(error == 0); |
1022 | return error; |
1023 | } |
1024 | |
1025 | /* |
1026 | * NOTE: in6_ifdetach() does not support loopback if at this moment. |
1027 | */ |
1028 | void |
1029 | in6_ifdetach(struct ifnet *ifp) |
1030 | { |
1031 | struct in6_ifaddr *ia, *nia; |
1032 | struct ifaddr *ifa; |
1033 | struct rtentry *rt; |
1034 | struct sockaddr_in6 sin6; |
1035 | struct in6_multi_mship *imm; |
1036 | int unlinked; |
1037 | |
1038 | LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED); |
1039 | |
1040 | /* remove neighbor management table */ |
1041 | nd6_purge(ifp); |
1042 | |
1043 | /* nuke any of IPv6 addresses we have */ |
1044 | lck_rw_lock_exclusive(lck: &in6_ifaddr_rwlock); |
1045 | boolean_t from_begining = TRUE; |
1046 | while (from_begining) { |
1047 | from_begining = FALSE; |
1048 | TAILQ_FOREACH(ia, &in6_ifaddrhead, ia6_link) { |
1049 | if (ia->ia_ifa.ifa_ifp != ifp) { |
1050 | continue; |
1051 | } |
1052 | ifa_addref(ifa: &ia->ia_ifa); /* for us */ |
1053 | lck_rw_done(lck: &in6_ifaddr_rwlock); |
1054 | in6_purgeaddr(&ia->ia_ifa); |
1055 | ifa_remref(ifa: &ia->ia_ifa); /* for us */ |
1056 | lck_rw_lock_exclusive(lck: &in6_ifaddr_rwlock); |
1057 | /* |
1058 | * Purging the address caused in6_ifaddr_rwlock |
1059 | * to be dropped and reacquired; |
1060 | * therefore search again from the beginning |
1061 | * of in6_ifaddrs list. |
1062 | */ |
1063 | from_begining = TRUE; |
1064 | break; |
1065 | } |
1066 | } |
1067 | lck_rw_done(lck: &in6_ifaddr_rwlock); |
1068 | |
1069 | ifnet_lock_exclusive(ifp); |
1070 | |
1071 | /* undo everything done by in6_ifattach(), just in case */ |
1072 | ifa = TAILQ_FIRST(&ifp->if_addrlist); |
1073 | while (ifa != NULL) { |
1074 | IFA_LOCK(ifa); |
1075 | if (ifa->ifa_addr->sa_family != AF_INET6 || |
1076 | !IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa->ifa_addr)->sin6_addr)) { |
1077 | IFA_UNLOCK(ifa); |
1078 | ifa = TAILQ_NEXT(ifa, ifa_list); |
1079 | continue; |
1080 | } |
1081 | |
1082 | ia = (struct in6_ifaddr *)ifa; |
1083 | |
1084 | /* hold a reference for this routine */ |
1085 | ifa_addref(ifa); |
1086 | /* remove from the linked list */ |
1087 | if_detach_ifa(ifp, ifa); |
1088 | IFA_UNLOCK(ifa); |
1089 | |
1090 | /* |
1091 | * Leaving the multicast group(s) may involve freeing the |
1092 | * link address multicast structure(s) for the interface, |
1093 | * which is protected by ifnet lock. To avoid violating |
1094 | * lock ordering, we must drop ifnet lock before doing so. |
1095 | * The ifa won't go away since we held a refcnt above. |
1096 | */ |
1097 | ifnet_lock_done(ifp); |
1098 | |
1099 | /* |
1100 | * We have to do this work manually here instead of calling |
1101 | * in6_purgeaddr() since in6_purgeaddr() uses the RTM_HOST flag. |
1102 | */ |
1103 | |
1104 | /* |
1105 | * leave from multicast groups we have joined for the interface |
1106 | */ |
1107 | IFA_LOCK(ifa); |
1108 | while ((imm = ia->ia6_memberships.lh_first) != NULL) { |
1109 | LIST_REMOVE(imm, i6mm_chain); |
1110 | IFA_UNLOCK(ifa); |
1111 | in6_leavegroup(imm); |
1112 | IFA_LOCK(ifa); |
1113 | } |
1114 | |
1115 | /* remove from the routing table */ |
1116 | if (ia->ia_flags & IFA_ROUTE) { |
1117 | IFA_UNLOCK(ifa); |
1118 | rt = rtalloc1(SA(&ia->ia_addr), 0, 0); |
1119 | if (rt != NULL) { |
1120 | (void) rtrequest(RTM_DELETE, |
1121 | SA(&ia->ia_addr), |
1122 | SA(&ia->ia_addr), |
1123 | SA(&ia->ia_prefixmask), |
1124 | rt->rt_flags, (struct rtentry **)0); |
1125 | rtfree(rt); |
1126 | } |
1127 | } else { |
1128 | IFA_UNLOCK(ifa); |
1129 | } |
1130 | |
1131 | /* also remove from the IPv6 address chain(itojun&jinmei) */ |
1132 | unlinked = 0; |
1133 | lck_rw_lock_exclusive(lck: &in6_ifaddr_rwlock); |
1134 | TAILQ_FOREACH(nia, &in6_ifaddrhead, ia6_link) { |
1135 | if (ia == nia) { |
1136 | TAILQ_REMOVE(&in6_ifaddrhead, ia, ia6_link); |
1137 | os_atomic_inc(&in6_ifaddrlist_genid, relaxed); |
1138 | unlinked = 1; |
1139 | break; |
1140 | } |
1141 | } |
1142 | lck_rw_done(lck: &in6_ifaddr_rwlock); |
1143 | |
1144 | /* |
1145 | * release another refcnt for the link from in6_ifaddrs. |
1146 | * Do this only if it's not already unlinked in the event |
1147 | * that we lost the race, since in6_ifaddr_rwlock was |
1148 | * momentarily dropped above. |
1149 | */ |
1150 | if (unlinked) { |
1151 | ifa_remref(ifa); |
1152 | } |
1153 | /* release reference held for this routine */ |
1154 | ifa_remref(ifa); |
1155 | |
1156 | /* |
1157 | * This is suboptimal, but since we dropped ifnet lock above |
1158 | * the list might have changed. Repeat the search from the |
1159 | * beginning until we find the first eligible IPv6 address. |
1160 | */ |
1161 | ifnet_lock_exclusive(ifp); |
1162 | ifa = TAILQ_FIRST(&ifp->if_addrlist); |
1163 | } |
1164 | ifnet_lock_done(ifp); |
1165 | |
1166 | /* invalidate route caches */ |
1167 | routegenid_inet6_update(); |
1168 | |
1169 | /* |
1170 | * remove neighbor management table. we call it twice just to make |
1171 | * sure we nuke everything. maybe we need just one call. |
1172 | * XXX: since the first call did not release addresses, some prefixes |
1173 | * might remain. We should call nd6_purge() again to release the |
1174 | * prefixes after removing all addresses above. |
1175 | * (Or can we just delay calling nd6_purge until at this point?) |
1176 | */ |
1177 | nd6_purge(ifp); |
1178 | |
1179 | /* remove route to link-local allnodes multicast (ff02::1) */ |
1180 | SOCKADDR_ZERO(&sin6, sizeof(sin6)); |
1181 | sin6.sin6_len = sizeof(struct sockaddr_in6); |
1182 | sin6.sin6_family = AF_INET6; |
1183 | sin6.sin6_addr = in6addr_linklocal_allnodes; |
1184 | if (in6_embedded_scope) { |
1185 | sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index); |
1186 | } else { |
1187 | sin6.sin6_scope_id = ifp->if_index; |
1188 | } |
1189 | rt = rtalloc1(SA(&sin6), 0, 0); |
1190 | if (rt != NULL) { |
1191 | RT_LOCK(rt); |
1192 | if (rt->rt_ifp == ifp) { |
1193 | /* |
1194 | * Prevent another thread from modifying rt_key, |
1195 | * rt_gateway via rt_setgate() after the rt_lock |
1196 | * is dropped by marking the route as defunct. |
1197 | */ |
1198 | rt->rt_flags |= RTF_CONDEMNED; |
1199 | RT_UNLOCK(rt); |
1200 | (void) rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, |
1201 | rt_mask(rt), rt->rt_flags, 0); |
1202 | } else { |
1203 | RT_UNLOCK(rt); |
1204 | } |
1205 | rtfree(rt); |
1206 | } |
1207 | } |
1208 | |
1209 | void |
1210 | in6_iid_mktmp(struct ifnet *ifp, u_int8_t *retbuf, const u_int8_t *baseid, |
1211 | int generate) |
1212 | { |
1213 | u_int8_t nullbuf[8]; |
1214 | struct nd_ifinfo *ndi = ND_IFINFO(ifp); |
1215 | |
1216 | VERIFY(ndi != NULL && ndi->initialized); |
1217 | lck_mtx_lock(lck: &ndi->lock); |
1218 | bzero(s: nullbuf, n: sizeof(nullbuf)); |
1219 | if (bcmp(s1: ndi->randomid, s2: nullbuf, n: sizeof(nullbuf)) == 0) { |
1220 | /* we've never created a random ID. Create a new one. */ |
1221 | generate = 1; |
1222 | } |
1223 | |
1224 | if (generate) { |
1225 | bcopy(src: baseid, dst: ndi->randomseed1, n: sizeof(ndi->randomseed1)); |
1226 | |
1227 | /* in6_generate_tmp_iid will update seedn and buf */ |
1228 | (void) in6_generate_tmp_iid(seed0: ndi->randomseed0, seed1: ndi->randomseed1, |
1229 | ret: ndi->randomid); |
1230 | } |
1231 | |
1232 | bcopy(src: ndi->randomid, dst: retbuf, n: 8); |
1233 | lck_mtx_unlock(lck: &ndi->lock); |
1234 | } |
1235 | |
1236 | void |
1237 | in6_tmpaddrtimer(void *arg) |
1238 | { |
1239 | #pragma unused(arg) |
1240 | struct ifnet *ifp = NULL; |
1241 | struct nd_ifinfo *ndi = NULL; |
1242 | u_int8_t nullbuf[8]; |
1243 | |
1244 | timeout(in6_tmpaddrtimer, arg: (caddr_t)0, ticks: (ip6_temp_preferred_lifetime - |
1245 | ip6_desync_factor - ip6_temp_regen_advance) * hz); |
1246 | |
1247 | bzero(s: nullbuf, n: sizeof(nullbuf)); |
1248 | ifnet_head_lock_shared(); |
1249 | for (ifp = ifnet_head.tqh_first; ifp; |
1250 | ifp = ifp->if_link.tqe_next) { |
1251 | ndi = ND_IFINFO(ifp); |
1252 | if ((NULL == ndi) || (FALSE == ndi->initialized)) { |
1253 | continue; |
1254 | } |
1255 | lck_mtx_lock(lck: &ndi->lock); |
1256 | if (bcmp(s1: ndi->randomid, s2: nullbuf, n: sizeof(nullbuf)) != 0) { |
1257 | /* |
1258 | * We've been generating a random ID on this interface. |
1259 | * Create a new one. |
1260 | */ |
1261 | (void) in6_generate_tmp_iid(seed0: ndi->randomseed0, |
1262 | seed1: ndi->randomseed1, ret: ndi->randomid); |
1263 | } |
1264 | lck_mtx_unlock(lck: &ndi->lock); |
1265 | } |
1266 | ifnet_head_done(); |
1267 | } |
1268 | |