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)
36 "Usage: ip tunnel { add | change | del | show | prl | 6rd } [ NAME ]\n"
37 " [ mode { ipip | gre | sit | isatap | vti } ] [ remote ADDR ] [ local ADDR ]\n"
38 " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"
39 " [ prl-default ADDR ] [ prl-nodefault ADDR ] [ prl-delete ADDR ]\n"
40 " [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n"
41 " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n"
43 "Where: NAME := STRING\n"
44 " ADDR := { IP_ADDRESS | any }\n"
45 " TOS := { STRING | 00..ff | inherit | inherit/STRING | inherit/00..ff }\n"
46 " TTL := { 1..255 | inherit }\n"
47 " KEY := { DOTTED_QUAD | NUMBER }\n");
51 static void set_tunnel_proto(struct ip_tunnel_parm
*p
, int proto
)
53 if (p
->iph
.protocol
&& p
->iph
.protocol
!= proto
) {
55 "You managed to ask for more than one tunnel mode.\n");
58 p
->iph
.protocol
= proto
;
61 static int parse_args(int argc
, char **argv
, int cmd
, struct ip_tunnel_parm
*p
)
64 const char *medium
= NULL
;
67 memset(p
, 0, sizeof(*p
));
71 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
73 p
->iph
.frag_off
= htons(IP_DF
);
76 if (strcmp(*argv
, "mode") == 0) {
78 if (strcmp(*argv
, "ipip") == 0 ||
79 strcmp(*argv
, "ip/ip") == 0) {
80 set_tunnel_proto(p
, IPPROTO_IPIP
);
81 } else if (strcmp(*argv
, "gre") == 0 ||
82 strcmp(*argv
, "gre/ip") == 0) {
83 set_tunnel_proto(p
, IPPROTO_GRE
);
84 } else if (strcmp(*argv
, "sit") == 0 ||
85 strcmp(*argv
, "ipv6/ip") == 0) {
86 set_tunnel_proto(p
, IPPROTO_IPV6
);
87 } else if (strcmp(*argv
, "isatap") == 0) {
88 set_tunnel_proto(p
, IPPROTO_IPV6
);
90 } else if (strcmp(*argv
, "vti") == 0) {
91 set_tunnel_proto(p
, IPPROTO_IPIP
);
92 p
->i_flags
|= VTI_ISVTI
;
95 "Unknown tunnel mode \"%s\"\n", *argv
);
98 } else if (strcmp(*argv
, "key") == 0) {
100 p
->i_flags
|= GRE_KEY
;
101 p
->o_flags
|= GRE_KEY
;
102 p
->i_key
= p
->o_key
= tnl_parse_key("key", *argv
);
103 } else if (strcmp(*argv
, "ikey") == 0) {
105 p
->i_flags
|= GRE_KEY
;
106 p
->i_key
= tnl_parse_key("ikey", *argv
);
107 } else if (strcmp(*argv
, "okey") == 0) {
109 p
->o_flags
|= GRE_KEY
;
110 p
->o_key
= tnl_parse_key("okey", *argv
);
111 } else if (strcmp(*argv
, "seq") == 0) {
112 p
->i_flags
|= GRE_SEQ
;
113 p
->o_flags
|= GRE_SEQ
;
114 } else if (strcmp(*argv
, "iseq") == 0) {
115 p
->i_flags
|= GRE_SEQ
;
116 } else if (strcmp(*argv
, "oseq") == 0) {
117 p
->o_flags
|= GRE_SEQ
;
118 } else if (strcmp(*argv
, "csum") == 0) {
119 p
->i_flags
|= GRE_CSUM
;
120 p
->o_flags
|= GRE_CSUM
;
121 } else if (strcmp(*argv
, "icsum") == 0) {
122 p
->i_flags
|= GRE_CSUM
;
123 } else if (strcmp(*argv
, "ocsum") == 0) {
124 p
->o_flags
|= GRE_CSUM
;
125 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
127 } else if (strcmp(*argv
, "pmtudisc") == 0) {
128 p
->iph
.frag_off
= htons(IP_DF
);
129 } else if (strcmp(*argv
, "remote") == 0) {
131 p
->iph
.daddr
= get_addr32(*argv
);
132 } else if (strcmp(*argv
, "local") == 0) {
134 p
->iph
.saddr
= get_addr32(*argv
);
135 } else if (strcmp(*argv
, "dev") == 0) {
138 } else if (strcmp(*argv
, "ttl") == 0 ||
139 strcmp(*argv
, "hoplimit") == 0 ||
140 strcmp(*argv
, "hlim") == 0) {
144 if (strcmp(*argv
, "inherit") != 0) {
145 if (get_u8(&uval
, *argv
, 0))
146 invarg("invalid TTL\n", *argv
);
149 } else if (strcmp(*argv
, "tos") == 0 ||
150 strcmp(*argv
, "tclass") == 0 ||
151 matches(*argv
, "dsfield") == 0) {
157 strsep(&dsfield
, "/");
158 if (strcmp(*argv
, "inherit") != 0) {
164 if (rtnl_dsfield_a2n(&uval
, dsfield
))
165 invarg("bad TOS value", *argv
);
169 if (strcmp(*argv
, "name") == 0)
171 else if (matches(*argv
, "help") == 0)
175 duparg2("name", *argv
);
176 if (get_ifname(p
->name
, *argv
))
177 invarg("\"name\" not a valid ifname", *argv
);
178 if (cmd
== SIOCCHGTUNNEL
&& count
== 0) {
179 struct ip_tunnel_parm old_p
= {};
181 if (tnl_get_ioctl(*argv
, &old_p
))
191 if (p
->iph
.protocol
== 0) {
192 if (memcmp(p
->name
, "gre", 3) == 0)
193 p
->iph
.protocol
= IPPROTO_GRE
;
194 else if (memcmp(p
->name
, "ipip", 4) == 0)
195 p
->iph
.protocol
= IPPROTO_IPIP
;
196 else if (memcmp(p
->name
, "sit", 3) == 0)
197 p
->iph
.protocol
= IPPROTO_IPV6
;
198 else if (memcmp(p
->name
, "isatap", 6) == 0) {
199 p
->iph
.protocol
= IPPROTO_IPV6
;
201 } else if (memcmp(p
->name
, "vti", 3) == 0) {
202 p
->iph
.protocol
= IPPROTO_IPIP
;
203 p
->i_flags
|= VTI_ISVTI
;
207 if ((p
->i_flags
& GRE_KEY
) || (p
->o_flags
& GRE_KEY
)) {
208 if (!(p
->i_flags
& VTI_ISVTI
) &&
209 (p
->iph
.protocol
!= IPPROTO_GRE
)) {
210 fprintf(stderr
, "Keys are not allowed with ipip and sit tunnels\n");
216 p
->link
= ll_name_to_index(medium
);
218 return nodev(medium
);
221 if (p
->i_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
222 p
->i_key
= p
->iph
.daddr
;
223 p
->i_flags
|= GRE_KEY
;
225 if (p
->o_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
226 p
->o_key
= p
->iph
.daddr
;
227 p
->o_flags
|= GRE_KEY
;
229 if (IN_MULTICAST(ntohl(p
->iph
.daddr
)) && !p
->iph
.saddr
) {
230 fprintf(stderr
, "A broadcast tunnel requires a source address\n");
234 p
->i_flags
|= SIT_ISATAP
;
239 static const char *tnl_defname(const struct ip_tunnel_parm
*p
)
241 switch (p
->iph
.protocol
) {
243 if (p
->i_flags
& VTI_ISVTI
)
255 static int do_add(int cmd
, int argc
, char **argv
)
257 struct ip_tunnel_parm p
;
260 if (parse_args(argc
, argv
, cmd
, &p
) < 0)
263 if (p
.iph
.ttl
&& p
.iph
.frag_off
== 0) {
264 fprintf(stderr
, "ttl != 0 and nopmtudisc are incompatible\n");
268 basedev
= tnl_defname(&p
);
271 "cannot determine tunnel mode (ipip, gre, vti or sit)\n");
275 return tnl_add_ioctl(cmd
, basedev
, p
.name
, &p
);
278 static int do_del(int argc
, char **argv
)
280 struct ip_tunnel_parm p
;
282 if (parse_args(argc
, argv
, SIOCDELTUNNEL
, &p
) < 0)
285 return tnl_del_ioctl(tnl_defname(&p
) ? : p
.name
, p
.name
, &p
);
288 static void print_tunnel(const void *t
)
290 const struct ip_tunnel_parm
*p
= t
;
291 struct ip_tunnel_6rd ip6rd
= {};
294 /* Do not use format_host() for local addr,
295 * symbolic name will not be useful.
297 open_json_object(NULL
);
298 print_color_string(PRINT_ANY
, COLOR_IFNAME
, "ifname", "%s: ", p
->name
);
299 snprintf(b1
, sizeof(b1
), "%s/ip", tnl_strproto(p
->iph
.protocol
));
300 print_string(PRINT_ANY
, "mode", "%s ", b1
);
301 print_null(PRINT_FP
, NULL
, "remote ", NULL
);
302 print_color_string(PRINT_ANY
, COLOR_INET
, "remote", "%s ",
303 p
->iph
.daddr
|| is_json_context()
304 ? format_host_r(AF_INET
, 4, &p
->iph
.daddr
, b1
, sizeof(b1
))
306 print_null(PRINT_FP
, NULL
, "local ", NULL
);
307 print_color_string(PRINT_ANY
, COLOR_INET
, "local", "%s",
308 p
->iph
.saddr
|| is_json_context()
309 ? rt_addr_n2a_r(AF_INET
, 4, &p
->iph
.saddr
, b1
, sizeof(b1
))
312 if (p
->iph
.protocol
== IPPROTO_IPV6
&& (p
->i_flags
& SIT_ISATAP
)) {
313 struct ip_tunnel_prl prl
[16] = {};
316 prl
[0].datalen
= sizeof(prl
) - sizeof(prl
[0]);
317 prl
[0].addr
= htonl(INADDR_ANY
);
319 if (!tnl_prl_ioctl(SIOCGETPRL
, p
->name
, prl
)) {
320 for (i
= 1; i
< ARRAY_SIZE(prl
); i
++) {
321 if (prl
[i
].addr
== htonl(INADDR_ANY
))
323 if (prl
[i
].flags
& PRL_DEFAULT
)
324 print_string(PRINT_ANY
, "pdr",
326 format_host(AF_INET
, 4, &prl
[i
].addr
));
328 print_string(PRINT_ANY
, "pr", " pr %s",
329 format_host(AF_INET
, 4, &prl
[i
].addr
));
335 const char *n
= ll_index_to_name(p
->link
);
338 print_string(PRINT_ANY
, "dev", " dev %s", n
);
342 print_uint(PRINT_ANY
, "ttl", " ttl %u", p
->iph
.ttl
);
344 print_string(PRINT_FP
, "ttl", " ttl %s", "inherit");
349 if (p
->iph
.tos
!= 1) {
350 if (!is_json_context() && p
->iph
.tos
& 1)
351 snprintf(b2
, sizeof(b2
), "%s%s",
352 p
->iph
.tos
& 1 ? "inherit/" : "",
353 rtnl_dsfield_n2a(p
->iph
.tos
& ~1, b1
, sizeof(b1
)));
355 snprintf(b2
, sizeof(b2
), "%s",
356 rtnl_dsfield_n2a(p
->iph
.tos
, b1
, sizeof(b1
)));
357 print_string(PRINT_ANY
, "tos", " tos %s", b2
);
359 print_string(PRINT_FP
, NULL
, " tos %s", "inherit");
363 if (!(p
->iph
.frag_off
& htons(IP_DF
)))
364 print_null(PRINT_ANY
, "nopmtudisc", " nopmtudisc", NULL
);
366 if (p
->iph
.protocol
== IPPROTO_IPV6
&& !tnl_ioctl_get_6rd(p
->name
, &ip6rd
) && ip6rd
.prefixlen
) {
367 print_string(PRINT_ANY
, "6rd-prefix", " 6rd-prefix %s",
368 inet_ntop(AF_INET6
, &ip6rd
.prefix
, b1
, sizeof(b1
)));
369 print_uint(PRINT_ANY
, "6rd-prefixlen", "/%u", ip6rd
.prefixlen
);
370 if (ip6rd
.relay_prefix
) {
371 print_string(PRINT_ANY
, "6rd-relay_prefix",
372 " 6rd-relay_prefix %s",
373 format_host(AF_INET
, 4, &ip6rd
.relay_prefix
));
374 print_uint(PRINT_ANY
, "6rd-relay_prefixlen", "/%u",
375 ip6rd
.relay_prefixlen
);
379 tnl_print_gre_flags(p
->iph
.protocol
, p
->i_flags
, p
->o_flags
,
386 static void ip_tunnel_parm_initialize(const struct tnl_print_nlmsg_info
*info
)
388 struct ip_tunnel_parm
*p2
= info
->p2
;
390 memset(p2
, 0, sizeof(*p2
));
393 static bool ip_tunnel_parm_match(const struct tnl_print_nlmsg_info
*info
)
395 const struct ip_tunnel_parm
*p1
= info
->p1
;
396 const struct ip_tunnel_parm
*p2
= info
->p2
;
398 return ((!p1
->link
|| p1
->link
== p2
->link
) &&
399 (!p1
->name
[0] || strcmp(p1
->name
, p2
->name
) == 0) &&
400 (!p1
->iph
.daddr
|| p1
->iph
.daddr
== p2
->iph
.daddr
) &&
401 (!p1
->iph
.saddr
|| p1
->iph
.saddr
== p2
->iph
.saddr
) &&
402 (!p1
->i_key
|| p1
->i_key
== p2
->i_key
));
405 static int do_show(int argc
, char **argv
)
407 struct ip_tunnel_parm p
, p1
;
410 if (parse_args(argc
, argv
, SIOCGETTUNNEL
, &p
) < 0)
413 basedev
= tnl_defname(&p
);
415 struct tnl_print_nlmsg_info info
= {
418 .init
= ip_tunnel_parm_initialize
,
419 .match
= ip_tunnel_parm_match
,
420 .print
= print_tunnel
,
423 return do_tunnels_list(&info
);
426 if (tnl_get_ioctl(p
.name
[0] ? p
.name
: basedev
, &p
))
434 static int do_prl(int argc
, char **argv
)
436 struct ip_tunnel_prl p
= {};
439 const char *medium
= NULL
;
442 if (strcmp(*argv
, "prl-default") == 0) {
445 p
.addr
= get_addr32(*argv
);
446 p
.flags
|= PRL_DEFAULT
;
448 } else if (strcmp(*argv
, "prl-nodefault") == 0) {
451 p
.addr
= get_addr32(*argv
);
453 } else if (strcmp(*argv
, "prl-delete") == 0) {
456 p
.addr
= get_addr32(*argv
);
458 } else if (strcmp(*argv
, "dev") == 0) {
460 if (check_ifname(*argv
))
461 invarg("\"dev\" not a valid ifname", *argv
);
465 "Invalid PRL parameter \"%s\"\n", *argv
);
470 "One PRL entry at a time\n");
476 fprintf(stderr
, "Must specify device\n");
480 return tnl_prl_ioctl(cmd
, medium
, &p
);
483 static int do_6rd(int argc
, char **argv
)
485 struct ip_tunnel_6rd ip6rd
= {};
487 const char *medium
= NULL
;
491 if (strcmp(*argv
, "6rd-prefix") == 0) {
493 if (get_prefix(&prefix
, *argv
, AF_INET6
))
494 invarg("invalid 6rd_prefix\n", *argv
);
496 memcpy(&ip6rd
.prefix
, prefix
.data
, 16);
497 ip6rd
.prefixlen
= prefix
.bitlen
;
498 } else if (strcmp(*argv
, "6rd-relay_prefix") == 0) {
500 if (get_prefix(&prefix
, *argv
, AF_INET
))
501 invarg("invalid 6rd-relay_prefix\n", *argv
);
503 memcpy(&ip6rd
.relay_prefix
, prefix
.data
, 4);
504 ip6rd
.relay_prefixlen
= prefix
.bitlen
;
505 } else if (strcmp(*argv
, "6rd-reset") == 0) {
507 } else if (strcmp(*argv
, "dev") == 0) {
509 if (check_ifname(*argv
))
510 invarg("\"dev\" not a valid ifname", *argv
);
514 "Invalid 6RD parameter \"%s\"\n", *argv
);
520 fprintf(stderr
, "Must specify device\n");
524 return tnl_6rd_ioctl(cmd
, medium
, &ip6rd
);
527 static int tunnel_mode_is_ipv6(char *tunnel_mode
)
529 static const char * const ipv6_modes
[] = {
530 "ipv6/ipv6", "ip6ip6",
532 "ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
533 "ip6gre", "gre/ipv6",
538 for (i
= 0; i
< ARRAY_SIZE(ipv6_modes
); i
++) {
539 if (strcmp(ipv6_modes
[i
], tunnel_mode
) == 0)
545 int do_iptunnel(int argc
, char **argv
)
549 for (i
= 0; i
< argc
- 1; i
++) {
550 if (strcmp(argv
[i
], "mode") == 0) {
551 if (tunnel_mode_is_ipv6(argv
[i
+ 1]))
552 preferred_family
= AF_INET6
;
556 switch (preferred_family
) {
558 preferred_family
= AF_INET
;
563 * This is silly enough but we have no easy way to make it
564 * protocol-independent because of unarranged structure between
568 return do_ip6tunnel(argc
, argv
);
570 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
575 if (matches(*argv
, "add") == 0)
576 return do_add(SIOCADDTUNNEL
, argc
- 1, argv
+ 1);
577 if (matches(*argv
, "change") == 0)
578 return do_add(SIOCCHGTUNNEL
, argc
- 1, argv
+ 1);
579 if (matches(*argv
, "delete") == 0)
580 return do_del(argc
- 1, argv
+ 1);
581 if (matches(*argv
, "show") == 0 ||
582 matches(*argv
, "lst") == 0 ||
583 matches(*argv
, "list") == 0)
584 return do_show(argc
- 1, argv
+ 1);
585 if (matches(*argv
, "prl") == 0)
586 return do_prl(argc
- 1, argv
+ 1);
587 if (matches(*argv
, "6rd") == 0)
588 return do_6rd(argc
- 1, argv
+ 1);
589 if (matches(*argv
, "help") == 0)
592 return do_show(0, NULL
);
594 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv
);