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