in6_ifattach.c revision 1.55 1 /* $NetBSD: in6_ifattach.c,v 1.55 2003/07/08 10:20:45 itojun 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.55 2003/07/08 10:20:45 itojun 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/ip6_var.h>
55 #include <netinet6/in6_ifattach.h>
56 #include <netinet6/ip6_var.h>
57 #include <netinet6/nd6.h>
58 #include <netinet6/ip6_mroute.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 static int get_rand_ifid __P((struct ifnet *, struct in6_addr *));
67 static int get_hw_ifid __P((struct ifnet *, struct in6_addr *));
68 static int get_ifid __P((struct ifnet *, struct ifnet *, struct in6_addr *));
69 static int in6_ifattach_linklocal __P((struct ifnet *, struct ifnet *));
70 static int in6_ifattach_loopback __P((struct ifnet *));
71
72 #define EUI64_GBIT 0x01
73 #define EUI64_UBIT 0x02
74 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (/*CONSTCOND*/ 0)
75 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
76 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
77 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
78 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
79
80 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
81 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
82
83 /*
84 * Generate a last-resort interface identifier, when the machine has no
85 * IEEE802/EUI64 address sources.
86 * The goal here is to get an interface identifier that is
87 * (1) random enough and (2) does not change across reboot.
88 * We currently use MD5(hostname) for it.
89 */
90 static int
91 get_rand_ifid(ifp, in6)
92 struct ifnet *ifp;
93 struct in6_addr *in6; /* upper 64bits are preserved */
94 {
95 MD5_CTX ctxt;
96 u_int8_t digest[16];
97
98 #if 0
99 /* we need at least several letters as seed for ifid */
100 if (hostnamelen < 3)
101 return -1;
102 #endif
103
104 /* generate 8 bytes of pseudo-random value. */
105 bzero(&ctxt, sizeof(ctxt));
106 MD5Init(&ctxt);
107 MD5Update(&ctxt, (u_char *)hostname, hostnamelen);
108 MD5Final(digest, &ctxt);
109
110 /* assumes sizeof(digest) > sizeof(ifid) */
111 bcopy(digest, &in6->s6_addr[8], 8);
112
113 /* make sure to set "u" bit to local, and "g" bit to individual. */
114 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
115 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
116
117 /* convert EUI64 into IPv6 interface identifier */
118 EUI64_TO_IFID(in6);
119
120 return 0;
121 }
122
123 /*
124 * Get interface identifier for the specified interface.
125 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
126 */
127 static int
128 get_hw_ifid(ifp, in6)
129 struct ifnet *ifp;
130 struct in6_addr *in6; /* upper 64bits are preserved */
131 {
132 struct ifaddr *ifa;
133 struct sockaddr_dl *sdl;
134 char *addr;
135 size_t addrlen;
136 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
137 static u_int8_t allone[8] =
138 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
139
140 for (ifa = ifp->if_addrlist.tqh_first;
141 ifa;
142 ifa = ifa->ifa_list.tqe_next)
143 {
144 if (ifa->ifa_addr->sa_family != AF_LINK)
145 continue;
146 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
147 if (sdl == NULL)
148 continue;
149 if (sdl->sdl_alen == 0)
150 continue;
151
152 goto found;
153 }
154
155 return -1;
156
157 found:
158 addr = LLADDR(sdl);
159 addrlen = sdl->sdl_alen;
160
161 switch (ifp->if_type) {
162 case IFT_IEEE1394:
163 case IFT_IEEE80211:
164 /* IEEE1394 uses 16byte length address starting with EUI64 */
165 if (addrlen > 8)
166 addrlen = 8;
167 break;
168 default:
169 break;
170 }
171
172 /* get EUI64 */
173 switch (ifp->if_type) {
174 /* IEEE802/EUI64 cases - what others? */
175 case IFT_ETHER:
176 case IFT_FDDI:
177 case IFT_ATM:
178 case IFT_IEEE1394:
179 case IFT_IEEE80211:
180 /* look at IEEE802/EUI64 only */
181 if (addrlen != 8 && addrlen != 6)
182 return -1;
183
184 /*
185 * check for invalid MAC address - on bsdi, we see it a lot
186 * since wildboar configures all-zero MAC on pccard before
187 * card insertion.
188 */
189 if (bcmp(addr, allzero, addrlen) == 0)
190 return -1;
191 if (bcmp(addr, allone, addrlen) == 0)
192 return -1;
193
194 /* make EUI64 address */
195 if (addrlen == 8)
196 bcopy(addr, &in6->s6_addr[8], 8);
197 else if (addrlen == 6) {
198 in6->s6_addr[8] = addr[0];
199 in6->s6_addr[9] = addr[1];
200 in6->s6_addr[10] = addr[2];
201 in6->s6_addr[11] = 0xff;
202 in6->s6_addr[12] = 0xfe;
203 in6->s6_addr[13] = addr[3];
204 in6->s6_addr[14] = addr[4];
205 in6->s6_addr[15] = addr[5];
206 }
207 break;
208
209 case IFT_ARCNET:
210 if (addrlen != 1)
211 return -1;
212 if (!addr[0])
213 return -1;
214
215 bzero(&in6->s6_addr[8], 8);
216 in6->s6_addr[15] = addr[0];
217
218 /*
219 * due to insufficient bitwidth, we mark it local.
220 */
221 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
222 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
223 break;
224
225 case IFT_GIF:
226 #ifdef IFT_STF
227 case IFT_STF:
228 #endif
229 /*
230 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
231 * however, IPv4 address is not very suitable as unique
232 * identifier source (can be renumbered).
233 * we don't do this.
234 */
235 return -1;
236
237 default:
238 return -1;
239 }
240
241 /* sanity check: g bit must not indicate "group" */
242 if (EUI64_GROUP(in6))
243 return -1;
244
245 /* convert EUI64 into IPv6 interface identifier */
246 EUI64_TO_IFID(in6);
247
248 /*
249 * sanity check: ifid must not be all zero, avoid conflict with
250 * subnet router anycast
251 */
252 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
253 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
254 return -1;
255 }
256
257 return 0;
258 }
259
260 /*
261 * Get interface identifier for the specified interface. If it is not
262 * available on ifp0, borrow interface identifier from other information
263 * sources.
264 */
265 static int
266 get_ifid(ifp0, altifp, in6)
267 struct ifnet *ifp0;
268 struct ifnet *altifp; /*secondary EUI64 source*/
269 struct in6_addr *in6;
270 {
271 struct ifnet *ifp;
272
273 /* first, try to get it from the interface itself */
274 if (get_hw_ifid(ifp0, in6) == 0) {
275 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
276 if_name(ifp0)));
277 goto success;
278 }
279
280 /* try secondary EUI64 source. this basically is for ATM PVC */
281 if (altifp && get_hw_ifid(altifp, in6) == 0) {
282 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
283 if_name(ifp0), if_name(altifp)));
284 goto success;
285 }
286
287 /* next, try to get it from some other hardware interface */
288 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
289 {
290 if (ifp == ifp0)
291 continue;
292 if (get_hw_ifid(ifp, in6) != 0)
293 continue;
294
295 /*
296 * to borrow ifid from other interface, ifid needs to be
297 * globally unique
298 */
299 if (IFID_UNIVERSAL(in6)) {
300 nd6log((LOG_DEBUG,
301 "%s: borrow interface identifier from %s\n",
302 if_name(ifp0), if_name(ifp)));
303 goto success;
304 }
305 }
306
307 /* last resort: get from random number source */
308 if (get_rand_ifid(ifp, in6) == 0) {
309 nd6log((LOG_DEBUG,
310 "%s: interface identifier generated by random number\n",
311 if_name(ifp0)));
312 goto success;
313 }
314
315 printf("%s: failed to get interface identifier\n", if_name(ifp0));
316 return -1;
317
318 success:
319 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
320 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
321 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
322 in6->s6_addr[14], in6->s6_addr[15]));
323 return 0;
324 }
325
326 static int
327 in6_ifattach_linklocal(ifp, altifp)
328 struct ifnet *ifp;
329 struct ifnet *altifp; /*secondary EUI64 source*/
330 {
331 struct in6_ifaddr *ia;
332 struct in6_aliasreq ifra;
333 struct nd_prefix pr0;
334 int i, error;
335
336 /*
337 * configure link-local address.
338 */
339 bzero(&ifra, sizeof(ifra));
340
341 /*
342 * in6_update_ifa() does not use ifra_name, but we accurately set it
343 * for safety.
344 */
345 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
346
347 ifra.ifra_addr.sin6_family = AF_INET6;
348 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
349 ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
350 ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
351 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
352 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
353 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
354 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
355 } else {
356 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
357 nd6log((LOG_ERR,
358 "%s: no ifid available\n", if_name(ifp)));
359 return (-1);
360 }
361 }
362
363 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
364 ifra.ifra_prefixmask.sin6_family = AF_INET6;
365 ifra.ifra_prefixmask.sin6_addr = in6mask64;
366 #ifdef SCOPEDROUTING
367 /* take into account the sin6_scope_id field for routing */
368 ifra.ifra_prefixmask.sin6_scope_id = 0xffffffff;
369 #endif
370 /* link-local addresses should NEVER expire. */
371 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
372 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
373
374 /*
375 * Do not let in6_update_ifa() do DAD, since we need a random delay
376 * before sending an NS at the first time the interface becomes up.
377 * Instead, in6_if_up() will start DAD with a proper random delay.
378 */
379 ifra.ifra_flags |= IN6_IFF_NODAD;
380
381 /*
382 * Now call in6_update_ifa() to do a bunch of procedures to configure
383 * a link-local address. We can set NULL to the 3rd argument, because
384 * we know there's no other link-local address on the interface
385 * and therefore we are adding one (instead of updating one).
386 */
387 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
388 /*
389 * XXX: When the interface does not support IPv6, this call
390 * would fail in the SIOCSIFADDR ioctl. I believe the
391 * notification is rather confusing in this case, so just
392 * suppress it. (jinmei (at) kame.net 20010130)
393 */
394 if (error != EAFNOSUPPORT)
395 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
396 "configure a link-local address on %s "
397 "(errno=%d)\n",
398 if_name(ifp), error));
399 return (-1);
400 }
401
402 /*
403 * Adjust ia6_flags so that in6_if_up will perform DAD.
404 * XXX: Some P2P interfaces seem not to send packets just after
405 * becoming up, so we skip p2p interfaces for safety.
406 */
407 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
408 #ifdef DIAGNOSTIC
409 if (!ia) {
410 panic("ia == NULL in in6_ifattach_linklocal");
411 /* NOTREACHED */
412 }
413 #endif
414 if (in6if_do_dad(ifp) && (ifp->if_flags & IFF_POINTOPOINT) == 0) {
415 ia->ia6_flags &= ~IN6_IFF_NODAD;
416 ia->ia6_flags |= IN6_IFF_TENTATIVE;
417 }
418
419 /*
420 * Make the link-local prefix (fe80::/64%link) as on-link.
421 * Since we'd like to manage prefixes separately from addresses,
422 * we make an ND6 prefix structure for the link-local prefix,
423 * and add it to the prefix list as a never-expire prefix.
424 * XXX: this change might affect some existing code base...
425 */
426 bzero(&pr0, sizeof(pr0));
427 pr0.ndpr_ifp = ifp;
428 /* this should be 64 at this moment. */
429 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
430 pr0.ndpr_mask = ifra.ifra_prefixmask.sin6_addr;
431 pr0.ndpr_prefix = ifra.ifra_addr;
432 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
433 for (i = 0; i < 4; i++) {
434 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
435 in6mask64.s6_addr32[i];
436 }
437 /*
438 * Initialize parameters. The link-local prefix must always be
439 * on-link, and its lifetimes never expire.
440 */
441 pr0.ndpr_raf_onlink = 1;
442 pr0.ndpr_raf_auto = 1; /* probably meaningless */
443 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
444 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
445 /*
446 * Since there is no other link-local addresses, nd6_prefix_lookup()
447 * probably returns NULL. However, we cannot always expect the result.
448 * For example, if we first remove the (only) existing link-local
449 * address, and then reconfigure another one, the prefix is still
450 * valid with referring to the old link-local address.
451 */
452 if (nd6_prefix_lookup(&pr0) == NULL) {
453 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
454 return (error);
455 }
456
457 return 0;
458 }
459
460 static int
461 in6_ifattach_loopback(ifp)
462 struct ifnet *ifp; /* must be IFT_LOOP */
463 {
464 struct in6_aliasreq ifra;
465 int error;
466
467 bzero(&ifra, sizeof(ifra));
468
469 /*
470 * in6_update_ifa() does not use ifra_name, but we accurately set it
471 * for safety.
472 */
473 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
474
475 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
476 ifra.ifra_prefixmask.sin6_family = AF_INET6;
477 ifra.ifra_prefixmask.sin6_addr = in6mask128;
478
479 /*
480 * Always initialize ia_dstaddr (= broadcast address) to loopback
481 * address. Follows IPv4 practice - see in_ifinit().
482 */
483 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
484 ifra.ifra_dstaddr.sin6_family = AF_INET6;
485 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
486
487 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
488 ifra.ifra_addr.sin6_family = AF_INET6;
489 ifra.ifra_addr.sin6_addr = in6addr_loopback;
490
491 /* the loopback address should NEVER expire. */
492 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
493 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
494
495 /* we don't need to perform DAD on loopback interfaces. */
496 ifra.ifra_flags |= IN6_IFF_NODAD;
497
498 /*
499 * We are sure that this is a newly assigned address, so we can set
500 * NULL to the 3rd arg.
501 */
502 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
503 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
504 "the loopback address on %s (errno=%d)\n",
505 if_name(ifp), error));
506 return (-1);
507 }
508
509 return 0;
510 }
511
512 /*
513 * compute NI group address, based on the current hostname setting.
514 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
515 *
516 * when ifp == NULL, the caller is responsible for filling scopeid.
517 */
518 int
519 in6_nigroup(ifp, name, namelen, sa6)
520 struct ifnet *ifp;
521 const char *name;
522 int namelen;
523 struct sockaddr_in6 *sa6;
524 {
525 const char *p;
526 u_int8_t *q;
527 MD5_CTX ctxt;
528 u_int8_t digest[16];
529 u_int8_t l;
530 u_int8_t n[64]; /* a single label must not exceed 63 chars */
531
532 if (!namelen || !name)
533 return -1;
534
535 p = name;
536 while (p && *p && *p != '.' && p - name < namelen)
537 p++;
538 if (p - name > sizeof(n) - 1)
539 return -1; /* label too long */
540 l = p - name;
541 strncpy((char *)n, name, l);
542 n[(int)l] = '\0';
543 for (q = n; *q; q++) {
544 if ('A' <= *q && *q <= 'Z')
545 *q = *q - 'A' + 'a';
546 }
547
548 /* generate 8 bytes of pseudo-random value. */
549 bzero(&ctxt, sizeof(ctxt));
550 MD5Init(&ctxt);
551 MD5Update(&ctxt, &l, sizeof(l));
552 MD5Update(&ctxt, n, l);
553 MD5Final(digest, &ctxt);
554
555 bzero(sa6, sizeof(*sa6));
556 sa6->sin6_family = AF_INET6;
557 sa6->sin6_len = sizeof(*sa6);
558 sa6->sin6_addr.s6_addr16[0] = htons(0xff02);
559 sa6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
560 sa6->sin6_addr.s6_addr8[11] = 2;
561 bcopy(digest, &sa6->sin6_addr.s6_addr32[3],
562 sizeof(sa6->sin6_addr.s6_addr32[3]));
563
564 return 0;
565 }
566
567 /*
568 * XXX multiple loopback interface needs more care. for instance,
569 * nodelocal address needs to be configured onto only one of them.
570 * XXX multiple link-local address case
571 */
572 void
573 in6_ifattach(ifp, altifp)
574 struct ifnet *ifp;
575 struct ifnet *altifp; /* secondary EUI64 source */
576 {
577 struct in6_ifaddr *ia;
578 struct in6_addr in6;
579
580 /* some of the interfaces are inherently not IPv6 capable */
581 switch (ifp->if_type) {
582 case IFT_BRIDGE:
583 return;
584 }
585
586 /*
587 * if link mtu is too small, don't try to configure IPv6.
588 * remember there could be some link-layer that has special
589 * fragmentation logic.
590 */
591 if (ifp->if_mtu < IPV6_MMTU) {
592 nd6log((LOG_INFO, "in6_ifattach: "
593 "%s has too small MTU, IPv6 not enabled\n",
594 if_name(ifp)));
595 return;
596 }
597
598 /* create a multicast kludge storage (if we have not had one) */
599 in6_createmkludge(ifp);
600
601 /*
602 * quirks based on interface type
603 */
604 switch (ifp->if_type) {
605 #ifdef IFT_STF
606 case IFT_STF:
607 /*
608 * 6to4 interface is a very special kind of beast.
609 * no multicast, no linklocal. RFC2529 specifies how to make
610 * linklocals for 6to4 interface, but there's no use and
611 * it is rather harmful to have one.
612 */
613 return;
614 #endif
615 default:
616 break;
617 }
618
619 /*
620 * usually, we require multicast capability to the interface
621 */
622 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
623 nd6log((LOG_INFO, "in6_ifattach: "
624 "%s is not multicast capable, IPv6 not enabled\n",
625 if_name(ifp)));
626 return;
627 }
628
629 /*
630 * assign loopback address for loopback interface.
631 * XXX multiple loopback interface case.
632 */
633 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
634 in6 = in6addr_loopback;
635 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
636 if (in6_ifattach_loopback(ifp) != 0)
637 return;
638 }
639 }
640
641 /*
642 * assign a link-local address, if there's none.
643 */
644 if (ip6_auto_linklocal) {
645 ia = in6ifa_ifpforlinklocal(ifp, 0);
646 if (ia == NULL) {
647 if (in6_ifattach_linklocal(ifp, altifp) == 0) {
648 /* linklocal address assigned */
649 } else {
650 /* failed to assign linklocal address. bark? */
651 }
652 }
653 }
654 }
655
656 /*
657 * NOTE: in6_ifdetach() does not support loopback if at this moment.
658 * We don't need this function in bsdi, because interfaces are never removed
659 * from the ifnet list in bsdi.
660 */
661 void
662 in6_ifdetach(ifp)
663 struct ifnet *ifp;
664 {
665 struct in6_ifaddr *ia, *oia;
666 struct ifaddr *ifa, *next;
667 struct rtentry *rt;
668 short rtflags;
669 struct sockaddr_in6 sin6;
670 struct in6_multi_mship *imm;
671
672 /* remove ip6_mrouter stuff */
673 ip6_mrouter_detach(ifp);
674
675 /* remove neighbor management table */
676 nd6_purge(ifp);
677
678 /* nuke any of IPv6 addresses we have */
679 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
680 {
681 next = ifa->ifa_list.tqe_next;
682 if (ifa->ifa_addr->sa_family != AF_INET6)
683 continue;
684 in6_purgeaddr(ifa);
685 }
686
687 /* undo everything done by in6_ifattach(), just in case */
688 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
689 {
690 next = ifa->ifa_list.tqe_next;
691
692 if (ifa->ifa_addr->sa_family != AF_INET6
693 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
694 continue;
695 }
696
697 ia = (struct in6_ifaddr *)ifa;
698
699 /*
700 * leave from multicast groups we have joined for the interface
701 */
702 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
703 LIST_REMOVE(imm, i6mm_chain);
704 in6_leavegroup(imm);
705 }
706
707 /* remove from the routing table */
708 if ((ia->ia_flags & IFA_ROUTE) &&
709 (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
710 rtflags = rt->rt_flags;
711 rtfree(rt);
712 rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
713 (struct sockaddr *)&ia->ia_addr,
714 (struct sockaddr *)&ia->ia_prefixmask,
715 rtflags, (struct rtentry **)0);
716 }
717
718 /* remove from the linked list */
719 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
720 IFAFREE(&ia->ia_ifa);
721
722 /* also remove from the IPv6 address chain(itojun&jinmei) */
723 oia = ia;
724 if (oia == (ia = in6_ifaddr))
725 in6_ifaddr = ia->ia_next;
726 else {
727 while (ia->ia_next && (ia->ia_next != oia))
728 ia = ia->ia_next;
729 if (ia->ia_next)
730 ia->ia_next = oia->ia_next;
731 else {
732 nd6log((LOG_ERR,
733 "%s: didn't unlink in6ifaddr from list\n",
734 if_name(ifp)));
735 }
736 }
737
738 IFAFREE(&oia->ia_ifa);
739 }
740
741 /* cleanup multicast address kludge table, if there is any */
742 in6_purgemkludge(ifp);
743
744 /*
745 * remove neighbor management table. we call it twice just to make
746 * sure we nuke everything. maybe we need just one call.
747 * XXX: since the first call did not release addresses, some prefixes
748 * might remain. We should call nd6_purge() again to release the
749 * prefixes after removing all addresses above.
750 * (Or can we just delay calling nd6_purge until at this point?)
751 */
752 nd6_purge(ifp);
753
754 /* remove route to link-local allnodes multicast (ff02::1) */
755 bzero(&sin6, sizeof(sin6));
756 sin6.sin6_len = sizeof(struct sockaddr_in6);
757 sin6.sin6_family = AF_INET6;
758 sin6.sin6_addr = in6addr_linklocal_allnodes;
759 sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
760 rt = rtalloc1((struct sockaddr *)&sin6, 0);
761 if (rt && rt->rt_ifp == ifp) {
762 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
763 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
764 rtfree(rt);
765 }
766 }
767