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