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