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
= {};
295 /* Do not use format_host() for local addr,
296 * symbolic name will not be useful.
298 printf("%s: %s/ip remote %s local %s",
300 tnl_strproto(p
->iph
.protocol
),
301 p
->iph
.daddr
? format_host_r(AF_INET
, 4, &p
->iph
.daddr
, s1
, sizeof(s1
)) : "any",
302 p
->iph
.saddr
? rt_addr_n2a_r(AF_INET
, 4, &p
->iph
.saddr
, s2
, sizeof(s2
)) : "any");
304 if (p
->iph
.protocol
== IPPROTO_IPV6
&& (p
->i_flags
& SIT_ISATAP
)) {
305 struct ip_tunnel_prl prl
[16] = {};
308 prl
[0].datalen
= sizeof(prl
) - sizeof(prl
[0]);
309 prl
[0].addr
= htonl(INADDR_ANY
);
311 if (!tnl_prl_ioctl(SIOCGETPRL
, p
->name
, prl
))
312 for (i
= 1; i
< ARRAY_SIZE(prl
); i
++) {
313 if (prl
[i
].addr
!= htonl(INADDR_ANY
)) {
315 (prl
[i
].flags
& PRL_DEFAULT
) ? "pdr" : "pr",
316 format_host(AF_INET
, 4, &prl
[i
].addr
));
322 const char *n
= ll_index_to_name(p
->link
);
325 printf(" dev %s", n
);
329 printf(" ttl %u", p
->iph
.ttl
);
331 printf(" ttl inherit");
339 printf("%c%s ", p
->iph
.tos
& 1 ? '/' : ' ',
340 rtnl_dsfield_n2a(p
->iph
.tos
& ~1, b1
, sizeof(b1
)));
343 if (!(p
->iph
.frag_off
& htons(IP_DF
)))
344 printf(" nopmtudisc");
346 if (p
->iph
.protocol
== IPPROTO_IPV6
&& !tnl_ioctl_get_6rd(p
->name
, &ip6rd
) && ip6rd
.prefixlen
) {
347 printf(" 6rd-prefix %s/%u",
348 inet_ntop(AF_INET6
, &ip6rd
.prefix
, s1
, sizeof(s1
)),
350 if (ip6rd
.relay_prefix
) {
351 printf(" 6rd-relay_prefix %s/%u",
352 format_host(AF_INET
, 4, &ip6rd
.relay_prefix
),
353 ip6rd
.relay_prefixlen
);
357 if ((p
->i_flags
& GRE_KEY
) && (p
->o_flags
& GRE_KEY
) && p
->o_key
== p
->i_key
)
358 printf(" key %u", ntohl(p
->i_key
));
359 else if ((p
->i_flags
| p
->o_flags
) & GRE_KEY
) {
360 if (p
->i_flags
& GRE_KEY
)
361 printf(" ikey %u", ntohl(p
->i_key
));
362 if (p
->o_flags
& GRE_KEY
)
363 printf(" okey %u", ntohl(p
->o_key
));
366 if (p
->i_flags
& GRE_SEQ
)
367 printf("%s Drop packets out of sequence.", _SL_
);
368 if (p
->i_flags
& GRE_CSUM
)
369 printf("%s Checksum in received packet is required.", _SL_
);
370 if (p
->o_flags
& GRE_SEQ
)
371 printf("%s Sequence packets on output.", _SL_
);
372 if (p
->o_flags
& GRE_CSUM
)
373 printf("%s Checksum output packets.", _SL_
);
377 static void ip_tunnel_parm_initialize(const struct tnl_print_nlmsg_info
*info
)
379 struct ip_tunnel_parm
*p2
= info
->p2
;
381 memset(p2
, 0, sizeof(*p2
));
384 static bool ip_tunnel_parm_match(const struct tnl_print_nlmsg_info
*info
)
386 const struct ip_tunnel_parm
*p1
= info
->p1
;
387 const struct ip_tunnel_parm
*p2
= info
->p2
;
389 return ((!p1
->link
|| p1
->link
== p2
->link
) &&
390 (!p1
->name
[0] || strcmp(p1
->name
, p2
->name
) == 0) &&
391 (!p1
->iph
.daddr
|| p1
->iph
.daddr
== p2
->iph
.daddr
) &&
392 (!p1
->iph
.saddr
|| p1
->iph
.saddr
== p2
->iph
.saddr
) &&
393 (!p1
->i_key
|| p1
->i_key
== p2
->i_key
));
396 static int do_show(int argc
, char **argv
)
398 struct ip_tunnel_parm p
, p1
;
401 if (parse_args(argc
, argv
, SIOCGETTUNNEL
, &p
) < 0)
404 basedev
= tnl_defname(&p
);
406 struct tnl_print_nlmsg_info info
= {
409 .init
= ip_tunnel_parm_initialize
,
410 .match
= ip_tunnel_parm_match
,
411 .print
= print_tunnel
,
414 return do_tunnels_list(&info
);
417 if (tnl_get_ioctl(p
.name
[0] ? p
.name
: basedev
, &p
))
425 static int do_prl(int argc
, char **argv
)
427 struct ip_tunnel_prl p
= {};
430 const char *medium
= NULL
;
433 if (strcmp(*argv
, "prl-default") == 0) {
436 p
.addr
= get_addr32(*argv
);
437 p
.flags
|= PRL_DEFAULT
;
439 } else if (strcmp(*argv
, "prl-nodefault") == 0) {
442 p
.addr
= get_addr32(*argv
);
444 } else if (strcmp(*argv
, "prl-delete") == 0) {
447 p
.addr
= get_addr32(*argv
);
449 } else if (strcmp(*argv
, "dev") == 0) {
451 if (check_ifname(*argv
))
452 invarg("\"dev\" not a valid ifname", *argv
);
456 "Invalid PRL parameter \"%s\"\n", *argv
);
461 "One PRL entry at a time\n");
467 fprintf(stderr
, "Must specify device\n");
471 return tnl_prl_ioctl(cmd
, medium
, &p
);
474 static int do_6rd(int argc
, char **argv
)
476 struct ip_tunnel_6rd ip6rd
= {};
478 const char *medium
= NULL
;
482 if (strcmp(*argv
, "6rd-prefix") == 0) {
484 if (get_prefix(&prefix
, *argv
, AF_INET6
))
485 invarg("invalid 6rd_prefix\n", *argv
);
487 memcpy(&ip6rd
.prefix
, prefix
.data
, 16);
488 ip6rd
.prefixlen
= prefix
.bitlen
;
489 } else if (strcmp(*argv
, "6rd-relay_prefix") == 0) {
491 if (get_prefix(&prefix
, *argv
, AF_INET
))
492 invarg("invalid 6rd-relay_prefix\n", *argv
);
494 memcpy(&ip6rd
.relay_prefix
, prefix
.data
, 4);
495 ip6rd
.relay_prefixlen
= prefix
.bitlen
;
496 } else if (strcmp(*argv
, "6rd-reset") == 0) {
498 } else if (strcmp(*argv
, "dev") == 0) {
500 if (check_ifname(*argv
))
501 invarg("\"dev\" not a valid ifname", *argv
);
505 "Invalid 6RD parameter \"%s\"\n", *argv
);
511 fprintf(stderr
, "Must specify device\n");
515 return tnl_6rd_ioctl(cmd
, medium
, &ip6rd
);
518 static int tunnel_mode_is_ipv6(char *tunnel_mode
)
520 static const char * const ipv6_modes
[] = {
521 "ipv6/ipv6", "ip6ip6",
523 "ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
524 "ip6gre", "gre/ipv6",
529 for (i
= 0; i
< ARRAY_SIZE(ipv6_modes
); i
++) {
530 if (strcmp(ipv6_modes
[i
], tunnel_mode
) == 0)
536 int do_iptunnel(int argc
, char **argv
)
540 for (i
= 0; i
< argc
- 1; i
++) {
541 if (strcmp(argv
[i
], "mode") == 0) {
542 if (tunnel_mode_is_ipv6(argv
[i
+ 1]))
543 preferred_family
= AF_INET6
;
547 switch (preferred_family
) {
549 preferred_family
= AF_INET
;
554 * This is silly enough but we have no easy way to make it
555 * protocol-independent because of unarranged structure between
559 return do_ip6tunnel(argc
, argv
);
561 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
566 if (matches(*argv
, "add") == 0)
567 return do_add(SIOCADDTUNNEL
, argc
- 1, argv
+ 1);
568 if (matches(*argv
, "change") == 0)
569 return do_add(SIOCCHGTUNNEL
, argc
- 1, argv
+ 1);
570 if (matches(*argv
, "delete") == 0)
571 return do_del(argc
- 1, argv
+ 1);
572 if (matches(*argv
, "show") == 0 ||
573 matches(*argv
, "lst") == 0 ||
574 matches(*argv
, "list") == 0)
575 return do_show(argc
- 1, argv
+ 1);
576 if (matches(*argv
, "prl") == 0)
577 return do_prl(argc
- 1, argv
+ 1);
578 if (matches(*argv
, "6rd") == 0)
579 return do_6rd(argc
- 1, argv
+ 1);
580 if (matches(*argv
, "help") == 0)
583 return do_show(0, NULL
);
585 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv
);