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