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