blob: 23b5d50137f71253bdf7f2a092a6cf2b36af3d21 [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
Glenn L McGrath275be872002-12-16 07:37:21 +000016#include <sys/socket.h>
17#include <sys/ioctl.h>
18
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000019#include <stdlib.h>
20#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 <arpa/inet.h>
24#include <netinet/ip.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000025#include <netinet/in.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000026
Eric Andersenab4e19a2003-01-14 08:54:08 +000027#include <net/if.h>
28#include <net/if_arp.h>
29
30#include <asm/types.h>
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000031#ifndef __constant_htons
Eric Andersenab4e19a2003-01-14 08:54:08 +000032#define __constant_htons htons
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000033#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000034#include <linux/if_tunnel.h>
35
36#include "rt_names.h"
37#include "utils.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000038#include "ip_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000039
Glenn L McGrath275be872002-12-16 07:37:21 +000040#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000041
42static int do_ioctl_get_ifindex(char *dev)
43{
44 struct ifreq ifr;
45 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000046
47 strcpy(ifr.ifr_name, dev);
48 fd = socket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000049 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 bb_perror_msg("ioctl");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000051 return 0;
52 }
53 close(fd);
54 return ifr.ifr_ifindex;
55}
56
57static int do_ioctl_get_iftype(char *dev)
58{
59 struct ifreq ifr;
60 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000061
62 strcpy(ifr.ifr_name, dev);
63 fd = socket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000064 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 bb_perror_msg("ioctl");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066 return -1;
67 }
68 close(fd);
69 return ifr.ifr_addr.sa_family;
70}
71
72
Glenn L McGrath8b602442002-11-28 12:19:51 +000073static char *do_ioctl_get_ifname(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000074{
75 static struct ifreq ifr;
76 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000077
78 ifr.ifr_ifindex = idx;
79 fd = socket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000080 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 bb_perror_msg("ioctl");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000082 return NULL;
83 }
84 close(fd);
85 return ifr.ifr_name;
86}
87
88
89
90static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
91{
92 struct ifreq ifr;
93 int fd;
94 int err;
95
96 strcpy(ifr.ifr_name, basedev);
97 ifr.ifr_ifru.ifru_data = (void*)p;
98 fd = socket(AF_INET, SOCK_DGRAM, 0);
99 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000100 if (err) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 bb_perror_msg("ioctl");
Glenn L McGrath8b602442002-11-28 12:19:51 +0000102 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000103 close(fd);
104 return err;
105}
106
107static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
108{
109 struct ifreq ifr;
110 int fd;
111 int err;
112
Glenn L McGrath8b602442002-11-28 12:19:51 +0000113 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000114 strcpy(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000115 } else {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000116 strcpy(ifr.ifr_name, basedev);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000117 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000118 ifr.ifr_ifru.ifru_data = (void*)p;
119 fd = socket(AF_INET, SOCK_DGRAM, 0);
120 err = ioctl(fd, cmd, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000121 if (err) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 bb_perror_msg("ioctl");
Glenn L McGrath8b602442002-11-28 12:19:51 +0000123 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000124 close(fd);
125 return err;
126}
127
128static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
129{
130 struct ifreq ifr;
131 int fd;
132 int err;
133
Glenn L McGrath8b602442002-11-28 12:19:51 +0000134 if (p->name[0]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000135 strcpy(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000136 } else {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000137 strcpy(ifr.ifr_name, basedev);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000138 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000139 ifr.ifr_ifru.ifru_data = (void*)p;
140 fd = socket(AF_INET, SOCK_DGRAM, 0);
141 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000142 if (err) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 bb_perror_msg("ioctl");
Glenn L McGrath8b602442002-11-28 12:19:51 +0000144 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000145 close(fd);
146 return err;
147}
148
149static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
150{
151 int count = 0;
152 char medium[IFNAMSIZ];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153 memset(p, 0, sizeof(*p));
154 memset(&medium, 0, sizeof(medium));
155
156 p->iph.version = 4;
157 p->iph.ihl = 5;
158#ifndef IP_DF
159#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
160#endif
161 p->iph.frag_off = htons(IP_DF);
162
163 while (argc > 0) {
164 if (strcmp(*argv, "mode") == 0) {
165 NEXT_ARG();
166 if (strcmp(*argv, "ipip") == 0 ||
167 strcmp(*argv, "ip/ip") == 0) {
168 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 bb_error_msg("You managed to ask for more than one tunnel mode.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000170 exit(-1);
171 }
172 p->iph.protocol = IPPROTO_IPIP;
173 } else if (strcmp(*argv, "gre") == 0 ||
174 strcmp(*argv, "gre/ip") == 0) {
175 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000176 bb_error_msg("You managed to ask for more than one tunnel mode.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000177 exit(-1);
178 }
179 p->iph.protocol = IPPROTO_GRE;
180 } else if (strcmp(*argv, "sit") == 0 ||
181 strcmp(*argv, "ipv6/ip") == 0) {
182 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 bb_error_msg("You managed to ask for more than one tunnel mode.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000184 exit(-1);
185 }
186 p->iph.protocol = IPPROTO_IPV6;
187 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 bb_error_msg("Cannot guess tunnel mode.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000189 exit(-1);
190 }
191 } else if (strcmp(*argv, "key") == 0) {
192 unsigned uval;
193 NEXT_ARG();
194 p->i_flags |= GRE_KEY;
195 p->o_flags |= GRE_KEY;
196 if (strchr(*argv, '.'))
197 p->i_key = p->o_key = get_addr32(*argv);
198 else {
199 if (get_unsigned(&uval, *argv, 0)<0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000200 bb_error_msg("invalid value of \"key\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000201 exit(-1);
202 }
203 p->i_key = p->o_key = htonl(uval);
204 }
205 } else if (strcmp(*argv, "ikey") == 0) {
206 unsigned uval;
207 NEXT_ARG();
208 p->i_flags |= GRE_KEY;
209 if (strchr(*argv, '.'))
210 p->o_key = get_addr32(*argv);
211 else {
212 if (get_unsigned(&uval, *argv, 0)<0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000213 bb_error_msg("invalid value of \"ikey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000214 exit(-1);
215 }
216 p->i_key = htonl(uval);
217 }
218 } else if (strcmp(*argv, "okey") == 0) {
219 unsigned uval;
220 NEXT_ARG();
221 p->o_flags |= GRE_KEY;
222 if (strchr(*argv, '.'))
223 p->o_key = get_addr32(*argv);
224 else {
225 if (get_unsigned(&uval, *argv, 0)<0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000226 bb_error_msg("invalid value of \"okey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000227 exit(-1);
228 }
229 p->o_key = htonl(uval);
230 }
231 } else if (strcmp(*argv, "seq") == 0) {
232 p->i_flags |= GRE_SEQ;
233 p->o_flags |= GRE_SEQ;
234 } else if (strcmp(*argv, "iseq") == 0) {
235 p->i_flags |= GRE_SEQ;
236 } else if (strcmp(*argv, "oseq") == 0) {
237 p->o_flags |= GRE_SEQ;
238 } else if (strcmp(*argv, "csum") == 0) {
239 p->i_flags |= GRE_CSUM;
240 p->o_flags |= GRE_CSUM;
241 } else if (strcmp(*argv, "icsum") == 0) {
242 p->i_flags |= GRE_CSUM;
243 } else if (strcmp(*argv, "ocsum") == 0) {
244 p->o_flags |= GRE_CSUM;
245 } else if (strcmp(*argv, "nopmtudisc") == 0) {
246 p->iph.frag_off = 0;
247 } else if (strcmp(*argv, "pmtudisc") == 0) {
248 p->iph.frag_off = htons(IP_DF);
249 } else if (strcmp(*argv, "remote") == 0) {
250 NEXT_ARG();
251 if (strcmp(*argv, "any"))
252 p->iph.daddr = get_addr32(*argv);
253 } else if (strcmp(*argv, "local") == 0) {
254 NEXT_ARG();
255 if (strcmp(*argv, "any"))
256 p->iph.saddr = get_addr32(*argv);
257 } else if (strcmp(*argv, "dev") == 0) {
258 NEXT_ARG();
259 strncpy(medium, *argv, IFNAMSIZ-1);
260 } else if (strcmp(*argv, "ttl") == 0) {
261 unsigned uval;
262 NEXT_ARG();
263 if (strcmp(*argv, "inherit") != 0) {
264 if (get_unsigned(&uval, *argv, 0))
265 invarg("invalid TTL\n", *argv);
266 if (uval > 255)
267 invarg("TTL must be <=255\n", *argv);
268 p->iph.ttl = uval;
269 }
270 } else if (strcmp(*argv, "tos") == 0 ||
271 matches(*argv, "dsfield") == 0) {
272 __u32 uval;
273 NEXT_ARG();
274 if (strcmp(*argv, "inherit") != 0) {
275 if (rtnl_dsfield_a2n(&uval, *argv))
276 invarg("bad TOS value", *argv);
277 p->iph.tos = uval;
278 } else
279 p->iph.tos = 1;
280 } else {
281 if (strcmp(*argv, "name") == 0) {
282 NEXT_ARG();
283 }
284 if (p->name[0])
285 duparg2("name", *argv);
286 strncpy(p->name, *argv, IFNAMSIZ);
287 if (cmd == SIOCCHGTUNNEL && count == 0) {
288 struct ip_tunnel_parm old_p;
289 memset(&old_p, 0, sizeof(old_p));
290 if (do_get_ioctl(*argv, &old_p))
291 return -1;
292 *p = old_p;
293 }
294 }
295 count++;
296 argc--; argv++;
297 }
298
299
300 if (p->iph.protocol == 0) {
301 if (memcmp(p->name, "gre", 3) == 0)
302 p->iph.protocol = IPPROTO_GRE;
303 else if (memcmp(p->name, "ipip", 4) == 0)
304 p->iph.protocol = IPPROTO_IPIP;
305 else if (memcmp(p->name, "sit", 3) == 0)
306 p->iph.protocol = IPPROTO_IPV6;
307 }
308
309 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
310 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000311 bb_error_msg("Keys are not allowed with ipip and sit.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000312 return -1;
313 }
314 }
315
316 if (medium[0]) {
317 p->link = do_ioctl_get_ifindex(medium);
318 if (p->link == 0)
319 return -1;
320 }
321
322 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
323 p->i_key = p->iph.daddr;
324 p->i_flags |= GRE_KEY;
325 }
326 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
327 p->o_key = p->iph.daddr;
328 p->o_flags |= GRE_KEY;
329 }
330 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000331 bb_error_msg("Broadcast tunnel requires a source address.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000332 return -1;
333 }
334 return 0;
335}
336
337
338static int do_add(int cmd, int argc, char **argv)
339{
340 struct ip_tunnel_parm p;
341
342 if (parse_args(argc, argv, cmd, &p) < 0)
343 return -1;
344
345 if (p.iph.ttl && p.iph.frag_off == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000346 bb_error_msg("ttl != 0 and noptmudisc are incompatible");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000347 return -1;
348 }
349
350 switch (p.iph.protocol) {
351 case IPPROTO_IPIP:
352 return do_add_ioctl(cmd, "tunl0", &p);
353 case IPPROTO_GRE:
354 return do_add_ioctl(cmd, "gre0", &p);
355 case IPPROTO_IPV6:
356 return do_add_ioctl(cmd, "sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000357 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000358 bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000359 return -1;
360 }
361 return -1;
362}
363
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000364static int do_del(int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000365{
366 struct ip_tunnel_parm p;
367
368 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
369 return -1;
370
371 switch (p.iph.protocol) {
372 case IPPROTO_IPIP:
373 return do_del_ioctl("tunl0", &p);
374 case IPPROTO_GRE:
375 return do_del_ioctl("gre0", &p);
376 case IPPROTO_IPV6:
377 return do_del_ioctl("sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000378 default:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000379 return do_del_ioctl(p.name, &p);
380 }
381 return -1;
382}
383
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000384static void print_tunnel(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000385{
386 char s1[256];
387 char s2[256];
388 char s3[64];
389 char s4[64];
390
391 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
392 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
393 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
394 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
395
396 printf("%s: %s/ip remote %s local %s ",
397 p->name,
398 p->iph.protocol == IPPROTO_IPIP ? "ip" :
399 (p->iph.protocol == IPPROTO_GRE ? "gre" :
400 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
401 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
402 if (p->link) {
403 char *n = do_ioctl_get_ifname(p->link);
404 if (n)
405 printf(" dev %s ", n);
406 }
407 if (p->iph.ttl)
408 printf(" ttl %d ", p->iph.ttl);
409 else
410 printf(" ttl inherit ");
411 if (p->iph.tos) {
412 SPRINT_BUF(b1);
413 printf(" tos");
414 if (p->iph.tos&1)
415 printf(" inherit");
416 if (p->iph.tos&~1)
417 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
418 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
419 }
420 if (!(p->iph.frag_off&htons(IP_DF)))
421 printf(" nopmtudisc");
422
423 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
424 printf(" key %s", s3);
425 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
426 if (p->i_flags&GRE_KEY)
427 printf(" ikey %s ", s3);
428 if (p->o_flags&GRE_KEY)
429 printf(" okey %s ", s4);
430 }
431
432 if (p->i_flags&GRE_SEQ)
433 printf("%s Drop packets out of sequence.\n", _SL_);
434 if (p->i_flags&GRE_CSUM)
435 printf("%s Checksum in received packet is required.", _SL_);
436 if (p->o_flags&GRE_SEQ)
437 printf("%s Sequence packets on output.", _SL_);
438 if (p->o_flags&GRE_CSUM)
439 printf("%s Checksum output packets.", _SL_);
440}
441
442static int do_tunnels_list(struct ip_tunnel_parm *p)
443{
444 char name[IFNAMSIZ];
445 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
446 rx_fifo, rx_frame,
447 tx_bytes, tx_packets, tx_errs, tx_drops,
448 tx_fifo, tx_colls, tx_carrier, rx_multi;
449 int type;
450 struct ip_tunnel_parm p1;
451
452 char buf[512];
453 FILE *fp = fopen("/proc/net/dev", "r");
454 if (fp == NULL) {
455 perror("fopen");
456 return -1;
457 }
458
459 fgets(buf, sizeof(buf), fp);
460 fgets(buf, sizeof(buf), fp);
461
462 while (fgets(buf, sizeof(buf), fp) != NULL) {
463 char *ptr;
464 buf[sizeof(buf) - 1] = 0;
465 if ((ptr = strchr(buf, ':')) == NULL ||
466 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000467 bb_error_msg("Wrong format of /proc/net/dev. Sorry.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000468 return -1;
469 }
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +0000470 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 +0000471 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
472 &rx_fifo, &rx_frame, &rx_multi,
473 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
474 &tx_fifo, &tx_colls, &tx_carrier) != 14)
475 continue;
476 if (p->name[0] && strcmp(p->name, name))
477 continue;
478 type = do_ioctl_get_iftype(name);
479 if (type == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000480 bb_error_msg("Failed to get type of [%s]", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000481 continue;
482 }
483 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
484 continue;
485 memset(&p1, 0, sizeof(p1));
486 if (do_get_ioctl(name, &p1))
487 continue;
488 if ((p->link && p1.link != p->link) ||
489 (p->name[0] && strcmp(p1.name, p->name)) ||
490 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
491 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
492 (p->i_key && p1.i_key != p->i_key))
493 continue;
494 print_tunnel(&p1);
495 printf("\n");
496 }
497 return 0;
498}
499
500static int do_show(int argc, char **argv)
501{
502 int err;
503 struct ip_tunnel_parm p;
504
505 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
506 return -1;
507
508 switch (p.iph.protocol) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000509 case IPPROTO_IPIP:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000510 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
511 break;
512 case IPPROTO_GRE:
513 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
514 break;
515 case IPPROTO_IPV6:
516 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
517 break;
518 default:
519 do_tunnels_list(&p);
520 return 0;
521 }
522 if (err)
523 return -1;
524
525 print_tunnel(&p);
526 printf("\n");
527 return 0;
528}
529
530int do_iptunnel(int argc, char **argv)
531{
532 if (argc > 0) {
533 if (matches(*argv, "add") == 0)
534 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
535 if (matches(*argv, "change") == 0)
536 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
537 if (matches(*argv, "del") == 0)
538 return do_del(argc-1, argv+1);
539 if (matches(*argv, "show") == 0 ||
540 matches(*argv, "lst") == 0 ||
541 matches(*argv, "list") == 0)
542 return do_show(argc-1, argv+1);
543 } else
544 return do_show(0, NULL);
545
Eric Andersenf71ad6c2004-04-26 19:32:49 +0000546 bb_error_msg("Command \"%s\" is unknown.", *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000547 exit(-1);
548}