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