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