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