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