Home | History | Annotate | Line # | Download | only in netinet6
in6.c revision 1.67
      1 /*	$NetBSD: in6.c,v 1.67 2002/06/11 07:28:05 itojun 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. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *	This product includes software developed by the University of
     48  *	California, Berkeley and its contributors.
     49  * 4. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)in.c	8.2 (Berkeley) 11/15/93
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: in6.c,v 1.67 2002/06/11 07:28:05 itojun Exp $");
     70 
     71 #include "opt_inet.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/ioctl.h>
     75 #include <sys/errno.h>
     76 #include <sys/malloc.h>
     77 #include <sys/socket.h>
     78 #include <sys/socketvar.h>
     79 #include <sys/sockio.h>
     80 #include <sys/systm.h>
     81 #include <sys/proc.h>
     82 #include <sys/time.h>
     83 #include <sys/kernel.h>
     84 #include <sys/syslog.h>
     85 
     86 #include <net/if.h>
     87 #include <net/if_types.h>
     88 #include <net/route.h>
     89 #include <net/if_dl.h>
     90 
     91 #include <netinet/in.h>
     92 #include <netinet/in_var.h>
     93 #include <net/if_ether.h>
     94 
     95 #include <netinet/ip6.h>
     96 #include <netinet6/ip6_var.h>
     97 #include <netinet6/nd6.h>
     98 #include <netinet6/mld6_var.h>
     99 #include <netinet6/ip6_mroute.h>
    100 #include <netinet6/in6_ifattach.h>
    101 
    102 #include <net/net_osdep.h>
    103 
    104 /* enable backward compatibility code for obsoleted ioctls */
    105 #define COMPAT_IN6IFIOCTL
    106 
    107 /*
    108  * Definitions of some costant IP6 addresses.
    109  */
    110 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
    111 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
    112 const struct in6_addr in6addr_nodelocal_allnodes =
    113 	IN6ADDR_NODELOCAL_ALLNODES_INIT;
    114 const struct in6_addr in6addr_linklocal_allnodes =
    115 	IN6ADDR_LINKLOCAL_ALLNODES_INIT;
    116 const struct in6_addr in6addr_linklocal_allrouters =
    117 	IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
    118 
    119 const struct in6_addr in6mask0 = IN6MASK0;
    120 const struct in6_addr in6mask32 = IN6MASK32;
    121 const struct in6_addr in6mask64 = IN6MASK64;
    122 const struct in6_addr in6mask96 = IN6MASK96;
    123 const struct in6_addr in6mask128 = IN6MASK128;
    124 
    125 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
    126 				     0, 0, IN6ADDR_ANY_INIT, 0};
    127 
    128 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
    129 	struct ifnet *, struct proc *));
    130 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
    131 	struct sockaddr_in6 *, int));
    132 static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
    133 
    134 /*
    135  * This structure is used to keep track of in6_multi chains which belong to
    136  * deleted interface addresses.
    137  */
    138 static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
    139 
    140 struct multi6_kludge {
    141 	LIST_ENTRY(multi6_kludge) mk_entry;
    142 	struct ifnet *mk_ifp;
    143 	struct in6_multihead mk_head;
    144 };
    145 
    146 /*
    147  * Subroutine for in6_ifaddloop() and in6_ifremloop().
    148  * This routine does actual work.
    149  */
    150 static void
    151 in6_ifloop_request(int cmd, struct ifaddr *ifa)
    152 {
    153 	struct sockaddr_in6 lo_sa;
    154 	struct sockaddr_in6 all1_sa;
    155 	struct rtentry *nrt = NULL;
    156 	int e;
    157 
    158 	bzero(&lo_sa, sizeof(lo_sa));
    159 	bzero(&all1_sa, sizeof(all1_sa));
    160 	lo_sa.sin6_family = all1_sa.sin6_family = AF_INET6;
    161 	lo_sa.sin6_len = all1_sa.sin6_len = sizeof(struct sockaddr_in6);
    162 	lo_sa.sin6_addr = in6addr_loopback;
    163 	all1_sa.sin6_addr = in6mask128;
    164 
    165 	/*
    166 	 * We specify the address itself as the gateway, and set the
    167 	 * RTF_LLINFO flag, so that the corresponding host route would have
    168 	 * the flag, and thus applications that assume traditional behavior
    169 	 * would be happy.  Note that we assume the caller of the function
    170 	 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
    171 	 * which changes the outgoing interface to the loopback interface.
    172 	 */
    173 	e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
    174 	    (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
    175 	if (e != 0) {
    176 		log(LOG_ERR, "in6_ifloop_request: "
    177 		    "%s operation failed for %s (errno=%d)\n",
    178 		    cmd == RTM_ADD ? "ADD" : "DELETE",
    179 		    ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
    180 		    e);
    181 	}
    182 
    183 	/*
    184 	 * Make sure rt_ifa be equal to IFA, the second argument of the
    185 	 * function.
    186 	 * We need this because when we refer to rt_ifa->ia6_flags in
    187 	 * ip6_input, we assume that the rt_ifa points to the address instead
    188 	 * of the loopback address.
    189 	 */
    190 	if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
    191 		IFAFREE(nrt->rt_ifa);
    192 		IFAREF(ifa);
    193 		nrt->rt_ifa = ifa;
    194 	}
    195 
    196 	/*
    197 	 * Report the addition/removal of the address to the routing socket.
    198 	 * XXX: since we called rtinit for a p2p interface with a destination,
    199 	 *      we end up reporting twice in such a case.  Should we rather
    200 	 *      omit the second report?
    201 	 */
    202 	if (nrt) {
    203 		rt_newaddrmsg(cmd, ifa, e, nrt);
    204 		if (cmd == RTM_DELETE) {
    205 			if (nrt->rt_refcnt <= 0) {
    206 				/* XXX: we should free the entry ourselves. */
    207 				nrt->rt_refcnt++;
    208 				rtfree(nrt);
    209 			}
    210 		} else {
    211 			/* the cmd must be RTM_ADD here */
    212 			nrt->rt_refcnt--;
    213 		}
    214 	}
    215 }
    216 
    217 /*
    218  * Add ownaddr as loopback rtentry.  We previously add the route only if
    219  * necessary (ex. on a p2p link).  However, since we now manage addresses
    220  * separately from prefixes, we should always add the route.  We can't
    221  * rely on the cloning mechanism from the corresponding interface route
    222  * any more.
    223  */
    224 static void
    225 in6_ifaddloop(struct ifaddr *ifa)
    226 {
    227 	struct rtentry *rt;
    228 
    229 	/* If there is no loopback entry, allocate one. */
    230 	rt = rtalloc1(ifa->ifa_addr, 0);
    231 	if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
    232 	    (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
    233 		in6_ifloop_request(RTM_ADD, ifa);
    234 	if (rt)
    235 		rt->rt_refcnt--;
    236 }
    237 
    238 /*
    239  * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
    240  * if it exists.
    241  */
    242 static void
    243 in6_ifremloop(struct ifaddr *ifa)
    244 {
    245 	struct in6_ifaddr *ia;
    246 	struct rtentry *rt;
    247 	int ia_count = 0;
    248 
    249 	/*
    250 	 * Some of BSD variants do not remove cloned routes
    251 	 * from an interface direct route, when removing the direct route
    252 	 * (see comments in net/net_osdep.h).  Even for variants that do remove
    253 	 * cloned routes, they could fail to remove the cloned routes when
    254 	 * we handle multple addresses that share a common prefix.
    255 	 * So, we should remove the route corresponding to the deleted address.
    256 	 */
    257 
    258 	/*
    259 	 * Delete the entry only if exact one ifa exists.  More than one ifa
    260 	 * can exist if we assign a same single address to multiple
    261 	 * (probably p2p) interfaces.
    262 	 * XXX: we should avoid such a configuration in IPv6...
    263 	 */
    264 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
    265 		if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
    266 			ia_count++;
    267 			if (ia_count > 1)
    268 				break;
    269 		}
    270 	}
    271 
    272 	if (ia_count == 1) {
    273 		/*
    274 		 * Before deleting, check if a corresponding loopbacked host
    275 		 * route surely exists.  With this check, we can avoid to
    276 		 * delete an interface direct route whose destination is same
    277 		 * as the address being removed.  This can happen when removing
    278 		 * a subnet-router anycast address on an interface attahced
    279 		 * to a shared medium.
    280 		 */
    281 		rt = rtalloc1(ifa->ifa_addr, 0);
    282 		if (rt != NULL && (rt->rt_flags & RTF_HOST) != 0 &&
    283 		    (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
    284 			rt->rt_refcnt--;
    285 			in6_ifloop_request(RTM_DELETE, ifa);
    286 		}
    287 	}
    288 }
    289 
    290 int
    291 in6_ifindex2scopeid(idx)
    292 	int idx;
    293 {
    294 	struct ifnet *ifp;
    295 	struct ifaddr *ifa;
    296 	struct sockaddr_in6 *sin6;
    297 
    298 	if (idx < 0 || if_index < idx)
    299 		return -1;
    300 	ifp = ifindex2ifnet[idx];
    301 	if (!ifp)
    302 		return -1;
    303 
    304 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
    305 	{
    306 		if (ifa->ifa_addr->sa_family != AF_INET6)
    307 			continue;
    308 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
    309 		if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
    310 			return sin6->sin6_scope_id & 0xffff;
    311 	}
    312 
    313 	return -1;
    314 }
    315 
    316 int
    317 in6_mask2len(mask, lim0)
    318 	struct in6_addr *mask;
    319 	u_char *lim0;
    320 {
    321 	int x = 0, y;
    322 	u_char *lim = lim0, *p;
    323 
    324 	/* ignore the scope_id part */
    325 	if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
    326 		lim = (u_char *)mask + sizeof(*mask);
    327 	for (p = (u_char *)mask; p < lim; x++, p++) {
    328 		if (*p != 0xff)
    329 			break;
    330 	}
    331 	y = 0;
    332 	if (p < lim) {
    333 		for (y = 0; y < 8; y++) {
    334 			if ((*p & (0x80 >> y)) == 0)
    335 				break;
    336 		}
    337 	}
    338 
    339 	/*
    340 	 * when the limit pointer is given, do a stricter check on the
    341 	 * remaining bits.
    342 	 */
    343 	if (p < lim) {
    344 		if (y != 0 && (*p & (0x00ff >> y)) != 0)
    345 			return(-1);
    346 		for (p = p + 1; p < lim; p++)
    347 			if (*p != 0)
    348 				return(-1);
    349 	}
    350 
    351 	return x * 8 + y;
    352 }
    353 
    354 #define ifa2ia6(ifa)	((struct in6_ifaddr *)(ifa))
    355 #define ia62ifa(ia6)	(&((ia6)->ia_ifa))
    356 
    357 int
    358 in6_control(so, cmd, data, ifp, p)
    359 	struct	socket *so;
    360 	u_long cmd;
    361 	caddr_t	data;
    362 	struct ifnet *ifp;
    363 	struct proc *p;
    364 {
    365 	struct	in6_ifreq *ifr = (struct in6_ifreq *)data;
    366 	struct	in6_ifaddr *ia = NULL;
    367 	struct	in6_aliasreq *ifra = (struct in6_aliasreq *)data;
    368 	struct sockaddr_in6 *sa6;
    369 	time_t time_second = (time_t)time.tv_sec;
    370 	int privileged;
    371 
    372 	privileged = 0;
    373 	if (p && !suser(p->p_ucred, &p->p_acflag))
    374 		privileged++;
    375 
    376 	switch (cmd) {
    377 	case SIOCGETSGCNT_IN6:
    378 	case SIOCGETMIFCNT_IN6:
    379 		return (mrt6_ioctl(cmd, data));
    380 	}
    381 
    382 	if (ifp == NULL)
    383 		return(EOPNOTSUPP);
    384 
    385 	switch (cmd) {
    386 	case SIOCSNDFLUSH_IN6:
    387 	case SIOCSPFXFLUSH_IN6:
    388 	case SIOCSRTRFLUSH_IN6:
    389 	case SIOCSDEFIFACE_IN6:
    390 	case SIOCSIFINFO_FLAGS:
    391 		if (!privileged)
    392 			return(EPERM);
    393 		/* FALLTHROUGH */
    394 	case OSIOCGIFINFO_IN6:
    395 	case SIOCGIFINFO_IN6:
    396 	case SIOCGDRLST_IN6:
    397 	case SIOCGPRLST_IN6:
    398 	case SIOCGNBRINFO_IN6:
    399 	case SIOCGDEFIFACE_IN6:
    400 		return(nd6_ioctl(cmd, data, ifp));
    401 	}
    402 
    403 	switch (cmd) {
    404 	case SIOCSIFPREFIX_IN6:
    405 	case SIOCDIFPREFIX_IN6:
    406 	case SIOCAIFPREFIX_IN6:
    407 	case SIOCCIFPREFIX_IN6:
    408 	case SIOCSGIFPREFIX_IN6:
    409 	case SIOCGIFPREFIX_IN6:
    410 		log(LOG_NOTICE,
    411 		    "prefix ioctls are now invalidated. "
    412 		    "please use ifconfig.\n");
    413 		return(EOPNOTSUPP);
    414 	}
    415 
    416 	switch (cmd) {
    417 	case SIOCALIFADDR:
    418 	case SIOCDLIFADDR:
    419 		if (!privileged)
    420 			return(EPERM);
    421 		/* FALLTHROUGH */
    422 	case SIOCGLIFADDR:
    423 		return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
    424 	}
    425 
    426 	/*
    427 	 * Find address for this interface, if it exists.
    428 	 *
    429 	 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
    430 	 * only, and used the first interface address as the target of other
    431 	 * operations (without checking ifra_addr).  This was because netinet
    432 	 * code/API assumed at most 1 interface address per interface.
    433 	 * Since IPv6 allows a node to assign multiple addresses
    434 	 * on a single interface, we almost always look and check the
    435 	 * presence of ifra_addr, and reject invalid ones here.
    436 	 * It also decreases duplicated code among SIOC*_IN6 operations.
    437 	 */
    438 	switch (cmd) {
    439 	case SIOCAIFADDR_IN6:
    440 	case SIOCSIFPHYADDR_IN6:
    441 		sa6 = &ifra->ifra_addr;
    442 		break;
    443 	case SIOCSIFADDR_IN6:
    444 	case SIOCGIFADDR_IN6:
    445 	case SIOCSIFDSTADDR_IN6:
    446 	case SIOCSIFNETMASK_IN6:
    447 	case SIOCGIFDSTADDR_IN6:
    448 	case SIOCGIFNETMASK_IN6:
    449 	case SIOCDIFADDR_IN6:
    450 	case SIOCGIFPSRCADDR_IN6:
    451 	case SIOCGIFPDSTADDR_IN6:
    452 	case SIOCGIFAFLAG_IN6:
    453 	case SIOCSNDFLUSH_IN6:
    454 	case SIOCSPFXFLUSH_IN6:
    455 	case SIOCSRTRFLUSH_IN6:
    456 	case SIOCGIFALIFETIME_IN6:
    457 	case SIOCSIFALIFETIME_IN6:
    458 	case SIOCGIFSTAT_IN6:
    459 	case SIOCGIFSTAT_ICMP6:
    460 		sa6 = &ifr->ifr_addr;
    461 		break;
    462 	default:
    463 		sa6 = NULL;
    464 		break;
    465 	}
    466 	if (sa6 && sa6->sin6_family == AF_INET6) {
    467 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
    468 			if (sa6->sin6_addr.s6_addr16[1] == 0) {
    469 				/* link ID is not embedded by the user */
    470 				sa6->sin6_addr.s6_addr16[1] =
    471 				    htons(ifp->if_index);
    472 			} else if (sa6->sin6_addr.s6_addr16[1] !=
    473 			    htons(ifp->if_index)) {
    474 				return(EINVAL);	/* link ID contradicts */
    475 			}
    476 			if (sa6->sin6_scope_id) {
    477 				if (sa6->sin6_scope_id !=
    478 				    (u_int32_t)ifp->if_index)
    479 					return(EINVAL);
    480 				sa6->sin6_scope_id = 0; /* XXX: good way? */
    481 			}
    482 		}
    483 		ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
    484 	} else
    485 		ia = NULL;
    486 
    487 	switch (cmd) {
    488 	case SIOCSIFADDR_IN6:
    489 	case SIOCSIFDSTADDR_IN6:
    490 	case SIOCSIFNETMASK_IN6:
    491 		/*
    492 		 * Since IPv6 allows a node to assign multiple addresses
    493 		 * on a single interface, SIOCSIFxxx ioctls are deprecated.
    494 		 */
    495 		return(EINVAL);
    496 
    497 	case SIOCDIFADDR_IN6:
    498 		/*
    499 		 * for IPv4, we look for existing in_ifaddr here to allow
    500 		 * "ifconfig if0 delete" to remove the first IPv4 address on
    501 		 * the interface.  For IPv6, as the spec allows multiple
    502 		 * interface address from the day one, we consider "remove the
    503 		 * first one" semantics to be not preferable.
    504 		 */
    505 		if (ia == NULL)
    506 			return(EADDRNOTAVAIL);
    507 		/* FALLTHROUGH */
    508 	case SIOCAIFADDR_IN6:
    509 		/*
    510 		 * We always require users to specify a valid IPv6 address for
    511 		 * the corresponding operation.
    512 		 */
    513 		if (ifra->ifra_addr.sin6_family != AF_INET6 ||
    514 		    ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
    515 			return(EAFNOSUPPORT);
    516 		if (!privileged)
    517 			return(EPERM);
    518 
    519 		break;
    520 
    521 	case SIOCGIFADDR_IN6:
    522 		/* This interface is basically deprecated. use SIOCGIFCONF. */
    523 		/* FALLTHROUGH */
    524 	case SIOCGIFAFLAG_IN6:
    525 	case SIOCGIFNETMASK_IN6:
    526 	case SIOCGIFDSTADDR_IN6:
    527 	case SIOCGIFALIFETIME_IN6:
    528 		/* must think again about its semantics */
    529 		if (ia == NULL)
    530 			return(EADDRNOTAVAIL);
    531 		break;
    532 	case SIOCSIFALIFETIME_IN6:
    533 	    {
    534 		struct in6_addrlifetime *lt;
    535 
    536 		if (!privileged)
    537 			return(EPERM);
    538 		if (ia == NULL)
    539 			return(EADDRNOTAVAIL);
    540 		/* sanity for overflow - beware unsigned */
    541 		lt = &ifr->ifr_ifru.ifru_lifetime;
    542 		if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
    543 		 && lt->ia6t_vltime + time_second < time_second) {
    544 			return EINVAL;
    545 		}
    546 		if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
    547 		 && lt->ia6t_pltime + time_second < time_second) {
    548 			return EINVAL;
    549 		}
    550 		break;
    551 	    }
    552 	}
    553 
    554 	switch (cmd) {
    555 
    556 	case SIOCGIFADDR_IN6:
    557 		ifr->ifr_addr = ia->ia_addr;
    558 		break;
    559 
    560 	case SIOCGIFDSTADDR_IN6:
    561 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    562 			return(EINVAL);
    563 		/*
    564 		 * XXX: should we check if ifa_dstaddr is NULL and return
    565 		 * an error?
    566 		 */
    567 		ifr->ifr_dstaddr = ia->ia_dstaddr;
    568 		break;
    569 
    570 	case SIOCGIFNETMASK_IN6:
    571 		ifr->ifr_addr = ia->ia_prefixmask;
    572 		break;
    573 
    574 	case SIOCGIFAFLAG_IN6:
    575 		ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
    576 		break;
    577 
    578 	case SIOCGIFSTAT_IN6:
    579 		if (ifp == NULL)
    580 			return EINVAL;
    581 		bzero(&ifr->ifr_ifru.ifru_stat,
    582 		    sizeof(ifr->ifr_ifru.ifru_stat));
    583 		ifr->ifr_ifru.ifru_stat =
    584 		    *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
    585 		break;
    586 
    587 	case SIOCGIFSTAT_ICMP6:
    588 		if (ifp == NULL)
    589 			return EINVAL;
    590 		bzero(&ifr->ifr_ifru.ifru_stat,
    591 		    sizeof(ifr->ifr_ifru.ifru_icmp6stat));
    592 		ifr->ifr_ifru.ifru_icmp6stat =
    593 		    *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
    594 		break;
    595 
    596 	case SIOCGIFALIFETIME_IN6:
    597 		ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
    598 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
    599 			time_t maxexpire;
    600 			struct in6_addrlifetime *retlt =
    601 			    &ifr->ifr_ifru.ifru_lifetime;
    602 
    603 			/*
    604 			 * XXX: adjust expiration time assuming time_t is
    605 			 * signed.
    606 			 */
    607 			maxexpire = (-1) &
    608 			    ~(1 << ((sizeof(maxexpire) * 8) - 1));
    609 			if (ia->ia6_lifetime.ia6t_vltime <
    610 			    maxexpire - ia->ia6_updatetime) {
    611 				retlt->ia6t_expire = ia->ia6_updatetime +
    612 				    ia->ia6_lifetime.ia6t_vltime;
    613 			} else
    614 				retlt->ia6t_expire = maxexpire;
    615 		}
    616 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
    617 			time_t maxexpire;
    618 			struct in6_addrlifetime *retlt =
    619 			    &ifr->ifr_ifru.ifru_lifetime;
    620 
    621 			/*
    622 			 * XXX: adjust expiration time assuming time_t is
    623 			 * signed.
    624 			 */
    625 			maxexpire = (-1) &
    626 			    ~(1 << ((sizeof(maxexpire) * 8) - 1));
    627 			if (ia->ia6_lifetime.ia6t_pltime <
    628 			    maxexpire - ia->ia6_updatetime) {
    629 				retlt->ia6t_preferred = ia->ia6_updatetime +
    630 				    ia->ia6_lifetime.ia6t_pltime;
    631 			} else
    632 				retlt->ia6t_preferred = maxexpire;
    633 		}
    634 		break;
    635 
    636 	case SIOCSIFALIFETIME_IN6:
    637 		ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
    638 		/* for sanity */
    639 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
    640 			ia->ia6_lifetime.ia6t_expire =
    641 				time_second + ia->ia6_lifetime.ia6t_vltime;
    642 		} else
    643 			ia->ia6_lifetime.ia6t_expire = 0;
    644 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
    645 			ia->ia6_lifetime.ia6t_preferred =
    646 				time_second + ia->ia6_lifetime.ia6t_pltime;
    647 		} else
    648 			ia->ia6_lifetime.ia6t_preferred = 0;
    649 		break;
    650 
    651 	case SIOCAIFADDR_IN6:
    652 	{
    653 		int i, error = 0;
    654 		struct nd_prefix pr0, *pr;
    655 
    656 		/*
    657 		 * first, make or update the interface address structure,
    658 		 * and link it to the list.
    659 		 */
    660 		if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
    661 			return(error);
    662 		if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
    663 		    == NULL) {
    664 		    	/*
    665 			 * this can happen when the user specify the 0 valid
    666 			 * lifetime.
    667 			 */
    668 			break;
    669 		}
    670 
    671 		/*
    672 		 * then, make the prefix on-link on the interface.
    673 		 * XXX: we'd rather create the prefix before the address, but
    674 		 * we need at least one address to install the corresponding
    675 		 * interface route, so we configure the address first.
    676 		 */
    677 
    678 		/*
    679 		 * convert mask to prefix length (prefixmask has already
    680 		 * been validated in in6_update_ifa().
    681 		 */
    682 		bzero(&pr0, sizeof(pr0));
    683 		pr0.ndpr_ifp = ifp;
    684 		pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
    685 		    NULL);
    686 		if (pr0.ndpr_plen == 128) {
    687 			break;	/* we don't need to install a host route. */
    688 		}
    689 		pr0.ndpr_prefix = ifra->ifra_addr;
    690 		pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
    691 		/* apply the mask for safety. */
    692 		for (i = 0; i < 4; i++) {
    693 			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
    694 			    ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
    695 		}
    696 		/*
    697 		 * XXX: since we don't have an API to set prefix (not address)
    698 		 * lifetimes, we just use the same lifetimes as addresses.
    699 		 * The (temporarily) installed lifetimes can be overridden by
    700 		 * later advertised RAs (when accept_rtadv is non 0), which is
    701 		 * an intended behavior.
    702 		 */
    703 		pr0.ndpr_raf_onlink = 1; /* should be configurable? */
    704 		pr0.ndpr_raf_auto =
    705 		    ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
    706 		pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
    707 		pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
    708 
    709 		/* add the prefix if there's one. */
    710 		if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
    711 			/*
    712 			 * nd6_prelist_add will install the corresponding
    713 			 * interface route.
    714 			 */
    715 			if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
    716 				return(error);
    717 			if (pr == NULL) {
    718 				log(LOG_ERR, "nd6_prelist_add succeeded but "
    719 				    "no prefix\n");
    720 				return(EINVAL); /* XXX panic here? */
    721 			}
    722 		}
    723 		if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
    724 		    ia->ia6_ndpr == NULL) { /* new autoconfed addr */
    725 			ia->ia6_ndpr = pr;
    726 			pr->ndpr_refcnt++;
    727 		}
    728 
    729 		/*
    730 		 * this might affect the status of autoconfigured addresses,
    731 		 * that is, this address might make other addresses detached.
    732 		 */
    733 		pfxlist_onlink_check();
    734 
    735 		break;
    736 	}
    737 
    738 	case SIOCDIFADDR_IN6:
    739 	{
    740 		int i = 0, purgeprefix = 0;
    741 		struct nd_prefix pr0, *pr = NULL;
    742 
    743 		/*
    744 		 * If the address being deleted is the only one that owns
    745 		 * the corresponding prefix, expire the prefix as well.
    746 		 * XXX: theoretically, we don't have to worry about such
    747 		 * relationship, since we separate the address management
    748 		 * and the prefix management.  We do this, however, to provide
    749 		 * as much backward compatibility as possible in terms of
    750 		 * the ioctl operation.
    751 		 */
    752 		bzero(&pr0, sizeof(pr0));
    753 		pr0.ndpr_ifp = ifp;
    754 		pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
    755 		    NULL);
    756 		if (pr0.ndpr_plen == 128)
    757 			goto purgeaddr;
    758 		pr0.ndpr_prefix = ia->ia_addr;
    759 		pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
    760 		for (i = 0; i < 4; i++) {
    761 			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
    762 			    ia->ia_prefixmask.sin6_addr.s6_addr32[i];
    763 		}
    764 		/*
    765 		 * The logic of the following condition is a bit complicated.
    766 		 * We expire the prefix when
    767 		 * 1. the address obeys autoconfiguration and it is the
    768 		 *    only owner of the associated prefix, or
    769 		 * 2. the address does not obey autoconf and there is no
    770 		 *    other owner of the prefix.
    771 		 */
    772 		if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
    773 		    (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
    774 		      pr->ndpr_refcnt == 1) ||
    775 		     ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
    776 		      pr->ndpr_refcnt == 0)))
    777 			purgeprefix = 1;
    778 
    779 	  purgeaddr:
    780 		in6_purgeaddr(&ia->ia_ifa);
    781 		if (pr && purgeprefix)
    782 			prelist_remove(pr);
    783 		break;
    784 	}
    785 
    786 	default:
    787 		if (ifp == NULL || ifp->if_ioctl == 0)
    788 			return(EOPNOTSUPP);
    789 		return((*ifp->if_ioctl)(ifp, cmd, data));
    790 	}
    791 
    792 	return(0);
    793 }
    794 
    795 /*
    796  * Update parameters of an IPv6 interface address.
    797  * If necessary, a new entry is created and linked into address chains.
    798  * This function is separated from in6_control().
    799  * XXX: should this be performed under splnet()?
    800  */
    801 int
    802 in6_update_ifa(ifp, ifra, ia)
    803 	struct ifnet *ifp;
    804 	struct in6_aliasreq *ifra;
    805 	struct in6_ifaddr *ia;
    806 {
    807 	int error = 0, hostIsNew = 0, plen = -1;
    808 	struct in6_ifaddr *oia;
    809 	struct sockaddr_in6 dst6;
    810 	struct in6_addrlifetime *lt;
    811 	struct in6_multi_mship *imm;
    812 	time_t time_second = (time_t)time.tv_sec;
    813 	struct rtentry *rt;
    814 
    815 	/* Validate parameters */
    816 	if (ifp == NULL || ifra == NULL) /* this maybe redundant */
    817 		return(EINVAL);
    818 
    819 	/*
    820 	 * The destination address for a p2p link must have a family
    821 	 * of AF_UNSPEC or AF_INET6.
    822 	 */
    823 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
    824 	    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
    825 	    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
    826 		return(EAFNOSUPPORT);
    827 	/*
    828 	 * validate ifra_prefixmask.  don't check sin6_family, netmask
    829 	 * does not carry fields other than sin6_len.
    830 	 */
    831 	if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
    832 		return(EINVAL);
    833 	/*
    834 	 * Because the IPv6 address architecture is classless, we require
    835 	 * users to specify a (non 0) prefix length (mask) for a new address.
    836 	 * We also require the prefix (when specified) mask is valid, and thus
    837 	 * reject a non-consecutive mask.
    838 	 */
    839 	if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
    840 		return(EINVAL);
    841 	if (ifra->ifra_prefixmask.sin6_len != 0) {
    842 		plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
    843 		    (u_char *)&ifra->ifra_prefixmask +
    844 		    ifra->ifra_prefixmask.sin6_len);
    845 		if (plen <= 0)
    846 			return(EINVAL);
    847 	} else {
    848 		/*
    849 		 * In this case, ia must not be NULL.  We just use its prefix
    850 		 * length.
    851 		 */
    852 		plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
    853 	}
    854 	/*
    855 	 * If the destination address on a p2p interface is specified,
    856 	 * and the address is a scoped one, validate/set the scope
    857 	 * zone identifier.
    858 	 */
    859 	dst6 = ifra->ifra_dstaddr;
    860 	if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
    861 	    (dst6.sin6_family == AF_INET6)) {
    862 		/* link-local index check: should be a separate function? */
    863 		if (IN6_IS_ADDR_LINKLOCAL(&dst6.sin6_addr)) {
    864 			if (dst6.sin6_addr.s6_addr16[1] == 0) {
    865 				/*
    866 				 * interface ID is not embedded by
    867 				 * the user
    868 				 */
    869 				dst6.sin6_addr.s6_addr16[1] =
    870 				    htons(ifp->if_index);
    871 			} else if (dst6.sin6_addr.s6_addr16[1] !=
    872 			    htons(ifp->if_index)) {
    873 				return(EINVAL);	/* ifid contradicts */
    874 			}
    875 		}
    876 	}
    877 	/*
    878 	 * The destination address can be specified only for a p2p or a
    879 	 * loopback interface.  If specified, the corresponding prefix length
    880 	 * must be 128.
    881 	 */
    882 	if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
    883 #ifdef FORCE_P2PPLEN
    884 		int i;
    885 #endif
    886 
    887 		if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
    888 			/* XXX: noisy message */
    889 			nd6log((LOG_INFO, "in6_update_ifa: a destination can be "
    890 			    "specified for a p2p or a loopback IF only\n"));
    891 			return(EINVAL);
    892 		}
    893 		if (plen != 128) {
    894 			nd6log((LOG_INFO, "in6_update_ifa: prefixlen should be "
    895 			    "128 when dstaddr is specified\n"));
    896 #ifdef FORCE_P2PPLEN
    897 			/*
    898 			 * To be compatible with old configurations,
    899 			 * such as ifconfig gif0 inet6 2001::1 2001::2
    900 			 * prefixlen 126, we override the specified
    901 			 * prefixmask as if the prefix length was 128.
    902 			 */
    903 			ifra->ifra_prefixmask.sin6_len =
    904 			    sizeof(struct sockaddr_in6);
    905 			for (i = 0; i < 4; i++)
    906 				ifra->ifra_prefixmask.sin6_addr.s6_addr32[i] =
    907 				    0xffffffff;
    908 			plen = 128;
    909 #else
    910 			return(EINVAL);
    911 #endif
    912 		}
    913 	}
    914 	/* lifetime consistency check */
    915 	lt = &ifra->ifra_lifetime;
    916 	if (lt->ia6t_pltime > lt->ia6t_vltime)
    917 		return(EINVAL);
    918 	if (lt->ia6t_vltime == 0) {
    919 		/*
    920 		 * the following log might be noisy, but this is a typical
    921 		 * configuration mistake or a tool's bug.
    922 		 */
    923 		nd6log((LOG_INFO,
    924 		    "in6_update_ifa: valid lifetime is 0 for %s\n",
    925 		    ip6_sprintf(&ifra->ifra_addr.sin6_addr)));
    926 
    927 		if (ia == NULL)
    928 			return(0); /* there's nothing to do */
    929 	}
    930 
    931 	/*
    932 	 * If this is a new address, allocate a new ifaddr and link it
    933 	 * into chains.
    934 	 */
    935 	if (ia == NULL) {
    936 		hostIsNew = 1;
    937 		/*
    938 		 * When in6_update_ifa() is called in a process of a received
    939 		 * RA, it is called under an interrupt context.  So, we should
    940 		 * call malloc with M_NOWAIT.
    941 		 */
    942 		ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
    943 		    M_NOWAIT);
    944 		if (ia == NULL)
    945 			return (ENOBUFS);
    946 		bzero((caddr_t)ia, sizeof(*ia));
    947 		LIST_INIT(&ia->ia6_memberships);
    948 		/* Initialize the address and masks, and put time stamp */
    949 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
    950 		ia->ia_addr.sin6_family = AF_INET6;
    951 		ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
    952 		ia->ia6_createtime = ia->ia6_updatetime = time_second;
    953 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
    954 			/*
    955 			 * XXX: some functions expect that ifa_dstaddr is not
    956 			 * NULL for p2p interfaces.
    957 			 */
    958 			ia->ia_ifa.ifa_dstaddr =
    959 			    (struct sockaddr *)&ia->ia_dstaddr;
    960 		} else {
    961 			ia->ia_ifa.ifa_dstaddr = NULL;
    962 		}
    963 		ia->ia_ifa.ifa_netmask =
    964 		    (struct sockaddr *)&ia->ia_prefixmask;
    965 
    966 		ia->ia_ifp = ifp;
    967 		if ((oia = in6_ifaddr) != NULL) {
    968 			for ( ; oia->ia_next; oia = oia->ia_next)
    969 				continue;
    970 			oia->ia_next = ia;
    971 		} else
    972 			in6_ifaddr = ia;
    973 		/* gain a refcnt for the link from in6_ifaddr */
    974 		IFAREF(&ia->ia_ifa);
    975 
    976 		TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
    977 				  ifa_list);
    978 		/* gain another refcnt for the link from if_addrlist */
    979 		IFAREF(&ia->ia_ifa);
    980 	}
    981 
    982 	/* set prefix mask */
    983 	if (ifra->ifra_prefixmask.sin6_len) {
    984 		/*
    985 		 * We prohibit changing the prefix length of an existing
    986 		 * address, because
    987 		 * + such an operation should be rare in IPv6, and
    988 		 * + the operation would confuse prefix management.
    989 		 */
    990 		if (ia->ia_prefixmask.sin6_len &&
    991 		    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
    992 			nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
    993 			    " existing (%s) address should not be changed\n",
    994 			    ip6_sprintf(&ia->ia_addr.sin6_addr)));
    995 			error = EINVAL;
    996 			goto unlink;
    997 		}
    998 		ia->ia_prefixmask = ifra->ifra_prefixmask;
    999 	}
   1000 
   1001 	/*
   1002 	 * If a new destination address is specified, scrub the old one and
   1003 	 * install the new destination.  Note that the interface must be
   1004 	 * p2p or loopback (see the check above.)
   1005 	 */
   1006 	if (dst6.sin6_family == AF_INET6 &&
   1007 	    !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
   1008 		int e;
   1009 
   1010 		if ((ia->ia_flags & IFA_ROUTE) != 0 &&
   1011 		    (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
   1012 			nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
   1013 			    "a route to the old destination: %s\n",
   1014 			    ip6_sprintf(&ia->ia_addr.sin6_addr)));
   1015 			/* proceed anyway... */
   1016 		} else
   1017 			ia->ia_flags &= ~IFA_ROUTE;
   1018 		ia->ia_dstaddr = dst6;
   1019 	}
   1020 
   1021 	/*
   1022 	 * Set lifetimes.  We do not refer to ia6t_expire and ia6t_preferred
   1023 	 * to see if the address is deprecated or invalidated, but initialize
   1024 	 * these members for applications.
   1025 	 */
   1026 	ia->ia6_lifetime = ifra->ifra_lifetime;
   1027 	if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
   1028 		ia->ia6_lifetime.ia6t_expire =
   1029 		    time_second + ia->ia6_lifetime.ia6t_vltime;
   1030 	} else
   1031 		ia->ia6_lifetime.ia6t_expire = 0;
   1032 	if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
   1033 		ia->ia6_lifetime.ia6t_preferred =
   1034 		    time_second + ia->ia6_lifetime.ia6t_pltime;
   1035 	} else
   1036 		ia->ia6_lifetime.ia6t_preferred = 0;
   1037 
   1038 	/* reset the interface and routing table appropriately. */
   1039 	if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
   1040 		goto unlink;
   1041 
   1042 	/*
   1043 	 * Make the address tentative before joining multicast addresses,
   1044 	 * so that corresponding MLD responses would not have a tentative
   1045 	 * source address.
   1046 	 */
   1047 	ia->ia6_flags = ifra->ifra_flags;
   1048 	ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/* safety */
   1049 	if (hostIsNew && in6if_do_dad(ifp))
   1050 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
   1051 
   1052 	/*
   1053 	 * Beyond this point, we should call in6_purgeaddr upon an error,
   1054 	 * not just go to unlink.
   1055 	 */
   1056 
   1057 	if ((ifp->if_flags & IFF_MULTICAST) != 0) {
   1058 		struct sockaddr_in6 mltaddr, mltmask;
   1059 #ifndef SCOPEDROUTING
   1060 		u_int32_t zoneid = 0;
   1061 #endif
   1062 
   1063 		if (hostIsNew) {
   1064 			/* join solicited multicast addr for new host id */
   1065 			struct sockaddr_in6 llsol;
   1066 
   1067 			bzero(&llsol, sizeof(llsol));
   1068 			llsol.sin6_family = AF_INET6;
   1069 			llsol.sin6_len = sizeof(llsol);
   1070 			llsol.sin6_addr.s6_addr16[0] = htons(0xff02);
   1071 			llsol.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
   1072 			llsol.sin6_addr.s6_addr32[1] = 0;
   1073 			llsol.sin6_addr.s6_addr32[2] = htonl(1);
   1074 			llsol.sin6_addr.s6_addr32[3] =
   1075 			    ifra->ifra_addr.sin6_addr.s6_addr32[3];
   1076 			llsol.sin6_addr.s6_addr8[12] = 0xff;
   1077 			imm = in6_joingroup(ifp, &llsol.sin6_addr, &error);
   1078 			if (imm) {
   1079 				LIST_INSERT_HEAD(&ia->ia6_memberships, imm,
   1080 				    i6mm_chain);
   1081 			} else {
   1082 				nd6log((LOG_ERR, "in6_update_ifa: addmulti "
   1083 				    "failed for %s on %s (errno=%d)\n",
   1084 				    ip6_sprintf(&llsol.sin6_addr),
   1085 				    if_name(ifp), error));
   1086 				goto cleanup;
   1087 			}
   1088 		}
   1089 
   1090 		bzero(&mltmask, sizeof(mltmask));
   1091 		mltmask.sin6_len = sizeof(struct sockaddr_in6);
   1092 		mltmask.sin6_family = AF_INET6;
   1093 		mltmask.sin6_addr = in6mask32;
   1094 
   1095 		/*
   1096 		 * join link-local all-nodes address
   1097 		 */
   1098 		bzero(&mltaddr, sizeof(mltaddr));
   1099 		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
   1100 		mltaddr.sin6_family = AF_INET6;
   1101 		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
   1102 		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
   1103 
   1104 		/*
   1105 		 * XXX: do we really need this automatic routes?
   1106 		 * We should probably reconsider this stuff.  Most applications
   1107 		 * actually do not need the routes, since they usually specify
   1108 		 * the outgoing interface.
   1109 		 */
   1110 		rt = rtalloc1((struct sockaddr *)&mltaddr, 0);
   1111 		if (rt) {
   1112 			/*
   1113 			 * 32bit came from "mltmask"
   1114 			 * XXX: only works in !SCOPEDROUTING case.
   1115 			 */
   1116 			if (memcmp(&mltaddr.sin6_addr,
   1117 			    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
   1118 			    32 / 8)) {
   1119 				RTFREE(rt);
   1120 				rt = NULL;
   1121 			}
   1122 		}
   1123 		if (!rt) {
   1124 			struct rt_addrinfo info;
   1125 
   1126 			bzero(&info, sizeof(info));
   1127 			info.rti_info[RTAX_DST] = (struct sockaddr *)&mltaddr;
   1128 			info.rti_info[RTAX_GATEWAY] =
   1129 			    (struct sockaddr *)&ia->ia_addr;
   1130 			info.rti_info[RTAX_NETMASK] =
   1131 			    (struct sockaddr *)&mltmask;
   1132 			info.rti_info[RTAX_IFA] =
   1133 			    (struct sockaddr *)&ia->ia_addr;
   1134 			/* XXX: we need RTF_CLONING to fake nd6_rtrequest */
   1135 			info.rti_flags = RTF_UP | RTF_CLONING;
   1136 			error = rtrequest1(RTM_ADD, &info, NULL);
   1137 			if (error)
   1138 				goto cleanup;
   1139 		} else {
   1140 			RTFREE(rt);
   1141 		}
   1142 #ifndef SCOPEDROUTING
   1143 		mltaddr.sin6_scope_id = zoneid;	/* XXX */
   1144 #endif
   1145 		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
   1146 		if (imm) {
   1147 			LIST_INSERT_HEAD(&ia->ia6_memberships, imm,
   1148 			    i6mm_chain);
   1149 		} else {
   1150 			nd6log((LOG_WARNING,
   1151 			    "in6_update_ifa: addmulti failed for "
   1152 			    "%s on %s (errno=%d)\n",
   1153 			    ip6_sprintf(&mltaddr.sin6_addr),
   1154 			    if_name(ifp), error));
   1155 			goto cleanup;
   1156 		}
   1157 
   1158 		/*
   1159 		 * join node information group address
   1160 		 */
   1161 		if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr) == 0) {
   1162 			imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
   1163 			if (imm) {
   1164 				LIST_INSERT_HEAD(&ia->ia6_memberships, imm,
   1165 				    i6mm_chain);
   1166 			} else {
   1167 				nd6log((LOG_WARNING, "in6_update_ifa: "
   1168 				    "addmulti failed for %s on %s (errno=%d)\n",
   1169 				    ip6_sprintf(&mltaddr.sin6_addr),
   1170 				    if_name(ifp), error));
   1171 				/* XXX not very fatal, go on... */
   1172 			}
   1173 		}
   1174 
   1175 		if (ifp->if_flags & IFF_LOOPBACK) {
   1176 			/*
   1177 			 * join node-local all-nodes address, on loopback.
   1178 			 * (ff01::1%ifN, and ff01::%ifN/32)
   1179 			 */
   1180 			mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
   1181 
   1182 			/* XXX: again, do we really need the route? */
   1183 			rt = rtalloc1((struct sockaddr *)&mltaddr, 0);
   1184 			if (rt) {
   1185 				/* 32bit came from "mltmask" */
   1186 				if (memcmp(&mltaddr.sin6_addr,
   1187 				    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
   1188 				    32 / 8)) {
   1189 					RTFREE(rt);
   1190 					rt = NULL;
   1191 				}
   1192 			}
   1193 			if (!rt) {
   1194 				struct rt_addrinfo info;
   1195 
   1196 				bzero(&info, sizeof(info));
   1197 				info.rti_info[RTAX_DST] = (struct sockaddr *)&mltaddr;
   1198 				info.rti_info[RTAX_GATEWAY] =
   1199 				    (struct sockaddr *)&ia->ia_addr;
   1200 				info.rti_info[RTAX_NETMASK] =
   1201 				    (struct sockaddr *)&mltmask;
   1202 				info.rti_info[RTAX_IFA] =
   1203 				    (struct sockaddr *)&ia->ia_addr;
   1204 				info.rti_flags = RTF_UP | RTF_CLONING;
   1205 				error = rtrequest1(RTM_ADD, &info, NULL);
   1206 				if (error)
   1207 					goto cleanup;
   1208 			} else {
   1209 				RTFREE(rt);
   1210 			}
   1211 			imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
   1212 			if (imm) {
   1213 				LIST_INSERT_HEAD(&ia->ia6_memberships, imm,
   1214 				    i6mm_chain);
   1215 			} else {
   1216 				nd6log((LOG_WARNING, "in6_update_ifa: "
   1217 				    "addmulti failed for %s on %s "
   1218 				    "(errno=%d)\n",
   1219 				    ip6_sprintf(&mltaddr.sin6_addr),
   1220 				    if_name(ifp), error));
   1221 				goto cleanup;
   1222 			}
   1223 		}
   1224 	}
   1225 
   1226 	/*
   1227 	 * Perform DAD, if needed.
   1228 	 * XXX It may be of use, if we can administratively
   1229 	 * disable DAD.
   1230 	 */
   1231 	if (hostIsNew && in6if_do_dad(ifp) &&
   1232 	    (ifra->ifra_flags & IN6_IFF_NODAD) == 0)
   1233 	{
   1234 		nd6_dad_start((struct ifaddr *)ia, NULL);
   1235 	}
   1236 
   1237 	return(error);
   1238 
   1239   unlink:
   1240 	/*
   1241 	 * XXX: if a change of an existing address failed, keep the entry
   1242 	 * anyway.
   1243 	 */
   1244 	if (hostIsNew)
   1245 		in6_unlink_ifa(ia, ifp);
   1246 	return(error);
   1247 
   1248   cleanup:
   1249 	in6_purgeaddr(&ia->ia_ifa);
   1250 	return error;
   1251 }
   1252 
   1253 void
   1254 in6_purgeaddr(ifa)
   1255 	struct ifaddr *ifa;
   1256 {
   1257 	struct ifnet *ifp = ifa->ifa_ifp;
   1258 	struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
   1259 	struct in6_multi_mship *imm;
   1260 
   1261 	/* stop DAD processing */
   1262 	nd6_dad_stop(ifa);
   1263 
   1264 	/*
   1265 	 * delete route to the destination of the address being purged.
   1266 	 * The interface must be p2p or loopback in this case.
   1267 	 */
   1268 	if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
   1269 		int e;
   1270 
   1271 		if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
   1272 		    != 0) {
   1273 			log(LOG_ERR, "in6_purgeaddr: failed to remove "
   1274 			    "a route to the p2p destination: %s on %s, "
   1275 			    "errno=%d\n",
   1276 			    ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
   1277 			    e);
   1278 			/* proceed anyway... */
   1279 		} else
   1280 			ia->ia_flags &= ~IFA_ROUTE;
   1281 	}
   1282 
   1283 	/* Remove ownaddr's loopback rtentry, if it exists. */
   1284 	in6_ifremloop(&(ia->ia_ifa));
   1285 
   1286 	/*
   1287 	 * leave from multicast groups we have joined for the interface
   1288 	 */
   1289 	while ((imm = ia->ia6_memberships.lh_first) != NULL) {
   1290 		LIST_REMOVE(imm, i6mm_chain);
   1291 		in6_leavegroup(imm);
   1292 	}
   1293 
   1294 	in6_unlink_ifa(ia, ifp);
   1295 }
   1296 
   1297 static void
   1298 in6_unlink_ifa(ia, ifp)
   1299 	struct in6_ifaddr *ia;
   1300 	struct ifnet *ifp;
   1301 {
   1302 	struct in6_ifaddr *oia;
   1303 	int	s = splnet();
   1304 
   1305 	TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
   1306 	/* release a refcnt for the link from if_addrlist */
   1307 	IFAFREE(&ia->ia_ifa);
   1308 
   1309 	oia = ia;
   1310 	if (oia == (ia = in6_ifaddr))
   1311 		in6_ifaddr = ia->ia_next;
   1312 	else {
   1313 		while (ia->ia_next && (ia->ia_next != oia))
   1314 			ia = ia->ia_next;
   1315 		if (ia->ia_next)
   1316 			ia->ia_next = oia->ia_next;
   1317 		else {
   1318 			/* search failed */
   1319 			printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
   1320 		}
   1321 	}
   1322 
   1323 	if (oia->ia6_multiaddrs.lh_first != NULL) {
   1324 		/*
   1325 		 * XXX thorpej (at) netbsd.org -- if the interface is going
   1326 		 * XXX away, don't save the multicast entries, delete them!
   1327 		 */
   1328 		if (oia->ia_ifa.ifa_ifp->if_output == if_nulloutput) {
   1329 			struct in6_multi *in6m;
   1330 
   1331 			while ((in6m =
   1332 			    LIST_FIRST(&oia->ia6_multiaddrs)) != NULL)
   1333 				in6_delmulti(in6m);
   1334 		} else
   1335 			in6_savemkludge(oia);
   1336 	}
   1337 
   1338 	/*
   1339 	 * When an autoconfigured address is being removed, release the
   1340 	 * reference to the base prefix.  Also, since the release might
   1341 	 * affect the status of other (detached) addresses, call
   1342 	 * pfxlist_onlink_check().
   1343 	 */
   1344 	if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
   1345 		if (oia->ia6_ndpr == NULL) {
   1346 			log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
   1347 			    "%p has no prefix\n", oia);
   1348 		} else {
   1349 			oia->ia6_ndpr->ndpr_refcnt--;
   1350 			oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
   1351 			oia->ia6_ndpr = NULL;
   1352 		}
   1353 
   1354 		pfxlist_onlink_check();
   1355 	}
   1356 
   1357 	/*
   1358 	 * release another refcnt for the link from in6_ifaddr.
   1359 	 * Note that we should decrement the refcnt at least once for all *BSD.
   1360 	 */
   1361 	IFAFREE(&oia->ia_ifa);
   1362 
   1363 	splx(s);
   1364 }
   1365 
   1366 void
   1367 in6_purgeif(ifp)
   1368 	struct ifnet *ifp;
   1369 {
   1370 	struct ifaddr *ifa, *nifa;
   1371 
   1372 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
   1373 	{
   1374 		nifa = TAILQ_NEXT(ifa, ifa_list);
   1375 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1376 			continue;
   1377 		in6_purgeaddr(ifa);
   1378 	}
   1379 
   1380 	in6_ifdetach(ifp);
   1381 }
   1382 
   1383 /*
   1384  * SIOC[GAD]LIFADDR.
   1385  *	SIOCGLIFADDR: get first address. (?)
   1386  *	SIOCGLIFADDR with IFLR_PREFIX:
   1387  *		get first address that matches the specified prefix.
   1388  *	SIOCALIFADDR: add the specified address.
   1389  *	SIOCALIFADDR with IFLR_PREFIX:
   1390  *		add the specified prefix, filling hostid part from
   1391  *		the first link-local address.  prefixlen must be <= 64.
   1392  *	SIOCDLIFADDR: delete the specified address.
   1393  *	SIOCDLIFADDR with IFLR_PREFIX:
   1394  *		delete the first address that matches the specified prefix.
   1395  * return values:
   1396  *	EINVAL on invalid parameters
   1397  *	EADDRNOTAVAIL on prefix match failed/specified address not found
   1398  *	other values may be returned from in6_ioctl()
   1399  *
   1400  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
   1401  * this is to accomodate address naming scheme other than RFC2374,
   1402  * in the future.
   1403  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
   1404  * address encoding scheme. (see figure on page 8)
   1405  */
   1406 static int
   1407 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
   1408 	struct socket *so;
   1409 	u_long cmd;
   1410 	caddr_t	data;
   1411 	struct ifnet *ifp;
   1412 	struct proc *p;
   1413 {
   1414 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
   1415 	struct ifaddr *ifa;
   1416 	struct sockaddr *sa;
   1417 
   1418 	/* sanity checks */
   1419 	if (!data || !ifp) {
   1420 		panic("invalid argument to in6_lifaddr_ioctl");
   1421 		/* NOTREACHED */
   1422 	}
   1423 
   1424 	switch (cmd) {
   1425 	case SIOCGLIFADDR:
   1426 		/* address must be specified on GET with IFLR_PREFIX */
   1427 		if ((iflr->flags & IFLR_PREFIX) == 0)
   1428 			break;
   1429 		/* FALLTHROUGH */
   1430 	case SIOCALIFADDR:
   1431 	case SIOCDLIFADDR:
   1432 		/* address must be specified on ADD and DELETE */
   1433 		sa = (struct sockaddr *)&iflr->addr;
   1434 		if (sa->sa_family != AF_INET6)
   1435 			return EINVAL;
   1436 		if (sa->sa_len != sizeof(struct sockaddr_in6))
   1437 			return EINVAL;
   1438 		/* XXX need improvement */
   1439 		sa = (struct sockaddr *)&iflr->dstaddr;
   1440 		if (sa->sa_family && sa->sa_family != AF_INET6)
   1441 			return EINVAL;
   1442 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
   1443 			return EINVAL;
   1444 		break;
   1445 	default: /* shouldn't happen */
   1446 #if 0
   1447 		panic("invalid cmd to in6_lifaddr_ioctl");
   1448 		/* NOTREACHED */
   1449 #else
   1450 		return EOPNOTSUPP;
   1451 #endif
   1452 	}
   1453 	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
   1454 		return EINVAL;
   1455 
   1456 	switch (cmd) {
   1457 	case SIOCALIFADDR:
   1458 	    {
   1459 		struct in6_aliasreq ifra;
   1460 		struct in6_addr *hostid = NULL;
   1461 		int prefixlen;
   1462 
   1463 		if ((iflr->flags & IFLR_PREFIX) != 0) {
   1464 			struct sockaddr_in6 *sin6;
   1465 
   1466 			/*
   1467 			 * hostid is to fill in the hostid part of the
   1468 			 * address.  hostid points to the first link-local
   1469 			 * address attached to the interface.
   1470 			 */
   1471 			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
   1472 			if (!ifa)
   1473 				return EADDRNOTAVAIL;
   1474 			hostid = IFA_IN6(ifa);
   1475 
   1476 		 	/* prefixlen must be <= 64. */
   1477 			if (64 < iflr->prefixlen)
   1478 				return EINVAL;
   1479 			prefixlen = iflr->prefixlen;
   1480 
   1481 			/* hostid part must be zero. */
   1482 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1483 			if (sin6->sin6_addr.s6_addr32[2] != 0
   1484 			 || sin6->sin6_addr.s6_addr32[3] != 0) {
   1485 				return EINVAL;
   1486 			}
   1487 		} else
   1488 			prefixlen = iflr->prefixlen;
   1489 
   1490 		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
   1491 		bzero(&ifra, sizeof(ifra));
   1492 		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
   1493 
   1494 		bcopy(&iflr->addr, &ifra.ifra_addr,
   1495 		    ((struct sockaddr *)&iflr->addr)->sa_len);
   1496 		if (hostid) {
   1497 			/* fill in hostid part */
   1498 			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
   1499 			    hostid->s6_addr32[2];
   1500 			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
   1501 			    hostid->s6_addr32[3];
   1502 		}
   1503 
   1504 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
   1505 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
   1506 			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
   1507 			if (hostid) {
   1508 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
   1509 				    hostid->s6_addr32[2];
   1510 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
   1511 				    hostid->s6_addr32[3];
   1512 			}
   1513 		}
   1514 
   1515 		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
   1516 		in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
   1517 
   1518 		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
   1519 		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
   1520 	    }
   1521 	case SIOCGLIFADDR:
   1522 	case SIOCDLIFADDR:
   1523 	    {
   1524 		struct in6_ifaddr *ia;
   1525 		struct in6_addr mask, candidate, match;
   1526 		struct sockaddr_in6 *sin6;
   1527 		int cmp;
   1528 
   1529 		bzero(&mask, sizeof(mask));
   1530 		if (iflr->flags & IFLR_PREFIX) {
   1531 			/* lookup a prefix rather than address. */
   1532 			in6_prefixlen2mask(&mask, iflr->prefixlen);
   1533 
   1534 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1535 			bcopy(&sin6->sin6_addr, &match, sizeof(match));
   1536 			match.s6_addr32[0] &= mask.s6_addr32[0];
   1537 			match.s6_addr32[1] &= mask.s6_addr32[1];
   1538 			match.s6_addr32[2] &= mask.s6_addr32[2];
   1539 			match.s6_addr32[3] &= mask.s6_addr32[3];
   1540 
   1541 			/* if you set extra bits, that's wrong */
   1542 			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
   1543 				return EINVAL;
   1544 
   1545 			cmp = 1;
   1546 		} else {
   1547 			if (cmd == SIOCGLIFADDR) {
   1548 				/* on getting an address, take the 1st match */
   1549 				cmp = 0;	/* XXX */
   1550 			} else {
   1551 				/* on deleting an address, do exact match */
   1552 				in6_prefixlen2mask(&mask, 128);
   1553 				sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1554 				bcopy(&sin6->sin6_addr, &match, sizeof(match));
   1555 
   1556 				cmp = 1;
   1557 			}
   1558 		}
   1559 
   1560 		for (ifa = ifp->if_addrlist.tqh_first;
   1561 		     ifa;
   1562 		     ifa = ifa->ifa_list.tqe_next)
   1563 		{
   1564 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1565 				continue;
   1566 			if (!cmp)
   1567 				break;
   1568 
   1569 			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
   1570 			candidate.s6_addr32[0] &= mask.s6_addr32[0];
   1571 			candidate.s6_addr32[1] &= mask.s6_addr32[1];
   1572 			candidate.s6_addr32[2] &= mask.s6_addr32[2];
   1573 			candidate.s6_addr32[3] &= mask.s6_addr32[3];
   1574 			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
   1575 				break;
   1576 		}
   1577 		if (!ifa)
   1578 			return EADDRNOTAVAIL;
   1579 		ia = ifa2ia6(ifa);
   1580 
   1581 		if (cmd == SIOCGLIFADDR) {
   1582 			/* fill in the if_laddrreq structure */
   1583 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
   1584 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1585 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
   1586 				    ia->ia_dstaddr.sin6_len);
   1587 			} else
   1588 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
   1589 
   1590 			iflr->prefixlen =
   1591 			    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
   1592 
   1593 			iflr->flags = ia->ia6_flags;	/* XXX */
   1594 
   1595 			return 0;
   1596 		} else {
   1597 			struct in6_aliasreq ifra;
   1598 
   1599 			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
   1600 			bzero(&ifra, sizeof(ifra));
   1601 			bcopy(iflr->iflr_name, ifra.ifra_name,
   1602 			    sizeof(ifra.ifra_name));
   1603 
   1604 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
   1605 			    ia->ia_addr.sin6_len);
   1606 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1607 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
   1608 				    ia->ia_dstaddr.sin6_len);
   1609 			} else {
   1610 				bzero(&ifra.ifra_dstaddr,
   1611 				    sizeof(ifra.ifra_dstaddr));
   1612 			}
   1613 			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
   1614 			    ia->ia_prefixmask.sin6_len);
   1615 
   1616 			ifra.ifra_flags = ia->ia6_flags;
   1617 			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
   1618 			    ifp, p);
   1619 		}
   1620 	    }
   1621 	}
   1622 
   1623 	return EOPNOTSUPP;	/* just for safety */
   1624 }
   1625 
   1626 /*
   1627  * Initialize an interface's intetnet6 address
   1628  * and routing table entry.
   1629  */
   1630 static int
   1631 in6_ifinit(ifp, ia, sin6, newhost)
   1632 	struct ifnet *ifp;
   1633 	struct in6_ifaddr *ia;
   1634 	struct sockaddr_in6 *sin6;
   1635 	int newhost;
   1636 {
   1637 	int	error = 0, plen, ifacount = 0;
   1638 	int	s = splnet();
   1639 	struct ifaddr *ifa;
   1640 
   1641 	/*
   1642 	 * Give the interface a chance to initialize
   1643 	 * if this is its first address,
   1644 	 * and to validate the address if necessary.
   1645 	 */
   1646 	for (ifa = ifp->if_addrlist.tqh_first; ifa;
   1647 	     ifa = ifa->ifa_list.tqe_next)
   1648 	{
   1649 		if (ifa->ifa_addr == NULL)
   1650 			continue;	/* just for safety */
   1651 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1652 			continue;
   1653 		ifacount++;
   1654 	}
   1655 
   1656 	ia->ia_addr = *sin6;
   1657 
   1658 	if (ifacount <= 1 && ifp->if_ioctl &&
   1659 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
   1660 		splx(s);
   1661 		return(error);
   1662 	}
   1663 	splx(s);
   1664 
   1665 	ia->ia_ifa.ifa_metric = ifp->if_metric;
   1666 
   1667 	/* we could do in(6)_socktrim here, but just omit it at this moment. */
   1668 
   1669 	/*
   1670 	 * Special case:
   1671 	 * If the destination address is specified for a point-to-point
   1672 	 * interface, install a route to the destination as an interface
   1673 	 * direct route.
   1674 	 */
   1675 	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
   1676 	if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
   1677 		if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
   1678 				    RTF_UP | RTF_HOST)) != 0)
   1679 			return(error);
   1680 		ia->ia_flags |= IFA_ROUTE;
   1681 	}
   1682 
   1683 	/* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
   1684 	if (newhost) {
   1685 		/* set the rtrequest function to create llinfo */
   1686 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
   1687 		in6_ifaddloop(&(ia->ia_ifa));
   1688 	}
   1689 
   1690 	if (ifp->if_flags & IFF_MULTICAST)
   1691 		in6_restoremkludge(ia, ifp);
   1692 
   1693 	return(error);
   1694 }
   1695 
   1696 /*
   1697  * Multicast address kludge:
   1698  * If there were any multicast addresses attached to this interface address,
   1699  * either move them to another address on this interface, or save them until
   1700  * such time as this interface is reconfigured for IPv6.
   1701  */
   1702 void
   1703 in6_savemkludge(oia)
   1704 	struct in6_ifaddr *oia;
   1705 {
   1706 	struct in6_ifaddr *ia;
   1707 	struct in6_multi *in6m, *next;
   1708 
   1709 	IFP_TO_IA6(oia->ia_ifp, ia);
   1710 	if (ia) {	/* there is another address */
   1711 		for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
   1712 			next = in6m->in6m_entry.le_next;
   1713 			IFAFREE(&in6m->in6m_ia->ia_ifa);
   1714 			IFAREF(&ia->ia_ifa);
   1715 			in6m->in6m_ia = ia;
   1716 			LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
   1717 		}
   1718 	} else {	/* last address on this if deleted, save */
   1719 		struct multi6_kludge *mk;
   1720 
   1721 		for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
   1722 			if (mk->mk_ifp == oia->ia_ifp)
   1723 				break;
   1724 		}
   1725 		if (mk == NULL) /* this should not happen! */
   1726 			panic("in6_savemkludge: no kludge space");
   1727 
   1728 		for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
   1729 			next = in6m->in6m_entry.le_next;
   1730 			IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
   1731 			in6m->in6m_ia = NULL;
   1732 			LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
   1733 		}
   1734 	}
   1735 }
   1736 
   1737 /*
   1738  * Continuation of multicast address hack:
   1739  * If there was a multicast group list previously saved for this interface,
   1740  * then we re-attach it to the first address configured on the i/f.
   1741  */
   1742 void
   1743 in6_restoremkludge(ia, ifp)
   1744 	struct in6_ifaddr *ia;
   1745 	struct ifnet *ifp;
   1746 {
   1747 	struct multi6_kludge *mk;
   1748 
   1749 	for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
   1750 		if (mk->mk_ifp == ifp) {
   1751 			struct in6_multi *in6m, *next;
   1752 
   1753 			for (in6m = mk->mk_head.lh_first; in6m; in6m = next) {
   1754 				next = in6m->in6m_entry.le_next;
   1755 				in6m->in6m_ia = ia;
   1756 				IFAREF(&ia->ia_ifa);
   1757 				LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
   1758 						 in6m, in6m_entry);
   1759 			}
   1760 			LIST_INIT(&mk->mk_head);
   1761 			break;
   1762 		}
   1763 	}
   1764 }
   1765 
   1766 /*
   1767  * Allocate space for the kludge at interface initialization time.
   1768  * Formerly, we dynamically allocated the space in in6_savemkludge() with
   1769  * malloc(M_WAITOK).  However, it was wrong since the function could be called
   1770  * under an interrupt context (software timer on address lifetime expiration).
   1771  * Also, we cannot just give up allocating the strucutre, since the group
   1772  * membership structure is very complex and we need to keep it anyway.
   1773  * Of course, this function MUST NOT be called under an interrupt context.
   1774  * Specifically, it is expected to be called only from in6_ifattach(), though
   1775  * it is a global function.
   1776  */
   1777 void
   1778 in6_createmkludge(ifp)
   1779 	struct ifnet *ifp;
   1780 {
   1781 	struct multi6_kludge *mk;
   1782 
   1783 	for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
   1784 		/* If we've already had one, do not allocate. */
   1785 		if (mk->mk_ifp == ifp)
   1786 			return;
   1787 	}
   1788 
   1789 	mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
   1790 
   1791 	bzero(mk, sizeof(*mk));
   1792 	LIST_INIT(&mk->mk_head);
   1793 	mk->mk_ifp = ifp;
   1794 	LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
   1795 }
   1796 
   1797 void
   1798 in6_purgemkludge(ifp)
   1799 	struct ifnet *ifp;
   1800 {
   1801 	struct multi6_kludge *mk;
   1802 	struct in6_multi *in6m;
   1803 
   1804 	for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
   1805 		if (mk->mk_ifp != ifp)
   1806 			continue;
   1807 
   1808 		/* leave from all multicast groups joined */
   1809 		while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL)
   1810 			in6_delmulti(in6m);
   1811 		LIST_REMOVE(mk, mk_entry);
   1812 		free(mk, M_IPMADDR);
   1813 		break;
   1814 	}
   1815 }
   1816 
   1817 /*
   1818  * Add an address to the list of IP6 multicast addresses for a
   1819  * given interface.
   1820  */
   1821 struct	in6_multi *
   1822 in6_addmulti(maddr6, ifp, errorp)
   1823 	struct in6_addr *maddr6;
   1824 	struct ifnet *ifp;
   1825 	int *errorp;
   1826 {
   1827 	struct	in6_ifaddr *ia;
   1828 	struct	in6_ifreq ifr;
   1829 	struct	in6_multi *in6m;
   1830 	int	s = splsoftnet();
   1831 
   1832 	*errorp = 0;
   1833 	/*
   1834 	 * See if address already in list.
   1835 	 */
   1836 	IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
   1837 	if (in6m != NULL) {
   1838 		/*
   1839 		 * Found it; just increment the refrence count.
   1840 		 */
   1841 		in6m->in6m_refcount++;
   1842 	} else {
   1843 		/*
   1844 		 * New address; allocate a new multicast record
   1845 		 * and link it into the interface's multicast list.
   1846 		 */
   1847 		in6m = (struct in6_multi *)
   1848 			malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
   1849 		if (in6m == NULL) {
   1850 			splx(s);
   1851 			*errorp = ENOBUFS;
   1852 			return(NULL);
   1853 		}
   1854 		in6m->in6m_addr = *maddr6;
   1855 		in6m->in6m_ifp = ifp;
   1856 		in6m->in6m_refcount = 1;
   1857 		IFP_TO_IA6(ifp, ia);
   1858 		if (ia == NULL) {
   1859 			free(in6m, M_IPMADDR);
   1860 			splx(s);
   1861 			*errorp = EADDRNOTAVAIL; /* appropriate? */
   1862 			return(NULL);
   1863 		}
   1864 		in6m->in6m_ia = ia;
   1865 		IFAREF(&ia->ia_ifa); /* gain a reference */
   1866 		LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
   1867 
   1868 		/*
   1869 		 * Ask the network driver to update its multicast reception
   1870 		 * filter appropriately for the new address.
   1871 		 */
   1872 		bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
   1873 		ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
   1874 		ifr.ifr_addr.sin6_family = AF_INET6;
   1875 		ifr.ifr_addr.sin6_addr = *maddr6;
   1876 		if (ifp->if_ioctl == NULL)
   1877 			*errorp = ENXIO; /* XXX: appropriate? */
   1878 		else
   1879 			*errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
   1880 			    (caddr_t)&ifr);
   1881 		if (*errorp) {
   1882 			LIST_REMOVE(in6m, in6m_entry);
   1883 			free(in6m, M_IPMADDR);
   1884 			IFAFREE(&ia->ia_ifa);
   1885 			splx(s);
   1886 			return(NULL);
   1887 		}
   1888 		/*
   1889 		 * Let MLD6 know that we have joined a new IP6 multicast
   1890 		 * group.
   1891 		 */
   1892 		mld6_start_listening(in6m);
   1893 	}
   1894 	splx(s);
   1895 	return(in6m);
   1896 }
   1897 
   1898 /*
   1899  * Delete a multicast address record.
   1900  */
   1901 void
   1902 in6_delmulti(in6m)
   1903 	struct in6_multi *in6m;
   1904 {
   1905 	struct	in6_ifreq ifr;
   1906 	int	s = splsoftnet();
   1907 
   1908 	if (--in6m->in6m_refcount == 0) {
   1909 		/*
   1910 		 * No remaining claims to this record; let MLD6 know
   1911 		 * that we are leaving the multicast group.
   1912 		 */
   1913 		mld6_stop_listening(in6m);
   1914 
   1915 		/*
   1916 		 * Unlink from list.
   1917 		 */
   1918 		LIST_REMOVE(in6m, in6m_entry);
   1919 		if (in6m->in6m_ia) {
   1920 			IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
   1921 		}
   1922 
   1923 		/*
   1924 		 * Notify the network driver to update its multicast
   1925 		 * reception filter.
   1926 		 */
   1927 		bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
   1928 		ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
   1929 		ifr.ifr_addr.sin6_family = AF_INET6;
   1930 		ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
   1931 		(*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
   1932 					    SIOCDELMULTI, (caddr_t)&ifr);
   1933 		free(in6m, M_IPMADDR);
   1934 	}
   1935 	splx(s);
   1936 }
   1937 
   1938 struct in6_multi_mship *
   1939 in6_joingroup(ifp, addr, errorp)
   1940 	struct ifnet *ifp;
   1941 	struct in6_addr *addr;
   1942 	int *errorp;
   1943 {
   1944 	struct in6_multi_mship *imm;
   1945 
   1946 	imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
   1947 	if (!imm) {
   1948 		*errorp = ENOBUFS;
   1949 		return NULL;
   1950 	}
   1951 	imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
   1952 	if (!imm->i6mm_maddr) {
   1953 		/* *errorp is alrady set */
   1954 		free(imm, M_IPMADDR);
   1955 		return NULL;
   1956 	}
   1957 	return imm;
   1958 }
   1959 
   1960 int
   1961 in6_leavegroup(imm)
   1962 	struct in6_multi_mship *imm;
   1963 {
   1964 
   1965 	if (imm->i6mm_maddr)
   1966 		in6_delmulti(imm->i6mm_maddr);
   1967 	free(imm,  M_IPMADDR);
   1968 	return 0;
   1969 }
   1970 
   1971 /*
   1972  * Find an IPv6 interface link-local address specific to an interface.
   1973  */
   1974 struct in6_ifaddr *
   1975 in6ifa_ifpforlinklocal(ifp, ignoreflags)
   1976 	struct ifnet *ifp;
   1977 	int ignoreflags;
   1978 {
   1979 	struct ifaddr *ifa;
   1980 
   1981 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1982 	{
   1983 		if (ifa->ifa_addr == NULL)
   1984 			continue;	/* just for safety */
   1985 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1986 			continue;
   1987 		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
   1988 			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
   1989 			     ignoreflags) != 0)
   1990 				continue;
   1991 			break;
   1992 		}
   1993 	}
   1994 
   1995 	return((struct in6_ifaddr *)ifa);
   1996 }
   1997 
   1998 
   1999 /*
   2000  * find the internet address corresponding to a given interface and address.
   2001  */
   2002 struct in6_ifaddr *
   2003 in6ifa_ifpwithaddr(ifp, addr)
   2004 	struct ifnet *ifp;
   2005 	struct in6_addr *addr;
   2006 {
   2007 	struct ifaddr *ifa;
   2008 
   2009 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   2010 	{
   2011 		if (ifa->ifa_addr == NULL)
   2012 			continue;	/* just for safety */
   2013 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2014 			continue;
   2015 		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
   2016 			break;
   2017 	}
   2018 
   2019 	return((struct in6_ifaddr *)ifa);
   2020 }
   2021 
   2022 /*
   2023  * Convert IP6 address to printable (loggable) representation.
   2024  */
   2025 static char digits[] = "0123456789abcdef";
   2026 static int ip6round = 0;
   2027 char *
   2028 ip6_sprintf(addr)
   2029 	const struct in6_addr *addr;
   2030 {
   2031 	static char ip6buf[8][48];
   2032 	int i;
   2033 	char *cp;
   2034 	const u_short *a = (const u_short *)addr;
   2035 	const u_char *d;
   2036 	int dcolon = 0;
   2037 
   2038 	ip6round = (ip6round + 1) & 7;
   2039 	cp = ip6buf[ip6round];
   2040 
   2041 	for (i = 0; i < 8; i++) {
   2042 		if (dcolon == 1) {
   2043 			if (*a == 0) {
   2044 				if (i == 7)
   2045 					*cp++ = ':';
   2046 				a++;
   2047 				continue;
   2048 			} else
   2049 				dcolon = 2;
   2050 		}
   2051 		if (*a == 0) {
   2052 			if (dcolon == 0 && *(a + 1) == 0) {
   2053 				if (i == 0)
   2054 					*cp++ = ':';
   2055 				*cp++ = ':';
   2056 				dcolon = 1;
   2057 			} else {
   2058 				*cp++ = '0';
   2059 				*cp++ = ':';
   2060 			}
   2061 			a++;
   2062 			continue;
   2063 		}
   2064 		d = (const u_char *)a;
   2065 		*cp++ = digits[*d >> 4];
   2066 		*cp++ = digits[*d++ & 0xf];
   2067 		*cp++ = digits[*d >> 4];
   2068 		*cp++ = digits[*d & 0xf];
   2069 		*cp++ = ':';
   2070 		a++;
   2071 	}
   2072 	*--cp = 0;
   2073 	return(ip6buf[ip6round]);
   2074 }
   2075 
   2076 /*
   2077  * Get a scope of the address. Node-local, link-local, site-local or global.
   2078  */
   2079 int
   2080 in6_addrscope (addr)
   2081 struct in6_addr *addr;
   2082 {
   2083 	int scope;
   2084 
   2085 	if (addr->s6_addr8[0] == 0xfe) {
   2086 		scope = addr->s6_addr8[1] & 0xc0;
   2087 
   2088 		switch (scope) {
   2089 		case 0x80:
   2090 			return IPV6_ADDR_SCOPE_LINKLOCAL;
   2091 			break;
   2092 		case 0xc0:
   2093 			return IPV6_ADDR_SCOPE_SITELOCAL;
   2094 			break;
   2095 		default:
   2096 			return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
   2097 			break;
   2098 		}
   2099 	}
   2100 
   2101 
   2102 	if (addr->s6_addr8[0] == 0xff) {
   2103 		scope = addr->s6_addr8[1] & 0x0f;
   2104 
   2105 		/*
   2106 		 * due to other scope such as reserved,
   2107 		 * return scope doesn't work.
   2108 		 */
   2109 		switch (scope) {
   2110 		case IPV6_ADDR_SCOPE_NODELOCAL:
   2111 			return IPV6_ADDR_SCOPE_NODELOCAL;
   2112 			break;
   2113 		case IPV6_ADDR_SCOPE_LINKLOCAL:
   2114 			return IPV6_ADDR_SCOPE_LINKLOCAL;
   2115 			break;
   2116 		case IPV6_ADDR_SCOPE_SITELOCAL:
   2117 			return IPV6_ADDR_SCOPE_SITELOCAL;
   2118 			break;
   2119 		default:
   2120 			return IPV6_ADDR_SCOPE_GLOBAL;
   2121 			break;
   2122 		}
   2123 	}
   2124 
   2125 	if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
   2126 		if (addr->s6_addr8[15] == 1) /* loopback */
   2127 			return IPV6_ADDR_SCOPE_NODELOCAL;
   2128 		if (addr->s6_addr8[15] == 0) /* unspecified */
   2129 			return IPV6_ADDR_SCOPE_LINKLOCAL;
   2130 	}
   2131 
   2132 	return IPV6_ADDR_SCOPE_GLOBAL;
   2133 }
   2134 
   2135 int
   2136 in6_addr2scopeid(ifp, addr)
   2137 	struct ifnet *ifp;	/* must not be NULL */
   2138 	struct in6_addr *addr;	/* must not be NULL */
   2139 {
   2140 	int scope = in6_addrscope(addr);
   2141 
   2142 	switch (scope) {
   2143 	case IPV6_ADDR_SCOPE_NODELOCAL:
   2144 		return(-1);	/* XXX: is this an appropriate value? */
   2145 
   2146 	case IPV6_ADDR_SCOPE_LINKLOCAL:
   2147 		/* XXX: we do not distinguish between a link and an I/F. */
   2148 		return(ifp->if_index);
   2149 
   2150 	case IPV6_ADDR_SCOPE_SITELOCAL:
   2151 		return(0);	/* XXX: invalid. */
   2152 
   2153 	default:
   2154 		return(0);	/* XXX: treat as global. */
   2155 	}
   2156 }
   2157 
   2158 int
   2159 in6_is_addr_deprecated(sa6)
   2160 	struct sockaddr_in6 *sa6;
   2161 {
   2162 	struct in6_ifaddr *ia;
   2163 
   2164 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
   2165 		if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
   2166 		    &sa6->sin6_addr) &&
   2167 #ifdef SCOPEDROUTING
   2168 		    ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
   2169 #endif
   2170 		    (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
   2171 			return(1); /* true */
   2172 
   2173 		/* XXX: do we still have to go thru the rest of the list? */
   2174 	}
   2175 
   2176 	return(0);		/* false */
   2177 }
   2178 
   2179 /*
   2180  * return length of part which dst and src are equal
   2181  * hard coding...
   2182  */
   2183 int
   2184 in6_matchlen(src, dst)
   2185 struct in6_addr *src, *dst;
   2186 {
   2187 	int match = 0;
   2188 	u_char *s = (u_char *)src, *d = (u_char *)dst;
   2189 	u_char *lim = s + 16, r;
   2190 
   2191 	while (s < lim)
   2192 		if ((r = (*d++ ^ *s++)) != 0) {
   2193 			while (r < 128) {
   2194 				match++;
   2195 				r <<= 1;
   2196 			}
   2197 			break;
   2198 		} else
   2199 			match += 8;
   2200 	return match;
   2201 }
   2202 
   2203 /* XXX: to be scope conscious */
   2204 int
   2205 in6_are_prefix_equal(p1, p2, len)
   2206 	struct in6_addr *p1, *p2;
   2207 	int len;
   2208 {
   2209 	int bytelen, bitlen;
   2210 
   2211 	/* sanity check */
   2212 	if (0 > len || len > 128) {
   2213 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
   2214 		    len);
   2215 		return(0);
   2216 	}
   2217 
   2218 	bytelen = len / 8;
   2219 	bitlen = len % 8;
   2220 
   2221 	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
   2222 		return(0);
   2223 	if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
   2224 	    p2->s6_addr[bytelen] >> (8 - bitlen))
   2225 		return(0);
   2226 
   2227 	return(1);
   2228 }
   2229 
   2230 void
   2231 in6_prefixlen2mask(maskp, len)
   2232 	struct in6_addr *maskp;
   2233 	int len;
   2234 {
   2235 	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
   2236 	int bytelen, bitlen, i;
   2237 
   2238 	/* sanity check */
   2239 	if (0 > len || len > 128) {
   2240 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
   2241 		    len);
   2242 		return;
   2243 	}
   2244 
   2245 	bzero(maskp, sizeof(*maskp));
   2246 	bytelen = len / 8;
   2247 	bitlen = len % 8;
   2248 	for (i = 0; i < bytelen; i++)
   2249 		maskp->s6_addr[i] = 0xff;
   2250 	if (bitlen)
   2251 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
   2252 }
   2253 
   2254 /*
   2255  * return the best address out of the same scope
   2256  */
   2257 struct in6_ifaddr *
   2258 in6_ifawithscope(oifp, dst)
   2259 	struct ifnet *oifp;
   2260 	struct in6_addr *dst;
   2261 {
   2262 	int dst_scope =	in6_addrscope(dst), src_scope, best_scope = 0;
   2263 	int blen = -1;
   2264 	struct ifaddr *ifa;
   2265 	struct ifnet *ifp;
   2266 	struct in6_ifaddr *ifa_best = NULL;
   2267 
   2268 	if (oifp == NULL) {
   2269 		printf("in6_ifawithscope: output interface is not specified\n");
   2270 		return(NULL);
   2271 	}
   2272 
   2273 	/*
   2274 	 * We search for all addresses on all interfaces from the beginning.
   2275 	 * Comparing an interface with the outgoing interface will be done
   2276 	 * only at the final stage of tiebreaking.
   2277 	 */
   2278 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
   2279 	{
   2280 		/*
   2281 		 * We can never take an address that breaks the scope zone
   2282 		 * of the destination.
   2283 		 */
   2284 		if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
   2285 			continue;
   2286 
   2287 		for (ifa = ifp->if_addrlist.tqh_first; ifa;
   2288 		     ifa = ifa->ifa_list.tqe_next)
   2289 		{
   2290 			int tlen = -1, dscopecmp, bscopecmp, matchcmp;
   2291 
   2292 			if (ifa->ifa_addr->sa_family != AF_INET6)
   2293 				continue;
   2294 
   2295 			src_scope = in6_addrscope(IFA_IN6(ifa));
   2296 
   2297 #ifdef ADDRSELECT_DEBUG		/* should be removed after stabilization */
   2298 			dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
   2299 			printf("in6_ifawithscope: dst=%s bestaddr=%s, "
   2300 			       "newaddr=%s, scope=%x, dcmp=%d, bcmp=%d, "
   2301 			       "matchlen=%d, flgs=%x\n",
   2302 			       ip6_sprintf(dst),
   2303 			       ifa_best ? ip6_sprintf(&ifa_best->ia_addr.sin6_addr) : "none",
   2304 			       ip6_sprintf(IFA_IN6(ifa)), src_scope,
   2305 			       dscopecmp,
   2306 			       ifa_best ? IN6_ARE_SCOPE_CMP(src_scope, best_scope) : -1,
   2307 			       in6_matchlen(IFA_IN6(ifa), dst),
   2308 			       ((struct in6_ifaddr *)ifa)->ia6_flags);
   2309 #endif
   2310 
   2311 			/*
   2312 			 * Don't use an address before completing DAD
   2313 			 * nor a duplicated address.
   2314 			 */
   2315 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
   2316 			    IN6_IFF_NOTREADY)
   2317 				continue;
   2318 
   2319 			/* XXX: is there any case to allow anycasts? */
   2320 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
   2321 			    IN6_IFF_ANYCAST)
   2322 				continue;
   2323 
   2324 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
   2325 			    IN6_IFF_DETACHED)
   2326 				continue;
   2327 
   2328 			/*
   2329 			 * If this is the first address we find,
   2330 			 * keep it anyway.
   2331 			 */
   2332 			if (ifa_best == NULL)
   2333 				goto replace;
   2334 
   2335 			/*
   2336 			 * ifa_best is never NULL beyond this line except
   2337 			 * within the block labeled "replace".
   2338 			 */
   2339 
   2340 			/*
   2341 			 * If ifa_best has a smaller scope than dst and
   2342 			 * the current address has a larger one than
   2343 			 * (or equal to) dst, always replace ifa_best.
   2344 			 * Also, if the current address has a smaller scope
   2345 			 * than dst, ignore it unless ifa_best also has a
   2346 			 * smaller scope.
   2347 			 */
   2348 			if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
   2349 			    IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
   2350 				goto replace;
   2351 			if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
   2352 			    IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
   2353 				continue;
   2354 
   2355 			/*
   2356 			 * A deprecated address SHOULD NOT be used in new
   2357 			 * communications if an alternate (non-deprecated)
   2358 			 * address is available and has sufficient scope.
   2359 			 * RFC 2462, Section 5.5.4.
   2360 			 */
   2361 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
   2362 			    IN6_IFF_DEPRECATED) {
   2363 				/*
   2364 				 * Ignore any deprecated addresses if
   2365 				 * specified by configuration.
   2366 				 */
   2367 				if (!ip6_use_deprecated)
   2368 					continue;
   2369 
   2370 				/*
   2371 				 * If we have already found a non-deprecated
   2372 				 * candidate, just ignore deprecated addresses.
   2373 				 */
   2374 				if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
   2375 				    == 0)
   2376 					continue;
   2377 			}
   2378 
   2379 			/*
   2380 			 * A non-deprecated address is always preferred
   2381 			 * to a deprecated one regardless of scopes and
   2382 			 * address matching.
   2383 			 */
   2384 			if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
   2385 			    (((struct in6_ifaddr *)ifa)->ia6_flags &
   2386 			     IN6_IFF_DEPRECATED) == 0)
   2387 				goto replace;
   2388 
   2389 			/*
   2390 			 * At this point, we have two cases:
   2391 			 * 1. we are looking at a non-deprecated address,
   2392 			 *    and ifa_best is also non-deprecated.
   2393 			 * 2. we are looking at a deprecated address,
   2394 			 *    and ifa_best is also deprecated.
   2395 			 * Also, we do not have to consider a case where
   2396 			 * the scope of if_best is larger(smaller) than dst and
   2397 			 * the scope of the current address is smaller(larger)
   2398 			 * than dst. Such a case has already been covered.
   2399 			 * Tiebreaking is done according to the following
   2400 			 * items:
   2401 			 * - the scope comparison between the address and
   2402 			 *   dst (dscopecmp)
   2403 			 * - the scope comparison between the address and
   2404 			 *   ifa_best (bscopecmp)
   2405 			 * - if the address match dst longer than ifa_best
   2406 			 *   (matchcmp)
   2407 			 * - if the address is on the outgoing I/F (outI/F)
   2408 			 *
   2409 			 * Roughly speaking, the selection policy is
   2410 			 * - the most important item is scope. The same scope
   2411 			 *   is best. Then search for a larger scope.
   2412 			 *   Smaller scopes are the last resort.
   2413 			 * - A deprecated address is chosen only when we have
   2414 			 *   no address that has an enough scope, but is
   2415 			 *   prefered to any addresses of smaller scopes.
   2416 			 * - Longest address match against dst is considered
   2417 			 *   only for addresses that has the same scope of dst.
   2418 			 * - If there is no other reasons to choose one,
   2419 			 *   addresses on the outgoing I/F are preferred.
   2420 			 *
   2421 			 * The precise decision table is as follows:
   2422 			 * dscopecmp bscopecmp matchcmp outI/F | replace?
   2423 			 *    !equal     equal      N/A    Yes |      Yes (1)
   2424 			 *    !equal     equal      N/A     No |       No (2)
   2425 			 *    larger    larger      N/A    N/A |       No (3)
   2426 			 *    larger   smaller      N/A    N/A |      Yes (4)
   2427 			 *   smaller    larger      N/A    N/A |      Yes (5)
   2428 			 *   smaller   smaller      N/A    N/A |       No (6)
   2429 			 *     equal   smaller      N/A    N/A |      Yes (7)
   2430 			 *     equal    larger       (already done)
   2431 			 *     equal     equal   larger    N/A |      Yes (8)
   2432 			 *     equal     equal  smaller    N/A |       No (9)
   2433 			 *     equal     equal    equal    Yes |      Yes (a)
   2434 			 *     eaual     eqaul    equal     No |       No (b)
   2435 			 */
   2436 			dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
   2437 			bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
   2438 
   2439 			if (dscopecmp && bscopecmp == 0) {
   2440 				if (oifp == ifp) /* (1) */
   2441 					goto replace;
   2442 				continue; /* (2) */
   2443 			}
   2444 			if (dscopecmp > 0) {
   2445 				if (bscopecmp > 0) /* (3) */
   2446 					continue;
   2447 				goto replace; /* (4) */
   2448 			}
   2449 			if (dscopecmp < 0) {
   2450 				if (bscopecmp > 0) /* (5) */
   2451 					goto replace;
   2452 				continue; /* (6) */
   2453 			}
   2454 
   2455 			/* now dscopecmp must be 0 */
   2456 			if (bscopecmp < 0)
   2457 				goto replace; /* (7) */
   2458 
   2459 			/*
   2460 			 * At last both dscopecmp and bscopecmp must be 0.
   2461 			 * We need address matching against dst for
   2462 			 * tiebreaking.
   2463 			 */
   2464 			tlen = in6_matchlen(IFA_IN6(ifa), dst);
   2465 			matchcmp = tlen - blen;
   2466 			if (matchcmp > 0) /* (8) */
   2467 				goto replace;
   2468 			if (matchcmp < 0) /* (9) */
   2469 				continue;
   2470 			if (oifp == ifp) /* (a) */
   2471 				goto replace;
   2472 			continue; /* (b) */
   2473 
   2474 		  replace:
   2475 			ifa_best = (struct in6_ifaddr *)ifa;
   2476 			blen = tlen >= 0 ? tlen :
   2477 				in6_matchlen(IFA_IN6(ifa), dst);
   2478 			best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
   2479 		}
   2480 	}
   2481 
   2482 	/* count statistics for future improvements */
   2483 	if (ifa_best == NULL)
   2484 		ip6stat.ip6s_sources_none++;
   2485 	else {
   2486 		if (oifp == ifa_best->ia_ifp)
   2487 			ip6stat.ip6s_sources_sameif[best_scope]++;
   2488 		else
   2489 			ip6stat.ip6s_sources_otherif[best_scope]++;
   2490 
   2491 		if (best_scope == dst_scope)
   2492 			ip6stat.ip6s_sources_samescope[best_scope]++;
   2493 		else
   2494 			ip6stat.ip6s_sources_otherscope[best_scope]++;
   2495 
   2496 		if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
   2497 			ip6stat.ip6s_sources_deprecated[best_scope]++;
   2498 	}
   2499 
   2500 	return(ifa_best);
   2501 }
   2502 
   2503 /*
   2504  * return the best address out of the same scope. if no address was
   2505  * found, return the first valid address from designated IF.
   2506  */
   2507 struct in6_ifaddr *
   2508 in6_ifawithifp(ifp, dst)
   2509 	struct ifnet *ifp;
   2510 	struct in6_addr *dst;
   2511 {
   2512 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
   2513 	struct ifaddr *ifa;
   2514 	struct in6_ifaddr *besta = 0;
   2515 	struct in6_ifaddr *dep[2];	/* last-resort: deprecated */
   2516 
   2517 	dep[0] = dep[1] = NULL;
   2518 
   2519 	/*
   2520 	 * We first look for addresses in the same scope.
   2521 	 * If there is one, return it.
   2522 	 * If two or more, return one which matches the dst longest.
   2523 	 * If none, return one of global addresses assigned other ifs.
   2524 	 */
   2525 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   2526 	{
   2527 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2528 			continue;
   2529 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
   2530 			continue; /* XXX: is there any case to allow anycast? */
   2531 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
   2532 			continue; /* don't use this interface */
   2533 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
   2534 			continue;
   2535 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
   2536 			if (ip6_use_deprecated)
   2537 				dep[0] = (struct in6_ifaddr *)ifa;
   2538 			continue;
   2539 		}
   2540 
   2541 		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
   2542 			/*
   2543 			 * call in6_matchlen() as few as possible
   2544 			 */
   2545 			if (besta) {
   2546 				if (blen == -1)
   2547 					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
   2548 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
   2549 				if (tlen > blen) {
   2550 					blen = tlen;
   2551 					besta = (struct in6_ifaddr *)ifa;
   2552 				}
   2553 			} else
   2554 				besta = (struct in6_ifaddr *)ifa;
   2555 		}
   2556 	}
   2557 	if (besta)
   2558 		return(besta);
   2559 
   2560 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   2561 	{
   2562 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2563 			continue;
   2564 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
   2565 			continue; /* XXX: is there any case to allow anycast? */
   2566 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
   2567 			continue; /* don't use this interface */
   2568 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
   2569 			continue;
   2570 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
   2571 			if (ip6_use_deprecated)
   2572 				dep[1] = (struct in6_ifaddr *)ifa;
   2573 			continue;
   2574 		}
   2575 
   2576 		return (struct in6_ifaddr *)ifa;
   2577 	}
   2578 
   2579 	/* use the last-resort values, that are, deprecated addresses */
   2580 	if (dep[0])
   2581 		return dep[0];
   2582 	if (dep[1])
   2583 		return dep[1];
   2584 
   2585 	return NULL;
   2586 }
   2587 
   2588 /*
   2589  * perform DAD when interface becomes IFF_UP.
   2590  */
   2591 void
   2592 in6_if_up(ifp)
   2593 	struct ifnet *ifp;
   2594 {
   2595 	struct ifaddr *ifa;
   2596 	struct in6_ifaddr *ia;
   2597 	int dad_delay;		/* delay ticks before DAD output */
   2598 
   2599 	/*
   2600 	 * special cases, like 6to4, are handled in in6_ifattach
   2601 	 */
   2602 	in6_ifattach(ifp, NULL);
   2603 
   2604 	dad_delay = 0;
   2605 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   2606 	{
   2607 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2608 			continue;
   2609 		ia = (struct in6_ifaddr *)ifa;
   2610 		if (ia->ia6_flags & IN6_IFF_TENTATIVE)
   2611 			nd6_dad_start(ifa, &dad_delay);
   2612 	}
   2613 }
   2614 
   2615 int
   2616 in6if_do_dad(ifp)
   2617 	struct ifnet *ifp;
   2618 {
   2619 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
   2620 		return(0);
   2621 
   2622 	switch (ifp->if_type) {
   2623 	case IFT_FAITH:
   2624 		/*
   2625 		 * These interfaces do not have the IFF_LOOPBACK flag,
   2626 		 * but loop packets back.  We do not have to do DAD on such
   2627 		 * interfaces.  We should even omit it, because loop-backed
   2628 		 * NS would confuse the DAD procedure.
   2629 		 */
   2630 		return(0);
   2631 	default:
   2632 		/*
   2633 		 * Our DAD routine requires the interface up and running.
   2634 		 * However, some interfaces can be up before the RUNNING
   2635 		 * status.  Additionaly, users may try to assign addresses
   2636 		 * before the interface becomes up (or running).
   2637 		 * We simply skip DAD in such a case as a work around.
   2638 		 * XXX: we should rather mark "tentative" on such addresses,
   2639 		 * and do DAD after the interface becomes ready.
   2640 		 */
   2641 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
   2642 		    (IFF_UP|IFF_RUNNING))
   2643 			return(0);
   2644 
   2645 		return(1);
   2646 	}
   2647 }
   2648 
   2649 /*
   2650  * Calculate max IPv6 MTU through all the interfaces and store it
   2651  * to in6_maxmtu.
   2652  */
   2653 void
   2654 in6_setmaxmtu()
   2655 {
   2656 	unsigned long maxmtu = 0;
   2657 	struct ifnet *ifp;
   2658 
   2659 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
   2660 	{
   2661 		/* this function can be called during ifnet initialization */
   2662 		if (!ifp->if_afdata[AF_INET6])
   2663 			continue;
   2664 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
   2665 		    IN6_LINKMTU(ifp) > maxmtu)
   2666 			maxmtu = IN6_LINKMTU(ifp);
   2667 	}
   2668 	if (maxmtu)	     /* update only when maxmtu is positive */
   2669 		in6_maxmtu = maxmtu;
   2670 }
   2671 
   2672 void *
   2673 in6_domifattach(ifp)
   2674 	struct ifnet *ifp;
   2675 {
   2676 	struct in6_ifextra *ext;
   2677 
   2678 	ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
   2679 	bzero(ext, sizeof(*ext));
   2680 
   2681 	ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
   2682 	    M_IFADDR, M_WAITOK);
   2683 	bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
   2684 
   2685 	ext->icmp6_ifstat =
   2686 	    (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
   2687 	    M_IFADDR, M_WAITOK);
   2688 	bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
   2689 
   2690 	ext->nd_ifinfo = nd6_ifattach(ifp);
   2691 	return ext;
   2692 }
   2693 
   2694 void
   2695 in6_domifdetach(ifp, aux)
   2696 	struct ifnet *ifp;
   2697 	void *aux;
   2698 {
   2699 	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
   2700 
   2701 	nd6_ifdetach(ext->nd_ifinfo);
   2702 	free(ext->in6_ifstat, M_IFADDR);
   2703 	free(ext->icmp6_ifstat, M_IFADDR);
   2704 	free(ext, M_IFADDR);
   2705 }
   2706