in6_ifattach.c revision 1.68
1/*	$OpenBSD: in6_ifattach.c,v 1.68 2014/01/21 10:18:26 mpi 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_var.h>
45#include <net/if_dl.h>
46#include <net/if_types.h>
47#include <net/route.h>
48
49#include <netinet/in.h>
50#include <netinet/if_ether.h>
51
52#include <netinet6/in6_var.h>
53#include <netinet/ip6.h>
54#include <netinet6/ip6_var.h>
55#include <netinet6/in6_ifattach.h>
56#include <netinet6/ip6_var.h>
57#include <netinet6/nd6.h>
58#ifdef MROUTING
59#include <netinet6/ip6_mroute.h>
60#endif
61
62#include <dev/rndvar.h>
63
64unsigned long in6_maxmtu = 0;
65
66int ip6_auto_linklocal = 1;	/* enable by default */
67
68int get_last_resort_ifid(struct ifnet *, struct in6_addr *);
69int get_hw_ifid(struct ifnet *, struct in6_addr *);
70int get_ifid(struct ifnet *, struct in6_addr *);
71int in6_ifattach_loopback(struct ifnet *);
72
73#define EUI64_GBIT	0x01
74#define EUI64_UBIT	0x02
75#define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
76#define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
77#define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
78#define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
79#define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
80
81#define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
82#define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
83
84/*
85 * Generate a last-resort interface identifier, when the machine has no
86 * IEEE802/EUI64 address sources.
87 * The goal here is to get an interface identifier that is
88 * (1) random enough and (2) does not change across reboot.
89 * We currently use MD5(hostname) for it.
90 *
91 * in6 - upper 64bits are preserved
92 */
93int
94get_last_resort_ifid(struct ifnet *ifp, struct in6_addr *in6)
95{
96	MD5_CTX ctxt;
97	u_int8_t digest[16];
98
99#if 0
100	/* we need at least several letters as seed for ifid */
101	if (hostnamelen < 3)
102		return -1;
103#endif
104
105	/* generate 8 bytes of pseudo-random value. */
106	bzero(&ctxt, sizeof(ctxt));
107	MD5Init(&ctxt);
108	MD5Update(&ctxt, hostname, hostnamelen);
109	MD5Final(digest, &ctxt);
110
111	/* assumes sizeof(digest) > sizeof(ifid) */
112	bcopy(digest, &in6->s6_addr[8], 8);
113
114	/* make sure to set "u" bit to local, and "g" bit to individual. */
115	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
116	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
117
118	/* convert EUI64 into IPv6 interface identifier */
119	EUI64_TO_IFID(in6);
120
121	return 0;
122}
123
124/*
125 * Generate a random interface identifier.
126 *
127 * in6 - upper 64bits are preserved
128 */
129void
130in6_get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
131{
132	arc4random_buf(&in6->s6_addr32[2], 8);
133
134	/* make sure to set "u" bit to local, and "g" bit to individual. */
135	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
136	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
137
138	/* convert EUI64 into IPv6 interface identifier */
139	EUI64_TO_IFID(in6);
140}
141
142/*
143 * Get interface identifier for the specified interface.
144 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
145 *
146 * in6 - upper 64bits are preserved
147 */
148int
149get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
150{
151	struct ifaddr *ifa;
152	struct sockaddr_dl *sdl;
153	char *addr;
154	size_t addrlen;
155	static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
156	static u_int8_t allone[8] =
157		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
158
159	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
160		if (ifa->ifa_addr->sa_family != AF_LINK)
161			continue;
162		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
163		if (sdl == NULL)
164			continue;
165		if (sdl->sdl_alen == 0)
166			continue;
167
168		goto found;
169	}
170
171	return -1;
172
173found:
174	addr = LLADDR(sdl);
175	addrlen = sdl->sdl_alen;
176
177	switch (ifp->if_type) {
178	case IFT_IEEE1394:
179	case IFT_IEEE80211:
180		/* IEEE1394 uses 16byte length address starting with EUI64 */
181		if (addrlen > 8)
182			addrlen = 8;
183		break;
184	default:
185		break;
186	}
187
188	/* get EUI64 */
189	switch (ifp->if_type) {
190	/* IEEE802/EUI64 cases - what others? */
191	case IFT_ETHER:
192	case IFT_CARP:
193	case IFT_IEEE1394:
194	case IFT_IEEE80211:
195		/* look at IEEE802/EUI64 only */
196		if (addrlen != 8 && addrlen != 6)
197			return -1;
198
199		/*
200		 * check for invalid MAC address - on bsdi, we see it a lot
201		 * since wildboar configures all-zero MAC on pccard before
202		 * card insertion.
203		 */
204		if (bcmp(addr, allzero, addrlen) == 0)
205			return -1;
206		if (bcmp(addr, allone, addrlen) == 0)
207			return -1;
208
209		/* make EUI64 address */
210		if (addrlen == 8)
211			bcopy(addr, &in6->s6_addr[8], 8);
212		else if (addrlen == 6) {
213			in6->s6_addr[8] = addr[0];
214			in6->s6_addr[9] = addr[1];
215			in6->s6_addr[10] = addr[2];
216			in6->s6_addr[11] = 0xff;
217			in6->s6_addr[12] = 0xfe;
218			in6->s6_addr[13] = addr[3];
219			in6->s6_addr[14] = addr[4];
220			in6->s6_addr[15] = addr[5];
221		}
222		break;
223
224	case IFT_GIF:
225		/*
226		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
227		 * however, IPv4 address is not very suitable as unique
228		 * identifier source (can be renumbered).
229		 * we don't do this.
230		 */
231		return -1;
232
233	default:
234		return -1;
235	}
236
237	/* sanity check: g bit must not indicate "group" */
238	if (EUI64_GROUP(in6))
239		return -1;
240
241	/* convert EUI64 into IPv6 interface identifier */
242	EUI64_TO_IFID(in6);
243
244	/*
245	 * sanity check: ifid must not be all zero, avoid conflict with
246	 * subnet router anycast
247	 */
248	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
249	    bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
250		return -1;
251	}
252
253	return 0;
254}
255
256/*
257 * Get interface identifier for the specified interface.  If it is not
258 * available on ifp0, borrow interface identifier from other information
259 * sources.
260 */
261int
262get_ifid(struct ifnet *ifp0, struct in6_addr *in6)
263{
264	struct ifnet *ifp;
265
266	/* first, try to get it from the interface itself */
267	if (get_hw_ifid(ifp0, in6) == 0) {
268		nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
269		    ifp0->if_xname));
270		goto success;
271	}
272
273	/* next, try to get it from some other hardware interface */
274	TAILQ_FOREACH(ifp, &ifnet, if_list) {
275		if (ifp == ifp0)
276			continue;
277		if (get_hw_ifid(ifp, in6) != 0)
278			continue;
279
280		/*
281		 * to borrow ifid from other interface, ifid needs to be
282		 * globally unique
283		 */
284		if (IFID_UNIVERSAL(in6)) {
285			nd6log((LOG_DEBUG,
286			    "%s: borrow interface identifier from %s\n",
287			    ifp0->if_xname, ifp->if_xname));
288			goto success;
289		}
290	}
291
292	/* last resort: get from random number source */
293	if (get_last_resort_ifid(ifp, in6) == 0) {
294		nd6log((LOG_DEBUG,
295		    "%s: interface identifier generated by random number\n",
296		    ifp0->if_xname));
297		goto success;
298	}
299
300	printf("%s: failed to get interface identifier\n", ifp0->if_xname);
301	return -1;
302
303success:
304	nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
305	    ifp0->if_xname, in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
306	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
307	    in6->s6_addr[14], in6->s6_addr[15]));
308	return 0;
309}
310
311/*
312 * ifid - used as EUI64 if not NULL, overrides other EUI64 sources
313 */
314
315int
316in6_ifattach_linklocal(struct ifnet *ifp, struct in6_addr *ifid)
317{
318	struct in6_ifaddr *ia6;
319	struct in6_aliasreq ifra;
320	struct nd_prefix pr0;
321	int i, s, error;
322
323	/*
324	 * configure link-local address.
325	 */
326	bzero(&ifra, sizeof(ifra));
327
328	/*
329	 * in6_update_ifa() does not use ifra_name, but we accurately set it
330	 * for safety.
331	 */
332	strncpy(ifra.ifra_name, ifp->if_xname, sizeof(ifra.ifra_name));
333
334	ifra.ifra_addr.sin6_family = AF_INET6;
335	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
336	ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
337	ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
338	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
339	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
340		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
341		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
342	} else if (ifid) {
343		ifra.ifra_addr.sin6_addr = *ifid;
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		ifra.ifra_addr.sin6_addr.s6_addr[8] &= ~EUI64_GBIT;
348		ifra.ifra_addr.sin6_addr.s6_addr[8] |= EUI64_UBIT;
349	} else {
350		if (get_ifid(ifp, &ifra.ifra_addr.sin6_addr) != 0) {
351			nd6log((LOG_ERR,
352			    "%s: no ifid available\n", ifp->if_xname));
353			return (-1);
354		}
355	}
356
357	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
358	ifra.ifra_prefixmask.sin6_family = AF_INET6;
359	ifra.ifra_prefixmask.sin6_addr = in6mask64;
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. In the case of CARP, we may be called after
374	 * one has already been configured, so check if it's already there
375	 * with in6ifa_ifpforlinklocal() and clobber it if it exists.
376	 */
377	s = splsoftnet();
378	error = in6_update_ifa(ifp, &ifra, in6ifa_ifpforlinklocal(ifp, 0));
379	splx(s);
380
381	if (error != 0) {
382		/*
383		 * XXX: When the interface does not support IPv6, this call
384		 * would fail in the SIOCSIFADDR ioctl.  I believe the
385		 * notification is rather confusing in this case, so just
386		 * suppress it.  (jinmei@kame.net 20010130)
387		 */
388		if (error != EAFNOSUPPORT)
389			nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
390			    "configure a link-local address on %s "
391			    "(errno=%d)\n",
392			    ifp->if_xname, error));
393		return (-1);
394	}
395
396	/*
397	 * Adjust ia6_flags so that in6_if_up will perform DAD.
398	 * XXX: Some P2P interfaces seem not to send packets just after
399	 * becoming up, so we skip p2p interfaces for safety.
400	 */
401	ia6 = in6ifa_ifpforlinklocal(ifp, 0); /* ia6 must not be NULL */
402#ifdef DIAGNOSTIC
403	if (!ia6) {
404		panic("ia6 == NULL in in6_ifattach_linklocal");
405		/* NOTREACHED */
406	}
407#endif
408	if (in6if_do_dad(ifp) && ((ifp->if_flags & IFF_POINTOPOINT) ||
409	    (ifp->if_type == IFT_CARP)) == 0) {
410		ia6->ia6_flags &= ~IN6_IFF_NODAD;
411		ia6->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
455/*
456 * ifp - must be IFT_LOOP
457 */
458
459int
460in6_ifattach_loopback(struct ifnet *ifp)
461{
462	struct in6_aliasreq ifra;
463	int error;
464
465	bzero(&ifra, sizeof(ifra));
466
467	/*
468	 * in6_update_ifa() does not use ifra_name, but we accurately set it
469	 * for safety.
470	 */
471	strncpy(ifra.ifra_name, ifp->if_xname, sizeof(ifra.ifra_name));
472
473	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
474	ifra.ifra_prefixmask.sin6_family = AF_INET6;
475	ifra.ifra_prefixmask.sin6_addr = in6mask128;
476
477	/*
478	 * Always initialize ia_dstaddr (= broadcast address) to loopback
479	 * address.  Follows IPv4 practice - see in_ifinit().
480	 */
481	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
482	ifra.ifra_dstaddr.sin6_family = AF_INET6;
483	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
484
485	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
486	ifra.ifra_addr.sin6_family = AF_INET6;
487	ifra.ifra_addr.sin6_addr = in6addr_loopback;
488
489	/* the loopback  address should NEVER expire. */
490	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
491	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
492
493	/* we don't need to perform DAD on loopback interfaces. */
494	ifra.ifra_flags |= IN6_IFF_NODAD;
495
496	/*
497	 * We are sure that this is a newly assigned address, so we can set
498	 * NULL to the 3rd arg.
499	 */
500	if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
501		nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
502		    "the loopback address on %s (errno=%d)\n",
503		    ifp->if_xname, error));
504		return (-1);
505	}
506
507	return 0;
508}
509
510/*
511 * compute NI group address, based on the current hostname setting.
512 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
513 *
514 * when ifp == NULL, the caller is responsible for filling scopeid.
515 */
516int
517in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
518    struct sockaddr_in6 *sa6)
519{
520	const char *p;
521	u_int8_t *q;
522	MD5_CTX ctxt;
523	u_int8_t digest[16];
524	u_int8_t l;
525	u_int8_t 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((char *)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(struct ifnet *ifp)
569{
570	struct in6_ifaddr *ia6;
571	struct in6_addr in6;
572
573	/* some of the interfaces are inherently not IPv6 capable */
574	switch (ifp->if_type) {
575	case IFT_BRIDGE:
576	case IFT_ENC:
577	case IFT_PFLOG:
578	case IFT_PFSYNC:
579		return;
580	}
581
582	/*
583	 * if link mtu is too small, don't try to configure IPv6.
584	 * remember there could be some link-layer that has special
585	 * fragmentation logic.
586	 */
587	if (ifp->if_mtu < IPV6_MMTU) {
588		nd6log((LOG_INFO, "in6_ifattach: "
589		    "%s has too small MTU, IPv6 not enabled\n",
590		    ifp->if_xname));
591		return;
592	}
593
594	/*
595	 * quirks based on interface type
596	 */
597	switch (ifp->if_type) {
598	/* we attach a link-local address when a vhid is assigned */
599	case IFT_CARP:
600		return;
601	default:
602		break;
603	}
604
605	/*
606	 * usually, we require multicast capability to the interface
607	 */
608	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
609		nd6log((LOG_INFO, "in6_ifattach: "
610		    "%s is not multicast capable, IPv6 not enabled\n",
611		    ifp->if_xname));
612		return;
613	}
614
615	/*
616	 * assign loopback address for loopback interface.
617	 * XXX multiple loopback interface case.
618	 */
619	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
620		in6 = in6addr_loopback;
621		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
622			if (in6_ifattach_loopback(ifp) != 0)
623				return;
624		}
625	}
626
627	/*
628	 * assign a link-local address, if there's none.
629	 */
630	if (ip6_auto_linklocal) {
631		ia6 = in6ifa_ifpforlinklocal(ifp, 0);
632		if (ia6 == NULL) {
633			if (in6_ifattach_linklocal(ifp, NULL) == 0) {
634				/* linklocal address assigned */
635			} else {
636				/* failed to assign linklocal address. bark? */
637			}
638		}
639	}
640}
641
642/*
643 * NOTE: in6_ifdetach() does not support loopback if at this moment.
644 */
645void
646in6_ifdetach(struct ifnet *ifp)
647{
648	struct ifaddr *ifa, *next;
649	struct rtentry *rt;
650	struct sockaddr_in6 sin6;
651
652#ifdef MROUTING
653	/* remove ip6_mrouter stuff */
654	ip6_mrouter_detach(ifp);
655#endif
656
657	/* remove neighbor management table */
658	nd6_purge(ifp);
659
660	/* nuke any of IPv6 addresses we have */
661	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrlist, ifa_list, next) {
662		if (ifa->ifa_addr->sa_family != AF_INET6)
663			continue;
664		in6_purgeaddr(ifa);
665	}
666
667	/*
668	 * remove neighbor management table.  we call it twice just to make
669	 * sure we nuke everything.  maybe we need just one call.
670	 * XXX: since the first call did not release addresses, some prefixes
671	 * might remain.  We should call nd6_purge() again to release the
672	 * prefixes after removing all addresses above.
673	 * (Or can we just delay calling nd6_purge until at this point?)
674	 */
675	nd6_purge(ifp);
676
677	/* remove route to interface local allnodes multicast (ff01::1) */
678	bzero(&sin6, sizeof(sin6));
679	sin6.sin6_len = sizeof(struct sockaddr_in6);
680	sin6.sin6_family = AF_INET6;
681	sin6.sin6_addr = in6addr_intfacelocal_allnodes;
682	sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
683	rt = rtalloc1(sin6tosa(&sin6), 0, ifp->if_rdomain);
684	if (rt && rt->rt_ifp == ifp) {
685		struct rt_addrinfo info;
686
687		bzero(&info, sizeof(info));
688		info.rti_flags = rt->rt_flags;
689		info.rti_info[RTAX_DST] = rt_key(rt);
690		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
691		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
692		rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL,
693		    ifp->if_rdomain);
694		rtfree(rt);
695	}
696
697	/* remove route to link-local allnodes multicast (ff02::1) */
698	bzero(&sin6, sizeof(sin6));
699	sin6.sin6_len = sizeof(struct sockaddr_in6);
700	sin6.sin6_family = AF_INET6;
701	sin6.sin6_addr = in6addr_linklocal_allnodes;
702	sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
703	rt = rtalloc1(sin6tosa(&sin6), 0, ifp->if_rdomain);
704	if (rt && rt->rt_ifp == ifp) {
705		struct rt_addrinfo info;
706
707		bzero(&info, sizeof(info));
708		info.rti_flags = rt->rt_flags;
709		info.rti_info[RTAX_DST] = rt_key(rt);
710		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
711		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
712		rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL,
713		    ifp->if_rdomain);
714		rtfree(rt);
715	}
716}
717