xref: /freebsd/sbin/ifconfig/af_inet6.c (revision 5faf8dcb55e773b33af6993e567709c38ec7702b)
15faf8dcbSSam Leffler /*
25faf8dcbSSam Leffler  * Copyright (c) 1983, 1993
35faf8dcbSSam Leffler  *	The Regents of the University of California.  All rights reserved.
45faf8dcbSSam Leffler  *
55faf8dcbSSam Leffler  * Redistribution and use in source and binary forms, with or without
65faf8dcbSSam Leffler  * modification, are permitted provided that the following conditions
75faf8dcbSSam Leffler  * are met:
85faf8dcbSSam Leffler  * 1. Redistributions of source code must retain the above copyright
95faf8dcbSSam Leffler  *    notice, this list of conditions and the following disclaimer.
105faf8dcbSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
115faf8dcbSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
125faf8dcbSSam Leffler  *    documentation and/or other materials provided with the distribution.
135faf8dcbSSam Leffler  * 4. Neither the name of the University nor the names of its contributors
145faf8dcbSSam Leffler  *    may be used to endorse or promote products derived from this software
155faf8dcbSSam Leffler  *    without specific prior written permission.
165faf8dcbSSam Leffler  *
175faf8dcbSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
185faf8dcbSSam Leffler  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
195faf8dcbSSam Leffler  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
205faf8dcbSSam Leffler  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
215faf8dcbSSam Leffler  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
225faf8dcbSSam Leffler  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
235faf8dcbSSam Leffler  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
245faf8dcbSSam Leffler  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
255faf8dcbSSam Leffler  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
265faf8dcbSSam Leffler  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
275faf8dcbSSam Leffler  * SUCH DAMAGE.
285faf8dcbSSam Leffler  */
295faf8dcbSSam Leffler 
305faf8dcbSSam Leffler #ifndef lint
315faf8dcbSSam Leffler static const char rcsid[] =
325faf8dcbSSam Leffler   "$FreeBSD$";
335faf8dcbSSam Leffler #endif /* not lint */
345faf8dcbSSam Leffler 
355faf8dcbSSam Leffler #include <sys/param.h>
365faf8dcbSSam Leffler #include <sys/ioctl.h>
375faf8dcbSSam Leffler #include <sys/socket.h>
385faf8dcbSSam Leffler #include <net/if.h>
395faf8dcbSSam Leffler #include <net/route.h>		/* for RTX_IFA */
405faf8dcbSSam Leffler 
415faf8dcbSSam Leffler #include <err.h>
425faf8dcbSSam Leffler #include <stdio.h>
435faf8dcbSSam Leffler #include <stdlib.h>
445faf8dcbSSam Leffler #include <string.h>
455faf8dcbSSam Leffler #include <unistd.h>
465faf8dcbSSam Leffler #include <ifaddrs.h>
475faf8dcbSSam Leffler 
485faf8dcbSSam Leffler #include <arpa/inet.h>
495faf8dcbSSam Leffler 
505faf8dcbSSam Leffler #include <netinet/in.h>
515faf8dcbSSam Leffler #include <net/if_var.h>		/* for struct ifaddr */
525faf8dcbSSam Leffler #include <netinet/in_var.h>
535faf8dcbSSam Leffler #include <arpa/inet.h>
545faf8dcbSSam Leffler #include <netdb.h>
555faf8dcbSSam Leffler 
565faf8dcbSSam Leffler #include <netinet6/nd6.h>	/* Define ND6_INFINITE_LIFETIME */
575faf8dcbSSam Leffler 
585faf8dcbSSam Leffler #include "ifconfig.h"
595faf8dcbSSam Leffler 
605faf8dcbSSam Leffler /* wrapper for KAME-special getnameinfo() */
615faf8dcbSSam Leffler #ifndef NI_WITHSCOPEID
625faf8dcbSSam Leffler #define	NI_WITHSCOPEID	0
635faf8dcbSSam Leffler #endif
645faf8dcbSSam Leffler 
655faf8dcbSSam Leffler static	struct in6_ifreq in6_ridreq;
665faf8dcbSSam Leffler static	struct in6_aliasreq in6_addreq =
675faf8dcbSSam Leffler   { { 0 },
685faf8dcbSSam Leffler     { 0 },
695faf8dcbSSam Leffler     { 0 },
705faf8dcbSSam Leffler     { 0 },
715faf8dcbSSam Leffler     0,
725faf8dcbSSam Leffler     { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
735faf8dcbSSam Leffler static	int ip6lifetime;
745faf8dcbSSam Leffler 
755faf8dcbSSam Leffler static	void in6_fillscopeid(struct sockaddr_in6 *sin6);
765faf8dcbSSam Leffler static	int prefix(void *, int);
775faf8dcbSSam Leffler static	char *sec2str(time_t);
785faf8dcbSSam Leffler static	int explicit_prefix = 0;
795faf8dcbSSam Leffler 
805faf8dcbSSam Leffler static	char addr_buf[MAXHOSTNAMELEN *2 + 1];	/*for getnameinfo()*/
815faf8dcbSSam Leffler 
825faf8dcbSSam Leffler static void
835faf8dcbSSam Leffler setifprefixlen(const char *addr, int dummy __unused, int s,
845faf8dcbSSam Leffler     const struct afswtch *afp)
855faf8dcbSSam Leffler {
865faf8dcbSSam Leffler         if (afp->af_getprefix != NULL)
875faf8dcbSSam Leffler                 afp->af_getprefix(addr, MASK);
885faf8dcbSSam Leffler 	explicit_prefix = 1;
895faf8dcbSSam Leffler }
905faf8dcbSSam Leffler 
915faf8dcbSSam Leffler static void
925faf8dcbSSam Leffler setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
935faf8dcbSSam Leffler     const struct afswtch *afp)
945faf8dcbSSam Leffler {
955faf8dcbSSam Leffler 	if (afp->af_af != AF_INET6)
965faf8dcbSSam Leffler 		err(1, "address flags can be set only for inet6 addresses");
975faf8dcbSSam Leffler 
985faf8dcbSSam Leffler 	if (flag < 0)
995faf8dcbSSam Leffler 		in6_addreq.ifra_flags &= ~(-flag);
1005faf8dcbSSam Leffler 	else
1015faf8dcbSSam Leffler 		in6_addreq.ifra_flags |= flag;
1025faf8dcbSSam Leffler }
1035faf8dcbSSam Leffler 
1045faf8dcbSSam Leffler static void
1055faf8dcbSSam Leffler setip6lifetime(const char *cmd, const char *val, int s,
1065faf8dcbSSam Leffler     const struct afswtch *afp)
1075faf8dcbSSam Leffler {
1085faf8dcbSSam Leffler 	time_t newval, t;
1095faf8dcbSSam Leffler 	char *ep;
1105faf8dcbSSam Leffler 
1115faf8dcbSSam Leffler 	t = time(NULL);
1125faf8dcbSSam Leffler 	newval = (time_t)strtoul(val, &ep, 0);
1135faf8dcbSSam Leffler 	if (val == ep)
1145faf8dcbSSam Leffler 		errx(1, "invalid %s", cmd);
1155faf8dcbSSam Leffler 	if (afp->af_af != AF_INET6)
1165faf8dcbSSam Leffler 		errx(1, "%s not allowed for the AF", cmd);
1175faf8dcbSSam Leffler 	if (strcmp(cmd, "vltime") == 0) {
1185faf8dcbSSam Leffler 		in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
1195faf8dcbSSam Leffler 		in6_addreq.ifra_lifetime.ia6t_vltime = newval;
1205faf8dcbSSam Leffler 	} else if (strcmp(cmd, "pltime") == 0) {
1215faf8dcbSSam Leffler 		in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
1225faf8dcbSSam Leffler 		in6_addreq.ifra_lifetime.ia6t_pltime = newval;
1235faf8dcbSSam Leffler 	}
1245faf8dcbSSam Leffler }
1255faf8dcbSSam Leffler 
1265faf8dcbSSam Leffler static void
1275faf8dcbSSam Leffler setip6pltime(const char *seconds, int dummy __unused, int s,
1285faf8dcbSSam Leffler     const struct afswtch *afp)
1295faf8dcbSSam Leffler {
1305faf8dcbSSam Leffler 	setip6lifetime("pltime", seconds, s, afp);
1315faf8dcbSSam Leffler }
1325faf8dcbSSam Leffler 
1335faf8dcbSSam Leffler static void
1345faf8dcbSSam Leffler setip6vltime(const char *seconds, int dummy __unused, int s,
1355faf8dcbSSam Leffler     const struct afswtch *afp)
1365faf8dcbSSam Leffler {
1375faf8dcbSSam Leffler 	setip6lifetime("vltime", seconds, s, afp);
1385faf8dcbSSam Leffler }
1395faf8dcbSSam Leffler 
1405faf8dcbSSam Leffler static void
1415faf8dcbSSam Leffler setip6eui64(const char *cmd, int dummy __unused, int s,
1425faf8dcbSSam Leffler     const struct afswtch *afp)
1435faf8dcbSSam Leffler {
1445faf8dcbSSam Leffler 	struct ifaddrs *ifap, *ifa;
1455faf8dcbSSam Leffler 	const struct sockaddr_in6 *sin6 = NULL;
1465faf8dcbSSam Leffler 	const struct in6_addr *lladdr = NULL;
1475faf8dcbSSam Leffler 	struct in6_addr *in6;
1485faf8dcbSSam Leffler 
1495faf8dcbSSam Leffler 	if (afp->af_af != AF_INET6)
1505faf8dcbSSam Leffler 		errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
1515faf8dcbSSam Leffler  	in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
1525faf8dcbSSam Leffler 	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
1535faf8dcbSSam Leffler 		errx(EXIT_FAILURE, "interface index is already filled");
1545faf8dcbSSam Leffler 	if (getifaddrs(&ifap) != 0)
1555faf8dcbSSam Leffler 		err(EXIT_FAILURE, "getifaddrs");
1565faf8dcbSSam Leffler 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1575faf8dcbSSam Leffler 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
1585faf8dcbSSam Leffler 		    strcmp(ifa->ifa_name, name) == 0) {
1595faf8dcbSSam Leffler 			sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
1605faf8dcbSSam Leffler 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1615faf8dcbSSam Leffler 				lladdr = &sin6->sin6_addr;
1625faf8dcbSSam Leffler 				break;
1635faf8dcbSSam Leffler 			}
1645faf8dcbSSam Leffler 		}
1655faf8dcbSSam Leffler 	}
1665faf8dcbSSam Leffler 	if (!lladdr)
1675faf8dcbSSam Leffler 		errx(EXIT_FAILURE, "could not determine link local address");
1685faf8dcbSSam Leffler 
1695faf8dcbSSam Leffler  	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
1705faf8dcbSSam Leffler 
1715faf8dcbSSam Leffler 	freeifaddrs(ifap);
1725faf8dcbSSam Leffler }
1735faf8dcbSSam Leffler 
1745faf8dcbSSam Leffler static void
1755faf8dcbSSam Leffler in6_fillscopeid(struct sockaddr_in6 *sin6)
1765faf8dcbSSam Leffler {
1775faf8dcbSSam Leffler #if defined(__KAME__) && defined(KAME_SCOPEID)
1785faf8dcbSSam Leffler 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1795faf8dcbSSam Leffler 		sin6->sin6_scope_id =
1805faf8dcbSSam Leffler 			ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1815faf8dcbSSam Leffler 		sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1825faf8dcbSSam Leffler 	}
1835faf8dcbSSam Leffler #endif
1845faf8dcbSSam Leffler }
1855faf8dcbSSam Leffler 
1865faf8dcbSSam Leffler static void
1875faf8dcbSSam Leffler in6_status(int s __unused, const struct rt_addrinfo * info)
1885faf8dcbSSam Leffler {
1895faf8dcbSSam Leffler 	struct sockaddr_in6 *sin, null_sin;
1905faf8dcbSSam Leffler 	struct in6_ifreq ifr6;
1915faf8dcbSSam Leffler 	int s6;
1925faf8dcbSSam Leffler 	u_int32_t flags6;
1935faf8dcbSSam Leffler 	struct in6_addrlifetime lifetime;
1945faf8dcbSSam Leffler 	time_t t = time(NULL);
1955faf8dcbSSam Leffler 	int error;
1965faf8dcbSSam Leffler 	u_int32_t scopeid;
1975faf8dcbSSam Leffler 
1985faf8dcbSSam Leffler 	memset(&null_sin, 0, sizeof(null_sin));
1995faf8dcbSSam Leffler 
2005faf8dcbSSam Leffler 	sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
2015faf8dcbSSam Leffler 	if (sin == NULL)
2025faf8dcbSSam Leffler 		return;
2035faf8dcbSSam Leffler 
2045faf8dcbSSam Leffler 	strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
2055faf8dcbSSam Leffler 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
2065faf8dcbSSam Leffler 		warn("socket(AF_INET6,SOCK_DGRAM)");
2075faf8dcbSSam Leffler 		return;
2085faf8dcbSSam Leffler 	}
2095faf8dcbSSam Leffler 	ifr6.ifr_addr = *sin;
2105faf8dcbSSam Leffler 	if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
2115faf8dcbSSam Leffler 		warn("ioctl(SIOCGIFAFLAG_IN6)");
2125faf8dcbSSam Leffler 		close(s6);
2135faf8dcbSSam Leffler 		return;
2145faf8dcbSSam Leffler 	}
2155faf8dcbSSam Leffler 	flags6 = ifr6.ifr_ifru.ifru_flags6;
2165faf8dcbSSam Leffler 	memset(&lifetime, 0, sizeof(lifetime));
2175faf8dcbSSam Leffler 	ifr6.ifr_addr = *sin;
2185faf8dcbSSam Leffler 	if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
2195faf8dcbSSam Leffler 		warn("ioctl(SIOCGIFALIFETIME_IN6)");
2205faf8dcbSSam Leffler 		close(s6);
2215faf8dcbSSam Leffler 		return;
2225faf8dcbSSam Leffler 	}
2235faf8dcbSSam Leffler 	lifetime = ifr6.ifr_ifru.ifru_lifetime;
2245faf8dcbSSam Leffler 	close(s6);
2255faf8dcbSSam Leffler 
2265faf8dcbSSam Leffler 	/* XXX: embedded link local addr check */
2275faf8dcbSSam Leffler 	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
2285faf8dcbSSam Leffler 	    *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
2295faf8dcbSSam Leffler 		u_short index;
2305faf8dcbSSam Leffler 
2315faf8dcbSSam Leffler 		index = *(u_short *)&sin->sin6_addr.s6_addr[2];
2325faf8dcbSSam Leffler 		*(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
2335faf8dcbSSam Leffler 		if (sin->sin6_scope_id == 0)
2345faf8dcbSSam Leffler 			sin->sin6_scope_id = ntohs(index);
2355faf8dcbSSam Leffler 	}
2365faf8dcbSSam Leffler 	scopeid = sin->sin6_scope_id;
2375faf8dcbSSam Leffler 
2385faf8dcbSSam Leffler 	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
2395faf8dcbSSam Leffler 			    sizeof(addr_buf), NULL, 0,
2405faf8dcbSSam Leffler 			    NI_NUMERICHOST|NI_WITHSCOPEID);
2415faf8dcbSSam Leffler 	if (error != 0)
2425faf8dcbSSam Leffler 		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
2435faf8dcbSSam Leffler 			  sizeof(addr_buf));
2445faf8dcbSSam Leffler 	printf("\tinet6 %s ", addr_buf);
2455faf8dcbSSam Leffler 
2465faf8dcbSSam Leffler 	if (flags & IFF_POINTOPOINT) {
2475faf8dcbSSam Leffler 		/* note RTAX_BRD overlap with IFF_BROADCAST */
2485faf8dcbSSam Leffler 		sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
2495faf8dcbSSam Leffler 		/*
2505faf8dcbSSam Leffler 		 * some of the interfaces do not have valid destination
2515faf8dcbSSam Leffler 		 * address.
2525faf8dcbSSam Leffler 		 */
2535faf8dcbSSam Leffler 		if (sin && sin->sin6_family == AF_INET6) {
2545faf8dcbSSam Leffler 			int error;
2555faf8dcbSSam Leffler 
2565faf8dcbSSam Leffler 			/* XXX: embedded link local addr check */
2575faf8dcbSSam Leffler 			if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
2585faf8dcbSSam Leffler 			    *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
2595faf8dcbSSam Leffler 				u_short index;
2605faf8dcbSSam Leffler 
2615faf8dcbSSam Leffler 				index = *(u_short *)&sin->sin6_addr.s6_addr[2];
2625faf8dcbSSam Leffler 				*(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
2635faf8dcbSSam Leffler 				if (sin->sin6_scope_id == 0)
2645faf8dcbSSam Leffler 					sin->sin6_scope_id = ntohs(index);
2655faf8dcbSSam Leffler 			}
2665faf8dcbSSam Leffler 
2675faf8dcbSSam Leffler 			error = getnameinfo((struct sockaddr *)sin,
2685faf8dcbSSam Leffler 					    sin->sin6_len, addr_buf,
2695faf8dcbSSam Leffler 					    sizeof(addr_buf), NULL, 0,
2705faf8dcbSSam Leffler 					    NI_NUMERICHOST|NI_WITHSCOPEID);
2715faf8dcbSSam Leffler 			if (error != 0)
2725faf8dcbSSam Leffler 				inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
2735faf8dcbSSam Leffler 					  sizeof(addr_buf));
2745faf8dcbSSam Leffler 			printf("--> %s ", addr_buf);
2755faf8dcbSSam Leffler 		}
2765faf8dcbSSam Leffler 	}
2775faf8dcbSSam Leffler 
2785faf8dcbSSam Leffler 	sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
2795faf8dcbSSam Leffler 	if (!sin)
2805faf8dcbSSam Leffler 		sin = &null_sin;
2815faf8dcbSSam Leffler 	printf("prefixlen %d ", prefix(&sin->sin6_addr,
2825faf8dcbSSam Leffler 		sizeof(struct in6_addr)));
2835faf8dcbSSam Leffler 
2845faf8dcbSSam Leffler 	if ((flags6 & IN6_IFF_ANYCAST) != 0)
2855faf8dcbSSam Leffler 		printf("anycast ");
2865faf8dcbSSam Leffler 	if ((flags6 & IN6_IFF_TENTATIVE) != 0)
2875faf8dcbSSam Leffler 		printf("tentative ");
2885faf8dcbSSam Leffler 	if ((flags6 & IN6_IFF_DUPLICATED) != 0)
2895faf8dcbSSam Leffler 		printf("duplicated ");
2905faf8dcbSSam Leffler 	if ((flags6 & IN6_IFF_DETACHED) != 0)
2915faf8dcbSSam Leffler 		printf("detached ");
2925faf8dcbSSam Leffler 	if ((flags6 & IN6_IFF_DEPRECATED) != 0)
2935faf8dcbSSam Leffler 		printf("deprecated ");
2945faf8dcbSSam Leffler 	if ((flags6 & IN6_IFF_AUTOCONF) != 0)
2955faf8dcbSSam Leffler 		printf("autoconf ");
2965faf8dcbSSam Leffler 	if ((flags6 & IN6_IFF_TEMPORARY) != 0)
2975faf8dcbSSam Leffler 		printf("temporary ");
2985faf8dcbSSam Leffler 
2995faf8dcbSSam Leffler         if (scopeid)
3005faf8dcbSSam Leffler 		printf("scopeid 0x%x ", scopeid);
3015faf8dcbSSam Leffler 
3025faf8dcbSSam Leffler 	if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
3035faf8dcbSSam Leffler 		printf("pltime ");
3045faf8dcbSSam Leffler 		if (lifetime.ia6t_preferred) {
3055faf8dcbSSam Leffler 			printf("%s ", lifetime.ia6t_preferred < t
3065faf8dcbSSam Leffler 				? "0" : sec2str(lifetime.ia6t_preferred - t));
3075faf8dcbSSam Leffler 		} else
3085faf8dcbSSam Leffler 			printf("infty ");
3095faf8dcbSSam Leffler 
3105faf8dcbSSam Leffler 		printf("vltime ");
3115faf8dcbSSam Leffler 		if (lifetime.ia6t_expire) {
3125faf8dcbSSam Leffler 			printf("%s ", lifetime.ia6t_expire < t
3135faf8dcbSSam Leffler 				? "0" : sec2str(lifetime.ia6t_expire - t));
3145faf8dcbSSam Leffler 		} else
3155faf8dcbSSam Leffler 			printf("infty ");
3165faf8dcbSSam Leffler 	}
3175faf8dcbSSam Leffler 
3185faf8dcbSSam Leffler 	putchar('\n');
3195faf8dcbSSam Leffler }
3205faf8dcbSSam Leffler 
3215faf8dcbSSam Leffler #define	SIN6(x) ((struct sockaddr_in6 *) &(x))
3225faf8dcbSSam Leffler static struct	sockaddr_in6 *sin6tab[] = {
3235faf8dcbSSam Leffler 	SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
3245faf8dcbSSam Leffler 	SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)
3255faf8dcbSSam Leffler };
3265faf8dcbSSam Leffler 
3275faf8dcbSSam Leffler static void
3285faf8dcbSSam Leffler in6_getprefix(const char *plen, int which)
3295faf8dcbSSam Leffler {
3305faf8dcbSSam Leffler 	struct sockaddr_in6 *sin = sin6tab[which];
3315faf8dcbSSam Leffler 	u_char *cp;
3325faf8dcbSSam Leffler 	int len = atoi(plen);
3335faf8dcbSSam Leffler 
3345faf8dcbSSam Leffler 	if ((len < 0) || (len > 128))
3355faf8dcbSSam Leffler 		errx(1, "%s: bad value", plen);
3365faf8dcbSSam Leffler 	sin->sin6_len = sizeof(*sin);
3375faf8dcbSSam Leffler 	if (which != MASK)
3385faf8dcbSSam Leffler 		sin->sin6_family = AF_INET6;
3395faf8dcbSSam Leffler 	if ((len == 0) || (len == 128)) {
3405faf8dcbSSam Leffler 		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
3415faf8dcbSSam Leffler 		return;
3425faf8dcbSSam Leffler 	}
3435faf8dcbSSam Leffler 	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
3445faf8dcbSSam Leffler 	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
3455faf8dcbSSam Leffler 		*cp++ = 0xff;
3465faf8dcbSSam Leffler 	*cp = 0xff << (8 - len);
3475faf8dcbSSam Leffler }
3485faf8dcbSSam Leffler 
3495faf8dcbSSam Leffler static void
3505faf8dcbSSam Leffler in6_getaddr(const char *s, int which)
3515faf8dcbSSam Leffler {
3525faf8dcbSSam Leffler 	struct sockaddr_in6 *sin = sin6tab[which];
3535faf8dcbSSam Leffler 	struct addrinfo hints, *res;
3545faf8dcbSSam Leffler 	int error = -1;
3555faf8dcbSSam Leffler 
3565faf8dcbSSam Leffler 	newaddr &= 1;
3575faf8dcbSSam Leffler 
3585faf8dcbSSam Leffler 	sin->sin6_len = sizeof(*sin);
3595faf8dcbSSam Leffler 	if (which != MASK)
3605faf8dcbSSam Leffler 		sin->sin6_family = AF_INET6;
3615faf8dcbSSam Leffler 
3625faf8dcbSSam Leffler 	if (which == ADDR) {
3635faf8dcbSSam Leffler 		char *p = NULL;
3645faf8dcbSSam Leffler 		if((p = strrchr(s, '/')) != NULL) {
3655faf8dcbSSam Leffler 			*p = '\0';
3665faf8dcbSSam Leffler 			in6_getprefix(p + 1, MASK);
3675faf8dcbSSam Leffler 			explicit_prefix = 1;
3685faf8dcbSSam Leffler 		}
3695faf8dcbSSam Leffler 	}
3705faf8dcbSSam Leffler 
3715faf8dcbSSam Leffler 	if (sin->sin6_family == AF_INET6) {
3725faf8dcbSSam Leffler 		bzero(&hints, sizeof(struct addrinfo));
3735faf8dcbSSam Leffler 		hints.ai_family = AF_INET6;
3745faf8dcbSSam Leffler 		error = getaddrinfo(s, NULL, &hints, &res);
3755faf8dcbSSam Leffler 	}
3765faf8dcbSSam Leffler 	if (error != 0) {
3775faf8dcbSSam Leffler 		if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
3785faf8dcbSSam Leffler 			errx(1, "%s: bad value", s);
3795faf8dcbSSam Leffler 	} else
3805faf8dcbSSam Leffler 		bcopy(res->ai_addr, sin, res->ai_addrlen);
3815faf8dcbSSam Leffler }
3825faf8dcbSSam Leffler 
3835faf8dcbSSam Leffler static int
3845faf8dcbSSam Leffler prefix(void *val, int size)
3855faf8dcbSSam Leffler {
3865faf8dcbSSam Leffler         u_char *name = (u_char *)val;
3875faf8dcbSSam Leffler         int byte, bit, plen = 0;
3885faf8dcbSSam Leffler 
3895faf8dcbSSam Leffler         for (byte = 0; byte < size; byte++, plen += 8)
3905faf8dcbSSam Leffler                 if (name[byte] != 0xff)
3915faf8dcbSSam Leffler                         break;
3925faf8dcbSSam Leffler 	if (byte == size)
3935faf8dcbSSam Leffler 		return (plen);
3945faf8dcbSSam Leffler 	for (bit = 7; bit != 0; bit--, plen++)
3955faf8dcbSSam Leffler                 if (!(name[byte] & (1 << bit)))
3965faf8dcbSSam Leffler                         break;
3975faf8dcbSSam Leffler         for (; bit != 0; bit--)
3985faf8dcbSSam Leffler                 if (name[byte] & (1 << bit))
3995faf8dcbSSam Leffler                         return(0);
4005faf8dcbSSam Leffler         byte++;
4015faf8dcbSSam Leffler         for (; byte < size; byte++)
4025faf8dcbSSam Leffler                 if (name[byte])
4035faf8dcbSSam Leffler                         return(0);
4045faf8dcbSSam Leffler         return (plen);
4055faf8dcbSSam Leffler }
4065faf8dcbSSam Leffler 
4075faf8dcbSSam Leffler static char *
4085faf8dcbSSam Leffler sec2str(time_t total)
4095faf8dcbSSam Leffler {
4105faf8dcbSSam Leffler 	static char result[256];
4115faf8dcbSSam Leffler 	int days, hours, mins, secs;
4125faf8dcbSSam Leffler 	int first = 1;
4135faf8dcbSSam Leffler 	char *p = result;
4145faf8dcbSSam Leffler 
4155faf8dcbSSam Leffler 	if (0) {
4165faf8dcbSSam Leffler 		days = total / 3600 / 24;
4175faf8dcbSSam Leffler 		hours = (total / 3600) % 24;
4185faf8dcbSSam Leffler 		mins = (total / 60) % 60;
4195faf8dcbSSam Leffler 		secs = total % 60;
4205faf8dcbSSam Leffler 
4215faf8dcbSSam Leffler 		if (days) {
4225faf8dcbSSam Leffler 			first = 0;
4235faf8dcbSSam Leffler 			p += sprintf(p, "%dd", days);
4245faf8dcbSSam Leffler 		}
4255faf8dcbSSam Leffler 		if (!first || hours) {
4265faf8dcbSSam Leffler 			first = 0;
4275faf8dcbSSam Leffler 			p += sprintf(p, "%dh", hours);
4285faf8dcbSSam Leffler 		}
4295faf8dcbSSam Leffler 		if (!first || mins) {
4305faf8dcbSSam Leffler 			first = 0;
4315faf8dcbSSam Leffler 			p += sprintf(p, "%dm", mins);
4325faf8dcbSSam Leffler 		}
4335faf8dcbSSam Leffler 		sprintf(p, "%ds", secs);
4345faf8dcbSSam Leffler 	} else
4355faf8dcbSSam Leffler 		sprintf(result, "%lu", (unsigned long)total);
4365faf8dcbSSam Leffler 
4375faf8dcbSSam Leffler 	return(result);
4385faf8dcbSSam Leffler }
4395faf8dcbSSam Leffler 
4405faf8dcbSSam Leffler static void
4415faf8dcbSSam Leffler in6_postproc(int s, const struct afswtch *afp)
4425faf8dcbSSam Leffler {
4435faf8dcbSSam Leffler 	if (explicit_prefix == 0) {
4445faf8dcbSSam Leffler 		/* Aggregatable address architecture defines all prefixes
4455faf8dcbSSam Leffler 		   are 64. So, it is convenient to set prefixlen to 64 if
4465faf8dcbSSam Leffler 		   it is not specified. */
4475faf8dcbSSam Leffler 		setifprefixlen("64", 0, s, afp);
4485faf8dcbSSam Leffler 		/* in6_getprefix("64", MASK) if MASK is available here... */
4495faf8dcbSSam Leffler 	}
4505faf8dcbSSam Leffler }
4515faf8dcbSSam Leffler 
4525faf8dcbSSam Leffler static void
4535faf8dcbSSam Leffler in6_status_tunnel(int s)
4545faf8dcbSSam Leffler {
4555faf8dcbSSam Leffler 	char src[NI_MAXHOST];
4565faf8dcbSSam Leffler 	char dst[NI_MAXHOST];
4575faf8dcbSSam Leffler #ifdef NI_WITHSCOPEID
4585faf8dcbSSam Leffler 	const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
4595faf8dcbSSam Leffler #else
4605faf8dcbSSam Leffler 	const int niflag = NI_NUMERICHOST;
4615faf8dcbSSam Leffler #endif
4625faf8dcbSSam Leffler 	struct in6_ifreq in6_ifr;
4635faf8dcbSSam Leffler 	const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
4645faf8dcbSSam Leffler 
4655faf8dcbSSam Leffler 	memset(&in6_ifr, 0, sizeof(in6_ifr));
4665faf8dcbSSam Leffler 	strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
4675faf8dcbSSam Leffler 
4685faf8dcbSSam Leffler 	if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
4695faf8dcbSSam Leffler 		return;
4705faf8dcbSSam Leffler 	if (sa->sa_family == AF_INET6)
4715faf8dcbSSam Leffler 		in6_fillscopeid(&in6_ifr.ifr_addr);
4725faf8dcbSSam Leffler 	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, niflag) != 0)
4735faf8dcbSSam Leffler 		src[0] = '\0';
4745faf8dcbSSam Leffler 
4755faf8dcbSSam Leffler 	if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
4765faf8dcbSSam Leffler 		return;
4775faf8dcbSSam Leffler 	if (sa->sa_family == AF_INET6)
4785faf8dcbSSam Leffler 		in6_fillscopeid(&in6_ifr.ifr_addr);
4795faf8dcbSSam Leffler 	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, niflag) != 0)
4805faf8dcbSSam Leffler 		dst[0] = '\0';
4815faf8dcbSSam Leffler 
4825faf8dcbSSam Leffler 	printf("\ttunnel inet6 %s --> %s\n", src, dst);
4835faf8dcbSSam Leffler }
4845faf8dcbSSam Leffler 
4855faf8dcbSSam Leffler static void
4865faf8dcbSSam Leffler in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
4875faf8dcbSSam Leffler {
4885faf8dcbSSam Leffler 	struct in6_aliasreq in6_addreq;
4895faf8dcbSSam Leffler 
4905faf8dcbSSam Leffler 	memset(&in6_addreq, 0, sizeof(in6_addreq));
4915faf8dcbSSam Leffler 	strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
4925faf8dcbSSam Leffler 	memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
4935faf8dcbSSam Leffler 	memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
4945faf8dcbSSam Leffler 	    dstres->ai_addr->sa_len);
4955faf8dcbSSam Leffler 
4965faf8dcbSSam Leffler 	if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
4975faf8dcbSSam Leffler 		warn("SIOCSIFPHYADDR_IN6");
4985faf8dcbSSam Leffler }
4995faf8dcbSSam Leffler 
5005faf8dcbSSam Leffler static struct cmd inet6_cmds[] = {
5015faf8dcbSSam Leffler 	DEF_CMD_ARG("prefixlen",			setifprefixlen),
5025faf8dcbSSam Leffler 	DEF_CMD("anycast",	IN6_IFF_ANYCAST,	setip6flags),
5035faf8dcbSSam Leffler 	DEF_CMD("tentative",	IN6_IFF_TENTATIVE,	setip6flags),
5045faf8dcbSSam Leffler 	DEF_CMD("-tentative",	-IN6_IFF_TENTATIVE,	setip6flags),
5055faf8dcbSSam Leffler 	DEF_CMD("deprecated",	IN6_IFF_DEPRECATED,	setip6flags),
5065faf8dcbSSam Leffler 	DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED,	setip6flags),
5075faf8dcbSSam Leffler 	DEF_CMD("autoconf",	IN6_IFF_AUTOCONF,	setip6flags),
5085faf8dcbSSam Leffler 	DEF_CMD("-autoconf",	-IN6_IFF_AUTOCONF,	setip6flags),
5095faf8dcbSSam Leffler 	DEF_CMD_ARG("pltime",        			setip6pltime),
5105faf8dcbSSam Leffler 	DEF_CMD_ARG("vltime",        			setip6vltime),
5115faf8dcbSSam Leffler 	DEF_CMD("eui64",	0,			setip6eui64),
5125faf8dcbSSam Leffler };
5135faf8dcbSSam Leffler 
5145faf8dcbSSam Leffler static struct afswtch af_inet6 = {
5155faf8dcbSSam Leffler 	.af_name	= "inet6",
5165faf8dcbSSam Leffler 	.af_af		= AF_INET6,
5175faf8dcbSSam Leffler 	.af_status	= in6_status,
5185faf8dcbSSam Leffler 	.af_getaddr	= in6_getaddr,
5195faf8dcbSSam Leffler 	.af_getprefix	= in6_getprefix,
5205faf8dcbSSam Leffler 	.af_postproc	= in6_postproc,
5215faf8dcbSSam Leffler 	.af_status_tunnel = in6_status_tunnel,
5225faf8dcbSSam Leffler 	.af_settunnel	= in6_set_tunnel,
5235faf8dcbSSam Leffler 	.af_difaddr	= SIOCDIFADDR_IN6,
5245faf8dcbSSam Leffler 	.af_aifaddr	= SIOCAIFADDR_IN6,
5255faf8dcbSSam Leffler 	.af_ridreq	= &in6_addreq,
5265faf8dcbSSam Leffler 	.af_addreq	= &in6_addreq,
5275faf8dcbSSam Leffler };
5285faf8dcbSSam Leffler 
5295faf8dcbSSam Leffler static void
5305faf8dcbSSam Leffler in6_Lopt_cb(const char *optarg __unused)
5315faf8dcbSSam Leffler {
5325faf8dcbSSam Leffler 	ip6lifetime++;	/* print IPv6 address lifetime */
5335faf8dcbSSam Leffler }
5345faf8dcbSSam Leffler static struct option in6_Lopt = { "L", "[-L]", in6_Lopt_cb };
5355faf8dcbSSam Leffler 
5365faf8dcbSSam Leffler static __constructor void
5375faf8dcbSSam Leffler inet6_ctor(void)
5385faf8dcbSSam Leffler {
5395faf8dcbSSam Leffler #define	N(a)	(sizeof(a) / sizeof(a[0]))
5405faf8dcbSSam Leffler 	int i;
5415faf8dcbSSam Leffler 
5425faf8dcbSSam Leffler 	for (i = 0; i < N(inet6_cmds);  i++)
5435faf8dcbSSam Leffler 		cmd_register(&inet6_cmds[i]);
5445faf8dcbSSam Leffler 	af_register(&af_inet6);
5455faf8dcbSSam Leffler 	opt_register(&in6_Lopt);
5465faf8dcbSSam Leffler #undef N
5475faf8dcbSSam Leffler }
548