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>
28 #include <linux/if_arp.h>
29 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <arpa/inet.h>
32 #include <linux/types.h>
33 #include <linux/if_tunnel.h>
37 #include "net-support.h"
42 #define GRE_CSUM htons(0x8000)
44 #define GRE_ROUTING htons(0x4000)
46 #define GRE_KEY htons(0x2000)
48 #define GRE_SEQ htons(0x1000)
50 #define GRE_STRICT htons(0x0800)
52 #define GRE_REC htons(0x0700)
54 #define GRE_FLAGS htons(0x00F8)
56 #define GRE_VERSION htons(0x0007)
58 /* Old versions of glibc do not define this */
59 #if __GLIBC__ == 2 && __GLIBC_MINOR__ == 0
60 #define IPPROTO_GRE 47
65 char *Release
= RELEASE
,
66 *Version
= "iptunnel 1.01",
67 *Signature
= "Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>";
69 static void version(void)
71 printf("%s\n%s\n%s\n", Release
, Version
, Signature
);
75 static void usage(void) __attribute__((noreturn
));
77 static void usage(void)
79 fprintf(stderr
, _("Usage: iptunnel { add | change | del | show } [ NAME ]\n"));
80 fprintf(stderr
, _(" [ mode { ipip | gre | sit } ] [ remote ADDR ] [ local ADDR ]\n"));
81 fprintf(stderr
, _(" [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"));
82 fprintf(stderr
, _(" [ ttl TTL ] [ tos TOS ] [ nopmtudisc ] [ dev PHYS_DEV ]\n"));
83 fprintf(stderr
, _(" iptunnel -V | --version\n\n"));
84 fprintf(stderr
, _("Where: NAME := STRING\n"));
85 fprintf(stderr
, _(" ADDR := { IP_ADDRESS | any }\n"));
86 fprintf(stderr
, _(" TOS := { NUMBER | inherit }\n"));
87 fprintf(stderr
, _(" TTL := { 1..255 | inherit }\n"));
88 fprintf(stderr
, _(" KEY := { DOTTED_QUAD | NUMBER }\n"));
92 static int do_ioctl_get_ifindex(char *dev
)
98 strcpy(ifr
.ifr_name
, dev
);
99 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
100 err
= ioctl(fd
, SIOCGIFINDEX
, &ifr
);
106 return ifr
.ifr_ifindex
;
109 static int do_ioctl_get_iftype(char *dev
)
115 strcpy(ifr
.ifr_name
, dev
);
116 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
117 err
= ioctl(fd
, SIOCGIFHWADDR
, &ifr
);
123 return ifr
.ifr_addr
.sa_family
;
127 static char * do_ioctl_get_ifname(int idx
)
129 static struct ifreq ifr
;
133 ifr
.ifr_ifindex
= idx
;
134 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
135 err
= ioctl(fd
, SIOCGIFNAME
, &ifr
);
146 static int do_get_ioctl(char *basedev
, struct ip_tunnel_parm
*p
)
152 strcpy(ifr
.ifr_name
, basedev
);
153 ifr
.ifr_ifru
.ifru_data
= (void*)p
;
154 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
155 err
= ioctl(fd
, SIOCGETTUNNEL
, &ifr
);
162 static int do_add_ioctl(int cmd
, char *basedev
, struct ip_tunnel_parm
*p
)
168 strcpy(ifr
.ifr_name
, basedev
);
169 ifr
.ifr_ifru
.ifru_data
= (void*)p
;
170 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
171 err
= ioctl(fd
, cmd
, &ifr
);
178 static int do_del_ioctl(char *basedev
, struct ip_tunnel_parm
*p
)
184 strcpy(ifr
.ifr_name
, basedev
);
185 ifr
.ifr_ifru
.ifru_data
= (void*)p
;
186 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
187 err
= ioctl(fd
, SIOCDELTUNNEL
, &ifr
);
194 static int parse_args(int argc
, char **argv
, struct ip_tunnel_parm
*p
)
196 char medium
[IFNAMSIZ
];
198 memset(p
, 0, sizeof(*p
));
199 memset(&medium
, 0, sizeof(medium
));
204 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
206 p
->iph
.frag_off
= htons(IP_DF
);
209 if (strcmp(*argv
, "mode") == 0) {
211 if (strcmp(*argv
, "ipip") == 0) {
214 p
->iph
.protocol
= IPPROTO_IPIP
;
215 } else if (strcmp(*argv
, "gre") == 0) {
218 p
->iph
.protocol
= IPPROTO_GRE
;
219 } else if (strcmp(*argv
, "sit") == 0) {
222 p
->iph
.protocol
= IPPROTO_IPV6
;
225 } else if (strcmp(*argv
, "key") == 0) {
228 p
->i_flags
|= GRE_KEY
;
229 p
->o_flags
|= GRE_KEY
;
230 if (strchr(*argv
, '.'))
231 p
->i_key
= p
->o_key
= get_addr32(*argv
);
233 if (scan_number(*argv
, &uval
)<0)
235 p
->i_key
= p
->o_key
= htonl(uval
);
237 } else if (strcmp(*argv
, "ikey") == 0) {
240 p
->i_flags
|= GRE_KEY
;
241 if (strchr(*argv
, '.'))
242 p
->o_key
= get_addr32(*argv
);
244 if (scan_number(*argv
, &uval
)<0)
246 p
->i_key
= htonl(uval
);
248 } else if (strcmp(*argv
, "okey") == 0) {
251 p
->o_flags
|= GRE_KEY
;
252 if (strchr(*argv
, '.'))
253 p
->o_key
= get_addr32(*argv
);
255 if (scan_number(*argv
, &uval
)<0)
257 p
->o_key
= htonl(uval
);
259 } else if (strcmp(*argv
, "seq") == 0) {
260 p
->i_flags
|= GRE_SEQ
;
261 p
->o_flags
|= GRE_SEQ
;
262 } else if (strcmp(*argv
, "iseq") == 0) {
263 p
->i_flags
|= GRE_SEQ
;
264 } else if (strcmp(*argv
, "oseq") == 0) {
265 p
->o_flags
|= GRE_SEQ
;
266 } else if (strcmp(*argv
, "csum") == 0) {
267 p
->i_flags
|= GRE_CSUM
;
268 p
->o_flags
|= GRE_CSUM
;
269 } else if (strcmp(*argv
, "icsum") == 0) {
270 p
->i_flags
|= GRE_CSUM
;
271 } else if (strcmp(*argv
, "ocsum") == 0) {
272 p
->o_flags
|= GRE_CSUM
;
273 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
275 } else if (strcmp(*argv
, "remote") == 0) {
277 if (strcmp(*argv
, "any"))
278 p
->iph
.daddr
= get_addr32(*argv
);
279 } else if (strcmp(*argv
, "local") == 0) {
281 if (strcmp(*argv
, "any"))
282 p
->iph
.saddr
= get_addr32(*argv
);
283 } else if (strcmp(*argv
, "dev") == 0) {
285 safe_strncpy(medium
, *argv
, IFNAMSIZ
-1);
286 } else if (strcmp(*argv
, "ttl") == 0) {
289 if (strcmp(*argv
, "inherit") != 0) {
290 if (scan_number(*argv
, &uval
)<0)
296 } else if (strcmp(*argv
, "tos") == 0) {
299 if (strcmp(*argv
, "inherit") != 0) {
300 if (scan_number(*argv
, &uval
)<0)
310 safe_strncpy(p
->name
, *argv
, IFNAMSIZ
);
315 if (p
->iph
.protocol
== 0) {
316 if (memcmp(p
->name
, "gre", 3) == 0)
317 p
->iph
.protocol
= IPPROTO_GRE
;
318 else if (memcmp(p
->name
, "ipip", 4) == 0)
319 p
->iph
.protocol
= IPPROTO_IPIP
;
320 else if (memcmp(p
->name
, "sit", 3) == 0)
321 p
->iph
.protocol
= IPPROTO_IPV6
;
324 if (p
->iph
.protocol
== IPPROTO_IPIP
|| p
->iph
.protocol
== IPPROTO_IPV6
) {
325 if ((p
->i_flags
& GRE_KEY
) || (p
->o_flags
& GRE_KEY
)) {
326 fprintf(stderr
, _("Keys are not allowed with ipip and sit.\n"));
332 p
->link
= do_ioctl_get_ifindex(medium
);
337 if (p
->i_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
338 p
->i_key
= p
->iph
.daddr
;
339 p
->i_flags
|= GRE_KEY
;
341 if (p
->o_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
342 p
->o_key
= p
->iph
.daddr
;
343 p
->o_flags
|= GRE_KEY
;
345 if (IN_MULTICAST(ntohl(p
->iph
.daddr
)) && !p
->iph
.saddr
) {
346 fprintf(stderr
, _("Broadcast tunnel requires a source address.\n"));
353 static int do_add(int cmd
, int argc
, char **argv
)
355 struct ip_tunnel_parm p
;
357 if (parse_args(argc
, argv
, &p
) < 0)
360 if (p
.iph
.ttl
&& p
.iph
.frag_off
== 0) {
361 fprintf(stderr
, _("ttl != 0 and noptmudisc are incompatible\n"));
365 switch (p
.iph
.protocol
) {
367 return do_add_ioctl(cmd
, "tunl0", &p
);
369 return do_add_ioctl(cmd
, "gre0", &p
);
371 return do_add_ioctl(cmd
, "sit0", &p
);
373 fprintf(stderr
, _("cannot determine tunnel mode (ipip, gre or sit)\n"));
379 int do_del(int argc
, char **argv
)
381 struct ip_tunnel_parm p
;
383 if (parse_args(argc
, argv
, &p
) < 0)
386 switch (p
.iph
.protocol
) {
388 return do_del_ioctl(p
.name
[0] ? p
.name
: "tunl0", &p
);
390 return do_del_ioctl(p
.name
[0] ? p
.name
: "gre0", &p
);
392 return do_del_ioctl(p
.name
[0] ? p
.name
: "sit0", &p
);
394 return do_del_ioctl(p
.name
, &p
);
399 void print_tunnel(struct ip_tunnel_parm
*p
)
406 format_host(AF_INET
, &p
->iph
.daddr
, s1
, sizeof(s1
));
407 format_host(AF_INET
, &p
->iph
.saddr
, s2
, sizeof(s2
));
408 inet_ntop(AF_INET
, &p
->i_key
, s3
, sizeof(s3
));
409 inet_ntop(AF_INET
, &p
->o_key
, s4
, sizeof(s4
));
411 printf(_("%s: %s/ip remote %s local %s "),
413 p
->iph
.protocol
== IPPROTO_IPIP
? "ip" :
414 (p
->iph
.protocol
== IPPROTO_GRE
? "gre" :
415 (p
->iph
.protocol
== IPPROTO_IPV6
? "ipv6" : _("unknown"))),
416 p
->iph
.daddr
? s1
: "any", p
->iph
.saddr
? s2
: "any");
418 char *n
= do_ioctl_get_ifname(p
->link
);
420 printf(" dev %s ", n
);
423 printf(" ttl %d ", p
->iph
.ttl
);
425 printf(" ttl inherit ");
431 printf("%c%02x ", p
->iph
.tos
&1 ? '/' : ' ', p
->iph
.tos
&~1);
433 if (!(p
->iph
.frag_off
&htons(IP_DF
)))
434 printf(" nopmtudisc");
436 if ((p
->i_flags
&GRE_KEY
) && (p
->o_flags
&GRE_KEY
) && p
->o_key
== p
->i_key
)
437 printf(" key %s", s3
);
438 else if ((p
->i_flags
|p
->o_flags
)&GRE_KEY
) {
439 if (p
->i_flags
&GRE_KEY
)
440 printf(" ikey %s ", s3
);
441 if (p
->o_flags
&GRE_KEY
)
442 printf(" okey %s ", s4
);
446 if (p
->i_flags
&GRE_SEQ
)
447 printf(_(" Drop packets out of sequence.\n"));
448 if (p
->i_flags
&GRE_CSUM
)
449 printf(_(" Checksum in received packet is required.\n"));
450 if (p
->o_flags
&GRE_SEQ
)
451 printf(_(" Sequence packets on output.\n"));
452 if (p
->o_flags
&GRE_CSUM
)
453 printf(_(" Checksum output packets.\n"));
456 static int do_tunnels_list(struct ip_tunnel_parm
*p
)
459 unsigned long rx_bytes
, rx_packets
, rx_errs
, rx_drops
,
461 tx_bytes
, tx_packets
, tx_errs
, tx_drops
,
462 tx_fifo
, tx_colls
, tx_carrier
, rx_multi
;
464 struct ip_tunnel_parm p1
;
467 FILE *fp
= fopen("/proc/net/dev", "r");
473 fgets(buf
, sizeof(buf
), fp
);
474 fgets(buf
, sizeof(buf
), fp
);
476 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
478 buf
[sizeof(buf
) - 1] = 0;
479 if ((ptr
= strchr(buf
, ':')) == NULL
||
480 (*ptr
++ = 0, sscanf(buf
, "%s", name
) != 1)) {
481 fprintf(stderr
, _("Wrong format of /proc/net/dev. Sorry.\n"));
484 if (sscanf(ptr
, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
485 &rx_bytes
, &rx_packets
, &rx_errs
, &rx_drops
,
486 &rx_fifo
, &rx_frame
, &rx_multi
,
487 &tx_bytes
, &tx_packets
, &tx_errs
, &tx_drops
,
488 &tx_fifo
, &tx_colls
, &tx_carrier
) != 14)
490 if (p
->name
[0] && strcmp(p
->name
, name
))
492 type
= do_ioctl_get_iftype(name
);
494 fprintf(stderr
, _("Failed to get type of [%s]\n"), name
);
497 if (type
!= ARPHRD_TUNNEL
&& type
!= ARPHRD_IPGRE
&& type
!= ARPHRD_SIT
)
499 memset(&p1
, 0, sizeof(p1
));
500 if (do_get_ioctl(name
, &p1
))
502 if ((p
->link
&& p1
.link
!= p
->link
) ||
503 (p
->name
[0] && strcmp(p1
.name
, p
->name
)) ||
504 (p
->iph
.daddr
&& p1
.iph
.daddr
!= p
->iph
.daddr
) ||
505 (p
->iph
.saddr
&& p1
.iph
.saddr
!= p
->iph
.saddr
) ||
506 (p
->i_key
&& p1
.i_key
!= p
->i_key
))
510 printf(_("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts\n"));
511 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld\n",
512 rx_packets
, rx_bytes
, rx_errs
, rx_frame
, rx_fifo
, rx_multi
);
513 printf(_("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs\n"));
514 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld\n\n",
515 tx_packets
, tx_bytes
, tx_errs
, tx_colls
, tx_carrier
, tx_drops
);
521 static int do_show(int argc
, char **argv
)
524 struct ip_tunnel_parm p
;
526 if (parse_args(argc
, argv
, &p
) < 0)
529 switch (p
.iph
.protocol
) {
531 err
= do_get_ioctl(p
.name
[0] ? p
.name
: "tunl0", &p
);
534 err
= do_get_ioctl(p
.name
[0] ? p
.name
: "gre0", &p
);
537 err
= do_get_ioctl(p
.name
[0] ? p
.name
: "sit0", &p
);
550 int do_iptunnel(int argc
, char **argv
)
553 if (matches(*argv
, "add") == 0)
554 return do_add(SIOCADDTUNNEL
, argc
-1, argv
+1);
555 if (matches(*argv
, "change") == 0)
556 return do_add(SIOCCHGTUNNEL
, argc
-1, argv
+1);
557 if (matches(*argv
, "del") == 0)
558 return do_del(argc
-1, argv
+1);
559 if (matches(*argv
, "show") == 0 ||
560 matches(*argv
, "lst") == 0 ||
561 matches(*argv
, "list") == 0)
562 return do_show(argc
-1, argv
+1);
564 return do_show(0, NULL
);
570 int preferred_family
= AF_UNSPEC
;
572 int resolve_hosts
= 0;
574 int main(int argc
, char **argv
)
579 bindtextdomain("net-tools", "/usr/share/locale");
580 textdomain("net-tools");
583 basename
= strrchr(argv
[0], '/');
584 if (basename
== NULL
)
590 if (argv
[1][0] != '-')
592 if (matches(argv
[1], "-family") == 0) {
597 if (strcmp(argv
[1], "inet") == 0)
598 preferred_family
= AF_INET
;
599 else if (strcmp(argv
[1], "inet6") == 0)
600 preferred_family
= AF_INET6
;
603 } else if (matches(argv
[1], "-stats") == 0 ||
604 matches(argv
[1], "-statistics") == 0) {
606 } else if (matches(argv
[1], "-resolve") == 0) {
608 } else if ((matches(argv
[1], "-V") == 0) || (matches(argv
[1], "--version") == 0)) {
615 return do_iptunnel(argc
-1, argv
+1);