Home | History | Annotate | Line # | Download | only in netinet6
in6.c revision 1.103
      1 /*	$NetBSD: in6.c,v 1.103 2006/05/18 09:05:51 liamjfoy Exp $	*/
      2 /*	$KAME: in6.c,v 1.198 2001/07/18 09:12:38 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1991, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  *
     61  *	@(#)in.c	8.2 (Berkeley) 11/15/93
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: in6.c,v 1.103 2006/05/18 09:05:51 liamjfoy Exp $");
     66 
     67 #include "opt_inet.h"
     68 #include "opt_pfil_hooks.h"
     69 
     70 #include <sys/param.h>
     71 #include <sys/ioctl.h>
     72 #include <sys/errno.h>
     73 #include <sys/malloc.h>
     74 #include <sys/socket.h>
     75 #include <sys/socketvar.h>
     76 #include <sys/sockio.h>
     77 #include <sys/systm.h>
     78 #include <sys/proc.h>
     79 #include <sys/time.h>
     80 #include <sys/kernel.h>
     81 #include <sys/syslog.h>
     82 #include <sys/kauth.h>
     83 
     84 #include <net/if.h>
     85 #include <net/if_types.h>
     86 #include <net/route.h>
     87 #include <net/if_dl.h>
     88 
     89 #include <netinet/in.h>
     90 #include <netinet/in_var.h>
     91 #include <net/if_ether.h>
     92 
     93 #include <netinet/ip6.h>
     94 #include <netinet6/ip6_var.h>
     95 #include <netinet6/nd6.h>
     96 #include <netinet6/mld6_var.h>
     97 #include <netinet6/ip6_mroute.h>
     98 #include <netinet6/in6_ifattach.h>
     99 #include <netinet6/scope6_var.h>
    100 
    101 #include <net/net_osdep.h>
    102 
    103 #ifdef PFIL_HOOKS
    104 #include <net/pfil.h>
    105 #endif
    106 
    107 MALLOC_DEFINE(M_IP6OPT, "ip6_options", "IPv6 options");
    108 
    109 /* enable backward compatibility code for obsoleted ioctls */
    110 #define COMPAT_IN6IFIOCTL
    111 
    112 /*
    113  * Definitions of some constant IP6 addresses.
    114  */
    115 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
    116 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
    117 const struct in6_addr in6addr_nodelocal_allnodes =
    118 	IN6ADDR_NODELOCAL_ALLNODES_INIT;
    119 const struct in6_addr in6addr_linklocal_allnodes =
    120 	IN6ADDR_LINKLOCAL_ALLNODES_INIT;
    121 const struct in6_addr in6addr_linklocal_allrouters =
    122 	IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
    123 
    124 const struct in6_addr in6mask0 = IN6MASK0;
    125 const struct in6_addr in6mask32 = IN6MASK32;
    126 const struct in6_addr in6mask64 = IN6MASK64;
    127 const struct in6_addr in6mask96 = IN6MASK96;
    128 const struct in6_addr in6mask128 = IN6MASK128;
    129 
    130 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
    131 				     0, 0, IN6ADDR_ANY_INIT, 0};
    132 
    133 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
    134 	struct ifnet *, struct proc *));
    135 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
    136 	struct sockaddr_in6 *, int));
    137 static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
    138 
    139 /*
    140  * Subroutine for in6_ifaddloop() and in6_ifremloop().
    141  * This routine does actual work.
    142  */
    143 static void
    144 in6_ifloop_request(int cmd, struct ifaddr *ifa)
    145 {
    146 	struct sockaddr_in6 lo_sa;
    147 	struct sockaddr_in6 all1_sa;
    148 	struct rtentry *nrt = NULL;
    149 	int e;
    150 
    151 	bzero(&lo_sa, sizeof(lo_sa));
    152 	bzero(&all1_sa, sizeof(all1_sa));
    153 	lo_sa.sin6_family = all1_sa.sin6_family = AF_INET6;
    154 	lo_sa.sin6_len = all1_sa.sin6_len = sizeof(struct sockaddr_in6);
    155 	lo_sa.sin6_addr = in6addr_loopback;
    156 	all1_sa.sin6_addr = in6mask128;
    157 
    158 	/*
    159 	 * We specify the address itself as the gateway, and set the
    160 	 * RTF_LLINFO flag, so that the corresponding host route would have
    161 	 * the flag, and thus applications that assume traditional behavior
    162 	 * would be happy.  Note that we assume the caller of the function
    163 	 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
    164 	 * which changes the outgoing interface to the loopback interface.
    165 	 */
    166 	e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
    167 	    (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
    168 	if (e != 0) {
    169 		log(LOG_ERR, "in6_ifloop_request: "
    170 		    "%s operation failed for %s (errno=%d)\n",
    171 		    cmd == RTM_ADD ? "ADD" : "DELETE",
    172 		    ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
    173 		    e);
    174 	}
    175 
    176 	/*
    177 	 * Make sure rt_ifa be equal to IFA, the second argument of the
    178 	 * function.
    179 	 * We need this because when we refer to rt_ifa->ia6_flags in
    180 	 * ip6_input, we assume that the rt_ifa points to the address instead
    181 	 * of the loopback address.
    182 	 */
    183 	if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
    184 		IFAFREE(nrt->rt_ifa);
    185 		IFAREF(ifa);
    186 		nrt->rt_ifa = ifa;
    187 	}
    188 
    189 	/*
    190 	 * Report the addition/removal of the address to the routing socket.
    191 	 * XXX: since we called rtinit for a p2p interface with a destination,
    192 	 *      we end up reporting twice in such a case.  Should we rather
    193 	 *      omit the second report?
    194 	 */
    195 	if (nrt) {
    196 		rt_newaddrmsg(cmd, ifa, e, nrt);
    197 		if (cmd == RTM_DELETE) {
    198 			if (nrt->rt_refcnt <= 0) {
    199 				/* XXX: we should free the entry ourselves. */
    200 				nrt->rt_refcnt++;
    201 				rtfree(nrt);
    202 			}
    203 		} else {
    204 			/* the cmd must be RTM_ADD here */
    205 			nrt->rt_refcnt--;
    206 		}
    207 	}
    208 }
    209 
    210 /*
    211  * Add ownaddr as loopback rtentry.  We previously add the route only if
    212  * necessary (ex. on a p2p link).  However, since we now manage addresses
    213  * separately from prefixes, we should always add the route.  We can't
    214  * rely on the cloning mechanism from the corresponding interface route
    215  * any more.
    216  */
    217 void
    218 in6_ifaddloop(struct ifaddr *ifa)
    219 {
    220 	struct rtentry *rt;
    221 
    222 	/* If there is no loopback entry, allocate one. */
    223 	rt = rtalloc1(ifa->ifa_addr, 0);
    224 	if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
    225 	    (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
    226 		in6_ifloop_request(RTM_ADD, ifa);
    227 	if (rt)
    228 		rt->rt_refcnt--;
    229 }
    230 
    231 /*
    232  * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
    233  * if it exists.
    234  */
    235 void
    236 in6_ifremloop(struct ifaddr *ifa)
    237 {
    238 	struct in6_ifaddr *ia;
    239 	struct rtentry *rt;
    240 	int ia_count = 0;
    241 
    242 	/*
    243 	 * Some of BSD variants do not remove cloned routes
    244 	 * from an interface direct route, when removing the direct route
    245 	 * (see comments in net/net_osdep.h).  Even for variants that do remove
    246 	 * cloned routes, they could fail to remove the cloned routes when
    247 	 * we handle multple addresses that share a common prefix.
    248 	 * So, we should remove the route corresponding to the deleted address.
    249 	 */
    250 
    251 	/*
    252 	 * Delete the entry only if exact one ifa exists.  More than one ifa
    253 	 * can exist if we assign a same single address to multiple
    254 	 * (probably p2p) interfaces.
    255 	 * XXX: we should avoid such a configuration in IPv6...
    256 	 */
    257 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
    258 		if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
    259 			ia_count++;
    260 			if (ia_count > 1)
    261 				break;
    262 		}
    263 	}
    264 
    265 	if (ia_count == 1) {
    266 		/*
    267 		 * Before deleting, check if a corresponding loopbacked host
    268 		 * route surely exists.  With this check, we can avoid to
    269 		 * delete an interface direct route whose destination is same
    270 		 * as the address being removed.  This can happen when removing
    271 		 * a subnet-router anycast address on an interface attahced
    272 		 * to a shared medium.
    273 		 */
    274 		rt = rtalloc1(ifa->ifa_addr, 0);
    275 		if (rt != NULL && (rt->rt_flags & RTF_HOST) != 0 &&
    276 		    (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
    277 			rt->rt_refcnt--;
    278 			in6_ifloop_request(RTM_DELETE, ifa);
    279 		}
    280 	}
    281 }
    282 
    283 int
    284 in6_mask2len(mask, lim0)
    285 	struct in6_addr *mask;
    286 	u_char *lim0;
    287 {
    288 	int x = 0, y;
    289 	u_char *lim = lim0, *p;
    290 
    291 	/* ignore the scope_id part */
    292 	if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
    293 		lim = (u_char *)mask + sizeof(*mask);
    294 	for (p = (u_char *)mask; p < lim; x++, p++) {
    295 		if (*p != 0xff)
    296 			break;
    297 	}
    298 	y = 0;
    299 	if (p < lim) {
    300 		for (y = 0; y < 8; y++) {
    301 			if ((*p & (0x80 >> y)) == 0)
    302 				break;
    303 		}
    304 	}
    305 
    306 	/*
    307 	 * when the limit pointer is given, do a stricter check on the
    308 	 * remaining bits.
    309 	 */
    310 	if (p < lim) {
    311 		if (y != 0 && (*p & (0x00ff >> y)) != 0)
    312 			return (-1);
    313 		for (p = p + 1; p < lim; p++)
    314 			if (*p != 0)
    315 				return (-1);
    316 	}
    317 
    318 	return x * 8 + y;
    319 }
    320 
    321 #define ifa2ia6(ifa)	((struct in6_ifaddr *)(ifa))
    322 #define ia62ifa(ia6)	(&((ia6)->ia_ifa))
    323 
    324 int
    325 in6_control(so, cmd, data, ifp, p)
    326 	struct	socket *so;
    327 	u_long cmd;
    328 	caddr_t	data;
    329 	struct ifnet *ifp;
    330 	struct proc *p;
    331 {
    332 	struct	in6_ifreq *ifr = (struct in6_ifreq *)data;
    333 	struct	in6_ifaddr *ia = NULL;
    334 	struct	in6_aliasreq *ifra = (struct in6_aliasreq *)data;
    335 	struct sockaddr_in6 *sa6;
    336 	int error, privileged;
    337 
    338 	privileged = 0;
    339 	if (p && !kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag))
    340 		privileged++;
    341 
    342 	switch (cmd) {
    343 	case SIOCGETSGCNT_IN6:
    344 	case SIOCGETMIFCNT_IN6:
    345 		return (mrt6_ioctl(cmd, data));
    346 	}
    347 
    348 	if (ifp == NULL)
    349 		return (EOPNOTSUPP);
    350 
    351 	switch (cmd) {
    352 	case SIOCSNDFLUSH_IN6:
    353 	case SIOCSPFXFLUSH_IN6:
    354 	case SIOCSRTRFLUSH_IN6:
    355 	case SIOCSDEFIFACE_IN6:
    356 	case SIOCSIFINFO_FLAGS:
    357 	case SIOCSIFINFO_IN6:
    358 		if (!privileged)
    359 			return (EPERM);
    360 		/* FALLTHROUGH */
    361 	case OSIOCGIFINFO_IN6:
    362 	case SIOCGIFINFO_IN6:
    363 	case SIOCGDRLST_IN6:
    364 	case SIOCGPRLST_IN6:
    365 	case SIOCGNBRINFO_IN6:
    366 	case SIOCGDEFIFACE_IN6:
    367 		return (nd6_ioctl(cmd, data, ifp));
    368 	}
    369 
    370 	switch (cmd) {
    371 	case SIOCSIFPREFIX_IN6:
    372 	case SIOCDIFPREFIX_IN6:
    373 	case SIOCAIFPREFIX_IN6:
    374 	case SIOCCIFPREFIX_IN6:
    375 	case SIOCSGIFPREFIX_IN6:
    376 	case SIOCGIFPREFIX_IN6:
    377 		log(LOG_NOTICE,
    378 		    "prefix ioctls are now invalidated. "
    379 		    "please use ifconfig.\n");
    380 		return (EOPNOTSUPP);
    381 	}
    382 
    383 	switch (cmd) {
    384 	case SIOCALIFADDR:
    385 	case SIOCDLIFADDR:
    386 		if (!privileged)
    387 			return (EPERM);
    388 		/* FALLTHROUGH */
    389 	case SIOCGLIFADDR:
    390 		return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
    391 	}
    392 
    393 	/*
    394 	 * Find address for this interface, if it exists.
    395 	 *
    396 	 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
    397 	 * only, and used the first interface address as the target of other
    398 	 * operations (without checking ifra_addr).  This was because netinet
    399 	 * code/API assumed at most 1 interface address per interface.
    400 	 * Since IPv6 allows a node to assign multiple addresses
    401 	 * on a single interface, we almost always look and check the
    402 	 * presence of ifra_addr, and reject invalid ones here.
    403 	 * It also decreases duplicated code among SIOC*_IN6 operations.
    404 	 */
    405 	switch (cmd) {
    406 	case SIOCAIFADDR_IN6:
    407 	case SIOCSIFPHYADDR_IN6:
    408 		sa6 = &ifra->ifra_addr;
    409 		break;
    410 	case SIOCSIFADDR_IN6:
    411 	case SIOCGIFADDR_IN6:
    412 	case SIOCSIFDSTADDR_IN6:
    413 	case SIOCSIFNETMASK_IN6:
    414 	case SIOCGIFDSTADDR_IN6:
    415 	case SIOCGIFNETMASK_IN6:
    416 	case SIOCDIFADDR_IN6:
    417 	case SIOCGIFPSRCADDR_IN6:
    418 	case SIOCGIFPDSTADDR_IN6:
    419 	case SIOCGIFAFLAG_IN6:
    420 	case SIOCSNDFLUSH_IN6:
    421 	case SIOCSPFXFLUSH_IN6:
    422 	case SIOCSRTRFLUSH_IN6:
    423 	case SIOCGIFALIFETIME_IN6:
    424 	case SIOCSIFALIFETIME_IN6:
    425 	case SIOCGIFSTAT_IN6:
    426 	case SIOCGIFSTAT_ICMP6:
    427 		sa6 = &ifr->ifr_addr;
    428 		break;
    429 	default:
    430 		sa6 = NULL;
    431 		break;
    432 	}
    433 	if (sa6 && sa6->sin6_family == AF_INET6) {
    434 		if (sa6->sin6_scope_id != 0)
    435 			error = sa6_embedscope(sa6, 0);
    436 		else
    437 			error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
    438 		if (error != 0)
    439 			return (error);
    440 		ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
    441 	} else
    442 		ia = NULL;
    443 
    444 	switch (cmd) {
    445 	case SIOCSIFADDR_IN6:
    446 	case SIOCSIFDSTADDR_IN6:
    447 	case SIOCSIFNETMASK_IN6:
    448 		/*
    449 		 * Since IPv6 allows a node to assign multiple addresses
    450 		 * on a single interface, SIOCSIFxxx ioctls are deprecated.
    451 		 */
    452 		return (EINVAL);
    453 
    454 	case SIOCDIFADDR_IN6:
    455 		/*
    456 		 * for IPv4, we look for existing in_ifaddr here to allow
    457 		 * "ifconfig if0 delete" to remove the first IPv4 address on
    458 		 * the interface.  For IPv6, as the spec allows multiple
    459 		 * interface address from the day one, we consider "remove the
    460 		 * first one" semantics to be not preferable.
    461 		 */
    462 		if (ia == NULL)
    463 			return (EADDRNOTAVAIL);
    464 		/* FALLTHROUGH */
    465 	case SIOCAIFADDR_IN6:
    466 		/*
    467 		 * We always require users to specify a valid IPv6 address for
    468 		 * the corresponding operation.
    469 		 */
    470 		if (ifra->ifra_addr.sin6_family != AF_INET6 ||
    471 		    ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
    472 			return (EAFNOSUPPORT);
    473 		if (!privileged)
    474 			return (EPERM);
    475 
    476 		break;
    477 
    478 	case SIOCGIFADDR_IN6:
    479 		/* This interface is basically deprecated. use SIOCGIFCONF. */
    480 		/* FALLTHROUGH */
    481 	case SIOCGIFAFLAG_IN6:
    482 	case SIOCGIFNETMASK_IN6:
    483 	case SIOCGIFDSTADDR_IN6:
    484 	case SIOCGIFALIFETIME_IN6:
    485 		/* must think again about its semantics */
    486 		if (ia == NULL)
    487 			return (EADDRNOTAVAIL);
    488 		break;
    489 	case SIOCSIFALIFETIME_IN6:
    490 	    {
    491 		struct in6_addrlifetime *lt;
    492 
    493 		if (!privileged)
    494 			return (EPERM);
    495 		if (ia == NULL)
    496 			return (EADDRNOTAVAIL);
    497 		/* sanity for overflow - beware unsigned */
    498 		lt = &ifr->ifr_ifru.ifru_lifetime;
    499 		if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
    500 		 && lt->ia6t_vltime + time.tv_sec < time.tv_sec) {
    501 			return EINVAL;
    502 		}
    503 		if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
    504 		 && lt->ia6t_pltime + time.tv_sec < time.tv_sec) {
    505 			return EINVAL;
    506 		}
    507 		break;
    508 	    }
    509 	}
    510 
    511 	switch (cmd) {
    512 
    513 	case SIOCGIFADDR_IN6:
    514 		ifr->ifr_addr = ia->ia_addr;
    515 		if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
    516 			return (error);
    517 		break;
    518 
    519 	case SIOCGIFDSTADDR_IN6:
    520 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    521 			return (EINVAL);
    522 		/*
    523 		 * XXX: should we check if ifa_dstaddr is NULL and return
    524 		 * an error?
    525 		 */
    526 		ifr->ifr_dstaddr = ia->ia_dstaddr;
    527 		if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
    528 			return (error);
    529 		break;
    530 
    531 	case SIOCGIFNETMASK_IN6:
    532 		ifr->ifr_addr = ia->ia_prefixmask;
    533 		break;
    534 
    535 	case SIOCGIFAFLAG_IN6:
    536 		ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
    537 		break;
    538 
    539 	case SIOCGIFSTAT_IN6:
    540 		if (ifp == NULL)
    541 			return EINVAL;
    542 		bzero(&ifr->ifr_ifru.ifru_stat,
    543 		    sizeof(ifr->ifr_ifru.ifru_stat));
    544 		ifr->ifr_ifru.ifru_stat =
    545 		    *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
    546 		break;
    547 
    548 	case SIOCGIFSTAT_ICMP6:
    549 		if (ifp == NULL)
    550 			return EINVAL;
    551 		bzero(&ifr->ifr_ifru.ifru_icmp6stat,
    552 		    sizeof(ifr->ifr_ifru.ifru_icmp6stat));
    553 		ifr->ifr_ifru.ifru_icmp6stat =
    554 		    *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
    555 		break;
    556 
    557 	case SIOCGIFALIFETIME_IN6:
    558 		ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
    559 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
    560 			time_t maxexpire;
    561 			struct in6_addrlifetime *retlt =
    562 			    &ifr->ifr_ifru.ifru_lifetime;
    563 
    564 			/*
    565 			 * XXX: adjust expiration time assuming time_t is
    566 			 * signed.
    567 			 */
    568 			maxexpire = (-1) &
    569 			    ~(1 << ((sizeof(maxexpire) * 8) - 1));
    570 			if (ia->ia6_lifetime.ia6t_vltime <
    571 			    maxexpire - ia->ia6_updatetime) {
    572 				retlt->ia6t_expire = ia->ia6_updatetime +
    573 				    ia->ia6_lifetime.ia6t_vltime;
    574 			} else
    575 				retlt->ia6t_expire = maxexpire;
    576 		}
    577 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
    578 			time_t maxexpire;
    579 			struct in6_addrlifetime *retlt =
    580 			    &ifr->ifr_ifru.ifru_lifetime;
    581 
    582 			/*
    583 			 * XXX: adjust expiration time assuming time_t is
    584 			 * signed.
    585 			 */
    586 			maxexpire = (-1) &
    587 			    ~(1 << ((sizeof(maxexpire) * 8) - 1));
    588 			if (ia->ia6_lifetime.ia6t_pltime <
    589 			    maxexpire - ia->ia6_updatetime) {
    590 				retlt->ia6t_preferred = ia->ia6_updatetime +
    591 				    ia->ia6_lifetime.ia6t_pltime;
    592 			} else
    593 				retlt->ia6t_preferred = maxexpire;
    594 		}
    595 		break;
    596 
    597 	case SIOCSIFALIFETIME_IN6:
    598 		ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
    599 		/* for sanity */
    600 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
    601 			ia->ia6_lifetime.ia6t_expire =
    602 				time.tv_sec + ia->ia6_lifetime.ia6t_vltime;
    603 		} else
    604 			ia->ia6_lifetime.ia6t_expire = 0;
    605 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
    606 			ia->ia6_lifetime.ia6t_preferred =
    607 				time.tv_sec + ia->ia6_lifetime.ia6t_pltime;
    608 		} else
    609 			ia->ia6_lifetime.ia6t_preferred = 0;
    610 		break;
    611 
    612 	case SIOCAIFADDR_IN6:
    613 	{
    614 		int i;
    615 		struct nd_prefixctl pr0;
    616 		struct nd_prefix *pr;
    617 
    618 		/* reject read-only flags */
    619 		if ((ifra->ifra_flags & IN6_IFF_DUPLICATED) != 0 ||
    620 		    (ifra->ifra_flags & IN6_IFF_DETACHED) != 0 ||
    621 		    (ifra->ifra_flags & IN6_IFF_NODAD) != 0 ||
    622 		    (ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0) {
    623 			return (EINVAL);
    624 		}
    625 		/*
    626 		 * first, make or update the interface address structure,
    627 		 * and link it to the list.
    628 		 */
    629 		if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
    630 			return (error);
    631 		if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
    632 		    == NULL) {
    633 		    	/*
    634 			 * this can happen when the user specify the 0 valid
    635 			 * lifetime.
    636 			 */
    637 			break;
    638 		}
    639 
    640 		/*
    641 		 * then, make the prefix on-link on the interface.
    642 		 * XXX: we'd rather create the prefix before the address, but
    643 		 * we need at least one address to install the corresponding
    644 		 * interface route, so we configure the address first.
    645 		 */
    646 
    647 		/*
    648 		 * convert mask to prefix length (prefixmask has already
    649 		 * been validated in in6_update_ifa().
    650 		 */
    651 		bzero(&pr0, sizeof(pr0));
    652 		pr0.ndpr_ifp = ifp;
    653 		pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
    654 		    NULL);
    655 		if (pr0.ndpr_plen == 128) {
    656 			break;	/* we don't need to install a host route. */
    657 		}
    658 		pr0.ndpr_prefix = ifra->ifra_addr;
    659 		/* apply the mask for safety. */
    660 		for (i = 0; i < 4; i++) {
    661 			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
    662 			    ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
    663 		}
    664 		/*
    665 		 * XXX: since we don't have an API to set prefix (not address)
    666 		 * lifetimes, we just use the same lifetimes as addresses.
    667 		 * The (temporarily) installed lifetimes can be overridden by
    668 		 * later advertised RAs (when accept_rtadv is non 0), which is
    669 		 * an intended behavior.
    670 		 */
    671 		pr0.ndpr_raf_onlink = 1; /* should be configurable? */
    672 		pr0.ndpr_raf_auto =
    673 		    ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
    674 		pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
    675 		pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
    676 
    677 		/* add the prefix if not yet. */
    678 		if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
    679 			/*
    680 			 * nd6_prelist_add will install the corresponding
    681 			 * interface route.
    682 			 */
    683 			if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
    684 				return (error);
    685 			if (pr == NULL) {
    686 				log(LOG_ERR, "nd6_prelist_add succeeded but "
    687 				    "no prefix\n");
    688 				return (EINVAL); /* XXX panic here? */
    689 			}
    690 		}
    691 
    692 		/* relate the address to the prefix */
    693 		if (ia->ia6_ndpr == NULL) {
    694 			ia->ia6_ndpr = pr;
    695 			pr->ndpr_refcnt++;
    696 
    697 			/*
    698 			 * If this is the first autoconf address from the
    699 			 * prefix, create a temporary address as well
    700 			 * (when required).
    701 			 */
    702 			if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
    703 			    ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
    704 				int e;
    705 				if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
    706 					log(LOG_NOTICE, "in6_control: failed "
    707 					    "to create a temporary address, "
    708 					    "errno=%d\n", e);
    709 				}
    710 			}
    711 		}
    712 
    713 		/*
    714 		 * this might affect the status of autoconfigured addresses,
    715 		 * that is, this address might make other addresses detached.
    716 		 */
    717 		pfxlist_onlink_check();
    718 
    719 #ifdef PFIL_HOOKS
    720 		(void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCAIFADDR_IN6,
    721 		    ifp, PFIL_IFADDR);
    722 #endif
    723 
    724 		break;
    725 	}
    726 
    727 	case SIOCDIFADDR_IN6:
    728 	{
    729 		struct nd_prefix *pr;
    730 
    731 		/*
    732 		 * If the address being deleted is the only one that owns
    733 		 * the corresponding prefix, expire the prefix as well.
    734 		 * XXX: theoretically, we don't have to worry about such
    735 		 * relationship, since we separate the address management
    736 		 * and the prefix management.  We do this, however, to provide
    737 		 * as much backward compatibility as possible in terms of
    738 		 * the ioctl operation.
    739 		 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
    740 		 */
    741 		pr = ia->ia6_ndpr;
    742 		in6_purgeaddr(&ia->ia_ifa);
    743 		if (pr && pr->ndpr_refcnt == 0)
    744 			prelist_remove(pr);
    745 #ifdef PFIL_HOOKS
    746 		(void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCDIFADDR_IN6,
    747 		    ifp, PFIL_IFADDR);
    748 #endif
    749 		break;
    750 	}
    751 
    752 	default:
    753 		if (ifp == NULL || ifp->if_ioctl == 0)
    754 			return (EOPNOTSUPP);
    755 		return ((*ifp->if_ioctl)(ifp, cmd, data));
    756 	}
    757 
    758 	return (0);
    759 }
    760 
    761 /*
    762  * Update parameters of an IPv6 interface address.
    763  * If necessary, a new entry is created and linked into address chains.
    764  * This function is separated from in6_control().
    765  * XXX: should this be performed under splnet()?
    766  */
    767 int
    768 in6_update_ifa(ifp, ifra, ia, flags)
    769 	struct ifnet *ifp;
    770 	struct in6_aliasreq *ifra;
    771 	struct in6_ifaddr *ia;
    772 	int flags;
    773 {
    774 	int error = 0, hostIsNew = 0, plen = -1;
    775 	struct in6_ifaddr *oia;
    776 	struct sockaddr_in6 dst6;
    777 	struct in6_addrlifetime *lt;
    778 	struct in6_multi_mship *imm;
    779 	struct in6_multi *in6m_sol;
    780 	struct rtentry *rt;
    781 	int dad_delay;
    782 
    783 	in6m_sol = NULL;
    784 
    785 	/* Validate parameters */
    786 	if (ifp == NULL || ifra == NULL) /* this maybe redundant */
    787 		return (EINVAL);
    788 
    789 	/*
    790 	 * The destination address for a p2p link must have a family
    791 	 * of AF_UNSPEC or AF_INET6.
    792 	 */
    793 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
    794 	    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
    795 	    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
    796 		return (EAFNOSUPPORT);
    797 	/*
    798 	 * validate ifra_prefixmask.  don't check sin6_family, netmask
    799 	 * does not carry fields other than sin6_len.
    800 	 */
    801 	if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
    802 		return (EINVAL);
    803 	/*
    804 	 * Because the IPv6 address architecture is classless, we require
    805 	 * users to specify a (non 0) prefix length (mask) for a new address.
    806 	 * We also require the prefix (when specified) mask is valid, and thus
    807 	 * reject a non-consecutive mask.
    808 	 */
    809 	if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
    810 		return (EINVAL);
    811 	if (ifra->ifra_prefixmask.sin6_len != 0) {
    812 		plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
    813 		    (u_char *)&ifra->ifra_prefixmask +
    814 		    ifra->ifra_prefixmask.sin6_len);
    815 		if (plen <= 0)
    816 			return (EINVAL);
    817 	} else {
    818 		/*
    819 		 * In this case, ia must not be NULL.  We just use its prefix
    820 		 * length.
    821 		 */
    822 		plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
    823 	}
    824 	/*
    825 	 * If the destination address on a p2p interface is specified,
    826 	 * and the address is a scoped one, validate/set the scope
    827 	 * zone identifier.
    828 	 */
    829 	dst6 = ifra->ifra_dstaddr;
    830 	if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
    831 	    (dst6.sin6_family == AF_INET6)) {
    832 		struct in6_addr in6_tmp;
    833 		u_int32_t zoneid;
    834 
    835 		in6_tmp = dst6.sin6_addr;
    836 		if (in6_setscope(&in6_tmp, ifp, &zoneid))
    837 			return (EINVAL); /* XXX: should be impossible */
    838 
    839 		if (dst6.sin6_scope_id != 0) {
    840 			if (dst6.sin6_scope_id != zoneid)
    841 				return (EINVAL);
    842 		} else		/* user omit to specify the ID. */
    843 			dst6.sin6_scope_id = zoneid;
    844 
    845 		/* convert into the internal form */
    846 		if (sa6_embedscope(&dst6, 0))
    847 			return (EINVAL); /* XXX: should be impossible */
    848 	}
    849 	/*
    850 	 * The destination address can be specified only for a p2p or a
    851 	 * loopback interface.  If specified, the corresponding prefix length
    852 	 * must be 128.
    853 	 */
    854 	if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
    855 #ifdef FORCE_P2PPLEN
    856 		int i;
    857 #endif
    858 
    859 		if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
    860 			/* XXX: noisy message */
    861 			nd6log((LOG_INFO, "in6_update_ifa: a destination can "
    862 			    "be specified for a p2p or a loopback IF only\n"));
    863 			return (EINVAL);
    864 		}
    865 		if (plen != 128) {
    866 			nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
    867 			    "be 128 when dstaddr is specified\n"));
    868 #ifdef FORCE_P2PPLEN
    869 			/*
    870 			 * To be compatible with old configurations,
    871 			 * such as ifconfig gif0 inet6 2001::1 2001::2
    872 			 * prefixlen 126, we override the specified
    873 			 * prefixmask as if the prefix length was 128.
    874 			 */
    875 			ifra->ifra_prefixmask.sin6_len =
    876 			    sizeof(struct sockaddr_in6);
    877 			for (i = 0; i < 4; i++)
    878 				ifra->ifra_prefixmask.sin6_addr.s6_addr32[i] =
    879 				    0xffffffff;
    880 			plen = 128;
    881 #else
    882 			return (EINVAL);
    883 #endif
    884 		}
    885 	}
    886 	/* lifetime consistency check */
    887 	lt = &ifra->ifra_lifetime;
    888 	if (lt->ia6t_pltime > lt->ia6t_vltime)
    889 		return (EINVAL);
    890 	if (lt->ia6t_vltime == 0) {
    891 		/*
    892 		 * the following log might be noisy, but this is a typical
    893 		 * configuration mistake or a tool's bug.
    894 		 */
    895 		nd6log((LOG_INFO,
    896 		    "in6_update_ifa: valid lifetime is 0 for %s\n",
    897 		    ip6_sprintf(&ifra->ifra_addr.sin6_addr)));
    898 
    899 		if (ia == NULL)
    900 			return (0); /* there's nothing to do */
    901 	}
    902 
    903 	/*
    904 	 * If this is a new address, allocate a new ifaddr and link it
    905 	 * into chains.
    906 	 */
    907 	if (ia == NULL) {
    908 		hostIsNew = 1;
    909 		/*
    910 		 * When in6_update_ifa() is called in a process of a received
    911 		 * RA, it is called under an interrupt context.  So, we should
    912 		 * call malloc with M_NOWAIT.
    913 		 */
    914 		ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
    915 		    M_NOWAIT);
    916 		if (ia == NULL)
    917 			return (ENOBUFS);
    918 		bzero((caddr_t)ia, sizeof(*ia));
    919 		LIST_INIT(&ia->ia6_memberships);
    920 		/* Initialize the address and masks, and put time stamp */
    921 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
    922 		ia->ia_addr.sin6_family = AF_INET6;
    923 		ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
    924 		ia->ia6_createtime = time.tv_sec;
    925 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
    926 			/*
    927 			 * XXX: some functions expect that ifa_dstaddr is not
    928 			 * NULL for p2p interfaces.
    929 			 */
    930 			ia->ia_ifa.ifa_dstaddr =
    931 			    (struct sockaddr *)&ia->ia_dstaddr;
    932 		} else {
    933 			ia->ia_ifa.ifa_dstaddr = NULL;
    934 		}
    935 		ia->ia_ifa.ifa_netmask =
    936 		    (struct sockaddr *)&ia->ia_prefixmask;
    937 
    938 		ia->ia_ifp = ifp;
    939 		if ((oia = in6_ifaddr) != NULL) {
    940 			for ( ; oia->ia_next; oia = oia->ia_next)
    941 				continue;
    942 			oia->ia_next = ia;
    943 		} else
    944 			in6_ifaddr = ia;
    945 		/* gain a refcnt for the link from in6_ifaddr */
    946 		IFAREF(&ia->ia_ifa);
    947 
    948 		TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
    949 				  ifa_list);
    950 		/* gain another refcnt for the link from if_addrlist */
    951 		IFAREF(&ia->ia_ifa);
    952 	}
    953 
    954 	/* update timestamp */
    955 	ia->ia6_updatetime = time.tv_sec;
    956 
    957 	/* set prefix mask */
    958 	if (ifra->ifra_prefixmask.sin6_len) {
    959 		/*
    960 		 * We prohibit changing the prefix length of an existing
    961 		 * address, because
    962 		 * + such an operation should be rare in IPv6, and
    963 		 * + the operation would confuse prefix management.
    964 		 */
    965 		if (ia->ia_prefixmask.sin6_len &&
    966 		    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
    967 			nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
    968 			    " existing (%s) address should not be changed\n",
    969 			    ip6_sprintf(&ia->ia_addr.sin6_addr)));
    970 			error = EINVAL;
    971 			goto unlink;
    972 		}
    973 		ia->ia_prefixmask = ifra->ifra_prefixmask;
    974 	}
    975 
    976 	/*
    977 	 * If a new destination address is specified, scrub the old one and
    978 	 * install the new destination.  Note that the interface must be
    979 	 * p2p or loopback (see the check above.)
    980 	 */
    981 	if (dst6.sin6_family == AF_INET6 &&
    982 	    !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
    983 		if ((ia->ia_flags & IFA_ROUTE) != 0 &&
    984 		    rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST) != 0) {
    985 			nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
    986 			    "a route to the old destination: %s\n",
    987 			    ip6_sprintf(&ia->ia_addr.sin6_addr)));
    988 			/* proceed anyway... */
    989 		} else
    990 			ia->ia_flags &= ~IFA_ROUTE;
    991 		ia->ia_dstaddr = dst6;
    992 	}
    993 
    994 	/*
    995 	 * Set lifetimes.  We do not refer to ia6t_expire and ia6t_preferred
    996 	 * to see if the address is deprecated or invalidated, but initialize
    997 	 * these members for applications.
    998 	 */
    999 	ia->ia6_lifetime = ifra->ifra_lifetime;
   1000 	if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
   1001 		ia->ia6_lifetime.ia6t_expire =
   1002 		    time.tv_sec + ia->ia6_lifetime.ia6t_vltime;
   1003 	} else
   1004 		ia->ia6_lifetime.ia6t_expire = 0;
   1005 	if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
   1006 		ia->ia6_lifetime.ia6t_preferred =
   1007 		    time.tv_sec + ia->ia6_lifetime.ia6t_pltime;
   1008 	} else
   1009 		ia->ia6_lifetime.ia6t_preferred = 0;
   1010 
   1011 	/* reset the interface and routing table appropriately. */
   1012 	if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
   1013 		goto unlink;
   1014 
   1015 	/*
   1016 	 * configure address flags.
   1017 	 */
   1018 	ia->ia6_flags = ifra->ifra_flags;
   1019 	/*
   1020 	 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
   1021 	 * userland, make it deprecated.
   1022 	 */
   1023 	if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
   1024 		ia->ia6_lifetime.ia6t_pltime = 0;
   1025 		ia->ia6_lifetime.ia6t_preferred = time.tv_sec;
   1026 	}
   1027 
   1028 	/*
   1029 	 * Make the address tentative before joining multicast addresses,
   1030 	 * so that corresponding MLD responses would not have a tentative
   1031 	 * source address.
   1032 	 */
   1033 	ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/* safety */
   1034 	if (hostIsNew && in6if_do_dad(ifp))
   1035 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
   1036 
   1037 	/*
   1038 	 * We are done if we have simply modified an existing address.
   1039 	 */
   1040 	if (!hostIsNew)
   1041 		return (error);
   1042 
   1043 	/*
   1044 	 * Beyond this point, we should call in6_purgeaddr upon an error,
   1045 	 * not just go to unlink.
   1046 	 */
   1047 
   1048 	/* join necessary multicast groups */
   1049 	if ((ifp->if_flags & IFF_MULTICAST) != 0) {
   1050 		struct sockaddr_in6 mltaddr, mltmask;
   1051 		struct in6_addr llsol;
   1052 
   1053 		/* join solicited multicast addr for new host id */
   1054 		bzero(&llsol, sizeof(struct in6_addr));
   1055 		llsol.s6_addr16[0] = htons(0xff02);
   1056 		llsol.s6_addr32[1] = 0;
   1057 		llsol.s6_addr32[2] = htonl(1);
   1058 		llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
   1059 		llsol.s6_addr8[12] = 0xff;
   1060 		if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
   1061 			/* XXX: should not happen */
   1062 			log(LOG_ERR, "in6_update_ifa: "
   1063 			    "in6_setscope failed\n");
   1064 			goto cleanup;
   1065 		}
   1066 		dad_delay = 0;
   1067 		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
   1068 			/*
   1069 			 * We need a random delay for DAD on the address
   1070 			 * being configured.  It also means delaying
   1071 			 * transmission of the corresponding MLD report to
   1072 			 * avoid report collision.
   1073 			 * [draft-ietf-ipv6-rfc2462bis-02.txt]
   1074 			 */
   1075 			dad_delay = arc4random() %
   1076 			    (MAX_RTR_SOLICITATION_DELAY * hz);
   1077 		}
   1078 
   1079 #define	MLTMASK_LEN  4	/* mltmask's masklen (=32bit=4octet) */
   1080 		/* join solicited multicast addr for new host id */
   1081 		imm = in6_joingroup(ifp, &llsol, &error, dad_delay);
   1082 		if (!imm) {
   1083 			nd6log((LOG_ERR,
   1084 			    "in6_update_ifa: addmulti "
   1085 			    "failed for %s on %s (errno=%d)\n",
   1086 			    ip6_sprintf(&llsol), if_name(ifp), error));
   1087 			goto cleanup;
   1088 		}
   1089 		LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
   1090 		in6m_sol = imm->i6mm_maddr;
   1091 
   1092 		bzero(&mltmask, sizeof(mltmask));
   1093 		mltmask.sin6_len = sizeof(struct sockaddr_in6);
   1094 		mltmask.sin6_family = AF_INET6;
   1095 		mltmask.sin6_addr = in6mask32;
   1096 
   1097 		/*
   1098 		 * join link-local all-nodes address
   1099 		 */
   1100 		bzero(&mltaddr, sizeof(mltaddr));
   1101 		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
   1102 		mltaddr.sin6_family = AF_INET6;
   1103 		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
   1104 		if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
   1105 		    0)
   1106 			goto cleanup; /* XXX: should not fail */
   1107 
   1108 		/*
   1109 		 * XXX: do we really need this automatic routes?
   1110 		 * We should probably reconsider this stuff.  Most applications
   1111 		 * actually do not need the routes, since they usually specify
   1112 		 * the outgoing interface.
   1113 		 */
   1114 		rt = rtalloc1((struct sockaddr *)&mltaddr, 0);
   1115 		if (rt) {
   1116 			if (memcmp(&mltaddr.sin6_addr,
   1117 			    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
   1118 			    MLTMASK_LEN)) {
   1119 				RTFREE(rt);
   1120 				rt = NULL;
   1121 			}
   1122 		}
   1123 		if (!rt) {
   1124 			struct rt_addrinfo info;
   1125 
   1126 			bzero(&info, sizeof(info));
   1127 			info.rti_info[RTAX_DST] = (struct sockaddr *)&mltaddr;
   1128 			info.rti_info[RTAX_GATEWAY] =
   1129 			    (struct sockaddr *)&ia->ia_addr;
   1130 			info.rti_info[RTAX_NETMASK] =
   1131 			    (struct sockaddr *)&mltmask;
   1132 			info.rti_info[RTAX_IFA] =
   1133 			    (struct sockaddr *)&ia->ia_addr;
   1134 			/* XXX: we need RTF_CLONING to fake nd6_rtrequest */
   1135 			info.rti_flags = RTF_UP | RTF_CLONING;
   1136 			error = rtrequest1(RTM_ADD, &info, NULL);
   1137 			if (error)
   1138 				goto cleanup;
   1139 		} else {
   1140 			RTFREE(rt);
   1141 		}
   1142 		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
   1143 		if (!imm) {
   1144 			nd6log((LOG_WARNING,
   1145 			    "in6_update_ifa: addmulti failed for "
   1146 			    "%s on %s (errno=%d)\n",
   1147 			    ip6_sprintf(&mltaddr.sin6_addr),
   1148 			    if_name(ifp), error));
   1149 			goto cleanup;
   1150 		}
   1151 		LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
   1152 
   1153 		/*
   1154 		 * join node information group address
   1155 		 */
   1156 		dad_delay = 0;
   1157 		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
   1158 			/*
   1159 			 * The spec doesn't say anything about delay for this
   1160 			 * group, but the same logic should apply.
   1161 			 */
   1162 			dad_delay = arc4random() %
   1163 			    (MAX_RTR_SOLICITATION_DELAY * hz);
   1164 		}
   1165 		if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr) == 0) {
   1166 			imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
   1167 			    dad_delay); /* XXX jinmei */
   1168 			if (!imm) {
   1169 				nd6log((LOG_WARNING, "in6_update_ifa: "
   1170 				    "addmulti failed for %s on %s (errno=%d)\n",
   1171 				    ip6_sprintf(&mltaddr.sin6_addr),
   1172 				    if_name(ifp), error));
   1173 				/* XXX not very fatal, go on... */
   1174 			} else {
   1175 				LIST_INSERT_HEAD(&ia->ia6_memberships,
   1176 				    imm, i6mm_chain);
   1177 			}
   1178 		}
   1179 
   1180 
   1181 		/*
   1182 		 * join interface-local all-nodes address.
   1183 		 * (ff01::1%ifN, and ff01::%ifN/32)
   1184 		 */
   1185 		mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
   1186 		if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
   1187 		    != 0)
   1188 			goto cleanup; /* XXX: should not fail */
   1189 
   1190 		/* XXX: again, do we really need the route? */
   1191 		rt = rtalloc1((struct sockaddr *)&mltaddr, 0);
   1192 		if (rt) {
   1193 			/* 32bit came from "mltmask" */
   1194 			if (memcmp(&mltaddr.sin6_addr,
   1195 			    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
   1196 			    32 / 8)) {
   1197 				RTFREE(rt);
   1198 				rt = NULL;
   1199 			}
   1200 		}
   1201 		if (!rt) {
   1202 			struct rt_addrinfo info;
   1203 
   1204 			bzero(&info, sizeof(info));
   1205 			info.rti_info[RTAX_DST] = (struct sockaddr *)&mltaddr;
   1206 			info.rti_info[RTAX_GATEWAY] =
   1207 			    (struct sockaddr *)&ia->ia_addr;
   1208 			info.rti_info[RTAX_NETMASK] =
   1209 			    (struct sockaddr *)&mltmask;
   1210 			info.rti_info[RTAX_IFA] =
   1211 			    (struct sockaddr *)&ia->ia_addr;
   1212 			info.rti_flags = RTF_UP | RTF_CLONING;
   1213 			error = rtrequest1(RTM_ADD, &info, NULL);
   1214 			if (error)
   1215 				goto cleanup;
   1216 #undef	MLTMASK_LEN
   1217 		} else {
   1218 			RTFREE(rt);
   1219 		}
   1220 		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
   1221 		if (!imm) {
   1222 			nd6log((LOG_WARNING, "in6_update_ifa: "
   1223 			    "addmulti failed for %s on %s (errno=%d)\n",
   1224 			    ip6_sprintf(&mltaddr.sin6_addr),
   1225 			    if_name(ifp), error));
   1226 			goto cleanup;
   1227 		} else {
   1228 			LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
   1229 		}
   1230 	}
   1231 
   1232 	/*
   1233 	 * Perform DAD, if needed.
   1234 	 * XXX It may be of use, if we can administratively
   1235 	 * disable DAD.
   1236 	 */
   1237 	if (hostIsNew && in6if_do_dad(ifp) &&
   1238 	    ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
   1239 	    (ia->ia6_flags & IN6_IFF_TENTATIVE))
   1240 	{
   1241 		int mindelay, maxdelay;
   1242 
   1243 		dad_delay = 0;
   1244 		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
   1245 			/*
   1246 			 * We need to impose a delay before sending an NS
   1247 			 * for DAD.  Check if we also needed a delay for the
   1248 			 * corresponding MLD message.  If we did, the delay
   1249 			 * should be larger than the MLD delay (this could be
   1250 			 * relaxed a bit, but this simple logic is at least
   1251 			 * safe).
   1252 			 */
   1253 			mindelay = 0;
   1254 			if (in6m_sol != NULL &&
   1255 			    in6m_sol->in6m_state == MLD_REPORTPENDING) {
   1256 				mindelay = in6m_sol->in6m_timer;
   1257 			}
   1258 			maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
   1259 			if (maxdelay - mindelay == 0)
   1260 				dad_delay = 0;
   1261 			else {
   1262 				dad_delay =
   1263 				    (arc4random() % (maxdelay - mindelay)) +
   1264 				    mindelay;
   1265 			}
   1266 		}
   1267 		nd6_dad_start((struct ifaddr *)ia, dad_delay);
   1268 	}
   1269 
   1270 	return (error);
   1271 
   1272   unlink:
   1273 	/*
   1274 	 * XXX: if a change of an existing address failed, keep the entry
   1275 	 * anyway.
   1276 	 */
   1277 	if (hostIsNew)
   1278 		in6_unlink_ifa(ia, ifp);
   1279 	return (error);
   1280 
   1281   cleanup:
   1282 	in6_purgeaddr(&ia->ia_ifa);
   1283 	return error;
   1284 }
   1285 
   1286 void
   1287 in6_purgeaddr(ifa)
   1288 	struct ifaddr *ifa;
   1289 {
   1290 	struct ifnet *ifp = ifa->ifa_ifp;
   1291 	struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
   1292 	struct in6_multi_mship *imm;
   1293 
   1294 	/* stop DAD processing */
   1295 	nd6_dad_stop(ifa);
   1296 
   1297 	/*
   1298 	 * delete route to the destination of the address being purged.
   1299 	 * The interface must be p2p or loopback in this case.
   1300 	 */
   1301 	if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
   1302 		int e;
   1303 
   1304 		if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
   1305 		    != 0) {
   1306 			log(LOG_ERR, "in6_purgeaddr: failed to remove "
   1307 			    "a route to the p2p destination: %s on %s, "
   1308 			    "errno=%d\n",
   1309 			    ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
   1310 			    e);
   1311 			/* proceed anyway... */
   1312 		} else
   1313 			ia->ia_flags &= ~IFA_ROUTE;
   1314 	}
   1315 
   1316 	/* Remove ownaddr's loopback rtentry, if it exists. */
   1317 	in6_ifremloop(&(ia->ia_ifa));
   1318 
   1319 	/*
   1320 	 * leave from multicast groups we have joined for the interface
   1321 	 */
   1322 	while ((imm = ia->ia6_memberships.lh_first) != NULL) {
   1323 		LIST_REMOVE(imm, i6mm_chain);
   1324 		in6_leavegroup(imm);
   1325 	}
   1326 
   1327 	in6_unlink_ifa(ia, ifp);
   1328 }
   1329 
   1330 static void
   1331 in6_unlink_ifa(ia, ifp)
   1332 	struct in6_ifaddr *ia;
   1333 	struct ifnet *ifp;
   1334 {
   1335 	struct in6_ifaddr *oia;
   1336 	int	s = splnet();
   1337 
   1338 	TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
   1339 	/* release a refcnt for the link from if_addrlist */
   1340 	IFAFREE(&ia->ia_ifa);
   1341 
   1342 	oia = ia;
   1343 	if (oia == (ia = in6_ifaddr))
   1344 		in6_ifaddr = ia->ia_next;
   1345 	else {
   1346 		while (ia->ia_next && (ia->ia_next != oia))
   1347 			ia = ia->ia_next;
   1348 		if (ia->ia_next)
   1349 			ia->ia_next = oia->ia_next;
   1350 		else {
   1351 			/* search failed */
   1352 			printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
   1353 		}
   1354 	}
   1355 
   1356 	if (oia->ia6_multiaddrs.lh_first != NULL) {
   1357 		/*
   1358 		 * XXX thorpej (at) NetBSD.org -- if the interface is going
   1359 		 * XXX away, don't save the multicast entries, delete them!
   1360 		 */
   1361 		if (oia->ia_ifa.ifa_ifp->if_output == if_nulloutput) {
   1362 			struct in6_multi *in6m;
   1363 
   1364 			while ((in6m =
   1365 			    LIST_FIRST(&oia->ia6_multiaddrs)) != NULL)
   1366 				in6_delmulti(in6m);
   1367 		} else
   1368 			in6_savemkludge(oia);
   1369 	}
   1370 
   1371 	/*
   1372 	 * Release the reference to the base prefix.  There should be a
   1373 	 * positive reference.
   1374 	 */
   1375 	if (oia->ia6_ndpr == NULL) {
   1376 		nd6log((LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
   1377 		    "%p has no prefix\n", oia));
   1378 	} else {
   1379 		oia->ia6_ndpr->ndpr_refcnt--;
   1380 		oia->ia6_ndpr = NULL;
   1381 	}
   1382 
   1383 	/*
   1384 	 * Also, if the address being removed is autoconf'ed, call
   1385 	 * pfxlist_onlink_check() since the release might affect the status of
   1386 	 * other (detached) addresses.
   1387 	 */
   1388 	if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0)
   1389 		pfxlist_onlink_check();
   1390 
   1391 	/*
   1392 	 * release another refcnt for the link from in6_ifaddr.
   1393 	 * Note that we should decrement the refcnt at least once for all *BSD.
   1394 	 */
   1395 	IFAFREE(&oia->ia_ifa);
   1396 
   1397 	splx(s);
   1398 }
   1399 
   1400 void
   1401 in6_purgeif(ifp)
   1402 	struct ifnet *ifp;
   1403 {
   1404 	struct ifaddr *ifa, *nifa;
   1405 
   1406 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
   1407 	{
   1408 		nifa = TAILQ_NEXT(ifa, ifa_list);
   1409 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1410 			continue;
   1411 		in6_purgeaddr(ifa);
   1412 	}
   1413 
   1414 	in6_ifdetach(ifp);
   1415 }
   1416 
   1417 /*
   1418  * SIOC[GAD]LIFADDR.
   1419  *	SIOCGLIFADDR: get first address. (?)
   1420  *	SIOCGLIFADDR with IFLR_PREFIX:
   1421  *		get first address that matches the specified prefix.
   1422  *	SIOCALIFADDR: add the specified address.
   1423  *	SIOCALIFADDR with IFLR_PREFIX:
   1424  *		add the specified prefix, filling hostid part from
   1425  *		the first link-local address.  prefixlen must be <= 64.
   1426  *	SIOCDLIFADDR: delete the specified address.
   1427  *	SIOCDLIFADDR with IFLR_PREFIX:
   1428  *		delete the first address that matches the specified prefix.
   1429  * return values:
   1430  *	EINVAL on invalid parameters
   1431  *	EADDRNOTAVAIL on prefix match failed/specified address not found
   1432  *	other values may be returned from in6_ioctl()
   1433  *
   1434  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
   1435  * this is to accomodate address naming scheme other than RFC2374,
   1436  * in the future.
   1437  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
   1438  * address encoding scheme. (see figure on page 8)
   1439  */
   1440 static int
   1441 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
   1442 	struct socket *so;
   1443 	u_long cmd;
   1444 	caddr_t	data;
   1445 	struct ifnet *ifp;
   1446 	struct proc *p;
   1447 {
   1448 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
   1449 	struct ifaddr *ifa;
   1450 	struct sockaddr *sa;
   1451 
   1452 	/* sanity checks */
   1453 	if (!data || !ifp) {
   1454 		panic("invalid argument to in6_lifaddr_ioctl");
   1455 		/* NOTREACHED */
   1456 	}
   1457 
   1458 	switch (cmd) {
   1459 	case SIOCGLIFADDR:
   1460 		/* address must be specified on GET with IFLR_PREFIX */
   1461 		if ((iflr->flags & IFLR_PREFIX) == 0)
   1462 			break;
   1463 		/* FALLTHROUGH */
   1464 	case SIOCALIFADDR:
   1465 	case SIOCDLIFADDR:
   1466 		/* address must be specified on ADD and DELETE */
   1467 		sa = (struct sockaddr *)&iflr->addr;
   1468 		if (sa->sa_family != AF_INET6)
   1469 			return EINVAL;
   1470 		if (sa->sa_len != sizeof(struct sockaddr_in6))
   1471 			return EINVAL;
   1472 		/* XXX need improvement */
   1473 		sa = (struct sockaddr *)&iflr->dstaddr;
   1474 		if (sa->sa_family && sa->sa_family != AF_INET6)
   1475 			return EINVAL;
   1476 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
   1477 			return EINVAL;
   1478 		break;
   1479 	default: /* shouldn't happen */
   1480 #if 0
   1481 		panic("invalid cmd to in6_lifaddr_ioctl");
   1482 		/* NOTREACHED */
   1483 #else
   1484 		return EOPNOTSUPP;
   1485 #endif
   1486 	}
   1487 	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
   1488 		return EINVAL;
   1489 
   1490 	switch (cmd) {
   1491 	case SIOCALIFADDR:
   1492 	    {
   1493 		struct in6_aliasreq ifra;
   1494 		struct in6_addr *xhostid = NULL;
   1495 		int prefixlen;
   1496 
   1497 		if ((iflr->flags & IFLR_PREFIX) != 0) {
   1498 			struct sockaddr_in6 *sin6;
   1499 
   1500 			/*
   1501 			 * xhostid is to fill in the hostid part of the
   1502 			 * address.  xhostid points to the first link-local
   1503 			 * address attached to the interface.
   1504 			 */
   1505 			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
   1506 			if (!ifa)
   1507 				return EADDRNOTAVAIL;
   1508 			xhostid = IFA_IN6(ifa);
   1509 
   1510 		 	/* prefixlen must be <= 64. */
   1511 			if (64 < iflr->prefixlen)
   1512 				return EINVAL;
   1513 			prefixlen = iflr->prefixlen;
   1514 
   1515 			/* hostid part must be zero. */
   1516 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1517 			if (sin6->sin6_addr.s6_addr32[2] != 0
   1518 			 || sin6->sin6_addr.s6_addr32[3] != 0) {
   1519 				return EINVAL;
   1520 			}
   1521 		} else
   1522 			prefixlen = iflr->prefixlen;
   1523 
   1524 		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
   1525 		bzero(&ifra, sizeof(ifra));
   1526 		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
   1527 
   1528 		bcopy(&iflr->addr, &ifra.ifra_addr,
   1529 		    ((struct sockaddr *)&iflr->addr)->sa_len);
   1530 		if (xhostid) {
   1531 			/* fill in hostid part */
   1532 			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
   1533 			    xhostid->s6_addr32[2];
   1534 			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
   1535 			    xhostid->s6_addr32[3];
   1536 		}
   1537 
   1538 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
   1539 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
   1540 			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
   1541 			if (xhostid) {
   1542 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
   1543 				    xhostid->s6_addr32[2];
   1544 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
   1545 				    xhostid->s6_addr32[3];
   1546 			}
   1547 		}
   1548 
   1549 		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
   1550 		in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
   1551 
   1552 		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
   1553 		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
   1554 	    }
   1555 	case SIOCGLIFADDR:
   1556 	case SIOCDLIFADDR:
   1557 	    {
   1558 		struct in6_ifaddr *ia;
   1559 		struct in6_addr mask, candidate, match;
   1560 		struct sockaddr_in6 *sin6;
   1561 		int cmp;
   1562 
   1563 		bzero(&mask, sizeof(mask));
   1564 		if (iflr->flags & IFLR_PREFIX) {
   1565 			/* lookup a prefix rather than address. */
   1566 			in6_prefixlen2mask(&mask, iflr->prefixlen);
   1567 
   1568 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1569 			bcopy(&sin6->sin6_addr, &match, sizeof(match));
   1570 			match.s6_addr32[0] &= mask.s6_addr32[0];
   1571 			match.s6_addr32[1] &= mask.s6_addr32[1];
   1572 			match.s6_addr32[2] &= mask.s6_addr32[2];
   1573 			match.s6_addr32[3] &= mask.s6_addr32[3];
   1574 
   1575 			/* if you set extra bits, that's wrong */
   1576 			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
   1577 				return EINVAL;
   1578 
   1579 			cmp = 1;
   1580 		} else {
   1581 			if (cmd == SIOCGLIFADDR) {
   1582 				/* on getting an address, take the 1st match */
   1583 				cmp = 0;	/* XXX */
   1584 			} else {
   1585 				/* on deleting an address, do exact match */
   1586 				in6_prefixlen2mask(&mask, 128);
   1587 				sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1588 				bcopy(&sin6->sin6_addr, &match, sizeof(match));
   1589 
   1590 				cmp = 1;
   1591 			}
   1592 		}
   1593 
   1594 		for (ifa = ifp->if_addrlist.tqh_first;
   1595 		     ifa;
   1596 		     ifa = ifa->ifa_list.tqe_next)
   1597 		{
   1598 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1599 				continue;
   1600 			if (!cmp)
   1601 				break;
   1602 
   1603 			/*
   1604 			 * XXX: this is adhoc, but is necessary to allow
   1605 			 * a user to specify fe80::/64 (not /10) for a
   1606 			 * link-local address.
   1607 			 */
   1608 			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
   1609 			in6_clearscope(&candidate);
   1610 			candidate.s6_addr32[0] &= mask.s6_addr32[0];
   1611 			candidate.s6_addr32[1] &= mask.s6_addr32[1];
   1612 			candidate.s6_addr32[2] &= mask.s6_addr32[2];
   1613 			candidate.s6_addr32[3] &= mask.s6_addr32[3];
   1614 			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
   1615 				break;
   1616 		}
   1617 		if (!ifa)
   1618 			return EADDRNOTAVAIL;
   1619 		ia = ifa2ia6(ifa);
   1620 
   1621 		if (cmd == SIOCGLIFADDR) {
   1622 			int error;
   1623 
   1624 			/* fill in the if_laddrreq structure */
   1625 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
   1626 			error = sa6_recoverscope(
   1627 			    (struct sockaddr_in6 *)&iflr->addr);
   1628 			if (error != 0)
   1629 				return (error);
   1630 
   1631 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1632 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
   1633 				    ia->ia_dstaddr.sin6_len);
   1634 				error = sa6_recoverscope(
   1635 				    (struct sockaddr_in6 *)&iflr->dstaddr);
   1636 				if (error != 0)
   1637 					return (error);
   1638 			} else
   1639 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
   1640 
   1641 			iflr->prefixlen =
   1642 			    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
   1643 
   1644 			iflr->flags = ia->ia6_flags;	/* XXX */
   1645 
   1646 			return 0;
   1647 		} else {
   1648 			struct in6_aliasreq ifra;
   1649 
   1650 			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
   1651 			bzero(&ifra, sizeof(ifra));
   1652 			bcopy(iflr->iflr_name, ifra.ifra_name,
   1653 			    sizeof(ifra.ifra_name));
   1654 
   1655 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
   1656 			    ia->ia_addr.sin6_len);
   1657 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1658 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
   1659 				    ia->ia_dstaddr.sin6_len);
   1660 			} else {
   1661 				bzero(&ifra.ifra_dstaddr,
   1662 				    sizeof(ifra.ifra_dstaddr));
   1663 			}
   1664 			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
   1665 			    ia->ia_prefixmask.sin6_len);
   1666 
   1667 			ifra.ifra_flags = ia->ia6_flags;
   1668 			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
   1669 			    ifp, p);
   1670 		}
   1671 	    }
   1672 	}
   1673 
   1674 	return EOPNOTSUPP;	/* just for safety */
   1675 }
   1676 
   1677 /*
   1678  * Initialize an interface's intetnet6 address
   1679  * and routing table entry.
   1680  */
   1681 static int
   1682 in6_ifinit(ifp, ia, sin6, newhost)
   1683 	struct ifnet *ifp;
   1684 	struct in6_ifaddr *ia;
   1685 	struct sockaddr_in6 *sin6;
   1686 	int newhost;
   1687 {
   1688 	int	error = 0, plen, ifacount = 0;
   1689 	int	s = splnet();
   1690 	struct ifaddr *ifa;
   1691 
   1692 	/*
   1693 	 * Give the interface a chance to initialize
   1694 	 * if this is its first address,
   1695 	 * and to validate the address if necessary.
   1696 	 */
   1697 	for (ifa = ifp->if_addrlist.tqh_first; ifa;
   1698 	     ifa = ifa->ifa_list.tqe_next)
   1699 	{
   1700 		if (ifa->ifa_addr == NULL)
   1701 			continue;	/* just for safety */
   1702 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1703 			continue;
   1704 		ifacount++;
   1705 	}
   1706 
   1707 	ia->ia_addr = *sin6;
   1708 
   1709 	if (ifacount <= 1 && ifp->if_ioctl &&
   1710 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
   1711 		splx(s);
   1712 		return (error);
   1713 	}
   1714 	splx(s);
   1715 
   1716 	ia->ia_ifa.ifa_metric = ifp->if_metric;
   1717 
   1718 	/* we could do in(6)_socktrim here, but just omit it at this moment. */
   1719 
   1720 	/*
   1721 	 * Special case:
   1722 	 * If the destination address is specified for a point-to-point
   1723 	 * interface, install a route to the destination as an interface
   1724 	 * direct route.
   1725 	 */
   1726 	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
   1727 	if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
   1728 		if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
   1729 				    RTF_UP | RTF_HOST)) != 0)
   1730 			return (error);
   1731 		ia->ia_flags |= IFA_ROUTE;
   1732 	}
   1733 
   1734 	/* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
   1735 	if (newhost) {
   1736 		/* set the rtrequest function to create llinfo */
   1737 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
   1738 		in6_ifaddloop(&(ia->ia_ifa));
   1739 	}
   1740 
   1741 	if (ifp->if_flags & IFF_MULTICAST)
   1742 		in6_restoremkludge(ia, ifp);
   1743 
   1744 	return (error);
   1745 }
   1746 
   1747 /*
   1748  * Find an IPv6 interface link-local address specific to an interface.
   1749  */
   1750 struct in6_ifaddr *
   1751 in6ifa_ifpforlinklocal(ifp, ignoreflags)
   1752 	struct ifnet *ifp;
   1753 	int ignoreflags;
   1754 {
   1755 	struct ifaddr *ifa;
   1756 
   1757 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1758 	{
   1759 		if (ifa->ifa_addr == NULL)
   1760 			continue;	/* just for safety */
   1761 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1762 			continue;
   1763 		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
   1764 			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
   1765 			     ignoreflags) != 0)
   1766 				continue;
   1767 			break;
   1768 		}
   1769 	}
   1770 
   1771 	return ((struct in6_ifaddr *)ifa);
   1772 }
   1773 
   1774 
   1775 /*
   1776  * find the internet address corresponding to a given interface and address.
   1777  */
   1778 struct in6_ifaddr *
   1779 in6ifa_ifpwithaddr(ifp, addr)
   1780 	struct ifnet *ifp;
   1781 	struct in6_addr *addr;
   1782 {
   1783 	struct ifaddr *ifa;
   1784 
   1785 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1786 	{
   1787 		if (ifa->ifa_addr == NULL)
   1788 			continue;	/* just for safety */
   1789 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1790 			continue;
   1791 		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
   1792 			break;
   1793 	}
   1794 
   1795 	return ((struct in6_ifaddr *)ifa);
   1796 }
   1797 
   1798 /*
   1799  * Convert IP6 address to printable (loggable) representation.
   1800  */
   1801 static int ip6round = 0;
   1802 char *
   1803 ip6_sprintf(addr)
   1804 	const struct in6_addr *addr;
   1805 {
   1806 	static char ip6buf[8][48];
   1807 	int i;
   1808 	char *cp;
   1809 	const u_int16_t *a = (const u_int16_t *)addr;
   1810 	const u_int8_t *d;
   1811 	int dcolon = 0;
   1812 
   1813 	ip6round = (ip6round + 1) & 7;
   1814 	cp = ip6buf[ip6round];
   1815 
   1816 	for (i = 0; i < 8; i++) {
   1817 		if (dcolon == 1) {
   1818 			if (*a == 0) {
   1819 				if (i == 7)
   1820 					*cp++ = ':';
   1821 				a++;
   1822 				continue;
   1823 			} else
   1824 				dcolon = 2;
   1825 		}
   1826 		if (*a == 0) {
   1827 			if (dcolon == 0 && *(a + 1) == 0) {
   1828 				if (i == 0)
   1829 					*cp++ = ':';
   1830 				*cp++ = ':';
   1831 				dcolon = 1;
   1832 			} else {
   1833 				*cp++ = '0';
   1834 				*cp++ = ':';
   1835 			}
   1836 			a++;
   1837 			continue;
   1838 		}
   1839 		d = (const u_char *)a;
   1840 		*cp++ = hexdigits[*d >> 4];
   1841 		*cp++ = hexdigits[*d++ & 0xf];
   1842 		*cp++ = hexdigits[*d >> 4];
   1843 		*cp++ = hexdigits[*d & 0xf];
   1844 		*cp++ = ':';
   1845 		a++;
   1846 	}
   1847 	*--cp = 0;
   1848 	return (ip6buf[ip6round]);
   1849 }
   1850 
   1851 /*
   1852  * Determine if an address is on a local network.
   1853  */
   1854 int
   1855 in6_localaddr(in6)
   1856 	struct in6_addr *in6;
   1857 {
   1858 	struct in6_ifaddr *ia;
   1859 
   1860 	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
   1861 		return (1);
   1862 
   1863 	for (ia = in6_ifaddr; ia; ia = ia->ia_next)
   1864 		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
   1865 					      &ia->ia_prefixmask.sin6_addr))
   1866 			return (1);
   1867 
   1868 	return (0);
   1869 }
   1870 
   1871 int
   1872 in6_is_addr_deprecated(sa6)
   1873 	struct sockaddr_in6 *sa6;
   1874 {
   1875 	struct in6_ifaddr *ia;
   1876 
   1877 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
   1878 		if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
   1879 		    &sa6->sin6_addr) &&
   1880 #ifdef SCOPEDROUTING
   1881 		    ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
   1882 #endif
   1883 		    (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
   1884 			return (1); /* true */
   1885 
   1886 		/* XXX: do we still have to go thru the rest of the list? */
   1887 	}
   1888 
   1889 	return (0);		/* false */
   1890 }
   1891 
   1892 /*
   1893  * return length of part which dst and src are equal
   1894  * hard coding...
   1895  */
   1896 int
   1897 in6_matchlen(src, dst)
   1898 struct in6_addr *src, *dst;
   1899 {
   1900 	int match = 0;
   1901 	u_char *s = (u_char *)src, *d = (u_char *)dst;
   1902 	u_char *lim = s + 16, r;
   1903 
   1904 	while (s < lim)
   1905 		if ((r = (*d++ ^ *s++)) != 0) {
   1906 			while (r < 128) {
   1907 				match++;
   1908 				r <<= 1;
   1909 			}
   1910 			break;
   1911 		} else
   1912 			match += 8;
   1913 	return match;
   1914 }
   1915 
   1916 /* XXX: to be scope conscious */
   1917 int
   1918 in6_are_prefix_equal(p1, p2, len)
   1919 	struct in6_addr *p1, *p2;
   1920 	int len;
   1921 {
   1922 	int bytelen, bitlen;
   1923 
   1924 	/* sanity check */
   1925 	if (len < 0 || len > 128) {
   1926 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
   1927 		    len);
   1928 		return (0);
   1929 	}
   1930 
   1931 	bytelen = len / 8;
   1932 	bitlen = len % 8;
   1933 
   1934 	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
   1935 		return (0);
   1936 	if (bitlen != 0 &&
   1937 	    p1->s6_addr[bytelen] >> (8 - bitlen) !=
   1938 	    p2->s6_addr[bytelen] >> (8 - bitlen))
   1939 		return (0);
   1940 
   1941 	return (1);
   1942 }
   1943 
   1944 void
   1945 in6_prefixlen2mask(maskp, len)
   1946 	struct in6_addr *maskp;
   1947 	int len;
   1948 {
   1949 	static const u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
   1950 	int bytelen, bitlen, i;
   1951 
   1952 	/* sanity check */
   1953 	if (len < 0 || len > 128) {
   1954 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
   1955 		    len);
   1956 		return;
   1957 	}
   1958 
   1959 	bzero(maskp, sizeof(*maskp));
   1960 	bytelen = len / 8;
   1961 	bitlen = len % 8;
   1962 	for (i = 0; i < bytelen; i++)
   1963 		maskp->s6_addr[i] = 0xff;
   1964 	if (bitlen)
   1965 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
   1966 }
   1967 
   1968 /*
   1969  * return the best address out of the same scope. if no address was
   1970  * found, return the first valid address from designated IF.
   1971  */
   1972 struct in6_ifaddr *
   1973 in6_ifawithifp(ifp, dst)
   1974 	struct ifnet *ifp;
   1975 	struct in6_addr *dst;
   1976 {
   1977 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
   1978 	struct ifaddr *ifa;
   1979 	struct in6_ifaddr *besta = 0;
   1980 	struct in6_ifaddr *dep[2];	/* last-resort: deprecated */
   1981 
   1982 	dep[0] = dep[1] = NULL;
   1983 
   1984 	/*
   1985 	 * We first look for addresses in the same scope.
   1986 	 * If there is one, return it.
   1987 	 * If two or more, return one which matches the dst longest.
   1988 	 * If none, return one of global addresses assigned other ifs.
   1989 	 */
   1990 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1991 	{
   1992 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1993 			continue;
   1994 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
   1995 			continue; /* XXX: is there any case to allow anycast? */
   1996 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
   1997 			continue; /* don't use this interface */
   1998 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
   1999 			continue;
   2000 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
   2001 			if (ip6_use_deprecated)
   2002 				dep[0] = (struct in6_ifaddr *)ifa;
   2003 			continue;
   2004 		}
   2005 
   2006 		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
   2007 			/*
   2008 			 * call in6_matchlen() as few as possible
   2009 			 */
   2010 			if (besta) {
   2011 				if (blen == -1)
   2012 					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
   2013 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
   2014 				if (tlen > blen) {
   2015 					blen = tlen;
   2016 					besta = (struct in6_ifaddr *)ifa;
   2017 				}
   2018 			} else
   2019 				besta = (struct in6_ifaddr *)ifa;
   2020 		}
   2021 	}
   2022 	if (besta)
   2023 		return (besta);
   2024 
   2025 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   2026 	{
   2027 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2028 			continue;
   2029 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
   2030 			continue; /* XXX: is there any case to allow anycast? */
   2031 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
   2032 			continue; /* don't use this interface */
   2033 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
   2034 			continue;
   2035 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
   2036 			if (ip6_use_deprecated)
   2037 				dep[1] = (struct in6_ifaddr *)ifa;
   2038 			continue;
   2039 		}
   2040 
   2041 		return (struct in6_ifaddr *)ifa;
   2042 	}
   2043 
   2044 	/* use the last-resort values, that are, deprecated addresses */
   2045 	if (dep[0])
   2046 		return dep[0];
   2047 	if (dep[1])
   2048 		return dep[1];
   2049 
   2050 	return NULL;
   2051 }
   2052 
   2053 /*
   2054  * perform DAD when interface becomes IFF_UP.
   2055  */
   2056 void
   2057 in6_if_up(ifp)
   2058 	struct ifnet *ifp;
   2059 {
   2060 	struct ifaddr *ifa;
   2061 	struct in6_ifaddr *ia;
   2062 
   2063 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   2064 	{
   2065 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2066 			continue;
   2067 		ia = (struct in6_ifaddr *)ifa;
   2068 		if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
   2069 			/*
   2070 			 * The TENTATIVE flag was likely set by hand
   2071 			 * beforehand, implicitly indicating the need for DAD.
   2072 			 * We may be able to skip the random delay in this
   2073 			 * case, but we impose delays just in case.
   2074 			 */
   2075 			nd6_dad_start(ifa,
   2076 			    arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
   2077 		}
   2078 	}
   2079 
   2080 	/*
   2081 	 * special cases, like 6to4, are handled in in6_ifattach
   2082 	 */
   2083 	in6_ifattach(ifp, NULL);
   2084 }
   2085 
   2086 int
   2087 in6if_do_dad(ifp)
   2088 	struct ifnet *ifp;
   2089 {
   2090 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
   2091 		return (0);
   2092 
   2093 	switch (ifp->if_type) {
   2094 	case IFT_FAITH:
   2095 		/*
   2096 		 * These interfaces do not have the IFF_LOOPBACK flag,
   2097 		 * but loop packets back.  We do not have to do DAD on such
   2098 		 * interfaces.  We should even omit it, because loop-backed
   2099 		 * NS would confuse the DAD procedure.
   2100 		 */
   2101 		return (0);
   2102 	default:
   2103 		/*
   2104 		 * Our DAD routine requires the interface up and running.
   2105 		 * However, some interfaces can be up before the RUNNING
   2106 		 * status.  Additionaly, users may try to assign addresses
   2107 		 * before the interface becomes up (or running).
   2108 		 * We simply skip DAD in such a case as a work around.
   2109 		 * XXX: we should rather mark "tentative" on such addresses,
   2110 		 * and do DAD after the interface becomes ready.
   2111 		 */
   2112 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
   2113 		    (IFF_UP|IFF_RUNNING))
   2114 			return (0);
   2115 
   2116 		return (1);
   2117 	}
   2118 }
   2119 
   2120 /*
   2121  * Calculate max IPv6 MTU through all the interfaces and store it
   2122  * to in6_maxmtu.
   2123  */
   2124 void
   2125 in6_setmaxmtu()
   2126 {
   2127 	unsigned long maxmtu = 0;
   2128 	struct ifnet *ifp;
   2129 
   2130 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
   2131 	{
   2132 		/* this function can be called during ifnet initialization */
   2133 		if (!ifp->if_afdata[AF_INET6])
   2134 			continue;
   2135 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
   2136 		    IN6_LINKMTU(ifp) > maxmtu)
   2137 			maxmtu = IN6_LINKMTU(ifp);
   2138 	}
   2139 	if (maxmtu)	     /* update only when maxmtu is positive */
   2140 		in6_maxmtu = maxmtu;
   2141 }
   2142 
   2143 /*
   2144  * Provide the length of interface identifiers to be used for the link attached
   2145  * to the given interface.  The length should be defined in "IPv6 over
   2146  * xxx-link" document.  Note that address architecture might also define
   2147  * the length for a particular set of address prefixes, regardless of the
   2148  * link type.  As clarified in rfc2462bis, those two definitions should be
   2149  * consistent, and those really are as of August 2004.
   2150  */
   2151 int
   2152 in6_if2idlen(ifp)
   2153 	struct ifnet *ifp;
   2154 {
   2155 	switch (ifp->if_type) {
   2156 	case IFT_ETHER:		/* RFC2464 */
   2157 	case IFT_PROPVIRTUAL:	/* XXX: no RFC. treat it as ether */
   2158 	case IFT_L2VLAN:	/* ditto */
   2159 	case IFT_IEEE80211:	/* ditto */
   2160 	case IFT_FDDI:		/* RFC2467 */
   2161 	case IFT_ISO88025:	/* RFC2470 (IPv6 over Token Ring) */
   2162 	case IFT_PPP:		/* RFC2472 */
   2163 	case IFT_ARCNET:	/* RFC2497 */
   2164 	case IFT_FRELAY:	/* RFC2590 */
   2165 	case IFT_IEEE1394:	/* RFC3146 */
   2166 	case IFT_GIF:		/* draft-ietf-v6ops-mech-v2-07 */
   2167 	case IFT_LOOP:		/* XXX: is this really correct? */
   2168 		return 64;
   2169 	default:
   2170 		/*
   2171 		 * Unknown link type:
   2172 		 * It might be controversial to use the today's common constant
   2173 		 * of 64 for these cases unconditionally.  For full compliance,
   2174 		 * we should return an error in this case.  On the other hand,
   2175 		 * if we simply miss the standard for the link type or a new
   2176 		 * standard is defined for a new link type, the IFID length
   2177 		 * is very likely to be the common constant.  As a compromise,
   2178 		 * we always use the constant, but make an explicit notice
   2179 		 * indicating the "unknown" case.
   2180 		 */
   2181 		printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
   2182 		return 64;
   2183 	}
   2184 }
   2185 
   2186 void *
   2187 in6_domifattach(ifp)
   2188 	struct ifnet *ifp;
   2189 {
   2190 	struct in6_ifextra *ext;
   2191 
   2192 	ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
   2193 	bzero(ext, sizeof(*ext));
   2194 
   2195 	ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
   2196 	    M_IFADDR, M_WAITOK);
   2197 	bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
   2198 
   2199 	ext->icmp6_ifstat =
   2200 	    (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
   2201 	    M_IFADDR, M_WAITOK);
   2202 	bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
   2203 
   2204 	ext->nd_ifinfo = nd6_ifattach(ifp);
   2205 	ext->scope6_id = scope6_ifattach(ifp);
   2206 	return ext;
   2207 }
   2208 
   2209 void
   2210 in6_domifdetach(ifp, aux)
   2211 	struct ifnet *ifp;
   2212 	void *aux;
   2213 {
   2214 	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
   2215 
   2216 	nd6_ifdetach(ext->nd_ifinfo);
   2217 	free(ext->in6_ifstat, M_IFADDR);
   2218 	free(ext->icmp6_ifstat, M_IFADDR);
   2219 	scope6_ifdetach(ext->scope6_id);
   2220 	free(ext, M_IFADDR);
   2221 }
   2222