xref: /freebsd/sbin/ifconfig/af_inet6.c (revision b306c604df541dede4d0f3cc96188bbf5b6719fe)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char rcsid[] =
34   "$FreeBSD$";
35 #endif /* not lint */
36 
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <net/if.h>
41 
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <time.h>
48 #include <ifaddrs.h>
49 
50 #include <arpa/inet.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <arpa/inet.h>
55 #include <netdb.h>
56 
57 #include <netinet6/nd6.h>	/* Define ND6_INFINITE_LIFETIME */
58 
59 #include "ifconfig.h"
60 #include "ifconfig_netlink.h"
61 
62 #ifndef WITHOUT_NETLINK
63 struct in6_px {
64 	struct in6_addr		addr;
65 	int			plen;
66 	bool			set;
67 };
68 struct in6_pdata {
69 	struct in6_px		addr;
70 	struct in6_px		dst_addr;
71 	struct in6_addrlifetime	lifetime;
72 	uint32_t		flags;
73 	uint32_t		vhid;
74 };
75 
76 static struct in6_pdata in6_del;
77 static struct in6_pdata in6_add = {
78 	.lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME },
79 };
80 #else
81 static	struct in6_ifreq in6_ridreq;
82 static	struct in6_aliasreq in6_addreq =
83   { .ifra_flags = 0,
84     .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
85 #endif
86 static	int ip6lifetime;
87 
88 #ifdef WITHOUT_NETLINK
89 static	int prefix(void *, int);
90 #endif
91 static	char *sec2str(time_t);
92 static	int explicit_prefix = 0;
93 extern	char *f_inet6, *f_addr;
94 
95 extern void setnd6flags(if_ctx *, const char *, int);
96 extern void setnd6defif(if_ctx *,const char *, int);
97 extern void nd6_status(if_ctx *);
98 
99 static	char addr_buf[NI_MAXHOST];	/*for getnameinfo()*/
100 
101 static void
102 setifprefixlen(if_ctx *ctx __netlink_unused, const char *addr, int dummy __unused)
103 {
104 #ifdef WITHOUT_NETLINK
105 	const struct afswtch *afp = ctx->afp;
106 
107         if (afp->af_getprefix != NULL)
108                 afp->af_getprefix(addr, MASK);
109 #else
110 	int plen = strtol(addr, NULL, 10);
111 
112 	if ((plen < 0) || (plen > 128))
113 		errx(1, "%s: bad value", addr);
114 	in6_add.addr.plen = plen;
115 #endif
116 	explicit_prefix = 1;
117 }
118 
119 static void
120 setip6flags(if_ctx *ctx, const char *dummyaddr __unused, int flag)
121 {
122 	const struct afswtch *afp = ctx->afp;
123 
124 	if (afp->af_af != AF_INET6)
125 		err(1, "address flags can be set only for inet6 addresses");
126 
127 #ifdef WITHOUT_NETLINK
128 	if (flag < 0)
129 		in6_addreq.ifra_flags &= ~(-flag);
130 	else
131 		in6_addreq.ifra_flags |= flag;
132 #else
133 	if (flag < 0)
134 		in6_add.flags &= ~(-flag);
135 	else
136 		in6_add.flags |= flag;
137 #endif
138 }
139 
140 static void
141 setip6lifetime(if_ctx *ctx, const char *cmd, const char *val)
142 {
143 	const struct afswtch *afp = ctx->afp;
144 	struct timespec now;
145 	time_t newval;
146 	char *ep;
147 #ifdef WITHOUT_NETLINK
148 	struct in6_addrlifetime *lifetime = &in6_addreq.ifra_lifetime;
149 #else
150 	struct in6_addrlifetime *lifetime = &in6_add.lifetime;
151 #endif
152 
153 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
154 	newval = (time_t)strtoul(val, &ep, 0);
155 	if (val == ep)
156 		errx(1, "invalid %s", cmd);
157 	if (afp->af_af != AF_INET6)
158 		errx(1, "%s not allowed for the AF", cmd);
159 	if (strcmp(cmd, "vltime") == 0) {
160 		lifetime->ia6t_expire = now.tv_sec + newval;
161 		lifetime->ia6t_vltime = newval;
162 	} else if (strcmp(cmd, "pltime") == 0) {
163 		lifetime->ia6t_preferred = now.tv_sec + newval;
164 		lifetime->ia6t_pltime = newval;
165 	}
166 }
167 
168 static void
169 setip6pltime(if_ctx *ctx, const char *seconds, int dummy __unused)
170 {
171 	setip6lifetime(ctx, "pltime", seconds);
172 }
173 
174 static void
175 setip6vltime(if_ctx *ctx, const char *seconds, int dummy __unused)
176 {
177 	setip6lifetime(ctx, "vltime", seconds);
178 }
179 
180 static void
181 setip6eui64(if_ctx *ctx, const char *cmd, int dummy __unused)
182 {
183 	const struct afswtch *afp = ctx->afp;
184 	struct ifaddrs *ifap, *ifa;
185 	const struct sockaddr_in6 *sin6 = NULL;
186 	const struct in6_addr *lladdr = NULL;
187 	struct in6_addr *in6;
188 
189 	if (afp->af_af != AF_INET6)
190 		errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
191 #ifdef WITHOUT_NETLINK
192  	in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
193 #else
194 	in6 = &in6_add.addr.addr;
195 #endif
196 	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
197 		errx(EXIT_FAILURE, "interface index is already filled");
198 	if (getifaddrs(&ifap) != 0)
199 		err(EXIT_FAILURE, "getifaddrs");
200 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
201 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
202 		    strcmp(ifa->ifa_name, ctx->ifname) == 0) {
203 			sin6 = (const struct sockaddr_in6 *)satosin6(ifa->ifa_addr);
204 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
205 				lladdr = &sin6->sin6_addr;
206 				break;
207 			}
208 		}
209 	}
210 	if (!lladdr)
211 		errx(EXIT_FAILURE, "could not determine link local address");
212 
213  	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
214 
215 	freeifaddrs(ifap);
216 }
217 
218 static void
219 print_addr(struct sockaddr_in6 *sin)
220 {
221 	int error, n_flags;
222 
223 	if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
224 		n_flags = 0;
225 	else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
226 		n_flags = NI_NOFQDN;
227 	else
228 		n_flags = NI_NUMERICHOST;
229 	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len,
230 			    addr_buf, sizeof(addr_buf), NULL, 0,
231 			    n_flags);
232 	if (error != 0)
233 		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
234 			  sizeof(addr_buf));
235 	printf("\tinet6 %s", addr_buf);
236 }
237 
238 static void
239 print_p2p(struct sockaddr_in6 *sin)
240 {
241 	int error;
242 
243 	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
244 	    sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
245 
246 	if (error != 0)
247 		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, sizeof(addr_buf));
248 	printf(" --> %s", addr_buf);
249 }
250 
251 static void
252 print_mask(int plen)
253 {
254 	if (f_inet6 != NULL && strcmp(f_inet6, "cidr") == 0)
255 		printf("/%d", plen);
256 	else
257 		printf(" prefixlen %d", plen);
258 }
259 
260 static void
261 print_flags(int flags6)
262 {
263 	if ((flags6 & IN6_IFF_ANYCAST) != 0)
264 		printf(" anycast");
265 	if ((flags6 & IN6_IFF_TENTATIVE) != 0)
266 		printf(" tentative");
267 	if ((flags6 & IN6_IFF_DUPLICATED) != 0)
268 		printf(" duplicated");
269 	if ((flags6 & IN6_IFF_DETACHED) != 0)
270 		printf(" detached");
271 	if ((flags6 & IN6_IFF_DEPRECATED) != 0)
272 		printf(" deprecated");
273 	if ((flags6 & IN6_IFF_AUTOCONF) != 0)
274 		printf(" autoconf");
275 	if ((flags6 & IN6_IFF_TEMPORARY) != 0)
276 		printf(" temporary");
277 	if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0)
278 		printf(" prefer_source");
279 
280 }
281 
282 static void
283 print_lifetime(const char *prepend, time_t px_time, struct timespec *now)
284 {
285 	printf(" %s", prepend);
286 	if (px_time == 0)
287 		printf(" infty");
288 
289 	printf(" %s", px_time < now->tv_sec ? "0" : sec2str(px_time - now->tv_sec));
290 }
291 
292 #ifdef WITHOUT_NETLINK
293 static void
294 in6_status(if_ctx *ctx, const struct ifaddrs *ifa)
295 {
296 	struct sockaddr_in6 *sin, null_sin = {};
297 	struct in6_ifreq ifr6;
298 	int s6;
299 	u_int32_t flags6;
300 	struct in6_addrlifetime lifetime;
301 
302 	sin = satosin6(ifa->ifa_addr);
303 	if (sin == NULL)
304 		return;
305 
306 	strlcpy(ifr6.ifr_name, ctx->ifname, sizeof(ifr6.ifr_name));
307 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
308 		warn("socket(AF_INET6,SOCK_DGRAM)");
309 		return;
310 	}
311 	ifr6.ifr_addr = *sin;
312 	if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
313 		warn("ioctl(SIOCGIFAFLAG_IN6)");
314 		close(s6);
315 		return;
316 	}
317 	flags6 = ifr6.ifr_ifru.ifru_flags6;
318 	memset(&lifetime, 0, sizeof(lifetime));
319 	ifr6.ifr_addr = *sin;
320 	if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
321 		warn("ioctl(SIOCGIFALIFETIME_IN6)");
322 		close(s6);
323 		return;
324 	}
325 	lifetime = ifr6.ifr_ifru.ifru_lifetime;
326 	close(s6);
327 
328 	print_addr(sin);
329 
330 	if (ifa->ifa_flags & IFF_POINTOPOINT) {
331 		sin = satosin6(ifa->ifa_dstaddr);
332 		/*
333 		 * some of the interfaces do not have valid destination
334 		 * address.
335 		 */
336 		if (sin != NULL && sin->sin6_family == AF_INET6)
337 			print_p2p(sin);
338 	}
339 
340 	sin = satosin6(ifa->ifa_netmask);
341 	if (sin == NULL)
342 		sin = &null_sin;
343 	print_mask(prefix(&sin->sin6_addr, sizeof(struct in6_addr)));
344 
345 	print_flags(flags6);
346 
347 	if ((satosin6(ifa->ifa_addr))->sin6_scope_id)
348 		printf(" scopeid 0x%x",
349 		    (satosin6(ifa->ifa_addr))->sin6_scope_id);
350 
351 	if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
352 		struct timespec now;
353 
354 		clock_gettime(CLOCK_MONOTONIC_FAST, &now);
355 		print_lifetime("pltime", lifetime.ia6t_preferred, &now);
356 		print_lifetime("vltime", lifetime.ia6t_expire, &now);
357 	}
358 
359 	print_vhid(ifa);
360 
361 	putchar('\n');
362 }
363 
364 #else
365 static void
366 show_lifetime(struct ifa_cacheinfo *ci)
367 {
368 	struct timespec now;
369 	uint32_t pl, vl;
370 
371 	if (ci == NULL)
372 		return;
373 
374 	int count = ci->ifa_prefered != ND6_INFINITE_LIFETIME;
375 	count += ci->ifa_valid != ND6_INFINITE_LIFETIME;
376 	if (count == 0)
377 		return;
378 
379 	pl = (ci->ifa_prefered == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_prefered;
380 	vl = (ci->ifa_valid == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_valid;
381 
382 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
383 	print_lifetime("pltime", pl + now.tv_sec, &now);
384 	print_lifetime("vltime", vl + now.tv_sec, &now);
385 }
386 
387 static void
388 in6_status_nl(if_ctx *ctx __unused, if_link_t *link __unused, if_addr_t *ifa)
389 {
390 	int plen = ifa->ifa_prefixlen;
391 	uint32_t scopeid;
392 
393 	if (ifa->ifa_local == NULL) {
394 		/* Non-P2P address */
395 		scopeid = satosin6(ifa->ifa_address)->sin6_scope_id;
396 		print_addr(satosin6(ifa->ifa_address));
397 	} else {
398 		scopeid = satosin6(ifa->ifa_local)->sin6_scope_id;
399 		print_addr(satosin6(ifa->ifa_local));
400 		print_p2p(satosin6(ifa->ifa_address));
401 	}
402 
403 	print_mask(plen);
404 	print_flags(ifa->ifaf_flags);
405 
406 	if (scopeid != 0)
407 		printf(" scopeid 0x%x", scopeid);
408 
409 	show_lifetime(ifa->ifa_cacheinfo);
410 
411 	if (ifa->ifaf_vhid != 0)
412 		printf(" vhid %d", ifa->ifaf_vhid);
413 
414 	putchar('\n');
415 }
416 
417 static struct in6_px *sin6tab_nl[] = {
418         &in6_del.addr,          /* RIDADDR */
419         &in6_add.addr,          /* ADDR */
420         NULL,                   /* MASK */
421         &in6_add.dst_addr,      /* DSTADDR*/
422 };
423 
424 static void
425 in6_copyaddr(if_ctx *ctx __unused, int to, int from)
426 {
427 	sin6tab_nl[to]->addr = sin6tab_nl[from]->addr;
428 	sin6tab_nl[to]->set = sin6tab_nl[from]->set;
429 }
430 
431 static void
432 in6_getaddr(const char *addr_str, int which)
433 {
434         struct in6_px *px = sin6tab_nl[which];
435 
436         px->set = true;
437         px->plen = 128;
438         if (which == ADDR) {
439                 char *p = NULL;
440                 if((p = strrchr(addr_str, '/')) != NULL) {
441                         *p = '\0';
442                         int plen = strtol(p + 1, NULL, 10);
443 			if (plen < 0 || plen > 128)
444                                 errx(1, "%s: bad value", p + 1);
445                         px->plen = plen;
446                         explicit_prefix = 1;
447                 }
448         }
449 
450         struct addrinfo hints = { .ai_family = AF_INET6 };
451         struct addrinfo *res;
452 
453         int error = getaddrinfo(addr_str, NULL, &hints, &res);
454         if (error != 0) {
455                 if (inet_pton(AF_INET6, addr_str, &px->addr) != 1)
456                         errx(1, "%s: bad value", addr_str);
457         } else {
458                 struct sockaddr_in6 *sin6;
459 
460                 sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
461                 px->addr = sin6->sin6_addr;
462                 freeaddrinfo(res);
463         }
464 }
465 
466 static int
467 in6_exec_nl(if_ctx *ctx, unsigned long action, void *data)
468 {
469 	struct in6_pdata *pdata = (struct in6_pdata *)data;
470 	struct snl_writer nw = {};
471 
472 	snl_init_writer(ctx->io_ss, &nw);
473 	struct nlmsghdr *hdr = snl_create_msg_request(&nw, action);
474 	struct ifaddrmsg *ifahdr = snl_reserve_msg_object(&nw, struct ifaddrmsg);
475 
476 	ifahdr->ifa_family = AF_INET6;
477 	ifahdr->ifa_prefixlen = pdata->addr.plen;
478 	ifahdr->ifa_index = if_nametoindex_nl(ctx->io_ss, ctx->ifname);
479 
480 	snl_add_msg_attr_ip6(&nw, IFA_LOCAL, &pdata->addr.addr);
481 	if (action == NL_RTM_NEWADDR && pdata->dst_addr.set)
482 		snl_add_msg_attr_ip6(&nw, IFA_ADDRESS, &pdata->dst_addr.addr);
483 
484 	struct ifa_cacheinfo ci = {
485 		.ifa_prefered = pdata->lifetime.ia6t_pltime,
486 		.ifa_valid =  pdata->lifetime.ia6t_vltime,
487 	};
488 	snl_add_msg_attr(&nw, IFA_CACHEINFO, sizeof(ci), &ci);
489 
490 	int off = snl_add_msg_attr_nested(&nw, IFA_FREEBSD);
491 	snl_add_msg_attr_u32(&nw, IFAF_FLAGS, pdata->flags);
492 	if (pdata->vhid != 0)
493 		snl_add_msg_attr_u32(&nw, IFAF_VHID, pdata->vhid);
494 	snl_end_attr_nested(&nw, off);
495 
496 	if (!snl_finalize_msg(&nw) || !snl_send_message(ctx->io_ss, hdr))
497 		return (0);
498 
499 	struct snl_errmsg_data e = {};
500 	snl_read_reply_code(ctx->io_ss, hdr->nlmsg_seq, &e);
501 
502 	return (e.error);
503 }
504 #endif
505 
506 #ifdef WITHOUT_NETLINK
507 static struct	sockaddr_in6 *sin6tab[] = {
508 	&in6_ridreq.ifr_addr, &in6_addreq.ifra_addr,
509 	&in6_addreq.ifra_prefixmask, &in6_addreq.ifra_dstaddr
510 };
511 
512 static void
513 in6_copyaddr(if_ctx *ctx __unused, int to, int from)
514 {
515 	memcpy(sin6tab[to], sin6tab[from], sizeof(struct sockaddr_in6));
516 }
517 
518 static void
519 in6_getprefix(const char *plen, int which)
520 {
521 	struct sockaddr_in6 *sin = sin6tab[which];
522 	u_char *cp;
523 	int len = atoi(plen);
524 
525 	if ((len < 0) || (len > 128))
526 		errx(1, "%s: bad value", plen);
527 	sin->sin6_len = sizeof(*sin);
528 	if (which != MASK)
529 		sin->sin6_family = AF_INET6;
530 	if ((len == 0) || (len == 128)) {
531 		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
532 		return;
533 	}
534 	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
535 	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
536 		*cp++ = 0xff;
537 	*cp = 0xff << (8 - len);
538 }
539 
540 static void
541 in6_getaddr(const char *s, int which)
542 {
543 	struct sockaddr_in6 *sin = sin6tab[which];
544 	struct addrinfo hints, *res;
545 	int error = -1;
546 
547 	sin->sin6_len = sizeof(*sin);
548 	if (which != MASK)
549 		sin->sin6_family = AF_INET6;
550 
551 	if (which == ADDR) {
552 		char *p = NULL;
553 		if((p = strrchr(s, '/')) != NULL) {
554 			*p = '\0';
555 			in6_getprefix(p + 1, MASK);
556 			explicit_prefix = 1;
557 		}
558 	}
559 
560 	if (sin->sin6_family == AF_INET6) {
561 		bzero(&hints, sizeof(struct addrinfo));
562 		hints.ai_family = AF_INET6;
563 		error = getaddrinfo(s, NULL, &hints, &res);
564 		if (error != 0) {
565 			if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
566 				errx(1, "%s: bad value", s);
567 		} else {
568 			bcopy(res->ai_addr, sin, res->ai_addrlen);
569 			freeaddrinfo(res);
570 		}
571 	}
572 }
573 
574 static int
575 prefix(void *val, int size)
576 {
577 	u_char *name = (u_char *)val;
578 	int byte, bit, plen = 0;
579 
580 	for (byte = 0; byte < size; byte++, plen += 8)
581 		if (name[byte] != 0xff)
582 			break;
583 	if (byte == size)
584 		return (plen);
585 	for (bit = 7; bit != 0; bit--, plen++)
586 		if (!(name[byte] & (1 << bit)))
587 			break;
588 	for (; bit != 0; bit--)
589 		if (name[byte] & (1 << bit))
590 			return(0);
591 	byte++;
592 	for (; byte < size; byte++)
593 		if (name[byte])
594 			return(0);
595 	return (plen);
596 }
597 #endif
598 
599 static char *
600 sec2str(time_t total)
601 {
602 	static char result[256];
603 	int days, hours, mins, secs;
604 	int first = 1;
605 	char *p = result;
606 
607 	if (0) {
608 		days = total / 3600 / 24;
609 		hours = (total / 3600) % 24;
610 		mins = (total / 60) % 60;
611 		secs = total % 60;
612 
613 		if (days) {
614 			first = 0;
615 			p += sprintf(p, "%dd", days);
616 		}
617 		if (!first || hours) {
618 			first = 0;
619 			p += sprintf(p, "%dh", hours);
620 		}
621 		if (!first || mins) {
622 			first = 0;
623 			p += sprintf(p, "%dm", mins);
624 		}
625 		sprintf(p, "%ds", secs);
626 	} else
627 		sprintf(result, "%lu", (unsigned long)total);
628 
629 	return(result);
630 }
631 
632 static void
633 in6_postproc(if_ctx *ctx, int newaddr __unused,
634     int ifflags __unused)
635 {
636 	if (explicit_prefix == 0) {
637 		/* Aggregatable address architecture defines all prefixes
638 		   are 64. So, it is convenient to set prefixlen to 64 if
639 		   it is not specified. */
640 		setifprefixlen(ctx, "64", 0);
641 		/* in6_getprefix("64", MASK) if MASK is available here... */
642 	}
643 }
644 
645 static void
646 in6_status_tunnel(if_ctx *ctx)
647 {
648 	char src[NI_MAXHOST];
649 	char dst[NI_MAXHOST];
650 	struct in6_ifreq in6_ifr;
651 	const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
652 
653 	memset(&in6_ifr, 0, sizeof(in6_ifr));
654 	strlcpy(in6_ifr.ifr_name, ctx->ifname, sizeof(in6_ifr.ifr_name));
655 
656 	if (ioctl_ctx(ctx, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
657 		return;
658 	if (sa->sa_family != AF_INET6)
659 		return;
660 	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
661 	    NI_NUMERICHOST) != 0)
662 		src[0] = '\0';
663 
664 	if (ioctl_ctx(ctx, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
665 		return;
666 	if (sa->sa_family != AF_INET6)
667 		return;
668 	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
669 	    NI_NUMERICHOST) != 0)
670 		dst[0] = '\0';
671 
672 	printf("\ttunnel inet6 %s --> %s\n", src, dst);
673 }
674 
675 static void
676 in6_set_tunnel(if_ctx *ctx, struct addrinfo *srcres, struct addrinfo *dstres)
677 {
678 	struct in6_aliasreq in6_req = {};
679 
680 	strlcpy(in6_req.ifra_name, ctx->ifname, sizeof(in6_req.ifra_name));
681 	memcpy(&in6_req.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
682 	memcpy(&in6_req.ifra_dstaddr, dstres->ai_addr,
683 	    dstres->ai_addr->sa_len);
684 
685 	if (ioctl_ctx(ctx, SIOCSIFPHYADDR_IN6, &in6_req) < 0)
686 		warn("SIOCSIFPHYADDR_IN6");
687 }
688 
689 static void
690 in6_set_vhid(int vhid)
691 {
692 #ifdef WITHOUT_NETLINK
693 	in6_addreq.ifra_vhid = vhid;
694 #else
695 	in6_add.vhid = (uint32_t)vhid;
696 #endif
697 }
698 
699 static struct cmd inet6_cmds[] = {
700 	DEF_CMD_ARG("prefixlen",			setifprefixlen),
701 	DEF_CMD("anycast",	IN6_IFF_ANYCAST,	setip6flags),
702 	DEF_CMD("tentative",	IN6_IFF_TENTATIVE,	setip6flags),
703 	DEF_CMD("-tentative",	-IN6_IFF_TENTATIVE,	setip6flags),
704 	DEF_CMD("deprecated",	IN6_IFF_DEPRECATED,	setip6flags),
705 	DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED,	setip6flags),
706 	DEF_CMD("autoconf",	IN6_IFF_AUTOCONF,	setip6flags),
707 	DEF_CMD("-autoconf",	-IN6_IFF_AUTOCONF,	setip6flags),
708 	DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE,	setip6flags),
709 	DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
710 	DEF_CMD("accept_rtadv",	ND6_IFF_ACCEPT_RTADV,	setnd6flags),
711 	DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV,	setnd6flags),
712 	DEF_CMD("no_radr",	ND6_IFF_NO_RADR,	setnd6flags),
713 	DEF_CMD("-no_radr",	-ND6_IFF_NO_RADR,	setnd6flags),
714 	DEF_CMD("defaultif",	1,			setnd6defif),
715 	DEF_CMD("-defaultif",	-1,			setnd6defif),
716 	DEF_CMD("ifdisabled",	ND6_IFF_IFDISABLED,	setnd6flags),
717 	DEF_CMD("-ifdisabled",	-ND6_IFF_IFDISABLED,	setnd6flags),
718 	DEF_CMD("nud",		ND6_IFF_PERFORMNUD,	setnd6flags),
719 	DEF_CMD("-nud",		-ND6_IFF_PERFORMNUD,	setnd6flags),
720 	DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
721 	DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
722 	DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
723 	DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
724 	DEF_CMD("no_dad",	ND6_IFF_NO_DAD,		setnd6flags),
725 	DEF_CMD("-no_dad",	-ND6_IFF_NO_DAD,	setnd6flags),
726 	DEF_CMD_ARG("pltime",        			setip6pltime),
727 	DEF_CMD_ARG("vltime",        			setip6vltime),
728 	DEF_CMD("eui64",	0,			setip6eui64),
729 #ifdef EXPERIMENTAL
730 	DEF_CMD("ipv6_only",	ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
731 	DEF_CMD("-ipv6_only",	-ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
732 #endif
733 };
734 
735 static struct afswtch af_inet6 = {
736 	.af_name	= "inet6",
737 	.af_af		= AF_INET6,
738 #ifdef WITHOUT_NETLINK
739 	.af_status	= in6_status,
740 #else
741 	.af_status	= in6_status_nl,
742 #endif
743 	.af_getaddr	= in6_getaddr,
744 	.af_copyaddr	= in6_copyaddr,
745 #ifdef WITHOUT_NETLINK
746 	.af_getprefix	= in6_getprefix,
747 #endif
748 	.af_other_status = nd6_status,
749 	.af_postproc	= in6_postproc,
750 	.af_status_tunnel = in6_status_tunnel,
751 	.af_settunnel	= in6_set_tunnel,
752 	.af_setvhid	= in6_set_vhid,
753 #ifdef WITHOUT_NETLINK
754 	.af_difaddr	= SIOCDIFADDR_IN6,
755 	.af_aifaddr	= SIOCAIFADDR_IN6,
756 	.af_ridreq	= &in6_addreq,
757 	.af_addreq	= &in6_addreq,
758 	.af_exec	= af_exec_ioctl,
759 #else
760 	.af_difaddr	= NL_RTM_DELADDR,
761 	.af_aifaddr	= NL_RTM_NEWADDR,
762 	.af_ridreq	= &in6_add,
763 	.af_addreq	= &in6_add,
764 	.af_exec	= in6_exec_nl,
765 #endif
766 };
767 
768 static void
769 in6_Lopt_cb(const char *arg __unused)
770 {
771 	ip6lifetime++;	/* print IPv6 address lifetime */
772 }
773 static struct option in6_Lopt = {
774 	.opt = "L",
775 	.opt_usage = "[-L]",
776 	.cb = in6_Lopt_cb
777 };
778 
779 static __constructor void
780 inet6_ctor(void)
781 {
782 	size_t i;
783 
784 #ifndef RESCUE
785 	if (!feature_present("inet6"))
786 		return;
787 #endif
788 
789 	for (i = 0; i < nitems(inet6_cmds);  i++)
790 		cmd_register(&inet6_cmds[i]);
791 	af_register(&af_inet6);
792 	opt_register(&in6_Lopt);
793 }
794