LIBPRIV: Add private includes for dfuife_curses.
[dragonfly.git] / sbin / ifconfig / af_inet6.c
blob683e388e6f9080a0454dcb09421db12be2b2366a
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
29 * $FreeBSD: src/sbin/ifconfig/af_inet6.c,v 1.3 2005/06/16 19:37:09 ume Exp $
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 */
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <time.h>
44 #include <ifaddrs.h>
46 #include <arpa/inet.h>
48 #include <netinet/in.h>
49 #include <net/if_var.h> /* for struct ifaddr */
50 #include <netinet/in_var.h>
51 #include <netdb.h>
53 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
55 #include "ifconfig.h"
57 static struct in6_ifreq in6_ridreq;
58 static struct in6_aliasreq in6_addreq =
59 { { 0 },
60 { 0 },
61 { 0 },
62 { 0 },
63 0,
64 { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
65 static int ip6lifetime;
67 static void in6_fillscopeid(struct sockaddr_in6 *sin6);
68 static int prefix(void *, int);
69 static char *sec2str(time_t);
70 static int explicit_prefix = 0;
72 static char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/
74 static void
75 setifprefixlen(const char *addr, int dummy __unused, int s,
76 const struct afswtch *afp)
78 if (afp->af_getprefix != NULL)
79 afp->af_getprefix(addr, MASK);
80 explicit_prefix = 1;
83 static void
84 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
85 const struct afswtch *afp)
87 if (afp->af_af != AF_INET6)
88 err(1, "address flags can be set only for inet6 addresses");
90 if (flag < 0)
91 in6_addreq.ifra_flags &= ~(-flag);
92 else
93 in6_addreq.ifra_flags |= flag;
96 static void
97 setip6lifetime(const char *cmd, const char *val, int s,
98 const struct afswtch *afp)
100 struct timespec now;
101 time_t newval;
102 char *ep;
104 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
105 newval = (time_t)strtoul(val, &ep, 0);
106 if (val == ep)
107 errx(1, "invalid %s", cmd);
108 if (afp->af_af != AF_INET6)
109 errx(1, "%s not allowed for the AF", cmd);
110 if (strcmp(cmd, "vltime") == 0) {
111 in6_addreq.ifra_lifetime.ia6t_expire = now.tv_sec + newval;
112 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
113 } else if (strcmp(cmd, "pltime") == 0) {
114 in6_addreq.ifra_lifetime.ia6t_preferred = now.tv_sec + newval;
115 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
119 static void
120 setip6pltime(const char *seconds, int dummy __unused, int s,
121 const struct afswtch *afp)
123 setip6lifetime("pltime", seconds, s, afp);
126 static void
127 setip6vltime(const char *seconds, int dummy __unused, int s,
128 const struct afswtch *afp)
130 setip6lifetime("vltime", seconds, s, afp);
133 static void
134 setip6eui64(const char *cmd, int dummy __unused, int s,
135 const struct afswtch *afp)
137 struct ifaddrs *ifap, *ifa;
138 const struct sockaddr_in6 *sin6 = NULL;
139 const struct in6_addr *lladdr = NULL;
140 struct in6_addr *in6;
142 if (afp->af_af != AF_INET6)
143 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
144 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
145 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
146 errx(EXIT_FAILURE, "interface index is already filled");
147 if (getifaddrs(&ifap) != 0)
148 err(EXIT_FAILURE, "getifaddrs");
149 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
150 if (ifa->ifa_addr->sa_family == AF_INET6 &&
151 strcmp(ifa->ifa_name, name) == 0) {
152 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
153 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
154 lladdr = &sin6->sin6_addr;
155 break;
159 if (!lladdr)
160 errx(EXIT_FAILURE, "could not determine link local address");
162 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
164 freeifaddrs(ifap);
167 static void
168 in6_fillscopeid(struct sockaddr_in6 *sin6)
170 #if defined(__KAME__) && defined(KAME_SCOPEID)
171 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
172 sin6->sin6_scope_id =
173 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
174 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
176 #endif
179 static void
180 in6_status(int s __unused, const struct rt_addrinfo * info)
182 struct sockaddr_in6 *sin, null_sin;
183 struct in6_ifreq ifr6;
184 int s6;
185 u_int32_t flags6;
186 struct in6_addrlifetime lifetime;
187 struct timespec now;
188 int error;
189 u_int32_t scopeid;
191 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
193 memset(&null_sin, 0, sizeof(null_sin));
195 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
196 if (sin == NULL)
197 return;
199 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
200 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
201 warn("socket(AF_INET6,SOCK_DGRAM)");
202 return;
204 ifr6.ifr_addr = *sin;
205 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
206 warn("ioctl(SIOCGIFAFLAG_IN6)");
207 close(s6);
208 return;
210 flags6 = ifr6.ifr_ifru.ifru_flags6;
211 memset(&lifetime, 0, sizeof(lifetime));
212 ifr6.ifr_addr = *sin;
213 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
214 warn("ioctl(SIOCGIFALIFETIME_IN6)");
215 close(s6);
216 return;
218 lifetime = ifr6.ifr_ifru.ifru_lifetime;
219 close(s6);
221 /* XXX: embedded link local addr check */
222 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
223 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
224 u_short index;
226 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
227 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
228 if (sin->sin6_scope_id == 0)
229 sin->sin6_scope_id = ntohs(index);
231 scopeid = sin->sin6_scope_id;
233 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
234 sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
235 if (error != 0)
236 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
237 sizeof(addr_buf));
238 printf("\tinet6 %s ", addr_buf);
240 if (flags & IFF_POINTOPOINT) {
241 /* note RTAX_BRD overlap with IFF_BROADCAST */
242 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
244 * some of the interfaces do not have valid destination
245 * address.
247 if (sin && sin->sin6_family == AF_INET6) {
248 int error;
250 /* XXX: embedded link local addr check */
251 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
252 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
253 u_short index;
255 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
256 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
257 if (sin->sin6_scope_id == 0)
258 sin->sin6_scope_id = ntohs(index);
261 error = getnameinfo((struct sockaddr *)sin,
262 sin->sin6_len, addr_buf,
263 sizeof(addr_buf), NULL, 0,
264 NI_NUMERICHOST);
265 if (error != 0)
266 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
267 sizeof(addr_buf));
268 printf("--> %s ", addr_buf);
272 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
273 if (!sin)
274 sin = &null_sin;
275 printf("prefixlen %d ", prefix(&sin->sin6_addr,
276 sizeof(struct in6_addr)));
278 if ((flags6 & IN6_IFF_ANYCAST) != 0)
279 printf("anycast ");
280 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
281 printf("tentative ");
282 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
283 printf("duplicated ");
284 if ((flags6 & IN6_IFF_DETACHED) != 0)
285 printf("detached ");
286 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
287 printf("deprecated ");
288 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
289 printf("autoconf ");
290 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
291 printf("temporary ");
293 if (scopeid)
294 printf("scopeid 0x%x ", scopeid);
296 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
297 printf("pltime ");
298 if (lifetime.ia6t_preferred) {
299 printf("%s ", lifetime.ia6t_preferred < now.tv_sec
300 ? "0" : sec2str(lifetime.ia6t_preferred - now.tv_sec));
301 } else
302 printf("infty ");
304 printf("vltime ");
305 if (lifetime.ia6t_expire) {
306 printf("%s ", lifetime.ia6t_expire < now.tv_sec
307 ? "0" : sec2str(lifetime.ia6t_expire - now.tv_sec));
308 } else
309 printf("infty ");
312 putchar('\n');
315 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
316 static struct sockaddr_in6 *sin6tab[] = {
317 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
318 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)
321 static void
322 in6_getprefix(const char *plen, int which)
324 struct sockaddr_in6 *sin = sin6tab[which];
325 u_char *cp;
326 int len = atoi(plen);
328 if ((len < 0) || (len > 128))
329 errx(1, "%s: bad value", plen);
330 sin->sin6_len = sizeof(*sin);
331 if (which != MASK)
332 sin->sin6_family = AF_INET6;
333 if ((len == 0) || (len == 128)) {
334 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
335 return;
337 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
338 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
339 *cp++ = 0xff;
340 *cp = 0xff << (8 - len);
343 static void
344 in6_getaddr(const char *s, int which)
346 struct sockaddr_in6 *sin = sin6tab[which];
347 struct addrinfo hints, *res;
348 int error = -1;
350 newaddr &= 1;
352 sin->sin6_len = sizeof(*sin);
353 if (which != MASK)
354 sin->sin6_family = AF_INET6;
356 if (which == ADDR) {
357 char *p = NULL;
358 if((p = strrchr(s, '/')) != NULL) {
359 *p = '\0';
360 in6_getprefix(p + 1, MASK);
361 explicit_prefix = 1;
365 if (sin->sin6_family == AF_INET6) {
366 bzero(&hints, sizeof(struct addrinfo));
367 hints.ai_family = AF_INET6;
368 error = getaddrinfo(s, NULL, &hints, &res);
370 if (error != 0) {
371 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
372 errx(1, "%s: bad value", s);
373 } else
374 bcopy(res->ai_addr, sin, res->ai_addrlen);
377 static int
378 prefix(void *val, int size)
380 u_char *name = (u_char *)val;
381 int byte, bit, plen = 0;
383 for (byte = 0; byte < size; byte++, plen += 8)
384 if (name[byte] != 0xff)
385 break;
386 if (byte == size)
387 return (plen);
388 for (bit = 7; bit != 0; bit--, plen++)
389 if (!(name[byte] & (1 << bit)))
390 break;
391 for (; bit != 0; bit--)
392 if (name[byte] & (1 << bit))
393 return(0);
394 byte++;
395 for (; byte < size; byte++)
396 if (name[byte])
397 return(0);
398 return (plen);
401 static char *
402 sec2str(time_t total)
404 static char result[256];
405 int days, hours, mins, secs;
406 int first = 1;
407 char *p = result;
409 if (0) {
410 days = total / 3600 / 24;
411 hours = (total / 3600) % 24;
412 mins = (total / 60) % 60;
413 secs = total % 60;
415 if (days) {
416 first = 0;
417 p += sprintf(p, "%dd", days);
419 if (!first || hours) {
420 first = 0;
421 p += sprintf(p, "%dh", hours);
423 if (!first || mins) {
424 first = 0;
425 p += sprintf(p, "%dm", mins);
427 sprintf(p, "%ds", secs);
428 } else
429 sprintf(result, "%lu", (unsigned long)total);
431 return(result);
434 static void
435 in6_postproc(int s, const struct afswtch *afp)
437 if (explicit_prefix == 0) {
438 /* Aggregatable address architecture defines all prefixes
439 are 64. So, it is convenient to set prefixlen to 64 if
440 it is not specified. */
441 setifprefixlen("64", 0, s, afp);
442 /* in6_getprefix("64", MASK) if MASK is available here... */
446 static void
447 in6_status_tunnel(int s)
449 char src[NI_MAXHOST];
450 char dst[NI_MAXHOST];
451 struct in6_ifreq in6_ifr;
452 const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
454 memset(&in6_ifr, 0, sizeof(in6_ifr));
455 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
457 if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
458 return;
459 if (sa->sa_family != AF_INET6)
460 return;
461 in6_fillscopeid(&in6_ifr.ifr_addr);
462 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
463 NI_NUMERICHOST) != 0)
464 src[0] = '\0';
466 if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
467 return;
468 if (sa->sa_family != AF_INET6)
469 return;
470 in6_fillscopeid(&in6_ifr.ifr_addr);
471 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
472 NI_NUMERICHOST) != 0)
473 dst[0] = '\0';
475 printf("\ttunnel inet6 %s --> %s\n", src, dst);
478 static void
479 in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
481 struct in6_aliasreq in6_addreq;
483 memset(&in6_addreq, 0, sizeof(in6_addreq));
484 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
485 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
486 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
487 dstres->ai_addr->sa_len);
489 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
490 warn("SIOCSIFPHYADDR_IN6");
493 static struct cmd inet6_cmds[] = {
494 DEF_CMD_ARG("prefixlen", setifprefixlen),
495 DEF_CMD("anycast", IN6_IFF_ANYCAST, setip6flags),
496 DEF_CMD("tentative", IN6_IFF_TENTATIVE, setip6flags),
497 DEF_CMD("-tentative", -IN6_IFF_TENTATIVE, setip6flags),
498 DEF_CMD("deprecated", IN6_IFF_DEPRECATED, setip6flags),
499 DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED, setip6flags),
500 DEF_CMD("autoconf", IN6_IFF_AUTOCONF, setip6flags),
501 DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF, setip6flags),
502 DEF_CMD_ARG("pltime", setip6pltime),
503 DEF_CMD_ARG("vltime", setip6vltime),
504 DEF_CMD("eui64", 0, setip6eui64),
507 static struct afswtch af_inet6 = {
508 .af_name = "inet6",
509 .af_af = AF_INET6,
510 .af_status = in6_status,
511 .af_getaddr = in6_getaddr,
512 .af_getprefix = in6_getprefix,
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_ridreq,
519 .af_addreq = &in6_addreq,
522 static void
523 in6_Lopt_cb(const char *optarg __unused)
525 ip6lifetime++; /* print IPv6 address lifetime */
527 static struct option in6_Lopt = { "L", "[-L]", in6_Lopt_cb };
529 static __constructor(101) void
530 inet6_ctor(void)
532 #define N(a) (sizeof(a) / sizeof(a[0]))
533 int i;
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