xref: /freebsd/sbin/ifconfig/af_inet6.c (revision 7fa282e616c303261904cb6158959d5a784dc3ee)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
45faf8dcbSSam Leffler  * Copyright (c) 1983, 1993
55faf8dcbSSam Leffler  *	The Regents of the University of California.  All rights reserved.
65faf8dcbSSam Leffler  *
75faf8dcbSSam Leffler  * Redistribution and use in source and binary forms, with or without
85faf8dcbSSam Leffler  * modification, are permitted provided that the following conditions
95faf8dcbSSam Leffler  * are met:
105faf8dcbSSam Leffler  * 1. Redistributions of source code must retain the above copyright
115faf8dcbSSam Leffler  *    notice, this list of conditions and the following disclaimer.
125faf8dcbSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
135faf8dcbSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
145faf8dcbSSam Leffler  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
165faf8dcbSSam Leffler  *    may be used to endorse or promote products derived from this software
175faf8dcbSSam Leffler  *    without specific prior written permission.
185faf8dcbSSam Leffler  *
195faf8dcbSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205faf8dcbSSam Leffler  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215faf8dcbSSam Leffler  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225faf8dcbSSam Leffler  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235faf8dcbSSam Leffler  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245faf8dcbSSam Leffler  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255faf8dcbSSam Leffler  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265faf8dcbSSam Leffler  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275faf8dcbSSam Leffler  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285faf8dcbSSam Leffler  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295faf8dcbSSam Leffler  * SUCH DAMAGE.
305faf8dcbSSam Leffler  */
315faf8dcbSSam Leffler 
325faf8dcbSSam Leffler #ifndef lint
335faf8dcbSSam Leffler static const char rcsid[] =
345faf8dcbSSam Leffler   "$FreeBSD$";
355faf8dcbSSam Leffler #endif /* not lint */
365faf8dcbSSam Leffler 
375faf8dcbSSam Leffler #include <sys/param.h>
385faf8dcbSSam Leffler #include <sys/ioctl.h>
395faf8dcbSSam Leffler #include <sys/socket.h>
405faf8dcbSSam Leffler #include <net/if.h>
415faf8dcbSSam Leffler 
425faf8dcbSSam Leffler #include <err.h>
435faf8dcbSSam Leffler #include <stdio.h>
445faf8dcbSSam Leffler #include <stdlib.h>
455faf8dcbSSam Leffler #include <string.h>
465faf8dcbSSam Leffler #include <unistd.h>
47965ef03cSHajimu UMEMOTO #include <time.h>
485faf8dcbSSam Leffler #include <ifaddrs.h>
495faf8dcbSSam Leffler 
505faf8dcbSSam Leffler #include <arpa/inet.h>
515faf8dcbSSam Leffler 
525faf8dcbSSam Leffler #include <netinet/in.h>
535faf8dcbSSam Leffler #include <netinet/in_var.h>
545faf8dcbSSam Leffler #include <arpa/inet.h>
555faf8dcbSSam Leffler #include <netdb.h>
565faf8dcbSSam Leffler 
575faf8dcbSSam Leffler #include <netinet6/nd6.h>	/* Define ND6_INFINITE_LIFETIME */
585faf8dcbSSam Leffler 
595faf8dcbSSam Leffler #include "ifconfig.h"
604c91a5dfSAlexander V. Chernikov #include "ifconfig_netlink.h"
615faf8dcbSSam Leffler 
624bf44dd7SAlexander V. Chernikov #ifndef WITHOUT_NETLINK
634bf44dd7SAlexander V. Chernikov struct in6_px {
644bf44dd7SAlexander V. Chernikov 	struct in6_addr		addr;
654bf44dd7SAlexander V. Chernikov 	int			plen;
664bf44dd7SAlexander V. Chernikov 	bool			set;
674bf44dd7SAlexander V. Chernikov };
684bf44dd7SAlexander V. Chernikov struct in6_pdata {
694bf44dd7SAlexander V. Chernikov 	struct in6_px		addr;
704bf44dd7SAlexander V. Chernikov 	struct in6_px		dst_addr;
714bf44dd7SAlexander V. Chernikov 	struct in6_addrlifetime	lifetime;
724bf44dd7SAlexander V. Chernikov 	uint32_t		flags;
734bf44dd7SAlexander V. Chernikov 	uint32_t		vhid;
744bf44dd7SAlexander V. Chernikov };
754bf44dd7SAlexander V. Chernikov 
764bf44dd7SAlexander V. Chernikov static struct in6_pdata in6_del;
774bf44dd7SAlexander V. Chernikov static struct in6_pdata in6_add = {
784bf44dd7SAlexander V. Chernikov 	.lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME },
794bf44dd7SAlexander V. Chernikov };
804bf44dd7SAlexander V. Chernikov #else
815faf8dcbSSam Leffler static	struct in6_ifreq in6_ridreq;
825faf8dcbSSam Leffler static	struct in6_aliasreq in6_addreq =
83b59dcaeeSXin LI   { .ifra_flags = 0,
84b59dcaeeSXin LI     .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
854bf44dd7SAlexander V. Chernikov #endif
865faf8dcbSSam Leffler static	int ip6lifetime;
875faf8dcbSSam Leffler 
8844cd85d4SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
895faf8dcbSSam Leffler static	int prefix(void *, int);
9044cd85d4SAlexander V. Chernikov #endif
915faf8dcbSSam Leffler static	char *sec2str(time_t);
925faf8dcbSSam Leffler static	int explicit_prefix = 0;
93776db16aSAllan Jude extern	char *f_inet6, *f_addr;
945faf8dcbSSam Leffler 
956e3a9d7fSAlexander V. Chernikov extern void setnd6flags(if_ctx *, const char *, int);
966e3a9d7fSAlexander V. Chernikov extern void setnd6defif(if_ctx *,const char *, int);
976e3a9d7fSAlexander V. Chernikov extern void nd6_status(if_ctx *);
98a283298cSHiroki Sato 
99776db16aSAllan Jude static	char addr_buf[NI_MAXHOST];	/*for getnameinfo()*/
1005faf8dcbSSam Leffler 
1015faf8dcbSSam Leffler static void
1020c2beef7SAlexander V. Chernikov setifprefixlen(if_ctx *ctx __netlink_unused, const char *addr, int dummy __unused)
1035faf8dcbSSam Leffler {
1044bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
1056e3a9d7fSAlexander V. Chernikov 	const struct afswtch *afp = ctx->afp;
1066e3a9d7fSAlexander V. Chernikov 
1075faf8dcbSSam Leffler         if (afp->af_getprefix != NULL)
1085faf8dcbSSam Leffler                 afp->af_getprefix(addr, MASK);
1094bf44dd7SAlexander V. Chernikov #else
1104bf44dd7SAlexander V. Chernikov 	int plen = strtol(addr, NULL, 10);
1114bf44dd7SAlexander V. Chernikov 
1124bf44dd7SAlexander V. Chernikov 	if ((plen < 0) || (plen > 128))
1134bf44dd7SAlexander V. Chernikov 		errx(1, "%s: bad value", addr);
1144bf44dd7SAlexander V. Chernikov 	in6_add.addr.plen = plen;
1154bf44dd7SAlexander V. Chernikov #endif
1165faf8dcbSSam Leffler 	explicit_prefix = 1;
1175faf8dcbSSam Leffler }
1185faf8dcbSSam Leffler 
1195faf8dcbSSam Leffler static void
1206e3a9d7fSAlexander V. Chernikov setip6flags(if_ctx *ctx, const char *dummyaddr __unused, int flag)
1215faf8dcbSSam Leffler {
1226e3a9d7fSAlexander V. Chernikov 	const struct afswtch *afp = ctx->afp;
1236e3a9d7fSAlexander V. Chernikov 
1245faf8dcbSSam Leffler 	if (afp->af_af != AF_INET6)
1255faf8dcbSSam Leffler 		err(1, "address flags can be set only for inet6 addresses");
1265faf8dcbSSam Leffler 
1274bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
1285faf8dcbSSam Leffler 	if (flag < 0)
1295faf8dcbSSam Leffler 		in6_addreq.ifra_flags &= ~(-flag);
1305faf8dcbSSam Leffler 	else
1315faf8dcbSSam Leffler 		in6_addreq.ifra_flags |= flag;
1324bf44dd7SAlexander V. Chernikov #else
1334bf44dd7SAlexander V. Chernikov 	if (flag < 0)
1344bf44dd7SAlexander V. Chernikov 		in6_add.flags &= ~(-flag);
1354bf44dd7SAlexander V. Chernikov 	else
1364bf44dd7SAlexander V. Chernikov 		in6_add.flags |= flag;
1374bf44dd7SAlexander V. Chernikov #endif
1385faf8dcbSSam Leffler }
1395faf8dcbSSam Leffler 
1405faf8dcbSSam Leffler static void
1416e3a9d7fSAlexander V. Chernikov setip6lifetime(if_ctx *ctx, const char *cmd, const char *val)
1425faf8dcbSSam Leffler {
1436e3a9d7fSAlexander V. Chernikov 	const struct afswtch *afp = ctx->afp;
144965ef03cSHajimu UMEMOTO 	struct timespec now;
145965ef03cSHajimu UMEMOTO 	time_t newval;
1465faf8dcbSSam Leffler 	char *ep;
1474bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
1484bf44dd7SAlexander V. Chernikov 	struct in6_addrlifetime *lifetime = &in6_addreq.ifra_lifetime;
1494bf44dd7SAlexander V. Chernikov #else
1504bf44dd7SAlexander V. Chernikov 	struct in6_addrlifetime *lifetime = &in6_add.lifetime;
1514bf44dd7SAlexander V. Chernikov #endif
1525faf8dcbSSam Leffler 
153965ef03cSHajimu UMEMOTO 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1545faf8dcbSSam Leffler 	newval = (time_t)strtoul(val, &ep, 0);
1555faf8dcbSSam Leffler 	if (val == ep)
1565faf8dcbSSam Leffler 		errx(1, "invalid %s", cmd);
1575faf8dcbSSam Leffler 	if (afp->af_af != AF_INET6)
1585faf8dcbSSam Leffler 		errx(1, "%s not allowed for the AF", cmd);
1595faf8dcbSSam Leffler 	if (strcmp(cmd, "vltime") == 0) {
1604bf44dd7SAlexander V. Chernikov 		lifetime->ia6t_expire = now.tv_sec + newval;
1614bf44dd7SAlexander V. Chernikov 		lifetime->ia6t_vltime = newval;
1625faf8dcbSSam Leffler 	} else if (strcmp(cmd, "pltime") == 0) {
1634bf44dd7SAlexander V. Chernikov 		lifetime->ia6t_preferred = now.tv_sec + newval;
1644bf44dd7SAlexander V. Chernikov 		lifetime->ia6t_pltime = newval;
1655faf8dcbSSam Leffler 	}
1665faf8dcbSSam Leffler }
1675faf8dcbSSam Leffler 
1685faf8dcbSSam Leffler static void
1696e3a9d7fSAlexander V. Chernikov setip6pltime(if_ctx *ctx, const char *seconds, int dummy __unused)
1705faf8dcbSSam Leffler {
1716e3a9d7fSAlexander V. Chernikov 	setip6lifetime(ctx, "pltime", seconds);
1725faf8dcbSSam Leffler }
1735faf8dcbSSam Leffler 
1745faf8dcbSSam Leffler static void
1756e3a9d7fSAlexander V. Chernikov setip6vltime(if_ctx *ctx, const char *seconds, int dummy __unused)
1765faf8dcbSSam Leffler {
1776e3a9d7fSAlexander V. Chernikov 	setip6lifetime(ctx, "vltime", seconds);
1785faf8dcbSSam Leffler }
1795faf8dcbSSam Leffler 
1805faf8dcbSSam Leffler static void
1816e3a9d7fSAlexander V. Chernikov setip6eui64(if_ctx *ctx, const char *cmd, int dummy __unused)
1825faf8dcbSSam Leffler {
1836e3a9d7fSAlexander V. Chernikov 	const struct afswtch *afp = ctx->afp;
1845faf8dcbSSam Leffler 	struct ifaddrs *ifap, *ifa;
1855faf8dcbSSam Leffler 	const struct sockaddr_in6 *sin6 = NULL;
1865faf8dcbSSam Leffler 	const struct in6_addr *lladdr = NULL;
1875faf8dcbSSam Leffler 	struct in6_addr *in6;
1885faf8dcbSSam Leffler 
1895faf8dcbSSam Leffler 	if (afp->af_af != AF_INET6)
1905faf8dcbSSam Leffler 		errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
1914bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
1925faf8dcbSSam Leffler  	in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
1934bf44dd7SAlexander V. Chernikov #else
1944bf44dd7SAlexander V. Chernikov 	in6 = &in6_add.addr.addr;
1954bf44dd7SAlexander V. Chernikov #endif
1965faf8dcbSSam Leffler 	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
1975faf8dcbSSam Leffler 		errx(EXIT_FAILURE, "interface index is already filled");
1985faf8dcbSSam Leffler 	if (getifaddrs(&ifap) != 0)
1995faf8dcbSSam Leffler 		err(EXIT_FAILURE, "getifaddrs");
2005faf8dcbSSam Leffler 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
2015faf8dcbSSam Leffler 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
20285e0016aSAlexander V. Chernikov 		    strcmp(ifa->ifa_name, ctx->ifname) == 0) {
20344cd85d4SAlexander V. Chernikov 			sin6 = (const struct sockaddr_in6 *)satosin6(ifa->ifa_addr);
2045faf8dcbSSam Leffler 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2055faf8dcbSSam Leffler 				lladdr = &sin6->sin6_addr;
2065faf8dcbSSam Leffler 				break;
2075faf8dcbSSam Leffler 			}
2085faf8dcbSSam Leffler 		}
2095faf8dcbSSam Leffler 	}
2105faf8dcbSSam Leffler 	if (!lladdr)
2115faf8dcbSSam Leffler 		errx(EXIT_FAILURE, "could not determine link local address");
2125faf8dcbSSam Leffler 
2135faf8dcbSSam Leffler  	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
2145faf8dcbSSam Leffler 
2155faf8dcbSSam Leffler 	freeifaddrs(ifap);
2165faf8dcbSSam Leffler }
2175faf8dcbSSam Leffler 
2185faf8dcbSSam Leffler static void
219d1cd0344SAlexander V. Chernikov print_addr(struct sockaddr_in6 *sin)
220d1cd0344SAlexander V. Chernikov {
221d1cd0344SAlexander V. Chernikov 	int error, n_flags;
222d1cd0344SAlexander V. Chernikov 
223d1cd0344SAlexander V. Chernikov 	if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
224d1cd0344SAlexander V. Chernikov 		n_flags = 0;
225d1cd0344SAlexander V. Chernikov 	else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
226d1cd0344SAlexander V. Chernikov 		n_flags = NI_NOFQDN;
227d1cd0344SAlexander V. Chernikov 	else
228d1cd0344SAlexander V. Chernikov 		n_flags = NI_NUMERICHOST;
229d1cd0344SAlexander V. Chernikov 	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len,
230d1cd0344SAlexander V. Chernikov 			    addr_buf, sizeof(addr_buf), NULL, 0,
231d1cd0344SAlexander V. Chernikov 			    n_flags);
232d1cd0344SAlexander V. Chernikov 	if (error != 0)
233d1cd0344SAlexander V. Chernikov 		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
234d1cd0344SAlexander V. Chernikov 			  sizeof(addr_buf));
235d1cd0344SAlexander V. Chernikov 	printf("\tinet6 %s", addr_buf);
236d1cd0344SAlexander V. Chernikov }
237d1cd0344SAlexander V. Chernikov 
238d1cd0344SAlexander V. Chernikov static void
239d1cd0344SAlexander V. Chernikov print_p2p(struct sockaddr_in6 *sin)
240d1cd0344SAlexander V. Chernikov {
241d1cd0344SAlexander V. Chernikov 	int error;
242d1cd0344SAlexander V. Chernikov 
243d1cd0344SAlexander V. Chernikov 	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
244d1cd0344SAlexander V. Chernikov 	    sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
245d1cd0344SAlexander V. Chernikov 
246d1cd0344SAlexander V. Chernikov 	if (error != 0)
247d1cd0344SAlexander V. Chernikov 		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, sizeof(addr_buf));
248d1cd0344SAlexander V. Chernikov 	printf(" --> %s", addr_buf);
249d1cd0344SAlexander V. Chernikov }
250d1cd0344SAlexander V. Chernikov 
251d1cd0344SAlexander V. Chernikov static void
252d1cd0344SAlexander V. Chernikov print_mask(int plen)
253d1cd0344SAlexander V. Chernikov {
254d1cd0344SAlexander V. Chernikov 	if (f_inet6 != NULL && strcmp(f_inet6, "cidr") == 0)
255d1cd0344SAlexander V. Chernikov 		printf("/%d", plen);
256d1cd0344SAlexander V. Chernikov 	else
257d1cd0344SAlexander V. Chernikov 		printf(" prefixlen %d", plen);
258d1cd0344SAlexander V. Chernikov }
259d1cd0344SAlexander V. Chernikov 
260d1cd0344SAlexander V. Chernikov static void
261d1cd0344SAlexander V. Chernikov print_flags(int flags6)
262d1cd0344SAlexander V. Chernikov {
263d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_ANYCAST) != 0)
264d1cd0344SAlexander V. Chernikov 		printf(" anycast");
265d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_TENTATIVE) != 0)
266d1cd0344SAlexander V. Chernikov 		printf(" tentative");
267d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_DUPLICATED) != 0)
268d1cd0344SAlexander V. Chernikov 		printf(" duplicated");
269d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_DETACHED) != 0)
270d1cd0344SAlexander V. Chernikov 		printf(" detached");
271d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_DEPRECATED) != 0)
272d1cd0344SAlexander V. Chernikov 		printf(" deprecated");
273d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_AUTOCONF) != 0)
274d1cd0344SAlexander V. Chernikov 		printf(" autoconf");
275d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_TEMPORARY) != 0)
276d1cd0344SAlexander V. Chernikov 		printf(" temporary");
277d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0)
278d1cd0344SAlexander V. Chernikov 		printf(" prefer_source");
279d1cd0344SAlexander V. Chernikov 
280d1cd0344SAlexander V. Chernikov }
281d1cd0344SAlexander V. Chernikov 
282d1cd0344SAlexander V. Chernikov static void
283d1cd0344SAlexander V. Chernikov print_lifetime(const char *prepend, time_t px_time, struct timespec *now)
284d1cd0344SAlexander V. Chernikov {
285d1cd0344SAlexander V. Chernikov 	printf(" %s", prepend);
286d1cd0344SAlexander V. Chernikov 	if (px_time == 0)
287d1cd0344SAlexander V. Chernikov 		printf(" infty");
288d1cd0344SAlexander V. Chernikov 
289d1cd0344SAlexander V. Chernikov 	printf(" %s", px_time < now->tv_sec ? "0" : sec2str(px_time - now->tv_sec));
290d1cd0344SAlexander V. Chernikov }
291d1cd0344SAlexander V. Chernikov 
2924c91a5dfSAlexander V. Chernikov #ifdef WITHOUT_NETLINK
293d1cd0344SAlexander V. Chernikov static void
294*7fa282e6SAlexander V. Chernikov in6_status(if_ctx *ctx, const struct ifaddrs *ifa)
2955faf8dcbSSam Leffler {
296d1cd0344SAlexander V. Chernikov 	struct sockaddr_in6 *sin, null_sin = {};
2975faf8dcbSSam Leffler 	struct in6_ifreq ifr6;
2985faf8dcbSSam Leffler 	int s6;
2995faf8dcbSSam Leffler 	u_int32_t flags6;
3005faf8dcbSSam Leffler 	struct in6_addrlifetime lifetime;
3015faf8dcbSSam Leffler 
302cb8c905aSSam Leffler 	sin = (struct sockaddr_in6 *)ifa->ifa_addr;
3035faf8dcbSSam Leffler 	if (sin == NULL)
3045faf8dcbSSam Leffler 		return;
3055faf8dcbSSam Leffler 
306*7fa282e6SAlexander V. Chernikov 	strlcpy(ifr6.ifr_name, ctx->ifname, sizeof(ifr6.ifr_name));
3075faf8dcbSSam Leffler 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
3085faf8dcbSSam Leffler 		warn("socket(AF_INET6,SOCK_DGRAM)");
3095faf8dcbSSam Leffler 		return;
3105faf8dcbSSam Leffler 	}
3115faf8dcbSSam Leffler 	ifr6.ifr_addr = *sin;
3125faf8dcbSSam Leffler 	if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
3135faf8dcbSSam Leffler 		warn("ioctl(SIOCGIFAFLAG_IN6)");
3145faf8dcbSSam Leffler 		close(s6);
3155faf8dcbSSam Leffler 		return;
3165faf8dcbSSam Leffler 	}
3175faf8dcbSSam Leffler 	flags6 = ifr6.ifr_ifru.ifru_flags6;
3185faf8dcbSSam Leffler 	memset(&lifetime, 0, sizeof(lifetime));
3195faf8dcbSSam Leffler 	ifr6.ifr_addr = *sin;
3205faf8dcbSSam Leffler 	if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
3215faf8dcbSSam Leffler 		warn("ioctl(SIOCGIFALIFETIME_IN6)");
3225faf8dcbSSam Leffler 		close(s6);
3235faf8dcbSSam Leffler 		return;
3245faf8dcbSSam Leffler 	}
3255faf8dcbSSam Leffler 	lifetime = ifr6.ifr_ifru.ifru_lifetime;
3265faf8dcbSSam Leffler 	close(s6);
3275faf8dcbSSam Leffler 
328d1cd0344SAlexander V. Chernikov 	print_addr(sin);
3295faf8dcbSSam Leffler 
330cb8c905aSSam Leffler 	if (ifa->ifa_flags & IFF_POINTOPOINT) {
331cb8c905aSSam Leffler 		sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
3325faf8dcbSSam Leffler 		/*
3335faf8dcbSSam Leffler 		 * some of the interfaces do not have valid destination
3345faf8dcbSSam Leffler 		 * address.
3355faf8dcbSSam Leffler 		 */
336d1cd0344SAlexander V. Chernikov 		if (sin != NULL && sin->sin6_family == AF_INET6)
337d1cd0344SAlexander V. Chernikov 			print_p2p(sin);
3385faf8dcbSSam Leffler 	}
3395faf8dcbSSam Leffler 
340cb8c905aSSam Leffler 	sin = (struct sockaddr_in6 *)ifa->ifa_netmask;
341cb8c905aSSam Leffler 	if (sin == NULL)
3425faf8dcbSSam Leffler 		sin = &null_sin;
343d1cd0344SAlexander V. Chernikov 	print_mask(prefix(&sin->sin6_addr, sizeof(struct in6_addr)));
3445faf8dcbSSam Leffler 
345d1cd0344SAlexander V. Chernikov 	print_flags(flags6);
3465faf8dcbSSam Leffler 
347776db16aSAllan Jude 	if (((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id)
348976e4945SVsevolod Stakhov 		printf(" scopeid 0x%x",
349976e4945SVsevolod Stakhov 		    ((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id);
3505faf8dcbSSam Leffler 
3515faf8dcbSSam Leffler 	if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
352d1cd0344SAlexander V. Chernikov 		struct timespec now;
3535faf8dcbSSam Leffler 
354d1cd0344SAlexander V. Chernikov 		clock_gettime(CLOCK_MONOTONIC_FAST, &now);
355d1cd0344SAlexander V. Chernikov 		print_lifetime("pltime", lifetime.ia6t_preferred, &now);
356d1cd0344SAlexander V. Chernikov 		print_lifetime("vltime", lifetime.ia6t_expire, &now);
3575faf8dcbSSam Leffler 	}
3585faf8dcbSSam Leffler 
3593927d0fbSAlexander V. Chernikov 	print_vhid(ifa);
36008b68b0eSGleb Smirnoff 
3615faf8dcbSSam Leffler 	putchar('\n');
3625faf8dcbSSam Leffler }
3635faf8dcbSSam Leffler 
3644c91a5dfSAlexander V. Chernikov #else
3654c91a5dfSAlexander V. Chernikov static void
3664c91a5dfSAlexander V. Chernikov show_lifetime(struct ifa_cacheinfo *ci)
3674c91a5dfSAlexander V. Chernikov {
3684c91a5dfSAlexander V. Chernikov 	struct timespec now;
3694c91a5dfSAlexander V. Chernikov 	uint32_t pl, vl;
3704c91a5dfSAlexander V. Chernikov 
3714c91a5dfSAlexander V. Chernikov 	if (ci == NULL)
3724c91a5dfSAlexander V. Chernikov 		return;
3734c91a5dfSAlexander V. Chernikov 
3744c91a5dfSAlexander V. Chernikov 	int count = ci->ifa_prefered != ND6_INFINITE_LIFETIME;
3754c91a5dfSAlexander V. Chernikov 	count += ci->ifa_valid != ND6_INFINITE_LIFETIME;
3764c91a5dfSAlexander V. Chernikov 	if (count == 0)
3774c91a5dfSAlexander V. Chernikov 		return;
3784c91a5dfSAlexander V. Chernikov 
3794c91a5dfSAlexander V. Chernikov 	pl = (ci->ifa_prefered == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_prefered;
3804c91a5dfSAlexander V. Chernikov 	vl = (ci->ifa_valid == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_valid;
3814c91a5dfSAlexander V. Chernikov 
3824c91a5dfSAlexander V. Chernikov 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
3834c91a5dfSAlexander V. Chernikov 	print_lifetime("pltime", pl + now.tv_sec, &now);
3844c91a5dfSAlexander V. Chernikov 	print_lifetime("vltime", vl + now.tv_sec, &now);
3854c91a5dfSAlexander V. Chernikov }
3864c91a5dfSAlexander V. Chernikov 
3874c91a5dfSAlexander V. Chernikov static void
3880c2beef7SAlexander V. Chernikov in6_status_nl(if_ctx *ctx __unused, if_link_t *link __unused, if_addr_t *ifa)
3894c91a5dfSAlexander V. Chernikov {
3904c91a5dfSAlexander V. Chernikov 	int plen = ifa->ifa_prefixlen;
3914c91a5dfSAlexander V. Chernikov 	uint32_t scopeid;
3924c91a5dfSAlexander V. Chernikov 
3934c91a5dfSAlexander V. Chernikov 	if (ifa->ifa_local == NULL) {
3944c91a5dfSAlexander V. Chernikov 		/* Non-P2P address */
3954c91a5dfSAlexander V. Chernikov 		scopeid = satosin6(ifa->ifa_address)->sin6_scope_id;
3964c91a5dfSAlexander V. Chernikov 		print_addr(satosin6(ifa->ifa_address));
3974c91a5dfSAlexander V. Chernikov 	} else {
3984c91a5dfSAlexander V. Chernikov 		scopeid = satosin6(ifa->ifa_local)->sin6_scope_id;
3994c91a5dfSAlexander V. Chernikov 		print_addr(satosin6(ifa->ifa_local));
4004c91a5dfSAlexander V. Chernikov 		print_p2p(satosin6(ifa->ifa_address));
4014c91a5dfSAlexander V. Chernikov 	}
4024c91a5dfSAlexander V. Chernikov 
4034c91a5dfSAlexander V. Chernikov 	print_mask(plen);
4044c91a5dfSAlexander V. Chernikov 	print_flags(ifa->ifaf_flags);
4054c91a5dfSAlexander V. Chernikov 
4064c91a5dfSAlexander V. Chernikov 	if (scopeid != 0)
4074c91a5dfSAlexander V. Chernikov 		printf(" scopeid 0x%x", scopeid);
4084c91a5dfSAlexander V. Chernikov 
4094c91a5dfSAlexander V. Chernikov 	show_lifetime(ifa->ifa_cacheinfo);
4104c91a5dfSAlexander V. Chernikov 
4114c91a5dfSAlexander V. Chernikov 	if (ifa->ifaf_vhid != 0)
4124c91a5dfSAlexander V. Chernikov 		printf(" vhid %d", ifa->ifaf_vhid);
4134c91a5dfSAlexander V. Chernikov 
4144c91a5dfSAlexander V. Chernikov 	putchar('\n');
4154c91a5dfSAlexander V. Chernikov }
4164bf44dd7SAlexander V. Chernikov 
4174bf44dd7SAlexander V. Chernikov static struct in6_px *sin6tab_nl[] = {
4184bf44dd7SAlexander V. Chernikov         &in6_del.addr,          /* RIDADDR */
4194bf44dd7SAlexander V. Chernikov         &in6_add.addr,          /* ADDR */
4204bf44dd7SAlexander V. Chernikov         NULL,                   /* MASK */
4214bf44dd7SAlexander V. Chernikov         &in6_add.dst_addr,      /* DSTADDR*/
4224bf44dd7SAlexander V. Chernikov };
4234bf44dd7SAlexander V. Chernikov 
4244bf44dd7SAlexander V. Chernikov static void
4250c2beef7SAlexander V. Chernikov in6_copyaddr(if_ctx *ctx __unused, int to, int from)
4266d204407SAlexander V. Chernikov {
4276d204407SAlexander V. Chernikov 	sin6tab_nl[to]->addr = sin6tab_nl[from]->addr;
4286d204407SAlexander V. Chernikov 	sin6tab_nl[to]->set = sin6tab_nl[from]->set;
4296d204407SAlexander V. Chernikov }
4306d204407SAlexander V. Chernikov 
4316d204407SAlexander V. Chernikov static void
4324bf44dd7SAlexander V. Chernikov in6_getaddr(const char *addr_str, int which)
4334bf44dd7SAlexander V. Chernikov {
4344bf44dd7SAlexander V. Chernikov         struct in6_px *px = sin6tab_nl[which];
4354bf44dd7SAlexander V. Chernikov 
4364bf44dd7SAlexander V. Chernikov         px->set = true;
4374bf44dd7SAlexander V. Chernikov         px->plen = 128;
4384bf44dd7SAlexander V. Chernikov         if (which == ADDR) {
4394bf44dd7SAlexander V. Chernikov                 char *p = NULL;
4404bf44dd7SAlexander V. Chernikov                 if((p = strrchr(addr_str, '/')) != NULL) {
4414bf44dd7SAlexander V. Chernikov                         *p = '\0';
4424bf44dd7SAlexander V. Chernikov                         int plen = strtol(p + 1, NULL, 10);
4434bf44dd7SAlexander V. Chernikov 			if (plen < 0 || plen > 128)
4444bf44dd7SAlexander V. Chernikov                                 errx(1, "%s: bad value", p + 1);
4454bf44dd7SAlexander V. Chernikov                         px->plen = plen;
4464bf44dd7SAlexander V. Chernikov                         explicit_prefix = 1;
4474bf44dd7SAlexander V. Chernikov                 }
4484bf44dd7SAlexander V. Chernikov         }
4494bf44dd7SAlexander V. Chernikov 
4504bf44dd7SAlexander V. Chernikov         struct addrinfo hints = { .ai_family = AF_INET6 };
4514bf44dd7SAlexander V. Chernikov         struct addrinfo *res;
4524bf44dd7SAlexander V. Chernikov 
4534bf44dd7SAlexander V. Chernikov         int error = getaddrinfo(addr_str, NULL, &hints, &res);
4544bf44dd7SAlexander V. Chernikov         if (error != 0) {
4554bf44dd7SAlexander V. Chernikov                 if (inet_pton(AF_INET6, addr_str, &px->addr) != 1)
4564bf44dd7SAlexander V. Chernikov                         errx(1, "%s: bad value", addr_str);
4574bf44dd7SAlexander V. Chernikov         } else {
4584bf44dd7SAlexander V. Chernikov                 struct sockaddr_in6 *sin6;
4594bf44dd7SAlexander V. Chernikov 
4604bf44dd7SAlexander V. Chernikov                 sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
4614bf44dd7SAlexander V. Chernikov                 px->addr = sin6->sin6_addr;
4624bf44dd7SAlexander V. Chernikov                 freeaddrinfo(res);
4634bf44dd7SAlexander V. Chernikov         }
4644bf44dd7SAlexander V. Chernikov }
4654bf44dd7SAlexander V. Chernikov 
4664bf44dd7SAlexander V. Chernikov static int
4674bf44dd7SAlexander V. Chernikov in6_exec_nl(if_ctx *ctx, unsigned long action, void *data)
4684bf44dd7SAlexander V. Chernikov {
4694bf44dd7SAlexander V. Chernikov 	struct in6_pdata *pdata = (struct in6_pdata *)data;
4704bf44dd7SAlexander V. Chernikov 	struct snl_writer nw = {};
4714bf44dd7SAlexander V. Chernikov 
4724bf44dd7SAlexander V. Chernikov 	snl_init_writer(ctx->io_ss, &nw);
4734bf44dd7SAlexander V. Chernikov 	struct nlmsghdr *hdr = snl_create_msg_request(&nw, action);
4744bf44dd7SAlexander V. Chernikov 	struct ifaddrmsg *ifahdr = snl_reserve_msg_object(&nw, struct ifaddrmsg);
4754bf44dd7SAlexander V. Chernikov 
4764bf44dd7SAlexander V. Chernikov 	ifahdr->ifa_family = AF_INET6;
4774bf44dd7SAlexander V. Chernikov 	ifahdr->ifa_prefixlen = pdata->addr.plen;
47885e0016aSAlexander V. Chernikov 	ifahdr->ifa_index = if_nametoindex_nl(ctx->io_ss, ctx->ifname);
4794bf44dd7SAlexander V. Chernikov 
4804bf44dd7SAlexander V. Chernikov 	snl_add_msg_attr_ip6(&nw, IFA_LOCAL, &pdata->addr.addr);
4814bf44dd7SAlexander V. Chernikov 	if (action == NL_RTM_NEWADDR && pdata->dst_addr.set)
4824bf44dd7SAlexander V. Chernikov 		snl_add_msg_attr_ip6(&nw, IFA_ADDRESS, &pdata->dst_addr.addr);
4834bf44dd7SAlexander V. Chernikov 
4844bf44dd7SAlexander V. Chernikov 	struct ifa_cacheinfo ci = {
4854bf44dd7SAlexander V. Chernikov 		.ifa_prefered = pdata->lifetime.ia6t_pltime,
4864bf44dd7SAlexander V. Chernikov 		.ifa_valid =  pdata->lifetime.ia6t_vltime,
4874bf44dd7SAlexander V. Chernikov 	};
4884bf44dd7SAlexander V. Chernikov 	snl_add_msg_attr(&nw, IFA_CACHEINFO, sizeof(ci), &ci);
4894bf44dd7SAlexander V. Chernikov 
4904bf44dd7SAlexander V. Chernikov 	int off = snl_add_msg_attr_nested(&nw, IFA_FREEBSD);
4914bf44dd7SAlexander V. Chernikov 	snl_add_msg_attr_u32(&nw, IFAF_FLAGS, pdata->flags);
4924bf44dd7SAlexander V. Chernikov 	if (pdata->vhid != 0)
4934bf44dd7SAlexander V. Chernikov 		snl_add_msg_attr_u32(&nw, IFAF_VHID, pdata->vhid);
4944bf44dd7SAlexander V. Chernikov 	snl_end_attr_nested(&nw, off);
4954bf44dd7SAlexander V. Chernikov 
4964bf44dd7SAlexander V. Chernikov 	if (!snl_finalize_msg(&nw) || !snl_send_message(ctx->io_ss, hdr))
4974bf44dd7SAlexander V. Chernikov 		return (0);
4984bf44dd7SAlexander V. Chernikov 
4994bf44dd7SAlexander V. Chernikov 	struct snl_errmsg_data e = {};
5004bf44dd7SAlexander V. Chernikov 	snl_read_reply_code(ctx->io_ss, hdr->nlmsg_seq, &e);
5014bf44dd7SAlexander V. Chernikov 
5024bf44dd7SAlexander V. Chernikov 	return (e.error);
5034bf44dd7SAlexander V. Chernikov }
5044c91a5dfSAlexander V. Chernikov #endif
5054c91a5dfSAlexander V. Chernikov 
5064bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
5075faf8dcbSSam Leffler static struct	sockaddr_in6 *sin6tab[] = {
50844cd85d4SAlexander V. Chernikov 	&in6_ridreq.ifr_addr, &in6_addreq.ifra_addr,
50944cd85d4SAlexander V. Chernikov 	&in6_addreq.ifra_prefixmask, &in6_addreq.ifra_dstaddr
5105faf8dcbSSam Leffler };
5115faf8dcbSSam Leffler 
5125faf8dcbSSam Leffler static void
5136d204407SAlexander V. Chernikov in6_copyaddr(if_ctx *ctx, int to, int from)
5146d204407SAlexander V. Chernikov {
5156d204407SAlexander V. Chernikov 	memcpy(sin6tab[to], sin6tab[from], sizeof(struct sockaddr_in6));
5166d204407SAlexander V. Chernikov }
5176d204407SAlexander V. Chernikov 
5186d204407SAlexander V. Chernikov static void
5195faf8dcbSSam Leffler in6_getprefix(const char *plen, int which)
5205faf8dcbSSam Leffler {
5215faf8dcbSSam Leffler 	struct sockaddr_in6 *sin = sin6tab[which];
5225faf8dcbSSam Leffler 	u_char *cp;
5235faf8dcbSSam Leffler 	int len = atoi(plen);
5245faf8dcbSSam Leffler 
5255faf8dcbSSam Leffler 	if ((len < 0) || (len > 128))
5265faf8dcbSSam Leffler 		errx(1, "%s: bad value", plen);
5275faf8dcbSSam Leffler 	sin->sin6_len = sizeof(*sin);
5285faf8dcbSSam Leffler 	if (which != MASK)
5295faf8dcbSSam Leffler 		sin->sin6_family = AF_INET6;
5305faf8dcbSSam Leffler 	if ((len == 0) || (len == 128)) {
5315faf8dcbSSam Leffler 		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
5325faf8dcbSSam Leffler 		return;
5335faf8dcbSSam Leffler 	}
5345faf8dcbSSam Leffler 	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
5355faf8dcbSSam Leffler 	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
5365faf8dcbSSam Leffler 		*cp++ = 0xff;
5375faf8dcbSSam Leffler 	*cp = 0xff << (8 - len);
5385faf8dcbSSam Leffler }
5395faf8dcbSSam Leffler 
5405faf8dcbSSam Leffler static void
5415faf8dcbSSam Leffler in6_getaddr(const char *s, int which)
5425faf8dcbSSam Leffler {
5435faf8dcbSSam Leffler 	struct sockaddr_in6 *sin = sin6tab[which];
5445faf8dcbSSam Leffler 	struct addrinfo hints, *res;
5455faf8dcbSSam Leffler 	int error = -1;
5465faf8dcbSSam Leffler 
5475faf8dcbSSam Leffler 	sin->sin6_len = sizeof(*sin);
5485faf8dcbSSam Leffler 	if (which != MASK)
5495faf8dcbSSam Leffler 		sin->sin6_family = AF_INET6;
5505faf8dcbSSam Leffler 
5515faf8dcbSSam Leffler 	if (which == ADDR) {
5525faf8dcbSSam Leffler 		char *p = NULL;
5535faf8dcbSSam Leffler 		if((p = strrchr(s, '/')) != NULL) {
5545faf8dcbSSam Leffler 			*p = '\0';
5555faf8dcbSSam Leffler 			in6_getprefix(p + 1, MASK);
5565faf8dcbSSam Leffler 			explicit_prefix = 1;
5575faf8dcbSSam Leffler 		}
5585faf8dcbSSam Leffler 	}
5595faf8dcbSSam Leffler 
5605faf8dcbSSam Leffler 	if (sin->sin6_family == AF_INET6) {
5615faf8dcbSSam Leffler 		bzero(&hints, sizeof(struct addrinfo));
5625faf8dcbSSam Leffler 		hints.ai_family = AF_INET6;
5635faf8dcbSSam Leffler 		error = getaddrinfo(s, NULL, &hints, &res);
5645faf8dcbSSam Leffler 		if (error != 0) {
5655faf8dcbSSam Leffler 			if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
5665faf8dcbSSam Leffler 				errx(1, "%s: bad value", s);
5676223cc33SAlan Somers 		} else {
5685faf8dcbSSam Leffler 			bcopy(res->ai_addr, sin, res->ai_addrlen);
5696223cc33SAlan Somers 			freeaddrinfo(res);
5706223cc33SAlan Somers 		}
5716223cc33SAlan Somers 	}
5725faf8dcbSSam Leffler }
5735faf8dcbSSam Leffler 
5745faf8dcbSSam Leffler static int
5755faf8dcbSSam Leffler prefix(void *val, int size)
5765faf8dcbSSam Leffler {
5775faf8dcbSSam Leffler 	u_char *name = (u_char *)val;
5785faf8dcbSSam Leffler 	int byte, bit, plen = 0;
5795faf8dcbSSam Leffler 
5805faf8dcbSSam Leffler 	for (byte = 0; byte < size; byte++, plen += 8)
5815faf8dcbSSam Leffler 		if (name[byte] != 0xff)
5825faf8dcbSSam Leffler 			break;
5835faf8dcbSSam Leffler 	if (byte == size)
5845faf8dcbSSam Leffler 		return (plen);
5855faf8dcbSSam Leffler 	for (bit = 7; bit != 0; bit--, plen++)
5865faf8dcbSSam Leffler 		if (!(name[byte] & (1 << bit)))
5875faf8dcbSSam Leffler 			break;
5885faf8dcbSSam Leffler 	for (; bit != 0; bit--)
5895faf8dcbSSam Leffler 		if (name[byte] & (1 << bit))
5905faf8dcbSSam Leffler 			return(0);
5915faf8dcbSSam Leffler 	byte++;
5925faf8dcbSSam Leffler 	for (; byte < size; byte++)
5935faf8dcbSSam Leffler 		if (name[byte])
5945faf8dcbSSam Leffler 			return(0);
5955faf8dcbSSam Leffler 	return (plen);
5965faf8dcbSSam Leffler }
59744cd85d4SAlexander V. Chernikov #endif
5985faf8dcbSSam Leffler 
5995faf8dcbSSam Leffler static char *
6005faf8dcbSSam Leffler sec2str(time_t total)
6015faf8dcbSSam Leffler {
6025faf8dcbSSam Leffler 	static char result[256];
6035faf8dcbSSam Leffler 	int days, hours, mins, secs;
6045faf8dcbSSam Leffler 	int first = 1;
6055faf8dcbSSam Leffler 	char *p = result;
6065faf8dcbSSam Leffler 
6075faf8dcbSSam Leffler 	if (0) {
6085faf8dcbSSam Leffler 		days = total / 3600 / 24;
6095faf8dcbSSam Leffler 		hours = (total / 3600) % 24;
6105faf8dcbSSam Leffler 		mins = (total / 60) % 60;
6115faf8dcbSSam Leffler 		secs = total % 60;
6125faf8dcbSSam Leffler 
6135faf8dcbSSam Leffler 		if (days) {
6145faf8dcbSSam Leffler 			first = 0;
6155faf8dcbSSam Leffler 			p += sprintf(p, "%dd", days);
6165faf8dcbSSam Leffler 		}
6175faf8dcbSSam Leffler 		if (!first || hours) {
6185faf8dcbSSam Leffler 			first = 0;
6195faf8dcbSSam Leffler 			p += sprintf(p, "%dh", hours);
6205faf8dcbSSam Leffler 		}
6215faf8dcbSSam Leffler 		if (!first || mins) {
6225faf8dcbSSam Leffler 			first = 0;
6235faf8dcbSSam Leffler 			p += sprintf(p, "%dm", mins);
6245faf8dcbSSam Leffler 		}
6255faf8dcbSSam Leffler 		sprintf(p, "%ds", secs);
6265faf8dcbSSam Leffler 	} else
6275faf8dcbSSam Leffler 		sprintf(result, "%lu", (unsigned long)total);
6285faf8dcbSSam Leffler 
6295faf8dcbSSam Leffler 	return(result);
6305faf8dcbSSam Leffler }
6315faf8dcbSSam Leffler 
6325faf8dcbSSam Leffler static void
6336e3a9d7fSAlexander V. Chernikov in6_postproc(if_ctx *ctx, int newaddr __unused,
634d8237b95SMike Karels     int ifflags __unused)
6355faf8dcbSSam Leffler {
6365faf8dcbSSam Leffler 	if (explicit_prefix == 0) {
6375faf8dcbSSam Leffler 		/* Aggregatable address architecture defines all prefixes
6385faf8dcbSSam Leffler 		   are 64. So, it is convenient to set prefixlen to 64 if
6395faf8dcbSSam Leffler 		   it is not specified. */
6406e3a9d7fSAlexander V. Chernikov 		setifprefixlen(ctx, "64", 0);
6415faf8dcbSSam Leffler 		/* in6_getprefix("64", MASK) if MASK is available here... */
6425faf8dcbSSam Leffler 	}
6435faf8dcbSSam Leffler }
6445faf8dcbSSam Leffler 
6455faf8dcbSSam Leffler static void
646c6f0602fSAlexander V. Chernikov in6_status_tunnel(if_ctx *ctx)
6475faf8dcbSSam Leffler {
6485faf8dcbSSam Leffler 	char src[NI_MAXHOST];
6495faf8dcbSSam Leffler 	char dst[NI_MAXHOST];
6505faf8dcbSSam Leffler 	struct in6_ifreq in6_ifr;
6515faf8dcbSSam Leffler 	const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
6525faf8dcbSSam Leffler 
6535faf8dcbSSam Leffler 	memset(&in6_ifr, 0, sizeof(in6_ifr));
65485e0016aSAlexander V. Chernikov 	strlcpy(in6_ifr.ifr_name, ctx->ifname, sizeof(in6_ifr.ifr_name));
6555faf8dcbSSam Leffler 
656c6f0602fSAlexander V. Chernikov 	if (ioctl_ctx(ctx, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
6575faf8dcbSSam Leffler 		return;
65873bbc449SHajimu UMEMOTO 	if (sa->sa_family != AF_INET6)
65973bbc449SHajimu UMEMOTO 		return;
6604f101318SHajimu UMEMOTO 	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
6614f101318SHajimu UMEMOTO 	    NI_NUMERICHOST) != 0)
6625faf8dcbSSam Leffler 		src[0] = '\0';
6635faf8dcbSSam Leffler 
664c6f0602fSAlexander V. Chernikov 	if (ioctl_ctx(ctx, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
6655faf8dcbSSam Leffler 		return;
66673bbc449SHajimu UMEMOTO 	if (sa->sa_family != AF_INET6)
66773bbc449SHajimu UMEMOTO 		return;
6684f101318SHajimu UMEMOTO 	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
6694f101318SHajimu UMEMOTO 	    NI_NUMERICHOST) != 0)
6705faf8dcbSSam Leffler 		dst[0] = '\0';
6715faf8dcbSSam Leffler 
6725faf8dcbSSam Leffler 	printf("\ttunnel inet6 %s --> %s\n", src, dst);
6735faf8dcbSSam Leffler }
6745faf8dcbSSam Leffler 
6755faf8dcbSSam Leffler static void
676c6f0602fSAlexander V. Chernikov in6_set_tunnel(if_ctx *ctx, struct addrinfo *srcres, struct addrinfo *dstres)
6775faf8dcbSSam Leffler {
6785faf8dcbSSam Leffler 	struct in6_aliasreq in6_addreq;
6795faf8dcbSSam Leffler 
6805faf8dcbSSam Leffler 	memset(&in6_addreq, 0, sizeof(in6_addreq));
68185e0016aSAlexander V. Chernikov 	strlcpy(in6_addreq.ifra_name, ctx->ifname, sizeof(in6_addreq.ifra_name));
6825faf8dcbSSam Leffler 	memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
6835faf8dcbSSam Leffler 	memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
6845faf8dcbSSam Leffler 	    dstres->ai_addr->sa_len);
6855faf8dcbSSam Leffler 
686c6f0602fSAlexander V. Chernikov 	if (ioctl_ctx(ctx, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
6875faf8dcbSSam Leffler 		warn("SIOCSIFPHYADDR_IN6");
6885faf8dcbSSam Leffler }
6895faf8dcbSSam Leffler 
690a72b7890SAlexander V. Chernikov static void
691a72b7890SAlexander V. Chernikov in6_set_vhid(int vhid)
692a72b7890SAlexander V. Chernikov {
6934bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
694a72b7890SAlexander V. Chernikov 	in6_addreq.ifra_vhid = vhid;
6954bf44dd7SAlexander V. Chernikov #else
6964bf44dd7SAlexander V. Chernikov 	in6_add.vhid = (uint32_t)vhid;
6974bf44dd7SAlexander V. Chernikov #endif
698a72b7890SAlexander V. Chernikov }
699a72b7890SAlexander V. Chernikov 
7005faf8dcbSSam Leffler static struct cmd inet6_cmds[] = {
7015faf8dcbSSam Leffler 	DEF_CMD_ARG("prefixlen",			setifprefixlen),
7025faf8dcbSSam Leffler 	DEF_CMD("anycast",	IN6_IFF_ANYCAST,	setip6flags),
7035faf8dcbSSam Leffler 	DEF_CMD("tentative",	IN6_IFF_TENTATIVE,	setip6flags),
7045faf8dcbSSam Leffler 	DEF_CMD("-tentative",	-IN6_IFF_TENTATIVE,	setip6flags),
7055faf8dcbSSam Leffler 	DEF_CMD("deprecated",	IN6_IFF_DEPRECATED,	setip6flags),
7065faf8dcbSSam Leffler 	DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED,	setip6flags),
7075faf8dcbSSam Leffler 	DEF_CMD("autoconf",	IN6_IFF_AUTOCONF,	setip6flags),
7085faf8dcbSSam Leffler 	DEF_CMD("-autoconf",	-IN6_IFF_AUTOCONF,	setip6flags),
7095dbfa43fSAndrey V. Elsukov 	DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE,	setip6flags),
7105dbfa43fSAndrey V. Elsukov 	DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
711a283298cSHiroki Sato 	DEF_CMD("accept_rtadv",	ND6_IFF_ACCEPT_RTADV,	setnd6flags),
712a283298cSHiroki Sato 	DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV,	setnd6flags),
713e7fa8d0aSHiroki Sato 	DEF_CMD("no_radr",	ND6_IFF_NO_RADR,	setnd6flags),
714e7fa8d0aSHiroki Sato 	DEF_CMD("-no_radr",	-ND6_IFF_NO_RADR,	setnd6flags),
715a283298cSHiroki Sato 	DEF_CMD("defaultif",	1,			setnd6defif),
716a283298cSHiroki Sato 	DEF_CMD("-defaultif",	-1,			setnd6defif),
717a283298cSHiroki Sato 	DEF_CMD("ifdisabled",	ND6_IFF_IFDISABLED,	setnd6flags),
718a283298cSHiroki Sato 	DEF_CMD("-ifdisabled",	-ND6_IFF_IFDISABLED,	setnd6flags),
719a283298cSHiroki Sato 	DEF_CMD("nud",		ND6_IFF_PERFORMNUD,	setnd6flags),
720a283298cSHiroki Sato 	DEF_CMD("-nud",		-ND6_IFF_PERFORMNUD,	setnd6flags),
721a283298cSHiroki Sato 	DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
722a283298cSHiroki Sato 	DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
723164051ceSHajimu UMEMOTO 	DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
724164051ceSHajimu UMEMOTO 	DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
72523e9ffb0SHiroki Sato 	DEF_CMD("no_dad",	ND6_IFF_NO_DAD,		setnd6flags),
72623e9ffb0SHiroki Sato 	DEF_CMD("-no_dad",	-ND6_IFF_NO_DAD,	setnd6flags),
7275faf8dcbSSam Leffler 	DEF_CMD_ARG("pltime",        			setip6pltime),
7285faf8dcbSSam Leffler 	DEF_CMD_ARG("vltime",        			setip6vltime),
7295faf8dcbSSam Leffler 	DEF_CMD("eui64",	0,			setip6eui64),
73021231a7aSBjoern A. Zeeb #ifdef EXPERIMENTAL
73121231a7aSBjoern A. Zeeb 	DEF_CMD("ipv6_only",	ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
73221231a7aSBjoern A. Zeeb 	DEF_CMD("-ipv6_only",	-ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
73321231a7aSBjoern A. Zeeb #endif
7345faf8dcbSSam Leffler };
7355faf8dcbSSam Leffler 
7365faf8dcbSSam Leffler static struct afswtch af_inet6 = {
7375faf8dcbSSam Leffler 	.af_name	= "inet6",
7385faf8dcbSSam Leffler 	.af_af		= AF_INET6,
7394c91a5dfSAlexander V. Chernikov #ifdef WITHOUT_NETLINK
7405faf8dcbSSam Leffler 	.af_status	= in6_status,
7414c91a5dfSAlexander V. Chernikov #else
7426e3a9d7fSAlexander V. Chernikov 	.af_status	= in6_status_nl,
7434c91a5dfSAlexander V. Chernikov #endif
7445faf8dcbSSam Leffler 	.af_getaddr	= in6_getaddr,
7456d204407SAlexander V. Chernikov 	.af_copyaddr	= in6_copyaddr,
7464bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
7475faf8dcbSSam Leffler 	.af_getprefix	= in6_getprefix,
7484bf44dd7SAlexander V. Chernikov #endif
749c3cc3217SHiroki Sato 	.af_other_status = nd6_status,
7505faf8dcbSSam Leffler 	.af_postproc	= in6_postproc,
7515faf8dcbSSam Leffler 	.af_status_tunnel = in6_status_tunnel,
7525faf8dcbSSam Leffler 	.af_settunnel	= in6_set_tunnel,
753a72b7890SAlexander V. Chernikov 	.af_setvhid	= in6_set_vhid,
7544bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
7555faf8dcbSSam Leffler 	.af_difaddr	= SIOCDIFADDR_IN6,
7565faf8dcbSSam Leffler 	.af_aifaddr	= SIOCAIFADDR_IN6,
7571b153c92SBruce M Simpson 	.af_ridreq	= &in6_addreq,
7585faf8dcbSSam Leffler 	.af_addreq	= &in6_addreq,
7594bf44dd7SAlexander V. Chernikov 	.af_exec	= af_exec_ioctl,
7604bf44dd7SAlexander V. Chernikov #else
7614bf44dd7SAlexander V. Chernikov 	.af_difaddr	= NL_RTM_DELADDR,
7624bf44dd7SAlexander V. Chernikov 	.af_aifaddr	= NL_RTM_NEWADDR,
7634bf44dd7SAlexander V. Chernikov 	.af_ridreq	= &in6_add,
7644bf44dd7SAlexander V. Chernikov 	.af_addreq	= &in6_add,
7654bf44dd7SAlexander V. Chernikov 	.af_exec	= in6_exec_nl,
7664bf44dd7SAlexander V. Chernikov #endif
7675faf8dcbSSam Leffler };
7685faf8dcbSSam Leffler 
7695faf8dcbSSam Leffler static void
7700c2beef7SAlexander V. Chernikov in6_Lopt_cb(const char *arg __unused)
7715faf8dcbSSam Leffler {
7725faf8dcbSSam Leffler 	ip6lifetime++;	/* print IPv6 address lifetime */
7735faf8dcbSSam Leffler }
77407a51db9SVsevolod Stakhov static struct option in6_Lopt = {
77507a51db9SVsevolod Stakhov 	.opt = "L",
77607a51db9SVsevolod Stakhov 	.opt_usage = "[-L]",
77707a51db9SVsevolod Stakhov 	.cb = in6_Lopt_cb
77807a51db9SVsevolod Stakhov };
7795faf8dcbSSam Leffler 
7805faf8dcbSSam Leffler static __constructor void
7815faf8dcbSSam Leffler inet6_ctor(void)
7825faf8dcbSSam Leffler {
783b59dcaeeSXin LI 	size_t i;
7845faf8dcbSSam Leffler 
785ad2571a2SBjoern A. Zeeb #ifndef RESCUE
7865af3fa9aSBjoern A. Zeeb 	if (!feature_present("inet6"))
7875af3fa9aSBjoern A. Zeeb 		return;
788ad2571a2SBjoern A. Zeeb #endif
7895af3fa9aSBjoern A. Zeeb 
790abd71050SEnji Cooper 	for (i = 0; i < nitems(inet6_cmds);  i++)
7915faf8dcbSSam Leffler 		cmd_register(&inet6_cmds[i]);
7925faf8dcbSSam Leffler 	af_register(&af_inet6);
7935faf8dcbSSam Leffler 	opt_register(&in6_Lopt);
7945faf8dcbSSam Leffler }
795