xref: /freebsd/sbin/ifconfig/af_inet6.c (revision ec0e626bafb335b30c499d06066997f54b10c092)
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  * 4. 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 <net/if_var.h>		/* for struct ifaddr */
52 #include <netinet/in_var.h>
53 #include <arpa/inet.h>
54 #include <netdb.h>
55 
56 #include <netinet6/nd6.h>	/* Define ND6_INFINITE_LIFETIME */
57 
58 #include "ifconfig.h"
59 
60 static	struct in6_ifreq in6_ridreq;
61 static	struct in6_aliasreq in6_addreq =
62   { .ifra_flags = 0,
63     .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
64 static	int ip6lifetime;
65 
66 static	int prefix(void *, int);
67 static	char *sec2str(time_t);
68 static	int explicit_prefix = 0;
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[MAXHOSTNAMELEN *2 + 1];	/*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;
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 	strncpy(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 	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
211 			    sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
212 	if (error != 0)
213 		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
214 			  sizeof(addr_buf));
215 	printf("\tinet6 %s ", addr_buf);
216 
217 	if (ifa->ifa_flags & IFF_POINTOPOINT) {
218 		sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
219 		/*
220 		 * some of the interfaces do not have valid destination
221 		 * address.
222 		 */
223 		if (sin != NULL && sin->sin6_family == AF_INET6) {
224 			int error;
225 
226 			error = getnameinfo((struct sockaddr *)sin,
227 					    sin->sin6_len, addr_buf,
228 					    sizeof(addr_buf), NULL, 0,
229 					    NI_NUMERICHOST);
230 			if (error != 0)
231 				inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
232 					  sizeof(addr_buf));
233 			printf("--> %s ", addr_buf);
234 		}
235 	}
236 
237 	sin = (struct sockaddr_in6 *)ifa->ifa_netmask;
238 	if (sin == NULL)
239 		sin = &null_sin;
240 	printf("prefixlen %d ", prefix(&sin->sin6_addr,
241 		sizeof(struct in6_addr)));
242 
243 	if ((flags6 & IN6_IFF_ANYCAST) != 0)
244 		printf("anycast ");
245 	if ((flags6 & IN6_IFF_TENTATIVE) != 0)
246 		printf("tentative ");
247 	if ((flags6 & IN6_IFF_DUPLICATED) != 0)
248 		printf("duplicated ");
249 	if ((flags6 & IN6_IFF_DETACHED) != 0)
250 		printf("detached ");
251 	if ((flags6 & IN6_IFF_DEPRECATED) != 0)
252 		printf("deprecated ");
253 	if ((flags6 & IN6_IFF_AUTOCONF) != 0)
254 		printf("autoconf ");
255 	if ((flags6 & IN6_IFF_TEMPORARY) != 0)
256 		printf("temporary ");
257 	if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0)
258 		printf("prefer_source ");
259 
260 	if (((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id)
261 		printf("scopeid 0x%x ",
262 		    ((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id);
263 
264 	if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
265 		printf("pltime ");
266 		if (lifetime.ia6t_preferred) {
267 			printf("%s ", lifetime.ia6t_preferred < now.tv_sec
268 			    ? "0" :
269 			    sec2str(lifetime.ia6t_preferred - now.tv_sec));
270 		} else
271 			printf("infty ");
272 
273 		printf("vltime ");
274 		if (lifetime.ia6t_expire) {
275 			printf("%s ", lifetime.ia6t_expire < now.tv_sec
276 			    ? "0" :
277 			    sec2str(lifetime.ia6t_expire - now.tv_sec));
278 		} else
279 			printf("infty ");
280 	}
281 
282 	print_vhid(ifa, " ");
283 
284 	putchar('\n');
285 }
286 
287 #define	SIN6(x) ((struct sockaddr_in6 *) &(x))
288 static struct	sockaddr_in6 *sin6tab[] = {
289 	SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
290 	SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)
291 };
292 
293 static void
294 in6_getprefix(const char *plen, int which)
295 {
296 	struct sockaddr_in6 *sin = sin6tab[which];
297 	u_char *cp;
298 	int len = atoi(plen);
299 
300 	if ((len < 0) || (len > 128))
301 		errx(1, "%s: bad value", plen);
302 	sin->sin6_len = sizeof(*sin);
303 	if (which != MASK)
304 		sin->sin6_family = AF_INET6;
305 	if ((len == 0) || (len == 128)) {
306 		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
307 		return;
308 	}
309 	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
310 	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
311 		*cp++ = 0xff;
312 	*cp = 0xff << (8 - len);
313 }
314 
315 static void
316 in6_getaddr(const char *s, int which)
317 {
318 	struct sockaddr_in6 *sin = sin6tab[which];
319 	struct addrinfo hints, *res;
320 	int error = -1;
321 
322 	newaddr &= 1;
323 
324 	sin->sin6_len = sizeof(*sin);
325 	if (which != MASK)
326 		sin->sin6_family = AF_INET6;
327 
328 	if (which == ADDR) {
329 		char *p = NULL;
330 		if((p = strrchr(s, '/')) != NULL) {
331 			*p = '\0';
332 			in6_getprefix(p + 1, MASK);
333 			explicit_prefix = 1;
334 		}
335 	}
336 
337 	if (sin->sin6_family == AF_INET6) {
338 		bzero(&hints, sizeof(struct addrinfo));
339 		hints.ai_family = AF_INET6;
340 		error = getaddrinfo(s, NULL, &hints, &res);
341 	}
342 	if (error != 0) {
343 		if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
344 			errx(1, "%s: bad value", s);
345 	} else
346 		bcopy(res->ai_addr, sin, res->ai_addrlen);
347 }
348 
349 static int
350 prefix(void *val, int size)
351 {
352 	u_char *name = (u_char *)val;
353 	int byte, bit, plen = 0;
354 
355 	for (byte = 0; byte < size; byte++, plen += 8)
356 		if (name[byte] != 0xff)
357 			break;
358 	if (byte == size)
359 		return (plen);
360 	for (bit = 7; bit != 0; bit--, plen++)
361 		if (!(name[byte] & (1 << bit)))
362 			break;
363 	for (; bit != 0; bit--)
364 		if (name[byte] & (1 << bit))
365 			return(0);
366 	byte++;
367 	for (; byte < size; byte++)
368 		if (name[byte])
369 			return(0);
370 	return (plen);
371 }
372 
373 static char *
374 sec2str(time_t total)
375 {
376 	static char result[256];
377 	int days, hours, mins, secs;
378 	int first = 1;
379 	char *p = result;
380 
381 	if (0) {
382 		days = total / 3600 / 24;
383 		hours = (total / 3600) % 24;
384 		mins = (total / 60) % 60;
385 		secs = total % 60;
386 
387 		if (days) {
388 			first = 0;
389 			p += sprintf(p, "%dd", days);
390 		}
391 		if (!first || hours) {
392 			first = 0;
393 			p += sprintf(p, "%dh", hours);
394 		}
395 		if (!first || mins) {
396 			first = 0;
397 			p += sprintf(p, "%dm", mins);
398 		}
399 		sprintf(p, "%ds", secs);
400 	} else
401 		sprintf(result, "%lu", (unsigned long)total);
402 
403 	return(result);
404 }
405 
406 static void
407 in6_postproc(int s, const struct afswtch *afp)
408 {
409 	if (explicit_prefix == 0) {
410 		/* Aggregatable address architecture defines all prefixes
411 		   are 64. So, it is convenient to set prefixlen to 64 if
412 		   it is not specified. */
413 		setifprefixlen("64", 0, s, afp);
414 		/* in6_getprefix("64", MASK) if MASK is available here... */
415 	}
416 }
417 
418 static void
419 in6_status_tunnel(int s)
420 {
421 	char src[NI_MAXHOST];
422 	char dst[NI_MAXHOST];
423 	struct in6_ifreq in6_ifr;
424 	const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
425 
426 	memset(&in6_ifr, 0, sizeof(in6_ifr));
427 	strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
428 
429 	if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
430 		return;
431 	if (sa->sa_family != AF_INET6)
432 		return;
433 	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
434 	    NI_NUMERICHOST) != 0)
435 		src[0] = '\0';
436 
437 	if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
438 		return;
439 	if (sa->sa_family != AF_INET6)
440 		return;
441 	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
442 	    NI_NUMERICHOST) != 0)
443 		dst[0] = '\0';
444 
445 	printf("\ttunnel inet6 %s --> %s\n", src, dst);
446 }
447 
448 static void
449 in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
450 {
451 	struct in6_aliasreq in6_addreq;
452 
453 	memset(&in6_addreq, 0, sizeof(in6_addreq));
454 	strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
455 	memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
456 	memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
457 	    dstres->ai_addr->sa_len);
458 
459 	if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
460 		warn("SIOCSIFPHYADDR_IN6");
461 }
462 
463 static struct cmd inet6_cmds[] = {
464 	DEF_CMD_ARG("prefixlen",			setifprefixlen),
465 	DEF_CMD("anycast",	IN6_IFF_ANYCAST,	setip6flags),
466 	DEF_CMD("tentative",	IN6_IFF_TENTATIVE,	setip6flags),
467 	DEF_CMD("-tentative",	-IN6_IFF_TENTATIVE,	setip6flags),
468 	DEF_CMD("deprecated",	IN6_IFF_DEPRECATED,	setip6flags),
469 	DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED,	setip6flags),
470 	DEF_CMD("autoconf",	IN6_IFF_AUTOCONF,	setip6flags),
471 	DEF_CMD("-autoconf",	-IN6_IFF_AUTOCONF,	setip6flags),
472 	DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE,	setip6flags),
473 	DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
474 	DEF_CMD("accept_rtadv",	ND6_IFF_ACCEPT_RTADV,	setnd6flags),
475 	DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV,	setnd6flags),
476 	DEF_CMD("no_radr",	ND6_IFF_NO_RADR,	setnd6flags),
477 	DEF_CMD("-no_radr",	-ND6_IFF_NO_RADR,	setnd6flags),
478 	DEF_CMD("defaultif",	1,			setnd6defif),
479 	DEF_CMD("-defaultif",	-1,			setnd6defif),
480 	DEF_CMD("ifdisabled",	ND6_IFF_IFDISABLED,	setnd6flags),
481 	DEF_CMD("-ifdisabled",	-ND6_IFF_IFDISABLED,	setnd6flags),
482 	DEF_CMD("nud",		ND6_IFF_PERFORMNUD,	setnd6flags),
483 	DEF_CMD("-nud",		-ND6_IFF_PERFORMNUD,	setnd6flags),
484 	DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
485 	DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
486 	DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
487 	DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
488 	DEF_CMD("no_dad",	ND6_IFF_NO_DAD,		setnd6flags),
489 	DEF_CMD("-no_dad",	-ND6_IFF_NO_DAD,	setnd6flags),
490 	DEF_CMD("ignoreloop",	ND6_IFF_IGNORELOOP,	setnd6flags),
491 	DEF_CMD("-ignoreloop",	-ND6_IFF_IGNORELOOP,	setnd6flags),
492 	DEF_CMD_ARG("pltime",        			setip6pltime),
493 	DEF_CMD_ARG("vltime",        			setip6vltime),
494 	DEF_CMD("eui64",	0,			setip6eui64),
495 };
496 
497 static struct afswtch af_inet6 = {
498 	.af_name	= "inet6",
499 	.af_af		= AF_INET6,
500 	.af_status	= in6_status,
501 	.af_getaddr	= in6_getaddr,
502 	.af_getprefix	= in6_getprefix,
503 	.af_other_status = nd6_status,
504 	.af_postproc	= in6_postproc,
505 	.af_status_tunnel = in6_status_tunnel,
506 	.af_settunnel	= in6_set_tunnel,
507 	.af_difaddr	= SIOCDIFADDR_IN6,
508 	.af_aifaddr	= SIOCAIFADDR_IN6,
509 	.af_ridreq	= &in6_addreq,
510 	.af_addreq	= &in6_addreq,
511 };
512 
513 static void
514 in6_Lopt_cb(const char *optarg __unused)
515 {
516 	ip6lifetime++;	/* print IPv6 address lifetime */
517 }
518 static struct option in6_Lopt = {
519 	.opt = "L",
520 	.opt_usage = "[-L]",
521 	.cb = in6_Lopt_cb
522 };
523 
524 static __constructor void
525 inet6_ctor(void)
526 {
527 #define	N(a)	(sizeof(a) / sizeof(a[0]))
528 	size_t i;
529 
530 #ifndef RESCUE
531 	if (!feature_present("inet6"))
532 		return;
533 #endif
534 
535 	for (i = 0; i < N(inet6_cmds);  i++)
536 		cmd_register(&inet6_cmds[i]);
537 	af_register(&af_inet6);
538 	opt_register(&in6_Lopt);
539 #undef N
540 }
541