Home | History | Annotate | Line # | Download | only in netinet6
      1  1.122  knakahar /*	$NetBSD: in6_ifattach.c,v 1.122 2024/04/11 07:34:37 knakahara 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.122  knakahar __KERNEL_RCSID(0, "$NetBSD: in6_ifattach.c,v 1.122 2024/04/11 07:34:37 knakahara 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.122  knakahar 	case IFT_IPSEC:
    266   1.25    itojun #ifdef IFT_STF
    267   1.25    itojun 	case IFT_STF:
    268   1.25    itojun #endif
    269   1.25    itojun 		/*
    270   1.34    itojun 		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
    271   1.27    itojun 		 * however, IPv4 address is not very suitable as unique
    272   1.27    itojun 		 * identifier source (can be renumbered).
    273   1.27    itojun 		 * we don't do this.
    274   1.25    itojun 		 */
    275   1.25    itojun 		return -1;
    276   1.25    itojun 
    277    1.7    itojun 	default:
    278   1.25    itojun 		return -1;
    279   1.25    itojun 	}
    280   1.25    itojun 
    281   1.25    itojun 	/* sanity check: g bit must not indicate "group" */
    282   1.25    itojun 	if (EUI64_GROUP(in6))
    283   1.25    itojun 		return -1;
    284   1.25    itojun 
    285   1.25    itojun 	/* convert EUI64 into IPv6 interface identifier */
    286   1.25    itojun 	EUI64_TO_IFID(in6);
    287   1.25    itojun 
    288   1.25    itojun 	/*
    289   1.25    itojun 	 * sanity check: ifid must not be all zero, avoid conflict with
    290   1.25    itojun 	 * subnet router anycast
    291   1.25    itojun 	 */
    292   1.25    itojun 	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
    293   1.64    rpaulo 	    memcmp(&in6->s6_addr[9], allzero, 7) == 0) {
    294   1.25    itojun 		return -1;
    295    1.7    itojun 	}
    296    1.7    itojun 
    297    1.7    itojun 	return 0;
    298    1.2    itojun }
    299    1.2    itojun 
    300    1.2    itojun /*
    301   1.25    itojun  * Get interface identifier for the specified interface.  If it is not
    302   1.25    itojun  * available on ifp0, borrow interface identifier from other information
    303   1.25    itojun  * sources.
    304   1.71  christos  *
    305   1.71  christos  * altifp - secondary EUI64 source
    306   1.13    itojun  */
    307   1.13    itojun static int
    308   1.71  christos get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
    309   1.71  christos 	struct in6_addr *in6)
    310   1.13    itojun {
    311   1.25    itojun 	struct ifnet *ifp;
    312   1.98     ozaki 	int s;
    313   1.25    itojun 
    314   1.25    itojun 	/* first, try to get it from the interface itself */
    315   1.64    rpaulo 	if (in6_get_hw_ifid(ifp0, in6) == 0) {
    316   1.96     ozaki 		nd6log(LOG_DEBUG, "%s: got interface identifier from itself\n",
    317   1.96     ozaki 		    if_name(ifp0));
    318   1.25    itojun 		goto success;
    319   1.25    itojun 	}
    320   1.25    itojun 
    321   1.25    itojun 	/* try secondary EUI64 source. this basically is for ATM PVC */
    322   1.64    rpaulo 	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
    323   1.96     ozaki 		nd6log(LOG_DEBUG, "%s: got interface identifier from %s\n",
    324   1.96     ozaki 		    if_name(ifp0), if_name(altifp));
    325   1.25    itojun 		goto success;
    326   1.25    itojun 	}
    327   1.25    itojun 
    328   1.25    itojun 	/* next, try to get it from some other hardware interface */
    329   1.98     ozaki 	s = pserialize_read_enter();
    330   1.98     ozaki 	IFNET_READER_FOREACH(ifp) {
    331   1.25    itojun 		if (ifp == ifp0)
    332   1.25    itojun 			continue;
    333   1.64    rpaulo 		if (in6_get_hw_ifid(ifp, in6) != 0)
    334   1.25    itojun 			continue;
    335   1.27    itojun 
    336   1.25    itojun 		/*
    337   1.25    itojun 		 * to borrow ifid from other interface, ifid needs to be
    338   1.25    itojun 		 * globally unique
    339   1.25    itojun 		 */
    340   1.25    itojun 		if (IFID_UNIVERSAL(in6)) {
    341   1.96     ozaki 			nd6log(LOG_DEBUG,
    342   1.34    itojun 			    "%s: borrow interface identifier from %s\n",
    343   1.96     ozaki 			    if_name(ifp0), if_name(ifp));
    344  1.106     ozaki 			pserialize_read_exit(s);
    345   1.25    itojun 			goto success;
    346   1.25    itojun 		}
    347   1.25    itojun 	}
    348   1.98     ozaki 	pserialize_read_exit(s);
    349   1.13    itojun 
    350   1.64    rpaulo #if 0
    351   1.64    rpaulo 	/* get from hostid - only for certain architectures */
    352   1.64    rpaulo 	if (get_hostid_ifid(ifp, in6) == 0) {
    353   1.96     ozaki 		nd6log(LOG_DEBUG,
    354   1.64    rpaulo 		    "%s: interface identifier generated by hostid\n",
    355   1.96     ozaki 		    if_name(ifp0));
    356   1.64    rpaulo 		goto success;
    357   1.64    rpaulo 	}
    358   1.64    rpaulo #endif
    359   1.64    rpaulo 
    360   1.25    itojun 	/* last resort: get from random number source */
    361   1.97     ozaki 	if (get_rand_ifid(in6) == 0) {
    362   1.96     ozaki 		nd6log(LOG_DEBUG,
    363   1.34    itojun 		    "%s: interface identifier generated by random number\n",
    364   1.96     ozaki 		    if_name(ifp0));
    365   1.25    itojun 		goto success;
    366   1.25    itojun 	}
    367   1.13    itojun 
    368   1.31    itojun 	printf("%s: failed to get interface identifier\n", if_name(ifp0));
    369   1.25    itojun 	return -1;
    370   1.13    itojun 
    371   1.25    itojun success:
    372   1.96     ozaki 	nd6log(LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
    373   1.47    itojun 	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
    374   1.47    itojun 	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
    375   1.96     ozaki 	    in6->s6_addr[14], in6->s6_addr[15]);
    376   1.13    itojun 	return 0;
    377   1.13    itojun }
    378   1.13    itojun 
    379   1.71  christos /*
    380   1.71  christos  * altifp - secondary EUI64 source
    381   1.71  christos  */
    382   1.71  christos 
    383   1.25    itojun static int
    384   1.71  christos in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
    385   1.48    itojun {
    386   1.48    itojun 	struct in6_aliasreq ifra;
    387  1.105       roy 	int error;
    388   1.25    itojun 
    389   1.25    itojun 	/*
    390   1.48    itojun 	 * configure link-local address.
    391   1.25    itojun 	 */
    392   1.64    rpaulo 	memset(&ifra, 0, sizeof(ifra));
    393    1.2    itojun 
    394   1.25    itojun 	/*
    395   1.48    itojun 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
    396   1.48    itojun 	 * for safety.
    397   1.25    itojun 	 */
    398   1.48    itojun 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
    399    1.2    itojun 
    400   1.48    itojun 	ifra.ifra_addr.sin6_family = AF_INET6;
    401   1.48    itojun 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
    402   1.63    rpaulo 	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
    403   1.48    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
    404   1.48    itojun 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    405   1.48    itojun 		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
    406   1.48    itojun 		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
    407   1.48    itojun 	} else {
    408   1.48    itojun 		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
    409   1.96     ozaki 			nd6log(LOG_ERR,
    410   1.96     ozaki 			    "%s: no ifid available\n", if_name(ifp));
    411   1.69    dyoung 			return -1;
    412   1.25    itojun 		}
    413   1.25    itojun 	}
    414   1.63    rpaulo 	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
    415   1.69    dyoung 		return -1;
    416   1.25    itojun 
    417   1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask64, 0, 0, 0);
    418   1.48    itojun 	/* link-local addresses should NEVER expire. */
    419   1.48    itojun 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    420   1.48    itojun 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    421   1.25    itojun 
    422   1.48    itojun 	/*
    423   1.48    itojun 	 * Now call in6_update_ifa() to do a bunch of procedures to configure
    424   1.70    dyoung 	 * a link-local address. We can set the 3rd argument to NULL, because
    425   1.48    itojun 	 * we know there's no other link-local address on the interface
    426   1.48    itojun 	 * and therefore we are adding one (instead of updating one).
    427   1.48    itojun 	 */
    428  1.109  christos 	if ((error = in6_update_ifa(ifp, &ifra, IN6_IFAUPDATE_DADDELAY)) != 0) {
    429   1.25    itojun 		/*
    430   1.48    itojun 		 * XXX: When the interface does not support IPv6, this call
    431   1.82    dyoung 		 * would fail in the SIOCINITIFADDR ioctl.  I believe the
    432   1.48    itojun 		 * notification is rather confusing in this case, so just
    433   1.48    itojun 		 * suppress it.  (jinmei (at) kame.net 20010130)
    434   1.25    itojun 		 */
    435   1.48    itojun 		if (error != EAFNOSUPPORT)
    436   1.96     ozaki 			nd6log(LOG_NOTICE,
    437   1.96     ozaki 			    "failed to configure a link-local address on %s "
    438   1.48    itojun 			    "(errno=%d)\n",
    439   1.96     ozaki 			    if_name(ifp), error);
    440   1.69    dyoung 		return -1;
    441   1.25    itojun 	}
    442   1.25    itojun 
    443   1.25    itojun 	return 0;
    444   1.25    itojun }
    445   1.25    itojun 
    446   1.71  christos /*
    447  1.121   msaitoh  * ifp - must be IFT_LOOP
    448   1.71  christos  */
    449   1.71  christos 
    450   1.25    itojun static int
    451   1.71  christos in6_ifattach_loopback(struct ifnet *ifp)
    452   1.25    itojun {
    453   1.48    itojun 	struct in6_aliasreq ifra;
    454   1.48    itojun 	int error;
    455   1.48    itojun 
    456   1.64    rpaulo 	memset(&ifra, 0, sizeof(ifra));
    457   1.25    itojun 
    458   1.25    itojun 	/*
    459   1.48    itojun 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
    460   1.48    itojun 	 * for safety.
    461   1.25    itojun 	 */
    462   1.48    itojun 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
    463   1.48    itojun 
    464   1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask128, 0, 0, 0);
    465   1.25    itojun 
    466   1.25    itojun 	/*
    467   1.25    itojun 	 * Always initialize ia_dstaddr (= broadcast address) to loopback
    468   1.48    itojun 	 * address.  Follows IPv4 practice - see in_ifinit().
    469   1.48    itojun 	 */
    470   1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_dstaddr, &in6addr_loopback, 0, 0, 0);
    471   1.48    itojun 
    472   1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_addr, &in6addr_loopback, 0, 0, 0);
    473   1.48    itojun 
    474   1.48    itojun 	/* the loopback  address should NEVER expire. */
    475   1.48    itojun 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    476   1.48    itojun 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    477   1.48    itojun 
    478   1.48    itojun 	/* we don't need to perform DAD on loopback interfaces. */
    479   1.48    itojun 	ifra.ifra_flags |= IN6_IFF_NODAD;
    480   1.48    itojun 
    481   1.48    itojun 	/*
    482   1.48    itojun 	 * We are sure that this is a newly assigned address, so we can set
    483   1.48    itojun 	 * NULL to the 3rd arg.
    484   1.48    itojun 	 */
    485  1.109  christos 	if ((error = in6_update_ifa(ifp, &ifra, 0)) != 0) {
    486   1.96     ozaki 		nd6log(LOG_ERR, "failed to configure "
    487   1.48    itojun 		    "the loopback address on %s (errno=%d)\n",
    488   1.96     ozaki 		    if_name(ifp), error);
    489   1.69    dyoung 		return -1;
    490   1.48    itojun 	}
    491   1.25    itojun 
    492   1.48    itojun 	return 0;
    493   1.48    itojun }
    494   1.48    itojun 
    495   1.48    itojun /*
    496   1.48    itojun  * compute NI group address, based on the current hostname setting.
    497   1.48    itojun  * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
    498   1.48    itojun  *
    499   1.48    itojun  * when ifp == NULL, the caller is responsible for filling scopeid.
    500   1.48    itojun  */
    501   1.48    itojun int
    502   1.71  christos in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
    503   1.71  christos 	struct sockaddr_in6 *sa6)
    504   1.48    itojun {
    505   1.48    itojun 	const char *p;
    506   1.52    itojun 	u_int8_t *q;
    507   1.48    itojun 	MD5_CTX ctxt;
    508   1.48    itojun 	u_int8_t digest[16];
    509   1.50    itojun 	u_int8_t l;
    510   1.50    itojun 	u_int8_t n[64];	/* a single label must not exceed 63 chars */
    511   1.25    itojun 
    512   1.48    itojun 	if (!namelen || !name)
    513   1.25    itojun 		return -1;
    514   1.48    itojun 
    515   1.48    itojun 	p = name;
    516   1.48    itojun 	while (p && *p && *p != '.' && p - name < namelen)
    517   1.48    itojun 		p++;
    518   1.48    itojun 	if (p - name > sizeof(n) - 1)
    519   1.48    itojun 		return -1;	/* label too long */
    520   1.48    itojun 	l = p - name;
    521   1.50    itojun 	strncpy((char *)n, name, l);
    522   1.48    itojun 	n[(int)l] = '\0';
    523   1.48    itojun 	for (q = n; *q; q++) {
    524   1.48    itojun 		if ('A' <= *q && *q <= 'Z')
    525   1.48    itojun 			*q = *q - 'A' + 'a';
    526    1.2    itojun 	}
    527   1.25    itojun 
    528   1.48    itojun 	/* generate 8 bytes of pseudo-random value. */
    529   1.64    rpaulo 	memset(&ctxt, 0, sizeof(ctxt));
    530   1.48    itojun 	MD5Init(&ctxt);
    531   1.48    itojun 	MD5Update(&ctxt, &l, sizeof(l));
    532   1.48    itojun 	MD5Update(&ctxt, n, l);
    533   1.48    itojun 	MD5Final(digest, &ctxt);
    534   1.48    itojun 
    535   1.64    rpaulo 	memset(sa6, 0, sizeof(*sa6));
    536   1.48    itojun 	sa6->sin6_family = AF_INET6;
    537   1.48    itojun 	sa6->sin6_len = sizeof(*sa6);
    538   1.48    itojun 	sa6->sin6_addr.s6_addr16[0] = htons(0xff02);
    539   1.48    itojun 	sa6->sin6_addr.s6_addr8[11] = 2;
    540   1.64    rpaulo 	memcpy(&sa6->sin6_addr.s6_addr32[3], digest,
    541   1.48    itojun 	    sizeof(sa6->sin6_addr.s6_addr32[3]));
    542   1.63    rpaulo 	if (in6_setscope(&sa6->sin6_addr, ifp, NULL))
    543   1.69    dyoung 		return -1; /* XXX: should not fail */
    544   1.48    itojun 
    545   1.25    itojun 	return 0;
    546    1.2    itojun }
    547    1.2    itojun 
    548   1.17    itojun /*
    549   1.17    itojun  * XXX multiple loopback interface needs more care.  for instance,
    550   1.17    itojun  * nodelocal address needs to be configured onto only one of them.
    551   1.25    itojun  * XXX multiple link-local address case
    552   1.71  christos  *
    553   1.71  christos  * altifp - secondary EUI64 source
    554   1.17    itojun  */
    555    1.2    itojun void
    556   1.71  christos in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
    557    1.2    itojun {
    558   1.85  christos 	struct in6_ifaddr *ia;
    559   1.85  christos 	struct in6_addr in6;
    560   1.13    itojun 
    561  1.114     ozaki 	KASSERT(IFNET_LOCKED(ifp));
    562  1.114     ozaki 
    563   1.38    itojun 	/* some of the interfaces are inherently not IPv6 capable */
    564   1.38    itojun 	switch (ifp->if_type) {
    565   1.42    itojun 	case IFT_BRIDGE:
    566  1.111  knakahar 	case IFT_L2TP:
    567  1.120  yamaguch 	case IFT_IEEE8023ADLAG:
    568   1.59  christos #ifdef IFT_PFLOG
    569   1.58    itojun 	case IFT_PFLOG:
    570   1.59  christos #endif
    571   1.59  christos #ifdef IFT_PFSYNC
    572   1.58    itojun 	case IFT_PFSYNC:
    573   1.59  christos #endif
    574   1.91       roy 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
    575   1.91       roy 		ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
    576   1.42    itojun 		return;
    577   1.38    itojun 	}
    578   1.38    itojun 
    579   1.46    itojun 	/*
    580   1.46    itojun 	 * if link mtu is too small, don't try to configure IPv6.
    581   1.46    itojun 	 * remember there could be some link-layer that has special
    582   1.46    itojun 	 * fragmentation logic.
    583   1.46    itojun 	 */
    584   1.49    itojun 	if (ifp->if_mtu < IPV6_MMTU) {
    585   1.96     ozaki 		nd6log(LOG_INFO, "%s has too small MTU, IPv6 not enabled\n",
    586   1.96     ozaki 		    if_name(ifp));
    587   1.46    itojun 		return;
    588   1.49    itojun 	}
    589   1.46    itojun 
    590    1.2    itojun 	/*
    591   1.25    itojun 	 * quirks based on interface type
    592    1.2    itojun 	 */
    593   1.25    itojun 	switch (ifp->if_type) {
    594   1.25    itojun #ifdef IFT_STF
    595   1.25    itojun 	case IFT_STF:
    596   1.25    itojun 		/*
    597   1.38    itojun 		 * 6to4 interface is a very special kind of beast.
    598   1.38    itojun 		 * no multicast, no linklocal.  RFC2529 specifies how to make
    599   1.38    itojun 		 * linklocals for 6to4 interface, but there's no use and
    600   1.38    itojun 		 * it is rather harmful to have one.
    601   1.25    itojun 		 */
    602   1.91       roy 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
    603   1.46    itojun 		return;
    604   1.25    itojun #endif
    605   1.65  liamjfoy 	case IFT_CARP:
    606   1.65  liamjfoy 		return;
    607   1.25    itojun 	default:
    608   1.25    itojun 		break;
    609    1.7    itojun 	}
    610    1.2    itojun 
    611    1.2    itojun 	/*
    612   1.25    itojun 	 * usually, we require multicast capability to the interface
    613    1.2    itojun 	 */
    614   1.25    itojun 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
    615   1.96     ozaki 		nd6log(LOG_INFO,
    616   1.38    itojun 		    "%s is not multicast capable, IPv6 not enabled\n",
    617   1.96     ozaki 		    if_name(ifp));
    618   1.25    itojun 		return;
    619   1.25    itojun 	}
    620   1.15   thorpej 
    621    1.2    itojun 	/*
    622   1.48    itojun 	 * assign loopback address for loopback interface.
    623   1.48    itojun 	 * XXX multiple loopback interface case.
    624    1.2    itojun 	 */
    625   1.48    itojun 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    626   1.48    itojun 		in6 = in6addr_loopback;
    627  1.114     ozaki 		/* These are safe and atomic thanks to IFNET_LOCK */
    628   1.25    itojun 		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
    629  1.114     ozaki 			if (in6_ifattach_loopback(ifp) != 0)
    630   1.25    itojun 				return;
    631   1.25    itojun 		}
    632   1.25    itojun 	}
    633    1.2    itojun 
    634    1.2    itojun 	/*
    635   1.48    itojun 	 * assign a link-local address, if there's none.
    636    1.2    itojun 	 */
    637   1.91       roy 	if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
    638  1.103     ozaki 	    ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
    639  1.113     ozaki 		int bound = curlwp_bind();
    640  1.113     ozaki 		struct psref psref;
    641  1.113     ozaki 		ia = in6ifa_ifpforlinklocal_psref(ifp, 0, &psref);
    642   1.70    dyoung 		if (ia == NULL && in6_ifattach_linklocal(ifp, altifp) != 0) {
    643   1.70    dyoung 			printf("%s: cannot assign link-local address\n",
    644   1.70    dyoung 			    ifp->if_xname);
    645    1.2    itojun 		}
    646  1.113     ozaki 		ia6_release(ia, &psref);
    647  1.113     ozaki 		curlwp_bindx(bound);
    648    1.2    itojun 	}
    649    1.2    itojun }
    650    1.2    itojun 
    651   1.17    itojun /*
    652   1.17    itojun  * NOTE: in6_ifdetach() does not support loopback if at this moment.
    653   1.41    itojun  * We don't need this function in bsdi, because interfaces are never removed
    654   1.41    itojun  * from the ifnet list in bsdi.
    655   1.17    itojun  */
    656    1.2    itojun void
    657   1.71  christos in6_ifdetach(struct ifnet *ifp)
    658    1.2    itojun {
    659   1.55    itojun 
    660  1.107     ozaki 	/* nuke any of IPv6 addresses we have */
    661  1.107     ozaki 	if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr);
    662  1.107     ozaki 
    663  1.112     ozaki 	in6_purge_multi(ifp);
    664  1.112     ozaki 
    665   1.55    itojun 	/* remove ip6_mrouter stuff */
    666   1.55    itojun 	ip6_mrouter_detach(ifp);
    667   1.18    itojun 
    668  1.108     ozaki 	/* remove neighbor management table */
    669   1.95    martin 	nd6_purge(ifp, NULL);
    670   1.64    rpaulo }
    671