]>
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 void set_tunnel_proto(struct ip_tunnel_parm
*p
, int proto
)
52 if (p
->iph
.protocol
&& p
->iph
.protocol
!= proto
) {
54 "You managed to ask for more than one tunnel mode.\n");
57 p
->iph
.protocol
= proto
;
60 static int parse_args(int argc
, char **argv
, int cmd
, struct ip_tunnel_parm
*p
)
63 char medium
[IFNAMSIZ
] = {};
66 memset(p
, 0, sizeof(*p
));
70 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
72 p
->iph
.frag_off
= htons(IP_DF
);
75 if (strcmp(*argv
, "mode") == 0) {
77 if (strcmp(*argv
, "ipip") == 0 ||
78 strcmp(*argv
, "ip/ip") == 0) {
79 set_tunnel_proto(p
, IPPROTO_IPIP
);
80 } else if (strcmp(*argv
, "gre") == 0 ||
81 strcmp(*argv
, "gre/ip") == 0) {
82 set_tunnel_proto(p
, IPPROTO_GRE
);
83 } else if (strcmp(*argv
, "sit") == 0 ||
84 strcmp(*argv
, "ipv6/ip") == 0) {
85 set_tunnel_proto(p
, IPPROTO_IPV6
);
86 } else if (strcmp(*argv
, "isatap") == 0) {
87 set_tunnel_proto(p
, IPPROTO_IPV6
);
89 } else if (strcmp(*argv
, "vti") == 0) {
90 set_tunnel_proto(p
, IPPROTO_IPIP
);
91 p
->i_flags
|= VTI_ISVTI
;
94 "Unknown tunnel mode \"%s\"\n", *argv
);
97 } else if (strcmp(*argv
, "key") == 0) {
99 p
->i_flags
|= GRE_KEY
;
100 p
->o_flags
|= GRE_KEY
;
101 p
->i_key
= p
->o_key
= tnl_parse_key("key", *argv
);
102 } else if (strcmp(*argv
, "ikey") == 0) {
104 p
->i_flags
|= GRE_KEY
;
105 p
->i_key
= tnl_parse_key("ikey", *argv
);
106 } else if (strcmp(*argv
, "okey") == 0) {
108 p
->o_flags
|= GRE_KEY
;
109 p
->o_key
= tnl_parse_key("okey", *argv
);
110 } else if (strcmp(*argv
, "seq") == 0) {
111 p
->i_flags
|= GRE_SEQ
;
112 p
->o_flags
|= GRE_SEQ
;
113 } else if (strcmp(*argv
, "iseq") == 0) {
114 p
->i_flags
|= GRE_SEQ
;
115 } else if (strcmp(*argv
, "oseq") == 0) {
116 p
->o_flags
|= GRE_SEQ
;
117 } else if (strcmp(*argv
, "csum") == 0) {
118 p
->i_flags
|= GRE_CSUM
;
119 p
->o_flags
|= GRE_CSUM
;
120 } else if (strcmp(*argv
, "icsum") == 0) {
121 p
->i_flags
|= GRE_CSUM
;
122 } else if (strcmp(*argv
, "ocsum") == 0) {
123 p
->o_flags
|= GRE_CSUM
;
124 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
126 } else if (strcmp(*argv
, "pmtudisc") == 0) {
127 p
->iph
.frag_off
= htons(IP_DF
);
128 } else if (strcmp(*argv
, "remote") == 0) {
130 if (strcmp(*argv
, "any"))
131 p
->iph
.daddr
= get_addr32(*argv
);
133 p
->iph
.daddr
= htonl(INADDR_ANY
);
134 } else if (strcmp(*argv
, "local") == 0) {
136 if (strcmp(*argv
, "any"))
137 p
->iph
.saddr
= get_addr32(*argv
);
139 p
->iph
.saddr
= htonl(INADDR_ANY
);
140 } else if (strcmp(*argv
, "dev") == 0) {
142 strncpy(medium
, *argv
, IFNAMSIZ
- 1);
143 } else if (strcmp(*argv
, "ttl") == 0 ||
144 strcmp(*argv
, "hoplimit") == 0 ||
145 strcmp(*argv
, "hlim") == 0) {
149 if (strcmp(*argv
, "inherit") != 0) {
150 if (get_u8(&uval
, *argv
, 0))
151 invarg("invalid TTL\n", *argv
);
154 } else if (strcmp(*argv
, "tos") == 0 ||
155 strcmp(*argv
, "tclass") == 0 ||
156 matches(*argv
, "dsfield") == 0) {
162 strsep(&dsfield
, "/");
163 if (strcmp(*argv
, "inherit") != 0) {
169 if (rtnl_dsfield_a2n(&uval
, dsfield
))
170 invarg("bad TOS value", *argv
);
174 if (strcmp(*argv
, "name") == 0)
176 else if (matches(*argv
, "help") == 0)
180 duparg2("name", *argv
);
181 strncpy(p
->name
, *argv
, IFNAMSIZ
- 1);
182 if (cmd
== SIOCCHGTUNNEL
&& count
== 0) {
183 struct ip_tunnel_parm old_p
= {};
185 if (tnl_get_ioctl(*argv
, &old_p
))
195 if (p
->iph
.protocol
== 0) {
196 if (memcmp(p
->name
, "gre", 3) == 0)
197 p
->iph
.protocol
= IPPROTO_GRE
;
198 else if (memcmp(p
->name
, "ipip", 4) == 0)
199 p
->iph
.protocol
= IPPROTO_IPIP
;
200 else if (memcmp(p
->name
, "sit", 3) == 0)
201 p
->iph
.protocol
= IPPROTO_IPV6
;
202 else if (memcmp(p
->name
, "isatap", 6) == 0) {
203 p
->iph
.protocol
= IPPROTO_IPV6
;
205 } else if (memcmp(p
->name
, "vti", 3) == 0) {
206 p
->iph
.protocol
= IPPROTO_IPIP
;
207 p
->i_flags
|= VTI_ISVTI
;
211 if ((p
->i_flags
& GRE_KEY
) || (p
->o_flags
& GRE_KEY
)) {
212 if (!(p
->i_flags
& VTI_ISVTI
) &&
213 (p
->iph
.protocol
!= IPPROTO_GRE
)) {
214 fprintf(stderr
, "Keys are not allowed with ipip and sit tunnels\n");
220 p
->link
= ll_name_to_index(medium
);
222 fprintf(stderr
, "Cannot find device \"%s\"\n", medium
);
227 if (p
->i_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
228 p
->i_key
= p
->iph
.daddr
;
229 p
->i_flags
|= GRE_KEY
;
231 if (p
->o_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
232 p
->o_key
= p
->iph
.daddr
;
233 p
->o_flags
|= GRE_KEY
;
235 if (IN_MULTICAST(ntohl(p
->iph
.daddr
)) && !p
->iph
.saddr
) {
236 fprintf(stderr
, "A broadcast tunnel requires a source address\n");
240 p
->i_flags
|= SIT_ISATAP
;
245 static const char *tnl_defname(const struct ip_tunnel_parm
*p
)
247 switch (p
->iph
.protocol
) {
249 if (p
->i_flags
& VTI_ISVTI
)
261 static int do_add(int cmd
, int argc
, char **argv
)
263 struct ip_tunnel_parm p
;
266 if (parse_args(argc
, argv
, cmd
, &p
) < 0)
269 if (p
.iph
.ttl
&& p
.iph
.frag_off
== 0) {
270 fprintf(stderr
, "ttl != 0 and nopmtudisc are incompatible\n");
274 basedev
= tnl_defname(&p
);
277 "cannot determine tunnel mode (ipip, gre, vti or sit)\n");
281 return tnl_add_ioctl(cmd
, basedev
, p
.name
, &p
);
284 static int do_del(int argc
, char **argv
)
286 struct ip_tunnel_parm p
;
288 if (parse_args(argc
, argv
, SIOCDELTUNNEL
, &p
) < 0)
291 return tnl_del_ioctl(tnl_defname(&p
) ? : p
.name
, p
.name
, &p
);
294 static void print_tunnel(struct ip_tunnel_parm
*p
)
296 struct ip_tunnel_6rd ip6rd
= {};
300 /* Do not use format_host() for local addr,
301 * symbolic name will not be useful.
303 printf("%s: %s/ip remote %s local %s",
305 tnl_strproto(p
->iph
.protocol
),
306 p
->iph
.daddr
? format_host_r(AF_INET
, 4, &p
->iph
.daddr
, s1
, sizeof(s1
)) : "any",
307 p
->iph
.saddr
? rt_addr_n2a_r(AF_INET
, 4, &p
->iph
.saddr
, s2
, sizeof(s2
)) : "any");
309 if (p
->iph
.protocol
== IPPROTO_IPV6
&& (p
->i_flags
& SIT_ISATAP
)) {
310 struct ip_tunnel_prl prl
[16] = {};
313 prl
[0].datalen
= sizeof(prl
) - sizeof(prl
[0]);
314 prl
[0].addr
= htonl(INADDR_ANY
);
316 if (!tnl_prl_ioctl(SIOCGETPRL
, p
->name
, prl
))
317 for (i
= 1; i
< ARRAY_SIZE(prl
); i
++) {
318 if (prl
[i
].addr
!= htonl(INADDR_ANY
)) {
320 (prl
[i
].flags
& PRL_DEFAULT
) ? "pdr" : "pr",
321 format_host(AF_INET
, 4, &prl
[i
].addr
));
327 const char *n
= ll_index_to_name(p
->link
);
330 printf(" dev %s", n
);
334 printf(" ttl %d", p
->iph
.ttl
);
336 printf(" ttl inherit");
344 printf("%c%s ", p
->iph
.tos
& 1 ? '/' : ' ',
345 rtnl_dsfield_n2a(p
->iph
.tos
& ~1, b1
, sizeof(b1
)));
348 if (!(p
->iph
.frag_off
& htons(IP_DF
)))
349 printf(" nopmtudisc");
351 if (p
->iph
.protocol
== IPPROTO_IPV6
&& !tnl_ioctl_get_6rd(p
->name
, &ip6rd
) && ip6rd
.prefixlen
) {
352 printf(" 6rd-prefix %s/%u",
353 inet_ntop(AF_INET6
, &ip6rd
.prefix
, s1
, sizeof(s1
)),
355 if (ip6rd
.relay_prefix
) {
356 printf(" 6rd-relay_prefix %s/%u",
357 format_host(AF_INET
, 4, &ip6rd
.relay_prefix
),
358 ip6rd
.relay_prefixlen
);
362 if ((p
->i_flags
& GRE_KEY
) && (p
->o_flags
& GRE_KEY
) && p
->o_key
== p
->i_key
)
363 printf(" key %u", ntohl(p
->i_key
));
364 else if ((p
->i_flags
| p
->o_flags
) & GRE_KEY
) {
365 if (p
->i_flags
& GRE_KEY
)
366 printf(" ikey %u", ntohl(p
->i_key
));
367 if (p
->o_flags
& GRE_KEY
)
368 printf(" okey %u", ntohl(p
->o_key
));
371 if (p
->i_flags
& GRE_SEQ
)
372 printf("%s Drop packets out of sequence.", _SL_
);
373 if (p
->i_flags
& GRE_CSUM
)
374 printf("%s Checksum in received packet is required.", _SL_
);
375 if (p
->o_flags
& GRE_SEQ
)
376 printf("%s Sequence packets on output.", _SL_
);
377 if (p
->o_flags
& GRE_CSUM
)
378 printf("%s Checksum output packets.", _SL_
);
381 static int do_tunnels_list(struct ip_tunnel_parm
*p
)
385 FILE *fp
= fopen("/proc/net/dev", "r");
392 /* skip header lines */
393 if (!fgets(buf
, sizeof(buf
), fp
) ||
394 !fgets(buf
, sizeof(buf
), fp
)) {
395 fprintf(stderr
, "/proc/net/dev read error\n");
399 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
402 struct ip_tunnel_parm p1
= {};
405 buf
[sizeof(buf
) - 1] = 0;
406 ptr
= strchr(buf
, ':');
408 (*ptr
++ = 0, sscanf(buf
, "%s", name
) != 1)) {
409 fprintf(stderr
, "Wrong format for /proc/net/dev. Giving up.\n");
412 if (p
->name
[0] && strcmp(p
->name
, name
))
414 index
= ll_name_to_index(name
);
417 type
= ll_index_to_type(index
);
419 fprintf(stderr
, "Failed to get type of \"%s\"\n", name
);
422 if (type
!= ARPHRD_TUNNEL
&& type
!= ARPHRD_IPGRE
&& type
!= ARPHRD_SIT
)
424 if (tnl_get_ioctl(name
, &p1
))
426 if ((p
->link
&& p1
.link
!= p
->link
) ||
427 (p
->name
[0] && strcmp(p1
.name
, p
->name
)) ||
428 (p
->iph
.daddr
&& p1
.iph
.daddr
!= p
->iph
.daddr
) ||
429 (p
->iph
.saddr
&& p1
.iph
.saddr
!= p
->iph
.saddr
) ||
430 (p
->i_key
&& p1
.i_key
!= p
->i_key
))
434 tnl_print_stats(ptr
);
443 static int do_show(int argc
, char **argv
)
445 struct ip_tunnel_parm p
;
449 if (parse_args(argc
, argv
, SIOCGETTUNNEL
, &p
) < 0)
452 basedev
= tnl_defname(&p
);
454 return do_tunnels_list(&p
);
456 if (tnl_get_ioctl(p
.name
[0] ? p
.name
: basedev
, &p
))
464 static int do_prl(int argc
, char **argv
)
466 struct ip_tunnel_prl p
= {};
470 char medium
[IFNAMSIZ
] = {};
473 if (strcmp(*argv
, "prl-default") == 0) {
476 p
.addr
= get_addr32(*argv
);
477 p
.flags
|= PRL_DEFAULT
;
479 } else if (strcmp(*argv
, "prl-nodefault") == 0) {
482 p
.addr
= get_addr32(*argv
);
484 } else if (strcmp(*argv
, "prl-delete") == 0) {
487 p
.addr
= get_addr32(*argv
);
489 } else if (strcmp(*argv
, "dev") == 0) {
491 strncpy(medium
, *argv
, IFNAMSIZ
-1);
495 "Invalid PRL parameter \"%s\"\n", *argv
);
500 "One PRL entry at a time\n");
506 fprintf(stderr
, "Must specify device\n");
510 return tnl_prl_ioctl(cmd
, medium
, &p
);
513 static int do_6rd(int argc
, char **argv
)
515 struct ip_tunnel_6rd ip6rd
= {};
518 char medium
[IFNAMSIZ
] = {};
522 if (strcmp(*argv
, "6rd-prefix") == 0) {
524 if (get_prefix(&prefix
, *argv
, AF_INET6
))
525 invarg("invalid 6rd_prefix\n", *argv
);
527 memcpy(&ip6rd
.prefix
, prefix
.data
, 16);
528 ip6rd
.prefixlen
= prefix
.bitlen
;
529 } else if (strcmp(*argv
, "6rd-relay_prefix") == 0) {
531 if (get_prefix(&prefix
, *argv
, AF_INET
))
532 invarg("invalid 6rd-relay_prefix\n", *argv
);
534 memcpy(&ip6rd
.relay_prefix
, prefix
.data
, 4);
535 ip6rd
.relay_prefixlen
= prefix
.bitlen
;
536 } else if (strcmp(*argv
, "6rd-reset") == 0) {
538 } else if (strcmp(*argv
, "dev") == 0) {
540 strncpy(medium
, *argv
, IFNAMSIZ
-1);
544 "Invalid 6RD parameter \"%s\"\n", *argv
);
550 fprintf(stderr
, "Must specify device\n");
554 return tnl_6rd_ioctl(cmd
, medium
, &ip6rd
);
557 static int tunnel_mode_is_ipv6(char *tunnel_mode
)
559 static const char * const ipv6_modes
[] = {
560 "ipv6/ipv6", "ip6ip6",
562 "ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
563 "ip6gre", "gre/ipv6",
568 for (i
= 0; i
< ARRAY_SIZE(ipv6_modes
); i
++) {
569 if (strcmp(ipv6_modes
[i
], tunnel_mode
) == 0)
575 int do_iptunnel(int argc
, char **argv
)
579 for (i
= 0; i
< argc
- 1; i
++) {
580 if (strcmp(argv
[i
], "mode") == 0) {
581 if (tunnel_mode_is_ipv6(argv
[i
+ 1]))
582 preferred_family
= AF_INET6
;
586 switch (preferred_family
) {
588 preferred_family
= AF_INET
;
593 * This is silly enough but we have no easy way to make it
594 * protocol-independent because of unarranged structure between
598 return do_ip6tunnel(argc
, argv
);
600 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
605 if (matches(*argv
, "add") == 0)
606 return do_add(SIOCADDTUNNEL
, argc
- 1, argv
+ 1);
607 if (matches(*argv
, "change") == 0)
608 return do_add(SIOCCHGTUNNEL
, argc
- 1, argv
+ 1);
609 if (matches(*argv
, "delete") == 0)
610 return do_del(argc
- 1, argv
+ 1);
611 if (matches(*argv
, "show") == 0 ||
612 matches(*argv
, "lst") == 0 ||
613 matches(*argv
, "list") == 0)
614 return do_show(argc
- 1, argv
+ 1);
615 if (matches(*argv
, "prl") == 0)
616 return do_prl(argc
- 1, argv
+ 1);
617 if (matches(*argv
, "6rd") == 0)
618 return do_6rd(argc
- 1, argv
+ 1);
619 if (matches(*argv
, "help") == 0)
622 return do_show(0, NULL
);
624 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv
);