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