blob: dbde477a02ec4db7ff068600e48884f429711d78 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
3 * iptunnel.c "ip tunnel"
4 *
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +00005 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 *
9 *
10 * Changes:
11 *
12 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
13 * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
14 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
15 */
16
Rob Landleyecae66a2006-06-02 20:53:38 +000017#include "libbb.h"
Glenn L McGrath275be872002-12-16 07:37:21 +000018#include <sys/socket.h>
19#include <sys/ioctl.h>
20
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000021#include <string.h>
22#include <unistd.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000023
Glenn L McGrath275be872002-12-16 07:37:21 +000024#include <netinet/ip.h>
25
Eric Andersenab4e19a2003-01-14 08:54:08 +000026#include <net/if.h>
27#include <net/if_arp.h>
28
29#include <asm/types.h>
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000030#ifndef __constant_htons
Eric Andersenab4e19a2003-01-14 08:54:08 +000031#define __constant_htons htons
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000032#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000033#include <linux/if_tunnel.h>
34
35#include "rt_names.h"
36#include "utils.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000037#include "ip_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000039
40static int do_ioctl_get_ifindex(char *dev)
41{
42 struct ifreq ifr;
43 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000044
Denis Vlasenko229b3d22006-11-27 23:44:57 +000045 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Denis Vlasenko27af5a02006-09-03 12:21:59 +000046 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000047 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 bb_perror_msg("ioctl");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049 return 0;
50 }
51 close(fd);
52 return ifr.ifr_ifindex;
53}
54
55static int do_ioctl_get_iftype(char *dev)
56{
57 struct ifreq ifr;
58 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000059
Denis Vlasenko229b3d22006-11-27 23:44:57 +000060 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Denis Vlasenko27af5a02006-09-03 12:21:59 +000061 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000062 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 bb_perror_msg("ioctl");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000064 return -1;
65 }
66 close(fd);
67 return ifr.ifr_addr.sa_family;
68}
69
70
Glenn L McGrath8b602442002-11-28 12:19:51 +000071static char *do_ioctl_get_ifname(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000072{
73 static struct ifreq ifr;
74 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000075
76 ifr.ifr_ifindex = idx;
Denis Vlasenko27af5a02006-09-03 12:21:59 +000077 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000078 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 bb_perror_msg("ioctl");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000080 return NULL;
81 }
82 close(fd);
83 return ifr.ifr_name;
84}
85
86
87
88static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
89{
90 struct ifreq ifr;
91 int fd;
92 int err;
93
Denis Vlasenko229b3d22006-11-27 23:44:57 +000094 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000095 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +000096 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000097 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +000098 if (err) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 bb_perror_msg("ioctl");
Glenn L McGrath8b602442002-11-28 12:19:51 +0000100 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000101 close(fd);
102 return err;
103}
104
105static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
106{
107 struct ifreq ifr;
108 int fd;
109 int err;
110
Glenn L McGrath8b602442002-11-28 12:19:51 +0000111 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000112 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +0000113 } else {
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000114 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +0000115 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000116 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000117 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000118 err = ioctl(fd, cmd, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000119 if (err) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000120 bb_perror_msg("ioctl");
Glenn L McGrath8b602442002-11-28 12:19:51 +0000121 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122 close(fd);
123 return err;
124}
125
126static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
127{
128 struct ifreq ifr;
129 int fd;
130 int err;
131
Glenn L McGrath8b602442002-11-28 12:19:51 +0000132 if (p->name[0]) {
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000133 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +0000134 } else {
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000135 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +0000136 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000137 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000138 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000139 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000140 if (err) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000141 bb_perror_msg("ioctl");
Glenn L McGrath8b602442002-11-28 12:19:51 +0000142 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000143 close(fd);
144 return err;
145}
146
147static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
148{
149 int count = 0;
150 char medium[IFNAMSIZ];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000151 memset(p, 0, sizeof(*p));
152 memset(&medium, 0, sizeof(medium));
153
154 p->iph.version = 4;
155 p->iph.ihl = 5;
156#ifndef IP_DF
157#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
158#endif
159 p->iph.frag_off = htons(IP_DF);
160
161 while (argc > 0) {
162 if (strcmp(*argv, "mode") == 0) {
163 NEXT_ARG();
164 if (strcmp(*argv, "ipip") == 0 ||
165 strcmp(*argv, "ip/ip") == 0) {
166 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000167 bb_error_msg("you managed to ask for more than one tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168 exit(-1);
169 }
170 p->iph.protocol = IPPROTO_IPIP;
171 } else if (strcmp(*argv, "gre") == 0 ||
172 strcmp(*argv, "gre/ip") == 0) {
173 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000174 bb_error_msg("you managed to ask for more than one tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000175 exit(-1);
176 }
177 p->iph.protocol = IPPROTO_GRE;
178 } else if (strcmp(*argv, "sit") == 0 ||
179 strcmp(*argv, "ipv6/ip") == 0) {
180 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000181 bb_error_msg("you managed to ask for more than one tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000182 exit(-1);
183 }
184 p->iph.protocol = IPPROTO_IPV6;
185 } else {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000186 bb_error_msg("cannot guess tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000187 exit(-1);
188 }
189 } else if (strcmp(*argv, "key") == 0) {
190 unsigned uval;
191 NEXT_ARG();
192 p->i_flags |= GRE_KEY;
193 p->o_flags |= GRE_KEY;
194 if (strchr(*argv, '.'))
195 p->i_key = p->o_key = get_addr32(*argv);
196 else {
197 if (get_unsigned(&uval, *argv, 0)<0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000198 bb_error_msg("invalid value of \"key\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000199 exit(-1);
200 }
201 p->i_key = p->o_key = htonl(uval);
202 }
203 } else if (strcmp(*argv, "ikey") == 0) {
204 unsigned uval;
205 NEXT_ARG();
206 p->i_flags |= GRE_KEY;
207 if (strchr(*argv, '.'))
208 p->o_key = get_addr32(*argv);
209 else {
210 if (get_unsigned(&uval, *argv, 0)<0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000211 bb_error_msg("invalid value of \"ikey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000212 exit(-1);
213 }
214 p->i_key = htonl(uval);
215 }
216 } else if (strcmp(*argv, "okey") == 0) {
217 unsigned uval;
218 NEXT_ARG();
219 p->o_flags |= GRE_KEY;
220 if (strchr(*argv, '.'))
221 p->o_key = get_addr32(*argv);
222 else {
223 if (get_unsigned(&uval, *argv, 0)<0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000224 bb_error_msg("invalid value of \"okey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000225 exit(-1);
226 }
227 p->o_key = htonl(uval);
228 }
229 } else if (strcmp(*argv, "seq") == 0) {
230 p->i_flags |= GRE_SEQ;
231 p->o_flags |= GRE_SEQ;
232 } else if (strcmp(*argv, "iseq") == 0) {
233 p->i_flags |= GRE_SEQ;
234 } else if (strcmp(*argv, "oseq") == 0) {
235 p->o_flags |= GRE_SEQ;
236 } else if (strcmp(*argv, "csum") == 0) {
237 p->i_flags |= GRE_CSUM;
238 p->o_flags |= GRE_CSUM;
239 } else if (strcmp(*argv, "icsum") == 0) {
240 p->i_flags |= GRE_CSUM;
241 } else if (strcmp(*argv, "ocsum") == 0) {
242 p->o_flags |= GRE_CSUM;
243 } else if (strcmp(*argv, "nopmtudisc") == 0) {
244 p->iph.frag_off = 0;
245 } else if (strcmp(*argv, "pmtudisc") == 0) {
246 p->iph.frag_off = htons(IP_DF);
247 } else if (strcmp(*argv, "remote") == 0) {
248 NEXT_ARG();
249 if (strcmp(*argv, "any"))
250 p->iph.daddr = get_addr32(*argv);
251 } else if (strcmp(*argv, "local") == 0) {
252 NEXT_ARG();
253 if (strcmp(*argv, "any"))
254 p->iph.saddr = get_addr32(*argv);
255 } else if (strcmp(*argv, "dev") == 0) {
256 NEXT_ARG();
257 strncpy(medium, *argv, IFNAMSIZ-1);
258 } else if (strcmp(*argv, "ttl") == 0) {
259 unsigned uval;
260 NEXT_ARG();
261 if (strcmp(*argv, "inherit") != 0) {
262 if (get_unsigned(&uval, *argv, 0))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000263 invarg(*argv, "TTL");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000264 if (uval > 255)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000265 invarg(*argv, "TTL must be <=255");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266 p->iph.ttl = uval;
267 }
268 } else if (strcmp(*argv, "tos") == 0 ||
269 matches(*argv, "dsfield") == 0) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000270 uint32_t uval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000271 NEXT_ARG();
272 if (strcmp(*argv, "inherit") != 0) {
273 if (rtnl_dsfield_a2n(&uval, *argv))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000274 invarg(*argv, "TOS");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000275 p->iph.tos = uval;
276 } else
277 p->iph.tos = 1;
278 } else {
279 if (strcmp(*argv, "name") == 0) {
280 NEXT_ARG();
281 }
282 if (p->name[0])
283 duparg2("name", *argv);
284 strncpy(p->name, *argv, IFNAMSIZ);
285 if (cmd == SIOCCHGTUNNEL && count == 0) {
286 struct ip_tunnel_parm old_p;
287 memset(&old_p, 0, sizeof(old_p));
288 if (do_get_ioctl(*argv, &old_p))
289 return -1;
290 *p = old_p;
291 }
292 }
293 count++;
294 argc--; argv++;
295 }
296
297
298 if (p->iph.protocol == 0) {
299 if (memcmp(p->name, "gre", 3) == 0)
300 p->iph.protocol = IPPROTO_GRE;
301 else if (memcmp(p->name, "ipip", 4) == 0)
302 p->iph.protocol = IPPROTO_IPIP;
303 else if (memcmp(p->name, "sit", 3) == 0)
304 p->iph.protocol = IPPROTO_IPV6;
305 }
306
307 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
308 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000309 bb_error_msg("keys are not allowed with ipip and sit");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000310 return -1;
311 }
312 }
313
314 if (medium[0]) {
315 p->link = do_ioctl_get_ifindex(medium);
316 if (p->link == 0)
317 return -1;
318 }
319
320 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
321 p->i_key = p->iph.daddr;
322 p->i_flags |= GRE_KEY;
323 }
324 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
325 p->o_key = p->iph.daddr;
326 p->o_flags |= GRE_KEY;
327 }
328 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000329 bb_error_msg("broadcast tunnel requires a source address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000330 return -1;
331 }
332 return 0;
333}
334
335
336static int do_add(int cmd, int argc, char **argv)
337{
338 struct ip_tunnel_parm p;
339
340 if (parse_args(argc, argv, cmd, &p) < 0)
341 return -1;
342
343 if (p.iph.ttl && p.iph.frag_off == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000344 bb_error_msg("ttl != 0 and noptmudisc are incompatible");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000345 return -1;
346 }
347
348 switch (p.iph.protocol) {
349 case IPPROTO_IPIP:
350 return do_add_ioctl(cmd, "tunl0", &p);
351 case IPPROTO_GRE:
352 return do_add_ioctl(cmd, "gre0", &p);
353 case IPPROTO_IPV6:
354 return do_add_ioctl(cmd, "sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000355 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000356 bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000357 return -1;
358 }
359 return -1;
360}
361
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000362static int do_del(int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000363{
364 struct ip_tunnel_parm p;
365
366 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
367 return -1;
368
369 switch (p.iph.protocol) {
370 case IPPROTO_IPIP:
371 return do_del_ioctl("tunl0", &p);
372 case IPPROTO_GRE:
373 return do_del_ioctl("gre0", &p);
374 case IPPROTO_IPV6:
375 return do_del_ioctl("sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000376 default:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000377 return do_del_ioctl(p.name, &p);
378 }
379 return -1;
380}
381
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000382static void print_tunnel(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000383{
384 char s1[256];
385 char s2[256];
386 char s3[64];
387 char s4[64];
388
389 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
390 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
391 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
392 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
393
394 printf("%s: %s/ip remote %s local %s ",
395 p->name,
396 p->iph.protocol == IPPROTO_IPIP ? "ip" :
397 (p->iph.protocol == IPPROTO_GRE ? "gre" :
398 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
399 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
400 if (p->link) {
401 char *n = do_ioctl_get_ifname(p->link);
402 if (n)
403 printf(" dev %s ", n);
404 }
405 if (p->iph.ttl)
406 printf(" ttl %d ", p->iph.ttl);
407 else
408 printf(" ttl inherit ");
409 if (p->iph.tos) {
410 SPRINT_BUF(b1);
411 printf(" tos");
412 if (p->iph.tos&1)
413 printf(" inherit");
414 if (p->iph.tos&~1)
415 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
416 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
417 }
418 if (!(p->iph.frag_off&htons(IP_DF)))
419 printf(" nopmtudisc");
420
421 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
422 printf(" key %s", s3);
423 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
424 if (p->i_flags&GRE_KEY)
425 printf(" ikey %s ", s3);
426 if (p->o_flags&GRE_KEY)
427 printf(" okey %s ", s4);
428 }
429
430 if (p->i_flags&GRE_SEQ)
431 printf("%s Drop packets out of sequence.\n", _SL_);
432 if (p->i_flags&GRE_CSUM)
433 printf("%s Checksum in received packet is required.", _SL_);
434 if (p->o_flags&GRE_SEQ)
435 printf("%s Sequence packets on output.", _SL_);
436 if (p->o_flags&GRE_CSUM)
437 printf("%s Checksum output packets.", _SL_);
438}
439
440static int do_tunnels_list(struct ip_tunnel_parm *p)
441{
442 char name[IFNAMSIZ];
443 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
444 rx_fifo, rx_frame,
445 tx_bytes, tx_packets, tx_errs, tx_drops,
446 tx_fifo, tx_colls, tx_carrier, rx_multi;
447 int type;
448 struct ip_tunnel_parm p1;
449
450 char buf[512];
451 FILE *fp = fopen("/proc/net/dev", "r");
452 if (fp == NULL) {
453 perror("fopen");
454 return -1;
455 }
456
457 fgets(buf, sizeof(buf), fp);
458 fgets(buf, sizeof(buf), fp);
459
460 while (fgets(buf, sizeof(buf), fp) != NULL) {
461 char *ptr;
462 buf[sizeof(buf) - 1] = 0;
463 if ((ptr = strchr(buf, ':')) == NULL ||
464 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000465 bb_error_msg("wrong format of /proc/net/dev. Sorry");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000466 return -1;
467 }
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +0000468 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 +0000469 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
470 &rx_fifo, &rx_frame, &rx_multi,
471 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
472 &tx_fifo, &tx_colls, &tx_carrier) != 14)
473 continue;
474 if (p->name[0] && strcmp(p->name, name))
475 continue;
476 type = do_ioctl_get_iftype(name);
477 if (type == -1) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000478 bb_error_msg("failed to get type of [%s]", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000479 continue;
480 }
481 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
482 continue;
483 memset(&p1, 0, sizeof(p1));
484 if (do_get_ioctl(name, &p1))
485 continue;
486 if ((p->link && p1.link != p->link) ||
487 (p->name[0] && strcmp(p1.name, p->name)) ||
488 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
489 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
490 (p->i_key && p1.i_key != p->i_key))
491 continue;
492 print_tunnel(&p1);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000493 puts("");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000494 }
495 return 0;
496}
497
498static int do_show(int argc, char **argv)
499{
500 int err;
501 struct ip_tunnel_parm p;
502
503 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
504 return -1;
505
506 switch (p.iph.protocol) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000507 case IPPROTO_IPIP:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000508 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
509 break;
510 case IPPROTO_GRE:
511 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
512 break;
513 case IPPROTO_IPV6:
514 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
515 break;
516 default:
517 do_tunnels_list(&p);
518 return 0;
519 }
520 if (err)
521 return -1;
522
523 print_tunnel(&p);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000524 puts("");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000525 return 0;
526}
527
528int do_iptunnel(int argc, char **argv)
529{
530 if (argc > 0) {
531 if (matches(*argv, "add") == 0)
532 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
533 if (matches(*argv, "change") == 0)
534 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
535 if (matches(*argv, "del") == 0)
536 return do_del(argc-1, argv+1);
537 if (matches(*argv, "show") == 0 ||
538 matches(*argv, "lst") == 0 ||
539 matches(*argv, "list") == 0)
540 return do_show(argc-1, argv+1);
541 } else
542 return do_show(0, NULL);
543
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000544 bb_error_msg("command \"%s\" is unknown", *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000545 exit(-1);
546}