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