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