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