Home | History | Annotate | Line # | Download | only in netinet6
in6_ifattach.c revision 1.115
      1 /*	$NetBSD: in6_ifattach.c,v 1.115 2018/05/01 07:21:39 maxv 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 2018/05/01 07:21:39 maxv 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_FDDI:
    366 	case IFT_ATM:
    367 	case IFT_IEEE1394:
    368 	case IFT_IEEE80211:
    369 		/* look at IEEE802/EUI64 only */
    370 		if (addrlen != 8 && addrlen != 6)
    371 			return -1;
    372 
    373 		/*
    374 		 * check for invalid MAC address - on bsdi, we see it a lot
    375 		 * since wildboar configures all-zero MAC on pccard before
    376 		 * card insertion.
    377 		 */
    378 		if (memcmp(addr, allzero, addrlen) == 0)
    379 			return -1;
    380 		if (memcmp(addr, allone, addrlen) == 0)
    381 			return -1;
    382 
    383 		/* make EUI64 address */
    384 		if (addrlen == 8)
    385 			memcpy(&in6->s6_addr[8], addr, 8);
    386 		else if (addrlen == 6) {
    387 			in6->s6_addr[8] = addr[0];
    388 			in6->s6_addr[9] = addr[1];
    389 			in6->s6_addr[10] = addr[2];
    390 			in6->s6_addr[11] = 0xff;
    391 			in6->s6_addr[12] = 0xfe;
    392 			in6->s6_addr[13] = addr[3];
    393 			in6->s6_addr[14] = addr[4];
    394 			in6->s6_addr[15] = addr[5];
    395 		}
    396 		break;
    397 
    398 	case IFT_ARCNET:
    399 		if (addrlen != 1)
    400 			return -1;
    401 		if (!addr[0])
    402 			return -1;
    403 
    404 		memset(&in6->s6_addr[8], 0, 8);
    405 		in6->s6_addr[15] = addr[0];
    406 
    407 		/*
    408 		 * due to insufficient bitwidth, we mark it local.
    409 		 */
    410 		in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
    411 		in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
    412 		break;
    413 
    414 	case IFT_GIF:
    415 #ifdef IFT_STF
    416 	case IFT_STF:
    417 #endif
    418 		/*
    419 		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
    420 		 * however, IPv4 address is not very suitable as unique
    421 		 * identifier source (can be renumbered).
    422 		 * we don't do this.
    423 		 */
    424 		return -1;
    425 
    426 	default:
    427 		return -1;
    428 	}
    429 
    430 	/* sanity check: g bit must not indicate "group" */
    431 	if (EUI64_GROUP(in6))
    432 		return -1;
    433 
    434 	/* convert EUI64 into IPv6 interface identifier */
    435 	EUI64_TO_IFID(in6);
    436 
    437 	/*
    438 	 * sanity check: ifid must not be all zero, avoid conflict with
    439 	 * subnet router anycast
    440 	 */
    441 	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
    442 	    memcmp(&in6->s6_addr[9], allzero, 7) == 0) {
    443 		return -1;
    444 	}
    445 
    446 	return 0;
    447 }
    448 
    449 /*
    450  * Get interface identifier for the specified interface.  If it is not
    451  * available on ifp0, borrow interface identifier from other information
    452  * sources.
    453  *
    454  * altifp - secondary EUI64 source
    455  */
    456 static int
    457 get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
    458 	struct in6_addr *in6)
    459 {
    460 	struct ifnet *ifp;
    461 	int s;
    462 
    463 	/* first, try to get it from the interface itself */
    464 	if (in6_get_hw_ifid(ifp0, in6) == 0) {
    465 		nd6log(LOG_DEBUG, "%s: got interface identifier from itself\n",
    466 		    if_name(ifp0));
    467 		goto success;
    468 	}
    469 
    470 	/* try secondary EUI64 source. this basically is for ATM PVC */
    471 	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
    472 		nd6log(LOG_DEBUG, "%s: got interface identifier from %s\n",
    473 		    if_name(ifp0), if_name(altifp));
    474 		goto success;
    475 	}
    476 
    477 	/* next, try to get it from some other hardware interface */
    478 	s = pserialize_read_enter();
    479 	IFNET_READER_FOREACH(ifp) {
    480 		if (ifp == ifp0)
    481 			continue;
    482 		if (in6_get_hw_ifid(ifp, in6) != 0)
    483 			continue;
    484 
    485 		/*
    486 		 * to borrow ifid from other interface, ifid needs to be
    487 		 * globally unique
    488 		 */
    489 		if (IFID_UNIVERSAL(in6)) {
    490 			nd6log(LOG_DEBUG,
    491 			    "%s: borrow interface identifier from %s\n",
    492 			    if_name(ifp0), if_name(ifp));
    493 			pserialize_read_exit(s);
    494 			goto success;
    495 		}
    496 	}
    497 	pserialize_read_exit(s);
    498 
    499 #if 0
    500 	/* get from hostid - only for certain architectures */
    501 	if (get_hostid_ifid(ifp, in6) == 0) {
    502 		nd6log(LOG_DEBUG,
    503 		    "%s: interface identifier generated by hostid\n",
    504 		    if_name(ifp0));
    505 		goto success;
    506 	}
    507 #endif
    508 
    509 	/* last resort: get from random number source */
    510 	if (get_rand_ifid(in6) == 0) {
    511 		nd6log(LOG_DEBUG,
    512 		    "%s: interface identifier generated by random number\n",
    513 		    if_name(ifp0));
    514 		goto success;
    515 	}
    516 
    517 	printf("%s: failed to get interface identifier\n", if_name(ifp0));
    518 	return -1;
    519 
    520 success:
    521 	nd6log(LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
    522 	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
    523 	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
    524 	    in6->s6_addr[14], in6->s6_addr[15]);
    525 	return 0;
    526 }
    527 
    528 /*
    529  * altifp - secondary EUI64 source
    530  */
    531 
    532 static int
    533 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
    534 {
    535 	struct in6_aliasreq ifra;
    536 	int error;
    537 
    538 	/*
    539 	 * configure link-local address.
    540 	 */
    541 	memset(&ifra, 0, sizeof(ifra));
    542 
    543 	/*
    544 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
    545 	 * for safety.
    546 	 */
    547 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
    548 
    549 	ifra.ifra_addr.sin6_family = AF_INET6;
    550 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
    551 	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
    552 	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
    553 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    554 		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
    555 		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
    556 	} else {
    557 		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
    558 			nd6log(LOG_ERR,
    559 			    "%s: no ifid available\n", if_name(ifp));
    560 			return -1;
    561 		}
    562 	}
    563 	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
    564 		return -1;
    565 
    566 	sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask64, 0, 0, 0);
    567 	/* link-local addresses should NEVER expire. */
    568 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    569 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    570 
    571 	/*
    572 	 * Now call in6_update_ifa() to do a bunch of procedures to configure
    573 	 * a link-local address. We can set the 3rd argument to NULL, because
    574 	 * we know there's no other link-local address on the interface
    575 	 * and therefore we are adding one (instead of updating one).
    576 	 */
    577 	if ((error = in6_update_ifa(ifp, &ifra, IN6_IFAUPDATE_DADDELAY)) != 0) {
    578 		/*
    579 		 * XXX: When the interface does not support IPv6, this call
    580 		 * would fail in the SIOCINITIFADDR ioctl.  I believe the
    581 		 * notification is rather confusing in this case, so just
    582 		 * suppress it.  (jinmei (at) kame.net 20010130)
    583 		 */
    584 		if (error != EAFNOSUPPORT)
    585 			nd6log(LOG_NOTICE,
    586 			    "failed to configure a link-local address on %s "
    587 			    "(errno=%d)\n",
    588 			    if_name(ifp), error);
    589 		return -1;
    590 	}
    591 
    592 	return 0;
    593 }
    594 
    595 /*
    596  * ifp - mut be IFT_LOOP
    597  */
    598 
    599 static int
    600 in6_ifattach_loopback(struct ifnet *ifp)
    601 {
    602 	struct in6_aliasreq ifra;
    603 	int error;
    604 
    605 	memset(&ifra, 0, sizeof(ifra));
    606 
    607 	/*
    608 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
    609 	 * for safety.
    610 	 */
    611 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
    612 
    613 	sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask128, 0, 0, 0);
    614 
    615 	/*
    616 	 * Always initialize ia_dstaddr (= broadcast address) to loopback
    617 	 * address.  Follows IPv4 practice - see in_ifinit().
    618 	 */
    619 	sockaddr_in6_init(&ifra.ifra_dstaddr, &in6addr_loopback, 0, 0, 0);
    620 
    621 	sockaddr_in6_init(&ifra.ifra_addr, &in6addr_loopback, 0, 0, 0);
    622 
    623 	/* the loopback  address should NEVER expire. */
    624 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    625 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    626 
    627 	/* we don't need to perform DAD on loopback interfaces. */
    628 	ifra.ifra_flags |= IN6_IFF_NODAD;
    629 
    630 	/*
    631 	 * We are sure that this is a newly assigned address, so we can set
    632 	 * NULL to the 3rd arg.
    633 	 */
    634 	if ((error = in6_update_ifa(ifp, &ifra, 0)) != 0) {
    635 		nd6log(LOG_ERR, "failed to configure "
    636 		    "the loopback address on %s (errno=%d)\n",
    637 		    if_name(ifp), error);
    638 		return -1;
    639 	}
    640 
    641 	return 0;
    642 }
    643 
    644 /*
    645  * compute NI group address, based on the current hostname setting.
    646  * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
    647  *
    648  * when ifp == NULL, the caller is responsible for filling scopeid.
    649  */
    650 int
    651 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
    652 	struct sockaddr_in6 *sa6)
    653 {
    654 	const char *p;
    655 	u_int8_t *q;
    656 	MD5_CTX ctxt;
    657 	u_int8_t digest[16];
    658 	u_int8_t l;
    659 	u_int8_t n[64];	/* a single label must not exceed 63 chars */
    660 
    661 	if (!namelen || !name)
    662 		return -1;
    663 
    664 	p = name;
    665 	while (p && *p && *p != '.' && p - name < namelen)
    666 		p++;
    667 	if (p - name > sizeof(n) - 1)
    668 		return -1;	/* label too long */
    669 	l = p - name;
    670 	strncpy((char *)n, name, l);
    671 	n[(int)l] = '\0';
    672 	for (q = n; *q; q++) {
    673 		if ('A' <= *q && *q <= 'Z')
    674 			*q = *q - 'A' + 'a';
    675 	}
    676 
    677 	/* generate 8 bytes of pseudo-random value. */
    678 	memset(&ctxt, 0, sizeof(ctxt));
    679 	MD5Init(&ctxt);
    680 	MD5Update(&ctxt, &l, sizeof(l));
    681 	MD5Update(&ctxt, n, l);
    682 	MD5Final(digest, &ctxt);
    683 
    684 	memset(sa6, 0, sizeof(*sa6));
    685 	sa6->sin6_family = AF_INET6;
    686 	sa6->sin6_len = sizeof(*sa6);
    687 	sa6->sin6_addr.s6_addr16[0] = htons(0xff02);
    688 	sa6->sin6_addr.s6_addr8[11] = 2;
    689 	memcpy(&sa6->sin6_addr.s6_addr32[3], digest,
    690 	    sizeof(sa6->sin6_addr.s6_addr32[3]));
    691 	if (in6_setscope(&sa6->sin6_addr, ifp, NULL))
    692 		return -1; /* XXX: should not fail */
    693 
    694 	return 0;
    695 }
    696 
    697 /*
    698  * XXX multiple loopback interface needs more care.  for instance,
    699  * nodelocal address needs to be configured onto only one of them.
    700  * XXX multiple link-local address case
    701  *
    702  * altifp - secondary EUI64 source
    703  */
    704 void
    705 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
    706 {
    707 	struct in6_ifaddr *ia;
    708 	struct in6_addr in6;
    709 
    710 	KASSERT(IFNET_LOCKED(ifp));
    711 
    712 	/* some of the interfaces are inherently not IPv6 capable */
    713 	switch (ifp->if_type) {
    714 	case IFT_BRIDGE:
    715 	case IFT_L2TP:
    716 #ifdef IFT_PFLOG
    717 	case IFT_PFLOG:
    718 #endif
    719 #ifdef IFT_PFSYNC
    720 	case IFT_PFSYNC:
    721 #endif
    722 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
    723 		ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
    724 		return;
    725 	}
    726 
    727 	/*
    728 	 * if link mtu is too small, don't try to configure IPv6.
    729 	 * remember there could be some link-layer that has special
    730 	 * fragmentation logic.
    731 	 */
    732 	if (ifp->if_mtu < IPV6_MMTU) {
    733 		nd6log(LOG_INFO, "%s has too small MTU, IPv6 not enabled\n",
    734 		    if_name(ifp));
    735 		return;
    736 	}
    737 
    738 	/*
    739 	 * quirks based on interface type
    740 	 */
    741 	switch (ifp->if_type) {
    742 #ifdef IFT_STF
    743 	case IFT_STF:
    744 		/*
    745 		 * 6to4 interface is a very special kind of beast.
    746 		 * no multicast, no linklocal.  RFC2529 specifies how to make
    747 		 * linklocals for 6to4 interface, but there's no use and
    748 		 * it is rather harmful to have one.
    749 		 */
    750 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
    751 		return;
    752 #endif
    753 	case IFT_CARP:
    754 		return;
    755 	default:
    756 		break;
    757 	}
    758 
    759 	/*
    760 	 * usually, we require multicast capability to the interface
    761 	 */
    762 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
    763 		nd6log(LOG_INFO,
    764 		    "%s is not multicast capable, IPv6 not enabled\n",
    765 		    if_name(ifp));
    766 		return;
    767 	}
    768 
    769 	/*
    770 	 * assign loopback address for loopback interface.
    771 	 * XXX multiple loopback interface case.
    772 	 */
    773 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    774 		in6 = in6addr_loopback;
    775 		/* These are safe and atomic thanks to IFNET_LOCK */
    776 		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
    777 			if (in6_ifattach_loopback(ifp) != 0)
    778 				return;
    779 		}
    780 	}
    781 
    782 	/*
    783 	 * assign a link-local address, if there's none.
    784 	 */
    785 	if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
    786 	    ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
    787 		int bound = curlwp_bind();
    788 		struct psref psref;
    789 		ia = in6ifa_ifpforlinklocal_psref(ifp, 0, &psref);
    790 		if (ia == NULL && in6_ifattach_linklocal(ifp, altifp) != 0) {
    791 			printf("%s: cannot assign link-local address\n",
    792 			    ifp->if_xname);
    793 		}
    794 		ia6_release(ia, &psref);
    795 		curlwp_bindx(bound);
    796 	}
    797 }
    798 
    799 /*
    800  * NOTE: in6_ifdetach() does not support loopback if at this moment.
    801  * We don't need this function in bsdi, because interfaces are never removed
    802  * from the ifnet list in bsdi.
    803  */
    804 void
    805 in6_ifdetach(struct ifnet *ifp)
    806 {
    807 
    808 	/* nuke any of IPv6 addresses we have */
    809 	if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr);
    810 
    811 	in6_purge_multi(ifp);
    812 
    813 	/* remove ip6_mrouter stuff */
    814 	ip6_mrouter_detach(ifp);
    815 
    816 	/* remove neighbor management table */
    817 	nd6_purge(ifp, NULL);
    818 
    819 	nd6_assert_purged(ifp);
    820 }
    821 
    822 int
    823 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
    824 	const u_int8_t *baseid, int generate)
    825 {
    826 	u_int8_t nullbuf[8];
    827 	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
    828 
    829 	memset(nullbuf, 0, sizeof(nullbuf));
    830 	if (memcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
    831 		/* we've never created a random ID.  Create a new one. */
    832 		generate = 1;
    833 	}
    834 
    835 	if (generate) {
    836 		memcpy(ndi->randomseed1, baseid, sizeof(ndi->randomseed1));
    837 
    838 		/* generate_tmp_ifid will update seedn and buf */
    839 		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
    840 		    ndi->randomid);
    841 	}
    842 	memcpy(retbuf, ndi->randomid, 8);
    843 	if (generate && memcmp(retbuf, nullbuf, sizeof(nullbuf)) == 0) {
    844 		/* generate_tmp_ifid could not found a good ID. */
    845 		return -1;
    846 	}
    847 
    848 	return 0;
    849 }
    850 
    851 void
    852 in6_tmpaddrtimer(void *ignored_arg)
    853 {
    854 	struct nd_ifinfo *ndi;
    855 	u_int8_t nullbuf[8];
    856 	struct ifnet *ifp;
    857 	int s;
    858 
    859 	/* XXX NOMPSAFE still need softnet_lock */
    860 	mutex_enter(softnet_lock);
    861 	KERNEL_LOCK(1, NULL);
    862 
    863 	callout_reset(&in6_tmpaddrtimer_ch,
    864 	    (ip6_temp_preferred_lifetime - ip6_desync_factor -
    865 	    ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL);
    866 
    867 	memset(nullbuf, 0, sizeof(nullbuf));
    868 	s = pserialize_read_enter();
    869 	IFNET_READER_FOREACH(ifp) {
    870 		ndi = ND_IFINFO(ifp);
    871 		if (memcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
    872 			/*
    873 			 * We've been generating a random ID on this interface.
    874 			 * Create a new one.
    875 			 */
    876 			(void)generate_tmp_ifid(ndi->randomseed0,
    877 			    ndi->randomseed1, ndi->randomid);
    878 		}
    879 	}
    880 	pserialize_read_exit(s);
    881 
    882 	KERNEL_UNLOCK_ONE(NULL);
    883 	mutex_exit(softnet_lock);
    884 }
    885