af_inet6.c revision 166446
1138593Ssam/*
2138593Ssam * Copyright (c) 1983, 1993
3138593Ssam *	The Regents of the University of California.  All rights reserved.
4138593Ssam *
5138593Ssam * Redistribution and use in source and binary forms, with or without
6138593Ssam * modification, are permitted provided that the following conditions
7138593Ssam * are met:
8138593Ssam * 1. Redistributions of source code must retain the above copyright
9138593Ssam *    notice, this list of conditions and the following disclaimer.
10138593Ssam * 2. Redistributions in binary form must reproduce the above copyright
11138593Ssam *    notice, this list of conditions and the following disclaimer in the
12138593Ssam *    documentation and/or other materials provided with the distribution.
13138593Ssam * 4. Neither the name of the University nor the names of its contributors
14138593Ssam *    may be used to endorse or promote products derived from this software
15138593Ssam *    without specific prior written permission.
16138593Ssam *
17138593Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18138593Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19138593Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20138593Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21138593Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22138593Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23138593Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24138593Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25138593Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26138593Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27138593Ssam * SUCH DAMAGE.
28138593Ssam */
29138593Ssam
30138593Ssam#ifndef lint
31138593Ssamstatic const char rcsid[] =
32138593Ssam  "$FreeBSD: head/sbin/ifconfig/af_inet6.c 166446 2007-02-03 03:40:33Z bms $";
33138593Ssam#endif /* not lint */
34138593Ssam
35138593Ssam#include <sys/param.h>
36138593Ssam#include <sys/ioctl.h>
37138593Ssam#include <sys/socket.h>
38138593Ssam#include <net/if.h>
39138593Ssam#include <net/route.h>		/* for RTX_IFA */
40138593Ssam
41138593Ssam#include <err.h>
42138593Ssam#include <stdio.h>
43138593Ssam#include <stdlib.h>
44138593Ssam#include <string.h>
45138593Ssam#include <unistd.h>
46138593Ssam#include <ifaddrs.h>
47138593Ssam
48138593Ssam#include <arpa/inet.h>
49138593Ssam
50138593Ssam#include <netinet/in.h>
51138593Ssam#include <net/if_var.h>		/* for struct ifaddr */
52138593Ssam#include <netinet/in_var.h>
53138593Ssam#include <arpa/inet.h>
54138593Ssam#include <netdb.h>
55138593Ssam
56138593Ssam#include <netinet6/nd6.h>	/* Define ND6_INFINITE_LIFETIME */
57138593Ssam
58138593Ssam#include "ifconfig.h"
59138593Ssam
60138593Ssamstatic	struct in6_ifreq in6_ridreq;
61138593Ssamstatic	struct in6_aliasreq in6_addreq =
62138593Ssam  { { 0 },
63138593Ssam    { 0 },
64138593Ssam    { 0 },
65138593Ssam    { 0 },
66138593Ssam    0,
67138593Ssam    { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
68138593Ssamstatic	int ip6lifetime;
69138593Ssam
70138593Ssamstatic	void in6_fillscopeid(struct sockaddr_in6 *sin6);
71138593Ssamstatic	int prefix(void *, int);
72138593Ssamstatic	char *sec2str(time_t);
73138593Ssamstatic	int explicit_prefix = 0;
74138593Ssam
75138593Ssamstatic	char addr_buf[MAXHOSTNAMELEN *2 + 1];	/*for getnameinfo()*/
76138593Ssam
77138593Ssamstatic void
78138593Ssamsetifprefixlen(const char *addr, int dummy __unused, int s,
79138593Ssam    const struct afswtch *afp)
80138593Ssam{
81138593Ssam        if (afp->af_getprefix != NULL)
82138593Ssam                afp->af_getprefix(addr, MASK);
83138593Ssam	explicit_prefix = 1;
84138593Ssam}
85138593Ssam
86138593Ssamstatic void
87138593Ssamsetip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
88138593Ssam    const struct afswtch *afp)
89138593Ssam{
90138593Ssam	if (afp->af_af != AF_INET6)
91138593Ssam		err(1, "address flags can be set only for inet6 addresses");
92138593Ssam
93138593Ssam	if (flag < 0)
94138593Ssam		in6_addreq.ifra_flags &= ~(-flag);
95138593Ssam	else
96138593Ssam		in6_addreq.ifra_flags |= flag;
97138593Ssam}
98138593Ssam
99138593Ssamstatic void
100138593Ssamsetip6lifetime(const char *cmd, const char *val, int s,
101138593Ssam    const struct afswtch *afp)
102138593Ssam{
103138593Ssam	time_t newval, t;
104138593Ssam	char *ep;
105138593Ssam
106138593Ssam	t = time(NULL);
107138593Ssam	newval = (time_t)strtoul(val, &ep, 0);
108138593Ssam	if (val == ep)
109138593Ssam		errx(1, "invalid %s", cmd);
110138593Ssam	if (afp->af_af != AF_INET6)
111138593Ssam		errx(1, "%s not allowed for the AF", cmd);
112138593Ssam	if (strcmp(cmd, "vltime") == 0) {
113138593Ssam		in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
114138593Ssam		in6_addreq.ifra_lifetime.ia6t_vltime = newval;
115138593Ssam	} else if (strcmp(cmd, "pltime") == 0) {
116138593Ssam		in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
117138593Ssam		in6_addreq.ifra_lifetime.ia6t_pltime = newval;
118138593Ssam	}
119138593Ssam}
120138593Ssam
121138593Ssamstatic void
122138593Ssamsetip6pltime(const char *seconds, int dummy __unused, int s,
123138593Ssam    const struct afswtch *afp)
124138593Ssam{
125138593Ssam	setip6lifetime("pltime", seconds, s, afp);
126138593Ssam}
127138593Ssam
128138593Ssamstatic void
129138593Ssamsetip6vltime(const char *seconds, int dummy __unused, int s,
130138593Ssam    const struct afswtch *afp)
131138593Ssam{
132138593Ssam	setip6lifetime("vltime", seconds, s, afp);
133138593Ssam}
134138593Ssam
135138593Ssamstatic void
136138593Ssamsetip6eui64(const char *cmd, int dummy __unused, int s,
137138593Ssam    const struct afswtch *afp)
138138593Ssam{
139138593Ssam	struct ifaddrs *ifap, *ifa;
140138593Ssam	const struct sockaddr_in6 *sin6 = NULL;
141138593Ssam	const struct in6_addr *lladdr = NULL;
142138593Ssam	struct in6_addr *in6;
143138593Ssam
144138593Ssam	if (afp->af_af != AF_INET6)
145138593Ssam		errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
146138593Ssam 	in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
147138593Ssam	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
148138593Ssam		errx(EXIT_FAILURE, "interface index is already filled");
149138593Ssam	if (getifaddrs(&ifap) != 0)
150138593Ssam		err(EXIT_FAILURE, "getifaddrs");
151138593Ssam	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
152138593Ssam		if (ifa->ifa_addr->sa_family == AF_INET6 &&
153138593Ssam		    strcmp(ifa->ifa_name, name) == 0) {
154138593Ssam			sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
155138593Ssam			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
156138593Ssam				lladdr = &sin6->sin6_addr;
157138593Ssam				break;
158138593Ssam			}
159138593Ssam		}
160138593Ssam	}
161138593Ssam	if (!lladdr)
162138593Ssam		errx(EXIT_FAILURE, "could not determine link local address");
163138593Ssam
164138593Ssam 	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
165138593Ssam
166138593Ssam	freeifaddrs(ifap);
167138593Ssam}
168138593Ssam
169138593Ssamstatic void
170138593Ssamin6_fillscopeid(struct sockaddr_in6 *sin6)
171138593Ssam{
172138593Ssam#if defined(__KAME__) && defined(KAME_SCOPEID)
173138593Ssam	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
174138593Ssam		sin6->sin6_scope_id =
175138593Ssam			ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
176138593Ssam		sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
177138593Ssam	}
178138593Ssam#endif
179138593Ssam}
180138593Ssam
181138593Ssamstatic void
182138593Ssamin6_status(int s __unused, const struct rt_addrinfo * info)
183138593Ssam{
184138593Ssam	struct sockaddr_in6 *sin, null_sin;
185138593Ssam	struct in6_ifreq ifr6;
186138593Ssam	int s6;
187138593Ssam	u_int32_t flags6;
188138593Ssam	struct in6_addrlifetime lifetime;
189138593Ssam	time_t t = time(NULL);
190138593Ssam	int error;
191138593Ssam	u_int32_t scopeid;
192138593Ssam
193138593Ssam	memset(&null_sin, 0, sizeof(null_sin));
194138593Ssam
195138593Ssam	sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
196138593Ssam	if (sin == NULL)
197138593Ssam		return;
198138593Ssam
199138593Ssam	strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
200138593Ssam	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
201138593Ssam		warn("socket(AF_INET6,SOCK_DGRAM)");
202138593Ssam		return;
203138593Ssam	}
204138593Ssam	ifr6.ifr_addr = *sin;
205138593Ssam	if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
206138593Ssam		warn("ioctl(SIOCGIFAFLAG_IN6)");
207138593Ssam		close(s6);
208138593Ssam		return;
209138593Ssam	}
210138593Ssam	flags6 = ifr6.ifr_ifru.ifru_flags6;
211138593Ssam	memset(&lifetime, 0, sizeof(lifetime));
212138593Ssam	ifr6.ifr_addr = *sin;
213138593Ssam	if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
214138593Ssam		warn("ioctl(SIOCGIFALIFETIME_IN6)");
215138593Ssam		close(s6);
216138593Ssam		return;
217138593Ssam	}
218138593Ssam	lifetime = ifr6.ifr_ifru.ifru_lifetime;
219138593Ssam	close(s6);
220138593Ssam
221138593Ssam	/* XXX: embedded link local addr check */
222138593Ssam	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
223138593Ssam	    *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
224138593Ssam		u_short index;
225138593Ssam
226138593Ssam		index = *(u_short *)&sin->sin6_addr.s6_addr[2];
227138593Ssam		*(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
228138593Ssam		if (sin->sin6_scope_id == 0)
229138593Ssam			sin->sin6_scope_id = ntohs(index);
230138593Ssam	}
231138593Ssam	scopeid = sin->sin6_scope_id;
232138593Ssam
233138593Ssam	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
234146187Sume			    sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
235138593Ssam	if (error != 0)
236138593Ssam		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
237138593Ssam			  sizeof(addr_buf));
238138593Ssam	printf("\tinet6 %s ", addr_buf);
239138593Ssam
240138593Ssam	if (flags & IFF_POINTOPOINT) {
241138593Ssam		/* note RTAX_BRD overlap with IFF_BROADCAST */
242138593Ssam		sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
243138593Ssam		/*
244138593Ssam		 * some of the interfaces do not have valid destination
245138593Ssam		 * address.
246138593Ssam		 */
247138593Ssam		if (sin && sin->sin6_family == AF_INET6) {
248138593Ssam			int error;
249138593Ssam
250138593Ssam			/* XXX: embedded link local addr check */
251138593Ssam			if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
252138593Ssam			    *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
253138593Ssam				u_short index;
254138593Ssam
255138593Ssam				index = *(u_short *)&sin->sin6_addr.s6_addr[2];
256138593Ssam				*(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
257138593Ssam				if (sin->sin6_scope_id == 0)
258138593Ssam					sin->sin6_scope_id = ntohs(index);
259138593Ssam			}
260138593Ssam
261138593Ssam			error = getnameinfo((struct sockaddr *)sin,
262138593Ssam					    sin->sin6_len, addr_buf,
263138593Ssam					    sizeof(addr_buf), NULL, 0,
264146187Sume					    NI_NUMERICHOST);
265138593Ssam			if (error != 0)
266138593Ssam				inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
267138593Ssam					  sizeof(addr_buf));
268138593Ssam			printf("--> %s ", addr_buf);
269138593Ssam		}
270138593Ssam	}
271138593Ssam
272138593Ssam	sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
273138593Ssam	if (!sin)
274138593Ssam		sin = &null_sin;
275138593Ssam	printf("prefixlen %d ", prefix(&sin->sin6_addr,
276138593Ssam		sizeof(struct in6_addr)));
277138593Ssam
278138593Ssam	if ((flags6 & IN6_IFF_ANYCAST) != 0)
279138593Ssam		printf("anycast ");
280138593Ssam	if ((flags6 & IN6_IFF_TENTATIVE) != 0)
281138593Ssam		printf("tentative ");
282138593Ssam	if ((flags6 & IN6_IFF_DUPLICATED) != 0)
283138593Ssam		printf("duplicated ");
284138593Ssam	if ((flags6 & IN6_IFF_DETACHED) != 0)
285138593Ssam		printf("detached ");
286138593Ssam	if ((flags6 & IN6_IFF_DEPRECATED) != 0)
287138593Ssam		printf("deprecated ");
288138593Ssam	if ((flags6 & IN6_IFF_AUTOCONF) != 0)
289138593Ssam		printf("autoconf ");
290138593Ssam	if ((flags6 & IN6_IFF_TEMPORARY) != 0)
291138593Ssam		printf("temporary ");
292138593Ssam
293138593Ssam        if (scopeid)
294138593Ssam		printf("scopeid 0x%x ", scopeid);
295138593Ssam
296138593Ssam	if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
297138593Ssam		printf("pltime ");
298138593Ssam		if (lifetime.ia6t_preferred) {
299138593Ssam			printf("%s ", lifetime.ia6t_preferred < t
300138593Ssam				? "0" : sec2str(lifetime.ia6t_preferred - t));
301138593Ssam		} else
302138593Ssam			printf("infty ");
303138593Ssam
304138593Ssam		printf("vltime ");
305138593Ssam		if (lifetime.ia6t_expire) {
306138593Ssam			printf("%s ", lifetime.ia6t_expire < t
307138593Ssam				? "0" : sec2str(lifetime.ia6t_expire - t));
308138593Ssam		} else
309138593Ssam			printf("infty ");
310138593Ssam	}
311138593Ssam
312138593Ssam	putchar('\n');
313138593Ssam}
314138593Ssam
315138593Ssam#define	SIN6(x) ((struct sockaddr_in6 *) &(x))
316138593Ssamstatic struct	sockaddr_in6 *sin6tab[] = {
317138593Ssam	SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
318138593Ssam	SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)
319138593Ssam};
320138593Ssam
321138593Ssamstatic void
322138593Ssamin6_getprefix(const char *plen, int which)
323138593Ssam{
324138593Ssam	struct sockaddr_in6 *sin = sin6tab[which];
325138593Ssam	u_char *cp;
326138593Ssam	int len = atoi(plen);
327138593Ssam
328138593Ssam	if ((len < 0) || (len > 128))
329138593Ssam		errx(1, "%s: bad value", plen);
330138593Ssam	sin->sin6_len = sizeof(*sin);
331138593Ssam	if (which != MASK)
332138593Ssam		sin->sin6_family = AF_INET6;
333138593Ssam	if ((len == 0) || (len == 128)) {
334138593Ssam		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
335138593Ssam		return;
336138593Ssam	}
337138593Ssam	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
338138593Ssam	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
339138593Ssam		*cp++ = 0xff;
340138593Ssam	*cp = 0xff << (8 - len);
341138593Ssam}
342138593Ssam
343138593Ssamstatic void
344138593Ssamin6_getaddr(const char *s, int which)
345138593Ssam{
346138593Ssam	struct sockaddr_in6 *sin = sin6tab[which];
347138593Ssam	struct addrinfo hints, *res;
348138593Ssam	int error = -1;
349138593Ssam
350138593Ssam	newaddr &= 1;
351138593Ssam
352138593Ssam	sin->sin6_len = sizeof(*sin);
353138593Ssam	if (which != MASK)
354138593Ssam		sin->sin6_family = AF_INET6;
355138593Ssam
356138593Ssam	if (which == ADDR) {
357138593Ssam		char *p = NULL;
358138593Ssam		if((p = strrchr(s, '/')) != NULL) {
359138593Ssam			*p = '\0';
360138593Ssam			in6_getprefix(p + 1, MASK);
361138593Ssam			explicit_prefix = 1;
362138593Ssam		}
363138593Ssam	}
364138593Ssam
365138593Ssam	if (sin->sin6_family == AF_INET6) {
366138593Ssam		bzero(&hints, sizeof(struct addrinfo));
367138593Ssam		hints.ai_family = AF_INET6;
368138593Ssam		error = getaddrinfo(s, NULL, &hints, &res);
369138593Ssam	}
370138593Ssam	if (error != 0) {
371138593Ssam		if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
372138593Ssam			errx(1, "%s: bad value", s);
373138593Ssam	} else
374138593Ssam		bcopy(res->ai_addr, sin, res->ai_addrlen);
375138593Ssam}
376138593Ssam
377138593Ssamstatic int
378138593Ssamprefix(void *val, int size)
379138593Ssam{
380138593Ssam        u_char *name = (u_char *)val;
381138593Ssam        int byte, bit, plen = 0;
382138593Ssam
383138593Ssam        for (byte = 0; byte < size; byte++, plen += 8)
384138593Ssam                if (name[byte] != 0xff)
385138593Ssam                        break;
386138593Ssam	if (byte == size)
387138593Ssam		return (plen);
388138593Ssam	for (bit = 7; bit != 0; bit--, plen++)
389138593Ssam                if (!(name[byte] & (1 << bit)))
390138593Ssam                        break;
391138593Ssam        for (; bit != 0; bit--)
392138593Ssam                if (name[byte] & (1 << bit))
393138593Ssam                        return(0);
394138593Ssam        byte++;
395138593Ssam        for (; byte < size; byte++)
396138593Ssam                if (name[byte])
397138593Ssam                        return(0);
398138593Ssam        return (plen);
399138593Ssam}
400138593Ssam
401138593Ssamstatic char *
402138593Ssamsec2str(time_t total)
403138593Ssam{
404138593Ssam	static char result[256];
405138593Ssam	int days, hours, mins, secs;
406138593Ssam	int first = 1;
407138593Ssam	char *p = result;
408138593Ssam
409138593Ssam	if (0) {
410138593Ssam		days = total / 3600 / 24;
411138593Ssam		hours = (total / 3600) % 24;
412138593Ssam		mins = (total / 60) % 60;
413138593Ssam		secs = total % 60;
414138593Ssam
415138593Ssam		if (days) {
416138593Ssam			first = 0;
417138593Ssam			p += sprintf(p, "%dd", days);
418138593Ssam		}
419138593Ssam		if (!first || hours) {
420138593Ssam			first = 0;
421138593Ssam			p += sprintf(p, "%dh", hours);
422138593Ssam		}
423138593Ssam		if (!first || mins) {
424138593Ssam			first = 0;
425138593Ssam			p += sprintf(p, "%dm", mins);
426138593Ssam		}
427138593Ssam		sprintf(p, "%ds", secs);
428138593Ssam	} else
429138593Ssam		sprintf(result, "%lu", (unsigned long)total);
430138593Ssam
431138593Ssam	return(result);
432138593Ssam}
433138593Ssam
434138593Ssamstatic void
435138593Ssamin6_postproc(int s, const struct afswtch *afp)
436138593Ssam{
437138593Ssam	if (explicit_prefix == 0) {
438138593Ssam		/* Aggregatable address architecture defines all prefixes
439138593Ssam		   are 64. So, it is convenient to set prefixlen to 64 if
440138593Ssam		   it is not specified. */
441138593Ssam		setifprefixlen("64", 0, s, afp);
442138593Ssam		/* in6_getprefix("64", MASK) if MASK is available here... */
443138593Ssam	}
444138593Ssam}
445138593Ssam
446138593Ssamstatic void
447138593Ssamin6_status_tunnel(int s)
448138593Ssam{
449138593Ssam	char src[NI_MAXHOST];
450138593Ssam	char dst[NI_MAXHOST];
451138593Ssam	struct in6_ifreq in6_ifr;
452138593Ssam	const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
453138593Ssam
454138593Ssam	memset(&in6_ifr, 0, sizeof(in6_ifr));
455138593Ssam	strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
456138593Ssam
457138593Ssam	if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
458138593Ssam		return;
459147437Sume	if (sa->sa_family != AF_INET6)
460147437Sume		return;
461147437Sume	in6_fillscopeid(&in6_ifr.ifr_addr);
462146187Sume	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
463146187Sume	    NI_NUMERICHOST) != 0)
464138593Ssam		src[0] = '\0';
465138593Ssam
466138593Ssam	if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
467138593Ssam		return;
468147437Sume	if (sa->sa_family != AF_INET6)
469147437Sume		return;
470147437Sume	in6_fillscopeid(&in6_ifr.ifr_addr);
471146187Sume	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
472146187Sume	    NI_NUMERICHOST) != 0)
473138593Ssam		dst[0] = '\0';
474138593Ssam
475138593Ssam	printf("\ttunnel inet6 %s --> %s\n", src, dst);
476138593Ssam}
477138593Ssam
478138593Ssamstatic void
479138593Ssamin6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
480138593Ssam{
481138593Ssam	struct in6_aliasreq in6_addreq;
482138593Ssam
483138593Ssam	memset(&in6_addreq, 0, sizeof(in6_addreq));
484138593Ssam	strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
485138593Ssam	memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
486138593Ssam	memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
487138593Ssam	    dstres->ai_addr->sa_len);
488138593Ssam
489138593Ssam	if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
490138593Ssam		warn("SIOCSIFPHYADDR_IN6");
491138593Ssam}
492138593Ssam
493138593Ssamstatic struct cmd inet6_cmds[] = {
494138593Ssam	DEF_CMD_ARG("prefixlen",			setifprefixlen),
495138593Ssam	DEF_CMD("anycast",	IN6_IFF_ANYCAST,	setip6flags),
496138593Ssam	DEF_CMD("tentative",	IN6_IFF_TENTATIVE,	setip6flags),
497138593Ssam	DEF_CMD("-tentative",	-IN6_IFF_TENTATIVE,	setip6flags),
498138593Ssam	DEF_CMD("deprecated",	IN6_IFF_DEPRECATED,	setip6flags),
499138593Ssam	DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED,	setip6flags),
500138593Ssam	DEF_CMD("autoconf",	IN6_IFF_AUTOCONF,	setip6flags),
501138593Ssam	DEF_CMD("-autoconf",	-IN6_IFF_AUTOCONF,	setip6flags),
502138593Ssam	DEF_CMD_ARG("pltime",        			setip6pltime),
503138593Ssam	DEF_CMD_ARG("vltime",        			setip6vltime),
504138593Ssam	DEF_CMD("eui64",	0,			setip6eui64),
505138593Ssam};
506138593Ssam
507138593Ssamstatic struct afswtch af_inet6 = {
508138593Ssam	.af_name	= "inet6",
509138593Ssam	.af_af		= AF_INET6,
510138593Ssam	.af_status	= in6_status,
511138593Ssam	.af_getaddr	= in6_getaddr,
512138593Ssam	.af_getprefix	= in6_getprefix,
513138593Ssam	.af_postproc	= in6_postproc,
514138593Ssam	.af_status_tunnel = in6_status_tunnel,
515138593Ssam	.af_settunnel	= in6_set_tunnel,
516138593Ssam	.af_difaddr	= SIOCDIFADDR_IN6,
517138593Ssam	.af_aifaddr	= SIOCAIFADDR_IN6,
518166446Sbms	.af_ridreq	= &in6_addreq,
519138593Ssam	.af_addreq	= &in6_addreq,
520138593Ssam};
521138593Ssam
522138593Ssamstatic void
523138593Ssamin6_Lopt_cb(const char *optarg __unused)
524138593Ssam{
525138593Ssam	ip6lifetime++;	/* print IPv6 address lifetime */
526138593Ssam}
527138593Ssamstatic struct option in6_Lopt = { "L", "[-L]", in6_Lopt_cb };
528138593Ssam
529138593Ssamstatic __constructor void
530138593Ssaminet6_ctor(void)
531138593Ssam{
532138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
533138593Ssam	int i;
534138593Ssam
535138593Ssam	for (i = 0; i < N(inet6_cmds);  i++)
536138593Ssam		cmd_register(&inet6_cmds[i]);
537138593Ssam	af_register(&af_inet6);
538138593Ssam	opt_register(&in6_Lopt);
539138593Ssam#undef N
540138593Ssam}
541