xref: /freebsd/sbin/ifconfig/af_inet6.c (revision 44d314f704764f0247a540648a4b4fc3e8012133)
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 <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 
69 extern void setnd6flags(const char *, int, int, const struct afswtch *);
70 extern void setnd6defif(const char *, int, int, const struct afswtch *);
71 extern void nd6_status(int);
72 
73 static	char addr_buf[MAXHOSTNAMELEN *2 + 1];	/*for getnameinfo()*/
74 
75 static void
76 setifprefixlen(const char *addr, int dummy __unused, int s,
77     const struct afswtch *afp)
78 {
79         if (afp->af_getprefix != NULL)
80                 afp->af_getprefix(addr, MASK);
81 	explicit_prefix = 1;
82 }
83 
84 static void
85 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
86     const struct afswtch *afp)
87 {
88 	if (afp->af_af != AF_INET6)
89 		err(1, "address flags can be set only for inet6 addresses");
90 
91 	if (flag < 0)
92 		in6_addreq.ifra_flags &= ~(-flag);
93 	else
94 		in6_addreq.ifra_flags |= flag;
95 }
96 
97 static void
98 setip6lifetime(const char *cmd, const char *val, int s,
99     const struct afswtch *afp)
100 {
101 	struct timespec now;
102 	time_t newval;
103 	char *ep;
104 
105 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
106 	newval = (time_t)strtoul(val, &ep, 0);
107 	if (val == ep)
108 		errx(1, "invalid %s", cmd);
109 	if (afp->af_af != AF_INET6)
110 		errx(1, "%s not allowed for the AF", cmd);
111 	if (strcmp(cmd, "vltime") == 0) {
112 		in6_addreq.ifra_lifetime.ia6t_expire = now.tv_sec + newval;
113 		in6_addreq.ifra_lifetime.ia6t_vltime = newval;
114 	} else if (strcmp(cmd, "pltime") == 0) {
115 		in6_addreq.ifra_lifetime.ia6t_preferred = now.tv_sec + newval;
116 		in6_addreq.ifra_lifetime.ia6t_pltime = newval;
117 	}
118 }
119 
120 static void
121 setip6pltime(const char *seconds, int dummy __unused, int s,
122     const struct afswtch *afp)
123 {
124 	setip6lifetime("pltime", seconds, s, afp);
125 }
126 
127 static void
128 setip6vltime(const char *seconds, int dummy __unused, int s,
129     const struct afswtch *afp)
130 {
131 	setip6lifetime("vltime", seconds, s, afp);
132 }
133 
134 static void
135 setip6eui64(const char *cmd, int dummy __unused, int s,
136     const struct afswtch *afp)
137 {
138 	struct ifaddrs *ifap, *ifa;
139 	const struct sockaddr_in6 *sin6 = NULL;
140 	const struct in6_addr *lladdr = NULL;
141 	struct in6_addr *in6;
142 
143 	if (afp->af_af != AF_INET6)
144 		errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
145  	in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
146 	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
147 		errx(EXIT_FAILURE, "interface index is already filled");
148 	if (getifaddrs(&ifap) != 0)
149 		err(EXIT_FAILURE, "getifaddrs");
150 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
151 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
152 		    strcmp(ifa->ifa_name, name) == 0) {
153 			sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
154 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
155 				lladdr = &sin6->sin6_addr;
156 				break;
157 			}
158 		}
159 	}
160 	if (!lladdr)
161 		errx(EXIT_FAILURE, "could not determine link local address");
162 
163  	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
164 
165 	freeifaddrs(ifap);
166 }
167 
168 static void
169 in6_status(int s __unused, const struct ifaddrs *ifa)
170 {
171 	struct sockaddr_in6 *sin, null_sin;
172 	struct in6_ifreq ifr6;
173 	int s6;
174 	u_int32_t flags6;
175 	struct in6_addrlifetime lifetime;
176 	struct timespec now;
177 	int error;
178 
179 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
180 
181 	memset(&null_sin, 0, sizeof(null_sin));
182 
183 	sin = (struct sockaddr_in6 *)ifa->ifa_addr;
184 	if (sin == NULL)
185 		return;
186 
187 	strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
188 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
189 		warn("socket(AF_INET6,SOCK_DGRAM)");
190 		return;
191 	}
192 	ifr6.ifr_addr = *sin;
193 	if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
194 		warn("ioctl(SIOCGIFAFLAG_IN6)");
195 		close(s6);
196 		return;
197 	}
198 	flags6 = ifr6.ifr_ifru.ifru_flags6;
199 	memset(&lifetime, 0, sizeof(lifetime));
200 	ifr6.ifr_addr = *sin;
201 	if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
202 		warn("ioctl(SIOCGIFALIFETIME_IN6)");
203 		close(s6);
204 		return;
205 	}
206 	lifetime = ifr6.ifr_ifru.ifru_lifetime;
207 	close(s6);
208 
209 	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
210 			    sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
211 	if (error != 0)
212 		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
213 			  sizeof(addr_buf));
214 	printf("\tinet6 %s ", addr_buf);
215 
216 	if (ifa->ifa_flags & IFF_POINTOPOINT) {
217 		sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
218 		/*
219 		 * some of the interfaces do not have valid destination
220 		 * address.
221 		 */
222 		if (sin != NULL && sin->sin6_family == AF_INET6) {
223 			int error;
224 
225 			error = getnameinfo((struct sockaddr *)sin,
226 					    sin->sin6_len, addr_buf,
227 					    sizeof(addr_buf), NULL, 0,
228 					    NI_NUMERICHOST);
229 			if (error != 0)
230 				inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
231 					  sizeof(addr_buf));
232 			printf("--> %s ", addr_buf);
233 		}
234 	}
235 
236 	sin = (struct sockaddr_in6 *)ifa->ifa_netmask;
237 	if (sin == NULL)
238 		sin = &null_sin;
239 	printf("prefixlen %d ", prefix(&sin->sin6_addr,
240 		sizeof(struct in6_addr)));
241 
242 	if ((flags6 & IN6_IFF_ANYCAST) != 0)
243 		printf("anycast ");
244 	if ((flags6 & IN6_IFF_TENTATIVE) != 0)
245 		printf("tentative ");
246 	if ((flags6 & IN6_IFF_DUPLICATED) != 0)
247 		printf("duplicated ");
248 	if ((flags6 & IN6_IFF_DETACHED) != 0)
249 		printf("detached ");
250 	if ((flags6 & IN6_IFF_DEPRECATED) != 0)
251 		printf("deprecated ");
252 	if ((flags6 & IN6_IFF_AUTOCONF) != 0)
253 		printf("autoconf ");
254 	if ((flags6 & IN6_IFF_TEMPORARY) != 0)
255 		printf("temporary ");
256 	if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0)
257 		printf("prefer_source ");
258 
259 	if (((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id)
260 		printf("scopeid 0x%x ",
261 		    ((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id);
262 
263 	if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
264 		printf("pltime ");
265 		if (lifetime.ia6t_preferred) {
266 			printf("%s ", lifetime.ia6t_preferred < now.tv_sec
267 			    ? "0" :
268 			    sec2str(lifetime.ia6t_preferred - now.tv_sec));
269 		} else
270 			printf("infty ");
271 
272 		printf("vltime ");
273 		if (lifetime.ia6t_expire) {
274 			printf("%s ", lifetime.ia6t_expire < now.tv_sec
275 			    ? "0" :
276 			    sec2str(lifetime.ia6t_expire - now.tv_sec));
277 		} else
278 			printf("infty ");
279 	}
280 
281 	print_vhid(ifa, " ");
282 
283 	putchar('\n');
284 }
285 
286 #define	SIN6(x) ((struct sockaddr_in6 *) &(x))
287 static struct	sockaddr_in6 *sin6tab[] = {
288 	SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
289 	SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)
290 };
291 
292 static void
293 in6_getprefix(const char *plen, int which)
294 {
295 	struct sockaddr_in6 *sin = sin6tab[which];
296 	u_char *cp;
297 	int len = atoi(plen);
298 
299 	if ((len < 0) || (len > 128))
300 		errx(1, "%s: bad value", plen);
301 	sin->sin6_len = sizeof(*sin);
302 	if (which != MASK)
303 		sin->sin6_family = AF_INET6;
304 	if ((len == 0) || (len == 128)) {
305 		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
306 		return;
307 	}
308 	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
309 	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
310 		*cp++ = 0xff;
311 	*cp = 0xff << (8 - len);
312 }
313 
314 static void
315 in6_getaddr(const char *s, int which)
316 {
317 	struct sockaddr_in6 *sin = sin6tab[which];
318 	struct addrinfo hints, *res;
319 	int error = -1;
320 
321 	newaddr &= 1;
322 
323 	sin->sin6_len = sizeof(*sin);
324 	if (which != MASK)
325 		sin->sin6_family = AF_INET6;
326 
327 	if (which == ADDR) {
328 		char *p = NULL;
329 		if((p = strrchr(s, '/')) != NULL) {
330 			*p = '\0';
331 			in6_getprefix(p + 1, MASK);
332 			explicit_prefix = 1;
333 		}
334 	}
335 
336 	if (sin->sin6_family == AF_INET6) {
337 		bzero(&hints, sizeof(struct addrinfo));
338 		hints.ai_family = AF_INET6;
339 		error = getaddrinfo(s, NULL, &hints, &res);
340 	}
341 	if (error != 0) {
342 		if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
343 			errx(1, "%s: bad value", s);
344 	} else
345 		bcopy(res->ai_addr, sin, res->ai_addrlen);
346 }
347 
348 static int
349 prefix(void *val, int size)
350 {
351 	u_char *name = (u_char *)val;
352 	int byte, bit, plen = 0;
353 
354 	for (byte = 0; byte < size; byte++, plen += 8)
355 		if (name[byte] != 0xff)
356 			break;
357 	if (byte == size)
358 		return (plen);
359 	for (bit = 7; bit != 0; bit--, plen++)
360 		if (!(name[byte] & (1 << bit)))
361 			break;
362 	for (; bit != 0; bit--)
363 		if (name[byte] & (1 << bit))
364 			return(0);
365 	byte++;
366 	for (; byte < size; byte++)
367 		if (name[byte])
368 			return(0);
369 	return (plen);
370 }
371 
372 static char *
373 sec2str(time_t total)
374 {
375 	static char result[256];
376 	int days, hours, mins, secs;
377 	int first = 1;
378 	char *p = result;
379 
380 	if (0) {
381 		days = total / 3600 / 24;
382 		hours = (total / 3600) % 24;
383 		mins = (total / 60) % 60;
384 		secs = total % 60;
385 
386 		if (days) {
387 			first = 0;
388 			p += sprintf(p, "%dd", days);
389 		}
390 		if (!first || hours) {
391 			first = 0;
392 			p += sprintf(p, "%dh", hours);
393 		}
394 		if (!first || mins) {
395 			first = 0;
396 			p += sprintf(p, "%dm", mins);
397 		}
398 		sprintf(p, "%ds", secs);
399 	} else
400 		sprintf(result, "%lu", (unsigned long)total);
401 
402 	return(result);
403 }
404 
405 static void
406 in6_postproc(int s, const struct afswtch *afp)
407 {
408 	if (explicit_prefix == 0) {
409 		/* Aggregatable address architecture defines all prefixes
410 		   are 64. So, it is convenient to set prefixlen to 64 if
411 		   it is not specified. */
412 		setifprefixlen("64", 0, s, afp);
413 		/* in6_getprefix("64", MASK) if MASK is available here... */
414 	}
415 }
416 
417 static void
418 in6_status_tunnel(int s)
419 {
420 	char src[NI_MAXHOST];
421 	char dst[NI_MAXHOST];
422 	struct in6_ifreq in6_ifr;
423 	const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
424 
425 	memset(&in6_ifr, 0, sizeof(in6_ifr));
426 	strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
427 
428 	if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
429 		return;
430 	if (sa->sa_family != AF_INET6)
431 		return;
432 	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
433 	    NI_NUMERICHOST) != 0)
434 		src[0] = '\0';
435 
436 	if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
437 		return;
438 	if (sa->sa_family != AF_INET6)
439 		return;
440 	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
441 	    NI_NUMERICHOST) != 0)
442 		dst[0] = '\0';
443 
444 	printf("\ttunnel inet6 %s --> %s\n", src, dst);
445 }
446 
447 static void
448 in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
449 {
450 	struct in6_aliasreq in6_addreq;
451 
452 	memset(&in6_addreq, 0, sizeof(in6_addreq));
453 	strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
454 	memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
455 	memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
456 	    dstres->ai_addr->sa_len);
457 
458 	if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
459 		warn("SIOCSIFPHYADDR_IN6");
460 }
461 
462 static struct cmd inet6_cmds[] = {
463 	DEF_CMD_ARG("prefixlen",			setifprefixlen),
464 	DEF_CMD("anycast",	IN6_IFF_ANYCAST,	setip6flags),
465 	DEF_CMD("tentative",	IN6_IFF_TENTATIVE,	setip6flags),
466 	DEF_CMD("-tentative",	-IN6_IFF_TENTATIVE,	setip6flags),
467 	DEF_CMD("deprecated",	IN6_IFF_DEPRECATED,	setip6flags),
468 	DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED,	setip6flags),
469 	DEF_CMD("autoconf",	IN6_IFF_AUTOCONF,	setip6flags),
470 	DEF_CMD("-autoconf",	-IN6_IFF_AUTOCONF,	setip6flags),
471 	DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE,	setip6flags),
472 	DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
473 	DEF_CMD("accept_rtadv",	ND6_IFF_ACCEPT_RTADV,	setnd6flags),
474 	DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV,	setnd6flags),
475 	DEF_CMD("no_radr",	ND6_IFF_NO_RADR,	setnd6flags),
476 	DEF_CMD("-no_radr",	-ND6_IFF_NO_RADR,	setnd6flags),
477 	DEF_CMD("defaultif",	1,			setnd6defif),
478 	DEF_CMD("-defaultif",	-1,			setnd6defif),
479 	DEF_CMD("ifdisabled",	ND6_IFF_IFDISABLED,	setnd6flags),
480 	DEF_CMD("-ifdisabled",	-ND6_IFF_IFDISABLED,	setnd6flags),
481 	DEF_CMD("nud",		ND6_IFF_PERFORMNUD,	setnd6flags),
482 	DEF_CMD("-nud",		-ND6_IFF_PERFORMNUD,	setnd6flags),
483 	DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
484 	DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
485 	DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
486 	DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
487 	DEF_CMD("no_dad",	ND6_IFF_NO_DAD,		setnd6flags),
488 	DEF_CMD("-no_dad",	-ND6_IFF_NO_DAD,	setnd6flags),
489 	DEF_CMD_ARG("pltime",        			setip6pltime),
490 	DEF_CMD_ARG("vltime",        			setip6vltime),
491 	DEF_CMD("eui64",	0,			setip6eui64),
492 };
493 
494 static struct afswtch af_inet6 = {
495 	.af_name	= "inet6",
496 	.af_af		= AF_INET6,
497 	.af_status	= in6_status,
498 	.af_getaddr	= in6_getaddr,
499 	.af_getprefix	= in6_getprefix,
500 	.af_other_status = nd6_status,
501 	.af_postproc	= in6_postproc,
502 	.af_status_tunnel = in6_status_tunnel,
503 	.af_settunnel	= in6_set_tunnel,
504 	.af_difaddr	= SIOCDIFADDR_IN6,
505 	.af_aifaddr	= SIOCAIFADDR_IN6,
506 	.af_ridreq	= &in6_addreq,
507 	.af_addreq	= &in6_addreq,
508 };
509 
510 static void
511 in6_Lopt_cb(const char *optarg __unused)
512 {
513 	ip6lifetime++;	/* print IPv6 address lifetime */
514 }
515 static struct option in6_Lopt = {
516 	.opt = "L",
517 	.opt_usage = "[-L]",
518 	.cb = in6_Lopt_cb
519 };
520 
521 static __constructor void
522 inet6_ctor(void)
523 {
524 #define	N(a)	(sizeof(a) / sizeof(a[0]))
525 	size_t i;
526 
527 #ifndef RESCUE
528 	if (!feature_present("inet6"))
529 		return;
530 #endif
531 
532 	for (i = 0; i < N(inet6_cmds);  i++)
533 		cmd_register(&inet6_cmds[i]);
534 	af_register(&af_inet6);
535 	opt_register(&in6_Lopt);
536 #undef N
537 }
538