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