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