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