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 const char *medium
= NULL
;
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 p
->iph
.daddr
= get_addr32(*argv
);
131 } else if (strcmp(*argv
, "local") == 0) {
133 p
->iph
.saddr
= get_addr32(*argv
);
134 } else if (strcmp(*argv
, "dev") == 0) {
137 } else if (strcmp(*argv
, "ttl") == 0 ||
138 strcmp(*argv
, "hoplimit") == 0 ||
139 strcmp(*argv
, "hlim") == 0) {
143 if (strcmp(*argv
, "inherit") != 0) {
144 if (get_u8(&uval
, *argv
, 0))
145 invarg("invalid TTL\n", *argv
);
148 } else if (strcmp(*argv
, "tos") == 0 ||
149 strcmp(*argv
, "tclass") == 0 ||
150 matches(*argv
, "dsfield") == 0) {
156 strsep(&dsfield
, "/");
157 if (strcmp(*argv
, "inherit") != 0) {
163 if (rtnl_dsfield_a2n(&uval
, dsfield
))
164 invarg("bad TOS value", *argv
);
168 if (strcmp(*argv
, "name") == 0)
170 else if (matches(*argv
, "help") == 0)
174 duparg2("name", *argv
);
175 if (get_ifname(p
->name
, *argv
))
176 invarg("\"name\" not a valid ifname", *argv
);
177 if (cmd
== SIOCCHGTUNNEL
&& count
== 0) {
178 struct ip_tunnel_parm old_p
= {};
180 if (tnl_get_ioctl(*argv
, &old_p
))
190 if (p
->iph
.protocol
== 0) {
191 if (memcmp(p
->name
, "gre", 3) == 0)
192 p
->iph
.protocol
= IPPROTO_GRE
;
193 else if (memcmp(p
->name
, "ipip", 4) == 0)
194 p
->iph
.protocol
= IPPROTO_IPIP
;
195 else if (memcmp(p
->name
, "sit", 3) == 0)
196 p
->iph
.protocol
= IPPROTO_IPV6
;
197 else if (memcmp(p
->name
, "isatap", 6) == 0) {
198 p
->iph
.protocol
= IPPROTO_IPV6
;
200 } else if (memcmp(p
->name
, "vti", 3) == 0) {
201 p
->iph
.protocol
= IPPROTO_IPIP
;
202 p
->i_flags
|= VTI_ISVTI
;
206 if ((p
->i_flags
& GRE_KEY
) || (p
->o_flags
& GRE_KEY
)) {
207 if (!(p
->i_flags
& VTI_ISVTI
) &&
208 (p
->iph
.protocol
!= IPPROTO_GRE
)) {
209 fprintf(stderr
, "Keys are not allowed with ipip and sit tunnels\n");
215 p
->link
= ll_name_to_index(medium
);
217 return nodev(medium
);
220 if (p
->i_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
221 p
->i_key
= p
->iph
.daddr
;
222 p
->i_flags
|= GRE_KEY
;
224 if (p
->o_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
225 p
->o_key
= p
->iph
.daddr
;
226 p
->o_flags
|= GRE_KEY
;
228 if (IN_MULTICAST(ntohl(p
->iph
.daddr
)) && !p
->iph
.saddr
) {
229 fprintf(stderr
, "A broadcast tunnel requires a source address\n");
233 p
->i_flags
|= SIT_ISATAP
;
238 static const char *tnl_defname(const struct ip_tunnel_parm
*p
)
240 switch (p
->iph
.protocol
) {
242 if (p
->i_flags
& VTI_ISVTI
)
254 static int do_add(int cmd
, int argc
, char **argv
)
256 struct ip_tunnel_parm p
;
259 if (parse_args(argc
, argv
, cmd
, &p
) < 0)
262 if (p
.iph
.ttl
&& p
.iph
.frag_off
== 0) {
263 fprintf(stderr
, "ttl != 0 and nopmtudisc are incompatible\n");
267 basedev
= tnl_defname(&p
);
270 "cannot determine tunnel mode (ipip, gre, vti or sit)\n");
274 return tnl_add_ioctl(cmd
, basedev
, p
.name
, &p
);
277 static int do_del(int argc
, char **argv
)
279 struct ip_tunnel_parm p
;
281 if (parse_args(argc
, argv
, SIOCDELTUNNEL
, &p
) < 0)
284 return tnl_del_ioctl(tnl_defname(&p
) ? : p
.name
, p
.name
, &p
);
287 static void print_tunnel(const void *t
)
289 const struct ip_tunnel_parm
*p
= t
;
290 struct ip_tunnel_6rd ip6rd
= {};
294 /* Do not use format_host() for local addr,
295 * symbolic name will not be useful.
297 printf("%s: %s/ip remote %s local %s",
299 tnl_strproto(p
->iph
.protocol
),
300 p
->iph
.daddr
? format_host_r(AF_INET
, 4, &p
->iph
.daddr
, s1
, sizeof(s1
)) : "any",
301 p
->iph
.saddr
? rt_addr_n2a_r(AF_INET
, 4, &p
->iph
.saddr
, s2
, sizeof(s2
)) : "any");
303 if (p
->iph
.protocol
== IPPROTO_IPV6
&& (p
->i_flags
& SIT_ISATAP
)) {
304 struct ip_tunnel_prl prl
[16] = {};
307 prl
[0].datalen
= sizeof(prl
) - sizeof(prl
[0]);
308 prl
[0].addr
= htonl(INADDR_ANY
);
310 if (!tnl_prl_ioctl(SIOCGETPRL
, p
->name
, prl
))
311 for (i
= 1; i
< ARRAY_SIZE(prl
); i
++) {
312 if (prl
[i
].addr
!= htonl(INADDR_ANY
)) {
314 (prl
[i
].flags
& PRL_DEFAULT
) ? "pdr" : "pr",
315 format_host(AF_INET
, 4, &prl
[i
].addr
));
321 const char *n
= ll_index_to_name(p
->link
);
324 printf(" dev %s", n
);
328 printf(" ttl %u", p
->iph
.ttl
);
330 printf(" ttl inherit");
338 printf("%c%s ", p
->iph
.tos
& 1 ? '/' : ' ',
339 rtnl_dsfield_n2a(p
->iph
.tos
& ~1, b1
, sizeof(b1
)));
342 if (!(p
->iph
.frag_off
& htons(IP_DF
)))
343 printf(" nopmtudisc");
345 if (p
->iph
.protocol
== IPPROTO_IPV6
&& !tnl_ioctl_get_6rd(p
->name
, &ip6rd
) && ip6rd
.prefixlen
) {
346 printf(" 6rd-prefix %s/%u",
347 inet_ntop(AF_INET6
, &ip6rd
.prefix
, s1
, sizeof(s1
)),
349 if (ip6rd
.relay_prefix
) {
350 printf(" 6rd-relay_prefix %s/%u",
351 format_host(AF_INET
, 4, &ip6rd
.relay_prefix
),
352 ip6rd
.relay_prefixlen
);
356 if ((p
->i_flags
& GRE_KEY
) && (p
->o_flags
& GRE_KEY
) && p
->o_key
== p
->i_key
)
357 printf(" key %u", ntohl(p
->i_key
));
358 else if ((p
->i_flags
| p
->o_flags
) & GRE_KEY
) {
359 if (p
->i_flags
& GRE_KEY
)
360 printf(" ikey %u", ntohl(p
->i_key
));
361 if (p
->o_flags
& GRE_KEY
)
362 printf(" okey %u", ntohl(p
->o_key
));
365 if (p
->i_flags
& GRE_SEQ
)
366 printf("%s Drop packets out of sequence.", _SL_
);
367 if (p
->i_flags
& GRE_CSUM
)
368 printf("%s Checksum in received packet is required.", _SL_
);
369 if (p
->o_flags
& GRE_SEQ
)
370 printf("%s Sequence packets on output.", _SL_
);
371 if (p
->o_flags
& GRE_CSUM
)
372 printf("%s Checksum output packets.", _SL_
);
376 static void ip_tunnel_parm_initialize(const struct tnl_print_nlmsg_info
*info
)
378 struct ip_tunnel_parm
*p2
= info
->p2
;
380 memset(p2
, 0, sizeof(*p2
));
383 static bool ip_tunnel_parm_match(const struct tnl_print_nlmsg_info
*info
)
385 const struct ip_tunnel_parm
*p1
= info
->p1
;
386 const struct ip_tunnel_parm
*p2
= info
->p2
;
388 return ((!p1
->link
|| p1
->link
== p2
->link
) &&
389 (!p1
->name
[0] || strcmp(p1
->name
, p2
->name
) == 0) &&
390 (!p1
->iph
.daddr
|| p1
->iph
.daddr
== p2
->iph
.daddr
) &&
391 (!p1
->iph
.saddr
|| p1
->iph
.saddr
== p2
->iph
.saddr
) &&
392 (!p1
->i_key
|| p1
->i_key
== p2
->i_key
));
395 static int do_show(int argc
, char **argv
)
397 struct ip_tunnel_parm p
, p1
;
400 if (parse_args(argc
, argv
, SIOCGETTUNNEL
, &p
) < 0)
403 basedev
= tnl_defname(&p
);
405 struct tnl_print_nlmsg_info info
= {
408 .init
= ip_tunnel_parm_initialize
,
409 .match
= ip_tunnel_parm_match
,
410 .print
= print_tunnel
,
413 return do_tunnels_list(&info
);
416 if (tnl_get_ioctl(p
.name
[0] ? p
.name
: basedev
, &p
))
424 static int do_prl(int argc
, char **argv
)
426 struct ip_tunnel_prl p
= {};
429 const char *medium
= NULL
;
432 if (strcmp(*argv
, "prl-default") == 0) {
435 p
.addr
= get_addr32(*argv
);
436 p
.flags
|= PRL_DEFAULT
;
438 } else if (strcmp(*argv
, "prl-nodefault") == 0) {
441 p
.addr
= get_addr32(*argv
);
443 } else if (strcmp(*argv
, "prl-delete") == 0) {
446 p
.addr
= get_addr32(*argv
);
448 } else if (strcmp(*argv
, "dev") == 0) {
450 if (check_ifname(*argv
))
451 invarg("\"dev\" not a valid ifname", *argv
);
455 "Invalid PRL parameter \"%s\"\n", *argv
);
460 "One PRL entry at a time\n");
466 fprintf(stderr
, "Must specify device\n");
470 return tnl_prl_ioctl(cmd
, medium
, &p
);
473 static int do_6rd(int argc
, char **argv
)
475 struct ip_tunnel_6rd ip6rd
= {};
477 const char *medium
= NULL
;
481 if (strcmp(*argv
, "6rd-prefix") == 0) {
483 if (get_prefix(&prefix
, *argv
, AF_INET6
))
484 invarg("invalid 6rd_prefix\n", *argv
);
486 memcpy(&ip6rd
.prefix
, prefix
.data
, 16);
487 ip6rd
.prefixlen
= prefix
.bitlen
;
488 } else if (strcmp(*argv
, "6rd-relay_prefix") == 0) {
490 if (get_prefix(&prefix
, *argv
, AF_INET
))
491 invarg("invalid 6rd-relay_prefix\n", *argv
);
493 memcpy(&ip6rd
.relay_prefix
, prefix
.data
, 4);
494 ip6rd
.relay_prefixlen
= prefix
.bitlen
;
495 } else if (strcmp(*argv
, "6rd-reset") == 0) {
497 } else if (strcmp(*argv
, "dev") == 0) {
499 if (check_ifname(*argv
))
500 invarg("\"dev\" not a valid ifname", *argv
);
504 "Invalid 6RD parameter \"%s\"\n", *argv
);
510 fprintf(stderr
, "Must specify device\n");
514 return tnl_6rd_ioctl(cmd
, medium
, &ip6rd
);
517 static int tunnel_mode_is_ipv6(char *tunnel_mode
)
519 static const char * const ipv6_modes
[] = {
520 "ipv6/ipv6", "ip6ip6",
522 "ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
523 "ip6gre", "gre/ipv6",
528 for (i
= 0; i
< ARRAY_SIZE(ipv6_modes
); i
++) {
529 if (strcmp(ipv6_modes
[i
], tunnel_mode
) == 0)
535 int do_iptunnel(int argc
, char **argv
)
539 for (i
= 0; i
< argc
- 1; i
++) {
540 if (strcmp(argv
[i
], "mode") == 0) {
541 if (tunnel_mode_is_ipv6(argv
[i
+ 1]))
542 preferred_family
= AF_INET6
;
546 switch (preferred_family
) {
548 preferred_family
= AF_INET
;
553 * This is silly enough but we have no easy way to make it
554 * protocol-independent because of unarranged structure between
558 return do_ip6tunnel(argc
, argv
);
560 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
565 if (matches(*argv
, "add") == 0)
566 return do_add(SIOCADDTUNNEL
, argc
- 1, argv
+ 1);
567 if (matches(*argv
, "change") == 0)
568 return do_add(SIOCCHGTUNNEL
, argc
- 1, argv
+ 1);
569 if (matches(*argv
, "delete") == 0)
570 return do_del(argc
- 1, argv
+ 1);
571 if (matches(*argv
, "show") == 0 ||
572 matches(*argv
, "lst") == 0 ||
573 matches(*argv
, "list") == 0)
574 return do_show(argc
- 1, argv
+ 1);
575 if (matches(*argv
, "prl") == 0)
576 return do_prl(argc
- 1, argv
+ 1);
577 if (matches(*argv
, "6rd") == 0)
578 return do_6rd(argc
- 1, argv
+ 1);
579 if (matches(*argv
, "help") == 0)
582 return do_show(0, NULL
);
584 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv
);