blob: 1b1458e1ec2602eba9967d765650f7b1835f2e52 [file] [log] [blame]
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00001/*
2 * iptunnel.c "ip tunnel"
3 *
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +00004 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00005 *
6 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
7 *
8 *
9 * Changes:
10 *
11 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
12 * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
13 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
14 */
15
Rob Landleyecae66a2006-06-02 20:53:38 +000016#include "libbb.h"
Glenn L McGrath275be872002-12-16 07:37:21 +000017#include <sys/socket.h>
18#include <sys/ioctl.h>
19
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000020#include <string.h>
21#include <unistd.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000022
Glenn L McGrath275be872002-12-16 07:37:21 +000023#include <netinet/ip.h>
24
Eric Andersenab4e19a2003-01-14 08:54:08 +000025#include <net/if.h>
26#include <net/if_arp.h>
27
28#include <asm/types.h>
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000029#ifndef __constant_htons
Eric Andersenab4e19a2003-01-14 08:54:08 +000030#define __constant_htons htons
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000031#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032#include <linux/if_tunnel.h>
33
34#include "rt_names.h"
35#include "utils.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000036#include "ip_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000037
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038
39static int do_ioctl_get_ifindex(char *dev)
40{
41 struct ifreq ifr;
42 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000043
44 strcpy(ifr.ifr_name, dev);
45 fd = socket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000046 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 bb_perror_msg("ioctl");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000048 return 0;
49 }
50 close(fd);
51 return ifr.ifr_ifindex;
52}
53
54static int do_ioctl_get_iftype(char *dev)
55{
56 struct ifreq ifr;
57 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000058
59 strcpy(ifr.ifr_name, dev);
60 fd = socket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000061 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 bb_perror_msg("ioctl");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063 return -1;
64 }
65 close(fd);
66 return ifr.ifr_addr.sa_family;
67}
68
69
Glenn L McGrath8b602442002-11-28 12:19:51 +000070static char *do_ioctl_get_ifname(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071{
72 static struct ifreq ifr;
73 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000074
75 ifr.ifr_ifindex = idx;
76 fd = socket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000077 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 bb_perror_msg("ioctl");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079 return NULL;
80 }
81 close(fd);
82 return ifr.ifr_name;
83}
84
85
86
87static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
88{
89 struct ifreq ifr;
90 int fd;
91 int err;
92
93 strcpy(ifr.ifr_name, basedev);
94 ifr.ifr_ifru.ifru_data = (void*)p;
95 fd = socket(AF_INET, SOCK_DGRAM, 0);
96 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +000097 if (err) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 bb_perror_msg("ioctl");
Glenn L McGrath8b602442002-11-28 12:19:51 +000099 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000100 close(fd);
101 return err;
102}
103
104static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
105{
106 struct ifreq ifr;
107 int fd;
108 int err;
109
Glenn L McGrath8b602442002-11-28 12:19:51 +0000110 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000111 strcpy(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000112 } else {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000113 strcpy(ifr.ifr_name, basedev);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000114 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115 ifr.ifr_ifru.ifru_data = (void*)p;
116 fd = socket(AF_INET, SOCK_DGRAM, 0);
117 err = ioctl(fd, cmd, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000118 if (err) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 bb_perror_msg("ioctl");
Glenn L McGrath8b602442002-11-28 12:19:51 +0000120 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000121 close(fd);
122 return err;
123}
124
125static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
126{
127 struct ifreq ifr;
128 int fd;
129 int err;
130
Glenn L McGrath8b602442002-11-28 12:19:51 +0000131 if (p->name[0]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000132 strcpy(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000133 } else {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000134 strcpy(ifr.ifr_name, basedev);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000135 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136 ifr.ifr_ifru.ifru_data = (void*)p;
137 fd = socket(AF_INET, SOCK_DGRAM, 0);
138 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000139 if (err) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 bb_perror_msg("ioctl");
Glenn L McGrath8b602442002-11-28 12:19:51 +0000141 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000142 close(fd);
143 return err;
144}
145
146static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
147{
148 int count = 0;
149 char medium[IFNAMSIZ];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000150 memset(p, 0, sizeof(*p));
151 memset(&medium, 0, sizeof(medium));
152
153 p->iph.version = 4;
154 p->iph.ihl = 5;
155#ifndef IP_DF
156#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
157#endif
158 p->iph.frag_off = htons(IP_DF);
159
160 while (argc > 0) {
161 if (strcmp(*argv, "mode") == 0) {
162 NEXT_ARG();
163 if (strcmp(*argv, "ipip") == 0 ||
164 strcmp(*argv, "ip/ip") == 0) {
165 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000166 bb_error_msg("You managed to ask for more than one tunnel mode.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000167 exit(-1);
168 }
169 p->iph.protocol = IPPROTO_IPIP;
170 } else if (strcmp(*argv, "gre") == 0 ||
171 strcmp(*argv, "gre/ip") == 0) {
172 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000173 bb_error_msg("You managed to ask for more than one tunnel mode.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000174 exit(-1);
175 }
176 p->iph.protocol = IPPROTO_GRE;
177 } else if (strcmp(*argv, "sit") == 0 ||
178 strcmp(*argv, "ipv6/ip") == 0) {
179 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 bb_error_msg("You managed to ask for more than one tunnel mode.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000181 exit(-1);
182 }
183 p->iph.protocol = IPPROTO_IPV6;
184 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000185 bb_error_msg("Cannot guess tunnel mode.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000186 exit(-1);
187 }
188 } else if (strcmp(*argv, "key") == 0) {
189 unsigned uval;
190 NEXT_ARG();
191 p->i_flags |= GRE_KEY;
192 p->o_flags |= GRE_KEY;
193 if (strchr(*argv, '.'))
194 p->i_key = p->o_key = get_addr32(*argv);
195 else {
196 if (get_unsigned(&uval, *argv, 0)<0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000197 bb_error_msg("invalid value of \"key\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000198 exit(-1);
199 }
200 p->i_key = p->o_key = htonl(uval);
201 }
202 } else if (strcmp(*argv, "ikey") == 0) {
203 unsigned uval;
204 NEXT_ARG();
205 p->i_flags |= GRE_KEY;
206 if (strchr(*argv, '.'))
207 p->o_key = get_addr32(*argv);
208 else {
209 if (get_unsigned(&uval, *argv, 0)<0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000210 bb_error_msg("invalid value of \"ikey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000211 exit(-1);
212 }
213 p->i_key = htonl(uval);
214 }
215 } else if (strcmp(*argv, "okey") == 0) {
216 unsigned uval;
217 NEXT_ARG();
218 p->o_flags |= GRE_KEY;
219 if (strchr(*argv, '.'))
220 p->o_key = get_addr32(*argv);
221 else {
222 if (get_unsigned(&uval, *argv, 0)<0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000223 bb_error_msg("invalid value of \"okey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000224 exit(-1);
225 }
226 p->o_key = htonl(uval);
227 }
228 } else if (strcmp(*argv, "seq") == 0) {
229 p->i_flags |= GRE_SEQ;
230 p->o_flags |= GRE_SEQ;
231 } else if (strcmp(*argv, "iseq") == 0) {
232 p->i_flags |= GRE_SEQ;
233 } else if (strcmp(*argv, "oseq") == 0) {
234 p->o_flags |= GRE_SEQ;
235 } else if (strcmp(*argv, "csum") == 0) {
236 p->i_flags |= GRE_CSUM;
237 p->o_flags |= GRE_CSUM;
238 } else if (strcmp(*argv, "icsum") == 0) {
239 p->i_flags |= GRE_CSUM;
240 } else if (strcmp(*argv, "ocsum") == 0) {
241 p->o_flags |= GRE_CSUM;
242 } else if (strcmp(*argv, "nopmtudisc") == 0) {
243 p->iph.frag_off = 0;
244 } else if (strcmp(*argv, "pmtudisc") == 0) {
245 p->iph.frag_off = htons(IP_DF);
246 } else if (strcmp(*argv, "remote") == 0) {
247 NEXT_ARG();
248 if (strcmp(*argv, "any"))
249 p->iph.daddr = get_addr32(*argv);
250 } else if (strcmp(*argv, "local") == 0) {
251 NEXT_ARG();
252 if (strcmp(*argv, "any"))
253 p->iph.saddr = get_addr32(*argv);
254 } else if (strcmp(*argv, "dev") == 0) {
255 NEXT_ARG();
256 strncpy(medium, *argv, IFNAMSIZ-1);
257 } else if (strcmp(*argv, "ttl") == 0) {
258 unsigned uval;
259 NEXT_ARG();
260 if (strcmp(*argv, "inherit") != 0) {
261 if (get_unsigned(&uval, *argv, 0))
262 invarg("invalid TTL\n", *argv);
263 if (uval > 255)
264 invarg("TTL must be <=255\n", *argv);
265 p->iph.ttl = uval;
266 }
267 } else if (strcmp(*argv, "tos") == 0 ||
268 matches(*argv, "dsfield") == 0) {
269 __u32 uval;
270 NEXT_ARG();
271 if (strcmp(*argv, "inherit") != 0) {
272 if (rtnl_dsfield_a2n(&uval, *argv))
273 invarg("bad TOS value", *argv);
274 p->iph.tos = uval;
275 } else
276 p->iph.tos = 1;
277 } else {
278 if (strcmp(*argv, "name") == 0) {
279 NEXT_ARG();
280 }
281 if (p->name[0])
282 duparg2("name", *argv);
283 strncpy(p->name, *argv, IFNAMSIZ);
284 if (cmd == SIOCCHGTUNNEL && count == 0) {
285 struct ip_tunnel_parm old_p;
286 memset(&old_p, 0, sizeof(old_p));
287 if (do_get_ioctl(*argv, &old_p))
288 return -1;
289 *p = old_p;
290 }
291 }
292 count++;
293 argc--; argv++;
294 }
295
296
297 if (p->iph.protocol == 0) {
298 if (memcmp(p->name, "gre", 3) == 0)
299 p->iph.protocol = IPPROTO_GRE;
300 else if (memcmp(p->name, "ipip", 4) == 0)
301 p->iph.protocol = IPPROTO_IPIP;
302 else if (memcmp(p->name, "sit", 3) == 0)
303 p->iph.protocol = IPPROTO_IPV6;
304 }
305
306 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
307 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000308 bb_error_msg("Keys are not allowed with ipip and sit.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000309 return -1;
310 }
311 }
312
313 if (medium[0]) {
314 p->link = do_ioctl_get_ifindex(medium);
315 if (p->link == 0)
316 return -1;
317 }
318
319 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
320 p->i_key = p->iph.daddr;
321 p->i_flags |= GRE_KEY;
322 }
323 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
324 p->o_key = p->iph.daddr;
325 p->o_flags |= GRE_KEY;
326 }
327 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000328 bb_error_msg("Broadcast tunnel requires a source address.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000329 return -1;
330 }
331 return 0;
332}
333
334
335static int do_add(int cmd, int argc, char **argv)
336{
337 struct ip_tunnel_parm p;
338
339 if (parse_args(argc, argv, cmd, &p) < 0)
340 return -1;
341
342 if (p.iph.ttl && p.iph.frag_off == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000343 bb_error_msg("ttl != 0 and noptmudisc are incompatible");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000344 return -1;
345 }
346
347 switch (p.iph.protocol) {
348 case IPPROTO_IPIP:
349 return do_add_ioctl(cmd, "tunl0", &p);
350 case IPPROTO_GRE:
351 return do_add_ioctl(cmd, "gre0", &p);
352 case IPPROTO_IPV6:
353 return do_add_ioctl(cmd, "sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000354 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000355 bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000356 return -1;
357 }
358 return -1;
359}
360
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000361static int do_del(int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000362{
363 struct ip_tunnel_parm p;
364
365 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
366 return -1;
367
368 switch (p.iph.protocol) {
369 case IPPROTO_IPIP:
370 return do_del_ioctl("tunl0", &p);
371 case IPPROTO_GRE:
372 return do_del_ioctl("gre0", &p);
373 case IPPROTO_IPV6:
374 return do_del_ioctl("sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000375 default:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000376 return do_del_ioctl(p.name, &p);
377 }
378 return -1;
379}
380
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000381static void print_tunnel(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000382{
383 char s1[256];
384 char s2[256];
385 char s3[64];
386 char s4[64];
387
388 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
389 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
390 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
391 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
392
393 printf("%s: %s/ip remote %s local %s ",
394 p->name,
395 p->iph.protocol == IPPROTO_IPIP ? "ip" :
396 (p->iph.protocol == IPPROTO_GRE ? "gre" :
397 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
398 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
399 if (p->link) {
400 char *n = do_ioctl_get_ifname(p->link);
401 if (n)
402 printf(" dev %s ", n);
403 }
404 if (p->iph.ttl)
405 printf(" ttl %d ", p->iph.ttl);
406 else
407 printf(" ttl inherit ");
408 if (p->iph.tos) {
409 SPRINT_BUF(b1);
410 printf(" tos");
411 if (p->iph.tos&1)
412 printf(" inherit");
413 if (p->iph.tos&~1)
414 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
415 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
416 }
417 if (!(p->iph.frag_off&htons(IP_DF)))
418 printf(" nopmtudisc");
419
420 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
421 printf(" key %s", s3);
422 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
423 if (p->i_flags&GRE_KEY)
424 printf(" ikey %s ", s3);
425 if (p->o_flags&GRE_KEY)
426 printf(" okey %s ", s4);
427 }
428
429 if (p->i_flags&GRE_SEQ)
430 printf("%s Drop packets out of sequence.\n", _SL_);
431 if (p->i_flags&GRE_CSUM)
432 printf("%s Checksum in received packet is required.", _SL_);
433 if (p->o_flags&GRE_SEQ)
434 printf("%s Sequence packets on output.", _SL_);
435 if (p->o_flags&GRE_CSUM)
436 printf("%s Checksum output packets.", _SL_);
437}
438
439static int do_tunnels_list(struct ip_tunnel_parm *p)
440{
441 char name[IFNAMSIZ];
442 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
443 rx_fifo, rx_frame,
444 tx_bytes, tx_packets, tx_errs, tx_drops,
445 tx_fifo, tx_colls, tx_carrier, rx_multi;
446 int type;
447 struct ip_tunnel_parm p1;
448
449 char buf[512];
450 FILE *fp = fopen("/proc/net/dev", "r");
451 if (fp == NULL) {
452 perror("fopen");
453 return -1;
454 }
455
456 fgets(buf, sizeof(buf), fp);
457 fgets(buf, sizeof(buf), fp);
458
459 while (fgets(buf, sizeof(buf), fp) != NULL) {
460 char *ptr;
461 buf[sizeof(buf) - 1] = 0;
462 if ((ptr = strchr(buf, ':')) == NULL ||
463 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000464 bb_error_msg("Wrong format of /proc/net/dev. Sorry.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000465 return -1;
466 }
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +0000467 if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000468 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
469 &rx_fifo, &rx_frame, &rx_multi,
470 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
471 &tx_fifo, &tx_colls, &tx_carrier) != 14)
472 continue;
473 if (p->name[0] && strcmp(p->name, name))
474 continue;
475 type = do_ioctl_get_iftype(name);
476 if (type == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000477 bb_error_msg("Failed to get type of [%s]", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000478 continue;
479 }
480 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
481 continue;
482 memset(&p1, 0, sizeof(p1));
483 if (do_get_ioctl(name, &p1))
484 continue;
485 if ((p->link && p1.link != p->link) ||
486 (p->name[0] && strcmp(p1.name, p->name)) ||
487 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
488 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
489 (p->i_key && p1.i_key != p->i_key))
490 continue;
491 print_tunnel(&p1);
492 printf("\n");
493 }
494 return 0;
495}
496
497static int do_show(int argc, char **argv)
498{
499 int err;
500 struct ip_tunnel_parm p;
501
502 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
503 return -1;
504
505 switch (p.iph.protocol) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000506 case IPPROTO_IPIP:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000507 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
508 break;
509 case IPPROTO_GRE:
510 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
511 break;
512 case IPPROTO_IPV6:
513 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
514 break;
515 default:
516 do_tunnels_list(&p);
517 return 0;
518 }
519 if (err)
520 return -1;
521
522 print_tunnel(&p);
523 printf("\n");
524 return 0;
525}
526
527int do_iptunnel(int argc, char **argv)
528{
529 if (argc > 0) {
530 if (matches(*argv, "add") == 0)
531 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
532 if (matches(*argv, "change") == 0)
533 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
534 if (matches(*argv, "del") == 0)
535 return do_del(argc-1, argv+1);
536 if (matches(*argv, "show") == 0 ||
537 matches(*argv, "lst") == 0 ||
538 matches(*argv, "list") == 0)
539 return do_show(argc-1, argv+1);
540 } else
541 return do_show(0, NULL);
542
Eric Andersenf71ad6c2004-04-26 19:32:49 +0000543 bb_error_msg("Command \"%s\" is unknown.", *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000544 exit(-1);
545}