]>
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
));
67 memset(&medium
, 0, sizeof(medium
));
72 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
74 p
->iph
.frag_off
= htons(IP_DF
);
77 if (strcmp(*argv
, "mode") == 0) {
79 if (strcmp(*argv
, "ipip") == 0 ||
80 strcmp(*argv
, "ip/ip") == 0) {
81 set_tunnel_proto(p
, IPPROTO_IPIP
);
82 } else if (strcmp(*argv
, "gre") == 0 ||
83 strcmp(*argv
, "gre/ip") == 0) {
84 set_tunnel_proto(p
, IPPROTO_GRE
);
85 } else if (strcmp(*argv
, "sit") == 0 ||
86 strcmp(*argv
, "ipv6/ip") == 0) {
87 set_tunnel_proto(p
, IPPROTO_IPV6
);
88 } else if (strcmp(*argv
, "isatap") == 0) {
89 set_tunnel_proto(p
, IPPROTO_IPV6
);
91 } else if (strcmp(*argv
, "vti") == 0) {
92 set_tunnel_proto(p
, IPPROTO_IPIP
);
93 p
->i_flags
|= VTI_ISVTI
;
96 "Unknown tunnel mode \"%s\"\n", *argv
);
99 } else if (strcmp(*argv
, "key") == 0) {
101 p
->i_flags
|= GRE_KEY
;
102 p
->o_flags
|= GRE_KEY
;
103 p
->i_key
= p
->o_key
= tnl_parse_key("key", *argv
);
104 } else if (strcmp(*argv
, "ikey") == 0) {
106 p
->i_flags
|= GRE_KEY
;
107 p
->i_key
= tnl_parse_key("ikey", *argv
);
108 } else if (strcmp(*argv
, "okey") == 0) {
110 p
->o_flags
|= GRE_KEY
;
111 p
->o_key
= tnl_parse_key("okey", *argv
);
112 } else if (strcmp(*argv
, "seq") == 0) {
113 p
->i_flags
|= GRE_SEQ
;
114 p
->o_flags
|= GRE_SEQ
;
115 } else if (strcmp(*argv
, "iseq") == 0) {
116 p
->i_flags
|= GRE_SEQ
;
117 } else if (strcmp(*argv
, "oseq") == 0) {
118 p
->o_flags
|= GRE_SEQ
;
119 } else if (strcmp(*argv
, "csum") == 0) {
120 p
->i_flags
|= GRE_CSUM
;
121 p
->o_flags
|= GRE_CSUM
;
122 } else if (strcmp(*argv
, "icsum") == 0) {
123 p
->i_flags
|= GRE_CSUM
;
124 } else if (strcmp(*argv
, "ocsum") == 0) {
125 p
->o_flags
|= GRE_CSUM
;
126 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
128 } else if (strcmp(*argv
, "pmtudisc") == 0) {
129 p
->iph
.frag_off
= htons(IP_DF
);
130 } else if (strcmp(*argv
, "remote") == 0) {
132 if (strcmp(*argv
, "any"))
133 p
->iph
.daddr
= get_addr32(*argv
);
135 p
->iph
.daddr
= htonl(INADDR_ANY
);
136 } else if (strcmp(*argv
, "local") == 0) {
138 if (strcmp(*argv
, "any"))
139 p
->iph
.saddr
= get_addr32(*argv
);
141 p
->iph
.saddr
= htonl(INADDR_ANY
);
142 } else if (strcmp(*argv
, "dev") == 0) {
144 strncpy(medium
, *argv
, IFNAMSIZ
- 1);
145 } else if (strcmp(*argv
, "ttl") == 0 ||
146 strcmp(*argv
, "hoplimit") == 0 ||
147 strcmp(*argv
, "hlim") == 0) {
151 if (strcmp(*argv
, "inherit") != 0) {
152 if (get_u8(&uval
, *argv
, 0))
153 invarg("invalid TTL\n", *argv
);
156 } else if (strcmp(*argv
, "tos") == 0 ||
157 strcmp(*argv
, "tclass") == 0 ||
158 matches(*argv
, "dsfield") == 0) {
164 strsep(&dsfield
, "/");
165 if (strcmp(*argv
, "inherit") != 0) {
171 if (rtnl_dsfield_a2n(&uval
, dsfield
))
172 invarg("bad TOS value", *argv
);
176 if (strcmp(*argv
, "name") == 0)
178 else if (matches(*argv
, "help") == 0)
182 duparg2("name", *argv
);
183 strncpy(p
->name
, *argv
, IFNAMSIZ
- 1);
184 if (cmd
== SIOCCHGTUNNEL
&& count
== 0) {
185 struct ip_tunnel_parm old_p
;
187 memset(&old_p
, 0, sizeof(old_p
));
188 if (tnl_get_ioctl(*argv
, &old_p
))
198 if (p
->iph
.protocol
== 0) {
199 if (memcmp(p
->name
, "gre", 3) == 0)
200 p
->iph
.protocol
= IPPROTO_GRE
;
201 else if (memcmp(p
->name
, "ipip", 4) == 0)
202 p
->iph
.protocol
= IPPROTO_IPIP
;
203 else if (memcmp(p
->name
, "sit", 3) == 0)
204 p
->iph
.protocol
= IPPROTO_IPV6
;
205 else if (memcmp(p
->name
, "isatap", 6) == 0) {
206 p
->iph
.protocol
= IPPROTO_IPV6
;
208 } else if (memcmp(p
->name
, "vti", 3) == 0) {
209 p
->iph
.protocol
= IPPROTO_IPIP
;
210 p
->i_flags
|= VTI_ISVTI
;
214 if ((p
->i_flags
& GRE_KEY
) || (p
->o_flags
& GRE_KEY
)) {
215 if (!(p
->i_flags
& VTI_ISVTI
) &&
216 (p
->iph
.protocol
!= IPPROTO_GRE
)) {
217 fprintf(stderr
, "Keys are not allowed with ipip and sit tunnels\n");
223 p
->link
= ll_name_to_index(medium
);
225 fprintf(stderr
, "Cannot find device \"%s\"\n", medium
);
230 if (p
->i_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
231 p
->i_key
= p
->iph
.daddr
;
232 p
->i_flags
|= GRE_KEY
;
234 if (p
->o_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
235 p
->o_key
= p
->iph
.daddr
;
236 p
->o_flags
|= GRE_KEY
;
238 if (IN_MULTICAST(ntohl(p
->iph
.daddr
)) && !p
->iph
.saddr
) {
239 fprintf(stderr
, "A broadcast tunnel requires a source address\n");
243 p
->i_flags
|= SIT_ISATAP
;
248 static const char *tnl_defname(const struct ip_tunnel_parm
*p
)
250 switch (p
->iph
.protocol
) {
252 if (p
->i_flags
& VTI_ISVTI
)
264 static int do_add(int cmd
, int argc
, char **argv
)
266 struct ip_tunnel_parm p
;
269 if (parse_args(argc
, argv
, cmd
, &p
) < 0)
272 if (p
.iph
.ttl
&& p
.iph
.frag_off
== 0) {
273 fprintf(stderr
, "ttl != 0 and nopmtudisc are incompatible\n");
277 basedev
= tnl_defname(&p
);
280 "cannot determine tunnel mode (ipip, gre, vti or sit)\n");
284 return tnl_add_ioctl(cmd
, basedev
, p
.name
, &p
);
287 static int do_del(int argc
, char **argv
)
289 struct ip_tunnel_parm p
;
291 if (parse_args(argc
, argv
, SIOCDELTUNNEL
, &p
) < 0)
294 return tnl_del_ioctl(tnl_defname(&p
) ? : p
.name
, p
.name
, &p
);
297 static void print_tunnel(struct ip_tunnel_parm
*p
)
299 struct ip_tunnel_6rd ip6rd
;
303 memset(&ip6rd
, 0, sizeof(ip6rd
));
305 /* Do not use format_host() for local addr,
306 * symbolic name will not be useful.
308 printf("%s: %s/ip remote %s local %s",
310 tnl_strproto(p
->iph
.protocol
),
311 p
->iph
.daddr
? format_host_r(AF_INET
, 4, &p
->iph
.daddr
, s1
, sizeof(s1
)) : "any",
312 p
->iph
.saddr
? rt_addr_n2a_r(AF_INET
, 4, &p
->iph
.saddr
, s2
, sizeof(s2
)) : "any");
314 if (p
->iph
.protocol
== IPPROTO_IPV6
&& (p
->i_flags
& SIT_ISATAP
)) {
315 struct ip_tunnel_prl prl
[16];
318 memset(prl
, 0, sizeof(prl
));
319 prl
[0].datalen
= sizeof(prl
) - sizeof(prl
[0]);
320 prl
[0].addr
= htonl(INADDR_ANY
);
322 if (!tnl_prl_ioctl(SIOCGETPRL
, p
->name
, prl
))
323 for (i
= 1; i
< ARRAY_SIZE(prl
); i
++) {
324 if (prl
[i
].addr
!= htonl(INADDR_ANY
)) {
326 (prl
[i
].flags
& PRL_DEFAULT
) ? "pdr" : "pr",
327 format_host(AF_INET
, 4, &prl
[i
].addr
));
333 const char *n
= ll_index_to_name(p
->link
);
336 printf(" dev %s", n
);
340 printf(" ttl %d", p
->iph
.ttl
);
342 printf(" ttl inherit");
350 printf("%c%s ", p
->iph
.tos
& 1 ? '/' : ' ',
351 rtnl_dsfield_n2a(p
->iph
.tos
& ~1, b1
, sizeof(b1
)));
354 if (!(p
->iph
.frag_off
& htons(IP_DF
)))
355 printf(" nopmtudisc");
357 if (p
->iph
.protocol
== IPPROTO_IPV6
&& !tnl_ioctl_get_6rd(p
->name
, &ip6rd
) && ip6rd
.prefixlen
) {
358 printf(" 6rd-prefix %s/%u",
359 inet_ntop(AF_INET6
, &ip6rd
.prefix
, s1
, sizeof(s1
)),
361 if (ip6rd
.relay_prefix
) {
362 printf(" 6rd-relay_prefix %s/%u",
363 format_host(AF_INET
, 4, &ip6rd
.relay_prefix
),
364 ip6rd
.relay_prefixlen
);
368 if ((p
->i_flags
& GRE_KEY
) && (p
->o_flags
& GRE_KEY
) && p
->o_key
== p
->i_key
)
369 printf(" key %u", ntohl(p
->i_key
));
370 else if ((p
->i_flags
| p
->o_flags
) & GRE_KEY
) {
371 if (p
->i_flags
& GRE_KEY
)
372 printf(" ikey %u", ntohl(p
->i_key
));
373 if (p
->o_flags
& GRE_KEY
)
374 printf(" okey %u", ntohl(p
->o_key
));
377 if (p
->i_flags
& GRE_SEQ
)
378 printf("%s Drop packets out of sequence.", _SL_
);
379 if (p
->i_flags
& GRE_CSUM
)
380 printf("%s Checksum in received packet is required.", _SL_
);
381 if (p
->o_flags
& GRE_SEQ
)
382 printf("%s Sequence packets on output.", _SL_
);
383 if (p
->o_flags
& GRE_CSUM
)
384 printf("%s Checksum output packets.", _SL_
);
387 static int do_tunnels_list(struct ip_tunnel_parm
*p
)
391 FILE *fp
= fopen("/proc/net/dev", "r");
398 /* skip header lines */
399 if (!fgets(buf
, sizeof(buf
), fp
) ||
400 !fgets(buf
, sizeof(buf
), fp
)) {
401 fprintf(stderr
, "/proc/net/dev read error\n");
405 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
408 struct ip_tunnel_parm p1
;
411 buf
[sizeof(buf
) - 1] = 0;
412 ptr
= strchr(buf
, ':');
414 (*ptr
++ = 0, sscanf(buf
, "%s", name
) != 1)) {
415 fprintf(stderr
, "Wrong format for /proc/net/dev. Giving up.\n");
418 if (p
->name
[0] && strcmp(p
->name
, name
))
420 index
= ll_name_to_index(name
);
423 type
= ll_index_to_type(index
);
425 fprintf(stderr
, "Failed to get type of \"%s\"\n", name
);
428 if (type
!= ARPHRD_TUNNEL
&& type
!= ARPHRD_IPGRE
&& type
!= ARPHRD_SIT
)
430 memset(&p1
, 0, sizeof(p1
));
431 if (tnl_get_ioctl(name
, &p1
))
433 if ((p
->link
&& p1
.link
!= p
->link
) ||
434 (p
->name
[0] && strcmp(p1
.name
, p
->name
)) ||
435 (p
->iph
.daddr
&& p1
.iph
.daddr
!= p
->iph
.daddr
) ||
436 (p
->iph
.saddr
&& p1
.iph
.saddr
!= p
->iph
.saddr
) ||
437 (p
->i_key
&& p1
.i_key
!= p
->i_key
))
441 tnl_print_stats(ptr
);
450 static int do_show(int argc
, char **argv
)
452 struct ip_tunnel_parm p
;
456 if (parse_args(argc
, argv
, SIOCGETTUNNEL
, &p
) < 0)
459 basedev
= tnl_defname(&p
);
461 return do_tunnels_list(&p
);
463 if (tnl_get_ioctl(p
.name
[0] ? p
.name
: basedev
, &p
))
471 static int do_prl(int argc
, char **argv
)
473 struct ip_tunnel_prl p
;
477 char medium
[IFNAMSIZ
];
479 memset(&p
, 0, sizeof(p
));
480 memset(&medium
, 0, sizeof(medium
));
483 if (strcmp(*argv
, "prl-default") == 0) {
486 p
.addr
= get_addr32(*argv
);
487 p
.flags
|= PRL_DEFAULT
;
489 } else if (strcmp(*argv
, "prl-nodefault") == 0) {
492 p
.addr
= get_addr32(*argv
);
494 } else if (strcmp(*argv
, "prl-delete") == 0) {
497 p
.addr
= get_addr32(*argv
);
499 } else if (strcmp(*argv
, "dev") == 0) {
501 strncpy(medium
, *argv
, IFNAMSIZ
-1);
505 "Invalid PRL parameter \"%s\"\n", *argv
);
510 "One PRL entry at a time\n");
516 fprintf(stderr
, "Must specify device\n");
520 return tnl_prl_ioctl(cmd
, medium
, &p
);
523 static int do_6rd(int argc
, char **argv
)
525 struct ip_tunnel_6rd ip6rd
;
528 char medium
[IFNAMSIZ
];
531 memset(&ip6rd
, 0, sizeof(ip6rd
));
532 memset(&medium
, 0, sizeof(medium
));
535 if (strcmp(*argv
, "6rd-prefix") == 0) {
537 if (get_prefix(&prefix
, *argv
, AF_INET6
))
538 invarg("invalid 6rd_prefix\n", *argv
);
540 memcpy(&ip6rd
.prefix
, prefix
.data
, 16);
541 ip6rd
.prefixlen
= prefix
.bitlen
;
542 } else if (strcmp(*argv
, "6rd-relay_prefix") == 0) {
544 if (get_prefix(&prefix
, *argv
, AF_INET
))
545 invarg("invalid 6rd-relay_prefix\n", *argv
);
547 memcpy(&ip6rd
.relay_prefix
, prefix
.data
, 4);
548 ip6rd
.relay_prefixlen
= prefix
.bitlen
;
549 } else if (strcmp(*argv
, "6rd-reset") == 0) {
551 } else if (strcmp(*argv
, "dev") == 0) {
553 strncpy(medium
, *argv
, IFNAMSIZ
-1);
557 "Invalid 6RD parameter \"%s\"\n", *argv
);
563 fprintf(stderr
, "Must specify device\n");
567 return tnl_6rd_ioctl(cmd
, medium
, &ip6rd
);
570 static int tunnel_mode_is_ipv6(char *tunnel_mode
)
572 static const char * const ipv6_modes
[] = {
573 "ipv6/ipv6", "ip6ip6",
575 "ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
576 "ip6gre", "gre/ipv6",
581 for (i
= 0; i
< ARRAY_SIZE(ipv6_modes
); i
++) {
582 if (strcmp(ipv6_modes
[i
], tunnel_mode
) == 0)
588 int do_iptunnel(int argc
, char **argv
)
592 for (i
= 0; i
< argc
- 1; i
++) {
593 if (strcmp(argv
[i
], "mode") == 0) {
594 if (tunnel_mode_is_ipv6(argv
[i
+ 1]))
595 preferred_family
= AF_INET6
;
599 switch (preferred_family
) {
601 preferred_family
= AF_INET
;
606 * This is silly enough but we have no easy way to make it
607 * protocol-independent because of unarranged structure between
611 return do_ip6tunnel(argc
, argv
);
613 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
618 if (matches(*argv
, "add") == 0)
619 return do_add(SIOCADDTUNNEL
, argc
- 1, argv
+ 1);
620 if (matches(*argv
, "change") == 0)
621 return do_add(SIOCCHGTUNNEL
, argc
- 1, argv
+ 1);
622 if (matches(*argv
, "delete") == 0)
623 return do_del(argc
- 1, argv
+ 1);
624 if (matches(*argv
, "show") == 0 ||
625 matches(*argv
, "lst") == 0 ||
626 matches(*argv
, "list") == 0)
627 return do_show(argc
- 1, argv
+ 1);
628 if (matches(*argv
, "prl") == 0)
629 return do_prl(argc
- 1, argv
+ 1);
630 if (matches(*argv
, "6rd") == 0)
631 return do_6rd(argc
- 1, argv
+ 1);
632 if (matches(*argv
, "help") == 0)
635 return do_show(0, NULL
);
637 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv
);