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