1 /* $FreeBSD: src/sys/netinet6/in6_ifattach.c,v 1.2.2.6 2002/04/28 05:40:26 suz Exp $ */
2 /* $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun 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/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/kernel.h>
39 #include <sys/syslog.h>
40 #include <sys/md5.h>
41 #include <sys/thread2.h>
42
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46 #include <net/route.h>
47
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/if_ether.h>
51 #include <netinet/in_pcb.h>
52 #include <netinet/udp_var.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/nd6.h>
60 #include <netinet6/scope6_var.h>
61
62 #include <net/net_osdep.h>
63
64 unsigned long in6_maxmtu = 0;
65
66 #ifdef IP6_AUTO_LINKLOCAL
67 int ip6_auto_linklocal = IP6_AUTO_LINKLOCAL;
68 #else
69 int ip6_auto_linklocal = 1; /* enable by default */
70 #endif
71
72 struct callout in6_tmpaddrtimer_ch;
73
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_hw_ifid (struct ifnet *, struct in6_addr *);
79 static int get_ifid (struct ifnet *, struct ifnet *, struct in6_addr *);
80 static int in6_ifattach_linklocal (struct ifnet *, struct ifnet *);
81 static int in6_ifattach_loopback (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 static int
102 get_rand_ifid(struct ifnet *ifp,
103 struct in6_addr *in6) /* upper 64bits are preserved */
104 {
105 MD5_CTX ctxt;
106 u_int8_t digest[16];
107 int hostnamelen = strlen(hostname);
108
109 #if 0
110 /* we need at least several letters as seed for ifid */
111 if (hostnamelen < 3)
112 return -1;
113 #endif
114
115 /* generate 8 bytes of pseudo-random value. */
116 bzero(&ctxt, sizeof(ctxt));
117 MD5Init(&ctxt);
118 MD5Update(&ctxt, hostname, hostnamelen);
119 MD5Final(digest, &ctxt);
120
121 /* assumes sizeof(digest) > sizeof(ifid) */
122 bcopy(digest, &in6->s6_addr[8], 8);
123
124 /* make sure to set "u" bit to local, and "g" bit to individual. */
125 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
126 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
127
128 /* convert EUI64 into IPv6 interface identifier */
129 EUI64_TO_IFID(in6);
130
131 return 0;
132 }
133
134 static int
135 generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
136 {
137 MD5_CTX ctxt;
138 u_int8_t seed[16], digest[16], nullbuf[8];
139 u_int32_t val32;
140 struct timeval tv;
141
142 /* If there's no hisotry, 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 microtime(&tv);
149 val32 = krandom() ^ tv.tv_usec;
150 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
151 }
152 } else {
153 bcopy(seed0, seed, 8);
154 }
155
156 /* copy the right-most 64-bits of the given address */
157 /* XXX assumption on the size of IFID */
158 bcopy(seed1, &seed[8], 8);
159
160 if (0) { /* for debugging purposes only */
161 int i;
162
163 kprintf("generate_tmp_ifid: new randomized ID from: ");
164 for (i = 0; i < 16; i++)
165 kprintf("%02x", seed[i]);
166 kprintf(" ");
167 }
168
169 /* generate 16 bytes of pseudo-random value. */
170 bzero(&ctxt, sizeof(ctxt));
171 MD5Init(&ctxt);
172 MD5Update(&ctxt, seed, sizeof(seed));
173 MD5Final(digest, &ctxt);
174
175 /*
176 * RFC 3041 3.2.1. (3)
177 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
178 * left-most bit is numbered 0) to zero.
179 */
180 bcopy(digest, ret, 8);
181 ret[0] &= ~EUI64_UBIT;
182
183 /*
184 * XXX: we'd like to ensure that the generated value is not zero
185 * for simplicity. If the caclculated digest happens to be zero,
186 * use a random non-zero value as the last resort.
187 */
188 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
189 log(LOG_INFO,
190 "generate_tmp_ifid: computed MD5 value is zero.\n");
191
192 microtime(&tv);
193 val32 = krandom() ^ tv.tv_usec;
194 val32 = 1 + (val32 % (0xffffffff - 1));
195 }
196
197 /*
198 * RFC 3041 3.2.1. (4)
199 * Take the rightmost 64-bits of the MD5 digest and save them in
200 * stable storage as the history value to be used in the next
201 * iteration of the algorithm.
202 */
203 bcopy(&digest[8], seed0, 8);
204
205 if (0) { /* for debugging purposes only */
206 int i;
207
208 kprintf("to: ");
209 for (i = 0; i < 16; i++)
210 kprintf("%02x", digest[i]);
211 kprintf("\n");
212 }
213
214 return 0;
215 }
216
217 /*
218 * Get interface identifier for the specified interface.
219 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
220 */
221 static int
222 get_hw_ifid(struct ifnet *ifp,
223 struct in6_addr *in6) /* upper 64bits are preserved */
224 {
225 struct ifaddr_container *ifac;
226 struct sockaddr_dl *sdl;
227 u_int8_t *addr;
228 size_t addrlen;
229 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
230 static u_int8_t allone[8] =
231 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
232
233 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
234 struct ifaddr *ifa = ifac->ifa;
235
236 if (ifa->ifa_addr->sa_family != AF_LINK)
237 continue;
238 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
239 if (sdl == NULL)
240 continue;
241 if (sdl->sdl_alen == 0)
242 continue;
243
244 goto found;
245 }
246
247 return -1;
248
249 found:
250 addr = LLADDR(sdl);
251 addrlen = sdl->sdl_alen;
252
253 /* get EUI64 */
254 switch (ifp->if_type) {
255 case IFT_ETHER:
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 case IFT_GIF:
295 #ifdef IFT_STF
296 case IFT_STF:
297 #endif
298 /*
299 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
300 * however, IPv4 address is not very suitable as unique
301 * identifier source (can be renumbered).
302 * we don't do this.
303 */
304 return -1;
305
306 default:
307 return -1;
308 }
309
310 /* sanity check: g bit must not indicate "group" */
311 if (EUI64_GROUP(in6))
312 return -1;
313
314 /* convert EUI64 into IPv6 interface identifier */
315 EUI64_TO_IFID(in6);
316
317 /*
318 * sanity check: ifid must not be all zero, avoid conflict with
319 * subnet router anycast
320 */
321 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
322 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
323 return -1;
324 }
325
326 return 0;
327 }
328
329 /*
330 * Get interface identifier for the specified interface. If it is not
331 * available on ifp0, borrow interface identifier from other information
332 * sources.
333 */
334 static int
335 get_ifid(struct ifnet *ifp0,
336 struct ifnet *altifp, /* secondary EUI64 source */
337 struct in6_addr *in6)
338 {
339 struct ifnet *ifp;
340
341 /* first, try to get it from the interface itself */
342 if (get_hw_ifid(ifp0, in6) == 0) {
343 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
344 if_name(ifp0)));
345 goto success;
346 }
347
348 /* try secondary EUI64 source. this basically is for ATM PVC */
349 if (altifp && get_hw_ifid(altifp, in6) == 0) {
350 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
351 if_name(ifp0), if_name(altifp)));
352 goto success;
353 }
354
355 /* next, try to get it from some other hardware interface */
356 TAILQ_FOREACH(ifp, &ifnet, if_list) {
357 if (ifp == ifp0)
358 continue;
359 if (get_hw_ifid(ifp, in6) != 0)
360 continue;
361
362 /*
363 * to borrow ifid from other interface, ifid needs to be
364 * globally unique
365 */
366 if (IFID_UNIVERSAL(in6)) {
367 nd6log((LOG_DEBUG,
368 "%s: borrow interface identifier from %s\n",
369 if_name(ifp0), if_name(ifp)));
370 goto success;
371 }
372 }
373
374 /* last resort: get from random number source */
375 if (get_rand_ifid(ifp, in6) == 0) {
376 nd6log((LOG_DEBUG,
377 "%s: interface identifier generated by random number\n",
378 if_name(ifp0)));
379 goto success;
380 }
381
382 kprintf("%s: failed to get interface identifier\n", if_name(ifp0));
383 return -1;
384
385 success:
386 nd6log((LOG_INFO, "%s: ifid: "
387 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
388 if_name(ifp0),
389 in6->s6_addr[8], in6->s6_addr[9],
390 in6->s6_addr[10], in6->s6_addr[11],
391 in6->s6_addr[12], in6->s6_addr[13],
392 in6->s6_addr[14], in6->s6_addr[15]));
393 return 0;
394 }
395
396 static int
397 in6_ifattach_linklocal(struct ifnet *ifp,
398 struct ifnet *altifp) /* secondary EUI64 source */
399 {
400 struct in6_ifaddr *ia;
401 struct in6_aliasreq ifra;
402 struct nd_prefix pr0;
403 int i, error;
404
405 /*
406 * configure link-local address.
407 */
408 bzero(&ifra, sizeof(ifra));
409
410 /*
411 * in6_update_ifa() does not use ifra_name, but we accurately set it
412 * for safety.
413 */
414 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
415
416 ifra.ifra_addr.sin6_family = AF_INET6;
417 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
418 ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
419 ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); /* XXX */
420 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
421 if (ifp->if_flags & IFF_LOOPBACK) {
422 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
423 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
424 } else {
425 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
426 nd6log((LOG_ERR,
427 "%s: no ifid available\n", if_name(ifp)));
428 return -1;
429 }
430 }
431
432 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
433 ifra.ifra_prefixmask.sin6_family = AF_INET6;
434 ifra.ifra_prefixmask.sin6_addr = in6mask64;
435
436 /* link-local addresses should NEVER expire. */
437 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
438 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
439
440 /*
441 * Do not let in6_update_ifa() do DAD, since we need a random delay
442 * before sending an NS at the first time the interface becomes up.
443 * Instead, in6_if_up() will start DAD with a proper random delay.
444 */
445 ifra.ifra_flags |= IN6_IFF_NODAD;
446
447 /*
448 * Now call in6_update_ifa() to do a bunch of procedures to configure
449 * a link-local address. We can set NULL to the 3rd argument, because
450 * we know there's no other link-local address on the interface
451 * and therefore we are adding one (instead of updating one).
452 */
453 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
454 /*
455 * XXX: When the interface does not support IPv6, this call
456 * would fail in the SIOCSIFADDR ioctl. I believe the
457 * notification is rather confusing in this case, so just
458 * supress it. (jinmei@kame.net 20010130)
459 */
460 if (error != EAFNOSUPPORT)
461 log(LOG_NOTICE, "in6_ifattach_linklocal: failed to "
462 "configure a link-local address on %s "
463 "(errno=%d)\n",
464 if_name(ifp), error);
465 return (-1);
466 }
467
468 /*
469 * Adjust ia6_flags so that in6_if_up will perform DAD.
470 * XXX: Some P2P interfaces seem not to send packets just after
471 * becoming up, so we skip p2p interfaces for safety.
472 */
473 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
474 #ifdef DIAGNOSTIC
475 if (!ia) {
476 panic("ia == NULL in in6_ifattach_linklocal");
477 /* NOTREACHED */
478 }
479 #endif
480 if (in6if_do_dad(ifp) && !(ifp->if_flags & IFF_POINTOPOINT)) {
481 ia->ia6_flags &= ~IN6_IFF_NODAD;
482 ia->ia6_flags |= IN6_IFF_TENTATIVE;
483 }
484
485 /*
486 * Make the link-local prefix (fe80::/64%link) 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_mask = ifra.ifra_prefixmask.sin6_addr;
497 pr0.ndpr_prefix = ifra.ifra_addr;
498 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
499 for (i = 0; i < 4; i++) {
500 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
501 in6mask64.s6_addr32[i];
502 }
503 /*
504 * Initialize parameters. The link-local prefix must always be
505 * on-link, and its lifetimes never expire.
506 */
507 pr0.ndpr_raf_onlink = 1;
508 pr0.ndpr_raf_auto = 1; /* probably meaningless */
509 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
510 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
511 /*
512 * Since there is no other link-local addresses, nd6_prefix_lookup()
513 * probably returns NULL. However, we cannot always expect the result.
514 * For example, if we first remove the (only) existing link-local
515 * address, and then reconfigure another one, the prefix is still
516 * valid with referring to the old link-local address.
517 */
518 if (nd6_prefix_lookup(&pr0) == NULL) {
519 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
520 return (error);
521 }
522
523 return 0;
524 }
525
526 static int
527 in6_ifattach_loopback(struct ifnet *ifp) /* must be IFT_LOOP */
528 {
529 struct in6_aliasreq ifra;
530 int error;
531
532 bzero(&ifra, sizeof(ifra));
533
534 /*
535 * in6_update_ifa() does not use ifra_name, but we accurately set it
536 * for safety.
537 */
538 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
539
540 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
541 ifra.ifra_prefixmask.sin6_family = AF_INET6;
542 ifra.ifra_prefixmask.sin6_addr = in6mask128;
543
544 /*
545 * Always initialize ia_dstaddr (= broadcast address) to loopback
546 * address. Follows IPv4 practice - see in_ifinit().
547 */
548 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
549 ifra.ifra_dstaddr.sin6_family = AF_INET6;
550 ifra.ifra_dstaddr.sin6_addr = kin6addr_loopback;
551
552 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
553 ifra.ifra_addr.sin6_family = AF_INET6;
554 ifra.ifra_addr.sin6_addr = kin6addr_loopback;
555
556 /* the loopback address should NEVER expire. */
557 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
558 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
559
560 /* we don't need to perform DAD on loopback interfaces. */
561 ifra.ifra_flags |= IN6_IFF_NODAD;
562
563 /* skip registration to the prefix list. XXX should be temporary. */
564 ifra.ifra_flags |= IN6_IFF_NOPFX;
565
566 /*
567 * We are sure that this is a newly assigned address, so we can set
568 * NULL to the 3rd arg.
569 */
570 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
571 log(LOG_ERR, "in6_ifattach_loopback: failed to configure "
572 "the loopback address on %s (errno=%d)\n",
573 if_name(ifp), error);
574 return (-1);
575 }
576
577 return 0;
578 }
579
580 /*
581 * compute NI group address, based on the current hostname setting.
582 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
583 *
584 * when ifp == NULL, the caller is responsible for filling scopeid.
585 */
586 int
587 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
588 struct in6_addr *in6)
589 {
590 const char *p;
591 u_char *q;
592 MD5_CTX ctxt;
593 u_int8_t digest[16];
594 char l;
595 char n[64]; /* a single label must not exceed 63 chars */
596
597 if (!namelen || !name)
598 return -1;
599
600 p = name;
601 while (p && *p && *p != '.' && p - name < namelen)
602 p++;
603 if (p - name > sizeof(n) - 1)
604 return -1; /* label too long */
605 l = p - name;
606 strncpy(n, name, l);
607 n[(int)l] = '\0';
608 for (q = n; *q; q++) {
609 if ('A' <= *q && *q <= 'Z')
610 *q = *q - 'A' + 'a';
611 }
612
613 /* generate 8 bytes of pseudo-random value. */
614 bzero(&ctxt, sizeof(ctxt));
615 MD5Init(&ctxt);
616 MD5Update(&ctxt, &l, sizeof(l));
617 MD5Update(&ctxt, n, l);
618 MD5Final(digest, &ctxt);
619
620 bzero(in6, sizeof(*in6));
621 in6->s6_addr16[0] = htons(0xff02);
622 if (ifp)
623 in6->s6_addr16[1] = htons(ifp->if_index);
624 in6->s6_addr8[11] = 2;
625 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
626
627 return 0;
628 }
629
630 void
631 in6_nigroup_attach(const char *name, int namelen)
632 {
633 struct ifnet *ifp;
634 struct sockaddr_in6 mltaddr;
635 struct in6_multi *in6m;
636 int error;
637
638 bzero(&mltaddr, sizeof(mltaddr));
639 mltaddr.sin6_family = AF_INET6;
640 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
641 if (in6_nigroup(NULL, name, namelen, &mltaddr.sin6_addr) != 0)
642 return;
643
644 TAILQ_FOREACH(ifp, &ifnet, if_list) {
645 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
646 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
647 if (!in6m) {
648 if (!in6_addmulti(&mltaddr.sin6_addr, ifp, &error)) {
649 nd6log((LOG_ERR, "%s: failed to join %s "
650 "(errno=%d)\n", if_name(ifp),
651 ip6_sprintf(&mltaddr.sin6_addr),
652 error));
653 }
654 }
655 }
656 }
657
658 void
659 in6_nigroup_detach(const char *name, int namelen)
660 {
661 struct ifnet *ifp;
662 struct sockaddr_in6 mltaddr;
663 struct in6_multi *in6m;
664
665 bzero(&mltaddr, sizeof(mltaddr));
666 mltaddr.sin6_family = AF_INET6;
667 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
668 if (in6_nigroup(NULL, name, namelen, &mltaddr.sin6_addr) != 0)
669 return;
670
671 TAILQ_FOREACH(ifp, &ifnet, if_list) {
672 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
673 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
674 if (in6m)
675 in6_delmulti(in6m);
676 }
677 }
678
679 /*
680 * XXX multiple loopback interface needs more care. for instance,
681 * nodelocal address needs to be configured onto only one of them.
682 * XXX multiple link-local address case
683 */
684 void
685 in6_ifattach(struct ifnet *ifp,
686 struct ifnet *altifp) /* secondary EUI64 source */
687 {
688 struct in6_ifaddr *ia;
689 struct in6_addr in6;
690
691 /* some of the interfaces are inherently not IPv6 capable */
692 switch (ifp->if_type) {
693 #ifdef IFT_BRIDGE /*OpenBSD 2.8*/
694 case IFT_BRIDGE:
695 return;
696 #endif
697 case IFT_PFLOG:
698 case IFT_PFSYNC:
699 case IFT_CARP:
700 return;
701 }
702
703 /*
704 * quirks based on interface type
705 */
706 switch (ifp->if_type) {
707 #ifdef IFT_STF
708 case IFT_STF:
709 /*
710 * 6to4 interface is a very special kind of beast.
711 * no multicast, no linklocal. RFC2529 specifies how to make
712 * linklocals for 6to4 interface, but there's no use and
713 * it is rather harmful to have one.
714 */
715 goto statinit;
716 #endif
717 default:
718 break;
719 }
720
721 /*
722 * usually, we require multicast capability to the interface
723 */
724 if (!(ifp->if_flags & IFF_MULTICAST)) {
725 log(LOG_INFO, "in6_ifattach: "
726 "%s is not multicast capable, IPv6 not enabled\n",
727 if_name(ifp));
728 return;
729 }
730
731 /*
732 * assign loopback address for loopback interface.
733 * XXX multiple loopback interface case.
734 */
735 if (ifp->if_flags & IFF_LOOPBACK) {
736 in6 = kin6addr_loopback;
737 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
738 if (in6_ifattach_loopback(ifp) != 0)
739 return;
740 }
741 }
742
743 /*
744 * assign a link-local address, if there's none.
745 */
746 if (ip6_auto_linklocal) {
747 ia = in6ifa_ifpforlinklocal(ifp, 0);
748 if (ia == NULL) {
749 if (in6_ifattach_linklocal(ifp, altifp) == 0) {
750 /* linklocal address assigned */
751 } else {
752 /* failed to assign linklocal address. bark? */
753 }
754 }
755 }
756
757 #ifdef IFT_STF /* XXX */
758 statinit:
759 #endif
760
761 /* update dynamically. */
762 if (in6_maxmtu < ifp->if_mtu)
763 in6_maxmtu = ifp->if_mtu;
764 }
765
766 /*
767 * NOTE: in6_ifdetach() does not support loopback if at this moment.
768 * We don't need this function in bsdi, because interfaces are never removed
769 * from the ifnet list in bsdi.
770 */
771 void
772 in6_ifdetach(struct ifnet *ifp)
773 {
774 struct in6_ifaddr *ia, *oia;
775 struct ifaddr_container *ifac, *next;
776 struct rtentry *rt;
777 short rtflags;
778 struct sockaddr_in6 sin6;
779 struct in6_multi *in6m;
780 struct in6_multi *in6m_next;
781
782 /* nuke prefix list. this may try to remove some of ifaddrs as well */
783 in6_purgeprefix(ifp);
784
785 /* remove neighbor management table */
786 nd6_purge(ifp);
787
788 /* nuke any of IPv6 addresses we have */
789 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid], ifa_link, next) {
790 struct ifaddr *ifa = ifac->ifa;
791
792 if (ifa->ifa_addr->sa_family != AF_INET6)
793 continue;
794 in6_purgeaddr(ifa);
795 }
796
797 /* undo everything done by in6_ifattach(), just in case */
798 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid], ifa_link, next) {
799 struct ifaddr *ifa = ifac->ifa;
800
801 if (ifa->ifa_addr->sa_family != AF_INET6
802 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
803 continue;
804 }
805
806 ia = (struct in6_ifaddr *)ifa;
807
808 /* remove from the routing table */
809 if ((ia->ia_flags & IFA_ROUTE) &&
810 (rt = rtpurelookup((struct sockaddr *)&ia->ia_addr))) {
811 rtflags = rt->rt_flags;
812 --rt->rt_refcnt;
813 rtrequest(RTM_DELETE,
814 (struct sockaddr *)&ia->ia_addr,
815 (struct sockaddr *)&ia->ia_addr,
816 (struct sockaddr *)&ia->ia_prefixmask,
817 rtflags, NULL);
818 }
819
820 /* remove from the linked list */
821 ifa_ifunlink((struct ifaddr *)ia, ifp);
822
823 /* also remove from the IPv6 address chain(itojun&jinmei) */
824 oia = ia;
825 if (oia == (ia = in6_ifaddr))
826 in6_ifaddr = ia->ia_next;
827 else {
828 while (ia->ia_next && (ia->ia_next != oia))
829 ia = ia->ia_next;
830 if (ia->ia_next)
831 ia->ia_next = oia->ia_next;
832 else {
833 nd6log((LOG_ERR,
834 "%s: didn't unlink in6ifaddr from "
835 "list\n", if_name(ifp)));
836 }
837 }
838
839 crit_enter(); /* XXX MP not MP safe */
840 _IFAFREE(&oia->ia_ifa, 0);
841 crit_exit();
842
843 ifa_destroy(&ia->ia_ifa);
844 }
845
846 /* leave from all multicast groups joined */
847 udbinfo_lock();
848 in6_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);
849 udbinfo_unlock();
850
851 in6_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
852 for (in6m = LIST_FIRST(&in6_multihead); in6m; in6m = in6m_next) {
853 in6m_next = LIST_NEXT(in6m, in6m_entry);
854 if (in6m->in6m_ifp != ifp)
855 continue;
856 in6_delmulti(in6m);
857 in6m = NULL;
858 }
859
860 /*
861 * remove neighbor management table. we call it twice just to make
862 * sure we nuke everything. maybe we need just one call.
863 * XXX: since the first call did not release addresses, some prefixes
864 * might remain. We should call nd6_purge() again to release the
865 * prefixes after removing all addresses above.
866 * (Or can we just delay calling nd6_purge until at this point?)
867 */
868 nd6_purge(ifp);
869
870 /* remove route to link-local allnodes multicast (ff02::1) */
871 bzero(&sin6, sizeof(sin6));
872 sin6.sin6_len = sizeof(struct sockaddr_in6);
873 sin6.sin6_family = AF_INET6;
874 sin6.sin6_addr = kin6addr_linklocal_allnodes;
875 sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
876 rt = rtpurelookup((struct sockaddr *)&sin6);
877 if (rt != NULL && rt->rt_ifp == ifp) {
878 --rt->rt_refcnt;
879 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
880 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
881 }
882 }
883
884 void
885 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf, const u_int8_t *baseid,
886 int generate)
887 {
888 u_int8_t nullbuf[8];
889 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
890
891 bzero(nullbuf, sizeof(nullbuf));
892 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
893 /* we've never created a random ID. Create a new one. */
894 generate = 1;
895 }
896
897 if (generate) {
898 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
899
900 /* generate_tmp_ifid will update seedn and buf */
901 generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
902 ndi->randomid);
903 }
904 bcopy(ndi->randomid, retbuf, 8);
905 }
906
907 void
908 in6_tmpaddrtimer(void *ignored_arg)
909 {
910 struct nd_ifinfo *ndi;
911 u_int8_t nullbuf[8];
912 struct ifnet *ifp;
913
914 crit_enter();
915
916 callout_reset(&in6_tmpaddrtimer_ch,
917 (ip6_temp_preferred_lifetime - ip6_desync_factor -
918 ip6_temp_regen_advance) * hz,
919 in6_tmpaddrtimer, NULL);
920
921 bzero(nullbuf, sizeof(nullbuf));
922 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
923 if (ifp->if_afdata[AF_INET6] == NULL)
924 continue;
925 ndi = ND_IFINFO(ifp);
926 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
927 /*
928 * We've been generating a random ID on this interface.
929 * Create a new one.
930 */
931 generate_tmp_ifid(ndi->randomseed0,
932 ndi->randomseed1,
933 ndi->randomid);
934 }
935 }
936
937 crit_exit();
938 }
Cache object: 9131e765c6d34e2bb2f1bd03d204e11b
|