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