xref: /freebsd/sbin/ifconfig/af_inet6.c (revision b0b1dbdd)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char rcsid[] =
32   "$FreeBSD$";
33 #endif /* not lint */
34 
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <net/if.h>
39 
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <time.h>
46 #include <ifaddrs.h>
47 
48 #include <arpa/inet.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <arpa/inet.h>
53 #include <netdb.h>
54 
55 #include <netinet6/nd6.h>	/* Define ND6_INFINITE_LIFETIME */
56 
57 #include "ifconfig.h"
58 
59 static	struct in6_ifreq in6_ridreq;
60 static	struct in6_aliasreq in6_addreq =
61   { .ifra_flags = 0,
62     .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
63 static	int ip6lifetime;
64 
65 static	int prefix(void *, int);
66 static	char *sec2str(time_t);
67 static	int explicit_prefix = 0;
68 extern	char *f_inet6, *f_addr;
69 
70 extern void setnd6flags(const char *, int, int, const struct afswtch *);
71 extern void setnd6defif(const char *, int, int, const struct afswtch *);
72 extern void nd6_status(int);
73 
74 static	char addr_buf[NI_MAXHOST];	/*for getnameinfo()*/
75 
76 static void
77 setifprefixlen(const char *addr, int dummy __unused, int s,
78     const struct afswtch *afp)
79 {
80         if (afp->af_getprefix != NULL)
81                 afp->af_getprefix(addr, MASK);
82 	explicit_prefix = 1;
83 }
84 
85 static void
86 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
87     const struct afswtch *afp)
88 {
89 	if (afp->af_af != AF_INET6)
90 		err(1, "address flags can be set only for inet6 addresses");
91 
92 	if (flag < 0)
93 		in6_addreq.ifra_flags &= ~(-flag);
94 	else
95 		in6_addreq.ifra_flags |= flag;
96 }
97 
98 static void
99 setip6lifetime(const char *cmd, const char *val, int s,
100     const struct afswtch *afp)
101 {
102 	struct timespec now;
103 	time_t newval;
104 	char *ep;
105 
106 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
107 	newval = (time_t)strtoul(val, &ep, 0);
108 	if (val == ep)
109 		errx(1, "invalid %s", cmd);
110 	if (afp->af_af != AF_INET6)
111 		errx(1, "%s not allowed for the AF", cmd);
112 	if (strcmp(cmd, "vltime") == 0) {
113 		in6_addreq.ifra_lifetime.ia6t_expire = now.tv_sec + newval;
114 		in6_addreq.ifra_lifetime.ia6t_vltime = newval;
115 	} else if (strcmp(cmd, "pltime") == 0) {
116 		in6_addreq.ifra_lifetime.ia6t_preferred = now.tv_sec + newval;
117 		in6_addreq.ifra_lifetime.ia6t_pltime = newval;
118 	}
119 }
120 
121 static void
122 setip6pltime(const char *seconds, int dummy __unused, int s,
123     const struct afswtch *afp)
124 {
125 	setip6lifetime("pltime", seconds, s, afp);
126 }
127 
128 static void
129 setip6vltime(const char *seconds, int dummy __unused, int s,
130     const struct afswtch *afp)
131 {
132 	setip6lifetime("vltime", seconds, s, afp);
133 }
134 
135 static void
136 setip6eui64(const char *cmd, int dummy __unused, int s,
137     const struct afswtch *afp)
138 {
139 	struct ifaddrs *ifap, *ifa;
140 	const struct sockaddr_in6 *sin6 = NULL;
141 	const struct in6_addr *lladdr = NULL;
142 	struct in6_addr *in6;
143 
144 	if (afp->af_af != AF_INET6)
145 		errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
146  	in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
147 	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
148 		errx(EXIT_FAILURE, "interface index is already filled");
149 	if (getifaddrs(&ifap) != 0)
150 		err(EXIT_FAILURE, "getifaddrs");
151 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
152 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
153 		    strcmp(ifa->ifa_name, name) == 0) {
154 			sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
155 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
156 				lladdr = &sin6->sin6_addr;
157 				break;
158 			}
159 		}
160 	}
161 	if (!lladdr)
162 		errx(EXIT_FAILURE, "could not determine link local address");
163 
164  	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
165 
166 	freeifaddrs(ifap);
167 }
168 
169 static void
170 in6_status(int s __unused, const struct ifaddrs *ifa)
171 {
172 	struct sockaddr_in6 *sin, null_sin;
173 	struct in6_ifreq ifr6;
174 	int s6;
175 	u_int32_t flags6;
176 	struct in6_addrlifetime lifetime;
177 	struct timespec now;
178 	int error, n_flags;
179 
180 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
181 
182 	memset(&null_sin, 0, sizeof(null_sin));
183 
184 	sin = (struct sockaddr_in6 *)ifa->ifa_addr;
185 	if (sin == NULL)
186 		return;
187 
188 	strlcpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
189 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
190 		warn("socket(AF_INET6,SOCK_DGRAM)");
191 		return;
192 	}
193 	ifr6.ifr_addr = *sin;
194 	if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
195 		warn("ioctl(SIOCGIFAFLAG_IN6)");
196 		close(s6);
197 		return;
198 	}
199 	flags6 = ifr6.ifr_ifru.ifru_flags6;
200 	memset(&lifetime, 0, sizeof(lifetime));
201 	ifr6.ifr_addr = *sin;
202 	if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
203 		warn("ioctl(SIOCGIFALIFETIME_IN6)");
204 		close(s6);
205 		return;
206 	}
207 	lifetime = ifr6.ifr_ifru.ifru_lifetime;
208 	close(s6);
209 
210 	if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
211 		n_flags = 0;
212 	else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
213 		n_flags = NI_NOFQDN;
214 	else
215 		n_flags = NI_NUMERICHOST;
216 	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len,
217 			    addr_buf, sizeof(addr_buf), NULL, 0,
218 			    n_flags);
219 	if (error != 0)
220 		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
221 			  sizeof(addr_buf));
222 	printf("\tinet6 %s", addr_buf);
223 
224 	if (ifa->ifa_flags & IFF_POINTOPOINT) {
225 		sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
226 		/*
227 		 * some of the interfaces do not have valid destination
228 		 * address.
229 		 */
230 		if (sin != NULL && sin->sin6_family == AF_INET6) {
231 			int error;
232 
233 			error = getnameinfo((struct sockaddr *)sin,
234 					    sin->sin6_len, addr_buf,
235 					    sizeof(addr_buf), NULL, 0,
236 					    NI_NUMERICHOST);
237 			if (error != 0)
238 				inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
239 					  sizeof(addr_buf));
240 			printf(" --> %s", addr_buf);
241 		}
242 	}
243 
244 	sin = (struct sockaddr_in6 *)ifa->ifa_netmask;
245 	if (sin == NULL)
246 		sin = &null_sin;
247 	if (f_inet6 != NULL && strcmp(f_inet6, "cidr") == 0)
248 		printf("/%d ", prefix(&sin->sin6_addr,
249 			sizeof(struct in6_addr)));
250 	else
251 		printf(" prefixlen %d ", prefix(&sin->sin6_addr,
252 			sizeof(struct in6_addr)));
253 
254 	if ((flags6 & IN6_IFF_ANYCAST) != 0)
255 		printf("anycast ");
256 	if ((flags6 & IN6_IFF_TENTATIVE) != 0)
257 		printf("tentative ");
258 	if ((flags6 & IN6_IFF_DUPLICATED) != 0)
259 		printf("duplicated ");
260 	if ((flags6 & IN6_IFF_DETACHED) != 0)
261 		printf("detached ");
262 	if ((flags6 & IN6_IFF_DEPRECATED) != 0)
263 		printf("deprecated ");
264 	if ((flags6 & IN6_IFF_AUTOCONF) != 0)
265 		printf("autoconf ");
266 	if ((flags6 & IN6_IFF_TEMPORARY) != 0)
267 		printf("temporary ");
268 	if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0)
269 		printf("prefer_source ");
270 
271 	if (((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id)
272 		printf("scopeid 0x%x ",
273 		    ((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id);
274 
275 	if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
276 		printf("pltime ");
277 		if (lifetime.ia6t_preferred) {
278 			printf("%s ", lifetime.ia6t_preferred < now.tv_sec
279 			    ? "0" :
280 			    sec2str(lifetime.ia6t_preferred - now.tv_sec));
281 		} else
282 			printf("infty ");
283 
284 		printf("vltime ");
285 		if (lifetime.ia6t_expire) {
286 			printf("%s ", lifetime.ia6t_expire < now.tv_sec
287 			    ? "0" :
288 			    sec2str(lifetime.ia6t_expire - now.tv_sec));
289 		} else
290 			printf("infty ");
291 	}
292 
293 	print_vhid(ifa, " ");
294 
295 	putchar('\n');
296 }
297 
298 #define	SIN6(x) ((struct sockaddr_in6 *) &(x))
299 static struct	sockaddr_in6 *sin6tab[] = {
300 	SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
301 	SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)
302 };
303 
304 static void
305 in6_getprefix(const char *plen, int which)
306 {
307 	struct sockaddr_in6 *sin = sin6tab[which];
308 	u_char *cp;
309 	int len = atoi(plen);
310 
311 	if ((len < 0) || (len > 128))
312 		errx(1, "%s: bad value", plen);
313 	sin->sin6_len = sizeof(*sin);
314 	if (which != MASK)
315 		sin->sin6_family = AF_INET6;
316 	if ((len == 0) || (len == 128)) {
317 		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
318 		return;
319 	}
320 	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
321 	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
322 		*cp++ = 0xff;
323 	*cp = 0xff << (8 - len);
324 }
325 
326 static void
327 in6_getaddr(const char *s, int which)
328 {
329 	struct sockaddr_in6 *sin = sin6tab[which];
330 	struct addrinfo hints, *res;
331 	int error = -1;
332 
333 	newaddr &= 1;
334 
335 	sin->sin6_len = sizeof(*sin);
336 	if (which != MASK)
337 		sin->sin6_family = AF_INET6;
338 
339 	if (which == ADDR) {
340 		char *p = NULL;
341 		if((p = strrchr(s, '/')) != NULL) {
342 			*p = '\0';
343 			in6_getprefix(p + 1, MASK);
344 			explicit_prefix = 1;
345 		}
346 	}
347 
348 	if (sin->sin6_family == AF_INET6) {
349 		bzero(&hints, sizeof(struct addrinfo));
350 		hints.ai_family = AF_INET6;
351 		error = getaddrinfo(s, NULL, &hints, &res);
352 	}
353 	if (error != 0) {
354 		if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
355 			errx(1, "%s: bad value", s);
356 	} else
357 		bcopy(res->ai_addr, sin, res->ai_addrlen);
358 }
359 
360 static int
361 prefix(void *val, int size)
362 {
363 	u_char *name = (u_char *)val;
364 	int byte, bit, plen = 0;
365 
366 	for (byte = 0; byte < size; byte++, plen += 8)
367 		if (name[byte] != 0xff)
368 			break;
369 	if (byte == size)
370 		return (plen);
371 	for (bit = 7; bit != 0; bit--, plen++)
372 		if (!(name[byte] & (1 << bit)))
373 			break;
374 	for (; bit != 0; bit--)
375 		if (name[byte] & (1 << bit))
376 			return(0);
377 	byte++;
378 	for (; byte < size; byte++)
379 		if (name[byte])
380 			return(0);
381 	return (plen);
382 }
383 
384 static char *
385 sec2str(time_t total)
386 {
387 	static char result[256];
388 	int days, hours, mins, secs;
389 	int first = 1;
390 	char *p = result;
391 
392 	if (0) {
393 		days = total / 3600 / 24;
394 		hours = (total / 3600) % 24;
395 		mins = (total / 60) % 60;
396 		secs = total % 60;
397 
398 		if (days) {
399 			first = 0;
400 			p += sprintf(p, "%dd", days);
401 		}
402 		if (!first || hours) {
403 			first = 0;
404 			p += sprintf(p, "%dh", hours);
405 		}
406 		if (!first || mins) {
407 			first = 0;
408 			p += sprintf(p, "%dm", mins);
409 		}
410 		sprintf(p, "%ds", secs);
411 	} else
412 		sprintf(result, "%lu", (unsigned long)total);
413 
414 	return(result);
415 }
416 
417 static void
418 in6_postproc(int s, const struct afswtch *afp)
419 {
420 	if (explicit_prefix == 0) {
421 		/* Aggregatable address architecture defines all prefixes
422 		   are 64. So, it is convenient to set prefixlen to 64 if
423 		   it is not specified. */
424 		setifprefixlen("64", 0, s, afp);
425 		/* in6_getprefix("64", MASK) if MASK is available here... */
426 	}
427 }
428 
429 static void
430 in6_status_tunnel(int s)
431 {
432 	char src[NI_MAXHOST];
433 	char dst[NI_MAXHOST];
434 	struct in6_ifreq in6_ifr;
435 	const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
436 
437 	memset(&in6_ifr, 0, sizeof(in6_ifr));
438 	strlcpy(in6_ifr.ifr_name, name, sizeof(in6_ifr.ifr_name));
439 
440 	if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
441 		return;
442 	if (sa->sa_family != AF_INET6)
443 		return;
444 	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
445 	    NI_NUMERICHOST) != 0)
446 		src[0] = '\0';
447 
448 	if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
449 		return;
450 	if (sa->sa_family != AF_INET6)
451 		return;
452 	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
453 	    NI_NUMERICHOST) != 0)
454 		dst[0] = '\0';
455 
456 	printf("\ttunnel inet6 %s --> %s\n", src, dst);
457 }
458 
459 static void
460 in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
461 {
462 	struct in6_aliasreq in6_addreq;
463 
464 	memset(&in6_addreq, 0, sizeof(in6_addreq));
465 	strlcpy(in6_addreq.ifra_name, name, sizeof(in6_addreq.ifra_name));
466 	memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
467 	memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
468 	    dstres->ai_addr->sa_len);
469 
470 	if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
471 		warn("SIOCSIFPHYADDR_IN6");
472 }
473 
474 static struct cmd inet6_cmds[] = {
475 	DEF_CMD_ARG("prefixlen",			setifprefixlen),
476 	DEF_CMD("anycast",	IN6_IFF_ANYCAST,	setip6flags),
477 	DEF_CMD("tentative",	IN6_IFF_TENTATIVE,	setip6flags),
478 	DEF_CMD("-tentative",	-IN6_IFF_TENTATIVE,	setip6flags),
479 	DEF_CMD("deprecated",	IN6_IFF_DEPRECATED,	setip6flags),
480 	DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED,	setip6flags),
481 	DEF_CMD("autoconf",	IN6_IFF_AUTOCONF,	setip6flags),
482 	DEF_CMD("-autoconf",	-IN6_IFF_AUTOCONF,	setip6flags),
483 	DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE,	setip6flags),
484 	DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
485 	DEF_CMD("accept_rtadv",	ND6_IFF_ACCEPT_RTADV,	setnd6flags),
486 	DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV,	setnd6flags),
487 	DEF_CMD("no_radr",	ND6_IFF_NO_RADR,	setnd6flags),
488 	DEF_CMD("-no_radr",	-ND6_IFF_NO_RADR,	setnd6flags),
489 	DEF_CMD("defaultif",	1,			setnd6defif),
490 	DEF_CMD("-defaultif",	-1,			setnd6defif),
491 	DEF_CMD("ifdisabled",	ND6_IFF_IFDISABLED,	setnd6flags),
492 	DEF_CMD("-ifdisabled",	-ND6_IFF_IFDISABLED,	setnd6flags),
493 	DEF_CMD("nud",		ND6_IFF_PERFORMNUD,	setnd6flags),
494 	DEF_CMD("-nud",		-ND6_IFF_PERFORMNUD,	setnd6flags),
495 	DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
496 	DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
497 	DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
498 	DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
499 	DEF_CMD("no_dad",	ND6_IFF_NO_DAD,		setnd6flags),
500 	DEF_CMD("-no_dad",	-ND6_IFF_NO_DAD,	setnd6flags),
501 	DEF_CMD_ARG("pltime",        			setip6pltime),
502 	DEF_CMD_ARG("vltime",        			setip6vltime),
503 	DEF_CMD("eui64",	0,			setip6eui64),
504 };
505 
506 static struct afswtch af_inet6 = {
507 	.af_name	= "inet6",
508 	.af_af		= AF_INET6,
509 	.af_status	= in6_status,
510 	.af_getaddr	= in6_getaddr,
511 	.af_getprefix	= in6_getprefix,
512 	.af_other_status = nd6_status,
513 	.af_postproc	= in6_postproc,
514 	.af_status_tunnel = in6_status_tunnel,
515 	.af_settunnel	= in6_set_tunnel,
516 	.af_difaddr	= SIOCDIFADDR_IN6,
517 	.af_aifaddr	= SIOCAIFADDR_IN6,
518 	.af_ridreq	= &in6_addreq,
519 	.af_addreq	= &in6_addreq,
520 };
521 
522 static void
523 in6_Lopt_cb(const char *optarg __unused)
524 {
525 	ip6lifetime++;	/* print IPv6 address lifetime */
526 }
527 static struct option in6_Lopt = {
528 	.opt = "L",
529 	.opt_usage = "[-L]",
530 	.cb = in6_Lopt_cb
531 };
532 
533 static __constructor void
534 inet6_ctor(void)
535 {
536 	size_t i;
537 
538 #ifndef RESCUE
539 	if (!feature_present("inet6"))
540 		return;
541 #endif
542 
543 	for (i = 0; i < nitems(inet6_cmds);  i++)
544 		cmd_register(&inet6_cmds[i]);
545 	af_register(&af_inet6);
546 	opt_register(&in6_Lopt);
547 }
548