Home | History | Annotate | Line # | Download | only in netinet6
in6_ifattach.c revision 1.114.2.1
      1  1.114.2.1  pgoyette /*	$NetBSD: in6_ifattach.c,v 1.114.2.1 2018/05/02 07:20:23 pgoyette Exp $	*/
      2       1.37    itojun /*	$KAME: in6_ifattach.c,v 1.124 2001/07/18 08:32:51 jinmei Exp $	*/
      3        1.3   thorpej 
      4        1.2    itojun /*
      5        1.2    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6        1.2    itojun  * All rights reserved.
      7       1.24    itojun  *
      8        1.2    itojun  * Redistribution and use in source and binary forms, with or without
      9        1.2    itojun  * modification, are permitted provided that the following conditions
     10        1.2    itojun  * are met:
     11        1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     12        1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     13        1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     15        1.2    itojun  *    documentation and/or other materials provided with the distribution.
     16        1.2    itojun  * 3. Neither the name of the project nor the names of its contributors
     17        1.2    itojun  *    may be used to endorse or promote products derived from this software
     18        1.2    itojun  *    without specific prior written permission.
     19       1.24    itojun  *
     20        1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21        1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22        1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23        1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24        1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25        1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26        1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27        1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28        1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29        1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30        1.2    itojun  * SUCH DAMAGE.
     31        1.2    itojun  */
     32       1.39     lukem 
     33       1.39     lukem #include <sys/cdefs.h>
     34  1.114.2.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: in6_ifattach.c,v 1.114.2.1 2018/05/02 07:20:23 pgoyette Exp $");
     35        1.2    itojun 
     36        1.2    itojun #include <sys/param.h>
     37        1.2    itojun #include <sys/systm.h>
     38       1.83    dyoung #include <sys/kmem.h>
     39        1.2    itojun #include <sys/socket.h>
     40        1.2    itojun #include <sys/sockio.h>
     41       1.13    itojun #include <sys/kernel.h>
     42       1.34    itojun #include <sys/syslog.h>
     43       1.13    itojun #include <sys/md5.h>
     44       1.80        ad #include <sys/socketvar.h>
     45       1.86       tls #include <sys/cprng.h>
     46        1.2    itojun 
     47        1.2    itojun #include <net/if.h>
     48        1.2    itojun #include <net/if_dl.h>
     49        1.2    itojun #include <net/if_types.h>
     50        1.2    itojun #include <net/route.h>
     51        1.2    itojun 
     52        1.2    itojun #include <netinet/in.h>
     53        1.2    itojun #include <netinet/in_var.h>
     54        1.2    itojun 
     55       1.19    itojun #include <netinet/ip6.h>
     56        1.2    itojun #include <netinet6/in6_ifattach.h>
     57        1.2    itojun #include <netinet6/ip6_var.h>
     58        1.2    itojun #include <netinet6/nd6.h>
     59       1.55    itojun #include <netinet6/ip6_mroute.h>
     60       1.63    rpaulo #include <netinet6/scope6_var.h>
     61        1.2    itojun 
     62        1.2    itojun unsigned long in6_maxmtu = 0;
     63        1.2    itojun 
     64       1.48    itojun int ip6_auto_linklocal = 1;	/* enable by default */
     65       1.48    itojun 
     66       1.72        ad callout_t in6_tmpaddrtimer_ch;
     67       1.64    rpaulo 
     68       1.64    rpaulo 
     69       1.64    rpaulo #if 0
     70       1.74    dyoung static int get_hostid_ifid(struct ifnet *, struct in6_addr *);
     71       1.64    rpaulo #endif
     72       1.97     ozaki static int get_rand_ifid(struct in6_addr *);
     73       1.74    dyoung static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
     74       1.74    dyoung static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
     75       1.74    dyoung static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
     76       1.74    dyoung static int in6_ifattach_loopback(struct ifnet *);
     77       1.25    itojun 
     78       1.25    itojun #define EUI64_GBIT	0x01
     79       1.25    itojun #define EUI64_UBIT	0x02
     80       1.54     perry #define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (/*CONSTCOND*/ 0)
     81       1.25    itojun #define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
     82       1.25    itojun #define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
     83       1.25    itojun #define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
     84       1.25    itojun #define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
     85       1.25    itojun 
     86       1.25    itojun #define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
     87       1.25    itojun #define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
     88       1.25    itojun 
     89       1.64    rpaulo #define GEN_TEMPID_RETRY_MAX 5
     90       1.64    rpaulo 
     91       1.64    rpaulo #if 0
     92       1.64    rpaulo /*
     93       1.64    rpaulo  * Generate a last-resort interface identifier from hostid.
     94       1.64    rpaulo  * works only for certain architectures (like sparc).
     95       1.64    rpaulo  * also, using hostid itself may constitute a privacy threat, much worse
     96       1.64    rpaulo  * than MAC addresses (hostids are used for software licensing).
     97       1.64    rpaulo  * maybe we should use MD5(hostid) instead.
     98       1.71  christos  *
     99       1.71  christos  * in6 - upper 64bits are preserved
    100       1.64    rpaulo  */
    101       1.64    rpaulo static int
    102       1.71  christos get_hostid_ifid(struct ifnet *ifp, struct in6_addr *in6)
    103       1.64    rpaulo {
    104       1.64    rpaulo 	int off, len;
    105       1.64    rpaulo 	static const uint8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    106       1.64    rpaulo 	static const uint8_t allone[8] =
    107       1.64    rpaulo 	    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    108       1.64    rpaulo 
    109       1.64    rpaulo 	if (!hostid)
    110       1.64    rpaulo 		return -1;
    111       1.64    rpaulo 
    112       1.64    rpaulo 	/* get up to 8 bytes from the hostid field - should we get */
    113       1.64    rpaulo 	len = (sizeof(hostid) > 8) ? 8 : sizeof(hostid);
    114       1.64    rpaulo 	off = sizeof(*in6) - len;
    115       1.64    rpaulo 	memcpy(&in6->s6_addr[off], &hostid, len);
    116       1.64    rpaulo 
    117       1.64    rpaulo 	/* make sure we do not return anything bogus */
    118       1.64    rpaulo 	if (memcmp(&in6->s6_addr[8], allzero, sizeof(allzero)))
    119       1.64    rpaulo 		return -1;
    120       1.64    rpaulo 	if (memcmp(&in6->s6_addr[8], allone, sizeof(allone)))
    121       1.64    rpaulo 		return -1;
    122       1.64    rpaulo 
    123       1.64    rpaulo 	/* make sure to set "u" bit to local, and "g" bit to individual. */
    124       1.64    rpaulo 	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
    125       1.64    rpaulo 	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
    126       1.64    rpaulo 
    127       1.64    rpaulo 	/* convert EUI64 into IPv6 interface identifier */
    128       1.64    rpaulo 	EUI64_TO_IFID(in6);
    129       1.64    rpaulo 
    130       1.64    rpaulo 	return 0;
    131       1.64    rpaulo }
    132       1.64    rpaulo #endif
    133       1.64    rpaulo 
    134       1.25    itojun /*
    135       1.25    itojun  * Generate a last-resort interface identifier, when the machine has no
    136       1.25    itojun  * IEEE802/EUI64 address sources.
    137       1.25    itojun  * The goal here is to get an interface identifier that is
    138       1.25    itojun  * (1) random enough and (2) does not change across reboot.
    139       1.25    itojun  * We currently use MD5(hostname) for it.
    140       1.25    itojun  */
    141       1.25    itojun static int
    142       1.97     ozaki get_rand_ifid(struct in6_addr *in6)	/* upper 64bits are preserved */
    143       1.25    itojun {
    144       1.25    itojun 	MD5_CTX ctxt;
    145       1.25    itojun 	u_int8_t digest[16];
    146       1.25    itojun 
    147       1.25    itojun #if 0
    148       1.25    itojun 	/* we need at least several letters as seed for ifid */
    149       1.25    itojun 	if (hostnamelen < 3)
    150       1.25    itojun 		return -1;
    151       1.25    itojun #endif
    152       1.25    itojun 
    153       1.25    itojun 	/* generate 8 bytes of pseudo-random value. */
    154       1.64    rpaulo 	memset(&ctxt, 0, sizeof(ctxt));
    155       1.25    itojun 	MD5Init(&ctxt);
    156       1.50    itojun 	MD5Update(&ctxt, (u_char *)hostname, hostnamelen);
    157       1.25    itojun 	MD5Final(digest, &ctxt);
    158       1.25    itojun 
    159       1.25    itojun 	/* assumes sizeof(digest) > sizeof(ifid) */
    160       1.64    rpaulo 	memcpy(&in6->s6_addr[8], digest, 8);
    161        1.2    itojun 
    162       1.25    itojun 	/* make sure to set "u" bit to local, and "g" bit to individual. */
    163       1.25    itojun 	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
    164       1.25    itojun 	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
    165       1.25    itojun 
    166       1.25    itojun 	/* convert EUI64 into IPv6 interface identifier */
    167       1.25    itojun 	EUI64_TO_IFID(in6);
    168       1.25    itojun 
    169       1.25    itojun 	return 0;
    170       1.25    itojun }
    171        1.2    itojun 
    172       1.64    rpaulo static int
    173       1.71  christos generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
    174       1.64    rpaulo {
    175       1.64    rpaulo 	MD5_CTX ctxt;
    176       1.64    rpaulo 	u_int8_t seed[16], digest[16], nullbuf[8];
    177       1.64    rpaulo 	/*
    178       1.64    rpaulo 	 * interface ID for subnet anycast addresses.
    179       1.64    rpaulo 	 * XXX: we assume the unicast address range that requires IDs
    180       1.64    rpaulo 	 * in EUI-64 format.
    181       1.64    rpaulo 	 */
    182       1.64    rpaulo 	static const uint8_t anycast_id[8] = { 0xfd, 0xff, 0xff, 0xff,
    183       1.64    rpaulo 	    0xff, 0xff, 0xff, 0x80 };
    184       1.64    rpaulo 	static const uint8_t isatap_id[4] = { 0x00, 0x00, 0x5e, 0xfe };
    185       1.64    rpaulo 	int badid, retry = 0;
    186       1.64    rpaulo 
    187       1.64    rpaulo 	/* If there's no hisotry, start with a random seed. */
    188       1.64    rpaulo 	memset(nullbuf, 0, sizeof(nullbuf));
    189       1.64    rpaulo 	if (memcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
    190       1.86       tls 		cprng_fast(seed, sizeof(seed));
    191       1.64    rpaulo 	} else
    192       1.64    rpaulo 		memcpy(seed, seed0, 8);
    193       1.64    rpaulo 
    194       1.64    rpaulo 	/* copy the right-most 64-bits of the given address */
    195       1.64    rpaulo 	/* XXX assumption on the size of IFID */
    196       1.64    rpaulo 	memcpy(&seed[8], seed1, 8);
    197       1.64    rpaulo 
    198       1.64    rpaulo   again:
    199       1.64    rpaulo 	/* for debugging purposes only */
    200       1.64    rpaulo #if 0
    201       1.64    rpaulo 	{
    202       1.64    rpaulo 		int i;
    203       1.64    rpaulo 
    204       1.64    rpaulo 		printf("generate_tmp_ifid: new randomized ID from: ");
    205       1.64    rpaulo 		for (i = 0; i < 16; i++)
    206       1.64    rpaulo 			printf("%02x", seed[i]);
    207       1.64    rpaulo 		printf(" ");
    208       1.64    rpaulo 	}
    209       1.64    rpaulo #endif
    210       1.64    rpaulo 
    211       1.64    rpaulo 	/* generate 16 bytes of pseudo-random value. */
    212       1.64    rpaulo 	memset(&ctxt, 0, sizeof(ctxt));
    213       1.64    rpaulo 	MD5Init(&ctxt);
    214       1.64    rpaulo 	MD5Update(&ctxt, seed, sizeof(seed));
    215       1.64    rpaulo 	MD5Final(digest, &ctxt);
    216       1.64    rpaulo 
    217       1.64    rpaulo 	/*
    218       1.64    rpaulo 	 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (3)
    219       1.64    rpaulo 	 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
    220       1.64    rpaulo 	 * left-most bit is numbered 0) to zero.
    221       1.64    rpaulo 	 */
    222       1.64    rpaulo 	memcpy(ret, digest, 8);
    223       1.64    rpaulo 	ret[0] &= ~EUI64_UBIT;
    224       1.64    rpaulo 
    225       1.64    rpaulo 	/*
    226       1.64    rpaulo 	 * Reject inappropriate identifiers according to
    227       1.64    rpaulo 	 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (4)
    228       1.64    rpaulo 	 * At this moment, we reject following cases:
    229       1.64    rpaulo 	 * - all 0 identifier
    230       1.64    rpaulo 	 * - identifiers that conflict with reserved subnet anycast addresses,
    231       1.64    rpaulo 	 *   which are defined in RFC 2526.
    232       1.64    rpaulo 	 * - identifiers that conflict with ISATAP addresses
    233       1.64    rpaulo 	 * - identifiers used in our own addresses
    234       1.64    rpaulo 	 */
    235       1.64    rpaulo 	badid = 0;
    236       1.64    rpaulo 	if (memcmp(nullbuf, ret, sizeof(nullbuf)) == 0)
    237       1.64    rpaulo 		badid = 1;
    238       1.64    rpaulo 	else if (memcmp(anycast_id, ret, 7) == 0 &&
    239       1.64    rpaulo 	    (anycast_id[7] & ret[7]) == anycast_id[7]) {
    240       1.64    rpaulo 		badid = 1;
    241       1.64    rpaulo 	} else if (memcmp(isatap_id, ret, sizeof(isatap_id)) == 0)
    242       1.64    rpaulo 		badid = 1;
    243       1.64    rpaulo 	else {
    244       1.64    rpaulo 		struct in6_ifaddr *ia;
    245      1.102     ozaki 		int s = pserialize_read_enter();
    246       1.64    rpaulo 
    247      1.100     ozaki 		IN6_ADDRLIST_READER_FOREACH(ia) {
    248       1.64    rpaulo 			if (!memcmp(&ia->ia_addr.sin6_addr.s6_addr[8],
    249       1.64    rpaulo 			    ret, 8)) {
    250       1.64    rpaulo 				badid = 1;
    251       1.64    rpaulo 				break;
    252       1.64    rpaulo 			}
    253       1.64    rpaulo 		}
    254      1.102     ozaki 		pserialize_read_exit(s);
    255       1.64    rpaulo 	}
    256       1.64    rpaulo 
    257       1.64    rpaulo 	/*
    258       1.64    rpaulo 	 * In the event that an unacceptable identifier has been generated,
    259       1.64    rpaulo 	 * restart the process, using the right-most 64 bits of the MD5 digest
    260       1.64    rpaulo 	 * obtained in place of the history value.
    261       1.64    rpaulo 	 */
    262       1.64    rpaulo 	if (badid) {
    263       1.64    rpaulo 		/* for debugging purposes only */
    264       1.64    rpaulo #if 0
    265       1.64    rpaulo 		{
    266       1.64    rpaulo 			int i;
    267       1.64    rpaulo 
    268       1.64    rpaulo 			printf("unacceptable random ID: ");
    269       1.64    rpaulo 			for (i = 0; i < 16; i++)
    270       1.64    rpaulo 				printf("%02x", digest[i]);
    271       1.64    rpaulo 			printf("\n");
    272       1.64    rpaulo 		}
    273       1.64    rpaulo #endif
    274       1.64    rpaulo 
    275       1.64    rpaulo 		if (++retry < GEN_TEMPID_RETRY_MAX) {
    276       1.64    rpaulo 			memcpy(seed, &digest[8], 8);
    277       1.64    rpaulo 			goto again;
    278       1.64    rpaulo 		} else {
    279       1.64    rpaulo 			/*
    280       1.64    rpaulo 			 * We're so unlucky.  Give up for now, and return
    281       1.64    rpaulo 			 * all 0 IDs to tell the caller not to make a
    282       1.64    rpaulo 			 * temporary address.
    283       1.64    rpaulo 			 */
    284       1.96     ozaki 			nd6log(LOG_NOTICE, "never found a good ID\n");
    285       1.64    rpaulo 			memset(ret, 0, 8);
    286       1.64    rpaulo 		}
    287       1.64    rpaulo 	}
    288       1.64    rpaulo 
    289       1.64    rpaulo 	/*
    290       1.64    rpaulo 	 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (6)
    291       1.64    rpaulo 	 * Take the rightmost 64-bits of the MD5 digest and save them in
    292       1.64    rpaulo 	 * stable storage as the history value to be used in the next
    293       1.64    rpaulo 	 * iteration of the algorithm.
    294       1.64    rpaulo 	 */
    295       1.64    rpaulo 	memcpy(seed0, &digest[8], 8);
    296       1.64    rpaulo 
    297       1.64    rpaulo 	/* for debugging purposes only */
    298       1.64    rpaulo #if 0
    299       1.64    rpaulo 	{
    300       1.64    rpaulo 		int i;
    301       1.64    rpaulo 
    302       1.64    rpaulo 		printf("to: ");
    303       1.64    rpaulo 		for (i = 0; i < 16; i++)
    304       1.64    rpaulo 			printf("%02x", digest[i]);
    305       1.64    rpaulo 		printf("\n");
    306       1.64    rpaulo 	}
    307       1.64    rpaulo #endif
    308       1.64    rpaulo 
    309       1.64    rpaulo 	return 0;
    310       1.64    rpaulo }
    311       1.81    dyoung 
    312       1.25    itojun /*
    313       1.25    itojun  * Get interface identifier for the specified interface.
    314       1.71  christos  *
    315       1.71  christos  * in6 - upper 64bits are preserved
    316       1.25    itojun  */
    317       1.64    rpaulo int
    318       1.71  christos in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
    319        1.2    itojun {
    320       1.25    itojun 	struct ifaddr *ifa;
    321      1.103     ozaki 	const struct sockaddr_dl *sdl = NULL;
    322      1.104     ozaki 	const char *addr = NULL; /* XXX gcc 4.8 -Werror=maybe-uninitialized */
    323      1.104     ozaki 	size_t addrlen = 0; /* XXX gcc 4.8 -Werror=maybe-uninitialized */
    324       1.25    itojun 	static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    325       1.25    itojun 	static u_int8_t allone[8] =
    326       1.25    itojun 		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    327      1.103     ozaki 	int s;
    328       1.25    itojun 
    329      1.103     ozaki 	s = pserialize_read_enter();
    330      1.101     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
    331      1.103     ozaki 		const struct sockaddr_dl *tsdl;
    332       1.25    itojun 		if (ifa->ifa_addr->sa_family != AF_LINK)
    333       1.25    itojun 			continue;
    334       1.81    dyoung 		tsdl = satocsdl(ifa->ifa_addr);
    335       1.81    dyoung 		if (tsdl == NULL || tsdl->sdl_alen == 0)
    336       1.25    itojun 			continue;
    337      1.103     ozaki 		if (sdl == NULL || ifa == ifp->if_dl || ifa == ifp->if_hwdl) {
    338       1.81    dyoung 			sdl = tsdl;
    339      1.103     ozaki 			addr = CLLADDR(sdl);
    340      1.103     ozaki 			addrlen = sdl->sdl_alen;
    341      1.103     ozaki 		}
    342       1.81    dyoung 		if (ifa == ifp->if_hwdl)
    343       1.81    dyoung 			break;
    344       1.25    itojun 	}
    345      1.103     ozaki 	pserialize_read_exit(s);
    346       1.25    itojun 
    347       1.81    dyoung 	if (sdl == NULL)
    348       1.81    dyoung 		return -1;
    349       1.25    itojun 
    350       1.48    itojun 	switch (ifp->if_type) {
    351       1.48    itojun 	case IFT_IEEE1394:
    352       1.48    itojun 	case IFT_IEEE80211:
    353       1.48    itojun 		/* IEEE1394 uses 16byte length address starting with EUI64 */
    354       1.48    itojun 		if (addrlen > 8)
    355       1.48    itojun 			addrlen = 8;
    356       1.48    itojun 		break;
    357       1.48    itojun 	default:
    358       1.48    itojun 		break;
    359       1.48    itojun 	}
    360       1.48    itojun 
    361       1.25    itojun 	/* get EUI64 */
    362       1.25    itojun 	switch (ifp->if_type) {
    363       1.48    itojun 	/* IEEE802/EUI64 cases - what others? */
    364       1.25    itojun 	case IFT_ETHER:
    365       1.25    itojun 	case IFT_FDDI:
    366       1.25    itojun 	case IFT_ATM:
    367       1.32      onoe 	case IFT_IEEE1394:
    368       1.48    itojun 	case IFT_IEEE80211:
    369       1.25    itojun 		/* look at IEEE802/EUI64 only */
    370       1.25    itojun 		if (addrlen != 8 && addrlen != 6)
    371       1.25    itojun 			return -1;
    372       1.13    itojun 
    373       1.25    itojun 		/*
    374       1.25    itojun 		 * check for invalid MAC address - on bsdi, we see it a lot
    375       1.25    itojun 		 * since wildboar configures all-zero MAC on pccard before
    376       1.25    itojun 		 * card insertion.
    377       1.25    itojun 		 */
    378       1.64    rpaulo 		if (memcmp(addr, allzero, addrlen) == 0)
    379       1.25    itojun 			return -1;
    380       1.64    rpaulo 		if (memcmp(addr, allone, addrlen) == 0)
    381       1.25    itojun 			return -1;
    382       1.25    itojun 
    383       1.25    itojun 		/* make EUI64 address */
    384       1.25    itojun 		if (addrlen == 8)
    385       1.64    rpaulo 			memcpy(&in6->s6_addr[8], addr, 8);
    386       1.25    itojun 		else if (addrlen == 6) {
    387       1.25    itojun 			in6->s6_addr[8] = addr[0];
    388       1.25    itojun 			in6->s6_addr[9] = addr[1];
    389       1.25    itojun 			in6->s6_addr[10] = addr[2];
    390       1.25    itojun 			in6->s6_addr[11] = 0xff;
    391       1.26    itojun 			in6->s6_addr[12] = 0xfe;
    392       1.25    itojun 			in6->s6_addr[13] = addr[3];
    393       1.25    itojun 			in6->s6_addr[14] = addr[4];
    394       1.25    itojun 			in6->s6_addr[15] = addr[5];
    395       1.25    itojun 		}
    396        1.7    itojun 		break;
    397       1.25    itojun 
    398       1.25    itojun 	case IFT_ARCNET:
    399       1.25    itojun 		if (addrlen != 1)
    400       1.25    itojun 			return -1;
    401       1.25    itojun 		if (!addr[0])
    402       1.25    itojun 			return -1;
    403       1.25    itojun 
    404       1.64    rpaulo 		memset(&in6->s6_addr[8], 0, 8);
    405       1.25    itojun 		in6->s6_addr[15] = addr[0];
    406       1.25    itojun 
    407       1.27    itojun 		/*
    408       1.27    itojun 		 * due to insufficient bitwidth, we mark it local.
    409       1.27    itojun 		 */
    410       1.25    itojun 		in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
    411       1.25    itojun 		in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
    412        1.7    itojun 		break;
    413       1.25    itojun 
    414       1.25    itojun 	case IFT_GIF:
    415       1.25    itojun #ifdef IFT_STF
    416       1.25    itojun 	case IFT_STF:
    417       1.25    itojun #endif
    418       1.25    itojun 		/*
    419       1.34    itojun 		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
    420       1.27    itojun 		 * however, IPv4 address is not very suitable as unique
    421       1.27    itojun 		 * identifier source (can be renumbered).
    422       1.27    itojun 		 * we don't do this.
    423       1.25    itojun 		 */
    424       1.25    itojun 		return -1;
    425       1.25    itojun 
    426        1.7    itojun 	default:
    427       1.25    itojun 		return -1;
    428       1.25    itojun 	}
    429       1.25    itojun 
    430       1.25    itojun 	/* sanity check: g bit must not indicate "group" */
    431       1.25    itojun 	if (EUI64_GROUP(in6))
    432       1.25    itojun 		return -1;
    433       1.25    itojun 
    434       1.25    itojun 	/* convert EUI64 into IPv6 interface identifier */
    435       1.25    itojun 	EUI64_TO_IFID(in6);
    436       1.25    itojun 
    437       1.25    itojun 	/*
    438       1.25    itojun 	 * sanity check: ifid must not be all zero, avoid conflict with
    439       1.25    itojun 	 * subnet router anycast
    440       1.25    itojun 	 */
    441       1.25    itojun 	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
    442       1.64    rpaulo 	    memcmp(&in6->s6_addr[9], allzero, 7) == 0) {
    443       1.25    itojun 		return -1;
    444        1.7    itojun 	}
    445        1.7    itojun 
    446        1.7    itojun 	return 0;
    447        1.2    itojun }
    448        1.2    itojun 
    449        1.2    itojun /*
    450       1.25    itojun  * Get interface identifier for the specified interface.  If it is not
    451       1.25    itojun  * available on ifp0, borrow interface identifier from other information
    452       1.25    itojun  * sources.
    453       1.71  christos  *
    454       1.71  christos  * altifp - secondary EUI64 source
    455       1.13    itojun  */
    456       1.13    itojun static int
    457       1.71  christos get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
    458       1.71  christos 	struct in6_addr *in6)
    459       1.13    itojun {
    460       1.25    itojun 	struct ifnet *ifp;
    461       1.98     ozaki 	int s;
    462       1.25    itojun 
    463       1.25    itojun 	/* first, try to get it from the interface itself */
    464       1.64    rpaulo 	if (in6_get_hw_ifid(ifp0, in6) == 0) {
    465       1.96     ozaki 		nd6log(LOG_DEBUG, "%s: got interface identifier from itself\n",
    466       1.96     ozaki 		    if_name(ifp0));
    467       1.25    itojun 		goto success;
    468       1.25    itojun 	}
    469       1.25    itojun 
    470       1.25    itojun 	/* try secondary EUI64 source. this basically is for ATM PVC */
    471       1.64    rpaulo 	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
    472       1.96     ozaki 		nd6log(LOG_DEBUG, "%s: got interface identifier from %s\n",
    473       1.96     ozaki 		    if_name(ifp0), if_name(altifp));
    474       1.25    itojun 		goto success;
    475       1.25    itojun 	}
    476       1.25    itojun 
    477       1.25    itojun 	/* next, try to get it from some other hardware interface */
    478       1.98     ozaki 	s = pserialize_read_enter();
    479       1.98     ozaki 	IFNET_READER_FOREACH(ifp) {
    480       1.25    itojun 		if (ifp == ifp0)
    481       1.25    itojun 			continue;
    482       1.64    rpaulo 		if (in6_get_hw_ifid(ifp, in6) != 0)
    483       1.25    itojun 			continue;
    484       1.27    itojun 
    485       1.25    itojun 		/*
    486       1.25    itojun 		 * to borrow ifid from other interface, ifid needs to be
    487       1.25    itojun 		 * globally unique
    488       1.25    itojun 		 */
    489       1.25    itojun 		if (IFID_UNIVERSAL(in6)) {
    490       1.96     ozaki 			nd6log(LOG_DEBUG,
    491       1.34    itojun 			    "%s: borrow interface identifier from %s\n",
    492       1.96     ozaki 			    if_name(ifp0), if_name(ifp));
    493      1.106     ozaki 			pserialize_read_exit(s);
    494       1.25    itojun 			goto success;
    495       1.25    itojun 		}
    496       1.25    itojun 	}
    497       1.98     ozaki 	pserialize_read_exit(s);
    498       1.13    itojun 
    499       1.64    rpaulo #if 0
    500       1.64    rpaulo 	/* get from hostid - only for certain architectures */
    501       1.64    rpaulo 	if (get_hostid_ifid(ifp, in6) == 0) {
    502       1.96     ozaki 		nd6log(LOG_DEBUG,
    503       1.64    rpaulo 		    "%s: interface identifier generated by hostid\n",
    504       1.96     ozaki 		    if_name(ifp0));
    505       1.64    rpaulo 		goto success;
    506       1.64    rpaulo 	}
    507       1.64    rpaulo #endif
    508       1.64    rpaulo 
    509       1.25    itojun 	/* last resort: get from random number source */
    510       1.97     ozaki 	if (get_rand_ifid(in6) == 0) {
    511       1.96     ozaki 		nd6log(LOG_DEBUG,
    512       1.34    itojun 		    "%s: interface identifier generated by random number\n",
    513       1.96     ozaki 		    if_name(ifp0));
    514       1.25    itojun 		goto success;
    515       1.25    itojun 	}
    516       1.13    itojun 
    517       1.31    itojun 	printf("%s: failed to get interface identifier\n", if_name(ifp0));
    518       1.25    itojun 	return -1;
    519       1.13    itojun 
    520       1.25    itojun success:
    521       1.96     ozaki 	nd6log(LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
    522       1.47    itojun 	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
    523       1.47    itojun 	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
    524       1.96     ozaki 	    in6->s6_addr[14], in6->s6_addr[15]);
    525       1.13    itojun 	return 0;
    526       1.13    itojun }
    527       1.13    itojun 
    528       1.71  christos /*
    529       1.71  christos  * altifp - secondary EUI64 source
    530       1.71  christos  */
    531       1.71  christos 
    532       1.25    itojun static int
    533       1.71  christos in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
    534       1.48    itojun {
    535       1.48    itojun 	struct in6_aliasreq ifra;
    536      1.105       roy 	int error;
    537       1.25    itojun 
    538       1.25    itojun 	/*
    539       1.48    itojun 	 * configure link-local address.
    540       1.25    itojun 	 */
    541       1.64    rpaulo 	memset(&ifra, 0, sizeof(ifra));
    542        1.2    itojun 
    543       1.25    itojun 	/*
    544       1.48    itojun 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
    545       1.48    itojun 	 * for safety.
    546       1.25    itojun 	 */
    547       1.48    itojun 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
    548        1.2    itojun 
    549       1.48    itojun 	ifra.ifra_addr.sin6_family = AF_INET6;
    550       1.48    itojun 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
    551       1.63    rpaulo 	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
    552       1.48    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
    553       1.48    itojun 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    554       1.48    itojun 		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
    555       1.48    itojun 		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
    556       1.48    itojun 	} else {
    557       1.48    itojun 		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
    558       1.96     ozaki 			nd6log(LOG_ERR,
    559       1.96     ozaki 			    "%s: no ifid available\n", if_name(ifp));
    560       1.69    dyoung 			return -1;
    561       1.25    itojun 		}
    562       1.25    itojun 	}
    563       1.63    rpaulo 	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
    564       1.69    dyoung 		return -1;
    565       1.25    itojun 
    566       1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask64, 0, 0, 0);
    567       1.48    itojun 	/* link-local addresses should NEVER expire. */
    568       1.48    itojun 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    569       1.48    itojun 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    570       1.25    itojun 
    571       1.48    itojun 	/*
    572       1.48    itojun 	 * Now call in6_update_ifa() to do a bunch of procedures to configure
    573       1.70    dyoung 	 * a link-local address. We can set the 3rd argument to NULL, because
    574       1.48    itojun 	 * we know there's no other link-local address on the interface
    575       1.48    itojun 	 * and therefore we are adding one (instead of updating one).
    576       1.48    itojun 	 */
    577      1.109  christos 	if ((error = in6_update_ifa(ifp, &ifra, IN6_IFAUPDATE_DADDELAY)) != 0) {
    578       1.25    itojun 		/*
    579       1.48    itojun 		 * XXX: When the interface does not support IPv6, this call
    580       1.82    dyoung 		 * would fail in the SIOCINITIFADDR ioctl.  I believe the
    581       1.48    itojun 		 * notification is rather confusing in this case, so just
    582       1.48    itojun 		 * suppress it.  (jinmei (at) kame.net 20010130)
    583       1.25    itojun 		 */
    584       1.48    itojun 		if (error != EAFNOSUPPORT)
    585       1.96     ozaki 			nd6log(LOG_NOTICE,
    586       1.96     ozaki 			    "failed to configure a link-local address on %s "
    587       1.48    itojun 			    "(errno=%d)\n",
    588       1.96     ozaki 			    if_name(ifp), error);
    589       1.69    dyoung 		return -1;
    590       1.25    itojun 	}
    591       1.25    itojun 
    592       1.25    itojun 	return 0;
    593       1.25    itojun }
    594       1.25    itojun 
    595       1.71  christos /*
    596       1.71  christos  * ifp - mut be IFT_LOOP
    597       1.71  christos  */
    598       1.71  christos 
    599       1.25    itojun static int
    600       1.71  christos in6_ifattach_loopback(struct ifnet *ifp)
    601       1.25    itojun {
    602       1.48    itojun 	struct in6_aliasreq ifra;
    603       1.48    itojun 	int error;
    604       1.48    itojun 
    605       1.64    rpaulo 	memset(&ifra, 0, sizeof(ifra));
    606       1.25    itojun 
    607       1.25    itojun 	/*
    608       1.48    itojun 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
    609       1.48    itojun 	 * for safety.
    610       1.25    itojun 	 */
    611       1.48    itojun 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
    612       1.48    itojun 
    613       1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask128, 0, 0, 0);
    614       1.25    itojun 
    615       1.25    itojun 	/*
    616       1.25    itojun 	 * Always initialize ia_dstaddr (= broadcast address) to loopback
    617       1.48    itojun 	 * address.  Follows IPv4 practice - see in_ifinit().
    618       1.48    itojun 	 */
    619       1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_dstaddr, &in6addr_loopback, 0, 0, 0);
    620       1.48    itojun 
    621       1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_addr, &in6addr_loopback, 0, 0, 0);
    622       1.48    itojun 
    623       1.48    itojun 	/* the loopback  address should NEVER expire. */
    624       1.48    itojun 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    625       1.48    itojun 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    626       1.48    itojun 
    627       1.48    itojun 	/* we don't need to perform DAD on loopback interfaces. */
    628       1.48    itojun 	ifra.ifra_flags |= IN6_IFF_NODAD;
    629       1.48    itojun 
    630       1.48    itojun 	/*
    631       1.48    itojun 	 * We are sure that this is a newly assigned address, so we can set
    632       1.48    itojun 	 * NULL to the 3rd arg.
    633       1.48    itojun 	 */
    634      1.109  christos 	if ((error = in6_update_ifa(ifp, &ifra, 0)) != 0) {
    635       1.96     ozaki 		nd6log(LOG_ERR, "failed to configure "
    636       1.48    itojun 		    "the loopback address on %s (errno=%d)\n",
    637       1.96     ozaki 		    if_name(ifp), error);
    638       1.69    dyoung 		return -1;
    639       1.48    itojun 	}
    640       1.25    itojun 
    641       1.48    itojun 	return 0;
    642       1.48    itojun }
    643       1.48    itojun 
    644       1.48    itojun /*
    645       1.48    itojun  * compute NI group address, based on the current hostname setting.
    646       1.48    itojun  * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
    647       1.48    itojun  *
    648       1.48    itojun  * when ifp == NULL, the caller is responsible for filling scopeid.
    649       1.48    itojun  */
    650       1.48    itojun int
    651       1.71  christos in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
    652       1.71  christos 	struct sockaddr_in6 *sa6)
    653       1.48    itojun {
    654       1.48    itojun 	const char *p;
    655       1.52    itojun 	u_int8_t *q;
    656       1.48    itojun 	MD5_CTX ctxt;
    657       1.48    itojun 	u_int8_t digest[16];
    658       1.50    itojun 	u_int8_t l;
    659       1.50    itojun 	u_int8_t n[64];	/* a single label must not exceed 63 chars */
    660       1.25    itojun 
    661       1.48    itojun 	if (!namelen || !name)
    662       1.25    itojun 		return -1;
    663       1.48    itojun 
    664       1.48    itojun 	p = name;
    665       1.48    itojun 	while (p && *p && *p != '.' && p - name < namelen)
    666       1.48    itojun 		p++;
    667       1.48    itojun 	if (p - name > sizeof(n) - 1)
    668       1.48    itojun 		return -1;	/* label too long */
    669       1.48    itojun 	l = p - name;
    670       1.50    itojun 	strncpy((char *)n, name, l);
    671       1.48    itojun 	n[(int)l] = '\0';
    672       1.48    itojun 	for (q = n; *q; q++) {
    673       1.48    itojun 		if ('A' <= *q && *q <= 'Z')
    674       1.48    itojun 			*q = *q - 'A' + 'a';
    675        1.2    itojun 	}
    676       1.25    itojun 
    677       1.48    itojun 	/* generate 8 bytes of pseudo-random value. */
    678       1.64    rpaulo 	memset(&ctxt, 0, sizeof(ctxt));
    679       1.48    itojun 	MD5Init(&ctxt);
    680       1.48    itojun 	MD5Update(&ctxt, &l, sizeof(l));
    681       1.48    itojun 	MD5Update(&ctxt, n, l);
    682       1.48    itojun 	MD5Final(digest, &ctxt);
    683       1.48    itojun 
    684       1.64    rpaulo 	memset(sa6, 0, sizeof(*sa6));
    685       1.48    itojun 	sa6->sin6_family = AF_INET6;
    686       1.48    itojun 	sa6->sin6_len = sizeof(*sa6);
    687       1.48    itojun 	sa6->sin6_addr.s6_addr16[0] = htons(0xff02);
    688       1.48    itojun 	sa6->sin6_addr.s6_addr8[11] = 2;
    689       1.64    rpaulo 	memcpy(&sa6->sin6_addr.s6_addr32[3], digest,
    690       1.48    itojun 	    sizeof(sa6->sin6_addr.s6_addr32[3]));
    691       1.63    rpaulo 	if (in6_setscope(&sa6->sin6_addr, ifp, NULL))
    692       1.69    dyoung 		return -1; /* XXX: should not fail */
    693       1.48    itojun 
    694       1.25    itojun 	return 0;
    695        1.2    itojun }
    696        1.2    itojun 
    697       1.17    itojun /*
    698       1.17    itojun  * XXX multiple loopback interface needs more care.  for instance,
    699       1.17    itojun  * nodelocal address needs to be configured onto only one of them.
    700       1.25    itojun  * XXX multiple link-local address case
    701       1.71  christos  *
    702       1.71  christos  * altifp - secondary EUI64 source
    703       1.17    itojun  */
    704        1.2    itojun void
    705       1.71  christos in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
    706        1.2    itojun {
    707       1.85  christos 	struct in6_ifaddr *ia;
    708       1.85  christos 	struct in6_addr in6;
    709       1.13    itojun 
    710      1.114     ozaki 	KASSERT(IFNET_LOCKED(ifp));
    711      1.114     ozaki 
    712       1.38    itojun 	/* some of the interfaces are inherently not IPv6 capable */
    713       1.38    itojun 	switch (ifp->if_type) {
    714       1.42    itojun 	case IFT_BRIDGE:
    715      1.111  knakahar 	case IFT_L2TP:
    716       1.59  christos #ifdef IFT_PFLOG
    717       1.58    itojun 	case IFT_PFLOG:
    718       1.59  christos #endif
    719       1.59  christos #ifdef IFT_PFSYNC
    720       1.58    itojun 	case IFT_PFSYNC:
    721       1.59  christos #endif
    722       1.91       roy 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
    723       1.91       roy 		ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
    724       1.42    itojun 		return;
    725       1.38    itojun 	}
    726       1.38    itojun 
    727       1.46    itojun 	/*
    728       1.46    itojun 	 * if link mtu is too small, don't try to configure IPv6.
    729       1.46    itojun 	 * remember there could be some link-layer that has special
    730       1.46    itojun 	 * fragmentation logic.
    731       1.46    itojun 	 */
    732       1.49    itojun 	if (ifp->if_mtu < IPV6_MMTU) {
    733       1.96     ozaki 		nd6log(LOG_INFO, "%s has too small MTU, IPv6 not enabled\n",
    734       1.96     ozaki 		    if_name(ifp));
    735       1.46    itojun 		return;
    736       1.49    itojun 	}
    737       1.46    itojun 
    738        1.2    itojun 	/*
    739       1.25    itojun 	 * quirks based on interface type
    740        1.2    itojun 	 */
    741       1.25    itojun 	switch (ifp->if_type) {
    742       1.25    itojun #ifdef IFT_STF
    743       1.25    itojun 	case IFT_STF:
    744       1.25    itojun 		/*
    745       1.38    itojun 		 * 6to4 interface is a very special kind of beast.
    746       1.38    itojun 		 * no multicast, no linklocal.  RFC2529 specifies how to make
    747       1.38    itojun 		 * linklocals for 6to4 interface, but there's no use and
    748       1.38    itojun 		 * it is rather harmful to have one.
    749       1.25    itojun 		 */
    750       1.91       roy 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
    751       1.46    itojun 		return;
    752       1.25    itojun #endif
    753       1.65  liamjfoy 	case IFT_CARP:
    754       1.65  liamjfoy 		return;
    755       1.25    itojun 	default:
    756       1.25    itojun 		break;
    757        1.7    itojun 	}
    758        1.2    itojun 
    759        1.2    itojun 	/*
    760       1.25    itojun 	 * usually, we require multicast capability to the interface
    761        1.2    itojun 	 */
    762       1.25    itojun 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
    763       1.96     ozaki 		nd6log(LOG_INFO,
    764       1.38    itojun 		    "%s is not multicast capable, IPv6 not enabled\n",
    765       1.96     ozaki 		    if_name(ifp));
    766       1.25    itojun 		return;
    767       1.25    itojun 	}
    768       1.15   thorpej 
    769        1.2    itojun 	/*
    770       1.48    itojun 	 * assign loopback address for loopback interface.
    771       1.48    itojun 	 * XXX multiple loopback interface case.
    772        1.2    itojun 	 */
    773       1.48    itojun 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    774       1.48    itojun 		in6 = in6addr_loopback;
    775      1.114     ozaki 		/* These are safe and atomic thanks to IFNET_LOCK */
    776       1.25    itojun 		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
    777      1.114     ozaki 			if (in6_ifattach_loopback(ifp) != 0)
    778       1.25    itojun 				return;
    779       1.25    itojun 		}
    780       1.25    itojun 	}
    781        1.2    itojun 
    782        1.2    itojun 	/*
    783       1.48    itojun 	 * assign a link-local address, if there's none.
    784        1.2    itojun 	 */
    785       1.91       roy 	if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
    786      1.103     ozaki 	    ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
    787      1.113     ozaki 		int bound = curlwp_bind();
    788      1.113     ozaki 		struct psref psref;
    789      1.113     ozaki 		ia = in6ifa_ifpforlinklocal_psref(ifp, 0, &psref);
    790       1.70    dyoung 		if (ia == NULL && in6_ifattach_linklocal(ifp, altifp) != 0) {
    791       1.70    dyoung 			printf("%s: cannot assign link-local address\n",
    792       1.70    dyoung 			    ifp->if_xname);
    793        1.2    itojun 		}
    794      1.113     ozaki 		ia6_release(ia, &psref);
    795      1.113     ozaki 		curlwp_bindx(bound);
    796        1.2    itojun 	}
    797        1.2    itojun }
    798        1.2    itojun 
    799       1.17    itojun /*
    800       1.17    itojun  * NOTE: in6_ifdetach() does not support loopback if at this moment.
    801       1.41    itojun  * We don't need this function in bsdi, because interfaces are never removed
    802       1.41    itojun  * from the ifnet list in bsdi.
    803       1.17    itojun  */
    804        1.2    itojun void
    805       1.71  christos in6_ifdetach(struct ifnet *ifp)
    806        1.2    itojun {
    807       1.55    itojun 
    808      1.107     ozaki 	/* nuke any of IPv6 addresses we have */
    809      1.107     ozaki 	if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr);
    810      1.107     ozaki 
    811      1.112     ozaki 	in6_purge_multi(ifp);
    812      1.112     ozaki 
    813       1.55    itojun 	/* remove ip6_mrouter stuff */
    814       1.55    itojun 	ip6_mrouter_detach(ifp);
    815       1.18    itojun 
    816      1.108     ozaki 	/* remove neighbor management table */
    817       1.95    martin 	nd6_purge(ifp, NULL);
    818      1.108     ozaki 
    819      1.108     ozaki 	nd6_assert_purged(ifp);
    820        1.2    itojun }
    821       1.64    rpaulo 
    822       1.64    rpaulo int
    823       1.71  christos in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
    824       1.71  christos 	const u_int8_t *baseid, int generate)
    825       1.64    rpaulo {
    826       1.64    rpaulo 	u_int8_t nullbuf[8];
    827       1.64    rpaulo 	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
    828       1.64    rpaulo 
    829       1.64    rpaulo 	memset(nullbuf, 0, sizeof(nullbuf));
    830       1.64    rpaulo 	if (memcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
    831       1.64    rpaulo 		/* we've never created a random ID.  Create a new one. */
    832       1.64    rpaulo 		generate = 1;
    833       1.64    rpaulo 	}
    834       1.64    rpaulo 
    835       1.64    rpaulo 	if (generate) {
    836       1.64    rpaulo 		memcpy(ndi->randomseed1, baseid, sizeof(ndi->randomseed1));
    837       1.64    rpaulo 
    838       1.64    rpaulo 		/* generate_tmp_ifid will update seedn and buf */
    839       1.64    rpaulo 		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
    840       1.64    rpaulo 		    ndi->randomid);
    841       1.64    rpaulo 	}
    842       1.64    rpaulo 	memcpy(retbuf, ndi->randomid, 8);
    843       1.64    rpaulo 	if (generate && memcmp(retbuf, nullbuf, sizeof(nullbuf)) == 0) {
    844       1.64    rpaulo 		/* generate_tmp_ifid could not found a good ID. */
    845       1.69    dyoung 		return -1;
    846       1.64    rpaulo 	}
    847       1.64    rpaulo 
    848       1.69    dyoung 	return 0;
    849       1.64    rpaulo }
    850       1.64    rpaulo 
    851       1.64    rpaulo void
    852       1.67  christos in6_tmpaddrtimer(void *ignored_arg)
    853       1.64    rpaulo {
    854       1.64    rpaulo 	struct nd_ifinfo *ndi;
    855       1.64    rpaulo 	u_int8_t nullbuf[8];
    856       1.64    rpaulo 	struct ifnet *ifp;
    857       1.98     ozaki 	int s;
    858       1.80        ad 
    859      1.110     ozaki 	/* XXX NOMPSAFE still need softnet_lock */
    860       1.80        ad 	mutex_enter(softnet_lock);
    861       1.80        ad 	KERNEL_LOCK(1, NULL);
    862       1.64    rpaulo 
    863       1.64    rpaulo 	callout_reset(&in6_tmpaddrtimer_ch,
    864       1.64    rpaulo 	    (ip6_temp_preferred_lifetime - ip6_desync_factor -
    865       1.64    rpaulo 	    ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL);
    866       1.64    rpaulo 
    867       1.64    rpaulo 	memset(nullbuf, 0, sizeof(nullbuf));
    868       1.98     ozaki 	s = pserialize_read_enter();
    869       1.98     ozaki 	IFNET_READER_FOREACH(ifp) {
    870       1.64    rpaulo 		ndi = ND_IFINFO(ifp);
    871       1.64    rpaulo 		if (memcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
    872       1.64    rpaulo 			/*
    873       1.64    rpaulo 			 * We've been generating a random ID on this interface.
    874       1.64    rpaulo 			 * Create a new one.
    875       1.64    rpaulo 			 */
    876       1.64    rpaulo 			(void)generate_tmp_ifid(ndi->randomseed0,
    877       1.64    rpaulo 			    ndi->randomseed1, ndi->randomid);
    878       1.64    rpaulo 		}
    879       1.64    rpaulo 	}
    880       1.98     ozaki 	pserialize_read_exit(s);
    881       1.64    rpaulo 
    882       1.80        ad 	KERNEL_UNLOCK_ONE(NULL);
    883       1.80        ad 	mutex_exit(softnet_lock);
    884       1.64    rpaulo }
    885