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
25 #include <sys/socket.h>
26 #include <sys/ioctl.h>
27 #include <netinet/in.h>
28 #include <netinet/ip.h>
29 #include <arpa/inet.h>
30 #if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1))
32 #include <net/if_arp.h>
35 #include <linux/if_arp.h>
37 #include <linux/types.h>
38 #include <linux/if_tunnel.h>
42 #include "net-support.h"
47 #define GRE_CSUM htons(0x8000)
49 #define GRE_ROUTING htons(0x4000)
51 #define GRE_KEY htons(0x2000)
53 #define GRE_SEQ htons(0x1000)
55 #define GRE_STRICT htons(0x0800)
57 #define GRE_REC htons(0x0700)
59 #define GRE_FLAGS htons(0x00F8)
61 #define GRE_VERSION htons(0x0007)
63 /* Old versions of glibc do not define this */
64 #if __GLIBC__ == 2 && __GLIBC_MINOR__ == 0
65 #define IPPROTO_GRE 47
70 char *Release = RELEASE,
71 *Version = "iptunnel 1.01",
72 *Signature = "Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>";
74 static void version(void)
76 printf("%s\n%s\n%s\n", Release, Version, Signature);
80 static void usage(void) __attribute__((noreturn));
82 static void usage(void)
84 fprintf(stderr, _("Usage: iptunnel { add | change | del | show } [ NAME ]\n"));
85 fprintf(stderr, _(" [ mode { ipip | gre | sit } ] [ remote ADDR ] [ local ADDR ]\n"));
86 fprintf(stderr, _(" [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"));
87 fprintf(stderr, _(" [ ttl TTL ] [ tos TOS ] [ nopmtudisc ] [ dev PHYS_DEV ]\n"));
88 fprintf(stderr, _(" iptunnel -V | --version\n\n"));
89 fprintf(stderr, _("Where: NAME := STRING\n"));
90 fprintf(stderr, _(" ADDR := { IP_ADDRESS | any }\n"));
91 fprintf(stderr, _(" TOS := { NUMBER | inherit }\n"));
92 fprintf(stderr, _(" TTL := { 1..255 | inherit }\n"));
93 fprintf(stderr, _(" KEY := { DOTTED_QUAD | NUMBER }\n"));
97 static int do_ioctl_get_ifindex(char *dev)
103 strcpy(ifr.ifr_name, dev);
104 fd = socket(AF_INET, SOCK_DGRAM, 0);
105 err = ioctl(fd, SIOCGIFINDEX, &ifr);
111 return ifr.ifr_ifindex;
114 static int do_ioctl_get_iftype(char *dev)
120 strcpy(ifr.ifr_name, dev);
121 fd = socket(AF_INET, SOCK_DGRAM, 0);
122 err = ioctl(fd, SIOCGIFHWADDR, &ifr);
128 return ifr.ifr_addr.sa_family;
132 static char * do_ioctl_get_ifname(int idx)
134 static struct ifreq ifr;
138 ifr.ifr_ifindex = idx;
139 fd = socket(AF_INET, SOCK_DGRAM, 0);
140 err = ioctl(fd, SIOCGIFNAME, &ifr);
151 static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
157 strcpy(ifr.ifr_name, basedev);
158 ifr.ifr_ifru.ifru_data = (void*)p;
159 fd = socket(AF_INET, SOCK_DGRAM, 0);
160 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
167 static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
173 strcpy(ifr.ifr_name, basedev);
174 ifr.ifr_ifru.ifru_data = (void*)p;
175 fd = socket(AF_INET, SOCK_DGRAM, 0);
176 err = ioctl(fd, cmd, &ifr);
183 static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
189 strcpy(ifr.ifr_name, basedev);
190 ifr.ifr_ifru.ifru_data = (void*)p;
191 fd = socket(AF_INET, SOCK_DGRAM, 0);
192 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
199 static int parse_args(int argc, char **argv, struct ip_tunnel_parm *p)
201 char medium[IFNAMSIZ];
203 memset(p, 0, sizeof(*p));
204 memset(&medium, 0, sizeof(medium));
209 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
211 p->iph.frag_off = htons(IP_DF);
214 if (strcmp(*argv, "mode") == 0) {
216 if (strcmp(*argv, "ipip") == 0) {
219 p->iph.protocol = IPPROTO_IPIP;
220 } else if (strcmp(*argv, "gre") == 0) {
223 p->iph.protocol = IPPROTO_GRE;
224 } else if (strcmp(*argv, "sit") == 0) {
227 p->iph.protocol = IPPROTO_IPV6;
230 } else if (strcmp(*argv, "key") == 0) {
233 p->i_flags |= GRE_KEY;
234 p->o_flags |= GRE_KEY;
235 if (strchr(*argv, '.'))
236 p->i_key = p->o_key = get_addr32(*argv);
238 if (scan_number(*argv, &uval)<0)
240 p->i_key = p->o_key = htonl(uval);
242 } else if (strcmp(*argv, "ikey") == 0) {
245 p->i_flags |= GRE_KEY;
246 if (strchr(*argv, '.'))
247 p->o_key = get_addr32(*argv);
249 if (scan_number(*argv, &uval)<0)
251 p->i_key = htonl(uval);
253 } else if (strcmp(*argv, "okey") == 0) {
256 p->o_flags |= GRE_KEY;
257 if (strchr(*argv, '.'))
258 p->o_key = get_addr32(*argv);
260 if (scan_number(*argv, &uval)<0)
262 p->o_key = htonl(uval);
264 } else if (strcmp(*argv, "seq") == 0) {
265 p->i_flags |= GRE_SEQ;
266 p->o_flags |= GRE_SEQ;
267 } else if (strcmp(*argv, "iseq") == 0) {
268 p->i_flags |= GRE_SEQ;
269 } else if (strcmp(*argv, "oseq") == 0) {
270 p->o_flags |= GRE_SEQ;
271 } else if (strcmp(*argv, "csum") == 0) {
272 p->i_flags |= GRE_CSUM;
273 p->o_flags |= GRE_CSUM;
274 } else if (strcmp(*argv, "icsum") == 0) {
275 p->i_flags |= GRE_CSUM;
276 } else if (strcmp(*argv, "ocsum") == 0) {
277 p->o_flags |= GRE_CSUM;
278 } else if (strcmp(*argv, "nopmtudisc") == 0) {
280 } else if (strcmp(*argv, "remote") == 0) {
282 if (strcmp(*argv, "any"))
283 p->iph.daddr = get_addr32(*argv);
284 } else if (strcmp(*argv, "local") == 0) {
286 if (strcmp(*argv, "any"))
287 p->iph.saddr = get_addr32(*argv);
288 } else if (strcmp(*argv, "dev") == 0) {
290 safe_strncpy(medium, *argv, IFNAMSIZ-1);
291 } else if (strcmp(*argv, "ttl") == 0) {
294 if (strcmp(*argv, "inherit") != 0) {
295 if (scan_number(*argv, &uval)<0)
301 } else if (strcmp(*argv, "tos") == 0) {
304 if (strcmp(*argv, "inherit") != 0) {
305 if (scan_number(*argv, &uval)<0)
315 safe_strncpy(p->name, *argv, IFNAMSIZ);
320 if (p->iph.protocol == 0) {
321 if (memcmp(p->name, "gre", 3) == 0)
322 p->iph.protocol = IPPROTO_GRE;
323 else if (memcmp(p->name, "ipip", 4) == 0)
324 p->iph.protocol = IPPROTO_IPIP;
325 else if (memcmp(p->name, "sit", 3) == 0)
326 p->iph.protocol = IPPROTO_IPV6;
329 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
330 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
331 fprintf(stderr, _("Keys are not allowed with ipip and sit.\n"));
337 p->link = do_ioctl_get_ifindex(medium);
342 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
343 p->i_key = p->iph.daddr;
344 p->i_flags |= GRE_KEY;
346 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
347 p->o_key = p->iph.daddr;
348 p->o_flags |= GRE_KEY;
350 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
351 fprintf(stderr, _("Broadcast tunnel requires a source address.\n"));
358 static int do_add(int cmd, int argc, char **argv)
360 struct ip_tunnel_parm p;
362 if (parse_args(argc, argv, &p) < 0)
365 if (p.iph.ttl && p.iph.frag_off == 0) {
366 fprintf(stderr, _("ttl != 0 and noptmudisc are incompatible\n"));
370 switch (p.iph.protocol) {
372 return do_add_ioctl(cmd, "tunl0", &p);
374 return do_add_ioctl(cmd, "gre0", &p);
376 return do_add_ioctl(cmd, "sit0", &p);
378 fprintf(stderr, _("cannot determine tunnel mode (ipip, gre or sit)\n"));
384 int do_del(int argc, char **argv)
386 struct ip_tunnel_parm p;
388 if (parse_args(argc, argv, &p) < 0)
391 switch (p.iph.protocol) {
393 return do_del_ioctl(p.name[0] ? p.name : "tunl0", &p);
395 return do_del_ioctl(p.name[0] ? p.name : "gre0", &p);
397 return do_del_ioctl(p.name[0] ? p.name : "sit0", &p);
399 return do_del_ioctl(p.name, &p);
404 void print_tunnel(struct ip_tunnel_parm *p)
411 format_host(AF_INET, &p->iph.daddr, s1, sizeof(s1));
412 format_host(AF_INET, &p->iph.saddr, s2, sizeof(s2));
413 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
414 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
416 printf(_("%s: %s/ip remote %s local %s "),
418 p->iph.protocol == IPPROTO_IPIP ? "ip" :
419 (p->iph.protocol == IPPROTO_GRE ? "gre" :
420 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : _("unknown"))),
421 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
423 char *n = do_ioctl_get_ifname(p->link);
425 printf(" dev %s ", n);
428 printf(" ttl %d ", p->iph.ttl);
430 printf(" ttl inherit ");
436 printf("%c%02x ", p->iph.tos&1 ? '/' : ' ', p->iph.tos&~1);
438 if (!(p->iph.frag_off&htons(IP_DF)))
439 printf(" nopmtudisc");
441 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
442 printf(" key %s", s3);
443 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
444 if (p->i_flags&GRE_KEY)
445 printf(" ikey %s ", s3);
446 if (p->o_flags&GRE_KEY)
447 printf(" okey %s ", s4);
451 if (p->i_flags&GRE_SEQ)
452 printf(_(" Drop packets out of sequence.\n"));
453 if (p->i_flags&GRE_CSUM)
454 printf(_(" Checksum in received packet is required.\n"));
455 if (p->o_flags&GRE_SEQ)
456 printf(_(" Sequence packets on output.\n"));
457 if (p->o_flags&GRE_CSUM)
458 printf(_(" Checksum output packets.\n"));
461 static int do_tunnels_list(struct ip_tunnel_parm *p)
464 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
466 tx_bytes, tx_packets, tx_errs, tx_drops,
467 tx_fifo, tx_colls, tx_carrier, rx_multi;
469 struct ip_tunnel_parm p1;
472 FILE *fp = fopen("/proc/net/dev", "r");
478 fgets(buf, sizeof(buf), fp);
479 fgets(buf, sizeof(buf), fp);
481 while (fgets(buf, sizeof(buf), fp) != NULL) {
483 buf[sizeof(buf) - 1] = 0;
484 if ((ptr = strchr(buf, ':')) == NULL ||
485 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
486 fprintf(stderr, _("Wrong format of /proc/net/dev. Sorry.\n"));
489 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
490 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
491 &rx_fifo, &rx_frame, &rx_multi,
492 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
493 &tx_fifo, &tx_colls, &tx_carrier) != 14)
495 if (p->name[0] && strcmp(p->name, name))
497 type = do_ioctl_get_iftype(name);
499 fprintf(stderr, _("Failed to get type of [%s]\n"), name);
502 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
504 memset(&p1, 0, sizeof(p1));
505 if (do_get_ioctl(name, &p1))
507 if ((p->link && p1.link != p->link) ||
508 (p->name[0] && strcmp(p1.name, p->name)) ||
509 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
510 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
511 (p->i_key && p1.i_key != p->i_key))
515 printf(_("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts\n"));
516 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld\n",
517 rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi);
518 printf(_("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs\n"));
519 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld\n\n",
520 tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
526 static int do_show(int argc, char **argv)
529 struct ip_tunnel_parm p;
531 if (parse_args(argc, argv, &p) < 0)
534 switch (p.iph.protocol) {
536 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
539 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
542 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
555 int do_iptunnel(int argc, char **argv)
558 if (matches(*argv, "add") == 0)
559 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
560 if (matches(*argv, "change") == 0)
561 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
562 if (matches(*argv, "del") == 0)
563 return do_del(argc-1, argv+1);
564 if (matches(*argv, "show") == 0 ||
565 matches(*argv, "lst") == 0 ||
566 matches(*argv, "list") == 0)
567 return do_show(argc-1, argv+1);
569 return do_show(0, NULL);
575 int preferred_family = AF_UNSPEC;
577 int resolve_hosts = 0;
579 int main(int argc, char **argv)
584 bindtextdomain("net-tools", "/usr/share/locale");
585 textdomain("net-tools");
588 basename = strrchr(argv[0], '/');
589 if (basename == NULL)
595 if (argv[1][0] != '-')
597 if (matches(argv[1], "-family") == 0) {
602 if (strcmp(argv[1], "inet") == 0)
603 preferred_family = AF_INET;
604 else if (strcmp(argv[1], "inet6") == 0)
605 preferred_family = AF_INET6;
608 } else if (matches(argv[1], "-stats") == 0 ||
609 matches(argv[1], "-statistics") == 0) {
611 } else if (matches(argv[1], "-resolve") == 0) {
613 } else if ((matches(argv[1], "-V") == 0) || (matches(argv[1], "--version") == 0)) {
620 return do_iptunnel(argc-1, argv+1);