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