]>
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>
14 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
15 * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
16 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <arpa/inet.h>
26 #include <sys/ioctl.h>
28 #include <linux/if_arp.h>
30 #include <linux/if_tunnel.h>
34 #include "ip_common.h"
37 static void usage(void) __attribute__((noreturn
));
39 static void usage(void)
41 fprintf(stderr
, "Usage: ip tunnel { add | change | del | show } [ NAME ]\n");
42 fprintf(stderr
, " [ mode { ipip | gre | sit | isatap } ] [ remote ADDR ] [ local ADDR ]\n");
43 fprintf(stderr
, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
44 fprintf(stderr
, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
45 fprintf(stderr
, "\n");
46 fprintf(stderr
, "Where: NAME := STRING\n");
47 fprintf(stderr
, " ADDR := { IP_ADDRESS | any }\n");
48 fprintf(stderr
, " TOS := { NUMBER | inherit }\n");
49 fprintf(stderr
, " TTL := { 1..255 | inherit }\n");
50 fprintf(stderr
, " KEY := { DOTTED_QUAD | NUMBER }\n");
54 static int parse_args(int argc
, char **argv
, int cmd
, struct ip_tunnel_parm
*p
)
57 char medium
[IFNAMSIZ
];
60 memset(p
, 0, sizeof(*p
));
61 memset(&medium
, 0, sizeof(medium
));
66 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
68 p
->iph
.frag_off
= htons(IP_DF
);
71 if (strcmp(*argv
, "mode") == 0) {
73 if (strcmp(*argv
, "ipip") == 0 ||
74 strcmp(*argv
, "ip/ip") == 0) {
75 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_IPIP
) {
76 fprintf(stderr
,"You managed to ask for more than one tunnel mode.\n");
79 p
->iph
.protocol
= IPPROTO_IPIP
;
80 } else if (strcmp(*argv
, "gre") == 0 ||
81 strcmp(*argv
, "gre/ip") == 0) {
82 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_GRE
) {
83 fprintf(stderr
,"You managed to ask for more than one tunnel mode.\n");
86 p
->iph
.protocol
= IPPROTO_GRE
;
87 } else if (strcmp(*argv
, "sit") == 0 ||
88 strcmp(*argv
, "ipv6/ip") == 0) {
89 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_IPV6
) {
90 fprintf(stderr
,"You managed to ask for more than one tunnel mode.\n");
93 p
->iph
.protocol
= IPPROTO_IPV6
;
94 } else if (strcmp(*argv
, "isatap") == 0) {
95 if (p
->iph
.protocol
&& p
->iph
.protocol
!= IPPROTO_IPV6
) {
96 fprintf(stderr
, "You managed to ask for more than one tunnel mode.\n");
99 p
->iph
.protocol
= IPPROTO_IPV6
;
102 fprintf(stderr
,"Cannot guess tunnel mode.\n");
105 } else if (strcmp(*argv
, "key") == 0) {
108 p
->i_flags
|= GRE_KEY
;
109 p
->o_flags
|= GRE_KEY
;
110 if (strchr(*argv
, '.'))
111 p
->i_key
= p
->o_key
= get_addr32(*argv
);
113 if (get_unsigned(&uval
, *argv
, 0)<0) {
114 fprintf(stderr
, "invalid value of \"key\"\n");
117 p
->i_key
= p
->o_key
= htonl(uval
);
119 } else if (strcmp(*argv
, "ikey") == 0) {
122 p
->i_flags
|= GRE_KEY
;
123 if (strchr(*argv
, '.'))
124 p
->i_key
= get_addr32(*argv
);
126 if (get_unsigned(&uval
, *argv
, 0)<0) {
127 fprintf(stderr
, "invalid value of \"ikey\"\n");
130 p
->i_key
= htonl(uval
);
132 } else if (strcmp(*argv
, "okey") == 0) {
135 p
->o_flags
|= GRE_KEY
;
136 if (strchr(*argv
, '.'))
137 p
->o_key
= get_addr32(*argv
);
139 if (get_unsigned(&uval
, *argv
, 0)<0) {
140 fprintf(stderr
, "invalid value of \"okey\"\n");
143 p
->o_key
= htonl(uval
);
145 } else if (strcmp(*argv
, "seq") == 0) {
146 p
->i_flags
|= GRE_SEQ
;
147 p
->o_flags
|= GRE_SEQ
;
148 } else if (strcmp(*argv
, "iseq") == 0) {
149 p
->i_flags
|= GRE_SEQ
;
150 } else if (strcmp(*argv
, "oseq") == 0) {
151 p
->o_flags
|= GRE_SEQ
;
152 } else if (strcmp(*argv
, "csum") == 0) {
153 p
->i_flags
|= GRE_CSUM
;
154 p
->o_flags
|= GRE_CSUM
;
155 } else if (strcmp(*argv
, "icsum") == 0) {
156 p
->i_flags
|= GRE_CSUM
;
157 } else if (strcmp(*argv
, "ocsum") == 0) {
158 p
->o_flags
|= GRE_CSUM
;
159 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
161 } else if (strcmp(*argv
, "pmtudisc") == 0) {
162 p
->iph
.frag_off
= htons(IP_DF
);
163 } else if (strcmp(*argv
, "remote") == 0) {
165 if (strcmp(*argv
, "any"))
166 p
->iph
.daddr
= get_addr32(*argv
);
167 } else if (strcmp(*argv
, "local") == 0) {
169 if (strcmp(*argv
, "any"))
170 p
->iph
.saddr
= get_addr32(*argv
);
171 } else if (strcmp(*argv
, "dev") == 0) {
173 strncpy(medium
, *argv
, IFNAMSIZ
-1);
174 } else if (strcmp(*argv
, "ttl") == 0 ||
175 strcmp(*argv
, "hoplimit") == 0) {
178 if (strcmp(*argv
, "inherit") != 0) {
179 if (get_unsigned(&uval
, *argv
, 0))
180 invarg("invalid TTL\n", *argv
);
182 invarg("TTL must be <=255\n", *argv
);
185 } else if (strcmp(*argv
, "tos") == 0 ||
186 strcmp(*argv
, "tclass") == 0 ||
187 matches(*argv
, "dsfield") == 0) {
190 if (strcmp(*argv
, "inherit") != 0) {
191 if (rtnl_dsfield_a2n(&uval
, *argv
))
192 invarg("bad TOS value", *argv
);
197 if (strcmp(*argv
, "name") == 0) {
199 } else if (matches(*argv
, "help") == 0)
202 duparg2("name", *argv
);
203 strncpy(p
->name
, *argv
, IFNAMSIZ
);
204 if (cmd
== SIOCCHGTUNNEL
&& count
== 0) {
205 struct ip_tunnel_parm old_p
;
206 memset(&old_p
, 0, sizeof(old_p
));
207 if (tnl_get_ioctl(*argv
, &old_p
))
217 if (p
->iph
.protocol
== 0) {
218 if (memcmp(p
->name
, "gre", 3) == 0)
219 p
->iph
.protocol
= IPPROTO_GRE
;
220 else if (memcmp(p
->name
, "ipip", 4) == 0)
221 p
->iph
.protocol
= IPPROTO_IPIP
;
222 else if (memcmp(p
->name
, "sit", 3) == 0)
223 p
->iph
.protocol
= IPPROTO_IPV6
;
224 else if (memcmp(p
->name
, "isatap", 6) == 0) {
225 p
->iph
.protocol
= IPPROTO_IPV6
;
230 if (p
->iph
.protocol
== IPPROTO_IPIP
|| p
->iph
.protocol
== IPPROTO_IPV6
) {
231 if ((p
->i_flags
& GRE_KEY
) || (p
->o_flags
& GRE_KEY
)) {
232 fprintf(stderr
, "Keys are not allowed with ipip and sit.\n");
238 p
->link
= tnl_ioctl_get_ifindex(medium
);
243 if (p
->i_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
244 p
->i_key
= p
->iph
.daddr
;
245 p
->i_flags
|= GRE_KEY
;
247 if (p
->o_key
== 0 && IN_MULTICAST(ntohl(p
->iph
.daddr
))) {
248 p
->o_key
= p
->iph
.daddr
;
249 p
->o_flags
|= GRE_KEY
;
251 if (IN_MULTICAST(ntohl(p
->iph
.daddr
)) && !p
->iph
.saddr
) {
252 fprintf(stderr
, "Broadcast tunnel requires a source address.\n");
257 fprintf(stderr
, "no remote with isatap.\n");
260 p
->i_flags
|= SIT_ISATAP
;
267 static int do_add(int cmd
, int argc
, char **argv
)
269 struct ip_tunnel_parm p
;
271 if (parse_args(argc
, argv
, cmd
, &p
) < 0)
274 if (p
.iph
.ttl
&& p
.iph
.frag_off
== 0) {
275 fprintf(stderr
, "ttl != 0 and noptmudisc are incompatible\n");
279 switch (p
.iph
.protocol
) {
281 return tnl_add_ioctl(cmd
, "tunl0", p
.name
, &p
);
283 return tnl_add_ioctl(cmd
, "gre0", p
.name
, &p
);
285 return tnl_add_ioctl(cmd
, "sit0", p
.name
, &p
);
287 fprintf(stderr
, "cannot determine tunnel mode (ipip, gre or sit)\n");
293 static int do_del(int argc
, char **argv
)
295 struct ip_tunnel_parm p
;
297 if (parse_args(argc
, argv
, SIOCDELTUNNEL
, &p
) < 0)
300 switch (p
.iph
.protocol
) {
302 return tnl_del_ioctl("tunl0", p
.name
, &p
);
304 return tnl_del_ioctl("gre0", p
.name
, &p
);
306 return tnl_del_ioctl("sit0", p
.name
, &p
);
308 return tnl_del_ioctl(p
.name
, p
.name
, &p
);
313 static void print_tunnel(struct ip_tunnel_parm
*p
)
320 inet_ntop(AF_INET
, &p
->i_key
, s3
, sizeof(s3
));
321 inet_ntop(AF_INET
, &p
->o_key
, s4
, sizeof(s4
));
323 /* Do not use format_host() for local addr,
324 * symbolic name will not be useful.
326 printf("%s: %s/ip remote %s local %s ",
328 tnl_strproto(p
->iph
.protocol
),
329 p
->iph
.daddr
? format_host(AF_INET
, 4, &p
->iph
.daddr
, s1
, sizeof(s1
)) : "any",
330 p
->iph
.saddr
? rt_addr_n2a(AF_INET
, 4, &p
->iph
.saddr
, s2
, sizeof(s2
)) : "any");
333 char *n
= tnl_ioctl_get_ifname(p
->link
);
335 printf(" dev %s ", n
);
339 printf(" ttl %d ", p
->iph
.ttl
);
341 printf(" ttl inherit ");
349 printf("%c%s ", p
->iph
.tos
&1 ? '/' : ' ',
350 rtnl_dsfield_n2a(p
->iph
.tos
&~1, b1
, sizeof(b1
)));
353 if (!(p
->iph
.frag_off
&htons(IP_DF
)))
354 printf(" nopmtudisc");
356 if ((p
->i_flags
&GRE_KEY
) && (p
->o_flags
&GRE_KEY
) && p
->o_key
== p
->i_key
)
357 printf(" key %s", s3
);
358 else if ((p
->i_flags
|p
->o_flags
)&GRE_KEY
) {
359 if (p
->i_flags
&GRE_KEY
)
360 printf(" ikey %s ", s3
);
361 if (p
->o_flags
&GRE_KEY
)
362 printf(" okey %s ", s4
);
365 if (p
->i_flags
&GRE_SEQ
)
366 printf("%s Drop packets out of sequence.\n", _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_
);
375 static int do_tunnels_list(struct ip_tunnel_parm
*p
)
378 unsigned long rx_bytes
, rx_packets
, rx_errs
, rx_drops
,
380 tx_bytes
, tx_packets
, tx_errs
, tx_drops
,
381 tx_fifo
, tx_colls
, tx_carrier
, rx_multi
;
383 struct ip_tunnel_parm p1
;
386 FILE *fp
= fopen("/proc/net/dev", "r");
392 fgets(buf
, sizeof(buf
), fp
);
393 fgets(buf
, sizeof(buf
), fp
);
395 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
397 buf
[sizeof(buf
) - 1] = 0;
398 if ((ptr
= strchr(buf
, ':')) == NULL
||
399 (*ptr
++ = 0, sscanf(buf
, "%s", name
) != 1)) {
400 fprintf(stderr
, "Wrong format of /proc/net/dev. Sorry.\n");
403 if (sscanf(ptr
, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
404 &rx_bytes
, &rx_packets
, &rx_errs
, &rx_drops
,
405 &rx_fifo
, &rx_frame
, &rx_multi
,
406 &tx_bytes
, &tx_packets
, &tx_errs
, &tx_drops
,
407 &tx_fifo
, &tx_colls
, &tx_carrier
) != 14)
409 if (p
->name
[0] && strcmp(p
->name
, name
))
411 type
= tnl_ioctl_get_iftype(name
);
413 fprintf(stderr
, "Failed to get type of [%s]\n", name
);
416 if (type
!= ARPHRD_TUNNEL
&& type
!= ARPHRD_IPGRE
&& type
!= ARPHRD_SIT
)
418 memset(&p1
, 0, sizeof(p1
));
419 if (tnl_get_ioctl(name
, &p1
))
421 if ((p
->link
&& p1
.link
!= p
->link
) ||
422 (p
->name
[0] && strcmp(p1
.name
, p
->name
)) ||
423 (p
->iph
.daddr
&& p1
.iph
.daddr
!= p
->iph
.daddr
) ||
424 (p
->iph
.saddr
&& p1
.iph
.saddr
!= p
->iph
.saddr
) ||
425 (p
->i_key
&& p1
.i_key
!= p
->i_key
))
430 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_
);
431 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
432 rx_packets
, rx_bytes
, rx_errs
, rx_frame
, rx_fifo
, rx_multi
, _SL_
);
433 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_
);
434 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
435 tx_packets
, tx_bytes
, tx_errs
, tx_colls
, tx_carrier
, tx_drops
);
442 static int do_show(int argc
, char **argv
)
445 struct ip_tunnel_parm p
;
447 if (parse_args(argc
, argv
, SIOCGETTUNNEL
, &p
) < 0)
450 switch (p
.iph
.protocol
) {
452 err
= tnl_get_ioctl(p
.name
[0] ? p
.name
: "tunl0", &p
);
455 err
= tnl_get_ioctl(p
.name
[0] ? p
.name
: "gre0", &p
);
458 err
= tnl_get_ioctl(p
.name
[0] ? p
.name
: "sit0", &p
);
472 int do_iptunnel(int argc
, char **argv
)
474 switch (preferred_family
) {
476 preferred_family
= AF_INET
;
481 * This is silly enough but we have no easy way to make it
482 * protocol-independent because of unarranged structure between
486 return do_ip6tunnel(argc
, argv
);
488 fprintf(stderr
, "Unsupported family:%d\n", preferred_family
);
493 if (matches(*argv
, "add") == 0)
494 return do_add(SIOCADDTUNNEL
, argc
-1, argv
+1);
495 if (matches(*argv
, "change") == 0)
496 return do_add(SIOCCHGTUNNEL
, argc
-1, argv
+1);
497 if (matches(*argv
, "del") == 0)
498 return do_del(argc
-1, argv
+1);
499 if (matches(*argv
, "show") == 0 ||
500 matches(*argv
, "lst") == 0 ||
501 matches(*argv
, "list") == 0)
502 return do_show(argc
-1, argv
+1);
503 if (matches(*argv
, "help") == 0)
506 return do_show(0, NULL
);
508 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv
);