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