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