Home | History | Annotate | Line # | Download | only in netinet6
in6.c revision 1.9
      1 /*	$NetBSD: in6.c,v 1.9 2000/01/06 15:46:09 itojun 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 		nrt->rt_ifa->ifa_refcnt--;
    214 		ifa->ifa_refcnt++;
    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 			TAILQ_INSERT_TAIL(&ifp->if_addrlist,
    563 				(struct ifaddr *)ia, ifa_list);
    564 			if ((ifp->if_flags & IFF_LOOPBACK) == 0)
    565 				in6_interfaces++;	/*XXX*/
    566 		}
    567 
    568 		if (cmd == SIOCAIFADDR_IN6) {
    569 			/* sanity for overflow - beware unsigned */
    570 			struct in6_addrlifetime *lt;
    571 			lt = &ifra->ifra_lifetime;
    572 			if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
    573 			 && lt->ia6t_vltime + time_second < time_second) {
    574 				return EINVAL;
    575 			}
    576 			if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
    577 			 && lt->ia6t_pltime + time_second < time_second) {
    578 				return EINVAL;
    579 			}
    580 		}
    581 		break;
    582 
    583 	case SIOCGIFADDR_IN6:
    584 		/* This interface is basically deprecated. use SIOCGIFCONF. */
    585 		/* fall through */
    586 	case SIOCGIFAFLAG_IN6:
    587 	case SIOCGIFNETMASK_IN6:
    588 	case SIOCGIFDSTADDR_IN6:
    589 	case SIOCGIFALIFETIME_IN6:
    590 		/* must think again about its semantics */
    591 		if (ia == 0)
    592 			return(EADDRNOTAVAIL);
    593 		break;
    594 	case SIOCSIFALIFETIME_IN6:
    595 	    {
    596 		struct in6_addrlifetime *lt;
    597 
    598 		if (!privileged)
    599 			return(EPERM);
    600 		if (ia == 0)
    601 			return(EADDRNOTAVAIL);
    602 		/* sanity for overflow - beware unsigned */
    603 		lt = &ifr->ifr_ifru.ifru_lifetime;
    604 		if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
    605 		 && lt->ia6t_vltime + time_second < time_second) {
    606 			return EINVAL;
    607 		}
    608 		if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
    609 		 && lt->ia6t_pltime + time_second < time_second) {
    610 			return EINVAL;
    611 		}
    612 		break;
    613 	    }
    614 	}
    615 
    616 	switch (cmd) {
    617 
    618 	case SIOCGIFADDR_IN6:
    619 		ifr->ifr_addr = ia->ia_addr;
    620 		break;
    621 
    622 	case SIOCGIFDSTADDR_IN6:
    623 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    624 			return(EINVAL);
    625 		ifr->ifr_dstaddr = ia->ia_dstaddr;
    626 		break;
    627 
    628 	case SIOCGIFNETMASK_IN6:
    629 		ifr->ifr_addr = ia->ia_prefixmask;
    630 		break;
    631 
    632 	case SIOCGIFAFLAG_IN6:
    633 		ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
    634 		break;
    635 
    636 	case SIOCGIFSTAT_IN6:
    637 		if (ifp == NULL)
    638 			return EINVAL;
    639 		if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
    640 		 || in6_ifstat[ifp->if_index] == NULL) {
    641 			/* return EAFNOSUPPORT? */
    642 			bzero(&ifr->ifr_ifru.ifru_stat,
    643 				sizeof(ifr->ifr_ifru.ifru_stat));
    644 		} else
    645 			ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
    646 		break;
    647 
    648 	case SIOCGIFSTAT_ICMP6:
    649 		if (ifp == NULL)
    650 			return EINVAL;
    651 		if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
    652 		    icmp6_ifstat[ifp->if_index] == NULL) {
    653 			/* return EAFNOSUPPORT? */
    654 			bzero(&ifr->ifr_ifru.ifru_stat,
    655 				sizeof(ifr->ifr_ifru.ifru_icmp6stat));
    656 		} else
    657 			ifr->ifr_ifru.ifru_icmp6stat =
    658 				*icmp6_ifstat[ifp->if_index];
    659 		break;
    660 
    661 	case SIOCSIFDSTADDR_IN6:
    662 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    663 			return(EINVAL);
    664 		oldaddr = ia->ia_dstaddr;
    665 		ia->ia_dstaddr = ifr->ifr_dstaddr;
    666 
    667 		/* link-local index check */
    668 		if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
    669 			if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
    670 				/* interface ID is not embedded by the user */
    671 				ia->ia_dstaddr.sin6_addr.s6_addr16[1]
    672 					= htons(ifp->if_index);
    673 			}
    674 			else
    675 				if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
    676 				    htons(ifp->if_index)) {
    677 					ia->ia_dstaddr = oldaddr;
    678 					return(EINVAL);	/* ifid is contradict */
    679 				}
    680 		}
    681 
    682 		if (ifp->if_ioctl && (error = (ifp->if_ioctl)
    683 				      (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
    684 			ia->ia_dstaddr = oldaddr;
    685 			return(error);
    686 		}
    687 		if (ia->ia_flags & IFA_ROUTE) {
    688 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
    689 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    690 			ia->ia_ifa.ifa_dstaddr =
    691 				(struct sockaddr *)&ia->ia_dstaddr;
    692 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
    693 		}
    694 		break;
    695 
    696 	case SIOCGIFALIFETIME_IN6:
    697 		ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
    698 		break;
    699 
    700 	case SIOCSIFALIFETIME_IN6:
    701 		ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
    702 		/* for sanity */
    703 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
    704 			ia->ia6_lifetime.ia6t_expire =
    705 				time_second + ia->ia6_lifetime.ia6t_vltime;
    706 		} else
    707 			ia->ia6_lifetime.ia6t_expire = 0;
    708 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
    709 			ia->ia6_lifetime.ia6t_preferred =
    710 				time_second + ia->ia6_lifetime.ia6t_pltime;
    711 		} else
    712 			ia->ia6_lifetime.ia6t_preferred = 0;
    713 		break;
    714 
    715 	case SIOCSIFADDR_IN6:
    716 		return(in6_ifinit(ifp, ia, &ifr->ifr_addr, 1));
    717 
    718 	case SIOCSIFNETMASK_IN6:
    719 		ia->ia_prefixmask = ifr->ifr_addr;
    720 		bzero(&net, sizeof(net));
    721 		net.sin6_len = sizeof(struct sockaddr_in6);
    722 		net.sin6_family = AF_INET6;
    723 		net.sin6_port = htons(0);
    724 		net.sin6_flowinfo = htonl(0);
    725 		net.sin6_addr.s6_addr32[0]
    726 			= ia->ia_addr.sin6_addr.s6_addr32[0] &
    727 				ia->ia_prefixmask.sin6_addr.s6_addr32[0];
    728 		net.sin6_addr.s6_addr32[1]
    729 			= ia->ia_addr.sin6_addr.s6_addr32[1] &
    730 				ia->ia_prefixmask.sin6_addr.s6_addr32[1];
    731 		net.sin6_addr.s6_addr32[2]
    732 			= ia->ia_addr.sin6_addr.s6_addr32[2] &
    733 				ia->ia_prefixmask.sin6_addr.s6_addr32[2];
    734 		net.sin6_addr.s6_addr32[3]
    735 			= ia->ia_addr.sin6_addr.s6_addr32[3] &
    736 				ia->ia_prefixmask.sin6_addr.s6_addr32[3];
    737 		ia->ia_net = net;
    738 		break;
    739 
    740 	case SIOCAIFADDR_IN6:
    741 		prefixIsNew = 0;
    742 		hostIsNew = 1;
    743 
    744 		if (ifra->ifra_addr.sin6_len == 0) {
    745 			ifra->ifra_addr = ia->ia_addr;
    746 			hostIsNew = 0;
    747 		} else if (IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr,
    748 					      &ia->ia_addr.sin6_addr))
    749 			hostIsNew = 0;
    750 
    751 		if (ifra->ifra_prefixmask.sin6_len) {
    752 			in6_ifscrub(ifp, ia);
    753 			ia->ia_prefixmask = ifra->ifra_prefixmask;
    754 			prefixIsNew = 1;
    755 		}
    756 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    757 		    (ifra->ifra_dstaddr.sin6_family == AF_INET6)) {
    758 			in6_ifscrub(ifp, ia);
    759 			ia->ia_dstaddr = ifra->ifra_dstaddr;
    760 			/* link-local index check: should be a separate function? */
    761 			if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
    762 				if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
    763 					/*
    764 					 * interface ID is not embedded by
    765 					 * the user
    766 					 */
    767 					ia->ia_dstaddr.sin6_addr.s6_addr16[1]
    768 						= htons(ifp->if_index);
    769 				}
    770 				else
    771 					if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
    772 					    htons(ifp->if_index)) {
    773 						ia->ia_dstaddr = oldaddr;
    774 						return(EINVAL);	/* ifid is contradict */
    775 					}
    776 			}
    777 			prefixIsNew = 1; /* We lie; but effect's the same */
    778 		}
    779 		if (ifra->ifra_addr.sin6_family == AF_INET6 &&
    780 		    (hostIsNew || prefixIsNew))
    781 			error = in6_ifinit(ifp, ia, &ifra->ifra_addr, 0);
    782 		if (ifra->ifra_addr.sin6_family == AF_INET6
    783 		    && hostIsNew && (ifp->if_flags & IFF_MULTICAST)) {
    784 			int error_local = 0;
    785 
    786 			/*
    787 			 * join solicited multicast addr for new host id
    788 			 */
    789 			struct in6_addr llsol;
    790 			bzero(&llsol, sizeof(struct in6_addr));
    791 			llsol.s6_addr16[0] = htons(0xff02);
    792 			llsol.s6_addr16[1] = htons(ifp->if_index);
    793 			llsol.s6_addr32[1] = 0;
    794 			llsol.s6_addr32[2] = htonl(1);
    795 			llsol.s6_addr32[3] =
    796 				ifra->ifra_addr.sin6_addr.s6_addr32[3];
    797 			llsol.s6_addr8[12] = 0xff;
    798 			(void)in6_addmulti(&llsol, ifp, &error_local);
    799 			if (error == 0)
    800 				error = error_local;
    801 		}
    802 		/* Join dstaddr's solicited multicast if necessary. */
    803 		if (nd6_proxyall && hostIsNew) {
    804 			int error_local;
    805 
    806 			error_local = in6_ifaddproxy(ia);
    807 			if (error == 0)
    808 				error = error_local;
    809 		}
    810 
    811 		ia->ia6_flags = ifra->ifra_flags;
    812 		ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/*safety*/
    813 
    814 		ia->ia6_lifetime = ifra->ifra_lifetime;
    815 		/* for sanity */
    816 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
    817 			ia->ia6_lifetime.ia6t_expire =
    818 				time_second + ia->ia6_lifetime.ia6t_vltime;
    819 		} else
    820 			ia->ia6_lifetime.ia6t_expire = 0;
    821 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
    822 			ia->ia6_lifetime.ia6t_preferred =
    823 				time_second + ia->ia6_lifetime.ia6t_pltime;
    824 		} else
    825 			ia->ia6_lifetime.ia6t_preferred = 0;
    826 
    827 		/*
    828 		 * Perform DAD, if needed.
    829 		 * XXX It may be of use, if we can administratively
    830 		 * disable DAD.
    831 		 */
    832 		switch (ifp->if_type) {
    833 		case IFT_ARCNET:
    834 		case IFT_ETHER:
    835 		case IFT_FDDI:
    836 #if 0
    837 		case IFT_ATM:
    838 		case IFT_SLIP:
    839 		case IFT_PPP:
    840 #endif
    841 			ia->ia6_flags |= IN6_IFF_TENTATIVE;
    842 			nd6_dad_start((struct ifaddr *)ia, NULL);
    843 			break;
    844 		case IFT_FAITH:
    845 		case IFT_GIF:
    846 		case IFT_LOOP:
    847 		default:
    848 			break;
    849 		}
    850 
    851 		if (hostIsNew) {
    852 			int iilen;
    853 			int error_local = 0;
    854 
    855 			iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) -
    856 				in6_mask2len(&ia->ia_prefixmask.sin6_addr);
    857 			error_local = in6_prefix_add_ifid(iilen, ia);
    858 			if (error == 0)
    859 				error = error_local;
    860 		}
    861 
    862 		return(error);
    863 
    864 	case SIOCDIFADDR_IN6:
    865 		in6_ifscrub(ifp, ia);
    866 
    867 		if (ifp->if_flags & IFF_MULTICAST) {
    868 			/*
    869 			 * delete solicited multicast addr for deleting host id
    870 			 */
    871 			struct in6_multi *in6m;
    872 			struct in6_addr llsol;
    873 			bzero(&llsol, sizeof(struct in6_addr));
    874 			llsol.s6_addr16[0] = htons(0xff02);
    875 			llsol.s6_addr16[1] = htons(ifp->if_index);
    876 			llsol.s6_addr32[1] = 0;
    877 			llsol.s6_addr32[2] = htonl(1);
    878 			llsol.s6_addr32[3] =
    879 				ia->ia_addr.sin6_addr.s6_addr32[3];
    880 			llsol.s6_addr8[12] = 0xff;
    881 
    882 			IN6_LOOKUP_MULTI(llsol, ifp, in6m);
    883 			if (in6m)
    884 				in6_delmulti(in6m);
    885 		}
    886 		/* Leave dstaddr's solicited multicast if necessary. */
    887 		if (nd6_proxyall)
    888 			in6_ifremproxy(ia);
    889 
    890 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    891 		oia = ia;
    892 		if (oia == (ia = in6_ifaddr))
    893 			in6_ifaddr = ia->ia_next;
    894 		else {
    895 			while (ia->ia_next && (ia->ia_next != oia))
    896 				ia = ia->ia_next;
    897 			if (ia->ia_next)
    898 				ia->ia_next = oia->ia_next;
    899 			else
    900 				printf("Didn't unlink in6_ifaddr from list\n");
    901 		}
    902 		{
    903 			int iilen;
    904 
    905 			iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) -
    906 				in6_mask2len(&oia->ia_prefixmask.sin6_addr);
    907 			in6_prefix_remove_ifid(iilen, oia);
    908 		}
    909 		if (oia->ia6_multiaddrs.lh_first == NULL) {
    910 			IFAFREE(&oia->ia_ifa);
    911 			break;
    912 		}
    913 		else
    914 			in6_savemkludge(oia);
    915 
    916 		IFAFREE((&oia->ia_ifa));
    917 		break;
    918 
    919 	default:
    920 		if (ifp == 0 || ifp->if_ioctl == 0)
    921 			return(EOPNOTSUPP);
    922 		return((*ifp->if_ioctl)(ifp, cmd, data));
    923 	}
    924 	return(0);
    925 }
    926 
    927 /*
    928  * SIOC[GAD]LIFADDR.
    929  *	SIOCGLIFADDR: get first address. (???)
    930  *	SIOCGLIFADDR with IFLR_PREFIX:
    931  *		get first address that matches the specified prefix.
    932  *	SIOCALIFADDR: add the specified address.
    933  *	SIOCALIFADDR with IFLR_PREFIX:
    934  *		add the specified prefix, filling hostid part from
    935  *		the first link-local address.  prefixlen must be <= 64.
    936  *	SIOCDLIFADDR: delete the specified address.
    937  *	SIOCDLIFADDR with IFLR_PREFIX:
    938  *		delete the first address that matches the specified prefix.
    939  * return values:
    940  *	EINVAL on invalid parameters
    941  *	EADDRNOTAVAIL on prefix match failed/specified address not found
    942  *	other values may be returned from in6_ioctl()
    943  *
    944  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
    945  * this is to accomodate address naming scheme other than RFC2374,
    946  * in the future.
    947  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
    948  * address encoding scheme. (see figure on page 8)
    949  */
    950 static int
    951 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
    952 	struct socket *so;
    953 	u_long cmd;
    954 	caddr_t	data;
    955 	struct ifnet *ifp;
    956 	struct proc *p;
    957 {
    958 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
    959 	struct ifaddr *ifa;
    960 	struct sockaddr *sa;
    961 
    962 	/* sanity checks */
    963 	if (!data || !ifp) {
    964 		panic("invalid argument to in6_lifaddr_ioctl");
    965 		/*NOTRECHED*/
    966 	}
    967 
    968 	switch (cmd) {
    969 	case SIOCGLIFADDR:
    970 		/* address must be specified on GET with IFLR_PREFIX */
    971 		if ((iflr->flags & IFLR_PREFIX) == 0)
    972 			break;
    973 		/*FALLTHROUGH*/
    974 	case SIOCALIFADDR:
    975 	case SIOCDLIFADDR:
    976 		/* address must be specified on ADD and DELETE */
    977 		sa = (struct sockaddr *)&iflr->addr;
    978 		if (sa->sa_family != AF_INET6)
    979 			return EINVAL;
    980 		if (sa->sa_len != sizeof(struct sockaddr_in6))
    981 			return EINVAL;
    982 		/* XXX need improvement */
    983 		sa = (struct sockaddr *)&iflr->dstaddr;
    984 		if (sa->sa_family && sa->sa_family != AF_INET6)
    985 			return EINVAL;
    986 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
    987 			return EINVAL;
    988 		break;
    989 	default: /*shouldn't happen*/
    990 #if 0
    991 		panic("invalid cmd to in6_lifaddr_ioctl");
    992 		/*NOTREACHED*/
    993 #else
    994 		return EOPNOTSUPP;
    995 #endif
    996 	}
    997 	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
    998 		return EINVAL;
    999 
   1000 	switch (cmd) {
   1001 	case SIOCALIFADDR:
   1002 	    {
   1003 		struct in6_aliasreq ifra;
   1004 		struct in6_addr *hostid = NULL;
   1005 		int prefixlen;
   1006 
   1007 		if ((iflr->flags & IFLR_PREFIX) != 0) {
   1008 			struct sockaddr_in6 *sin6;
   1009 
   1010 			/*
   1011 			 * hostid is to fill in the hostid part of the
   1012 			 * address.  hostid points to the first link-local
   1013 			 * address attached to the interface.
   1014 			 */
   1015 			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp);
   1016 			if (!ifa)
   1017 				return EADDRNOTAVAIL;
   1018 			hostid = IFA_IN6(ifa);
   1019 
   1020 		 	/* prefixlen must be <= 64. */
   1021 			if (64 < iflr->prefixlen)
   1022 				return EINVAL;
   1023 			prefixlen = iflr->prefixlen;
   1024 
   1025 			/* hostid part must be zero. */
   1026 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1027 			if (sin6->sin6_addr.s6_addr32[2] != 0
   1028 			 || sin6->sin6_addr.s6_addr32[3] != 0) {
   1029 				return EINVAL;
   1030 			}
   1031 		} else
   1032 			prefixlen = iflr->prefixlen;
   1033 
   1034 		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
   1035 		bzero(&ifra, sizeof(ifra));
   1036 		bcopy(iflr->iflr_name, ifra.ifra_name,
   1037 			sizeof(ifra.ifra_name));
   1038 
   1039 		bcopy(&iflr->addr, &ifra.ifra_addr,
   1040 			((struct sockaddr *)&iflr->addr)->sa_len);
   1041 		if (hostid) {
   1042 			/* fill in hostid part */
   1043 			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
   1044 				hostid->s6_addr32[2];
   1045 			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
   1046 				hostid->s6_addr32[3];
   1047 		}
   1048 
   1049 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
   1050 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
   1051 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
   1052 			if (hostid) {
   1053 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
   1054 					hostid->s6_addr32[2];
   1055 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
   1056 					hostid->s6_addr32[3];
   1057 			}
   1058 		}
   1059 
   1060 		ifra.ifra_prefixmask.sin6_family = AF_INET6;
   1061 		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
   1062 		in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
   1063 
   1064 		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
   1065 		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
   1066 	    }
   1067 	case SIOCGLIFADDR:
   1068 	case SIOCDLIFADDR:
   1069 	    {
   1070 		struct in6_ifaddr *ia;
   1071 		struct in6_addr mask, candidate, match;
   1072 		struct sockaddr_in6 *sin6;
   1073 		int cmp;
   1074 
   1075 		bzero(&mask, sizeof(mask));
   1076 		if (iflr->flags & IFLR_PREFIX) {
   1077 			/* lookup a prefix rather than address. */
   1078 			in6_len2mask(&mask, iflr->prefixlen);
   1079 
   1080 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1081 			bcopy(&sin6->sin6_addr, &match, sizeof(match));
   1082 			match.s6_addr32[0] &= mask.s6_addr32[0];
   1083 			match.s6_addr32[1] &= mask.s6_addr32[1];
   1084 			match.s6_addr32[2] &= mask.s6_addr32[2];
   1085 			match.s6_addr32[3] &= mask.s6_addr32[3];
   1086 
   1087 			/* if you set extra bits, that's wrong */
   1088 			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
   1089 				return EINVAL;
   1090 
   1091 			cmp = 1;
   1092 		} else {
   1093 			if (cmd == SIOCGLIFADDR) {
   1094 				/* on getting an address, take the 1st match */
   1095 				cmp = 0;	/*XXX*/
   1096 			} else {
   1097 				/* on deleting an address, do exact match */
   1098 				in6_len2mask(&mask, 128);
   1099 				sin6 = (struct sockaddr_in6 *)&iflr->addr;
   1100 				bcopy(&sin6->sin6_addr, &match, sizeof(match));
   1101 
   1102 				cmp = 1;
   1103 			}
   1104 		}
   1105 
   1106 		for (ifa = ifp->if_addrlist.tqh_first;
   1107 		     ifa;
   1108 		     ifa = ifa->ifa_list.tqe_next)
   1109 		{
   1110 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1111 				continue;
   1112 			if (!cmp)
   1113 				break;
   1114 			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
   1115 			candidate.s6_addr32[0] &= mask.s6_addr32[0];
   1116 			candidate.s6_addr32[1] &= mask.s6_addr32[1];
   1117 			candidate.s6_addr32[2] &= mask.s6_addr32[2];
   1118 			candidate.s6_addr32[3] &= mask.s6_addr32[3];
   1119 			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
   1120 				break;
   1121 		}
   1122 		if (!ifa)
   1123 			return EADDRNOTAVAIL;
   1124 		ia = ifa2ia6(ifa);
   1125 
   1126 		if (cmd == SIOCGLIFADDR) {
   1127 			/* fill in the if_laddrreq structure */
   1128 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
   1129 
   1130 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1131 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
   1132 					ia->ia_dstaddr.sin6_len);
   1133 			} else
   1134 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
   1135 
   1136 			iflr->prefixlen =
   1137 				in6_mask2len(&ia->ia_prefixmask.sin6_addr);
   1138 
   1139 			iflr->flags = ia->ia6_flags;	/*XXX*/
   1140 
   1141 			return 0;
   1142 		} else {
   1143 			struct in6_aliasreq ifra;
   1144 
   1145 			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
   1146 			bzero(&ifra, sizeof(ifra));
   1147 			bcopy(iflr->iflr_name, ifra.ifra_name,
   1148 				sizeof(ifra.ifra_name));
   1149 
   1150 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
   1151 				ia->ia_addr.sin6_len);
   1152 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1153 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
   1154 					ia->ia_dstaddr.sin6_len);
   1155 			}
   1156 			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
   1157 				ia->ia_prefixmask.sin6_len);
   1158 
   1159 			ifra.ifra_flags = ia->ia6_flags;
   1160 			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
   1161 				ifp, p);
   1162 		}
   1163 	    }
   1164 	}
   1165 
   1166 	return EOPNOTSUPP;	/*just for safety*/
   1167 }
   1168 
   1169 /*
   1170  * Delete any existing route for an interface.
   1171  */
   1172 void
   1173 in6_ifscrub(ifp, ia)
   1174 	register struct ifnet *ifp;
   1175 	register struct in6_ifaddr *ia;
   1176 {
   1177 	if ((ia->ia_flags & IFA_ROUTE) == 0)
   1178 		return;
   1179 	if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
   1180 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
   1181 	else
   1182 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
   1183 	ia->ia_flags &= ~IFA_ROUTE;
   1184 
   1185 	/* Remove ownaddr's loopback rtentry, if it exists. */
   1186 	in6_ifremloop(&(ia->ia_ifa));
   1187 }
   1188 
   1189 /*
   1190  * Initialize an interface's intetnet6 address
   1191  * and routing table entry.
   1192  */
   1193 int
   1194 in6_ifinit(ifp, ia, sin6, scrub)
   1195 	struct ifnet *ifp;
   1196 	struct in6_ifaddr *ia;
   1197 	struct sockaddr_in6 *sin6;
   1198 	int scrub;
   1199 {
   1200 	struct	sockaddr_in6 oldaddr;
   1201 	int	error, flags = RTF_UP;
   1202 	int	s = splimp();
   1203 
   1204 	oldaddr = ia->ia_addr;
   1205 	ia->ia_addr = *sin6;
   1206 	/*
   1207 	 * Give the interface a chance to initialize
   1208 	 * if this is its first address,
   1209 	 * and to validate the address if necessary.
   1210 	 */
   1211 	if (ifp->if_ioctl &&
   1212 	   (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
   1213 		splx(s);
   1214 		ia->ia_addr = oldaddr;
   1215 		return(error);
   1216 	}
   1217 
   1218 	switch (ifp->if_type) {
   1219 	case IFT_ARCNET:
   1220 	case IFT_ETHER:
   1221 	case IFT_FDDI:
   1222 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
   1223 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
   1224 		break;
   1225 	case IFT_PPP:
   1226 		ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest;
   1227 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
   1228 		break;
   1229 	}
   1230 
   1231 	splx(s);
   1232 	if (scrub) {
   1233 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
   1234 		in6_ifscrub(ifp, ia);
   1235 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
   1236 	}
   1237 	/* xxx
   1238 	 * in_socktrim
   1239 	 */
   1240 	/*
   1241 	 * Add route for the network.
   1242 	 */
   1243 	ia->ia_ifa.ifa_metric = ifp->if_metric;
   1244 	if (ifp->if_flags & IFF_LOOPBACK) {
   1245 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
   1246 		flags |= RTF_HOST;
   1247 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
   1248 		if (ia->ia_dstaddr.sin6_family != AF_INET6)
   1249 			return(0);
   1250 		flags |= RTF_HOST;
   1251 	}
   1252 	if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
   1253 		ia->ia_flags |= IFA_ROUTE;
   1254 
   1255 	/* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
   1256 	in6_ifaddloop(&(ia->ia_ifa));
   1257 
   1258 	if (ifp->if_flags & IFF_MULTICAST)
   1259 		in6_restoremkludge(ia, ifp);
   1260 
   1261 	return(error);
   1262 }
   1263 
   1264 /*
   1265  * Multicast address kludge:
   1266  * If there were any multicast addresses attached to this interface address,
   1267  * either move them to another address on this interface, or save them until
   1268  * such time as this interface is reconfigured for IPv6.
   1269  */
   1270 void
   1271 in6_savemkludge(oia)
   1272 	struct in6_ifaddr *oia;
   1273 {
   1274 	struct in6_ifaddr *ia;
   1275 	struct in6_multi *in6m, *next;
   1276 
   1277 	IFP_TO_IA6(oia->ia_ifp, ia);
   1278 	if (ia) {	/* there is another address */
   1279 		for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
   1280 			next = in6m->in6m_entry.le_next;
   1281 			IFAFREE(&in6m->in6m_ia->ia_ifa);
   1282 			ia->ia_ifa.ifa_refcnt++;
   1283 			in6m->in6m_ia = ia;
   1284 			LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
   1285 		}
   1286 	} else {	/* last address on this if deleted, save */
   1287 		struct multi6_kludge *mk;
   1288 
   1289 		mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
   1290 
   1291 		LIST_INIT(&mk->mk_head);
   1292 		mk->mk_ifp = oia->ia_ifp;
   1293 
   1294 		for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
   1295 			next = in6m->in6m_entry.le_next;
   1296 			LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
   1297 		}
   1298 
   1299 		if (mk->mk_head.lh_first != NULL) {
   1300 			LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
   1301 		}
   1302 		else {
   1303 			FREE(mk, M_IPMADDR);
   1304 		}
   1305 	}
   1306 }
   1307 
   1308 /*
   1309  * Continuation of multicast address hack:
   1310  * If there was a multicast group list previously saved for this interface,
   1311  * then we re-attach it to the first address configured on the i/f.
   1312  */
   1313 void
   1314 in6_restoremkludge(ia, ifp)
   1315 	struct in6_ifaddr *ia;
   1316 	struct ifnet *ifp;
   1317 {
   1318 	struct multi6_kludge *mk;
   1319 
   1320 	for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
   1321 		if (mk->mk_ifp == ifp) {
   1322 			struct in6_multi *in6m, *next;
   1323 
   1324 			for (in6m = mk->mk_head.lh_first; in6m; in6m = next){
   1325 				next = in6m->in6m_entry.le_next;
   1326 				LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
   1327 						 in6m, in6m_entry);
   1328 			}
   1329 			LIST_REMOVE(mk, mk_entry);
   1330 			free(mk, M_IPMADDR);
   1331 			break;
   1332 		}
   1333 	}
   1334 }
   1335 
   1336 /*
   1337  * Add an address to the list of IP6 multicast addresses for a
   1338  * given interface.
   1339  */
   1340 struct	in6_multi *
   1341 in6_addmulti(maddr6, ifp, errorp)
   1342 	register struct in6_addr *maddr6;
   1343 	register struct ifnet *ifp;
   1344 	int *errorp;
   1345 {
   1346 	struct	in6_ifaddr *ia;
   1347 	struct	in6_ifreq ifr;
   1348 	struct	in6_multi *in6m;
   1349 	int	s = splsoftnet();
   1350 
   1351 	*errorp = 0;
   1352 	/*
   1353 	 * See if address already in list.
   1354 	 */
   1355 	IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
   1356 	if (in6m != NULL) {
   1357 		/*
   1358 		 * Found it; just increment the refrence count.
   1359 		 */
   1360 		in6m->in6m_refcount++;
   1361 	} else {
   1362 		/*
   1363 		 * New address; allocate a new multicast record
   1364 		 * and link it into the interface's multicast list.
   1365 		 */
   1366 		in6m = (struct in6_multi *)
   1367 			malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
   1368 		if (in6m == NULL) {
   1369 			splx(s);
   1370 			*errorp = ENOBUFS;
   1371 			return(NULL);
   1372 		}
   1373 		in6m->in6m_addr = *maddr6;
   1374 		in6m->in6m_ifp = ifp;
   1375 		in6m->in6m_refcount = 1;
   1376 		IFP_TO_IA6(ifp, ia);
   1377 		if (ia == NULL) {
   1378 			free(in6m, M_IPMADDR);
   1379 			splx(s);
   1380 			*errorp = EADDRNOTAVAIL; /* appropriate? */
   1381 			return(NULL);
   1382 		}
   1383 		in6m->in6m_ia = ia;
   1384 		ia->ia_ifa.ifa_refcnt++; /* gain a reference */
   1385 		LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
   1386 
   1387 		/*
   1388 		 * Ask the network driver to update its multicast reception
   1389 		 * filter appropriately for the new address.
   1390 		 */
   1391 		bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
   1392 		ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
   1393 		ifr.ifr_addr.sin6_family = AF_INET6;
   1394 		ifr.ifr_addr.sin6_addr = *maddr6;
   1395 		if (ifp->if_ioctl == NULL)
   1396 			*errorp = ENXIO; /* XXX: appropriate? */
   1397 		else
   1398 			*errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
   1399 						    (caddr_t)&ifr);
   1400 		if (*errorp) {
   1401 			LIST_REMOVE(in6m, in6m_entry);
   1402 			free(in6m, M_IPMADDR);
   1403 			splx(s);
   1404 			return(NULL);
   1405 		}
   1406 		/*
   1407 		 * Let MLD6 know that we have joined a new IP6 multicast
   1408 		 * group.
   1409 		 */
   1410 		mld6_start_listening(in6m);
   1411 	}
   1412 	splx(s);
   1413 	return(in6m);
   1414 }
   1415 
   1416 /*
   1417  * Delete a multicast address record.
   1418  */
   1419 void
   1420 in6_delmulti(in6m)
   1421 	struct in6_multi *in6m;
   1422 {
   1423 	struct	in6_ifreq ifr;
   1424 	int	s = splsoftnet();
   1425 
   1426 	if (--in6m->in6m_refcount == 0) {
   1427 		/*
   1428 		 * No remaining claims to this record; let MLD6 know
   1429 		 * that we are leaving the multicast group.
   1430 		 */
   1431 		mld6_stop_listening(in6m);
   1432 
   1433 		/*
   1434 		 * Unlink from list.
   1435 		 */
   1436 		LIST_REMOVE(in6m, in6m_entry);
   1437 		IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
   1438 
   1439 		/*
   1440 		 * Notify the network driver to update its multicast
   1441 		 * reception filter.
   1442 		 */
   1443 		bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
   1444 		ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
   1445 		ifr.ifr_addr.sin6_family = AF_INET6;
   1446 		ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
   1447 		(*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
   1448 					    SIOCDELMULTI, (caddr_t)&ifr);
   1449 		free(in6m, M_IPMADDR);
   1450 	}
   1451 	splx(s);
   1452 }
   1453 
   1454 /*
   1455  * Find an IPv6 interface link-local address specific to an interface.
   1456  */
   1457 struct in6_ifaddr *
   1458 in6ifa_ifpforlinklocal(ifp)
   1459 	struct ifnet *ifp;
   1460 {
   1461 	register struct ifaddr *ifa;
   1462 
   1463 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1464 	{
   1465 		if (ifa->ifa_addr == NULL)
   1466 			continue;	/* just for safety */
   1467 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1468 			continue;
   1469 		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)))
   1470 			break;
   1471 	}
   1472 
   1473 	return((struct in6_ifaddr *)ifa);
   1474 }
   1475 
   1476 
   1477 /*
   1478  * find the internet address corresponding to a given interface and address.
   1479  */
   1480 struct in6_ifaddr *
   1481 in6ifa_ifpwithaddr(ifp, addr)
   1482 	struct ifnet *ifp;
   1483 	struct in6_addr *addr;
   1484 {
   1485 	register struct ifaddr *ifa;
   1486 
   1487 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1488 	{
   1489 		if (ifa->ifa_addr == NULL)
   1490 			continue;	/* just for safety */
   1491 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1492 			continue;
   1493 		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
   1494 			break;
   1495 	}
   1496 
   1497 	return((struct in6_ifaddr *)ifa);
   1498 }
   1499 
   1500 /*
   1501  * Convert IP6 address to printable (loggable) representation.
   1502  */
   1503 static char digits[] = "0123456789abcdef";
   1504 static int ip6round = 0;
   1505 char *
   1506 ip6_sprintf(addr)
   1507 register struct in6_addr *addr;
   1508 {
   1509 	static char ip6buf[8][48];
   1510 	register int i;
   1511 	register char *cp;
   1512 	register u_short *a = (u_short *)addr;
   1513 	register u_char *d;
   1514 	int dcolon = 0;
   1515 
   1516 	ip6round = (ip6round + 1) & 7;
   1517 	cp = ip6buf[ip6round];
   1518 
   1519 	for (i = 0; i < 8; i++) {
   1520 		if (dcolon == 1) {
   1521 			if (*a == 0) {
   1522 				if (i == 7)
   1523 					*cp++ = ':';
   1524 				a++;
   1525 				continue;
   1526 			} else
   1527 				dcolon = 2;
   1528 		}
   1529 		if (*a == 0) {
   1530 			if (dcolon == 0 && *(a + 1) == 0) {
   1531 				if (i == 0)
   1532 					*cp++ = ':';
   1533 				*cp++ = ':';
   1534 				dcolon = 1;
   1535 			} else {
   1536 				*cp++ = '0';
   1537 				*cp++ = ':';
   1538 			}
   1539 			a++;
   1540 			continue;
   1541 		}
   1542 		d = (u_char *)a;
   1543 		*cp++ = digits[*d >> 4];
   1544 		*cp++ = digits[*d++ & 0xf];
   1545 		*cp++ = digits[*d >> 4];
   1546 		*cp++ = digits[*d & 0xf];
   1547 		*cp++ = ':';
   1548 		a++;
   1549 	}
   1550 	*--cp = 0;
   1551 	return(ip6buf[ip6round]);
   1552 }
   1553 
   1554 int
   1555 in6_localaddr(in6)
   1556 	struct in6_addr *in6;
   1557 {
   1558 	struct in6_ifaddr *ia;
   1559 
   1560 	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
   1561 		return 1;
   1562 
   1563 	for (ia = in6_ifaddr; ia; ia = ia->ia_next)
   1564 		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
   1565 					      &ia->ia_prefixmask.sin6_addr))
   1566 			return 1;
   1567 
   1568 	return (0);
   1569 }
   1570 
   1571 /*
   1572  * Get a scope of the address. Node-local, link-local, site-local or global.
   1573  */
   1574 int
   1575 in6_addrscope (addr)
   1576 struct in6_addr *addr;
   1577 {
   1578 	int scope;
   1579 
   1580 	if (addr->s6_addr8[0] == 0xfe) {
   1581 		scope = addr->s6_addr8[1] & 0xc0;
   1582 
   1583 		switch (scope) {
   1584 		case 0x80:
   1585 			return IPV6_ADDR_SCOPE_LINKLOCAL;
   1586 			break;
   1587 		case 0xc0:
   1588 			return IPV6_ADDR_SCOPE_SITELOCAL;
   1589 			break;
   1590 		default:
   1591 			return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
   1592 			break;
   1593 		}
   1594 	}
   1595 
   1596 
   1597 	if (addr->s6_addr8[0] == 0xff) {
   1598 		scope = addr->s6_addr8[1] & 0x0f;
   1599 
   1600 		/*
   1601 		 * due to other scope such as reserved,
   1602 		 * return scope doesn't work.
   1603 		 */
   1604 		switch (scope) {
   1605 		case IPV6_ADDR_SCOPE_NODELOCAL:
   1606 			return IPV6_ADDR_SCOPE_NODELOCAL;
   1607 			break;
   1608 		case IPV6_ADDR_SCOPE_LINKLOCAL:
   1609 			return IPV6_ADDR_SCOPE_LINKLOCAL;
   1610 			break;
   1611 		case IPV6_ADDR_SCOPE_SITELOCAL:
   1612 			return IPV6_ADDR_SCOPE_SITELOCAL;
   1613 			break;
   1614 		default:
   1615 			return IPV6_ADDR_SCOPE_GLOBAL;
   1616 			break;
   1617 		}
   1618 	}
   1619 
   1620 	if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) {
   1621 		if (addr->s6_addr8[15] == 1) /* loopback */
   1622 			return IPV6_ADDR_SCOPE_NODELOCAL;
   1623 		if (addr->s6_addr8[15] == 0) /* unspecified */
   1624 			return IPV6_ADDR_SCOPE_LINKLOCAL;
   1625 	}
   1626 
   1627 	return IPV6_ADDR_SCOPE_GLOBAL;
   1628 }
   1629 
   1630 /*
   1631  * return length of part which dst and src are equal
   1632  * hard coding...
   1633  */
   1634 
   1635 int
   1636 in6_matchlen(src, dst)
   1637 struct in6_addr *src, *dst;
   1638 {
   1639 	int match = 0;
   1640 	u_char *s = (u_char *)src, *d = (u_char *)dst;
   1641 	u_char *lim = s + 16, r;
   1642 
   1643 	while (s < lim)
   1644 		if ((r = (*d++ ^ *s++)) != 0) {
   1645 			while (r < 128) {
   1646 				match++;
   1647 				r <<= 1;
   1648 			}
   1649 			break;
   1650 		} else
   1651 			match += 8;
   1652 	return match;
   1653 }
   1654 
   1655 int
   1656 in6_are_prefix_equal(p1, p2, len)
   1657 	struct in6_addr *p1, *p2;
   1658 	int len;
   1659 {
   1660 	int bytelen, bitlen;
   1661 
   1662 	/* sanity check */
   1663 	if (0 > len || len > 128) {
   1664 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
   1665 		    len);
   1666 		return(0);
   1667 	}
   1668 
   1669 	bytelen = len / 8;
   1670 	bitlen = len % 8;
   1671 
   1672 	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
   1673 		return(0);
   1674 	if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
   1675 	    p2->s6_addr[bytelen] >> (8 - bitlen))
   1676 		return(0);
   1677 
   1678 	return(1);
   1679 }
   1680 
   1681 void
   1682 in6_prefixlen2mask(maskp, len)
   1683 	struct in6_addr *maskp;
   1684 	int len;
   1685 {
   1686 	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
   1687 	int bytelen, bitlen, i;
   1688 
   1689 	/* sanity check */
   1690 	if (0 > len || len > 128) {
   1691 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
   1692 		    len);
   1693 		return;
   1694 	}
   1695 
   1696 	bzero(maskp, sizeof(*maskp));
   1697 	bytelen = len / 8;
   1698 	bitlen = len % 8;
   1699 	for (i = 0; i < bytelen; i++)
   1700 		maskp->s6_addr[i] = 0xff;
   1701 	if (bitlen)
   1702 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
   1703 }
   1704 
   1705 /*
   1706  * return the best address out of the same scope
   1707  */
   1708 
   1709 struct in6_ifaddr *
   1710 in6_ifawithscope(ifp, dst)
   1711 	register struct ifnet *ifp;
   1712 	register struct in6_addr *dst;
   1713 {
   1714 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
   1715 	struct ifaddr *ifa;
   1716 	struct in6_ifaddr *besta = NULL, *ia;
   1717 	struct in6_ifaddr *dep[2];	/*last-resort: deprecated*/
   1718 
   1719 	dep[0] = dep[1] = NULL;
   1720 
   1721 	/*
   1722 	 * We first look for addresses in the same scope.
   1723 	 * If there is one, return it.
   1724 	 * If two or more, return one which matches the dst longest.
   1725 	 * If none, return one of global addresses assigned other ifs.
   1726 	 */
   1727 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1728 	{
   1729 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1730 			continue;
   1731 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
   1732 			continue; /* XXX: is there any case to allow anycast? */
   1733 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
   1734 			continue; /* don't use this interface */
   1735 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
   1736 			continue;
   1737 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
   1738 			if (ip6_use_deprecated)
   1739 				dep[0] = (struct in6_ifaddr *)ifa;
   1740 			continue;
   1741 		}
   1742 
   1743 		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
   1744 			/*
   1745 			 * call in6_matchlen() as few as possible
   1746 			 */
   1747 			if (besta) {
   1748 				if (blen == -1)
   1749 					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
   1750 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
   1751 				if (tlen > blen) {
   1752 					blen = tlen;
   1753 					besta = (struct in6_ifaddr *)ifa;
   1754 				}
   1755 			} else
   1756 				besta = (struct in6_ifaddr *)ifa;
   1757 		}
   1758 	}
   1759 	if (besta)
   1760 		return besta;
   1761 
   1762 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
   1763 		if (IPV6_ADDR_SCOPE_GLOBAL !=
   1764 		    in6_addrscope(&(ia->ia_addr.sin6_addr)))
   1765 			continue;
   1766 		/* XXX: is there any case to allow anycast? */
   1767 		if ((ia->ia6_flags & IN6_IFF_ANYCAST) != 0)
   1768 			continue;
   1769 		if ((ia->ia6_flags & IN6_IFF_NOTREADY) != 0)
   1770 			continue;
   1771 		if ((ia->ia6_flags & IN6_IFF_DETACHED) != 0)
   1772 			continue;
   1773 		if ((ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
   1774 			if (ip6_use_deprecated)
   1775 				dep[1] = (struct in6_ifaddr *)ifa;
   1776 			continue;
   1777 		}
   1778 		return ia;
   1779 	}
   1780 
   1781 	/* use the last-resort values, that are, deprecated addresses */
   1782 	if (dep[0])
   1783 		return dep[0];
   1784 	if (dep[1])
   1785 		return dep[1];
   1786 
   1787 	return NULL;
   1788 }
   1789 
   1790 /*
   1791  * return the best address out of the same scope. if no address was
   1792  * found, return the first valid address from designated IF.
   1793  */
   1794 
   1795 struct in6_ifaddr *
   1796 in6_ifawithifp(ifp, dst)
   1797 	register struct ifnet *ifp;
   1798 	register struct in6_addr *dst;
   1799 {
   1800 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
   1801 	struct ifaddr *ifa;
   1802 	struct in6_ifaddr *besta = 0;
   1803 	struct in6_ifaddr *dep[2];	/*last-resort: deprecated*/
   1804 
   1805 	dep[0] = dep[1] = NULL;
   1806 
   1807 	/*
   1808 	 * We first look for addresses in the same scope.
   1809 	 * If there is one, return it.
   1810 	 * If two or more, return one which matches the dst longest.
   1811 	 * If none, return one of global addresses assigned other ifs.
   1812 	 */
   1813 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1814 	{
   1815 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1816 			continue;
   1817 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
   1818 			continue; /* XXX: is there any case to allow anycast? */
   1819 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
   1820 			continue; /* don't use this interface */
   1821 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
   1822 			continue;
   1823 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
   1824 			if (ip6_use_deprecated)
   1825 				dep[0] = (struct in6_ifaddr *)ifa;
   1826 			continue;
   1827 		}
   1828 
   1829 		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
   1830 			/*
   1831 			 * call in6_matchlen() as few as possible
   1832 			 */
   1833 			if (besta) {
   1834 				if (blen == -1)
   1835 					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
   1836 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
   1837 				if (tlen > blen) {
   1838 					blen = tlen;
   1839 					besta = (struct in6_ifaddr *)ifa;
   1840 				}
   1841 			} else
   1842 				besta = (struct in6_ifaddr *)ifa;
   1843 		}
   1844 	}
   1845 	if (besta)
   1846 		return(besta);
   1847 
   1848 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1849 	{
   1850 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1851 			continue;
   1852 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
   1853 			continue; /* XXX: is there any case to allow anycast? */
   1854 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
   1855 			continue; /* don't use this interface */
   1856 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
   1857 			continue;
   1858 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
   1859 			if (ip6_use_deprecated)
   1860 				dep[1] = (struct in6_ifaddr *)ifa;
   1861 			continue;
   1862 		}
   1863 
   1864 		return (struct in6_ifaddr *)ifa;
   1865 	}
   1866 
   1867 	/* use the last-resort values, that are, deprecated addresses */
   1868 	if (dep[0])
   1869 		return dep[0];
   1870 	if (dep[1])
   1871 		return dep[1];
   1872 
   1873 	return NULL;
   1874 }
   1875 
   1876 /*
   1877  * perform DAD when interface becomes IFF_UP.
   1878  */
   1879 void
   1880 in6_if_up(ifp)
   1881 	struct ifnet *ifp;
   1882 {
   1883 	struct ifaddr *ifa;
   1884 	struct in6_ifaddr *ia;
   1885 	struct sockaddr_dl *sdl;
   1886 	int type;
   1887 	struct ether_addr ea;
   1888 	int off;
   1889 	int dad_delay;		/* delay ticks before DAD output */
   1890 
   1891 	bzero(&ea, sizeof(ea));
   1892 	sdl = NULL;
   1893 
   1894 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1895 	{
   1896 		if (ifa->ifa_addr->sa_family == AF_INET6
   1897 		 && IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
   1898 			goto dad;
   1899 		}
   1900 		if (ifa->ifa_addr->sa_family != AF_LINK)
   1901 			continue;
   1902 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
   1903 		break;
   1904 	}
   1905 
   1906 	switch (ifp->if_type) {
   1907 	case IFT_LOOP:
   1908 		in6_ifattach(ifp, IN6_IFT_LOOP, NULL, 1);
   1909 		break;
   1910 	case IFT_SLIP:
   1911 	case IFT_PPP:
   1912 	case IFT_GIF:
   1913 	case IFT_FAITH:
   1914 		type = IN6_IFT_P2P;
   1915 		in6_ifattach(ifp, type, 0, 1);
   1916 		break;
   1917 	case IFT_ETHER:
   1918 	case IFT_FDDI:
   1919 	case IFT_ATM:
   1920 		type = IN6_IFT_802;
   1921 		if (sdl == NULL)
   1922 			break;
   1923 		off = sdl->sdl_nlen;
   1924 		if (bcmp(&sdl->sdl_data[off], &ea, sizeof(ea)) != 0)
   1925 			in6_ifattach(ifp, type, LLADDR(sdl), 0);
   1926 		break;
   1927 	case IFT_ARCNET:
   1928 		type = IN6_IFT_ARCNET;
   1929 		if (sdl == NULL)
   1930 			break;
   1931 		off = sdl->sdl_nlen;
   1932 		if (sdl->sdl_data[off] != 0)	/* XXX ?: */
   1933 			in6_ifattach(ifp, type, LLADDR(sdl), 0);
   1934 		break;
   1935 	default:
   1936 		break;
   1937 	}
   1938 
   1939 dad:
   1940 	dad_delay = 0;
   1941 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
   1942 	{
   1943 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1944 			continue;
   1945 		ia = (struct in6_ifaddr *)ifa;
   1946 		if (ia->ia6_flags & IN6_IFF_TENTATIVE)
   1947 			nd6_dad_start(ifa, &dad_delay);
   1948 	}
   1949 }
   1950 
   1951 /*
   1952  * Calculate max IPv6 MTU through all the interfaces and store it
   1953  * to in6_maxmtu.
   1954  */
   1955 void
   1956 in6_setmaxmtu()
   1957 {
   1958 	unsigned long maxmtu = 0;
   1959 	struct ifnet *ifp;
   1960 
   1961 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
   1962 	{
   1963 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
   1964 		    nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
   1965 			maxmtu =  nd_ifinfo[ifp->if_index].linkmtu;
   1966 	}
   1967 	if (maxmtu)	/* update only when maxmtu is positive */
   1968 		in6_maxmtu = maxmtu;
   1969 }
   1970