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