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