Home | History | Annotate | Line # | Download | only in netinet6
in6.c revision 1.21
      1 /*	$NetBSD: in6.c,v 1.21 2000/02/26 08:39:19 itojun Exp $	*/
      2 /*	$KAME: in6.c,v 1.55 2000/02/25 00:32:23 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 "opt_inet.h"
     69 
     70 #include <sys/param.h>
     71 #include <sys/ioctl.h>
     72 #include <sys/errno.h>
     73 #include <sys/malloc.h>
     74 #include <sys/socket.h>
     75 #include <sys/socketvar.h>
     76 #include <sys/sockio.h>
     77 #include <sys/systm.h>
     78 #include <sys/proc.h>
     79 #include <sys/time.h>
     80 #include <sys/kernel.h>
     81 #include <sys/syslog.h>
     82 
     83 #include <net/if.h>
     84 #include <net/if_types.h>
     85 #include <net/route.h>
     86 #include "gif.h"
     87 #if NGIF > 0
     88 #include <net/if_gif.h>
     89 #endif
     90 #include <net/if_dl.h>
     91 
     92 #include <netinet/in.h>
     93 #include <netinet/in_var.h>
     94 #include <net/if_ether.h>
     95 
     96 #include <netinet6/nd6.h>
     97 #include <netinet/ip6.h>
     98 #include <netinet6/ip6_var.h>
     99 #include <netinet6/mld6_var.h>
    100 #include <netinet6/ip6_mroute.h>
    101 #include <netinet6/in6_ifattach.h>
    102 
    103 #include <net/net_osdep.h>
    104 
    105 /* enable backward compatibility code for obsoleted ioctls */
    106 #define COMPAT_IN6IFIOCTL
    107 
    108 /*
    109  * Definitions of some costant IP6 addresses.
    110  */
    111 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
    112 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
    113 const struct in6_addr in6addr_nodelocal_allnodes =
    114 	IN6ADDR_NODELOCAL_ALLNODES_INIT;
    115 const struct in6_addr in6addr_linklocal_allnodes =
    116 	IN6ADDR_LINKLOCAL_ALLNODES_INIT;
    117 const struct in6_addr in6addr_linklocal_allrouters =
    118 	IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
    119 
    120 const struct in6_addr in6mask0 = IN6MASK0;
    121 const struct in6_addr in6mask32 = IN6MASK32;
    122 const struct in6_addr in6mask64 = IN6MASK64;
    123 const struct in6_addr in6mask96 = IN6MASK96;
    124 const struct in6_addr in6mask128 = IN6MASK128;
    125 
    126 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
    127 	struct ifnet *, struct proc *));
    128 
    129 /*
    130  * This structure is used to keep track of in6_multi chains which belong to
    131  * deleted interface addresses.
    132  */
    133 static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
    134 
    135 struct multi6_kludge {
    136 	LIST_ENTRY(multi6_kludge) mk_entry;
    137 	struct ifnet *mk_ifp;
    138 	struct in6_multihead mk_head;
    139 };
    140 
    141 /*
    142  * Check if the loopback entry will be automatically generated.
    143  *   if 0 returned, will not be automatically generated.
    144  *   if 1 returned, will be automatically generated.
    145  */
    146 static int
    147 in6_is_ifloop_auto(struct ifaddr *ifa)
    148 {
    149 #define SIN6(s) ((struct sockaddr_in6 *)s)
    150 	/*
    151 	 * If RTF_CLONING is unset, or (IFF_LOOPBACK | IFF_POINTOPOINT),
    152 	 * or netmask is all0 or all1, then cloning will not happen,
    153 	 * then we can't rely on its loopback entry generation.
    154 	 */
    155 	if ((ifa->ifa_flags & RTF_CLONING) == 0 ||
    156 	    (ifa->ifa_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) ||
    157 	    (SIN6(ifa->ifa_netmask)->sin6_len == sizeof(struct sockaddr_in6)
    158 	     &&
    159 	     IN6_ARE_ADDR_EQUAL(&SIN6(ifa->ifa_netmask)->sin6_addr,
    160 				&in6mask128)) ||
    161 	    ((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_len == 0)
    162 		return 0;
    163 	else
    164 		return 1;
    165 #undef SIN6
    166 }
    167 
    168 /*
    169  * Subroutine for in6_ifaddloop() and in6_ifremloop().
    170  * This routine does actual work.
    171  */
    172 static void
    173 in6_ifloop_request(int cmd, struct ifaddr *ifa)
    174 {
    175 	struct sockaddr_in6 lo_sa;
    176 	struct sockaddr_in6 all1_sa;
    177 	struct rtentry *nrt = NULL;
    178 
    179 	bzero(&lo_sa, sizeof(lo_sa));
    180 	bzero(&all1_sa, sizeof(all1_sa));
    181 	lo_sa.sin6_family = AF_INET6;
    182 	lo_sa.sin6_len = sizeof(struct sockaddr_in6);
    183 	all1_sa = lo_sa;
    184 	lo_sa.sin6_addr = in6addr_loopback;
    185 	all1_sa.sin6_addr = in6mask128;
    186 
    187 	/* So we add or remove static loopback entry, here. */
    188 	rtrequest(cmd, ifa->ifa_addr,
    189 		  (struct sockaddr *)&lo_sa,
    190 		  (struct sockaddr *)&all1_sa,
    191 		  RTF_UP|RTF_HOST, &nrt);
    192 
    193 	/*
    194 	 * Make sure rt_ifa be equal to IFA, the second argument of the
    195 	 * function.
    196 	 * We need this because when we refer rt_ifa->ia6_flags in ip6_input,
    197 	 * we assume that the rt_ifa points to the address instead of the
    198 	 * loopback address.
    199 	 */
    200 	if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
    201 		IFAFREE(nrt->rt_ifa);
    202 		IFAREF(ifa);
    203 		nrt->rt_ifa = ifa;
    204 	}
    205 	if (nrt)
    206 		nrt->rt_refcnt--;
    207 }
    208 
    209 /*
    210  * Add ownaddr as loopback rtentry, if necessary(ex. on p2p link).
    211  * Because, KAME needs loopback rtentry for ownaddr check in
    212  * ip6_input().
    213  */
    214 static void
    215 in6_ifaddloop(struct ifaddr *ifa)
    216 {
    217 	if (!in6_is_ifloop_auto(ifa)) {
    218 		struct rtentry *rt;
    219 
    220 		/* If there is no loopback entry, allocate one. */
    221 		rt = rtalloc1(ifa->ifa_addr, 0);
    222 		if (rt == 0 || (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
    223 			in6_ifloop_request(RTM_ADD, ifa);
    224 		if (rt)
    225 			rt->rt_refcnt--;
    226 	}
    227 }
    228 
    229 /*
    230  * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
    231  * if it exists.
    232  */
    233 static void
    234 in6_ifremloop(struct ifaddr *ifa)
    235 {
    236 	if (!in6_is_ifloop_auto(ifa)) {
    237 		struct in6_ifaddr *ia;
    238 		int ia_count = 0;
    239 
    240 		/* If only one ifa for the loopback entry, delete it. */
    241 		for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
    242 			if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa),
    243 					       &ia->ia_addr.sin6_addr)) {
    244 				ia_count++;
    245 				if (ia_count > 1)
    246 					break;
    247 			}
    248 		}
    249 		if (ia_count == 1)
    250 			in6_ifloop_request(RTM_DELETE, ifa);
    251 	}
    252 }
    253 
    254 int
    255 in6_ifindex2scopeid(idx)
    256 	int idx;
    257 {
    258 	struct ifnet *ifp;
    259 	struct ifaddr *ifa;
    260 	struct sockaddr_in6 *sin6;
    261 
    262 	if (idx < 0 || if_index < idx)
    263 		return -1;
    264 	ifp = ifindex2ifnet[idx];
    265 
    266 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
    267 	{
    268 		if (ifa->ifa_addr->sa_family != AF_INET6)
    269 			continue;
    270 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
    271 		if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
    272 			return sin6->sin6_scope_id & 0xffff;
    273 	}
    274 
    275 	return -1;
    276 }
    277 
    278 int
    279 in6_mask2len(mask)
    280 	struct in6_addr *mask;
    281 {
    282 	int x, y;
    283 
    284 	for (x = 0; x < sizeof(*mask); x++) {
    285 		if (mask->s6_addr8[x] != 0xff)
    286 			break;
    287 	}
    288 	y = 0;
    289 	if (x < sizeof(*mask)) {
    290 		for (y = 0; y < 8; y++) {
    291 			if ((mask->s6_addr8[x] & (0x80 >> y)) == 0)
    292 				break;
    293 		}
    294 	}
    295 	return x * 8 + y;
    296 }
    297 
    298 void
    299 in6_len2mask(mask, len)
    300 	struct in6_addr *mask;
    301 	int len;
    302 {
    303 	int i;
    304 
    305 	bzero(mask, sizeof(*mask));
    306 	for (i = 0; i < len / 8; i++)
    307 		mask->s6_addr8[i] = 0xff;
    308 	if (len % 8)
    309 		mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
    310 }
    311 
    312 #define ifa2ia6(ifa)	((struct in6_ifaddr *)(ifa))
    313 #define ia62ifa(ia6)	(&((ia6)->ia_ifa))
    314 
    315 int
    316 in6_control(so, cmd, data, ifp, p)
    317 	struct	socket *so;
    318 	u_long cmd;
    319 	caddr_t	data;
    320 	struct ifnet *ifp;
    321 	struct proc *p;
    322 {
    323 	struct	in6_ifreq *ifr = (struct in6_ifreq *)data;
    324 	struct	in6_ifaddr *ia, *oia;
    325 	struct	in6_aliasreq *ifra = (struct in6_aliasreq *)data;
    326 	struct	sockaddr_in6 oldaddr;
    327 #ifdef COMPAT_IN6IFIOCTL
    328 	struct sockaddr_in6 net;
    329 #endif
    330 	int	error = 0, hostIsNew, prefixIsNew;
    331 	time_t time_second = (time_t)time.tv_sec;
    332 	int privileged;
    333 
    334 	privileged = 0;
    335 	if (p && !suser(p->p_ucred, &p->p_acflag))
    336 		privileged++;
    337 
    338 	/*
    339 	 * xxx should prevent processes for link-local addresses?
    340 	 */
    341 #if NGIF > 0
    342 	if (ifp && ifp->if_type == IFT_GIF) {
    343 		switch (cmd) {
    344 		case SIOCSIFPHYADDR_IN6:
    345 			if (!privileged)
    346 				return(EPERM);
    347 			/*fall through*/
    348 		case SIOCGIFPSRCADDR_IN6:
    349 		case SIOCGIFPDSTADDR_IN6:
    350 			return gif_ioctl(ifp, cmd, data);
    351 		}
    352 	}
    353 #endif
    354 	switch (cmd) {
    355 	case SIOCGETSGCNT_IN6:
    356 	case SIOCGETMIFCNT_IN6:
    357 		return (mrt6_ioctl(cmd, data));
    358 	}
    359 
    360 	if (ifp == NULL)
    361 		return(EOPNOTSUPP);
    362 
    363 	switch (cmd) {
    364 	case SIOCSNDFLUSH_IN6:
    365 	case SIOCSPFXFLUSH_IN6:
    366 	case SIOCSRTRFLUSH_IN6:
    367 	case SIOCSDEFIFACE_IN6:
    368 		if (!privileged)
    369 			return(EPERM);
    370 		/*fall through*/
    371 	case SIOCGIFINFO_IN6:
    372 	case SIOCGDRLST_IN6:
    373 	case SIOCGPRLST_IN6:
    374 	case SIOCGNBRINFO_IN6:
    375 	case SIOCGDEFIFACE_IN6:
    376 		return(nd6_ioctl(cmd, data, ifp));
    377 	}
    378 
    379 	switch (cmd) {
    380 	case SIOCSIFPREFIX_IN6:
    381 	case SIOCDIFPREFIX_IN6:
    382 	case SIOCAIFPREFIX_IN6:
    383 	case SIOCCIFPREFIX_IN6:
    384 	case SIOCSGIFPREFIX_IN6:
    385 		if (!privileged)
    386 			return(EPERM);
    387 		/*fall through*/
    388 	case SIOCGIFPREFIX_IN6:
    389 		return(in6_prefix_ioctl(so, cmd, data, ifp));
    390 	}
    391 
    392 	switch (cmd) {
    393 	case SIOCALIFADDR:
    394 	case SIOCDLIFADDR:
    395 		if (!privileged)
    396 			return(EPERM);
    397 		/*fall through*/
    398 	case SIOCGLIFADDR:
    399 		return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
    400 	}
    401 
    402 	/*
    403 	 * Find address for this interface, if it exists.
    404 	 */
    405 	if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
    406 		struct sockaddr_in6 *sa6 =
    407 			(struct sockaddr_in6 *)&ifra->ifra_addr;
    408 
    409 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
    410 			if (sa6->sin6_addr.s6_addr16[1] == 0) {
    411 				/* interface ID is not embedded by the user */
    412 				sa6->sin6_addr.s6_addr16[1] =
    413 					htons(ifp->if_index);
    414 			}
    415 			else if (sa6->sin6_addr.s6_addr16[1] !=
    416 				    htons(ifp->if_index)) {
    417 				return(EINVAL);	/* ifid is contradict */
    418 			}
    419 			if (sa6->sin6_scope_id) {
    420 				if (sa6->sin6_scope_id !=
    421 				    (u_int32_t)ifp->if_index)
    422 					return(EINVAL);
    423 				sa6->sin6_scope_id = 0; /* XXX: good way? */
    424 			}
    425 		}
    426 		ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
    427 	}
    428 
    429 	switch (cmd) {
    430 
    431 	case SIOCDIFADDR_IN6:
    432 		/*
    433 		 * for IPv4, we look for existing in6_ifaddr here to allow
    434 		 * "ifconfig if0 delete" to remove first IPv4 address on the
    435 		 * interface.  For IPv6, as the spec allow multiple interface
    436 		 * address from the day one, we consider "remove the first one"
    437 		 * semantics to be not preferrable.
    438 		 */
    439 		if (ia == NULL)
    440 			return(EADDRNOTAVAIL);
    441 		/* FALLTHROUGH */
    442 	case SIOCAIFADDR_IN6:
    443 	case SIOCSIFADDR_IN6:
    444 #ifdef COMPAT_IN6IFIOCTL
    445 	case SIOCSIFDSTADDR_IN6:
    446 	case SIOCSIFNETMASK_IN6:
    447 		/*
    448 		 * Since IPv6 allows a node to assign multiple addresses
    449 		 * on a single interface, SIOCSIFxxx ioctls are not suitable
    450 		 * and should be unused.
    451 		 */
    452 #endif
    453 		if (ifra->ifra_addr.sin6_family != AF_INET6)
    454 			return(EAFNOSUPPORT);
    455 		if (!privileged)
    456 			return(EPERM);
    457 		if (ia == NULL) {
    458 			ia = (struct in6_ifaddr *)
    459 				malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
    460 			if (ia == NULL)
    461 				return (ENOBUFS);
    462 			bzero((caddr_t)ia, sizeof(*ia));
    463 			/* Initialize the address and masks */
    464 			ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
    465 			ia->ia_addr.sin6_family = AF_INET6;
    466 			ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
    467 			ia->ia_ifa.ifa_dstaddr
    468 				= (struct sockaddr *)&ia->ia_dstaddr;
    469 			if (ifp->if_flags & IFF_POINTOPOINT) {
    470 				ia->ia_dstaddr.sin6_family = AF_INET6;
    471 				ia->ia_dstaddr.sin6_len = sizeof(ia->ia_dstaddr);
    472 			}
    473 			ia->ia_ifa.ifa_netmask
    474 				= (struct sockaddr *)&ia->ia_prefixmask;
    475 
    476 			ia->ia_ifp = ifp;
    477 			if ((oia = in6_ifaddr) != NULL) {
    478 				for ( ; oia->ia_next; oia = oia->ia_next)
    479 					continue;
    480 				oia->ia_next = ia;
    481 			} else
    482 				in6_ifaddr = ia;
    483 			IFAREF(&ia->ia_ifa);
    484 
    485 			TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
    486 			    ifa_list);
    487 			IFAREF(&ia->ia_ifa);
    488 		}
    489 
    490 		if (cmd == SIOCAIFADDR_IN6) {
    491 			/* sanity for overflow - beware unsigned */
    492 			struct in6_addrlifetime *lt;
    493 			lt = &ifra->ifra_lifetime;
    494 			if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
    495 			 && lt->ia6t_vltime + time_second < time_second) {
    496 				return EINVAL;
    497 			}
    498 			if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
    499 			 && lt->ia6t_pltime + time_second < time_second) {
    500 				return EINVAL;
    501 			}
    502 		}
    503 		break;
    504 
    505 	case SIOCGIFADDR_IN6:
    506 		/* This interface is basically deprecated. use SIOCGIFCONF. */
    507 		/* fall through */
    508 	case SIOCGIFAFLAG_IN6:
    509 	case SIOCGIFNETMASK_IN6:
    510 	case SIOCGIFDSTADDR_IN6:
    511 	case SIOCGIFALIFETIME_IN6:
    512 		/* must think again about its semantics */
    513 		if (ia == NULL)
    514 			return(EADDRNOTAVAIL);
    515 		break;
    516 	case SIOCSIFALIFETIME_IN6:
    517 	    {
    518 		struct in6_addrlifetime *lt;
    519 
    520 		if (!privileged)
    521 			return(EPERM);
    522 		if (ia == NULL)
    523 			return(EADDRNOTAVAIL);
    524 		/* sanity for overflow - beware unsigned */
    525 		lt = &ifr->ifr_ifru.ifru_lifetime;
    526 		if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
    527 		 && lt->ia6t_vltime + time_second < time_second) {
    528 			return EINVAL;
    529 		}
    530 		if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
    531 		 && lt->ia6t_pltime + time_second < time_second) {
    532 			return EINVAL;
    533 		}
    534 		break;
    535 	    }
    536 	}
    537 
    538 	switch (cmd) {
    539 
    540 	case SIOCGIFADDR_IN6:
    541 		ifr->ifr_addr = ia->ia_addr;
    542 		break;
    543 
    544 	case SIOCGIFDSTADDR_IN6:
    545 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    546 			return(EINVAL);
    547 		ifr->ifr_dstaddr = ia->ia_dstaddr;
    548 		break;
    549 
    550 	case SIOCGIFNETMASK_IN6:
    551 		ifr->ifr_addr = ia->ia_prefixmask;
    552 		break;
    553 
    554 	case SIOCGIFAFLAG_IN6:
    555 		ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
    556 		break;
    557 
    558 	case SIOCGIFSTAT_IN6:
    559 		if (ifp == NULL)
    560 			return EINVAL;
    561 		if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
    562 		 || in6_ifstat[ifp->if_index] == NULL) {
    563 			/* return EAFNOSUPPORT? */
    564 			bzero(&ifr->ifr_ifru.ifru_stat,
    565 				sizeof(ifr->ifr_ifru.ifru_stat));
    566 		} else
    567 			ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
    568 		break;
    569 
    570 	case SIOCGIFSTAT_ICMP6:
    571 		if (ifp == NULL)
    572 			return EINVAL;
    573 		if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
    574 		    icmp6_ifstat[ifp->if_index] == NULL) {
    575 			/* return EAFNOSUPPORT? */
    576 			bzero(&ifr->ifr_ifru.ifru_stat,
    577 				sizeof(ifr->ifr_ifru.ifru_icmp6stat));
    578 		} else
    579 			ifr->ifr_ifru.ifru_icmp6stat =
    580 				*icmp6_ifstat[ifp->if_index];
    581 		break;
    582 
    583 #ifdef COMPAT_IN6IFIOCTL		/* should be unused */
    584 	case SIOCSIFDSTADDR_IN6:
    585 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    586 			return(EINVAL);
    587 		oldaddr = ia->ia_dstaddr;
    588 		ia->ia_dstaddr = ifr->ifr_dstaddr;
    589 
    590 		/* link-local index check */
    591 		if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
    592 			if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
    593 				/* interface ID is not embedded by the user */
    594 				ia->ia_dstaddr.sin6_addr.s6_addr16[1]
    595 					= htons(ifp->if_index);
    596 			}
    597 			else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
    598 				    htons(ifp->if_index)) {
    599 				ia->ia_dstaddr = oldaddr;
    600 				return(EINVAL);	/* ifid is contradict */
    601 			}
    602 		}
    603 
    604 		if (ifp->if_ioctl && (error = (ifp->if_ioctl)
    605 				      (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
    606 			ia->ia_dstaddr = oldaddr;
    607 			return(error);
    608 		}
    609 		if (ia->ia_flags & IFA_ROUTE) {
    610 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
    611 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    612 			ia->ia_ifa.ifa_dstaddr =
    613 				(struct sockaddr *)&ia->ia_dstaddr;
    614 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
    615 		}
    616 		break;
    617 
    618 #endif
    619 	case SIOCGIFALIFETIME_IN6:
    620 		ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
    621 		break;
    622 
    623 	case SIOCSIFALIFETIME_IN6:
    624 		ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
    625 		/* for sanity */
    626 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
    627 			ia->ia6_lifetime.ia6t_expire =
    628 				time_second + ia->ia6_lifetime.ia6t_vltime;
    629 		} else
    630 			ia->ia6_lifetime.ia6t_expire = 0;
    631 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
    632 			ia->ia6_lifetime.ia6t_preferred =
    633 				time_second + ia->ia6_lifetime.ia6t_pltime;
    634 		} else
    635 			ia->ia6_lifetime.ia6t_preferred = 0;
    636 		break;
    637 
    638 	case SIOCSIFADDR_IN6:
    639 		return(in6_ifinit(ifp, ia, &ifr->ifr_addr, 1));
    640 
    641 #ifdef COMPAT_IN6IFIOCTL		/* XXX should be unused */
    642 	case SIOCSIFNETMASK_IN6:
    643 		ia->ia_prefixmask = ifr->ifr_addr;
    644 		bzero(&net, sizeof(net));
    645 		net.sin6_len = sizeof(struct sockaddr_in6);
    646 		net.sin6_family = AF_INET6;
    647 		net.sin6_port = htons(0);
    648 		net.sin6_flowinfo = htonl(0);
    649 		net.sin6_addr.s6_addr32[0]
    650 			= ia->ia_addr.sin6_addr.s6_addr32[0] &
    651 				ia->ia_prefixmask.sin6_addr.s6_addr32[0];
    652 		net.sin6_addr.s6_addr32[1]
    653 			= ia->ia_addr.sin6_addr.s6_addr32[1] &
    654 				ia->ia_prefixmask.sin6_addr.s6_addr32[1];
    655 		net.sin6_addr.s6_addr32[2]
    656 			= ia->ia_addr.sin6_addr.s6_addr32[2] &
    657 				ia->ia_prefixmask.sin6_addr.s6_addr32[2];
    658 		net.sin6_addr.s6_addr32[3]
    659 			= ia->ia_addr.sin6_addr.s6_addr32[3] &
    660 				ia->ia_prefixmask.sin6_addr.s6_addr32[3];
    661 		ia->ia_net = net;
    662 		break;
    663 #endif
    664 
    665 	case SIOCAIFADDR_IN6:
    666 		prefixIsNew = 0;
    667 		hostIsNew = 1;
    668 
    669 		if (ifra->ifra_addr.sin6_len == 0) {
    670 			ifra->ifra_addr = ia->ia_addr;
    671 			hostIsNew = 0;
    672 		} else if (IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr,
    673 					      &ia->ia_addr.sin6_addr))
    674 			hostIsNew = 0;
    675 
    676 		/* Validate address families: */
    677 		/*
    678 		 * The destination address for a p2p link must have a family
    679 		 * of AF_UNSPEC or AF_INET6.
    680 		 */
    681 		if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
    682 		    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
    683 		    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
    684 			return(EAFNOSUPPORT);
    685 		/*
    686 		 * The prefixmask must have a family of AF_UNSPEC or AF_INET6.
    687 		 */
    688 		if (ifra->ifra_prefixmask.sin6_family != AF_INET6 &&
    689 		    ifra->ifra_prefixmask.sin6_family != AF_UNSPEC)
    690 			return(EAFNOSUPPORT);
    691 
    692 		if (ifra->ifra_prefixmask.sin6_len) {
    693 			in6_ifscrub(ifp, ia);
    694 			ia->ia_prefixmask = ifra->ifra_prefixmask;
    695 			prefixIsNew = 1;
    696 		}
    697 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    698 		    (ifra->ifra_dstaddr.sin6_family == AF_INET6)) {
    699 			in6_ifscrub(ifp, ia);
    700 			oldaddr = ia->ia_dstaddr;
    701 			ia->ia_dstaddr = ifra->ifra_dstaddr;
    702 			/* link-local index check: should be a separate function? */
    703 			if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
    704 				if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
    705 					/*
    706 					 * interface ID is not embedded by
    707 					 * the user
    708 					 */
    709 					ia->ia_dstaddr.sin6_addr.s6_addr16[1]
    710 						= htons(ifp->if_index);
    711 				} else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
    712 					    htons(ifp->if_index)) {
    713 					ia->ia_dstaddr = oldaddr;
    714 					return(EINVAL);	/* ifid is contradict */
    715 				}
    716 			}
    717 			prefixIsNew = 1; /* We lie; but effect's the same */
    718 		}
    719 		if (hostIsNew || prefixIsNew)
    720 			error = in6_ifinit(ifp, ia, &ifra->ifra_addr, 0);
    721 		if (hostIsNew && (ifp->if_flags & IFF_MULTICAST)) {
    722 			int error_local = 0;
    723 
    724 			/*
    725 			 * join solicited multicast addr for new host id
    726 			 */
    727 			struct in6_addr llsol;
    728 			bzero(&llsol, sizeof(struct in6_addr));
    729 			llsol.s6_addr16[0] = htons(0xff02);
    730 			llsol.s6_addr16[1] = htons(ifp->if_index);
    731 			llsol.s6_addr32[1] = 0;
    732 			llsol.s6_addr32[2] = htonl(1);
    733 			llsol.s6_addr32[3] =
    734 				ifra->ifra_addr.sin6_addr.s6_addr32[3];
    735 			llsol.s6_addr8[12] = 0xff;
    736 			(void)in6_addmulti(&llsol, ifp, &error_local);
    737 			if (error == 0)
    738 				error = error_local;
    739 		}
    740 
    741 		ia->ia6_flags = ifra->ifra_flags;
    742 		ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/*safety*/
    743 
    744 		ia->ia6_lifetime = ifra->ifra_lifetime;
    745 		/* for sanity */
    746 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
    747 			ia->ia6_lifetime.ia6t_expire =
    748 				time_second + ia->ia6_lifetime.ia6t_vltime;
    749 		} else
    750 			ia->ia6_lifetime.ia6t_expire = 0;
    751 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
    752 			ia->ia6_lifetime.ia6t_preferred =
    753 				time_second + ia->ia6_lifetime.ia6t_pltime;
    754 		} else
    755 			ia->ia6_lifetime.ia6t_preferred = 0;
    756 
    757 		/*
    758 		 * Perform DAD, if needed.
    759 		 * XXX It may be of use, if we can administratively
    760 		 * disable DAD.
    761 		 */
    762 		switch (ifp->if_type) {
    763 		case IFT_ARCNET:
    764 		case IFT_ETHER:
    765 		case IFT_FDDI:
    766 #if 0
    767 		case IFT_ATM:
    768 		case IFT_SLIP:
    769 		case IFT_PPP:
    770 #endif
    771 			ia->ia6_flags |= IN6_IFF_TENTATIVE;
    772 			nd6_dad_start(&ia->ia_ifa, NULL);
    773 			break;
    774 		case IFT_FAITH:
    775 		case IFT_GIF:
    776 		case IFT_LOOP:
    777 		default:
    778 			break;
    779 		}
    780 
    781 		if (hostIsNew) {
    782 			int iilen;
    783 			int error_local = 0;
    784 
    785 			iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) -
    786 				in6_mask2len(&ia->ia_prefixmask.sin6_addr);
    787 			error_local = in6_prefix_add_ifid(iilen, ia);
    788 			if (error == 0)
    789 				error = error_local;
    790 		}
    791 
    792 		return(error);
    793 
    794 	case SIOCDIFADDR_IN6:
    795 		in6_purgeaddr(&ia->ia_ifa, ifp);
    796 		break;
    797 
    798 	default:
    799 		if (ifp == NULL || ifp->if_ioctl == 0)
    800 			return(EOPNOTSUPP);
    801 		return((*ifp->if_ioctl)(ifp, cmd, data));
    802 	}
    803 	return(0);
    804 }
    805 
    806 void
    807 in6_purgeaddr(ifa, ifp)
    808 	struct ifaddr *ifa;
    809 	struct ifnet *ifp;
    810 {
    811 	struct in6_ifaddr *oia, *ia = (void *) ifa;
    812 
    813 	in6_ifscrub(ifp, ia);
    814 
    815 	if (ifp->if_flags & IFF_MULTICAST) {
    816 		/*
    817 		 * delete solicited multicast addr for deleting host id
    818 		 */
    819 		struct in6_multi *in6m;
    820 		struct in6_addr llsol;
    821 		bzero(&llsol, sizeof(struct in6_addr));
    822 		llsol.s6_addr16[0] = htons(0xff02);
    823 		llsol.s6_addr16[1] = htons(ifp->if_index);
    824 		llsol.s6_addr32[1] = 0;
    825 		llsol.s6_addr32[2] = htonl(1);
    826 		llsol.s6_addr32[3] =
    827 			ia->ia_addr.sin6_addr.s6_addr32[3];
    828 		llsol.s6_addr8[12] = 0xff;
    829 
    830 		IN6_LOOKUP_MULTI(llsol, ifp, in6m);
    831 		if (in6m)
    832 			in6_delmulti(in6m);
    833 	}
    834 
    835 	TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
    836 	IFAFREE(&ia->ia_ifa);
    837 
    838 	oia = ia;
    839 	if (oia == (ia = in6_ifaddr))
    840 		in6_ifaddr = ia->ia_next;
    841 	else {
    842 		while (ia->ia_next && (ia->ia_next != oia))
    843 			ia = ia->ia_next;
    844 		if (ia->ia_next)
    845 			ia->ia_next = oia->ia_next;
    846 		else
    847 			printf("Didn't unlink in6_ifaddr from list\n");
    848 	}
    849 	{
    850 		int iilen;
    851 
    852 		iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) -
    853 			in6_mask2len(&oia->ia_prefixmask.sin6_addr);
    854 		in6_prefix_remove_ifid(iilen, oia);
    855 	}
    856 	if (oia->ia6_multiaddrs.lh_first != NULL) {
    857 		/*
    858 		 * XXX thorpej (at) netbsd.org -- if the interface is going
    859 		 * XXX away, don't save the multicast entries, delete them!
    860 		 */
    861 		if (oia->ia_ifa.ifa_ifp->if_output == if_nulloutput) {
    862 			struct in6_multi *in6m;
    863 
    864 			while ((in6m =
    865 			    LIST_FIRST(&oia->ia6_multiaddrs)) != NULL)
    866 				in6_delmulti(in6m);
    867 		} else
    868 			in6_savemkludge(oia);
    869 	}
    870 
    871 	IFAFREE(&oia->ia_ifa);
    872 }
    873 
    874 void
    875 in6_purgeif(ifp)
    876 	struct ifnet *ifp;
    877 {
    878 	struct ifaddr *ifa, *nifa;
    879 
    880 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
    881 		nifa = TAILQ_NEXT(ifa, ifa_list);
    882 		if (ifa->ifa_addr->sa_family != AF_INET6)
    883 			continue;
    884 		in6_purgeaddr(ifa, ifp);
    885 	}
    886 
    887 	in6_ifdetach(ifp);
    888 }
    889 
    890 /*
    891  * SIOC[GAD]LIFADDR.
    892  *	SIOCGLIFADDR: get first address. (???)
    893  *	SIOCGLIFADDR with IFLR_PREFIX:
    894  *		get first address that matches the specified prefix.
    895  *	SIOCALIFADDR: add the specified address.
    896  *	SIOCALIFADDR with IFLR_PREFIX:
    897  *		add the specified prefix, filling hostid part from
    898  *		the first link-local address.  prefixlen must be <= 64.
    899  *	SIOCDLIFADDR: delete the specified address.
    900  *	SIOCDLIFADDR with IFLR_PREFIX:
    901  *		delete the first address that matches the specified prefix.
    902  * return values:
    903  *	EINVAL on invalid parameters
    904  *	EADDRNOTAVAIL on prefix match failed/specified address not found
    905  *	other values may be returned from in6_ioctl()
    906  *
    907  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
    908  * this is to accomodate address naming scheme other than RFC2374,
    909  * in the future.
    910  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
    911  * address encoding scheme. (see figure on page 8)
    912  */
    913 static int
    914 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
    915 	struct socket *so;
    916 	u_long cmd;
    917 	caddr_t	data;
    918 	struct ifnet *ifp;
    919 	struct proc *p;
    920 {
    921 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
    922 	struct ifaddr *ifa;
    923 	struct sockaddr *sa;
    924 
    925 	/* sanity checks */
    926 	if (!data || !ifp) {
    927 		panic("invalid argument to in6_lifaddr_ioctl");
    928 		/*NOTRECHED*/
    929 	}
    930 
    931 	switch (cmd) {
    932 	case SIOCGLIFADDR:
    933 		/* address must be specified on GET with IFLR_PREFIX */
    934 		if ((iflr->flags & IFLR_PREFIX) == 0)
    935 			break;
    936 		/*FALLTHROUGH*/
    937 	case SIOCALIFADDR:
    938 	case SIOCDLIFADDR:
    939 		/* address must be specified on ADD and DELETE */
    940 		sa = (struct sockaddr *)&iflr->addr;
    941 		if (sa->sa_family != AF_INET6)
    942 			return EINVAL;
    943 		if (sa->sa_len != sizeof(struct sockaddr_in6))
    944 			return EINVAL;
    945 		/* XXX need improvement */
    946 		sa = (struct sockaddr *)&iflr->dstaddr;
    947 		if (sa->sa_family && sa->sa_family != AF_INET6)
    948 			return EINVAL;
    949 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
    950 			return EINVAL;
    951 		break;
    952 	default: /*shouldn't happen*/
    953 #if 0
    954 		panic("invalid cmd to in6_lifaddr_ioctl");
    955 		/*NOTREACHED*/
    956 #else
    957 		return EOPNOTSUPP;
    958 #endif
    959 	}
    960 	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
    961 		return EINVAL;
    962 
    963 	switch (cmd) {
    964 	case SIOCALIFADDR:
    965 	    {
    966 		struct in6_aliasreq ifra;
    967 		struct in6_addr *hostid = NULL;
    968 		int prefixlen;
    969 
    970 		if ((iflr->flags & IFLR_PREFIX) != 0) {
    971 			struct sockaddr_in6 *sin6;
    972 
    973 			/*
    974 			 * hostid is to fill in the hostid part of the
    975 			 * address.  hostid points to the first link-local
    976 			 * address attached to the interface.
    977 			 */
    978 			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
    979 			if (!ifa)
    980 				return EADDRNOTAVAIL;
    981 			hostid = IFA_IN6(ifa);
    982 
    983 		 	/* prefixlen must be <= 64. */
    984 			if (64 < iflr->prefixlen)
    985 				return EINVAL;
    986 			prefixlen = iflr->prefixlen;
    987 
    988 			/* hostid part must be zero. */
    989 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
    990 			if (sin6->sin6_addr.s6_addr32[2] != 0
    991 			 || sin6->sin6_addr.s6_addr32[3] != 0) {
    992 				return EINVAL;
    993 			}
    994 		} else
    995 			prefixlen = iflr->prefixlen;
    996 
    997 		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
    998 		bzero(&ifra, sizeof(ifra));
    999 		bcopy(iflr->iflr_name, ifra.ifra_name,
   1000 			sizeof(ifra.ifra_name));
   1001 
   1002 		bcopy(&iflr->addr, &ifra.ifra_addr,
   1003 			((struct sockaddr *)&iflr->addr)->sa_len);
   1004 		if (hostid) {
   1005 			/* fill in hostid part */
   1006 			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
   1007 				hostid->s6_addr32[2];
   1008 			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
   1009 				hostid->s6_addr32[3];
   1010 		}
   1011 
   1012 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
   1013 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
   1014 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
   1015 			if (hostid) {
   1016 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
   1017 					hostid->s6_addr32[2];
   1018 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
   1019 					hostid->s6_addr32[3];
   1020 			}
   1021 		}
   1022 
   1023 		ifra.ifra_prefixmask.sin6_family = AF_INET6;
   1024 		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
   1025 		in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
   1026 
   1027 		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
   1028 		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
   1029 	    }
   1030 	case SIOCGLIFADDR:
   1031 	case SIOCDLIFADDR:
   1032 	    {
   1033 		struct in6_ifaddr *ia;
   1034 		struct in6_addr mask, candidate, match;
   1035 		struct sockaddr_in6 *sin6;
   1036 		int cmp;
   1037 
   1038 		bzero(&mask, sizeof(mask));
   1039 		if (iflr->flags & IFLR_PREFIX) {
   1040 			/* lookup a prefix rather than address. */
   1041 			in6_len2mask(&mask, iflr->prefixlen);
   1042 
   1043 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1044 			bcopy(&sin6->sin6_addr, &match, sizeof(match));
   1045 			match.s6_addr32[0] &= mask.s6_addr32[0];
   1046 			match.s6_addr32[1] &= mask.s6_addr32[1];
   1047 			match.s6_addr32[2] &= mask.s6_addr32[2];
   1048 			match.s6_addr32[3] &= mask.s6_addr32[3];
   1049 
   1050 			/* if you set extra bits, that's wrong */
   1051 			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
   1052 				return EINVAL;
   1053 
   1054 			cmp = 1;
   1055 		} else {
   1056 			if (cmd == SIOCGLIFADDR) {
   1057 				/* on getting an address, take the 1st match */
   1058 				cmp = 0;	/*XXX*/
   1059 			} else {
   1060 				/* on deleting an address, do exact match */
   1061 				in6_len2mask(&mask, 128);
   1062 				sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1063 				bcopy(&sin6->sin6_addr, &match, sizeof(match));
   1064 
   1065 				cmp = 1;
   1066 			}
   1067 		}
   1068 
   1069 		for (ifa = ifp->if_addrlist.tqh_first;
   1070 		     ifa;
   1071 		     ifa = ifa->ifa_list.tqe_next)
   1072 		{
   1073 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1074 				continue;
   1075 			if (!cmp)
   1076 				break;
   1077 			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
   1078 			candidate.s6_addr32[0] &= mask.s6_addr32[0];
   1079 			candidate.s6_addr32[1] &= mask.s6_addr32[1];
   1080 			candidate.s6_addr32[2] &= mask.s6_addr32[2];
   1081 			candidate.s6_addr32[3] &= mask.s6_addr32[3];
   1082 			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
   1083 				break;
   1084 		}
   1085 		if (!ifa)
   1086 			return EADDRNOTAVAIL;
   1087 		ia = ifa2ia6(ifa);
   1088 
   1089 		if (cmd == SIOCGLIFADDR) {
   1090 			/* fill in the if_laddrreq structure */
   1091 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
   1092 
   1093 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1094 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
   1095 					ia->ia_dstaddr.sin6_len);
   1096 			} else
   1097 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
   1098 
   1099 			iflr->prefixlen =
   1100 				in6_mask2len(&ia->ia_prefixmask.sin6_addr);
   1101 
   1102 			iflr->flags = ia->ia6_flags;	/*XXX*/
   1103 
   1104 			return 0;
   1105 		} else {
   1106 			struct in6_aliasreq ifra;
   1107 
   1108 			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
   1109 			bzero(&ifra, sizeof(ifra));
   1110 			bcopy(iflr->iflr_name, ifra.ifra_name,
   1111 				sizeof(ifra.ifra_name));
   1112 
   1113 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
   1114 				ia->ia_addr.sin6_len);
   1115 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1116 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
   1117 					ia->ia_dstaddr.sin6_len);
   1118 			}
   1119 			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
   1120 				ia->ia_prefixmask.sin6_len);
   1121 
   1122 			ifra.ifra_flags = ia->ia6_flags;
   1123 			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
   1124 				ifp, p);
   1125 		}
   1126 	    }
   1127 	}
   1128 
   1129 	return EOPNOTSUPP;	/*just for safety*/
   1130 }
   1131 
   1132 /*
   1133  * Delete any existing route for an interface.
   1134  */
   1135 void
   1136 in6_ifscrub(ifp, ia)
   1137 	register struct ifnet *ifp;
   1138 	register struct in6_ifaddr *ia;
   1139 {
   1140 	if ((ia->ia_flags & IFA_ROUTE) == 0)
   1141 		return;
   1142 	if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
   1143 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
   1144 	else
   1145 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
   1146 	ia->ia_flags &= ~IFA_ROUTE;
   1147 
   1148 	/* Remove ownaddr's loopback rtentry, if it exists. */
   1149 	in6_ifremloop(&(ia->ia_ifa));
   1150 }
   1151 
   1152 /*
   1153  * Initialize an interface's intetnet6 address
   1154  * and routing table entry.
   1155  */
   1156 int
   1157 in6_ifinit(ifp, ia, sin6, scrub)
   1158 	struct ifnet *ifp;
   1159 	struct in6_ifaddr *ia;
   1160 	struct sockaddr_in6 *sin6;
   1161 	int scrub;
   1162 {
   1163 	struct	sockaddr_in6 oldaddr;
   1164 	int	error, flags = RTF_UP;
   1165 	int	s = splimp();
   1166 
   1167 	oldaddr = ia->ia_addr;
   1168 	ia->ia_addr = *sin6;
   1169 	/*
   1170 	 * Give the interface a chance to initialize
   1171 	 * if this is its first address,
   1172 	 * and to validate the address if necessary.
   1173 	 */
   1174 	if (ifp->if_ioctl &&
   1175 	   (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
   1176 		splx(s);
   1177 		ia->ia_addr = oldaddr;
   1178 		return(error);
   1179 	}
   1180 
   1181 	switch (ifp->if_type) {
   1182 	case IFT_ARCNET:
   1183 	case IFT_ETHER:
   1184 	case IFT_FDDI:
   1185 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
   1186 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
   1187 		break;
   1188 	case IFT_PPP:
   1189 		ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest;
   1190 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
   1191 		break;
   1192 	}
   1193 
   1194 	splx(s);
   1195 	if (scrub) {
   1196 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
   1197 		in6_ifscrub(ifp, ia);
   1198 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
   1199 	}
   1200 	/* xxx
   1201 	 * in_socktrim
   1202 	 */
   1203 	/*
   1204 	 * Add route for the network.
   1205 	 */
   1206 	ia->ia_ifa.ifa_metric = ifp->if_metric;
   1207 	if (ifp->if_flags & IFF_LOOPBACK) {
   1208 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
   1209 		flags |= RTF_HOST;
   1210 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
   1211 		if (ia->ia_dstaddr.sin6_family != AF_INET6)
   1212 			return(0);
   1213 		flags |= RTF_HOST;
   1214 	}
   1215 	if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
   1216 		ia->ia_flags |= IFA_ROUTE;
   1217 
   1218 	/* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
   1219 	in6_ifaddloop(&(ia->ia_ifa));
   1220 
   1221 	if (ifp->if_flags & IFF_MULTICAST)
   1222 		in6_restoremkludge(ia, ifp);
   1223 
   1224 	return(error);
   1225 }
   1226 
   1227 /*
   1228  * Multicast address kludge:
   1229  * If there were any multicast addresses attached to this interface address,
   1230  * either move them to another address on this interface, or save them until
   1231  * such time as this interface is reconfigured for IPv6.
   1232  */
   1233 void
   1234 in6_savemkludge(oia)
   1235 	struct in6_ifaddr *oia;
   1236 {
   1237 	struct in6_ifaddr *ia;
   1238 	struct in6_multi *in6m, *next;
   1239 
   1240 	IFP_TO_IA6(oia->ia_ifp, ia);
   1241 	if (ia) {	/* there is another address */
   1242 		for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
   1243 			next = in6m->in6m_entry.le_next;
   1244 			IFAFREE(&in6m->in6m_ia->ia_ifa);
   1245 			IFAREF(&ia->ia_ifa);
   1246 			in6m->in6m_ia = ia;
   1247 			LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
   1248 		}
   1249 	} else {	/* last address on this if deleted, save */
   1250 		struct multi6_kludge *mk;
   1251 
   1252 		mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
   1253 
   1254 		LIST_INIT(&mk->mk_head);
   1255 		mk->mk_ifp = oia->ia_ifp;
   1256 
   1257 		for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
   1258 			next = in6m->in6m_entry.le_next;
   1259 			IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
   1260 			in6m->in6m_ia = NULL;
   1261 			LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
   1262 		}
   1263 
   1264 		if (mk->mk_head.lh_first != NULL) {
   1265 			LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
   1266 		}
   1267 		else {
   1268 			FREE(mk, M_IPMADDR);
   1269 		}
   1270 	}
   1271 }
   1272 
   1273 /*
   1274  * Continuation of multicast address hack:
   1275  * If there was a multicast group list previously saved for this interface,
   1276  * then we re-attach it to the first address configured on the i/f.
   1277  */
   1278 void
   1279 in6_restoremkludge(ia, ifp)
   1280 	struct in6_ifaddr *ia;
   1281 	struct ifnet *ifp;
   1282 {
   1283 	struct multi6_kludge *mk;
   1284 
   1285 	for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
   1286 		if (mk->mk_ifp == ifp) {
   1287 			struct in6_multi *in6m, *next;
   1288 
   1289 			for (in6m = mk->mk_head.lh_first; in6m; in6m = next){
   1290 				next = in6m->in6m_entry.le_next;
   1291 				in6m->in6m_ia = ia;
   1292 				IFAREF(&ia->ia_ifa);	/* gain a reference */
   1293 				LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
   1294 						 in6m, in6m_entry);
   1295 			}
   1296 			LIST_REMOVE(mk, mk_entry);
   1297 			free(mk, M_IPMADDR);
   1298 			break;
   1299 		}
   1300 	}
   1301 }
   1302 
   1303 void
   1304 in6_purgemkludge(ifp)
   1305 	struct ifnet *ifp;
   1306 {
   1307 	struct multi6_kludge *mk;
   1308 	struct in6_multi *in6m;
   1309 
   1310 	for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
   1311 		if (mk->mk_ifp != ifp)
   1312 			continue;
   1313 
   1314 		/* leave from all multicast groups joined */
   1315 		while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL)
   1316 			in6_delmulti(in6m);
   1317 		LIST_REMOVE(mk, mk_entry);
   1318 		free(mk, M_IPMADDR);
   1319 		break;
   1320 	}
   1321 }
   1322 
   1323 /*
   1324  * Add an address to the list of IP6 multicast addresses for a
   1325  * given interface.
   1326  */
   1327 struct	in6_multi *
   1328 in6_addmulti(maddr6, ifp, errorp)
   1329 	register struct in6_addr *maddr6;
   1330 	register struct ifnet *ifp;
   1331 	int *errorp;
   1332 {
   1333 	struct	in6_ifaddr *ia;
   1334 	struct	in6_ifreq ifr;
   1335 	struct	in6_multi *in6m;
   1336 	int	s = splsoftnet();
   1337 
   1338 	*errorp = 0;
   1339 	/*
   1340 	 * See if address already in list.
   1341 	 */
   1342 	IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
   1343 	if (in6m != NULL) {
   1344 		/*
   1345 		 * Found it; just increment the refrence count.
   1346 		 */
   1347 		in6m->in6m_refcount++;
   1348 	} else {
   1349 		/*
   1350 		 * New address; allocate a new multicast record
   1351 		 * and link it into the interface's multicast list.
   1352 		 */
   1353 		in6m = (struct in6_multi *)
   1354 			malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
   1355 		if (in6m == NULL) {
   1356 			splx(s);
   1357 			*errorp = ENOBUFS;
   1358 			return(NULL);
   1359 		}
   1360 		in6m->in6m_addr = *maddr6;
   1361 		in6m->in6m_ifp = ifp;
   1362 		in6m->in6m_refcount = 1;
   1363 		IFP_TO_IA6(ifp, ia);
   1364 		if (ia == NULL) {
   1365 			free(in6m, M_IPMADDR);
   1366 			splx(s);
   1367 			*errorp = EADDRNOTAVAIL; /* appropriate? */
   1368 			return(NULL);
   1369 		}
   1370 		in6m->in6m_ia = ia;
   1371 		IFAREF(&ia->ia_ifa);	/* gain a reference */
   1372 		LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
   1373 
   1374 		/*
   1375 		 * Ask the network driver to update its multicast reception
   1376 		 * filter appropriately for the new address.
   1377 		 */
   1378 		bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
   1379 		ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
   1380 		ifr.ifr_addr.sin6_family = AF_INET6;
   1381 		ifr.ifr_addr.sin6_addr = *maddr6;
   1382 		if (ifp->if_ioctl == NULL)
   1383 			*errorp = ENXIO; /* XXX: appropriate? */
   1384 		else
   1385 			*errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
   1386 						    (caddr_t)&ifr);
   1387 		if (*errorp) {
   1388 			LIST_REMOVE(in6m, in6m_entry);
   1389 			free(in6m, M_IPMADDR);
   1390 			splx(s);
   1391 			return(NULL);
   1392 		}
   1393 		/*
   1394 		 * Let MLD6 know that we have joined a new IP6 multicast
   1395 		 * group.
   1396 		 */
   1397 		mld6_start_listening(in6m);
   1398 	}
   1399 	splx(s);
   1400 	return(in6m);
   1401 }
   1402 
   1403 /*
   1404  * Delete a multicast address record.
   1405  */
   1406 void
   1407 in6_delmulti(in6m)
   1408 	struct in6_multi *in6m;
   1409 {
   1410 	struct	in6_ifreq ifr;
   1411 	int	s = splsoftnet();
   1412 
   1413 	if (--in6m->in6m_refcount == 0) {
   1414 		/*
   1415 		 * No remaining claims to this record; let MLD6 know
   1416 		 * that we are leaving the multicast group.
   1417 		 */
   1418 		mld6_stop_listening(in6m);
   1419 
   1420 		/*
   1421 		 * Unlink from list.
   1422 		 */
   1423 		LIST_REMOVE(in6m, in6m_entry);
   1424 		if (in6m->in6m_ia)
   1425 			IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
   1426 
   1427 		/*
   1428 		 * Notify the network driver to update its multicast
   1429 		 * reception filter.
   1430 		 */
   1431 		bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
   1432 		ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
   1433 		ifr.ifr_addr.sin6_family = AF_INET6;
   1434 		ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
   1435 		(*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
   1436 					    SIOCDELMULTI, (caddr_t)&ifr);
   1437 		free(in6m, M_IPMADDR);
   1438 	}
   1439 	splx(s);
   1440 }
   1441 
   1442 /*
   1443  * Find an IPv6 interface link-local address specific to an interface.
   1444  */
   1445 struct in6_ifaddr *
   1446 in6ifa_ifpforlinklocal(ifp, ignoreflags)
   1447 	struct ifnet *ifp;
   1448 	int ignoreflags;
   1449 {
   1450 	register struct ifaddr *ifa;
   1451 
   1452 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1453 	{
   1454 		if (ifa->ifa_addr == NULL)
   1455 			continue;	/* just for safety */
   1456 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1457 			continue;
   1458 		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
   1459 			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
   1460 			     ignoreflags) != 0)
   1461 				continue;
   1462 			break;
   1463 		}
   1464 	}
   1465 
   1466 	return((struct in6_ifaddr *)ifa);
   1467 }
   1468 
   1469 
   1470 /*
   1471  * find the internet address corresponding to a given interface and address.
   1472  */
   1473 struct in6_ifaddr *
   1474 in6ifa_ifpwithaddr(ifp, addr)
   1475 	struct ifnet *ifp;
   1476 	struct in6_addr *addr;
   1477 {
   1478 	register struct ifaddr *ifa;
   1479 
   1480 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1481 	{
   1482 		if (ifa->ifa_addr == NULL)
   1483 			continue;	/* just for safety */
   1484 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1485 			continue;
   1486 		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
   1487 			break;
   1488 	}
   1489 
   1490 	return((struct in6_ifaddr *)ifa);
   1491 }
   1492 
   1493 /*
   1494  * Convert IP6 address to printable (loggable) representation.
   1495  */
   1496 static char digits[] = "0123456789abcdef";
   1497 static int ip6round = 0;
   1498 char *
   1499 ip6_sprintf(addr)
   1500 register struct in6_addr *addr;
   1501 {
   1502 	static char ip6buf[8][48];
   1503 	register int i;
   1504 	register char *cp;
   1505 	register u_short *a = (u_short *)addr;
   1506 	register u_char *d;
   1507 	int dcolon = 0;
   1508 
   1509 	ip6round = (ip6round + 1) & 7;
   1510 	cp = ip6buf[ip6round];
   1511 
   1512 	for (i = 0; i < 8; i++) {
   1513 		if (dcolon == 1) {
   1514 			if (*a == 0) {
   1515 				if (i == 7)
   1516 					*cp++ = ':';
   1517 				a++;
   1518 				continue;
   1519 			} else
   1520 				dcolon = 2;
   1521 		}
   1522 		if (*a == 0) {
   1523 			if (dcolon == 0 && *(a + 1) == 0) {
   1524 				if (i == 0)
   1525 					*cp++ = ':';
   1526 				*cp++ = ':';
   1527 				dcolon = 1;
   1528 			} else {
   1529 				*cp++ = '0';
   1530 				*cp++ = ':';
   1531 			}
   1532 			a++;
   1533 			continue;
   1534 		}
   1535 		d = (u_char *)a;
   1536 		*cp++ = digits[*d >> 4];
   1537 		*cp++ = digits[*d++ & 0xf];
   1538 		*cp++ = digits[*d >> 4];
   1539 		*cp++ = digits[*d & 0xf];
   1540 		*cp++ = ':';
   1541 		a++;
   1542 	}
   1543 	*--cp = 0;
   1544 	return(ip6buf[ip6round]);
   1545 }
   1546 
   1547 int
   1548 in6_localaddr(in6)
   1549 	struct in6_addr *in6;
   1550 {
   1551 	struct in6_ifaddr *ia;
   1552 
   1553 	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
   1554 		return 1;
   1555 
   1556 	for (ia = in6_ifaddr; ia; ia = ia->ia_next)
   1557 		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
   1558 					      &ia->ia_prefixmask.sin6_addr))
   1559 			return 1;
   1560 
   1561 	return (0);
   1562 }
   1563 
   1564 /*
   1565  * Get a scope of the address. Node-local, link-local, site-local or global.
   1566  */
   1567 int
   1568 in6_addrscope (addr)
   1569 struct in6_addr *addr;
   1570 {
   1571 	int scope;
   1572 
   1573 	if (addr->s6_addr8[0] == 0xfe) {
   1574 		scope = addr->s6_addr8[1] & 0xc0;
   1575 
   1576 		switch (scope) {
   1577 		case 0x80:
   1578 			return IPV6_ADDR_SCOPE_LINKLOCAL;
   1579 			break;
   1580 		case 0xc0:
   1581 			return IPV6_ADDR_SCOPE_SITELOCAL;
   1582 			break;
   1583 		default:
   1584 			return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
   1585 			break;
   1586 		}
   1587 	}
   1588 
   1589 
   1590 	if (addr->s6_addr8[0] == 0xff) {
   1591 		scope = addr->s6_addr8[1] & 0x0f;
   1592 
   1593 		/*
   1594 		 * due to other scope such as reserved,
   1595 		 * return scope doesn't work.
   1596 		 */
   1597 		switch (scope) {
   1598 		case IPV6_ADDR_SCOPE_NODELOCAL:
   1599 			return IPV6_ADDR_SCOPE_NODELOCAL;
   1600 			break;
   1601 		case IPV6_ADDR_SCOPE_LINKLOCAL:
   1602 			return IPV6_ADDR_SCOPE_LINKLOCAL;
   1603 			break;
   1604 		case IPV6_ADDR_SCOPE_SITELOCAL:
   1605 			return IPV6_ADDR_SCOPE_SITELOCAL;
   1606 			break;
   1607 		default:
   1608 			return IPV6_ADDR_SCOPE_GLOBAL;
   1609 			break;
   1610 		}
   1611 	}
   1612 
   1613 	if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) {
   1614 		if (addr->s6_addr8[15] == 1) /* loopback */
   1615 			return IPV6_ADDR_SCOPE_NODELOCAL;
   1616 		if (addr->s6_addr8[15] == 0) /* unspecified */
   1617 			return IPV6_ADDR_SCOPE_LINKLOCAL;
   1618 	}
   1619 
   1620 	return IPV6_ADDR_SCOPE_GLOBAL;
   1621 }
   1622 
   1623 int
   1624 in6_addr2scopeid(ifp, addr)
   1625 	struct ifnet *ifp;	/* must not be NULL */
   1626 	struct in6_addr *addr;	/* must not be NULL */
   1627 {
   1628 	int scope = in6_addrscope(addr);
   1629 
   1630 	switch(scope) {
   1631 	case IPV6_ADDR_SCOPE_NODELOCAL:
   1632 		return(-1);	/* XXX: is this an appropriate value? */
   1633 
   1634 	case IPV6_ADDR_SCOPE_LINKLOCAL:
   1635 		/* XXX: we do not distinguish between a link and an I/F. */
   1636 		return(ifp->if_index);
   1637 
   1638 	case IPV6_ADDR_SCOPE_SITELOCAL:
   1639 		return(0);	/* XXX: invalid. */
   1640 
   1641 	default:
   1642 		return(0);	/* XXX: treat as global. */
   1643 	}
   1644 }
   1645 
   1646 /*
   1647  * return length of part which dst and src are equal
   1648  * hard coding...
   1649  */
   1650 
   1651 int
   1652 in6_matchlen(src, dst)
   1653 struct in6_addr *src, *dst;
   1654 {
   1655 	int match = 0;
   1656 	u_char *s = (u_char *)src, *d = (u_char *)dst;
   1657 	u_char *lim = s + 16, r;
   1658 
   1659 	while (s < lim)
   1660 		if ((r = (*d++ ^ *s++)) != 0) {
   1661 			while (r < 128) {
   1662 				match++;
   1663 				r <<= 1;
   1664 			}
   1665 			break;
   1666 		} else
   1667 			match += 8;
   1668 	return match;
   1669 }
   1670 
   1671 int
   1672 in6_are_prefix_equal(p1, p2, len)
   1673 	struct in6_addr *p1, *p2;
   1674 	int len;
   1675 {
   1676 	int bytelen, bitlen;
   1677 
   1678 	/* sanity check */
   1679 	if (0 > len || len > 128) {
   1680 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
   1681 		    len);
   1682 		return(0);
   1683 	}
   1684 
   1685 	bytelen = len / 8;
   1686 	bitlen = len % 8;
   1687 
   1688 	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
   1689 		return(0);
   1690 	if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
   1691 	    p2->s6_addr[bytelen] >> (8 - bitlen))
   1692 		return(0);
   1693 
   1694 	return(1);
   1695 }
   1696 
   1697 void
   1698 in6_prefixlen2mask(maskp, len)
   1699 	struct in6_addr *maskp;
   1700 	int len;
   1701 {
   1702 	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
   1703 	int bytelen, bitlen, i;
   1704 
   1705 	/* sanity check */
   1706 	if (0 > len || len > 128) {
   1707 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
   1708 		    len);
   1709 		return;
   1710 	}
   1711 
   1712 	bzero(maskp, sizeof(*maskp));
   1713 	bytelen = len / 8;
   1714 	bitlen = len % 8;
   1715 	for (i = 0; i < bytelen; i++)
   1716 		maskp->s6_addr[i] = 0xff;
   1717 	if (bitlen)
   1718 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
   1719 }
   1720 
   1721 /*
   1722  * return the best address out of the same scope
   1723  */
   1724 struct in6_ifaddr *
   1725 in6_ifawithscope(oifp, dst)
   1726 	register struct ifnet *oifp;
   1727 	register struct in6_addr *dst;
   1728 {
   1729 	int dst_scope =	in6_addrscope(dst), src_scope, best_scope;
   1730 	int blen = -1;
   1731 	struct ifaddr *ifa;
   1732 	struct ifnet *ifp;
   1733 	struct in6_ifaddr *ifa_best = NULL;
   1734 
   1735 	if (oifp == NULL) {
   1736 		printf("in6_ifawithscope: output interface is not specified\n");
   1737 		return(NULL);
   1738 	}
   1739 
   1740 	/*
   1741 	 * We search for all addresses on all interfaces from the beginning.
   1742 	 * Comparing an interface with the outgoing interface will be done
   1743 	 * only at the final stage of tiebreaking.
   1744 	 */
   1745 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
   1746 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
   1747 #else
   1748 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
   1749 #endif
   1750 	{
   1751 		/*
   1752 		 * We can never take an address that breaks the scope zone
   1753 		 * of the destination.
   1754 		 */
   1755 		if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
   1756 			continue;
   1757 
   1758 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
   1759 		for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
   1760 #else
   1761 		for (ifa = ifp->if_addrlist.tqh_first; ifa;
   1762 		     ifa = ifa->ifa_list.tqe_next)
   1763 #endif
   1764 		{
   1765 			int tlen = -1, dscopecmp, bscopecmp, matchcmp;
   1766 
   1767 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1768 				continue;
   1769 
   1770 			src_scope = in6_addrscope(IFA_IN6(ifa));
   1771 
   1772 #ifdef ADDRSELECT_DEBUG		/* should be removed after stabilization */
   1773 			dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
   1774 			printf("in6_ifawithscope: dst=%s bestaddr=%s, "
   1775 			       "newaddr=%s, scope=%x, dcmp=%d, bcmp=%d, "
   1776 			       "matchlen=%d, flgs=%x\n",
   1777 			       ip6_sprintf(dst),
   1778 			       ifa_best ? ip6_sprintf(&ifa_best->ia_addr.sin6_addr) : "none",
   1779 			       ip6_sprintf(IFA_IN6(ifa)), src_scope,
   1780 			       dscopecmp,
   1781 			       ifa_best ? IN6_ARE_SCOPE_CMP(src_scope, best_scope) : -1,
   1782 			       in6_matchlen(IFA_IN6(ifa), dst),
   1783 			       ((struct in6_ifaddr *)ifa)->ia6_flags);
   1784 #endif
   1785 
   1786 			/*
   1787 			 * Don't use an address before completing DAD
   1788 			 * nor a duplicated address.
   1789 			 */
   1790 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
   1791 			    IN6_IFF_NOTREADY)
   1792 				continue;
   1793 
   1794 			/* XXX: is there any case to allow anycasts? */
   1795 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
   1796 			    IN6_IFF_ANYCAST)
   1797 				continue;
   1798 
   1799 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
   1800 			    IN6_IFF_DETACHED)
   1801 				continue;
   1802 
   1803 			/*
   1804 			 * If this is the first address we find,
   1805 			 * keep it anyway.
   1806 			 */
   1807 			if (ifa_best == NULL)
   1808 				goto replace;
   1809 
   1810 			/*
   1811 			 * ifa_best is never NULL beyond this line except
   1812 			 * within the block labeled "replace".
   1813 			 */
   1814 
   1815 			/*
   1816 			 * If ifa_best has a smaller scope than dst and
   1817 			 * the current address has a larger one than
   1818 			 * (or equal to) dst, always replace ifa_best.
   1819 			 * Also, if the current address has a smaller scope
   1820 			 * than dst, ignore it unless ifa_best also has a
   1821 			 * smaller scope.
   1822 			 */
   1823 			if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
   1824 			    IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
   1825 				goto replace;
   1826 			if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
   1827 			    IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
   1828 				continue;
   1829 
   1830 			/*
   1831 			 * A deprecated address SHOULD NOT be used in new
   1832 			 * communications if an alternate (non-deprecated)
   1833 			 * address is available and has sufficient scope.
   1834 			 * RFC 2462, Section 5.5.4.
   1835 			 */
   1836 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
   1837 			    IN6_IFF_DEPRECATED) {
   1838 				/*
   1839 				 * Ignore any deprecated addresses if
   1840 				 * specified by configuration.
   1841 				 */
   1842 				if (!ip6_use_deprecated)
   1843 					continue;
   1844 
   1845 				/*
   1846 				 * If we have already found a non-deprecated
   1847 				 * candidate, just ignore deprecated addresses.
   1848 				 */
   1849 				if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
   1850 				    == 0)
   1851 					continue;
   1852 			}
   1853 
   1854 			/*
   1855 			 * A non-deprecated address is always preferred
   1856 			 * to a deprecated one regardless of scopes and
   1857 			 * address matching.
   1858 			 */
   1859 			if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
   1860 			    (((struct in6_ifaddr *)ifa)->ia6_flags &
   1861 			     IN6_IFF_DEPRECATED) == 0)
   1862 				goto replace;
   1863 
   1864 			/*
   1865 			 * At this point, we have two cases:
   1866 			 * 1. we are looking at a non-deprecated address,
   1867 			 *    and ifa_best is also non-deprecated.
   1868 			 * 2. we are looking at a deprecated address,
   1869 			 *    and ifa_best is also deprecated.
   1870 			 * Also, we do not have to consider a case where
   1871 			 * the scope of if_best is larger(smaller) than dst and
   1872 			 * the scope of the current address is smaller(larger)
   1873 			 * than dst. Such a case has already been covered.
   1874 			 * Tiebreaking is done according to the following
   1875 			 * items:
   1876 			 * - the scope comparison between the address and
   1877 			 *   dst (dscopecmp)
   1878 			 * - the scope comparison between the address and
   1879 			 *   ifa_best (bscopecmp)
   1880 			 * - if the address match dst longer than ifa_best
   1881 			 *   (matchcmp)
   1882 			 * - if the address is on the outgoing I/F (outI/F)
   1883 			 *
   1884 			 * Roughly speaking, the selection policy is
   1885 			 * - the most important item is scope. The same scope
   1886 			 *   is best. Then search for a larger scope.
   1887 			 *   Smaller scopes are the last resort.
   1888 			 * - A deprecated address is chosen only when we have
   1889 			 *   no address that has an enough scope, but is
   1890 			 *   prefered to any addresses of smaller scopes.
   1891 			 * - Longest address match against dst is considered
   1892 			 *   only for addresses that has the same scope of dst.
   1893 			 * - If there is no other reasons to choose one,
   1894 			 *   addresses on the outgoing I/F are preferred.
   1895 			 *
   1896 			 * The precise decision table is as follows:
   1897 			 * dscopecmp bscopecmp matchcmp outI/F | replace?
   1898 			 *    !equal     equal      N/A    Yes |      Yes (1)
   1899 			 *    !equal     equal      N/A     No |       No (2)
   1900 			 *    larger    larger      N/A    N/A |       No (3)
   1901 			 *    larger   smaller      N/A    N/A |      Yes (4)
   1902 			 *   smaller    larger      N/A    N/A |      Yes (5)
   1903 			 *   smaller   smaller      N/A    N/A |       No (6)
   1904 			 *     equal   smaller      N/A    N/A |      Yes (7)
   1905 			 *     equal    larger       (already done)
   1906 			 *     equal     equal   larger    N/A |      Yes (8)
   1907 			 *     equal     equal  smaller    N/A |       No (9)
   1908 			 *     equal     equal    equal    Yes |      Yes (a)
   1909 			 *     eaual     eqaul    equal     No |       No (b)
   1910 			 */
   1911 			dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
   1912 			bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
   1913 
   1914 			if (dscopecmp && bscopecmp == 0) {
   1915 				if (oifp == ifp) /* (1) */
   1916 					goto replace;
   1917 				continue; /* (2) */
   1918 			}
   1919 			if (dscopecmp > 0) {
   1920 				if (bscopecmp > 0) /* (3) */
   1921 					continue;
   1922 				goto replace; /* (4) */
   1923 			}
   1924 			if (dscopecmp < 0) {
   1925 				if (bscopecmp > 0) /* (5) */
   1926 					goto replace;
   1927 				continue; /* (6) */
   1928 			}
   1929 
   1930 			/* now dscopecmp must be 0 */
   1931 			if (bscopecmp < 0)
   1932 				goto replace; /* (7) */
   1933 
   1934 			/*
   1935 			 * At last both dscopecmp and bscopecmp must be 0.
   1936 			 * We need address matching against dst for
   1937 			 * tiebreaking.
   1938 			 */
   1939 			tlen = in6_matchlen(IFA_IN6(ifa), dst);
   1940 			matchcmp = tlen - blen;
   1941 			if (matchcmp > 0) /* (8) */
   1942 				goto replace;
   1943 			if (matchcmp < 0) /* (9) */
   1944 				continue;
   1945 			if (oifp == ifp) /* (a) */
   1946 				goto replace;
   1947 			continue; /* (b) */
   1948 
   1949 		  replace:
   1950 			ifa_best = (struct in6_ifaddr *)ifa;
   1951 			blen = tlen >= 0 ? tlen :
   1952 				in6_matchlen(IFA_IN6(ifa), dst);
   1953 			best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
   1954 		}
   1955 	}
   1956 
   1957 	/* count statistics for future improvements */
   1958 	if (ifa_best == NULL)
   1959 		ip6stat.ip6s_sources_none++;
   1960 	else {
   1961 		if (oifp == ifa_best->ia_ifp)
   1962 			ip6stat.ip6s_sources_sameif[best_scope]++;
   1963 		else
   1964 			ip6stat.ip6s_sources_otherif[best_scope]++;
   1965 
   1966 		if (best_scope == dst_scope)
   1967 			ip6stat.ip6s_sources_samescope[best_scope]++;
   1968 		else
   1969 			ip6stat.ip6s_sources_otherscope[best_scope]++;
   1970 
   1971 		if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
   1972 			ip6stat.ip6s_sources_deprecated[best_scope]++;
   1973 	}
   1974 
   1975 	return(ifa_best);
   1976 }
   1977 
   1978 /*
   1979  * return the best address out of the same scope. if no address was
   1980  * found, return the first valid address from designated IF.
   1981  */
   1982 
   1983 struct in6_ifaddr *
   1984 in6_ifawithifp(ifp, dst)
   1985 	register struct ifnet *ifp;
   1986 	register struct in6_addr *dst;
   1987 {
   1988 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
   1989 	struct ifaddr *ifa;
   1990 	struct in6_ifaddr *besta = 0;
   1991 	struct in6_ifaddr *dep[2];	/*last-resort: deprecated*/
   1992 
   1993 	dep[0] = dep[1] = NULL;
   1994 
   1995 	/*
   1996 	 * We first look for addresses in the same scope.
   1997 	 * If there is one, return it.
   1998 	 * If two or more, return one which matches the dst longest.
   1999 	 * If none, return one of global addresses assigned other ifs.
   2000 	 */
   2001 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   2002 	{
   2003 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2004 			continue;
   2005 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
   2006 			continue; /* XXX: is there any case to allow anycast? */
   2007 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
   2008 			continue; /* don't use this interface */
   2009 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
   2010 			continue;
   2011 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
   2012 			if (ip6_use_deprecated)
   2013 				dep[0] = (struct in6_ifaddr *)ifa;
   2014 			continue;
   2015 		}
   2016 
   2017 		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
   2018 			/*
   2019 			 * call in6_matchlen() as few as possible
   2020 			 */
   2021 			if (besta) {
   2022 				if (blen == -1)
   2023 					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
   2024 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
   2025 				if (tlen > blen) {
   2026 					blen = tlen;
   2027 					besta = (struct in6_ifaddr *)ifa;
   2028 				}
   2029 			} else
   2030 				besta = (struct in6_ifaddr *)ifa;
   2031 		}
   2032 	}
   2033 	if (besta)
   2034 		return(besta);
   2035 
   2036 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   2037 	{
   2038 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2039 			continue;
   2040 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
   2041 			continue; /* XXX: is there any case to allow anycast? */
   2042 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
   2043 			continue; /* don't use this interface */
   2044 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
   2045 			continue;
   2046 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
   2047 			if (ip6_use_deprecated)
   2048 				dep[1] = (struct in6_ifaddr *)ifa;
   2049 			continue;
   2050 		}
   2051 
   2052 		return (struct in6_ifaddr *)ifa;
   2053 	}
   2054 
   2055 	/* use the last-resort values, that are, deprecated addresses */
   2056 	if (dep[0])
   2057 		return dep[0];
   2058 	if (dep[1])
   2059 		return dep[1];
   2060 
   2061 	return NULL;
   2062 }
   2063 
   2064 /*
   2065  * perform DAD when interface becomes IFF_UP.
   2066  */
   2067 void
   2068 in6_if_up(ifp)
   2069 	struct ifnet *ifp;
   2070 {
   2071 	struct ifaddr *ifa;
   2072 	struct in6_ifaddr *ia;
   2073 	struct sockaddr_dl *sdl;
   2074 	int type;
   2075 	struct ether_addr ea;
   2076 	int off;
   2077 	int dad_delay;		/* delay ticks before DAD output */
   2078 
   2079 	bzero(&ea, sizeof(ea));
   2080 	sdl = NULL;
   2081 
   2082 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   2083 	{
   2084 		if (ifa->ifa_addr->sa_family == AF_INET6
   2085 		 && IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
   2086 			goto dad;
   2087 		}
   2088 		if (ifa->ifa_addr->sa_family != AF_LINK)
   2089 			continue;
   2090 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
   2091 		break;
   2092 	}
   2093 
   2094 	switch (ifp->if_type) {
   2095 	case IFT_LOOP:
   2096 		in6_ifattach(ifp, IN6_IFT_LOOP, NULL, 1);
   2097 		break;
   2098 	case IFT_SLIP:
   2099 	case IFT_PPP:
   2100 	case IFT_GIF:
   2101 	case IFT_FAITH:
   2102 		type = IN6_IFT_P2P;
   2103 		in6_ifattach(ifp, type, 0, 1);
   2104 		break;
   2105 	case IFT_ETHER:
   2106 	case IFT_FDDI:
   2107 	case IFT_ATM:
   2108 		type = IN6_IFT_802;
   2109 		if (sdl == NULL)
   2110 			break;
   2111 		off = sdl->sdl_nlen;
   2112 		if (bcmp(&sdl->sdl_data[off], &ea, sizeof(ea)) != 0)
   2113 			in6_ifattach(ifp, type, LLADDR(sdl), 0);
   2114 		break;
   2115 	case IFT_ARCNET:
   2116 		type = IN6_IFT_ARCNET;
   2117 		if (sdl == NULL)
   2118 			break;
   2119 		off = sdl->sdl_nlen;
   2120 		if (sdl->sdl_data[off] != 0)	/* XXX ?: */
   2121 			in6_ifattach(ifp, type, LLADDR(sdl), 0);
   2122 		break;
   2123 	default:
   2124 		break;
   2125 	}
   2126 
   2127 dad:
   2128 	dad_delay = 0;
   2129 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   2130 	{
   2131 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2132 			continue;
   2133 		ia = (struct in6_ifaddr *)ifa;
   2134 		if (ia->ia6_flags & IN6_IFF_TENTATIVE)
   2135 			nd6_dad_start(ifa, &dad_delay);
   2136 	}
   2137 }
   2138 
   2139 /*
   2140  * Calculate max IPv6 MTU through all the interfaces and store it
   2141  * to in6_maxmtu.
   2142  */
   2143 void
   2144 in6_setmaxmtu()
   2145 {
   2146 	unsigned long maxmtu = 0;
   2147 	struct ifnet *ifp;
   2148 
   2149 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
   2150 	{
   2151 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
   2152 		    nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
   2153 			maxmtu =  nd_ifinfo[ifp->if_index].linkmtu;
   2154 	}
   2155 	if (maxmtu)	/* update only when maxmtu is positive */
   2156 		in6_maxmtu = maxmtu;
   2157 }
   2158