]>
Commit | Line | Data |
---|---|---|
2d21ac55 | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2003-2011 Apple Inc. All rights reserved. |
2d21ac55 A |
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 | */ | |
55e303ae A |
28 | /* $FreeBSD: src/sys/netinet6/in6_ifattach.c,v 1.8 2002/04/19 04:46:22 suz Exp $ */ |
29 | /* $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $ */ | |
1c79356b A |
30 | |
31 | /* | |
32 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
33 | * All rights reserved. | |
34 | * | |
35 | * Redistribution and use in source and binary forms, with or without | |
36 | * modification, are permitted provided that the following conditions | |
37 | * are met: | |
38 | * 1. Redistributions of source code must retain the above copyright | |
39 | * notice, this list of conditions and the following disclaimer. | |
40 | * 2. Redistributions in binary form must reproduce the above copyright | |
41 | * notice, this list of conditions and the following disclaimer in the | |
42 | * documentation and/or other materials provided with the distribution. | |
43 | * 3. Neither the name of the project nor the names of its contributors | |
44 | * may be used to endorse or promote products derived from this software | |
45 | * without specific prior written permission. | |
46 | * | |
47 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
48 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
49 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
50 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
51 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
52 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
53 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
54 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
55 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
56 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
57 | * SUCH DAMAGE. | |
58 | */ | |
59 | ||
60 | #include <sys/param.h> | |
61 | #include <sys/systm.h> | |
62 | #include <sys/malloc.h> | |
63 | #include <sys/socket.h> | |
9bccf70c | 64 | #include <sys/socketvar.h> |
1c79356b A |
65 | #include <sys/sockio.h> |
66 | #include <sys/kernel.h> | |
9bccf70c | 67 | #include <sys/syslog.h> |
2d21ac55 A |
68 | #include <libkern/crypto/md5.h> |
69 | #include <libkern/OSAtomic.h> | |
91447636 | 70 | #include <kern/lock.h> |
1c79356b A |
71 | |
72 | #include <net/if.h> | |
73 | #include <net/if_dl.h> | |
74 | #include <net/if_types.h> | |
75 | #include <net/route.h> | |
2d21ac55 | 76 | #include <net/kpi_protocol.h> |
1c79356b A |
77 | |
78 | #include <netinet/in.h> | |
79 | #include <netinet/in_var.h> | |
1c79356b | 80 | #include <netinet/if_ether.h> |
9bccf70c | 81 | #include <netinet/in_pcb.h> |
6d2010ae | 82 | #include <netinet/icmp6.h> |
1c79356b A |
83 | |
84 | #include <netinet/ip6.h> | |
85 | #include <netinet6/ip6_var.h> | |
9bccf70c A |
86 | #include <netinet6/in6_var.h> |
87 | #include <netinet6/in6_pcb.h> | |
1c79356b A |
88 | #include <netinet6/in6_ifattach.h> |
89 | #include <netinet6/ip6_var.h> | |
90 | #include <netinet6/nd6.h> | |
9bccf70c | 91 | #include <netinet6/scope6_var.h> |
1c79356b A |
92 | |
93 | #include <net/net_osdep.h> | |
94 | ||
1c79356b A |
95 | struct in6_ifstat **in6_ifstat = NULL; |
96 | struct icmp6_ifstat **icmp6_ifstat = NULL; | |
97 | size_t in6_ifstatmax = 0; | |
98 | size_t icmp6_ifstatmax = 0; | |
b0d623f7 | 99 | u_int32_t in6_maxmtu = 0; |
91447636 | 100 | extern lck_mtx_t *nd6_mutex; |
1c79356b | 101 | |
9bccf70c A |
102 | #if IP6_AUTO_LINKLOCAL |
103 | int ip6_auto_linklocal = IP6_AUTO_LINKLOCAL; | |
104 | #else | |
105 | int ip6_auto_linklocal = 1; /* enable by default */ | |
106 | #endif | |
1c79356b | 107 | |
6d2010ae A |
108 | int loopattach6_done = 0; |
109 | ||
9bccf70c A |
110 | extern struct inpcbinfo udbinfo; |
111 | extern struct inpcbinfo ripcbinfo; | |
1c79356b | 112 | |
91447636 A |
113 | static int get_rand_ifid(struct ifnet *, struct in6_addr *); |
114 | static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *); | |
6d2010ae | 115 | int in6_get_hw_ifid(struct ifnet *, struct in6_addr *); |
91447636 A |
116 | static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *); |
117 | static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *, struct in6_aliasreq *); | |
118 | static int in6_ifattach_loopback(struct ifnet *); | |
1c79356b | 119 | |
9bccf70c A |
120 | #define EUI64_GBIT 0x01 |
121 | #define EUI64_UBIT 0x02 | |
122 | #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) | |
123 | #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) | |
124 | #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6)) | |
125 | #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT) | |
126 | #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6)) | |
127 | ||
128 | #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6)) | |
129 | #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6)) | |
1c79356b A |
130 | |
131 | /* | |
132 | * Generate a last-resort interface identifier, when the machine has no | |
133 | * IEEE802/EUI64 address sources. | |
9bccf70c A |
134 | * The goal here is to get an interface identifier that is |
135 | * (1) random enough and (2) does not change across reboot. | |
136 | * We currently use MD5(hostname) for it. | |
6d2010ae A |
137 | * |
138 | * in6 - upper 64bits are preserved | |
1c79356b A |
139 | */ |
140 | static int | |
91447636 | 141 | get_rand_ifid( |
2d21ac55 | 142 | __unused struct ifnet *ifp, |
91447636 | 143 | struct in6_addr *in6) /* upper 64bits are preserved */ |
1c79356b A |
144 | { |
145 | MD5_CTX ctxt; | |
146 | u_int8_t digest[16]; | |
6d2010ae | 147 | int hostnlen = strlen(hostname); |
9bccf70c A |
148 | |
149 | #if 0 | |
150 | /* we need at least several letters as seed for ifid */ | |
2d21ac55 | 151 | if (len < 3) |
9bccf70c | 152 | return -1; |
1c79356b A |
153 | #endif |
154 | ||
9bccf70c | 155 | /* generate 8 bytes of pseudo-random value. */ |
1c79356b A |
156 | bzero(&ctxt, sizeof(ctxt)); |
157 | MD5Init(&ctxt); | |
6d2010ae | 158 | MD5Update(&ctxt, hostname, hostnlen); |
1c79356b A |
159 | MD5Final(digest, &ctxt); |
160 | ||
9bccf70c A |
161 | /* assumes sizeof(digest) > sizeof(ifid) */ |
162 | bcopy(digest, &in6->s6_addr[8], 8); | |
1c79356b A |
163 | |
164 | /* make sure to set "u" bit to local, and "g" bit to individual. */ | |
9bccf70c A |
165 | in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ |
166 | in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ | |
167 | ||
168 | /* convert EUI64 into IPv6 interface identifier */ | |
169 | EUI64_TO_IFID(in6); | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
174 | static int | |
91447636 A |
175 | generate_tmp_ifid( |
176 | u_int8_t *seed0, | |
177 | const u_int8_t *seed1, | |
178 | u_int8_t *ret) | |
9bccf70c A |
179 | { |
180 | MD5_CTX ctxt; | |
181 | u_int8_t seed[16], digest[16], nullbuf[8]; | |
182 | u_int32_t val32; | |
183 | struct timeval tv; | |
184 | ||
6d2010ae | 185 | /* If there's no history, start with a random seed. */ |
9bccf70c A |
186 | bzero(nullbuf, sizeof(nullbuf)); |
187 | if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) { | |
188 | int i; | |
189 | ||
190 | for (i = 0; i < 2; i++) { | |
191 | microtime(&tv); | |
192 | val32 = random() ^ tv.tv_usec; | |
193 | bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32)); | |
194 | } | |
55e303ae | 195 | } else { |
9bccf70c | 196 | bcopy(seed0, seed, 8); |
55e303ae | 197 | } |
9bccf70c A |
198 | |
199 | /* copy the right-most 64-bits of the given address */ | |
200 | /* XXX assumption on the size of IFID */ | |
201 | bcopy(seed1, &seed[8], 8); | |
202 | ||
203 | if (0) { /* for debugging purposes only */ | |
204 | int i; | |
205 | ||
206 | printf("generate_tmp_ifid: new randomized ID from: "); | |
207 | for (i = 0; i < 16; i++) | |
208 | printf("%02x", seed[i]); | |
209 | printf(" "); | |
210 | } | |
211 | ||
212 | /* generate 16 bytes of pseudo-random value. */ | |
213 | bzero(&ctxt, sizeof(ctxt)); | |
214 | MD5Init(&ctxt); | |
215 | MD5Update(&ctxt, seed, sizeof(seed)); | |
216 | MD5Final(digest, &ctxt); | |
217 | ||
218 | /* | |
6d2010ae | 219 | * RFC 4941 3.2.1. (3) |
9bccf70c A |
220 | * Take the left-most 64-bits of the MD5 digest and set bit 6 (the |
221 | * left-most bit is numbered 0) to zero. | |
222 | */ | |
223 | bcopy(digest, ret, 8); | |
224 | ret[0] &= ~EUI64_UBIT; | |
225 | ||
226 | /* | |
227 | * XXX: we'd like to ensure that the generated value is not zero | |
228 | * for simplicity. If the caclculated digest happens to be zero, | |
229 | * use a random non-zero value as the last resort. | |
230 | */ | |
231 | if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) { | |
6d2010ae A |
232 | nd6log((LOG_INFO, |
233 | "generate_tmp_ifid: computed MD5 value is zero.\n")); | |
9bccf70c A |
234 | |
235 | microtime(&tv); | |
236 | val32 = random() ^ tv.tv_usec; | |
237 | val32 = 1 + (val32 % (0xffffffff - 1)); | |
238 | } | |
239 | ||
240 | /* | |
6d2010ae | 241 | * RFC 4941 3.2.1. (4) |
9bccf70c A |
242 | * Take the rightmost 64-bits of the MD5 digest and save them in |
243 | * stable storage as the history value to be used in the next | |
6d2010ae | 244 | * iteration of the algorithm. |
9bccf70c A |
245 | */ |
246 | bcopy(&digest[8], seed0, 8); | |
247 | ||
248 | if (0) { /* for debugging purposes only */ | |
249 | int i; | |
250 | ||
251 | printf("to: "); | |
252 | for (i = 0; i < 16; i++) | |
253 | printf("%02x", digest[i]); | |
254 | printf("\n"); | |
255 | } | |
1c79356b A |
256 | |
257 | return 0; | |
258 | } | |
259 | ||
260 | /* | |
9bccf70c A |
261 | * Get interface identifier for the specified interface. |
262 | * XXX assumes single sockaddr_dl (AF_LINK address) per an interface | |
6d2010ae A |
263 | * |
264 | * in6 - upper 64bits are preserved | |
1c79356b | 265 | */ |
6d2010ae A |
266 | int |
267 | in6_get_hw_ifid( | |
91447636 A |
268 | struct ifnet *ifp, |
269 | struct in6_addr *in6) /* upper 64bits are preserved */ | |
9bccf70c | 270 | { |
6d2010ae | 271 | struct ifaddr *ifa = NULL; |
1c79356b | 272 | struct sockaddr_dl *sdl; |
9bccf70c A |
273 | u_int8_t *addr; |
274 | size_t addrlen; | |
275 | static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; | |
276 | static u_int8_t allone[8] = | |
277 | { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; | |
6d2010ae | 278 | int err = -1; |
9bccf70c | 279 | |
91447636 A |
280 | /* Why doesn't this code use ifnet_addrs? */ |
281 | ifnet_lock_shared(ifp); | |
6d2010ae A |
282 | ifa = ifp->if_lladdr; |
283 | sdl = (struct sockaddr_dl *)ifa->ifa_addr; | |
284 | if (sdl->sdl_alen == 0) { | |
285 | ifnet_lock_done(ifp); | |
286 | return (-1); | |
9bccf70c | 287 | } |
6d2010ae | 288 | IFA_ADDREF(ifa); /* for this routine */ |
91447636 | 289 | ifnet_lock_done(ifp); |
1c79356b | 290 | |
6d2010ae | 291 | IFA_LOCK(ifa); |
2d21ac55 | 292 | addr = (u_int8_t *) LLADDR(sdl); |
9bccf70c A |
293 | addrlen = sdl->sdl_alen; |
294 | ||
295 | /* get EUI64 */ | |
296 | switch (ifp->if_type) { | |
297 | case IFT_ETHER: | |
298 | case IFT_FDDI: | |
6d2010ae | 299 | case IFT_ISO88025: |
9bccf70c A |
300 | case IFT_ATM: |
301 | case IFT_IEEE1394: | |
91447636 A |
302 | case IFT_L2VLAN: |
303 | case IFT_IEEE8023ADLAG: | |
9bccf70c A |
304 | #if IFT_IEEE80211 |
305 | case IFT_IEEE80211: | |
1c79356b | 306 | #endif |
b7266188 | 307 | case IFT_BRIDGE: |
9bccf70c A |
308 | /* IEEE802/EUI64 cases - what others? */ |
309 | /* IEEE1394 uses 16byte length address starting with EUI64 */ | |
310 | if (addrlen > 8) | |
311 | addrlen = 8; | |
312 | ||
313 | /* look at IEEE802/EUI64 only */ | |
314 | if (addrlen != 8 && addrlen != 6) | |
6d2010ae | 315 | goto done; |
9bccf70c A |
316 | |
317 | /* | |
318 | * check for invalid MAC address - on bsdi, we see it a lot | |
319 | * since wildboar configures all-zero MAC on pccard before | |
320 | * card insertion. | |
321 | */ | |
322 | if (bcmp(addr, allzero, addrlen) == 0) | |
6d2010ae | 323 | goto done; |
9bccf70c | 324 | if (bcmp(addr, allone, addrlen) == 0) |
6d2010ae | 325 | goto done; |
9bccf70c A |
326 | |
327 | /* make EUI64 address */ | |
328 | if (addrlen == 8) | |
329 | bcopy(addr, &in6->s6_addr[8], 8); | |
330 | else if (addrlen == 6) { | |
331 | in6->s6_addr[8] = addr[0]; | |
332 | in6->s6_addr[9] = addr[1]; | |
333 | in6->s6_addr[10] = addr[2]; | |
334 | in6->s6_addr[11] = 0xff; | |
335 | in6->s6_addr[12] = 0xfe; | |
336 | in6->s6_addr[13] = addr[3]; | |
337 | in6->s6_addr[14] = addr[4]; | |
338 | in6->s6_addr[15] = addr[5]; | |
339 | } | |
340 | break; | |
341 | ||
342 | case IFT_ARCNET: | |
343 | if (addrlen != 1) | |
6d2010ae | 344 | goto done; |
9bccf70c | 345 | if (!addr[0]) |
6d2010ae | 346 | goto done; |
9bccf70c A |
347 | |
348 | bzero(&in6->s6_addr[8], 8); | |
349 | in6->s6_addr[15] = addr[0]; | |
350 | ||
351 | /* | |
352 | * due to insufficient bitwidth, we mark it local. | |
353 | */ | |
354 | in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ | |
355 | in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ | |
356 | break; | |
357 | ||
358 | case IFT_GIF: | |
359 | #if IFT_STF | |
360 | case IFT_STF: | |
361 | #endif | |
362 | /* | |
363 | * RFC2893 says: "SHOULD use IPv4 address as ifid source". | |
364 | * however, IPv4 address is not very suitable as unique | |
365 | * identifier source (can be renumbered). | |
366 | * we don't do this. | |
367 | */ | |
6d2010ae A |
368 | goto done; |
369 | ||
370 | case IFT_CELLULAR: | |
371 | goto done; | |
9bccf70c A |
372 | |
373 | default: | |
6d2010ae | 374 | goto done; |
9bccf70c A |
375 | } |
376 | ||
377 | /* sanity check: g bit must not indicate "group" */ | |
378 | if (EUI64_GROUP(in6)) | |
6d2010ae | 379 | goto done; |
9bccf70c A |
380 | |
381 | /* convert EUI64 into IPv6 interface identifier */ | |
382 | EUI64_TO_IFID(in6); | |
383 | ||
384 | /* | |
385 | * sanity check: ifid must not be all zero, avoid conflict with | |
386 | * subnet router anycast | |
387 | */ | |
388 | if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && | |
389 | bcmp(&in6->s6_addr[9], allzero, 7) == 0) { | |
6d2010ae | 390 | goto done; |
9bccf70c A |
391 | } |
392 | ||
6d2010ae A |
393 | err = 0; /* found */ |
394 | ||
395 | done: | |
396 | /* This must not be the last reference to the lladdr */ | |
397 | if (IFA_REMREF_LOCKED(ifa) == NULL) { | |
398 | panic("%s: unexpected (missing) refcnt ifa=%p", __func__, ifa); | |
399 | /* NOTREACHED */ | |
400 | } | |
401 | IFA_UNLOCK(ifa); | |
402 | return (err); | |
9bccf70c A |
403 | } |
404 | ||
405 | /* | |
406 | * Get interface identifier for the specified interface. If it is not | |
407 | * available on ifp0, borrow interface identifier from other information | |
408 | * sources. | |
6d2010ae A |
409 | * |
410 | * altifp - secondary EUI64 source | |
9bccf70c A |
411 | */ |
412 | static int | |
91447636 A |
413 | get_ifid( |
414 | struct ifnet *ifp0, | |
415 | struct ifnet *altifp, /* secondary EUI64 source */ | |
416 | struct in6_addr *in6) | |
9bccf70c A |
417 | { |
418 | struct ifnet *ifp; | |
419 | ||
420 | /* first, try to get it from the interface itself */ | |
6d2010ae | 421 | if (in6_get_hw_ifid(ifp0, in6) == 0) { |
9bccf70c A |
422 | nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", |
423 | if_name(ifp0))); | |
424 | goto success; | |
425 | } | |
426 | ||
427 | /* try secondary EUI64 source. this basically is for ATM PVC */ | |
6d2010ae | 428 | if (altifp && in6_get_hw_ifid(altifp, in6) == 0) { |
9bccf70c A |
429 | nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", |
430 | if_name(ifp0), if_name(altifp))); | |
431 | goto success; | |
432 | } | |
433 | ||
434 | /* next, try to get it from some other hardware interface */ | |
91447636 A |
435 | ifnet_head_lock_shared(); |
436 | TAILQ_FOREACH(ifp, &ifnet_head, if_list) { | |
9bccf70c | 437 | if (ifp == ifp0) |
1c79356b | 438 | continue; |
6d2010ae | 439 | if (in6_get_hw_ifid(ifp, in6) != 0) |
9bccf70c A |
440 | continue; |
441 | ||
442 | /* | |
443 | * to borrow ifid from other interface, ifid needs to be | |
444 | * globally unique | |
445 | */ | |
446 | if (IFID_UNIVERSAL(in6)) { | |
447 | nd6log((LOG_DEBUG, | |
448 | "%s: borrow interface identifier from %s\n", | |
449 | if_name(ifp0), if_name(ifp))); | |
91447636 | 450 | ifnet_head_done(); |
9bccf70c A |
451 | goto success; |
452 | } | |
453 | } | |
91447636 | 454 | ifnet_head_done(); |
9bccf70c A |
455 | |
456 | /* last resort: get from random number source */ | |
457 | if (get_rand_ifid(ifp, in6) == 0) { | |
458 | nd6log((LOG_DEBUG, | |
459 | "%s: interface identifier generated by random number\n", | |
460 | if_name(ifp0))); | |
461 | goto success; | |
462 | } | |
463 | ||
464 | printf("%s: failed to get interface identifier\n", if_name(ifp0)); | |
465 | return -1; | |
466 | ||
467 | success: | |
468 | nd6log((LOG_INFO, "%s: ifid: " | |
469 | "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", | |
470 | if_name(ifp0), | |
471 | in6->s6_addr[8], in6->s6_addr[9], | |
472 | in6->s6_addr[10], in6->s6_addr[11], | |
473 | in6->s6_addr[12], in6->s6_addr[13], | |
474 | in6->s6_addr[14], in6->s6_addr[15])); | |
475 | return 0; | |
476 | } | |
477 | ||
478 | static int | |
91447636 A |
479 | in6_ifattach_linklocal( |
480 | struct ifnet *ifp, | |
481 | struct ifnet *altifp, /* secondary EUI64 source */ | |
482 | struct in6_aliasreq *ifra_passed) | |
9bccf70c A |
483 | { |
484 | struct in6_ifaddr *ia; | |
485 | struct in6_aliasreq ifra; | |
2d21ac55 | 486 | struct nd_prefix pr0, *pr; |
91447636 | 487 | int i, error; |
9bccf70c A |
488 | |
489 | /* | |
490 | * configure link-local address. | |
491 | */ | |
492 | bzero(&ifra, sizeof(ifra)); | |
493 | ||
2d21ac55 | 494 | proto_plumb(PF_INET6, ifp); |
55e303ae | 495 | |
9bccf70c A |
496 | /* |
497 | * in6_update_ifa() does not use ifra_name, but we accurately set it | |
498 | * for safety. | |
499 | */ | |
500 | strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); | |
501 | ||
6d2010ae A |
502 | if ((ifp->if_eflags & IFEF_NOAUTOIPV6LL) != 0 && |
503 | ifra_passed != NULL) /* interface provided both addresses for us */ | |
55e303ae A |
504 | bcopy(&ifra_passed->ifra_addr, &(ifra.ifra_addr), sizeof(struct sockaddr_in6)); |
505 | else { | |
506 | ifra.ifra_addr.sin6_family = AF_INET6; | |
507 | ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); | |
508 | ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80); | |
6d2010ae | 509 | ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); |
55e303ae A |
510 | ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; |
511 | if ((ifp->if_flags & IFF_LOOPBACK) != 0) { | |
512 | ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; | |
513 | ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); | |
514 | } else { | |
515 | if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) { | |
516 | nd6log((LOG_ERR, | |
517 | " %s: no ifid available\n", if_name(ifp))); | |
6d2010ae | 518 | return EADDRNOTAVAIL; |
55e303ae | 519 | } |
1c79356b | 520 | } |
55e303ae | 521 | } |
6d2010ae A |
522 | if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL)) |
523 | return (EADDRNOTAVAIL); | |
524 | ||
9bccf70c A |
525 | ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); |
526 | ifra.ifra_prefixmask.sin6_family = AF_INET6; | |
527 | ifra.ifra_prefixmask.sin6_addr = in6mask64; | |
9bccf70c A |
528 | /* link-local addresses should NEVER expire. */ |
529 | ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; | |
530 | ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; | |
531 | ||
9bccf70c A |
532 | /* |
533 | * Now call in6_update_ifa() to do a bunch of procedures to configure | |
6d2010ae | 534 | * a link-local address. We can set the 3rd argument to NULL, because |
55e303ae A |
535 | * we know there's no other link-local address on the interface |
536 | * and therefore we are adding one (instead of updating one). | |
9bccf70c | 537 | */ |
6d2010ae A |
538 | if ((error = in6_update_ifa(ifp, &ifra, NULL, |
539 | IN6_IFAUPDATE_DADDELAY, M_WAITOK)) != 0) { | |
9bccf70c A |
540 | /* |
541 | * XXX: When the interface does not support IPv6, this call | |
542 | * would fail in the SIOCSIFADDR ioctl. I believe the | |
543 | * notification is rather confusing in this case, so just | |
6d2010ae | 544 | * suppress it. (jinmei@kame.net 20010130) |
9bccf70c A |
545 | */ |
546 | if (error != EAFNOSUPPORT) | |
6d2010ae | 547 | nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to " |
9bccf70c A |
548 | "configure a link-local address on %s " |
549 | "(errno=%d)\n", | |
6d2010ae A |
550 | if_name(ifp), error)); |
551 | return (EADDRNOTAVAIL); | |
9bccf70c A |
552 | } |
553 | ||
9bccf70c A |
554 | ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */ |
555 | #if DIAGNOSTIC | |
556 | if (!ia) { | |
557 | panic("ia == NULL in in6_ifattach_linklocal"); | |
558 | /*NOTREACHED*/ | |
559 | } | |
560 | #endif | |
9bccf70c | 561 | /* |
6d2010ae | 562 | * Make the link-local prefix (fe80::%link/64) as on-link. |
9bccf70c A |
563 | * Since we'd like to manage prefixes separately from addresses, |
564 | * we make an ND6 prefix structure for the link-local prefix, | |
565 | * and add it to the prefix list as a never-expire prefix. | |
566 | * XXX: this change might affect some existing code base... | |
567 | */ | |
568 | bzero(&pr0, sizeof(pr0)); | |
6d2010ae | 569 | lck_mtx_init(&pr0.ndpr_lock, ifa_mtx_grp, ifa_mtx_attr); |
9bccf70c A |
570 | pr0.ndpr_ifp = ifp; |
571 | /* this should be 64 at this moment. */ | |
572 | pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); | |
573 | pr0.ndpr_mask = ifra.ifra_prefixmask.sin6_addr; | |
574 | pr0.ndpr_prefix = ifra.ifra_addr; | |
575 | /* apply the mask for safety. (nd6_prelist_add will apply it again) */ | |
576 | for (i = 0; i < 4; i++) { | |
577 | pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= | |
578 | in6mask64.s6_addr32[i]; | |
579 | } | |
580 | /* | |
581 | * Initialize parameters. The link-local prefix must always be | |
582 | * on-link, and its lifetimes never expire. | |
583 | */ | |
584 | pr0.ndpr_raf_onlink = 1; | |
585 | pr0.ndpr_raf_auto = 1; /* probably meaningless */ | |
586 | pr0.ndpr_vltime = ND6_INFINITE_LIFETIME; | |
587 | pr0.ndpr_pltime = ND6_INFINITE_LIFETIME; | |
6d2010ae | 588 | pr0.ndpr_stateflags |= NDPRF_STATIC; |
9bccf70c A |
589 | /* |
590 | * Since there is no other link-local addresses, nd6_prefix_lookup() | |
591 | * probably returns NULL. However, we cannot always expect the result. | |
592 | * For example, if we first remove the (only) existing link-local | |
593 | * address, and then reconfigure another one, the prefix is still | |
594 | * valid with referring to the old link-local address. | |
595 | */ | |
2d21ac55 | 596 | if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { |
6d2010ae A |
597 | if ((error = nd6_prelist_add(&pr0, NULL, &pr, TRUE)) != 0) { |
598 | IFA_REMREF(&ia->ia_ifa); | |
599 | lck_mtx_destroy(&pr0.ndpr_lock, ifa_mtx_grp); | |
9bccf70c | 600 | return(error); |
b0d623f7 | 601 | } |
9bccf70c A |
602 | } |
603 | ||
b0d623f7 A |
604 | if (ia != NULL) { |
605 | in6_post_msg(ifp, KEV_INET6_NEW_LL_ADDR, ia); | |
6d2010ae | 606 | IFA_REMREF(&ia->ia_ifa); |
b0d623f7 | 607 | } |
2d21ac55 A |
608 | |
609 | /* Drop use count held above during lookup/add */ | |
610 | if (pr != NULL) | |
6d2010ae A |
611 | NDPR_REMREF(pr); |
612 | ||
613 | lck_mtx_destroy(&pr0.ndpr_lock, ifa_mtx_grp); | |
2d21ac55 | 614 | |
9bccf70c A |
615 | return 0; |
616 | } | |
617 | ||
618 | static int | |
91447636 A |
619 | in6_ifattach_loopback( |
620 | struct ifnet *ifp) /* must be IFT_LOOP */ | |
9bccf70c A |
621 | { |
622 | struct in6_aliasreq ifra; | |
623 | int error; | |
624 | ||
625 | bzero(&ifra, sizeof(ifra)); | |
626 | ||
627 | /* | |
628 | * in6_update_ifa() does not use ifra_name, but we accurately set it | |
629 | * for safety. | |
630 | */ | |
631 | strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); | |
632 | ||
633 | ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); | |
634 | ifra.ifra_prefixmask.sin6_family = AF_INET6; | |
635 | ifra.ifra_prefixmask.sin6_addr = in6mask128; | |
636 | ||
637 | /* | |
638 | * Always initialize ia_dstaddr (= broadcast address) to loopback | |
639 | * address. Follows IPv4 practice - see in_ifinit(). | |
640 | */ | |
641 | ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6); | |
642 | ifra.ifra_dstaddr.sin6_family = AF_INET6; | |
643 | ifra.ifra_dstaddr.sin6_addr = in6addr_loopback; | |
644 | ||
645 | ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); | |
646 | ifra.ifra_addr.sin6_family = AF_INET6; | |
647 | ifra.ifra_addr.sin6_addr = in6addr_loopback; | |
648 | ||
649 | /* the loopback address should NEVER expire. */ | |
650 | ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; | |
651 | ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; | |
652 | ||
55e303ae | 653 | /* we don't need to perform DAD on loopback interfaces. */ |
9bccf70c A |
654 | ifra.ifra_flags |= IN6_IFF_NODAD; |
655 | ||
656 | /* skip registration to the prefix list. XXX should be temporary. */ | |
657 | ifra.ifra_flags |= IN6_IFF_NOPFX; | |
658 | ||
659 | /* | |
55e303ae A |
660 | * We are sure that this is a newly assigned address, so we can set |
661 | * NULL to the 3rd arg. | |
9bccf70c | 662 | */ |
6d2010ae A |
663 | if ((error = in6_update_ifa(ifp, &ifra, NULL, 0, M_WAITOK)) != 0) { |
664 | nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure " | |
9bccf70c | 665 | "the loopback address on %s (errno=%d)\n", |
6d2010ae A |
666 | if_name(ifp), error)); |
667 | return (EADDRNOTAVAIL); | |
9bccf70c A |
668 | } |
669 | ||
670 | return 0; | |
1c79356b A |
671 | } |
672 | ||
673 | /* | |
9bccf70c A |
674 | * compute NI group address, based on the current hostname setting. |
675 | * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later). | |
676 | * | |
677 | * when ifp == NULL, the caller is responsible for filling scopeid. | |
1c79356b | 678 | */ |
9bccf70c | 679 | int |
91447636 A |
680 | in6_nigroup( |
681 | struct ifnet *ifp, | |
682 | const char *name, | |
683 | int namelen, | |
684 | struct in6_addr *in6) | |
1c79356b | 685 | { |
9bccf70c A |
686 | const char *p; |
687 | u_char *q; | |
688 | MD5_CTX ctxt; | |
689 | u_int8_t digest[16]; | |
690 | char l; | |
691 | char n[64]; /* a single label must not exceed 63 chars */ | |
692 | ||
693 | if (!namelen || !name) | |
694 | return -1; | |
695 | ||
696 | p = name; | |
697 | while (p && *p && *p != '.' && p - name < namelen) | |
698 | p++; | |
699 | if (p - name > sizeof(n) - 1) | |
55e303ae | 700 | return -1; /* label too long */ |
9bccf70c A |
701 | l = p - name; |
702 | strncpy(n, name, l); | |
703 | n[(int)l] = '\0'; | |
2d21ac55 | 704 | for (q = (u_char *) n; *q; q++) { |
9bccf70c A |
705 | if ('A' <= *q && *q <= 'Z') |
706 | *q = *q - 'A' + 'a'; | |
707 | } | |
1c79356b | 708 | |
9bccf70c A |
709 | /* generate 8 bytes of pseudo-random value. */ |
710 | bzero(&ctxt, sizeof(ctxt)); | |
711 | MD5Init(&ctxt); | |
712 | MD5Update(&ctxt, &l, sizeof(l)); | |
713 | MD5Update(&ctxt, n, l); | |
714 | MD5Final(digest, &ctxt); | |
715 | ||
716 | bzero(in6, sizeof(*in6)); | |
6d2010ae | 717 | in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL; |
9bccf70c A |
718 | in6->s6_addr8[11] = 2; |
719 | bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3])); | |
6d2010ae A |
720 | if (in6_setscope(in6, ifp, NULL)) |
721 | return (-1); /* XXX: should not fail */ | |
9bccf70c A |
722 | |
723 | return 0; | |
724 | } | |
725 | ||
6d2010ae A |
726 | int |
727 | in6_domifattach(struct ifnet *ifp) | |
9bccf70c | 728 | { |
6d2010ae | 729 | int error = 0; |
1c79356b | 730 | |
6d2010ae A |
731 | if ((error = proto_plumb(PF_INET6, ifp))) { |
732 | if (error != EEXIST) | |
733 | log(LOG_ERR, "%s: proto_plumb returned %d if=%s%d\n", | |
734 | __func__, error, ifp->if_name, ifp->if_unit); | |
735 | } else { | |
736 | nd6_ifattach(ifp); | |
737 | scope6_ifattach(ifp); | |
1c79356b | 738 | } |
9bccf70c | 739 | |
6d2010ae | 740 | return (error); |
9bccf70c A |
741 | } |
742 | ||
6d2010ae | 743 | |
9bccf70c A |
744 | /* |
745 | * XXX multiple loopback interface needs more care. for instance, | |
746 | * nodelocal address needs to be configured onto only one of them. | |
747 | * XXX multiple link-local address case | |
748 | */ | |
b0d623f7 | 749 | int |
91447636 A |
750 | in6_ifattach( |
751 | struct ifnet *ifp, | |
752 | struct ifnet *altifp, /* secondary EUI64 source */ | |
753 | struct in6_aliasreq *ifra) | |
9bccf70c A |
754 | { |
755 | static size_t if_indexlim = 8; | |
756 | struct in6_ifaddr *ia; | |
6d2010ae | 757 | struct in6_addr in6; |
b0d623f7 | 758 | int error; |
9bccf70c | 759 | |
6d2010ae | 760 | lck_rw_lock_exclusive(&in6_ifs_rwlock); |
1c79356b A |
761 | /* |
762 | * We have some arrays that should be indexed by if_index. | |
763 | * since if_index will grow dynamically, they should grow too. | |
764 | * struct in6_ifstat **in6_ifstat | |
765 | * struct icmp6_ifstat **icmp6_ifstat | |
766 | */ | |
9bccf70c A |
767 | if (in6_ifstat == NULL || icmp6_ifstat == NULL || |
768 | if_index >= if_indexlim) { | |
1c79356b A |
769 | while (if_index >= if_indexlim) |
770 | if_indexlim <<= 1; | |
b0d623f7 A |
771 | } |
772 | ||
b0d623f7 A |
773 | /* grow in6_ifstat */ |
774 | if (in6_ifstatmax < if_indexlim) { | |
775 | size_t n; | |
776 | caddr_t q; | |
777 | ||
1c79356b A |
778 | n = if_indexlim * sizeof(struct in6_ifstat *); |
779 | q = (caddr_t)_MALLOC(n, M_IFADDR, M_WAITOK); | |
b0d623f7 | 780 | if (q == NULL) { |
6d2010ae | 781 | lck_rw_done(&in6_ifs_rwlock); |
b0d623f7 A |
782 | return ENOBUFS; |
783 | } | |
1c79356b A |
784 | bzero(q, n); |
785 | if (in6_ifstat) { | |
786 | bcopy((caddr_t)in6_ifstat, q, | |
b0d623f7 | 787 | in6_ifstatmax * sizeof(struct in6_ifstat *)); |
9bccf70c | 788 | FREE((caddr_t)in6_ifstat, M_IFADDR); |
1c79356b A |
789 | } |
790 | in6_ifstat = (struct in6_ifstat **)q; | |
791 | in6_ifstatmax = if_indexlim; | |
b0d623f7 A |
792 | } |
793 | ||
794 | if (in6_ifstat[ifp->if_index] == NULL) { | |
795 | in6_ifstat[ifp->if_index] = (struct in6_ifstat *) | |
796 | _MALLOC(sizeof(struct in6_ifstat), M_IFADDR, M_WAITOK); | |
797 | if (in6_ifstat[ifp->if_index] == NULL) { | |
6d2010ae | 798 | lck_rw_done(&in6_ifs_rwlock); |
b0d623f7 A |
799 | return ENOBUFS; |
800 | } | |
801 | bzero(in6_ifstat[ifp->if_index], sizeof(struct in6_ifstat)); | |
802 | } | |
6d2010ae | 803 | lck_rw_done(&in6_ifs_rwlock); |
1c79356b | 804 | |
6d2010ae | 805 | lck_rw_lock_exclusive(&icmp6_ifs_rwlock); |
b0d623f7 A |
806 | if (icmp6_ifstatmax < if_indexlim) { |
807 | size_t n; | |
808 | caddr_t q; | |
809 | ||
1c79356b A |
810 | n = if_indexlim * sizeof(struct icmp6_ifstat *); |
811 | q = (caddr_t)_MALLOC(n, M_IFADDR, M_WAITOK); | |
b0d623f7 | 812 | if (q == NULL) { |
6d2010ae | 813 | lck_rw_done(&icmp6_ifs_rwlock); |
b0d623f7 A |
814 | return ENOBUFS; |
815 | } | |
1c79356b A |
816 | bzero(q, n); |
817 | if (icmp6_ifstat) { | |
818 | bcopy((caddr_t)icmp6_ifstat, q, | |
b0d623f7 | 819 | icmp6_ifstatmax * sizeof(struct icmp6_ifstat *)); |
9bccf70c | 820 | FREE((caddr_t)icmp6_ifstat, M_IFADDR); |
1c79356b A |
821 | } |
822 | icmp6_ifstat = (struct icmp6_ifstat **)q; | |
823 | icmp6_ifstatmax = if_indexlim; | |
824 | } | |
825 | ||
b0d623f7 A |
826 | if (icmp6_ifstat[ifp->if_index] == NULL) { |
827 | icmp6_ifstat[ifp->if_index] = (struct icmp6_ifstat *) | |
828 | _MALLOC(sizeof(struct icmp6_ifstat), M_IFADDR, M_WAITOK); | |
829 | if (icmp6_ifstat[ifp->if_index] == NULL) { | |
6d2010ae | 830 | lck_rw_done(&icmp6_ifs_rwlock); |
b0d623f7 A |
831 | return ENOBUFS; |
832 | } | |
833 | bzero(icmp6_ifstat[ifp->if_index], sizeof(struct icmp6_ifstat)); | |
834 | } | |
6d2010ae | 835 | lck_rw_done(&icmp6_ifs_rwlock); |
b0d623f7 | 836 | |
55e303ae | 837 | /* initialize NDP variables */ |
b0d623f7 A |
838 | if ((error = nd6_ifattach(ifp)) != 0) |
839 | return error; | |
55e303ae | 840 | |
9bccf70c | 841 | /* initialize scope identifiers */ |
b0d623f7 A |
842 | if ((error = scope6_ifattach(ifp)) != 0) |
843 | return error; | |
1c79356b A |
844 | |
845 | /* | |
9bccf70c | 846 | * quirks based on interface type |
1c79356b | 847 | */ |
9bccf70c A |
848 | switch (ifp->if_type) { |
849 | #if IFT_STF | |
850 | case IFT_STF: | |
851 | /* | |
55e303ae A |
852 | * 6to4 interface is a very special kind of beast. |
853 | * no multicast, no linklocal. RFC2529 specifies how to make | |
854 | * linklocals for 6to4 interface, but there's no use and | |
855 | * it is rather harmful to have one. | |
9bccf70c A |
856 | */ |
857 | goto statinit; | |
0b4e3aa0 | 858 | #endif |
9bccf70c | 859 | default: |
1c79356b | 860 | break; |
1c79356b A |
861 | } |
862 | ||
1c79356b | 863 | /* |
9bccf70c | 864 | * usually, we require multicast capability to the interface |
1c79356b | 865 | */ |
9bccf70c | 866 | if ((ifp->if_flags & IFF_MULTICAST) == 0) { |
6d2010ae A |
867 | nd6log((LOG_INFO, "in6_ifattach: ", |
868 | "%s is not multicast capable, IPv6 not enabled\n", | |
869 | if_name(ifp))); | |
b0d623f7 | 870 | return EINVAL; |
1c79356b A |
871 | } |
872 | ||
1c79356b | 873 | /* |
9bccf70c A |
874 | * assign loopback address for loopback interface. |
875 | * XXX multiple loopback interface case. | |
1c79356b | 876 | */ |
9bccf70c | 877 | if ((ifp->if_flags & IFF_LOOPBACK) != 0) { |
6d2010ae A |
878 | struct in6_ifaddr *ia6 = NULL; |
879 | if (!OSCompareAndSwap(0, 1, (UInt32 *)&loopattach6_done)) { | |
880 | in6 = in6addr_loopback; | |
881 | if ((ia6 = in6ifa_ifpwithaddr(ifp, &in6)) == NULL) { | |
882 | if (in6_ifattach_loopback(ifp) != 0) { | |
883 | OSCompareAndSwap(1, 0, (UInt32 *)&loopattach6_done); | |
884 | return EINVAL; | |
885 | } | |
886 | } | |
887 | else { | |
888 | IFA_REMREF(&ia6->ia_ifa); | |
889 | } | |
890 | } | |
1c79356b A |
891 | } |
892 | ||
893 | /* | |
6d2010ae | 894 | * assign a link-local address, if there's none. |
1c79356b | 895 | */ |
9bccf70c A |
896 | if (ip6_auto_linklocal) { |
897 | ia = in6ifa_ifpforlinklocal(ifp, 0); | |
898 | if (ia == NULL) { | |
55e303ae | 899 | if (in6_ifattach_linklocal(ifp, altifp, ifra) == 0) { |
9bccf70c A |
900 | /* linklocal address assigned */ |
901 | } else { | |
6d2010ae | 902 | nd6log((LOG_INFO, "in6_ifattach: %s failed to " |
b0d623f7 | 903 | "attach a linklocal address.\n", |
6d2010ae | 904 | if_name(ifp))); |
9bccf70c A |
905 | /* failed to assign linklocal address. bark? */ |
906 | } | |
b0d623f7 | 907 | } else { |
6d2010ae | 908 | IFA_REMREF(&ia->ia_ifa); |
1c79356b A |
909 | } |
910 | } | |
911 | ||
9bccf70c A |
912 | #if IFT_STF /* XXX */ |
913 | statinit: | |
914 | #endif | |
915 | ||
1c79356b A |
916 | /* update dynamically. */ |
917 | if (in6_maxmtu < ifp->if_mtu) | |
918 | in6_maxmtu = ifp->if_mtu; | |
919 | ||
b0d623f7 | 920 | return 0; |
1c79356b A |
921 | } |
922 | ||
923 | /* | |
924 | * NOTE: in6_ifdetach() does not support loopback if at this moment. | |
925 | */ | |
926 | void | |
6d2010ae | 927 | in6_ifdetach(struct ifnet *ifp) |
1c79356b | 928 | { |
6d2010ae A |
929 | struct in6_ifaddr *ia, *oia; |
930 | struct ifaddr *ifa; | |
1c79356b | 931 | struct rtentry *rt; |
1c79356b | 932 | struct sockaddr_in6 sin6; |
6d2010ae A |
933 | struct in6_multi_mship *imm; |
934 | int unlinked; | |
1c79356b | 935 | |
6d2010ae | 936 | lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED); |
1c79356b A |
937 | |
938 | /* remove neighbor management table */ | |
939 | nd6_purge(ifp); | |
940 | ||
9bccf70c | 941 | /* nuke any of IPv6 addresses we have */ |
6d2010ae A |
942 | lck_rw_lock_exclusive(&in6_ifaddr_rwlock); |
943 | ia = in6_ifaddrs; | |
944 | while (ia != NULL) { | |
945 | if (ia->ia_ifa.ifa_ifp != ifp) { | |
946 | ia = ia->ia_next; | |
9bccf70c | 947 | continue; |
6d2010ae A |
948 | } |
949 | IFA_ADDREF(&ia->ia_ifa); /* for us */ | |
950 | lck_rw_done(&in6_ifaddr_rwlock); | |
951 | in6_purgeaddr(&ia->ia_ifa); | |
952 | IFA_REMREF(&ia->ia_ifa); /* for us */ | |
953 | lck_rw_lock_exclusive(&in6_ifaddr_rwlock); | |
954 | /* | |
955 | * Purging the address caused in6_ifaddr_rwlock | |
956 | * to be dropped and reacquired; | |
957 | * therefore search again from the beginning | |
958 | * of in6_ifaddrs list. | |
959 | */ | |
960 | ia = in6_ifaddrs; | |
9bccf70c | 961 | } |
6d2010ae | 962 | lck_rw_done(&in6_ifaddr_rwlock); |
91447636 A |
963 | |
964 | ifnet_lock_exclusive(ifp); | |
9bccf70c A |
965 | |
966 | /* undo everything done by in6_ifattach(), just in case */ | |
6d2010ae A |
967 | ifa = TAILQ_FIRST(&ifp->if_addrlist); |
968 | while (ifa != NULL) { | |
969 | IFA_LOCK(ifa); | |
970 | if (ifa->ifa_addr->sa_family != AF_INET6 || | |
971 | !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)-> | |
972 | sin6_addr)) { | |
973 | IFA_UNLOCK(ifa); | |
974 | ifa = TAILQ_NEXT(ifa, ifa_list); | |
1c79356b A |
975 | continue; |
976 | } | |
977 | ||
978 | ia = (struct in6_ifaddr *)ifa; | |
979 | ||
6d2010ae A |
980 | /* hold a reference for this routine */ |
981 | IFA_ADDREF_LOCKED(ifa); | |
982 | /* remove from the linked list */ | |
983 | if_detach_ifa(ifp, ifa); | |
984 | IFA_UNLOCK(ifa); | |
985 | ||
986 | /* | |
987 | * Leaving the multicast group(s) may involve freeing the | |
988 | * link address multicast structure(s) for the interface, | |
989 | * which is protected by ifnet lock. To avoid violating | |
990 | * lock ordering, we must drop ifnet lock before doing so. | |
991 | * The ifa won't go away since we held a refcnt above. | |
992 | */ | |
993 | ifnet_lock_done(ifp); | |
994 | ||
995 | /* | |
996 | * We have to do this work manually here instead of calling | |
997 | * in6_purgeaddr() since in6_purgeaddr() uses the RTM_HOST flag. | |
998 | */ | |
999 | ||
1000 | /* | |
1001 | * leave from multicast groups we have joined for the interface | |
1002 | */ | |
1003 | IFA_LOCK(ifa); | |
1004 | while ((imm = ia->ia6_memberships.lh_first) != NULL) { | |
1005 | LIST_REMOVE(imm, i6mm_chain); | |
1006 | IFA_UNLOCK(ifa); | |
1007 | in6_leavegroup(imm); | |
1008 | IFA_LOCK(ifa); | |
1c79356b A |
1009 | } |
1010 | ||
6d2010ae A |
1011 | /* remove from the routing table */ |
1012 | if (ia->ia_flags & IFA_ROUTE) { | |
1013 | IFA_UNLOCK(ifa); | |
1014 | rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0); | |
1015 | if (rt != NULL) { | |
1016 | (void) rtrequest(RTM_DELETE, | |
1017 | (struct sockaddr *)&ia->ia_addr, | |
1018 | (struct sockaddr *)&ia->ia_addr, | |
1019 | (struct sockaddr *)&ia->ia_prefixmask, | |
1020 | rt->rt_flags, (struct rtentry **)0); | |
1021 | rtfree(rt); | |
1022 | } | |
1023 | } else { | |
1024 | IFA_UNLOCK(ifa); | |
1025 | } | |
1c79356b A |
1026 | |
1027 | /* also remove from the IPv6 address chain(itojun&jinmei) */ | |
6d2010ae | 1028 | unlinked = 1; |
1c79356b | 1029 | oia = ia; |
6d2010ae A |
1030 | lck_rw_lock_exclusive(&in6_ifaddr_rwlock); |
1031 | if (oia == (ia = in6_ifaddrs)) { | |
91447636 | 1032 | in6_ifaddrs = ia->ia_next; |
6d2010ae | 1033 | } else { |
1c79356b A |
1034 | while (ia->ia_next && (ia->ia_next != oia)) |
1035 | ia = ia->ia_next; | |
6d2010ae | 1036 | if (ia->ia_next) { |
1c79356b | 1037 | ia->ia_next = oia->ia_next; |
6d2010ae A |
1038 | } else { |
1039 | nd6log((LOG_ERR, | |
9bccf70c A |
1040 | "%s: didn't unlink in6ifaddr from " |
1041 | "list\n", if_name(ifp))); | |
6d2010ae | 1042 | unlinked = 0; |
9bccf70c | 1043 | } |
1c79356b | 1044 | } |
6d2010ae A |
1045 | lck_rw_done(&in6_ifaddr_rwlock); |
1046 | ||
1047 | ifa = &oia->ia_ifa; | |
1048 | /* | |
1049 | * release another refcnt for the link from in6_ifaddrs. | |
1050 | * Do this only if it's not already unlinked in the event | |
1051 | * that we lost the race, since in6_ifaddr_rwlock was momentarily | |
1052 | * dropped above. | |
1053 | */ | |
1054 | if (unlinked) | |
1055 | IFA_REMREF(ifa); | |
1056 | /* release reference held for this routine */ | |
1057 | IFA_REMREF(ifa); | |
1c79356b | 1058 | |
6d2010ae A |
1059 | /* |
1060 | * This is suboptimal, but since we dropped ifnet lock above | |
1061 | * the list might have changed. Repeat the search from the | |
1062 | * beginning until we find the first eligible IPv6 address. | |
1063 | */ | |
1064 | ifnet_lock_exclusive(ifp); | |
1065 | ifa = TAILQ_FIRST(&ifp->if_addrlist); | |
1c79356b | 1066 | } |
91447636 | 1067 | ifnet_lock_done(ifp); |
1c79356b | 1068 | |
9bccf70c A |
1069 | /* |
1070 | * remove neighbor management table. we call it twice just to make | |
1071 | * sure we nuke everything. maybe we need just one call. | |
1072 | * XXX: since the first call did not release addresses, some prefixes | |
1073 | * might remain. We should call nd6_purge() again to release the | |
1074 | * prefixes after removing all addresses above. | |
1075 | * (Or can we just delay calling nd6_purge until at this point?) | |
1076 | */ | |
1c79356b A |
1077 | nd6_purge(ifp); |
1078 | ||
1079 | /* remove route to link-local allnodes multicast (ff02::1) */ | |
1080 | bzero(&sin6, sizeof(sin6)); | |
1081 | sin6.sin6_len = sizeof(struct sockaddr_in6); | |
1082 | sin6.sin6_family = AF_INET6; | |
1083 | sin6.sin6_addr = in6addr_linklocal_allnodes; | |
1084 | sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index); | |
b0d623f7 | 1085 | rt = rtalloc1((struct sockaddr *)&sin6, 0, 0); |
2d21ac55 | 1086 | if (rt != NULL) { |
b0d623f7 | 1087 | RT_LOCK(rt); |
2d21ac55 | 1088 | if (rt->rt_ifp == ifp) { |
b0d623f7 A |
1089 | /* |
1090 | * Prevent another thread from modifying rt_key, | |
1091 | * rt_gateway via rt_setgate() after the rt_lock | |
1092 | * is dropped by marking the route as defunct. | |
1093 | */ | |
1094 | rt->rt_flags |= RTF_CONDEMNED; | |
1095 | RT_UNLOCK(rt); | |
1096 | (void) rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, | |
1097 | rt_mask(rt), rt->rt_flags, 0); | |
1098 | } else { | |
1099 | RT_UNLOCK(rt); | |
2d21ac55 | 1100 | } |
b0d623f7 | 1101 | rtfree(rt); |
1c79356b A |
1102 | } |
1103 | } | |
9bccf70c A |
1104 | |
1105 | void | |
91447636 A |
1106 | in6_get_tmpifid( |
1107 | struct ifnet *ifp, | |
1108 | u_int8_t *retbuf, | |
1109 | const u_int8_t *baseid, | |
1110 | int generate) | |
9bccf70c A |
1111 | { |
1112 | u_int8_t nullbuf[8]; | |
b0d623f7 | 1113 | struct nd_ifinfo *ndi; |
9bccf70c | 1114 | |
b0d623f7 A |
1115 | lck_rw_lock_shared(nd_if_rwlock); |
1116 | ndi = &nd_ifinfo[ifp->if_index]; | |
9bccf70c A |
1117 | bzero(nullbuf, sizeof(nullbuf)); |
1118 | if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) { | |
1119 | /* we've never created a random ID. Create a new one. */ | |
1120 | generate = 1; | |
1121 | } | |
1122 | ||
1123 | if (generate) { | |
1124 | bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1)); | |
1125 | ||
1126 | /* generate_tmp_ifid will update seedn and buf */ | |
1127 | (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1, | |
1128 | ndi->randomid); | |
1129 | } | |
1130 | bcopy(ndi->randomid, retbuf, 8); | |
b0d623f7 | 1131 | lck_rw_done(nd_if_rwlock); |
9bccf70c A |
1132 | } |
1133 | ||
9bccf70c | 1134 | void |
91447636 | 1135 | in6_tmpaddrtimer( |
2d21ac55 | 1136 | __unused void *ignored_arg) |
9bccf70c A |
1137 | { |
1138 | int i; | |
1139 | struct nd_ifinfo *ndi; | |
1140 | u_int8_t nullbuf[8]; | |
9bccf70c | 1141 | |
91447636 | 1142 | timeout(in6_tmpaddrtimer, (caddr_t)0, |
9bccf70c A |
1143 | (ip6_temp_preferred_lifetime - ip6_desync_factor - |
1144 | ip6_temp_regen_advance) * hz); | |
1145 | ||
ab86ba33 | 1146 | if (ip6_use_tempaddr) { |
b0d623f7 | 1147 | lck_rw_lock_shared(nd_if_rwlock); |
ab86ba33 A |
1148 | bzero(nullbuf, sizeof(nullbuf)); |
1149 | for (i = 1; i < nd_ifinfo_indexlim + 1; i++) { | |
1150 | ndi = &nd_ifinfo[i]; | |
6d2010ae | 1151 | if ((ndi->flags | ND6_IFF_PERFORMNUD) != ND6_IFF_PERFORMNUD) |
ab86ba33 A |
1152 | continue; |
1153 | if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) { | |
1154 | /* | |
1155 | * We've been generating a random ID on this interface. | |
1156 | * Create a new one. | |
1157 | */ | |
1158 | (void)generate_tmp_ifid(ndi->randomseed0, | |
1159 | ndi->randomseed1, | |
1160 | ndi->randomid); | |
1161 | } | |
9bccf70c | 1162 | } |
b0d623f7 | 1163 | lck_rw_done(nd_if_rwlock); |
9bccf70c | 1164 | } |
9bccf70c | 1165 | } |