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