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