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