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