Home | History | Annotate | Line # | Download | only in netinet6
in6_ifattach.c revision 1.6
      1  1.6   itojun /*	$NetBSD: in6_ifattach.c,v 1.6 1999/09/08 00:50:15 itojun Exp $	*/
      2  1.3  thorpej 
      3  1.2   itojun /*
      4  1.2   itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  1.2   itojun  * All rights reserved.
      6  1.2   itojun  *
      7  1.2   itojun  * Redistribution and use in source and binary forms, with or without
      8  1.2   itojun  * modification, are permitted provided that the following conditions
      9  1.2   itojun  * are met:
     10  1.2   itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.2   itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.2   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2   itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.2   itojun  *    documentation and/or other materials provided with the distribution.
     15  1.2   itojun  * 3. Neither the name of the project nor the names of its contributors
     16  1.2   itojun  *    may be used to endorse or promote products derived from this software
     17  1.2   itojun  *    without specific prior written permission.
     18  1.2   itojun  *
     19  1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.2   itojun  * SUCH DAMAGE.
     30  1.2   itojun  */
     31  1.2   itojun 
     32  1.2   itojun #include <sys/param.h>
     33  1.2   itojun #include <sys/systm.h>
     34  1.2   itojun #include <sys/malloc.h>
     35  1.2   itojun #include <sys/socket.h>
     36  1.2   itojun #include <sys/sockio.h>
     37  1.2   itojun 
     38  1.2   itojun #include <net/if.h>
     39  1.2   itojun #include <net/if_dl.h>
     40  1.2   itojun #include <net/if_types.h>
     41  1.2   itojun #include <net/route.h>
     42  1.2   itojun 
     43  1.2   itojun #include <netinet/in.h>
     44  1.2   itojun #include <netinet/in_var.h>
     45  1.2   itojun #ifndef __NetBSD__
     46  1.2   itojun #include <netinet/if_ether.h>
     47  1.2   itojun #endif
     48  1.2   itojun 
     49  1.2   itojun #include <netinet6/in6.h>
     50  1.2   itojun #include <netinet6/ip6.h>
     51  1.2   itojun #include <netinet6/ip6_var.h>
     52  1.2   itojun #include <netinet6/in6_ifattach.h>
     53  1.2   itojun #include <netinet6/ip6.h>
     54  1.2   itojun #include <netinet6/ip6_var.h>
     55  1.2   itojun #include <netinet6/nd6.h>
     56  1.2   itojun 
     57  1.2   itojun static	struct in6_addr llsol;
     58  1.2   itojun 
     59  1.2   itojun struct in6_addr **in6_iflladdr = NULL;
     60  1.2   itojun unsigned long in6_maxmtu = 0;
     61  1.2   itojun 
     62  1.2   itojun int found_first_ifid = 0;
     63  1.2   itojun #define IFID_LEN 8
     64  1.2   itojun static char first_ifid[IFID_LEN];
     65  1.2   itojun 
     66  1.2   itojun static void ieee802_to_eui64 __P((u_int8_t *, u_int8_t *));
     67  1.2   itojun 
     68  1.2   itojun static void
     69  1.2   itojun ieee802_to_eui64(dst, src)
     70  1.2   itojun 	u_int8_t *dst;
     71  1.2   itojun 	u_int8_t *src;
     72  1.2   itojun {
     73  1.2   itojun 	dst[0] = src[0];
     74  1.2   itojun 	dst[1] = src[1];
     75  1.2   itojun 	dst[2] = src[2];
     76  1.2   itojun 	dst[3] = 0xff;
     77  1.2   itojun 	dst[4] = 0xfe;
     78  1.2   itojun 	dst[5] = src[3];
     79  1.2   itojun 	dst[6] = src[4];
     80  1.2   itojun 	dst[7] = src[5];
     81  1.2   itojun }
     82  1.2   itojun 
     83  1.2   itojun /*
     84  1.5   itojun  * Find first ifid on list of interfaces.
     85  1.5   itojun  * This is assumed that ifp0's interface token (for example, IEEE802 MAC)
     86  1.5   itojun  * is globally unique.  We may need to have a flag parameter in the future.
     87  1.2   itojun  */
     88  1.2   itojun int
     89  1.2   itojun in6_ifattach_getifid(ifp0)
     90  1.2   itojun 	struct ifnet *ifp0;
     91  1.2   itojun {
     92  1.2   itojun 	struct ifnet *ifp;
     93  1.2   itojun 	struct ifaddr *ifa;
     94  1.2   itojun 	u_int8_t *addr = NULL;
     95  1.2   itojun 	int addrlen = 0;
     96  1.2   itojun 	struct sockaddr_dl *sdl;
     97  1.2   itojun 
     98  1.2   itojun 	if (found_first_ifid)
     99  1.2   itojun 		return 0;
    100  1.2   itojun 
    101  1.2   itojun 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
    102  1.2   itojun 		if (ifp0 != NULL && ifp0 != ifp)
    103  1.2   itojun 			continue;
    104  1.2   itojun 		for (ifa = ifp->if_addrlist.tqh_first;
    105  1.2   itojun 		     ifa;
    106  1.2   itojun 		     ifa = ifa->ifa_list.tqe_next) {
    107  1.2   itojun 			if (ifa->ifa_addr->sa_family != AF_LINK)
    108  1.2   itojun 				continue;
    109  1.2   itojun 			sdl = (struct sockaddr_dl *)ifa->ifa_addr;
    110  1.2   itojun 			if (sdl == NULL)
    111  1.2   itojun 				continue;
    112  1.2   itojun 			if (sdl->sdl_alen == 0)
    113  1.2   itojun 				continue;
    114  1.2   itojun 			switch (ifp->if_type) {
    115  1.2   itojun 			case IFT_ETHER:
    116  1.2   itojun 			case IFT_FDDI:
    117  1.2   itojun 			case IFT_ATM:
    118  1.2   itojun 			/* what others? */
    119  1.2   itojun 				addr = LLADDR(sdl);
    120  1.2   itojun 				addrlen = sdl->sdl_alen;
    121  1.2   itojun 				goto found;
    122  1.2   itojun 			default:
    123  1.2   itojun 				break;
    124  1.2   itojun 			}
    125  1.2   itojun 		}
    126  1.2   itojun 	}
    127  1.4  thorpej #ifdef DEBUG
    128  1.4  thorpej 	printf("in6_ifattach_getifid: failed to get EUI64");
    129  1.4  thorpej #endif
    130  1.2   itojun 	return EADDRNOTAVAIL;
    131  1.2   itojun 
    132  1.2   itojun found:
    133  1.2   itojun 	switch (addrlen) {
    134  1.2   itojun 	case 6:
    135  1.2   itojun 		ieee802_to_eui64(first_ifid, addr);
    136  1.2   itojun 		found_first_ifid = 1;
    137  1.2   itojun 		break;
    138  1.2   itojun 	case 8:
    139  1.2   itojun 		bcopy(addr, first_ifid, 8);
    140  1.2   itojun 		found_first_ifid = 1;
    141  1.2   itojun 		break;
    142  1.2   itojun 	default:
    143  1.2   itojun 		break;
    144  1.2   itojun 	}
    145  1.2   itojun 
    146  1.2   itojun 	if (found_first_ifid) {
    147  1.4  thorpej 		printf("%s: supplying EUI64: "
    148  1.4  thorpej 			"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
    149  1.2   itojun 			ifp->if_xname,
    150  1.2   itojun 			first_ifid[0] & 0xff, first_ifid[1] & 0xff,
    151  1.2   itojun 			first_ifid[2] & 0xff, first_ifid[3] & 0xff,
    152  1.2   itojun 			first_ifid[4] & 0xff, first_ifid[5] & 0xff,
    153  1.2   itojun 			first_ifid[6] & 0xff, first_ifid[7] & 0xff);
    154  1.5   itojun 
    155  1.5   itojun 		/* invert u bit to convert EUI64 to RFC2373 interface ID. */
    156  1.5   itojun 		first_ifid[0] ^= 0x02;
    157  1.5   itojun 
    158  1.2   itojun 		return 0;
    159  1.2   itojun 	} else {
    160  1.4  thorpej #ifdef DEBUG
    161  1.4  thorpej 		printf("in6_ifattach_getifid: failed to get EUI64");
    162  1.4  thorpej #endif
    163  1.2   itojun 		return EADDRNOTAVAIL;
    164  1.2   itojun 	}
    165  1.2   itojun }
    166  1.2   itojun 
    167  1.2   itojun /*
    168  1.2   itojun  * add link-local address to *pseudo* p2p interfaces.
    169  1.2   itojun  * get called when the first MAC address is made available in in6_ifattach().
    170  1.5   itojun  *
    171  1.5   itojun  * XXX I start feeling this as a bad idea. (itojun)
    172  1.2   itojun  */
    173  1.2   itojun void
    174  1.2   itojun in6_ifattach_p2p()
    175  1.2   itojun {
    176  1.2   itojun 	struct ifnet *ifp;
    177  1.2   itojun 
    178  1.2   itojun 	/* prevent infinite loop. just in case. */
    179  1.4  thorpej 	if (found_first_ifid == 0)
    180  1.2   itojun 		return;
    181  1.2   itojun 
    182  1.2   itojun 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
    183  1.2   itojun 		switch (ifp->if_type) {
    184  1.2   itojun 		case IFT_GIF:
    185  1.2   itojun 			/* pseudo interfaces - safe to initialize here */
    186  1.2   itojun 			in6_ifattach(ifp, IN6_IFT_P2P, 0, 0);
    187  1.2   itojun 			break;
    188  1.5   itojun 		case IFT_FAITH:
    189  1.5   itojun 			/* this mistakingly becomes IFF_UP */
    190  1.5   itojun 			break;
    191  1.2   itojun 		case IFT_SLIP:
    192  1.2   itojun 			/* IPv6 is not supported */
    193  1.2   itojun 			break;
    194  1.2   itojun 		case IFT_PPP:
    195  1.2   itojun 			/* this is not a pseudo interface, skip it */
    196  1.2   itojun 			break;
    197  1.2   itojun 		default:
    198  1.2   itojun 			break;
    199  1.2   itojun 		}
    200  1.2   itojun 	}
    201  1.2   itojun }
    202  1.2   itojun 
    203  1.2   itojun void
    204  1.2   itojun in6_ifattach(ifp, type, laddr, noloop)
    205  1.2   itojun 	struct ifnet *ifp;
    206  1.2   itojun 	u_int type;
    207  1.2   itojun 	caddr_t laddr;
    208  1.2   itojun 	int noloop;
    209  1.2   itojun 	/* xxx sizeof(laddr) */
    210  1.2   itojun {
    211  1.2   itojun 	static size_t if_indexlim = 8;
    212  1.2   itojun 	struct sockaddr_in6 mltaddr;
    213  1.2   itojun 	struct sockaddr_in6 mltmask;
    214  1.2   itojun 	struct sockaddr_in6 gate;
    215  1.2   itojun 	struct sockaddr_in6 mask;
    216  1.2   itojun 
    217  1.2   itojun 	struct in6_ifaddr *ia, *ib, *oia;
    218  1.2   itojun 	struct ifaddr *ifa;
    219  1.2   itojun 	int rtflag = 0;
    220  1.2   itojun 
    221  1.2   itojun 	if (type == IN6_IFT_P2P && found_first_ifid == 0) {
    222  1.4  thorpej 		printf("%s: no ifid available for IPv6 link-local address\n",
    223  1.2   itojun 			ifp->if_xname);
    224  1.2   itojun 		return;
    225  1.2   itojun 	}
    226  1.2   itojun 
    227  1.2   itojun 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
    228  1.4  thorpej 		printf("%s: not multicast capable, IPv6 not enabled\n",
    229  1.2   itojun 			ifp->if_xname);
    230  1.2   itojun 		return;
    231  1.2   itojun 	}
    232  1.2   itojun 
    233  1.2   itojun 	/*
    234  1.2   itojun 	 * We have some arrays that should be indexed by if_index.
    235  1.2   itojun 	 * since if_index will grow dynamically, they should grow too.
    236  1.2   itojun 	 *	struct in6_addr **in6_iflladdr
    237  1.2   itojun 	 */
    238  1.2   itojun 	if (in6_iflladdr == NULL || if_index >= if_indexlim) {
    239  1.2   itojun 		size_t n;
    240  1.2   itojun 		caddr_t q;
    241  1.2   itojun 
    242  1.2   itojun 		while(if_index >= if_indexlim)
    243  1.2   itojun 			if_indexlim <<= 1;
    244  1.2   itojun 
    245  1.2   itojun 		/* grow in6_iflladdr */
    246  1.2   itojun 		n = if_indexlim * sizeof(struct in6_addr *);
    247  1.2   itojun 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    248  1.2   itojun 		bzero(q, n);
    249  1.2   itojun 		if (in6_iflladdr) {
    250  1.2   itojun 			bcopy((caddr_t)in6_iflladdr, q, n/2);
    251  1.2   itojun 			free((caddr_t)in6_iflladdr, M_IFADDR);
    252  1.2   itojun 		}
    253  1.2   itojun 		in6_iflladdr = (struct in6_addr **)q;
    254  1.2   itojun 	}
    255  1.2   itojun 
    256  1.2   itojun 	/*
    257  1.2   itojun 	 * To prevent to assign link-local address to PnP network
    258  1.2   itojun 	 * cards multiple times.
    259  1.2   itojun 	 * This is lengthy for P2P and LOOP but works.
    260  1.2   itojun 	 */
    261  1.2   itojun 	ifa = TAILQ_FIRST(&ifp->if_addrlist);
    262  1.2   itojun 	if (ifa != NULL) {
    263  1.2   itojun 		for ( ; ifa; ifa = TAILQ_NEXT(ifa, ifa_list)) {
    264  1.2   itojun 			if (ifa->ifa_addr->sa_family != AF_INET6)
    265  1.2   itojun 				continue;
    266  1.2   itojun 			if (IN6_IS_ADDR_LINKLOCAL(&satosin6(ifa->ifa_addr)->sin6_addr))
    267  1.2   itojun 				return;
    268  1.2   itojun 		}
    269  1.2   itojun 	} else
    270  1.2   itojun 		TAILQ_INIT(&ifp->if_addrlist);
    271  1.2   itojun 
    272  1.2   itojun 	/*
    273  1.2   itojun 	 * link-local address
    274  1.2   itojun 	 */
    275  1.2   itojun 	ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
    276  1.2   itojun 	bzero((caddr_t)ia, sizeof(*ia));
    277  1.2   itojun 	ia->ia_ifa.ifa_addr =    (struct sockaddr *)&ia->ia_addr;
    278  1.2   itojun 	ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
    279  1.2   itojun 	ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
    280  1.2   itojun 	ia->ia_ifp = ifp;
    281  1.2   itojun 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    282  1.2   itojun 	/*
    283  1.2   itojun 	 * Also link into the IPv6 address chain beginning with in6_ifaddr.
    284  1.2   itojun 	 * kazu opposed it, but itojun & jinmei wanted.
    285  1.2   itojun 	 */
    286  1.2   itojun 	if ((oia = in6_ifaddr) != NULL) {
    287  1.2   itojun 		for (; oia->ia_next; oia = oia->ia_next)
    288  1.2   itojun 			continue;
    289  1.2   itojun 		oia->ia_next = ia;
    290  1.2   itojun 	} else
    291  1.2   itojun 		in6_ifaddr = ia;
    292  1.2   itojun 
    293  1.2   itojun 	ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
    294  1.2   itojun 	ia->ia_prefixmask.sin6_family = AF_INET6;
    295  1.2   itojun 	ia->ia_prefixmask.sin6_addr = in6mask64;
    296  1.2   itojun 
    297  1.2   itojun 	bzero(&ia->ia_addr, sizeof(struct sockaddr_in6));
    298  1.2   itojun 	ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
    299  1.2   itojun 	ia->ia_addr.sin6_family = AF_INET6;
    300  1.2   itojun 	ia->ia_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
    301  1.2   itojun 	ia->ia_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
    302  1.2   itojun 	ia->ia_addr.sin6_addr.s6_addr32[1] = 0;
    303  1.2   itojun 
    304  1.2   itojun 	switch (type) {
    305  1.2   itojun 	case IN6_IFT_LOOP:
    306  1.2   itojun 		ia->ia_addr.sin6_addr.s6_addr32[2] = 0;
    307  1.2   itojun 		ia->ia_addr.sin6_addr.s6_addr32[3] = htonl(1);
    308  1.2   itojun 		break;
    309  1.2   itojun 	case IN6_IFT_802:
    310  1.2   itojun 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
    311  1.2   itojun 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
    312  1.2   itojun 		rtflag = RTF_CLONING;
    313  1.2   itojun 		/* fall through */
    314  1.2   itojun 	case IN6_IFT_P2P802:
    315  1.2   itojun 		if (laddr == NULL)
    316  1.2   itojun 			break;
    317  1.2   itojun 		ieee802_to_eui64(&ia->ia_addr.sin6_addr.s6_addr8[8], laddr);
    318  1.6   itojun 		/* invert u bit to convert EUI64 to RFC2373 interface ID. */
    319  1.6   itojun 		ia->ia_addr.sin6_addr.s6_addr8[8] ^= 0x02;
    320  1.4  thorpej 		if (found_first_ifid == 0) {
    321  1.2   itojun 			if (in6_ifattach_getifid(ifp) == 0)
    322  1.2   itojun 				in6_ifattach_p2p();
    323  1.2   itojun 		}
    324  1.2   itojun 		break;
    325  1.2   itojun 	case IN6_IFT_P2P:
    326  1.2   itojun 		bcopy((caddr_t)first_ifid,
    327  1.2   itojun 		      (caddr_t)&ia->ia_addr.sin6_addr.s6_addr8[8],
    328  1.2   itojun 		      IFID_LEN);
    329  1.2   itojun 		break;
    330  1.2   itojun 	}
    331  1.2   itojun 
    332  1.2   itojun 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    333  1.2   itojun 
    334  1.2   itojun 	if (ifp->if_ioctl != NULL) {
    335  1.2   itojun 		int s;
    336  1.2   itojun 		int error;
    337  1.2   itojun 
    338  1.2   itojun 		/*
    339  1.2   itojun 		 * give the interface a chance to initialize, in case this
    340  1.2   itojun 		 * is the first address to be added.
    341  1.2   itojun 		 */
    342  1.2   itojun 		s = splimp();
    343  1.2   itojun 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
    344  1.2   itojun 		splx(s);
    345  1.2   itojun 
    346  1.2   itojun 		if (error) {
    347  1.2   itojun 			switch (error) {
    348  1.2   itojun 			case EAFNOSUPPORT:
    349  1.4  thorpej 				printf("%s: IPv6 not supported\n",
    350  1.2   itojun 					ifp->if_xname);
    351  1.2   itojun 				break;
    352  1.2   itojun 			default:
    353  1.4  thorpej 				printf("%s: SIOCSIFADDR error %d\n",
    354  1.2   itojun 					ifp->if_xname, error);
    355  1.2   itojun 				break;
    356  1.2   itojun 			}
    357  1.2   itojun 
    358  1.2   itojun 			/* undo changes */
    359  1.2   itojun 			TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    360  1.2   itojun 			if (oia)
    361  1.2   itojun 				oia->ia_next = ia->ia_next;
    362  1.2   itojun 			else
    363  1.2   itojun 				in6_ifaddr = ia->ia_next;
    364  1.2   itojun 			free(ia, M_IFADDR);
    365  1.2   itojun 			return;
    366  1.2   itojun 		}
    367  1.2   itojun 	}
    368  1.2   itojun 
    369  1.2   itojun 	/* add route to the interface. */
    370  1.2   itojun 	rtrequest(RTM_ADD,
    371  1.2   itojun 		  (struct sockaddr *)&ia->ia_addr,
    372  1.2   itojun 		  (struct sockaddr *)&ia->ia_addr,
    373  1.2   itojun 		  (struct sockaddr *)&ia->ia_prefixmask,
    374  1.2   itojun 		  RTF_UP|rtflag,
    375  1.2   itojun 		  (struct rtentry **)0);
    376  1.2   itojun 	ia->ia_flags |= IFA_ROUTE;
    377  1.2   itojun 
    378  1.2   itojun 	if (type == IN6_IFT_P2P || type == IN6_IFT_P2P802) {
    379  1.2   itojun 		/*
    380  1.2   itojun 		 * route local address to loopback
    381  1.2   itojun 		 */
    382  1.2   itojun 		bzero(&gate, sizeof(gate));
    383  1.2   itojun 		gate.sin6_len = sizeof(struct sockaddr_in6);
    384  1.2   itojun 		gate.sin6_family = AF_INET6;
    385  1.2   itojun 		gate.sin6_addr = in6addr_loopback;
    386  1.2   itojun 		bzero(&mask, sizeof(mask));
    387  1.2   itojun 		mask.sin6_len = sizeof(struct sockaddr_in6);
    388  1.2   itojun 		mask.sin6_family = AF_INET6;
    389  1.2   itojun 		mask.sin6_addr = in6mask64;
    390  1.2   itojun 		rtrequest(RTM_ADD,
    391  1.2   itojun 			  (struct sockaddr *)&ia->ia_addr,
    392  1.2   itojun 			  (struct sockaddr *)&gate,
    393  1.2   itojun 			  (struct sockaddr *)&mask,
    394  1.2   itojun 			  RTF_UP|RTF_HOST,
    395  1.2   itojun 			  (struct rtentry **)0);
    396  1.2   itojun 	}
    397  1.2   itojun 
    398  1.2   itojun 	/*
    399  1.2   itojun 	 * loopback address
    400  1.2   itojun 	 */
    401  1.2   itojun 	ib = (struct in6_ifaddr *)NULL;
    402  1.2   itojun 	if (type == IN6_IFT_LOOP) {
    403  1.2   itojun 		ib = (struct in6_ifaddr *)
    404  1.2   itojun 			malloc(sizeof(*ib), M_IFADDR, M_WAITOK);
    405  1.2   itojun 		bzero((caddr_t)ib, sizeof(*ib));
    406  1.2   itojun 		ib->ia_ifa.ifa_addr = (struct sockaddr *)&ib->ia_addr;
    407  1.2   itojun 		ib->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ib->ia_dstaddr;
    408  1.2   itojun 		ib->ia_ifa.ifa_netmask = (struct sockaddr *)&ib->ia_prefixmask;
    409  1.2   itojun 		ib->ia_ifp = ifp;
    410  1.2   itojun 
    411  1.2   itojun 		ia->ia_next = ib;
    412  1.2   itojun 		TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ib,
    413  1.2   itojun 			ifa_list);
    414  1.2   itojun 
    415  1.2   itojun 		ib->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
    416  1.2   itojun 		ib->ia_prefixmask.sin6_family = AF_INET6;
    417  1.2   itojun 		ib->ia_prefixmask.sin6_addr = in6mask128;
    418  1.2   itojun 		ib->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
    419  1.2   itojun 		ib->ia_addr.sin6_family = AF_INET6;
    420  1.2   itojun 		ib->ia_addr.sin6_addr = in6addr_loopback;
    421  1.2   itojun #ifdef __bsdi__
    422  1.2   itojun 		/*
    423  1.2   itojun 		 * It is necessary to set the loopback address to the dstaddr
    424  1.2   itojun 		 * field at least for BSDI. Without this setting, the BSDI
    425  1.2   itojun 		 * version of ifa_ifwithroute() rejects to add a route
    426  1.2   itojun 		 * to the loopback interface.
    427  1.2   itojun 		 */
    428  1.2   itojun 		ib->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
    429  1.2   itojun 		ib->ia_dstaddr.sin6_family = AF_INET6;
    430  1.2   itojun 		ib->ia_dstaddr.sin6_addr = in6addr_loopback;
    431  1.2   itojun #endif /* __bsdi__ */
    432  1.2   itojun 
    433  1.2   itojun 		ib->ia_ifa.ifa_metric = ifp->if_metric;
    434  1.2   itojun 
    435  1.2   itojun 		rtrequest(RTM_ADD,
    436  1.2   itojun 			  (struct sockaddr *)&ib->ia_addr,
    437  1.2   itojun 			  (struct sockaddr *)&ib->ia_addr,
    438  1.2   itojun 			  (struct sockaddr *)&ib->ia_prefixmask,
    439  1.2   itojun 			  RTF_UP|RTF_HOST,
    440  1.2   itojun 			  (struct rtentry **)0);
    441  1.2   itojun 
    442  1.2   itojun 		ib->ia_flags |= IFA_ROUTE;
    443  1.2   itojun 	}
    444  1.2   itojun 
    445  1.2   itojun 	/*
    446  1.2   itojun 	 * join multicast
    447  1.2   itojun 	 */
    448  1.2   itojun 	if (ifp->if_flags & IFF_MULTICAST) {
    449  1.2   itojun 		int error;	/* not used */
    450  1.2   itojun 
    451  1.2   itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
    452  1.2   itojun 		/* Restore saved multicast addresses(if any). */
    453  1.2   itojun 		in6_restoremkludge(ia, ifp);
    454  1.2   itojun #endif
    455  1.2   itojun 
    456  1.2   itojun 		bzero(&mltmask, sizeof(mltmask));
    457  1.2   itojun 		mltmask.sin6_len = sizeof(struct sockaddr_in6);
    458  1.2   itojun 		mltmask.sin6_family = AF_INET6;
    459  1.2   itojun 		mltmask.sin6_addr = in6mask32;
    460  1.2   itojun 
    461  1.2   itojun 		/*
    462  1.2   itojun 		 * join link-local all-nodes address
    463  1.2   itojun 		 */
    464  1.2   itojun 		bzero(&mltaddr, sizeof(mltaddr));
    465  1.2   itojun 		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
    466  1.2   itojun 		mltaddr.sin6_family = AF_INET6;
    467  1.2   itojun 		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
    468  1.2   itojun 		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
    469  1.2   itojun 		rtrequest(RTM_ADD,
    470  1.2   itojun 			  (struct sockaddr *)&mltaddr,
    471  1.2   itojun 			  (struct sockaddr *)&ia->ia_addr,
    472  1.2   itojun 			  (struct sockaddr *)&mltmask,
    473  1.2   itojun 			  RTF_UP|RTF_CLONING,  /* xxx */
    474  1.2   itojun 			  (struct rtentry **)0);
    475  1.2   itojun 		(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
    476  1.2   itojun 
    477  1.2   itojun 		if (type == IN6_IFT_LOOP) {
    478  1.2   itojun 			/*
    479  1.2   itojun 			 * join node-local all-nodes address
    480  1.2   itojun 			 */
    481  1.2   itojun 			mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
    482  1.2   itojun 			rtrequest(RTM_ADD,
    483  1.2   itojun 				  (struct sockaddr *)&mltaddr,
    484  1.2   itojun 				  (struct sockaddr *)&ib->ia_addr,
    485  1.2   itojun 				  (struct sockaddr *)&mltmask,
    486  1.2   itojun 				  RTF_UP,
    487  1.2   itojun 				  (struct rtentry **)0);
    488  1.2   itojun 			(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
    489  1.2   itojun 		} else {
    490  1.2   itojun 			/*
    491  1.2   itojun 			 * join solicited multicast address
    492  1.2   itojun 			 */
    493  1.2   itojun 			bzero(&llsol, sizeof(llsol));
    494  1.2   itojun 			llsol.s6_addr16[0] = htons(0xff02);
    495  1.2   itojun 			llsol.s6_addr16[1] = htons(ifp->if_index);
    496  1.2   itojun 			llsol.s6_addr32[1] = 0;
    497  1.2   itojun 			llsol.s6_addr32[2] = htonl(1);
    498  1.2   itojun 			llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
    499  1.2   itojun 			llsol.s6_addr8[12] = 0xff;
    500  1.2   itojun 			(void)in6_addmulti(&llsol, ifp, &error);
    501  1.2   itojun 		}
    502  1.2   itojun 	}
    503  1.2   itojun 
    504  1.2   itojun 	/* update dynamically. */
    505  1.2   itojun 	in6_iflladdr[ifp->if_index] = &ia->ia_addr.sin6_addr;
    506  1.2   itojun 	if (in6_maxmtu < ifp->if_mtu)
    507  1.2   itojun 		in6_maxmtu = ifp->if_mtu;
    508  1.2   itojun 
    509  1.2   itojun 	/* initialize NDP variables */
    510  1.2   itojun 	nd6_ifattach(ifp);
    511  1.2   itojun 
    512  1.2   itojun 	/* mark the address TENTATIVE, if needed. */
    513  1.2   itojun 	switch (ifp->if_type) {
    514  1.2   itojun 	case IFT_ETHER:
    515  1.2   itojun 	case IFT_FDDI:
    516  1.2   itojun #if 0
    517  1.2   itojun 	case IFT_ATM:
    518  1.2   itojun 	case IFT_SLIP:
    519  1.2   itojun 	case IFT_PPP:
    520  1.2   itojun #endif
    521  1.2   itojun 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
    522  1.2   itojun 		/* nd6_dad_start() will be called in in6_if_up */
    523  1.2   itojun 		break;
    524  1.2   itojun 	case IFT_GIF:
    525  1.2   itojun 	case IFT_LOOP:
    526  1.2   itojun 	case IFT_FAITH:
    527  1.2   itojun 	default:
    528  1.2   itojun 		break;
    529  1.2   itojun 	}
    530  1.2   itojun 
    531  1.2   itojun 	return;
    532  1.2   itojun }
    533  1.2   itojun 
    534  1.2   itojun void
    535  1.2   itojun in6_ifdetach(ifp)
    536  1.2   itojun 	struct ifnet *ifp;
    537  1.2   itojun {
    538  1.2   itojun 	struct in6_ifaddr *ia, *oia;
    539  1.2   itojun 	struct ifaddr *ifa;
    540  1.2   itojun 	struct rtentry *rt;
    541  1.2   itojun 	short rtflags;
    542  1.2   itojun 
    543  1.2   itojun 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) {
    544  1.2   itojun 		if (ifa->ifa_addr->sa_family != AF_INET6
    545  1.2   itojun 		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
    546  1.2   itojun 			continue;
    547  1.2   itojun 		}
    548  1.2   itojun 
    549  1.2   itojun 		ia = (struct in6_ifaddr *)ifa;
    550  1.2   itojun 
    551  1.2   itojun 		/* remove from the routing table */
    552  1.2   itojun 		if ((ia->ia_flags & IFA_ROUTE)
    553  1.2   itojun 		 && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
    554  1.2   itojun 			rtflags = rt->rt_flags;
    555  1.2   itojun 			rtfree(rt);
    556  1.2   itojun 			rtrequest(RTM_DELETE,
    557  1.2   itojun 				(struct sockaddr *)&ia->ia_addr,
    558  1.2   itojun 				(struct sockaddr *)&ia->ia_addr,
    559  1.2   itojun 				(struct sockaddr *)&ia->ia_prefixmask,
    560  1.2   itojun 				rtflags, (struct rtentry **)0);
    561  1.2   itojun 		}
    562  1.2   itojun 
    563  1.2   itojun 		/* remove from the linked list */
    564  1.2   itojun 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    565  1.2   itojun 
    566  1.2   itojun 		/* also remove from the IPv6 address chain(itojun&jinmei) */
    567  1.2   itojun 		oia = ia;
    568  1.2   itojun 		if (oia == (ia = in6_ifaddr))
    569  1.2   itojun 			in6_ifaddr = ia->ia_next;
    570  1.2   itojun 		else {
    571  1.2   itojun 			while (ia->ia_next && (ia->ia_next != oia))
    572  1.2   itojun 				ia = ia->ia_next;
    573  1.2   itojun 			if (ia->ia_next)
    574  1.2   itojun 				ia->ia_next = oia->ia_next;
    575  1.4  thorpej #ifdef DEBUG
    576  1.2   itojun 			else
    577  1.4  thorpej 				printf("%s: didn't unlink in6ifaddr from "
    578  1.4  thorpej 				    "list\n", ifp->if_xname);
    579  1.4  thorpej #endif
    580  1.2   itojun 		}
    581  1.2   itojun 
    582  1.2   itojun 		free(ia, M_IFADDR);
    583  1.2   itojun 	}
    584  1.2   itojun }
    585