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