xref: /freebsd/sys/netinet6/in6_ifattach.c (revision 9080e7d02381b66da47ef84a7083bba935d00f22)
1caf43b02SWarner Losh /*-
282cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
382cd038dSYoshinobu Inoue  * All rights reserved.
482cd038dSYoshinobu Inoue  *
582cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
682cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
782cd038dSYoshinobu Inoue  * are met:
882cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
982cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1082cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1282cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1382cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1482cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1582cd038dSYoshinobu Inoue  *    without specific prior written permission.
1682cd038dSYoshinobu Inoue  *
1782cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1882cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1982cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2082cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2182cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2282cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2382cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2482cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2582cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2682cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2782cd038dSYoshinobu Inoue  * SUCH DAMAGE.
28b48287a3SDavid E. O'Brien  *
29b48287a3SDavid E. O'Brien  *	$KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
3082cd038dSYoshinobu Inoue  */
3182cd038dSYoshinobu Inoue 
32b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
33b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$");
34b48287a3SDavid E. O'Brien 
3582cd038dSYoshinobu Inoue #include <sys/param.h>
3682cd038dSYoshinobu Inoue #include <sys/systm.h>
3782cd038dSYoshinobu Inoue #include <sys/malloc.h>
3882cd038dSYoshinobu Inoue #include <sys/socket.h>
3982cd038dSYoshinobu Inoue #include <sys/sockio.h>
4076ca6f88SJamie Gritton #include <sys/jail.h>
4182cd038dSYoshinobu Inoue #include <sys/kernel.h>
42530c0060SRobert Watson #include <sys/proc.h>
4333841545SHajimu UMEMOTO #include <sys/syslog.h>
4482cd038dSYoshinobu Inoue #include <sys/md5.h>
4582cd038dSYoshinobu Inoue 
4682cd038dSYoshinobu Inoue #include <net/if.h>
4776039bc8SGleb Smirnoff #include <net/if_var.h>
4882cd038dSYoshinobu Inoue #include <net/if_dl.h>
4982cd038dSYoshinobu Inoue #include <net/if_types.h>
5082cd038dSYoshinobu Inoue #include <net/route.h>
514b79449eSBjoern A. Zeeb #include <net/vnet.h>
5282cd038dSYoshinobu Inoue 
5382cd038dSYoshinobu Inoue #include <netinet/in.h>
5482cd038dSYoshinobu Inoue #include <netinet/in_var.h>
5582cd038dSYoshinobu Inoue #include <netinet/if_ether.h>
56e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h>
57eddfbb76SRobert Watson #include <netinet/ip_var.h>
58eddfbb76SRobert Watson #include <netinet/udp.h>
59eddfbb76SRobert Watson #include <netinet/udp_var.h>
6082cd038dSYoshinobu Inoue 
61686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
6282cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
6333841545SHajimu UMEMOTO #include <netinet6/in6_var.h>
64e43cc4aeSHajimu UMEMOTO #include <netinet6/in6_pcb.h>
6582cd038dSYoshinobu Inoue #include <netinet6/in6_ifattach.h>
6682cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
6782cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
6833cde130SBruce M Simpson #include <netinet6/mld6_var.h>
69686cdd19SJun-ichiro itojun Hagino #include <netinet6/scope6_var.h>
7082cd038dSYoshinobu Inoue 
7182cea7e6SBjoern A. Zeeb VNET_DEFINE(unsigned long, in6_maxmtu) = 0;
72eddfbb76SRobert Watson 
7382cea7e6SBjoern A. Zeeb #ifdef IP6_AUTO_LINKLOCAL
7482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL;
7582cea7e6SBjoern A. Zeeb #else
7682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_auto_linklocal) = 1;	/* enabled by default */
7782cea7e6SBjoern A. Zeeb #endif
7882cea7e6SBjoern A. Zeeb 
7982cea7e6SBjoern A. Zeeb VNET_DEFINE(struct callout, in6_tmpaddrtimer_ch);
801e77c105SRobert Watson #define	V_in6_tmpaddrtimer_ch		VNET(in6_tmpaddrtimer_ch)
81eddfbb76SRobert Watson 
82eddfbb76SRobert Watson VNET_DECLARE(struct inpcbinfo, ripcbinfo);
831e77c105SRobert Watson #define	V_ripcbinfo			VNET(ripcbinfo)
84e43cc4aeSHajimu UMEMOTO 
859233d8f3SDavid E. O'Brien static int get_rand_ifid(struct ifnet *, struct in6_addr *);
869233d8f3SDavid E. O'Brien static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
879233d8f3SDavid E. O'Brien static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
889233d8f3SDavid E. O'Brien static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
899233d8f3SDavid E. O'Brien static int in6_ifattach_loopback(struct ifnet *);
909233d8f3SDavid E. O'Brien static void in6_purgemaddrs(struct ifnet *);
9182cd038dSYoshinobu Inoue 
92686cdd19SJun-ichiro itojun Hagino #define EUI64_GBIT	0x01
93686cdd19SJun-ichiro itojun Hagino #define EUI64_UBIT	0x02
94686cdd19SJun-ichiro itojun Hagino #define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
95686cdd19SJun-ichiro itojun Hagino #define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
96686cdd19SJun-ichiro itojun Hagino #define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
97686cdd19SJun-ichiro itojun Hagino #define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
98686cdd19SJun-ichiro itojun Hagino #define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
9982cd038dSYoshinobu Inoue 
100686cdd19SJun-ichiro itojun Hagino #define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
101686cdd19SJun-ichiro itojun Hagino #define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
10282cd038dSYoshinobu Inoue 
10382cd038dSYoshinobu Inoue /*
10482cd038dSYoshinobu Inoue  * Generate a last-resort interface identifier, when the machine has no
10582cd038dSYoshinobu Inoue  * IEEE802/EUI64 address sources.
106686cdd19SJun-ichiro itojun Hagino  * The goal here is to get an interface identifier that is
107686cdd19SJun-ichiro itojun Hagino  * (1) random enough and (2) does not change across reboot.
108686cdd19SJun-ichiro itojun Hagino  * We currently use MD5(hostname) for it.
1091272577eSXin LI  *
1101272577eSXin LI  * in6 - upper 64bits are preserved
11182cd038dSYoshinobu Inoue  */
11282cd038dSYoshinobu Inoue static int
1131272577eSXin LI get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
11482cd038dSYoshinobu Inoue {
11582cd038dSYoshinobu Inoue 	MD5_CTX ctxt;
11676ca6f88SJamie Gritton 	struct prison *pr;
11782cd038dSYoshinobu Inoue 	u_int8_t digest[16];
1184f7d1876SRobert Watson 	int hostnamelen;
11982cd038dSYoshinobu Inoue 
12076ca6f88SJamie Gritton 	pr = curthread->td_ucred->cr_prison;
12176ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
122c1f19219SJamie Gritton 	hostnamelen = strlen(pr->pr_hostname);
123686cdd19SJun-ichiro itojun Hagino #if 0
124686cdd19SJun-ichiro itojun Hagino 	/* we need at least several letters as seed for ifid */
12576ca6f88SJamie Gritton 	if (hostnamelen < 3) {
12676ca6f88SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
127686cdd19SJun-ichiro itojun Hagino 		return -1;
12876ca6f88SJamie Gritton 	}
129686cdd19SJun-ichiro itojun Hagino #endif
130686cdd19SJun-ichiro itojun Hagino 
13182cd038dSYoshinobu Inoue 	/* generate 8 bytes of pseudo-random value. */
13282cd038dSYoshinobu Inoue 	bzero(&ctxt, sizeof(ctxt));
13382cd038dSYoshinobu Inoue 	MD5Init(&ctxt);
134c1f19219SJamie Gritton 	MD5Update(&ctxt, pr->pr_hostname, hostnamelen);
13576ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
13682cd038dSYoshinobu Inoue 	MD5Final(digest, &ctxt);
13782cd038dSYoshinobu Inoue 
138686cdd19SJun-ichiro itojun Hagino 	/* assumes sizeof(digest) > sizeof(ifid) */
139686cdd19SJun-ichiro itojun Hagino 	bcopy(digest, &in6->s6_addr[8], 8);
14082cd038dSYoshinobu Inoue 
14182cd038dSYoshinobu Inoue 	/* make sure to set "u" bit to local, and "g" bit to individual. */
142686cdd19SJun-ichiro itojun Hagino 	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
143686cdd19SJun-ichiro itojun Hagino 	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
144686cdd19SJun-ichiro itojun Hagino 
145686cdd19SJun-ichiro itojun Hagino 	/* convert EUI64 into IPv6 interface identifier */
146686cdd19SJun-ichiro itojun Hagino 	EUI64_TO_IFID(in6);
14782cd038dSYoshinobu Inoue 
14882cd038dSYoshinobu Inoue 	return 0;
14982cd038dSYoshinobu Inoue }
15082cd038dSYoshinobu Inoue 
15133841545SHajimu UMEMOTO static int
1521272577eSXin LI generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
15333841545SHajimu UMEMOTO {
15433841545SHajimu UMEMOTO 	MD5_CTX ctxt;
15533841545SHajimu UMEMOTO 	u_int8_t seed[16], digest[16], nullbuf[8];
15633841545SHajimu UMEMOTO 	u_int32_t val32;
15733841545SHajimu UMEMOTO 
1581272577eSXin LI 	/* If there's no history, start with a random seed. */
15933841545SHajimu UMEMOTO 	bzero(nullbuf, sizeof(nullbuf));
16033841545SHajimu UMEMOTO 	if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
16133841545SHajimu UMEMOTO 		int i;
16233841545SHajimu UMEMOTO 
16333841545SHajimu UMEMOTO 		for (i = 0; i < 2; i++) {
164a02e1e2bSHajimu UMEMOTO 			val32 = arc4random();
16533841545SHajimu UMEMOTO 			bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
16633841545SHajimu UMEMOTO 		}
1677efe5d92SHajimu UMEMOTO 	} else
16833841545SHajimu UMEMOTO 		bcopy(seed0, seed, 8);
16933841545SHajimu UMEMOTO 
17033841545SHajimu UMEMOTO 	/* copy the right-most 64-bits of the given address */
17133841545SHajimu UMEMOTO 	/* XXX assumption on the size of IFID */
17233841545SHajimu UMEMOTO 	bcopy(seed1, &seed[8], 8);
17333841545SHajimu UMEMOTO 
17433841545SHajimu UMEMOTO 	if (0) {		/* for debugging purposes only */
17533841545SHajimu UMEMOTO 		int i;
17633841545SHajimu UMEMOTO 
17733841545SHajimu UMEMOTO 		printf("generate_tmp_ifid: new randomized ID from: ");
17833841545SHajimu UMEMOTO 		for (i = 0; i < 16; i++)
17933841545SHajimu UMEMOTO 			printf("%02x", seed[i]);
18033841545SHajimu UMEMOTO 		printf(" ");
18133841545SHajimu UMEMOTO 	}
18233841545SHajimu UMEMOTO 
18333841545SHajimu UMEMOTO 	/* generate 16 bytes of pseudo-random value. */
18433841545SHajimu UMEMOTO 	bzero(&ctxt, sizeof(ctxt));
18533841545SHajimu UMEMOTO 	MD5Init(&ctxt);
18633841545SHajimu UMEMOTO 	MD5Update(&ctxt, seed, sizeof(seed));
18733841545SHajimu UMEMOTO 	MD5Final(digest, &ctxt);
18833841545SHajimu UMEMOTO 
18933841545SHajimu UMEMOTO 	/*
19033841545SHajimu UMEMOTO 	 * RFC 3041 3.2.1. (3)
19133841545SHajimu UMEMOTO 	 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
19233841545SHajimu UMEMOTO 	 * left-most bit is numbered 0) to zero.
19333841545SHajimu UMEMOTO 	 */
19433841545SHajimu UMEMOTO 	bcopy(digest, ret, 8);
19533841545SHajimu UMEMOTO 	ret[0] &= ~EUI64_UBIT;
19633841545SHajimu UMEMOTO 
19733841545SHajimu UMEMOTO 	/*
19833841545SHajimu UMEMOTO 	 * XXX: we'd like to ensure that the generated value is not zero
19933841545SHajimu UMEMOTO 	 * for simplicity.  If the caclculated digest happens to be zero,
20033841545SHajimu UMEMOTO 	 * use a random non-zero value as the last resort.
20133841545SHajimu UMEMOTO 	 */
20233841545SHajimu UMEMOTO 	if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
2032ce62dceSSUZUKI Shinsuke 		nd6log((LOG_INFO,
2042ce62dceSSUZUKI Shinsuke 		    "generate_tmp_ifid: computed MD5 value is zero.\n"));
20533841545SHajimu UMEMOTO 
206a02e1e2bSHajimu UMEMOTO 		val32 = arc4random();
20733841545SHajimu UMEMOTO 		val32 = 1 + (val32 % (0xffffffff - 1));
20833841545SHajimu UMEMOTO 	}
20933841545SHajimu UMEMOTO 
21033841545SHajimu UMEMOTO 	/*
21133841545SHajimu UMEMOTO 	 * RFC 3041 3.2.1. (4)
21233841545SHajimu UMEMOTO 	 * Take the rightmost 64-bits of the MD5 digest and save them in
21333841545SHajimu UMEMOTO 	 * stable storage as the history value to be used in the next
21433841545SHajimu UMEMOTO 	 * iteration of the algorithm.
21533841545SHajimu UMEMOTO 	 */
21633841545SHajimu UMEMOTO 	bcopy(&digest[8], seed0, 8);
21733841545SHajimu UMEMOTO 
21833841545SHajimu UMEMOTO 	if (0) {		/* for debugging purposes only */
21933841545SHajimu UMEMOTO 		int i;
22033841545SHajimu UMEMOTO 
22133841545SHajimu UMEMOTO 		printf("to: ");
22233841545SHajimu UMEMOTO 		for (i = 0; i < 16; i++)
22333841545SHajimu UMEMOTO 			printf("%02x", digest[i]);
22433841545SHajimu UMEMOTO 		printf("\n");
22533841545SHajimu UMEMOTO 	}
22633841545SHajimu UMEMOTO 
22733841545SHajimu UMEMOTO 	return 0;
22833841545SHajimu UMEMOTO }
22933841545SHajimu UMEMOTO 
23082cd038dSYoshinobu Inoue /*
231686cdd19SJun-ichiro itojun Hagino  * Get interface identifier for the specified interface.
232686cdd19SJun-ichiro itojun Hagino  * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
2331272577eSXin LI  *
2341272577eSXin LI  * in6 - upper 64bits are preserved
23582cd038dSYoshinobu Inoue  */
236d28bde66SSUZUKI Shinsuke int
2371272577eSXin LI in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
23882cd038dSYoshinobu Inoue {
239686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ifa;
240686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_dl *sdl;
241686cdd19SJun-ichiro itojun Hagino 	u_int8_t *addr;
242686cdd19SJun-ichiro itojun Hagino 	size_t addrlen;
243686cdd19SJun-ichiro itojun Hagino 	static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
244686cdd19SJun-ichiro itojun Hagino 	static u_int8_t allone[8] =
245686cdd19SJun-ichiro itojun Hagino 		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
246686cdd19SJun-ichiro itojun Hagino 
247137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
248c4dd3fe1SRobert Watson 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
24982cd038dSYoshinobu Inoue 		if (ifa->ifa_addr->sa_family != AF_LINK)
25082cd038dSYoshinobu Inoue 			continue;
25182cd038dSYoshinobu Inoue 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
25282cd038dSYoshinobu Inoue 		if (sdl == NULL)
25382cd038dSYoshinobu Inoue 			continue;
25482cd038dSYoshinobu Inoue 		if (sdl->sdl_alen == 0)
25582cd038dSYoshinobu Inoue 			continue;
256686cdd19SJun-ichiro itojun Hagino 
257686cdd19SJun-ichiro itojun Hagino 		goto found;
258686cdd19SJun-ichiro itojun Hagino 	}
259137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
260686cdd19SJun-ichiro itojun Hagino 
261686cdd19SJun-ichiro itojun Hagino 	return -1;
262686cdd19SJun-ichiro itojun Hagino 
263686cdd19SJun-ichiro itojun Hagino found:
2648c0fec80SRobert Watson 	IF_ADDR_LOCK_ASSERT(ifp);
265686cdd19SJun-ichiro itojun Hagino 	addr = LLADDR(sdl);
266686cdd19SJun-ichiro itojun Hagino 	addrlen = sdl->sdl_alen;
267686cdd19SJun-ichiro itojun Hagino 
268686cdd19SJun-ichiro itojun Hagino 	/* get EUI64 */
26982cd038dSYoshinobu Inoue 	switch (ifp->if_type) {
270af805644SHiroki Sato 	case IFT_BRIDGE:
27182cd038dSYoshinobu Inoue 	case IFT_ETHER:
272e9a23b55SJohn Hay 	case IFT_L2VLAN:
27382cd038dSYoshinobu Inoue 	case IFT_FDDI:
2742049fdeeSMatthew N. Dodd 	case IFT_ISO88025:
27582cd038dSYoshinobu Inoue 	case IFT_ATM:
27633841545SHajimu UMEMOTO 	case IFT_IEEE1394:
27733841545SHajimu UMEMOTO #ifdef IFT_IEEE80211
27833841545SHajimu UMEMOTO 	case IFT_IEEE80211:
27933841545SHajimu UMEMOTO #endif
28082cd038dSYoshinobu Inoue 		/* IEEE802/EUI64 cases - what others? */
28133841545SHajimu UMEMOTO 		/* IEEE1394 uses 16byte length address starting with EUI64 */
28233841545SHajimu UMEMOTO 		if (addrlen > 8)
28333841545SHajimu UMEMOTO 			addrlen = 8;
284686cdd19SJun-ichiro itojun Hagino 
285686cdd19SJun-ichiro itojun Hagino 		/* look at IEEE802/EUI64 only */
2861e6a4139SRobert Watson 		if (addrlen != 8 && addrlen != 6) {
287137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
288686cdd19SJun-ichiro itojun Hagino 			return -1;
2891e6a4139SRobert Watson 		}
290686cdd19SJun-ichiro itojun Hagino 
29182cd038dSYoshinobu Inoue 		/*
292686cdd19SJun-ichiro itojun Hagino 		 * check for invalid MAC address - on bsdi, we see it a lot
293686cdd19SJun-ichiro itojun Hagino 		 * since wildboar configures all-zero MAC on pccard before
294686cdd19SJun-ichiro itojun Hagino 		 * card insertion.
29582cd038dSYoshinobu Inoue 		 */
2961e6a4139SRobert Watson 		if (bcmp(addr, allzero, addrlen) == 0) {
297137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
298686cdd19SJun-ichiro itojun Hagino 			return -1;
2991e6a4139SRobert Watson 		}
3001e6a4139SRobert Watson 		if (bcmp(addr, allone, addrlen) == 0) {
301137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
302686cdd19SJun-ichiro itojun Hagino 			return -1;
3031e6a4139SRobert Watson 		}
304686cdd19SJun-ichiro itojun Hagino 
305686cdd19SJun-ichiro itojun Hagino 		/* make EUI64 address */
306686cdd19SJun-ichiro itojun Hagino 		if (addrlen == 8)
307686cdd19SJun-ichiro itojun Hagino 			bcopy(addr, &in6->s6_addr[8], 8);
308686cdd19SJun-ichiro itojun Hagino 		else if (addrlen == 6) {
309686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[8] = addr[0];
310686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[9] = addr[1];
311686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[10] = addr[2];
312686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[11] = 0xff;
313686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[12] = 0xfe;
314686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[13] = addr[3];
315686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[14] = addr[4];
316686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[15] = addr[5];
317686cdd19SJun-ichiro itojun Hagino 		}
31882cd038dSYoshinobu Inoue 		break;
319686cdd19SJun-ichiro itojun Hagino 
32082cd038dSYoshinobu Inoue 	case IFT_ARCNET:
3211e6a4139SRobert Watson 		if (addrlen != 1) {
322137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
323686cdd19SJun-ichiro itojun Hagino 			return -1;
3241e6a4139SRobert Watson 		}
3251e6a4139SRobert Watson 		if (!addr[0]) {
326137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
327686cdd19SJun-ichiro itojun Hagino 			return -1;
3281e6a4139SRobert Watson 		}
329686cdd19SJun-ichiro itojun Hagino 
330686cdd19SJun-ichiro itojun Hagino 		bzero(&in6->s6_addr[8], 8);
331686cdd19SJun-ichiro itojun Hagino 		in6->s6_addr[15] = addr[0];
332686cdd19SJun-ichiro itojun Hagino 
33382cd038dSYoshinobu Inoue 		/*
334686cdd19SJun-ichiro itojun Hagino 		 * due to insufficient bitwidth, we mark it local.
33582cd038dSYoshinobu Inoue 		 */
336686cdd19SJun-ichiro itojun Hagino 		in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
337686cdd19SJun-ichiro itojun Hagino 		in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
33882cd038dSYoshinobu Inoue 		break;
339686cdd19SJun-ichiro itojun Hagino 
340686cdd19SJun-ichiro itojun Hagino 	case IFT_GIF:
341686cdd19SJun-ichiro itojun Hagino #ifdef IFT_STF
342686cdd19SJun-ichiro itojun Hagino 	case IFT_STF:
34382cd038dSYoshinobu Inoue #endif
344686cdd19SJun-ichiro itojun Hagino 		/*
34533841545SHajimu UMEMOTO 		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
346686cdd19SJun-ichiro itojun Hagino 		 * however, IPv4 address is not very suitable as unique
347686cdd19SJun-ichiro itojun Hagino 		 * identifier source (can be renumbered).
348686cdd19SJun-ichiro itojun Hagino 		 * we don't do this.
349686cdd19SJun-ichiro itojun Hagino 		 */
350137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
351686cdd19SJun-ichiro itojun Hagino 		return -1;
35282cd038dSYoshinobu Inoue 
353686cdd19SJun-ichiro itojun Hagino 	default:
354137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
355686cdd19SJun-ichiro itojun Hagino 		return -1;
356686cdd19SJun-ichiro itojun Hagino 	}
35782cd038dSYoshinobu Inoue 
358686cdd19SJun-ichiro itojun Hagino 	/* sanity check: g bit must not indicate "group" */
3591e6a4139SRobert Watson 	if (EUI64_GROUP(in6)) {
360137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
361686cdd19SJun-ichiro itojun Hagino 		return -1;
3621e6a4139SRobert Watson 	}
36382cd038dSYoshinobu Inoue 
364686cdd19SJun-ichiro itojun Hagino 	/* convert EUI64 into IPv6 interface identifier */
365686cdd19SJun-ichiro itojun Hagino 	EUI64_TO_IFID(in6);
366686cdd19SJun-ichiro itojun Hagino 
367686cdd19SJun-ichiro itojun Hagino 	/*
368686cdd19SJun-ichiro itojun Hagino 	 * sanity check: ifid must not be all zero, avoid conflict with
369686cdd19SJun-ichiro itojun Hagino 	 * subnet router anycast
370686cdd19SJun-ichiro itojun Hagino 	 */
371686cdd19SJun-ichiro itojun Hagino 	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
372686cdd19SJun-ichiro itojun Hagino 	    bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
373137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
374686cdd19SJun-ichiro itojun Hagino 		return -1;
375686cdd19SJun-ichiro itojun Hagino 	}
37682cd038dSYoshinobu Inoue 
377137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
37882cd038dSYoshinobu Inoue 	return 0;
379686cdd19SJun-ichiro itojun Hagino }
380686cdd19SJun-ichiro itojun Hagino 
381686cdd19SJun-ichiro itojun Hagino /*
382686cdd19SJun-ichiro itojun Hagino  * Get interface identifier for the specified interface.  If it is not
383686cdd19SJun-ichiro itojun Hagino  * available on ifp0, borrow interface identifier from other information
384686cdd19SJun-ichiro itojun Hagino  * sources.
3851272577eSXin LI  *
3861272577eSXin LI  * altifp - secondary EUI64 source
387686cdd19SJun-ichiro itojun Hagino  */
388686cdd19SJun-ichiro itojun Hagino static int
3891272577eSXin LI get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
3901272577eSXin LI     struct in6_addr *in6)
391686cdd19SJun-ichiro itojun Hagino {
392686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
393686cdd19SJun-ichiro itojun Hagino 
394686cdd19SJun-ichiro itojun Hagino 	/* first, try to get it from the interface itself */
395d28bde66SSUZUKI Shinsuke 	if (in6_get_hw_ifid(ifp0, in6) == 0) {
39633841545SHajimu UMEMOTO 		nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
39733841545SHajimu UMEMOTO 		    if_name(ifp0)));
398686cdd19SJun-ichiro itojun Hagino 		goto success;
399686cdd19SJun-ichiro itojun Hagino 	}
400686cdd19SJun-ichiro itojun Hagino 
401686cdd19SJun-ichiro itojun Hagino 	/* try secondary EUI64 source. this basically is for ATM PVC */
402d28bde66SSUZUKI Shinsuke 	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
40333841545SHajimu UMEMOTO 		nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
40433841545SHajimu UMEMOTO 		    if_name(ifp0), if_name(altifp)));
405686cdd19SJun-ichiro itojun Hagino 		goto success;
406686cdd19SJun-ichiro itojun Hagino 	}
407686cdd19SJun-ichiro itojun Hagino 
408686cdd19SJun-ichiro itojun Hagino 	/* next, try to get it from some other hardware interface */
40977dfcdc4SRobert Watson 	IFNET_RLOCK_NOSLEEP();
4103b0b2840SJohn Baldwin 	TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
411686cdd19SJun-ichiro itojun Hagino 		if (ifp == ifp0)
412686cdd19SJun-ichiro itojun Hagino 			continue;
413d28bde66SSUZUKI Shinsuke 		if (in6_get_hw_ifid(ifp, in6) != 0)
414686cdd19SJun-ichiro itojun Hagino 			continue;
415686cdd19SJun-ichiro itojun Hagino 
416686cdd19SJun-ichiro itojun Hagino 		/*
417686cdd19SJun-ichiro itojun Hagino 		 * to borrow ifid from other interface, ifid needs to be
418686cdd19SJun-ichiro itojun Hagino 		 * globally unique
419686cdd19SJun-ichiro itojun Hagino 		 */
420686cdd19SJun-ichiro itojun Hagino 		if (IFID_UNIVERSAL(in6)) {
42133841545SHajimu UMEMOTO 			nd6log((LOG_DEBUG,
42233841545SHajimu UMEMOTO 			    "%s: borrow interface identifier from %s\n",
42333841545SHajimu UMEMOTO 			    if_name(ifp0), if_name(ifp)));
42477dfcdc4SRobert Watson 			IFNET_RUNLOCK_NOSLEEP();
425686cdd19SJun-ichiro itojun Hagino 			goto success;
426686cdd19SJun-ichiro itojun Hagino 		}
427686cdd19SJun-ichiro itojun Hagino 	}
42877dfcdc4SRobert Watson 	IFNET_RUNLOCK_NOSLEEP();
429686cdd19SJun-ichiro itojun Hagino 
430686cdd19SJun-ichiro itojun Hagino 	/* last resort: get from random number source */
431686cdd19SJun-ichiro itojun Hagino 	if (get_rand_ifid(ifp, in6) == 0) {
43233841545SHajimu UMEMOTO 		nd6log((LOG_DEBUG,
43333841545SHajimu UMEMOTO 		    "%s: interface identifier generated by random number\n",
43433841545SHajimu UMEMOTO 		    if_name(ifp0)));
435686cdd19SJun-ichiro itojun Hagino 		goto success;
436686cdd19SJun-ichiro itojun Hagino 	}
437686cdd19SJun-ichiro itojun Hagino 
438de9c893eSJun-ichiro itojun Hagino 	printf("%s: failed to get interface identifier\n", if_name(ifp0));
439686cdd19SJun-ichiro itojun Hagino 	return -1;
440686cdd19SJun-ichiro itojun Hagino 
441686cdd19SJun-ichiro itojun Hagino success:
4427efe5d92SHajimu UMEMOTO 	nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
4437efe5d92SHajimu UMEMOTO 	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
4447efe5d92SHajimu UMEMOTO 	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
44533841545SHajimu UMEMOTO 	    in6->s6_addr[14], in6->s6_addr[15]));
446686cdd19SJun-ichiro itojun Hagino 	return 0;
447686cdd19SJun-ichiro itojun Hagino }
448686cdd19SJun-ichiro itojun Hagino 
4491272577eSXin LI /*
4501272577eSXin LI  * altifp - secondary EUI64 source
4511272577eSXin LI  */
452686cdd19SJun-ichiro itojun Hagino static int
4531272577eSXin LI in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
454686cdd19SJun-ichiro itojun Hagino {
455686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia;
45633841545SHajimu UMEMOTO 	struct in6_aliasreq ifra;
457743eee66SSUZUKI Shinsuke 	struct nd_prefixctl pr0;
4581dc8f6a8SAlexander V. Chernikov 	int error;
459686cdd19SJun-ichiro itojun Hagino 
460686cdd19SJun-ichiro itojun Hagino 	/*
46133841545SHajimu UMEMOTO 	 * configure link-local address.
462686cdd19SJun-ichiro itojun Hagino 	 */
463*9080e7d0SAlexander V. Chernikov 	in6_prepare_ifra(&ifra, NULL, &in6mask64);
464686cdd19SJun-ichiro itojun Hagino 
465a1f7e5f8SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
46633841545SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
46733841545SHajimu UMEMOTO 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
46833841545SHajimu UMEMOTO 		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
46933841545SHajimu UMEMOTO 		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
47033841545SHajimu UMEMOTO 	} else {
47133841545SHajimu UMEMOTO 		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
47233841545SHajimu UMEMOTO 			nd6log((LOG_ERR,
47333841545SHajimu UMEMOTO 			    "%s: no ifid available\n", if_name(ifp)));
4747efe5d92SHajimu UMEMOTO 			return (-1);
47533841545SHajimu UMEMOTO 		}
47633841545SHajimu UMEMOTO 	}
477a1f7e5f8SHajimu UMEMOTO 	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
478a1f7e5f8SHajimu UMEMOTO 		return (-1);
47933841545SHajimu UMEMOTO 
48033841545SHajimu UMEMOTO 	/* link-local addresses should NEVER expire. */
48133841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
48233841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
483686cdd19SJun-ichiro itojun Hagino 
48433841545SHajimu UMEMOTO 	/*
48533841545SHajimu UMEMOTO 	 * Now call in6_update_ifa() to do a bunch of procedures to configure
4862ce62dceSSUZUKI Shinsuke 	 * a link-local address. We can set the 3rd argument to NULL, because
48788ff5695SSUZUKI Shinsuke 	 * we know there's no other link-local address on the interface
48888ff5695SSUZUKI Shinsuke 	 * and therefore we are adding one (instead of updating one).
48933841545SHajimu UMEMOTO 	 */
490743eee66SSUZUKI Shinsuke 	if ((error = in6_update_ifa(ifp, &ifra, NULL,
491743eee66SSUZUKI Shinsuke 				    IN6_IFAUPDATE_DADDELAY)) != 0) {
49233841545SHajimu UMEMOTO 		/*
49333841545SHajimu UMEMOTO 		 * XXX: When the interface does not support IPv6, this call
49433841545SHajimu UMEMOTO 		 * would fail in the SIOCSIFADDR ioctl.  I believe the
49533841545SHajimu UMEMOTO 		 * notification is rather confusing in this case, so just
4967efe5d92SHajimu UMEMOTO 		 * suppress it.  (jinmei@kame.net 20010130)
49733841545SHajimu UMEMOTO 		 */
49833841545SHajimu UMEMOTO 		if (error != EAFNOSUPPORT)
4992ce62dceSSUZUKI Shinsuke 			nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
50033841545SHajimu UMEMOTO 			    "configure a link-local address on %s "
50133841545SHajimu UMEMOTO 			    "(errno=%d)\n",
5022ce62dceSSUZUKI Shinsuke 			    if_name(ifp), error));
50333841545SHajimu UMEMOTO 		return (-1);
504686cdd19SJun-ichiro itojun Hagino 	}
50533841545SHajimu UMEMOTO 
50633841545SHajimu UMEMOTO 	ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
507f67e13d6SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia == NULL, ifp=%p", __func__, ifp));
508f67e13d6SBjoern A. Zeeb 
5098c0fec80SRobert Watson 	ifa_free(&ia->ia_ifa);
510686cdd19SJun-ichiro itojun Hagino 
51133841545SHajimu UMEMOTO 	/*
5127efe5d92SHajimu UMEMOTO 	 * Make the link-local prefix (fe80::%link/64) as on-link.
51333841545SHajimu UMEMOTO 	 * Since we'd like to manage prefixes separately from addresses,
51433841545SHajimu UMEMOTO 	 * we make an ND6 prefix structure for the link-local prefix,
51533841545SHajimu UMEMOTO 	 * and add it to the prefix list as a never-expire prefix.
51633841545SHajimu UMEMOTO 	 * XXX: this change might affect some existing code base...
51733841545SHajimu UMEMOTO 	 */
51833841545SHajimu UMEMOTO 	bzero(&pr0, sizeof(pr0));
51933841545SHajimu UMEMOTO 	pr0.ndpr_ifp = ifp;
52033841545SHajimu UMEMOTO 	/* this should be 64 at this moment. */
52133841545SHajimu UMEMOTO 	pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
52233841545SHajimu UMEMOTO 	pr0.ndpr_prefix = ifra.ifra_addr;
52333841545SHajimu UMEMOTO 	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
5241dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, &in6mask64);
52533841545SHajimu UMEMOTO 	/*
52633841545SHajimu UMEMOTO 	 * Initialize parameters.  The link-local prefix must always be
52733841545SHajimu UMEMOTO 	 * on-link, and its lifetimes never expire.
52833841545SHajimu UMEMOTO 	 */
52933841545SHajimu UMEMOTO 	pr0.ndpr_raf_onlink = 1;
53033841545SHajimu UMEMOTO 	pr0.ndpr_raf_auto = 1;	/* probably meaningless */
53133841545SHajimu UMEMOTO 	pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
53233841545SHajimu UMEMOTO 	pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
53333841545SHajimu UMEMOTO 	/*
53433841545SHajimu UMEMOTO 	 * Since there is no other link-local addresses, nd6_prefix_lookup()
53533841545SHajimu UMEMOTO 	 * probably returns NULL.  However, we cannot always expect the result.
53633841545SHajimu UMEMOTO 	 * For example, if we first remove the (only) existing link-local
53733841545SHajimu UMEMOTO 	 * address, and then reconfigure another one, the prefix is still
53833841545SHajimu UMEMOTO 	 * valid with referring to the old link-local address.
53933841545SHajimu UMEMOTO 	 */
54033841545SHajimu UMEMOTO 	if (nd6_prefix_lookup(&pr0) == NULL) {
54133841545SHajimu UMEMOTO 		if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
54233841545SHajimu UMEMOTO 			return (error);
543686cdd19SJun-ichiro itojun Hagino 	}
544686cdd19SJun-ichiro itojun Hagino 
545686cdd19SJun-ichiro itojun Hagino 	return 0;
546686cdd19SJun-ichiro itojun Hagino }
547686cdd19SJun-ichiro itojun Hagino 
5481272577eSXin LI /*
5491272577eSXin LI  * ifp - must be IFT_LOOP
5501272577eSXin LI  */
551686cdd19SJun-ichiro itojun Hagino static int
5521272577eSXin LI in6_ifattach_loopback(struct ifnet *ifp)
553686cdd19SJun-ichiro itojun Hagino {
55433841545SHajimu UMEMOTO 	struct in6_aliasreq ifra;
55533841545SHajimu UMEMOTO 	int error;
55633841545SHajimu UMEMOTO 
557*9080e7d0SAlexander V. Chernikov 	in6_prepare_ifra(&ifra, &in6addr_loopback, &in6mask128);
558686cdd19SJun-ichiro itojun Hagino 
559686cdd19SJun-ichiro itojun Hagino 	/*
560686cdd19SJun-ichiro itojun Hagino 	 * Always initialize ia_dstaddr (= broadcast address) to loopback
56133841545SHajimu UMEMOTO 	 * address.  Follows IPv4 practice - see in_ifinit().
562686cdd19SJun-ichiro itojun Hagino 	 */
56333841545SHajimu UMEMOTO 	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
56433841545SHajimu UMEMOTO 	ifra.ifra_dstaddr.sin6_family = AF_INET6;
56533841545SHajimu UMEMOTO 	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
566686cdd19SJun-ichiro itojun Hagino 
56733841545SHajimu UMEMOTO 	/* the loopback  address should NEVER expire. */
56833841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
56933841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
570686cdd19SJun-ichiro itojun Hagino 
57188ff5695SSUZUKI Shinsuke 	/* we don't need to perform DAD on loopback interfaces. */
57233841545SHajimu UMEMOTO 	ifra.ifra_flags |= IN6_IFF_NODAD;
57333841545SHajimu UMEMOTO 
57433841545SHajimu UMEMOTO 	/* skip registration to the prefix list. XXX should be temporary. */
57533841545SHajimu UMEMOTO 	ifra.ifra_flags |= IN6_IFF_NOPFX;
57633841545SHajimu UMEMOTO 
57733841545SHajimu UMEMOTO 	/*
57888ff5695SSUZUKI Shinsuke 	 * We are sure that this is a newly assigned address, so we can set
57988ff5695SSUZUKI Shinsuke 	 * NULL to the 3rd arg.
58033841545SHajimu UMEMOTO 	 */
581743eee66SSUZUKI Shinsuke 	if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
5822ce62dceSSUZUKI Shinsuke 		nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
58333841545SHajimu UMEMOTO 		    "the loopback address on %s (errno=%d)\n",
5842ce62dceSSUZUKI Shinsuke 		    if_name(ifp), error));
58533841545SHajimu UMEMOTO 		return (-1);
586686cdd19SJun-ichiro itojun Hagino 	}
587686cdd19SJun-ichiro itojun Hagino 
588686cdd19SJun-ichiro itojun Hagino 	return 0;
589686cdd19SJun-ichiro itojun Hagino }
590686cdd19SJun-ichiro itojun Hagino 
591686cdd19SJun-ichiro itojun Hagino /*
592686cdd19SJun-ichiro itojun Hagino  * compute NI group address, based on the current hostname setting.
5935df1b6b5SHiroki Sato  * see RFC 4620.
594686cdd19SJun-ichiro itojun Hagino  *
595686cdd19SJun-ichiro itojun Hagino  * when ifp == NULL, the caller is responsible for filling scopeid.
5965df1b6b5SHiroki Sato  *
5975df1b6b5SHiroki Sato  * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address
5985df1b6b5SHiroki Sato  * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620.
599686cdd19SJun-ichiro itojun Hagino  */
6005df1b6b5SHiroki Sato static int
6015df1b6b5SHiroki Sato in6_nigroup0(struct ifnet *ifp, const char *name, int namelen,
6025df1b6b5SHiroki Sato     struct in6_addr *in6, int oldmcprefix)
603686cdd19SJun-ichiro itojun Hagino {
60476ca6f88SJamie Gritton 	struct prison *pr;
605686cdd19SJun-ichiro itojun Hagino 	const char *p;
60633841545SHajimu UMEMOTO 	u_char *q;
607686cdd19SJun-ichiro itojun Hagino 	MD5_CTX ctxt;
608686cdd19SJun-ichiro itojun Hagino 	u_int8_t digest[16];
609686cdd19SJun-ichiro itojun Hagino 	char l;
61033841545SHajimu UMEMOTO 	char n[64];	/* a single label must not exceed 63 chars */
611686cdd19SJun-ichiro itojun Hagino 
6120304c731SJamie Gritton 	/*
6130304c731SJamie Gritton 	 * If no name is given and namelen is -1,
6140304c731SJamie Gritton 	 * we try to do the hostname lookup ourselves.
6150304c731SJamie Gritton 	 */
6160304c731SJamie Gritton 	if (!name && namelen == -1) {
61776ca6f88SJamie Gritton 		pr = curthread->td_ucred->cr_prison;
61876ca6f88SJamie Gritton 		mtx_lock(&pr->pr_mtx);
619c1f19219SJamie Gritton 		name = pr->pr_hostname;
6200304c731SJamie Gritton 		namelen = strlen(name);
6210304c731SJamie Gritton 	} else
62276ca6f88SJamie Gritton 		pr = NULL;
6230304c731SJamie Gritton 	if (!name || !namelen) {
62476ca6f88SJamie Gritton 		if (pr != NULL)
62576ca6f88SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
626686cdd19SJun-ichiro itojun Hagino 		return -1;
6270304c731SJamie Gritton 	}
628686cdd19SJun-ichiro itojun Hagino 
629686cdd19SJun-ichiro itojun Hagino 	p = name;
630686cdd19SJun-ichiro itojun Hagino 	while (p && *p && *p != '.' && p - name < namelen)
631686cdd19SJun-ichiro itojun Hagino 		p++;
6320304c731SJamie Gritton 	if (p == name || p - name > sizeof(n) - 1) {
63376ca6f88SJamie Gritton 		if (pr != NULL)
63476ca6f88SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
635686cdd19SJun-ichiro itojun Hagino 		return -1;	/* label too long */
6360304c731SJamie Gritton 	}
637686cdd19SJun-ichiro itojun Hagino 	l = p - name;
63833841545SHajimu UMEMOTO 	strncpy(n, name, l);
63976ca6f88SJamie Gritton 	if (pr != NULL)
64076ca6f88SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
64133841545SHajimu UMEMOTO 	n[(int)l] = '\0';
64233841545SHajimu UMEMOTO 	for (q = n; *q; q++) {
64333841545SHajimu UMEMOTO 		if ('A' <= *q && *q <= 'Z')
64433841545SHajimu UMEMOTO 			*q = *q - 'A' + 'a';
64533841545SHajimu UMEMOTO 	}
646686cdd19SJun-ichiro itojun Hagino 
6475df1b6b5SHiroki Sato 	/* generate 16 bytes of pseudo-random value. */
648686cdd19SJun-ichiro itojun Hagino 	bzero(&ctxt, sizeof(ctxt));
649686cdd19SJun-ichiro itojun Hagino 	MD5Init(&ctxt);
650686cdd19SJun-ichiro itojun Hagino 	MD5Update(&ctxt, &l, sizeof(l));
65133841545SHajimu UMEMOTO 	MD5Update(&ctxt, n, l);
652686cdd19SJun-ichiro itojun Hagino 	MD5Final(digest, &ctxt);
653686cdd19SJun-ichiro itojun Hagino 
654686cdd19SJun-ichiro itojun Hagino 	bzero(in6, sizeof(*in6));
6552ce62dceSSUZUKI Shinsuke 	in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
656686cdd19SJun-ichiro itojun Hagino 	in6->s6_addr8[11] = 2;
6575df1b6b5SHiroki Sato 	if (oldmcprefix == 0) {
6585df1b6b5SHiroki Sato 		in6->s6_addr8[12] = 0xff;
6595df1b6b5SHiroki Sato 	 	/* Copy the first 24 bits of 128-bit hash into the address. */
6605df1b6b5SHiroki Sato 		bcopy(digest, &in6->s6_addr8[13], 3);
6615df1b6b5SHiroki Sato 	} else {
6625df1b6b5SHiroki Sato 	 	/* Copy the first 32 bits of 128-bit hash into the address. */
663686cdd19SJun-ichiro itojun Hagino 		bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
6645df1b6b5SHiroki Sato 	}
665a1f7e5f8SHajimu UMEMOTO 	if (in6_setscope(in6, ifp, NULL))
666a1f7e5f8SHajimu UMEMOTO 		return (-1); /* XXX: should not fail */
667686cdd19SJun-ichiro itojun Hagino 
668686cdd19SJun-ichiro itojun Hagino 	return 0;
669686cdd19SJun-ichiro itojun Hagino }
670686cdd19SJun-ichiro itojun Hagino 
6715df1b6b5SHiroki Sato int
6725df1b6b5SHiroki Sato in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
6735df1b6b5SHiroki Sato     struct in6_addr *in6)
6745df1b6b5SHiroki Sato {
6755df1b6b5SHiroki Sato 
6765df1b6b5SHiroki Sato 	return (in6_nigroup0(ifp, name, namelen, in6, 0));
6775df1b6b5SHiroki Sato }
6785df1b6b5SHiroki Sato 
6795df1b6b5SHiroki Sato int
6805df1b6b5SHiroki Sato in6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen,
6815df1b6b5SHiroki Sato     struct in6_addr *in6)
6825df1b6b5SHiroki Sato {
6835df1b6b5SHiroki Sato 
6845df1b6b5SHiroki Sato 	return (in6_nigroup0(ifp, name, namelen, in6, 1));
6855df1b6b5SHiroki Sato }
6865df1b6b5SHiroki Sato 
68782cd038dSYoshinobu Inoue /*
688686cdd19SJun-ichiro itojun Hagino  * XXX multiple loopback interface needs more care.  for instance,
689686cdd19SJun-ichiro itojun Hagino  * nodelocal address needs to be configured onto only one of them.
690686cdd19SJun-ichiro itojun Hagino  * XXX multiple link-local address case
6911272577eSXin LI  *
6921272577eSXin LI  * altifp - secondary EUI64 source
69382cd038dSYoshinobu Inoue  */
69482cd038dSYoshinobu Inoue void
6951272577eSXin LI in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
69682cd038dSYoshinobu Inoue {
697686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia;
698686cdd19SJun-ichiro itojun Hagino 	struct in6_addr in6;
69933841545SHajimu UMEMOTO 
7000de0dd9bSHiroki Sato 	if (ifp->if_afdata[AF_INET6] == NULL)
701cc5934f5SMax Laier 		return;
70282cd038dSYoshinobu Inoue 	/*
703686cdd19SJun-ichiro itojun Hagino 	 * quirks based on interface type
70482cd038dSYoshinobu Inoue 	 */
705686cdd19SJun-ichiro itojun Hagino 	switch (ifp->if_type) {
706686cdd19SJun-ichiro itojun Hagino 	case IFT_STF:
70782cd038dSYoshinobu Inoue 		/*
70888ff5695SSUZUKI Shinsuke 		 * 6to4 interface is a very special kind of beast.
70988ff5695SSUZUKI Shinsuke 		 * no multicast, no linklocal.  RFC2529 specifies how to make
71088ff5695SSUZUKI Shinsuke 		 * linklocals for 6to4 interface, but there's no use and
71188ff5695SSUZUKI Shinsuke 		 * it is rather harmful to have one.
71282cd038dSYoshinobu Inoue 		 */
713af805644SHiroki Sato 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
714af805644SHiroki Sato 		break;
71582cd038dSYoshinobu Inoue 	default:
71682cd038dSYoshinobu Inoue 		break;
71782cd038dSYoshinobu Inoue 	}
71882cd038dSYoshinobu Inoue 
719686cdd19SJun-ichiro itojun Hagino 	/*
720686cdd19SJun-ichiro itojun Hagino 	 * usually, we require multicast capability to the interface
721686cdd19SJun-ichiro itojun Hagino 	 */
722686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
7232ce62dceSSUZUKI Shinsuke 		nd6log((LOG_INFO, "in6_ifattach: "
72433841545SHajimu UMEMOTO 		    "%s is not multicast capable, IPv6 not enabled\n",
7252ce62dceSSUZUKI Shinsuke 		    if_name(ifp)));
72682cd038dSYoshinobu Inoue 		return;
72782cd038dSYoshinobu Inoue 	}
728686cdd19SJun-ichiro itojun Hagino 
729686cdd19SJun-ichiro itojun Hagino 	/*
73033841545SHajimu UMEMOTO 	 * assign loopback address for loopback interface.
73133841545SHajimu UMEMOTO 	 * XXX multiple loopback interface case.
732686cdd19SJun-ichiro itojun Hagino 	 */
73333841545SHajimu UMEMOTO 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
7348c0fec80SRobert Watson 		struct ifaddr *ifa;
7358c0fec80SRobert Watson 
736686cdd19SJun-ichiro itojun Hagino 		in6 = in6addr_loopback;
7378c0fec80SRobert Watson 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &in6);
7388c0fec80SRobert Watson 		if (ifa == NULL) {
739686cdd19SJun-ichiro itojun Hagino 			if (in6_ifattach_loopback(ifp) != 0)
740686cdd19SJun-ichiro itojun Hagino 				return;
7418c0fec80SRobert Watson 		} else
7428c0fec80SRobert Watson 			ifa_free(ifa);
743686cdd19SJun-ichiro itojun Hagino 	}
744686cdd19SJun-ichiro itojun Hagino 
74533841545SHajimu UMEMOTO 	/*
74633841545SHajimu UMEMOTO 	 * assign a link-local address, if there's none.
74733841545SHajimu UMEMOTO 	 */
748af805644SHiroki Sato 	if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
749a283298cSHiroki Sato 	    ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
750a283298cSHiroki Sato 		int error;
751a283298cSHiroki Sato 
75233841545SHajimu UMEMOTO 		ia = in6ifa_ifpforlinklocal(ifp, 0);
75333841545SHajimu UMEMOTO 		if (ia == NULL) {
754a283298cSHiroki Sato 			error = in6_ifattach_linklocal(ifp, altifp);
75527f13d5dSHiroki Sato #if 0
756a283298cSHiroki Sato 			if (error)
757a283298cSHiroki Sato 				log(LOG_NOTICE, "in6_ifattach_linklocal: "
758a283298cSHiroki Sato 				    "failed to add a link-local addr to %s\n",
759a283298cSHiroki Sato 				    if_name(ifp));
76027f13d5dSHiroki Sato #endif
7618c0fec80SRobert Watson 		} else
7628c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
76333841545SHajimu UMEMOTO 	}
76433841545SHajimu UMEMOTO 
76582cd038dSYoshinobu Inoue 	/* update dynamically. */
766603724d3SBjoern A. Zeeb 	if (V_in6_maxmtu < ifp->if_mtu)
767603724d3SBjoern A. Zeeb 		V_in6_maxmtu = ifp->if_mtu;
76882cd038dSYoshinobu Inoue }
76982cd038dSYoshinobu Inoue 
770686cdd19SJun-ichiro itojun Hagino /*
771686cdd19SJun-ichiro itojun Hagino  * NOTE: in6_ifdetach() does not support loopback if at this moment.
77233841545SHajimu UMEMOTO  * We don't need this function in bsdi, because interfaces are never removed
77333841545SHajimu UMEMOTO  * from the ifnet list in bsdi.
774686cdd19SJun-ichiro itojun Hagino  */
77582cd038dSYoshinobu Inoue void
7761272577eSXin LI in6_ifdetach(struct ifnet *ifp)
77782cd038dSYoshinobu Inoue {
77880af0152SRobert Watson 	struct in6_ifaddr *ia;
779686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ifa, *next;
780c2c2a7c1SBjoern A. Zeeb 	struct radix_node_head *rnh;
78182cd038dSYoshinobu Inoue 	struct rtentry *rt;
782686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 sin6;
78309a52a55SJINMEI Tatuya 	struct in6_multi_mship *imm;
78482cd038dSYoshinobu Inoue 
7856378e1f3SHiroki Sato 	if (ifp->if_afdata[AF_INET6] == NULL)
7866378e1f3SHiroki Sato 		return;
7876378e1f3SHiroki Sato 
788686cdd19SJun-ichiro itojun Hagino 	/* remove neighbor management table */
789686cdd19SJun-ichiro itojun Hagino 	nd6_purge(ifp);
790686cdd19SJun-ichiro itojun Hagino 
791686cdd19SJun-ichiro itojun Hagino 	/* nuke any of IPv6 addresses we have */
792c4dd3fe1SRobert Watson 	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
793686cdd19SJun-ichiro itojun Hagino 		if (ifa->ifa_addr->sa_family != AF_INET6)
794686cdd19SJun-ichiro itojun Hagino 			continue;
79533841545SHajimu UMEMOTO 		in6_purgeaddr(ifa);
796686cdd19SJun-ichiro itojun Hagino 	}
797686cdd19SJun-ichiro itojun Hagino 
798686cdd19SJun-ichiro itojun Hagino 	/* undo everything done by in6_ifattach(), just in case */
799c4dd3fe1SRobert Watson 	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
80082cd038dSYoshinobu Inoue 		if (ifa->ifa_addr->sa_family != AF_INET6
80182cd038dSYoshinobu Inoue 		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
80282cd038dSYoshinobu Inoue 			continue;
80382cd038dSYoshinobu Inoue 		}
80482cd038dSYoshinobu Inoue 
80582cd038dSYoshinobu Inoue 		ia = (struct in6_ifaddr *)ifa;
80682cd038dSYoshinobu Inoue 
80709a52a55SJINMEI Tatuya 		/*
80809a52a55SJINMEI Tatuya 		 * leave from multicast groups we have joined for the interface
80909a52a55SJINMEI Tatuya 		 */
8103b0b2840SJohn Baldwin 		while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
81109a52a55SJINMEI Tatuya 			LIST_REMOVE(imm, i6mm_chain);
81209a52a55SJINMEI Tatuya 			in6_leavegroup(imm);
81309a52a55SJINMEI Tatuya 		}
81409a52a55SJINMEI Tatuya 
81581d5d46bSBjoern A. Zeeb 		/* Remove link-local from the routing table. */
81681d5d46bSBjoern A. Zeeb 		if (ia->ia_flags & IFA_ROUTE)
81781d5d46bSBjoern A. Zeeb 			(void)rtinit(&ia->ia_ifa, RTM_DELETE, ia->ia_flags);
81882cd038dSYoshinobu Inoue 
81982cd038dSYoshinobu Inoue 		/* remove from the linked list */
820137f91e8SJohn Baldwin 		IF_ADDR_WLOCK(ifp);
82180af0152SRobert Watson 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
822137f91e8SJohn Baldwin 		IF_ADDR_WUNLOCK(ifp);
82380af0152SRobert Watson 		ifa_free(ifa);				/* if_addrhead */
82482cd038dSYoshinobu Inoue 
825d1da0a06SRobert Watson 		IN6_IFADDR_WLOCK();
82680af0152SRobert Watson 		TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
827d1da0a06SRobert Watson 		IN6_IFADDR_WUNLOCK();
82880af0152SRobert Watson 		ifa_free(ifa);
829686cdd19SJun-ichiro itojun Hagino 	}
830686cdd19SJun-ichiro itojun Hagino 
831603724d3SBjoern A. Zeeb 	in6_pcbpurgeif0(&V_udbinfo, ifp);
832603724d3SBjoern A. Zeeb 	in6_pcbpurgeif0(&V_ripcbinfo, ifp);
83371498f30SBruce M Simpson 	/* leave from all multicast groups joined */
83471498f30SBruce M Simpson 	in6_purgemaddrs(ifp);
835686cdd19SJun-ichiro itojun Hagino 
83633841545SHajimu UMEMOTO 	/*
83733841545SHajimu UMEMOTO 	 * remove neighbor management table.  we call it twice just to make
83833841545SHajimu UMEMOTO 	 * sure we nuke everything.  maybe we need just one call.
83933841545SHajimu UMEMOTO 	 * XXX: since the first call did not release addresses, some prefixes
84033841545SHajimu UMEMOTO 	 * might remain.  We should call nd6_purge() again to release the
84133841545SHajimu UMEMOTO 	 * prefixes after removing all addresses above.
84233841545SHajimu UMEMOTO 	 * (Or can we just delay calling nd6_purge until at this point?)
84333841545SHajimu UMEMOTO 	 */
844686cdd19SJun-ichiro itojun Hagino 	nd6_purge(ifp);
845686cdd19SJun-ichiro itojun Hagino 
84681d5d46bSBjoern A. Zeeb 	/*
84781d5d46bSBjoern A. Zeeb 	 * Remove route to link-local allnodes multicast (ff02::1).
84881d5d46bSBjoern A. Zeeb 	 * These only get automatically installed for the default FIB.
84981d5d46bSBjoern A. Zeeb 	 */
850686cdd19SJun-ichiro itojun Hagino 	bzero(&sin6, sizeof(sin6));
851686cdd19SJun-ichiro itojun Hagino 	sin6.sin6_len = sizeof(struct sockaddr_in6);
852686cdd19SJun-ichiro itojun Hagino 	sin6.sin6_family = AF_INET6;
853686cdd19SJun-ichiro itojun Hagino 	sin6.sin6_addr = in6addr_linklocal_allnodes;
854a1f7e5f8SHajimu UMEMOTO 	if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
855a1f7e5f8SHajimu UMEMOTO 		/* XXX: should not fail */
856a1f7e5f8SHajimu UMEMOTO 		return;
8579c63e9dbSSam Leffler 	/* XXX grab lock first to avoid LOR */
85881d5d46bSBjoern A. Zeeb 	rnh = rt_tables_get_rnh(RT_DEFAULT_FIB, AF_INET6);
859c2c2a7c1SBjoern A. Zeeb 	if (rnh != NULL) {
860c2c2a7c1SBjoern A. Zeeb 		RADIX_NODE_HEAD_LOCK(rnh);
86181d5d46bSBjoern A. Zeeb 		rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, RTF_RNH_LOCKED,
86281d5d46bSBjoern A. Zeeb 		    RT_DEFAULT_FIB);
863d1dd20beSSam Leffler 		if (rt) {
8649c63e9dbSSam Leffler 			if (rt->rt_ifp == ifp)
8659c63e9dbSSam Leffler 				rtexpunge(rt);
8669c63e9dbSSam Leffler 			RTFREE_LOCKED(rt);
86782cd038dSYoshinobu Inoue 		}
868c2c2a7c1SBjoern A. Zeeb 		RADIX_NODE_HEAD_UNLOCK(rnh);
86982cd038dSYoshinobu Inoue 	}
8703f02eaf3SDon Lewis }
87133841545SHajimu UMEMOTO 
872743eee66SSUZUKI Shinsuke int
8731272577eSXin LI in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
8741272577eSXin LI     const u_int8_t *baseid, int generate)
87533841545SHajimu UMEMOTO {
87633841545SHajimu UMEMOTO 	u_int8_t nullbuf[8];
87731b1bfe1SHajimu UMEMOTO 	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
87833841545SHajimu UMEMOTO 
87933841545SHajimu UMEMOTO 	bzero(nullbuf, sizeof(nullbuf));
88033841545SHajimu UMEMOTO 	if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
88133841545SHajimu UMEMOTO 		/* we've never created a random ID.  Create a new one. */
88233841545SHajimu UMEMOTO 		generate = 1;
88333841545SHajimu UMEMOTO 	}
88433841545SHajimu UMEMOTO 
88533841545SHajimu UMEMOTO 	if (generate) {
88633841545SHajimu UMEMOTO 		bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
88733841545SHajimu UMEMOTO 
88833841545SHajimu UMEMOTO 		/* generate_tmp_ifid will update seedn and buf */
88933841545SHajimu UMEMOTO 		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
89033841545SHajimu UMEMOTO 		    ndi->randomid);
89133841545SHajimu UMEMOTO 	}
89233841545SHajimu UMEMOTO 	bcopy(ndi->randomid, retbuf, 8);
893743eee66SSUZUKI Shinsuke 
894743eee66SSUZUKI Shinsuke 	return (0);
89533841545SHajimu UMEMOTO }
89633841545SHajimu UMEMOTO 
89733841545SHajimu UMEMOTO void
898f6dfe47aSMarko Zec in6_tmpaddrtimer(void *arg)
89933841545SHajimu UMEMOTO {
900f6dfe47aSMarko Zec 	CURVNET_SET((struct vnet *) arg);
90133841545SHajimu UMEMOTO 	struct nd_ifinfo *ndi;
90233841545SHajimu UMEMOTO 	u_int8_t nullbuf[8];
90331b1bfe1SHajimu UMEMOTO 	struct ifnet *ifp;
90433841545SHajimu UMEMOTO 
905603724d3SBjoern A. Zeeb 	callout_reset(&V_in6_tmpaddrtimer_ch,
906603724d3SBjoern A. Zeeb 	    (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
907f6dfe47aSMarko Zec 	    V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
90833841545SHajimu UMEMOTO 
90933841545SHajimu UMEMOTO 	bzero(nullbuf, sizeof(nullbuf));
9103b0b2840SJohn Baldwin 	TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
91141541ebfSHiroki Sato 		if (ifp->if_afdata[AF_INET6] == NULL)
91241541ebfSHiroki Sato 			continue;
91331b1bfe1SHajimu UMEMOTO 		ndi = ND_IFINFO(ifp);
91433841545SHajimu UMEMOTO 		if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
91533841545SHajimu UMEMOTO 			/*
91633841545SHajimu UMEMOTO 			 * We've been generating a random ID on this interface.
91733841545SHajimu UMEMOTO 			 * Create a new one.
91833841545SHajimu UMEMOTO 			 */
91933841545SHajimu UMEMOTO 			(void)generate_tmp_ifid(ndi->randomseed0,
9207efe5d92SHajimu UMEMOTO 			    ndi->randomseed1, ndi->randomid);
92133841545SHajimu UMEMOTO 		}
92233841545SHajimu UMEMOTO 	}
92333841545SHajimu UMEMOTO 
924f6dfe47aSMarko Zec 	CURVNET_RESTORE();
92533841545SHajimu UMEMOTO }
92671498f30SBruce M Simpson 
92771498f30SBruce M Simpson static void
9281272577eSXin LI in6_purgemaddrs(struct ifnet *ifp)
92971498f30SBruce M Simpson {
93033cde130SBruce M Simpson 	LIST_HEAD(,in6_multi)	 purgeinms;
93133cde130SBruce M Simpson 	struct in6_multi	*inm, *tinm;
93233cde130SBruce M Simpson 	struct ifmultiaddr	*ifma;
93371498f30SBruce M Simpson 
93433cde130SBruce M Simpson 	LIST_INIT(&purgeinms);
93533cde130SBruce M Simpson 	IN6_MULTI_LOCK();
93633cde130SBruce M Simpson 
93733cde130SBruce M Simpson 	/*
93833cde130SBruce M Simpson 	 * Extract list of in6_multi associated with the detaching ifp
93933cde130SBruce M Simpson 	 * which the PF_INET6 layer is about to release.
94033cde130SBruce M Simpson 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
94133cde130SBruce M Simpson 	 * by code further down.
94233cde130SBruce M Simpson 	 */
943137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
94433cde130SBruce M Simpson 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
94533cde130SBruce M Simpson 		if (ifma->ifma_addr->sa_family != AF_INET6 ||
94633cde130SBruce M Simpson 		    ifma->ifma_protospec == NULL)
94733cde130SBruce M Simpson 			continue;
94833cde130SBruce M Simpson 		inm = (struct in6_multi *)ifma->ifma_protospec;
94933cde130SBruce M Simpson 		LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry);
95071498f30SBruce M Simpson 	}
951137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
95233cde130SBruce M Simpson 
95333cde130SBruce M Simpson 	LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) {
95433cde130SBruce M Simpson 		LIST_REMOVE(inm, in6m_entry);
95533cde130SBruce M Simpson 		in6m_release_locked(inm);
95633cde130SBruce M Simpson 	}
95733cde130SBruce M Simpson 	mld_ifdetach(ifp);
95833cde130SBruce M Simpson 
95933cde130SBruce M Simpson 	IN6_MULTI_UNLOCK();
96071498f30SBruce M Simpson }
961