Home | History | Annotate | Line # | Download | only in netinet6
in6_ifattach.c revision 1.24
      1  1.24   itojun /*	$NetBSD: in6_ifattach.c,v 1.24 2000/04/10 15:45:24 itojun Exp $	*/
      2  1.21   itojun /*	$KAME: in6_ifattach.c,v 1.39 2000/03/02 09:24:45 itojun 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.2   itojun 
     33   1.2   itojun #include <sys/param.h>
     34   1.2   itojun #include <sys/systm.h>
     35   1.2   itojun #include <sys/malloc.h>
     36   1.2   itojun #include <sys/socket.h>
     37   1.2   itojun #include <sys/sockio.h>
     38  1.13   itojun #include <sys/kernel.h>
     39  1.13   itojun #include <sys/md5.h>
     40   1.2   itojun 
     41   1.2   itojun #include <net/if.h>
     42   1.2   itojun #include <net/if_dl.h>
     43   1.2   itojun #include <net/if_types.h>
     44   1.2   itojun #include <net/route.h>
     45   1.2   itojun 
     46   1.2   itojun #include <netinet/in.h>
     47   1.2   itojun #include <netinet/in_var.h>
     48   1.2   itojun 
     49  1.19   itojun #include <netinet/ip6.h>
     50   1.2   itojun #include <netinet6/ip6_var.h>
     51   1.2   itojun #include <netinet6/in6_ifattach.h>
     52  1.19   itojun #include <netinet/ip6.h>
     53   1.2   itojun #include <netinet6/ip6_var.h>
     54   1.2   itojun #include <netinet6/nd6.h>
     55   1.2   itojun 
     56  1.13   itojun #include <net/net_osdep.h>
     57  1.13   itojun 
     58   1.2   itojun static	struct in6_addr llsol;
     59   1.2   itojun 
     60  1.13   itojun struct in6_ifstat **in6_ifstat = NULL;
     61  1.13   itojun struct icmp6_ifstat **icmp6_ifstat = NULL;
     62  1.13   itojun size_t in6_ifstatmax = 0;
     63  1.13   itojun size_t icmp6_ifstatmax = 0;
     64   1.2   itojun unsigned long in6_maxmtu = 0;
     65   1.2   itojun 
     66   1.2   itojun int found_first_ifid = 0;
     67   1.2   itojun #define IFID_LEN 8
     68  1.13   itojun static u_int8_t first_ifid[IFID_LEN];
     69   1.2   itojun 
     70   1.7   itojun static int laddr_to_eui64 __P((u_int8_t *, u_int8_t *, size_t));
     71  1.13   itojun static int gen_rand_eui64 __P((u_int8_t *));
     72   1.2   itojun 
     73   1.7   itojun static int
     74   1.7   itojun laddr_to_eui64(dst, src, len)
     75   1.2   itojun 	u_int8_t *dst;
     76   1.2   itojun 	u_int8_t *src;
     77   1.7   itojun 	size_t len;
     78   1.2   itojun {
     79  1.13   itojun 	static u_int8_t zero[8];
     80  1.13   itojun 
     81  1.13   itojun 	bzero(zero, sizeof(zero));
     82  1.13   itojun 
     83   1.7   itojun 	switch (len) {
     84   1.7   itojun 	case 6:
     85  1.13   itojun 		if (bcmp(zero, src, 6) == 0)
     86  1.13   itojun 			return EINVAL;
     87   1.7   itojun 		dst[0] = src[0];
     88   1.7   itojun 		dst[1] = src[1];
     89   1.7   itojun 		dst[2] = src[2];
     90   1.7   itojun 		dst[3] = 0xff;
     91   1.7   itojun 		dst[4] = 0xfe;
     92   1.7   itojun 		dst[5] = src[3];
     93   1.7   itojun 		dst[6] = src[4];
     94   1.7   itojun 		dst[7] = src[5];
     95   1.7   itojun 		break;
     96   1.7   itojun 	case 8:
     97  1.13   itojun 		if (bcmp(zero, src, 8) == 0)
     98  1.13   itojun 			return EINVAL;
     99   1.7   itojun 		bcopy(src, dst, len);
    100   1.7   itojun 		break;
    101   1.7   itojun 	default:
    102   1.7   itojun 		return EINVAL;
    103   1.7   itojun 	}
    104   1.7   itojun 
    105   1.7   itojun 	return 0;
    106   1.2   itojun }
    107   1.2   itojun 
    108   1.2   itojun /*
    109  1.13   itojun  * Generate a last-resort interface identifier, when the machine has no
    110  1.13   itojun  * IEEE802/EUI64 address sources.
    111  1.13   itojun  * The address should be random, and should not change across reboot.
    112  1.13   itojun  */
    113  1.13   itojun static int
    114  1.13   itojun gen_rand_eui64(dst)
    115  1.13   itojun 	u_int8_t *dst;
    116  1.13   itojun {
    117  1.13   itojun 	MD5_CTX ctxt;
    118  1.13   itojun 	u_int8_t digest[16];
    119  1.13   itojun 
    120  1.13   itojun 	/* generate 8bytes of pseudo-random value. */
    121  1.13   itojun 	bzero(&ctxt, sizeof(ctxt));
    122  1.13   itojun 	MD5Init(&ctxt);
    123  1.13   itojun 	MD5Update(&ctxt, hostname, hostnamelen);
    124  1.13   itojun 	MD5Final(digest, &ctxt);
    125  1.13   itojun 
    126  1.13   itojun 	/* assumes sizeof(digest) > sizeof(first_ifid) */
    127  1.13   itojun 	bcopy(digest, dst, 8);
    128  1.13   itojun 
    129  1.13   itojun 	/* make sure to set "u" bit to local, and "g" bit to individual. */
    130  1.13   itojun 	dst[0] &= 0xfe;
    131  1.13   itojun 	dst[0] |= 0x02;		/* EUI64 "local" */
    132  1.13   itojun 
    133  1.13   itojun 	return 0;
    134  1.13   itojun }
    135  1.13   itojun 
    136  1.13   itojun /*
    137   1.5   itojun  * Find first ifid on list of interfaces.
    138   1.5   itojun  * This is assumed that ifp0's interface token (for example, IEEE802 MAC)
    139   1.5   itojun  * is globally unique.  We may need to have a flag parameter in the future.
    140   1.2   itojun  */
    141   1.2   itojun int
    142   1.2   itojun in6_ifattach_getifid(ifp0)
    143   1.2   itojun 	struct ifnet *ifp0;
    144   1.2   itojun {
    145   1.2   itojun 	struct ifnet *ifp;
    146   1.2   itojun 	struct ifaddr *ifa;
    147   1.2   itojun 	u_int8_t *addr = NULL;
    148   1.2   itojun 	int addrlen = 0;
    149   1.2   itojun 	struct sockaddr_dl *sdl;
    150   1.2   itojun 
    151   1.2   itojun 	if (found_first_ifid)
    152   1.2   itojun 		return 0;
    153   1.2   itojun 
    154  1.13   itojun 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
    155  1.13   itojun 	{
    156   1.2   itojun 		if (ifp0 != NULL && ifp0 != ifp)
    157   1.2   itojun 			continue;
    158   1.2   itojun 		for (ifa = ifp->if_addrlist.tqh_first;
    159   1.2   itojun 		     ifa;
    160  1.13   itojun 		     ifa = ifa->ifa_list.tqe_next)
    161  1.13   itojun 		{
    162   1.2   itojun 			if (ifa->ifa_addr->sa_family != AF_LINK)
    163   1.2   itojun 				continue;
    164   1.2   itojun 			sdl = (struct sockaddr_dl *)ifa->ifa_addr;
    165   1.2   itojun 			if (sdl == NULL)
    166   1.2   itojun 				continue;
    167   1.2   itojun 			if (sdl->sdl_alen == 0)
    168   1.2   itojun 				continue;
    169   1.2   itojun 			switch (ifp->if_type) {
    170   1.2   itojun 			case IFT_ETHER:
    171   1.2   itojun 			case IFT_FDDI:
    172   1.2   itojun 			case IFT_ATM:
    173   1.7   itojun 				/* IEEE802/EUI64 cases - what others? */
    174   1.2   itojun 				addr = LLADDR(sdl);
    175   1.2   itojun 				addrlen = sdl->sdl_alen;
    176   1.7   itojun 				/*
    177   1.7   itojun 				 * to copy ifid from IEEE802/EUI64 interface,
    178   1.7   itojun 				 * u bit of the source needs to be 0.
    179   1.7   itojun 				 */
    180   1.7   itojun 				if ((addr[0] & 0x02) != 0)
    181   1.7   itojun 					break;
    182   1.2   itojun 				goto found;
    183  1.10   itojun 			case IFT_ARCNET:
    184  1.10   itojun 				/*
    185  1.10   itojun 				 * ARCnet interface token cannot be used as
    186  1.24   itojun 				 * globally unique identifier due to its
    187  1.10   itojun 				 * small bitwidth.
    188  1.10   itojun 				 */
    189  1.10   itojun 				break;
    190   1.2   itojun 			default:
    191   1.2   itojun 				break;
    192   1.2   itojun 			}
    193   1.2   itojun 		}
    194   1.2   itojun 	}
    195   1.4  thorpej #ifdef DEBUG
    196   1.4  thorpej 	printf("in6_ifattach_getifid: failed to get EUI64");
    197   1.4  thorpej #endif
    198   1.2   itojun 	return EADDRNOTAVAIL;
    199   1.2   itojun 
    200   1.2   itojun found:
    201   1.7   itojun 	if (laddr_to_eui64(first_ifid, addr, addrlen) == 0)
    202   1.2   itojun 		found_first_ifid = 1;
    203   1.2   itojun 
    204   1.2   itojun 	if (found_first_ifid) {
    205   1.4  thorpej 		printf("%s: supplying EUI64: "
    206   1.4  thorpej 			"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
    207  1.13   itojun 			if_name(ifp),
    208  1.13   itojun 			first_ifid[0], first_ifid[1],
    209  1.13   itojun 			first_ifid[2], first_ifid[3],
    210  1.13   itojun 			first_ifid[4], first_ifid[5],
    211  1.13   itojun 			first_ifid[6], first_ifid[7]);
    212   1.5   itojun 
    213   1.5   itojun 		/* invert u bit to convert EUI64 to RFC2373 interface ID. */
    214   1.5   itojun 		first_ifid[0] ^= 0x02;
    215   1.5   itojun 
    216   1.2   itojun 		return 0;
    217   1.2   itojun 	} else {
    218   1.4  thorpej #ifdef DEBUG
    219   1.4  thorpej 		printf("in6_ifattach_getifid: failed to get EUI64");
    220   1.4  thorpej #endif
    221   1.2   itojun 		return EADDRNOTAVAIL;
    222   1.2   itojun 	}
    223   1.2   itojun }
    224   1.2   itojun 
    225  1.17   itojun /*
    226  1.17   itojun  * XXX multiple loopback interface needs more care.  for instance,
    227  1.17   itojun  * nodelocal address needs to be configured onto only one of them.
    228  1.17   itojun  */
    229   1.2   itojun void
    230   1.2   itojun in6_ifattach(ifp, type, laddr, noloop)
    231   1.2   itojun 	struct ifnet *ifp;
    232   1.2   itojun 	u_int type;
    233   1.2   itojun 	caddr_t laddr;
    234   1.7   itojun 	/* size_t laddrlen; */
    235   1.2   itojun 	int noloop;
    236   1.2   itojun {
    237   1.2   itojun 	static size_t if_indexlim = 8;
    238   1.2   itojun 	struct sockaddr_in6 mltaddr;
    239   1.2   itojun 	struct sockaddr_in6 mltmask;
    240   1.2   itojun 	struct sockaddr_in6 gate;
    241   1.2   itojun 	struct sockaddr_in6 mask;
    242   1.2   itojun 
    243   1.2   itojun 	struct in6_ifaddr *ia, *ib, *oia;
    244   1.2   itojun 	struct ifaddr *ifa;
    245   1.2   itojun 	int rtflag = 0;
    246   1.2   itojun 
    247   1.2   itojun 	if (type == IN6_IFT_P2P && found_first_ifid == 0) {
    248   1.4  thorpej 		printf("%s: no ifid available for IPv6 link-local address\n",
    249  1.13   itojun 			if_name(ifp));
    250  1.13   itojun #if 0
    251   1.2   itojun 		return;
    252  1.13   itojun #else
    253  1.13   itojun 		/* last resort */
    254  1.13   itojun 		if (gen_rand_eui64(first_ifid) == 0) {
    255  1.13   itojun 			printf("%s: using random value as EUI64: "
    256  1.13   itojun 				"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
    257  1.13   itojun 				if_name(ifp),
    258  1.13   itojun 				first_ifid[0], first_ifid[1],
    259  1.13   itojun 				first_ifid[2], first_ifid[3],
    260  1.13   itojun 				first_ifid[4], first_ifid[5],
    261  1.13   itojun 				first_ifid[6], first_ifid[7]);
    262  1.13   itojun 			/*
    263  1.13   itojun 			 * invert u bit to convert EUI64 to RFC2373 interface
    264  1.13   itojun 			 * ID.
    265  1.13   itojun 			 */
    266  1.13   itojun 			first_ifid[0] ^= 0x02;
    267  1.13   itojun 
    268  1.13   itojun 			found_first_ifid = 1;
    269  1.13   itojun 		}
    270  1.13   itojun #endif
    271   1.2   itojun 	}
    272   1.2   itojun 
    273   1.2   itojun 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
    274   1.4  thorpej 		printf("%s: not multicast capable, IPv6 not enabled\n",
    275  1.13   itojun 			if_name(ifp));
    276   1.2   itojun 		return;
    277   1.2   itojun 	}
    278  1.13   itojun 
    279   1.2   itojun 	/*
    280   1.2   itojun 	 * We have some arrays that should be indexed by if_index.
    281   1.2   itojun 	 * since if_index will grow dynamically, they should grow too.
    282  1.13   itojun 	 *	struct in6_ifstat **in6_ifstat
    283  1.13   itojun 	 *	struct icmp6_ifstat **icmp6_ifstat
    284   1.2   itojun 	 */
    285  1.13   itojun 	if (in6_ifstat == NULL || icmp6_ifstat == NULL
    286  1.13   itojun 	 || if_index >= if_indexlim) {
    287   1.2   itojun 		size_t n;
    288   1.2   itojun 		caddr_t q;
    289   1.7   itojun 		size_t olim;
    290   1.2   itojun 
    291   1.7   itojun 		olim = if_indexlim;
    292   1.7   itojun 		while (if_index >= if_indexlim)
    293   1.2   itojun 			if_indexlim <<= 1;
    294   1.2   itojun 
    295  1.13   itojun 		/* grow in6_ifstat */
    296  1.13   itojun 		n = if_indexlim * sizeof(struct in6_ifstat *);
    297  1.13   itojun 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    298  1.13   itojun 		bzero(q, n);
    299  1.13   itojun 		if (in6_ifstat) {
    300  1.13   itojun 			bcopy((caddr_t)in6_ifstat, q,
    301  1.13   itojun 				olim * sizeof(struct in6_ifstat *));
    302  1.13   itojun 			free((caddr_t)in6_ifstat, M_IFADDR);
    303  1.13   itojun 		}
    304  1.13   itojun 		in6_ifstat = (struct in6_ifstat **)q;
    305  1.13   itojun 		in6_ifstatmax = if_indexlim;
    306  1.13   itojun 
    307  1.13   itojun 		/* grow icmp6_ifstat */
    308  1.13   itojun 		n = if_indexlim * sizeof(struct icmp6_ifstat *);
    309   1.2   itojun 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    310   1.2   itojun 		bzero(q, n);
    311  1.13   itojun 		if (icmp6_ifstat) {
    312  1.13   itojun 			bcopy((caddr_t)icmp6_ifstat, q,
    313  1.13   itojun 				olim * sizeof(struct icmp6_ifstat *));
    314  1.13   itojun 			free((caddr_t)icmp6_ifstat, M_IFADDR);
    315   1.2   itojun 		}
    316  1.13   itojun 		icmp6_ifstat = (struct icmp6_ifstat **)q;
    317  1.13   itojun 		icmp6_ifstatmax = if_indexlim;
    318   1.2   itojun 	}
    319   1.2   itojun 
    320   1.2   itojun 	/*
    321   1.2   itojun 	 * To prevent to assign link-local address to PnP network
    322  1.24   itojun 	 * cards multiple times.
    323   1.2   itojun 	 * This is lengthy for P2P and LOOP but works.
    324   1.2   itojun 	 */
    325   1.2   itojun 	ifa = TAILQ_FIRST(&ifp->if_addrlist);
    326   1.2   itojun 	if (ifa != NULL) {
    327   1.2   itojun 		for ( ; ifa; ifa = TAILQ_NEXT(ifa, ifa_list)) {
    328   1.2   itojun 			if (ifa->ifa_addr->sa_family != AF_INET6)
    329   1.2   itojun 				continue;
    330   1.2   itojun 			if (IN6_IS_ADDR_LINKLOCAL(&satosin6(ifa->ifa_addr)->sin6_addr))
    331   1.2   itojun 				return;
    332   1.2   itojun 		}
    333   1.7   itojun 	} else {
    334   1.2   itojun 		TAILQ_INIT(&ifp->if_addrlist);
    335   1.7   itojun 	}
    336   1.2   itojun 
    337   1.2   itojun 	/*
    338   1.2   itojun 	 * link-local address
    339   1.2   itojun 	 */
    340   1.2   itojun 	ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
    341   1.2   itojun 	bzero((caddr_t)ia, sizeof(*ia));
    342  1.23   itojun 	ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
    343  1.20   itojun 	if (ifp->if_flags & IFF_POINTOPOINT)
    344  1.20   itojun 		ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
    345  1.20   itojun 	else
    346  1.20   itojun 		ia->ia_ifa.ifa_dstaddr = NULL;
    347   1.2   itojun 	ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
    348   1.2   itojun 	ia->ia_ifp = ifp;
    349   1.2   itojun 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    350  1.15  thorpej 	IFAREF((struct ifaddr *)ia);
    351  1.15  thorpej 
    352   1.2   itojun 	/*
    353   1.2   itojun 	 * Also link into the IPv6 address chain beginning with in6_ifaddr.
    354   1.2   itojun 	 * kazu opposed it, but itojun & jinmei wanted.
    355   1.2   itojun 	 */
    356   1.2   itojun 	if ((oia = in6_ifaddr) != NULL) {
    357   1.2   itojun 		for (; oia->ia_next; oia = oia->ia_next)
    358   1.2   itojun 			continue;
    359   1.2   itojun 		oia->ia_next = ia;
    360   1.2   itojun 	} else
    361   1.2   itojun 		in6_ifaddr = ia;
    362  1.15  thorpej 	IFAREF((struct ifaddr *)ia);
    363   1.2   itojun 
    364   1.2   itojun 	ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
    365   1.2   itojun 	ia->ia_prefixmask.sin6_family = AF_INET6;
    366   1.2   itojun 	ia->ia_prefixmask.sin6_addr = in6mask64;
    367   1.2   itojun 
    368   1.2   itojun 	bzero(&ia->ia_addr, sizeof(struct sockaddr_in6));
    369   1.2   itojun 	ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
    370   1.2   itojun 	ia->ia_addr.sin6_family = AF_INET6;
    371   1.2   itojun 	ia->ia_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
    372   1.2   itojun 	ia->ia_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
    373   1.2   itojun 	ia->ia_addr.sin6_addr.s6_addr32[1] = 0;
    374   1.2   itojun 
    375   1.2   itojun 	switch (type) {
    376   1.2   itojun 	case IN6_IFT_LOOP:
    377   1.2   itojun 		ia->ia_addr.sin6_addr.s6_addr32[2] = 0;
    378   1.2   itojun 		ia->ia_addr.sin6_addr.s6_addr32[3] = htonl(1);
    379   1.2   itojun 		break;
    380   1.2   itojun 	case IN6_IFT_802:
    381   1.2   itojun 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
    382   1.2   itojun 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
    383   1.2   itojun 		rtflag = RTF_CLONING;
    384   1.2   itojun 		/* fall through */
    385   1.2   itojun 	case IN6_IFT_P2P802:
    386   1.2   itojun 		if (laddr == NULL)
    387   1.2   itojun 			break;
    388   1.7   itojun 		/* XXX use laddrlen */
    389   1.7   itojun 		if (laddr_to_eui64(&ia->ia_addr.sin6_addr.s6_addr8[8],
    390   1.7   itojun 				laddr, 6) != 0) {
    391   1.7   itojun 			break;
    392   1.7   itojun 		}
    393   1.6   itojun 		/* invert u bit to convert EUI64 to RFC2373 interface ID. */
    394   1.6   itojun 		ia->ia_addr.sin6_addr.s6_addr8[8] ^= 0x02;
    395  1.13   itojun 		if (found_first_ifid == 0)
    396  1.13   itojun 			in6_ifattach_getifid(ifp);
    397  1.13   itojun 		bzero(&ia->ia_dstaddr, sizeof(struct sockaddr_in6));
    398  1.13   itojun 		ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
    399  1.13   itojun 		ia->ia_dstaddr.sin6_family = AF_INET6;
    400   1.2   itojun 		break;
    401   1.2   itojun 	case IN6_IFT_P2P:
    402   1.2   itojun 		bcopy((caddr_t)first_ifid,
    403   1.2   itojun 		      (caddr_t)&ia->ia_addr.sin6_addr.s6_addr8[8],
    404   1.2   itojun 		      IFID_LEN);
    405  1.13   itojun 		bzero(&ia->ia_dstaddr, sizeof(struct sockaddr_in6));
    406  1.13   itojun 		ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
    407  1.13   itojun 		ia->ia_dstaddr.sin6_family = AF_INET6;
    408   1.2   itojun 		break;
    409   1.8       is 	case IN6_IFT_ARCNET:
    410   1.8       is 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
    411   1.8       is 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
    412   1.8       is 		rtflag = RTF_CLONING;
    413   1.8       is 		if (laddr == NULL)
    414   1.8       is 			break;
    415  1.12       is 
    416  1.12       is 		/* make non-global IF id out of link-level address */
    417  1.12       is 		bzero(&ia->ia_addr.sin6_addr.s6_addr8[8], 7);
    418  1.12       is 		ia->ia_addr.sin6_addr.s6_addr8[15] = *laddr;
    419   1.2   itojun 	}
    420   1.2   itojun 
    421   1.2   itojun 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    422   1.2   itojun 
    423   1.2   itojun 	if (ifp->if_ioctl != NULL) {
    424   1.2   itojun 		int s;
    425   1.2   itojun 		int error;
    426   1.2   itojun 
    427   1.2   itojun 		/*
    428   1.2   itojun 		 * give the interface a chance to initialize, in case this
    429   1.2   itojun 		 * is the first address to be added.
    430   1.2   itojun 		 */
    431   1.2   itojun 		s = splimp();
    432   1.2   itojun 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
    433   1.2   itojun 		splx(s);
    434   1.2   itojun 
    435   1.2   itojun 		if (error) {
    436   1.2   itojun 			switch (error) {
    437   1.2   itojun 			case EAFNOSUPPORT:
    438   1.4  thorpej 				printf("%s: IPv6 not supported\n",
    439  1.13   itojun 					if_name(ifp));
    440   1.2   itojun 				break;
    441   1.2   itojun 			default:
    442   1.4  thorpej 				printf("%s: SIOCSIFADDR error %d\n",
    443  1.13   itojun 					if_name(ifp), error);
    444   1.2   itojun 				break;
    445   1.2   itojun 			}
    446   1.2   itojun 
    447   1.2   itojun 			/* undo changes */
    448   1.2   itojun 			TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    449  1.24   itojun 			IFAFREE(&ia->ia_ifa);
    450   1.2   itojun 			if (oia)
    451   1.2   itojun 				oia->ia_next = ia->ia_next;
    452   1.2   itojun 			else
    453   1.2   itojun 				in6_ifaddr = ia->ia_next;
    454  1.24   itojun 			IFAFREE(&ia->ia_ifa);
    455   1.2   itojun 			return;
    456   1.2   itojun 		}
    457   1.2   itojun 	}
    458   1.2   itojun 
    459   1.2   itojun 	/* add route to the interface. */
    460   1.2   itojun 	rtrequest(RTM_ADD,
    461   1.2   itojun 		  (struct sockaddr *)&ia->ia_addr,
    462  1.24   itojun 		  (struct sockaddr *)&ia->ia_addr,
    463   1.2   itojun 		  (struct sockaddr *)&ia->ia_prefixmask,
    464   1.2   itojun 		  RTF_UP|rtflag,
    465   1.2   itojun 		  (struct rtentry **)0);
    466   1.2   itojun 	ia->ia_flags |= IFA_ROUTE;
    467   1.2   itojun 
    468   1.2   itojun 	if (type == IN6_IFT_P2P || type == IN6_IFT_P2P802) {
    469   1.2   itojun 		/*
    470   1.2   itojun 		 * route local address to loopback
    471   1.2   itojun 		 */
    472   1.2   itojun 		bzero(&gate, sizeof(gate));
    473   1.2   itojun 		gate.sin6_len = sizeof(struct sockaddr_in6);
    474   1.2   itojun 		gate.sin6_family = AF_INET6;
    475   1.2   itojun 		gate.sin6_addr = in6addr_loopback;
    476   1.2   itojun 		bzero(&mask, sizeof(mask));
    477   1.2   itojun 		mask.sin6_len = sizeof(struct sockaddr_in6);
    478   1.2   itojun 		mask.sin6_family = AF_INET6;
    479   1.2   itojun 		mask.sin6_addr = in6mask64;
    480   1.2   itojun 		rtrequest(RTM_ADD,
    481   1.2   itojun 			  (struct sockaddr *)&ia->ia_addr,
    482   1.2   itojun 			  (struct sockaddr *)&gate,
    483   1.2   itojun 			  (struct sockaddr *)&mask,
    484   1.2   itojun 			  RTF_UP|RTF_HOST,
    485   1.2   itojun 			  (struct rtentry **)0);
    486   1.2   itojun 	}
    487   1.2   itojun 
    488   1.2   itojun 	/*
    489   1.2   itojun 	 * loopback address
    490   1.2   itojun 	 */
    491   1.2   itojun 	ib = (struct in6_ifaddr *)NULL;
    492   1.2   itojun 	if (type == IN6_IFT_LOOP) {
    493   1.2   itojun 		ib = (struct in6_ifaddr *)
    494   1.2   itojun 			malloc(sizeof(*ib), M_IFADDR, M_WAITOK);
    495   1.2   itojun 		bzero((caddr_t)ib, sizeof(*ib));
    496   1.2   itojun 		ib->ia_ifa.ifa_addr = (struct sockaddr *)&ib->ia_addr;
    497   1.2   itojun 		ib->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ib->ia_dstaddr;
    498   1.2   itojun 		ib->ia_ifa.ifa_netmask = (struct sockaddr *)&ib->ia_prefixmask;
    499   1.2   itojun 		ib->ia_ifp = ifp;
    500   1.2   itojun 
    501   1.2   itojun 		ia->ia_next = ib;
    502   1.2   itojun 		TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ib,
    503   1.2   itojun 			ifa_list);
    504  1.21   itojun 		IFAREF((struct ifaddr *)ib);
    505   1.2   itojun 
    506   1.2   itojun 		ib->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
    507   1.2   itojun 		ib->ia_prefixmask.sin6_family = AF_INET6;
    508   1.2   itojun 		ib->ia_prefixmask.sin6_addr = in6mask128;
    509   1.2   itojun 		ib->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
    510   1.2   itojun 		ib->ia_addr.sin6_family = AF_INET6;
    511   1.2   itojun 		ib->ia_addr.sin6_addr = in6addr_loopback;
    512  1.21   itojun 
    513  1.21   itojun 		/*
    514  1.21   itojun 		 * Always initialize ia_dstaddr (= broadcast address)
    515  1.21   itojun 		 * to loopback address, to make getifaddr happier.
    516  1.21   itojun 		 *
    517  1.21   itojun 		 * For BSDI, it is mandatory.  The BSDI version of
    518  1.21   itojun 		 * ifa_ifwithroute() rejects to add a route to the loopback
    519  1.21   itojun 		 * interface.  Even for other systems, loopback looks somewhat
    520  1.21   itojun 		 * special.
    521  1.21   itojun 		 */
    522  1.21   itojun 		ib->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
    523  1.21   itojun 		ib->ia_dstaddr.sin6_family = AF_INET6;
    524  1.21   itojun 		ib->ia_dstaddr.sin6_addr = in6addr_loopback;
    525   1.2   itojun 
    526   1.2   itojun 		ib->ia_ifa.ifa_metric = ifp->if_metric;
    527   1.2   itojun 
    528   1.2   itojun 		rtrequest(RTM_ADD,
    529   1.2   itojun 			  (struct sockaddr *)&ib->ia_addr,
    530  1.24   itojun 			  (struct sockaddr *)&ib->ia_addr,
    531   1.2   itojun 			  (struct sockaddr *)&ib->ia_prefixmask,
    532   1.2   itojun 			  RTF_UP|RTF_HOST,
    533   1.2   itojun 			  (struct rtentry **)0);
    534   1.2   itojun 
    535   1.2   itojun 		ib->ia_flags |= IFA_ROUTE;
    536   1.2   itojun 	}
    537   1.2   itojun 
    538   1.2   itojun 	/*
    539   1.2   itojun 	 * join multicast
    540   1.2   itojun 	 */
    541   1.2   itojun 	if (ifp->if_flags & IFF_MULTICAST) {
    542   1.2   itojun 		int error;	/* not used */
    543   1.2   itojun 
    544   1.2   itojun 		/* Restore saved multicast addresses(if any). */
    545   1.2   itojun 		in6_restoremkludge(ia, ifp);
    546   1.2   itojun 
    547   1.2   itojun 		bzero(&mltmask, sizeof(mltmask));
    548   1.2   itojun 		mltmask.sin6_len = sizeof(struct sockaddr_in6);
    549   1.2   itojun 		mltmask.sin6_family = AF_INET6;
    550   1.2   itojun 		mltmask.sin6_addr = in6mask32;
    551   1.2   itojun 
    552   1.2   itojun 		/*
    553   1.2   itojun 		 * join link-local all-nodes address
    554   1.2   itojun 		 */
    555   1.2   itojun 		bzero(&mltaddr, sizeof(mltaddr));
    556   1.2   itojun 		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
    557   1.2   itojun 		mltaddr.sin6_family = AF_INET6;
    558   1.2   itojun 		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
    559   1.2   itojun 		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
    560   1.2   itojun 		rtrequest(RTM_ADD,
    561   1.2   itojun 			  (struct sockaddr *)&mltaddr,
    562  1.24   itojun 			  (struct sockaddr *)&ia->ia_addr,
    563   1.2   itojun 			  (struct sockaddr *)&mltmask,
    564   1.2   itojun 			  RTF_UP|RTF_CLONING,  /* xxx */
    565   1.2   itojun 			  (struct rtentry **)0);
    566   1.2   itojun 		(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
    567   1.2   itojun 
    568   1.2   itojun 		if (type == IN6_IFT_LOOP) {
    569   1.2   itojun 			/*
    570   1.2   itojun 			 * join node-local all-nodes address
    571   1.2   itojun 			 */
    572   1.2   itojun 			mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
    573   1.2   itojun 			rtrequest(RTM_ADD,
    574   1.2   itojun 				  (struct sockaddr *)&mltaddr,
    575  1.24   itojun 				  (struct sockaddr *)&ib->ia_addr,
    576   1.2   itojun 				  (struct sockaddr *)&mltmask,
    577   1.2   itojun 				  RTF_UP,
    578   1.2   itojun 				  (struct rtentry **)0);
    579   1.2   itojun 			(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
    580   1.2   itojun 		} else {
    581   1.2   itojun 			/*
    582   1.2   itojun 			 * join solicited multicast address
    583   1.2   itojun 			 */
    584   1.2   itojun 			bzero(&llsol, sizeof(llsol));
    585   1.2   itojun 			llsol.s6_addr16[0] = htons(0xff02);
    586   1.2   itojun 			llsol.s6_addr16[1] = htons(ifp->if_index);
    587   1.2   itojun 			llsol.s6_addr32[1] = 0;
    588   1.2   itojun 			llsol.s6_addr32[2] = htonl(1);
    589   1.2   itojun 			llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
    590   1.2   itojun 			llsol.s6_addr8[12] = 0xff;
    591   1.2   itojun 			(void)in6_addmulti(&llsol, ifp, &error);
    592   1.2   itojun 		}
    593   1.2   itojun 	}
    594   1.2   itojun 
    595   1.2   itojun 	/* update dynamically. */
    596   1.2   itojun 	if (in6_maxmtu < ifp->if_mtu)
    597   1.2   itojun 		in6_maxmtu = ifp->if_mtu;
    598   1.2   itojun 
    599  1.13   itojun 	if (in6_ifstat[ifp->if_index] == NULL) {
    600  1.13   itojun 		in6_ifstat[ifp->if_index] = (struct in6_ifstat *)
    601  1.13   itojun 			malloc(sizeof(struct in6_ifstat), M_IFADDR, M_WAITOK);
    602  1.13   itojun 		bzero(in6_ifstat[ifp->if_index], sizeof(struct in6_ifstat));
    603  1.13   itojun 	}
    604  1.13   itojun 	if (icmp6_ifstat[ifp->if_index] == NULL) {
    605  1.13   itojun 		icmp6_ifstat[ifp->if_index] = (struct icmp6_ifstat *)
    606  1.13   itojun 			malloc(sizeof(struct icmp6_ifstat), M_IFADDR, M_WAITOK);
    607  1.13   itojun 		bzero(icmp6_ifstat[ifp->if_index], sizeof(struct icmp6_ifstat));
    608  1.13   itojun 	}
    609  1.13   itojun 
    610   1.2   itojun 	/* initialize NDP variables */
    611   1.2   itojun 	nd6_ifattach(ifp);
    612   1.2   itojun 
    613   1.2   itojun 	/* mark the address TENTATIVE, if needed. */
    614   1.2   itojun 	switch (ifp->if_type) {
    615   1.8       is 	case IFT_ARCNET:
    616   1.2   itojun 	case IFT_ETHER:
    617   1.2   itojun 	case IFT_FDDI:
    618   1.2   itojun #if 0
    619   1.2   itojun 	case IFT_ATM:
    620   1.2   itojun 	case IFT_SLIP:
    621   1.2   itojun 	case IFT_PPP:
    622   1.2   itojun #endif
    623   1.2   itojun 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
    624   1.2   itojun 		/* nd6_dad_start() will be called in in6_if_up */
    625   1.2   itojun 		break;
    626  1.13   itojun 	case IFT_GIF:	/*XXX*/
    627   1.2   itojun 	case IFT_LOOP:
    628   1.2   itojun 	case IFT_FAITH:
    629   1.2   itojun 	default:
    630   1.2   itojun 		break;
    631   1.2   itojun 	}
    632   1.2   itojun 
    633   1.2   itojun 	return;
    634   1.2   itojun }
    635   1.2   itojun 
    636  1.17   itojun /*
    637  1.17   itojun  * NOTE: in6_ifdetach() does not support loopback if at this moment.
    638  1.17   itojun  */
    639   1.2   itojun void
    640   1.2   itojun in6_ifdetach(ifp)
    641   1.2   itojun 	struct ifnet *ifp;
    642   1.2   itojun {
    643   1.2   itojun 	struct in6_ifaddr *ia, *oia;
    644   1.2   itojun 	struct ifaddr *ifa;
    645   1.2   itojun 	struct rtentry *rt;
    646   1.2   itojun 	short rtflags;
    647  1.16   itojun 	struct sockaddr_in6 sin6;
    648   1.2   itojun 
    649  1.18   itojun 	/* nuke prefix list.  this may try to remove some of ifaddrs as well */
    650  1.18   itojun 	in6_purgeprefix(ifp);
    651  1.18   itojun 
    652  1.18   itojun 	/* remove neighbor management table */
    653  1.18   itojun 	nd6_purge(ifp);
    654  1.18   itojun 
    655  1.13   itojun 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
    656  1.13   itojun 	{
    657   1.2   itojun 		if (ifa->ifa_addr->sa_family != AF_INET6
    658   1.2   itojun 		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
    659   1.2   itojun 			continue;
    660   1.2   itojun 		}
    661   1.2   itojun 
    662   1.2   itojun 		ia = (struct in6_ifaddr *)ifa;
    663   1.2   itojun 
    664   1.2   itojun 		/* remove from the routing table */
    665   1.2   itojun 		if ((ia->ia_flags & IFA_ROUTE)
    666  1.14   itojun 		 && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
    667   1.2   itojun 			rtflags = rt->rt_flags;
    668   1.2   itojun 			rtfree(rt);
    669   1.2   itojun 			rtrequest(RTM_DELETE,
    670   1.2   itojun 				(struct sockaddr *)&ia->ia_addr,
    671  1.24   itojun 				(struct sockaddr *)&ia->ia_addr,
    672   1.2   itojun 				(struct sockaddr *)&ia->ia_prefixmask,
    673   1.2   itojun 				rtflags, (struct rtentry **)0);
    674   1.2   itojun 		}
    675   1.2   itojun 
    676   1.2   itojun 		/* remove from the linked list */
    677   1.2   itojun 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    678   1.2   itojun 
    679   1.2   itojun 		/* also remove from the IPv6 address chain(itojun&jinmei) */
    680   1.2   itojun 		oia = ia;
    681   1.2   itojun 		if (oia == (ia = in6_ifaddr))
    682   1.2   itojun 			in6_ifaddr = ia->ia_next;
    683   1.2   itojun 		else {
    684   1.2   itojun 			while (ia->ia_next && (ia->ia_next != oia))
    685   1.2   itojun 				ia = ia->ia_next;
    686   1.2   itojun 			if (ia->ia_next)
    687   1.2   itojun 				ia->ia_next = oia->ia_next;
    688   1.4  thorpej #ifdef DEBUG
    689   1.2   itojun 			else
    690   1.4  thorpej 				printf("%s: didn't unlink in6ifaddr from "
    691  1.13   itojun 				    "list\n", if_name(ifp));
    692   1.4  thorpej #endif
    693   1.2   itojun 		}
    694   1.2   itojun 
    695   1.2   itojun 		free(ia, M_IFADDR);
    696  1.16   itojun 	}
    697  1.16   itojun 
    698  1.17   itojun 	/* cleanup multicast address kludge table, if there is any */
    699  1.17   itojun 	in6_purgemkludge(ifp);
    700  1.18   itojun 
    701  1.16   itojun 	/* remove route to link-local allnodes multicast (ff02::1) */
    702  1.16   itojun 	bzero(&sin6, sizeof(sin6));
    703  1.16   itojun 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    704  1.16   itojun 	sin6.sin6_family = AF_INET6;
    705  1.16   itojun 	sin6.sin6_addr = in6addr_linklocal_allnodes;
    706  1.16   itojun 	sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
    707  1.16   itojun 	if ((rt = rtalloc1((struct sockaddr *)&sin6, 0)) != NULL) {
    708  1.16   itojun 		rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
    709  1.16   itojun 			rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
    710  1.16   itojun 		rtfree(rt);
    711   1.2   itojun 	}
    712   1.2   itojun }
    713