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