in6_ifattach.c revision 1.84 1 /* $NetBSD: in6_ifattach.c,v 1.84 2009/08/13 09:04:03 cegger Exp $ */
2 /* $KAME: in6_ifattach.c,v 1.124 2001/07/18 08:32:51 jinmei Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: in6_ifattach.c,v 1.84 2009/08/13 09:04:03 cegger Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kmem.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/kernel.h>
43 #include <sys/syslog.h>
44 #include <sys/md5.h>
45 #include <sys/socketvar.h>
46 #include <sys/workqueue.h>
47
48 #include <net/if.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
52
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55
56 #include <netinet/ip6.h>
57 #include <netinet6/in6_ifattach.h>
58 #include <netinet6/ip6_var.h>
59 #include <netinet6/nd6.h>
60 #include <netinet6/ip6_mroute.h>
61 #include <netinet6/scope6_var.h>
62
63 #include <net/net_osdep.h>
64
65 /* Record of an interface to add a link-local and possibly a loopback
66 * IPv6 address to, processed on a workqueue(9) by in6_ifaddrs_worker.
67 */
68 struct in6_ifaddr_work {
69 struct work iw_work;
70 struct ifindexgen {
71 int ig_idx; /* Interface index */
72 int ig_gen; /* Interface index generation */
73 } iw_idxgen, /* Identify of the interface
74 * to receive a link-local and
75 * possibly a loopback address.
76 */
77 iw_alt_idxgen; /* Optional identity of a second
78 * interface. If iw_alt_present
79 * is true, this field
80 * identifies a second interface
81 * whose EUI64 we use to derive
82 * the link-local address for
83 * the interface indicated by
84 * iw_idxgen.
85 */
86 bool iw_alt_present; /* iff true, iw_alt_idxgen is valid. */
87 };
88
89 unsigned long in6_maxmtu = 0;
90
91 int ip6_auto_linklocal = 1; /* enable by default */
92
93 callout_t in6_tmpaddrtimer_ch;
94
95 static struct workqueue *in6_ifaddrs_wq = NULL;
96
97 static void in6_ifaddrs_schedule(struct ifnet *, struct ifnet *);
98 static void in6_ifaddrs_init(struct ifnet *, struct ifnet *);
99 static void in6_ifaddrs_worker(struct work *, void *);
100
101 #if 0
102 static int get_hostid_ifid(struct ifnet *, struct in6_addr *);
103 #endif
104 static int get_rand_ifid(struct ifnet *, struct in6_addr *);
105 static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
106 static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
107 static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
108 static int in6_ifattach_loopback(struct ifnet *);
109
110 #define EUI64_GBIT 0x01
111 #define EUI64_UBIT 0x02
112 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (/*CONSTCOND*/ 0)
113 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
114 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
115 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
116 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
117
118 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
119 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
120
121 #define GEN_TEMPID_RETRY_MAX 5
122
123 #if 0
124 /*
125 * Generate a last-resort interface identifier from hostid.
126 * works only for certain architectures (like sparc).
127 * also, using hostid itself may constitute a privacy threat, much worse
128 * than MAC addresses (hostids are used for software licensing).
129 * maybe we should use MD5(hostid) instead.
130 *
131 * in6 - upper 64bits are preserved
132 */
133 static int
134 get_hostid_ifid(struct ifnet *ifp, struct in6_addr *in6)
135 {
136 int off, len;
137 static const uint8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
138 static const uint8_t allone[8] =
139 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
140
141 if (!hostid)
142 return -1;
143
144 /* get up to 8 bytes from the hostid field - should we get */
145 len = (sizeof(hostid) > 8) ? 8 : sizeof(hostid);
146 off = sizeof(*in6) - len;
147 memcpy(&in6->s6_addr[off], &hostid, len);
148
149 /* make sure we do not return anything bogus */
150 if (memcmp(&in6->s6_addr[8], allzero, sizeof(allzero)))
151 return -1;
152 if (memcmp(&in6->s6_addr[8], allone, sizeof(allone)))
153 return -1;
154
155 /* make sure to set "u" bit to local, and "g" bit to individual. */
156 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
157 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
158
159 /* convert EUI64 into IPv6 interface identifier */
160 EUI64_TO_IFID(in6);
161
162 return 0;
163 }
164 #endif
165
166 /*
167 * Generate a last-resort interface identifier, when the machine has no
168 * IEEE802/EUI64 address sources.
169 * The goal here is to get an interface identifier that is
170 * (1) random enough and (2) does not change across reboot.
171 * We currently use MD5(hostname) for it.
172 */
173 static int
174 get_rand_ifid(struct ifnet *ifp,
175 struct in6_addr *in6) /* upper 64bits are preserved */
176 {
177 MD5_CTX ctxt;
178 u_int8_t digest[16];
179
180 #if 0
181 /* we need at least several letters as seed for ifid */
182 if (hostnamelen < 3)
183 return -1;
184 #endif
185
186 /* generate 8 bytes of pseudo-random value. */
187 memset(&ctxt, 0, sizeof(ctxt));
188 MD5Init(&ctxt);
189 MD5Update(&ctxt, (u_char *)hostname, hostnamelen);
190 MD5Final(digest, &ctxt);
191
192 /* assumes sizeof(digest) > sizeof(ifid) */
193 memcpy(&in6->s6_addr[8], digest, 8);
194
195 /* make sure to set "u" bit to local, and "g" bit to individual. */
196 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
197 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
198
199 /* convert EUI64 into IPv6 interface identifier */
200 EUI64_TO_IFID(in6);
201
202 return 0;
203 }
204
205 static int
206 generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
207 {
208 MD5_CTX ctxt;
209 u_int8_t seed[16], digest[16], nullbuf[8];
210 u_int32_t val32;
211 /*
212 * interface ID for subnet anycast addresses.
213 * XXX: we assume the unicast address range that requires IDs
214 * in EUI-64 format.
215 */
216 static const uint8_t anycast_id[8] = { 0xfd, 0xff, 0xff, 0xff,
217 0xff, 0xff, 0xff, 0x80 };
218 static const uint8_t isatap_id[4] = { 0x00, 0x00, 0x5e, 0xfe };
219 int badid, retry = 0;
220
221 /* If there's no hisotry, start with a random seed. */
222 memset(nullbuf, 0, sizeof(nullbuf));
223 if (memcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
224 int i;
225
226 for (i = 0; i < 2; i++) {
227 val32 = arc4random();
228 memcpy(seed + sizeof(val32) * i, &val32, sizeof(val32));
229 }
230 } else
231 memcpy(seed, seed0, 8);
232
233 /* copy the right-most 64-bits of the given address */
234 /* XXX assumption on the size of IFID */
235 memcpy(&seed[8], seed1, 8);
236
237 again:
238 /* for debugging purposes only */
239 #if 0
240 {
241 int i;
242
243 printf("generate_tmp_ifid: new randomized ID from: ");
244 for (i = 0; i < 16; i++)
245 printf("%02x", seed[i]);
246 printf(" ");
247 }
248 #endif
249
250 /* generate 16 bytes of pseudo-random value. */
251 memset(&ctxt, 0, sizeof(ctxt));
252 MD5Init(&ctxt);
253 MD5Update(&ctxt, seed, sizeof(seed));
254 MD5Final(digest, &ctxt);
255
256 /*
257 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (3)
258 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
259 * left-most bit is numbered 0) to zero.
260 */
261 memcpy(ret, digest, 8);
262 ret[0] &= ~EUI64_UBIT;
263
264 /*
265 * Reject inappropriate identifiers according to
266 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (4)
267 * At this moment, we reject following cases:
268 * - all 0 identifier
269 * - identifiers that conflict with reserved subnet anycast addresses,
270 * which are defined in RFC 2526.
271 * - identifiers that conflict with ISATAP addresses
272 * - identifiers used in our own addresses
273 */
274 badid = 0;
275 if (memcmp(nullbuf, ret, sizeof(nullbuf)) == 0)
276 badid = 1;
277 else if (memcmp(anycast_id, ret, 7) == 0 &&
278 (anycast_id[7] & ret[7]) == anycast_id[7]) {
279 badid = 1;
280 } else if (memcmp(isatap_id, ret, sizeof(isatap_id)) == 0)
281 badid = 1;
282 else {
283 struct in6_ifaddr *ia;
284
285 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
286 if (!memcmp(&ia->ia_addr.sin6_addr.s6_addr[8],
287 ret, 8)) {
288 badid = 1;
289 break;
290 }
291 }
292 }
293
294 /*
295 * In the event that an unacceptable identifier has been generated,
296 * restart the process, using the right-most 64 bits of the MD5 digest
297 * obtained in place of the history value.
298 */
299 if (badid) {
300 /* for debugging purposes only */
301 #if 0
302 {
303 int i;
304
305 printf("unacceptable random ID: ");
306 for (i = 0; i < 16; i++)
307 printf("%02x", digest[i]);
308 printf("\n");
309 }
310 #endif
311
312 if (++retry < GEN_TEMPID_RETRY_MAX) {
313 memcpy(seed, &digest[8], 8);
314 goto again;
315 } else {
316 /*
317 * We're so unlucky. Give up for now, and return
318 * all 0 IDs to tell the caller not to make a
319 * temporary address.
320 */
321 nd6log((LOG_NOTICE,
322 "generate_tmp_ifid: never found a good ID\n"));
323 memset(ret, 0, 8);
324 }
325 }
326
327 /*
328 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (6)
329 * Take the rightmost 64-bits of the MD5 digest and save them in
330 * stable storage as the history value to be used in the next
331 * iteration of the algorithm.
332 */
333 memcpy(seed0, &digest[8], 8);
334
335 /* for debugging purposes only */
336 #if 0
337 {
338 int i;
339
340 printf("to: ");
341 for (i = 0; i < 16; i++)
342 printf("%02x", digest[i]);
343 printf("\n");
344 }
345 #endif
346
347 return 0;
348 }
349
350 /*
351 * Get interface identifier for the specified interface.
352 *
353 * in6 - upper 64bits are preserved
354 */
355 int
356 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
357 {
358 struct ifaddr *ifa;
359 const struct sockaddr_dl *sdl = NULL, *tsdl;
360 const char *addr;
361 size_t addrlen;
362 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
363 static u_int8_t allone[8] =
364 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
365
366 IFADDR_FOREACH(ifa, ifp) {
367 if (ifa->ifa_addr->sa_family != AF_LINK)
368 continue;
369 tsdl = satocsdl(ifa->ifa_addr);
370 if (tsdl == NULL || tsdl->sdl_alen == 0)
371 continue;
372 if (sdl == NULL || ifa == ifp->if_dl || ifa == ifp->if_hwdl)
373 sdl = tsdl;
374 if (ifa == ifp->if_hwdl)
375 break;
376 }
377
378 if (sdl == NULL)
379 return -1;
380
381 addr = CLLADDR(sdl);
382 addrlen = sdl->sdl_alen;
383
384 switch (ifp->if_type) {
385 case IFT_IEEE1394:
386 case IFT_IEEE80211:
387 /* IEEE1394 uses 16byte length address starting with EUI64 */
388 if (addrlen > 8)
389 addrlen = 8;
390 break;
391 default:
392 break;
393 }
394
395 /* get EUI64 */
396 switch (ifp->if_type) {
397 /* IEEE802/EUI64 cases - what others? */
398 case IFT_ETHER:
399 case IFT_FDDI:
400 case IFT_ATM:
401 case IFT_IEEE1394:
402 case IFT_IEEE80211:
403 /* look at IEEE802/EUI64 only */
404 if (addrlen != 8 && addrlen != 6)
405 return -1;
406
407 /*
408 * check for invalid MAC address - on bsdi, we see it a lot
409 * since wildboar configures all-zero MAC on pccard before
410 * card insertion.
411 */
412 if (memcmp(addr, allzero, addrlen) == 0)
413 return -1;
414 if (memcmp(addr, allone, addrlen) == 0)
415 return -1;
416
417 /* make EUI64 address */
418 if (addrlen == 8)
419 memcpy(&in6->s6_addr[8], addr, 8);
420 else if (addrlen == 6) {
421 in6->s6_addr[8] = addr[0];
422 in6->s6_addr[9] = addr[1];
423 in6->s6_addr[10] = addr[2];
424 in6->s6_addr[11] = 0xff;
425 in6->s6_addr[12] = 0xfe;
426 in6->s6_addr[13] = addr[3];
427 in6->s6_addr[14] = addr[4];
428 in6->s6_addr[15] = addr[5];
429 }
430 break;
431
432 case IFT_ARCNET:
433 if (addrlen != 1)
434 return -1;
435 if (!addr[0])
436 return -1;
437
438 memset(&in6->s6_addr[8], 0, 8);
439 in6->s6_addr[15] = addr[0];
440
441 /*
442 * due to insufficient bitwidth, we mark it local.
443 */
444 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
445 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
446 break;
447
448 case IFT_GIF:
449 #ifdef IFT_STF
450 case IFT_STF:
451 #endif
452 /*
453 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
454 * however, IPv4 address is not very suitable as unique
455 * identifier source (can be renumbered).
456 * we don't do this.
457 */
458 return -1;
459
460 default:
461 return -1;
462 }
463
464 /* sanity check: g bit must not indicate "group" */
465 if (EUI64_GROUP(in6))
466 return -1;
467
468 /* convert EUI64 into IPv6 interface identifier */
469 EUI64_TO_IFID(in6);
470
471 /*
472 * sanity check: ifid must not be all zero, avoid conflict with
473 * subnet router anycast
474 */
475 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
476 memcmp(&in6->s6_addr[9], allzero, 7) == 0) {
477 return -1;
478 }
479
480 return 0;
481 }
482
483 /*
484 * Get interface identifier for the specified interface. If it is not
485 * available on ifp0, borrow interface identifier from other information
486 * sources.
487 *
488 * altifp - secondary EUI64 source
489 */
490 static int
491 get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
492 struct in6_addr *in6)
493 {
494 struct ifnet *ifp;
495
496 /* first, try to get it from the interface itself */
497 if (in6_get_hw_ifid(ifp0, in6) == 0) {
498 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
499 if_name(ifp0)));
500 goto success;
501 }
502
503 /* try secondary EUI64 source. this basically is for ATM PVC */
504 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
505 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
506 if_name(ifp0), if_name(altifp)));
507 goto success;
508 }
509
510 /* next, try to get it from some other hardware interface */
511 TAILQ_FOREACH(ifp, &ifnet, if_list) {
512 if (ifp == ifp0)
513 continue;
514 if (in6_get_hw_ifid(ifp, in6) != 0)
515 continue;
516
517 /*
518 * to borrow ifid from other interface, ifid needs to be
519 * globally unique
520 */
521 if (IFID_UNIVERSAL(in6)) {
522 nd6log((LOG_DEBUG,
523 "%s: borrow interface identifier from %s\n",
524 if_name(ifp0), if_name(ifp)));
525 goto success;
526 }
527 }
528
529 #if 0
530 /* get from hostid - only for certain architectures */
531 if (get_hostid_ifid(ifp, in6) == 0) {
532 nd6log((LOG_DEBUG,
533 "%s: interface identifier generated by hostid\n",
534 if_name(ifp0)));
535 goto success;
536 }
537 #endif
538
539 /* last resort: get from random number source */
540 if (get_rand_ifid(ifp, in6) == 0) {
541 nd6log((LOG_DEBUG,
542 "%s: interface identifier generated by random number\n",
543 if_name(ifp0)));
544 goto success;
545 }
546
547 printf("%s: failed to get interface identifier\n", if_name(ifp0));
548 return -1;
549
550 success:
551 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
552 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
553 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
554 in6->s6_addr[14], in6->s6_addr[15]));
555 return 0;
556 }
557
558 /*
559 * altifp - secondary EUI64 source
560 */
561
562 static int
563 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
564 {
565 struct in6_ifaddr *ia;
566 struct in6_aliasreq ifra;
567 struct nd_prefixctl pr0;
568 int i, error;
569
570 /*
571 * configure link-local address.
572 */
573 memset(&ifra, 0, sizeof(ifra));
574
575 /*
576 * in6_update_ifa() does not use ifra_name, but we accurately set it
577 * for safety.
578 */
579 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
580
581 ifra.ifra_addr.sin6_family = AF_INET6;
582 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
583 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
584 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
585 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
586 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
587 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
588 } else {
589 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
590 nd6log((LOG_ERR,
591 "%s: no ifid available\n", if_name(ifp)));
592 return -1;
593 }
594 }
595 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
596 return -1;
597
598 sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask64, 0, 0, 0);
599 /* link-local addresses should NEVER expire. */
600 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
601 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
602
603 /*
604 * Now call in6_update_ifa() to do a bunch of procedures to configure
605 * a link-local address. We can set the 3rd argument to NULL, because
606 * we know there's no other link-local address on the interface
607 * and therefore we are adding one (instead of updating one).
608 */
609 if ((error = in6_update_ifa(ifp, &ifra, NULL,
610 IN6_IFAUPDATE_DADDELAY)) != 0) {
611 /*
612 * XXX: When the interface does not support IPv6, this call
613 * would fail in the SIOCINITIFADDR ioctl. I believe the
614 * notification is rather confusing in this case, so just
615 * suppress it. (jinmei (at) kame.net 20010130)
616 */
617 if (error != EAFNOSUPPORT)
618 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
619 "configure a link-local address on %s "
620 "(errno=%d)\n",
621 if_name(ifp), error));
622 return -1;
623 }
624
625 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
626 #ifdef DIAGNOSTIC
627 if (!ia) {
628 panic("ia == NULL in in6_ifattach_linklocal");
629 /* NOTREACHED */
630 }
631 #endif
632
633 /*
634 * Make the link-local prefix (fe80::/64%link) as on-link.
635 * Since we'd like to manage prefixes separately from addresses,
636 * we make an ND6 prefix structure for the link-local prefix,
637 * and add it to the prefix list as a never-expire prefix.
638 * XXX: this change might affect some existing code base...
639 */
640 memset(&pr0, 0, sizeof(pr0));
641 pr0.ndpr_ifp = ifp;
642 /* this should be 64 at this moment. */
643 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
644 pr0.ndpr_prefix = ifra.ifra_addr;
645 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
646 for (i = 0; i < 4; i++) {
647 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
648 in6mask64.s6_addr32[i];
649 }
650 /*
651 * Initialize parameters. The link-local prefix must always be
652 * on-link, and its lifetimes never expire.
653 */
654 pr0.ndpr_raf_onlink = 1;
655 pr0.ndpr_raf_auto = 1; /* probably meaningless */
656 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
657 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
658 /*
659 * Since there is no other link-local addresses, nd6_prefix_lookup()
660 * probably returns NULL. However, we cannot always expect the result.
661 * For example, if we first remove the (only) existing link-local
662 * address, and then reconfigure another one, the prefix is still
663 * valid with referring to the old link-local address.
664 */
665 if (nd6_prefix_lookup(&pr0) == NULL) {
666 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
667 return error;
668 }
669
670 return 0;
671 }
672
673 /*
674 * ifp - mut be IFT_LOOP
675 */
676
677 static int
678 in6_ifattach_loopback(struct ifnet *ifp)
679 {
680 struct in6_aliasreq ifra;
681 int error;
682
683 memset(&ifra, 0, sizeof(ifra));
684
685 /*
686 * in6_update_ifa() does not use ifra_name, but we accurately set it
687 * for safety.
688 */
689 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
690
691 sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask128, 0, 0, 0);
692
693 /*
694 * Always initialize ia_dstaddr (= broadcast address) to loopback
695 * address. Follows IPv4 practice - see in_ifinit().
696 */
697 sockaddr_in6_init(&ifra.ifra_dstaddr, &in6addr_loopback, 0, 0, 0);
698
699 sockaddr_in6_init(&ifra.ifra_addr, &in6addr_loopback, 0, 0, 0);
700
701 /* the loopback address should NEVER expire. */
702 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
703 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
704
705 /* we don't need to perform DAD on loopback interfaces. */
706 ifra.ifra_flags |= IN6_IFF_NODAD;
707
708 /*
709 * We are sure that this is a newly assigned address, so we can set
710 * NULL to the 3rd arg.
711 */
712 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
713 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
714 "the loopback address on %s (errno=%d)\n",
715 if_name(ifp), error));
716 return -1;
717 }
718
719 return 0;
720 }
721
722 /*
723 * compute NI group address, based on the current hostname setting.
724 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
725 *
726 * when ifp == NULL, the caller is responsible for filling scopeid.
727 */
728 int
729 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
730 struct sockaddr_in6 *sa6)
731 {
732 const char *p;
733 u_int8_t *q;
734 MD5_CTX ctxt;
735 u_int8_t digest[16];
736 u_int8_t l;
737 u_int8_t n[64]; /* a single label must not exceed 63 chars */
738
739 if (!namelen || !name)
740 return -1;
741
742 p = name;
743 while (p && *p && *p != '.' && p - name < namelen)
744 p++;
745 if (p - name > sizeof(n) - 1)
746 return -1; /* label too long */
747 l = p - name;
748 strncpy((char *)n, name, l);
749 n[(int)l] = '\0';
750 for (q = n; *q; q++) {
751 if ('A' <= *q && *q <= 'Z')
752 *q = *q - 'A' + 'a';
753 }
754
755 /* generate 8 bytes of pseudo-random value. */
756 memset(&ctxt, 0, sizeof(ctxt));
757 MD5Init(&ctxt);
758 MD5Update(&ctxt, &l, sizeof(l));
759 MD5Update(&ctxt, n, l);
760 MD5Final(digest, &ctxt);
761
762 memset(sa6, 0, sizeof(*sa6));
763 sa6->sin6_family = AF_INET6;
764 sa6->sin6_len = sizeof(*sa6);
765 sa6->sin6_addr.s6_addr16[0] = htons(0xff02);
766 sa6->sin6_addr.s6_addr8[11] = 2;
767 memcpy(&sa6->sin6_addr.s6_addr32[3], digest,
768 sizeof(sa6->sin6_addr.s6_addr32[3]));
769 if (in6_setscope(&sa6->sin6_addr, ifp, NULL))
770 return -1; /* XXX: should not fail */
771
772 return 0;
773 }
774
775 /*
776 * XXX multiple loopback interface needs more care. for instance,
777 * nodelocal address needs to be configured onto only one of them.
778 * XXX multiple link-local address case
779 *
780 * altifp - secondary EUI64 source
781 */
782 void
783 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
784 {
785
786 /* some of the interfaces are inherently not IPv6 capable */
787 switch (ifp->if_type) {
788 case IFT_BRIDGE:
789 #ifdef IFT_PFLOG
790 case IFT_PFLOG:
791 #endif
792 #ifdef IFT_PFSYNC
793 case IFT_PFSYNC:
794 #endif
795 return;
796 }
797
798 /*
799 * if link mtu is too small, don't try to configure IPv6.
800 * remember there could be some link-layer that has special
801 * fragmentation logic.
802 */
803 if (ifp->if_mtu < IPV6_MMTU) {
804 nd6log((LOG_INFO, "in6_ifattach: "
805 "%s has too small MTU, IPv6 not enabled\n",
806 if_name(ifp)));
807 return;
808 }
809
810 /* create a multicast kludge storage (if we have not had one) */
811 in6_createmkludge(ifp);
812
813 /*
814 * quirks based on interface type
815 */
816 switch (ifp->if_type) {
817 #ifdef IFT_STF
818 case IFT_STF:
819 /*
820 * 6to4 interface is a very special kind of beast.
821 * no multicast, no linklocal. RFC2529 specifies how to make
822 * linklocals for 6to4 interface, but there's no use and
823 * it is rather harmful to have one.
824 */
825 return;
826 #endif
827 case IFT_CARP:
828 return;
829 default:
830 break;
831 }
832
833 /*
834 * usually, we require multicast capability to the interface
835 */
836 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
837 nd6log((LOG_INFO, "in6_ifattach: "
838 "%s is not multicast capable, IPv6 not enabled\n",
839 if_name(ifp)));
840 return;
841 }
842
843 /* Assign addresses to ifp in another thread in order to
844 * avoid re-entering ifp->if_ioctl().
845 */
846 in6_ifaddrs_schedule(ifp, altifp);
847 }
848
849 /* in6_ifaddrs_init
850 *
851 * Add a link-local address to ifp, and if ifp is a loopback address,
852 * add a loopback address to it, too.
853 *
854 * If altifp is not NULL, derive the link-local address of ifp from the
855 * EUI64 of altifp.
856 */
857 void
858 in6_ifaddrs_init(struct ifnet *ifp, struct ifnet *altifp)
859 {
860 struct in6_addr in6;
861 struct in6_ifaddr *ia;
862
863 /*
864 * assign loopback address for loopback interface.
865 * XXX multiple loopback interface case.
866 */
867 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
868 in6 = in6addr_loopback;
869 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
870 if (in6_ifattach_loopback(ifp) != 0)
871 return;
872 }
873 }
874
875 /*
876 * assign a link-local address, if there's none.
877 */
878 if (ip6_auto_linklocal) {
879 ia = in6ifa_ifpforlinklocal(ifp, 0);
880 if (ia == NULL && in6_ifattach_linklocal(ifp, altifp) != 0) {
881 printf("%s: cannot assign link-local address\n",
882 ifp->if_xname);
883 }
884 }
885 }
886
887 /* getifp: helper for in6_ifaddrs_worker().
888 *
889 * Lookup an ifnet by ifindexgen, a pair consisting of an interface
890 * index (if_index) and an interface-index generation number.
891 */
892 static struct ifnet *
893 getifp(struct ifindexgen *ig)
894 {
895 int ifindex = ig->ig_idx;
896 uint64_t ifindex_gen = ig->ig_gen;
897 struct ifnet *ifp;
898
899 if (ifindex2ifnet == NULL)
900 printf("%s: no ifindices in use\n", __func__);
901 else if (ifindex >= if_indexlim) {
902 printf("%s: ifindex %d >= limit %zu\n", __func__, ifindex,
903 if_indexlim);
904 } else if ((ifp = ifindex2ifnet[ifindex]) == NULL)
905 printf("%s: ifindex %d not in use\n", __func__, ifindex);
906 else if (ifp->if_index_gen != ifindex_gen)
907 printf("%s: ifindex %d recycled\n", __func__, ifindex);
908 else
909 return ifp;
910 return NULL;
911 }
912
913 static void
914 in6_ifaddrs_worker(struct work *wk, void *arg)
915 {
916 struct in6_ifaddr_work *iw = (struct in6_ifaddr_work *)wk;
917 struct ifnet *altifp = NULL, *ifp;
918
919 if ((ifp = getifp(&iw->iw_idxgen)) != NULL &&
920 (!iw->iw_alt_present ||
921 (altifp = getifp(&iw->iw_alt_idxgen)) != NULL))
922 in6_ifaddrs_init(ifp, altifp);
923
924 kmem_free(iw, sizeof(*iw));
925 }
926
927 int
928 in6_ifaddrs_wq_establish(void)
929 {
930 int rc;
931
932 rc = workqueue_create(&in6_ifaddrs_wq, "in6ifaddr", in6_ifaddrs_worker,
933 NULL, PRI_KERNEL, IPL_NET, 0);
934
935 if (rc != 0) {
936 printf("%s: could not create inet6 ifaddrs workqueue.\n",
937 __func__);
938 }
939 return rc;
940 }
941
942 /* in6_ifaddrs_schedule
943 *
944 * Schedule ifp for a kernel thread to add addresses.
945 *
946 * The kernel thread will assign a link-local address to ifp, and if ifp
947 * is a loopback address, add a loopback address to it, too.
948 *
949 * If altifp is not NULL, derive the link-local address of ifp from the
950 * EUI64 of altifp.
951 */
952 void
953 in6_ifaddrs_schedule(struct ifnet *ifp, struct ifnet *altifp)
954 {
955 struct in6_ifaddr_work *iw;
956 struct ifindexgen *ig, *alt_ig;
957
958 iw = kmem_zalloc(sizeof(*iw), KM_SLEEP);
959
960 ig = &iw->iw_idxgen;
961 alt_ig = &iw->iw_alt_idxgen;
962
963 KASSERT(ifp->if_index < if_indexlim);
964 ig->ig_idx = ifp->if_index;
965 ig->ig_gen = ifp->if_index_gen;
966 if (altifp != NULL) {
967 KASSERT(altifp->if_index < if_indexlim);
968 alt_ig->ig_idx = altifp->if_index;
969 alt_ig->ig_gen = altifp->if_index_gen;
970 iw->iw_alt_present = true;
971 }
972
973 workqueue_enqueue(in6_ifaddrs_wq, &iw->iw_work, NULL);
974 }
975
976 /*
977 * NOTE: in6_ifdetach() does not support loopback if at this moment.
978 * We don't need this function in bsdi, because interfaces are never removed
979 * from the ifnet list in bsdi.
980 */
981 void
982 in6_ifdetach(struct ifnet *ifp)
983 {
984 struct in6_ifaddr *ia, *oia;
985 struct ifaddr *ifa, *next;
986 struct rtentry *rt;
987 short rtflags;
988 struct in6_multi_mship *imm;
989
990 /* remove ip6_mrouter stuff */
991 ip6_mrouter_detach(ifp);
992
993 /* remove neighbor management table */
994 nd6_purge(ifp);
995
996 /* XXX this code is duplicated in in6_purgeif() --dyoung */
997 /* nuke any of IPv6 addresses we have */
998 if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr);
999
1000 /* XXX isn't this code is redundant, given the above? --dyoung */
1001 /* XXX doesn't this code replicate code in in6_purgeaddr() ? --dyoung */
1002 /* undo everything done by in6_ifattach(), just in case */
1003 for (ifa = IFADDR_FIRST(ifp); ifa != NULL; ifa = next) {
1004 next = IFADDR_NEXT(ifa);
1005
1006 if (ifa->ifa_addr->sa_family != AF_INET6
1007 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
1008 continue;
1009 }
1010
1011 ia = (struct in6_ifaddr *)ifa;
1012
1013 /*
1014 * leave from multicast groups we have joined for the interface
1015 */
1016 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1017 LIST_REMOVE(imm, i6mm_chain);
1018 in6_leavegroup(imm);
1019 }
1020
1021 /* remove from the routing table */
1022 if ((ia->ia_flags & IFA_ROUTE) &&
1023 (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
1024 rtflags = rt->rt_flags;
1025 rtfree(rt);
1026 rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
1027 (struct sockaddr *)&ia->ia_addr,
1028 (struct sockaddr *)&ia->ia_prefixmask,
1029 rtflags, (struct rtentry **)0);
1030 }
1031
1032 /* remove from the linked list */
1033 ifa_remove(ifp, &ia->ia_ifa);
1034
1035 /* also remove from the IPv6 address chain(itojun&jinmei) */
1036 oia = ia;
1037 if (oia == (ia = in6_ifaddr))
1038 in6_ifaddr = ia->ia_next;
1039 else {
1040 while (ia->ia_next && (ia->ia_next != oia))
1041 ia = ia->ia_next;
1042 if (ia->ia_next)
1043 ia->ia_next = oia->ia_next;
1044 else {
1045 nd6log((LOG_ERR,
1046 "%s: didn't unlink in6ifaddr from list\n",
1047 if_name(ifp)));
1048 }
1049 }
1050
1051 IFAFREE(&oia->ia_ifa);
1052 }
1053
1054 /* cleanup multicast address kludge table, if there is any */
1055 in6_purgemkludge(ifp);
1056
1057 /*
1058 * remove neighbor management table. we call it twice just to make
1059 * sure we nuke everything. maybe we need just one call.
1060 * XXX: since the first call did not release addresses, some prefixes
1061 * might remain. We should call nd6_purge() again to release the
1062 * prefixes after removing all addresses above.
1063 * (Or can we just delay calling nd6_purge until at this point?)
1064 */
1065 nd6_purge(ifp);
1066 }
1067
1068 int
1069 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
1070 const u_int8_t *baseid, int generate)
1071 {
1072 u_int8_t nullbuf[8];
1073 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
1074
1075 memset(nullbuf, 0, sizeof(nullbuf));
1076 if (memcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
1077 /* we've never created a random ID. Create a new one. */
1078 generate = 1;
1079 }
1080
1081 if (generate) {
1082 memcpy(ndi->randomseed1, baseid, sizeof(ndi->randomseed1));
1083
1084 /* generate_tmp_ifid will update seedn and buf */
1085 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
1086 ndi->randomid);
1087 }
1088 memcpy(retbuf, ndi->randomid, 8);
1089 if (generate && memcmp(retbuf, nullbuf, sizeof(nullbuf)) == 0) {
1090 /* generate_tmp_ifid could not found a good ID. */
1091 return -1;
1092 }
1093
1094 return 0;
1095 }
1096
1097 void
1098 in6_tmpaddrtimer(void *ignored_arg)
1099 {
1100 struct nd_ifinfo *ndi;
1101 u_int8_t nullbuf[8];
1102 struct ifnet *ifp;
1103
1104 mutex_enter(softnet_lock);
1105 KERNEL_LOCK(1, NULL);
1106
1107 callout_reset(&in6_tmpaddrtimer_ch,
1108 (ip6_temp_preferred_lifetime - ip6_desync_factor -
1109 ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL);
1110
1111 memset(nullbuf, 0, sizeof(nullbuf));
1112 TAILQ_FOREACH(ifp, &ifnet, if_list) {
1113 ndi = ND_IFINFO(ifp);
1114 if (memcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
1115 /*
1116 * We've been generating a random ID on this interface.
1117 * Create a new one.
1118 */
1119 (void)generate_tmp_ifid(ndi->randomseed0,
1120 ndi->randomseed1, ndi->randomid);
1121 }
1122 }
1123
1124 KERNEL_UNLOCK_ONE(NULL);
1125 mutex_exit(softnet_lock);
1126 }
1127