in6_ifattach.c revision 1.35 1 /* $NetBSD: in6_ifattach.c,v 1.35 2001/04/13 23:30:25 thorpej Exp $ */
2 /* $KAME: in6_ifattach.c,v 1.100 2001/02/07 08:25:45 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
42 #include <net/if.h>
43 #include <net/if_dl.h>
44 #include <net/if_types.h>
45 #include <net/route.h>
46
47 #include <netinet/in.h>
48 #include <netinet/in_var.h>
49
50 #include <netinet/ip6.h>
51 #include <netinet6/ip6_var.h>
52 #include <netinet6/in6_ifattach.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet6/nd6.h>
55
56 #include <net/net_osdep.h>
57
58 struct in6_ifstat **in6_ifstat = NULL;
59 struct icmp6_ifstat **icmp6_ifstat = NULL;
60 size_t in6_ifstatmax = 0;
61 size_t icmp6_ifstatmax = 0;
62 unsigned long in6_maxmtu = 0;
63
64 static int get_rand_ifid __P((struct ifnet *, struct in6_addr *));
65 static int get_hw_ifid __P((struct ifnet *, struct in6_addr *));
66 static int get_ifid __P((struct ifnet *, struct ifnet *, struct in6_addr *));
67 static int in6_ifattach_addaddr __P((struct ifnet *, struct in6_ifaddr *));
68 static int in6_ifattach_linklocal __P((struct ifnet *, struct ifnet *));
69 static int in6_ifattach_loopback __P((struct ifnet *));
70
71 #define EUI64_GBIT 0x01
72 #define EUI64_UBIT 0x02
73 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
74 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
75 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
76 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
77 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
78
79 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
80 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
81
82 /*
83 * Generate a last-resort interface identifier, when the machine has no
84 * IEEE802/EUI64 address sources.
85 * The goal here is to get an interface identifier that is
86 * (1) random enough and (2) does not change across reboot.
87 * We currently use MD5(hostname) for it.
88 */
89 static int
90 get_rand_ifid(ifp, in6)
91 struct ifnet *ifp;
92 struct in6_addr *in6; /*upper 64bits are preserved */
93 {
94 MD5_CTX ctxt;
95 u_int8_t digest[16];
96
97 #if 0
98 /* we need at least several letters as seed for ifid */
99 if (hostnamelen < 3)
100 return -1;
101 #endif
102
103 /* generate 8 bytes of pseudo-random value. */
104 bzero(&ctxt, sizeof(ctxt));
105 MD5Init(&ctxt);
106 MD5Update(&ctxt, hostname, hostnamelen);
107 MD5Final(digest, &ctxt);
108
109 /* assumes sizeof(digest) > sizeof(ifid) */
110 bcopy(digest, &in6->s6_addr[8], 8);
111
112 /* make sure to set "u" bit to local, and "g" bit to individual. */
113 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
114 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
115
116 /* convert EUI64 into IPv6 interface identifier */
117 EUI64_TO_IFID(in6);
118
119 return 0;
120 }
121
122 /*
123 * Get interface identifier for the specified interface.
124 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
125 */
126 static int
127 get_hw_ifid(ifp, in6)
128 struct ifnet *ifp;
129 struct in6_addr *in6; /*upper 64bits are preserved */
130 {
131 struct ifaddr *ifa;
132 struct sockaddr_dl *sdl;
133 u_int8_t *addr;
134 size_t addrlen;
135 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
136 static u_int8_t allone[8] =
137 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
138
139 for (ifa = ifp->if_addrlist.tqh_first;
140 ifa;
141 ifa = ifa->ifa_list.tqe_next)
142 {
143 if (ifa->ifa_addr->sa_family != AF_LINK)
144 continue;
145 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
146 if (sdl == NULL)
147 continue;
148 if (sdl->sdl_alen == 0)
149 continue;
150
151 goto found;
152 }
153
154 return -1;
155
156 found:
157 addr = LLADDR(sdl);
158 addrlen = sdl->sdl_alen;
159
160 /* get EUI64 */
161 switch (ifp->if_type) {
162 case IFT_ETHER:
163 case IFT_FDDI:
164 case IFT_ATM:
165 case IFT_IEEE1394:
166 /* IEEE802/EUI64 cases - what others? */
167 /* IEEE1394 uses 16byte length address starting with EUI64 */
168 if (addrlen > 8)
169 addrlen = 8;
170
171 /* look at IEEE802/EUI64 only */
172 if (addrlen != 8 && addrlen != 6)
173 return -1;
174
175 /*
176 * check for invalid MAC address - on bsdi, we see it a lot
177 * since wildboar configures all-zero MAC on pccard before
178 * card insertion.
179 */
180 if (bcmp(addr, allzero, addrlen) == 0)
181 return -1;
182 if (bcmp(addr, allone, addrlen) == 0)
183 return -1;
184
185 /* make EUI64 address */
186 if (addrlen == 8)
187 bcopy(addr, &in6->s6_addr[8], 8);
188 else if (addrlen == 6) {
189 in6->s6_addr[8] = addr[0];
190 in6->s6_addr[9] = addr[1];
191 in6->s6_addr[10] = addr[2];
192 in6->s6_addr[11] = 0xff;
193 in6->s6_addr[12] = 0xfe;
194 in6->s6_addr[13] = addr[3];
195 in6->s6_addr[14] = addr[4];
196 in6->s6_addr[15] = addr[5];
197 }
198 break;
199
200 case IFT_ARCNET:
201 if (addrlen != 1)
202 return -1;
203 if (!addr[0])
204 return -1;
205
206 bzero(&in6->s6_addr[8], 8);
207 in6->s6_addr[15] = addr[0];
208
209 /*
210 * due to insufficient bitwidth, we mark it local.
211 */
212 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
213 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
214 break;
215
216 case IFT_GIF:
217 #ifdef IFT_STF
218 case IFT_STF:
219 #endif
220 /*
221 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
222 * however, IPv4 address is not very suitable as unique
223 * identifier source (can be renumbered).
224 * we don't do this.
225 */
226 return -1;
227
228 default:
229 return -1;
230 }
231
232 /* sanity check: g bit must not indicate "group" */
233 if (EUI64_GROUP(in6))
234 return -1;
235
236 /* convert EUI64 into IPv6 interface identifier */
237 EUI64_TO_IFID(in6);
238
239 /*
240 * sanity check: ifid must not be all zero, avoid conflict with
241 * subnet router anycast
242 */
243 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
244 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
245 return -1;
246 }
247
248 return 0;
249 }
250
251 /*
252 * Get interface identifier for the specified interface. If it is not
253 * available on ifp0, borrow interface identifier from other information
254 * sources.
255 */
256 static int
257 get_ifid(ifp0, altifp, in6)
258 struct ifnet *ifp0;
259 struct ifnet *altifp; /*secondary EUI64 source*/
260 struct in6_addr *in6;
261 {
262 struct ifnet *ifp;
263
264 /* first, try to get it from the interface itself */
265 if (get_hw_ifid(ifp0, in6) == 0) {
266 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
267 if_name(ifp0)));
268 goto success;
269 }
270
271 /* try secondary EUI64 source. this basically is for ATM PVC */
272 if (altifp && get_hw_ifid(altifp, in6) == 0) {
273 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
274 if_name(ifp0), if_name(altifp)));
275 goto success;
276 }
277
278 /* next, try to get it from some other hardware interface */
279 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
280 {
281 if (ifp == ifp0)
282 continue;
283 if (get_hw_ifid(ifp, in6) != 0)
284 continue;
285
286 /*
287 * to borrow ifid from other interface, ifid needs to be
288 * globally unique
289 */
290 if (IFID_UNIVERSAL(in6)) {
291 nd6log((LOG_DEBUG,
292 "%s: borrow interface identifier from %s\n",
293 if_name(ifp0), if_name(ifp)));
294 goto success;
295 }
296 }
297
298 /* last resort: get from random number source */
299 if (get_rand_ifid(ifp, in6) == 0) {
300 nd6log((LOG_DEBUG,
301 "%s: interface identifier generated by random number\n",
302 if_name(ifp0)));
303 goto success;
304 }
305
306 printf("%s: failed to get interface identifier\n", if_name(ifp0));
307 return -1;
308
309 success:
310 nd6log((LOG_INFO, "%s: ifid: "
311 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
312 if_name(ifp0),
313 in6->s6_addr[8], in6->s6_addr[9],
314 in6->s6_addr[10], in6->s6_addr[11],
315 in6->s6_addr[12], in6->s6_addr[13],
316 in6->s6_addr[14], in6->s6_addr[15]));
317 return 0;
318 }
319
320 /*
321 * configure IPv6 interface address. XXX code duplicated with in.c
322 */
323 static int
324 in6_ifattach_addaddr(ifp, ia)
325 struct ifnet *ifp;
326 struct in6_ifaddr *ia;
327 {
328 struct in6_ifaddr *oia;
329 struct ifaddr *ifa;
330 int error;
331 int rtflag;
332 struct in6_addr llsol;
333
334 /*
335 * initialize if_addrlist, if we are the very first one
336 */
337 ifa = TAILQ_FIRST(&ifp->if_addrlist);
338 if (ifa == NULL) {
339 TAILQ_INIT(&ifp->if_addrlist);
340 }
341
342 /*
343 * link the interface address to global list
344 */
345 TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
346 IFAREF(&ia->ia_ifa);
347
348 /*
349 * Also link into the IPv6 address chain beginning with in6_ifaddr.
350 * kazu opposed it, but itojun & jinmei wanted.
351 */
352 if ((oia = in6_ifaddr) != NULL) {
353 for (; oia->ia_next; oia = oia->ia_next)
354 continue;
355 oia->ia_next = ia;
356 } else
357 in6_ifaddr = ia;
358 IFAREF(&ia->ia_ifa);
359
360 /*
361 * give the interface a chance to initialize, in case this
362 * is the first address to be added.
363 */
364 if (ifp->if_ioctl != NULL) {
365 int s;
366 s = splnet();
367 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
368 splx(s);
369 } else
370 error = 0;
371 if (error) {
372 switch (error) {
373 case EAFNOSUPPORT:
374 printf("%s: IPv6 not supported\n", if_name(ifp));
375 break;
376 default:
377 printf("%s: SIOCSIFADDR error %d\n", if_name(ifp),
378 error);
379 break;
380 }
381
382 /* undo changes */
383 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
384 IFAFREE(&ia->ia_ifa);
385 if (oia)
386 oia->ia_next = ia->ia_next;
387 else
388 in6_ifaddr = ia->ia_next;
389 IFAFREE(&ia->ia_ifa);
390 return -1;
391 }
392
393 /* configure link-layer address resolution */
394 rtflag = 0;
395 if (IN6_ARE_ADDR_EQUAL(&ia->ia_prefixmask.sin6_addr, &in6mask128))
396 rtflag = RTF_HOST;
397 else {
398 switch (ifp->if_type) {
399 case IFT_LOOP:
400 #ifdef IFT_STF
401 case IFT_STF:
402 #endif
403 rtflag = 0;
404 break;
405 default:
406 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
407 ia->ia_ifa.ifa_flags |= RTF_CLONING;
408 rtflag = RTF_CLONING;
409 break;
410 }
411 }
412
413 /* add route to the interface. */
414 rtrequest(RTM_ADD,
415 (struct sockaddr *)&ia->ia_addr,
416 (struct sockaddr *)&ia->ia_addr,
417 (struct sockaddr *)&ia->ia_prefixmask,
418 RTF_UP | rtflag,
419 (struct rtentry **)0);
420 ia->ia_flags |= IFA_ROUTE;
421
422 if ((rtflag & RTF_CLONING) != 0 &&
423 (ifp->if_flags & IFF_MULTICAST) != 0) {
424 /* Restore saved multicast addresses (if any). */
425 in6_restoremkludge(ia, ifp);
426
427 /*
428 * join solicited multicast address
429 */
430 bzero(&llsol, sizeof(llsol));
431 llsol.s6_addr16[0] = htons(0xff02);
432 llsol.s6_addr16[1] = htons(ifp->if_index);
433 llsol.s6_addr32[1] = 0;
434 llsol.s6_addr32[2] = htonl(1);
435 llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
436 llsol.s6_addr8[12] = 0xff;
437 (void)in6_addmulti(&llsol, ifp, &error);
438
439 /* XXX should we run DAD on other interface types? */
440 switch (ifp->if_type) {
441 #if 1
442 case IFT_ARCNET:
443 case IFT_ETHER:
444 case IFT_FDDI:
445 case IFT_IEEE1394:
446 #else
447 default:
448 #endif
449 /* mark the address TENTATIVE, if needed. */
450 ia->ia6_flags |= IN6_IFF_TENTATIVE;
451 /* nd6_dad_start() will be called in in6_if_up */
452 }
453 }
454
455 return 0;
456 }
457
458 static int
459 in6_ifattach_linklocal(ifp, altifp)
460 struct ifnet *ifp;
461 struct ifnet *altifp; /*secondary EUI64 source*/
462 {
463 struct in6_ifaddr *ia;
464
465 /*
466 * configure link-local address
467 */
468 ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
469 bzero((caddr_t)ia, sizeof(*ia));
470 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
471 if (ifp->if_flags & IFF_POINTOPOINT)
472 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
473 else
474 ia->ia_ifa.ifa_dstaddr = NULL;
475 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
476 ia->ia_ifp = ifp;
477
478 bzero(&ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
479 ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
480 ia->ia_prefixmask.sin6_family = AF_INET6;
481 ia->ia_prefixmask.sin6_addr = in6mask64;
482
483 /* just in case */
484 bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
485 ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
486 ia->ia_dstaddr.sin6_family = AF_INET6;
487
488 bzero(&ia->ia_addr, sizeof(ia->ia_addr));
489 ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
490 ia->ia_addr.sin6_family = AF_INET6;
491 ia->ia_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
492 ia->ia_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
493 ia->ia_addr.sin6_addr.s6_addr32[1] = 0;
494 if (ifp->if_flags & IFF_LOOPBACK) {
495 ia->ia_addr.sin6_addr.s6_addr32[2] = 0;
496 ia->ia_addr.sin6_addr.s6_addr32[3] = htonl(1);
497 } else {
498 if (get_ifid(ifp, altifp, &ia->ia_addr.sin6_addr) != 0) {
499 nd6log((LOG_ERR,
500 "%s: no ifid available\n", if_name(ifp)));
501 free(ia, M_IFADDR);
502 return -1;
503 }
504 }
505
506 ia->ia_ifa.ifa_metric = ifp->if_metric;
507
508 if (in6_ifattach_addaddr(ifp, ia) != 0) {
509 /* ia will be freed on failure */
510 return -1;
511 }
512
513 return 0;
514 }
515
516 static int
517 in6_ifattach_loopback(ifp)
518 struct ifnet *ifp; /* must be IFT_LOOP */
519 {
520 struct in6_ifaddr *ia;
521
522 /*
523 * configure link-local address
524 */
525 ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
526 bzero((caddr_t)ia, sizeof(*ia));
527 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
528 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
529 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
530 ia->ia_ifp = ifp;
531
532 bzero(&ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
533 ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
534 ia->ia_prefixmask.sin6_family = AF_INET6;
535 ia->ia_prefixmask.sin6_addr = in6mask128;
536
537 /*
538 * Always initialize ia_dstaddr (= broadcast address) to loopback
539 * address, to make getifaddr happier.
540 *
541 * For BSDI, it is mandatory. The BSDI version of
542 * ifa_ifwithroute() rejects to add a route to the loopback
543 * interface. Even for other systems, loopback looks somewhat
544 * special.
545 */
546 bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
547 ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
548 ia->ia_dstaddr.sin6_family = AF_INET6;
549 ia->ia_dstaddr.sin6_addr = in6addr_loopback;
550
551 bzero(&ia->ia_addr, sizeof(ia->ia_addr));
552 ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
553 ia->ia_addr.sin6_family = AF_INET6;
554 ia->ia_addr.sin6_addr = in6addr_loopback;
555
556 ia->ia_ifa.ifa_metric = ifp->if_metric;
557
558 if (in6_ifattach_addaddr(ifp, ia) != 0) {
559 /* ia will be freed on failure */
560 return -1;
561 }
562
563 return 0;
564 }
565
566 /*
567 * XXX multiple loopback interface needs more care. for instance,
568 * nodelocal address needs to be configured onto only one of them.
569 * XXX multiple link-local address case
570 */
571 void
572 in6_ifattach(ifp, altifp)
573 struct ifnet *ifp;
574 struct ifnet *altifp; /* secondary EUI64 source */
575 {
576 static size_t if_indexlim = 8;
577 struct sockaddr_in6 mltaddr;
578 struct sockaddr_in6 mltmask;
579 struct sockaddr_in6 gate;
580 struct sockaddr_in6 mask;
581 struct in6_ifaddr *ia;
582 struct in6_addr in6;
583
584 /*
585 * We have some arrays that should be indexed by if_index.
586 * since if_index will grow dynamically, they should grow too.
587 * struct in6_ifstat **in6_ifstat
588 * struct icmp6_ifstat **icmp6_ifstat
589 */
590 if (in6_ifstat == NULL || icmp6_ifstat == NULL ||
591 if_index >= if_indexlim) {
592 size_t n;
593 caddr_t q;
594 size_t olim;
595
596 olim = if_indexlim;
597 while (if_index >= if_indexlim)
598 if_indexlim <<= 1;
599
600 /* grow in6_ifstat */
601 n = if_indexlim * sizeof(struct in6_ifstat *);
602 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
603 bzero(q, n);
604 if (in6_ifstat) {
605 bcopy((caddr_t)in6_ifstat, q,
606 olim * sizeof(struct in6_ifstat *));
607 free((caddr_t)in6_ifstat, M_IFADDR);
608 }
609 in6_ifstat = (struct in6_ifstat **)q;
610 in6_ifstatmax = if_indexlim;
611
612 /* grow icmp6_ifstat */
613 n = if_indexlim * sizeof(struct icmp6_ifstat *);
614 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
615 bzero(q, n);
616 if (icmp6_ifstat) {
617 bcopy((caddr_t)icmp6_ifstat, q,
618 olim * sizeof(struct icmp6_ifstat *));
619 free((caddr_t)icmp6_ifstat, M_IFADDR);
620 }
621 icmp6_ifstat = (struct icmp6_ifstat **)q;
622 icmp6_ifstatmax = if_indexlim;
623 }
624
625 /*
626 * quirks based on interface type
627 */
628 switch (ifp->if_type) {
629 #ifdef IFT_STF
630 case IFT_STF:
631 /*
632 * 6to4 interface is a very speical kind of beast.
633 * no multicast, no linklocal (based on 03 draft).
634 */
635 goto statinit;
636 #endif
637 default:
638 break;
639 }
640
641 /*
642 * usually, we require multicast capability to the interface
643 */
644 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
645 printf("%s: not multicast capable, IPv6 not enabled\n",
646 if_name(ifp));
647 return;
648 }
649
650 /*
651 * assign link-local address, if there's none
652 */
653 ia = in6ifa_ifpforlinklocal(ifp, 0);
654 if (ia == NULL) {
655 if (in6_ifattach_linklocal(ifp, altifp) != 0)
656 return;
657 ia = in6ifa_ifpforlinklocal(ifp, 0);
658
659 if (ia == NULL) {
660 printf("%s: failed to add link-local address\n",
661 if_name(ifp));
662
663 /* we can't initialize multicasts without link-local */
664 goto statinit;
665 }
666 }
667
668 if (ifp->if_flags & IFF_POINTOPOINT) {
669 /*
670 * route local address to loopback
671 */
672 bzero(&gate, sizeof(gate));
673 gate.sin6_len = sizeof(struct sockaddr_in6);
674 gate.sin6_family = AF_INET6;
675 gate.sin6_addr = in6addr_loopback;
676 bzero(&mask, sizeof(mask));
677 mask.sin6_len = sizeof(struct sockaddr_in6);
678 mask.sin6_family = AF_INET6;
679 mask.sin6_addr = in6mask64;
680 rtrequest(RTM_ADD,
681 (struct sockaddr *)&ia->ia_addr,
682 (struct sockaddr *)&gate,
683 (struct sockaddr *)&mask,
684 RTF_UP|RTF_HOST,
685 (struct rtentry **)0);
686 }
687
688 /*
689 * assign loopback address for loopback interface
690 * XXX multiple loopback interface case
691 */
692 in6 = in6addr_loopback;
693 if (ifp->if_flags & IFF_LOOPBACK) {
694 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
695 if (in6_ifattach_loopback(ifp) != 0)
696 return;
697 }
698 }
699
700 #ifdef DIAGNOSTIC
701 if (!ia) {
702 panic("ia == NULL in in6_ifattach");
703 /*NOTREACHED*/
704 }
705 #endif
706
707 /*
708 * join multicast
709 */
710 if (ifp->if_flags & IFF_MULTICAST) {
711 int error; /* not used */
712 struct in6_multi *in6m;
713
714 /* Restore saved multicast addresses (if any). */
715 in6_restoremkludge(ia, ifp);
716
717 bzero(&mltmask, sizeof(mltmask));
718 mltmask.sin6_len = sizeof(struct sockaddr_in6);
719 mltmask.sin6_family = AF_INET6;
720 mltmask.sin6_addr = in6mask32;
721
722 /*
723 * join link-local all-nodes address
724 */
725 bzero(&mltaddr, sizeof(mltaddr));
726 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
727 mltaddr.sin6_family = AF_INET6;
728 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
729 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
730
731 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
732 if (in6m == NULL) {
733 rtrequest(RTM_ADD,
734 (struct sockaddr *)&mltaddr,
735 (struct sockaddr *)&ia->ia_addr,
736 (struct sockaddr *)&mltmask,
737 RTF_UP|RTF_CLONING, /* xxx */
738 (struct rtentry **)0);
739 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
740 }
741
742 if (ifp->if_flags & IFF_LOOPBACK) {
743 in6 = in6addr_loopback;
744 ia = in6ifa_ifpwithaddr(ifp, &in6);
745 /*
746 * join node-local all-nodes address, on loopback
747 */
748 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
749
750 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
751 if (in6m == NULL && ia != NULL) {
752 rtrequest(RTM_ADD,
753 (struct sockaddr *)&mltaddr,
754 (struct sockaddr *)&ia->ia_addr,
755 (struct sockaddr *)&mltmask,
756 RTF_UP,
757 (struct rtentry **)0);
758 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
759 }
760 }
761 }
762
763 statinit:;
764
765 /* update dynamically. */
766 if (in6_maxmtu < ifp->if_mtu)
767 in6_maxmtu = ifp->if_mtu;
768
769 if (in6_ifstat[ifp->if_index] == NULL) {
770 in6_ifstat[ifp->if_index] = (struct in6_ifstat *)
771 malloc(sizeof(struct in6_ifstat), M_IFADDR, M_WAITOK);
772 bzero(in6_ifstat[ifp->if_index], sizeof(struct in6_ifstat));
773 }
774 if (icmp6_ifstat[ifp->if_index] == NULL) {
775 icmp6_ifstat[ifp->if_index] = (struct icmp6_ifstat *)
776 malloc(sizeof(struct icmp6_ifstat), M_IFADDR, M_WAITOK);
777 bzero(icmp6_ifstat[ifp->if_index], sizeof(struct icmp6_ifstat));
778 }
779
780 /* initialize NDP variables */
781 nd6_ifattach(ifp);
782 }
783
784 /*
785 * NOTE: in6_ifdetach() does not support loopback if at this moment.
786 */
787 void
788 in6_ifdetach(ifp)
789 struct ifnet *ifp;
790 {
791 struct in6_ifaddr *ia, *oia;
792 struct ifaddr *ifa, *next;
793 struct rtentry *rt;
794 short rtflags;
795 struct sockaddr_in6 sin6;
796
797 /* nuke prefix list. this may try to remove some of ifaddrs as well */
798 in6_purgeprefix(ifp);
799
800 /* remove neighbor management table */
801 nd6_purge(ifp);
802
803 /* nuke any of IPv6 addresses we have */
804 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
805 {
806 next = ifa->ifa_list.tqe_next;
807 if (ifa->ifa_addr->sa_family != AF_INET6)
808 continue;
809 in6_purgeaddr(ifa, ifp);
810 }
811
812 /* undo everything done by in6_ifattach(), just in case */
813 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
814 {
815 next = ifa->ifa_list.tqe_next;
816
817
818 if (ifa->ifa_addr->sa_family != AF_INET6
819 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
820 continue;
821 }
822
823 ia = (struct in6_ifaddr *)ifa;
824
825 /* remove from the routing table */
826 if ((ia->ia_flags & IFA_ROUTE)
827 && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
828 rtflags = rt->rt_flags;
829 rtfree(rt);
830 rtrequest(RTM_DELETE,
831 (struct sockaddr *)&ia->ia_addr,
832 (struct sockaddr *)&ia->ia_addr,
833 (struct sockaddr *)&ia->ia_prefixmask,
834 rtflags, (struct rtentry **)0);
835 }
836
837 /* remove from the linked list */
838 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
839 IFAFREE(&ia->ia_ifa);
840
841 /* also remove from the IPv6 address chain(itojun&jinmei) */
842 oia = ia;
843 if (oia == (ia = in6_ifaddr))
844 in6_ifaddr = ia->ia_next;
845 else {
846 while (ia->ia_next && (ia->ia_next != oia))
847 ia = ia->ia_next;
848 if (ia->ia_next)
849 ia->ia_next = oia->ia_next;
850 else {
851 nd6log((LOG_ERR,
852 "%s: didn't unlink in6ifaddr from "
853 "list\n", if_name(ifp)));
854 }
855 }
856
857 IFAFREE(&oia->ia_ifa);
858 }
859
860 /* cleanup multicast address kludge table, if there is any */
861 in6_purgemkludge(ifp);
862
863 /* remove neighbor management table */
864 nd6_purge(ifp);
865
866 /* remove route to link-local allnodes multicast (ff02::1) */
867 bzero(&sin6, sizeof(sin6));
868 sin6.sin6_len = sizeof(struct sockaddr_in6);
869 sin6.sin6_family = AF_INET6;
870 sin6.sin6_addr = in6addr_linklocal_allnodes;
871 sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
872 rt = rtalloc1((struct sockaddr *)&sin6, 0);
873 if (rt && rt->rt_ifp == ifp) {
874 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
875 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
876 rtfree(rt);
877 }
878 }
879