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