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