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