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