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