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