]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/iptunnel.c
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 <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/if_tunnel.h>
37 static void usage(void) __attribute__((noreturn
));
39 static void usage(void)
41 fprintf(stderr
, "Usage: ip tunnel { add | change | del | show } [ NAME ]\n");
42 fprintf(stderr
, " [ mode { ipip | gre | sit } ] [ remote ADDR ] [ local ADDR ]\n");
43 fprintf(stderr
, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
44 fprintf(stderr
, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
45 fprintf(stderr
, "\n");
46 fprintf(stderr
, "Where: NAME := STRING\n");
47 fprintf(stderr
, " ADDR := { IP_ADDRESS | any }\n");
48 fprintf(stderr
, " TOS := { NUMBER | inherit }\n");
49 fprintf(stderr
, " TTL := { 1..255 | inherit }\n");
50 fprintf(stderr
, " KEY := { DOTTED_QUAD | NUMBER }\n");
54 static int do_ioctl_get_ifindex(char *dev
)
60 strcpy(ifr
.ifr_name
, dev
);
61 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
62 err
= ioctl(fd
, SIOCGIFINDEX
, &ifr
);
68 return ifr
.ifr_ifindex
;
71 static int do_ioctl_get_iftype(char *dev
)
77 strcpy(ifr
.ifr_name
, dev
);
78 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
79 err
= ioctl(fd
, SIOCGIFHWADDR
, &ifr
);
85 return ifr
.ifr_addr
.sa_family
;
89 static char * do_ioctl_get_ifname(int idx
)
91 static struct ifreq ifr
;
95 ifr
.ifr_ifindex
= idx
;
96 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
97 err
= ioctl(fd
, SIOCGIFNAME
, &ifr
);
108 static int do_get_ioctl(char *basedev
, struct ip_tunnel_parm
*p
)
114 strcpy(ifr
.ifr_name
, basedev
);
115 ifr
.ifr_ifru
.ifru_data
= (void*)p
;
116 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
117 err
= ioctl(fd
, SIOCGETTUNNEL
, &ifr
);
124 static int do_add_ioctl(int cmd
, char *basedev
, struct ip_tunnel_parm
*p
)
130 if (cmd
== SIOCCHGTUNNEL
&& p
->name
[0])
131 strcpy(ifr
.ifr_name
, p
->name
);
133 strcpy(ifr
.ifr_name
, basedev
);
134 ifr
.ifr_ifru
.ifru_data
= (void*)p
;
135 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
136 err
= ioctl(fd
, cmd
, &ifr
);
143 static int do_del_ioctl(char *basedev
, struct ip_tunnel_parm
*p
)
150 strcpy(ifr
.ifr_name
, p
->name
);
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
, SIOCDELTUNNEL
, &ifr
);
162 static int parse_args(int argc
, char **argv
, int cmd
, struct ip_tunnel_parm
*p
)
165 char medium
[IFNAMSIZ
];
167 memset(p
, 0, sizeof(*p
));
168 memset(&medium
, 0, sizeof(medium
));
173 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
175 p
->iph
.frag_off
= htons(IP_DF
);
178 if (strcmp(*argv
, "mode") == 0) {
180 if (strcmp(*argv
, "ipip") == 0 ||
181 strcmp(*argv
, "ip/ip") == 0) {
182 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_IPIP
) {
183 fprintf(stderr
,"You managed to ask for more than one tunnel mode.\n");
186 p
->iph
.protocol
= IPPROTO_IPIP
;
187 } else if (strcmp(*argv
, "gre") == 0 ||
188 strcmp(*argv
, "gre/ip") == 0) {
189 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_GRE
) {
190 fprintf(stderr
,"You managed to ask for more than one tunnel mode.\n");
193 p
->iph
.protocol
= IPPROTO_GRE
;
194 } else if (strcmp(*argv
, "sit") == 0 ||
195 strcmp(*argv
, "ipv6/ip") == 0) {
196 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_IPV6
) {
197 fprintf(stderr
,"You managed to ask for more than one tunnel mode.\n");
200 p
->iph
.protocol
= IPPROTO_IPV6
;
202 fprintf(stderr
,"Cannot guess tunnel mode.\n");
205 } else if (strcmp(*argv
, "key") == 0) {
208 p
->i_flags
|= GRE_KEY
;
209 p
->o_flags
|= GRE_KEY
;
210 if (strchr(*argv
, '.'))
211 p
->i_key
= p
->o_key
= get_addr32(*argv
);
213 if (get_unsigned(&uval
, *argv
, 0)<0) {
214 fprintf(stderr
, "invalid value of \"key\"\n");
217 p
->i_key
= p
->o_key
= htonl(uval
);
219 } else if (strcmp(*argv
, "ikey") == 0) {
222 p
->i_flags
|= GRE_KEY
;
223 if (strchr(*argv
, '.'))
224 p
->o_key
= get_addr32(*argv
);
226 if (get_unsigned(&uval
, *argv
, 0)<0) {
227 fprintf(stderr
, "invalid value of \"ikey\"\n");
230 p
->i_key
= htonl(uval
);
232 } else if (strcmp(*argv
, "okey") == 0) {
235 p
->o_flags
|= GRE_KEY
;
236 if (strchr(*argv
, '.'))
237 p
->o_key
= get_addr32(*argv
);
239 if (get_unsigned(&uval
, *argv
, 0)<0) {
240 fprintf(stderr
, "invalid value of \"okey\"\n");
243 p
->o_key
= htonl(uval
);
245 } else if (strcmp(*argv
, "seq") == 0) {
246 p
->i_flags
|= GRE_SEQ
;
247 p
->o_flags
|= GRE_SEQ
;
248 } else if (strcmp(*argv
, "iseq") == 0) {
249 p
->i_flags
|= GRE_SEQ
;
250 } else if (strcmp(*argv
, "oseq") == 0) {
251 p
->o_flags
|= GRE_SEQ
;
252 } else if (strcmp(*argv
, "csum") == 0) {
253 p
->i_flags
|= GRE_CSUM
;
254 p
->o_flags
|= GRE_CSUM
;
255 } else if (strcmp(*argv
, "icsum") == 0) {
256 p
->i_flags
|= GRE_CSUM
;
257 } else if (strcmp(*argv
, "ocsum") == 0) {
258 p
->o_flags
|= GRE_CSUM
;
259 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
261 } else if (strcmp(*argv
, "pmtudisc") == 0) {
262 p
->iph
.frag_off
= htons(IP_DF
);
263 } else if (strcmp(*argv
, "remote") == 0) {
265 if (strcmp(*argv
, "any"))
266 p
->iph
.daddr
= get_addr32(*argv
);
267 } else if (strcmp(*argv
, "local") == 0) {
269 if (strcmp(*argv
, "any"))
270 p
->iph
.saddr
= get_addr32(*argv
);
271 } else if (strcmp(*argv
, "dev") == 0) {
273 strncpy(medium
, *argv
, IFNAMSIZ
-1);
274 } else if (strcmp(*argv
, "ttl") == 0) {
277 if (strcmp(*argv
, "inherit") != 0) {
278 if (get_unsigned(&uval
, *argv
, 0))
279 invarg("invalid TTL\n", *argv
);
281 invarg("TTL must be <=255\n", *argv
);
284 } else if (strcmp(*argv
, "tos") == 0 ||
285 matches(*argv
, "dsfield") == 0) {
288 if (strcmp(*argv
, "inherit") != 0) {
289 if (rtnl_dsfield_a2n(&uval
, *argv
))
290 invarg("bad TOS value", *argv
);
295 if (strcmp(*argv
, "name") == 0) {
298 if (matches(*argv
, "help") == 0)
301 duparg2("name", *argv
);
302 strncpy(p
->name
, *argv
, IFNAMSIZ
);
303 if (cmd
== SIOCCHGTUNNEL
&& count
== 0) {
304 struct ip_tunnel_parm old_p
;
305 memset(&old_p
, 0, sizeof(old_p
));
306 if (do_get_ioctl(*argv
, &old_p
))
316 if (p
->iph
.protocol
== 0) {
317 if (memcmp(p
->name
, "gre", 3) == 0)
318 p
->iph
.protocol
= IPPROTO_GRE
;
319 else if (memcmp(p
->name
, "ipip", 4) == 0)
320 p
->iph
.protocol
= IPPROTO_IPIP
;
321 else if (memcmp(p
->name
, "sit", 3) == 0)
322 p
->iph
.protocol
= IPPROTO_IPV6
;
325 if (p
->iph
.protocol
== IPPROTO_IPIP
|| p
->iph
.protocol
== IPPROTO_IPV6
) {
326 if ((p
->i_flags
& GRE_KEY
) || (p
->o_flags
& GRE_KEY
)) {
327 fprintf(stderr
, "Keys are not allowed with ipip and sit.\n");
333 p
->link
= do_ioctl_get_ifindex(medium
);
338 if (p
->i_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
339 p
->i_key
= p
->iph
.daddr
;
340 p
->i_flags
|= GRE_KEY
;
342 if (p
->o_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
343 p
->o_key
= p
->iph
.daddr
;
344 p
->o_flags
|= GRE_KEY
;
346 if (IN_MULTICAST(ntohl(p
->iph
.daddr
)) && !p
->iph
.saddr
) {
347 fprintf(stderr
, "Broadcast tunnel requires a source address.\n");
354 static int do_add(int cmd
, int argc
, char **argv
)
356 struct ip_tunnel_parm p
;
358 if (parse_args(argc
, argv
, cmd
, &p
) < 0)
361 if (p
.iph
.ttl
&& p
.iph
.frag_off
== 0) {
362 fprintf(stderr
, "ttl != 0 and noptmudisc are incompatible\n");
366 switch (p
.iph
.protocol
) {
368 return do_add_ioctl(cmd
, "tunl0", &p
);
370 return do_add_ioctl(cmd
, "gre0", &p
);
372 return do_add_ioctl(cmd
, "sit0", &p
);
374 fprintf(stderr
, "cannot determine tunnel mode (ipip, gre or sit)\n");
380 int do_del(int argc
, char **argv
)
382 struct ip_tunnel_parm p
;
384 if (parse_args(argc
, argv
, SIOCDELTUNNEL
, &p
) < 0)
387 switch (p
.iph
.protocol
) {
389 return do_del_ioctl("tunl0", &p
);
391 return do_del_ioctl("gre0", &p
);
393 return do_del_ioctl("sit0", &p
);
395 return do_del_ioctl(p
.name
, &p
);
400 void print_tunnel(struct ip_tunnel_parm
*p
)
407 inet_ntop(AF_INET
, &p
->i_key
, s3
, sizeof(s3
));
408 inet_ntop(AF_INET
, &p
->o_key
, s4
, sizeof(s4
));
410 /* Do not use format_host() for local addr,
411 * symbolic name will not be useful.
413 printf("%s: %s/ip remote %s local %s ",
415 p
->iph
.protocol
== IPPROTO_IPIP
? "ip" :
416 (p
->iph
.protocol
== IPPROTO_GRE
? "gre" :
417 (p
->iph
.protocol
== IPPROTO_IPV6
? "ipv6" : "unknown")),
418 p
->iph
.daddr
? format_host(AF_INET
, 4, &p
->iph
.daddr
, s1
, sizeof(s1
)) : "any",
419 p
->iph
.saddr
? rt_addr_n2a(AF_INET
, 4, &p
->iph
.saddr
, s2
, sizeof(s2
)) : "any");
422 char *n
= do_ioctl_get_ifname(p
->link
);
424 printf(" dev %s ", n
);
428 printf(" ttl %d ", p
->iph
.ttl
);
430 printf(" ttl inherit ");
438 printf("%c%s ", p
->iph
.tos
&1 ? '/' : ' ',
439 rtnl_dsfield_n2a(p
->iph
.tos
&~1, b1
, sizeof(b1
)));
442 if (!(p
->iph
.frag_off
&htons(IP_DF
)))
443 printf(" nopmtudisc");
445 if ((p
->i_flags
&GRE_KEY
) && (p
->o_flags
&GRE_KEY
) && p
->o_key
== p
->i_key
)
446 printf(" key %s", s3
);
447 else if ((p
->i_flags
|p
->o_flags
)&GRE_KEY
) {
448 if (p
->i_flags
&GRE_KEY
)
449 printf(" ikey %s ", s3
);
450 if (p
->o_flags
&GRE_KEY
)
451 printf(" okey %s ", s4
);
454 if (p
->i_flags
&GRE_SEQ
)
455 printf("%s Drop packets out of sequence.\n", _SL_
);
456 if (p
->i_flags
&GRE_CSUM
)
457 printf("%s Checksum in received packet is required.", _SL_
);
458 if (p
->o_flags
&GRE_SEQ
)
459 printf("%s Sequence packets on output.", _SL_
);
460 if (p
->o_flags
&GRE_CSUM
)
461 printf("%s Checksum output packets.", _SL_
);
464 static int do_tunnels_list(struct ip_tunnel_parm
*p
)
467 unsigned long rx_bytes
, rx_packets
, rx_errs
, rx_drops
,
469 tx_bytes
, tx_packets
, tx_errs
, tx_drops
,
470 tx_fifo
, tx_colls
, tx_carrier
, rx_multi
;
472 struct ip_tunnel_parm p1
;
475 FILE *fp
= fopen("/proc/net/dev", "r");
481 fgets(buf
, sizeof(buf
), fp
);
482 fgets(buf
, sizeof(buf
), fp
);
484 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
486 buf
[sizeof(buf
) - 1] = 0;
487 if ((ptr
= strchr(buf
, ':')) == NULL
||
488 (*ptr
++ = 0, sscanf(buf
, "%s", name
) != 1)) {
489 fprintf(stderr
, "Wrong format of /proc/net/dev. Sorry.\n");
492 if (sscanf(ptr
, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
493 &rx_bytes
, &rx_packets
, &rx_errs
, &rx_drops
,
494 &rx_fifo
, &rx_frame
, &rx_multi
,
495 &tx_bytes
, &tx_packets
, &tx_errs
, &tx_drops
,
496 &tx_fifo
, &tx_colls
, &tx_carrier
) != 14)
498 if (p
->name
[0] && strcmp(p
->name
, name
))
500 type
= do_ioctl_get_iftype(name
);
502 fprintf(stderr
, "Failed to get type of [%s]\n", name
);
505 if (type
!= ARPHRD_TUNNEL
&& type
!= ARPHRD_IPGRE
&& type
!= ARPHRD_SIT
)
507 memset(&p1
, 0, sizeof(p1
));
508 if (do_get_ioctl(name
, &p1
))
510 if ((p
->link
&& p1
.link
!= p
->link
) ||
511 (p
->name
[0] && strcmp(p1
.name
, p
->name
)) ||
512 (p
->iph
.daddr
&& p1
.iph
.daddr
!= p
->iph
.daddr
) ||
513 (p
->iph
.saddr
&& p1
.iph
.saddr
!= p
->iph
.saddr
) ||
514 (p
->i_key
&& p1
.i_key
!= p
->i_key
))
519 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_
);
520 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
521 rx_packets
, rx_bytes
, rx_errs
, rx_frame
, rx_fifo
, rx_multi
, _SL_
);
522 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_
);
523 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
524 tx_packets
, tx_bytes
, tx_errs
, tx_colls
, tx_carrier
, tx_drops
);
531 static int do_show(int argc
, char **argv
)
534 struct ip_tunnel_parm p
;
536 if (parse_args(argc
, argv
, SIOCGETTUNNEL
, &p
) < 0)
539 switch (p
.iph
.protocol
) {
541 err
= do_get_ioctl(p
.name
[0] ? p
.name
: "tunl0", &p
);
544 err
= do_get_ioctl(p
.name
[0] ? p
.name
: "gre0", &p
);
547 err
= do_get_ioctl(p
.name
[0] ? p
.name
: "sit0", &p
);
561 int do_iptunnel(int argc
, char **argv
)
564 if (matches(*argv
, "add") == 0)
565 return do_add(SIOCADDTUNNEL
, argc
-1, argv
+1);
566 if (matches(*argv
, "change") == 0)
567 return do_add(SIOCCHGTUNNEL
, argc
-1, argv
+1);
568 if (matches(*argv
, "del") == 0)
569 return do_del(argc
-1, argv
+1);
570 if (matches(*argv
, "show") == 0 ||
571 matches(*argv
, "lst") == 0 ||
572 matches(*argv
, "list") == 0)
573 return do_show(argc
-1, argv
+1);
574 if (matches(*argv
, "help") == 0)
577 return do_show(0, NULL
);
579 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv
);