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