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 * Bernd Eckenfels 990715: add linux/types.h (not clean but solves missing __u16
17 * Arnaldo Carvalho de Melo 20010404: use setlocale
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <netinet/in.h>
29 #include <netinet/ip.h>
30 #include <arpa/inet.h>
31 #if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1))
33 #include <net/if_arp.h>
36 #include <linux/if_arp.h>
38 #include <linux/types.h>
39 #include <linux/if_tunnel.h>
43 #include "net-support.h"
48 #define GRE_CSUM htons(0x8000)
50 #define GRE_ROUTING htons(0x4000)
52 #define GRE_KEY htons(0x2000)
54 #define GRE_SEQ htons(0x1000)
56 #define GRE_STRICT htons(0x0800)
58 #define GRE_REC htons(0x0700)
60 #define GRE_FLAGS htons(0x00F8)
62 #define GRE_VERSION htons(0x0007)
64 /* Old versions of glibc do not define this */
65 #if __GLIBC__ == 2 && __GLIBC_MINOR__ == 0
66 #define IPPROTO_GRE 47
71 char *Release = RELEASE,
72 *Version = "iptunnel 1.01",
73 *Signature = "Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>";
75 static void version(void)
77 printf("%s\n%s\n%s\n", Release, Version, Signature);
81 static void usage(void) __attribute__((noreturn));
83 static void usage(void)
85 fprintf(stderr, _("Usage: iptunnel { add | change | del | show } [ NAME ]\n"));
86 fprintf(stderr, _(" [ mode { ipip | gre | sit } ] [ remote ADDR ] [ local ADDR ]\n"));
87 fprintf(stderr, _(" [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"));
88 fprintf(stderr, _(" [ ttl TTL ] [ tos TOS ] [ nopmtudisc ] [ dev PHYS_DEV ]\n"));
89 fprintf(stderr, _(" iptunnel -V | --version\n\n"));
90 fprintf(stderr, _("Where: NAME := STRING\n"));
91 fprintf(stderr, _(" ADDR := { IP_ADDRESS | any }\n"));
92 fprintf(stderr, _(" TOS := { NUMBER | inherit }\n"));
93 fprintf(stderr, _(" TTL := { 1..255 | inherit }\n"));
94 fprintf(stderr, _(" KEY := { DOTTED_QUAD | NUMBER }\n"));
98 static int do_ioctl_get_ifindex(char *dev)
104 strcpy(ifr.ifr_name, dev);
105 fd = socket(AF_INET, SOCK_DGRAM, 0);
106 err = ioctl(fd, SIOCGIFINDEX, &ifr);
112 return ifr.ifr_ifindex;
115 static int do_ioctl_get_iftype(char *dev)
121 strcpy(ifr.ifr_name, dev);
122 fd = socket(AF_INET, SOCK_DGRAM, 0);
123 err = ioctl(fd, SIOCGIFHWADDR, &ifr);
129 return ifr.ifr_addr.sa_family;
133 static char * do_ioctl_get_ifname(int idx)
135 static struct ifreq ifr;
139 ifr.ifr_ifindex = idx;
140 fd = socket(AF_INET, SOCK_DGRAM, 0);
141 err = ioctl(fd, SIOCGIFNAME, &ifr);
152 static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
158 strcpy(ifr.ifr_name, basedev);
159 ifr.ifr_ifru.ifru_data = (void*)p;
160 fd = socket(AF_INET, SOCK_DGRAM, 0);
161 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
168 static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
174 strcpy(ifr.ifr_name, basedev);
175 ifr.ifr_ifru.ifru_data = (void*)p;
176 fd = socket(AF_INET, SOCK_DGRAM, 0);
177 err = ioctl(fd, cmd, &ifr);
184 static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
190 strcpy(ifr.ifr_name, basedev);
191 ifr.ifr_ifru.ifru_data = (void*)p;
192 fd = socket(AF_INET, SOCK_DGRAM, 0);
193 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
200 static int parse_args(int argc, char **argv, struct ip_tunnel_parm *p)
202 char medium[IFNAMSIZ];
204 memset(p, 0, sizeof(*p));
205 memset(&medium, 0, sizeof(medium));
210 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
212 p->iph.frag_off = htons(IP_DF);
215 if (strcmp(*argv, "mode") == 0) {
217 if (strcmp(*argv, "ipip") == 0) {
220 p->iph.protocol = IPPROTO_IPIP;
221 } else if (strcmp(*argv, "gre") == 0) {
224 p->iph.protocol = IPPROTO_GRE;
225 } else if (strcmp(*argv, "sit") == 0) {
228 p->iph.protocol = IPPROTO_IPV6;
231 } else if (strcmp(*argv, "key") == 0) {
234 p->i_flags |= GRE_KEY;
235 p->o_flags |= GRE_KEY;
236 if (strchr(*argv, '.'))
237 p->i_key = p->o_key = get_addr32(*argv);
239 if (scan_number(*argv, &uval)<0)
241 p->i_key = p->o_key = htonl(uval);
243 } else if (strcmp(*argv, "ikey") == 0) {
246 p->i_flags |= GRE_KEY;
247 if (strchr(*argv, '.'))
248 p->o_key = get_addr32(*argv);
250 if (scan_number(*argv, &uval)<0)
252 p->i_key = htonl(uval);
254 } else if (strcmp(*argv, "okey") == 0) {
257 p->o_flags |= GRE_KEY;
258 if (strchr(*argv, '.'))
259 p->o_key = get_addr32(*argv);
261 if (scan_number(*argv, &uval)<0)
263 p->o_key = htonl(uval);
265 } else if (strcmp(*argv, "seq") == 0) {
266 p->i_flags |= GRE_SEQ;
267 p->o_flags |= GRE_SEQ;
268 } else if (strcmp(*argv, "iseq") == 0) {
269 p->i_flags |= GRE_SEQ;
270 } else if (strcmp(*argv, "oseq") == 0) {
271 p->o_flags |= GRE_SEQ;
272 } else if (strcmp(*argv, "csum") == 0) {
273 p->i_flags |= GRE_CSUM;
274 p->o_flags |= GRE_CSUM;
275 } else if (strcmp(*argv, "icsum") == 0) {
276 p->i_flags |= GRE_CSUM;
277 } else if (strcmp(*argv, "ocsum") == 0) {
278 p->o_flags |= GRE_CSUM;
279 } else if (strcmp(*argv, "nopmtudisc") == 0) {
281 } else if (strcmp(*argv, "remote") == 0) {
283 if (strcmp(*argv, "any"))
284 p->iph.daddr = get_addr32(*argv);
285 } else if (strcmp(*argv, "local") == 0) {
287 if (strcmp(*argv, "any"))
288 p->iph.saddr = get_addr32(*argv);
289 } else if (strcmp(*argv, "dev") == 0) {
291 safe_strncpy(medium, *argv, IFNAMSIZ-1);
292 } else if (strcmp(*argv, "ttl") == 0) {
295 if (strcmp(*argv, "inherit") != 0) {
296 if (scan_number(*argv, &uval)<0)
302 } else if (strcmp(*argv, "tos") == 0) {
305 if (strcmp(*argv, "inherit") != 0) {
306 if (scan_number(*argv, &uval)<0)
316 safe_strncpy(p->name, *argv, IFNAMSIZ);
321 if (p->iph.protocol == 0) {
322 if (memcmp(p->name, "gre", 3) == 0)
323 p->iph.protocol = IPPROTO_GRE;
324 else if (memcmp(p->name, "ipip", 4) == 0)
325 p->iph.protocol = IPPROTO_IPIP;
326 else if (memcmp(p->name, "sit", 3) == 0)
327 p->iph.protocol = IPPROTO_IPV6;
330 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
331 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
332 fprintf(stderr, _("Keys are not allowed with ipip and sit.\n"));
338 p->link = do_ioctl_get_ifindex(medium);
343 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
344 p->i_key = p->iph.daddr;
345 p->i_flags |= GRE_KEY;
347 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
348 p->o_key = p->iph.daddr;
349 p->o_flags |= GRE_KEY;
351 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
352 fprintf(stderr, _("Broadcast tunnel requires a source address.\n"));
359 static int do_add(int cmd, int argc, char **argv)
361 struct ip_tunnel_parm p;
363 if (parse_args(argc, argv, &p) < 0)
366 if (p.iph.ttl && p.iph.frag_off == 0) {
367 fprintf(stderr, _("ttl != 0 and noptmudisc are incompatible\n"));
371 switch (p.iph.protocol) {
373 return do_add_ioctl(cmd, "tunl0", &p);
375 return do_add_ioctl(cmd, "gre0", &p);
377 return do_add_ioctl(cmd, "sit0", &p);
379 fprintf(stderr, _("cannot determine tunnel mode (ipip, gre or sit)\n"));
385 int do_del(int argc, char **argv)
387 struct ip_tunnel_parm p;
389 if (parse_args(argc, argv, &p) < 0)
392 switch (p.iph.protocol) {
394 return do_del_ioctl(p.name[0] ? p.name : "tunl0", &p);
396 return do_del_ioctl(p.name[0] ? p.name : "gre0", &p);
398 return do_del_ioctl(p.name[0] ? p.name : "sit0", &p);
400 return do_del_ioctl(p.name, &p);
405 void print_tunnel(struct ip_tunnel_parm *p)
412 format_host(AF_INET, &p->iph.daddr, s1, sizeof(s1));
413 format_host(AF_INET, &p->iph.saddr, s2, sizeof(s2));
414 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
415 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
417 printf(_("%s: %s/ip remote %s local %s "),
419 p->iph.protocol == IPPROTO_IPIP ? "ip" :
420 (p->iph.protocol == IPPROTO_GRE ? "gre" :
421 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : _("unknown"))),
422 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
424 char *n = do_ioctl_get_ifname(p->link);
426 printf(" dev %s ", n);
429 printf(" ttl %d ", p->iph.ttl);
431 printf(" ttl inherit ");
437 printf("%c%02x ", p->iph.tos&1 ? '/' : ' ', p->iph.tos&~1);
439 if (!(p->iph.frag_off&htons(IP_DF)))
440 printf(" nopmtudisc");
442 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
443 printf(" key %s", s3);
444 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
445 if (p->i_flags&GRE_KEY)
446 printf(" ikey %s ", s3);
447 if (p->o_flags&GRE_KEY)
448 printf(" okey %s ", s4);
452 if (p->i_flags&GRE_SEQ)
453 printf(_(" Drop packets out of sequence.\n"));
454 if (p->i_flags&GRE_CSUM)
455 printf(_(" Checksum in received packet is required.\n"));
456 if (p->o_flags&GRE_SEQ)
457 printf(_(" Sequence packets on output.\n"));
458 if (p->o_flags&GRE_CSUM)
459 printf(_(" Checksum output packets.\n"));
462 static int do_tunnels_list(struct ip_tunnel_parm *p)
465 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
467 tx_bytes, tx_packets, tx_errs, tx_drops,
468 tx_fifo, tx_colls, tx_carrier, rx_multi;
470 struct ip_tunnel_parm p1;
473 FILE *fp = fopen("/proc/net/dev", "r");
479 fgets(buf, sizeof(buf), fp);
480 fgets(buf, sizeof(buf), fp);
482 while (fgets(buf, sizeof(buf), fp) != NULL) {
484 buf[sizeof(buf) - 1] = 0;
485 if ((ptr = strchr(buf, ':')) == NULL ||
486 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
487 fprintf(stderr, _("Wrong format of /proc/net/dev. Sorry.\n"));
490 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
491 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
492 &rx_fifo, &rx_frame, &rx_multi,
493 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
494 &tx_fifo, &tx_colls, &tx_carrier) != 14)
496 if (p->name[0] && strcmp(p->name, name))
498 type = do_ioctl_get_iftype(name);
500 fprintf(stderr, _("Failed to get type of [%s]\n"), name);
503 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
505 memset(&p1, 0, sizeof(p1));
506 if (do_get_ioctl(name, &p1))
508 if ((p->link && p1.link != p->link) ||
509 (p->name[0] && strcmp(p1.name, p->name)) ||
510 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
511 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
512 (p->i_key && p1.i_key != p->i_key))
516 printf(_("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts\n"));
517 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld\n",
518 rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi);
519 printf(_("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs\n"));
520 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld\n\n",
521 tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
527 static int do_show(int argc, char **argv)
530 struct ip_tunnel_parm p;
532 if (parse_args(argc, argv, &p) < 0)
535 switch (p.iph.protocol) {
537 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
540 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
543 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
556 int do_iptunnel(int argc, char **argv)
559 if (matches(*argv, "add") == 0)
560 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
561 if (matches(*argv, "change") == 0)
562 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
563 if (matches(*argv, "del") == 0)
564 return do_del(argc-1, argv+1);
565 if (matches(*argv, "show") == 0 ||
566 matches(*argv, "lst") == 0 ||
567 matches(*argv, "list") == 0)
568 return do_show(argc-1, argv+1);
570 return do_show(0, NULL);
576 int preferred_family = AF_UNSPEC;
578 int resolve_hosts = 0;
580 int main(int argc, char **argv)
585 setlocale (LC_ALL, "");
586 bindtextdomain("net-tools", "/usr/share/locale");
587 textdomain("net-tools");
590 basename = strrchr(argv[0], '/');
591 if (basename == NULL)
597 if (argv[1][0] != '-')
599 if (matches(argv[1], "-family") == 0) {
604 if (strcmp(argv[1], "inet") == 0)
605 preferred_family = AF_INET;
606 else if (strcmp(argv[1], "inet6") == 0)
607 preferred_family = AF_INET6;
610 } else if (matches(argv[1], "-stats") == 0 ||
611 matches(argv[1], "-statistics") == 0) {
613 } else if (matches(argv[1], "-resolve") == 0) {
615 } else if ((matches(argv[1], "-V") == 0) || (matches(argv[1], "--version") == 0)) {
622 return do_iptunnel(argc-1, argv+1);