1 /*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/sockio.h>
40 #include <sys/kernel.h>
41 #include <sys/syslog.h>
42 #include <sys/md5.h>
43
44 #include <net/if.h>
45 #include <net/if_dl.h>
46 #include <net/if_types.h>
47 #include <net/route.h>
48
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/if_ether.h>
52 #include <netinet/in_pcb.h>
53
54 #include <netinet/ip6.h>
55 #include <netinet6/ip6_var.h>
56 #include <netinet6/in6_var.h>
57 #include <netinet6/in6_pcb.h>
58 #include <netinet6/in6_ifattach.h>
59 #include <netinet6/ip6_var.h>
60 #include <netinet6/nd6.h>
61 #include <netinet6/scope6_var.h>
62
63 unsigned long in6_maxmtu = 0;
64
65 #ifdef IP6_AUTO_LINKLOCAL
66 int ip6_auto_linklocal = IP6_AUTO_LINKLOCAL;
67 #else
68 int ip6_auto_linklocal = 1; /* enable by default */
69 #endif
70
71 struct callout in6_tmpaddrtimer_ch;
72
73 extern struct inpcbinfo udbinfo;
74 extern struct inpcbinfo ripcbinfo;
75
76 static int get_rand_ifid(struct ifnet *, struct in6_addr *);
77 static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
78 static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
79 static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
80 static int in6_ifattach_loopback(struct ifnet *);
81 static void in6_purgemaddrs(struct ifnet *);
82
83 #define EUI64_GBIT 0x01
84 #define EUI64_UBIT 0x02
85 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
86 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
87 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
88 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
89 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
90
91 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
92 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
93
94 /*
95 * Generate a last-resort interface identifier, when the machine has no
96 * IEEE802/EUI64 address sources.
97 * The goal here is to get an interface identifier that is
98 * (1) random enough and (2) does not change across reboot.
99 * We currently use MD5(hostname) for it.
100 *
101 * in6 - upper 64bits are preserved
102 */
103 static int
104 get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
105 {
106 MD5_CTX ctxt;
107 u_int8_t digest[16];
108 int hostnamelen = strlen(hostname);
109
110 #if 0
111 /* we need at least several letters as seed for ifid */
112 if (hostnamelen < 3)
113 return -1;
114 #endif
115
116 /* generate 8 bytes of pseudo-random value. */
117 bzero(&ctxt, sizeof(ctxt));
118 MD5Init(&ctxt);
119 MD5Update(&ctxt, hostname, hostnamelen);
120 MD5Final(digest, &ctxt);
121
122 /* assumes sizeof(digest) > sizeof(ifid) */
123 bcopy(digest, &in6->s6_addr[8], 8);
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
135 static int
136 generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
137 {
138 MD5_CTX ctxt;
139 u_int8_t seed[16], digest[16], nullbuf[8];
140 u_int32_t val32;
141
142 /* If there's no history, start with a random seed. */
143 bzero(nullbuf, sizeof(nullbuf));
144 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
145 int i;
146
147 for (i = 0; i < 2; i++) {
148 val32 = arc4random();
149 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
150 }
151 } else
152 bcopy(seed0, seed, 8);
153
154 /* copy the right-most 64-bits of the given address */
155 /* XXX assumption on the size of IFID */
156 bcopy(seed1, &seed[8], 8);
157
158 if (0) { /* for debugging purposes only */
159 int i;
160
161 printf("generate_tmp_ifid: new randomized ID from: ");
162 for (i = 0; i < 16; i++)
163 printf("%02x", seed[i]);
164 printf(" ");
165 }
166
167 /* generate 16 bytes of pseudo-random value. */
168 bzero(&ctxt, sizeof(ctxt));
169 MD5Init(&ctxt);
170 MD5Update(&ctxt, seed, sizeof(seed));
171 MD5Final(digest, &ctxt);
172
173 /*
174 * RFC 3041 3.2.1. (3)
175 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
176 * left-most bit is numbered 0) to zero.
177 */
178 bcopy(digest, ret, 8);
179 ret[0] &= ~EUI64_UBIT;
180
181 /*
182 * XXX: we'd like to ensure that the generated value is not zero
183 * for simplicity. If the caclculated digest happens to be zero,
184 * use a random non-zero value as the last resort.
185 */
186 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
187 nd6log((LOG_INFO,
188 "generate_tmp_ifid: computed MD5 value is zero.\n"));
189
190 val32 = arc4random();
191 val32 = 1 + (val32 % (0xffffffff - 1));
192 }
193
194 /*
195 * RFC 3041 3.2.1. (4)
196 * Take the rightmost 64-bits of the MD5 digest and save them in
197 * stable storage as the history value to be used in the next
198 * iteration of the algorithm.
199 */
200 bcopy(&digest[8], seed0, 8);
201
202 if (0) { /* for debugging purposes only */
203 int i;
204
205 printf("to: ");
206 for (i = 0; i < 16; i++)
207 printf("%02x", digest[i]);
208 printf("\n");
209 }
210
211 return 0;
212 }
213
214 /*
215 * Get interface identifier for the specified interface.
216 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
217 *
218 * in6 - upper 64bits are preserved
219 */
220 int
221 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
222 {
223 struct ifaddr *ifa;
224 struct sockaddr_dl *sdl;
225 u_int8_t *addr;
226 size_t addrlen;
227 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
228 static u_int8_t allone[8] =
229 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
230
231 for (ifa = ifp->if_addrlist.tqh_first;
232 ifa;
233 ifa = ifa->ifa_list.tqe_next) {
234 if (ifa->ifa_addr->sa_family != AF_LINK)
235 continue;
236 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
237 if (sdl == NULL)
238 continue;
239 if (sdl->sdl_alen == 0)
240 continue;
241
242 goto found;
243 }
244
245 return -1;
246
247 found:
248 addr = LLADDR(sdl);
249 addrlen = sdl->sdl_alen;
250
251 /* get EUI64 */
252 switch (ifp->if_type) {
253 case IFT_ETHER:
254 case IFT_FDDI:
255 case IFT_ISO88025:
256 case IFT_ATM:
257 case IFT_IEEE1394:
258 #ifdef IFT_IEEE80211
259 case IFT_IEEE80211:
260 #endif
261 /* IEEE802/EUI64 cases - what others? */
262 /* IEEE1394 uses 16byte length address starting with EUI64 */
263 if (addrlen > 8)
264 addrlen = 8;
265
266 /* look at IEEE802/EUI64 only */
267 if (addrlen != 8 && addrlen != 6)
268 return -1;
269
270 /*
271 * check for invalid MAC address - on bsdi, we see it a lot
272 * since wildboar configures all-zero MAC on pccard before
273 * card insertion.
274 */
275 if (bcmp(addr, allzero, addrlen) == 0)
276 return -1;
277 if (bcmp(addr, allone, addrlen) == 0)
278 return -1;
279
280 /* make EUI64 address */
281 if (addrlen == 8)
282 bcopy(addr, &in6->s6_addr[8], 8);
283 else if (addrlen == 6) {
284 in6->s6_addr[8] = addr[0];
285 in6->s6_addr[9] = addr[1];
286 in6->s6_addr[10] = addr[2];
287 in6->s6_addr[11] = 0xff;
288 in6->s6_addr[12] = 0xfe;
289 in6->s6_addr[13] = addr[3];
290 in6->s6_addr[14] = addr[4];
291 in6->s6_addr[15] = addr[5];
292 }
293 break;
294
295 case IFT_ARCNET:
296 if (addrlen != 1)
297 return -1;
298 if (!addr[0])
299 return -1;
300
301 bzero(&in6->s6_addr[8], 8);
302 in6->s6_addr[15] = addr[0];
303
304 /*
305 * due to insufficient bitwidth, we mark it local.
306 */
307 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
308 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
309 break;
310
311 case IFT_GIF:
312 #ifdef IFT_STF
313 case IFT_STF:
314 #endif
315 /*
316 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
317 * however, IPv4 address is not very suitable as unique
318 * identifier source (can be renumbered).
319 * we don't do this.
320 */
321 return -1;
322
323 default:
324 return -1;
325 }
326
327 /* sanity check: g bit must not indicate "group" */
328 if (EUI64_GROUP(in6))
329 return -1;
330
331 /* convert EUI64 into IPv6 interface identifier */
332 EUI64_TO_IFID(in6);
333
334 /*
335 * sanity check: ifid must not be all zero, avoid conflict with
336 * subnet router anycast
337 */
338 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
339 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
340 return -1;
341 }
342
343 return 0;
344 }
345
346 /*
347 * Get interface identifier for the specified interface. If it is not
348 * available on ifp0, borrow interface identifier from other information
349 * sources.
350 *
351 * altifp - secondary EUI64 source
352 */
353 static int
354 get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
355 struct in6_addr *in6)
356 {
357 struct ifnet *ifp;
358
359 /* first, try to get it from the interface itself */
360 if (in6_get_hw_ifid(ifp0, in6) == 0) {
361 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
362 if_name(ifp0)));
363 goto success;
364 }
365
366 /* try secondary EUI64 source. this basically is for ATM PVC */
367 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
368 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
369 if_name(ifp0), if_name(altifp)));
370 goto success;
371 }
372
373 /* next, try to get it from some other hardware interface */
374 IFNET_RLOCK();
375 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
376 if (ifp == ifp0)
377 continue;
378 if (in6_get_hw_ifid(ifp, in6) != 0)
379 continue;
380
381 /*
382 * to borrow ifid from other interface, ifid needs to be
383 * globally unique
384 */
385 if (IFID_UNIVERSAL(in6)) {
386 nd6log((LOG_DEBUG,
387 "%s: borrow interface identifier from %s\n",
388 if_name(ifp0), if_name(ifp)));
389 IFNET_RUNLOCK();
390 goto success;
391 }
392 }
393 IFNET_RUNLOCK();
394
395 /* last resort: get from random number source */
396 if (get_rand_ifid(ifp, in6) == 0) {
397 nd6log((LOG_DEBUG,
398 "%s: interface identifier generated by random number\n",
399 if_name(ifp0)));
400 goto success;
401 }
402
403 printf("%s: failed to get interface identifier\n", if_name(ifp0));
404 return -1;
405
406 success:
407 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
408 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
409 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
410 in6->s6_addr[14], in6->s6_addr[15]));
411 return 0;
412 }
413
414 /*
415 * altifp - secondary EUI64 source
416 */
417 static int
418 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
419 {
420 struct in6_ifaddr *ia;
421 struct in6_aliasreq ifra;
422 struct nd_prefixctl pr0;
423 int i, error;
424
425 /*
426 * configure link-local address.
427 */
428 bzero(&ifra, sizeof(ifra));
429
430 /*
431 * in6_update_ifa() does not use ifra_name, but we accurately set it
432 * for safety.
433 */
434 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
435
436 ifra.ifra_addr.sin6_family = AF_INET6;
437 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
438 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
439 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
440 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
441 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
442 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
443 } else {
444 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
445 nd6log((LOG_ERR,
446 "%s: no ifid available\n", if_name(ifp)));
447 return (-1);
448 }
449 }
450 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
451 return (-1);
452
453 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
454 ifra.ifra_prefixmask.sin6_family = AF_INET6;
455 ifra.ifra_prefixmask.sin6_addr = in6mask64;
456 /* link-local addresses should NEVER expire. */
457 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
458 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
459
460 /*
461 * Now call in6_update_ifa() to do a bunch of procedures to configure
462 * a link-local address. We can set the 3rd argument to NULL, because
463 * we know there's no other link-local address on the interface
464 * and therefore we are adding one (instead of updating one).
465 */
466 if ((error = in6_update_ifa(ifp, &ifra, NULL,
467 IN6_IFAUPDATE_DADDELAY)) != 0) {
468 /*
469 * XXX: When the interface does not support IPv6, this call
470 * would fail in the SIOCSIFADDR ioctl. I believe the
471 * notification is rather confusing in this case, so just
472 * suppress it. (jinmei@kame.net 20010130)
473 */
474 if (error != EAFNOSUPPORT)
475 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
476 "configure a link-local address on %s "
477 "(errno=%d)\n",
478 if_name(ifp), error));
479 return (-1);
480 }
481
482 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
483 KASSERT(ia != NULL, ("%s: ia == NULL, ifp=%p", __func__, ifp));
484
485 /*
486 * Make the link-local prefix (fe80::%link/64) as on-link.
487 * Since we'd like to manage prefixes separately from addresses,
488 * we make an ND6 prefix structure for the link-local prefix,
489 * and add it to the prefix list as a never-expire prefix.
490 * XXX: this change might affect some existing code base...
491 */
492 bzero(&pr0, sizeof(pr0));
493 pr0.ndpr_ifp = ifp;
494 /* this should be 64 at this moment. */
495 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
496 pr0.ndpr_prefix = ifra.ifra_addr;
497 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
498 for (i = 0; i < 4; i++) {
499 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
500 in6mask64.s6_addr32[i];
501 }
502 /*
503 * Initialize parameters. The link-local prefix must always be
504 * on-link, and its lifetimes never expire.
505 */
506 pr0.ndpr_raf_onlink = 1;
507 pr0.ndpr_raf_auto = 1; /* probably meaningless */
508 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
509 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
510 /*
511 * Since there is no other link-local addresses, nd6_prefix_lookup()
512 * probably returns NULL. However, we cannot always expect the result.
513 * For example, if we first remove the (only) existing link-local
514 * address, and then reconfigure another one, the prefix is still
515 * valid with referring to the old link-local address.
516 */
517 if (nd6_prefix_lookup(&pr0) == NULL) {
518 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
519 return (error);
520 }
521
522 return 0;
523 }
524
525 /*
526 * ifp - must be IFT_LOOP
527 */
528 static int
529 in6_ifattach_loopback(struct ifnet *ifp)
530 {
531 struct in6_aliasreq ifra;
532 int error;
533
534 bzero(&ifra, sizeof(ifra));
535
536 /*
537 * in6_update_ifa() does not use ifra_name, but we accurately set it
538 * for safety.
539 */
540 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
541
542 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
543 ifra.ifra_prefixmask.sin6_family = AF_INET6;
544 ifra.ifra_prefixmask.sin6_addr = in6mask128;
545
546 /*
547 * Always initialize ia_dstaddr (= broadcast address) to loopback
548 * address. Follows IPv4 practice - see in_ifinit().
549 */
550 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
551 ifra.ifra_dstaddr.sin6_family = AF_INET6;
552 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
553
554 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
555 ifra.ifra_addr.sin6_family = AF_INET6;
556 ifra.ifra_addr.sin6_addr = in6addr_loopback;
557
558 /* the loopback address should NEVER expire. */
559 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
560 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
561
562 /* we don't need to perform DAD on loopback interfaces. */
563 ifra.ifra_flags |= IN6_IFF_NODAD;
564
565 /* skip registration to the prefix list. XXX should be temporary. */
566 ifra.ifra_flags |= IN6_IFF_NOPFX;
567
568 /*
569 * We are sure that this is a newly assigned address, so we can set
570 * NULL to the 3rd arg.
571 */
572 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
573 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
574 "the loopback address on %s (errno=%d)\n",
575 if_name(ifp), error));
576 return (-1);
577 }
578
579 return 0;
580 }
581
582 /*
583 * compute NI group address, based on the current hostname setting.
584 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
585 *
586 * when ifp == NULL, the caller is responsible for filling scopeid.
587 */
588 int
589 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
590 struct in6_addr *in6)
591 {
592 const char *p;
593 u_char *q;
594 MD5_CTX ctxt;
595 u_int8_t digest[16];
596 char l;
597 char n[64]; /* a single label must not exceed 63 chars */
598
599 if (!namelen || !name)
600 return -1;
601
602 p = name;
603 while (p && *p && *p != '.' && p - name < namelen)
604 p++;
605 if (p - name > sizeof(n) - 1)
606 return -1; /* label too long */
607 l = p - name;
608 strncpy(n, name, l);
609 n[(int)l] = '\0';
610 for (q = n; *q; q++) {
611 if ('A' <= *q && *q <= 'Z')
612 *q = *q - 'A' + 'a';
613 }
614
615 /* generate 8 bytes of pseudo-random value. */
616 bzero(&ctxt, sizeof(ctxt));
617 MD5Init(&ctxt);
618 MD5Update(&ctxt, &l, sizeof(l));
619 MD5Update(&ctxt, n, l);
620 MD5Final(digest, &ctxt);
621
622 bzero(in6, sizeof(*in6));
623 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
624 in6->s6_addr8[11] = 2;
625 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
626 if (in6_setscope(in6, ifp, NULL))
627 return (-1); /* XXX: should not fail */
628
629 return 0;
630 }
631
632 /*
633 * XXX multiple loopback interface needs more care. for instance,
634 * nodelocal address needs to be configured onto only one of them.
635 * XXX multiple link-local address case
636 *
637 * altifp - secondary EUI64 source
638 */
639 void
640 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
641 {
642 struct in6_ifaddr *ia;
643 struct in6_addr in6;
644
645 /* some of the interfaces are inherently not IPv6 capable */
646 switch (ifp->if_type) {
647 case IFT_PFLOG:
648 case IFT_PFSYNC:
649 case IFT_CARP:
650 return;
651 }
652
653 /*
654 * quirks based on interface type
655 */
656 switch (ifp->if_type) {
657 #ifdef IFT_STF
658 case IFT_STF:
659 /*
660 * 6to4 interface is a very special kind of beast.
661 * no multicast, no linklocal. RFC2529 specifies how to make
662 * linklocals for 6to4 interface, but there's no use and
663 * it is rather harmful to have one.
664 */
665 goto statinit;
666 #endif
667 default:
668 break;
669 }
670
671 /*
672 * usually, we require multicast capability to the interface
673 */
674 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
675 nd6log((LOG_INFO, "in6_ifattach: "
676 "%s is not multicast capable, IPv6 not enabled\n",
677 if_name(ifp)));
678 return;
679 }
680
681 /*
682 * assign loopback address for loopback interface.
683 * XXX multiple loopback interface case.
684 */
685 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
686 in6 = in6addr_loopback;
687 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
688 if (in6_ifattach_loopback(ifp) != 0)
689 return;
690 }
691 }
692
693 /*
694 * assign a link-local address, if there's none.
695 */
696 if (ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) {
697 ia = in6ifa_ifpforlinklocal(ifp, 0);
698 if (ia == NULL) {
699 if (in6_ifattach_linklocal(ifp, altifp) == 0) {
700 /* linklocal address assigned */
701 } else {
702 /* failed to assign linklocal address. bark? */
703 }
704 }
705 }
706
707 #ifdef IFT_STF /* XXX */
708 statinit:
709 #endif
710
711 /* update dynamically. */
712 if (in6_maxmtu < ifp->if_mtu)
713 in6_maxmtu = ifp->if_mtu;
714 }
715
716 /*
717 * NOTE: in6_ifdetach() does not support loopback if at this moment.
718 * We don't need this function in bsdi, because interfaces are never removed
719 * from the ifnet list in bsdi.
720 */
721 void
722 in6_ifdetach(struct ifnet *ifp)
723 {
724 struct in6_ifaddr *ia, *oia;
725 struct ifaddr *ifa, *next;
726 struct rtentry *rt;
727 short rtflags;
728 struct sockaddr_in6 sin6;
729 struct in6_multi_mship *imm;
730
731 /* remove neighbor management table */
732 nd6_purge(ifp);
733
734 /* nuke any of IPv6 addresses we have */
735 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) {
736 next = ifa->ifa_list.tqe_next;
737 if (ifa->ifa_addr->sa_family != AF_INET6)
738 continue;
739 in6_purgeaddr(ifa);
740 }
741
742 /* undo everything done by in6_ifattach(), just in case */
743 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) {
744 next = ifa->ifa_list.tqe_next;
745
746 if (ifa->ifa_addr->sa_family != AF_INET6
747 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
748 continue;
749 }
750
751 ia = (struct in6_ifaddr *)ifa;
752
753 /*
754 * leave from multicast groups we have joined for the interface
755 */
756 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
757 LIST_REMOVE(imm, i6mm_chain);
758 in6_leavegroup(imm);
759 }
760
761 /* remove from the routing table */
762 if ((ia->ia_flags & IFA_ROUTE) &&
763 (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
764 rtflags = rt->rt_flags;
765 RTFREE_LOCKED(rt);
766 rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
767 (struct sockaddr *)&ia->ia_addr,
768 (struct sockaddr *)&ia->ia_prefixmask,
769 rtflags, (struct rtentry **)0);
770 }
771
772 /* remove from the linked list */
773 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
774 IFAFREE(&ia->ia_ifa);
775
776 /* also remove from the IPv6 address chain(itojun&jinmei) */
777 oia = ia;
778 if (oia == (ia = in6_ifaddr))
779 in6_ifaddr = ia->ia_next;
780 else {
781 while (ia->ia_next && (ia->ia_next != oia))
782 ia = ia->ia_next;
783 if (ia->ia_next)
784 ia->ia_next = oia->ia_next;
785 else {
786 nd6log((LOG_ERR,
787 "%s: didn't unlink in6ifaddr from list\n",
788 if_name(ifp)));
789 }
790 }
791
792 IFAFREE(&oia->ia_ifa);
793 }
794
795 in6_pcbpurgeif0(&udbinfo, ifp);
796 in6_pcbpurgeif0(&ripcbinfo, ifp);
797 /* leave from all multicast groups joined */
798 in6_purgemaddrs(ifp);
799
800 /*
801 * remove neighbor management table. we call it twice just to make
802 * sure we nuke everything. maybe we need just one call.
803 * XXX: since the first call did not release addresses, some prefixes
804 * might remain. We should call nd6_purge() again to release the
805 * prefixes after removing all addresses above.
806 * (Or can we just delay calling nd6_purge until at this point?)
807 */
808 nd6_purge(ifp);
809
810 /* remove route to link-local allnodes multicast (ff02::1) */
811 bzero(&sin6, sizeof(sin6));
812 sin6.sin6_len = sizeof(struct sockaddr_in6);
813 sin6.sin6_family = AF_INET6;
814 sin6.sin6_addr = in6addr_linklocal_allnodes;
815 if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
816 /* XXX: should not fail */
817 return;
818 /* XXX grab lock first to avoid LOR */
819 if (rt_tables[0][AF_INET6] != NULL) {
820 RADIX_NODE_HEAD_LOCK(rt_tables[0][AF_INET6]);
821 rt = rtalloc1((struct sockaddr *)&sin6, 0, RTF_RNH_LOCKED);
822 if (rt) {
823 if (rt->rt_ifp == ifp)
824 rtexpunge(rt);
825 RTFREE_LOCKED(rt);
826 }
827 RADIX_NODE_HEAD_UNLOCK(rt_tables[0][AF_INET6]);
828 }
829 }
830
831 int
832 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
833 const u_int8_t *baseid, int generate)
834 {
835 u_int8_t nullbuf[8];
836 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
837
838 bzero(nullbuf, sizeof(nullbuf));
839 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
840 /* we've never created a random ID. Create a new one. */
841 generate = 1;
842 }
843
844 if (generate) {
845 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
846
847 /* generate_tmp_ifid will update seedn and buf */
848 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
849 ndi->randomid);
850 }
851 bcopy(ndi->randomid, retbuf, 8);
852
853 return (0);
854 }
855
856 void
857 in6_tmpaddrtimer(void *ignored_arg)
858 {
859 struct nd_ifinfo *ndi;
860 u_int8_t nullbuf[8];
861 struct ifnet *ifp;
862 int s = splnet();
863
864 callout_reset(&in6_tmpaddrtimer_ch,
865 (ip6_temp_preferred_lifetime - ip6_desync_factor -
866 ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL);
867
868 bzero(nullbuf, sizeof(nullbuf));
869 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
870 ndi = ND_IFINFO(ifp);
871 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
872 /*
873 * We've been generating a random ID on this interface.
874 * Create a new one.
875 */
876 (void)generate_tmp_ifid(ndi->randomseed0,
877 ndi->randomseed1, ndi->randomid);
878 }
879 }
880
881 splx(s);
882 }
883
884 static void
885 in6_purgemaddrs(struct ifnet *ifp)
886 {
887 struct in6_multi *in6m;
888 struct in6_multi *oin6m;
889
890 #ifdef DIAGNOSTIC
891 printf("%s: purging ifp %p\n", __func__, ifp);
892 #endif
893
894 IFF_LOCKGIANT(ifp);
895 LIST_FOREACH_SAFE(in6m, &in6_multihead, in6m_entry, oin6m) {
896 if (in6m->in6m_ifp == ifp)
897 in6_delmulti(in6m);
898 }
899 IFF_UNLOCKGIANT(ifp);
900 }
Cache object: e5d449657bd479fa1645339594dfaac8
|