blob: 3d41a274cf196bb0d93bd3ece5b3790931be2d9b [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
2 * iptunnel.c "ip tunnel"
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000011 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090017#include <sys/types.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000018#include <sys/socket.h>
osdl.org!shemminger7272ddc2004-06-02 20:22:08 +000019#include <arpa/inet.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000020#include <sys/ioctl.h>
Stephen Hemmingerea71bea2010-11-28 10:35:28 -080021#include <net/if.h>
22#include <net/if_arp.h>
osdl.org!shemminger7272ddc2004-06-02 20:22:08 +000023#include <linux/ip.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000024#include <linux/if_tunnel.h>
25
26#include "rt_names.h"
27#include "utils.h"
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090028#include "ip_common.h"
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090029#include "tunnel.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000030
31static void usage(void) __attribute__((noreturn));
32
33static void usage(void)
34{
Alexandre Cassenb88215c2009-12-16 02:38:29 +000035 fprintf(stderr, "Usage: ip tunnel { add | change | del | show | prl | 6rd } [ NAME ]\n");
Templin, Fred L0bd17922007-11-26 09:46:06 -080036 fprintf(stderr, " [ mode { ipip | gre | sit | isatap } ] [ remote ADDR ] [ local ADDR ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000037 fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
Sascha Hlusiaka07e9912009-05-04 01:44:47 +020038 fprintf(stderr, " [ prl-default ADDR ] [ prl-nodefault ADDR ] [ prl-delete ADDR ]\n");
Alexandre Cassenb88215c2009-12-16 02:38:29 +000039 fprintf(stderr, " [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000040 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 := { NUMBER | inherit }\n");
45 fprintf(stderr, " TTL := { 1..255 | inherit }\n");
46 fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n");
47 exit(-1);
48}
49
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000050static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
51{
52 int count = 0;
53 char medium[IFNAMSIZ];
Templin, Fred L0bd17922007-11-26 09:46:06 -080054 int isatap = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000055
56 memset(p, 0, sizeof(*p));
57 memset(&medium, 0, sizeof(medium));
58
59 p->iph.version = 4;
60 p->iph.ihl = 5;
61#ifndef IP_DF
62#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
63#endif
64 p->iph.frag_off = htons(IP_DF);
65
66 while (argc > 0) {
67 if (strcmp(*argv, "mode") == 0) {
68 NEXT_ARG();
69 if (strcmp(*argv, "ipip") == 0 ||
70 strcmp(*argv, "ip/ip") == 0) {
71 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
72 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
73 exit(-1);
74 }
75 p->iph.protocol = IPPROTO_IPIP;
76 } else if (strcmp(*argv, "gre") == 0 ||
77 strcmp(*argv, "gre/ip") == 0) {
78 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
79 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
80 exit(-1);
81 }
82 p->iph.protocol = IPPROTO_GRE;
83 } else if (strcmp(*argv, "sit") == 0 ||
84 strcmp(*argv, "ipv6/ip") == 0) {
85 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
86 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
87 exit(-1);
88 }
89 p->iph.protocol = IPPROTO_IPV6;
Templin, Fred L0bd17922007-11-26 09:46:06 -080090 } else if (strcmp(*argv, "isatap") == 0) {
91 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
92 fprintf(stderr, "You managed to ask for more than one tunnel mode.\n");
93 exit(-1);
94 }
95 p->iph.protocol = IPPROTO_IPV6;
96 isatap++;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000097 } else {
98 fprintf(stderr,"Cannot guess tunnel mode.\n");
99 exit(-1);
100 }
101 } else if (strcmp(*argv, "key") == 0) {
102 unsigned uval;
103 NEXT_ARG();
104 p->i_flags |= GRE_KEY;
105 p->o_flags |= GRE_KEY;
106 if (strchr(*argv, '.'))
107 p->i_key = p->o_key = get_addr32(*argv);
108 else {
109 if (get_unsigned(&uval, *argv, 0)<0) {
110 fprintf(stderr, "invalid value of \"key\"\n");
111 exit(-1);
112 }
113 p->i_key = p->o_key = htonl(uval);
114 }
115 } else if (strcmp(*argv, "ikey") == 0) {
116 unsigned uval;
117 NEXT_ARG();
118 p->i_flags |= GRE_KEY;
119 if (strchr(*argv, '.'))
Herbert Xu4282c6c2007-10-12 10:56:40 +0200120 p->i_key = get_addr32(*argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000121 else {
122 if (get_unsigned(&uval, *argv, 0)<0) {
123 fprintf(stderr, "invalid value of \"ikey\"\n");
124 exit(-1);
125 }
126 p->i_key = htonl(uval);
127 }
128 } else if (strcmp(*argv, "okey") == 0) {
129 unsigned uval;
130 NEXT_ARG();
131 p->o_flags |= GRE_KEY;
132 if (strchr(*argv, '.'))
133 p->o_key = get_addr32(*argv);
134 else {
135 if (get_unsigned(&uval, *argv, 0)<0) {
136 fprintf(stderr, "invalid value of \"okey\"\n");
137 exit(-1);
138 }
139 p->o_key = htonl(uval);
140 }
141 } else if (strcmp(*argv, "seq") == 0) {
142 p->i_flags |= GRE_SEQ;
143 p->o_flags |= GRE_SEQ;
144 } else if (strcmp(*argv, "iseq") == 0) {
145 p->i_flags |= GRE_SEQ;
146 } else if (strcmp(*argv, "oseq") == 0) {
147 p->o_flags |= GRE_SEQ;
148 } else if (strcmp(*argv, "csum") == 0) {
149 p->i_flags |= GRE_CSUM;
150 p->o_flags |= GRE_CSUM;
151 } else if (strcmp(*argv, "icsum") == 0) {
152 p->i_flags |= GRE_CSUM;
153 } else if (strcmp(*argv, "ocsum") == 0) {
154 p->o_flags |= GRE_CSUM;
155 } else if (strcmp(*argv, "nopmtudisc") == 0) {
156 p->iph.frag_off = 0;
157 } else if (strcmp(*argv, "pmtudisc") == 0) {
158 p->iph.frag_off = htons(IP_DF);
159 } else if (strcmp(*argv, "remote") == 0) {
160 NEXT_ARG();
161 if (strcmp(*argv, "any"))
162 p->iph.daddr = get_addr32(*argv);
163 } else if (strcmp(*argv, "local") == 0) {
164 NEXT_ARG();
165 if (strcmp(*argv, "any"))
166 p->iph.saddr = get_addr32(*argv);
167 } else if (strcmp(*argv, "dev") == 0) {
168 NEXT_ARG();
169 strncpy(medium, *argv, IFNAMSIZ-1);
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400170 } else if (strcmp(*argv, "ttl") == 0 ||
171 strcmp(*argv, "hoplimit") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000172 unsigned uval;
173 NEXT_ARG();
174 if (strcmp(*argv, "inherit") != 0) {
175 if (get_unsigned(&uval, *argv, 0))
176 invarg("invalid TTL\n", *argv);
177 if (uval > 255)
178 invarg("TTL must be <=255\n", *argv);
179 p->iph.ttl = uval;
180 }
181 } else if (strcmp(*argv, "tos") == 0 ||
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400182 strcmp(*argv, "tclass") == 0 ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000183 matches(*argv, "dsfield") == 0) {
184 __u32 uval;
185 NEXT_ARG();
186 if (strcmp(*argv, "inherit") != 0) {
187 if (rtnl_dsfield_a2n(&uval, *argv))
188 invarg("bad TOS value", *argv);
189 p->iph.tos = uval;
190 } else
191 p->iph.tos = 1;
192 } else {
193 if (strcmp(*argv, "name") == 0) {
194 NEXT_ARG();
Andreas Henriksson1f1ae522008-09-18 10:55:03 -0700195 } else if (matches(*argv, "help") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000196 usage();
197 if (p->name[0])
198 duparg2("name", *argv);
199 strncpy(p->name, *argv, IFNAMSIZ);
200 if (cmd == SIOCCHGTUNNEL && count == 0) {
201 struct ip_tunnel_parm old_p;
202 memset(&old_p, 0, sizeof(old_p));
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900203 if (tnl_get_ioctl(*argv, &old_p))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000204 return -1;
205 *p = old_p;
206 }
207 }
208 count++;
209 argc--; argv++;
210 }
211
212
213 if (p->iph.protocol == 0) {
214 if (memcmp(p->name, "gre", 3) == 0)
215 p->iph.protocol = IPPROTO_GRE;
216 else if (memcmp(p->name, "ipip", 4) == 0)
217 p->iph.protocol = IPPROTO_IPIP;
218 else if (memcmp(p->name, "sit", 3) == 0)
219 p->iph.protocol = IPPROTO_IPV6;
Templin, Fred L0bd17922007-11-26 09:46:06 -0800220 else if (memcmp(p->name, "isatap", 6) == 0) {
221 p->iph.protocol = IPPROTO_IPV6;
222 isatap++;
223 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000224 }
225
226 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
227 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
228 fprintf(stderr, "Keys are not allowed with ipip and sit.\n");
229 return -1;
230 }
231 }
232
233 if (medium[0]) {
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800234 p->link = if_nametoindex(medium);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000235 if (p->link == 0)
236 return -1;
237 }
238
239 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
240 p->i_key = p->iph.daddr;
241 p->i_flags |= GRE_KEY;
242 }
243 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
244 p->o_key = p->iph.daddr;
245 p->o_flags |= GRE_KEY;
246 }
247 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
248 fprintf(stderr, "Broadcast tunnel requires a source address.\n");
249 return -1;
250 }
Sascha Hlusiakeeef12c2009-03-27 11:14:00 -0700251 if (isatap)
Templin, Fred L0bd17922007-11-26 09:46:06 -0800252 p->i_flags |= SIT_ISATAP;
Templin, Fred L0bd17922007-11-26 09:46:06 -0800253
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000254 return 0;
255}
256
257
258static int do_add(int cmd, int argc, char **argv)
259{
260 struct ip_tunnel_parm p;
261
262 if (parse_args(argc, argv, cmd, &p) < 0)
263 return -1;
264
265 if (p.iph.ttl && p.iph.frag_off == 0) {
266 fprintf(stderr, "ttl != 0 and noptmudisc are incompatible\n");
267 return -1;
268 }
269
270 switch (p.iph.protocol) {
271 case IPPROTO_IPIP:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900272 return tnl_add_ioctl(cmd, "tunl0", p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000273 case IPPROTO_GRE:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900274 return tnl_add_ioctl(cmd, "gre0", p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000275 case IPPROTO_IPV6:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900276 return tnl_add_ioctl(cmd, "sit0", p.name, &p);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800277 default:
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000278 fprintf(stderr, "cannot determine tunnel mode (ipip, gre or sit)\n");
279 return -1;
280 }
281 return -1;
282}
283
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900284static int do_del(int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000285{
286 struct ip_tunnel_parm p;
287
288 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
289 return -1;
290
291 switch (p.iph.protocol) {
292 case IPPROTO_IPIP:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900293 return tnl_del_ioctl("tunl0", p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000294 case IPPROTO_GRE:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900295 return tnl_del_ioctl("gre0", p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000296 case IPPROTO_IPV6:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900297 return tnl_del_ioctl("sit0", p.name, &p);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800298 default:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900299 return tnl_del_ioctl(p.name, p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000300 }
301 return -1;
302}
303
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900304static void print_tunnel(struct ip_tunnel_parm *p)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000305{
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000306 struct ip_tunnel_6rd ip6rd;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000307 char s1[1024];
308 char s2[1024];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000309
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000310 memset(&ip6rd, 0, sizeof(ip6rd));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000311
312 /* Do not use format_host() for local addr,
313 * symbolic name will not be useful.
314 */
315 printf("%s: %s/ip remote %s local %s ",
316 p->name,
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900317 tnl_strproto(p->iph.protocol),
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000318 p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any",
319 p->iph.saddr ? rt_addr_n2a(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any");
320
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200321 if (p->i_flags & SIT_ISATAP) {
322 struct ip_tunnel_prl prl[16];
323 int i;
324
325 memset(prl, 0, sizeof(prl));
326 prl[0].datalen = sizeof(prl) - sizeof(prl[0]);
327 prl[0].addr = htonl(INADDR_ANY);
328
329 if (!tnl_prl_ioctl(SIOCGETPRL, p->name, prl))
330 for (i = 1; i < sizeof(prl) / sizeof(prl[0]); i++)
331 {
332 if (prl[i].addr != htonl(INADDR_ANY)) {
333 printf(" %s %s ",
334 (prl[i].flags & PRL_DEFAULT) ? "pdr" : "pr",
335 format_host(AF_INET, 4, &prl[i].addr, s1, sizeof(s1)));
336 }
337 }
338 }
339
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000340 if (p->link) {
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800341 const char *n = ll_index_to_name(p->link);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000342 if (n)
343 printf(" dev %s ", n);
344 }
345
346 if (p->iph.ttl)
347 printf(" ttl %d ", p->iph.ttl);
348 else
349 printf(" ttl inherit ");
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800350
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000351 if (p->iph.tos) {
352 SPRINT_BUF(b1);
353 printf(" tos");
354 if (p->iph.tos&1)
355 printf(" inherit");
356 if (p->iph.tos&~1)
357 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
358 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
359 }
360
361 if (!(p->iph.frag_off&htons(IP_DF)))
362 printf(" nopmtudisc");
363
Alexandre Cassen3979ef92010-04-04 22:40:14 +0200364 if (p->iph.protocol == IPPROTO_IPV6 && !tnl_ioctl_get_6rd(p->name, &ip6rd) && ip6rd.prefixlen) {
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000365 printf(" 6rd-prefix %s/%u ",
366 inet_ntop(AF_INET6, &ip6rd.prefix, s1, sizeof(s1)),
367 ip6rd.prefixlen);
368 if (ip6rd.relay_prefix) {
369 printf("6rd-relay_prefix %s/%u ",
370 format_host(AF_INET, 4, &ip6rd.relay_prefix, s1, sizeof(s1)),
371 ip6rd.relay_prefixlen);
372 }
373 }
374
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000375 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
Timo Teräs6f4f7c42010-11-23 22:18:57 +0000376 printf(" key %u", ntohl(p->i_key));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000377 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
378 if (p->i_flags&GRE_KEY)
Timo Teräs6f4f7c42010-11-23 22:18:57 +0000379 printf(" ikey %u ", ntohl(p->i_key));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000380 if (p->o_flags&GRE_KEY)
Timo Teräs6f4f7c42010-11-23 22:18:57 +0000381 printf(" okey %u ", ntohl(p->o_key));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000382 }
383
384 if (p->i_flags&GRE_SEQ)
385 printf("%s Drop packets out of sequence.\n", _SL_);
386 if (p->i_flags&GRE_CSUM)
387 printf("%s Checksum in received packet is required.", _SL_);
388 if (p->o_flags&GRE_SEQ)
389 printf("%s Sequence packets on output.", _SL_);
390 if (p->o_flags&GRE_CSUM)
391 printf("%s Checksum output packets.", _SL_);
392}
393
394static int do_tunnels_list(struct ip_tunnel_parm *p)
395{
396 char name[IFNAMSIZ];
397 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
398 rx_fifo, rx_frame,
399 tx_bytes, tx_packets, tx_errs, tx_drops,
400 tx_fifo, tx_colls, tx_carrier, rx_multi;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000401 struct ip_tunnel_parm p1;
402
403 char buf[512];
404 FILE *fp = fopen("/proc/net/dev", "r");
405 if (fp == NULL) {
406 perror("fopen");
407 return -1;
408 }
409
Stephen Hemminger38c867d2011-03-09 10:41:09 -0800410 /* skip header lines */
411 if (!fgets(buf, sizeof(buf), fp) ||
412 !fgets(buf, sizeof(buf), fp)) {
413 fprintf(stderr, "/proc/net/dev read error\n");
Thomas Jarosch297452a2011-10-03 05:24:32 +0000414 fclose(fp);
Stephen Hemminger38c867d2011-03-09 10:41:09 -0800415 return -1;
416 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000417
418 while (fgets(buf, sizeof(buf), fp) != NULL) {
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800419 int index, type;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000420 char *ptr;
421 buf[sizeof(buf) - 1] = 0;
422 if ((ptr = strchr(buf, ':')) == NULL ||
423 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
424 fprintf(stderr, "Wrong format of /proc/net/dev. Sorry.\n");
Thomas Jarosch297452a2011-10-03 05:24:32 +0000425 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000426 return -1;
427 }
428 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
429 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
430 &rx_fifo, &rx_frame, &rx_multi,
431 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
432 &tx_fifo, &tx_colls, &tx_carrier) != 14)
433 continue;
434 if (p->name[0] && strcmp(p->name, name))
435 continue;
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800436 index = ll_name_to_index(name);
437 if (index == 0)
438 continue;
439 type = ll_index_to_type(index);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000440 if (type == -1) {
441 fprintf(stderr, "Failed to get type of [%s]\n", name);
442 continue;
443 }
444 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
445 continue;
446 memset(&p1, 0, sizeof(p1));
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900447 if (tnl_get_ioctl(name, &p1))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000448 continue;
449 if ((p->link && p1.link != p->link) ||
450 (p->name[0] && strcmp(p1.name, p->name)) ||
451 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
452 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
453 (p->i_key && p1.i_key != p->i_key))
454 continue;
455 print_tunnel(&p1);
456 if (show_stats) {
457 printf("%s", _SL_);
458 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
459 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
460 rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_);
461 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_);
462 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
463 tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
464 }
465 printf("\n");
466 }
Thomas Jarosch297452a2011-10-03 05:24:32 +0000467 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000468 return 0;
469}
470
471static int do_show(int argc, char **argv)
472{
473 int err;
474 struct ip_tunnel_parm p;
475
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800476 ll_init_map(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000477 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
478 return -1;
479
480 switch (p.iph.protocol) {
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800481 case IPPROTO_IPIP:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900482 err = tnl_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000483 break;
484 case IPPROTO_GRE:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900485 err = tnl_get_ioctl(p.name[0] ? p.name : "gre0", &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000486 break;
487 case IPPROTO_IPV6:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900488 err = tnl_get_ioctl(p.name[0] ? p.name : "sit0", &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000489 break;
490 default:
491 do_tunnels_list(&p);
492 return 0;
493 }
494 if (err)
495 return -1;
496
497 print_tunnel(&p);
498 printf("\n");
499 return 0;
500}
501
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200502static int do_prl(int argc, char **argv)
503{
504 struct ip_tunnel_prl p;
505 int count = 0;
506 int devname = 0;
507 int cmd = 0;
508 char medium[IFNAMSIZ];
509
510 memset(&p, 0, sizeof(p));
511 memset(&medium, 0, sizeof(medium));
512
513 while (argc > 0) {
514 if (strcmp(*argv, "prl-default") == 0) {
515 NEXT_ARG();
516 cmd = SIOCADDPRL;
517 p.addr = get_addr32(*argv);
518 p.flags |= PRL_DEFAULT;
519 count++;
520 } else if (strcmp(*argv, "prl-nodefault") == 0) {
521 NEXT_ARG();
522 cmd = SIOCADDPRL;
523 p.addr = get_addr32(*argv);
524 count++;
525 } else if (strcmp(*argv, "prl-delete") == 0) {
526 NEXT_ARG();
527 cmd = SIOCDELPRL;
528 p.addr = get_addr32(*argv);
529 count++;
530 } else if (strcmp(*argv, "dev") == 0) {
531 NEXT_ARG();
532 strncpy(medium, *argv, IFNAMSIZ-1);
533 devname++;
534 } else {
535 fprintf(stderr,"%s: Invalid PRL parameter.\n", *argv);
536 exit(-1);
537 }
538 if (count > 1) {
539 fprintf(stderr,"One PRL entry at a time.\n");
540 exit(-1);
541 }
542 argc--; argv++;
543 }
544 if (devname == 0) {
545 fprintf(stderr, "Must specify dev.\n");
546 exit(-1);
547 }
548
549 return tnl_prl_ioctl(cmd, medium, &p);
550}
551
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000552static int do_6rd(int argc, char **argv)
553{
554 struct ip_tunnel_6rd ip6rd;
555 int devname = 0;
556 int cmd = 0;
557 char medium[IFNAMSIZ];
558 inet_prefix prefix;
559
560 memset(&ip6rd, 0, sizeof(ip6rd));
561 memset(&medium, 0, sizeof(medium));
562
563 while (argc > 0) {
564 if (strcmp(*argv, "6rd-prefix") == 0) {
565 NEXT_ARG();
566 if (get_prefix(&prefix, *argv, AF_INET6))
567 invarg("invalid 6rd_prefix\n", *argv);
568 cmd = SIOCADD6RD;
569 memcpy(&ip6rd.prefix, prefix.data, 16);
570 ip6rd.prefixlen = prefix.bitlen;
571 } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
572 NEXT_ARG();
573 if (get_prefix(&prefix, *argv, AF_INET))
574 invarg("invalid 6rd-relay_prefix\n", *argv);
575 cmd = SIOCADD6RD;
576 memcpy(&ip6rd.relay_prefix, prefix.data, 4);
577 ip6rd.relay_prefixlen = prefix.bitlen;
578 } else if (strcmp(*argv, "6rd-reset") == 0) {
579 cmd = SIOCDEL6RD;
580 } else if (strcmp(*argv, "dev") == 0) {
581 NEXT_ARG();
582 strncpy(medium, *argv, IFNAMSIZ-1);
583 devname++;
584 } else {
585 fprintf(stderr,"%s: Invalid 6RD parameter.\n", *argv);
586 exit(-1);
587 }
588 argc--; argv++;
589 }
590 if (devname == 0) {
591 fprintf(stderr, "Must specify dev.\n");
592 exit(-1);
593 }
594
595 return tnl_6rd_ioctl(cmd, medium, &ip6rd);
596}
597
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000598int do_iptunnel(int argc, char **argv)
599{
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900600 switch (preferred_family) {
601 case AF_UNSPEC:
602 preferred_family = AF_INET;
603 break;
604 case AF_INET:
605 break;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900606 /*
607 * This is silly enough but we have no easy way to make it
608 * protocol-independent because of unarranged structure between
609 * IPv4 and IPv6.
610 */
611 case AF_INET6:
612 return do_ip6tunnel(argc, argv);
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900613 default:
614 fprintf(stderr, "Unsupported family:%d\n", preferred_family);
615 exit(-1);
616 }
617
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000618 if (argc > 0) {
619 if (matches(*argv, "add") == 0)
620 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
621 if (matches(*argv, "change") == 0)
622 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
623 if (matches(*argv, "del") == 0)
624 return do_del(argc-1, argv+1);
625 if (matches(*argv, "show") == 0 ||
626 matches(*argv, "lst") == 0 ||
627 matches(*argv, "list") == 0)
628 return do_show(argc-1, argv+1);
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200629 if (matches(*argv, "prl") == 0)
630 return do_prl(argc-1, argv+1);
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000631 if (matches(*argv, "6rd") == 0)
632 return do_6rd(argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000633 if (matches(*argv, "help") == 0)
634 usage();
635 } else
636 return do_show(0, NULL);
637
638 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv);
639 exit(-1);
640}