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