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