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