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