2 * iptunnel.c "ip tunnel"
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <arpa/inet.h>
26 #include <sys/ioctl.h>
28 #include <linux/if_arp.h>
30 #include <linux/if_tunnel.h>
34 #include "ip_common.h"
37 #define ETH_P_ETH 0x6558 /* Ethernet in Ethernet */
40 static void usage(void) __attribute__((noreturn));
42 static void usage(void)
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");
57 static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
60 char medium[IFNAMSIZ];
63 memset(p, 0, sizeof(*p));
64 memset(&medium, 0, sizeof(medium));
69 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
71 p->iph.frag_off = htons(IP_DF);
74 if (strcmp(*argv, "mode") == 0) {
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");
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");
89 p->iph.protocol = IPPROTO_GRE;
91 if (strcmp(*argv,"type") == 0)
94 printf("90 route %s\n", *argv);
95 if (strcmp(*argv,"eth") == 0)
97 p->proto_type = ETH_P_ETH;
101 p->proto_type = ETH_P_IP;
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");
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");
116 p->iph.protocol = IPPROTO_IPV6;
119 fprintf(stderr,"Cannot guess tunnel mode.\n");
122 } else if (strcmp(*argv, "key") == 0) {
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);
130 if (get_unsigned(&uval, *argv, 0)<0) {
131 fprintf(stderr, "invalid value of \"key\"\n");
134 p->i_key = p->o_key = htonl(uval);
136 } else if (strcmp(*argv, "ikey") == 0) {
139 p->i_flags |= GRE_KEY;
140 if (strchr(*argv, '.'))
141 p->i_key = get_addr32(*argv);
143 if (get_unsigned(&uval, *argv, 0)<0) {
144 fprintf(stderr, "invalid value of \"ikey\"\n");
147 p->i_key = htonl(uval);
149 } else if (strcmp(*argv, "okey") == 0) {
152 p->o_flags |= GRE_KEY;
153 if (strchr(*argv, '.'))
154 p->o_key = get_addr32(*argv);
156 if (get_unsigned(&uval, *argv, 0)<0) {
157 fprintf(stderr, "invalid value of \"okey\"\n");
160 p->o_key = htonl(uval);
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) {
178 } else if (strcmp(*argv, "pmtudisc") == 0) {
179 p->iph.frag_off = htons(IP_DF);
180 } else if (strcmp(*argv, "remote") == 0) {
182 if (strcmp(*argv, "any"))
183 p->iph.daddr = get_addr32(*argv);
184 } else if (strcmp(*argv, "local") == 0) {
186 if (strcmp(*argv, "any"))
187 p->iph.saddr = get_addr32(*argv);
188 } else if (strcmp(*argv, "dev") == 0) {
190 strncpy(medium, *argv, IFNAMSIZ-1);
191 } else if (strcmp(*argv, "ttl") == 0 ||
192 strcmp(*argv, "hoplimit") == 0) {
195 if (strcmp(*argv, "inherit") != 0) {
196 if (get_unsigned(&uval, *argv, 0))
197 invarg("invalid TTL\n", *argv);
199 invarg("TTL must be <=255\n", *argv);
202 } else if (strcmp(*argv, "tos") == 0 ||
203 strcmp(*argv, "tclass") == 0 ||
204 matches(*argv, "dsfield") == 0) {
207 if (strcmp(*argv, "inherit") != 0) {
208 if (rtnl_dsfield_a2n(&uval, *argv))
209 invarg("bad TOS value", *argv);
214 if (strcmp(*argv, "name") == 0) {
216 } else if (matches(*argv, "help") == 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))
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;
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");
255 p->link = tnl_ioctl_get_ifindex(medium);
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;
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;
268 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
269 fprintf(stderr, "Broadcast tunnel requires a source address.\n");
274 fprintf(stderr, "no remote with isatap.\n");
277 p->i_flags |= SIT_ISATAP;
284 static int do_add(int cmd, int argc, char **argv)
286 struct ip_tunnel_parm p;
288 if (parse_args(argc, argv, cmd, &p) < 0)
291 if (p.iph.ttl && p.iph.frag_off == 0) {
292 fprintf(stderr, "ttl != 0 and noptmudisc are incompatible\n");
296 switch (p.iph.protocol) {
298 return tnl_add_ioctl(cmd, "tunl0", p.name, &p);
300 return tnl_add_ioctl(cmd, "gre0", p.name, &p);
302 return tnl_add_ioctl(cmd, "sit0", p.name, &p);
304 fprintf(stderr, "cannot determine tunnel mode (ipip, gre or sit)\n");
310 static int do_del(int argc, char **argv)
312 struct ip_tunnel_parm p;
314 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
317 switch (p.iph.protocol) {
319 return tnl_del_ioctl("tunl0", p.name, &p);
321 return tnl_del_ioctl("gre0", p.name, &p);
323 return tnl_del_ioctl("sit0", p.name, &p);
325 return tnl_del_ioctl(p.name, p.name, &p);
330 static void print_tunnel(struct ip_tunnel_parm *p)
337 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
338 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
340 /* Do not use format_host() for local addr,
341 * symbolic name will not be useful.
343 if(p->proto_type == ETH_P_ETH)
344 printf("%s: %s/eth remote %s local %s ",
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");
350 printf("%s: %s/ip remote %s local %s ",
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");
357 char *n = tnl_ioctl_get_ifname(p->link);
359 printf(" dev %s ", n);
363 printf(" ttl %d ", p->iph.ttl);
365 printf(" ttl inherit ");
373 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
374 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
377 if (!(p->iph.frag_off&htons(IP_DF)))
378 printf(" nopmtudisc");
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);
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_);
399 static int do_tunnels_list(struct ip_tunnel_parm *p)
402 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
404 tx_bytes, tx_packets, tx_errs, tx_drops,
405 tx_fifo, tx_colls, tx_carrier, rx_multi;
407 struct ip_tunnel_parm p1;
410 FILE *fp = fopen("/proc/net/dev", "r");
416 fgets(buf, sizeof(buf), fp);
417 fgets(buf, sizeof(buf), fp);
419 while (fgets(buf, sizeof(buf), fp) != NULL) {
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");
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)
433 if (p->name[0] && strcmp(p->name, name))
435 type = tnl_ioctl_get_iftype(name);
437 fprintf(stderr, "Failed to get type of [%s]\n", name);
440 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
442 memset(&p1, 0, sizeof(p1));
443 if (tnl_get_ioctl(name, &p1))
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))
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);
466 static int do_show(int argc, char **argv)
469 struct ip_tunnel_parm p;
471 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
474 switch (p.iph.protocol) {
476 err = tnl_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
479 err = tnl_get_ioctl(p.name[0] ? p.name : "gre0", &p);
482 err = tnl_get_ioctl(p.name[0] ? p.name : "sit0", &p);
496 int do_iptunnel(int argc, char **argv)
498 switch (preferred_family) {
500 preferred_family = AF_INET;
505 * This is silly enough but we have no easy way to make it
506 * protocol-independent because of unarranged structure between
510 return do_ip6tunnel(argc, argv);
512 fprintf(stderr, "Unsupported family:%d\n", preferred_family);
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)
530 return do_show(0, NULL);
532 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv);