xref: /freebsd/sys/netinet6/in6_ifattach.c (revision 4f7d1876d532e6607cebd4ea3775094ee8c05dba)
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>
4082cd038dSYoshinobu Inoue #include <sys/kernel.h>
4133841545SHajimu UMEMOTO #include <sys/syslog.h>
4282cd038dSYoshinobu Inoue #include <sys/md5.h>
4382cd038dSYoshinobu Inoue 
4482cd038dSYoshinobu Inoue #include <net/if.h>
4582cd038dSYoshinobu Inoue #include <net/if_dl.h>
4682cd038dSYoshinobu Inoue #include <net/if_types.h>
4782cd038dSYoshinobu Inoue #include <net/route.h>
4882cd038dSYoshinobu Inoue 
4982cd038dSYoshinobu Inoue #include <netinet/in.h>
5082cd038dSYoshinobu Inoue #include <netinet/in_var.h>
5182cd038dSYoshinobu Inoue #include <netinet/if_ether.h>
52e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h>
5382cd038dSYoshinobu Inoue 
54686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
5582cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
5633841545SHajimu UMEMOTO #include <netinet6/in6_var.h>
57e43cc4aeSHajimu UMEMOTO #include <netinet6/in6_pcb.h>
5882cd038dSYoshinobu Inoue #include <netinet6/in6_ifattach.h>
5982cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
6082cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
61686cdd19SJun-ichiro itojun Hagino #include <netinet6/scope6_var.h>
6282cd038dSYoshinobu Inoue 
6382cd038dSYoshinobu Inoue unsigned long in6_maxmtu = 0;
6482cd038dSYoshinobu Inoue 
6533841545SHajimu UMEMOTO #ifdef IP6_AUTO_LINKLOCAL
6633841545SHajimu UMEMOTO int ip6_auto_linklocal = IP6_AUTO_LINKLOCAL;
6733841545SHajimu UMEMOTO #else
68f5c04409SHajimu UMEMOTO int ip6_auto_linklocal = 1;	/* enable by default */
6933841545SHajimu UMEMOTO #endif
7033841545SHajimu UMEMOTO 
7133841545SHajimu UMEMOTO struct callout in6_tmpaddrtimer_ch;
7233841545SHajimu UMEMOTO 
73e43cc4aeSHajimu UMEMOTO extern struct inpcbinfo udbinfo;
74e43cc4aeSHajimu UMEMOTO extern struct inpcbinfo ripcbinfo;
75e43cc4aeSHajimu UMEMOTO 
769233d8f3SDavid E. O'Brien static int get_rand_ifid(struct ifnet *, struct in6_addr *);
779233d8f3SDavid E. O'Brien static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
789233d8f3SDavid E. O'Brien static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
799233d8f3SDavid E. O'Brien static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
809233d8f3SDavid E. O'Brien static int in6_ifattach_loopback(struct ifnet *);
819233d8f3SDavid E. O'Brien static void in6_purgemaddrs(struct ifnet *);
8282cd038dSYoshinobu Inoue 
83686cdd19SJun-ichiro itojun Hagino #define EUI64_GBIT	0x01
84686cdd19SJun-ichiro itojun Hagino #define EUI64_UBIT	0x02
85686cdd19SJun-ichiro itojun Hagino #define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
86686cdd19SJun-ichiro itojun Hagino #define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
87686cdd19SJun-ichiro itojun Hagino #define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
88686cdd19SJun-ichiro itojun Hagino #define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
89686cdd19SJun-ichiro itojun Hagino #define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
9082cd038dSYoshinobu Inoue 
91686cdd19SJun-ichiro itojun Hagino #define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
92686cdd19SJun-ichiro itojun Hagino #define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
9382cd038dSYoshinobu Inoue 
9482cd038dSYoshinobu Inoue /*
9582cd038dSYoshinobu Inoue  * Generate a last-resort interface identifier, when the machine has no
9682cd038dSYoshinobu Inoue  * IEEE802/EUI64 address sources.
97686cdd19SJun-ichiro itojun Hagino  * The goal here is to get an interface identifier that is
98686cdd19SJun-ichiro itojun Hagino  * (1) random enough and (2) does not change across reboot.
99686cdd19SJun-ichiro itojun Hagino  * We currently use MD5(hostname) for it.
1001272577eSXin LI  *
1011272577eSXin LI  * in6 - upper 64bits are preserved
10282cd038dSYoshinobu Inoue  */
10382cd038dSYoshinobu Inoue static int
1041272577eSXin LI get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
10582cd038dSYoshinobu Inoue {
10682cd038dSYoshinobu Inoue 	MD5_CTX ctxt;
10782cd038dSYoshinobu Inoue 	u_int8_t digest[16];
1084f7d1876SRobert Watson 	int hostnamelen;
10982cd038dSYoshinobu Inoue 
1104f7d1876SRobert Watson 	mtx_lock(&hostname_mtx);
1114f7d1876SRobert Watson 	hostnamelen = strlen(hostname);
112686cdd19SJun-ichiro itojun Hagino #if 0
113686cdd19SJun-ichiro itojun Hagino 	/* we need at least several letters as seed for ifid */
114686cdd19SJun-ichiro itojun Hagino 	if (hostnamelen < 3)
115686cdd19SJun-ichiro itojun Hagino 		return -1;
116686cdd19SJun-ichiro itojun Hagino #endif
117686cdd19SJun-ichiro itojun Hagino 
11882cd038dSYoshinobu Inoue 	/* generate 8 bytes of pseudo-random value. */
11982cd038dSYoshinobu Inoue 	bzero(&ctxt, sizeof(ctxt));
12082cd038dSYoshinobu Inoue 	MD5Init(&ctxt);
12182cd038dSYoshinobu Inoue 	MD5Update(&ctxt, hostname, hostnamelen);
1224f7d1876SRobert Watson 	mtx_unlock(&hostname_mtx);
12382cd038dSYoshinobu Inoue 	MD5Final(digest, &ctxt);
12482cd038dSYoshinobu Inoue 
125686cdd19SJun-ichiro itojun Hagino 	/* assumes sizeof(digest) > sizeof(ifid) */
126686cdd19SJun-ichiro itojun Hagino 	bcopy(digest, &in6->s6_addr[8], 8);
12782cd038dSYoshinobu Inoue 
12882cd038dSYoshinobu Inoue 	/* make sure to set "u" bit to local, and "g" bit to individual. */
129686cdd19SJun-ichiro itojun Hagino 	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
130686cdd19SJun-ichiro itojun Hagino 	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
131686cdd19SJun-ichiro itojun Hagino 
132686cdd19SJun-ichiro itojun Hagino 	/* convert EUI64 into IPv6 interface identifier */
133686cdd19SJun-ichiro itojun Hagino 	EUI64_TO_IFID(in6);
13482cd038dSYoshinobu Inoue 
13582cd038dSYoshinobu Inoue 	return 0;
13682cd038dSYoshinobu Inoue }
13782cd038dSYoshinobu Inoue 
13833841545SHajimu UMEMOTO static int
1391272577eSXin LI generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
14033841545SHajimu UMEMOTO {
14133841545SHajimu UMEMOTO 	MD5_CTX ctxt;
14233841545SHajimu UMEMOTO 	u_int8_t seed[16], digest[16], nullbuf[8];
14333841545SHajimu UMEMOTO 	u_int32_t val32;
14433841545SHajimu UMEMOTO 
1451272577eSXin LI 	/* If there's no history, start with a random seed. */
14633841545SHajimu UMEMOTO 	bzero(nullbuf, sizeof(nullbuf));
14733841545SHajimu UMEMOTO 	if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
14833841545SHajimu UMEMOTO 		int i;
14933841545SHajimu UMEMOTO 
15033841545SHajimu UMEMOTO 		for (i = 0; i < 2; i++) {
151a02e1e2bSHajimu UMEMOTO 			val32 = arc4random();
15233841545SHajimu UMEMOTO 			bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
15333841545SHajimu UMEMOTO 		}
1547efe5d92SHajimu UMEMOTO 	} else
15533841545SHajimu UMEMOTO 		bcopy(seed0, seed, 8);
15633841545SHajimu UMEMOTO 
15733841545SHajimu UMEMOTO 	/* copy the right-most 64-bits of the given address */
15833841545SHajimu UMEMOTO 	/* XXX assumption on the size of IFID */
15933841545SHajimu UMEMOTO 	bcopy(seed1, &seed[8], 8);
16033841545SHajimu UMEMOTO 
16133841545SHajimu UMEMOTO 	if (0) {		/* for debugging purposes only */
16233841545SHajimu UMEMOTO 		int i;
16333841545SHajimu UMEMOTO 
16433841545SHajimu UMEMOTO 		printf("generate_tmp_ifid: new randomized ID from: ");
16533841545SHajimu UMEMOTO 		for (i = 0; i < 16; i++)
16633841545SHajimu UMEMOTO 			printf("%02x", seed[i]);
16733841545SHajimu UMEMOTO 		printf(" ");
16833841545SHajimu UMEMOTO 	}
16933841545SHajimu UMEMOTO 
17033841545SHajimu UMEMOTO 	/* generate 16 bytes of pseudo-random value. */
17133841545SHajimu UMEMOTO 	bzero(&ctxt, sizeof(ctxt));
17233841545SHajimu UMEMOTO 	MD5Init(&ctxt);
17333841545SHajimu UMEMOTO 	MD5Update(&ctxt, seed, sizeof(seed));
17433841545SHajimu UMEMOTO 	MD5Final(digest, &ctxt);
17533841545SHajimu UMEMOTO 
17633841545SHajimu UMEMOTO 	/*
17733841545SHajimu UMEMOTO 	 * RFC 3041 3.2.1. (3)
17833841545SHajimu UMEMOTO 	 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
17933841545SHajimu UMEMOTO 	 * left-most bit is numbered 0) to zero.
18033841545SHajimu UMEMOTO 	 */
18133841545SHajimu UMEMOTO 	bcopy(digest, ret, 8);
18233841545SHajimu UMEMOTO 	ret[0] &= ~EUI64_UBIT;
18333841545SHajimu UMEMOTO 
18433841545SHajimu UMEMOTO 	/*
18533841545SHajimu UMEMOTO 	 * XXX: we'd like to ensure that the generated value is not zero
18633841545SHajimu UMEMOTO 	 * for simplicity.  If the caclculated digest happens to be zero,
18733841545SHajimu UMEMOTO 	 * use a random non-zero value as the last resort.
18833841545SHajimu UMEMOTO 	 */
18933841545SHajimu UMEMOTO 	if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
1902ce62dceSSUZUKI Shinsuke 		nd6log((LOG_INFO,
1912ce62dceSSUZUKI Shinsuke 		    "generate_tmp_ifid: computed MD5 value is zero.\n"));
19233841545SHajimu UMEMOTO 
193a02e1e2bSHajimu UMEMOTO 		val32 = arc4random();
19433841545SHajimu UMEMOTO 		val32 = 1 + (val32 % (0xffffffff - 1));
19533841545SHajimu UMEMOTO 	}
19633841545SHajimu UMEMOTO 
19733841545SHajimu UMEMOTO 	/*
19833841545SHajimu UMEMOTO 	 * RFC 3041 3.2.1. (4)
19933841545SHajimu UMEMOTO 	 * Take the rightmost 64-bits of the MD5 digest and save them in
20033841545SHajimu UMEMOTO 	 * stable storage as the history value to be used in the next
20133841545SHajimu UMEMOTO 	 * iteration of the algorithm.
20233841545SHajimu UMEMOTO 	 */
20333841545SHajimu UMEMOTO 	bcopy(&digest[8], seed0, 8);
20433841545SHajimu UMEMOTO 
20533841545SHajimu UMEMOTO 	if (0) {		/* for debugging purposes only */
20633841545SHajimu UMEMOTO 		int i;
20733841545SHajimu UMEMOTO 
20833841545SHajimu UMEMOTO 		printf("to: ");
20933841545SHajimu UMEMOTO 		for (i = 0; i < 16; i++)
21033841545SHajimu UMEMOTO 			printf("%02x", digest[i]);
21133841545SHajimu UMEMOTO 		printf("\n");
21233841545SHajimu UMEMOTO 	}
21333841545SHajimu UMEMOTO 
21433841545SHajimu UMEMOTO 	return 0;
21533841545SHajimu UMEMOTO }
21633841545SHajimu UMEMOTO 
21782cd038dSYoshinobu Inoue /*
218686cdd19SJun-ichiro itojun Hagino  * Get interface identifier for the specified interface.
219686cdd19SJun-ichiro itojun Hagino  * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
2201272577eSXin LI  *
2211272577eSXin LI  * in6 - upper 64bits are preserved
22282cd038dSYoshinobu Inoue  */
223d28bde66SSUZUKI Shinsuke int
2241272577eSXin LI in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
22582cd038dSYoshinobu Inoue {
226686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ifa;
227686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_dl *sdl;
228686cdd19SJun-ichiro itojun Hagino 	u_int8_t *addr;
229686cdd19SJun-ichiro itojun Hagino 	size_t addrlen;
230686cdd19SJun-ichiro itojun Hagino 	static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
231686cdd19SJun-ichiro itojun Hagino 	static u_int8_t allone[8] =
232686cdd19SJun-ichiro itojun Hagino 		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
233686cdd19SJun-ichiro itojun Hagino 
234686cdd19SJun-ichiro itojun Hagino 	for (ifa = ifp->if_addrlist.tqh_first;
235686cdd19SJun-ichiro itojun Hagino 	     ifa;
2367efe5d92SHajimu UMEMOTO 	     ifa = ifa->ifa_list.tqe_next) {
23782cd038dSYoshinobu Inoue 		if (ifa->ifa_addr->sa_family != AF_LINK)
23882cd038dSYoshinobu Inoue 			continue;
23982cd038dSYoshinobu Inoue 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
24082cd038dSYoshinobu Inoue 		if (sdl == NULL)
24182cd038dSYoshinobu Inoue 			continue;
24282cd038dSYoshinobu Inoue 		if (sdl->sdl_alen == 0)
24382cd038dSYoshinobu Inoue 			continue;
244686cdd19SJun-ichiro itojun Hagino 
245686cdd19SJun-ichiro itojun Hagino 		goto found;
246686cdd19SJun-ichiro itojun Hagino 	}
247686cdd19SJun-ichiro itojun Hagino 
248686cdd19SJun-ichiro itojun Hagino 	return -1;
249686cdd19SJun-ichiro itojun Hagino 
250686cdd19SJun-ichiro itojun Hagino found:
251686cdd19SJun-ichiro itojun Hagino 	addr = LLADDR(sdl);
252686cdd19SJun-ichiro itojun Hagino 	addrlen = sdl->sdl_alen;
253686cdd19SJun-ichiro itojun Hagino 
254686cdd19SJun-ichiro itojun Hagino 	/* get EUI64 */
25582cd038dSYoshinobu Inoue 	switch (ifp->if_type) {
25682cd038dSYoshinobu Inoue 	case IFT_ETHER:
25782cd038dSYoshinobu Inoue 	case IFT_FDDI:
2582049fdeeSMatthew N. Dodd 	case IFT_ISO88025:
25982cd038dSYoshinobu Inoue 	case IFT_ATM:
26033841545SHajimu UMEMOTO 	case IFT_IEEE1394:
26133841545SHajimu UMEMOTO #ifdef IFT_IEEE80211
26233841545SHajimu UMEMOTO 	case IFT_IEEE80211:
26333841545SHajimu UMEMOTO #endif
26482cd038dSYoshinobu Inoue 		/* IEEE802/EUI64 cases - what others? */
26533841545SHajimu UMEMOTO 		/* IEEE1394 uses 16byte length address starting with EUI64 */
26633841545SHajimu UMEMOTO 		if (addrlen > 8)
26733841545SHajimu UMEMOTO 			addrlen = 8;
268686cdd19SJun-ichiro itojun Hagino 
269686cdd19SJun-ichiro itojun Hagino 		/* look at IEEE802/EUI64 only */
270686cdd19SJun-ichiro itojun Hagino 		if (addrlen != 8 && addrlen != 6)
271686cdd19SJun-ichiro itojun Hagino 			return -1;
272686cdd19SJun-ichiro itojun Hagino 
27382cd038dSYoshinobu Inoue 		/*
274686cdd19SJun-ichiro itojun Hagino 		 * check for invalid MAC address - on bsdi, we see it a lot
275686cdd19SJun-ichiro itojun Hagino 		 * since wildboar configures all-zero MAC on pccard before
276686cdd19SJun-ichiro itojun Hagino 		 * card insertion.
27782cd038dSYoshinobu Inoue 		 */
278686cdd19SJun-ichiro itojun Hagino 		if (bcmp(addr, allzero, addrlen) == 0)
279686cdd19SJun-ichiro itojun Hagino 			return -1;
280686cdd19SJun-ichiro itojun Hagino 		if (bcmp(addr, allone, addrlen) == 0)
281686cdd19SJun-ichiro itojun Hagino 			return -1;
282686cdd19SJun-ichiro itojun Hagino 
283686cdd19SJun-ichiro itojun Hagino 		/* make EUI64 address */
284686cdd19SJun-ichiro itojun Hagino 		if (addrlen == 8)
285686cdd19SJun-ichiro itojun Hagino 			bcopy(addr, &in6->s6_addr[8], 8);
286686cdd19SJun-ichiro itojun Hagino 		else if (addrlen == 6) {
287686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[8] = addr[0];
288686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[9] = addr[1];
289686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[10] = addr[2];
290686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[11] = 0xff;
291686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[12] = 0xfe;
292686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[13] = addr[3];
293686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[14] = addr[4];
294686cdd19SJun-ichiro itojun Hagino 			in6->s6_addr[15] = addr[5];
295686cdd19SJun-ichiro itojun Hagino 		}
29682cd038dSYoshinobu Inoue 		break;
297686cdd19SJun-ichiro itojun Hagino 
29882cd038dSYoshinobu Inoue 	case IFT_ARCNET:
299686cdd19SJun-ichiro itojun Hagino 		if (addrlen != 1)
300686cdd19SJun-ichiro itojun Hagino 			return -1;
301686cdd19SJun-ichiro itojun Hagino 		if (!addr[0])
302686cdd19SJun-ichiro itojun Hagino 			return -1;
303686cdd19SJun-ichiro itojun Hagino 
304686cdd19SJun-ichiro itojun Hagino 		bzero(&in6->s6_addr[8], 8);
305686cdd19SJun-ichiro itojun Hagino 		in6->s6_addr[15] = addr[0];
306686cdd19SJun-ichiro itojun Hagino 
30782cd038dSYoshinobu Inoue 		/*
308686cdd19SJun-ichiro itojun Hagino 		 * due to insufficient bitwidth, we mark it local.
30982cd038dSYoshinobu Inoue 		 */
310686cdd19SJun-ichiro itojun Hagino 		in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
311686cdd19SJun-ichiro itojun Hagino 		in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
31282cd038dSYoshinobu Inoue 		break;
313686cdd19SJun-ichiro itojun Hagino 
314686cdd19SJun-ichiro itojun Hagino 	case IFT_GIF:
315686cdd19SJun-ichiro itojun Hagino #ifdef IFT_STF
316686cdd19SJun-ichiro itojun Hagino 	case IFT_STF:
31782cd038dSYoshinobu Inoue #endif
318686cdd19SJun-ichiro itojun Hagino 		/*
31933841545SHajimu UMEMOTO 		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
320686cdd19SJun-ichiro itojun Hagino 		 * however, IPv4 address is not very suitable as unique
321686cdd19SJun-ichiro itojun Hagino 		 * identifier source (can be renumbered).
322686cdd19SJun-ichiro itojun Hagino 		 * we don't do this.
323686cdd19SJun-ichiro itojun Hagino 		 */
324686cdd19SJun-ichiro itojun Hagino 		return -1;
32582cd038dSYoshinobu Inoue 
326686cdd19SJun-ichiro itojun Hagino 	default:
327686cdd19SJun-ichiro itojun Hagino 		return -1;
328686cdd19SJun-ichiro itojun Hagino 	}
32982cd038dSYoshinobu Inoue 
330686cdd19SJun-ichiro itojun Hagino 	/* sanity check: g bit must not indicate "group" */
331686cdd19SJun-ichiro itojun Hagino 	if (EUI64_GROUP(in6))
332686cdd19SJun-ichiro itojun Hagino 		return -1;
33382cd038dSYoshinobu Inoue 
334686cdd19SJun-ichiro itojun Hagino 	/* convert EUI64 into IPv6 interface identifier */
335686cdd19SJun-ichiro itojun Hagino 	EUI64_TO_IFID(in6);
336686cdd19SJun-ichiro itojun Hagino 
337686cdd19SJun-ichiro itojun Hagino 	/*
338686cdd19SJun-ichiro itojun Hagino 	 * sanity check: ifid must not be all zero, avoid conflict with
339686cdd19SJun-ichiro itojun Hagino 	 * subnet router anycast
340686cdd19SJun-ichiro itojun Hagino 	 */
341686cdd19SJun-ichiro itojun Hagino 	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
342686cdd19SJun-ichiro itojun Hagino 	    bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
343686cdd19SJun-ichiro itojun Hagino 		return -1;
344686cdd19SJun-ichiro itojun Hagino 	}
34582cd038dSYoshinobu Inoue 
34682cd038dSYoshinobu Inoue 	return 0;
347686cdd19SJun-ichiro itojun Hagino }
348686cdd19SJun-ichiro itojun Hagino 
349686cdd19SJun-ichiro itojun Hagino /*
350686cdd19SJun-ichiro itojun Hagino  * Get interface identifier for the specified interface.  If it is not
351686cdd19SJun-ichiro itojun Hagino  * available on ifp0, borrow interface identifier from other information
352686cdd19SJun-ichiro itojun Hagino  * sources.
3531272577eSXin LI  *
3541272577eSXin LI  * altifp - secondary EUI64 source
355686cdd19SJun-ichiro itojun Hagino  */
356686cdd19SJun-ichiro itojun Hagino static int
3571272577eSXin LI get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
3581272577eSXin LI     struct in6_addr *in6)
359686cdd19SJun-ichiro itojun Hagino {
360686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
361686cdd19SJun-ichiro itojun Hagino 
362686cdd19SJun-ichiro itojun Hagino 	/* first, try to get it from the interface itself */
363d28bde66SSUZUKI Shinsuke 	if (in6_get_hw_ifid(ifp0, in6) == 0) {
36433841545SHajimu UMEMOTO 		nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
36533841545SHajimu UMEMOTO 		    if_name(ifp0)));
366686cdd19SJun-ichiro itojun Hagino 		goto success;
367686cdd19SJun-ichiro itojun Hagino 	}
368686cdd19SJun-ichiro itojun Hagino 
369686cdd19SJun-ichiro itojun Hagino 	/* try secondary EUI64 source. this basically is for ATM PVC */
370d28bde66SSUZUKI Shinsuke 	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
37133841545SHajimu UMEMOTO 		nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
37233841545SHajimu UMEMOTO 		    if_name(ifp0), if_name(altifp)));
373686cdd19SJun-ichiro itojun Hagino 		goto success;
374686cdd19SJun-ichiro itojun Hagino 	}
375686cdd19SJun-ichiro itojun Hagino 
376686cdd19SJun-ichiro itojun Hagino 	/* next, try to get it from some other hardware interface */
377b30a244cSJeffrey Hsu 	IFNET_RLOCK();
3787efe5d92SHajimu UMEMOTO 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
379686cdd19SJun-ichiro itojun Hagino 		if (ifp == ifp0)
380686cdd19SJun-ichiro itojun Hagino 			continue;
381d28bde66SSUZUKI Shinsuke 		if (in6_get_hw_ifid(ifp, in6) != 0)
382686cdd19SJun-ichiro itojun Hagino 			continue;
383686cdd19SJun-ichiro itojun Hagino 
384686cdd19SJun-ichiro itojun Hagino 		/*
385686cdd19SJun-ichiro itojun Hagino 		 * to borrow ifid from other interface, ifid needs to be
386686cdd19SJun-ichiro itojun Hagino 		 * globally unique
387686cdd19SJun-ichiro itojun Hagino 		 */
388686cdd19SJun-ichiro itojun Hagino 		if (IFID_UNIVERSAL(in6)) {
38933841545SHajimu UMEMOTO 			nd6log((LOG_DEBUG,
39033841545SHajimu UMEMOTO 			    "%s: borrow interface identifier from %s\n",
39133841545SHajimu UMEMOTO 			    if_name(ifp0), if_name(ifp)));
392b30a244cSJeffrey Hsu 			IFNET_RUNLOCK();
393686cdd19SJun-ichiro itojun Hagino 			goto success;
394686cdd19SJun-ichiro itojun Hagino 		}
395686cdd19SJun-ichiro itojun Hagino 	}
396b30a244cSJeffrey Hsu 	IFNET_RUNLOCK();
397686cdd19SJun-ichiro itojun Hagino 
398686cdd19SJun-ichiro itojun Hagino 	/* last resort: get from random number source */
399686cdd19SJun-ichiro itojun Hagino 	if (get_rand_ifid(ifp, in6) == 0) {
40033841545SHajimu UMEMOTO 		nd6log((LOG_DEBUG,
40133841545SHajimu UMEMOTO 		    "%s: interface identifier generated by random number\n",
40233841545SHajimu UMEMOTO 		    if_name(ifp0)));
403686cdd19SJun-ichiro itojun Hagino 		goto success;
404686cdd19SJun-ichiro itojun Hagino 	}
405686cdd19SJun-ichiro itojun Hagino 
406de9c893eSJun-ichiro itojun Hagino 	printf("%s: failed to get interface identifier\n", if_name(ifp0));
407686cdd19SJun-ichiro itojun Hagino 	return -1;
408686cdd19SJun-ichiro itojun Hagino 
409686cdd19SJun-ichiro itojun Hagino success:
4107efe5d92SHajimu UMEMOTO 	nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
4117efe5d92SHajimu UMEMOTO 	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
4127efe5d92SHajimu UMEMOTO 	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
41333841545SHajimu UMEMOTO 	    in6->s6_addr[14], in6->s6_addr[15]));
414686cdd19SJun-ichiro itojun Hagino 	return 0;
415686cdd19SJun-ichiro itojun Hagino }
416686cdd19SJun-ichiro itojun Hagino 
4171272577eSXin LI /*
4181272577eSXin LI  * altifp - secondary EUI64 source
4191272577eSXin LI  */
420686cdd19SJun-ichiro itojun Hagino static int
4211272577eSXin LI in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
422686cdd19SJun-ichiro itojun Hagino {
423686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia;
42433841545SHajimu UMEMOTO 	struct in6_aliasreq ifra;
425743eee66SSUZUKI Shinsuke 	struct nd_prefixctl pr0;
42633841545SHajimu UMEMOTO 	int i, error;
427686cdd19SJun-ichiro itojun Hagino 
428686cdd19SJun-ichiro itojun Hagino 	/*
42933841545SHajimu UMEMOTO 	 * configure link-local address.
430686cdd19SJun-ichiro itojun Hagino 	 */
43133841545SHajimu UMEMOTO 	bzero(&ifra, sizeof(ifra));
432686cdd19SJun-ichiro itojun Hagino 
43333841545SHajimu UMEMOTO 	/*
43433841545SHajimu UMEMOTO 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
43533841545SHajimu UMEMOTO 	 * for safety.
43633841545SHajimu UMEMOTO 	 */
43733841545SHajimu UMEMOTO 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
43833841545SHajimu UMEMOTO 
43933841545SHajimu UMEMOTO 	ifra.ifra_addr.sin6_family = AF_INET6;
44033841545SHajimu UMEMOTO 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
441a1f7e5f8SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
44233841545SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
44333841545SHajimu UMEMOTO 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
44433841545SHajimu UMEMOTO 		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
44533841545SHajimu UMEMOTO 		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
44633841545SHajimu UMEMOTO 	} else {
44733841545SHajimu UMEMOTO 		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
44833841545SHajimu UMEMOTO 			nd6log((LOG_ERR,
44933841545SHajimu UMEMOTO 			    "%s: no ifid available\n", if_name(ifp)));
4507efe5d92SHajimu UMEMOTO 			return (-1);
45133841545SHajimu UMEMOTO 		}
45233841545SHajimu UMEMOTO 	}
453a1f7e5f8SHajimu UMEMOTO 	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
454a1f7e5f8SHajimu UMEMOTO 		return (-1);
45533841545SHajimu UMEMOTO 
45633841545SHajimu UMEMOTO 	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
45733841545SHajimu UMEMOTO 	ifra.ifra_prefixmask.sin6_family = AF_INET6;
45833841545SHajimu UMEMOTO 	ifra.ifra_prefixmask.sin6_addr = in6mask64;
45933841545SHajimu UMEMOTO 	/* link-local addresses should NEVER expire. */
46033841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
46133841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
462686cdd19SJun-ichiro itojun Hagino 
46333841545SHajimu UMEMOTO 	/*
46433841545SHajimu UMEMOTO 	 * Now call in6_update_ifa() to do a bunch of procedures to configure
4652ce62dceSSUZUKI Shinsuke 	 * a link-local address. We can set the 3rd argument to NULL, because
46688ff5695SSUZUKI Shinsuke 	 * we know there's no other link-local address on the interface
46788ff5695SSUZUKI Shinsuke 	 * and therefore we are adding one (instead of updating one).
46833841545SHajimu UMEMOTO 	 */
469743eee66SSUZUKI Shinsuke 	if ((error = in6_update_ifa(ifp, &ifra, NULL,
470743eee66SSUZUKI Shinsuke 				    IN6_IFAUPDATE_DADDELAY)) != 0) {
47133841545SHajimu UMEMOTO 		/*
47233841545SHajimu UMEMOTO 		 * XXX: When the interface does not support IPv6, this call
47333841545SHajimu UMEMOTO 		 * would fail in the SIOCSIFADDR ioctl.  I believe the
47433841545SHajimu UMEMOTO 		 * notification is rather confusing in this case, so just
4757efe5d92SHajimu UMEMOTO 		 * suppress it.  (jinmei@kame.net 20010130)
47633841545SHajimu UMEMOTO 		 */
47733841545SHajimu UMEMOTO 		if (error != EAFNOSUPPORT)
4782ce62dceSSUZUKI Shinsuke 			nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
47933841545SHajimu UMEMOTO 			    "configure a link-local address on %s "
48033841545SHajimu UMEMOTO 			    "(errno=%d)\n",
4812ce62dceSSUZUKI Shinsuke 			    if_name(ifp), error));
48233841545SHajimu UMEMOTO 		return (-1);
483686cdd19SJun-ichiro itojun Hagino 	}
48433841545SHajimu UMEMOTO 
48533841545SHajimu UMEMOTO 	ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
48633841545SHajimu UMEMOTO #ifdef DIAGNOSTIC
48733841545SHajimu UMEMOTO 	if (!ia) {
48833841545SHajimu UMEMOTO 		panic("ia == NULL in in6_ifattach_linklocal");
48933841545SHajimu UMEMOTO 		/* NOTREACHED */
490686cdd19SJun-ichiro itojun Hagino 	}
491686cdd19SJun-ichiro itojun Hagino #endif
492686cdd19SJun-ichiro itojun Hagino 
49333841545SHajimu UMEMOTO 	/*
4947efe5d92SHajimu UMEMOTO 	 * Make the link-local prefix (fe80::%link/64) as on-link.
49533841545SHajimu UMEMOTO 	 * Since we'd like to manage prefixes separately from addresses,
49633841545SHajimu UMEMOTO 	 * we make an ND6 prefix structure for the link-local prefix,
49733841545SHajimu UMEMOTO 	 * and add it to the prefix list as a never-expire prefix.
49833841545SHajimu UMEMOTO 	 * XXX: this change might affect some existing code base...
49933841545SHajimu UMEMOTO 	 */
50033841545SHajimu UMEMOTO 	bzero(&pr0, sizeof(pr0));
50133841545SHajimu UMEMOTO 	pr0.ndpr_ifp = ifp;
50233841545SHajimu UMEMOTO 	/* this should be 64 at this moment. */
50333841545SHajimu UMEMOTO 	pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
50433841545SHajimu UMEMOTO 	pr0.ndpr_prefix = ifra.ifra_addr;
50533841545SHajimu UMEMOTO 	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
50633841545SHajimu UMEMOTO 	for (i = 0; i < 4; i++) {
50733841545SHajimu UMEMOTO 		pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
50833841545SHajimu UMEMOTO 		    in6mask64.s6_addr32[i];
50933841545SHajimu UMEMOTO 	}
51033841545SHajimu UMEMOTO 	/*
51133841545SHajimu UMEMOTO 	 * Initialize parameters.  The link-local prefix must always be
51233841545SHajimu UMEMOTO 	 * on-link, and its lifetimes never expire.
51333841545SHajimu UMEMOTO 	 */
51433841545SHajimu UMEMOTO 	pr0.ndpr_raf_onlink = 1;
51533841545SHajimu UMEMOTO 	pr0.ndpr_raf_auto = 1;	/* probably meaningless */
51633841545SHajimu UMEMOTO 	pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
51733841545SHajimu UMEMOTO 	pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
51833841545SHajimu UMEMOTO 	/*
51933841545SHajimu UMEMOTO 	 * Since there is no other link-local addresses, nd6_prefix_lookup()
52033841545SHajimu UMEMOTO 	 * probably returns NULL.  However, we cannot always expect the result.
52133841545SHajimu UMEMOTO 	 * For example, if we first remove the (only) existing link-local
52233841545SHajimu UMEMOTO 	 * address, and then reconfigure another one, the prefix is still
52333841545SHajimu UMEMOTO 	 * valid with referring to the old link-local address.
52433841545SHajimu UMEMOTO 	 */
52533841545SHajimu UMEMOTO 	if (nd6_prefix_lookup(&pr0) == NULL) {
52633841545SHajimu UMEMOTO 		if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
52733841545SHajimu UMEMOTO 			return (error);
528686cdd19SJun-ichiro itojun Hagino 	}
529686cdd19SJun-ichiro itojun Hagino 
530686cdd19SJun-ichiro itojun Hagino 	return 0;
531686cdd19SJun-ichiro itojun Hagino }
532686cdd19SJun-ichiro itojun Hagino 
5331272577eSXin LI /*
5341272577eSXin LI  * ifp - must be IFT_LOOP
5351272577eSXin LI  */
536686cdd19SJun-ichiro itojun Hagino static int
5371272577eSXin LI in6_ifattach_loopback(struct ifnet *ifp)
538686cdd19SJun-ichiro itojun Hagino {
53933841545SHajimu UMEMOTO 	struct in6_aliasreq ifra;
54033841545SHajimu UMEMOTO 	int error;
54133841545SHajimu UMEMOTO 
54233841545SHajimu UMEMOTO 	bzero(&ifra, sizeof(ifra));
543686cdd19SJun-ichiro itojun Hagino 
544686cdd19SJun-ichiro itojun Hagino 	/*
54533841545SHajimu UMEMOTO 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
54633841545SHajimu UMEMOTO 	 * for safety.
547686cdd19SJun-ichiro itojun Hagino 	 */
54833841545SHajimu UMEMOTO 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
549686cdd19SJun-ichiro itojun Hagino 
55033841545SHajimu UMEMOTO 	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
55133841545SHajimu UMEMOTO 	ifra.ifra_prefixmask.sin6_family = AF_INET6;
55233841545SHajimu UMEMOTO 	ifra.ifra_prefixmask.sin6_addr = in6mask128;
553686cdd19SJun-ichiro itojun Hagino 
554686cdd19SJun-ichiro itojun Hagino 	/*
555686cdd19SJun-ichiro itojun Hagino 	 * Always initialize ia_dstaddr (= broadcast address) to loopback
55633841545SHajimu UMEMOTO 	 * address.  Follows IPv4 practice - see in_ifinit().
557686cdd19SJun-ichiro itojun Hagino 	 */
55833841545SHajimu UMEMOTO 	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
55933841545SHajimu UMEMOTO 	ifra.ifra_dstaddr.sin6_family = AF_INET6;
56033841545SHajimu UMEMOTO 	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
561686cdd19SJun-ichiro itojun Hagino 
56233841545SHajimu UMEMOTO 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
56333841545SHajimu UMEMOTO 	ifra.ifra_addr.sin6_family = AF_INET6;
56433841545SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr = in6addr_loopback;
565686cdd19SJun-ichiro itojun Hagino 
56633841545SHajimu UMEMOTO 	/* the loopback  address should NEVER expire. */
56733841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
56833841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
569686cdd19SJun-ichiro itojun Hagino 
57088ff5695SSUZUKI Shinsuke 	/* we don't need to perform DAD on loopback interfaces. */
57133841545SHajimu UMEMOTO 	ifra.ifra_flags |= IN6_IFF_NODAD;
57233841545SHajimu UMEMOTO 
57333841545SHajimu UMEMOTO 	/* skip registration to the prefix list. XXX should be temporary. */
57433841545SHajimu UMEMOTO 	ifra.ifra_flags |= IN6_IFF_NOPFX;
57533841545SHajimu UMEMOTO 
57633841545SHajimu UMEMOTO 	/*
57788ff5695SSUZUKI Shinsuke 	 * We are sure that this is a newly assigned address, so we can set
57888ff5695SSUZUKI Shinsuke 	 * NULL to the 3rd arg.
57933841545SHajimu UMEMOTO 	 */
580743eee66SSUZUKI Shinsuke 	if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
5812ce62dceSSUZUKI Shinsuke 		nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
58233841545SHajimu UMEMOTO 		    "the loopback address on %s (errno=%d)\n",
5832ce62dceSSUZUKI Shinsuke 		    if_name(ifp), error));
58433841545SHajimu UMEMOTO 		return (-1);
585686cdd19SJun-ichiro itojun Hagino 	}
586686cdd19SJun-ichiro itojun Hagino 
587686cdd19SJun-ichiro itojun Hagino 	return 0;
588686cdd19SJun-ichiro itojun Hagino }
589686cdd19SJun-ichiro itojun Hagino 
590686cdd19SJun-ichiro itojun Hagino /*
591686cdd19SJun-ichiro itojun Hagino  * compute NI group address, based on the current hostname setting.
592686cdd19SJun-ichiro itojun Hagino  * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
593686cdd19SJun-ichiro itojun Hagino  *
594686cdd19SJun-ichiro itojun Hagino  * when ifp == NULL, the caller is responsible for filling scopeid.
595686cdd19SJun-ichiro itojun Hagino  */
59633841545SHajimu UMEMOTO int
5971272577eSXin LI in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
5981272577eSXin LI     struct in6_addr *in6)
599686cdd19SJun-ichiro itojun Hagino {
600686cdd19SJun-ichiro itojun Hagino 	const char *p;
60133841545SHajimu UMEMOTO 	u_char *q;
602686cdd19SJun-ichiro itojun Hagino 	MD5_CTX ctxt;
603686cdd19SJun-ichiro itojun Hagino 	u_int8_t digest[16];
604686cdd19SJun-ichiro itojun Hagino 	char l;
60533841545SHajimu UMEMOTO 	char n[64];	/* a single label must not exceed 63 chars */
606686cdd19SJun-ichiro itojun Hagino 
607686cdd19SJun-ichiro itojun Hagino 	if (!namelen || !name)
608686cdd19SJun-ichiro itojun Hagino 		return -1;
609686cdd19SJun-ichiro itojun Hagino 
610686cdd19SJun-ichiro itojun Hagino 	p = name;
611686cdd19SJun-ichiro itojun Hagino 	while (p && *p && *p != '.' && p - name < namelen)
612686cdd19SJun-ichiro itojun Hagino 		p++;
61333841545SHajimu UMEMOTO 	if (p - name > sizeof(n) - 1)
614686cdd19SJun-ichiro itojun Hagino 		return -1;	/* label too long */
615686cdd19SJun-ichiro itojun Hagino 	l = p - name;
61633841545SHajimu UMEMOTO 	strncpy(n, name, l);
61733841545SHajimu UMEMOTO 	n[(int)l] = '\0';
61833841545SHajimu UMEMOTO 	for (q = n; *q; q++) {
61933841545SHajimu UMEMOTO 		if ('A' <= *q && *q <= 'Z')
62033841545SHajimu UMEMOTO 			*q = *q - 'A' + 'a';
62133841545SHajimu UMEMOTO 	}
622686cdd19SJun-ichiro itojun Hagino 
623686cdd19SJun-ichiro itojun Hagino 	/* generate 8 bytes of pseudo-random value. */
624686cdd19SJun-ichiro itojun Hagino 	bzero(&ctxt, sizeof(ctxt));
625686cdd19SJun-ichiro itojun Hagino 	MD5Init(&ctxt);
626686cdd19SJun-ichiro itojun Hagino 	MD5Update(&ctxt, &l, sizeof(l));
62733841545SHajimu UMEMOTO 	MD5Update(&ctxt, n, l);
628686cdd19SJun-ichiro itojun Hagino 	MD5Final(digest, &ctxt);
629686cdd19SJun-ichiro itojun Hagino 
630686cdd19SJun-ichiro itojun Hagino 	bzero(in6, sizeof(*in6));
6312ce62dceSSUZUKI Shinsuke 	in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
632686cdd19SJun-ichiro itojun Hagino 	in6->s6_addr8[11] = 2;
633686cdd19SJun-ichiro itojun Hagino 	bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
634a1f7e5f8SHajimu UMEMOTO 	if (in6_setscope(in6, ifp, NULL))
635a1f7e5f8SHajimu UMEMOTO 		return (-1); /* XXX: should not fail */
636686cdd19SJun-ichiro itojun Hagino 
637686cdd19SJun-ichiro itojun Hagino 	return 0;
638686cdd19SJun-ichiro itojun Hagino }
639686cdd19SJun-ichiro itojun Hagino 
64082cd038dSYoshinobu Inoue /*
641686cdd19SJun-ichiro itojun Hagino  * XXX multiple loopback interface needs more care.  for instance,
642686cdd19SJun-ichiro itojun Hagino  * nodelocal address needs to be configured onto only one of them.
643686cdd19SJun-ichiro itojun Hagino  * XXX multiple link-local address case
6441272577eSXin LI  *
6451272577eSXin LI  * altifp - secondary EUI64 source
64682cd038dSYoshinobu Inoue  */
64782cd038dSYoshinobu Inoue void
6481272577eSXin LI in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
64982cd038dSYoshinobu Inoue {
650686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia;
651686cdd19SJun-ichiro itojun Hagino 	struct in6_addr in6;
65233841545SHajimu UMEMOTO 
65333841545SHajimu UMEMOTO 	/* some of the interfaces are inherently not IPv6 capable */
65433841545SHajimu UMEMOTO 	switch (ifp->if_type) {
655cc5934f5SMax Laier 	case IFT_PFLOG:
656cc5934f5SMax Laier 	case IFT_PFSYNC:
657a9771948SGleb Smirnoff 	case IFT_CARP:
658cc5934f5SMax Laier 		return;
65933841545SHajimu UMEMOTO 	}
66082cd038dSYoshinobu Inoue 
66182cd038dSYoshinobu Inoue 	/*
662686cdd19SJun-ichiro itojun Hagino 	 * quirks based on interface type
66382cd038dSYoshinobu Inoue 	 */
664686cdd19SJun-ichiro itojun Hagino 	switch (ifp->if_type) {
665686cdd19SJun-ichiro itojun Hagino #ifdef IFT_STF
666686cdd19SJun-ichiro itojun Hagino 	case IFT_STF:
66782cd038dSYoshinobu Inoue 		/*
66888ff5695SSUZUKI Shinsuke 		 * 6to4 interface is a very special kind of beast.
66988ff5695SSUZUKI Shinsuke 		 * no multicast, no linklocal.  RFC2529 specifies how to make
67088ff5695SSUZUKI Shinsuke 		 * linklocals for 6to4 interface, but there's no use and
67188ff5695SSUZUKI Shinsuke 		 * it is rather harmful to have one.
67282cd038dSYoshinobu Inoue 		 */
673686cdd19SJun-ichiro itojun Hagino 		goto statinit;
674686cdd19SJun-ichiro itojun Hagino #endif
67582cd038dSYoshinobu Inoue 	default:
67682cd038dSYoshinobu Inoue 		break;
67782cd038dSYoshinobu Inoue 	}
67882cd038dSYoshinobu Inoue 
679686cdd19SJun-ichiro itojun Hagino 	/*
680686cdd19SJun-ichiro itojun Hagino 	 * usually, we require multicast capability to the interface
681686cdd19SJun-ichiro itojun Hagino 	 */
682686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
6832ce62dceSSUZUKI Shinsuke 		nd6log((LOG_INFO, "in6_ifattach: "
68433841545SHajimu UMEMOTO 		    "%s is not multicast capable, IPv6 not enabled\n",
6852ce62dceSSUZUKI Shinsuke 		    if_name(ifp)));
68682cd038dSYoshinobu Inoue 		return;
68782cd038dSYoshinobu Inoue 	}
688686cdd19SJun-ichiro itojun Hagino 
689686cdd19SJun-ichiro itojun Hagino 	/*
69033841545SHajimu UMEMOTO 	 * assign loopback address for loopback interface.
69133841545SHajimu UMEMOTO 	 * XXX multiple loopback interface case.
692686cdd19SJun-ichiro itojun Hagino 	 */
69333841545SHajimu UMEMOTO 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
694686cdd19SJun-ichiro itojun Hagino 		in6 = in6addr_loopback;
695686cdd19SJun-ichiro itojun Hagino 		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
696686cdd19SJun-ichiro itojun Hagino 			if (in6_ifattach_loopback(ifp) != 0)
697686cdd19SJun-ichiro itojun Hagino 				return;
69882cd038dSYoshinobu Inoue 		}
699686cdd19SJun-ichiro itojun Hagino 	}
700686cdd19SJun-ichiro itojun Hagino 
70133841545SHajimu UMEMOTO 	/*
70233841545SHajimu UMEMOTO 	 * assign a link-local address, if there's none.
70333841545SHajimu UMEMOTO 	 */
70459280079SAndrew Thompson 	if (ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) {
70533841545SHajimu UMEMOTO 		ia = in6ifa_ifpforlinklocal(ifp, 0);
70633841545SHajimu UMEMOTO 		if (ia == NULL) {
70733841545SHajimu UMEMOTO 			if (in6_ifattach_linklocal(ifp, altifp) == 0) {
70833841545SHajimu UMEMOTO 				/* linklocal address assigned */
70933841545SHajimu UMEMOTO 			} else {
71033841545SHajimu UMEMOTO 				/* failed to assign linklocal address. bark? */
711686cdd19SJun-ichiro itojun Hagino 			}
71233841545SHajimu UMEMOTO 		}
71333841545SHajimu UMEMOTO 	}
71433841545SHajimu UMEMOTO 
71533841545SHajimu UMEMOTO #ifdef IFT_STF			/* XXX */
71633841545SHajimu UMEMOTO statinit:
717686cdd19SJun-ichiro itojun Hagino #endif
71882cd038dSYoshinobu Inoue 
71982cd038dSYoshinobu Inoue 	/* update dynamically. */
72082cd038dSYoshinobu Inoue 	if (in6_maxmtu < ifp->if_mtu)
72182cd038dSYoshinobu Inoue 		in6_maxmtu = ifp->if_mtu;
72282cd038dSYoshinobu Inoue }
72382cd038dSYoshinobu Inoue 
724686cdd19SJun-ichiro itojun Hagino /*
725686cdd19SJun-ichiro itojun Hagino  * NOTE: in6_ifdetach() does not support loopback if at this moment.
72633841545SHajimu UMEMOTO  * We don't need this function in bsdi, because interfaces are never removed
72733841545SHajimu UMEMOTO  * from the ifnet list in bsdi.
728686cdd19SJun-ichiro itojun Hagino  */
72982cd038dSYoshinobu Inoue void
7301272577eSXin LI in6_ifdetach(struct ifnet *ifp)
73182cd038dSYoshinobu Inoue {
73282cd038dSYoshinobu Inoue 	struct in6_ifaddr *ia, *oia;
733686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ifa, *next;
73482cd038dSYoshinobu Inoue 	struct rtentry *rt;
73582cd038dSYoshinobu Inoue 	short rtflags;
736686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 sin6;
73709a52a55SJINMEI Tatuya 	struct in6_multi_mship *imm;
73882cd038dSYoshinobu Inoue 
739686cdd19SJun-ichiro itojun Hagino 	/* remove neighbor management table */
740686cdd19SJun-ichiro itojun Hagino 	nd6_purge(ifp);
741686cdd19SJun-ichiro itojun Hagino 
742686cdd19SJun-ichiro itojun Hagino 	/* nuke any of IPv6 addresses we have */
7437efe5d92SHajimu UMEMOTO 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) {
744686cdd19SJun-ichiro itojun Hagino 		next = ifa->ifa_list.tqe_next;
745686cdd19SJun-ichiro itojun Hagino 		if (ifa->ifa_addr->sa_family != AF_INET6)
746686cdd19SJun-ichiro itojun Hagino 			continue;
74733841545SHajimu UMEMOTO 		in6_purgeaddr(ifa);
748686cdd19SJun-ichiro itojun Hagino 	}
749686cdd19SJun-ichiro itojun Hagino 
750686cdd19SJun-ichiro itojun Hagino 	/* undo everything done by in6_ifattach(), just in case */
7517efe5d92SHajimu UMEMOTO 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) {
752686cdd19SJun-ichiro itojun Hagino 		next = ifa->ifa_list.tqe_next;
753686cdd19SJun-ichiro itojun Hagino 
75482cd038dSYoshinobu Inoue 		if (ifa->ifa_addr->sa_family != AF_INET6
75582cd038dSYoshinobu Inoue 		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
75682cd038dSYoshinobu Inoue 			continue;
75782cd038dSYoshinobu Inoue 		}
75882cd038dSYoshinobu Inoue 
75982cd038dSYoshinobu Inoue 		ia = (struct in6_ifaddr *)ifa;
76082cd038dSYoshinobu Inoue 
76109a52a55SJINMEI Tatuya 		/*
76209a52a55SJINMEI Tatuya 		 * leave from multicast groups we have joined for the interface
76309a52a55SJINMEI Tatuya 		 */
76409a52a55SJINMEI Tatuya 		while ((imm = ia->ia6_memberships.lh_first) != NULL) {
76509a52a55SJINMEI Tatuya 			LIST_REMOVE(imm, i6mm_chain);
76609a52a55SJINMEI Tatuya 			in6_leavegroup(imm);
76709a52a55SJINMEI Tatuya 		}
76809a52a55SJINMEI Tatuya 
76982cd038dSYoshinobu Inoue 		/* remove from the routing table */
7707efe5d92SHajimu UMEMOTO 		if ((ia->ia_flags & IFA_ROUTE) &&
7717efe5d92SHajimu UMEMOTO 		    (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
77282cd038dSYoshinobu Inoue 			rtflags = rt->rt_flags;
77382cd038dSYoshinobu Inoue 			rtfree(rt);
7747efe5d92SHajimu UMEMOTO 			rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
77582cd038dSYoshinobu Inoue 			    (struct sockaddr *)&ia->ia_addr,
77682cd038dSYoshinobu Inoue 			    (struct sockaddr *)&ia->ia_prefixmask,
77782cd038dSYoshinobu Inoue 			    rtflags, (struct rtentry **)0);
77882cd038dSYoshinobu Inoue 		}
77982cd038dSYoshinobu Inoue 
78082cd038dSYoshinobu Inoue 		/* remove from the linked list */
78182cd038dSYoshinobu Inoue 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
782686cdd19SJun-ichiro itojun Hagino 		IFAFREE(&ia->ia_ifa);
78382cd038dSYoshinobu Inoue 
78482cd038dSYoshinobu Inoue 		/* also remove from the IPv6 address chain(itojun&jinmei) */
78582cd038dSYoshinobu Inoue 		oia = ia;
78682cd038dSYoshinobu Inoue 		if (oia == (ia = in6_ifaddr))
78782cd038dSYoshinobu Inoue 			in6_ifaddr = ia->ia_next;
78882cd038dSYoshinobu Inoue 		else {
78982cd038dSYoshinobu Inoue 			while (ia->ia_next && (ia->ia_next != oia))
79082cd038dSYoshinobu Inoue 				ia = ia->ia_next;
79182cd038dSYoshinobu Inoue 			if (ia->ia_next)
79282cd038dSYoshinobu Inoue 				ia->ia_next = oia->ia_next;
79333841545SHajimu UMEMOTO 			else {
79433841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
7957efe5d92SHajimu UMEMOTO 				    "%s: didn't unlink in6ifaddr from list\n",
7967efe5d92SHajimu UMEMOTO 				    if_name(ifp)));
79733841545SHajimu UMEMOTO 			}
79882cd038dSYoshinobu Inoue 		}
79982cd038dSYoshinobu Inoue 
800686cdd19SJun-ichiro itojun Hagino 		IFAFREE(&oia->ia_ifa);
801686cdd19SJun-ichiro itojun Hagino 	}
802686cdd19SJun-ichiro itojun Hagino 
80304f20737SRobert Watson 	in6_pcbpurgeif0(&udbinfo, ifp);
80404f20737SRobert Watson 	in6_pcbpurgeif0(&ripcbinfo, ifp);
80571498f30SBruce M Simpson 	/* leave from all multicast groups joined */
80671498f30SBruce M Simpson 	in6_purgemaddrs(ifp);
807686cdd19SJun-ichiro itojun Hagino 
80833841545SHajimu UMEMOTO 	/*
80933841545SHajimu UMEMOTO 	 * remove neighbor management table.  we call it twice just to make
81033841545SHajimu UMEMOTO 	 * sure we nuke everything.  maybe we need just one call.
81133841545SHajimu UMEMOTO 	 * XXX: since the first call did not release addresses, some prefixes
81233841545SHajimu UMEMOTO 	 * might remain.  We should call nd6_purge() again to release the
81333841545SHajimu UMEMOTO 	 * prefixes after removing all addresses above.
81433841545SHajimu UMEMOTO 	 * (Or can we just delay calling nd6_purge until at this point?)
81533841545SHajimu UMEMOTO 	 */
816686cdd19SJun-ichiro itojun Hagino 	nd6_purge(ifp);
817686cdd19SJun-ichiro itojun Hagino 
818686cdd19SJun-ichiro itojun Hagino 	/* remove route to link-local allnodes multicast (ff02::1) */
819686cdd19SJun-ichiro itojun Hagino 	bzero(&sin6, sizeof(sin6));
820686cdd19SJun-ichiro itojun Hagino 	sin6.sin6_len = sizeof(struct sockaddr_in6);
821686cdd19SJun-ichiro itojun Hagino 	sin6.sin6_family = AF_INET6;
822686cdd19SJun-ichiro itojun Hagino 	sin6.sin6_addr = in6addr_linklocal_allnodes;
823a1f7e5f8SHajimu UMEMOTO 	if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
824a1f7e5f8SHajimu UMEMOTO 		/* XXX: should not fail */
825a1f7e5f8SHajimu UMEMOTO 		return;
8269c63e9dbSSam Leffler 	/* XXX grab lock first to avoid LOR */
8278b07e49aSJulian Elischer 	if (rt_tables[0][AF_INET6] != NULL) {
8288b07e49aSJulian Elischer 		RADIX_NODE_HEAD_LOCK(rt_tables[0][AF_INET6]);
829e699b0aeSHajimu UMEMOTO 		rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
830d1dd20beSSam Leffler 		if (rt) {
8319c63e9dbSSam Leffler 			if (rt->rt_ifp == ifp)
8329c63e9dbSSam Leffler 				rtexpunge(rt);
8339c63e9dbSSam Leffler 			RTFREE_LOCKED(rt);
83482cd038dSYoshinobu Inoue 		}
8358b07e49aSJulian Elischer 		RADIX_NODE_HEAD_UNLOCK(rt_tables[0][AF_INET6]);
83682cd038dSYoshinobu Inoue 	}
8373f02eaf3SDon Lewis }
83833841545SHajimu UMEMOTO 
839743eee66SSUZUKI Shinsuke int
8401272577eSXin LI in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
8411272577eSXin LI     const u_int8_t *baseid, int generate)
84233841545SHajimu UMEMOTO {
84333841545SHajimu UMEMOTO 	u_int8_t nullbuf[8];
84431b1bfe1SHajimu UMEMOTO 	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
84533841545SHajimu UMEMOTO 
84633841545SHajimu UMEMOTO 	bzero(nullbuf, sizeof(nullbuf));
84733841545SHajimu UMEMOTO 	if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
84833841545SHajimu UMEMOTO 		/* we've never created a random ID.  Create a new one. */
84933841545SHajimu UMEMOTO 		generate = 1;
85033841545SHajimu UMEMOTO 	}
85133841545SHajimu UMEMOTO 
85233841545SHajimu UMEMOTO 	if (generate) {
85333841545SHajimu UMEMOTO 		bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
85433841545SHajimu UMEMOTO 
85533841545SHajimu UMEMOTO 		/* generate_tmp_ifid will update seedn and buf */
85633841545SHajimu UMEMOTO 		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
85733841545SHajimu UMEMOTO 		    ndi->randomid);
85833841545SHajimu UMEMOTO 	}
85933841545SHajimu UMEMOTO 	bcopy(ndi->randomid, retbuf, 8);
860743eee66SSUZUKI Shinsuke 
861743eee66SSUZUKI Shinsuke 	return (0);
86233841545SHajimu UMEMOTO }
86333841545SHajimu UMEMOTO 
86433841545SHajimu UMEMOTO void
8651272577eSXin LI in6_tmpaddrtimer(void *ignored_arg)
86633841545SHajimu UMEMOTO {
86733841545SHajimu UMEMOTO 	struct nd_ifinfo *ndi;
86833841545SHajimu UMEMOTO 	u_int8_t nullbuf[8];
86931b1bfe1SHajimu UMEMOTO 	struct ifnet *ifp;
87033841545SHajimu UMEMOTO 	int s = splnet();
87133841545SHajimu UMEMOTO 
87233841545SHajimu UMEMOTO 	callout_reset(&in6_tmpaddrtimer_ch,
87333841545SHajimu UMEMOTO 	    (ip6_temp_preferred_lifetime - ip6_desync_factor -
8747efe5d92SHajimu UMEMOTO 	    ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL);
87533841545SHajimu UMEMOTO 
87633841545SHajimu UMEMOTO 	bzero(nullbuf, sizeof(nullbuf));
87731b1bfe1SHajimu UMEMOTO 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
87831b1bfe1SHajimu UMEMOTO 		ndi = ND_IFINFO(ifp);
87933841545SHajimu UMEMOTO 		if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
88033841545SHajimu UMEMOTO 			/*
88133841545SHajimu UMEMOTO 			 * We've been generating a random ID on this interface.
88233841545SHajimu UMEMOTO 			 * Create a new one.
88333841545SHajimu UMEMOTO 			 */
88433841545SHajimu UMEMOTO 			(void)generate_tmp_ifid(ndi->randomseed0,
8857efe5d92SHajimu UMEMOTO 			    ndi->randomseed1, ndi->randomid);
88633841545SHajimu UMEMOTO 		}
88733841545SHajimu UMEMOTO 	}
88833841545SHajimu UMEMOTO 
88933841545SHajimu UMEMOTO 	splx(s);
89033841545SHajimu UMEMOTO }
89171498f30SBruce M Simpson 
89271498f30SBruce M Simpson static void
8931272577eSXin LI in6_purgemaddrs(struct ifnet *ifp)
89471498f30SBruce M Simpson {
89571498f30SBruce M Simpson 	struct in6_multi *in6m;
89671498f30SBruce M Simpson 	struct in6_multi *oin6m;
89771498f30SBruce M Simpson 
89871498f30SBruce M Simpson #ifdef DIAGNOSTIC
89971498f30SBruce M Simpson 	printf("%s: purging ifp %p\n", __func__, ifp);
90071498f30SBruce M Simpson #endif
90171498f30SBruce M Simpson 
90271498f30SBruce M Simpson 	IFF_LOCKGIANT(ifp);
90371498f30SBruce M Simpson 	LIST_FOREACH_SAFE(in6m, &in6_multihead, in6m_entry, oin6m) {
90471498f30SBruce M Simpson 		if (in6m->in6m_ifp == ifp)
90571498f30SBruce M Simpson 			in6_delmulti(in6m);
90671498f30SBruce M Simpson 	}
90771498f30SBruce M Simpson 	IFF_UNLOCKGIANT(ifp);
90871498f30SBruce M Simpson }
909