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