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