Home | History | Annotate | Line # | Download | only in netinet6
in6.c revision 1.239
      1 /*	$NetBSD: in6.c,v 1.239 2017/02/28 02:56:49 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.239 2017/02/28 02:56:49 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 = splsoftnet();
    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 = splsoftnet();
    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 splsoftnet()?
    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 	char ip6buf[INET6_ADDRSTRLEN];
    800 
    801 	KASSERT((iap == NULL && psref == NULL) ||
    802 	    (iap != NULL && psref != NULL));
    803 
    804 	in6m_sol = NULL;
    805 
    806 	/* Validate parameters */
    807 	if (ifp == NULL || ifra == NULL) /* this maybe redundant */
    808 		return EINVAL;
    809 
    810 	/*
    811 	 * The destination address for a p2p link must have a family
    812 	 * of AF_UNSPEC or AF_INET6.
    813 	 */
    814 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
    815 	    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
    816 	    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
    817 		return EAFNOSUPPORT;
    818 	/*
    819 	 * validate ifra_prefixmask.  don't check sin6_family, netmask
    820 	 * does not carry fields other than sin6_len.
    821 	 */
    822 	if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
    823 		return EINVAL;
    824 	/*
    825 	 * Because the IPv6 address architecture is classless, we require
    826 	 * users to specify a (non 0) prefix length (mask) for a new address.
    827 	 * We also require the prefix (when specified) mask is valid, and thus
    828 	 * reject a non-consecutive mask.
    829 	 */
    830 	if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
    831 		return EINVAL;
    832 	if (ifra->ifra_prefixmask.sin6_len != 0) {
    833 		plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
    834 		    (u_char *)&ifra->ifra_prefixmask +
    835 		    ifra->ifra_prefixmask.sin6_len);
    836 		if (plen <= 0)
    837 			return EINVAL;
    838 	} else {
    839 		/*
    840 		 * In this case, ia must not be NULL.  We just use its prefix
    841 		 * length.
    842 		 */
    843 		plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
    844 	}
    845 	/*
    846 	 * If the destination address on a p2p interface is specified,
    847 	 * and the address is a scoped one, validate/set the scope
    848 	 * zone identifier.
    849 	 */
    850 	dst6 = ifra->ifra_dstaddr;
    851 	if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
    852 	    (dst6.sin6_family == AF_INET6)) {
    853 		struct in6_addr in6_tmp;
    854 		u_int32_t zoneid;
    855 
    856 		in6_tmp = dst6.sin6_addr;
    857 		if (in6_setscope(&in6_tmp, ifp, &zoneid))
    858 			return EINVAL; /* XXX: should be impossible */
    859 
    860 		if (dst6.sin6_scope_id != 0) {
    861 			if (dst6.sin6_scope_id != zoneid)
    862 				return EINVAL;
    863 		} else		/* user omit to specify the ID. */
    864 			dst6.sin6_scope_id = zoneid;
    865 
    866 		/* convert into the internal form */
    867 		if (sa6_embedscope(&dst6, 0))
    868 			return EINVAL; /* XXX: should be impossible */
    869 	}
    870 	/*
    871 	 * The destination address can be specified only for a p2p or a
    872 	 * loopback interface.  If specified, the corresponding prefix length
    873 	 * must be 128.
    874 	 */
    875 	if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
    876 #ifdef FORCE_P2PPLEN
    877 		int i;
    878 #endif
    879 
    880 		if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
    881 			/* XXX: noisy message */
    882 			nd6log(LOG_INFO, "a destination can "
    883 			    "be specified for a p2p or a loopback IF only\n");
    884 			return EINVAL;
    885 		}
    886 		if (plen != 128) {
    887 			nd6log(LOG_INFO, "prefixlen should "
    888 			    "be 128 when dstaddr is specified\n");
    889 #ifdef FORCE_P2PPLEN
    890 			/*
    891 			 * To be compatible with old configurations,
    892 			 * such as ifconfig gif0 inet6 2001::1 2001::2
    893 			 * prefixlen 126, we override the specified
    894 			 * prefixmask as if the prefix length was 128.
    895 			 */
    896 			ifra->ifra_prefixmask.sin6_len =
    897 			    sizeof(struct sockaddr_in6);
    898 			for (i = 0; i < 4; i++)
    899 				ifra->ifra_prefixmask.sin6_addr.s6_addr32[i] =
    900 				    0xffffffff;
    901 			plen = 128;
    902 #else
    903 			return EINVAL;
    904 #endif
    905 		}
    906 	}
    907 	/* lifetime consistency check */
    908 	lt = &ifra->ifra_lifetime;
    909 	if (lt->ia6t_pltime > lt->ia6t_vltime)
    910 		return EINVAL;
    911 	if (lt->ia6t_vltime == 0) {
    912 		/*
    913 		 * the following log might be noisy, but this is a typical
    914 		 * configuration mistake or a tool's bug.
    915 		 */
    916 		nd6log(LOG_INFO, "valid lifetime is 0 for %s\n",
    917 		    IN6_PRINT(ip6buf, &ifra->ifra_addr.sin6_addr));
    918 
    919 		if (ia == NULL)
    920 			return 0; /* there's nothing to do */
    921 	}
    922 
    923 	/*
    924 	 * If this is a new address, allocate a new ifaddr and link it
    925 	 * into chains.
    926 	 */
    927 	if (ia == NULL) {
    928 		hostIsNew = 1;
    929 		/*
    930 		 * When in6_update_ifa() is called in a process of a received
    931 		 * RA, it is called under an interrupt context.  So, we should
    932 		 * call malloc with M_NOWAIT.
    933 		 */
    934 		ia = malloc(sizeof(*ia), M_IFADDR, M_NOWAIT|M_ZERO);
    935 		if (ia == NULL)
    936 			return ENOBUFS;
    937 		LIST_INIT(&ia->ia6_memberships);
    938 		/* Initialize the address and masks, and put time stamp */
    939 		ia->ia_ifa.ifa_addr = sin6tosa(&ia->ia_addr);
    940 		ia->ia_addr.sin6_family = AF_INET6;
    941 		ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
    942 		ia->ia6_createtime = time_uptime;
    943 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
    944 			/*
    945 			 * XXX: some functions expect that ifa_dstaddr is not
    946 			 * NULL for p2p interfaces.
    947 			 */
    948 			ia->ia_ifa.ifa_dstaddr = sin6tosa(&ia->ia_dstaddr);
    949 		} else {
    950 			ia->ia_ifa.ifa_dstaddr = NULL;
    951 		}
    952 		ia->ia_ifa.ifa_netmask = sin6tosa(&ia->ia_prefixmask);
    953 
    954 		ia->ia_ifp = ifp;
    955 		IN6_ADDRLIST_ENTRY_INIT(ia);
    956 		ifa_psref_init(&ia->ia_ifa);
    957 	}
    958 
    959 	/* update timestamp */
    960 	ia->ia6_updatetime = time_uptime;
    961 
    962 	/* set prefix mask */
    963 	if (ifra->ifra_prefixmask.sin6_len) {
    964 		if (ia->ia_prefixmask.sin6_len) {
    965 			/*
    966 			 * We prohibit changing the prefix length of an
    967 			 * existing autoconf address, because the operation
    968 			 * would confuse prefix management.
    969 			 */
    970 			if (ia->ia6_ndpr != NULL &&
    971 			    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) !=
    972 			    plen)
    973 			{
    974 				nd6log(LOG_INFO, "the prefix length of an"
    975 				    " existing (%s) autoconf address should"
    976 				    " not be changed\n",
    977 				    IN6_PRINT(ip6buf,
    978 				    &ia->ia_addr.sin6_addr));
    979 				error = EINVAL;
    980 				if (hostIsNew)
    981 					free(ia, M_IFADDR);
    982 				goto exit;
    983 			}
    984 
    985 			if (!IN6_ARE_ADDR_EQUAL(&ia->ia_prefixmask.sin6_addr,
    986 			    &ifra->ifra_prefixmask.sin6_addr))
    987 				in6_ifremprefix(ia);
    988 		}
    989 		ia->ia_prefixmask = ifra->ifra_prefixmask;
    990 	}
    991 
    992 	/* Set destination address. */
    993 	if (dst6.sin6_family == AF_INET6) {
    994 		if (!IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
    995 		    &ia->ia_dstaddr.sin6_addr))
    996 			in6_ifremprefix(ia);
    997 		ia->ia_dstaddr = dst6;
    998 	}
    999 
   1000 	/*
   1001 	 * Set lifetimes.  We do not refer to ia6t_expire and ia6t_preferred
   1002 	 * to see if the address is deprecated or invalidated, but initialize
   1003 	 * these members for applications.
   1004 	 */
   1005 	ia->ia6_lifetime = ifra->ifra_lifetime;
   1006 	if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
   1007 		ia->ia6_lifetime.ia6t_expire =
   1008 		    time_uptime + ia->ia6_lifetime.ia6t_vltime;
   1009 	} else
   1010 		ia->ia6_lifetime.ia6t_expire = 0;
   1011 	if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
   1012 		ia->ia6_lifetime.ia6t_preferred =
   1013 		    time_uptime + ia->ia6_lifetime.ia6t_pltime;
   1014 	} else
   1015 		ia->ia6_lifetime.ia6t_preferred = 0;
   1016 
   1017 	/*
   1018 	 * configure address flags.
   1019 	 * We need to preserve tentative state so DAD works if
   1020 	 * something adds the same address before DAD finishes.
   1021 	 */
   1022 	was_tentative = ia->ia6_flags & (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED);
   1023 	ia->ia6_flags = ifra->ifra_flags;
   1024 
   1025 	/*
   1026 	 * Make the address tentative before joining multicast addresses,
   1027 	 * so that corresponding MLD responses would not have a tentative
   1028 	 * source address.
   1029 	 */
   1030 	ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/* safety */
   1031 	if (ifp->if_link_state == LINK_STATE_DOWN) {
   1032 		ia->ia6_flags |= IN6_IFF_DETACHED;
   1033 		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1034 	} else if ((hostIsNew || was_tentative) && if_do_dad(ifp))
   1035 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
   1036 
   1037 	/*
   1038 	 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
   1039 	 * userland, make it deprecated.
   1040 	 */
   1041 	if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
   1042 		ia->ia6_lifetime.ia6t_pltime = 0;
   1043 		ia->ia6_lifetime.ia6t_preferred = time_uptime;
   1044 	}
   1045 
   1046 	if (hostIsNew) {
   1047 		/*
   1048 		 * We need a reference to ia before calling in6_ifinit.
   1049 		 * Otherwise ia can be freed in in6_ifinit accidentally.
   1050 		 */
   1051 		ifaref(&ia->ia_ifa);
   1052 	}
   1053 	/* reset the interface and routing table appropriately. */
   1054 	error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew);
   1055 	if (error != 0) {
   1056 		if (hostIsNew)
   1057 			free(ia, M_IFADDR);
   1058 		goto exit;
   1059 	}
   1060 
   1061 	/*
   1062 	 * We are done if we have simply modified an existing address.
   1063 	 */
   1064 	if (!hostIsNew)
   1065 		return error;
   1066 
   1067 	/*
   1068 	 * Insert ia to the global list and ifa to the interface's list.
   1069 	 * A reference to it is already gained above.
   1070 	 */
   1071 	mutex_enter(&in6_ifaddr_lock);
   1072 	IN6_ADDRLIST_WRITER_INSERT_TAIL(ia);
   1073 	mutex_exit(&in6_ifaddr_lock);
   1074 
   1075 	ifa_insert(ifp, &ia->ia_ifa);
   1076 
   1077 	/*
   1078 	 * Beyond this point, we should call in6_purgeaddr upon an error,
   1079 	 * not just go to unlink.
   1080 	 */
   1081 
   1082 	/* join necessary multicast groups */
   1083 	if ((ifp->if_flags & IFF_MULTICAST) != 0) {
   1084 		struct sockaddr_in6 mltaddr, mltmask;
   1085 		struct in6_addr llsol;
   1086 
   1087 		/* join solicited multicast addr for new host id */
   1088 		memset(&llsol, 0, sizeof(struct in6_addr));
   1089 		llsol.s6_addr16[0] = htons(0xff02);
   1090 		llsol.s6_addr32[1] = 0;
   1091 		llsol.s6_addr32[2] = htonl(1);
   1092 		llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
   1093 		llsol.s6_addr8[12] = 0xff;
   1094 		if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
   1095 			/* XXX: should not happen */
   1096 			log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
   1097 			goto cleanup;
   1098 		}
   1099 		dad_delay = 0;
   1100 		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
   1101 			/*
   1102 			 * We need a random delay for DAD on the address
   1103 			 * being configured.  It also means delaying
   1104 			 * transmission of the corresponding MLD report to
   1105 			 * avoid report collision.
   1106 			 * [draft-ietf-ipv6-rfc2462bis-02.txt]
   1107 			 */
   1108 			dad_delay = cprng_fast32() %
   1109 			    (MAX_RTR_SOLICITATION_DELAY * hz);
   1110 		}
   1111 
   1112 #define	MLTMASK_LEN  4	/* mltmask's masklen (=32bit=4octet) */
   1113 		/* join solicited multicast addr for new host id */
   1114 		imm = in6_joingroup(ifp, &llsol, &error, dad_delay);
   1115 		if (!imm) {
   1116 			nd6log(LOG_ERR,
   1117 			    "addmulti failed for %s on %s (errno=%d)\n",
   1118 			    IN6_PRINT(ip6buf, &llsol), if_name(ifp), error);
   1119 			goto cleanup;
   1120 		}
   1121 		LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
   1122 		in6m_sol = imm->i6mm_maddr;
   1123 
   1124 		sockaddr_in6_init(&mltmask, &in6mask32, 0, 0, 0);
   1125 
   1126 		/*
   1127 		 * join link-local all-nodes address
   1128 		 */
   1129 		sockaddr_in6_init(&mltaddr, &in6addr_linklocal_allnodes,
   1130 		    0, 0, 0);
   1131 		if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
   1132 			goto cleanup; /* XXX: should not fail */
   1133 
   1134 		/*
   1135 		 * XXX: do we really need this automatic routes?
   1136 		 * We should probably reconsider this stuff.  Most applications
   1137 		 * actually do not need the routes, since they usually specify
   1138 		 * the outgoing interface.
   1139 		 */
   1140 		rt = rtalloc1(sin6tosa(&mltaddr), 0);
   1141 		if (rt) {
   1142 			if (memcmp(&mltaddr.sin6_addr,
   1143 			    &satocsin6(rt_getkey(rt))->sin6_addr,
   1144 			    MLTMASK_LEN)) {
   1145 				rt_unref(rt);
   1146 				rt = NULL;
   1147 			} else if (rt->rt_ifp != ifp) {
   1148 				IN6_DPRINTF("%s: rt_ifp %p -> %p (%s) "
   1149 				    "network %04x:%04x::/32 = %04x:%04x::/32\n",
   1150 				    __func__, rt->rt_ifp, ifp, ifp->if_xname,
   1151 				    ntohs(mltaddr.sin6_addr.s6_addr16[0]),
   1152 				    ntohs(mltaddr.sin6_addr.s6_addr16[1]),
   1153 				    satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[0],
   1154 				    satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[1]);
   1155 				rt_replace_ifa(rt, &ia->ia_ifa);
   1156 				rt->rt_ifp = ifp;
   1157 			}
   1158 		}
   1159 		if (!rt) {
   1160 			struct rt_addrinfo info;
   1161 
   1162 			memset(&info, 0, sizeof(info));
   1163 			info.rti_info[RTAX_DST] = sin6tosa(&mltaddr);
   1164 			info.rti_info[RTAX_GATEWAY] = sin6tosa(&ia->ia_addr);
   1165 			info.rti_info[RTAX_NETMASK] = sin6tosa(&mltmask);
   1166 			info.rti_info[RTAX_IFA] = sin6tosa(&ia->ia_addr);
   1167 			/* XXX: we need RTF_CONNECTED to fake nd6_rtrequest */
   1168 			info.rti_flags = RTF_UP | RTF_CONNECTED;
   1169 			error = rtrequest1(RTM_ADD, &info, NULL);
   1170 			if (error)
   1171 				goto cleanup;
   1172 		} else {
   1173 			rt_unref(rt);
   1174 		}
   1175 		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
   1176 		if (!imm) {
   1177 			nd6log(LOG_WARNING,
   1178 			    "addmulti failed for %s on %s (errno=%d)\n",
   1179 			    IN6_PRINT(ip6buf, &mltaddr.sin6_addr),
   1180 			    if_name(ifp), error);
   1181 			goto cleanup;
   1182 		}
   1183 		LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
   1184 
   1185 		/*
   1186 		 * join node information group address
   1187 		 */
   1188 		dad_delay = 0;
   1189 		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
   1190 			/*
   1191 			 * The spec doesn't say anything about delay for this
   1192 			 * group, but the same logic should apply.
   1193 			 */
   1194 			dad_delay = cprng_fast32() %
   1195 			    (MAX_RTR_SOLICITATION_DELAY * hz);
   1196 		}
   1197 		if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr) != 0)
   1198 			;
   1199 		else if ((imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
   1200 		          dad_delay)) == NULL) { /* XXX jinmei */
   1201 			nd6log(LOG_WARNING,
   1202 			    "addmulti failed for %s on %s (errno=%d)\n",
   1203 			    IN6_PRINT(ip6buf, &mltaddr.sin6_addr),
   1204 			    if_name(ifp), error);
   1205 			/* XXX not very fatal, go on... */
   1206 		} else {
   1207 			LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
   1208 		}
   1209 
   1210 
   1211 		/*
   1212 		 * join interface-local all-nodes address.
   1213 		 * (ff01::1%ifN, and ff01::%ifN/32)
   1214 		 */
   1215 		mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
   1216 		if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
   1217 			goto cleanup; /* XXX: should not fail */
   1218 
   1219 		/* XXX: again, do we really need the route? */
   1220 		rt = rtalloc1(sin6tosa(&mltaddr), 0);
   1221 		if (rt) {
   1222 			/* 32bit came from "mltmask" */
   1223 			if (memcmp(&mltaddr.sin6_addr,
   1224 			    &satocsin6(rt_getkey(rt))->sin6_addr,
   1225 			    32 / NBBY)) {
   1226 				rt_unref(rt);
   1227 				rt = NULL;
   1228 			} else if (rt->rt_ifp != ifp) {
   1229 				IN6_DPRINTF("%s: rt_ifp %p -> %p (%s) "
   1230 				    "network %04x:%04x::/32 = %04x:%04x::/32\n",
   1231 				    __func__, rt->rt_ifp, ifp, ifp->if_xname,
   1232 				    ntohs(mltaddr.sin6_addr.s6_addr16[0]),
   1233 				    ntohs(mltaddr.sin6_addr.s6_addr16[1]),
   1234 				    satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[0],
   1235 				    satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[1]);
   1236 				rt_replace_ifa(rt, &ia->ia_ifa);
   1237 				rt->rt_ifp = ifp;
   1238 			}
   1239 		}
   1240 		if (!rt) {
   1241 			struct rt_addrinfo info;
   1242 
   1243 			memset(&info, 0, sizeof(info));
   1244 			info.rti_info[RTAX_DST] = sin6tosa(&mltaddr);
   1245 			info.rti_info[RTAX_GATEWAY] = sin6tosa(&ia->ia_addr);
   1246 			info.rti_info[RTAX_NETMASK] = sin6tosa(&mltmask);
   1247 			info.rti_info[RTAX_IFA] = sin6tosa(&ia->ia_addr);
   1248 			info.rti_flags = RTF_UP | RTF_CONNECTED;
   1249 			error = rtrequest1(RTM_ADD, &info, NULL);
   1250 			if (error)
   1251 				goto cleanup;
   1252 #undef	MLTMASK_LEN
   1253 		} else {
   1254 			rt_unref(rt);
   1255 		}
   1256 		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
   1257 		if (!imm) {
   1258 			nd6log(LOG_WARNING,
   1259 			    "addmulti failed for %s on %s (errno=%d)\n",
   1260 			    IN6_PRINT(ip6buf, &mltaddr.sin6_addr),
   1261 			    if_name(ifp), error);
   1262 			goto cleanup;
   1263 		} else {
   1264 			LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
   1265 		}
   1266 	}
   1267 
   1268 	/* Add local address to lltable, if necessary (ex. on p2p link). */
   1269 	error = nd6_add_ifa_lle(ia);
   1270 	if (error != 0)
   1271 		goto cleanup;
   1272 
   1273 	/*
   1274 	 * Perform DAD, if needed.
   1275 	 * XXX It may be of use, if we can administratively
   1276 	 * disable DAD.
   1277 	 */
   1278 	if (hostIsNew && if_do_dad(ifp) &&
   1279 	    ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
   1280 	    (ia->ia6_flags & IN6_IFF_TENTATIVE))
   1281 	{
   1282 		int mindelay, maxdelay;
   1283 
   1284 		dad_delay = 0;
   1285 		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
   1286 			/*
   1287 			 * We need to impose a delay before sending an NS
   1288 			 * for DAD.  Check if we also needed a delay for the
   1289 			 * corresponding MLD message.  If we did, the delay
   1290 			 * should be larger than the MLD delay (this could be
   1291 			 * relaxed a bit, but this simple logic is at least
   1292 			 * safe).
   1293 			 */
   1294 			mindelay = 0;
   1295 			if (in6m_sol != NULL &&
   1296 			    in6m_sol->in6m_state == MLD_REPORTPENDING) {
   1297 				mindelay = in6m_sol->in6m_timer;
   1298 			}
   1299 			maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
   1300 			if (maxdelay - mindelay == 0)
   1301 				dad_delay = 0;
   1302 			else {
   1303 				dad_delay =
   1304 				    (cprng_fast32() % (maxdelay - mindelay)) +
   1305 				    mindelay;
   1306 			}
   1307 		}
   1308 		/* +1 ensures callout is always used */
   1309 		nd6_dad_start(&ia->ia_ifa, dad_delay + 1);
   1310 	}
   1311 
   1312 	if (iap != NULL) {
   1313 		*iap = ia;
   1314 		if (hostIsNew)
   1315 			ia6_acquire(ia, psref);
   1316 	}
   1317 
   1318 	return 0;
   1319 
   1320   cleanup:
   1321 	in6_purgeaddr(&ia->ia_ifa);
   1322   exit:
   1323 	return error;
   1324 }
   1325 
   1326 int
   1327 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int flags)
   1328 {
   1329 	int rc, s;
   1330 
   1331 	s = splsoftnet();
   1332 	rc = in6_update_ifa1(ifp, ifra, NULL, NULL, flags);
   1333 	splx(s);
   1334 	return rc;
   1335 }
   1336 
   1337 void
   1338 in6_purgeaddr(struct ifaddr *ifa)
   1339 {
   1340 	struct ifnet *ifp = ifa->ifa_ifp;
   1341 	struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
   1342 	struct in6_multi_mship *imm;
   1343 
   1344 	KASSERT(!ifa_held(ifa));
   1345 
   1346 	ifa->ifa_flags |= IFA_DESTROYING;
   1347 
   1348 	/* stop DAD processing */
   1349 	nd6_dad_stop(ifa);
   1350 
   1351 	/* Delete any network route. */
   1352 	in6_ifremprefix(ia);
   1353 
   1354 	/* Remove ownaddr's loopback rtentry, if it exists. */
   1355 	in6_ifremlocal(&(ia->ia_ifa));
   1356 
   1357 	/*
   1358 	 * leave from multicast groups we have joined for the interface
   1359 	 */
   1360 	while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
   1361 		LIST_REMOVE(imm, i6mm_chain);
   1362 		in6_leavegroup(imm);
   1363 	}
   1364 
   1365 	in6_unlink_ifa(ia, ifp);
   1366 }
   1367 
   1368 static void
   1369 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
   1370 {
   1371 	int	s = splsoftnet();
   1372 
   1373 	mutex_enter(&in6_ifaddr_lock);
   1374 	IN6_ADDRLIST_WRITER_REMOVE(ia);
   1375 	ifa_remove(ifp, &ia->ia_ifa);
   1376 	mutex_exit(&in6_ifaddr_lock);
   1377 
   1378 	/*
   1379 	 * Release the reference to the ND prefix.
   1380 	 */
   1381 	if (ia->ia6_ndpr != NULL) {
   1382 		nd6_prefix_unref(ia->ia6_ndpr);
   1383 		ia->ia6_ndpr = NULL;
   1384 	}
   1385 
   1386 	/*
   1387 	 * Also, if the address being removed is autoconf'ed, call
   1388 	 * nd6_pfxlist_onlink_check() since the release might affect the status of
   1389 	 * other (detached) addresses.
   1390 	 */
   1391 	if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
   1392 		ND6_WLOCK();
   1393 		nd6_pfxlist_onlink_check();
   1394 		ND6_UNLOCK();
   1395 	}
   1396 
   1397 	IN6_ADDRLIST_ENTRY_DESTROY(ia);
   1398 
   1399 	/*
   1400 	 * release another refcnt for the link from in6_ifaddr.
   1401 	 * Note that we should decrement the refcnt at least once for all *BSD.
   1402 	 */
   1403 	ifafree(&ia->ia_ifa);
   1404 
   1405 	splx(s);
   1406 }
   1407 
   1408 void
   1409 in6_purgeif(struct ifnet *ifp)
   1410 {
   1411 
   1412 	in6_ifdetach(ifp);
   1413 }
   1414 
   1415 /*
   1416  * SIOC[GAD]LIFADDR.
   1417  *	SIOCGLIFADDR: get first address. (?)
   1418  *	SIOCGLIFADDR with IFLR_PREFIX:
   1419  *		get first address that matches the specified prefix.
   1420  *	SIOCALIFADDR: add the specified address.
   1421  *	SIOCALIFADDR with IFLR_PREFIX:
   1422  *		add the specified prefix, filling hostid part from
   1423  *		the first link-local address.  prefixlen must be <= 64.
   1424  *	SIOCDLIFADDR: delete the specified address.
   1425  *	SIOCDLIFADDR with IFLR_PREFIX:
   1426  *		delete the first address that matches the specified prefix.
   1427  * return values:
   1428  *	EINVAL on invalid parameters
   1429  *	EADDRNOTAVAIL on prefix match failed/specified address not found
   1430  *	other values may be returned from in6_ioctl()
   1431  *
   1432  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
   1433  * this is to accommodate address naming scheme other than RFC2374,
   1434  * in the future.
   1435  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
   1436  * address encoding scheme. (see figure on page 8)
   1437  */
   1438 static int
   1439 in6_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
   1440 	struct ifnet *ifp)
   1441 {
   1442 	struct in6_ifaddr *ia = NULL; /* XXX gcc 4.8 maybe-uninitialized */
   1443 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
   1444 	struct ifaddr *ifa;
   1445 	struct sockaddr *sa;
   1446 
   1447 	/* sanity checks */
   1448 	if (!data || !ifp) {
   1449 		panic("invalid argument to in6_lifaddr_ioctl");
   1450 		/* NOTREACHED */
   1451 	}
   1452 
   1453 	switch (cmd) {
   1454 	case SIOCGLIFADDR:
   1455 		/* address must be specified on GET with IFLR_PREFIX */
   1456 		if ((iflr->flags & IFLR_PREFIX) == 0)
   1457 			break;
   1458 		/* FALLTHROUGH */
   1459 	case SIOCALIFADDR:
   1460 	case SIOCDLIFADDR:
   1461 		/* address must be specified on ADD and DELETE */
   1462 		sa = (struct sockaddr *)&iflr->addr;
   1463 		if (sa->sa_family != AF_INET6)
   1464 			return EINVAL;
   1465 		if (sa->sa_len != sizeof(struct sockaddr_in6))
   1466 			return EINVAL;
   1467 		/* XXX need improvement */
   1468 		sa = (struct sockaddr *)&iflr->dstaddr;
   1469 		if (sa->sa_family && sa->sa_family != AF_INET6)
   1470 			return EINVAL;
   1471 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
   1472 			return EINVAL;
   1473 		break;
   1474 	default: /* shouldn't happen */
   1475 #if 0
   1476 		panic("invalid cmd to in6_lifaddr_ioctl");
   1477 		/* NOTREACHED */
   1478 #else
   1479 		return EOPNOTSUPP;
   1480 #endif
   1481 	}
   1482 	if (sizeof(struct in6_addr) * NBBY < iflr->prefixlen)
   1483 		return EINVAL;
   1484 
   1485 	switch (cmd) {
   1486 	case SIOCALIFADDR:
   1487 	    {
   1488 		struct in6_aliasreq ifra;
   1489 		struct in6_addr *xhostid = NULL;
   1490 		int prefixlen;
   1491 		int bound = curlwp_bind();
   1492 		struct psref psref;
   1493 
   1494 		if ((iflr->flags & IFLR_PREFIX) != 0) {
   1495 			struct sockaddr_in6 *sin6;
   1496 
   1497 			/*
   1498 			 * xhostid is to fill in the hostid part of the
   1499 			 * address.  xhostid points to the first link-local
   1500 			 * address attached to the interface.
   1501 			 */
   1502 			ia = in6ifa_ifpforlinklocal_psref(ifp, 0, &psref);
   1503 			if (ia == NULL) {
   1504 				curlwp_bindx(bound);
   1505 				return EADDRNOTAVAIL;
   1506 			}
   1507 			xhostid = IFA_IN6(&ia->ia_ifa);
   1508 
   1509 		 	/* prefixlen must be <= 64. */
   1510 			if (64 < iflr->prefixlen) {
   1511 				ia6_release(ia, &psref);
   1512 				curlwp_bindx(bound);
   1513 				return EINVAL;
   1514 			}
   1515 			prefixlen = iflr->prefixlen;
   1516 
   1517 			/* hostid part must be zero. */
   1518 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1519 			if (sin6->sin6_addr.s6_addr32[2] != 0
   1520 			 || sin6->sin6_addr.s6_addr32[3] != 0) {
   1521 				ia6_release(ia, &psref);
   1522 				curlwp_bindx(bound);
   1523 				return EINVAL;
   1524 			}
   1525 		} else
   1526 			prefixlen = iflr->prefixlen;
   1527 
   1528 		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
   1529 		memset(&ifra, 0, sizeof(ifra));
   1530 		memcpy(ifra.ifra_name, iflr->iflr_name, sizeof(ifra.ifra_name));
   1531 
   1532 		memcpy(&ifra.ifra_addr, &iflr->addr,
   1533 		    ((struct sockaddr *)&iflr->addr)->sa_len);
   1534 		if (xhostid) {
   1535 			/* fill in hostid part */
   1536 			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
   1537 			    xhostid->s6_addr32[2];
   1538 			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
   1539 			    xhostid->s6_addr32[3];
   1540 		}
   1541 
   1542 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
   1543 			memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
   1544 			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
   1545 			if (xhostid) {
   1546 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
   1547 				    xhostid->s6_addr32[2];
   1548 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
   1549 				    xhostid->s6_addr32[3];
   1550 			}
   1551 		}
   1552 		if (xhostid) {
   1553 			ia6_release(ia, &psref);
   1554 			ia = NULL;
   1555 		}
   1556 		curlwp_bindx(bound);
   1557 
   1558 		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
   1559 		in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
   1560 
   1561 		ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
   1562 		ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
   1563 		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
   1564 		return in6_control(so, SIOCAIFADDR_IN6, &ifra, ifp);
   1565 	    }
   1566 	case SIOCGLIFADDR:
   1567 	case SIOCDLIFADDR:
   1568 	    {
   1569 		struct in6_addr mask, candidate, match;
   1570 		struct sockaddr_in6 *sin6;
   1571 		int cmp;
   1572 		int error, s;
   1573 
   1574 		memset(&mask, 0, sizeof(mask));
   1575 		if (iflr->flags & IFLR_PREFIX) {
   1576 			/* lookup a prefix rather than address. */
   1577 			in6_prefixlen2mask(&mask, iflr->prefixlen);
   1578 
   1579 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1580 			memcpy(&match, &sin6->sin6_addr, sizeof(match));
   1581 			match.s6_addr32[0] &= mask.s6_addr32[0];
   1582 			match.s6_addr32[1] &= mask.s6_addr32[1];
   1583 			match.s6_addr32[2] &= mask.s6_addr32[2];
   1584 			match.s6_addr32[3] &= mask.s6_addr32[3];
   1585 
   1586 			/* if you set extra bits, that's wrong */
   1587 			if (memcmp(&match, &sin6->sin6_addr, sizeof(match)))
   1588 				return EINVAL;
   1589 
   1590 			cmp = 1;
   1591 		} else {
   1592 			if (cmd == SIOCGLIFADDR) {
   1593 				/* on getting an address, take the 1st match */
   1594 				cmp = 0;	/* XXX */
   1595 			} else {
   1596 				/* on deleting an address, do exact match */
   1597 				in6_prefixlen2mask(&mask, 128);
   1598 				sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1599 				memcpy(&match, &sin6->sin6_addr, sizeof(match));
   1600 
   1601 				cmp = 1;
   1602 			}
   1603 		}
   1604 
   1605 		s = pserialize_read_enter();
   1606 		IFADDR_READER_FOREACH(ifa, ifp) {
   1607 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1608 				continue;
   1609 			if (!cmp)
   1610 				break;
   1611 
   1612 			/*
   1613 			 * XXX: this is adhoc, but is necessary to allow
   1614 			 * a user to specify fe80::/64 (not /10) for a
   1615 			 * link-local address.
   1616 			 */
   1617 			memcpy(&candidate, IFA_IN6(ifa), sizeof(candidate));
   1618 			in6_clearscope(&candidate);
   1619 			candidate.s6_addr32[0] &= mask.s6_addr32[0];
   1620 			candidate.s6_addr32[1] &= mask.s6_addr32[1];
   1621 			candidate.s6_addr32[2] &= mask.s6_addr32[2];
   1622 			candidate.s6_addr32[3] &= mask.s6_addr32[3];
   1623 			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
   1624 				break;
   1625 		}
   1626 		if (!ifa) {
   1627 			error = EADDRNOTAVAIL;
   1628 			goto error;
   1629 		}
   1630 		ia = ifa2ia6(ifa);
   1631 
   1632 		if (cmd == SIOCGLIFADDR) {
   1633 			/* fill in the if_laddrreq structure */
   1634 			memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin6_len);
   1635 			error = sa6_recoverscope(
   1636 			    (struct sockaddr_in6 *)&iflr->addr);
   1637 			if (error != 0)
   1638 				goto error;
   1639 
   1640 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1641 				memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
   1642 				    ia->ia_dstaddr.sin6_len);
   1643 				error = sa6_recoverscope(
   1644 				    (struct sockaddr_in6 *)&iflr->dstaddr);
   1645 				if (error != 0)
   1646 					goto error;
   1647 			} else
   1648 				memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
   1649 
   1650 			iflr->prefixlen =
   1651 			    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
   1652 
   1653 			iflr->flags = ia->ia6_flags;	/* XXX */
   1654 
   1655 			error = 0;
   1656 		} else {
   1657 			struct in6_aliasreq ifra;
   1658 
   1659 			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
   1660 			memset(&ifra, 0, sizeof(ifra));
   1661 			memcpy(ifra.ifra_name, iflr->iflr_name,
   1662 			    sizeof(ifra.ifra_name));
   1663 
   1664 			memcpy(&ifra.ifra_addr, &ia->ia_addr,
   1665 			    ia->ia_addr.sin6_len);
   1666 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1667 				memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
   1668 				    ia->ia_dstaddr.sin6_len);
   1669 			} else {
   1670 				memset(&ifra.ifra_dstaddr, 0,
   1671 				    sizeof(ifra.ifra_dstaddr));
   1672 			}
   1673 			memcpy(&ifra.ifra_dstaddr, &ia->ia_prefixmask,
   1674 			    ia->ia_prefixmask.sin6_len);
   1675 
   1676 			ifra.ifra_flags = ia->ia6_flags;
   1677 			pserialize_read_exit(s);
   1678 
   1679 			return in6_control(so, SIOCDIFADDR_IN6, &ifra, ifp);
   1680 		}
   1681 	error:
   1682 		pserialize_read_exit(s);
   1683 		return error;
   1684 	    }
   1685 	}
   1686 
   1687 	return EOPNOTSUPP;	/* just for safety */
   1688 }
   1689 
   1690 /*
   1691  * Initialize an interface's internet6 address
   1692  * and routing table entry.
   1693  */
   1694 static int
   1695 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
   1696 	const struct sockaddr_in6 *sin6, int newhost)
   1697 {
   1698 	int	error = 0, ifacount = 0;
   1699 	int	s = splsoftnet();
   1700 	struct ifaddr *ifa;
   1701 
   1702 	/*
   1703 	 * Give the interface a chance to initialize
   1704 	 * if this is its first address,
   1705 	 * and to validate the address if necessary.
   1706 	 */
   1707 	IFADDR_READER_FOREACH(ifa, ifp) {
   1708 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1709 			continue;
   1710 		ifacount++;
   1711 	}
   1712 
   1713 	ia->ia_addr = *sin6;
   1714 
   1715 	if (ifacount <= 0 &&
   1716 	    (error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0) {
   1717 		splx(s);
   1718 		return error;
   1719 	}
   1720 	splx(s);
   1721 
   1722 	ia->ia_ifa.ifa_metric = ifp->if_metric;
   1723 
   1724 	/* we could do in(6)_socktrim here, but just omit it at this moment. */
   1725 
   1726 	/* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
   1727 	if (newhost) {
   1728 		/* set the rtrequest function to create llinfo */
   1729 		if (ifp->if_flags & IFF_POINTOPOINT)
   1730 			ia->ia_ifa.ifa_rtrequest = p2p_rtrequest;
   1731 		else if ((ifp->if_flags & IFF_LOOPBACK) == 0)
   1732 			ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
   1733 		in6_ifaddlocal(&ia->ia_ifa);
   1734 	} else {
   1735 		/* Inform the routing socket of new flags/timings */
   1736 		rt_newaddrmsg(RTM_NEWADDR, &ia->ia_ifa, 0, NULL);
   1737 	}
   1738 
   1739 	/* Add the network prefix route. */
   1740 	if ((error = in6_ifaddprefix(ia)) != 0) {
   1741 		if (newhost)
   1742 			in6_ifremlocal(&ia->ia_ifa);
   1743 		return error;
   1744 	}
   1745 
   1746 	return error;
   1747 }
   1748 
   1749 static struct ifaddr *
   1750 bestifa(struct ifaddr *best_ifa, struct ifaddr *ifa)
   1751 {
   1752 	if (best_ifa == NULL || best_ifa->ifa_preference < ifa->ifa_preference)
   1753 		return ifa;
   1754 	return best_ifa;
   1755 }
   1756 
   1757 /*
   1758  * Find an IPv6 interface link-local address specific to an interface.
   1759  */
   1760 struct in6_ifaddr *
   1761 in6ifa_ifpforlinklocal(const struct ifnet *ifp, const int ignoreflags)
   1762 {
   1763 	struct ifaddr *best_ifa = NULL, *ifa;
   1764 
   1765 	IFADDR_READER_FOREACH(ifa, ifp) {
   1766 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1767 			continue;
   1768 		if (!IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)))
   1769 			continue;
   1770 		if ((((struct in6_ifaddr *)ifa)->ia6_flags & ignoreflags) != 0)
   1771 			continue;
   1772 		best_ifa = bestifa(best_ifa, ifa);
   1773 	}
   1774 
   1775 	return (struct in6_ifaddr *)best_ifa;
   1776 }
   1777 
   1778 struct in6_ifaddr *
   1779 in6ifa_ifpforlinklocal_psref(const struct ifnet *ifp, const int ignoreflags,
   1780     struct psref *psref)
   1781 {
   1782 	struct in6_ifaddr *ia;
   1783 	int s = pserialize_read_enter();
   1784 
   1785 	ia = in6ifa_ifpforlinklocal(ifp, ignoreflags);
   1786 	if (ia != NULL)
   1787 		ia6_acquire(ia, psref);
   1788 	pserialize_read_exit(s);
   1789 
   1790 	return ia;
   1791 }
   1792 
   1793 /*
   1794  * find the internet address corresponding to a given address.
   1795  * ifaddr is returned referenced.
   1796  */
   1797 struct in6_ifaddr *
   1798 in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid)
   1799 {
   1800 	struct in6_ifaddr *ia;
   1801 	int s;
   1802 
   1803 	s = pserialize_read_enter();
   1804 	IN6_ADDRLIST_READER_FOREACH(ia) {
   1805 		if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) {
   1806 			if (zoneid != 0 &&
   1807 			    zoneid != ia->ia_addr.sin6_scope_id)
   1808 				continue;
   1809 			ifaref(&ia->ia_ifa);
   1810 			break;
   1811 		}
   1812 	}
   1813 	pserialize_read_exit(s);
   1814 
   1815 	return ia;
   1816 }
   1817 
   1818 /*
   1819  * find the internet address corresponding to a given interface and address.
   1820  */
   1821 struct in6_ifaddr *
   1822 in6ifa_ifpwithaddr(const struct ifnet *ifp, const struct in6_addr *addr)
   1823 {
   1824 	struct ifaddr *best_ifa = NULL, *ifa;
   1825 
   1826 	IFADDR_READER_FOREACH(ifa, ifp) {
   1827 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1828 			continue;
   1829 		if (!IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
   1830 			continue;
   1831 		best_ifa = bestifa(best_ifa, ifa);
   1832 	}
   1833 
   1834 	return (struct in6_ifaddr *)best_ifa;
   1835 }
   1836 
   1837 struct in6_ifaddr *
   1838 in6ifa_ifpwithaddr_psref(const struct ifnet *ifp, const struct in6_addr *addr,
   1839     struct psref *psref)
   1840 {
   1841 	struct in6_ifaddr *ia;
   1842 	int s = pserialize_read_enter();
   1843 
   1844 	ia = in6ifa_ifpwithaddr(ifp, addr);
   1845 	if (ia != NULL)
   1846 		ia6_acquire(ia, psref);
   1847 	pserialize_read_exit(s);
   1848 
   1849 	return ia;
   1850 }
   1851 
   1852 static struct in6_ifaddr *
   1853 bestia(struct in6_ifaddr *best_ia, struct in6_ifaddr *ia)
   1854 {
   1855 	if (best_ia == NULL ||
   1856 	    best_ia->ia_ifa.ifa_preference < ia->ia_ifa.ifa_preference)
   1857 		return ia;
   1858 	return best_ia;
   1859 }
   1860 
   1861 /*
   1862  * Determine if an address is on a local network.
   1863  */
   1864 int
   1865 in6_localaddr(const struct in6_addr *in6)
   1866 {
   1867 	struct in6_ifaddr *ia;
   1868 	int s;
   1869 
   1870 	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
   1871 		return 1;
   1872 
   1873 	s = pserialize_read_enter();
   1874 	IN6_ADDRLIST_READER_FOREACH(ia) {
   1875 		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
   1876 					      &ia->ia_prefixmask.sin6_addr)) {
   1877 			pserialize_read_exit(s);
   1878 			return 1;
   1879 		}
   1880 	}
   1881 	pserialize_read_exit(s);
   1882 
   1883 	return 0;
   1884 }
   1885 
   1886 int
   1887 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
   1888 {
   1889 	struct in6_ifaddr *ia;
   1890 	int s;
   1891 
   1892 	s = pserialize_read_enter();
   1893 	IN6_ADDRLIST_READER_FOREACH(ia) {
   1894 		if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
   1895 		    &sa6->sin6_addr) &&
   1896 #ifdef SCOPEDROUTING
   1897 		    ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
   1898 #endif
   1899 		    (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
   1900 			pserialize_read_exit(s);
   1901 			return 1; /* true */
   1902 		}
   1903 
   1904 		/* XXX: do we still have to go thru the rest of the list? */
   1905 	}
   1906 	pserialize_read_exit(s);
   1907 
   1908 	return 0;		/* false */
   1909 }
   1910 
   1911 /*
   1912  * return length of part which dst and src are equal
   1913  * hard coding...
   1914  */
   1915 int
   1916 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
   1917 {
   1918 	int match = 0;
   1919 	u_char *s = (u_char *)src, *d = (u_char *)dst;
   1920 	u_char *lim = s + 16, r;
   1921 
   1922 	while (s < lim)
   1923 		if ((r = (*d++ ^ *s++)) != 0) {
   1924 			while (r < 128) {
   1925 				match++;
   1926 				r <<= 1;
   1927 			}
   1928 			break;
   1929 		} else
   1930 			match += NBBY;
   1931 	return match;
   1932 }
   1933 
   1934 /* XXX: to be scope conscious */
   1935 int
   1936 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
   1937 {
   1938 	int bytelen, bitlen;
   1939 
   1940 	/* sanity check */
   1941 	if (len < 0 || len > 128) {
   1942 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
   1943 		    len);
   1944 		return 0;
   1945 	}
   1946 
   1947 	bytelen = len / NBBY;
   1948 	bitlen = len % NBBY;
   1949 
   1950 	if (memcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
   1951 		return 0;
   1952 	if (bitlen != 0 &&
   1953 	    p1->s6_addr[bytelen] >> (NBBY - bitlen) !=
   1954 	    p2->s6_addr[bytelen] >> (NBBY - bitlen))
   1955 		return 0;
   1956 
   1957 	return 1;
   1958 }
   1959 
   1960 void
   1961 in6_prefixlen2mask(struct in6_addr *maskp, int len)
   1962 {
   1963 	static const u_char maskarray[NBBY] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
   1964 	int bytelen, bitlen, i;
   1965 
   1966 	/* sanity check */
   1967 	if (len < 0 || len > 128) {
   1968 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
   1969 		    len);
   1970 		return;
   1971 	}
   1972 
   1973 	memset(maskp, 0, sizeof(*maskp));
   1974 	bytelen = len / NBBY;
   1975 	bitlen = len % NBBY;
   1976 	for (i = 0; i < bytelen; i++)
   1977 		maskp->s6_addr[i] = 0xff;
   1978 	if (bitlen)
   1979 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
   1980 }
   1981 
   1982 /*
   1983  * return the best address out of the same scope. if no address was
   1984  * found, return the first valid address from designated IF.
   1985  */
   1986 struct in6_ifaddr *
   1987 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
   1988 {
   1989 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
   1990 	struct ifaddr *ifa;
   1991 	struct in6_ifaddr *best_ia = NULL, *ia;
   1992 	struct in6_ifaddr *dep[2];	/* last-resort: deprecated */
   1993 
   1994 	dep[0] = dep[1] = NULL;
   1995 
   1996 	/*
   1997 	 * We first look for addresses in the same scope.
   1998 	 * If there is one, return it.
   1999 	 * If two or more, return one which matches the dst longest.
   2000 	 * If none, return one of global addresses assigned other ifs.
   2001 	 */
   2002 	IFADDR_READER_FOREACH(ifa, ifp) {
   2003 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2004 			continue;
   2005 		ia = (struct in6_ifaddr *)ifa;
   2006 		if (ia->ia6_flags & IN6_IFF_ANYCAST)
   2007 			continue; /* XXX: is there any case to allow anycast? */
   2008 		if (ia->ia6_flags & IN6_IFF_NOTREADY)
   2009 			continue; /* don't use this interface */
   2010 		if (ia->ia6_flags & IN6_IFF_DETACHED)
   2011 			continue;
   2012 		if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
   2013 			if (ip6_use_deprecated)
   2014 				dep[0] = ia;
   2015 			continue;
   2016 		}
   2017 
   2018 		if (dst_scope != in6_addrscope(IFA_IN6(ifa)))
   2019 			continue;
   2020 		/*
   2021 		 * call in6_matchlen() as few as possible
   2022 		 */
   2023 		if (best_ia == NULL) {
   2024 			best_ia = ia;
   2025 			continue;
   2026 		}
   2027 		if (blen == -1)
   2028 			blen = in6_matchlen(&best_ia->ia_addr.sin6_addr, dst);
   2029 		tlen = in6_matchlen(IFA_IN6(ifa), dst);
   2030 		if (tlen > blen) {
   2031 			blen = tlen;
   2032 			best_ia = ia;
   2033 		} else if (tlen == blen)
   2034 			best_ia = bestia(best_ia, ia);
   2035 	}
   2036 	if (best_ia != NULL)
   2037 		return best_ia;
   2038 
   2039 	IFADDR_READER_FOREACH(ifa, ifp) {
   2040 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2041 			continue;
   2042 		ia = (struct in6_ifaddr *)ifa;
   2043 		if (ia->ia6_flags & IN6_IFF_ANYCAST)
   2044 			continue; /* XXX: is there any case to allow anycast? */
   2045 		if (ia->ia6_flags & IN6_IFF_NOTREADY)
   2046 			continue; /* don't use this interface */
   2047 		if (ia->ia6_flags & IN6_IFF_DETACHED)
   2048 			continue;
   2049 		if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
   2050 			if (ip6_use_deprecated)
   2051 				dep[1] = (struct in6_ifaddr *)ifa;
   2052 			continue;
   2053 		}
   2054 
   2055 		best_ia = bestia(best_ia, ia);
   2056 	}
   2057 	if (best_ia != NULL)
   2058 		return best_ia;
   2059 
   2060 	/* use the last-resort values, that are, deprecated addresses */
   2061 	if (dep[0])
   2062 		return dep[0];
   2063 	if (dep[1])
   2064 		return dep[1];
   2065 
   2066 	return NULL;
   2067 }
   2068 
   2069 /*
   2070  * perform DAD when interface becomes IFF_UP.
   2071  */
   2072 void
   2073 in6_if_link_up(struct ifnet *ifp)
   2074 {
   2075 	struct ifaddr *ifa;
   2076 	struct in6_ifaddr *ia;
   2077 	int s, bound;
   2078 	char ip6buf[INET6_ADDRSTRLEN];
   2079 
   2080 	/* Ensure it's sane to run DAD */
   2081 	if (ifp->if_link_state == LINK_STATE_DOWN)
   2082 		return;
   2083 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
   2084 		return;
   2085 
   2086 	bound = curlwp_bind();
   2087 	s = pserialize_read_enter();
   2088 	IFADDR_READER_FOREACH(ifa, ifp) {
   2089 		struct psref psref;
   2090 
   2091 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2092 			continue;
   2093 
   2094 		ifa_acquire(ifa, &psref);
   2095 		pserialize_read_exit(s);
   2096 		ia = (struct in6_ifaddr *)ifa;
   2097 
   2098 		/* If detached then mark as tentative */
   2099 		if (ia->ia6_flags & IN6_IFF_DETACHED) {
   2100 			ia->ia6_flags &= ~IN6_IFF_DETACHED;
   2101 			if (if_do_dad(ifp)) {
   2102 				ia->ia6_flags |= IN6_IFF_TENTATIVE;
   2103 				nd6log(LOG_ERR, "%s marked tentative\n",
   2104 				    IN6_PRINT(ip6buf,
   2105 				    &ia->ia_addr.sin6_addr));
   2106 			} else if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0)
   2107 				rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   2108 		}
   2109 
   2110 		if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
   2111 			int rand_delay;
   2112 
   2113 			/* Clear the duplicated flag as we're starting DAD. */
   2114 			ia->ia6_flags &= ~IN6_IFF_DUPLICATED;
   2115 
   2116 			/*
   2117 			 * The TENTATIVE flag was likely set by hand
   2118 			 * beforehand, implicitly indicating the need for DAD.
   2119 			 * We may be able to skip the random delay in this
   2120 			 * case, but we impose delays just in case.
   2121 			 */
   2122 			rand_delay = cprng_fast32() %
   2123 			    (MAX_RTR_SOLICITATION_DELAY * hz);
   2124 			/* +1 ensures callout is always used */
   2125 			nd6_dad_start(ifa, rand_delay + 1);
   2126 		}
   2127 
   2128 		s = pserialize_read_enter();
   2129 		ifa_release(ifa, &psref);
   2130 	}
   2131 	pserialize_read_exit(s);
   2132 	curlwp_bindx(bound);
   2133 
   2134 	/* Restore any detached prefixes */
   2135 	ND6_WLOCK();
   2136 	nd6_pfxlist_onlink_check();
   2137 	ND6_UNLOCK();
   2138 }
   2139 
   2140 void
   2141 in6_if_up(struct ifnet *ifp)
   2142 {
   2143 
   2144 	/*
   2145 	 * special cases, like 6to4, are handled in in6_ifattach
   2146 	 */
   2147 	in6_ifattach(ifp, NULL);
   2148 
   2149 	/* interface may not support link state, so bring it up also */
   2150 	in6_if_link_up(ifp);
   2151 }
   2152 
   2153 /*
   2154  * Mark all addresses as detached.
   2155  */
   2156 void
   2157 in6_if_link_down(struct ifnet *ifp)
   2158 {
   2159 	struct ifaddr *ifa;
   2160 	struct in6_ifaddr *ia;
   2161 	int s, bound;
   2162 	char ip6buf[INET6_ADDRSTRLEN];
   2163 
   2164 	/* Any prefixes on this interface should be detached as well */
   2165 	ND6_WLOCK();
   2166 	nd6_pfxlist_onlink_check();
   2167 	ND6_UNLOCK();
   2168 
   2169 	bound = curlwp_bind();
   2170 	s = pserialize_read_enter();
   2171 	IFADDR_READER_FOREACH(ifa, ifp) {
   2172 		struct psref psref;
   2173 
   2174 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2175 			continue;
   2176 
   2177 		ifa_acquire(ifa, &psref);
   2178 		pserialize_read_exit(s);
   2179 		ia = (struct in6_ifaddr *)ifa;
   2180 
   2181 		/* Stop DAD processing */
   2182 		nd6_dad_stop(ifa);
   2183 
   2184 		/*
   2185 		 * Mark the address as detached.
   2186 		 * This satisfies RFC4862 Section 5.3, but we should apply
   2187 		 * this logic to all addresses to be a good citizen and
   2188 		 * avoid potential duplicated addresses.
   2189 		 * When the interface comes up again, detached addresses
   2190 		 * are marked tentative and DAD commences.
   2191 		 */
   2192 		if (!(ia->ia6_flags & IN6_IFF_DETACHED)) {
   2193 			nd6log(LOG_DEBUG, "%s marked detached\n",
   2194 			    IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr));
   2195 			ia->ia6_flags |= IN6_IFF_DETACHED;
   2196 			ia->ia6_flags &=
   2197 			    ~(IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED);
   2198 			rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   2199 		}
   2200 
   2201 		s = pserialize_read_enter();
   2202 		ifa_release(ifa, &psref);
   2203 	}
   2204 	pserialize_read_exit(s);
   2205 	curlwp_bindx(bound);
   2206 }
   2207 
   2208 void
   2209 in6_if_down(struct ifnet *ifp)
   2210 {
   2211 
   2212 	in6_if_link_down(ifp);
   2213 }
   2214 
   2215 void
   2216 in6_if_link_state_change(struct ifnet *ifp, int link_state)
   2217 {
   2218 
   2219 	switch (link_state) {
   2220 	case LINK_STATE_DOWN:
   2221 		in6_if_link_down(ifp);
   2222 		break;
   2223 	case LINK_STATE_UP:
   2224 		in6_if_link_up(ifp);
   2225 		break;
   2226 	}
   2227 }
   2228 
   2229 /*
   2230  * Calculate max IPv6 MTU through all the interfaces and store it
   2231  * to in6_maxmtu.
   2232  */
   2233 void
   2234 in6_setmaxmtu(void)
   2235 {
   2236 	unsigned long maxmtu = 0;
   2237 	struct ifnet *ifp;
   2238 	int s;
   2239 
   2240 	s = pserialize_read_enter();
   2241 	IFNET_READER_FOREACH(ifp) {
   2242 		/* this function can be called during ifnet initialization */
   2243 		if (!ifp->if_afdata[AF_INET6])
   2244 			continue;
   2245 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
   2246 		    IN6_LINKMTU(ifp) > maxmtu)
   2247 			maxmtu = IN6_LINKMTU(ifp);
   2248 	}
   2249 	pserialize_read_exit(s);
   2250 	if (maxmtu)	     /* update only when maxmtu is positive */
   2251 		in6_maxmtu = maxmtu;
   2252 }
   2253 
   2254 /*
   2255  * Provide the length of interface identifiers to be used for the link attached
   2256  * to the given interface.  The length should be defined in "IPv6 over
   2257  * xxx-link" document.  Note that address architecture might also define
   2258  * the length for a particular set of address prefixes, regardless of the
   2259  * link type.  As clarified in rfc2462bis, those two definitions should be
   2260  * consistent, and those really are as of August 2004.
   2261  */
   2262 int
   2263 in6_if2idlen(struct ifnet *ifp)
   2264 {
   2265 	switch (ifp->if_type) {
   2266 	case IFT_ETHER:		/* RFC2464 */
   2267 	case IFT_PROPVIRTUAL:	/* XXX: no RFC. treat it as ether */
   2268 	case IFT_L2VLAN:	/* ditto */
   2269 	case IFT_IEEE80211:	/* ditto */
   2270 	case IFT_FDDI:		/* RFC2467 */
   2271 	case IFT_ISO88025:	/* RFC2470 (IPv6 over Token Ring) */
   2272 	case IFT_PPP:		/* RFC2472 */
   2273 	case IFT_ARCNET:	/* RFC2497 */
   2274 	case IFT_FRELAY:	/* RFC2590 */
   2275 	case IFT_IEEE1394:	/* RFC3146 */
   2276 	case IFT_GIF:		/* draft-ietf-v6ops-mech-v2-07 */
   2277 	case IFT_LOOP:		/* XXX: is this really correct? */
   2278 		return 64;
   2279 	default:
   2280 		/*
   2281 		 * Unknown link type:
   2282 		 * It might be controversial to use the today's common constant
   2283 		 * of 64 for these cases unconditionally.  For full compliance,
   2284 		 * we should return an error in this case.  On the other hand,
   2285 		 * if we simply miss the standard for the link type or a new
   2286 		 * standard is defined for a new link type, the IFID length
   2287 		 * is very likely to be the common constant.  As a compromise,
   2288 		 * we always use the constant, but make an explicit notice
   2289 		 * indicating the "unknown" case.
   2290 		 */
   2291 		printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
   2292 		return 64;
   2293 	}
   2294 }
   2295 
   2296 struct in6_llentry {
   2297 	struct llentry		base;
   2298 };
   2299 
   2300 #define	IN6_LLTBL_DEFAULT_HSIZE	32
   2301 #define	IN6_LLTBL_HASH(k, h) \
   2302 	(((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
   2303 
   2304 /*
   2305  * Do actual deallocation of @lle.
   2306  * Called by LLE_FREE_LOCKED when number of references
   2307  * drops to zero.
   2308  */
   2309 static void
   2310 in6_lltable_destroy_lle(struct llentry *lle)
   2311 {
   2312 
   2313 	LLE_WUNLOCK(lle);
   2314 	LLE_LOCK_DESTROY(lle);
   2315 	kmem_intr_free(lle, sizeof(struct in6_llentry));
   2316 }
   2317 
   2318 static struct llentry *
   2319 in6_lltable_new(const struct in6_addr *addr6, u_int flags)
   2320 {
   2321 	struct in6_llentry *lle;
   2322 
   2323 	lle = kmem_intr_zalloc(sizeof(struct in6_llentry), KM_NOSLEEP);
   2324 	if (lle == NULL)		/* NB: caller generates msg */
   2325 		return NULL;
   2326 
   2327 	lle->base.r_l3addr.addr6 = *addr6;
   2328 	lle->base.lle_refcnt = 1;
   2329 	lle->base.lle_free = in6_lltable_destroy_lle;
   2330 	LLE_LOCK_INIT(&lle->base);
   2331 	callout_init(&lle->base.lle_timer, CALLOUT_MPSAFE);
   2332 
   2333 	return &lle->base;
   2334 }
   2335 
   2336 static int
   2337 in6_lltable_match_prefix(const struct sockaddr *prefix,
   2338     const struct sockaddr *mask, u_int flags, struct llentry *lle)
   2339 {
   2340 	const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
   2341 	const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
   2342 
   2343 	if (IN6_ARE_MASKED_ADDR_EQUAL(&lle->r_l3addr.addr6,
   2344 	    &pfx->sin6_addr, &msk->sin6_addr) &&
   2345 	    ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)))
   2346 		return 1;
   2347 
   2348 	return 0;
   2349 }
   2350 
   2351 static void
   2352 in6_lltable_free_entry(struct lltable *llt, struct llentry *lle)
   2353 {
   2354 	struct ifnet *ifp = llt->llt_ifp;
   2355 
   2356 	IF_AFDATA_WLOCK_ASSERT(ifp);
   2357 	LLE_WLOCK_ASSERT(lle);
   2358 
   2359 	/* Unlink entry from table */
   2360 	if ((lle->la_flags & LLE_LINKED) != 0) {
   2361 
   2362 		lltable_unlink_entry(llt, lle);
   2363 		KASSERT((lle->la_flags & LLE_LINKED) == 0);
   2364 	}
   2365 	/*
   2366 	 * We need to release the lock here to lle_timer proceeds;
   2367 	 * lle_timer should stop immediately if LLE_LINKED isn't set.
   2368 	 * Note that we cannot pass lle->lle_lock to callout_halt
   2369 	 * because it's a rwlock.
   2370 	 */
   2371 	LLE_ADDREF(lle);
   2372 	LLE_WUNLOCK(lle);
   2373 	IF_AFDATA_WUNLOCK(ifp);
   2374 
   2375 #ifdef NET_MPSAFE
   2376 	callout_halt(&lle->lle_timer, NULL);
   2377 #else
   2378 	if (mutex_owned(softnet_lock))
   2379 		callout_halt(&lle->lle_timer, softnet_lock);
   2380 	else
   2381 		callout_halt(&lle->lle_timer, NULL);
   2382 #endif
   2383 	LLE_WLOCK(lle);
   2384 	LLE_REMREF(lle);
   2385 
   2386 	lltable_drop_entry_queue(lle);
   2387 	LLE_FREE_LOCKED(lle);
   2388 
   2389 	IF_AFDATA_WLOCK(ifp);
   2390 }
   2391 
   2392 static int
   2393 in6_lltable_rtcheck(struct ifnet *ifp,
   2394 		    u_int flags,
   2395 		    const struct sockaddr *l3addr)
   2396 {
   2397 	struct rtentry *rt;
   2398 	char ip6buf[INET6_ADDRSTRLEN];
   2399 
   2400 	KASSERTMSG(l3addr->sa_family == AF_INET6,
   2401 	    "sin_family %d", l3addr->sa_family);
   2402 
   2403 	rt = rtalloc1(l3addr, 0);
   2404 	if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
   2405 		int s;
   2406 		struct ifaddr *ifa;
   2407 		/*
   2408 		 * Create an ND6 cache for an IPv6 neighbor
   2409 		 * that is not covered by our own prefix.
   2410 		 */
   2411 		/* XXX ifaof_ifpforaddr should take a const param */
   2412 		s = pserialize_read_enter();
   2413 		ifa = ifaof_ifpforaddr(l3addr, ifp);
   2414 		if (ifa != NULL) {
   2415 			pserialize_read_exit(s);
   2416 			if (rt != NULL)
   2417 				rt_unref(rt);
   2418 			return 0;
   2419 		}
   2420 		pserialize_read_exit(s);
   2421 		log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
   2422 		    IN6_PRINT(ip6buf,
   2423 		    &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
   2424 		if (rt != NULL)
   2425 			rt_unref(rt);
   2426 		return EINVAL;
   2427 	}
   2428 	rt_unref(rt);
   2429 	return 0;
   2430 }
   2431 
   2432 static inline uint32_t
   2433 in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize)
   2434 {
   2435 
   2436 	return IN6_LLTBL_HASH(dst->s6_addr32[3], hsize);
   2437 }
   2438 
   2439 static uint32_t
   2440 in6_lltable_hash(const struct llentry *lle, uint32_t hsize)
   2441 {
   2442 
   2443 	return in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize);
   2444 }
   2445 
   2446 static void
   2447 in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
   2448 {
   2449 	struct sockaddr_in6 *sin6;
   2450 
   2451 	sin6 = (struct sockaddr_in6 *)sa;
   2452 	bzero(sin6, sizeof(*sin6));
   2453 	sin6->sin6_family = AF_INET6;
   2454 	sin6->sin6_len = sizeof(*sin6);
   2455 	sin6->sin6_addr = lle->r_l3addr.addr6;
   2456 }
   2457 
   2458 static inline struct llentry *
   2459 in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst)
   2460 {
   2461 	struct llentry *lle;
   2462 	struct llentries *lleh;
   2463 	u_int hashidx;
   2464 
   2465 	hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize);
   2466 	lleh = &llt->lle_head[hashidx];
   2467 	LIST_FOREACH(lle, lleh, lle_next) {
   2468 		if (lle->la_flags & LLE_DELETED)
   2469 			continue;
   2470 		if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst))
   2471 			break;
   2472 	}
   2473 
   2474 	return lle;
   2475 }
   2476 
   2477 static int
   2478 in6_lltable_delete(struct lltable *llt, u_int flags,
   2479 	const struct sockaddr *l3addr)
   2480 {
   2481 	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
   2482 	struct llentry *lle;
   2483 
   2484 	IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
   2485 	KASSERTMSG(l3addr->sa_family == AF_INET6,
   2486 	    "sin_family %d", l3addr->sa_family);
   2487 
   2488 	lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
   2489 
   2490 	if (lle == NULL)
   2491 		return ENOENT;
   2492 
   2493 	LLE_WLOCK(lle);
   2494 	lle->la_flags |= LLE_DELETED;
   2495 #ifdef DIAGNOSTIC
   2496 	log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
   2497 #endif
   2498 	if ((lle->la_flags & (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
   2499 		llentry_free(lle);
   2500 	else
   2501 		LLE_WUNLOCK(lle);
   2502 
   2503 	return 0;
   2504 }
   2505 
   2506 static struct llentry *
   2507 in6_lltable_create(struct lltable *llt, u_int flags,
   2508 	const struct sockaddr *l3addr)
   2509 {
   2510 	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
   2511 	struct ifnet *ifp = llt->llt_ifp;
   2512 	struct llentry *lle;
   2513 
   2514 	IF_AFDATA_WLOCK_ASSERT(ifp);
   2515 	KASSERTMSG(l3addr->sa_family == AF_INET6,
   2516 	    "sin_family %d", l3addr->sa_family);
   2517 
   2518 	lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
   2519 
   2520 	if (lle != NULL) {
   2521 		LLE_WLOCK(lle);
   2522 		return lle;
   2523 	}
   2524 
   2525 	/*
   2526 	 * A route that covers the given address must have
   2527 	 * been installed 1st because we are doing a resolution,
   2528 	 * verify this.
   2529 	 */
   2530 	if (!(flags & LLE_IFADDR) &&
   2531 	    in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
   2532 		return NULL;
   2533 
   2534 	lle = in6_lltable_new(&sin6->sin6_addr, flags);
   2535 	if (lle == NULL) {
   2536 		log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
   2537 		return NULL;
   2538 	}
   2539 	lle->la_flags = flags;
   2540 	if ((flags & LLE_IFADDR) == LLE_IFADDR) {
   2541 		memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen);
   2542 		lle->la_flags |= LLE_VALID;
   2543 	}
   2544 
   2545 	lltable_link_entry(llt, lle);
   2546 	LLE_WLOCK(lle);
   2547 
   2548 	return lle;
   2549 }
   2550 
   2551 static struct llentry *
   2552 in6_lltable_lookup(struct lltable *llt, u_int flags,
   2553 	const struct sockaddr *l3addr)
   2554 {
   2555 	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
   2556 	struct llentry *lle;
   2557 
   2558 	IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
   2559 	KASSERTMSG(l3addr->sa_family == AF_INET6,
   2560 	    "sin_family %d", l3addr->sa_family);
   2561 
   2562 	lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
   2563 
   2564 	if (lle == NULL)
   2565 		return NULL;
   2566 
   2567 	if (flags & LLE_EXCLUSIVE)
   2568 		LLE_WLOCK(lle);
   2569 	else
   2570 		LLE_RLOCK(lle);
   2571 	return lle;
   2572 }
   2573 
   2574 static int
   2575 in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
   2576     struct rt_walkarg *w)
   2577 {
   2578 	struct sockaddr_in6 sin6;
   2579 
   2580 	LLTABLE_LOCK_ASSERT();
   2581 
   2582 	/* skip deleted entries */
   2583 	if (lle->la_flags & LLE_DELETED)
   2584 		return 0;
   2585 
   2586 	sockaddr_in6_init(&sin6, &lle->r_l3addr.addr6, 0, 0, 0);
   2587 
   2588 	return lltable_dump_entry(llt, lle, w, sin6tosa(&sin6));
   2589 }
   2590 
   2591 static struct lltable *
   2592 in6_lltattach(struct ifnet *ifp)
   2593 {
   2594 	struct lltable *llt;
   2595 
   2596 	llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE);
   2597 	llt->llt_af = AF_INET6;
   2598 	llt->llt_ifp = ifp;
   2599 
   2600 	llt->llt_lookup = in6_lltable_lookup;
   2601 	llt->llt_create = in6_lltable_create;
   2602 	llt->llt_delete = in6_lltable_delete;
   2603 	llt->llt_dump_entry = in6_lltable_dump_entry;
   2604 	llt->llt_hash = in6_lltable_hash;
   2605 	llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry;
   2606 	llt->llt_free_entry = in6_lltable_free_entry;
   2607 	llt->llt_match_prefix = in6_lltable_match_prefix;
   2608 	lltable_link(llt);
   2609 
   2610 	return llt;
   2611 }
   2612 
   2613 void *
   2614 in6_domifattach(struct ifnet *ifp)
   2615 {
   2616 	struct in6_ifextra *ext;
   2617 
   2618 	ext = malloc(sizeof(*ext), M_IFADDR, M_WAITOK|M_ZERO);
   2619 
   2620 	ext->in6_ifstat = malloc(sizeof(struct in6_ifstat),
   2621 	    M_IFADDR, M_WAITOK|M_ZERO);
   2622 
   2623 	ext->icmp6_ifstat = malloc(sizeof(struct icmp6_ifstat),
   2624 	    M_IFADDR, M_WAITOK|M_ZERO);
   2625 
   2626 	ext->nd_ifinfo = nd6_ifattach(ifp);
   2627 	ext->scope6_id = scope6_ifattach(ifp);
   2628 	ext->nprefixes = 0;
   2629 	ext->ndefrouters = 0;
   2630 
   2631 	ext->lltable = in6_lltattach(ifp);
   2632 
   2633 	return ext;
   2634 }
   2635 
   2636 void
   2637 in6_domifdetach(struct ifnet *ifp, void *aux)
   2638 {
   2639 	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
   2640 
   2641 	lltable_free(ext->lltable);
   2642 	ext->lltable = NULL;
   2643 	nd6_ifdetach(ifp, ext);
   2644 	free(ext->in6_ifstat, M_IFADDR);
   2645 	free(ext->icmp6_ifstat, M_IFADDR);
   2646 	scope6_ifdetach(ext->scope6_id);
   2647 	free(ext, M_IFADDR);
   2648 }
   2649 
   2650 /*
   2651  * Convert IPv4 address stored in struct in_addr to IPv4-Mapped IPv6 address
   2652  * stored in struct in6_addr as defined in RFC 4921 section 2.5.5.2.
   2653  */
   2654 void
   2655 in6_in_2_v4mapin6(const struct in_addr *in, struct in6_addr *in6)
   2656 {
   2657 	in6->s6_addr32[0] = 0;
   2658 	in6->s6_addr32[1] = 0;
   2659 	in6->s6_addr32[2] = IPV6_ADDR_INT32_SMP;
   2660 	in6->s6_addr32[3] = in->s_addr;
   2661 }
   2662 
   2663 /*
   2664  * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
   2665  * v4 mapped addr or v4 compat addr
   2666  */
   2667 void
   2668 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
   2669 {
   2670 	memset(sin, 0, sizeof(*sin));
   2671 	sin->sin_len = sizeof(struct sockaddr_in);
   2672 	sin->sin_family = AF_INET;
   2673 	sin->sin_port = sin6->sin6_port;
   2674 	sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
   2675 }
   2676 
   2677 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
   2678 void
   2679 in6_sin_2_v4mapsin6(const struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
   2680 {
   2681 	memset(sin6, 0, sizeof(*sin6));
   2682 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2683 	sin6->sin6_family = AF_INET6;
   2684 	sin6->sin6_port = sin->sin_port;
   2685 	in6_in_2_v4mapin6(&sin->sin_addr, &sin6->sin6_addr);
   2686 }
   2687 
   2688 /* Convert sockaddr_in6 into sockaddr_in. */
   2689 void
   2690 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
   2691 {
   2692 	struct sockaddr_in *sin_p;
   2693 	struct sockaddr_in6 sin6;
   2694 
   2695 	/*
   2696 	 * Save original sockaddr_in6 addr and convert it
   2697 	 * to sockaddr_in.
   2698 	 */
   2699 	sin6 = *(struct sockaddr_in6 *)nam;
   2700 	sin_p = (struct sockaddr_in *)nam;
   2701 	in6_sin6_2_sin(sin_p, &sin6);
   2702 }
   2703 
   2704 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
   2705 void
   2706 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
   2707 {
   2708 	struct sockaddr_in *sin_p;
   2709 	struct sockaddr_in6 *sin6_p;
   2710 
   2711 	sin6_p = malloc(sizeof(*sin6_p), M_SONAME, M_WAITOK);
   2712 	sin_p = (struct sockaddr_in *)*nam;
   2713 	in6_sin_2_v4mapsin6(sin_p, sin6_p);
   2714 	free(*nam, M_SONAME);
   2715 	*nam = sin6tosa(sin6_p);
   2716 }
   2717