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