Home | History | Annotate | Line # | Download | only in ifconfig
af_inet6.c revision 1.35
      1  1.35  riastrad /*	$NetBSD: af_inet6.c,v 1.35 2016/02/29 16:23:25 riastradh 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.35  riastrad __RCSID("$NetBSD: af_inet6.c,v 1.35 2016/02/29 16:23:25 riastradh 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.22    dyoung #include "extern.h"
     57   1.8    dyoung #include "parse.h"
     58   1.1   thorpej #include "extern.h"
     59  1.13    dyoung #include "af_inetany.h"
     60  1.27     pooka #include "prog_ops.h"
     61   1.1   thorpej 
     62  1.22    dyoung static void in6_constructor(void) __attribute__((constructor));
     63  1.22    dyoung static void in6_alias(const char *, prop_dictionary_t, prop_dictionary_t,
     64  1.22    dyoung     struct in6_ifreq *);
     65  1.22    dyoung static void in6_commit_address(prop_dictionary_t, prop_dictionary_t);
     66  1.22    dyoung 
     67  1.22    dyoung static int setia6eui64_impl(prop_dictionary_t, struct in6_aliasreq *);
     68  1.22    dyoung static int setia6flags_impl(prop_dictionary_t, struct in6_aliasreq *);
     69  1.22    dyoung static int setia6pltime_impl(prop_dictionary_t, struct in6_aliasreq *);
     70  1.22    dyoung static int setia6vltime_impl(prop_dictionary_t, struct in6_aliasreq *);
     71  1.22    dyoung 
     72  1.22    dyoung static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *);
     73  1.22    dyoung 
     74  1.22    dyoung static void in6_status(prop_dictionary_t, prop_dictionary_t, bool);
     75  1.34       roy static bool in6_addr_flags(struct ifaddrs *ifa, int);
     76  1.32       roy static bool in6_addr_tentative(struct ifaddrs *ifa);
     77  1.34       roy static bool in6_addr_tentative_or_detached(struct ifaddrs *ifa);
     78  1.22    dyoung 
     79  1.23    dyoung static struct usage_func usage;
     80  1.22    dyoung static cmdloop_branch_t branch[2];
     81  1.22    dyoung 
     82  1.12    dyoung static const struct kwinst ia6flagskw[] = {
     83  1.12    dyoung 	  IFKW("anycast",	IN6_IFF_ANYCAST)
     84  1.12    dyoung 	, IFKW("deprecated",	IN6_IFF_DEPRECATED)
     85  1.12    dyoung };
     86  1.12    dyoung 
     87  1.12    dyoung static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime,
     88  1.15    dyoung     "pltime", 0, NULL, "pltime", &command_root.pb_parser);
     89  1.12    dyoung 
     90  1.12    dyoung static struct pinteger parse_vltime = PINTEGER_INITIALIZER(&parse_vltime,
     91  1.15    dyoung     "vltime", 0, NULL, "vltime", &command_root.pb_parser);
     92  1.12    dyoung 
     93  1.12    dyoung static const struct kwinst inet6kw[] = {
     94  1.12    dyoung 	  {.k_word = "pltime", .k_nextparser = &parse_pltime.pi_parser}
     95  1.12    dyoung 	, {.k_word = "vltime", .k_nextparser = &parse_vltime.pi_parser}
     96  1.14    dyoung 	, {.k_word = "eui64", .k_key = "eui64", .k_type = KW_T_BOOL,
     97  1.15    dyoung 	   .k_bool = true, .k_nextparser = &command_root.pb_parser}
     98  1.12    dyoung };
     99  1.12    dyoung 
    100  1.15    dyoung struct pkw ia6flags = PKW_INITIALIZER(&ia6flags, "ia6flags", NULL,
    101  1.12    dyoung     "ia6flag", ia6flagskw, __arraycount(ia6flagskw), &command_root.pb_parser);
    102  1.12    dyoung struct pkw inet6 = PKW_INITIALIZER(&inet6, "IPv6 keywords", NULL,
    103  1.12    dyoung     NULL, inet6kw, __arraycount(inet6kw), NULL);
    104  1.12    dyoung 
    105  1.22    dyoung static struct afswtch in6af = {
    106  1.22    dyoung 	.af_name = "inet6", .af_af = AF_INET6, .af_status = in6_status,
    107  1.32       roy 	.af_addr_commit = in6_commit_address,
    108  1.34       roy 	.af_addr_tentative = in6_addr_tentative,
    109  1.34       roy 	.af_addr_tentative_or_detached = in6_addr_tentative_or_detached
    110  1.22    dyoung };
    111   1.8    dyoung 
    112   1.1   thorpej static int
    113   1.1   thorpej prefix(void *val, int size)
    114   1.1   thorpej {
    115   1.1   thorpej 	u_char *pname = (u_char *)val;
    116   1.1   thorpej 	int byte, bit, plen = 0;
    117   1.1   thorpej 
    118   1.1   thorpej 	for (byte = 0; byte < size; byte++, plen += 8)
    119   1.1   thorpej 		if (pname[byte] != 0xff)
    120   1.1   thorpej 			break;
    121   1.1   thorpej 	if (byte == size)
    122   1.1   thorpej 		return (plen);
    123   1.1   thorpej 	for (bit = 7; bit != 0; bit--, plen++)
    124   1.1   thorpej 		if (!(pname[byte] & (1 << bit)))
    125   1.1   thorpej 			break;
    126   1.1   thorpej 	for (; bit != 0; bit--)
    127   1.1   thorpej 		if (pname[byte] & (1 << bit))
    128   1.1   thorpej 			return(0);
    129   1.1   thorpej 	byte++;
    130   1.1   thorpej 	for (; byte < size; byte++)
    131   1.1   thorpej 		if (pname[byte])
    132   1.1   thorpej 			return(0);
    133   1.1   thorpej 	return (plen);
    134   1.1   thorpej }
    135   1.1   thorpej 
    136   1.8    dyoung int
    137   1.8    dyoung setia6flags_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    138   1.1   thorpej {
    139  1.10    dyoung 	int64_t ia6flag;
    140   1.1   thorpej 
    141  1.10    dyoung 	if (!prop_dictionary_get_int64(env, "ia6flag", &ia6flag)) {
    142   1.8    dyoung 		errno = ENOENT;
    143   1.8    dyoung 		return -1;
    144   1.8    dyoung 	}
    145   1.8    dyoung 
    146   1.8    dyoung 	if (ia6flag < 0) {
    147   1.8    dyoung 		ia6flag = -ia6flag;
    148   1.8    dyoung 		ifra->ifra_flags &= ~ia6flag;
    149   1.1   thorpej 	} else
    150   1.8    dyoung 		ifra->ifra_flags |= ia6flag;
    151   1.8    dyoung 	return 0;
    152   1.8    dyoung }
    153   1.8    dyoung 
    154   1.8    dyoung int
    155   1.8    dyoung setia6pltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    156   1.1   thorpej {
    157  1.10    dyoung 	int64_t pltime;
    158   1.8    dyoung 
    159  1.10    dyoung 	if (!prop_dictionary_get_int64(env, "pltime", &pltime)) {
    160   1.8    dyoung 		errno = ENOENT;
    161   1.8    dyoung 		return -1;
    162   1.8    dyoung 	}
    163   1.1   thorpej 
    164  1.10    dyoung 	return setia6lifetime(env, pltime,
    165   1.8    dyoung 	    &ifra->ifra_lifetime.ia6t_preferred,
    166   1.8    dyoung 	    &ifra->ifra_lifetime.ia6t_pltime);
    167   1.8    dyoung }
    168   1.8    dyoung 
    169   1.8    dyoung int
    170   1.8    dyoung setia6vltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    171   1.1   thorpej {
    172  1.10    dyoung 	int64_t vltime;
    173   1.8    dyoung 
    174  1.10    dyoung 	if (!prop_dictionary_get_int64(env, "vltime", &vltime)) {
    175   1.8    dyoung 		errno = ENOENT;
    176   1.8    dyoung 		return -1;
    177   1.8    dyoung 	}
    178   1.1   thorpej 
    179  1.10    dyoung 	return setia6lifetime(env, vltime,
    180   1.8    dyoung 		&ifra->ifra_lifetime.ia6t_expire,
    181   1.8    dyoung 		&ifra->ifra_lifetime.ia6t_vltime);
    182   1.8    dyoung }
    183   1.8    dyoung 
    184   1.8    dyoung static int
    185   1.8    dyoung setia6lifetime(prop_dictionary_t env, int64_t val, time_t *timep,
    186   1.8    dyoung     uint32_t *ivalp)
    187   1.1   thorpej {
    188   1.8    dyoung 	time_t t;
    189   1.8    dyoung 	int af;
    190   1.8    dyoung 
    191   1.8    dyoung 	if ((af = getaf(env)) == -1 || af != AF_INET6) {
    192   1.8    dyoung 		errx(EXIT_FAILURE,
    193   1.8    dyoung 		    "inet6 address lifetime not allowed for the AF");
    194   1.8    dyoung 	}
    195   1.1   thorpej 
    196   1.1   thorpej 	t = time(NULL);
    197   1.8    dyoung 	*timep = t + val;
    198   1.8    dyoung 	*ivalp = val;
    199   1.8    dyoung 	return 0;
    200   1.1   thorpej }
    201   1.1   thorpej 
    202   1.8    dyoung int
    203   1.8    dyoung setia6eui64_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    204   1.1   thorpej {
    205  1.14    dyoung 	char buf[2][80];
    206   1.1   thorpej 	struct ifaddrs *ifap, *ifa;
    207   1.1   thorpej 	const struct sockaddr_in6 *sin6 = NULL;
    208   1.1   thorpej 	const struct in6_addr *lladdr = NULL;
    209   1.1   thorpej 	struct in6_addr *in6;
    210   1.8    dyoung 	const char *ifname;
    211  1.14    dyoung 	bool doit = false;
    212   1.8    dyoung 	int af;
    213   1.1   thorpej 
    214  1.14    dyoung 	if (!prop_dictionary_get_bool(env, "eui64", &doit) || !doit) {
    215  1.14    dyoung 		errno = ENOENT;
    216  1.14    dyoung 		return -1;
    217  1.14    dyoung 	}
    218  1.14    dyoung 
    219   1.8    dyoung 	if ((ifname = getifname(env)) == NULL)
    220   1.8    dyoung 		return -1;
    221   1.8    dyoung 
    222   1.8    dyoung 	af = getaf(env);
    223   1.8    dyoung 	if (af != AF_INET6) {
    224   1.8    dyoung 		errx(EXIT_FAILURE,
    225   1.8    dyoung 		    "eui64 address modifier not allowed for the AF");
    226   1.8    dyoung 	}
    227   1.8    dyoung  	in6 = &ifra->ifra_addr.sin6_addr;
    228  1.14    dyoung 	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) {
    229  1.14    dyoung 		union {
    230  1.14    dyoung 			struct sockaddr_in6 sin6;
    231  1.14    dyoung 			struct sockaddr sa;
    232  1.14    dyoung 		} any = {.sin6 = {.sin6_family = AF_INET6}};
    233  1.14    dyoung 		memcpy(&any.sin6.sin6_addr, &in6addr_any,
    234  1.14    dyoung 		    sizeof(any.sin6.sin6_addr));
    235  1.14    dyoung 		(void)sockaddr_snprintf(buf[0], sizeof(buf[0]), "%a%%S",
    236  1.14    dyoung 		    &any.sa);
    237  1.14    dyoung 		(void)sockaddr_snprintf(buf[1], sizeof(buf[1]), "%a%%S",
    238  1.14    dyoung 		    (const struct sockaddr *)&ifra->ifra_addr);
    239  1.14    dyoung 		errx(EXIT_FAILURE, "interface index is already filled, %s | %s",
    240  1.14    dyoung 		    buf[0], buf[1]);
    241  1.14    dyoung 	}
    242   1.1   thorpej 	if (getifaddrs(&ifap) != 0)
    243   1.1   thorpej 		err(EXIT_FAILURE, "getifaddrs");
    244   1.1   thorpej 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    245   1.1   thorpej 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
    246   1.8    dyoung 		    strcmp(ifa->ifa_name, ifname) == 0) {
    247   1.1   thorpej 			sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
    248   1.1   thorpej 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    249   1.1   thorpej 				lladdr = &sin6->sin6_addr;
    250   1.1   thorpej 				break;
    251   1.1   thorpej 			}
    252   1.1   thorpej 		}
    253   1.1   thorpej 	}
    254  1.26    dyoung 	if (lladdr == NULL)
    255   1.1   thorpej 		errx(EXIT_FAILURE, "could not determine link local address");
    256   1.1   thorpej 
    257   1.1   thorpej  	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
    258   1.1   thorpej 
    259   1.1   thorpej 	freeifaddrs(ifap);
    260   1.8    dyoung 	return 0;
    261   1.8    dyoung }
    262   1.8    dyoung 
    263   1.1   thorpej /* XXX not really an alias */
    264   1.1   thorpej void
    265   1.8    dyoung in6_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
    266   1.8    dyoung     struct in6_ifreq *creq)
    267   1.1   thorpej {
    268   1.8    dyoung 	struct in6_ifreq ifr6;
    269   1.1   thorpej 	struct sockaddr_in6 *sin6;
    270   1.1   thorpej 	char hbuf[NI_MAXHOST];
    271   1.1   thorpej 	u_int32_t scopeid;
    272   1.8    dyoung 	int s;
    273  1.24    dyoung 	const int niflag = Nflag ? 0 : NI_NUMERICHOST;
    274   1.8    dyoung 	unsigned short flags;
    275   1.1   thorpej 
    276   1.1   thorpej 	/* Get the non-alias address for this interface. */
    277   1.8    dyoung 	if ((s = getsock(AF_INET6)) == -1) {
    278   1.3      tron 		if (errno == EAFNOSUPPORT)
    279   1.1   thorpej 			return;
    280   1.1   thorpej 		err(EXIT_FAILURE, "socket");
    281   1.1   thorpej 	}
    282   1.1   thorpej 
    283  1.17    dyoung 	sin6 = &creq->ifr_addr;
    284   1.1   thorpej 
    285  1.29  christos 	inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
    286   1.1   thorpej 	scopeid = sin6->sin6_scope_id;
    287  1.18    dyoung 	if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len,
    288   1.1   thorpej 			hbuf, sizeof(hbuf), NULL, 0, niflag))
    289   1.1   thorpej 		strlcpy(hbuf, "", sizeof(hbuf));	/* some message? */
    290   1.1   thorpej 	printf("\tinet6 %s", hbuf);
    291   1.1   thorpej 
    292   1.8    dyoung 	if (getifflags(env, oenv, &flags) == -1)
    293   1.8    dyoung 		err(EXIT_FAILURE, "%s: getifflags", __func__);
    294   1.8    dyoung 
    295   1.1   thorpej 	if (flags & IFF_POINTOPOINT) {
    296  1.18    dyoung 		ifr6 = *creq;
    297  1.27     pooka 		if (prog_ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
    298   1.1   thorpej 			if (errno != EADDRNOTAVAIL)
    299   1.1   thorpej 				warn("SIOCGIFDSTADDR_IN6");
    300   1.7    dyoung 			memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
    301   1.1   thorpej 			ifr6.ifr_addr.sin6_family = AF_INET6;
    302   1.1   thorpej 			ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
    303   1.1   thorpej 		}
    304  1.17    dyoung 		sin6 = &ifr6.ifr_addr;
    305  1.29  christos 		inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
    306   1.1   thorpej 		hbuf[0] = '\0';
    307   1.1   thorpej 		if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
    308   1.1   thorpej 				hbuf, sizeof(hbuf), NULL, 0, niflag))
    309   1.1   thorpej 			strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
    310   1.1   thorpej 		printf(" -> %s", hbuf);
    311   1.1   thorpej 	}
    312   1.1   thorpej 
    313  1.18    dyoung 	ifr6 = *creq;
    314  1.27     pooka 	if (prog_ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
    315   1.1   thorpej 		if (errno != EADDRNOTAVAIL)
    316   1.1   thorpej 			warn("SIOCGIFNETMASK_IN6");
    317   1.1   thorpej 	} else {
    318  1.17    dyoung 		sin6 = &ifr6.ifr_addr;
    319   1.1   thorpej 		printf(" prefixlen %d", prefix(&sin6->sin6_addr,
    320   1.1   thorpej 					       sizeof(struct in6_addr)));
    321   1.1   thorpej 	}
    322   1.1   thorpej 
    323  1.18    dyoung 	ifr6 = *creq;
    324  1.27     pooka 	if (prog_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
    325   1.1   thorpej 		if (errno != EADDRNOTAVAIL)
    326   1.1   thorpej 			warn("SIOCGIFAFLAG_IN6");
    327   1.1   thorpej 	} else {
    328   1.1   thorpej 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
    329   1.1   thorpej 			printf(" anycast");
    330   1.1   thorpej 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
    331   1.1   thorpej 			printf(" tentative");
    332   1.1   thorpej 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
    333   1.1   thorpej 			printf(" duplicated");
    334   1.1   thorpej 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
    335   1.1   thorpej 			printf(" detached");
    336   1.1   thorpej 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
    337   1.1   thorpej 			printf(" deprecated");
    338  1.31       roy 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_AUTOCONF)
    339  1.31       roy 			printf(" autoconf");
    340  1.31       roy 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TEMPORARY)
    341  1.31       roy 			printf(" temporary");
    342   1.1   thorpej 	}
    343   1.1   thorpej 
    344   1.1   thorpej 	if (scopeid)
    345   1.1   thorpej 		printf(" scopeid 0x%x", scopeid);
    346   1.1   thorpej 
    347  1.22    dyoung 	if (get_flag('L')) {
    348   1.1   thorpej 		struct in6_addrlifetime *lifetime;
    349  1.18    dyoung 		ifr6 = *creq;
    350   1.1   thorpej 		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
    351  1.27     pooka 		if (prog_ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
    352   1.1   thorpej 			if (errno != EADDRNOTAVAIL)
    353   1.1   thorpej 				warn("SIOCGIFALIFETIME_IN6");
    354   1.1   thorpej 		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
    355   1.1   thorpej 			time_t t = time(NULL);
    356   1.1   thorpej 			printf(" pltime ");
    357   1.1   thorpej 			if (lifetime->ia6t_preferred) {
    358  1.19    dyoung 				printf("%lu",
    359  1.19    dyoung 				    (unsigned long)(lifetime->ia6t_preferred -
    360  1.19    dyoung 				        MIN(t, lifetime->ia6t_preferred)));
    361   1.1   thorpej 			} else
    362   1.1   thorpej 				printf("infty");
    363   1.1   thorpej 
    364   1.1   thorpej 			printf(" vltime ");
    365   1.1   thorpej 			if (lifetime->ia6t_expire) {
    366  1.19    dyoung 				printf("%lu",
    367  1.19    dyoung 				    (unsigned long)(lifetime->ia6t_expire -
    368  1.19    dyoung 				        MIN(t, lifetime->ia6t_expire)));
    369   1.1   thorpej 			} else
    370   1.1   thorpej 				printf("infty");
    371   1.1   thorpej 		}
    372   1.1   thorpej 	}
    373   1.1   thorpej }
    374   1.1   thorpej 
    375  1.22    dyoung static void
    376   1.9    dyoung in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
    377   1.1   thorpej {
    378   1.1   thorpej 	struct ifaddrs *ifap, *ifa;
    379   1.8    dyoung 	struct in6_ifreq ifr;
    380   1.8    dyoung 	const char *ifname;
    381  1.25    dyoung 	bool printprefs = false;
    382   1.8    dyoung 
    383   1.8    dyoung 	if ((ifname = getifname(env)) == NULL)
    384   1.8    dyoung 		err(EXIT_FAILURE, "%s: getifname", __func__);
    385   1.1   thorpej 
    386   1.1   thorpej 	if (getifaddrs(&ifap) != 0)
    387   1.1   thorpej 		err(EXIT_FAILURE, "getifaddrs");
    388  1.25    dyoung 	printprefs = ifa_any_preferences(ifname, ifap, AF_INET6);
    389   1.1   thorpej 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    390   1.8    dyoung 		if (strcmp(ifname, ifa->ifa_name) != 0)
    391   1.1   thorpej 			continue;
    392   1.1   thorpej 		if (ifa->ifa_addr->sa_family != AF_INET6)
    393   1.1   thorpej 			continue;
    394   1.8    dyoung 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
    395   1.1   thorpej 			continue;
    396   1.1   thorpej 
    397   1.8    dyoung 		memset(&ifr, 0, sizeof(ifr));
    398   1.8    dyoung 		estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    399   1.8    dyoung 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    400   1.8    dyoung 		in6_alias(ifname, env, oenv, &ifr);
    401  1.25    dyoung 		if (printprefs)
    402  1.25    dyoung 			ifa_print_preference(ifa->ifa_name, ifa->ifa_addr);
    403  1.25    dyoung 		printf("\n");
    404   1.1   thorpej 	}
    405   1.1   thorpej 	freeifaddrs(ifap);
    406   1.1   thorpej }
    407   1.1   thorpej 
    408  1.13    dyoung static int
    409  1.22    dyoung in6_pre_aifaddr(prop_dictionary_t env, const struct afparam *param)
    410  1.13    dyoung {
    411  1.14    dyoung 	struct in6_aliasreq *ifra = param->req.buf;
    412  1.13    dyoung 
    413  1.13    dyoung 	setia6eui64_impl(env, ifra);
    414  1.13    dyoung 	setia6vltime_impl(env, ifra);
    415  1.13    dyoung 	setia6pltime_impl(env, ifra);
    416  1.13    dyoung 	setia6flags_impl(env, ifra);
    417  1.29  christos 	inet6_putscopeid(&ifra->ifra_addr, INET6_IS_ADDR_LINKLOCAL);
    418  1.29  christos 	inet6_putscopeid(&ifra->ifra_dstaddr, INET6_IS_ADDR_LINKLOCAL);
    419  1.13    dyoung 
    420  1.13    dyoung 	return 0;
    421  1.13    dyoung }
    422  1.13    dyoung 
    423  1.22    dyoung static void
    424  1.13    dyoung in6_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
    425  1.13    dyoung {
    426  1.14    dyoung 	struct in6_ifreq in6_ifr = {
    427  1.13    dyoung 		.ifr_addr = {
    428  1.13    dyoung 			.sin6_family = AF_INET6,
    429  1.20    dyoung 			.sin6_len = sizeof(in6_ifr.ifr_addr),
    430  1.13    dyoung 			.sin6_addr = {
    431  1.13    dyoung 				.s6_addr =
    432  1.13    dyoung 				    {0xff, 0xff, 0xff, 0xff,
    433  1.13    dyoung 				     0xff, 0xff, 0xff, 0xff}
    434  1.13    dyoung 			}
    435  1.13    dyoung 		}
    436  1.13    dyoung 	};
    437  1.13    dyoung 	static struct sockaddr_in6 in6_defmask = {
    438  1.20    dyoung 		.sin6_family = AF_INET6,
    439  1.20    dyoung 		.sin6_len = sizeof(in6_defmask),
    440  1.13    dyoung 		.sin6_addr = {
    441  1.13    dyoung 			.s6_addr = {0xff, 0xff, 0xff, 0xff,
    442  1.13    dyoung 			            0xff, 0xff, 0xff, 0xff}
    443  1.13    dyoung 		}
    444  1.13    dyoung 	};
    445  1.13    dyoung 
    446  1.14    dyoung 	struct in6_aliasreq in6_ifra = {
    447  1.13    dyoung 		.ifra_prefixmask = {
    448  1.20    dyoung 			.sin6_family = AF_INET6,
    449  1.20    dyoung 			.sin6_len = sizeof(in6_ifra.ifra_prefixmask),
    450  1.13    dyoung 			.sin6_addr = {
    451  1.13    dyoung 				.s6_addr =
    452  1.13    dyoung 				    {0xff, 0xff, 0xff, 0xff,
    453  1.13    dyoung 				     0xff, 0xff, 0xff, 0xff}}},
    454  1.13    dyoung 		.ifra_lifetime = {
    455  1.13    dyoung 			  .ia6t_pltime = ND6_INFINITE_LIFETIME
    456  1.13    dyoung 			, .ia6t_vltime = ND6_INFINITE_LIFETIME
    457  1.13    dyoung 		}
    458  1.13    dyoung 	};
    459  1.13    dyoung 	struct afparam in6param = {
    460  1.13    dyoung 		  .req = BUFPARAM(in6_ifra)
    461  1.13    dyoung 		, .dgreq = BUFPARAM(in6_ifr)
    462  1.13    dyoung 		, .name = {
    463  1.13    dyoung 			{.buf = in6_ifr.ifr_name,
    464  1.13    dyoung 			 .buflen = sizeof(in6_ifr.ifr_name)},
    465  1.13    dyoung 			{.buf = in6_ifra.ifra_name,
    466  1.13    dyoung 			 .buflen = sizeof(in6_ifra.ifra_name)}
    467  1.13    dyoung 		  }
    468  1.13    dyoung 		, .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
    469  1.13    dyoung 		, .addr = BUFPARAM(in6_ifra.ifra_addr)
    470  1.13    dyoung 		, .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
    471  1.13    dyoung 		, .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
    472  1.13    dyoung 		, .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
    473  1.13    dyoung 		, .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
    474  1.13    dyoung 		, .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
    475  1.13    dyoung 		, .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
    476  1.13    dyoung 		, .defmask = BUFPARAM(in6_defmask)
    477  1.13    dyoung 		, .pre_aifaddr = in6_pre_aifaddr
    478  1.13    dyoung 	};
    479  1.13    dyoung 	commit_address(env, oenv, &in6param);
    480  1.13    dyoung }
    481  1.22    dyoung 
    482  1.32       roy static bool
    483  1.34       roy in6_addr_flags(struct ifaddrs *ifa, int flags)
    484  1.32       roy {
    485  1.32       roy 	int s;
    486  1.32       roy 	struct in6_ifreq ifr;
    487  1.32       roy 
    488  1.32       roy 	if ((s = getsock(AF_INET6)) == -1)
    489  1.32       roy 		err(EXIT_FAILURE, "%s: getsock", __func__);
    490  1.32       roy 	memset(&ifr, 0, sizeof(ifr));
    491  1.35  riastrad 	estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    492  1.32       roy 	ifr.ifr_addr = *(struct sockaddr_in6 *)ifa->ifa_addr;
    493  1.33       roy 	if (prog_ioctl(s, SIOCGIFAFLAG_IN6, &ifr) == -1)
    494  1.32       roy 		err(EXIT_FAILURE, "SIOCGIFAFLAG_IN6");
    495  1.34       roy 	return ifr.ifr_ifru.ifru_flags6 & flags ? true : false;
    496  1.34       roy }
    497  1.34       roy 
    498  1.34       roy static bool
    499  1.34       roy in6_addr_tentative(struct ifaddrs *ifa)
    500  1.34       roy {
    501  1.34       roy 
    502  1.34       roy 	return in6_addr_flags(ifa, IN6_IFF_TENTATIVE);
    503  1.34       roy }
    504  1.34       roy 
    505  1.34       roy static bool
    506  1.34       roy in6_addr_tentative_or_detached(struct ifaddrs *ifa)
    507  1.34       roy {
    508  1.34       roy 
    509  1.34       roy 	return in6_addr_flags(ifa, IN6_IFF_TENTATIVE | IN6_IFF_DETACHED);
    510  1.32       roy }
    511  1.32       roy 
    512  1.22    dyoung static void
    513  1.23    dyoung in6_usage(prop_dictionary_t env)
    514  1.23    dyoung {
    515  1.23    dyoung 	fprintf(stderr,
    516  1.23    dyoung 	    "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
    517  1.30       roy 	    "\t[ pltime n ] [ vltime n ] "
    518  1.23    dyoung 	    "[ eui64 ]\n");
    519  1.23    dyoung }
    520  1.23    dyoung 
    521  1.23    dyoung static void
    522  1.22    dyoung in6_constructor(void)
    523  1.22    dyoung {
    524  1.22    dyoung 	if (register_flag('L') != 0)
    525  1.22    dyoung 		err(EXIT_FAILURE, __func__);
    526  1.22    dyoung 	register_family(&in6af);
    527  1.23    dyoung 	usage_func_init(&usage, in6_usage);
    528  1.23    dyoung 	register_usage(&usage);
    529  1.22    dyoung 	cmdloop_branch_init(&branch[0], &ia6flags.pk_parser);
    530  1.22    dyoung 	cmdloop_branch_init(&branch[1], &inet6.pk_parser);
    531  1.22    dyoung 	register_cmdloop_branch(&branch[0]);
    532  1.22    dyoung 	register_cmdloop_branch(&branch[1]);
    533  1.22    dyoung }
    534