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