The return value of nameif was wrong
[platform/upstream/net-tools.git] / iptunnel.c
1 /*
2  * iptunnel.c          "ip tunnel"
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  *
12  * Changes:
13  *
14  * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
15  * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
16  * Bernd Eckenfels 990715: add linux/types.h (not clean but solves missing __u16
17  * Arnaldo Carvalho de Melo 20010404: use setlocale
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <syslog.h>
25 #include <fcntl.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <netinet/in.h>
29 #include <netinet/ip.h>
30 #include <arpa/inet.h>
31 #if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1))
32 #include <net/if.h>
33 #include <net/if_arp.h>
34 #else
35 #include <linux/if.h>
36 #include <linux/if_arp.h>
37 #endif
38 #include <linux/types.h>
39 #include <linux/if_tunnel.h>
40
41 #include "config.h"
42 #include "intl.h"
43 #include "net-support.h"
44 #include "version.h"
45 #include "util.h"
46
47 #undef GRE_CSUM
48 #define GRE_CSUM        htons(0x8000)
49 #undef GRE_ROUTING
50 #define GRE_ROUTING     htons(0x4000)
51 #undef GRE_KEY
52 #define GRE_KEY         htons(0x2000)
53 #undef GRE_SEQ
54 #define GRE_SEQ         htons(0x1000)
55 #undef GRE_STRICT
56 #define GRE_STRICT      htons(0x0800)
57 #undef GRE_REC
58 #define GRE_REC         htons(0x0700)
59 #undef GRE_FLAGS
60 #define GRE_FLAGS       htons(0x00F8)
61 #undef GRE_VERSION
62 #define GRE_VERSION     htons(0x0007)
63
64 /* Old versions of glibc do not define this */
65 #if __GLIBC__ == 2 && __GLIBC_MINOR__ == 0
66 #define IPPROTO_GRE     47
67 #endif
68
69 #include "util-ank.h"
70
71 char *Release = RELEASE,
72      *Version = "iptunnel 1.01",
73      *Signature = "Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>";
74
75 static void version(void)
76 {
77         printf("%s\n%s\n%s\n", Release, Version, Signature);
78         exit(E_VERSION);
79 }
80
81 static void usage(void) __attribute__((noreturn));
82
83 static void usage(void)
84 {
85         fprintf(stderr, _("Usage: iptunnel { add | change | del | show } [ NAME ]\n"));
86         fprintf(stderr, _("          [ mode { ipip | gre | sit } ] [ remote ADDR ] [ local ADDR ]\n"));
87         fprintf(stderr, _("          [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"));
88         fprintf(stderr, _("          [ ttl TTL ] [ tos TOS ] [ nopmtudisc ] [ dev PHYS_DEV ]\n"));
89         fprintf(stderr, _("       iptunnel -V | --version\n\n"));
90         fprintf(stderr, _("Where: NAME := STRING\n"));
91         fprintf(stderr, _("       ADDR := { IP_ADDRESS | any }\n"));
92         fprintf(stderr, _("       TOS  := { NUMBER | inherit }\n"));
93         fprintf(stderr, _("       TTL  := { 1..255 | inherit }\n"));
94         fprintf(stderr, _("       KEY  := { DOTTED_QUAD | NUMBER }\n"));
95         exit(-1);
96 }
97
98 static int do_ioctl_get_ifindex(char *dev)
99 {
100         struct ifreq ifr;
101         int fd;
102         int err;
103
104         strcpy(ifr.ifr_name, dev);
105         fd = socket(AF_INET, SOCK_DGRAM, 0);
106         err = ioctl(fd, SIOCGIFINDEX, &ifr);
107         if (err) {
108                 perror("ioctl");
109                 return 0;
110         }
111         close(fd);
112         return ifr.ifr_ifindex;
113 }
114
115 static int do_ioctl_get_iftype(char *dev)
116 {
117         struct ifreq ifr;
118         int fd;
119         int err;
120
121         strcpy(ifr.ifr_name, dev);
122         fd = socket(AF_INET, SOCK_DGRAM, 0);
123         err = ioctl(fd, SIOCGIFHWADDR, &ifr);
124         if (err) {
125                 perror("ioctl");
126                 return -1;
127         }
128         close(fd);
129         return ifr.ifr_addr.sa_family;
130 }
131
132
133 static char * do_ioctl_get_ifname(int idx)
134 {
135         static struct ifreq ifr;
136         int fd;
137         int err;
138
139         ifr.ifr_ifindex = idx;
140         fd = socket(AF_INET, SOCK_DGRAM, 0);
141         err = ioctl(fd, SIOCGIFNAME, &ifr);
142         if (err) {
143                 perror("ioctl");
144                 return NULL;
145         }
146         close(fd);
147         return ifr.ifr_name;
148 }
149
150
151
152 static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
153 {
154         struct ifreq ifr;
155         int fd;
156         int err;
157
158         strcpy(ifr.ifr_name, basedev);
159         ifr.ifr_ifru.ifru_data = (void*)p;
160         fd = socket(AF_INET, SOCK_DGRAM, 0);
161         err = ioctl(fd, SIOCGETTUNNEL, &ifr);
162         if (err)
163                 perror("ioctl");
164         close(fd);
165         return err;
166 }
167
168 static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
169 {
170         struct ifreq ifr;
171         int fd;
172         int err;
173
174         strcpy(ifr.ifr_name, basedev);
175         ifr.ifr_ifru.ifru_data = (void*)p;
176         fd = socket(AF_INET, SOCK_DGRAM, 0);
177         err = ioctl(fd, cmd, &ifr);
178         if (err)
179                 perror("ioctl");
180         close(fd);
181         return err;
182 }
183
184 static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
185 {
186         struct ifreq ifr;
187         int fd;
188         int err;
189
190         strcpy(ifr.ifr_name, basedev);
191         ifr.ifr_ifru.ifru_data = (void*)p;
192         fd = socket(AF_INET, SOCK_DGRAM, 0);
193         err = ioctl(fd, SIOCDELTUNNEL, &ifr);
194         if (err)
195                 perror("ioctl");
196         close(fd);
197         return err;
198 }
199
200 static int parse_args(int argc, char **argv, struct ip_tunnel_parm *p)
201 {
202         char medium[IFNAMSIZ];
203
204         memset(p, 0, sizeof(*p));
205         memset(&medium, 0, sizeof(medium));
206
207         p->iph.version = 4;
208         p->iph.ihl = 5;
209 #ifndef IP_DF
210 #define IP_DF           0x4000          /* Flag: "Don't Fragment"       */
211 #endif
212         p->iph.frag_off = htons(IP_DF);
213
214         while (argc > 0) {
215                 if (strcmp(*argv, "mode") == 0) {
216                         NEXT_ARG();
217                         if (strcmp(*argv, "ipip") == 0) {
218                                 if (p->iph.protocol)
219                                         usage();
220                                 p->iph.protocol = IPPROTO_IPIP;
221                         } else if (strcmp(*argv, "gre") == 0) {
222                                 if (p->iph.protocol)
223                                         usage();
224                                 p->iph.protocol = IPPROTO_GRE;
225                         } else if (strcmp(*argv, "sit") == 0) {
226                                 if (p->iph.protocol)
227                                         usage();
228                                 p->iph.protocol = IPPROTO_IPV6;
229                         } else
230                                 usage();
231                 } else if (strcmp(*argv, "key") == 0) {
232                         unsigned uval;
233                         NEXT_ARG();
234                         p->i_flags |= GRE_KEY;
235                         p->o_flags |= GRE_KEY;
236                         if (strchr(*argv, '.'))
237                                 p->i_key = p->o_key = get_addr32(*argv);
238                         else {
239                                 if (scan_number(*argv, &uval)<0)
240                                         usage();
241                                 p->i_key = p->o_key = htonl(uval);
242                         }
243                 } else if (strcmp(*argv, "ikey") == 0) {
244                         unsigned uval;
245                         NEXT_ARG();
246                         p->i_flags |= GRE_KEY;
247                         if (strchr(*argv, '.'))
248                                 p->o_key = get_addr32(*argv);
249                         else {
250                                 if (scan_number(*argv, &uval)<0)
251                                         usage();
252                                 p->i_key = htonl(uval);
253                         }
254                 } else if (strcmp(*argv, "okey") == 0) {
255                         unsigned uval;
256                         NEXT_ARG();
257                         p->o_flags |= GRE_KEY;
258                         if (strchr(*argv, '.'))
259                                 p->o_key = get_addr32(*argv);
260                         else {
261                                 if (scan_number(*argv, &uval)<0)
262                                         usage();
263                                 p->o_key = htonl(uval);
264                         }
265                 } else if (strcmp(*argv, "seq") == 0) {
266                         p->i_flags |= GRE_SEQ;
267                         p->o_flags |= GRE_SEQ;
268                 } else if (strcmp(*argv, "iseq") == 0) {
269                         p->i_flags |= GRE_SEQ;
270                 } else if (strcmp(*argv, "oseq") == 0) {
271                         p->o_flags |= GRE_SEQ;
272                 } else if (strcmp(*argv, "csum") == 0) {
273                         p->i_flags |= GRE_CSUM;
274                         p->o_flags |= GRE_CSUM;
275                 } else if (strcmp(*argv, "icsum") == 0) {
276                         p->i_flags |= GRE_CSUM;
277                 } else if (strcmp(*argv, "ocsum") == 0) {
278                         p->o_flags |= GRE_CSUM;
279                 } else if (strcmp(*argv, "nopmtudisc") == 0) {
280                         p->iph.frag_off = 0;
281                 } else if (strcmp(*argv, "remote") == 0) {
282                         NEXT_ARG();
283                         if (strcmp(*argv, "any"))
284                                 p->iph.daddr = get_addr32(*argv);
285                 } else if (strcmp(*argv, "local") == 0) {
286                         NEXT_ARG();
287                         if (strcmp(*argv, "any"))
288                                 p->iph.saddr = get_addr32(*argv);
289                 } else if (strcmp(*argv, "dev") == 0) {
290                         NEXT_ARG();
291                         safe_strncpy(medium, *argv, IFNAMSIZ-1);
292                 } else if (strcmp(*argv, "ttl") == 0) {
293                         unsigned uval;
294                         NEXT_ARG();
295                         if (strcmp(*argv, "inherit") != 0) {
296                                 if (scan_number(*argv, &uval)<0)
297                                         usage();
298                                 if (uval > 255)
299                                         usage();
300                                 p->iph.ttl = uval;
301                         }
302                 } else if (strcmp(*argv, "tos") == 0) {
303                         unsigned uval;
304                         NEXT_ARG();
305                         if (strcmp(*argv, "inherit") != 0) {
306                                 if (scan_number(*argv, &uval)<0)
307                                         usage();
308                                 if (uval > 255)
309                                         usage();
310                                 p->iph.tos = uval;
311                         } else
312                                 p->iph.tos = 1;
313                 } else {
314                         if (p->name[0])
315                                 usage();
316                         safe_strncpy(p->name, *argv, IFNAMSIZ);
317                 }
318                 argc--; argv++;
319         }
320
321         if (p->iph.protocol == 0) {
322                 if (memcmp(p->name, "gre", 3) == 0)
323                         p->iph.protocol = IPPROTO_GRE;
324                 else if (memcmp(p->name, "ipip", 4) == 0)
325                         p->iph.protocol = IPPROTO_IPIP;
326                 else if (memcmp(p->name, "sit", 3) == 0)
327                         p->iph.protocol = IPPROTO_IPV6;
328         }
329
330         if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
331                 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
332                         fprintf(stderr, _("Keys are not allowed with ipip and sit.\n"));
333                         return -1;
334                 }
335         }
336
337         if (medium[0]) {
338                 p->link = do_ioctl_get_ifindex(medium);
339                 if (p->link == 0)
340                         return -1;
341         }
342
343         if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
344                 p->i_key = p->iph.daddr;
345                 p->i_flags |= GRE_KEY;
346         }
347         if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
348                 p->o_key = p->iph.daddr;
349                 p->o_flags |= GRE_KEY;
350         }
351         if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
352                 fprintf(stderr, _("Broadcast tunnel requires a source address.\n"));
353                 return -1;
354         }
355         return 0;
356 }
357
358
359 static int do_add(int cmd, int argc, char **argv)
360 {
361         struct ip_tunnel_parm p;
362
363         if (parse_args(argc, argv, &p) < 0)
364                 return -1;
365
366         if (p.iph.ttl && p.iph.frag_off == 0) {
367                 fprintf(stderr, _("ttl != 0 and noptmudisc are incompatible\n"));
368                 return -1;
369         }
370
371         switch (p.iph.protocol) {
372         case IPPROTO_IPIP:
373                 return do_add_ioctl(cmd, "tunl0", &p);
374         case IPPROTO_GRE:
375                 return do_add_ioctl(cmd, "gre0", &p);
376         case IPPROTO_IPV6:
377                 return do_add_ioctl(cmd, "sit0", &p);
378         default:        
379                 fprintf(stderr, _("cannot determine tunnel mode (ipip, gre or sit)\n"));
380                 return -1;
381         }
382         return -1;
383 }
384
385 int do_del(int argc, char **argv)
386 {
387         struct ip_tunnel_parm p;
388
389         if (parse_args(argc, argv, &p) < 0)
390                 return -1;
391
392         switch (p.iph.protocol) {
393         case IPPROTO_IPIP:      
394                 return do_del_ioctl(p.name[0] ? p.name : "tunl0", &p);
395         case IPPROTO_GRE:       
396                 return do_del_ioctl(p.name[0] ? p.name : "gre0", &p);
397         case IPPROTO_IPV6:      
398                 return do_del_ioctl(p.name[0] ? p.name : "sit0", &p);
399         default:        
400                 return do_del_ioctl(p.name, &p);
401         }
402         return -1;
403 }
404
405 void print_tunnel(struct ip_tunnel_parm *p)
406 {
407         char s1[256];
408         char s2[256];
409         char s3[64];
410         char s4[64];
411
412         format_host(AF_INET, &p->iph.daddr, s1, sizeof(s1));
413         format_host(AF_INET, &p->iph.saddr, s2, sizeof(s2));
414         inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
415         inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
416
417         printf(_("%s: %s/ip  remote %s  local %s "),
418                p->name,
419                p->iph.protocol == IPPROTO_IPIP ? "ip" :
420                (p->iph.protocol == IPPROTO_GRE ? "gre" :
421                 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : _("unknown"))),
422                p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
423         if (p->link) {
424                 char *n = do_ioctl_get_ifname(p->link);
425                 if (n)
426                         printf(" dev %s ", n);
427         }
428         if (p->iph.ttl)
429                 printf(" ttl %d ", p->iph.ttl);
430         else
431                 printf(" ttl inherit ");
432         if (p->iph.tos) {
433                 printf(" tos");
434                 if (p->iph.tos&1)
435                         printf(" inherit");
436                 if (p->iph.tos&~1)
437                         printf("%c%02x ", p->iph.tos&1 ? '/' : ' ', p->iph.tos&~1);
438         }
439         if (!(p->iph.frag_off&htons(IP_DF)))
440                 printf(" nopmtudisc");
441
442         if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
443                 printf(" key %s", s3);
444         else if ((p->i_flags|p->o_flags)&GRE_KEY) {
445                 if (p->i_flags&GRE_KEY)
446                         printf(" ikey %s ", s3);
447                 if (p->o_flags&GRE_KEY)
448                         printf(" okey %s ", s4);
449         }
450         printf("\n");
451
452         if (p->i_flags&GRE_SEQ)
453                 printf(_("  Drop packets out of sequence.\n"));
454         if (p->i_flags&GRE_CSUM)
455                 printf(_("  Checksum in received packet is required.\n"));
456         if (p->o_flags&GRE_SEQ)
457                 printf(_("  Sequence packets on output.\n"));
458         if (p->o_flags&GRE_CSUM)
459                 printf(_("  Checksum output packets.\n"));
460 }
461
462 static int do_tunnels_list(struct ip_tunnel_parm *p)
463 {
464         char name[IFNAMSIZ];
465         unsigned long  rx_bytes, rx_packets, rx_errs, rx_drops,
466         rx_fifo, rx_frame,
467         tx_bytes, tx_packets, tx_errs, tx_drops,
468         tx_fifo, tx_colls, tx_carrier, rx_multi;
469         int type;
470         struct ip_tunnel_parm p1;
471
472         char buf[512];
473         FILE *fp = fopen("/proc/net/dev", "r");
474         if (fp == NULL) {
475                 perror("fopen");
476                 return -1;
477         }
478
479         if (fgets(buf, sizeof(buf), fp))
480                 /* eat line */;
481         if (fgets(buf, sizeof(buf), fp))
482                 /* eat line */;
483
484         while (fgets(buf, sizeof(buf), fp) != NULL) {
485                 char *ptr;
486                 buf[sizeof(buf) - 1] = 0;
487                 if ((ptr = strchr(buf, ':')) == NULL ||
488                     (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
489                         fprintf(stderr, _("Wrong format of /proc/net/dev. Sorry.\n"));
490                         return -1;
491                 }
492                 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
493                            &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
494                            &rx_fifo, &rx_frame, &rx_multi,
495                            &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
496                            &tx_fifo, &tx_colls, &tx_carrier) != 14)
497                         continue;
498                 if (p->name[0] && strcmp(p->name, name))
499                         continue;
500                 type = do_ioctl_get_iftype(name);
501                 if (type == -1) {
502                         fprintf(stderr, _("Failed to get type of [%s]\n"), name);
503                         continue;
504                 }
505                 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
506                         continue;
507                 memset(&p1, 0, sizeof(p1));
508                 if (do_get_ioctl(name, &p1))
509                         continue;
510                 if ((p->link && p1.link != p->link) ||
511                     (p->name[0] && strcmp(p1.name, p->name)) ||
512                     (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
513                     (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
514                     (p->i_key && p1.i_key != p->i_key))
515                         continue;
516                 print_tunnel(&p1);
517                 if (show_stats) {
518                         printf(_("RX: Packets    Bytes        Errors CsumErrs OutOfSeq Mcasts\n"));
519                         printf("    %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld\n",
520                                rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi);
521                         printf(_("TX: Packets    Bytes        Errors DeadLoop NoRoute  NoBufs\n"));
522                         printf("    %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld\n\n",
523                                tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
524                 }
525         }
526         return 0;
527 }
528
529 static int do_show(int argc, char **argv)
530 {
531         int err;
532         struct ip_tunnel_parm p;
533
534         if (parse_args(argc, argv, &p) < 0)
535                 return -1;
536
537         switch (p.iph.protocol) {
538         case IPPROTO_IPIP:      
539                 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
540                 break;
541         case IPPROTO_GRE:
542                 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
543                 break;
544         case IPPROTO_IPV6:
545                 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
546                 break;
547         default:
548                 do_tunnels_list(&p);
549                 return 0;
550         }
551         if (err)
552                 return -1;
553
554         print_tunnel(&p);
555         return 0;
556 }
557
558 int do_iptunnel(int argc, char **argv)
559 {
560         if (argc > 0) {
561                 if (matches(*argv, "add") == 0)
562                         return do_add(SIOCADDTUNNEL, argc-1, argv+1);
563                 if (matches(*argv, "change") == 0)
564                         return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
565                 if (matches(*argv, "del") == 0)
566                         return do_del(argc-1, argv+1);
567                 if (matches(*argv, "show") == 0 ||
568                     matches(*argv, "lst") == 0 ||
569                     matches(*argv, "list") == 0)
570                         return do_show(argc-1, argv+1);
571         } else
572                 return do_show(0, NULL);
573
574         usage();
575 }
576
577
578 int preferred_family = AF_UNSPEC;
579 int show_stats = 0;
580 int resolve_hosts = 0;
581
582 int main(int argc, char **argv)
583 {
584         char *basename;
585
586 #if I18N
587         setlocale (LC_ALL, "");
588         bindtextdomain("net-tools", "/usr/share/locale");
589         textdomain("net-tools");
590 #endif
591
592         basename = strrchr(argv[0], '/');
593         if (basename == NULL)
594                 basename = argv[0];
595         else
596                 basename++;
597         
598         while (argc > 1) {
599                 if (argv[1][0] != '-')
600                         break;
601                 if (matches(argv[1], "-family") == 0) {
602                         argc--;
603                         argv++;
604                         if (argc <= 1)
605                                 usage();
606                         if (strcmp(argv[1], "inet") == 0)
607                                 preferred_family = AF_INET;
608                         else if (strcmp(argv[1], "inet6") == 0)
609                                 preferred_family = AF_INET6;
610                         else
611                                 usage();
612                 } else if (matches(argv[1], "-stats") == 0 ||
613                            matches(argv[1], "-statistics") == 0) {
614                         ++show_stats;
615                 } else if (matches(argv[1], "-resolve") == 0) {
616                         ++resolve_hosts;
617                 } else if ((matches(argv[1], "-V") == 0) || (matches(argv[1], "--version") == 0)) {
618                         version();
619                 } else
620                         usage();
621                 argc--; argv++;
622         }
623
624         return do_iptunnel(argc-1, argv+1);
625 }