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