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 | 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 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_GRE
) {
194 fprintf(stderr
,"You managed to ask for more than one tunnel mode.\n");
197 p
->iph
.protocol
= IPPROTO_GRE
;
198 } else if (strcmp(*argv
, "sit") == 0 ||
199 strcmp(*argv
, "ipv6/ip") == 0) {
200 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_IPV6
) {
201 fprintf(stderr
,"You managed to ask for more than one tunnel mode.\n");
204 p
->iph
.protocol
= IPPROTO_IPV6
;
206 fprintf(stderr
,"Cannot guess tunnel mode.\n");
209 } else if (strcmp(*argv
, "key") == 0) {
212 p
->i_flags
|= GRE_KEY
;
213 p
->o_flags
|= GRE_KEY
;
214 if (strchr(*argv
, '.'))
215 p
->i_key
= p
->o_key
= get_addr32(*argv
);
217 if (get_unsigned(&uval
, *argv
, 0)<0) {
218 fprintf(stderr
, "invalid value of \"key\"\n");
221 p
->i_key
= p
->o_key
= htonl(uval
);
223 } else if (strcmp(*argv
, "ikey") == 0) {
226 p
->i_flags
|= GRE_KEY
;
227 if (strchr(*argv
, '.'))
228 p
->o_key
= get_addr32(*argv
);
230 if (get_unsigned(&uval
, *argv
, 0)<0) {
231 fprintf(stderr
, "invalid value of \"ikey\"\n");
234 p
->i_key
= htonl(uval
);
236 } else if (strcmp(*argv
, "okey") == 0) {
239 p
->o_flags
|= GRE_KEY
;
240 if (strchr(*argv
, '.'))
241 p
->o_key
= get_addr32(*argv
);
243 if (get_unsigned(&uval
, *argv
, 0)<0) {
244 fprintf(stderr
, "invalid value of \"okey\"\n");
247 p
->o_key
= htonl(uval
);
249 } else if (strcmp(*argv
, "seq") == 0) {
250 p
->i_flags
|= GRE_SEQ
;
251 p
->o_flags
|= GRE_SEQ
;
252 } else if (strcmp(*argv
, "iseq") == 0) {
253 p
->i_flags
|= GRE_SEQ
;
254 } else if (strcmp(*argv
, "oseq") == 0) {
255 p
->o_flags
|= GRE_SEQ
;
256 } else if (strcmp(*argv
, "csum") == 0) {
257 p
->i_flags
|= GRE_CSUM
;
258 p
->o_flags
|= GRE_CSUM
;
259 } else if (strcmp(*argv
, "icsum") == 0) {
260 p
->i_flags
|= GRE_CSUM
;
261 } else if (strcmp(*argv
, "ocsum") == 0) {
262 p
->o_flags
|= GRE_CSUM
;
263 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
265 } else if (strcmp(*argv
, "pmtudisc") == 0) {
266 p
->iph
.frag_off
= htons(IP_DF
);
267 } else if (strcmp(*argv
, "remote") == 0) {
269 if (strcmp(*argv
, "any"))
270 p
->iph
.daddr
= get_addr32(*argv
);
271 } else if (strcmp(*argv
, "local") == 0) {
273 if (strcmp(*argv
, "any"))
274 p
->iph
.saddr
= get_addr32(*argv
);
275 } else if (strcmp(*argv
, "dev") == 0) {
277 strncpy(medium
, *argv
, IFNAMSIZ
-1);
278 } else if (strcmp(*argv
, "ttl") == 0) {
281 if (strcmp(*argv
, "inherit") != 0) {
282 if (get_unsigned(&uval
, *argv
, 0))
283 invarg("invalid TTL\n", *argv
);
285 invarg("TTL must be <=255\n", *argv
);
288 } else if (strcmp(*argv
, "tos") == 0 ||
289 matches(*argv
, "dsfield") == 0) {
292 if (strcmp(*argv
, "inherit") != 0) {
293 if (rtnl_dsfield_a2n(&uval
, *argv
))
294 invarg("bad TOS value", *argv
);
299 if (strcmp(*argv
, "name") == 0) {
302 if (matches(*argv
, "help") == 0)
305 duparg2("name", *argv
);
306 strncpy(p
->name
, *argv
, IFNAMSIZ
);
307 if (cmd
== SIOCCHGTUNNEL
&& count
== 0) {
308 struct ip_tunnel_parm old_p
;
309 memset(&old_p
, 0, sizeof(old_p
));
310 if (do_get_ioctl(*argv
, &old_p
))
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
, cmd
, &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
, SIOCDELTUNNEL
, &p
) < 0)
391 switch (p
.iph
.protocol
) {
393 return do_del_ioctl("tunl0", &p
);
395 return do_del_ioctl("gre0", &p
);
397 return do_del_ioctl("sit0", &p
);
399 return do_del_ioctl(p
.name
, &p
);
404 void print_tunnel(struct ip_tunnel_parm
*p
)
411 inet_ntop(AF_INET
, &p
->i_key
, s3
, sizeof(s3
));
412 inet_ntop(AF_INET
, &p
->o_key
, s4
, sizeof(s4
));
414 /* Do not use format_host() for local addr,
415 * symbolic name will not be useful.
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
? format_host(AF_INET
, 4, &p
->iph
.daddr
, s1
, sizeof(s1
)) : "any",
423 p
->iph
.saddr
? rt_addr_n2a(AF_INET
, 4, &p
->iph
.saddr
, s2
, sizeof(s2
)) : "any");
426 char *n
= do_ioctl_get_ifname(p
->link
);
428 printf(" dev %s ", n
);
432 printf(" ttl %d ", p
->iph
.ttl
);
434 printf(" ttl inherit ");
442 printf("%c%s ", p
->iph
.tos
&1 ? '/' : ' ',
443 rtnl_dsfield_n2a(p
->iph
.tos
&~1, b1
, sizeof(b1
)));
446 if (!(p
->iph
.frag_off
&htons(IP_DF
)))
447 printf(" nopmtudisc");
449 if ((p
->i_flags
&GRE_KEY
) && (p
->o_flags
&GRE_KEY
) && p
->o_key
== p
->i_key
)
450 printf(" key %s", s3
);
451 else if ((p
->i_flags
|p
->o_flags
)&GRE_KEY
) {
452 if (p
->i_flags
&GRE_KEY
)
453 printf(" ikey %s ", s3
);
454 if (p
->o_flags
&GRE_KEY
)
455 printf(" okey %s ", s4
);
458 if (p
->i_flags
&GRE_SEQ
)
459 printf("%s Drop packets out of sequence.\n", _SL_
);
460 if (p
->i_flags
&GRE_CSUM
)
461 printf("%s Checksum in received packet is required.", _SL_
);
462 if (p
->o_flags
&GRE_SEQ
)
463 printf("%s Sequence packets on output.", _SL_
);
464 if (p
->o_flags
&GRE_CSUM
)
465 printf("%s Checksum output packets.", _SL_
);
468 static int do_tunnels_list(struct ip_tunnel_parm
*p
)
471 unsigned long rx_bytes
, rx_packets
, rx_errs
, rx_drops
,
473 tx_bytes
, tx_packets
, tx_errs
, tx_drops
,
474 tx_fifo
, tx_colls
, tx_carrier
, rx_multi
;
476 struct ip_tunnel_parm p1
;
479 FILE *fp
= fopen("/proc/net/dev", "r");
485 fgets(buf
, sizeof(buf
), fp
);
486 fgets(buf
, sizeof(buf
), fp
);
488 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
490 buf
[sizeof(buf
) - 1] = 0;
491 if ((ptr
= strchr(buf
, ':')) == NULL
||
492 (*ptr
++ = 0, sscanf(buf
, "%s", name
) != 1)) {
493 fprintf(stderr
, "Wrong format of /proc/net/dev. Sorry.\n");
496 if (sscanf(ptr
, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
497 &rx_bytes
, &rx_packets
, &rx_errs
, &rx_drops
,
498 &rx_fifo
, &rx_frame
, &rx_multi
,
499 &tx_bytes
, &tx_packets
, &tx_errs
, &tx_drops
,
500 &tx_fifo
, &tx_colls
, &tx_carrier
) != 14)
502 if (p
->name
[0] && strcmp(p
->name
, name
))
504 type
= do_ioctl_get_iftype(name
);
506 fprintf(stderr
, "Failed to get type of [%s]\n", name
);
509 if (type
!= ARPHRD_TUNNEL
&& type
!= ARPHRD_IPGRE
&& type
!= ARPHRD_SIT
)
511 memset(&p1
, 0, sizeof(p1
));
512 if (do_get_ioctl(name
, &p1
))
514 if ((p
->link
&& p1
.link
!= p
->link
) ||
515 (p
->name
[0] && strcmp(p1
.name
, p
->name
)) ||
516 (p
->iph
.daddr
&& p1
.iph
.daddr
!= p
->iph
.daddr
) ||
517 (p
->iph
.saddr
&& p1
.iph
.saddr
!= p
->iph
.saddr
) ||
518 (p
->i_key
&& p1
.i_key
!= p
->i_key
))
523 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_
);
524 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
525 rx_packets
, rx_bytes
, rx_errs
, rx_frame
, rx_fifo
, rx_multi
, _SL_
);
526 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_
);
527 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
528 tx_packets
, tx_bytes
, tx_errs
, tx_colls
, tx_carrier
, tx_drops
);
535 static int do_show(int argc
, char **argv
)
538 struct ip_tunnel_parm p
;
540 if (parse_args(argc
, argv
, SIOCGETTUNNEL
, &p
) < 0)
543 switch (p
.iph
.protocol
) {
545 err
= do_get_ioctl(p
.name
[0] ? p
.name
: "tunl0", &p
);
548 err
= do_get_ioctl(p
.name
[0] ? p
.name
: "gre0", &p
);
551 err
= do_get_ioctl(p
.name
[0] ? p
.name
: "sit0", &p
);
565 int do_iptunnel(int argc
, char **argv
)
568 if (matches(*argv
, "add") == 0)
569 return do_add(SIOCADDTUNNEL
, argc
-1, argv
+1);
570 if (matches(*argv
, "change") == 0)
571 return do_add(SIOCCHGTUNNEL
, argc
-1, argv
+1);
572 if (matches(*argv
, "del") == 0)
573 return do_del(argc
-1, argv
+1);
574 if (matches(*argv
, "show") == 0 ||
575 matches(*argv
, "lst") == 0 ||
576 matches(*argv
, "list") == 0)
577 return do_show(argc
-1, argv
+1);
578 if (matches(*argv
, "help") == 0)
581 return do_show(0, NULL
);
583 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv
);