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
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <sys/ioctl.h>
30 #include <linux/if_arp.h>
33 #ifndef __constant_htons
34 #define __constant_htons(x) htons(x)
37 #include <linux/if_tunnel.h>
42 static void usage(void) __attribute__((noreturn));
44 static void usage(void)
46 fprintf(stderr, "Usage: ip tunnel { add | change | del | show } [ NAME ]\n");
47 fprintf(stderr, " [ mode { ipip | gre | gre/ip | gre/eth | sit } ] [ remote ADDR ] [ local ADDR ]\n");
48 fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
49 fprintf(stderr, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
50 fprintf(stderr, "\n");
51 fprintf(stderr, "Where: NAME := STRING\n");
52 fprintf(stderr, " ADDR := { IP_ADDRESS | any }\n");
53 fprintf(stderr, " TOS := { NUMBER | inherit }\n");
54 fprintf(stderr, " TTL := { 1..255 | inherit }\n");
55 fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n");
59 static int do_ioctl_get_ifindex(const char *dev)
65 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
66 fd = socket(AF_INET, SOCK_DGRAM, 0);
67 err = ioctl(fd, SIOCGIFINDEX, &ifr);
73 return ifr.ifr_ifindex;
76 static int do_ioctl_get_iftype(const char *dev)
82 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
83 fd = socket(AF_INET, SOCK_DGRAM, 0);
84 err = ioctl(fd, SIOCGIFHWADDR, &ifr);
90 return ifr.ifr_addr.sa_family;
94 static char * do_ioctl_get_ifname(int idx)
96 static struct ifreq ifr;
100 ifr.ifr_ifindex = idx;
101 fd = socket(AF_INET, SOCK_DGRAM, 0);
102 err = ioctl(fd, SIOCGIFNAME, &ifr);
112 static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
118 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
119 ifr.ifr_ifru.ifru_data = (void*)p;
120 fd = socket(AF_INET, SOCK_DGRAM, 0);
121 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
128 static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
134 if (cmd == SIOCCHGTUNNEL && p->name[0])
135 strncpy(ifr.ifr_name, p->name, IFNAMSIZ);
137 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
138 ifr.ifr_ifru.ifru_data = (void*)p;
139 fd = socket(AF_INET, SOCK_DGRAM, 0);
140 err = ioctl(fd, cmd, &ifr);
147 static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
154 strncpy(ifr.ifr_name, p->name, IFNAMSIZ);
156 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
157 ifr.ifr_ifru.ifru_data = (void*)p;
158 fd = socket(AF_INET, SOCK_DGRAM, 0);
159 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
166 static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
169 char medium[IFNAMSIZ];
171 memset(p, 0, sizeof(*p));
172 memset(&medium, 0, sizeof(medium));
177 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
179 p->iph.frag_off = htons(IP_DF);
182 if (strcmp(*argv, "mode") == 0) {
184 if (strcmp(*argv, "ipip") == 0 ||
185 strcmp(*argv, "ip/ip") == 0) {
186 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
187 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
190 p->iph.protocol = IPPROTO_IPIP;
191 } else if (strcmp(*argv, "gre") == 0 ||
192 strcmp(*argv, "gre/ip") == 0 ||
193 strcmp(*argv, "gre/eth") == 0) {
194 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
195 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
198 p->iph.protocol = IPPROTO_GRE;
199 if (strcmp(*argv,"gre/eth") == 0)
200 p->proto_type = ETH_P_ETH;
202 p->proto_type = ETH_P_IP;
203 } else if (strcmp(*argv, "sit") == 0 ||
204 strcmp(*argv, "ipv6/ip") == 0) {
205 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
206 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
209 p->iph.protocol = IPPROTO_IPV6;
211 fprintf(stderr,"Cannot guess tunnel mode.\n");
214 } else if (strcmp(*argv, "key") == 0) {
217 p->i_flags |= GRE_KEY;
218 p->o_flags |= GRE_KEY;
219 if (strchr(*argv, '.'))
220 p->i_key = p->o_key = get_addr32(*argv);
222 if (get_unsigned(&uval, *argv, 0)<0) {
223 fprintf(stderr, "invalid value of \"key\"\n");
226 p->i_key = p->o_key = htonl(uval);
228 } else if (strcmp(*argv, "ikey") == 0) {
231 p->i_flags |= GRE_KEY;
232 if (strchr(*argv, '.'))
233 p->o_key = get_addr32(*argv);
235 if (get_unsigned(&uval, *argv, 0)<0) {
236 fprintf(stderr, "invalid value of \"ikey\"\n");
239 p->i_key = htonl(uval);
241 } else if (strcmp(*argv, "okey") == 0) {
244 p->o_flags |= GRE_KEY;
245 if (strchr(*argv, '.'))
246 p->o_key = get_addr32(*argv);
248 if (get_unsigned(&uval, *argv, 0)<0) {
249 fprintf(stderr, "invalid value of \"okey\"\n");
252 p->o_key = htonl(uval);
254 } else if (strcmp(*argv, "seq") == 0) {
255 p->i_flags |= GRE_SEQ;
256 p->o_flags |= GRE_SEQ;
257 } else if (strcmp(*argv, "iseq") == 0) {
258 p->i_flags |= GRE_SEQ;
259 } else if (strcmp(*argv, "oseq") == 0) {
260 p->o_flags |= GRE_SEQ;
261 } else if (strcmp(*argv, "csum") == 0) {
262 p->i_flags |= GRE_CSUM;
263 p->o_flags |= GRE_CSUM;
264 } else if (strcmp(*argv, "icsum") == 0) {
265 p->i_flags |= GRE_CSUM;
266 } else if (strcmp(*argv, "ocsum") == 0) {
267 p->o_flags |= GRE_CSUM;
268 } else if (strcmp(*argv, "nopmtudisc") == 0) {
270 } else if (strcmp(*argv, "pmtudisc") == 0) {
271 p->iph.frag_off = htons(IP_DF);
272 } else if (strcmp(*argv, "remote") == 0) {
274 if (strcmp(*argv, "any"))
275 p->iph.daddr = get_addr32(*argv);
276 } else if (strcmp(*argv, "local") == 0) {
278 if (strcmp(*argv, "any"))
279 p->iph.saddr = get_addr32(*argv);
280 } else if (strcmp(*argv, "dev") == 0) {
282 strncpy(medium, *argv, IFNAMSIZ-1);
283 } else if (strcmp(*argv, "ttl") == 0) {
286 if (strcmp(*argv, "inherit") != 0) {
287 if (get_unsigned(&uval, *argv, 0))
288 invarg("invalid TTL\n", *argv);
290 invarg("TTL must be <=255\n", *argv);
293 } else if (strcmp(*argv, "tos") == 0 ||
294 matches(*argv, "dsfield") == 0) {
297 if (strcmp(*argv, "inherit") != 0) {
298 if (rtnl_dsfield_a2n(&uval, *argv))
299 invarg("bad TOS value", *argv);
304 if (strcmp(*argv, "name") == 0) {
307 if (matches(*argv, "help") == 0)
310 duparg2("name", *argv);
311 strncpy(p->name, *argv, IFNAMSIZ);
312 if (cmd == SIOCCHGTUNNEL && count == 0) {
313 struct ip_tunnel_parm old_p;
314 memset(&old_p, 0, sizeof(old_p));
315 if (do_get_ioctl(*argv, &old_p))
325 if (p->iph.protocol == 0) {
326 if (memcmp(p->name, "gre", 3) == 0)
327 p->iph.protocol = IPPROTO_GRE;
328 else if (memcmp(p->name, "ipip", 4) == 0)
329 p->iph.protocol = IPPROTO_IPIP;
330 else if (memcmp(p->name, "sit", 3) == 0)
331 p->iph.protocol = IPPROTO_IPV6;
334 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
335 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
336 fprintf(stderr, "Keys are not allowed with ipip and sit.\n");
342 p->link = do_ioctl_get_ifindex(medium);
347 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
348 p->i_key = p->iph.daddr;
349 p->i_flags |= GRE_KEY;
351 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
352 p->o_key = p->iph.daddr;
353 p->o_flags |= GRE_KEY;
355 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
356 fprintf(stderr, "Broadcast tunnel requires a source address.\n");
363 static int do_add(int cmd, int argc, char **argv)
365 struct ip_tunnel_parm p;
367 if (parse_args(argc, argv, cmd, &p) < 0)
370 if (p.iph.ttl && p.iph.frag_off == 0) {
371 fprintf(stderr, "ttl != 0 and noptmudisc are incompatible\n");
375 switch (p.iph.protocol) {
377 return do_add_ioctl(cmd, "tunl0", &p);
379 return do_add_ioctl(cmd, "gre0", &p);
381 return do_add_ioctl(cmd, "sit0", &p);
383 fprintf(stderr, "cannot determine tunnel mode (ipip, gre or sit)\n");
389 int do_del(int argc, char **argv)
391 struct ip_tunnel_parm p;
393 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
396 switch (p.iph.protocol) {
398 return do_del_ioctl("tunl0", &p);
400 return do_del_ioctl("gre0", &p);
402 return do_del_ioctl("sit0", &p);
404 return do_del_ioctl(p.name, &p);
409 void print_tunnel(struct ip_tunnel_parm *p)
416 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
417 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
419 /* Do not use format_host() for local addr,
420 * symbolic name will not be useful.
422 printf("%s: %s/%s remote %s local %s ",
424 p->iph.protocol == IPPROTO_IPIP ? "ip" :
425 (p->iph.protocol == IPPROTO_GRE ? "gre" :
426 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
427 (p->proto_type == ETH_P_ETH ? "eth" : "ip"),
428 p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any",
429 p->iph.saddr ? rt_addr_n2a(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any");
432 char *n = do_ioctl_get_ifname(p->link);
434 printf(" dev %s ", n);
438 printf(" ttl %d ", p->iph.ttl);
440 printf(" ttl inherit ");
448 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
449 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
452 if (!(p->iph.frag_off&htons(IP_DF)))
453 printf(" nopmtudisc");
455 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
456 printf(" key %s", s3);
457 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
458 if (p->i_flags&GRE_KEY)
459 printf(" ikey %s ", s3);
460 if (p->o_flags&GRE_KEY)
461 printf(" okey %s ", s4);
464 if (p->i_flags&GRE_SEQ)
465 printf("%s Drop packets out of sequence.\n", _SL_);
466 if (p->i_flags&GRE_CSUM)
467 printf("%s Checksum in received packet is required.", _SL_);
468 if (p->o_flags&GRE_SEQ)
469 printf("%s Sequence packets on output.", _SL_);
470 if (p->o_flags&GRE_CSUM)
471 printf("%s Checksum output packets.", _SL_);
474 static int do_tunnels_list(struct ip_tunnel_parm *p)
477 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
479 tx_bytes, tx_packets, tx_errs, tx_drops,
480 tx_fifo, tx_colls, tx_carrier, rx_multi;
482 struct ip_tunnel_parm p1;
485 FILE *fp = fopen("/proc/net/dev", "r");
491 fgets(buf, sizeof(buf), fp);
492 fgets(buf, sizeof(buf), fp);
494 while (fgets(buf, sizeof(buf), fp) != NULL) {
496 buf[sizeof(buf) - 1] = 0;
497 if ((ptr = strchr(buf, ':')) == NULL ||
498 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
499 fprintf(stderr, "Wrong format of /proc/net/dev. Sorry.\n");
502 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
503 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
504 &rx_fifo, &rx_frame, &rx_multi,
505 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
506 &tx_fifo, &tx_colls, &tx_carrier) != 14)
508 if (p->name[0] && strcmp(p->name, name))
510 type = do_ioctl_get_iftype(name);
512 fprintf(stderr, "Failed to get type of [%s]\n", name);
515 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
517 memset(&p1, 0, sizeof(p1));
518 if (do_get_ioctl(name, &p1))
520 if ((p->link && p1.link != p->link) ||
521 (p->name[0] && strcmp(p1.name, p->name)) ||
522 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
523 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
524 (p->i_key && p1.i_key != p->i_key))
529 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
530 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
531 rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_);
532 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_);
533 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
534 tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
541 static int do_show(int argc, char **argv)
544 struct ip_tunnel_parm p;
546 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
549 printf("NOTE: EGRE tunnels will not show up here, use ifconfig\n");
550 switch (p.iph.protocol) {
552 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
555 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
558 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
572 int do_iptunnel(int argc, char **argv)
575 if (matches(*argv, "add") == 0)
576 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
577 if (matches(*argv, "change") == 0)
578 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
579 if (matches(*argv, "del") == 0)
580 return do_del(argc-1, argv+1);
581 if (matches(*argv, "show") == 0 ||
582 matches(*argv, "lst") == 0 ||
583 matches(*argv, "list") == 0)
584 return do_show(argc-1, argv+1);
585 if (matches(*argv, "help") == 0)
588 return do_show(0, NULL);
590 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv);