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