Home | History | Annotate | Line # | Download | only in netinet6
in6_ifattach.c revision 1.95
      1  1.95    martin /*	$NetBSD: in6_ifattach.c,v 1.95 2015/02/23 19:15:59 martin 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.95    martin __KERNEL_RCSID(0, "$NetBSD: in6_ifattach.c,v 1.95 2015/02/23 19:15:59 martin 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.13    itojun #include <net/net_osdep.h>
     63  1.13    itojun 
     64   1.2    itojun unsigned long in6_maxmtu = 0;
     65   1.2    itojun 
     66  1.48    itojun int ip6_auto_linklocal = 1;	/* enable by default */
     67  1.48    itojun 
     68  1.72        ad callout_t in6_tmpaddrtimer_ch;
     69  1.64    rpaulo 
     70  1.64    rpaulo 
     71  1.64    rpaulo #if 0
     72  1.74    dyoung static int get_hostid_ifid(struct ifnet *, struct in6_addr *);
     73  1.64    rpaulo #endif
     74  1.74    dyoung static int get_rand_ifid(struct ifnet *, struct in6_addr *);
     75  1.74    dyoung static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
     76  1.74    dyoung static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
     77  1.74    dyoung static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
     78  1.74    dyoung static int in6_ifattach_loopback(struct ifnet *);
     79  1.25    itojun 
     80  1.25    itojun #define EUI64_GBIT	0x01
     81  1.25    itojun #define EUI64_UBIT	0x02
     82  1.54     perry #define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (/*CONSTCOND*/ 0)
     83  1.25    itojun #define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
     84  1.25    itojun #define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
     85  1.25    itojun #define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
     86  1.25    itojun #define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
     87  1.25    itojun 
     88  1.25    itojun #define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
     89  1.25    itojun #define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
     90  1.25    itojun 
     91  1.64    rpaulo #define GEN_TEMPID_RETRY_MAX 5
     92  1.64    rpaulo 
     93  1.64    rpaulo #if 0
     94  1.64    rpaulo /*
     95  1.64    rpaulo  * Generate a last-resort interface identifier from hostid.
     96  1.64    rpaulo  * works only for certain architectures (like sparc).
     97  1.64    rpaulo  * also, using hostid itself may constitute a privacy threat, much worse
     98  1.64    rpaulo  * than MAC addresses (hostids are used for software licensing).
     99  1.64    rpaulo  * maybe we should use MD5(hostid) instead.
    100  1.71  christos  *
    101  1.71  christos  * in6 - upper 64bits are preserved
    102  1.64    rpaulo  */
    103  1.64    rpaulo static int
    104  1.71  christos get_hostid_ifid(struct ifnet *ifp, struct in6_addr *in6)
    105  1.64    rpaulo {
    106  1.64    rpaulo 	int off, len;
    107  1.64    rpaulo 	static const uint8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    108  1.64    rpaulo 	static const uint8_t allone[8] =
    109  1.64    rpaulo 	    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    110  1.64    rpaulo 
    111  1.64    rpaulo 	if (!hostid)
    112  1.64    rpaulo 		return -1;
    113  1.64    rpaulo 
    114  1.64    rpaulo 	/* get up to 8 bytes from the hostid field - should we get */
    115  1.64    rpaulo 	len = (sizeof(hostid) > 8) ? 8 : sizeof(hostid);
    116  1.64    rpaulo 	off = sizeof(*in6) - len;
    117  1.64    rpaulo 	memcpy(&in6->s6_addr[off], &hostid, len);
    118  1.64    rpaulo 
    119  1.64    rpaulo 	/* make sure we do not return anything bogus */
    120  1.64    rpaulo 	if (memcmp(&in6->s6_addr[8], allzero, sizeof(allzero)))
    121  1.64    rpaulo 		return -1;
    122  1.64    rpaulo 	if (memcmp(&in6->s6_addr[8], allone, sizeof(allone)))
    123  1.64    rpaulo 		return -1;
    124  1.64    rpaulo 
    125  1.64    rpaulo 	/* make sure to set "u" bit to local, and "g" bit to individual. */
    126  1.64    rpaulo 	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
    127  1.64    rpaulo 	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
    128  1.64    rpaulo 
    129  1.64    rpaulo 	/* convert EUI64 into IPv6 interface identifier */
    130  1.64    rpaulo 	EUI64_TO_IFID(in6);
    131  1.64    rpaulo 
    132  1.64    rpaulo 	return 0;
    133  1.64    rpaulo }
    134  1.64    rpaulo #endif
    135  1.64    rpaulo 
    136  1.25    itojun /*
    137  1.25    itojun  * Generate a last-resort interface identifier, when the machine has no
    138  1.25    itojun  * IEEE802/EUI64 address sources.
    139  1.25    itojun  * The goal here is to get an interface identifier that is
    140  1.25    itojun  * (1) random enough and (2) does not change across reboot.
    141  1.25    itojun  * We currently use MD5(hostname) for it.
    142  1.25    itojun  */
    143  1.25    itojun static int
    144  1.67  christos get_rand_ifid(struct ifnet *ifp,
    145  1.66  christos 	struct in6_addr *in6)	/* upper 64bits are preserved */
    146  1.25    itojun {
    147  1.25    itojun 	MD5_CTX ctxt;
    148  1.25    itojun 	u_int8_t digest[16];
    149  1.25    itojun 
    150  1.25    itojun #if 0
    151  1.25    itojun 	/* we need at least several letters as seed for ifid */
    152  1.25    itojun 	if (hostnamelen < 3)
    153  1.25    itojun 		return -1;
    154  1.25    itojun #endif
    155  1.25    itojun 
    156  1.25    itojun 	/* generate 8 bytes of pseudo-random value. */
    157  1.64    rpaulo 	memset(&ctxt, 0, sizeof(ctxt));
    158  1.25    itojun 	MD5Init(&ctxt);
    159  1.50    itojun 	MD5Update(&ctxt, (u_char *)hostname, hostnamelen);
    160  1.25    itojun 	MD5Final(digest, &ctxt);
    161  1.25    itojun 
    162  1.25    itojun 	/* assumes sizeof(digest) > sizeof(ifid) */
    163  1.64    rpaulo 	memcpy(&in6->s6_addr[8], digest, 8);
    164   1.2    itojun 
    165  1.25    itojun 	/* make sure to set "u" bit to local, and "g" bit to individual. */
    166  1.25    itojun 	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
    167  1.25    itojun 	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
    168  1.25    itojun 
    169  1.25    itojun 	/* convert EUI64 into IPv6 interface identifier */
    170  1.25    itojun 	EUI64_TO_IFID(in6);
    171  1.25    itojun 
    172  1.25    itojun 	return 0;
    173  1.25    itojun }
    174   1.2    itojun 
    175  1.64    rpaulo static int
    176  1.71  christos generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
    177  1.64    rpaulo {
    178  1.64    rpaulo 	MD5_CTX ctxt;
    179  1.64    rpaulo 	u_int8_t seed[16], digest[16], nullbuf[8];
    180  1.64    rpaulo 	/*
    181  1.64    rpaulo 	 * interface ID for subnet anycast addresses.
    182  1.64    rpaulo 	 * XXX: we assume the unicast address range that requires IDs
    183  1.64    rpaulo 	 * in EUI-64 format.
    184  1.64    rpaulo 	 */
    185  1.64    rpaulo 	static const uint8_t anycast_id[8] = { 0xfd, 0xff, 0xff, 0xff,
    186  1.64    rpaulo 	    0xff, 0xff, 0xff, 0x80 };
    187  1.64    rpaulo 	static const uint8_t isatap_id[4] = { 0x00, 0x00, 0x5e, 0xfe };
    188  1.64    rpaulo 	int badid, retry = 0;
    189  1.64    rpaulo 
    190  1.64    rpaulo 	/* If there's no hisotry, start with a random seed. */
    191  1.64    rpaulo 	memset(nullbuf, 0, sizeof(nullbuf));
    192  1.64    rpaulo 	if (memcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
    193  1.86       tls 		cprng_fast(seed, sizeof(seed));
    194  1.64    rpaulo 	} else
    195  1.64    rpaulo 		memcpy(seed, seed0, 8);
    196  1.64    rpaulo 
    197  1.64    rpaulo 	/* copy the right-most 64-bits of the given address */
    198  1.64    rpaulo 	/* XXX assumption on the size of IFID */
    199  1.64    rpaulo 	memcpy(&seed[8], seed1, 8);
    200  1.64    rpaulo 
    201  1.64    rpaulo   again:
    202  1.64    rpaulo 	/* for debugging purposes only */
    203  1.64    rpaulo #if 0
    204  1.64    rpaulo 	{
    205  1.64    rpaulo 		int i;
    206  1.64    rpaulo 
    207  1.64    rpaulo 		printf("generate_tmp_ifid: new randomized ID from: ");
    208  1.64    rpaulo 		for (i = 0; i < 16; i++)
    209  1.64    rpaulo 			printf("%02x", seed[i]);
    210  1.64    rpaulo 		printf(" ");
    211  1.64    rpaulo 	}
    212  1.64    rpaulo #endif
    213  1.64    rpaulo 
    214  1.64    rpaulo 	/* generate 16 bytes of pseudo-random value. */
    215  1.64    rpaulo 	memset(&ctxt, 0, sizeof(ctxt));
    216  1.64    rpaulo 	MD5Init(&ctxt);
    217  1.64    rpaulo 	MD5Update(&ctxt, seed, sizeof(seed));
    218  1.64    rpaulo 	MD5Final(digest, &ctxt);
    219  1.64    rpaulo 
    220  1.64    rpaulo 	/*
    221  1.64    rpaulo 	 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (3)
    222  1.64    rpaulo 	 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
    223  1.64    rpaulo 	 * left-most bit is numbered 0) to zero.
    224  1.64    rpaulo 	 */
    225  1.64    rpaulo 	memcpy(ret, digest, 8);
    226  1.64    rpaulo 	ret[0] &= ~EUI64_UBIT;
    227  1.64    rpaulo 
    228  1.64    rpaulo 	/*
    229  1.64    rpaulo 	 * Reject inappropriate identifiers according to
    230  1.64    rpaulo 	 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (4)
    231  1.64    rpaulo 	 * At this moment, we reject following cases:
    232  1.64    rpaulo 	 * - all 0 identifier
    233  1.64    rpaulo 	 * - identifiers that conflict with reserved subnet anycast addresses,
    234  1.64    rpaulo 	 *   which are defined in RFC 2526.
    235  1.64    rpaulo 	 * - identifiers that conflict with ISATAP addresses
    236  1.64    rpaulo 	 * - identifiers used in our own addresses
    237  1.64    rpaulo 	 */
    238  1.64    rpaulo 	badid = 0;
    239  1.64    rpaulo 	if (memcmp(nullbuf, ret, sizeof(nullbuf)) == 0)
    240  1.64    rpaulo 		badid = 1;
    241  1.64    rpaulo 	else if (memcmp(anycast_id, ret, 7) == 0 &&
    242  1.64    rpaulo 	    (anycast_id[7] & ret[7]) == anycast_id[7]) {
    243  1.64    rpaulo 		badid = 1;
    244  1.64    rpaulo 	} else if (memcmp(isatap_id, ret, sizeof(isatap_id)) == 0)
    245  1.64    rpaulo 		badid = 1;
    246  1.64    rpaulo 	else {
    247  1.64    rpaulo 		struct in6_ifaddr *ia;
    248  1.64    rpaulo 
    249  1.64    rpaulo 		for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
    250  1.64    rpaulo 			if (!memcmp(&ia->ia_addr.sin6_addr.s6_addr[8],
    251  1.64    rpaulo 			    ret, 8)) {
    252  1.64    rpaulo 				badid = 1;
    253  1.64    rpaulo 				break;
    254  1.64    rpaulo 			}
    255  1.64    rpaulo 		}
    256  1.64    rpaulo 	}
    257  1.64    rpaulo 
    258  1.64    rpaulo 	/*
    259  1.64    rpaulo 	 * In the event that an unacceptable identifier has been generated,
    260  1.64    rpaulo 	 * restart the process, using the right-most 64 bits of the MD5 digest
    261  1.64    rpaulo 	 * obtained in place of the history value.
    262  1.64    rpaulo 	 */
    263  1.64    rpaulo 	if (badid) {
    264  1.64    rpaulo 		/* for debugging purposes only */
    265  1.64    rpaulo #if 0
    266  1.64    rpaulo 		{
    267  1.64    rpaulo 			int i;
    268  1.64    rpaulo 
    269  1.64    rpaulo 			printf("unacceptable random ID: ");
    270  1.64    rpaulo 			for (i = 0; i < 16; i++)
    271  1.64    rpaulo 				printf("%02x", digest[i]);
    272  1.64    rpaulo 			printf("\n");
    273  1.64    rpaulo 		}
    274  1.64    rpaulo #endif
    275  1.64    rpaulo 
    276  1.64    rpaulo 		if (++retry < GEN_TEMPID_RETRY_MAX) {
    277  1.64    rpaulo 			memcpy(seed, &digest[8], 8);
    278  1.64    rpaulo 			goto again;
    279  1.64    rpaulo 		} else {
    280  1.64    rpaulo 			/*
    281  1.64    rpaulo 			 * We're so unlucky.  Give up for now, and return
    282  1.64    rpaulo 			 * all 0 IDs to tell the caller not to make a
    283  1.64    rpaulo 			 * temporary address.
    284  1.64    rpaulo 			 */
    285  1.64    rpaulo 			nd6log((LOG_NOTICE,
    286  1.64    rpaulo 			    "generate_tmp_ifid: never found a good ID\n"));
    287  1.64    rpaulo 			memset(ret, 0, 8);
    288  1.64    rpaulo 		}
    289  1.64    rpaulo 	}
    290  1.64    rpaulo 
    291  1.64    rpaulo 	/*
    292  1.64    rpaulo 	 * draft-ietf-ipngwg-temp-addresses-v2-00.txt 3.2.1. (6)
    293  1.64    rpaulo 	 * Take the rightmost 64-bits of the MD5 digest and save them in
    294  1.64    rpaulo 	 * stable storage as the history value to be used in the next
    295  1.64    rpaulo 	 * iteration of the algorithm.
    296  1.64    rpaulo 	 */
    297  1.64    rpaulo 	memcpy(seed0, &digest[8], 8);
    298  1.64    rpaulo 
    299  1.64    rpaulo 	/* for debugging purposes only */
    300  1.64    rpaulo #if 0
    301  1.64    rpaulo 	{
    302  1.64    rpaulo 		int i;
    303  1.64    rpaulo 
    304  1.64    rpaulo 		printf("to: ");
    305  1.64    rpaulo 		for (i = 0; i < 16; i++)
    306  1.64    rpaulo 			printf("%02x", digest[i]);
    307  1.64    rpaulo 		printf("\n");
    308  1.64    rpaulo 	}
    309  1.64    rpaulo #endif
    310  1.64    rpaulo 
    311  1.64    rpaulo 	return 0;
    312  1.64    rpaulo }
    313  1.81    dyoung 
    314  1.25    itojun /*
    315  1.25    itojun  * Get interface identifier for the specified interface.
    316  1.71  christos  *
    317  1.71  christos  * in6 - upper 64bits are preserved
    318  1.25    itojun  */
    319  1.64    rpaulo int
    320  1.71  christos in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
    321   1.2    itojun {
    322  1.25    itojun 	struct ifaddr *ifa;
    323  1.81    dyoung 	const struct sockaddr_dl *sdl = NULL, *tsdl;
    324  1.73    dyoung 	const char *addr;
    325  1.25    itojun 	size_t addrlen;
    326  1.25    itojun 	static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    327  1.25    itojun 	static u_int8_t allone[8] =
    328  1.25    itojun 		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    329  1.25    itojun 
    330  1.76    dyoung 	IFADDR_FOREACH(ifa, ifp) {
    331  1.25    itojun 		if (ifa->ifa_addr->sa_family != AF_LINK)
    332  1.25    itojun 			continue;
    333  1.81    dyoung 		tsdl = satocsdl(ifa->ifa_addr);
    334  1.81    dyoung 		if (tsdl == NULL || tsdl->sdl_alen == 0)
    335  1.25    itojun 			continue;
    336  1.81    dyoung 		if (sdl == NULL || ifa == ifp->if_dl || ifa == ifp->if_hwdl)
    337  1.81    dyoung 			sdl = tsdl;
    338  1.81    dyoung 		if (ifa == ifp->if_hwdl)
    339  1.81    dyoung 			break;
    340  1.25    itojun 	}
    341  1.25    itojun 
    342  1.81    dyoung 	if (sdl == NULL)
    343  1.81    dyoung 		return -1;
    344  1.25    itojun 
    345  1.73    dyoung 	addr = CLLADDR(sdl);
    346  1.25    itojun 	addrlen = sdl->sdl_alen;
    347  1.25    itojun 
    348  1.48    itojun 	switch (ifp->if_type) {
    349  1.48    itojun 	case IFT_IEEE1394:
    350  1.48    itojun 	case IFT_IEEE80211:
    351  1.48    itojun 		/* IEEE1394 uses 16byte length address starting with EUI64 */
    352  1.48    itojun 		if (addrlen > 8)
    353  1.48    itojun 			addrlen = 8;
    354  1.48    itojun 		break;
    355  1.48    itojun 	default:
    356  1.48    itojun 		break;
    357  1.48    itojun 	}
    358  1.48    itojun 
    359  1.25    itojun 	/* get EUI64 */
    360  1.25    itojun 	switch (ifp->if_type) {
    361  1.48    itojun 	/* IEEE802/EUI64 cases - what others? */
    362  1.25    itojun 	case IFT_ETHER:
    363  1.25    itojun 	case IFT_FDDI:
    364  1.25    itojun 	case IFT_ATM:
    365  1.32      onoe 	case IFT_IEEE1394:
    366  1.48    itojun 	case IFT_IEEE80211:
    367  1.25    itojun 		/* look at IEEE802/EUI64 only */
    368  1.25    itojun 		if (addrlen != 8 && addrlen != 6)
    369  1.25    itojun 			return -1;
    370  1.13    itojun 
    371  1.25    itojun 		/*
    372  1.25    itojun 		 * check for invalid MAC address - on bsdi, we see it a lot
    373  1.25    itojun 		 * since wildboar configures all-zero MAC on pccard before
    374  1.25    itojun 		 * card insertion.
    375  1.25    itojun 		 */
    376  1.64    rpaulo 		if (memcmp(addr, allzero, addrlen) == 0)
    377  1.25    itojun 			return -1;
    378  1.64    rpaulo 		if (memcmp(addr, allone, addrlen) == 0)
    379  1.25    itojun 			return -1;
    380  1.25    itojun 
    381  1.25    itojun 		/* make EUI64 address */
    382  1.25    itojun 		if (addrlen == 8)
    383  1.64    rpaulo 			memcpy(&in6->s6_addr[8], addr, 8);
    384  1.25    itojun 		else if (addrlen == 6) {
    385  1.25    itojun 			in6->s6_addr[8] = addr[0];
    386  1.25    itojun 			in6->s6_addr[9] = addr[1];
    387  1.25    itojun 			in6->s6_addr[10] = addr[2];
    388  1.25    itojun 			in6->s6_addr[11] = 0xff;
    389  1.26    itojun 			in6->s6_addr[12] = 0xfe;
    390  1.25    itojun 			in6->s6_addr[13] = addr[3];
    391  1.25    itojun 			in6->s6_addr[14] = addr[4];
    392  1.25    itojun 			in6->s6_addr[15] = addr[5];
    393  1.25    itojun 		}
    394   1.7    itojun 		break;
    395  1.25    itojun 
    396  1.25    itojun 	case IFT_ARCNET:
    397  1.25    itojun 		if (addrlen != 1)
    398  1.25    itojun 			return -1;
    399  1.25    itojun 		if (!addr[0])
    400  1.25    itojun 			return -1;
    401  1.25    itojun 
    402  1.64    rpaulo 		memset(&in6->s6_addr[8], 0, 8);
    403  1.25    itojun 		in6->s6_addr[15] = addr[0];
    404  1.25    itojun 
    405  1.27    itojun 		/*
    406  1.27    itojun 		 * due to insufficient bitwidth, we mark it local.
    407  1.27    itojun 		 */
    408  1.25    itojun 		in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
    409  1.25    itojun 		in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
    410   1.7    itojun 		break;
    411  1.25    itojun 
    412  1.25    itojun 	case IFT_GIF:
    413  1.25    itojun #ifdef IFT_STF
    414  1.25    itojun 	case IFT_STF:
    415  1.25    itojun #endif
    416  1.25    itojun 		/*
    417  1.34    itojun 		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
    418  1.27    itojun 		 * however, IPv4 address is not very suitable as unique
    419  1.27    itojun 		 * identifier source (can be renumbered).
    420  1.27    itojun 		 * we don't do this.
    421  1.25    itojun 		 */
    422  1.25    itojun 		return -1;
    423  1.25    itojun 
    424   1.7    itojun 	default:
    425  1.25    itojun 		return -1;
    426  1.25    itojun 	}
    427  1.25    itojun 
    428  1.25    itojun 	/* sanity check: g bit must not indicate "group" */
    429  1.25    itojun 	if (EUI64_GROUP(in6))
    430  1.25    itojun 		return -1;
    431  1.25    itojun 
    432  1.25    itojun 	/* convert EUI64 into IPv6 interface identifier */
    433  1.25    itojun 	EUI64_TO_IFID(in6);
    434  1.25    itojun 
    435  1.25    itojun 	/*
    436  1.25    itojun 	 * sanity check: ifid must not be all zero, avoid conflict with
    437  1.25    itojun 	 * subnet router anycast
    438  1.25    itojun 	 */
    439  1.25    itojun 	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
    440  1.64    rpaulo 	    memcmp(&in6->s6_addr[9], allzero, 7) == 0) {
    441  1.25    itojun 		return -1;
    442   1.7    itojun 	}
    443   1.7    itojun 
    444   1.7    itojun 	return 0;
    445   1.2    itojun }
    446   1.2    itojun 
    447   1.2    itojun /*
    448  1.25    itojun  * Get interface identifier for the specified interface.  If it is not
    449  1.25    itojun  * available on ifp0, borrow interface identifier from other information
    450  1.25    itojun  * sources.
    451  1.71  christos  *
    452  1.71  christos  * altifp - secondary EUI64 source
    453  1.13    itojun  */
    454  1.13    itojun static int
    455  1.71  christos get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
    456  1.71  christos 	struct in6_addr *in6)
    457  1.13    itojun {
    458  1.25    itojun 	struct ifnet *ifp;
    459  1.25    itojun 
    460  1.25    itojun 	/* first, try to get it from the interface itself */
    461  1.64    rpaulo 	if (in6_get_hw_ifid(ifp0, in6) == 0) {
    462  1.34    itojun 		nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
    463  1.34    itojun 		    if_name(ifp0)));
    464  1.25    itojun 		goto success;
    465  1.25    itojun 	}
    466  1.25    itojun 
    467  1.25    itojun 	/* try secondary EUI64 source. this basically is for ATM PVC */
    468  1.64    rpaulo 	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
    469  1.34    itojun 		nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
    470  1.34    itojun 		    if_name(ifp0), if_name(altifp)));
    471  1.25    itojun 		goto success;
    472  1.25    itojun 	}
    473  1.25    itojun 
    474  1.25    itojun 	/* next, try to get it from some other hardware interface */
    475  1.90     rmind 	IFNET_FOREACH(ifp) {
    476  1.25    itojun 		if (ifp == ifp0)
    477  1.25    itojun 			continue;
    478  1.64    rpaulo 		if (in6_get_hw_ifid(ifp, in6) != 0)
    479  1.25    itojun 			continue;
    480  1.27    itojun 
    481  1.25    itojun 		/*
    482  1.25    itojun 		 * to borrow ifid from other interface, ifid needs to be
    483  1.25    itojun 		 * globally unique
    484  1.25    itojun 		 */
    485  1.25    itojun 		if (IFID_UNIVERSAL(in6)) {
    486  1.34    itojun 			nd6log((LOG_DEBUG,
    487  1.34    itojun 			    "%s: borrow interface identifier from %s\n",
    488  1.34    itojun 			    if_name(ifp0), if_name(ifp)));
    489  1.25    itojun 			goto success;
    490  1.25    itojun 		}
    491  1.25    itojun 	}
    492  1.13    itojun 
    493  1.64    rpaulo #if 0
    494  1.64    rpaulo 	/* get from hostid - only for certain architectures */
    495  1.64    rpaulo 	if (get_hostid_ifid(ifp, in6) == 0) {
    496  1.64    rpaulo 		nd6log((LOG_DEBUG,
    497  1.64    rpaulo 		    "%s: interface identifier generated by hostid\n",
    498  1.64    rpaulo 		    if_name(ifp0)));
    499  1.64    rpaulo 		goto success;
    500  1.64    rpaulo 	}
    501  1.64    rpaulo #endif
    502  1.64    rpaulo 
    503  1.25    itojun 	/* last resort: get from random number source */
    504  1.25    itojun 	if (get_rand_ifid(ifp, in6) == 0) {
    505  1.34    itojun 		nd6log((LOG_DEBUG,
    506  1.34    itojun 		    "%s: interface identifier generated by random number\n",
    507  1.34    itojun 		    if_name(ifp0)));
    508  1.25    itojun 		goto success;
    509  1.25    itojun 	}
    510  1.13    itojun 
    511  1.31    itojun 	printf("%s: failed to get interface identifier\n", if_name(ifp0));
    512  1.25    itojun 	return -1;
    513  1.13    itojun 
    514  1.25    itojun success:
    515  1.47    itojun 	nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
    516  1.47    itojun 	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
    517  1.47    itojun 	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
    518  1.47    itojun 	    in6->s6_addr[14], in6->s6_addr[15]));
    519  1.13    itojun 	return 0;
    520  1.13    itojun }
    521  1.13    itojun 
    522  1.71  christos /*
    523  1.71  christos  * altifp - secondary EUI64 source
    524  1.71  christos  */
    525  1.71  christos 
    526  1.25    itojun static int
    527  1.71  christos in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
    528  1.48    itojun {
    529  1.89    martin 	struct in6_ifaddr *ia __diagused;
    530  1.48    itojun 	struct in6_aliasreq ifra;
    531  1.92      matt 	struct nd_prefixctl prc0;
    532  1.48    itojun 	int i, error;
    533  1.25    itojun 
    534  1.25    itojun 	/*
    535  1.48    itojun 	 * configure link-local address.
    536  1.25    itojun 	 */
    537  1.64    rpaulo 	memset(&ifra, 0, sizeof(ifra));
    538   1.2    itojun 
    539  1.25    itojun 	/*
    540  1.48    itojun 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
    541  1.48    itojun 	 * for safety.
    542  1.25    itojun 	 */
    543  1.48    itojun 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
    544   1.2    itojun 
    545  1.48    itojun 	ifra.ifra_addr.sin6_family = AF_INET6;
    546  1.48    itojun 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
    547  1.63    rpaulo 	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
    548  1.48    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
    549  1.48    itojun 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    550  1.48    itojun 		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
    551  1.48    itojun 		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
    552  1.48    itojun 	} else {
    553  1.48    itojun 		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
    554  1.48    itojun 			nd6log((LOG_ERR,
    555  1.48    itojun 			    "%s: no ifid available\n", if_name(ifp)));
    556  1.69    dyoung 			return -1;
    557  1.25    itojun 		}
    558  1.25    itojun 	}
    559  1.63    rpaulo 	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
    560  1.69    dyoung 		return -1;
    561  1.25    itojun 
    562  1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask64, 0, 0, 0);
    563  1.48    itojun 	/* link-local addresses should NEVER expire. */
    564  1.48    itojun 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    565  1.48    itojun 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    566  1.25    itojun 
    567  1.48    itojun 	/*
    568  1.48    itojun 	 * Now call in6_update_ifa() to do a bunch of procedures to configure
    569  1.70    dyoung 	 * a link-local address. We can set the 3rd argument to NULL, because
    570  1.48    itojun 	 * we know there's no other link-local address on the interface
    571  1.48    itojun 	 * and therefore we are adding one (instead of updating one).
    572  1.48    itojun 	 */
    573  1.64    rpaulo 	if ((error = in6_update_ifa(ifp, &ifra, NULL,
    574  1.64    rpaulo 	    IN6_IFAUPDATE_DADDELAY)) != 0) {
    575  1.25    itojun 		/*
    576  1.48    itojun 		 * XXX: When the interface does not support IPv6, this call
    577  1.82    dyoung 		 * would fail in the SIOCINITIFADDR ioctl.  I believe the
    578  1.48    itojun 		 * notification is rather confusing in this case, so just
    579  1.48    itojun 		 * suppress it.  (jinmei (at) kame.net 20010130)
    580  1.25    itojun 		 */
    581  1.48    itojun 		if (error != EAFNOSUPPORT)
    582  1.49    itojun 			nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
    583  1.48    itojun 			    "configure a link-local address on %s "
    584  1.48    itojun 			    "(errno=%d)\n",
    585  1.49    itojun 			    if_name(ifp), error));
    586  1.69    dyoung 		return -1;
    587  1.25    itojun 	}
    588  1.25    itojun 
    589  1.48    itojun 	ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
    590  1.88       mrg 	KASSERTMSG(ia, "ia == NULL in in6_ifattach_linklocal");
    591  1.25    itojun 
    592  1.48    itojun 	/*
    593  1.48    itojun 	 * Make the link-local prefix (fe80::/64%link) as on-link.
    594  1.48    itojun 	 * Since we'd like to manage prefixes separately from addresses,
    595  1.48    itojun 	 * we make an ND6 prefix structure for the link-local prefix,
    596  1.48    itojun 	 * and add it to the prefix list as a never-expire prefix.
    597  1.48    itojun 	 * XXX: this change might affect some existing code base...
    598  1.48    itojun 	 */
    599  1.92      matt 	memset(&prc0, 0, sizeof(prc0));
    600  1.92      matt 	prc0.ndprc_ifp = ifp;
    601  1.48    itojun 	/* this should be 64 at this moment. */
    602  1.92      matt 	prc0.ndprc_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
    603  1.92      matt 	prc0.ndprc_prefix = ifra.ifra_addr;
    604  1.48    itojun 	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
    605  1.48    itojun 	for (i = 0; i < 4; i++) {
    606  1.92      matt 		prc0.ndprc_prefix.sin6_addr.s6_addr32[i] &=
    607  1.48    itojun 		    in6mask64.s6_addr32[i];
    608  1.48    itojun 	}
    609  1.48    itojun 	/*
    610  1.48    itojun 	 * Initialize parameters.  The link-local prefix must always be
    611  1.48    itojun 	 * on-link, and its lifetimes never expire.
    612  1.48    itojun 	 */
    613  1.92      matt 	prc0.ndprc_raf_onlink = 1;
    614  1.92      matt 	prc0.ndprc_raf_auto = 1;	/* probably meaningless */
    615  1.92      matt 	prc0.ndprc_vltime = ND6_INFINITE_LIFETIME;
    616  1.92      matt 	prc0.ndprc_pltime = ND6_INFINITE_LIFETIME;
    617  1.48    itojun 	/*
    618  1.48    itojun 	 * Since there is no other link-local addresses, nd6_prefix_lookup()
    619  1.48    itojun 	 * probably returns NULL.  However, we cannot always expect the result.
    620  1.48    itojun 	 * For example, if we first remove the (only) existing link-local
    621  1.48    itojun 	 * address, and then reconfigure another one, the prefix is still
    622  1.48    itojun 	 * valid with referring to the old link-local address.
    623  1.48    itojun 	 */
    624  1.92      matt 	if (nd6_prefix_lookup(&prc0) == NULL) {
    625  1.92      matt 		if ((error = nd6_prelist_add(&prc0, NULL, NULL)) != 0)
    626  1.69    dyoung 			return error;
    627  1.25    itojun 	}
    628  1.25    itojun 
    629  1.25    itojun 	return 0;
    630  1.25    itojun }
    631  1.25    itojun 
    632  1.71  christos /*
    633  1.71  christos  * ifp - mut be IFT_LOOP
    634  1.71  christos  */
    635  1.71  christos 
    636  1.25    itojun static int
    637  1.71  christos in6_ifattach_loopback(struct ifnet *ifp)
    638  1.25    itojun {
    639  1.48    itojun 	struct in6_aliasreq ifra;
    640  1.48    itojun 	int error;
    641  1.48    itojun 
    642  1.64    rpaulo 	memset(&ifra, 0, sizeof(ifra));
    643  1.25    itojun 
    644  1.25    itojun 	/*
    645  1.48    itojun 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
    646  1.48    itojun 	 * for safety.
    647  1.25    itojun 	 */
    648  1.48    itojun 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
    649  1.48    itojun 
    650  1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_prefixmask, &in6mask128, 0, 0, 0);
    651  1.25    itojun 
    652  1.25    itojun 	/*
    653  1.25    itojun 	 * Always initialize ia_dstaddr (= broadcast address) to loopback
    654  1.48    itojun 	 * address.  Follows IPv4 practice - see in_ifinit().
    655  1.48    itojun 	 */
    656  1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_dstaddr, &in6addr_loopback, 0, 0, 0);
    657  1.48    itojun 
    658  1.75    dyoung 	sockaddr_in6_init(&ifra.ifra_addr, &in6addr_loopback, 0, 0, 0);
    659  1.48    itojun 
    660  1.48    itojun 	/* the loopback  address should NEVER expire. */
    661  1.48    itojun 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    662  1.48    itojun 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    663  1.48    itojun 
    664  1.48    itojun 	/* we don't need to perform DAD on loopback interfaces. */
    665  1.48    itojun 	ifra.ifra_flags |= IN6_IFF_NODAD;
    666  1.48    itojun 
    667  1.48    itojun 	/*
    668  1.48    itojun 	 * We are sure that this is a newly assigned address, so we can set
    669  1.48    itojun 	 * NULL to the 3rd arg.
    670  1.48    itojun 	 */
    671  1.64    rpaulo 	if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
    672  1.49    itojun 		nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
    673  1.48    itojun 		    "the loopback address on %s (errno=%d)\n",
    674  1.49    itojun 		    if_name(ifp), error));
    675  1.69    dyoung 		return -1;
    676  1.48    itojun 	}
    677  1.25    itojun 
    678  1.48    itojun 	return 0;
    679  1.48    itojun }
    680  1.48    itojun 
    681  1.48    itojun /*
    682  1.48    itojun  * compute NI group address, based on the current hostname setting.
    683  1.48    itojun  * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
    684  1.48    itojun  *
    685  1.48    itojun  * when ifp == NULL, the caller is responsible for filling scopeid.
    686  1.48    itojun  */
    687  1.48    itojun int
    688  1.71  christos in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
    689  1.71  christos 	struct sockaddr_in6 *sa6)
    690  1.48    itojun {
    691  1.48    itojun 	const char *p;
    692  1.52    itojun 	u_int8_t *q;
    693  1.48    itojun 	MD5_CTX ctxt;
    694  1.48    itojun 	u_int8_t digest[16];
    695  1.50    itojun 	u_int8_t l;
    696  1.50    itojun 	u_int8_t n[64];	/* a single label must not exceed 63 chars */
    697  1.25    itojun 
    698  1.48    itojun 	if (!namelen || !name)
    699  1.25    itojun 		return -1;
    700  1.48    itojun 
    701  1.48    itojun 	p = name;
    702  1.48    itojun 	while (p && *p && *p != '.' && p - name < namelen)
    703  1.48    itojun 		p++;
    704  1.48    itojun 	if (p - name > sizeof(n) - 1)
    705  1.48    itojun 		return -1;	/* label too long */
    706  1.48    itojun 	l = p - name;
    707  1.50    itojun 	strncpy((char *)n, name, l);
    708  1.48    itojun 	n[(int)l] = '\0';
    709  1.48    itojun 	for (q = n; *q; q++) {
    710  1.48    itojun 		if ('A' <= *q && *q <= 'Z')
    711  1.48    itojun 			*q = *q - 'A' + 'a';
    712   1.2    itojun 	}
    713  1.25    itojun 
    714  1.48    itojun 	/* generate 8 bytes of pseudo-random value. */
    715  1.64    rpaulo 	memset(&ctxt, 0, sizeof(ctxt));
    716  1.48    itojun 	MD5Init(&ctxt);
    717  1.48    itojun 	MD5Update(&ctxt, &l, sizeof(l));
    718  1.48    itojun 	MD5Update(&ctxt, n, l);
    719  1.48    itojun 	MD5Final(digest, &ctxt);
    720  1.48    itojun 
    721  1.64    rpaulo 	memset(sa6, 0, sizeof(*sa6));
    722  1.48    itojun 	sa6->sin6_family = AF_INET6;
    723  1.48    itojun 	sa6->sin6_len = sizeof(*sa6);
    724  1.48    itojun 	sa6->sin6_addr.s6_addr16[0] = htons(0xff02);
    725  1.48    itojun 	sa6->sin6_addr.s6_addr8[11] = 2;
    726  1.64    rpaulo 	memcpy(&sa6->sin6_addr.s6_addr32[3], digest,
    727  1.48    itojun 	    sizeof(sa6->sin6_addr.s6_addr32[3]));
    728  1.63    rpaulo 	if (in6_setscope(&sa6->sin6_addr, ifp, NULL))
    729  1.69    dyoung 		return -1; /* XXX: should not fail */
    730  1.48    itojun 
    731  1.25    itojun 	return 0;
    732   1.2    itojun }
    733   1.2    itojun 
    734  1.17    itojun /*
    735  1.17    itojun  * XXX multiple loopback interface needs more care.  for instance,
    736  1.17    itojun  * nodelocal address needs to be configured onto only one of them.
    737  1.25    itojun  * XXX multiple link-local address case
    738  1.71  christos  *
    739  1.71  christos  * altifp - secondary EUI64 source
    740  1.17    itojun  */
    741   1.2    itojun void
    742  1.71  christos in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
    743   1.2    itojun {
    744  1.85  christos 	struct in6_ifaddr *ia;
    745  1.85  christos 	struct in6_addr in6;
    746  1.13    itojun 
    747  1.38    itojun 	/* some of the interfaces are inherently not IPv6 capable */
    748  1.38    itojun 	switch (ifp->if_type) {
    749  1.42    itojun 	case IFT_BRIDGE:
    750  1.59  christos #ifdef IFT_PFLOG
    751  1.58    itojun 	case IFT_PFLOG:
    752  1.59  christos #endif
    753  1.59  christos #ifdef IFT_PFSYNC
    754  1.58    itojun 	case IFT_PFSYNC:
    755  1.59  christos #endif
    756  1.91       roy 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
    757  1.91       roy 		ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
    758  1.42    itojun 		return;
    759  1.38    itojun 	}
    760  1.38    itojun 
    761  1.46    itojun 	/*
    762  1.46    itojun 	 * if link mtu is too small, don't try to configure IPv6.
    763  1.46    itojun 	 * remember there could be some link-layer that has special
    764  1.46    itojun 	 * fragmentation logic.
    765  1.46    itojun 	 */
    766  1.49    itojun 	if (ifp->if_mtu < IPV6_MMTU) {
    767  1.49    itojun 		nd6log((LOG_INFO, "in6_ifattach: "
    768  1.49    itojun 		    "%s has too small MTU, IPv6 not enabled\n",
    769  1.49    itojun 		    if_name(ifp)));
    770  1.46    itojun 		return;
    771  1.49    itojun 	}
    772  1.46    itojun 
    773  1.37    itojun 	/* create a multicast kludge storage (if we have not had one) */
    774  1.37    itojun 	in6_createmkludge(ifp);
    775   1.2    itojun 
    776   1.2    itojun 	/*
    777  1.25    itojun 	 * quirks based on interface type
    778   1.2    itojun 	 */
    779  1.25    itojun 	switch (ifp->if_type) {
    780  1.25    itojun #ifdef IFT_STF
    781  1.25    itojun 	case IFT_STF:
    782  1.25    itojun 		/*
    783  1.38    itojun 		 * 6to4 interface is a very special kind of beast.
    784  1.38    itojun 		 * no multicast, no linklocal.  RFC2529 specifies how to make
    785  1.38    itojun 		 * linklocals for 6to4 interface, but there's no use and
    786  1.38    itojun 		 * it is rather harmful to have one.
    787  1.25    itojun 		 */
    788  1.91       roy 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
    789  1.46    itojun 		return;
    790  1.25    itojun #endif
    791  1.65  liamjfoy 	case IFT_CARP:
    792  1.65  liamjfoy 		return;
    793  1.25    itojun 	default:
    794  1.25    itojun 		break;
    795   1.7    itojun 	}
    796   1.2    itojun 
    797   1.2    itojun 	/*
    798  1.25    itojun 	 * usually, we require multicast capability to the interface
    799   1.2    itojun 	 */
    800  1.25    itojun 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
    801  1.49    itojun 		nd6log((LOG_INFO, "in6_ifattach: "
    802  1.38    itojun 		    "%s is not multicast capable, IPv6 not enabled\n",
    803  1.49    itojun 		    if_name(ifp)));
    804  1.25    itojun 		return;
    805  1.25    itojun 	}
    806  1.15   thorpej 
    807   1.2    itojun 	/*
    808  1.48    itojun 	 * assign loopback address for loopback interface.
    809  1.48    itojun 	 * XXX multiple loopback interface case.
    810   1.2    itojun 	 */
    811  1.48    itojun 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    812  1.48    itojun 		in6 = in6addr_loopback;
    813  1.25    itojun 		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
    814  1.25    itojun 			if (in6_ifattach_loopback(ifp) != 0)
    815  1.25    itojun 				return;
    816  1.25    itojun 		}
    817  1.25    itojun 	}
    818   1.2    itojun 
    819   1.2    itojun 	/*
    820  1.48    itojun 	 * assign a link-local address, if there's none.
    821   1.2    itojun 	 */
    822  1.91       roy 	if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
    823  1.91       roy 	    ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)
    824  1.91       roy 	{
    825  1.48    itojun 		ia = in6ifa_ifpforlinklocal(ifp, 0);
    826  1.70    dyoung 		if (ia == NULL && in6_ifattach_linklocal(ifp, altifp) != 0) {
    827  1.70    dyoung 			printf("%s: cannot assign link-local address\n",
    828  1.70    dyoung 			    ifp->if_xname);
    829   1.2    itojun 		}
    830   1.2    itojun 	}
    831   1.2    itojun }
    832   1.2    itojun 
    833  1.17    itojun /*
    834  1.17    itojun  * NOTE: in6_ifdetach() does not support loopback if at this moment.
    835  1.41    itojun  * We don't need this function in bsdi, because interfaces are never removed
    836  1.41    itojun  * from the ifnet list in bsdi.
    837  1.17    itojun  */
    838   1.2    itojun void
    839  1.71  christos in6_ifdetach(struct ifnet *ifp)
    840   1.2    itojun {
    841   1.2    itojun 	struct in6_ifaddr *ia, *oia;
    842  1.30    itojun 	struct ifaddr *ifa, *next;
    843   1.2    itojun 	struct rtentry *rt;
    844   1.2    itojun 	short rtflags;
    845  1.48    itojun 	struct in6_multi_mship *imm;
    846  1.55    itojun 
    847  1.55    itojun 	/* remove ip6_mrouter stuff */
    848  1.55    itojun 	ip6_mrouter_detach(ifp);
    849  1.18    itojun 
    850  1.18    itojun 	/* remove neighbor management table */
    851  1.95    martin 	nd6_purge(ifp, NULL);
    852  1.18    itojun 
    853  1.76    dyoung 	/* XXX this code is duplicated in in6_purgeif() --dyoung */
    854  1.30    itojun 	/* nuke any of IPv6 addresses we have */
    855  1.78    dyoung 	if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr);
    856  1.30    itojun 
    857  1.76    dyoung 	/* XXX isn't this code is redundant, given the above? --dyoung */
    858  1.76    dyoung 	/* XXX doesn't this code replicate code in in6_purgeaddr() ? --dyoung */
    859  1.30    itojun 	/* undo everything done by in6_ifattach(), just in case */
    860  1.77    dyoung 	for (ifa = IFADDR_FIRST(ifp); ifa != NULL; ifa = next) {
    861  1.77    dyoung 		next = IFADDR_NEXT(ifa);
    862  1.30    itojun 
    863   1.2    itojun 		if (ifa->ifa_addr->sa_family != AF_INET6
    864   1.2    itojun 		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
    865   1.2    itojun 			continue;
    866   1.2    itojun 		}
    867   1.2    itojun 
    868   1.2    itojun 		ia = (struct in6_ifaddr *)ifa;
    869   1.2    itojun 
    870  1.48    itojun 		/*
    871  1.48    itojun 		 * leave from multicast groups we have joined for the interface
    872  1.48    itojun 		 */
    873  1.68    dyoung 		while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
    874  1.48    itojun 			LIST_REMOVE(imm, i6mm_chain);
    875  1.48    itojun 			in6_leavegroup(imm);
    876  1.48    itojun 		}
    877  1.48    itojun 
    878   1.2    itojun 		/* remove from the routing table */
    879  1.48    itojun 		if ((ia->ia_flags & IFA_ROUTE) &&
    880  1.48    itojun 		    (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
    881   1.2    itojun 			rtflags = rt->rt_flags;
    882   1.2    itojun 			rtfree(rt);
    883  1.47    itojun 			rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
    884  1.47    itojun 			    (struct sockaddr *)&ia->ia_addr,
    885  1.47    itojun 			    (struct sockaddr *)&ia->ia_prefixmask,
    886  1.87  christos 			    rtflags, NULL);
    887   1.2    itojun 		}
    888   1.2    itojun 
    889   1.2    itojun 		/* remove from the linked list */
    890  1.79    dyoung 		ifa_remove(ifp, &ia->ia_ifa);
    891   1.2    itojun 
    892   1.2    itojun 		/* also remove from the IPv6 address chain(itojun&jinmei) */
    893   1.2    itojun 		oia = ia;
    894   1.2    itojun 		if (oia == (ia = in6_ifaddr))
    895   1.2    itojun 			in6_ifaddr = ia->ia_next;
    896   1.2    itojun 		else {
    897   1.2    itojun 			while (ia->ia_next && (ia->ia_next != oia))
    898   1.2    itojun 				ia = ia->ia_next;
    899   1.2    itojun 			if (ia->ia_next)
    900   1.2    itojun 				ia->ia_next = oia->ia_next;
    901  1.34    itojun 			else {
    902  1.48    itojun 				nd6log((LOG_ERR,
    903  1.47    itojun 				    "%s: didn't unlink in6ifaddr from list\n",
    904  1.47    itojun 				    if_name(ifp)));
    905  1.34    itojun 			}
    906   1.2    itojun 		}
    907   1.2    itojun 
    908  1.93     rmind 		ifafree(&oia->ia_ifa);
    909  1.16    itojun 	}
    910  1.16    itojun 
    911  1.17    itojun 	/* cleanup multicast address kludge table, if there is any */
    912  1.17    itojun 	in6_purgemkludge(ifp);
    913  1.30    itojun 
    914  1.41    itojun 	/*
    915  1.41    itojun 	 * remove neighbor management table.  we call it twice just to make
    916  1.41    itojun 	 * sure we nuke everything.  maybe we need just one call.
    917  1.41    itojun 	 * XXX: since the first call did not release addresses, some prefixes
    918  1.41    itojun 	 * might remain.  We should call nd6_purge() again to release the
    919  1.41    itojun 	 * prefixes after removing all addresses above.
    920  1.41    itojun 	 * (Or can we just delay calling nd6_purge until at this point?)
    921  1.41    itojun 	 */
    922  1.95    martin 	nd6_purge(ifp, NULL);
    923   1.2    itojun }
    924  1.64    rpaulo 
    925  1.64    rpaulo int
    926  1.71  christos in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
    927  1.71  christos 	const u_int8_t *baseid, int generate)
    928  1.64    rpaulo {
    929  1.64    rpaulo 	u_int8_t nullbuf[8];
    930  1.64    rpaulo 	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
    931  1.64    rpaulo 
    932  1.64    rpaulo 	memset(nullbuf, 0, sizeof(nullbuf));
    933  1.64    rpaulo 	if (memcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
    934  1.64    rpaulo 		/* we've never created a random ID.  Create a new one. */
    935  1.64    rpaulo 		generate = 1;
    936  1.64    rpaulo 	}
    937  1.64    rpaulo 
    938  1.64    rpaulo 	if (generate) {
    939  1.64    rpaulo 		memcpy(ndi->randomseed1, baseid, sizeof(ndi->randomseed1));
    940  1.64    rpaulo 
    941  1.64    rpaulo 		/* generate_tmp_ifid will update seedn and buf */
    942  1.64    rpaulo 		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
    943  1.64    rpaulo 		    ndi->randomid);
    944  1.64    rpaulo 	}
    945  1.64    rpaulo 	memcpy(retbuf, ndi->randomid, 8);
    946  1.64    rpaulo 	if (generate && memcmp(retbuf, nullbuf, sizeof(nullbuf)) == 0) {
    947  1.64    rpaulo 		/* generate_tmp_ifid could not found a good ID. */
    948  1.69    dyoung 		return -1;
    949  1.64    rpaulo 	}
    950  1.64    rpaulo 
    951  1.69    dyoung 	return 0;
    952  1.64    rpaulo }
    953  1.64    rpaulo 
    954  1.64    rpaulo void
    955  1.67  christos in6_tmpaddrtimer(void *ignored_arg)
    956  1.64    rpaulo {
    957  1.64    rpaulo 	struct nd_ifinfo *ndi;
    958  1.64    rpaulo 	u_int8_t nullbuf[8];
    959  1.64    rpaulo 	struct ifnet *ifp;
    960  1.80        ad 
    961  1.80        ad 	mutex_enter(softnet_lock);
    962  1.80        ad 	KERNEL_LOCK(1, NULL);
    963  1.64    rpaulo 
    964  1.64    rpaulo 	callout_reset(&in6_tmpaddrtimer_ch,
    965  1.64    rpaulo 	    (ip6_temp_preferred_lifetime - ip6_desync_factor -
    966  1.64    rpaulo 	    ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL);
    967  1.64    rpaulo 
    968  1.64    rpaulo 	memset(nullbuf, 0, sizeof(nullbuf));
    969  1.90     rmind 	IFNET_FOREACH(ifp) {
    970  1.64    rpaulo 		ndi = ND_IFINFO(ifp);
    971  1.64    rpaulo 		if (memcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
    972  1.64    rpaulo 			/*
    973  1.64    rpaulo 			 * We've been generating a random ID on this interface.
    974  1.64    rpaulo 			 * Create a new one.
    975  1.64    rpaulo 			 */
    976  1.64    rpaulo 			(void)generate_tmp_ifid(ndi->randomseed0,
    977  1.64    rpaulo 			    ndi->randomseed1, ndi->randomid);
    978  1.64    rpaulo 		}
    979  1.64    rpaulo 	}
    980  1.64    rpaulo 
    981  1.80        ad 	KERNEL_UNLOCK_ONE(NULL);
    982  1.80        ad 	mutex_exit(softnet_lock);
    983  1.64    rpaulo }
    984