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