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