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