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