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