xref: /freebsd/sbin/ifconfig/af_inet6.c (revision 4f8f43b06ed07e96a250855488cc531799d5b78f)
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 #include <sys/param.h>
335faf8dcbSSam Leffler #include <sys/ioctl.h>
345faf8dcbSSam Leffler #include <sys/socket.h>
355faf8dcbSSam Leffler #include <net/if.h>
365faf8dcbSSam Leffler 
375faf8dcbSSam Leffler #include <err.h>
385faf8dcbSSam Leffler #include <stdio.h>
395faf8dcbSSam Leffler #include <stdlib.h>
405faf8dcbSSam Leffler #include <string.h>
415faf8dcbSSam Leffler #include <unistd.h>
42965ef03cSHajimu UMEMOTO #include <time.h>
435faf8dcbSSam Leffler #include <ifaddrs.h>
445faf8dcbSSam Leffler 
455faf8dcbSSam Leffler #include <arpa/inet.h>
465faf8dcbSSam Leffler 
475faf8dcbSSam Leffler #include <netinet/in.h>
485faf8dcbSSam Leffler #include <netinet/in_var.h>
495faf8dcbSSam Leffler #include <arpa/inet.h>
505faf8dcbSSam Leffler #include <netdb.h>
515faf8dcbSSam Leffler 
525faf8dcbSSam Leffler #include <netinet6/nd6.h>	/* Define ND6_INFINITE_LIFETIME */
535faf8dcbSSam Leffler 
545faf8dcbSSam Leffler #include "ifconfig.h"
554c91a5dfSAlexander V. Chernikov #include "ifconfig_netlink.h"
565faf8dcbSSam Leffler 
574bf44dd7SAlexander V. Chernikov #ifndef WITHOUT_NETLINK
584bf44dd7SAlexander V. Chernikov struct in6_px {
594bf44dd7SAlexander V. Chernikov 	struct in6_addr		addr;
604bf44dd7SAlexander V. Chernikov 	int			plen;
614bf44dd7SAlexander V. Chernikov 	bool			set;
624bf44dd7SAlexander V. Chernikov };
634bf44dd7SAlexander V. Chernikov struct in6_pdata {
644bf44dd7SAlexander V. Chernikov 	struct in6_px		addr;
654bf44dd7SAlexander V. Chernikov 	struct in6_px		dst_addr;
664bf44dd7SAlexander V. Chernikov 	struct in6_addrlifetime	lifetime;
674bf44dd7SAlexander V. Chernikov 	uint32_t		flags;
684bf44dd7SAlexander V. Chernikov 	uint32_t		vhid;
694bf44dd7SAlexander V. Chernikov };
704bf44dd7SAlexander V. Chernikov 
714bf44dd7SAlexander V. Chernikov static struct in6_pdata in6_del;
724bf44dd7SAlexander V. Chernikov static struct in6_pdata in6_add = {
734bf44dd7SAlexander V. Chernikov 	.lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME },
744bf44dd7SAlexander V. Chernikov };
754bf44dd7SAlexander V. Chernikov #else
765faf8dcbSSam Leffler static	struct in6_ifreq in6_ridreq;
775faf8dcbSSam Leffler static	struct in6_aliasreq in6_addreq =
78b59dcaeeSXin LI   { .ifra_flags = 0,
79b59dcaeeSXin LI     .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
804bf44dd7SAlexander V. Chernikov #endif
815faf8dcbSSam Leffler static	int ip6lifetime;
825faf8dcbSSam Leffler 
8344cd85d4SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
845faf8dcbSSam Leffler static	int prefix(void *, int);
8544cd85d4SAlexander V. Chernikov #endif
865faf8dcbSSam Leffler static	char *sec2str(time_t);
875faf8dcbSSam Leffler static	int explicit_prefix = 0;
88776db16aSAllan Jude extern	char *f_inet6, *f_addr;
895faf8dcbSSam Leffler 
906e3a9d7fSAlexander V. Chernikov extern void setnd6flags(if_ctx *, const char *, int);
916e3a9d7fSAlexander V. Chernikov extern void setnd6defif(if_ctx *,const char *, int);
926e3a9d7fSAlexander V. Chernikov extern void nd6_status(if_ctx *);
93a283298cSHiroki Sato 
94776db16aSAllan Jude static	char addr_buf[NI_MAXHOST];	/*for getnameinfo()*/
955faf8dcbSSam Leffler 
965faf8dcbSSam Leffler static void
setifprefixlen(if_ctx * ctx __netlink_unused,const char * addr,int dummy __unused)970c2beef7SAlexander V. Chernikov setifprefixlen(if_ctx *ctx __netlink_unused, const char *addr, int dummy __unused)
985faf8dcbSSam Leffler {
994bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
1006e3a9d7fSAlexander V. Chernikov 	const struct afswtch *afp = ctx->afp;
1016e3a9d7fSAlexander V. Chernikov 
1025faf8dcbSSam Leffler         if (afp->af_getprefix != NULL)
1035faf8dcbSSam Leffler                 afp->af_getprefix(addr, MASK);
1044bf44dd7SAlexander V. Chernikov #else
1054bf44dd7SAlexander V. Chernikov 	int plen = strtol(addr, NULL, 10);
1064bf44dd7SAlexander V. Chernikov 
1074bf44dd7SAlexander V. Chernikov 	if ((plen < 0) || (plen > 128))
1084bf44dd7SAlexander V. Chernikov 		errx(1, "%s: bad value", addr);
1094bf44dd7SAlexander V. Chernikov 	in6_add.addr.plen = plen;
1104bf44dd7SAlexander V. Chernikov #endif
1115faf8dcbSSam Leffler 	explicit_prefix = 1;
1125faf8dcbSSam Leffler }
1135faf8dcbSSam Leffler 
1145faf8dcbSSam Leffler static void
setip6flags(if_ctx * ctx,const char * dummyaddr __unused,int flag)1156e3a9d7fSAlexander V. Chernikov setip6flags(if_ctx *ctx, const char *dummyaddr __unused, int flag)
1165faf8dcbSSam Leffler {
1176e3a9d7fSAlexander V. Chernikov 	const struct afswtch *afp = ctx->afp;
1186e3a9d7fSAlexander V. Chernikov 
1195faf8dcbSSam Leffler 	if (afp->af_af != AF_INET6)
1205faf8dcbSSam Leffler 		err(1, "address flags can be set only for inet6 addresses");
1215faf8dcbSSam Leffler 
1224bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
1235faf8dcbSSam Leffler 	if (flag < 0)
1245faf8dcbSSam Leffler 		in6_addreq.ifra_flags &= ~(-flag);
1255faf8dcbSSam Leffler 	else
1265faf8dcbSSam Leffler 		in6_addreq.ifra_flags |= flag;
1274bf44dd7SAlexander V. Chernikov #else
1284bf44dd7SAlexander V. Chernikov 	if (flag < 0)
1294bf44dd7SAlexander V. Chernikov 		in6_add.flags &= ~(-flag);
1304bf44dd7SAlexander V. Chernikov 	else
1314bf44dd7SAlexander V. Chernikov 		in6_add.flags |= flag;
1324bf44dd7SAlexander V. Chernikov #endif
1335faf8dcbSSam Leffler }
1345faf8dcbSSam Leffler 
1355faf8dcbSSam Leffler static void
setip6lifetime(if_ctx * ctx,const char * cmd,const char * val)1366e3a9d7fSAlexander V. Chernikov setip6lifetime(if_ctx *ctx, const char *cmd, const char *val)
1375faf8dcbSSam Leffler {
1386e3a9d7fSAlexander V. Chernikov 	const struct afswtch *afp = ctx->afp;
139965ef03cSHajimu UMEMOTO 	struct timespec now;
140965ef03cSHajimu UMEMOTO 	time_t newval;
1415faf8dcbSSam Leffler 	char *ep;
1424bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
1434bf44dd7SAlexander V. Chernikov 	struct in6_addrlifetime *lifetime = &in6_addreq.ifra_lifetime;
1444bf44dd7SAlexander V. Chernikov #else
1454bf44dd7SAlexander V. Chernikov 	struct in6_addrlifetime *lifetime = &in6_add.lifetime;
1464bf44dd7SAlexander V. Chernikov #endif
1475faf8dcbSSam Leffler 
148965ef03cSHajimu UMEMOTO 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1495faf8dcbSSam Leffler 	newval = (time_t)strtoul(val, &ep, 0);
1505faf8dcbSSam Leffler 	if (val == ep)
1515faf8dcbSSam Leffler 		errx(1, "invalid %s", cmd);
1525faf8dcbSSam Leffler 	if (afp->af_af != AF_INET6)
1535faf8dcbSSam Leffler 		errx(1, "%s not allowed for the AF", cmd);
1545faf8dcbSSam Leffler 	if (strcmp(cmd, "vltime") == 0) {
1554bf44dd7SAlexander V. Chernikov 		lifetime->ia6t_expire = now.tv_sec + newval;
1564bf44dd7SAlexander V. Chernikov 		lifetime->ia6t_vltime = newval;
1575faf8dcbSSam Leffler 	} else if (strcmp(cmd, "pltime") == 0) {
1584bf44dd7SAlexander V. Chernikov 		lifetime->ia6t_preferred = now.tv_sec + newval;
1594bf44dd7SAlexander V. Chernikov 		lifetime->ia6t_pltime = newval;
1605faf8dcbSSam Leffler 	}
1615faf8dcbSSam Leffler }
1625faf8dcbSSam Leffler 
1635faf8dcbSSam Leffler static void
setip6pltime(if_ctx * ctx,const char * seconds,int dummy __unused)1646e3a9d7fSAlexander V. Chernikov setip6pltime(if_ctx *ctx, const char *seconds, int dummy __unused)
1655faf8dcbSSam Leffler {
1666e3a9d7fSAlexander V. Chernikov 	setip6lifetime(ctx, "pltime", seconds);
1675faf8dcbSSam Leffler }
1685faf8dcbSSam Leffler 
1695faf8dcbSSam Leffler static void
setip6vltime(if_ctx * ctx,const char * seconds,int dummy __unused)1706e3a9d7fSAlexander V. Chernikov setip6vltime(if_ctx *ctx, const char *seconds, int dummy __unused)
1715faf8dcbSSam Leffler {
1726e3a9d7fSAlexander V. Chernikov 	setip6lifetime(ctx, "vltime", seconds);
1735faf8dcbSSam Leffler }
1745faf8dcbSSam Leffler 
1755faf8dcbSSam Leffler static void
setip6eui64(if_ctx * ctx,const char * cmd,int dummy __unused)1766e3a9d7fSAlexander V. Chernikov setip6eui64(if_ctx *ctx, const char *cmd, int dummy __unused)
1775faf8dcbSSam Leffler {
1786e3a9d7fSAlexander V. Chernikov 	const struct afswtch *afp = ctx->afp;
1795faf8dcbSSam Leffler 	struct ifaddrs *ifap, *ifa;
1805faf8dcbSSam Leffler 	const struct sockaddr_in6 *sin6 = NULL;
1815faf8dcbSSam Leffler 	const struct in6_addr *lladdr = NULL;
1825faf8dcbSSam Leffler 	struct in6_addr *in6;
1835faf8dcbSSam Leffler 
1845faf8dcbSSam Leffler 	if (afp->af_af != AF_INET6)
1855faf8dcbSSam Leffler 		errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
1864bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
1875faf8dcbSSam Leffler  	in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
1884bf44dd7SAlexander V. Chernikov #else
1894bf44dd7SAlexander V. Chernikov 	in6 = &in6_add.addr.addr;
1904bf44dd7SAlexander V. Chernikov #endif
1915faf8dcbSSam Leffler 	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
1925faf8dcbSSam Leffler 		errx(EXIT_FAILURE, "interface index is already filled");
1935faf8dcbSSam Leffler 	if (getifaddrs(&ifap) != 0)
1945faf8dcbSSam Leffler 		err(EXIT_FAILURE, "getifaddrs");
1955faf8dcbSSam Leffler 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1965faf8dcbSSam Leffler 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
19785e0016aSAlexander V. Chernikov 		    strcmp(ifa->ifa_name, ctx->ifname) == 0) {
19844cd85d4SAlexander V. Chernikov 			sin6 = (const struct sockaddr_in6 *)satosin6(ifa->ifa_addr);
1995faf8dcbSSam Leffler 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2005faf8dcbSSam Leffler 				lladdr = &sin6->sin6_addr;
2015faf8dcbSSam Leffler 				break;
2025faf8dcbSSam Leffler 			}
2035faf8dcbSSam Leffler 		}
2045faf8dcbSSam Leffler 	}
2055faf8dcbSSam Leffler 	if (!lladdr)
2065faf8dcbSSam Leffler 		errx(EXIT_FAILURE, "could not determine link local address");
2075faf8dcbSSam Leffler 
2085faf8dcbSSam Leffler  	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
2095faf8dcbSSam Leffler 
2105faf8dcbSSam Leffler 	freeifaddrs(ifap);
2115faf8dcbSSam Leffler }
2125faf8dcbSSam Leffler 
2135faf8dcbSSam Leffler static void
print_addr(struct sockaddr_in6 * sin)214d1cd0344SAlexander V. Chernikov print_addr(struct sockaddr_in6 *sin)
215d1cd0344SAlexander V. Chernikov {
216d1cd0344SAlexander V. Chernikov 	int error, n_flags;
217d1cd0344SAlexander V. Chernikov 
218d1cd0344SAlexander V. Chernikov 	if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
219d1cd0344SAlexander V. Chernikov 		n_flags = 0;
220d1cd0344SAlexander V. Chernikov 	else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
221d1cd0344SAlexander V. Chernikov 		n_flags = NI_NOFQDN;
222d1cd0344SAlexander V. Chernikov 	else
223d1cd0344SAlexander V. Chernikov 		n_flags = NI_NUMERICHOST;
224d1cd0344SAlexander V. Chernikov 	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len,
225d1cd0344SAlexander V. Chernikov 			    addr_buf, sizeof(addr_buf), NULL, 0,
226d1cd0344SAlexander V. Chernikov 			    n_flags);
227d1cd0344SAlexander V. Chernikov 	if (error != 0)
228d1cd0344SAlexander V. Chernikov 		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
229d1cd0344SAlexander V. Chernikov 			  sizeof(addr_buf));
230d1cd0344SAlexander V. Chernikov 	printf("\tinet6 %s", addr_buf);
231d1cd0344SAlexander V. Chernikov }
232d1cd0344SAlexander V. Chernikov 
233d1cd0344SAlexander V. Chernikov static void
print_p2p(struct sockaddr_in6 * sin)234d1cd0344SAlexander V. Chernikov print_p2p(struct sockaddr_in6 *sin)
235d1cd0344SAlexander V. Chernikov {
236d1cd0344SAlexander V. Chernikov 	int error;
237d1cd0344SAlexander V. Chernikov 
238d1cd0344SAlexander V. Chernikov 	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
239d1cd0344SAlexander V. Chernikov 	    sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
240d1cd0344SAlexander V. Chernikov 
241d1cd0344SAlexander V. Chernikov 	if (error != 0)
242d1cd0344SAlexander V. Chernikov 		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, sizeof(addr_buf));
243d1cd0344SAlexander V. Chernikov 	printf(" --> %s", addr_buf);
244d1cd0344SAlexander V. Chernikov }
245d1cd0344SAlexander V. Chernikov 
246d1cd0344SAlexander V. Chernikov static void
print_mask(int plen)247d1cd0344SAlexander V. Chernikov print_mask(int plen)
248d1cd0344SAlexander V. Chernikov {
249d1cd0344SAlexander V. Chernikov 	if (f_inet6 != NULL && strcmp(f_inet6, "cidr") == 0)
250d1cd0344SAlexander V. Chernikov 		printf("/%d", plen);
251d1cd0344SAlexander V. Chernikov 	else
252d1cd0344SAlexander V. Chernikov 		printf(" prefixlen %d", plen);
253d1cd0344SAlexander V. Chernikov }
254d1cd0344SAlexander V. Chernikov 
255d1cd0344SAlexander V. Chernikov static void
print_flags(int flags6)256d1cd0344SAlexander V. Chernikov print_flags(int flags6)
257d1cd0344SAlexander V. Chernikov {
258d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_ANYCAST) != 0)
259d1cd0344SAlexander V. Chernikov 		printf(" anycast");
260d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_TENTATIVE) != 0)
261d1cd0344SAlexander V. Chernikov 		printf(" tentative");
262d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_DUPLICATED) != 0)
263d1cd0344SAlexander V. Chernikov 		printf(" duplicated");
264d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_DETACHED) != 0)
265d1cd0344SAlexander V. Chernikov 		printf(" detached");
266d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_DEPRECATED) != 0)
267d1cd0344SAlexander V. Chernikov 		printf(" deprecated");
268d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_AUTOCONF) != 0)
269d1cd0344SAlexander V. Chernikov 		printf(" autoconf");
270d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_TEMPORARY) != 0)
271d1cd0344SAlexander V. Chernikov 		printf(" temporary");
272d1cd0344SAlexander V. Chernikov 	if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0)
273d1cd0344SAlexander V. Chernikov 		printf(" prefer_source");
274d1cd0344SAlexander V. Chernikov 
275d1cd0344SAlexander V. Chernikov }
276d1cd0344SAlexander V. Chernikov 
277d1cd0344SAlexander V. Chernikov static void
print_lifetime(const char * prepend,time_t px_time,struct timespec * now)278d1cd0344SAlexander V. Chernikov print_lifetime(const char *prepend, time_t px_time, struct timespec *now)
279d1cd0344SAlexander V. Chernikov {
280d1cd0344SAlexander V. Chernikov 	printf(" %s", prepend);
281d1cd0344SAlexander V. Chernikov 	if (px_time == 0)
282d1cd0344SAlexander V. Chernikov 		printf(" infty");
283d1cd0344SAlexander V. Chernikov 
284d1cd0344SAlexander V. Chernikov 	printf(" %s", px_time < now->tv_sec ? "0" : sec2str(px_time - now->tv_sec));
285d1cd0344SAlexander V. Chernikov }
286d1cd0344SAlexander V. Chernikov 
2874c91a5dfSAlexander V. Chernikov #ifdef WITHOUT_NETLINK
288d1cd0344SAlexander V. Chernikov static void
in6_status(if_ctx * ctx,const struct ifaddrs * ifa)2897fa282e6SAlexander V. Chernikov in6_status(if_ctx *ctx, const struct ifaddrs *ifa)
2905faf8dcbSSam Leffler {
291d1cd0344SAlexander V. Chernikov 	struct sockaddr_in6 *sin, null_sin = {};
2925faf8dcbSSam Leffler 	struct in6_ifreq ifr6;
2935faf8dcbSSam Leffler 	int s6;
2945faf8dcbSSam Leffler 	u_int32_t flags6;
2955faf8dcbSSam Leffler 	struct in6_addrlifetime lifetime;
2965faf8dcbSSam Leffler 
297a3930cd4SAlexander V. Chernikov 	sin = satosin6(ifa->ifa_addr);
2985faf8dcbSSam Leffler 	if (sin == NULL)
2995faf8dcbSSam Leffler 		return;
3005faf8dcbSSam Leffler 
3017fa282e6SAlexander V. Chernikov 	strlcpy(ifr6.ifr_name, ctx->ifname, sizeof(ifr6.ifr_name));
3025faf8dcbSSam Leffler 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
3035faf8dcbSSam Leffler 		warn("socket(AF_INET6,SOCK_DGRAM)");
3045faf8dcbSSam Leffler 		return;
3055faf8dcbSSam Leffler 	}
3065faf8dcbSSam Leffler 	ifr6.ifr_addr = *sin;
3075faf8dcbSSam Leffler 	if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
3085faf8dcbSSam Leffler 		warn("ioctl(SIOCGIFAFLAG_IN6)");
3095faf8dcbSSam Leffler 		close(s6);
3105faf8dcbSSam Leffler 		return;
3115faf8dcbSSam Leffler 	}
3125faf8dcbSSam Leffler 	flags6 = ifr6.ifr_ifru.ifru_flags6;
3135faf8dcbSSam Leffler 	memset(&lifetime, 0, sizeof(lifetime));
3145faf8dcbSSam Leffler 	ifr6.ifr_addr = *sin;
3155faf8dcbSSam Leffler 	if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
3165faf8dcbSSam Leffler 		warn("ioctl(SIOCGIFALIFETIME_IN6)");
3175faf8dcbSSam Leffler 		close(s6);
3185faf8dcbSSam Leffler 		return;
3195faf8dcbSSam Leffler 	}
3205faf8dcbSSam Leffler 	lifetime = ifr6.ifr_ifru.ifru_lifetime;
3215faf8dcbSSam Leffler 	close(s6);
3225faf8dcbSSam Leffler 
323d1cd0344SAlexander V. Chernikov 	print_addr(sin);
3245faf8dcbSSam Leffler 
325cb8c905aSSam Leffler 	if (ifa->ifa_flags & IFF_POINTOPOINT) {
326a3930cd4SAlexander V. Chernikov 		sin = satosin6(ifa->ifa_dstaddr);
3275faf8dcbSSam Leffler 		/*
3285faf8dcbSSam Leffler 		 * some of the interfaces do not have valid destination
3295faf8dcbSSam Leffler 		 * address.
3305faf8dcbSSam Leffler 		 */
331d1cd0344SAlexander V. Chernikov 		if (sin != NULL && sin->sin6_family == AF_INET6)
332d1cd0344SAlexander V. Chernikov 			print_p2p(sin);
3335faf8dcbSSam Leffler 	}
3345faf8dcbSSam Leffler 
335a3930cd4SAlexander V. Chernikov 	sin = satosin6(ifa->ifa_netmask);
336cb8c905aSSam Leffler 	if (sin == NULL)
3375faf8dcbSSam Leffler 		sin = &null_sin;
338d1cd0344SAlexander V. Chernikov 	print_mask(prefix(&sin->sin6_addr, sizeof(struct in6_addr)));
3395faf8dcbSSam Leffler 
340d1cd0344SAlexander V. Chernikov 	print_flags(flags6);
3415faf8dcbSSam Leffler 
342a3930cd4SAlexander V. Chernikov 	if ((satosin6(ifa->ifa_addr))->sin6_scope_id)
343976e4945SVsevolod Stakhov 		printf(" scopeid 0x%x",
344a3930cd4SAlexander V. Chernikov 		    (satosin6(ifa->ifa_addr))->sin6_scope_id);
3455faf8dcbSSam Leffler 
3465faf8dcbSSam Leffler 	if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
347d1cd0344SAlexander V. Chernikov 		struct timespec now;
3485faf8dcbSSam Leffler 
349d1cd0344SAlexander V. Chernikov 		clock_gettime(CLOCK_MONOTONIC_FAST, &now);
350d1cd0344SAlexander V. Chernikov 		print_lifetime("pltime", lifetime.ia6t_preferred, &now);
351d1cd0344SAlexander V. Chernikov 		print_lifetime("vltime", lifetime.ia6t_expire, &now);
3525faf8dcbSSam Leffler 	}
3535faf8dcbSSam Leffler 
3543927d0fbSAlexander V. Chernikov 	print_vhid(ifa);
35508b68b0eSGleb Smirnoff 
3565faf8dcbSSam Leffler 	putchar('\n');
3575faf8dcbSSam Leffler }
3585faf8dcbSSam Leffler 
3594c91a5dfSAlexander V. Chernikov #else
3604c91a5dfSAlexander V. Chernikov static void
show_lifetime(struct ifa_cacheinfo * ci)3614c91a5dfSAlexander V. Chernikov show_lifetime(struct ifa_cacheinfo *ci)
3624c91a5dfSAlexander V. Chernikov {
3634c91a5dfSAlexander V. Chernikov 	struct timespec now;
3644c91a5dfSAlexander V. Chernikov 	uint32_t pl, vl;
3654c91a5dfSAlexander V. Chernikov 
3664c91a5dfSAlexander V. Chernikov 	if (ci == NULL)
3674c91a5dfSAlexander V. Chernikov 		return;
3684c91a5dfSAlexander V. Chernikov 
3694c91a5dfSAlexander V. Chernikov 	int count = ci->ifa_prefered != ND6_INFINITE_LIFETIME;
3704c91a5dfSAlexander V. Chernikov 	count += ci->ifa_valid != ND6_INFINITE_LIFETIME;
3714c91a5dfSAlexander V. Chernikov 	if (count == 0)
3724c91a5dfSAlexander V. Chernikov 		return;
3734c91a5dfSAlexander V. Chernikov 
3744c91a5dfSAlexander V. Chernikov 	pl = (ci->ifa_prefered == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_prefered;
3754c91a5dfSAlexander V. Chernikov 	vl = (ci->ifa_valid == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_valid;
3764c91a5dfSAlexander V. Chernikov 
3774c91a5dfSAlexander V. Chernikov 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
3784c91a5dfSAlexander V. Chernikov 	print_lifetime("pltime", pl + now.tv_sec, &now);
3794c91a5dfSAlexander V. Chernikov 	print_lifetime("vltime", vl + now.tv_sec, &now);
3804c91a5dfSAlexander V. Chernikov }
3814c91a5dfSAlexander V. Chernikov 
3824c91a5dfSAlexander V. Chernikov static void
in6_status_nl(if_ctx * ctx __unused,if_link_t * link __unused,if_addr_t * ifa)3830c2beef7SAlexander V. Chernikov in6_status_nl(if_ctx *ctx __unused, if_link_t *link __unused, if_addr_t *ifa)
3844c91a5dfSAlexander V. Chernikov {
3854c91a5dfSAlexander V. Chernikov 	int plen = ifa->ifa_prefixlen;
3864c91a5dfSAlexander V. Chernikov 	uint32_t scopeid;
3874c91a5dfSAlexander V. Chernikov 
3884c91a5dfSAlexander V. Chernikov 	if (ifa->ifa_local == NULL) {
3894c91a5dfSAlexander V. Chernikov 		/* Non-P2P address */
3904c91a5dfSAlexander V. Chernikov 		scopeid = satosin6(ifa->ifa_address)->sin6_scope_id;
3914c91a5dfSAlexander V. Chernikov 		print_addr(satosin6(ifa->ifa_address));
3924c91a5dfSAlexander V. Chernikov 	} else {
3934c91a5dfSAlexander V. Chernikov 		scopeid = satosin6(ifa->ifa_local)->sin6_scope_id;
3944c91a5dfSAlexander V. Chernikov 		print_addr(satosin6(ifa->ifa_local));
3954c91a5dfSAlexander V. Chernikov 		print_p2p(satosin6(ifa->ifa_address));
3964c91a5dfSAlexander V. Chernikov 	}
3974c91a5dfSAlexander V. Chernikov 
3984c91a5dfSAlexander V. Chernikov 	print_mask(plen);
3994c91a5dfSAlexander V. Chernikov 	print_flags(ifa->ifaf_flags);
4004c91a5dfSAlexander V. Chernikov 
4014c91a5dfSAlexander V. Chernikov 	if (scopeid != 0)
4024c91a5dfSAlexander V. Chernikov 		printf(" scopeid 0x%x", scopeid);
4034c91a5dfSAlexander V. Chernikov 
4044c91a5dfSAlexander V. Chernikov 	show_lifetime(ifa->ifa_cacheinfo);
4054c91a5dfSAlexander V. Chernikov 
4064c91a5dfSAlexander V. Chernikov 	if (ifa->ifaf_vhid != 0)
4074c91a5dfSAlexander V. Chernikov 		printf(" vhid %d", ifa->ifaf_vhid);
4084c91a5dfSAlexander V. Chernikov 
4094c91a5dfSAlexander V. Chernikov 	putchar('\n');
4104c91a5dfSAlexander V. Chernikov }
4114bf44dd7SAlexander V. Chernikov 
4124bf44dd7SAlexander V. Chernikov static struct in6_px *sin6tab_nl[] = {
4134bf44dd7SAlexander V. Chernikov         &in6_del.addr,          /* RIDADDR */
4144bf44dd7SAlexander V. Chernikov         &in6_add.addr,          /* ADDR */
4154bf44dd7SAlexander V. Chernikov         NULL,                   /* MASK */
4164bf44dd7SAlexander V. Chernikov         &in6_add.dst_addr,      /* DSTADDR*/
4174bf44dd7SAlexander V. Chernikov };
4184bf44dd7SAlexander V. Chernikov 
4194bf44dd7SAlexander V. Chernikov static void
in6_copyaddr(if_ctx * ctx __unused,int to,int from)4200c2beef7SAlexander V. Chernikov in6_copyaddr(if_ctx *ctx __unused, int to, int from)
4216d204407SAlexander V. Chernikov {
4226d204407SAlexander V. Chernikov 	sin6tab_nl[to]->addr = sin6tab_nl[from]->addr;
4236d204407SAlexander V. Chernikov 	sin6tab_nl[to]->set = sin6tab_nl[from]->set;
4246d204407SAlexander V. Chernikov }
4256d204407SAlexander V. Chernikov 
4266d204407SAlexander V. Chernikov static void
in6_getaddr(const char * addr_str,int which)4274bf44dd7SAlexander V. Chernikov in6_getaddr(const char *addr_str, int which)
4284bf44dd7SAlexander V. Chernikov {
4294bf44dd7SAlexander V. Chernikov         struct in6_px *px = sin6tab_nl[which];
4304bf44dd7SAlexander V. Chernikov 
4314bf44dd7SAlexander V. Chernikov         px->set = true;
4324bf44dd7SAlexander V. Chernikov         px->plen = 128;
4334bf44dd7SAlexander V. Chernikov         if (which == ADDR) {
4344bf44dd7SAlexander V. Chernikov                 char *p = NULL;
4354bf44dd7SAlexander V. Chernikov                 if((p = strrchr(addr_str, '/')) != NULL) {
4364bf44dd7SAlexander V. Chernikov                         *p = '\0';
4374bf44dd7SAlexander V. Chernikov                         int plen = strtol(p + 1, NULL, 10);
4384bf44dd7SAlexander V. Chernikov 			if (plen < 0 || plen > 128)
4394bf44dd7SAlexander V. Chernikov                                 errx(1, "%s: bad value", p + 1);
4404bf44dd7SAlexander V. Chernikov                         px->plen = plen;
4414bf44dd7SAlexander V. Chernikov                         explicit_prefix = 1;
4424bf44dd7SAlexander V. Chernikov                 }
4434bf44dd7SAlexander V. Chernikov         }
4444bf44dd7SAlexander V. Chernikov 
4454bf44dd7SAlexander V. Chernikov         struct addrinfo hints = { .ai_family = AF_INET6 };
4464bf44dd7SAlexander V. Chernikov         struct addrinfo *res;
4474bf44dd7SAlexander V. Chernikov 
4484bf44dd7SAlexander V. Chernikov         int error = getaddrinfo(addr_str, NULL, &hints, &res);
4494bf44dd7SAlexander V. Chernikov         if (error != 0) {
4504bf44dd7SAlexander V. Chernikov                 if (inet_pton(AF_INET6, addr_str, &px->addr) != 1)
4514bf44dd7SAlexander V. Chernikov                         errx(1, "%s: bad value", addr_str);
4524bf44dd7SAlexander V. Chernikov         } else {
4534bf44dd7SAlexander V. Chernikov                 struct sockaddr_in6 *sin6;
4544bf44dd7SAlexander V. Chernikov 
4554bf44dd7SAlexander V. Chernikov                 sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
4564bf44dd7SAlexander V. Chernikov                 px->addr = sin6->sin6_addr;
4574bf44dd7SAlexander V. Chernikov                 freeaddrinfo(res);
4584bf44dd7SAlexander V. Chernikov         }
4594bf44dd7SAlexander V. Chernikov }
4604bf44dd7SAlexander V. Chernikov 
4614bf44dd7SAlexander V. Chernikov static int
in6_exec_nl(if_ctx * ctx,unsigned long action,void * data)4624bf44dd7SAlexander V. Chernikov in6_exec_nl(if_ctx *ctx, unsigned long action, void *data)
4634bf44dd7SAlexander V. Chernikov {
4644bf44dd7SAlexander V. Chernikov 	struct in6_pdata *pdata = (struct in6_pdata *)data;
4654bf44dd7SAlexander V. Chernikov 	struct snl_writer nw = {};
4664bf44dd7SAlexander V. Chernikov 
4674bf44dd7SAlexander V. Chernikov 	snl_init_writer(ctx->io_ss, &nw);
4684bf44dd7SAlexander V. Chernikov 	struct nlmsghdr *hdr = snl_create_msg_request(&nw, action);
4694bf44dd7SAlexander V. Chernikov 	struct ifaddrmsg *ifahdr = snl_reserve_msg_object(&nw, struct ifaddrmsg);
4704bf44dd7SAlexander V. Chernikov 
4714bf44dd7SAlexander V. Chernikov 	ifahdr->ifa_family = AF_INET6;
4724bf44dd7SAlexander V. Chernikov 	ifahdr->ifa_prefixlen = pdata->addr.plen;
47385e0016aSAlexander V. Chernikov 	ifahdr->ifa_index = if_nametoindex_nl(ctx->io_ss, ctx->ifname);
4744bf44dd7SAlexander V. Chernikov 
4754bf44dd7SAlexander V. Chernikov 	snl_add_msg_attr_ip6(&nw, IFA_LOCAL, &pdata->addr.addr);
4764bf44dd7SAlexander V. Chernikov 	if (action == NL_RTM_NEWADDR && pdata->dst_addr.set)
4774bf44dd7SAlexander V. Chernikov 		snl_add_msg_attr_ip6(&nw, IFA_ADDRESS, &pdata->dst_addr.addr);
4784bf44dd7SAlexander V. Chernikov 
4794bf44dd7SAlexander V. Chernikov 	struct ifa_cacheinfo ci = {
4804bf44dd7SAlexander V. Chernikov 		.ifa_prefered = pdata->lifetime.ia6t_pltime,
4814bf44dd7SAlexander V. Chernikov 		.ifa_valid =  pdata->lifetime.ia6t_vltime,
4824bf44dd7SAlexander V. Chernikov 	};
4834bf44dd7SAlexander V. Chernikov 	snl_add_msg_attr(&nw, IFA_CACHEINFO, sizeof(ci), &ci);
4844bf44dd7SAlexander V. Chernikov 
4854bf44dd7SAlexander V. Chernikov 	int off = snl_add_msg_attr_nested(&nw, IFA_FREEBSD);
4864bf44dd7SAlexander V. Chernikov 	snl_add_msg_attr_u32(&nw, IFAF_FLAGS, pdata->flags);
4874bf44dd7SAlexander V. Chernikov 	if (pdata->vhid != 0)
4884bf44dd7SAlexander V. Chernikov 		snl_add_msg_attr_u32(&nw, IFAF_VHID, pdata->vhid);
4894bf44dd7SAlexander V. Chernikov 	snl_end_attr_nested(&nw, off);
4904bf44dd7SAlexander V. Chernikov 
491*4f8f43b0SKristof Provost 	if (! (hdr = snl_finalize_msg(&nw)) || !snl_send_message(ctx->io_ss, hdr))
4924bf44dd7SAlexander V. Chernikov 		return (0);
4934bf44dd7SAlexander V. Chernikov 
4944bf44dd7SAlexander V. Chernikov 	struct snl_errmsg_data e = {};
4954bf44dd7SAlexander V. Chernikov 	snl_read_reply_code(ctx->io_ss, hdr->nlmsg_seq, &e);
4964bf44dd7SAlexander V. Chernikov 
4974bf44dd7SAlexander V. Chernikov 	return (e.error);
4984bf44dd7SAlexander V. Chernikov }
4994c91a5dfSAlexander V. Chernikov #endif
5004c91a5dfSAlexander V. Chernikov 
5014bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
5025faf8dcbSSam Leffler static struct	sockaddr_in6 *sin6tab[] = {
50344cd85d4SAlexander V. Chernikov 	&in6_ridreq.ifr_addr, &in6_addreq.ifra_addr,
50444cd85d4SAlexander V. Chernikov 	&in6_addreq.ifra_prefixmask, &in6_addreq.ifra_dstaddr
5055faf8dcbSSam Leffler };
5065faf8dcbSSam Leffler 
5075faf8dcbSSam Leffler static void
in6_copyaddr(if_ctx * ctx __unused,int to,int from)508a3930cd4SAlexander V. Chernikov in6_copyaddr(if_ctx *ctx __unused, int to, int from)
5096d204407SAlexander V. Chernikov {
5106d204407SAlexander V. Chernikov 	memcpy(sin6tab[to], sin6tab[from], sizeof(struct sockaddr_in6));
5116d204407SAlexander V. Chernikov }
5126d204407SAlexander V. Chernikov 
5136d204407SAlexander V. Chernikov static void
in6_getprefix(const char * plen,int which)5145faf8dcbSSam Leffler in6_getprefix(const char *plen, int which)
5155faf8dcbSSam Leffler {
5165faf8dcbSSam Leffler 	struct sockaddr_in6 *sin = sin6tab[which];
5175faf8dcbSSam Leffler 	u_char *cp;
5185faf8dcbSSam Leffler 	int len = atoi(plen);
5195faf8dcbSSam Leffler 
5205faf8dcbSSam Leffler 	if ((len < 0) || (len > 128))
5215faf8dcbSSam Leffler 		errx(1, "%s: bad value", plen);
5225faf8dcbSSam Leffler 	sin->sin6_len = sizeof(*sin);
5235faf8dcbSSam Leffler 	if (which != MASK)
5245faf8dcbSSam Leffler 		sin->sin6_family = AF_INET6;
5255faf8dcbSSam Leffler 	if ((len == 0) || (len == 128)) {
5265faf8dcbSSam Leffler 		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
5275faf8dcbSSam Leffler 		return;
5285faf8dcbSSam Leffler 	}
5295faf8dcbSSam Leffler 	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
5305faf8dcbSSam Leffler 	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
5315faf8dcbSSam Leffler 		*cp++ = 0xff;
5325faf8dcbSSam Leffler 	*cp = 0xff << (8 - len);
5335faf8dcbSSam Leffler }
5345faf8dcbSSam Leffler 
5355faf8dcbSSam Leffler static void
in6_getaddr(const char * s,int which)5365faf8dcbSSam Leffler in6_getaddr(const char *s, int which)
5375faf8dcbSSam Leffler {
5385faf8dcbSSam Leffler 	struct sockaddr_in6 *sin = sin6tab[which];
5395faf8dcbSSam Leffler 	struct addrinfo hints, *res;
5405faf8dcbSSam Leffler 	int error = -1;
5415faf8dcbSSam Leffler 
5425faf8dcbSSam Leffler 	sin->sin6_len = sizeof(*sin);
5435faf8dcbSSam Leffler 	if (which != MASK)
5445faf8dcbSSam Leffler 		sin->sin6_family = AF_INET6;
5455faf8dcbSSam Leffler 
5465faf8dcbSSam Leffler 	if (which == ADDR) {
5475faf8dcbSSam Leffler 		char *p = NULL;
5485faf8dcbSSam Leffler 		if((p = strrchr(s, '/')) != NULL) {
5495faf8dcbSSam Leffler 			*p = '\0';
5505faf8dcbSSam Leffler 			in6_getprefix(p + 1, MASK);
5515faf8dcbSSam Leffler 			explicit_prefix = 1;
5525faf8dcbSSam Leffler 		}
5535faf8dcbSSam Leffler 	}
5545faf8dcbSSam Leffler 
5555faf8dcbSSam Leffler 	if (sin->sin6_family == AF_INET6) {
5565faf8dcbSSam Leffler 		bzero(&hints, sizeof(struct addrinfo));
5575faf8dcbSSam Leffler 		hints.ai_family = AF_INET6;
5585faf8dcbSSam Leffler 		error = getaddrinfo(s, NULL, &hints, &res);
5595faf8dcbSSam Leffler 		if (error != 0) {
5605faf8dcbSSam Leffler 			if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
5615faf8dcbSSam Leffler 				errx(1, "%s: bad value", s);
5626223cc33SAlan Somers 		} else {
5635faf8dcbSSam Leffler 			bcopy(res->ai_addr, sin, res->ai_addrlen);
5646223cc33SAlan Somers 			freeaddrinfo(res);
5656223cc33SAlan Somers 		}
5666223cc33SAlan Somers 	}
5675faf8dcbSSam Leffler }
5685faf8dcbSSam Leffler 
5695faf8dcbSSam Leffler static int
prefix(void * val,int size)5705faf8dcbSSam Leffler prefix(void *val, int size)
5715faf8dcbSSam Leffler {
5725faf8dcbSSam Leffler 	u_char *name = (u_char *)val;
5735faf8dcbSSam Leffler 	int byte, bit, plen = 0;
5745faf8dcbSSam Leffler 
5755faf8dcbSSam Leffler 	for (byte = 0; byte < size; byte++, plen += 8)
5765faf8dcbSSam Leffler 		if (name[byte] != 0xff)
5775faf8dcbSSam Leffler 			break;
5785faf8dcbSSam Leffler 	if (byte == size)
5795faf8dcbSSam Leffler 		return (plen);
5805faf8dcbSSam Leffler 	for (bit = 7; bit != 0; bit--, plen++)
5815faf8dcbSSam Leffler 		if (!(name[byte] & (1 << bit)))
5825faf8dcbSSam Leffler 			break;
5835faf8dcbSSam Leffler 	for (; bit != 0; bit--)
5845faf8dcbSSam Leffler 		if (name[byte] & (1 << bit))
5855faf8dcbSSam Leffler 			return(0);
5865faf8dcbSSam Leffler 	byte++;
5875faf8dcbSSam Leffler 	for (; byte < size; byte++)
5885faf8dcbSSam Leffler 		if (name[byte])
5895faf8dcbSSam Leffler 			return(0);
5905faf8dcbSSam Leffler 	return (plen);
5915faf8dcbSSam Leffler }
59244cd85d4SAlexander V. Chernikov #endif
5935faf8dcbSSam Leffler 
5945faf8dcbSSam Leffler static char *
sec2str(time_t total)5955faf8dcbSSam Leffler sec2str(time_t total)
5965faf8dcbSSam Leffler {
5975faf8dcbSSam Leffler 	static char result[256];
5985faf8dcbSSam Leffler 	int days, hours, mins, secs;
5995faf8dcbSSam Leffler 	int first = 1;
6005faf8dcbSSam Leffler 	char *p = result;
6015faf8dcbSSam Leffler 
6025faf8dcbSSam Leffler 	if (0) {
6035faf8dcbSSam Leffler 		days = total / 3600 / 24;
6045faf8dcbSSam Leffler 		hours = (total / 3600) % 24;
6055faf8dcbSSam Leffler 		mins = (total / 60) % 60;
6065faf8dcbSSam Leffler 		secs = total % 60;
6075faf8dcbSSam Leffler 
6085faf8dcbSSam Leffler 		if (days) {
6095faf8dcbSSam Leffler 			first = 0;
6105faf8dcbSSam Leffler 			p += sprintf(p, "%dd", days);
6115faf8dcbSSam Leffler 		}
6125faf8dcbSSam Leffler 		if (!first || hours) {
6135faf8dcbSSam Leffler 			first = 0;
6145faf8dcbSSam Leffler 			p += sprintf(p, "%dh", hours);
6155faf8dcbSSam Leffler 		}
6165faf8dcbSSam Leffler 		if (!first || mins) {
6175faf8dcbSSam Leffler 			first = 0;
6185faf8dcbSSam Leffler 			p += sprintf(p, "%dm", mins);
6195faf8dcbSSam Leffler 		}
6205faf8dcbSSam Leffler 		sprintf(p, "%ds", secs);
6215faf8dcbSSam Leffler 	} else
6225faf8dcbSSam Leffler 		sprintf(result, "%lu", (unsigned long)total);
6235faf8dcbSSam Leffler 
6245faf8dcbSSam Leffler 	return(result);
6255faf8dcbSSam Leffler }
6265faf8dcbSSam Leffler 
6275faf8dcbSSam Leffler static void
in6_postproc(if_ctx * ctx,int newaddr __unused,int ifflags __unused)6286e3a9d7fSAlexander V. Chernikov in6_postproc(if_ctx *ctx, int newaddr __unused,
629d8237b95SMike Karels     int ifflags __unused)
6305faf8dcbSSam Leffler {
6315faf8dcbSSam Leffler 	if (explicit_prefix == 0) {
6325faf8dcbSSam Leffler 		/* Aggregatable address architecture defines all prefixes
6335faf8dcbSSam Leffler 		   are 64. So, it is convenient to set prefixlen to 64 if
6345faf8dcbSSam Leffler 		   it is not specified. */
6356e3a9d7fSAlexander V. Chernikov 		setifprefixlen(ctx, "64", 0);
6365faf8dcbSSam Leffler 		/* in6_getprefix("64", MASK) if MASK is available here... */
6375faf8dcbSSam Leffler 	}
6385faf8dcbSSam Leffler }
6395faf8dcbSSam Leffler 
6405faf8dcbSSam Leffler static void
in6_status_tunnel(if_ctx * ctx)641c6f0602fSAlexander V. Chernikov in6_status_tunnel(if_ctx *ctx)
6425faf8dcbSSam Leffler {
6435faf8dcbSSam Leffler 	char src[NI_MAXHOST];
6445faf8dcbSSam Leffler 	char dst[NI_MAXHOST];
6455faf8dcbSSam Leffler 	struct in6_ifreq in6_ifr;
6465faf8dcbSSam Leffler 	const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
6475faf8dcbSSam Leffler 
6485faf8dcbSSam Leffler 	memset(&in6_ifr, 0, sizeof(in6_ifr));
64985e0016aSAlexander V. Chernikov 	strlcpy(in6_ifr.ifr_name, ctx->ifname, sizeof(in6_ifr.ifr_name));
6505faf8dcbSSam Leffler 
651c6f0602fSAlexander V. Chernikov 	if (ioctl_ctx(ctx, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
6525faf8dcbSSam Leffler 		return;
65373bbc449SHajimu UMEMOTO 	if (sa->sa_family != AF_INET6)
65473bbc449SHajimu UMEMOTO 		return;
6554f101318SHajimu UMEMOTO 	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
6564f101318SHajimu UMEMOTO 	    NI_NUMERICHOST) != 0)
6575faf8dcbSSam Leffler 		src[0] = '\0';
6585faf8dcbSSam Leffler 
659c6f0602fSAlexander V. Chernikov 	if (ioctl_ctx(ctx, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
6605faf8dcbSSam Leffler 		return;
66173bbc449SHajimu UMEMOTO 	if (sa->sa_family != AF_INET6)
66273bbc449SHajimu UMEMOTO 		return;
6634f101318SHajimu UMEMOTO 	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
6644f101318SHajimu UMEMOTO 	    NI_NUMERICHOST) != 0)
6655faf8dcbSSam Leffler 		dst[0] = '\0';
6665faf8dcbSSam Leffler 
6675faf8dcbSSam Leffler 	printf("\ttunnel inet6 %s --> %s\n", src, dst);
6685faf8dcbSSam Leffler }
6695faf8dcbSSam Leffler 
6705faf8dcbSSam Leffler static void
in6_set_tunnel(if_ctx * ctx,struct addrinfo * srcres,struct addrinfo * dstres)671c6f0602fSAlexander V. Chernikov in6_set_tunnel(if_ctx *ctx, struct addrinfo *srcres, struct addrinfo *dstres)
6725faf8dcbSSam Leffler {
673a3930cd4SAlexander V. Chernikov 	struct in6_aliasreq in6_req = {};
6745faf8dcbSSam Leffler 
675a3930cd4SAlexander V. Chernikov 	strlcpy(in6_req.ifra_name, ctx->ifname, sizeof(in6_req.ifra_name));
676a3930cd4SAlexander V. Chernikov 	memcpy(&in6_req.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
677a3930cd4SAlexander V. Chernikov 	memcpy(&in6_req.ifra_dstaddr, dstres->ai_addr,
6785faf8dcbSSam Leffler 	    dstres->ai_addr->sa_len);
6795faf8dcbSSam Leffler 
680a3930cd4SAlexander V. Chernikov 	if (ioctl_ctx(ctx, SIOCSIFPHYADDR_IN6, &in6_req) < 0)
6815faf8dcbSSam Leffler 		warn("SIOCSIFPHYADDR_IN6");
6825faf8dcbSSam Leffler }
6835faf8dcbSSam Leffler 
684a72b7890SAlexander V. Chernikov static void
in6_set_vhid(int vhid)685a72b7890SAlexander V. Chernikov in6_set_vhid(int vhid)
686a72b7890SAlexander V. Chernikov {
6874bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
688a72b7890SAlexander V. Chernikov 	in6_addreq.ifra_vhid = vhid;
6894bf44dd7SAlexander V. Chernikov #else
6904bf44dd7SAlexander V. Chernikov 	in6_add.vhid = (uint32_t)vhid;
6914bf44dd7SAlexander V. Chernikov #endif
692a72b7890SAlexander V. Chernikov }
693a72b7890SAlexander V. Chernikov 
6945faf8dcbSSam Leffler static struct cmd inet6_cmds[] = {
6955faf8dcbSSam Leffler 	DEF_CMD_ARG("prefixlen",			setifprefixlen),
6965faf8dcbSSam Leffler 	DEF_CMD("anycast",	IN6_IFF_ANYCAST,	setip6flags),
6975faf8dcbSSam Leffler 	DEF_CMD("tentative",	IN6_IFF_TENTATIVE,	setip6flags),
6985faf8dcbSSam Leffler 	DEF_CMD("-tentative",	-IN6_IFF_TENTATIVE,	setip6flags),
6995faf8dcbSSam Leffler 	DEF_CMD("deprecated",	IN6_IFF_DEPRECATED,	setip6flags),
7005faf8dcbSSam Leffler 	DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED,	setip6flags),
7015faf8dcbSSam Leffler 	DEF_CMD("autoconf",	IN6_IFF_AUTOCONF,	setip6flags),
7025faf8dcbSSam Leffler 	DEF_CMD("-autoconf",	-IN6_IFF_AUTOCONF,	setip6flags),
7035dbfa43fSAndrey V. Elsukov 	DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE,	setip6flags),
7045dbfa43fSAndrey V. Elsukov 	DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
705a283298cSHiroki Sato 	DEF_CMD("accept_rtadv",	ND6_IFF_ACCEPT_RTADV,	setnd6flags),
706a283298cSHiroki Sato 	DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV,	setnd6flags),
707e7fa8d0aSHiroki Sato 	DEF_CMD("no_radr",	ND6_IFF_NO_RADR,	setnd6flags),
708e7fa8d0aSHiroki Sato 	DEF_CMD("-no_radr",	-ND6_IFF_NO_RADR,	setnd6flags),
709a283298cSHiroki Sato 	DEF_CMD("defaultif",	1,			setnd6defif),
710a283298cSHiroki Sato 	DEF_CMD("-defaultif",	-1,			setnd6defif),
711a283298cSHiroki Sato 	DEF_CMD("ifdisabled",	ND6_IFF_IFDISABLED,	setnd6flags),
712a283298cSHiroki Sato 	DEF_CMD("-ifdisabled",	-ND6_IFF_IFDISABLED,	setnd6flags),
713a283298cSHiroki Sato 	DEF_CMD("nud",		ND6_IFF_PERFORMNUD,	setnd6flags),
714a283298cSHiroki Sato 	DEF_CMD("-nud",		-ND6_IFF_PERFORMNUD,	setnd6flags),
715a283298cSHiroki Sato 	DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
716a283298cSHiroki Sato 	DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
717164051ceSHajimu UMEMOTO 	DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
718164051ceSHajimu UMEMOTO 	DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
71923e9ffb0SHiroki Sato 	DEF_CMD("no_dad",	ND6_IFF_NO_DAD,		setnd6flags),
72023e9ffb0SHiroki Sato 	DEF_CMD("-no_dad",	-ND6_IFF_NO_DAD,	setnd6flags),
7215faf8dcbSSam Leffler 	DEF_CMD_ARG("pltime",        			setip6pltime),
7225faf8dcbSSam Leffler 	DEF_CMD_ARG("vltime",        			setip6vltime),
7235faf8dcbSSam Leffler 	DEF_CMD("eui64",	0,			setip6eui64),
72421231a7aSBjoern A. Zeeb #ifdef EXPERIMENTAL
72521231a7aSBjoern A. Zeeb 	DEF_CMD("ipv6_only",	ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
72621231a7aSBjoern A. Zeeb 	DEF_CMD("-ipv6_only",	-ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
72721231a7aSBjoern A. Zeeb #endif
7285faf8dcbSSam Leffler };
7295faf8dcbSSam Leffler 
7305faf8dcbSSam Leffler static struct afswtch af_inet6 = {
7315faf8dcbSSam Leffler 	.af_name	= "inet6",
7325faf8dcbSSam Leffler 	.af_af		= AF_INET6,
7334c91a5dfSAlexander V. Chernikov #ifdef WITHOUT_NETLINK
7345faf8dcbSSam Leffler 	.af_status	= in6_status,
7354c91a5dfSAlexander V. Chernikov #else
7366e3a9d7fSAlexander V. Chernikov 	.af_status	= in6_status_nl,
7374c91a5dfSAlexander V. Chernikov #endif
7385faf8dcbSSam Leffler 	.af_getaddr	= in6_getaddr,
7396d204407SAlexander V. Chernikov 	.af_copyaddr	= in6_copyaddr,
7404bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
7415faf8dcbSSam Leffler 	.af_getprefix	= in6_getprefix,
7424bf44dd7SAlexander V. Chernikov #endif
743c3cc3217SHiroki Sato 	.af_other_status = nd6_status,
7445faf8dcbSSam Leffler 	.af_postproc	= in6_postproc,
7455faf8dcbSSam Leffler 	.af_status_tunnel = in6_status_tunnel,
7465faf8dcbSSam Leffler 	.af_settunnel	= in6_set_tunnel,
747a72b7890SAlexander V. Chernikov 	.af_setvhid	= in6_set_vhid,
7484bf44dd7SAlexander V. Chernikov #ifdef WITHOUT_NETLINK
7495faf8dcbSSam Leffler 	.af_difaddr	= SIOCDIFADDR_IN6,
7505faf8dcbSSam Leffler 	.af_aifaddr	= SIOCAIFADDR_IN6,
7511b153c92SBruce M Simpson 	.af_ridreq	= &in6_addreq,
7525faf8dcbSSam Leffler 	.af_addreq	= &in6_addreq,
7534bf44dd7SAlexander V. Chernikov 	.af_exec	= af_exec_ioctl,
7544bf44dd7SAlexander V. Chernikov #else
7554bf44dd7SAlexander V. Chernikov 	.af_difaddr	= NL_RTM_DELADDR,
7564bf44dd7SAlexander V. Chernikov 	.af_aifaddr	= NL_RTM_NEWADDR,
7574bf44dd7SAlexander V. Chernikov 	.af_ridreq	= &in6_add,
7584bf44dd7SAlexander V. Chernikov 	.af_addreq	= &in6_add,
7594bf44dd7SAlexander V. Chernikov 	.af_exec	= in6_exec_nl,
7604bf44dd7SAlexander V. Chernikov #endif
7615faf8dcbSSam Leffler };
7625faf8dcbSSam Leffler 
7635faf8dcbSSam Leffler static void
in6_Lopt_cb(const char * arg __unused)7640c2beef7SAlexander V. Chernikov in6_Lopt_cb(const char *arg __unused)
7655faf8dcbSSam Leffler {
7665faf8dcbSSam Leffler 	ip6lifetime++;	/* print IPv6 address lifetime */
7675faf8dcbSSam Leffler }
76807a51db9SVsevolod Stakhov static struct option in6_Lopt = {
76907a51db9SVsevolod Stakhov 	.opt = "L",
77007a51db9SVsevolod Stakhov 	.opt_usage = "[-L]",
77107a51db9SVsevolod Stakhov 	.cb = in6_Lopt_cb
77207a51db9SVsevolod Stakhov };
7735faf8dcbSSam Leffler 
7745faf8dcbSSam Leffler static __constructor void
inet6_ctor(void)7755faf8dcbSSam Leffler inet6_ctor(void)
7765faf8dcbSSam Leffler {
777b59dcaeeSXin LI 	size_t i;
7785faf8dcbSSam Leffler 
779ad2571a2SBjoern A. Zeeb #ifndef RESCUE
7805af3fa9aSBjoern A. Zeeb 	if (!feature_present("inet6"))
7815af3fa9aSBjoern A. Zeeb 		return;
782ad2571a2SBjoern A. Zeeb #endif
7835af3fa9aSBjoern A. Zeeb 
784abd71050SEnji Cooper 	for (i = 0; i < nitems(inet6_cmds);  i++)
7855faf8dcbSSam Leffler 		cmd_register(&inet6_cmds[i]);
7865faf8dcbSSam Leffler 	af_register(&af_inet6);
7875faf8dcbSSam Leffler 	opt_register(&in6_Lopt);
7885faf8dcbSSam Leffler }
789