in6_ifattach.c revision 1.78
1/*	$OpenBSD: in6_ifattach.c,v 1.78 2014/12/04 00:02:15 tedu 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	 * Instead, in6_if_up() will start DAD with a proper random delay.
348	 */
349	ifra.ifra_flags |= IN6_IFF_NODAD;
350
351	/*
352	 * Now call in6_update_ifa() to do a bunch of procedures to configure
353	 * a link-local address. In the case of CARP, we may be called after
354	 * one has already been configured, so check if it's already there
355	 * with in6ifa_ifpforlinklocal() and clobber it if it exists.
356	 */
357	s = splsoftnet();
358	error = in6_update_ifa(ifp, &ifra, in6ifa_ifpforlinklocal(ifp, 0));
359	splx(s);
360
361	if (error != 0) {
362		/*
363		 * XXX: When the interface does not support IPv6, this call
364		 * would fail in the SIOCSIFADDR ioctl.  I believe the
365		 * notification is rather confusing in this case, so just
366		 * suppress it.  (jinmei@kame.net 20010130)
367		 */
368		if (error != EAFNOSUPPORT)
369			nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
370			    "configure a link-local address on %s "
371			    "(errno=%d)\n",
372			    ifp->if_xname, error));
373		return (-1);
374	}
375
376	/*
377	 * Adjust ia6_flags so that in6_if_up will perform DAD.
378	 * XXX: Some P2P interfaces seem not to send packets just after
379	 * becoming up, so we skip p2p interfaces for safety.
380	 */
381	ia6 = in6ifa_ifpforlinklocal(ifp, 0); /* ia6 must not be NULL */
382#ifdef DIAGNOSTIC
383	if (!ia6) {
384		panic("ia6 == NULL in in6_ifattach_linklocal");
385		/* NOTREACHED */
386	}
387#endif
388	if (in6if_do_dad(ifp) && ((ifp->if_flags & IFF_POINTOPOINT) ||
389	    (ifp->if_type == IFT_CARP)) == 0) {
390		ia6->ia6_flags &= ~IN6_IFF_NODAD;
391		ia6->ia6_flags |= IN6_IFF_TENTATIVE;
392	}
393
394	/*
395	 * Make the link-local prefix (fe80::/64%link) as on-link.
396	 * Since we'd like to manage prefixes separately from addresses,
397	 * we make an ND6 prefix structure for the link-local prefix,
398	 * and add it to the prefix list as a never-expire prefix.
399	 * XXX: this change might affect some existing code base...
400	 */
401	bzero(&pr0, sizeof(pr0));
402	pr0.ndpr_ifp = ifp;
403	/* this should be 64 at this moment. */
404	pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
405	pr0.ndpr_mask = ifra.ifra_prefixmask.sin6_addr;
406	pr0.ndpr_prefix = ifra.ifra_addr;
407	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
408	for (i = 0; i < 4; i++) {
409		pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
410		    in6mask64.s6_addr32[i];
411	}
412	/*
413	 * Initialize parameters.  The link-local prefix must always be
414	 * on-link, and its lifetimes never expire.
415	 */
416	pr0.ndpr_raf_onlink = 1;
417	pr0.ndpr_raf_auto = 1;	/* probably meaningless */
418	pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
419	pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
420	/*
421	 * Since there is no other link-local addresses, nd6_prefix_lookup()
422	 * probably returns NULL.  However, we cannot always expect the result.
423	 * For example, if we first remove the (only) existing link-local
424	 * address, and then reconfigure another one, the prefix is still
425	 * valid with referring to the old link-local address.
426	 */
427	if (nd6_prefix_lookup(&pr0) == NULL) {
428		if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
429			return (error);
430	}
431
432	return 0;
433}
434
435/*
436 * ifp - must be IFT_LOOP
437 */
438
439int
440in6_ifattach_loopback(struct ifnet *ifp)
441{
442	struct in6_aliasreq ifra;
443	int error;
444
445	bzero(&ifra, sizeof(ifra));
446
447	/*
448	 * in6_update_ifa() does not use ifra_name, but we accurately set it
449	 * for safety.
450	 */
451	strncpy(ifra.ifra_name, ifp->if_xname, sizeof(ifra.ifra_name));
452
453	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
454	ifra.ifra_prefixmask.sin6_family = AF_INET6;
455	ifra.ifra_prefixmask.sin6_addr = in6mask128;
456
457	/*
458	 * Always initialize ia_dstaddr (= broadcast address) to loopback
459	 * address.  Follows IPv4 practice - see in_ifinit().
460	 */
461	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
462	ifra.ifra_dstaddr.sin6_family = AF_INET6;
463	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
464
465	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
466	ifra.ifra_addr.sin6_family = AF_INET6;
467	ifra.ifra_addr.sin6_addr = in6addr_loopback;
468
469	/* the loopback  address should NEVER expire. */
470	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
471	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
472
473	/* we don't need to perform DAD on loopback interfaces. */
474	ifra.ifra_flags |= IN6_IFF_NODAD;
475
476	/*
477	 * We are sure that this is a newly assigned address, so we can set
478	 * NULL to the 3rd arg.
479	 */
480	if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
481		nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
482		    "the loopback address on %s (errno=%d)\n",
483		    ifp->if_xname, error));
484		return (-1);
485	}
486
487	return 0;
488}
489
490/*
491 * compute NI group address, based on the current hostname setting.
492 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
493 *
494 * when ifp == NULL, the caller is responsible for filling scopeid.
495 */
496int
497in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
498    struct sockaddr_in6 *sa6)
499{
500	const char *p;
501	u_int8_t *q;
502	SHA2_CTX ctx;
503	u_int8_t digest[SHA512_DIGEST_LENGTH];
504	u_int8_t l;
505	u_int8_t n[64];	/* a single label must not exceed 63 chars */
506
507	if (!namelen || !name)
508		return -1;
509
510	p = name;
511	while (p && *p && *p != '.' && p - name < namelen)
512		p++;
513	if (p - name > sizeof(n) - 1)
514		return -1;	/* label too long */
515	l = p - name;
516	strncpy((char *)n, name, l);
517	n[(int)l] = '\0';
518	for (q = n; *q; q++) {
519		if ('A' <= *q && *q <= 'Z')
520			*q = *q - 'A' + 'a';
521	}
522
523	/* generate 8 bytes of pseudo-random value. */
524	SHA512Init(&ctx);
525	SHA512Update(&ctx, &l, sizeof(l));
526	SHA512Update(&ctx, n, l);
527	SHA512Final(digest, &ctx);
528
529	bzero(sa6, sizeof(*sa6));
530	sa6->sin6_family = AF_INET6;
531	sa6->sin6_len = sizeof(*sa6);
532	sa6->sin6_addr.s6_addr16[0] = htons(0xff02);
533	sa6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
534	sa6->sin6_addr.s6_addr8[11] = 2;
535	bcopy(digest, &sa6->sin6_addr.s6_addr32[3],
536	    sizeof(sa6->sin6_addr.s6_addr32[3]));
537
538	return 0;
539}
540
541/*
542 * XXX multiple loopback interface needs more care.  for instance,
543 * nodelocal address needs to be configured onto only one of them.
544 * XXX multiple link-local address case
545 */
546void
547in6_ifattach(struct ifnet *ifp)
548{
549	struct in6_ifaddr *ia6;
550	struct in6_addr in6;
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		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/*
611 * NOTE: in6_ifdetach() does not support loopback if at this moment.
612 */
613void
614in6_ifdetach(struct ifnet *ifp)
615{
616	struct ifaddr *ifa, *next;
617	struct rtentry *rt;
618	struct sockaddr_in6 sin6;
619
620#ifdef MROUTING
621	/* remove ip6_mrouter stuff */
622	ip6_mrouter_detach(ifp);
623#endif
624
625	/* remove neighbor management table */
626	nd6_purge(ifp);
627
628	/* nuke any of IPv6 addresses we have */
629	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrlist, ifa_list, next) {
630		if (ifa->ifa_addr->sa_family != AF_INET6)
631			continue;
632		in6_purgeaddr(ifa);
633		dohooks(ifp->if_addrhooks, 0);
634	}
635
636	/*
637	 * remove neighbor management table.  we call it twice just to make
638	 * sure we nuke everything.  maybe we need just one call.
639	 * XXX: since the first call did not release addresses, some prefixes
640	 * might remain.  We should call nd6_purge() again to release the
641	 * prefixes after removing all addresses above.
642	 * (Or can we just delay calling nd6_purge until at this point?)
643	 */
644	nd6_purge(ifp);
645
646	/* remove route to interface local allnodes multicast (ff01::1) */
647	bzero(&sin6, sizeof(sin6));
648	sin6.sin6_len = sizeof(struct sockaddr_in6);
649	sin6.sin6_family = AF_INET6;
650	sin6.sin6_addr = in6addr_intfacelocal_allnodes;
651	sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
652	rt = rtalloc(sin6tosa(&sin6), 0, ifp->if_rdomain);
653	if (rt && rt->rt_ifp == ifp) {
654		struct rt_addrinfo info;
655
656		bzero(&info, sizeof(info));
657		info.rti_flags = rt->rt_flags;
658		info.rti_info[RTAX_DST] = rt_key(rt);
659		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
660		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
661		rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL,
662		    ifp->if_rdomain);
663		rtfree(rt);
664	}
665
666	/* remove route to link-local allnodes multicast (ff02::1) */
667	bzero(&sin6, sizeof(sin6));
668	sin6.sin6_len = sizeof(struct sockaddr_in6);
669	sin6.sin6_family = AF_INET6;
670	sin6.sin6_addr = in6addr_linklocal_allnodes;
671	sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
672	rt = rtalloc(sin6tosa(&sin6), 0, ifp->if_rdomain);
673	if (rt && rt->rt_ifp == ifp) {
674		struct rt_addrinfo info;
675
676		bzero(&info, sizeof(info));
677		info.rti_flags = rt->rt_flags;
678		info.rti_info[RTAX_DST] = rt_key(rt);
679		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
680		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
681		rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL,
682		    ifp->if_rdomain);
683		rtfree(rt);
684	}
685
686	if (ifp->if_xflags & IFXF_AUTOCONF6) {
687		nd6_rs_timeout_count--;
688		if (nd6_rs_timeout_count == 0)
689			timeout_del(&nd6_rs_output_timer);
690		if (RS_LHCOOKIE(ifp) != NULL)
691			hook_disestablish(ifp->if_linkstatehooks,
692			    RS_LHCOOKIE(ifp));
693	}
694}
695