in6_ifattach.c revision 1.22 1 /* $NetBSD: in6_ifattach.c,v 1.22 2000/03/23 07:03:29 thorpej Exp $ */
2 /* $KAME: in6_ifattach.c,v 1.39 2000/03/02 09:24: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/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 <netinet/ip6.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet6/nd6.h>
55
56 #include <net/net_osdep.h>
57
58 static struct in6_addr llsol;
59
60 struct in6_ifstat **in6_ifstat = NULL;
61 struct icmp6_ifstat **icmp6_ifstat = NULL;
62 size_t in6_ifstatmax = 0;
63 size_t icmp6_ifstatmax = 0;
64 unsigned long in6_maxmtu = 0;
65
66 int found_first_ifid = 0;
67 #define IFID_LEN 8
68 static u_int8_t first_ifid[IFID_LEN];
69
70 static int laddr_to_eui64 __P((u_int8_t *, u_int8_t *, size_t));
71 static int gen_rand_eui64 __P((u_int8_t *));
72
73 static int
74 laddr_to_eui64(dst, src, len)
75 u_int8_t *dst;
76 u_int8_t *src;
77 size_t len;
78 {
79 static u_int8_t zero[8];
80
81 bzero(zero, sizeof(zero));
82
83 switch (len) {
84 case 6:
85 if (bcmp(zero, src, 6) == 0)
86 return EINVAL;
87 dst[0] = src[0];
88 dst[1] = src[1];
89 dst[2] = src[2];
90 dst[3] = 0xff;
91 dst[4] = 0xfe;
92 dst[5] = src[3];
93 dst[6] = src[4];
94 dst[7] = src[5];
95 break;
96 case 8:
97 if (bcmp(zero, src, 8) == 0)
98 return EINVAL;
99 bcopy(src, dst, len);
100 break;
101 default:
102 return EINVAL;
103 }
104
105 return 0;
106 }
107
108 /*
109 * Generate a last-resort interface identifier, when the machine has no
110 * IEEE802/EUI64 address sources.
111 * The address should be random, and should not change across reboot.
112 */
113 static int
114 gen_rand_eui64(dst)
115 u_int8_t *dst;
116 {
117 MD5_CTX ctxt;
118 u_int8_t digest[16];
119
120 /* generate 8bytes of pseudo-random value. */
121 bzero(&ctxt, sizeof(ctxt));
122 MD5Init(&ctxt);
123 MD5Update(&ctxt, hostname, hostnamelen);
124 MD5Final(digest, &ctxt);
125
126 /* assumes sizeof(digest) > sizeof(first_ifid) */
127 bcopy(digest, dst, 8);
128
129 /* make sure to set "u" bit to local, and "g" bit to individual. */
130 dst[0] &= 0xfe;
131 dst[0] |= 0x02; /* EUI64 "local" */
132
133 return 0;
134 }
135
136 /*
137 * Find first ifid on list of interfaces.
138 * This is assumed that ifp0's interface token (for example, IEEE802 MAC)
139 * is globally unique. We may need to have a flag parameter in the future.
140 */
141 int
142 in6_ifattach_getifid(ifp0)
143 struct ifnet *ifp0;
144 {
145 struct ifnet *ifp;
146 struct ifaddr *ifa;
147 u_int8_t *addr = NULL;
148 int addrlen = 0;
149 struct sockaddr_dl *sdl;
150
151 if (found_first_ifid)
152 return 0;
153
154 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
155 {
156 if (ifp0 != NULL && ifp0 != ifp)
157 continue;
158 for (ifa = ifp->if_addrlist.tqh_first;
159 ifa;
160 ifa = ifa->ifa_list.tqe_next)
161 {
162 if (ifa->ifa_addr->sa_family != AF_LINK)
163 continue;
164 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
165 if (sdl == NULL)
166 continue;
167 if (sdl->sdl_alen == 0)
168 continue;
169 switch (ifp->if_type) {
170 case IFT_ETHER:
171 case IFT_FDDI:
172 case IFT_ATM:
173 /* IEEE802/EUI64 cases - what others? */
174 addr = LLADDR(sdl);
175 addrlen = sdl->sdl_alen;
176 /*
177 * to copy ifid from IEEE802/EUI64 interface,
178 * u bit of the source needs to be 0.
179 */
180 if ((addr[0] & 0x02) != 0)
181 break;
182 goto found;
183 case IFT_ARCNET:
184 /*
185 * ARCnet interface token cannot be used as
186 * globally unique identifier due to its
187 * small bitwidth.
188 */
189 break;
190 default:
191 break;
192 }
193 }
194 }
195 #ifdef DEBUG
196 printf("in6_ifattach_getifid: failed to get EUI64");
197 #endif
198 return EADDRNOTAVAIL;
199
200 found:
201 if (laddr_to_eui64(first_ifid, addr, addrlen) == 0)
202 found_first_ifid = 1;
203
204 if (found_first_ifid) {
205 printf("%s: supplying EUI64: "
206 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
207 if_name(ifp),
208 first_ifid[0], first_ifid[1],
209 first_ifid[2], first_ifid[3],
210 first_ifid[4], first_ifid[5],
211 first_ifid[6], first_ifid[7]);
212
213 /* invert u bit to convert EUI64 to RFC2373 interface ID. */
214 first_ifid[0] ^= 0x02;
215
216 return 0;
217 } else {
218 #ifdef DEBUG
219 printf("in6_ifattach_getifid: failed to get EUI64");
220 #endif
221 return EADDRNOTAVAIL;
222 }
223 }
224
225 /*
226 * XXX multiple loopback interface needs more care. for instance,
227 * nodelocal address needs to be configured onto only one of them.
228 */
229 void
230 in6_ifattach(ifp, type, laddr, noloop)
231 struct ifnet *ifp;
232 u_int type;
233 caddr_t laddr;
234 /* size_t laddrlen; */
235 int noloop;
236 {
237 static size_t if_indexlim = 8;
238 struct sockaddr_in6 mltaddr;
239 struct sockaddr_in6 mltmask;
240 struct sockaddr_in6 gate;
241 struct sockaddr_in6 mask;
242
243 struct in6_ifaddr *ia, *ib, *oia;
244 struct ifaddr *ifa;
245 int rtflag = 0;
246
247 if (type == IN6_IFT_P2P && found_first_ifid == 0) {
248 printf("%s: no ifid available for IPv6 link-local address\n",
249 if_name(ifp));
250 #if 0
251 return;
252 #else
253 /* last resort */
254 if (gen_rand_eui64(first_ifid) == 0) {
255 printf("%s: using random value as EUI64: "
256 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
257 if_name(ifp),
258 first_ifid[0], first_ifid[1],
259 first_ifid[2], first_ifid[3],
260 first_ifid[4], first_ifid[5],
261 first_ifid[6], first_ifid[7]);
262 /*
263 * invert u bit to convert EUI64 to RFC2373 interface
264 * ID.
265 */
266 first_ifid[0] ^= 0x02;
267
268 found_first_ifid = 1;
269 }
270 #endif
271 }
272
273 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
274 printf("%s: not multicast capable, IPv6 not enabled\n",
275 if_name(ifp));
276 return;
277 }
278
279 /*
280 * We have some arrays that should be indexed by if_index.
281 * since if_index will grow dynamically, they should grow too.
282 * struct in6_ifstat **in6_ifstat
283 * struct icmp6_ifstat **icmp6_ifstat
284 */
285 if (in6_ifstat == NULL || icmp6_ifstat == NULL
286 || if_index >= if_indexlim) {
287 size_t n;
288 caddr_t q;
289 size_t olim;
290
291 olim = if_indexlim;
292 while (if_index >= if_indexlim)
293 if_indexlim <<= 1;
294
295 /* grow in6_ifstat */
296 n = if_indexlim * sizeof(struct in6_ifstat *);
297 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
298 bzero(q, n);
299 if (in6_ifstat) {
300 bcopy((caddr_t)in6_ifstat, q,
301 olim * sizeof(struct in6_ifstat *));
302 free((caddr_t)in6_ifstat, M_IFADDR);
303 }
304 in6_ifstat = (struct in6_ifstat **)q;
305 in6_ifstatmax = if_indexlim;
306
307 /* grow icmp6_ifstat */
308 n = if_indexlim * sizeof(struct icmp6_ifstat *);
309 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
310 bzero(q, n);
311 if (icmp6_ifstat) {
312 bcopy((caddr_t)icmp6_ifstat, q,
313 olim * sizeof(struct icmp6_ifstat *));
314 free((caddr_t)icmp6_ifstat, M_IFADDR);
315 }
316 icmp6_ifstat = (struct icmp6_ifstat **)q;
317 icmp6_ifstatmax = if_indexlim;
318 }
319
320 /*
321 * To prevent to assign link-local address to PnP network
322 * cards multiple times.
323 * This is lengthy for P2P and LOOP but works.
324 */
325 ifa = TAILQ_FIRST(&ifp->if_addrlist);
326 if (ifa != NULL) {
327 for ( ; ifa; ifa = TAILQ_NEXT(ifa, ifa_list)) {
328 if (ifa->ifa_addr->sa_family != AF_INET6)
329 continue;
330 if (IN6_IS_ADDR_LINKLOCAL(&satosin6(ifa->ifa_addr)->sin6_addr))
331 return;
332 }
333 } else {
334 TAILQ_INIT(&ifp->if_addrlist);
335 }
336
337 /*
338 * link-local address
339 */
340 ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
341 bzero((caddr_t)ia, sizeof(*ia));
342 callout_init(&ia->ia6_dad_ch);
343 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
344 if (ifp->if_flags & IFF_POINTOPOINT)
345 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
346 else
347 ia->ia_ifa.ifa_dstaddr = NULL;
348 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
349 ia->ia_ifp = ifp;
350 TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
351 IFAREF((struct ifaddr *)ia);
352
353 /*
354 * Also link into the IPv6 address chain beginning with in6_ifaddr.
355 * kazu opposed it, but itojun & jinmei wanted.
356 */
357 if ((oia = in6_ifaddr) != NULL) {
358 for (; oia->ia_next; oia = oia->ia_next)
359 continue;
360 oia->ia_next = ia;
361 } else
362 in6_ifaddr = ia;
363 IFAREF((struct ifaddr *)ia);
364
365 ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
366 ia->ia_prefixmask.sin6_family = AF_INET6;
367 ia->ia_prefixmask.sin6_addr = in6mask64;
368
369 bzero(&ia->ia_addr, sizeof(struct sockaddr_in6));
370 ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
371 ia->ia_addr.sin6_family = AF_INET6;
372 ia->ia_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
373 ia->ia_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
374 ia->ia_addr.sin6_addr.s6_addr32[1] = 0;
375
376 switch (type) {
377 case IN6_IFT_LOOP:
378 ia->ia_addr.sin6_addr.s6_addr32[2] = 0;
379 ia->ia_addr.sin6_addr.s6_addr32[3] = htonl(1);
380 break;
381 case IN6_IFT_802:
382 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
383 ia->ia_ifa.ifa_flags |= RTF_CLONING;
384 rtflag = RTF_CLONING;
385 /* fall through */
386 case IN6_IFT_P2P802:
387 if (laddr == NULL)
388 break;
389 /* XXX use laddrlen */
390 if (laddr_to_eui64(&ia->ia_addr.sin6_addr.s6_addr8[8],
391 laddr, 6) != 0) {
392 break;
393 }
394 /* invert u bit to convert EUI64 to RFC2373 interface ID. */
395 ia->ia_addr.sin6_addr.s6_addr8[8] ^= 0x02;
396 if (found_first_ifid == 0)
397 in6_ifattach_getifid(ifp);
398 bzero(&ia->ia_dstaddr, sizeof(struct sockaddr_in6));
399 ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
400 ia->ia_dstaddr.sin6_family = AF_INET6;
401 break;
402 case IN6_IFT_P2P:
403 bcopy((caddr_t)first_ifid,
404 (caddr_t)&ia->ia_addr.sin6_addr.s6_addr8[8],
405 IFID_LEN);
406 bzero(&ia->ia_dstaddr, sizeof(struct sockaddr_in6));
407 ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
408 ia->ia_dstaddr.sin6_family = AF_INET6;
409 break;
410 case IN6_IFT_ARCNET:
411 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
412 ia->ia_ifa.ifa_flags |= RTF_CLONING;
413 rtflag = RTF_CLONING;
414 if (laddr == NULL)
415 break;
416
417 /* make non-global IF id out of link-level address */
418 bzero(&ia->ia_addr.sin6_addr.s6_addr8[8], 7);
419 ia->ia_addr.sin6_addr.s6_addr8[15] = *laddr;
420 }
421
422 ia->ia_ifa.ifa_metric = ifp->if_metric;
423
424 if (ifp->if_ioctl != NULL) {
425 int s;
426 int error;
427
428 /*
429 * give the interface a chance to initialize, in case this
430 * is the first address to be added.
431 */
432 s = splimp();
433 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
434 splx(s);
435
436 if (error) {
437 switch (error) {
438 case EAFNOSUPPORT:
439 printf("%s: IPv6 not supported\n",
440 if_name(ifp));
441 break;
442 default:
443 printf("%s: SIOCSIFADDR error %d\n",
444 if_name(ifp), error);
445 break;
446 }
447
448 /* undo changes */
449 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
450 IFAFREE((struct ifaddr *)ia);
451 if (oia)
452 oia->ia_next = ia->ia_next;
453 else
454 in6_ifaddr = ia->ia_next;
455 IFAFREE((struct ifaddr *)ia);
456 return;
457 }
458 }
459
460 /* add route to the interface. */
461 rtrequest(RTM_ADD,
462 (struct sockaddr *)&ia->ia_addr,
463 (struct sockaddr *)&ia->ia_addr,
464 (struct sockaddr *)&ia->ia_prefixmask,
465 RTF_UP|rtflag,
466 (struct rtentry **)0);
467 ia->ia_flags |= IFA_ROUTE;
468
469 if (type == IN6_IFT_P2P || type == IN6_IFT_P2P802) {
470 /*
471 * route local address to loopback
472 */
473 bzero(&gate, sizeof(gate));
474 gate.sin6_len = sizeof(struct sockaddr_in6);
475 gate.sin6_family = AF_INET6;
476 gate.sin6_addr = in6addr_loopback;
477 bzero(&mask, sizeof(mask));
478 mask.sin6_len = sizeof(struct sockaddr_in6);
479 mask.sin6_family = AF_INET6;
480 mask.sin6_addr = in6mask64;
481 rtrequest(RTM_ADD,
482 (struct sockaddr *)&ia->ia_addr,
483 (struct sockaddr *)&gate,
484 (struct sockaddr *)&mask,
485 RTF_UP|RTF_HOST,
486 (struct rtentry **)0);
487 }
488
489 /*
490 * loopback address
491 */
492 ib = (struct in6_ifaddr *)NULL;
493 if (type == IN6_IFT_LOOP) {
494 ib = (struct in6_ifaddr *)
495 malloc(sizeof(*ib), M_IFADDR, M_WAITOK);
496 bzero((caddr_t)ib, sizeof(*ib));
497 ib->ia_ifa.ifa_addr = (struct sockaddr *)&ib->ia_addr;
498 ib->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ib->ia_dstaddr;
499 ib->ia_ifa.ifa_netmask = (struct sockaddr *)&ib->ia_prefixmask;
500 ib->ia_ifp = ifp;
501
502 ia->ia_next = ib;
503 TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ib,
504 ifa_list);
505 IFAREF((struct ifaddr *)ib);
506
507 ib->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
508 ib->ia_prefixmask.sin6_family = AF_INET6;
509 ib->ia_prefixmask.sin6_addr = in6mask128;
510 ib->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
511 ib->ia_addr.sin6_family = AF_INET6;
512 ib->ia_addr.sin6_addr = in6addr_loopback;
513
514 /*
515 * Always initialize ia_dstaddr (= broadcast address)
516 * to loopback address, to make getifaddr happier.
517 *
518 * For BSDI, it is mandatory. The BSDI version of
519 * ifa_ifwithroute() rejects to add a route to the loopback
520 * interface. Even for other systems, loopback looks somewhat
521 * special.
522 */
523 ib->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
524 ib->ia_dstaddr.sin6_family = AF_INET6;
525 ib->ia_dstaddr.sin6_addr = in6addr_loopback;
526
527 ib->ia_ifa.ifa_metric = ifp->if_metric;
528
529 rtrequest(RTM_ADD,
530 (struct sockaddr *)&ib->ia_addr,
531 (struct sockaddr *)&ib->ia_addr,
532 (struct sockaddr *)&ib->ia_prefixmask,
533 RTF_UP|RTF_HOST,
534 (struct rtentry **)0);
535
536 ib->ia_flags |= IFA_ROUTE;
537 }
538
539 /*
540 * join multicast
541 */
542 if (ifp->if_flags & IFF_MULTICAST) {
543 int error; /* not used */
544
545 /* Restore saved multicast addresses(if any). */
546 in6_restoremkludge(ia, ifp);
547
548 bzero(&mltmask, sizeof(mltmask));
549 mltmask.sin6_len = sizeof(struct sockaddr_in6);
550 mltmask.sin6_family = AF_INET6;
551 mltmask.sin6_addr = in6mask32;
552
553 /*
554 * join link-local all-nodes address
555 */
556 bzero(&mltaddr, sizeof(mltaddr));
557 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
558 mltaddr.sin6_family = AF_INET6;
559 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
560 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
561 rtrequest(RTM_ADD,
562 (struct sockaddr *)&mltaddr,
563 (struct sockaddr *)&ia->ia_addr,
564 (struct sockaddr *)&mltmask,
565 RTF_UP|RTF_CLONING, /* xxx */
566 (struct rtentry **)0);
567 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
568
569 if (type == IN6_IFT_LOOP) {
570 /*
571 * join node-local all-nodes address
572 */
573 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
574 rtrequest(RTM_ADD,
575 (struct sockaddr *)&mltaddr,
576 (struct sockaddr *)&ib->ia_addr,
577 (struct sockaddr *)&mltmask,
578 RTF_UP,
579 (struct rtentry **)0);
580 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
581 } else {
582 /*
583 * join solicited multicast address
584 */
585 bzero(&llsol, sizeof(llsol));
586 llsol.s6_addr16[0] = htons(0xff02);
587 llsol.s6_addr16[1] = htons(ifp->if_index);
588 llsol.s6_addr32[1] = 0;
589 llsol.s6_addr32[2] = htonl(1);
590 llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
591 llsol.s6_addr8[12] = 0xff;
592 (void)in6_addmulti(&llsol, ifp, &error);
593 }
594 }
595
596 /* update dynamically. */
597 if (in6_maxmtu < ifp->if_mtu)
598 in6_maxmtu = ifp->if_mtu;
599
600 if (in6_ifstat[ifp->if_index] == NULL) {
601 in6_ifstat[ifp->if_index] = (struct in6_ifstat *)
602 malloc(sizeof(struct in6_ifstat), M_IFADDR, M_WAITOK);
603 bzero(in6_ifstat[ifp->if_index], sizeof(struct in6_ifstat));
604 }
605 if (icmp6_ifstat[ifp->if_index] == NULL) {
606 icmp6_ifstat[ifp->if_index] = (struct icmp6_ifstat *)
607 malloc(sizeof(struct icmp6_ifstat), M_IFADDR, M_WAITOK);
608 bzero(icmp6_ifstat[ifp->if_index], sizeof(struct icmp6_ifstat));
609 }
610
611 /* initialize NDP variables */
612 nd6_ifattach(ifp);
613
614 /* mark the address TENTATIVE, if needed. */
615 switch (ifp->if_type) {
616 case IFT_ARCNET:
617 case IFT_ETHER:
618 case IFT_FDDI:
619 #if 0
620 case IFT_ATM:
621 case IFT_SLIP:
622 case IFT_PPP:
623 #endif
624 ia->ia6_flags |= IN6_IFF_TENTATIVE;
625 /* nd6_dad_start() will be called in in6_if_up */
626 break;
627 case IFT_GIF: /*XXX*/
628 case IFT_LOOP:
629 case IFT_FAITH:
630 default:
631 break;
632 }
633
634 return;
635 }
636
637 /*
638 * NOTE: in6_ifdetach() does not support loopback if at this moment.
639 */
640 void
641 in6_ifdetach(ifp)
642 struct ifnet *ifp;
643 {
644 struct in6_ifaddr *ia, *oia;
645 struct ifaddr *ifa;
646 struct rtentry *rt;
647 short rtflags;
648 struct sockaddr_in6 sin6;
649
650 /* nuke prefix list. this may try to remove some of ifaddrs as well */
651 in6_purgeprefix(ifp);
652
653 /* remove neighbor management table */
654 nd6_purge(ifp);
655
656 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
657 {
658 if (ifa->ifa_addr->sa_family != AF_INET6
659 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
660 continue;
661 }
662
663 ia = (struct in6_ifaddr *)ifa;
664
665 /* remove from the routing table */
666 if ((ia->ia_flags & IFA_ROUTE)
667 && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
668 rtflags = rt->rt_flags;
669 rtfree(rt);
670 rtrequest(RTM_DELETE,
671 (struct sockaddr *)&ia->ia_addr,
672 (struct sockaddr *)&ia->ia_addr,
673 (struct sockaddr *)&ia->ia_prefixmask,
674 rtflags, (struct rtentry **)0);
675 }
676
677 /* remove from the linked list */
678 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
679
680 /* also remove from the IPv6 address chain(itojun&jinmei) */
681 oia = ia;
682 if (oia == (ia = in6_ifaddr))
683 in6_ifaddr = ia->ia_next;
684 else {
685 while (ia->ia_next && (ia->ia_next != oia))
686 ia = ia->ia_next;
687 if (ia->ia_next)
688 ia->ia_next = oia->ia_next;
689 #ifdef DEBUG
690 else
691 printf("%s: didn't unlink in6ifaddr from "
692 "list\n", if_name(ifp));
693 #endif
694 }
695
696 free(ia, M_IFADDR);
697 }
698
699 /* cleanup multicast address kludge table, if there is any */
700 in6_purgemkludge(ifp);
701
702 /* remove route to link-local allnodes multicast (ff02::1) */
703 bzero(&sin6, sizeof(sin6));
704 sin6.sin6_len = sizeof(struct sockaddr_in6);
705 sin6.sin6_family = AF_INET6;
706 sin6.sin6_addr = in6addr_linklocal_allnodes;
707 sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
708 if ((rt = rtalloc1((struct sockaddr *)&sin6, 0)) != NULL) {
709 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
710 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
711 rtfree(rt);
712 }
713 }
714