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