Home | History | Annotate | Line # | Download | only in netinet6
in6_ifattach.c revision 1.119.6.1
      1  1.119.6.1   thorpej /*	$NetBSD: in6_ifattach.c,v 1.119.6.1 2021/06/17 04:46:35 thorpej 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.119.6.1   thorpej __KERNEL_RCSID(0, "$NetBSD: in6_ifattach.c,v 1.119.6.1 2021/06/17 04:46:35 thorpej 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.48    itojun int ip6_auto_linklocal = 1;	/* enable by default */
     63       1.48    itojun 
     64       1.64    rpaulo #if 0
     65       1.74    dyoung static int get_hostid_ifid(struct ifnet *, struct in6_addr *);
     66       1.64    rpaulo #endif
     67       1.74    dyoung static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
     68       1.74    dyoung static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
     69       1.74    dyoung static int in6_ifattach_loopback(struct ifnet *);
     70       1.25    itojun 
     71       1.25    itojun #define EUI64_GBIT	0x01
     72       1.25    itojun #define EUI64_UBIT	0x02
     73       1.54     perry #define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (/*CONSTCOND*/ 0)
     74       1.25    itojun #define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
     75       1.25    itojun #define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
     76       1.25    itojun #define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
     77       1.25    itojun #define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
     78       1.25    itojun 
     79       1.25    itojun #define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
     80       1.25    itojun #define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
     81       1.25    itojun 
     82       1.64    rpaulo #if 0
     83       1.64    rpaulo /*
     84       1.64    rpaulo  * Generate a last-resort interface identifier from hostid.
     85       1.64    rpaulo  * works only for certain architectures (like sparc).
     86       1.64    rpaulo  * also, using hostid itself may constitute a privacy threat, much worse
     87       1.64    rpaulo  * than MAC addresses (hostids are used for software licensing).
     88       1.64    rpaulo  * maybe we should use MD5(hostid) instead.
     89       1.71  christos  *
     90       1.71  christos  * in6 - upper 64bits are preserved
     91       1.64    rpaulo  */
     92       1.64    rpaulo static int
     93       1.71  christos get_hostid_ifid(struct ifnet *ifp, struct in6_addr *in6)
     94       1.64    rpaulo {
     95       1.64    rpaulo 	int off, len;
     96       1.64    rpaulo 	static const uint8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
     97       1.64    rpaulo 	static const uint8_t allone[8] =
     98       1.64    rpaulo 	    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
     99       1.64    rpaulo 
    100       1.64    rpaulo 	if (!hostid)
    101       1.64    rpaulo 		return -1;
    102       1.64    rpaulo 
    103       1.64    rpaulo 	/* get up to 8 bytes from the hostid field - should we get */
    104       1.64    rpaulo 	len = (sizeof(hostid) > 8) ? 8 : sizeof(hostid);
    105       1.64    rpaulo 	off = sizeof(*in6) - len;
    106       1.64    rpaulo 	memcpy(&in6->s6_addr[off], &hostid, len);
    107       1.64    rpaulo 
    108       1.64    rpaulo 	/* make sure we do not return anything bogus */
    109       1.64    rpaulo 	if (memcmp(&in6->s6_addr[8], allzero, sizeof(allzero)))
    110       1.64    rpaulo 		return -1;
    111       1.64    rpaulo 	if (memcmp(&in6->s6_addr[8], allone, sizeof(allone)))
    112       1.64    rpaulo 		return -1;
    113       1.64    rpaulo 
    114       1.64    rpaulo 	/* make sure to set "u" bit to local, and "g" bit to individual. */
    115       1.64    rpaulo 	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
    116       1.64    rpaulo 	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
    117       1.64    rpaulo 
    118       1.64    rpaulo 	/* convert EUI64 into IPv6 interface identifier */
    119       1.64    rpaulo 	EUI64_TO_IFID(in6);
    120       1.64    rpaulo 
    121       1.64    rpaulo 	return 0;
    122       1.64    rpaulo }
    123       1.64    rpaulo #endif
    124       1.64    rpaulo 
    125       1.25    itojun /*
    126       1.25    itojun  * Generate a last-resort interface identifier, when the machine has no
    127       1.25    itojun  * IEEE802/EUI64 address sources.
    128       1.25    itojun  * The goal here is to get an interface identifier that is
    129       1.25    itojun  * (1) random enough and (2) does not change across reboot.
    130       1.25    itojun  * We currently use MD5(hostname) for it.
    131       1.25    itojun  */
    132       1.25    itojun static int
    133       1.97     ozaki get_rand_ifid(struct in6_addr *in6)	/* upper 64bits are preserved */
    134       1.25    itojun {
    135       1.25    itojun 	MD5_CTX ctxt;
    136       1.25    itojun 	u_int8_t digest[16];
    137       1.25    itojun 
    138       1.25    itojun #if 0
    139       1.25    itojun 	/* we need at least several letters as seed for ifid */
    140       1.25    itojun 	if (hostnamelen < 3)
    141       1.25    itojun 		return -1;
    142       1.25    itojun #endif
    143       1.25    itojun 
    144       1.25    itojun 	/* generate 8 bytes of pseudo-random value. */
    145       1.64    rpaulo 	memset(&ctxt, 0, sizeof(ctxt));
    146       1.25    itojun 	MD5Init(&ctxt);
    147       1.50    itojun 	MD5Update(&ctxt, (u_char *)hostname, hostnamelen);
    148       1.25    itojun 	MD5Final(digest, &ctxt);
    149       1.25    itojun 
    150       1.25    itojun 	/* assumes sizeof(digest) > sizeof(ifid) */
    151       1.64    rpaulo 	memcpy(&in6->s6_addr[8], digest, 8);
    152        1.2    itojun 
    153       1.25    itojun 	/* make sure to set "u" bit to local, and "g" bit to individual. */
    154       1.25    itojun 	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
    155       1.25    itojun 	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
    156       1.25    itojun 
    157       1.25    itojun 	/* convert EUI64 into IPv6 interface identifier */
    158       1.25    itojun 	EUI64_TO_IFID(in6);
    159       1.25    itojun 
    160       1.25    itojun 	return 0;
    161       1.25    itojun }
    162        1.2    itojun 
    163       1.25    itojun /*
    164       1.25    itojun  * Get interface identifier for the specified interface.
    165       1.71  christos  *
    166       1.71  christos  * in6 - upper 64bits are preserved
    167       1.25    itojun  */
    168       1.64    rpaulo int
    169       1.71  christos in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
    170        1.2    itojun {
    171       1.25    itojun 	struct ifaddr *ifa;
    172      1.103     ozaki 	const struct sockaddr_dl *sdl = NULL;
    173      1.104     ozaki 	const char *addr = NULL; /* XXX gcc 4.8 -Werror=maybe-uninitialized */
    174      1.104     ozaki 	size_t addrlen = 0; /* XXX gcc 4.8 -Werror=maybe-uninitialized */
    175       1.25    itojun 	static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    176       1.25    itojun 	static u_int8_t allone[8] =
    177       1.25    itojun 		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    178      1.103     ozaki 	int s;
    179       1.25    itojun 
    180      1.103     ozaki 	s = pserialize_read_enter();
    181      1.101     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
    182      1.103     ozaki 		const struct sockaddr_dl *tsdl;
    183       1.25    itojun 		if (ifa->ifa_addr->sa_family != AF_LINK)
    184       1.25    itojun 			continue;
    185       1.81    dyoung 		tsdl = satocsdl(ifa->ifa_addr);
    186       1.81    dyoung 		if (tsdl == NULL || tsdl->sdl_alen == 0)
    187       1.25    itojun 			continue;
    188      1.103     ozaki 		if (sdl == NULL || ifa == ifp->if_dl || ifa == ifp->if_hwdl) {
    189       1.81    dyoung 			sdl = tsdl;
    190      1.103     ozaki 			addr = CLLADDR(sdl);
    191      1.103     ozaki 			addrlen = sdl->sdl_alen;
    192      1.103     ozaki 		}
    193       1.81    dyoung 		if (ifa == ifp->if_hwdl)
    194       1.81    dyoung 			break;
    195       1.25    itojun 	}
    196      1.103     ozaki 	pserialize_read_exit(s);
    197       1.25    itojun 
    198       1.81    dyoung 	if (sdl == NULL)
    199       1.81    dyoung 		return -1;
    200       1.25    itojun 
    201       1.48    itojun 	switch (ifp->if_type) {
    202       1.48    itojun 	case IFT_IEEE1394:
    203       1.48    itojun 	case IFT_IEEE80211:
    204       1.48    itojun 		/* IEEE1394 uses 16byte length address starting with EUI64 */
    205       1.48    itojun 		if (addrlen > 8)
    206       1.48    itojun 			addrlen = 8;
    207       1.48    itojun 		break;
    208       1.48    itojun 	default:
    209       1.48    itojun 		break;
    210       1.48    itojun 	}
    211       1.48    itojun 
    212       1.25    itojun 	/* get EUI64 */
    213       1.25    itojun 	switch (ifp->if_type) {
    214       1.48    itojun 	/* IEEE802/EUI64 cases - what others? */
    215       1.25    itojun 	case IFT_ETHER:
    216       1.25    itojun 	case IFT_ATM:
    217       1.32      onoe 	case IFT_IEEE1394:
    218       1.48    itojun 	case IFT_IEEE80211:
    219       1.25    itojun 		/* look at IEEE802/EUI64 only */
    220       1.25    itojun 		if (addrlen != 8 && addrlen != 6)
    221       1.25    itojun 			return -1;
    222       1.13    itojun 
    223       1.25    itojun 		/*
    224       1.25    itojun 		 * check for invalid MAC address - on bsdi, we see it a lot
    225       1.25    itojun 		 * since wildboar configures all-zero MAC on pccard before
    226       1.25    itojun 		 * card insertion.
    227       1.25    itojun 		 */
    228       1.64    rpaulo 		if (memcmp(addr, allzero, addrlen) == 0)
    229       1.25    itojun 			return -1;
    230       1.64    rpaulo 		if (memcmp(addr, allone, addrlen) == 0)
    231       1.25    itojun 			return -1;
    232       1.25    itojun 
    233       1.25    itojun 		/* make EUI64 address */
    234       1.25    itojun 		if (addrlen == 8)
    235       1.64    rpaulo 			memcpy(&in6->s6_addr[8], addr, 8);
    236       1.25    itojun 		else if (addrlen == 6) {
    237       1.25    itojun 			in6->s6_addr[8] = addr[0];
    238       1.25    itojun 			in6->s6_addr[9] = addr[1];
    239       1.25    itojun 			in6->s6_addr[10] = addr[2];
    240       1.25    itojun 			in6->s6_addr[11] = 0xff;
    241       1.26    itojun 			in6->s6_addr[12] = 0xfe;
    242       1.25    itojun 			in6->s6_addr[13] = addr[3];
    243       1.25    itojun 			in6->s6_addr[14] = addr[4];
    244       1.25    itojun 			in6->s6_addr[15] = addr[5];
    245       1.25    itojun 		}
    246        1.7    itojun 		break;
    247       1.25    itojun 
    248       1.25    itojun 	case IFT_ARCNET:
    249       1.25    itojun 		if (addrlen != 1)
    250       1.25    itojun 			return -1;
    251       1.25    itojun 		if (!addr[0])
    252       1.25    itojun 			return -1;
    253       1.25    itojun 
    254       1.64    rpaulo 		memset(&in6->s6_addr[8], 0, 8);
    255       1.25    itojun 		in6->s6_addr[15] = addr[0];
    256       1.25    itojun 
    257       1.27    itojun 		/*
    258       1.27    itojun 		 * due to insufficient bitwidth, we mark it local.
    259       1.27    itojun 		 */
    260       1.25    itojun 		in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
    261       1.25    itojun 		in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
    262        1.7    itojun 		break;
    263       1.25    itojun 
    264       1.25    itojun 	case IFT_GIF:
    265       1.25    itojun #ifdef IFT_STF
    266       1.25    itojun 	case IFT_STF:
    267       1.25    itojun #endif
    268       1.25    itojun 		/*
    269       1.34    itojun 		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
    270       1.27    itojun 		 * however, IPv4 address is not very suitable as unique
    271       1.27    itojun 		 * identifier source (can be renumbered).
    272       1.27    itojun 		 * we don't do this.
    273       1.25    itojun 		 */
    274       1.25    itojun 		return -1;
    275       1.25    itojun 
    276        1.7    itojun 	default:
    277       1.25    itojun 		return -1;
    278       1.25    itojun 	}
    279       1.25    itojun 
    280       1.25    itojun 	/* sanity check: g bit must not indicate "group" */
    281       1.25    itojun 	if (EUI64_GROUP(in6))
    282       1.25    itojun 		return -1;
    283       1.25    itojun 
    284       1.25    itojun 	/* convert EUI64 into IPv6 interface identifier */
    285       1.25    itojun 	EUI64_TO_IFID(in6);
    286       1.25    itojun 
    287       1.25    itojun 	/*
    288       1.25    itojun 	 * sanity check: ifid must not be all zero, avoid conflict with
    289       1.25    itojun 	 * subnet router anycast
    290       1.25    itojun 	 */
    291       1.25    itojun 	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
    292       1.64    rpaulo 	    memcmp(&in6->s6_addr[9], allzero, 7) == 0) {
    293       1.25    itojun 		return -1;
    294        1.7    itojun 	}
    295        1.7    itojun 
    296        1.7    itojun 	return 0;
    297        1.2    itojun }
    298        1.2    itojun 
    299        1.2    itojun /*
    300       1.25    itojun  * Get interface identifier for the specified interface.  If it is not
    301       1.25    itojun  * available on ifp0, borrow interface identifier from other information
    302       1.25    itojun  * sources.
    303       1.71  christos  *
    304       1.71  christos  * altifp - secondary EUI64 source
    305       1.13    itojun  */
    306       1.13    itojun static int
    307       1.71  christos get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
    308       1.71  christos 	struct in6_addr *in6)
    309       1.13    itojun {
    310       1.25    itojun 	struct ifnet *ifp;
    311       1.98     ozaki 	int s;
    312       1.25    itojun 
    313       1.25    itojun 	/* first, try to get it from the interface itself */
    314       1.64    rpaulo 	if (in6_get_hw_ifid(ifp0, in6) == 0) {
    315       1.96     ozaki 		nd6log(LOG_DEBUG, "%s: got interface identifier from itself\n",
    316       1.96     ozaki 		    if_name(ifp0));
    317       1.25    itojun 		goto success;
    318       1.25    itojun 	}
    319       1.25    itojun 
    320       1.25    itojun 	/* try secondary EUI64 source. this basically is for ATM PVC */
    321       1.64    rpaulo 	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
    322       1.96     ozaki 		nd6log(LOG_DEBUG, "%s: got interface identifier from %s\n",
    323       1.96     ozaki 		    if_name(ifp0), if_name(altifp));
    324       1.25    itojun 		goto success;
    325       1.25    itojun 	}
    326       1.25    itojun 
    327       1.25    itojun 	/* next, try to get it from some other hardware interface */
    328       1.98     ozaki 	s = pserialize_read_enter();
    329       1.98     ozaki 	IFNET_READER_FOREACH(ifp) {
    330       1.25    itojun 		if (ifp == ifp0)
    331       1.25    itojun 			continue;
    332       1.64    rpaulo 		if (in6_get_hw_ifid(ifp, in6) != 0)
    333       1.25    itojun 			continue;
    334       1.27    itojun 
    335       1.25    itojun 		/*
    336       1.25    itojun 		 * to borrow ifid from other interface, ifid needs to be
    337       1.25    itojun 		 * globally unique
    338       1.25    itojun 		 */
    339       1.25    itojun 		if (IFID_UNIVERSAL(in6)) {
    340       1.96     ozaki 			nd6log(LOG_DEBUG,
    341       1.34    itojun 			    "%s: borrow interface identifier from %s\n",
    342       1.96     ozaki 			    if_name(ifp0), if_name(ifp));
    343      1.106     ozaki 			pserialize_read_exit(s);
    344       1.25    itojun 			goto success;
    345       1.25    itojun 		}
    346       1.25    itojun 	}
    347       1.98     ozaki 	pserialize_read_exit(s);
    348       1.13    itojun 
    349       1.64    rpaulo #if 0
    350       1.64    rpaulo 	/* get from hostid - only for certain architectures */
    351       1.64    rpaulo 	if (get_hostid_ifid(ifp, in6) == 0) {
    352       1.96     ozaki 		nd6log(LOG_DEBUG,
    353       1.64    rpaulo 		    "%s: interface identifier generated by hostid\n",
    354       1.96     ozaki 		    if_name(ifp0));
    355       1.64    rpaulo 		goto success;
    356       1.64    rpaulo 	}
    357       1.64    rpaulo #endif
    358       1.64    rpaulo 
    359       1.25    itojun 	/* last resort: get from random number source */
    360       1.97     ozaki 	if (get_rand_ifid(in6) == 0) {
    361       1.96     ozaki 		nd6log(LOG_DEBUG,
    362       1.34    itojun 		    "%s: interface identifier generated by random number\n",
    363       1.96     ozaki 		    if_name(ifp0));
    364       1.25    itojun 		goto success;
    365       1.25    itojun 	}
    366       1.13    itojun 
    367       1.31    itojun 	printf("%s: failed to get interface identifier\n", if_name(ifp0));
    368       1.25    itojun 	return -1;
    369       1.13    itojun 
    370       1.25    itojun success:
    371       1.96     ozaki 	nd6log(LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
    372       1.47    itojun 	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
    373       1.47    itojun 	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
    374       1.96     ozaki 	    in6->s6_addr[14], in6->s6_addr[15]);
    375       1.13    itojun 	return 0;
    376       1.13    itojun }
    377       1.13    itojun 
    378       1.71  christos /*
    379       1.71  christos  * altifp - secondary EUI64 source
    380       1.71  christos  */
    381       1.71  christos 
    382       1.25    itojun static int
    383       1.71  christos in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
    384       1.48    itojun {
    385       1.48    itojun 	struct in6_aliasreq ifra;
    386      1.105       roy 	int error;
    387       1.25    itojun 
    388       1.25    itojun 	/*
    389       1.48    itojun 	 * configure link-local address.
    390       1.25    itojun 	 */
    391       1.64    rpaulo 	memset(&ifra, 0, sizeof(ifra));
    392        1.2    itojun 
    393       1.25    itojun 	/*
    394       1.48    itojun 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
    395       1.48    itojun 	 * for safety.
    396       1.25    itojun 	 */
    397       1.48    itojun 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
    398        1.2    itojun 
    399       1.48    itojun 	ifra.ifra_addr.sin6_family = AF_INET6;
    400       1.48    itojun 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
    401       1.63    rpaulo 	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
    402       1.48    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
    403       1.48    itojun 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    404       1.48    itojun 		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
    405       1.48    itojun 		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
    406       1.48    itojun 	} else {
    407       1.48    itojun 		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
    408       1.96     ozaki 			nd6log(LOG_ERR,
    409       1.96     ozaki 			    "%s: no ifid available\n", if_name(ifp));
    410       1.69    dyoung 			return -1;
    411       1.25    itojun 		}
    412       1.25    itojun 	}
    413       1.63    rpaulo 	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
    414       1.69    dyoung 		return -1;
    415       1.25    itojun 
    416       1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask64, 0, 0, 0);
    417       1.48    itojun 	/* link-local addresses should NEVER expire. */
    418       1.48    itojun 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    419       1.48    itojun 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    420       1.25    itojun 
    421       1.48    itojun 	/*
    422       1.48    itojun 	 * Now call in6_update_ifa() to do a bunch of procedures to configure
    423       1.70    dyoung 	 * a link-local address. We can set the 3rd argument to NULL, because
    424       1.48    itojun 	 * we know there's no other link-local address on the interface
    425       1.48    itojun 	 * and therefore we are adding one (instead of updating one).
    426       1.48    itojun 	 */
    427      1.109  christos 	if ((error = in6_update_ifa(ifp, &ifra, IN6_IFAUPDATE_DADDELAY)) != 0) {
    428       1.25    itojun 		/*
    429       1.48    itojun 		 * XXX: When the interface does not support IPv6, this call
    430       1.82    dyoung 		 * would fail in the SIOCINITIFADDR ioctl.  I believe the
    431       1.48    itojun 		 * notification is rather confusing in this case, so just
    432       1.48    itojun 		 * suppress it.  (jinmei (at) kame.net 20010130)
    433       1.25    itojun 		 */
    434       1.48    itojun 		if (error != EAFNOSUPPORT)
    435       1.96     ozaki 			nd6log(LOG_NOTICE,
    436       1.96     ozaki 			    "failed to configure a link-local address on %s "
    437       1.48    itojun 			    "(errno=%d)\n",
    438       1.96     ozaki 			    if_name(ifp), error);
    439       1.69    dyoung 		return -1;
    440       1.25    itojun 	}
    441       1.25    itojun 
    442       1.25    itojun 	return 0;
    443       1.25    itojun }
    444       1.25    itojun 
    445       1.71  christos /*
    446       1.71  christos  * ifp - mut be IFT_LOOP
    447       1.71  christos  */
    448       1.71  christos 
    449       1.25    itojun static int
    450       1.71  christos in6_ifattach_loopback(struct ifnet *ifp)
    451       1.25    itojun {
    452       1.48    itojun 	struct in6_aliasreq ifra;
    453       1.48    itojun 	int error;
    454       1.48    itojun 
    455       1.64    rpaulo 	memset(&ifra, 0, sizeof(ifra));
    456       1.25    itojun 
    457       1.25    itojun 	/*
    458       1.48    itojun 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
    459       1.48    itojun 	 * for safety.
    460       1.25    itojun 	 */
    461       1.48    itojun 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
    462       1.48    itojun 
    463       1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask128, 0, 0, 0);
    464       1.25    itojun 
    465       1.25    itojun 	/*
    466       1.25    itojun 	 * Always initialize ia_dstaddr (= broadcast address) to loopback
    467       1.48    itojun 	 * address.  Follows IPv4 practice - see in_ifinit().
    468       1.48    itojun 	 */
    469       1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_dstaddr, &in6addr_loopback, 0, 0, 0);
    470       1.48    itojun 
    471       1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_addr, &in6addr_loopback, 0, 0, 0);
    472       1.48    itojun 
    473       1.48    itojun 	/* the loopback  address should NEVER expire. */
    474       1.48    itojun 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    475       1.48    itojun 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    476       1.48    itojun 
    477       1.48    itojun 	/* we don't need to perform DAD on loopback interfaces. */
    478       1.48    itojun 	ifra.ifra_flags |= IN6_IFF_NODAD;
    479       1.48    itojun 
    480       1.48    itojun 	/*
    481       1.48    itojun 	 * We are sure that this is a newly assigned address, so we can set
    482       1.48    itojun 	 * NULL to the 3rd arg.
    483       1.48    itojun 	 */
    484      1.109  christos 	if ((error = in6_update_ifa(ifp, &ifra, 0)) != 0) {
    485       1.96     ozaki 		nd6log(LOG_ERR, "failed to configure "
    486       1.48    itojun 		    "the loopback address on %s (errno=%d)\n",
    487       1.96     ozaki 		    if_name(ifp), error);
    488       1.69    dyoung 		return -1;
    489       1.48    itojun 	}
    490       1.25    itojun 
    491       1.48    itojun 	return 0;
    492       1.48    itojun }
    493       1.48    itojun 
    494       1.48    itojun /*
    495       1.48    itojun  * compute NI group address, based on the current hostname setting.
    496       1.48    itojun  * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
    497       1.48    itojun  *
    498       1.48    itojun  * when ifp == NULL, the caller is responsible for filling scopeid.
    499       1.48    itojun  */
    500       1.48    itojun int
    501       1.71  christos in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
    502       1.71  christos 	struct sockaddr_in6 *sa6)
    503       1.48    itojun {
    504       1.48    itojun 	const char *p;
    505       1.52    itojun 	u_int8_t *q;
    506       1.48    itojun 	MD5_CTX ctxt;
    507       1.48    itojun 	u_int8_t digest[16];
    508       1.50    itojun 	u_int8_t l;
    509       1.50    itojun 	u_int8_t n[64];	/* a single label must not exceed 63 chars */
    510       1.25    itojun 
    511       1.48    itojun 	if (!namelen || !name)
    512       1.25    itojun 		return -1;
    513       1.48    itojun 
    514       1.48    itojun 	p = name;
    515       1.48    itojun 	while (p && *p && *p != '.' && p - name < namelen)
    516       1.48    itojun 		p++;
    517       1.48    itojun 	if (p - name > sizeof(n) - 1)
    518       1.48    itojun 		return -1;	/* label too long */
    519       1.48    itojun 	l = p - name;
    520       1.50    itojun 	strncpy((char *)n, name, l);
    521       1.48    itojun 	n[(int)l] = '\0';
    522       1.48    itojun 	for (q = n; *q; q++) {
    523       1.48    itojun 		if ('A' <= *q && *q <= 'Z')
    524       1.48    itojun 			*q = *q - 'A' + 'a';
    525        1.2    itojun 	}
    526       1.25    itojun 
    527       1.48    itojun 	/* generate 8 bytes of pseudo-random value. */
    528       1.64    rpaulo 	memset(&ctxt, 0, sizeof(ctxt));
    529       1.48    itojun 	MD5Init(&ctxt);
    530       1.48    itojun 	MD5Update(&ctxt, &l, sizeof(l));
    531       1.48    itojun 	MD5Update(&ctxt, n, l);
    532       1.48    itojun 	MD5Final(digest, &ctxt);
    533       1.48    itojun 
    534       1.64    rpaulo 	memset(sa6, 0, sizeof(*sa6));
    535       1.48    itojun 	sa6->sin6_family = AF_INET6;
    536       1.48    itojun 	sa6->sin6_len = sizeof(*sa6);
    537       1.48    itojun 	sa6->sin6_addr.s6_addr16[0] = htons(0xff02);
    538       1.48    itojun 	sa6->sin6_addr.s6_addr8[11] = 2;
    539       1.64    rpaulo 	memcpy(&sa6->sin6_addr.s6_addr32[3], digest,
    540       1.48    itojun 	    sizeof(sa6->sin6_addr.s6_addr32[3]));
    541       1.63    rpaulo 	if (in6_setscope(&sa6->sin6_addr, ifp, NULL))
    542       1.69    dyoung 		return -1; /* XXX: should not fail */
    543       1.48    itojun 
    544       1.25    itojun 	return 0;
    545        1.2    itojun }
    546        1.2    itojun 
    547       1.17    itojun /*
    548       1.17    itojun  * XXX multiple loopback interface needs more care.  for instance,
    549       1.17    itojun  * nodelocal address needs to be configured onto only one of them.
    550       1.25    itojun  * XXX multiple link-local address case
    551       1.71  christos  *
    552       1.71  christos  * altifp - secondary EUI64 source
    553       1.17    itojun  */
    554        1.2    itojun void
    555       1.71  christos in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
    556        1.2    itojun {
    557       1.85  christos 	struct in6_ifaddr *ia;
    558       1.85  christos 	struct in6_addr in6;
    559       1.13    itojun 
    560      1.114     ozaki 	KASSERT(IFNET_LOCKED(ifp));
    561      1.114     ozaki 
    562       1.38    itojun 	/* some of the interfaces are inherently not IPv6 capable */
    563       1.38    itojun 	switch (ifp->if_type) {
    564       1.42    itojun 	case IFT_BRIDGE:
    565      1.111  knakahar 	case IFT_L2TP:
    566  1.119.6.1   thorpej 	case IFT_IEEE8023ADLAG:
    567       1.59  christos #ifdef IFT_PFLOG
    568       1.58    itojun 	case IFT_PFLOG:
    569       1.59  christos #endif
    570       1.59  christos #ifdef IFT_PFSYNC
    571       1.58    itojun 	case IFT_PFSYNC:
    572       1.59  christos #endif
    573       1.91       roy 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
    574       1.91       roy 		ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
    575       1.42    itojun 		return;
    576       1.38    itojun 	}
    577       1.38    itojun 
    578       1.46    itojun 	/*
    579       1.46    itojun 	 * if link mtu is too small, don't try to configure IPv6.
    580       1.46    itojun 	 * remember there could be some link-layer that has special
    581       1.46    itojun 	 * fragmentation logic.
    582       1.46    itojun 	 */
    583       1.49    itojun 	if (ifp->if_mtu < IPV6_MMTU) {
    584       1.96     ozaki 		nd6log(LOG_INFO, "%s has too small MTU, IPv6 not enabled\n",
    585       1.96     ozaki 		    if_name(ifp));
    586       1.46    itojun 		return;
    587       1.49    itojun 	}
    588       1.46    itojun 
    589        1.2    itojun 	/*
    590       1.25    itojun 	 * quirks based on interface type
    591        1.2    itojun 	 */
    592       1.25    itojun 	switch (ifp->if_type) {
    593       1.25    itojun #ifdef IFT_STF
    594       1.25    itojun 	case IFT_STF:
    595       1.25    itojun 		/*
    596       1.38    itojun 		 * 6to4 interface is a very special kind of beast.
    597       1.38    itojun 		 * no multicast, no linklocal.  RFC2529 specifies how to make
    598       1.38    itojun 		 * linklocals for 6to4 interface, but there's no use and
    599       1.38    itojun 		 * it is rather harmful to have one.
    600       1.25    itojun 		 */
    601       1.91       roy 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
    602       1.46    itojun 		return;
    603       1.25    itojun #endif
    604       1.65  liamjfoy 	case IFT_CARP:
    605       1.65  liamjfoy 		return;
    606       1.25    itojun 	default:
    607       1.25    itojun 		break;
    608        1.7    itojun 	}
    609        1.2    itojun 
    610        1.2    itojun 	/*
    611       1.25    itojun 	 * usually, we require multicast capability to the interface
    612        1.2    itojun 	 */
    613       1.25    itojun 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
    614       1.96     ozaki 		nd6log(LOG_INFO,
    615       1.38    itojun 		    "%s is not multicast capable, IPv6 not enabled\n",
    616       1.96     ozaki 		    if_name(ifp));
    617       1.25    itojun 		return;
    618       1.25    itojun 	}
    619       1.15   thorpej 
    620        1.2    itojun 	/*
    621       1.48    itojun 	 * assign loopback address for loopback interface.
    622       1.48    itojun 	 * XXX multiple loopback interface case.
    623        1.2    itojun 	 */
    624       1.48    itojun 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    625       1.48    itojun 		in6 = in6addr_loopback;
    626      1.114     ozaki 		/* These are safe and atomic thanks to IFNET_LOCK */
    627       1.25    itojun 		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
    628      1.114     ozaki 			if (in6_ifattach_loopback(ifp) != 0)
    629       1.25    itojun 				return;
    630       1.25    itojun 		}
    631       1.25    itojun 	}
    632        1.2    itojun 
    633        1.2    itojun 	/*
    634       1.48    itojun 	 * assign a link-local address, if there's none.
    635        1.2    itojun 	 */
    636       1.91       roy 	if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
    637      1.103     ozaki 	    ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
    638      1.113     ozaki 		int bound = curlwp_bind();
    639      1.113     ozaki 		struct psref psref;
    640      1.113     ozaki 		ia = in6ifa_ifpforlinklocal_psref(ifp, 0, &psref);
    641       1.70    dyoung 		if (ia == NULL && in6_ifattach_linklocal(ifp, altifp) != 0) {
    642       1.70    dyoung 			printf("%s: cannot assign link-local address\n",
    643       1.70    dyoung 			    ifp->if_xname);
    644        1.2    itojun 		}
    645      1.113     ozaki 		ia6_release(ia, &psref);
    646      1.113     ozaki 		curlwp_bindx(bound);
    647        1.2    itojun 	}
    648        1.2    itojun }
    649        1.2    itojun 
    650       1.17    itojun /*
    651       1.17    itojun  * NOTE: in6_ifdetach() does not support loopback if at this moment.
    652       1.41    itojun  * We don't need this function in bsdi, because interfaces are never removed
    653       1.41    itojun  * from the ifnet list in bsdi.
    654       1.17    itojun  */
    655        1.2    itojun void
    656       1.71  christos in6_ifdetach(struct ifnet *ifp)
    657        1.2    itojun {
    658       1.55    itojun 
    659      1.107     ozaki 	/* nuke any of IPv6 addresses we have */
    660      1.107     ozaki 	if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr);
    661      1.107     ozaki 
    662      1.112     ozaki 	in6_purge_multi(ifp);
    663      1.112     ozaki 
    664       1.55    itojun 	/* remove ip6_mrouter stuff */
    665       1.55    itojun 	ip6_mrouter_detach(ifp);
    666       1.18    itojun 
    667      1.108     ozaki 	/* remove neighbor management table */
    668       1.95    martin 	nd6_purge(ifp, NULL);
    669       1.64    rpaulo }
    670