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