]>
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>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <sys/ioctl.h>
22 #include <net/if_arp.h>
24 #include <linux/if_tunnel.h>
28 #include "ip_common.h"
31 static void usage(void) __attribute__((noreturn
));
33 static void usage(void)
35 fprintf(stderr
, "Usage: ip tunnel { add | change | del | show | prl | 6rd } [ NAME ]\n");
36 fprintf(stderr
, " [ mode { ipip | gre | sit | isatap | vti } ] [ remote ADDR ] [ local ADDR ]\n");
37 fprintf(stderr
, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
38 fprintf(stderr
, " [ prl-default ADDR ] [ prl-nodefault ADDR ] [ prl-delete ADDR ]\n");
39 fprintf(stderr
, " [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n");
40 fprintf(stderr
, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
41 fprintf(stderr
, "\n");
42 fprintf(stderr
, "Where: NAME := STRING\n");
43 fprintf(stderr
, " ADDR := { IP_ADDRESS | any }\n");
44 fprintf(stderr
, " TOS := { STRING | 00..ff | inherit | inherit/STRING | inherit/00..ff }\n");
45 fprintf(stderr
, " TTL := { 1..255 | inherit }\n");
46 fprintf(stderr
, " KEY := { DOTTED_QUAD | NUMBER }\n");
50 static int parse_args(int argc
, char **argv
, int cmd
, struct ip_tunnel_parm
*p
)
53 char medium
[IFNAMSIZ
];
56 memset(p
, 0, sizeof(*p
));
57 memset(&medium
, 0, sizeof(medium
));
62 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
64 p
->iph
.frag_off
= htons(IP_DF
);
67 if (strcmp(*argv
, "mode") == 0) {
69 if (strcmp(*argv
, "ipip") == 0 ||
70 strcmp(*argv
, "ip/ip") == 0) {
71 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_IPIP
) {
72 fprintf(stderr
,"You managed to ask for more than one tunnel mode.\n");
75 p
->iph
.protocol
= IPPROTO_IPIP
;
76 } else if (strcmp(*argv
, "gre") == 0 ||
77 strcmp(*argv
, "gre/ip") == 0) {
78 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_GRE
) {
79 fprintf(stderr
,"You managed to ask for more than one tunnel mode.\n");
82 p
->iph
.protocol
= IPPROTO_GRE
;
83 } else if (strcmp(*argv
, "sit") == 0 ||
84 strcmp(*argv
, "ipv6/ip") == 0) {
85 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_IPV6
) {
86 fprintf(stderr
,"You managed to ask for more than one tunnel mode.\n");
89 p
->iph
.protocol
= IPPROTO_IPV6
;
90 } else if (strcmp(*argv
, "isatap") == 0) {
91 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_IPV6
) {
92 fprintf(stderr
, "You managed to ask for more than one tunnel mode.\n");
95 p
->iph
.protocol
= IPPROTO_IPV6
;
97 } else if (strcmp(*argv
, "vti") == 0) {
98 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_IPIP
) {
99 fprintf(stderr
, "You managed to ask for more than one tunnel mode.\n");
102 p
->iph
.protocol
= IPPROTO_IPIP
;
103 p
->i_flags
|= VTI_ISVTI
;
105 fprintf(stderr
,"Unknown tunnel mode \"%s\"\n", *argv
);
108 } else if (strcmp(*argv
, "key") == 0) {
111 p
->i_flags
|= GRE_KEY
;
112 p
->o_flags
|= GRE_KEY
;
113 if (strchr(*argv
, '.'))
114 p
->i_key
= p
->o_key
= get_addr32(*argv
);
116 if (get_unsigned(&uval
, *argv
, 0)<0) {
117 fprintf(stderr
, "invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv
);
120 p
->i_key
= p
->o_key
= htonl(uval
);
122 } else if (strcmp(*argv
, "ikey") == 0) {
125 p
->i_flags
|= GRE_KEY
;
126 if (strchr(*argv
, '.'))
127 p
->i_key
= get_addr32(*argv
);
129 if (get_unsigned(&uval
, *argv
, 0)<0) {
130 fprintf(stderr
, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv
);
133 p
->i_key
= htonl(uval
);
135 } else if (strcmp(*argv
, "okey") == 0) {
138 p
->o_flags
|= GRE_KEY
;
139 if (strchr(*argv
, '.'))
140 p
->o_key
= get_addr32(*argv
);
142 if (get_unsigned(&uval
, *argv
, 0)<0) {
143 fprintf(stderr
, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv
);
146 p
->o_key
= htonl(uval
);
148 } else if (strcmp(*argv
, "seq") == 0) {
149 p
->i_flags
|= GRE_SEQ
;
150 p
->o_flags
|= GRE_SEQ
;
151 } else if (strcmp(*argv
, "iseq") == 0) {
152 p
->i_flags
|= GRE_SEQ
;
153 } else if (strcmp(*argv
, "oseq") == 0) {
154 p
->o_flags
|= GRE_SEQ
;
155 } else if (strcmp(*argv
, "csum") == 0) {
156 p
->i_flags
|= GRE_CSUM
;
157 p
->o_flags
|= GRE_CSUM
;
158 } else if (strcmp(*argv
, "icsum") == 0) {
159 p
->i_flags
|= GRE_CSUM
;
160 } else if (strcmp(*argv
, "ocsum") == 0) {
161 p
->o_flags
|= GRE_CSUM
;
162 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
164 } else if (strcmp(*argv
, "pmtudisc") == 0) {
165 p
->iph
.frag_off
= htons(IP_DF
);
166 } else if (strcmp(*argv
, "remote") == 0) {
168 if (strcmp(*argv
, "any"))
169 p
->iph
.daddr
= get_addr32(*argv
);
170 } else if (strcmp(*argv
, "local") == 0) {
172 if (strcmp(*argv
, "any"))
173 p
->iph
.saddr
= get_addr32(*argv
);
174 } else if (strcmp(*argv
, "dev") == 0) {
176 strncpy(medium
, *argv
, IFNAMSIZ
-1);
177 } else if (strcmp(*argv
, "ttl") == 0 ||
178 strcmp(*argv
, "hoplimit") == 0) {
181 if (strcmp(*argv
, "inherit") != 0) {
182 if (get_unsigned(&uval
, *argv
, 0))
183 invarg("invalid TTL\n", *argv
);
185 invarg("TTL must be <=255\n", *argv
);
188 } else if (strcmp(*argv
, "tos") == 0 ||
189 strcmp(*argv
, "tclass") == 0 ||
190 matches(*argv
, "dsfield") == 0) {
195 strsep(&dsfield
, "/");
196 if (strcmp(*argv
, "inherit") != 0) {
202 if (rtnl_dsfield_a2n(&uval
, dsfield
))
203 invarg("bad TOS value", *argv
);
207 if (strcmp(*argv
, "name") == 0) {
209 } else if (matches(*argv
, "help") == 0)
212 duparg2("name", *argv
);
213 strncpy(p
->name
, *argv
, IFNAMSIZ
);
214 if (cmd
== SIOCCHGTUNNEL
&& count
== 0) {
215 struct ip_tunnel_parm old_p
;
216 memset(&old_p
, 0, sizeof(old_p
));
217 if (tnl_get_ioctl(*argv
, &old_p
))
227 if (p
->iph
.protocol
== 0) {
228 if (memcmp(p
->name
, "gre", 3) == 0)
229 p
->iph
.protocol
= IPPROTO_GRE
;
230 else if (memcmp(p
->name
, "ipip", 4) == 0)
231 p
->iph
.protocol
= IPPROTO_IPIP
;
232 else if (memcmp(p
->name
, "sit", 3) == 0)
233 p
->iph
.protocol
= IPPROTO_IPV6
;
234 else if (memcmp(p
->name
, "isatap", 6) == 0) {
235 p
->iph
.protocol
= IPPROTO_IPV6
;
237 } else if (memcmp(p
->name
, "vti", 3) == 0) {
238 p
->iph
.protocol
= IPPROTO_IPIP
;
239 p
->i_flags
|= VTI_ISVTI
;
243 if ((p
->i_flags
& GRE_KEY
) || (p
->o_flags
& GRE_KEY
)) {
244 if (!(p
->i_flags
& VTI_ISVTI
) &&
245 (p
->iph
.protocol
!= IPPROTO_GRE
)) {
246 fprintf(stderr
, "Keys are not allowed with ipip and sit tunnels\n");
252 p
->link
= if_nametoindex(medium
);
257 if (p
->i_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
258 p
->i_key
= p
->iph
.daddr
;
259 p
->i_flags
|= GRE_KEY
;
261 if (p
->o_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
262 p
->o_key
= p
->iph
.daddr
;
263 p
->o_flags
|= GRE_KEY
;
265 if (IN_MULTICAST(ntohl(p
->iph
.daddr
)) && !p
->iph
.saddr
) {
266 fprintf(stderr
, "A broadcast tunnel requires a source address\n");
270 p
->i_flags
|= SIT_ISATAP
;
276 static int do_add(int cmd
, int argc
, char **argv
)
278 struct ip_tunnel_parm p
;
280 if (parse_args(argc
, argv
, cmd
, &p
) < 0)
283 if (p
.iph
.ttl
&& p
.iph
.frag_off
== 0) {
284 fprintf(stderr
, "ttl != 0 and nopmtudisc are incompatible\n");
288 switch (p
.iph
.protocol
) {
290 if (p
.i_flags
!= VTI_ISVTI
)
291 return tnl_add_ioctl(cmd
, "tunl0", p
.name
, &p
);
293 return tnl_add_ioctl(cmd
, "ip_vti0", p
.name
, &p
);
295 return tnl_add_ioctl(cmd
, "gre0", p
.name
, &p
);
297 return tnl_add_ioctl(cmd
, "sit0", p
.name
, &p
);
299 fprintf(stderr
, "cannot determine tunnel mode (ipip, gre, vti or sit)\n");
305 static int do_del(int argc
, char **argv
)
307 struct ip_tunnel_parm p
;
309 if (parse_args(argc
, argv
, SIOCDELTUNNEL
, &p
) < 0)
312 switch (p
.iph
.protocol
) {
314 if (p
.i_flags
!= VTI_ISVTI
)
315 return tnl_del_ioctl("tunl0", p
.name
, &p
);
317 return tnl_del_ioctl("ip_vti0", p
.name
, &p
);
319 return tnl_del_ioctl("gre0", p
.name
, &p
);
321 return tnl_del_ioctl("sit0", p
.name
, &p
);
323 return tnl_del_ioctl(p
.name
, p
.name
, &p
);
328 static void print_tunnel(struct ip_tunnel_parm
*p
)
330 struct ip_tunnel_6rd ip6rd
;
334 memset(&ip6rd
, 0, sizeof(ip6rd
));
336 /* Do not use format_host() for local addr,
337 * symbolic name will not be useful.
339 printf("%s: %s/ip remote %s local %s ",
341 tnl_strproto(p
->iph
.protocol
),
342 p
->iph
.daddr
? format_host(AF_INET
, 4, &p
->iph
.daddr
, s1
, sizeof(s1
)) : "any",
343 p
->iph
.saddr
? rt_addr_n2a(AF_INET
, 4, &p
->iph
.saddr
, s2
, sizeof(s2
)) : "any");
345 if (p
->iph
.protocol
== IPPROTO_IPV6
&& (p
->i_flags
& SIT_ISATAP
)) {
346 struct ip_tunnel_prl prl
[16];
349 memset(prl
, 0, sizeof(prl
));
350 prl
[0].datalen
= sizeof(prl
) - sizeof(prl
[0]);
351 prl
[0].addr
= htonl(INADDR_ANY
);
353 if (!tnl_prl_ioctl(SIOCGETPRL
, p
->name
, prl
))
354 for (i
= 1; i
< sizeof(prl
) / sizeof(prl
[0]); i
++)
356 if (prl
[i
].addr
!= htonl(INADDR_ANY
)) {
358 (prl
[i
].flags
& PRL_DEFAULT
) ? "pdr" : "pr",
359 format_host(AF_INET
, 4, &prl
[i
].addr
, s1
, sizeof(s1
)));
365 const char *n
= ll_index_to_name(p
->link
);
367 printf(" dev %s ", n
);
371 printf(" ttl %d ", p
->iph
.ttl
);
373 printf(" ttl inherit ");
381 printf("%c%s ", p
->iph
.tos
&1 ? '/' : ' ',
382 rtnl_dsfield_n2a(p
->iph
.tos
&~1, b1
, sizeof(b1
)));
385 if (!(p
->iph
.frag_off
&htons(IP_DF
)))
386 printf(" nopmtudisc");
388 if (p
->iph
.protocol
== IPPROTO_IPV6
&& !tnl_ioctl_get_6rd(p
->name
, &ip6rd
) && ip6rd
.prefixlen
) {
389 printf(" 6rd-prefix %s/%u ",
390 inet_ntop(AF_INET6
, &ip6rd
.prefix
, s1
, sizeof(s1
)),
392 if (ip6rd
.relay_prefix
) {
393 printf("6rd-relay_prefix %s/%u ",
394 format_host(AF_INET
, 4, &ip6rd
.relay_prefix
, s1
, sizeof(s1
)),
395 ip6rd
.relay_prefixlen
);
399 if ((p
->i_flags
&GRE_KEY
) && (p
->o_flags
&GRE_KEY
) && p
->o_key
== p
->i_key
)
400 printf(" key %u", ntohl(p
->i_key
));
401 else if ((p
->i_flags
|p
->o_flags
)&GRE_KEY
) {
402 if (p
->i_flags
&GRE_KEY
)
403 printf(" ikey %u ", ntohl(p
->i_key
));
404 if (p
->o_flags
&GRE_KEY
)
405 printf(" okey %u ", ntohl(p
->o_key
));
408 if (p
->i_flags
&GRE_SEQ
)
409 printf("%s Drop packets out of sequence.\n", _SL_
);
410 if (p
->i_flags
&GRE_CSUM
)
411 printf("%s Checksum in received packet is required.", _SL_
);
412 if (p
->o_flags
&GRE_SEQ
)
413 printf("%s Sequence packets on output.", _SL_
);
414 if (p
->o_flags
&GRE_CSUM
)
415 printf("%s Checksum output packets.", _SL_
);
418 static int do_tunnels_list(struct ip_tunnel_parm
*p
)
421 unsigned long rx_bytes
, rx_packets
, rx_errs
, rx_drops
,
423 tx_bytes
, tx_packets
, tx_errs
, tx_drops
,
424 tx_fifo
, tx_colls
, tx_carrier
, rx_multi
;
425 struct ip_tunnel_parm p1
;
428 FILE *fp
= fopen("/proc/net/dev", "r");
434 /* skip header lines */
435 if (!fgets(buf
, sizeof(buf
), fp
) ||
436 !fgets(buf
, sizeof(buf
), fp
)) {
437 fprintf(stderr
, "/proc/net/dev read error\n");
442 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
445 buf
[sizeof(buf
) - 1] = 0;
446 if ((ptr
= strchr(buf
, ':')) == NULL
||
447 (*ptr
++ = 0, sscanf(buf
, "%s", name
) != 1)) {
448 fprintf(stderr
, "Wrong format for /proc/net/dev. Giving up.\n");
452 if (sscanf(ptr
, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
453 &rx_bytes
, &rx_packets
, &rx_errs
, &rx_drops
,
454 &rx_fifo
, &rx_frame
, &rx_multi
,
455 &tx_bytes
, &tx_packets
, &tx_errs
, &tx_drops
,
456 &tx_fifo
, &tx_colls
, &tx_carrier
) != 14)
458 if (p
->name
[0] && strcmp(p
->name
, name
))
460 index
= ll_name_to_index(name
);
463 type
= ll_index_to_type(index
);
465 fprintf(stderr
, "Failed to get type of \"%s\"\n", name
);
468 if (type
!= ARPHRD_TUNNEL
&& type
!= ARPHRD_IPGRE
&& type
!= ARPHRD_SIT
)
470 memset(&p1
, 0, sizeof(p1
));
471 if (tnl_get_ioctl(name
, &p1
))
473 if ((p
->link
&& p1
.link
!= p
->link
) ||
474 (p
->name
[0] && strcmp(p1
.name
, p
->name
)) ||
475 (p
->iph
.daddr
&& p1
.iph
.daddr
!= p
->iph
.daddr
) ||
476 (p
->iph
.saddr
&& p1
.iph
.saddr
!= p
->iph
.saddr
) ||
477 (p
->i_key
&& p1
.i_key
!= p
->i_key
))
482 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_
);
483 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
484 rx_packets
, rx_bytes
, rx_errs
, rx_frame
, rx_fifo
, rx_multi
, _SL_
);
485 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_
);
486 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
487 tx_packets
, tx_bytes
, tx_errs
, tx_colls
, tx_carrier
, tx_drops
);
495 static int do_show(int argc
, char **argv
)
498 struct ip_tunnel_parm p
;
501 if (parse_args(argc
, argv
, SIOCGETTUNNEL
, &p
) < 0)
504 switch (p
.iph
.protocol
) {
506 if (p
.i_flags
!= VTI_ISVTI
)
507 err
= tnl_get_ioctl(p
.name
[0] ? p
.name
: "tunl0", &p
);
509 err
= tnl_get_ioctl(p
.name
[0] ? p
.name
: "ip_vti0", &p
);
512 err
= tnl_get_ioctl(p
.name
[0] ? p
.name
: "gre0", &p
);
515 err
= tnl_get_ioctl(p
.name
[0] ? p
.name
: "sit0", &p
);
529 static int do_prl(int argc
, char **argv
)
531 struct ip_tunnel_prl p
;
535 char medium
[IFNAMSIZ
];
537 memset(&p
, 0, sizeof(p
));
538 memset(&medium
, 0, sizeof(medium
));
541 if (strcmp(*argv
, "prl-default") == 0) {
544 p
.addr
= get_addr32(*argv
);
545 p
.flags
|= PRL_DEFAULT
;
547 } else if (strcmp(*argv
, "prl-nodefault") == 0) {
550 p
.addr
= get_addr32(*argv
);
552 } else if (strcmp(*argv
, "prl-delete") == 0) {
555 p
.addr
= get_addr32(*argv
);
557 } else if (strcmp(*argv
, "dev") == 0) {
559 strncpy(medium
, *argv
, IFNAMSIZ
-1);
562 fprintf(stderr
,"Invalid PRL parameter \"%s\"\n", *argv
);
566 fprintf(stderr
,"One PRL entry at a time\n");
572 fprintf(stderr
, "Must specify device\n");
576 return tnl_prl_ioctl(cmd
, medium
, &p
);
579 static int do_6rd(int argc
, char **argv
)
581 struct ip_tunnel_6rd ip6rd
;
584 char medium
[IFNAMSIZ
];
587 memset(&ip6rd
, 0, sizeof(ip6rd
));
588 memset(&medium
, 0, sizeof(medium
));
591 if (strcmp(*argv
, "6rd-prefix") == 0) {
593 if (get_prefix(&prefix
, *argv
, AF_INET6
))
594 invarg("invalid 6rd_prefix\n", *argv
);
596 memcpy(&ip6rd
.prefix
, prefix
.data
, 16);
597 ip6rd
.prefixlen
= prefix
.bitlen
;
598 } else if (strcmp(*argv
, "6rd-relay_prefix") == 0) {
600 if (get_prefix(&prefix
, *argv
, AF_INET
))
601 invarg("invalid 6rd-relay_prefix\n", *argv
);
603 memcpy(&ip6rd
.relay_prefix
, prefix
.data
, 4);
604 ip6rd
.relay_prefixlen
= prefix
.bitlen
;
605 } else if (strcmp(*argv
, "6rd-reset") == 0) {
607 } else if (strcmp(*argv
, "dev") == 0) {
609 strncpy(medium
, *argv
, IFNAMSIZ
-1);
612 fprintf(stderr
,"Invalid 6RD parameter \"%s\"\n", *argv
);
618 fprintf(stderr
, "Must specify device\n");
622 return tnl_6rd_ioctl(cmd
, medium
, &ip6rd
);
625 int do_iptunnel(int argc
, char **argv
)
627 switch (preferred_family
) {
629 preferred_family
= AF_INET
;
634 * This is silly enough but we have no easy way to make it
635 * protocol-independent because of unarranged structure between
639 return do_ip6tunnel(argc
, argv
);
641 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
646 if (matches(*argv
, "add") == 0)
647 return do_add(SIOCADDTUNNEL
, argc
-1, argv
+1);
648 if (matches(*argv
, "change") == 0)
649 return do_add(SIOCCHGTUNNEL
, argc
-1, argv
+1);
650 if (matches(*argv
, "delete") == 0)
651 return do_del(argc
-1, argv
+1);
652 if (matches(*argv
, "show") == 0 ||
653 matches(*argv
, "lst") == 0 ||
654 matches(*argv
, "list") == 0)
655 return do_show(argc
-1, argv
+1);
656 if (matches(*argv
, "prl") == 0)
657 return do_prl(argc
-1, argv
+1);
658 if (matches(*argv
, "6rd") == 0)
659 return do_6rd(argc
-1, argv
+1);
660 if (matches(*argv
, "help") == 0)
663 return do_show(0, NULL
);
665 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv
);