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