Home | History | Annotate | Line # | Download | only in ifconfig
af_inet6.c revision 1.14
      1  1.14    dyoung /*	$NetBSD: af_inet6.c,v 1.14 2008/05/11 22:07:23 dyoung Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4   1.1   thorpej  * Copyright (c) 1983, 1993
      5   1.1   thorpej  *      The Regents of the University of California.  All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.1   thorpej  * modification, are permitted provided that the following conditions
      9   1.1   thorpej  * are met:
     10   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.1   thorpej  * 3. Neither the name of the University nor the names of its contributors
     16   1.1   thorpej  *    may be used to endorse or promote products derived from this software
     17   1.1   thorpej  *    without specific prior written permission.
     18   1.1   thorpej  *
     19   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1   thorpej  * SUCH DAMAGE.
     30   1.1   thorpej  */
     31   1.1   thorpej 
     32   1.1   thorpej #include <sys/cdefs.h>
     33   1.1   thorpej #ifndef lint
     34  1.14    dyoung __RCSID("$NetBSD: af_inet6.c,v 1.14 2008/05/11 22:07:23 dyoung Exp $");
     35   1.1   thorpej #endif /* not lint */
     36   1.1   thorpej 
     37   1.1   thorpej #include <sys/param.h>
     38   1.1   thorpej #include <sys/ioctl.h>
     39   1.1   thorpej #include <sys/socket.h>
     40   1.1   thorpej 
     41   1.1   thorpej #include <net/if.h>
     42   1.1   thorpej #include <netinet/in.h>
     43   1.1   thorpej #include <netinet/in_var.h>
     44   1.1   thorpej #include <netinet6/nd6.h>
     45   1.1   thorpej 
     46   1.1   thorpej #include <err.h>
     47   1.1   thorpej #include <errno.h>
     48   1.1   thorpej #include <ifaddrs.h>
     49   1.1   thorpej #include <netdb.h>
     50   1.1   thorpej #include <string.h>
     51   1.1   thorpej #include <stdlib.h>
     52   1.1   thorpej #include <stdio.h>
     53   1.5  christos #include <util.h>
     54   1.1   thorpej 
     55   1.8    dyoung #include "env.h"
     56   1.8    dyoung #include "parse.h"
     57   1.1   thorpej #include "extern.h"
     58   1.1   thorpej #include "af_inet6.h"
     59  1.13    dyoung #include "af_inetany.h"
     60   1.1   thorpej 
     61   1.8    dyoung struct in6_ifreq    in6_ridreq = {
     62   1.8    dyoung 	.ifr_addr = {
     63   1.8    dyoung 		.sin6_family = AF_INET6,
     64   1.8    dyoung 		.sin6_addr = {
     65   1.8    dyoung 			.s6_addr =
     66   1.8    dyoung 			    {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
     67   1.8    dyoung 		}
     68   1.8    dyoung 	}
     69   1.8    dyoung };
     70   1.1   thorpej 
     71   1.6    dyoung struct in6_aliasreq in6_addreq = {
     72   1.6    dyoung 	.ifra_prefixmask = {
     73  1.11    dyoung 		.sin6_len = sizeof(in6_addreq.ifra_prefixmask),
     74   1.6    dyoung 		.sin6_addr = {
     75   1.6    dyoung 			.s6_addr =
     76   1.6    dyoung 			    {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}},
     77   1.6    dyoung 	.ifra_lifetime = {
     78   1.6    dyoung 		  .ia6t_pltime = ND6_INFINITE_LIFETIME
     79   1.6    dyoung 		, .ia6t_vltime = ND6_INFINITE_LIFETIME
     80   1.6    dyoung 	}
     81   1.6    dyoung };
     82   1.1   thorpej 
     83  1.12    dyoung static const struct kwinst ia6flagskw[] = {
     84  1.12    dyoung 	  IFKW("anycast",	IN6_IFF_ANYCAST)
     85  1.12    dyoung 	, IFKW("tentative",	IN6_IFF_TENTATIVE)
     86  1.12    dyoung 	, IFKW("deprecated",	IN6_IFF_DEPRECATED)
     87  1.12    dyoung };
     88  1.12    dyoung 
     89  1.12    dyoung static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime,
     90  1.12    dyoung     "pltime", 0, setia6pltime, "pltime", &command_root.pb_parser);
     91  1.12    dyoung 
     92  1.12    dyoung static struct pinteger parse_vltime = PINTEGER_INITIALIZER(&parse_vltime,
     93  1.12    dyoung     "vltime", 0, setia6vltime, "vltime", &command_root.pb_parser);
     94  1.12    dyoung 
     95  1.12    dyoung static const struct kwinst inet6kw[] = {
     96  1.12    dyoung 	  {.k_word = "pltime", .k_nextparser = &parse_pltime.pi_parser}
     97  1.12    dyoung 	, {.k_word = "vltime", .k_nextparser = &parse_vltime.pi_parser}
     98  1.14    dyoung 	, {.k_word = "eui64", .k_key = "eui64", .k_type = KW_T_BOOL,
     99  1.14    dyoung 	   .k_bool = true, .k_exec = setia6eui64,
    100  1.12    dyoung 	   .k_nextparser = &command_root.pb_parser}
    101  1.12    dyoung };
    102  1.12    dyoung 
    103  1.12    dyoung struct pkw ia6flags = PKW_INITIALIZER(&ia6flags, "ia6flags", setia6flags,
    104  1.12    dyoung     "ia6flag", ia6flagskw, __arraycount(ia6flagskw), &command_root.pb_parser);
    105  1.12    dyoung struct pkw inet6 = PKW_INITIALIZER(&inet6, "IPv6 keywords", NULL,
    106  1.12    dyoung     NULL, inet6kw, __arraycount(inet6kw), NULL);
    107  1.12    dyoung 
    108  1.14    dyoung static void in6_delscopeid(struct sockaddr_in6 *sin6);
    109   1.8    dyoung static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *);
    110   1.8    dyoung static void in6_alias(const char *, prop_dictionary_t, prop_dictionary_t,
    111   1.8    dyoung     struct in6_ifreq *);
    112   1.8    dyoung 
    113   1.1   thorpej static char *
    114   1.1   thorpej sec2str(time_t total)
    115   1.1   thorpej {
    116   1.1   thorpej 	static char result[256];
    117   1.1   thorpej 	int days, hours, mins, secs;
    118   1.1   thorpej 	int first = 1;
    119   1.1   thorpej 	char *p = result;
    120   1.1   thorpej 	char *end = &result[sizeof(result)];
    121   1.1   thorpej 	int n;
    122   1.1   thorpej 
    123   1.1   thorpej 	if (0) {	/*XXX*/
    124   1.1   thorpej 		days = total / 3600 / 24;
    125   1.1   thorpej 		hours = (total / 3600) % 24;
    126   1.1   thorpej 		mins = (total / 60) % 60;
    127   1.1   thorpej 		secs = total % 60;
    128   1.1   thorpej 
    129   1.1   thorpej 		if (days) {
    130   1.1   thorpej 			first = 0;
    131   1.1   thorpej 			n = snprintf(p, end - p, "%dd", days);
    132   1.1   thorpej 			if (n < 0 || n >= end - p)
    133   1.1   thorpej 				return(result);
    134   1.1   thorpej 			p += n;
    135   1.1   thorpej 		}
    136   1.1   thorpej 		if (!first || hours) {
    137   1.1   thorpej 			first = 0;
    138   1.1   thorpej 			n = snprintf(p, end - p, "%dh", hours);
    139   1.1   thorpej 			if (n < 0 || n >= end - p)
    140   1.1   thorpej 				return(result);
    141   1.1   thorpej 			p += n;
    142   1.1   thorpej 		}
    143   1.1   thorpej 		if (!first || mins) {
    144   1.1   thorpej 			first = 0;
    145   1.1   thorpej 			n = snprintf(p, end - p, "%dm", mins);
    146   1.1   thorpej 			if (n < 0 || n >= end - p)
    147   1.1   thorpej 				return(result);
    148   1.1   thorpej 			p += n;
    149   1.1   thorpej 		}
    150   1.1   thorpej 		snprintf(p, end - p, "%ds", secs);
    151   1.1   thorpej 	} else
    152   1.1   thorpej 		snprintf(p, end - p, "%lu", (u_long)total);
    153   1.1   thorpej 
    154   1.1   thorpej 	return(result);
    155   1.1   thorpej }
    156   1.1   thorpej 
    157   1.1   thorpej static int
    158   1.1   thorpej prefix(void *val, int size)
    159   1.1   thorpej {
    160   1.1   thorpej 	u_char *pname = (u_char *)val;
    161   1.1   thorpej 	int byte, bit, plen = 0;
    162   1.1   thorpej 
    163   1.1   thorpej 	for (byte = 0; byte < size; byte++, plen += 8)
    164   1.1   thorpej 		if (pname[byte] != 0xff)
    165   1.1   thorpej 			break;
    166   1.1   thorpej 	if (byte == size)
    167   1.1   thorpej 		return (plen);
    168   1.1   thorpej 	for (bit = 7; bit != 0; bit--, plen++)
    169   1.1   thorpej 		if (!(pname[byte] & (1 << bit)))
    170   1.1   thorpej 			break;
    171   1.1   thorpej 	for (; bit != 0; bit--)
    172   1.1   thorpej 		if (pname[byte] & (1 << bit))
    173   1.1   thorpej 			return(0);
    174   1.1   thorpej 	byte++;
    175   1.1   thorpej 	for (; byte < size; byte++)
    176   1.1   thorpej 		if (pname[byte])
    177   1.1   thorpej 			return(0);
    178   1.1   thorpej 	return (plen);
    179   1.1   thorpej }
    180   1.1   thorpej 
    181   1.8    dyoung int
    182   1.8    dyoung setia6flags_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    183   1.1   thorpej {
    184  1.10    dyoung 	int64_t ia6flag;
    185   1.1   thorpej 
    186  1.10    dyoung 	if (!prop_dictionary_get_int64(env, "ia6flag", &ia6flag)) {
    187   1.8    dyoung 		errno = ENOENT;
    188   1.8    dyoung 		return -1;
    189   1.8    dyoung 	}
    190   1.8    dyoung 
    191   1.8    dyoung 	if (ia6flag < 0) {
    192   1.8    dyoung 		ia6flag = -ia6flag;
    193   1.8    dyoung 		ifra->ifra_flags &= ~ia6flag;
    194   1.1   thorpej 	} else
    195   1.8    dyoung 		ifra->ifra_flags |= ia6flag;
    196   1.8    dyoung 	return 0;
    197   1.8    dyoung }
    198   1.8    dyoung 
    199   1.8    dyoung int
    200   1.8    dyoung setia6flags(prop_dictionary_t env, prop_dictionary_t xenv)
    201   1.8    dyoung {
    202   1.8    dyoung 	return setia6flags_impl(env, &in6_addreq);
    203   1.1   thorpej }
    204   1.1   thorpej 
    205   1.8    dyoung int
    206   1.8    dyoung setia6pltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    207   1.1   thorpej {
    208  1.10    dyoung 	int64_t pltime;
    209   1.8    dyoung 
    210  1.10    dyoung 	if (!prop_dictionary_get_int64(env, "pltime", &pltime)) {
    211   1.8    dyoung 		errno = ENOENT;
    212   1.8    dyoung 		return -1;
    213   1.8    dyoung 	}
    214   1.1   thorpej 
    215  1.10    dyoung 	return setia6lifetime(env, pltime,
    216   1.8    dyoung 	    &ifra->ifra_lifetime.ia6t_preferred,
    217   1.8    dyoung 	    &ifra->ifra_lifetime.ia6t_pltime);
    218   1.8    dyoung }
    219   1.8    dyoung 
    220   1.8    dyoung int
    221   1.8    dyoung setia6pltime(prop_dictionary_t env, prop_dictionary_t xenv)
    222   1.8    dyoung {
    223   1.8    dyoung 	return setia6pltime_impl(env, &in6_addreq);
    224   1.1   thorpej }
    225   1.1   thorpej 
    226   1.8    dyoung int
    227   1.8    dyoung setia6vltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    228   1.1   thorpej {
    229  1.10    dyoung 	int64_t vltime;
    230   1.8    dyoung 
    231  1.10    dyoung 	if (!prop_dictionary_get_int64(env, "vltime", &vltime)) {
    232   1.8    dyoung 		errno = ENOENT;
    233   1.8    dyoung 		return -1;
    234   1.8    dyoung 	}
    235   1.1   thorpej 
    236  1.10    dyoung 	return setia6lifetime(env, vltime,
    237   1.8    dyoung 		&ifra->ifra_lifetime.ia6t_expire,
    238   1.8    dyoung 		&ifra->ifra_lifetime.ia6t_vltime);
    239   1.8    dyoung }
    240   1.8    dyoung 
    241   1.8    dyoung int
    242   1.8    dyoung setia6vltime(prop_dictionary_t env, prop_dictionary_t xenv)
    243   1.8    dyoung {
    244   1.8    dyoung 	return setia6vltime_impl(env, &in6_addreq);
    245   1.1   thorpej }
    246   1.1   thorpej 
    247   1.8    dyoung static int
    248   1.8    dyoung setia6lifetime(prop_dictionary_t env, int64_t val, time_t *timep,
    249   1.8    dyoung     uint32_t *ivalp)
    250   1.1   thorpej {
    251   1.8    dyoung 	time_t t;
    252   1.8    dyoung 	int af;
    253   1.8    dyoung 
    254   1.8    dyoung 	if ((af = getaf(env)) == -1 || af != AF_INET6) {
    255   1.8    dyoung 		errx(EXIT_FAILURE,
    256   1.8    dyoung 		    "inet6 address lifetime not allowed for the AF");
    257   1.8    dyoung 	}
    258   1.1   thorpej 
    259   1.1   thorpej 	t = time(NULL);
    260   1.8    dyoung 	*timep = t + val;
    261   1.8    dyoung 	*ivalp = val;
    262   1.8    dyoung 	return 0;
    263   1.1   thorpej }
    264   1.1   thorpej 
    265   1.8    dyoung int
    266   1.8    dyoung setia6eui64_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    267   1.1   thorpej {
    268  1.14    dyoung 	char buf[2][80];
    269   1.1   thorpej 	struct ifaddrs *ifap, *ifa;
    270   1.1   thorpej 	const struct sockaddr_in6 *sin6 = NULL;
    271   1.1   thorpej 	const struct in6_addr *lladdr = NULL;
    272   1.1   thorpej 	struct in6_addr *in6;
    273   1.8    dyoung 	const char *ifname;
    274  1.14    dyoung 	bool doit = false;
    275   1.8    dyoung 	int af;
    276   1.1   thorpej 
    277  1.14    dyoung 	if (!prop_dictionary_get_bool(env, "eui64", &doit) || !doit) {
    278  1.14    dyoung 		errno = ENOENT;
    279  1.14    dyoung 		return -1;
    280  1.14    dyoung 	}
    281  1.14    dyoung 
    282   1.8    dyoung 	if ((ifname = getifname(env)) == NULL)
    283   1.8    dyoung 		return -1;
    284   1.8    dyoung 
    285   1.8    dyoung 	af = getaf(env);
    286   1.8    dyoung 	if (af != AF_INET6) {
    287   1.8    dyoung 		errx(EXIT_FAILURE,
    288   1.8    dyoung 		    "eui64 address modifier not allowed for the AF");
    289   1.8    dyoung 	}
    290   1.8    dyoung  	in6 = &ifra->ifra_addr.sin6_addr;
    291  1.14    dyoung 	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) {
    292  1.14    dyoung 		union {
    293  1.14    dyoung 			struct sockaddr_in6 sin6;
    294  1.14    dyoung 			struct sockaddr sa;
    295  1.14    dyoung 		} any = {.sin6 = {.sin6_family = AF_INET6}};
    296  1.14    dyoung 		memcpy(&any.sin6.sin6_addr, &in6addr_any,
    297  1.14    dyoung 		    sizeof(any.sin6.sin6_addr));
    298  1.14    dyoung 		(void)sockaddr_snprintf(buf[0], sizeof(buf[0]), "%a%%S",
    299  1.14    dyoung 		    &any.sa);
    300  1.14    dyoung 		(void)sockaddr_snprintf(buf[1], sizeof(buf[1]), "%a%%S",
    301  1.14    dyoung 		    (const struct sockaddr *)&ifra->ifra_addr);
    302  1.14    dyoung 		errx(EXIT_FAILURE, "interface index is already filled, %s | %s",
    303  1.14    dyoung 		    buf[0], buf[1]);
    304  1.14    dyoung 	}
    305   1.1   thorpej 	if (getifaddrs(&ifap) != 0)
    306   1.1   thorpej 		err(EXIT_FAILURE, "getifaddrs");
    307   1.1   thorpej 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    308   1.1   thorpej 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
    309   1.8    dyoung 		    strcmp(ifa->ifa_name, ifname) == 0) {
    310   1.1   thorpej 			sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
    311   1.1   thorpej 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    312   1.1   thorpej 				lladdr = &sin6->sin6_addr;
    313   1.1   thorpej 				break;
    314   1.1   thorpej 			}
    315   1.1   thorpej 		}
    316   1.1   thorpej 	}
    317   1.1   thorpej 	if (!lladdr)
    318   1.1   thorpej 		errx(EXIT_FAILURE, "could not determine link local address");
    319   1.1   thorpej 
    320   1.1   thorpej  	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
    321   1.1   thorpej 
    322   1.1   thorpej 	freeifaddrs(ifap);
    323   1.8    dyoung 	return 0;
    324   1.8    dyoung }
    325   1.8    dyoung 
    326   1.8    dyoung int
    327   1.8    dyoung setia6eui64(prop_dictionary_t env, prop_dictionary_t xenv)
    328   1.8    dyoung {
    329   1.8    dyoung 	return setia6eui64_impl(env, &in6_addreq);
    330   1.1   thorpej }
    331   1.1   thorpej 
    332  1.14    dyoung /* KAME idiosyncrasy */
    333  1.14    dyoung static void
    334  1.14    dyoung in6_delscopeid(struct sockaddr_in6 *sin6)
    335  1.14    dyoung {
    336  1.14    dyoung 	if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
    337  1.14    dyoung 	    sin6->sin6_scope_id == 0)
    338  1.14    dyoung 		return;
    339  1.14    dyoung 
    340  1.14    dyoung 	*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = htons(sin6->sin6_scope_id);
    341  1.14    dyoung 	sin6->sin6_scope_id = 0;
    342  1.14    dyoung }
    343  1.14    dyoung 
    344  1.14    dyoung /* KAME idiosyncrasy */
    345   1.1   thorpej void
    346   1.1   thorpej in6_fillscopeid(struct sockaddr_in6 *sin6)
    347   1.1   thorpej {
    348   1.1   thorpej 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    349   1.1   thorpej 		sin6->sin6_scope_id =
    350   1.1   thorpej 			ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
    351   1.1   thorpej 		sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
    352   1.1   thorpej 	}
    353   1.1   thorpej }
    354   1.1   thorpej 
    355   1.1   thorpej /* XXX not really an alias */
    356   1.1   thorpej void
    357   1.8    dyoung in6_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
    358   1.8    dyoung     struct in6_ifreq *creq)
    359   1.1   thorpej {
    360   1.8    dyoung 	struct in6_ifreq ifr6;
    361   1.1   thorpej 	struct sockaddr_in6 *sin6;
    362   1.1   thorpej 	char hbuf[NI_MAXHOST];
    363   1.1   thorpej 	u_int32_t scopeid;
    364   1.8    dyoung 	int s;
    365   1.1   thorpej 	const int niflag = NI_NUMERICHOST;
    366   1.8    dyoung 	unsigned short flags;
    367   1.1   thorpej 
    368   1.1   thorpej 	/* Get the non-alias address for this interface. */
    369   1.8    dyoung 	if ((s = getsock(AF_INET6)) == -1) {
    370   1.3      tron 		if (errno == EAFNOSUPPORT)
    371   1.1   thorpej 			return;
    372   1.1   thorpej 		err(EXIT_FAILURE, "socket");
    373   1.1   thorpej 	}
    374   1.1   thorpej 
    375   1.1   thorpej 	sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
    376   1.1   thorpej 
    377   1.1   thorpej 	in6_fillscopeid(sin6);
    378   1.1   thorpej 	scopeid = sin6->sin6_scope_id;
    379   1.1   thorpej 	if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
    380   1.1   thorpej 			hbuf, sizeof(hbuf), NULL, 0, niflag))
    381   1.1   thorpej 		strlcpy(hbuf, "", sizeof(hbuf));	/* some message? */
    382   1.1   thorpej 	printf("\tinet6 %s", hbuf);
    383   1.1   thorpej 
    384   1.8    dyoung 	if (getifflags(env, oenv, &flags) == -1)
    385   1.8    dyoung 		err(EXIT_FAILURE, "%s: getifflags", __func__);
    386   1.8    dyoung 
    387   1.1   thorpej 	if (flags & IFF_POINTOPOINT) {
    388   1.7    dyoung 		memset(&ifr6, 0, sizeof(ifr6));
    389   1.8    dyoung 		estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
    390   1.1   thorpej 		ifr6.ifr_addr = creq->ifr_addr;
    391   1.1   thorpej 		if (ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
    392   1.1   thorpej 			if (errno != EADDRNOTAVAIL)
    393   1.1   thorpej 				warn("SIOCGIFDSTADDR_IN6");
    394   1.7    dyoung 			memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
    395   1.1   thorpej 			ifr6.ifr_addr.sin6_family = AF_INET6;
    396   1.1   thorpej 			ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
    397   1.1   thorpej 		}
    398   1.1   thorpej 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
    399   1.1   thorpej 		in6_fillscopeid(sin6);
    400   1.1   thorpej 		hbuf[0] = '\0';
    401   1.1   thorpej 		if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
    402   1.1   thorpej 				hbuf, sizeof(hbuf), NULL, 0, niflag))
    403   1.1   thorpej 			strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
    404   1.1   thorpej 		printf(" -> %s", hbuf);
    405   1.1   thorpej 	}
    406   1.1   thorpej 
    407   1.7    dyoung 	memset(&ifr6, 0, sizeof(ifr6));
    408   1.8    dyoung 	estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
    409   1.1   thorpej 	ifr6.ifr_addr = creq->ifr_addr;
    410   1.1   thorpej 	if (ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
    411   1.1   thorpej 		if (errno != EADDRNOTAVAIL)
    412   1.1   thorpej 			warn("SIOCGIFNETMASK_IN6");
    413   1.1   thorpej 	} else {
    414   1.1   thorpej 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
    415   1.1   thorpej 		printf(" prefixlen %d", prefix(&sin6->sin6_addr,
    416   1.1   thorpej 					       sizeof(struct in6_addr)));
    417   1.1   thorpej 	}
    418   1.1   thorpej 
    419   1.7    dyoung 	memset(&ifr6, 0, sizeof(ifr6));
    420   1.8    dyoung 	estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
    421   1.1   thorpej 	ifr6.ifr_addr = creq->ifr_addr;
    422   1.1   thorpej 	if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
    423   1.1   thorpej 		if (errno != EADDRNOTAVAIL)
    424   1.1   thorpej 			warn("SIOCGIFAFLAG_IN6");
    425   1.1   thorpej 	} else {
    426   1.1   thorpej 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
    427   1.1   thorpej 			printf(" anycast");
    428   1.1   thorpej 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
    429   1.1   thorpej 			printf(" tentative");
    430   1.1   thorpej 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
    431   1.1   thorpej 			printf(" duplicated");
    432   1.1   thorpej 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
    433   1.1   thorpej 			printf(" detached");
    434   1.1   thorpej 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
    435   1.1   thorpej 			printf(" deprecated");
    436   1.1   thorpej 	}
    437   1.1   thorpej 
    438   1.1   thorpej 	if (scopeid)
    439   1.1   thorpej 		printf(" scopeid 0x%x", scopeid);
    440   1.1   thorpej 
    441   1.1   thorpej 	if (Lflag) {
    442   1.1   thorpej 		struct in6_addrlifetime *lifetime;
    443   1.7    dyoung 		memset(&ifr6, 0, sizeof(ifr6));
    444   1.8    dyoung 		estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
    445   1.1   thorpej 		ifr6.ifr_addr = creq->ifr_addr;
    446   1.1   thorpej 		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
    447   1.1   thorpej 		if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
    448   1.1   thorpej 			if (errno != EADDRNOTAVAIL)
    449   1.1   thorpej 				warn("SIOCGIFALIFETIME_IN6");
    450   1.1   thorpej 		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
    451   1.1   thorpej 			time_t t = time(NULL);
    452   1.1   thorpej 			printf(" pltime ");
    453   1.1   thorpej 			if (lifetime->ia6t_preferred) {
    454   1.1   thorpej 				printf("%s", lifetime->ia6t_preferred < t
    455   1.1   thorpej 					? "0"
    456   1.1   thorpej 					: sec2str(lifetime->ia6t_preferred - t));
    457   1.1   thorpej 			} else
    458   1.1   thorpej 				printf("infty");
    459   1.1   thorpej 
    460   1.1   thorpej 			printf(" vltime ");
    461   1.1   thorpej 			if (lifetime->ia6t_expire) {
    462   1.1   thorpej 				printf("%s", lifetime->ia6t_expire < t
    463   1.1   thorpej 					? "0"
    464   1.1   thorpej 					: sec2str(lifetime->ia6t_expire - t));
    465   1.1   thorpej 			} else
    466   1.1   thorpej 				printf("infty");
    467   1.1   thorpej 		}
    468   1.1   thorpej 	}
    469   1.1   thorpej 
    470   1.1   thorpej 	printf("\n");
    471   1.1   thorpej }
    472   1.1   thorpej 
    473   1.1   thorpej void
    474   1.9    dyoung in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
    475   1.1   thorpej {
    476   1.1   thorpej 	struct ifaddrs *ifap, *ifa;
    477   1.8    dyoung 	struct in6_ifreq ifr;
    478   1.8    dyoung 	const char *ifname;
    479   1.8    dyoung 
    480   1.8    dyoung 	if ((ifname = getifname(env)) == NULL)
    481   1.8    dyoung 		err(EXIT_FAILURE, "%s: getifname", __func__);
    482   1.1   thorpej 
    483   1.1   thorpej 	if (getifaddrs(&ifap) != 0)
    484   1.1   thorpej 		err(EXIT_FAILURE, "getifaddrs");
    485   1.1   thorpej 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    486   1.8    dyoung 		if (strcmp(ifname, ifa->ifa_name) != 0)
    487   1.1   thorpej 			continue;
    488   1.1   thorpej 		if (ifa->ifa_addr->sa_family != AF_INET6)
    489   1.1   thorpej 			continue;
    490   1.8    dyoung 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
    491   1.1   thorpej 			continue;
    492   1.1   thorpej 
    493   1.8    dyoung 		memset(&ifr, 0, sizeof(ifr));
    494   1.8    dyoung 		estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    495   1.8    dyoung 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    496   1.8    dyoung 		in6_alias(ifname, env, oenv, &ifr);
    497   1.1   thorpej 	}
    498   1.1   thorpej 	freeifaddrs(ifap);
    499   1.1   thorpej }
    500   1.1   thorpej 
    501   1.1   thorpej #define SIN6(x) ((struct sockaddr_in6 *) &(x))
    502   1.1   thorpej struct sockaddr_in6 *sin6tab[] = {
    503   1.1   thorpej     SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
    504   1.1   thorpej     SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
    505   1.1   thorpej 
    506   1.1   thorpej void
    507   1.8    dyoung in6_getaddr(const struct paddr_prefix *pfx, int which)
    508   1.1   thorpej {
    509   1.1   thorpej 	struct sockaddr_in6 *sin6 = sin6tab[which];
    510   1.8    dyoung 
    511   1.8    dyoung 	if (pfx->pfx_addr.sa_family != AF_INET6)
    512   1.8    dyoung 		errx(EXIT_FAILURE, "%s: address family mismatch", __func__);
    513   1.8    dyoung 
    514  1.11    dyoung 	if (which == ADDR && pfx->pfx_len >= 0)
    515   1.8    dyoung 		in6_getprefix(pfx->pfx_len, MASK);
    516   1.8    dyoung 
    517   1.8    dyoung 	memcpy(sin6, &pfx->pfx_addr, MIN(sizeof(*sin6), pfx->pfx_addr.sa_len));
    518   1.8    dyoung 
    519  1.14    dyoung 	in6_delscopeid(sin6);
    520   1.1   thorpej }
    521   1.1   thorpej 
    522   1.1   thorpej void
    523   1.8    dyoung in6_getprefix(int len, int which)
    524   1.1   thorpej {
    525   1.1   thorpej 	struct sockaddr_in6 *gpsin = sin6tab[which];
    526   1.1   thorpej 	u_char *cp;
    527   1.1   thorpej 
    528   1.8    dyoung 	if (len < 0 || len > 128)
    529   1.8    dyoung 		errx(EXIT_FAILURE, "%d: bad value", len);
    530   1.1   thorpej 	gpsin->sin6_len = sizeof(*gpsin);
    531   1.1   thorpej 	if (which != MASK)
    532   1.1   thorpej 		gpsin->sin6_family = AF_INET6;
    533   1.8    dyoung 	if (len == 0 || len == 128) {
    534   1.1   thorpej 		memset(&gpsin->sin6_addr, 0xff, sizeof(struct in6_addr));
    535   1.1   thorpej 		return;
    536   1.1   thorpej 	}
    537   1.1   thorpej 	memset((void *)&gpsin->sin6_addr, 0x00, sizeof(gpsin->sin6_addr));
    538   1.1   thorpej 	for (cp = (u_char *)&gpsin->sin6_addr; len > 7; len -= 8)
    539   1.1   thorpej 		*cp++ = 0xff;
    540   1.1   thorpej 	if (len)
    541   1.1   thorpej 		*cp = 0xff << (8 - len);
    542   1.1   thorpej }
    543  1.13    dyoung 
    544  1.13    dyoung static int
    545  1.14    dyoung in6_pre_aifaddr(prop_dictionary_t env, struct afparam *param)
    546  1.13    dyoung {
    547  1.14    dyoung 	struct in6_aliasreq *ifra = param->req.buf;
    548  1.13    dyoung 
    549  1.13    dyoung 	setia6eui64_impl(env, ifra);
    550  1.13    dyoung 	setia6vltime_impl(env, ifra);
    551  1.13    dyoung 	setia6pltime_impl(env, ifra);
    552  1.13    dyoung 	setia6flags_impl(env, ifra);
    553  1.14    dyoung 	in6_delscopeid(&ifra->ifra_addr);
    554  1.14    dyoung 	in6_delscopeid(&ifra->ifra_dstaddr);
    555  1.13    dyoung 
    556  1.13    dyoung 	return 0;
    557  1.13    dyoung }
    558  1.13    dyoung 
    559  1.13    dyoung void
    560  1.13    dyoung in6_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
    561  1.13    dyoung {
    562  1.14    dyoung 	struct in6_ifreq in6_ifr = {
    563  1.13    dyoung 		.ifr_addr = {
    564  1.13    dyoung 			.sin6_family = AF_INET6,
    565  1.13    dyoung 			.sin6_addr = {
    566  1.13    dyoung 				.s6_addr =
    567  1.13    dyoung 				    {0xff, 0xff, 0xff, 0xff,
    568  1.13    dyoung 				     0xff, 0xff, 0xff, 0xff}
    569  1.13    dyoung 			}
    570  1.13    dyoung 		}
    571  1.13    dyoung 	};
    572  1.13    dyoung 	static struct sockaddr_in6 in6_defmask = {
    573  1.13    dyoung 		.sin6_addr = {
    574  1.13    dyoung 			.s6_addr = {0xff, 0xff, 0xff, 0xff,
    575  1.13    dyoung 			            0xff, 0xff, 0xff, 0xff}
    576  1.13    dyoung 		}
    577  1.13    dyoung 	};
    578  1.13    dyoung 
    579  1.14    dyoung 	struct in6_aliasreq in6_ifra = {
    580  1.13    dyoung 		.ifra_prefixmask = {
    581  1.13    dyoung 			.sin6_addr = {
    582  1.13    dyoung 				.s6_addr =
    583  1.13    dyoung 				    {0xff, 0xff, 0xff, 0xff,
    584  1.13    dyoung 				     0xff, 0xff, 0xff, 0xff}}},
    585  1.13    dyoung 		.ifra_lifetime = {
    586  1.13    dyoung 			  .ia6t_pltime = ND6_INFINITE_LIFETIME
    587  1.13    dyoung 			, .ia6t_vltime = ND6_INFINITE_LIFETIME
    588  1.13    dyoung 		}
    589  1.13    dyoung 	};
    590  1.13    dyoung 	struct afparam in6param = {
    591  1.13    dyoung 		  .req = BUFPARAM(in6_ifra)
    592  1.13    dyoung 		, .dgreq = BUFPARAM(in6_ifr)
    593  1.13    dyoung 		, .name = {
    594  1.13    dyoung 			{.buf = in6_ifr.ifr_name,
    595  1.13    dyoung 			 .buflen = sizeof(in6_ifr.ifr_name)},
    596  1.13    dyoung 			{.buf = in6_ifra.ifra_name,
    597  1.13    dyoung 			 .buflen = sizeof(in6_ifra.ifra_name)}
    598  1.13    dyoung 		  }
    599  1.13    dyoung 		, .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
    600  1.13    dyoung 		, .addr = BUFPARAM(in6_ifra.ifra_addr)
    601  1.13    dyoung 		, .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
    602  1.13    dyoung 		, .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
    603  1.13    dyoung 		, .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
    604  1.13    dyoung 		, .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
    605  1.13    dyoung 		, .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
    606  1.13    dyoung 		, .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
    607  1.13    dyoung 		, .defmask = BUFPARAM(in6_defmask)
    608  1.13    dyoung 		, .pre_aifaddr = in6_pre_aifaddr
    609  1.13    dyoung 	};
    610  1.13    dyoung 	commit_address(env, oenv, &in6param);
    611  1.13    dyoung }
    612