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