Home | History | Annotate | Line # | Download | only in netinet6
in6_ifattach.c revision 1.16
      1 /*	$NetBSD: in6_ifattach.c,v 1.16 2000/02/02 13:44:06 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 void
    225 in6_ifattach(ifp, type, laddr, noloop)
    226 	struct ifnet *ifp;
    227 	u_int type;
    228 	caddr_t laddr;
    229 	/* size_t laddrlen; */
    230 	int noloop;
    231 {
    232 	static size_t if_indexlim = 8;
    233 	struct sockaddr_in6 mltaddr;
    234 	struct sockaddr_in6 mltmask;
    235 	struct sockaddr_in6 gate;
    236 	struct sockaddr_in6 mask;
    237 
    238 	struct in6_ifaddr *ia, *ib, *oia;
    239 	struct ifaddr *ifa;
    240 	int rtflag = 0;
    241 
    242 	if (type == IN6_IFT_P2P && found_first_ifid == 0) {
    243 		printf("%s: no ifid available for IPv6 link-local address\n",
    244 			if_name(ifp));
    245 #if 0
    246 		return;
    247 #else
    248 		/* last resort */
    249 		if (gen_rand_eui64(first_ifid) == 0) {
    250 			printf("%s: using random value as EUI64: "
    251 				"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
    252 				if_name(ifp),
    253 				first_ifid[0], first_ifid[1],
    254 				first_ifid[2], first_ifid[3],
    255 				first_ifid[4], first_ifid[5],
    256 				first_ifid[6], first_ifid[7]);
    257 			/*
    258 			 * invert u bit to convert EUI64 to RFC2373 interface
    259 			 * ID.
    260 			 */
    261 			first_ifid[0] ^= 0x02;
    262 
    263 			found_first_ifid = 1;
    264 		}
    265 #endif
    266 	}
    267 
    268 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
    269 		printf("%s: not multicast capable, IPv6 not enabled\n",
    270 			if_name(ifp));
    271 		return;
    272 	}
    273 
    274 	/*
    275 	 * We have some arrays that should be indexed by if_index.
    276 	 * since if_index will grow dynamically, they should grow too.
    277 	 *	struct in6_ifstat **in6_ifstat
    278 	 *	struct icmp6_ifstat **icmp6_ifstat
    279 	 */
    280 	if (in6_ifstat == NULL || icmp6_ifstat == NULL
    281 	 || if_index >= if_indexlim) {
    282 		size_t n;
    283 		caddr_t q;
    284 		size_t olim;
    285 
    286 		olim = if_indexlim;
    287 		while (if_index >= if_indexlim)
    288 			if_indexlim <<= 1;
    289 
    290 		/* grow in6_ifstat */
    291 		n = if_indexlim * sizeof(struct in6_ifstat *);
    292 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    293 		bzero(q, n);
    294 		if (in6_ifstat) {
    295 			bcopy((caddr_t)in6_ifstat, q,
    296 				olim * sizeof(struct in6_ifstat *));
    297 			free((caddr_t)in6_ifstat, M_IFADDR);
    298 		}
    299 		in6_ifstat = (struct in6_ifstat **)q;
    300 		in6_ifstatmax = if_indexlim;
    301 
    302 		/* grow icmp6_ifstat */
    303 		n = if_indexlim * sizeof(struct icmp6_ifstat *);
    304 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    305 		bzero(q, n);
    306 		if (icmp6_ifstat) {
    307 			bcopy((caddr_t)icmp6_ifstat, q,
    308 				olim * sizeof(struct icmp6_ifstat *));
    309 			free((caddr_t)icmp6_ifstat, M_IFADDR);
    310 		}
    311 		icmp6_ifstat = (struct icmp6_ifstat **)q;
    312 		icmp6_ifstatmax = if_indexlim;
    313 	}
    314 
    315 	/*
    316 	 * To prevent to assign link-local address to PnP network
    317 	 * cards multiple times.
    318 	 * This is lengthy for P2P and LOOP but works.
    319 	 */
    320 	ifa = TAILQ_FIRST(&ifp->if_addrlist);
    321 	if (ifa != NULL) {
    322 		for ( ; ifa; ifa = TAILQ_NEXT(ifa, ifa_list)) {
    323 			if (ifa->ifa_addr->sa_family != AF_INET6)
    324 				continue;
    325 			if (IN6_IS_ADDR_LINKLOCAL(&satosin6(ifa->ifa_addr)->sin6_addr))
    326 				return;
    327 		}
    328 	} else {
    329 		TAILQ_INIT(&ifp->if_addrlist);
    330 	}
    331 
    332 	/*
    333 	 * link-local address
    334 	 */
    335 	ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
    336 	bzero((caddr_t)ia, sizeof(*ia));
    337 	ia->ia_ifa.ifa_addr =    (struct sockaddr *)&ia->ia_addr;
    338 	ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
    339 	ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
    340 	ia->ia_ifp = ifp;
    341 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    342 	IFAREF((struct ifaddr *)ia);
    343 
    344 	/*
    345 	 * Also link into the IPv6 address chain beginning with in6_ifaddr.
    346 	 * kazu opposed it, but itojun & jinmei wanted.
    347 	 */
    348 	if ((oia = in6_ifaddr) != NULL) {
    349 		for (; oia->ia_next; oia = oia->ia_next)
    350 			continue;
    351 		oia->ia_next = ia;
    352 	} else
    353 		in6_ifaddr = ia;
    354 	IFAREF((struct ifaddr *)ia);
    355 
    356 	ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
    357 	ia->ia_prefixmask.sin6_family = AF_INET6;
    358 	ia->ia_prefixmask.sin6_addr = in6mask64;
    359 
    360 	bzero(&ia->ia_addr, sizeof(struct sockaddr_in6));
    361 	ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
    362 	ia->ia_addr.sin6_family = AF_INET6;
    363 	ia->ia_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
    364 	ia->ia_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
    365 	ia->ia_addr.sin6_addr.s6_addr32[1] = 0;
    366 
    367 	switch (type) {
    368 	case IN6_IFT_LOOP:
    369 		ia->ia_addr.sin6_addr.s6_addr32[2] = 0;
    370 		ia->ia_addr.sin6_addr.s6_addr32[3] = htonl(1);
    371 		break;
    372 	case IN6_IFT_802:
    373 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
    374 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
    375 		rtflag = RTF_CLONING;
    376 		/* fall through */
    377 	case IN6_IFT_P2P802:
    378 		if (laddr == NULL)
    379 			break;
    380 		/* XXX use laddrlen */
    381 		if (laddr_to_eui64(&ia->ia_addr.sin6_addr.s6_addr8[8],
    382 				laddr, 6) != 0) {
    383 			break;
    384 		}
    385 		/* invert u bit to convert EUI64 to RFC2373 interface ID. */
    386 		ia->ia_addr.sin6_addr.s6_addr8[8] ^= 0x02;
    387 		if (found_first_ifid == 0)
    388 			in6_ifattach_getifid(ifp);
    389 		bzero(&ia->ia_dstaddr, sizeof(struct sockaddr_in6));
    390 		ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
    391 		ia->ia_dstaddr.sin6_family = AF_INET6;
    392 		break;
    393 	case IN6_IFT_P2P:
    394 		bcopy((caddr_t)first_ifid,
    395 		      (caddr_t)&ia->ia_addr.sin6_addr.s6_addr8[8],
    396 		      IFID_LEN);
    397 		bzero(&ia->ia_dstaddr, sizeof(struct sockaddr_in6));
    398 		ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
    399 		ia->ia_dstaddr.sin6_family = AF_INET6;
    400 		break;
    401 	case IN6_IFT_ARCNET:
    402 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
    403 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
    404 		rtflag = RTF_CLONING;
    405 		if (laddr == NULL)
    406 			break;
    407 
    408 		/* make non-global IF id out of link-level address */
    409 		bzero(&ia->ia_addr.sin6_addr.s6_addr8[8], 7);
    410 		ia->ia_addr.sin6_addr.s6_addr8[15] = *laddr;
    411 	}
    412 
    413 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    414 
    415 	if (ifp->if_ioctl != NULL) {
    416 		int s;
    417 		int error;
    418 
    419 		/*
    420 		 * give the interface a chance to initialize, in case this
    421 		 * is the first address to be added.
    422 		 */
    423 		s = splimp();
    424 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
    425 		splx(s);
    426 
    427 		if (error) {
    428 			switch (error) {
    429 			case EAFNOSUPPORT:
    430 				printf("%s: IPv6 not supported\n",
    431 					if_name(ifp));
    432 				break;
    433 			default:
    434 				printf("%s: SIOCSIFADDR error %d\n",
    435 					if_name(ifp), error);
    436 				break;
    437 			}
    438 
    439 			/* undo changes */
    440 			TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    441 			IFAFREE((struct ifaddr *)ia);
    442 			if (oia)
    443 				oia->ia_next = ia->ia_next;
    444 			else
    445 				in6_ifaddr = ia->ia_next;
    446 			IFAFREE((struct ifaddr *)ia);
    447 			return;
    448 		}
    449 	}
    450 
    451 	/* add route to the interface. */
    452 	rtrequest(RTM_ADD,
    453 		  (struct sockaddr *)&ia->ia_addr,
    454 		  (struct sockaddr *)&ia->ia_addr,
    455 		  (struct sockaddr *)&ia->ia_prefixmask,
    456 		  RTF_UP|rtflag,
    457 		  (struct rtentry **)0);
    458 	ia->ia_flags |= IFA_ROUTE;
    459 
    460 	if (type == IN6_IFT_P2P || type == IN6_IFT_P2P802) {
    461 		/*
    462 		 * route local address to loopback
    463 		 */
    464 		bzero(&gate, sizeof(gate));
    465 		gate.sin6_len = sizeof(struct sockaddr_in6);
    466 		gate.sin6_family = AF_INET6;
    467 		gate.sin6_addr = in6addr_loopback;
    468 		bzero(&mask, sizeof(mask));
    469 		mask.sin6_len = sizeof(struct sockaddr_in6);
    470 		mask.sin6_family = AF_INET6;
    471 		mask.sin6_addr = in6mask64;
    472 		rtrequest(RTM_ADD,
    473 			  (struct sockaddr *)&ia->ia_addr,
    474 			  (struct sockaddr *)&gate,
    475 			  (struct sockaddr *)&mask,
    476 			  RTF_UP|RTF_HOST,
    477 			  (struct rtentry **)0);
    478 	}
    479 
    480 	/*
    481 	 * loopback address
    482 	 */
    483 	ib = (struct in6_ifaddr *)NULL;
    484 	if (type == IN6_IFT_LOOP) {
    485 		ib = (struct in6_ifaddr *)
    486 			malloc(sizeof(*ib), M_IFADDR, M_WAITOK);
    487 		bzero((caddr_t)ib, sizeof(*ib));
    488 		ib->ia_ifa.ifa_addr = (struct sockaddr *)&ib->ia_addr;
    489 		ib->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ib->ia_dstaddr;
    490 		ib->ia_ifa.ifa_netmask = (struct sockaddr *)&ib->ia_prefixmask;
    491 		ib->ia_ifp = ifp;
    492 
    493 		ia->ia_next = ib;
    494 		TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ib,
    495 			ifa_list);
    496 
    497 		ib->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
    498 		ib->ia_prefixmask.sin6_family = AF_INET6;
    499 		ib->ia_prefixmask.sin6_addr = in6mask128;
    500 		ib->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
    501 		ib->ia_addr.sin6_family = AF_INET6;
    502 		ib->ia_addr.sin6_addr = in6addr_loopback;
    503 
    504 		ib->ia_ifa.ifa_metric = ifp->if_metric;
    505 
    506 		rtrequest(RTM_ADD,
    507 			  (struct sockaddr *)&ib->ia_addr,
    508 			  (struct sockaddr *)&ib->ia_addr,
    509 			  (struct sockaddr *)&ib->ia_prefixmask,
    510 			  RTF_UP|RTF_HOST,
    511 			  (struct rtentry **)0);
    512 
    513 		ib->ia_flags |= IFA_ROUTE;
    514 	}
    515 
    516 	/*
    517 	 * join multicast
    518 	 */
    519 	if (ifp->if_flags & IFF_MULTICAST) {
    520 		int error;	/* not used */
    521 
    522 		/* Restore saved multicast addresses(if any). */
    523 		in6_restoremkludge(ia, ifp);
    524 
    525 		bzero(&mltmask, sizeof(mltmask));
    526 		mltmask.sin6_len = sizeof(struct sockaddr_in6);
    527 		mltmask.sin6_family = AF_INET6;
    528 		mltmask.sin6_addr = in6mask32;
    529 
    530 		/*
    531 		 * join link-local all-nodes address
    532 		 */
    533 		bzero(&mltaddr, sizeof(mltaddr));
    534 		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
    535 		mltaddr.sin6_family = AF_INET6;
    536 		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
    537 		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
    538 		rtrequest(RTM_ADD,
    539 			  (struct sockaddr *)&mltaddr,
    540 			  (struct sockaddr *)&ia->ia_addr,
    541 			  (struct sockaddr *)&mltmask,
    542 			  RTF_UP|RTF_CLONING,  /* xxx */
    543 			  (struct rtentry **)0);
    544 		(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
    545 
    546 		if (type == IN6_IFT_LOOP) {
    547 			/*
    548 			 * join node-local all-nodes address
    549 			 */
    550 			mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
    551 			rtrequest(RTM_ADD,
    552 				  (struct sockaddr *)&mltaddr,
    553 				  (struct sockaddr *)&ib->ia_addr,
    554 				  (struct sockaddr *)&mltmask,
    555 				  RTF_UP,
    556 				  (struct rtentry **)0);
    557 			(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
    558 		} else {
    559 			/*
    560 			 * join solicited multicast address
    561 			 */
    562 			bzero(&llsol, sizeof(llsol));
    563 			llsol.s6_addr16[0] = htons(0xff02);
    564 			llsol.s6_addr16[1] = htons(ifp->if_index);
    565 			llsol.s6_addr32[1] = 0;
    566 			llsol.s6_addr32[2] = htonl(1);
    567 			llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
    568 			llsol.s6_addr8[12] = 0xff;
    569 			(void)in6_addmulti(&llsol, ifp, &error);
    570 		}
    571 	}
    572 
    573 	/* update dynamically. */
    574 	if (in6_maxmtu < ifp->if_mtu)
    575 		in6_maxmtu = ifp->if_mtu;
    576 
    577 	if (in6_ifstat[ifp->if_index] == NULL) {
    578 		in6_ifstat[ifp->if_index] = (struct in6_ifstat *)
    579 			malloc(sizeof(struct in6_ifstat), M_IFADDR, M_WAITOK);
    580 		bzero(in6_ifstat[ifp->if_index], sizeof(struct in6_ifstat));
    581 	}
    582 	if (icmp6_ifstat[ifp->if_index] == NULL) {
    583 		icmp6_ifstat[ifp->if_index] = (struct icmp6_ifstat *)
    584 			malloc(sizeof(struct icmp6_ifstat), M_IFADDR, M_WAITOK);
    585 		bzero(icmp6_ifstat[ifp->if_index], sizeof(struct icmp6_ifstat));
    586 	}
    587 
    588 	/* initialize NDP variables */
    589 	nd6_ifattach(ifp);
    590 
    591 	/* mark the address TENTATIVE, if needed. */
    592 	switch (ifp->if_type) {
    593 	case IFT_ARCNET:
    594 	case IFT_ETHER:
    595 	case IFT_FDDI:
    596 #if 0
    597 	case IFT_ATM:
    598 	case IFT_SLIP:
    599 	case IFT_PPP:
    600 #endif
    601 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
    602 		/* nd6_dad_start() will be called in in6_if_up */
    603 		break;
    604 	case IFT_GIF:	/*XXX*/
    605 	case IFT_LOOP:
    606 	case IFT_FAITH:
    607 	default:
    608 		break;
    609 	}
    610 
    611 	return;
    612 }
    613 
    614 void
    615 in6_ifdetach(ifp)
    616 	struct ifnet *ifp;
    617 {
    618 	struct in6_ifaddr *ia, *oia;
    619 	struct ifaddr *ifa;
    620 	struct rtentry *rt;
    621 	short rtflags;
    622 	struct sockaddr_in6 sin6;
    623 
    624 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
    625 	{
    626 		if (ifa->ifa_addr->sa_family != AF_INET6
    627 		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
    628 			continue;
    629 		}
    630 
    631 		ia = (struct in6_ifaddr *)ifa;
    632 
    633 		/* remove from the routing table */
    634 		if ((ia->ia_flags & IFA_ROUTE)
    635 		 && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
    636 			rtflags = rt->rt_flags;
    637 			rtfree(rt);
    638 			rtrequest(RTM_DELETE,
    639 				(struct sockaddr *)&ia->ia_addr,
    640 				(struct sockaddr *)&ia->ia_addr,
    641 				(struct sockaddr *)&ia->ia_prefixmask,
    642 				rtflags, (struct rtentry **)0);
    643 		}
    644 
    645 		/* remove from the linked list */
    646 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    647 
    648 		/* also remove from the IPv6 address chain(itojun&jinmei) */
    649 		oia = ia;
    650 		if (oia == (ia = in6_ifaddr))
    651 			in6_ifaddr = ia->ia_next;
    652 		else {
    653 			while (ia->ia_next && (ia->ia_next != oia))
    654 				ia = ia->ia_next;
    655 			if (ia->ia_next)
    656 				ia->ia_next = oia->ia_next;
    657 #ifdef DEBUG
    658 			else
    659 				printf("%s: didn't unlink in6ifaddr from "
    660 				    "list\n", if_name(ifp));
    661 #endif
    662 		}
    663 
    664 		free(ia, M_IFADDR);
    665 	}
    666 
    667 	/* remove route to link-local allnodes multicast (ff02::1) */
    668 	bzero(&sin6, sizeof(sin6));
    669 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    670 	sin6.sin6_family = AF_INET6;
    671 	sin6.sin6_addr = in6addr_linklocal_allnodes;
    672 	sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
    673 	if ((rt = rtalloc1((struct sockaddr *)&sin6, 0)) != NULL) {
    674 		rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
    675 			rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
    676 		rtfree(rt);
    677 	}
    678 }
    679