fixed an logic error in the "not found" handling for deleting entries
[oss-qm-packages.git] / iptunnel.c
blob4943d83dc6a91fce9cc51efc357145ad78fc0990
1 /*
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>
12 * Changes:
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 * Bernd Eckenfels 990715: add linux/types.h (not clean but solves missing __u16
17 * Arnaldo Carvalho de Melo 20010404: use setlocale
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <syslog.h>
25 #include <fcntl.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <netinet/in.h>
29 #include <netinet/ip.h>
30 #include <arpa/inet.h>
31 #if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1))
32 #include <net/if.h>
33 #include <net/if_arp.h>
34 #else
35 #include <linux/if.h>
36 #include <linux/if_arp.h>
37 #endif
38 #include <linux/types.h>
39 #include <linux/if_tunnel.h>
41 #include "config.h"
42 #include "intl.h"
43 #include "net-support.h"
44 #include "version.h"
45 #include "util.h"
47 #undef GRE_CSUM
48 #define GRE_CSUM htons(0x8000)
49 #undef GRE_ROUTING
50 #define GRE_ROUTING htons(0x4000)
51 #undef GRE_KEY
52 #define GRE_KEY htons(0x2000)
53 #undef GRE_SEQ
54 #define GRE_SEQ htons(0x1000)
55 #undef GRE_STRICT
56 #define GRE_STRICT htons(0x0800)
57 #undef GRE_REC
58 #define GRE_REC htons(0x0700)
59 #undef GRE_FLAGS
60 #define GRE_FLAGS htons(0x00F8)
61 #undef GRE_VERSION
62 #define GRE_VERSION htons(0x0007)
64 /* Old versions of glibc do not define this */
65 #if __GLIBC__ == 2 && __GLIBC_MINOR__ == 0
66 #define IPPROTO_GRE 47
67 #endif
69 #include "util-ank.h"
71 char *Release = RELEASE,
72 *Version = "iptunnel 1.01",
73 *Signature = "Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>";
75 static void version(void)
77 printf("%s\n%s\n%s\n", Release, Version, Signature);
78 exit(E_VERSION);
81 static void usage(void) __attribute__((noreturn));
83 static void usage(void)
85 fprintf(stderr, _("Usage: iptunnel { add | change | del | show } [ NAME ]\n"));
86 fprintf(stderr, _(" [ mode { ipip | gre | sit } ] [ remote ADDR ] [ local ADDR ]\n"));
87 fprintf(stderr, _(" [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"));
88 fprintf(stderr, _(" [ ttl TTL ] [ tos TOS ] [ nopmtudisc ] [ dev PHYS_DEV ]\n"));
89 fprintf(stderr, _(" iptunnel -V | --version\n\n"));
90 fprintf(stderr, _("Where: NAME := STRING\n"));
91 fprintf(stderr, _(" ADDR := { IP_ADDRESS | any }\n"));
92 fprintf(stderr, _(" TOS := { NUMBER | inherit }\n"));
93 fprintf(stderr, _(" TTL := { 1..255 | inherit }\n"));
94 fprintf(stderr, _(" KEY := { DOTTED_QUAD | NUMBER }\n"));
95 exit(-1);
98 static int do_ioctl_get_ifindex(char *dev)
100 struct ifreq ifr;
101 int fd;
102 int err;
104 strcpy(ifr.ifr_name, dev);
105 fd = socket(AF_INET, SOCK_DGRAM, 0);
106 err = ioctl(fd, SIOCGIFINDEX, &ifr);
107 if (err) {
108 perror("ioctl");
109 return 0;
111 close(fd);
112 return ifr.ifr_ifindex;
115 static int do_ioctl_get_iftype(char *dev)
117 struct ifreq ifr;
118 int fd;
119 int err;
121 strcpy(ifr.ifr_name, dev);
122 fd = socket(AF_INET, SOCK_DGRAM, 0);
123 err = ioctl(fd, SIOCGIFHWADDR, &ifr);
124 if (err) {
125 perror("ioctl");
126 return -1;
128 close(fd);
129 return ifr.ifr_addr.sa_family;
133 static char * do_ioctl_get_ifname(int idx)
135 static struct ifreq ifr;
136 int fd;
137 int err;
139 ifr.ifr_ifindex = idx;
140 fd = socket(AF_INET, SOCK_DGRAM, 0);
141 err = ioctl(fd, SIOCGIFNAME, &ifr);
142 if (err) {
143 perror("ioctl");
144 return NULL;
146 close(fd);
147 return ifr.ifr_name;
152 static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
154 struct ifreq ifr;
155 int fd;
156 int err;
158 strcpy(ifr.ifr_name, basedev);
159 ifr.ifr_ifru.ifru_data = (void*)p;
160 fd = socket(AF_INET, SOCK_DGRAM, 0);
161 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
162 if (err)
163 perror("ioctl");
164 close(fd);
165 return err;
168 static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
170 struct ifreq ifr;
171 int fd;
172 int err;
174 strcpy(ifr.ifr_name, basedev);
175 ifr.ifr_ifru.ifru_data = (void*)p;
176 fd = socket(AF_INET, SOCK_DGRAM, 0);
177 err = ioctl(fd, cmd, &ifr);
178 if (err)
179 perror("ioctl");
180 close(fd);
181 return err;
184 static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
186 struct ifreq ifr;
187 int fd;
188 int err;
190 strcpy(ifr.ifr_name, basedev);
191 ifr.ifr_ifru.ifru_data = (void*)p;
192 fd = socket(AF_INET, SOCK_DGRAM, 0);
193 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
194 if (err)
195 perror("ioctl");
196 close(fd);
197 return err;
200 static int parse_args(int argc, char **argv, struct ip_tunnel_parm *p)
202 char medium[IFNAMSIZ];
204 memset(p, 0, sizeof(*p));
205 memset(&medium, 0, sizeof(medium));
207 p->iph.version = 4;
208 p->iph.ihl = 5;
209 #ifndef IP_DF
210 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
211 #endif
212 p->iph.frag_off = htons(IP_DF);
214 while (argc > 0) {
215 if (strcmp(*argv, "mode") == 0) {
216 NEXT_ARG();
217 if (strcmp(*argv, "ipip") == 0) {
218 if (p->iph.protocol)
219 usage();
220 p->iph.protocol = IPPROTO_IPIP;
221 } else if (strcmp(*argv, "gre") == 0) {
222 if (p->iph.protocol)
223 usage();
224 p->iph.protocol = IPPROTO_GRE;
225 } else if (strcmp(*argv, "sit") == 0) {
226 if (p->iph.protocol)
227 usage();
228 p->iph.protocol = IPPROTO_IPV6;
229 } else
230 usage();
231 } else if (strcmp(*argv, "key") == 0) {
232 unsigned uval;
233 NEXT_ARG();
234 p->i_flags |= GRE_KEY;
235 p->o_flags |= GRE_KEY;
236 if (strchr(*argv, '.'))
237 p->i_key = p->o_key = get_addr32(*argv);
238 else {
239 if (scan_number(*argv, &uval)<0)
240 usage();
241 p->i_key = p->o_key = htonl(uval);
243 } else if (strcmp(*argv, "ikey") == 0) {
244 unsigned uval;
245 NEXT_ARG();
246 p->i_flags |= GRE_KEY;
247 if (strchr(*argv, '.'))
248 p->o_key = get_addr32(*argv);
249 else {
250 if (scan_number(*argv, &uval)<0)
251 usage();
252 p->i_key = htonl(uval);
254 } else if (strcmp(*argv, "okey") == 0) {
255 unsigned uval;
256 NEXT_ARG();
257 p->o_flags |= GRE_KEY;
258 if (strchr(*argv, '.'))
259 p->o_key = get_addr32(*argv);
260 else {
261 if (scan_number(*argv, &uval)<0)
262 usage();
263 p->o_key = htonl(uval);
265 } else if (strcmp(*argv, "seq") == 0) {
266 p->i_flags |= GRE_SEQ;
267 p->o_flags |= GRE_SEQ;
268 } else if (strcmp(*argv, "iseq") == 0) {
269 p->i_flags |= GRE_SEQ;
270 } else if (strcmp(*argv, "oseq") == 0) {
271 p->o_flags |= GRE_SEQ;
272 } else if (strcmp(*argv, "csum") == 0) {
273 p->i_flags |= GRE_CSUM;
274 p->o_flags |= GRE_CSUM;
275 } else if (strcmp(*argv, "icsum") == 0) {
276 p->i_flags |= GRE_CSUM;
277 } else if (strcmp(*argv, "ocsum") == 0) {
278 p->o_flags |= GRE_CSUM;
279 } else if (strcmp(*argv, "nopmtudisc") == 0) {
280 p->iph.frag_off = 0;
281 } else if (strcmp(*argv, "remote") == 0) {
282 NEXT_ARG();
283 if (strcmp(*argv, "any"))
284 p->iph.daddr = get_addr32(*argv);
285 } else if (strcmp(*argv, "local") == 0) {
286 NEXT_ARG();
287 if (strcmp(*argv, "any"))
288 p->iph.saddr = get_addr32(*argv);
289 } else if (strcmp(*argv, "dev") == 0) {
290 NEXT_ARG();
291 safe_strncpy(medium, *argv, IFNAMSIZ-1);
292 } else if (strcmp(*argv, "ttl") == 0) {
293 unsigned uval;
294 NEXT_ARG();
295 if (strcmp(*argv, "inherit") != 0) {
296 if (scan_number(*argv, &uval)<0)
297 usage();
298 if (uval > 255)
299 usage();
300 p->iph.ttl = uval;
302 } else if (strcmp(*argv, "tos") == 0) {
303 unsigned uval;
304 NEXT_ARG();
305 if (strcmp(*argv, "inherit") != 0) {
306 if (scan_number(*argv, &uval)<0)
307 usage();
308 if (uval > 255)
309 usage();
310 p->iph.tos = uval;
311 } else
312 p->iph.tos = 1;
313 } else {
314 if (p->name[0])
315 usage();
316 safe_strncpy(p->name, *argv, IFNAMSIZ);
318 argc--; argv++;
321 if (p->iph.protocol == 0) {
322 if (memcmp(p->name, "gre", 3) == 0)
323 p->iph.protocol = IPPROTO_GRE;
324 else if (memcmp(p->name, "ipip", 4) == 0)
325 p->iph.protocol = IPPROTO_IPIP;
326 else if (memcmp(p->name, "sit", 3) == 0)
327 p->iph.protocol = IPPROTO_IPV6;
330 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
331 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
332 fprintf(stderr, _("Keys are not allowed with ipip and sit.\n"));
333 return -1;
337 if (medium[0]) {
338 p->link = do_ioctl_get_ifindex(medium);
339 if (p->link == 0)
340 return -1;
343 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
344 p->i_key = p->iph.daddr;
345 p->i_flags |= GRE_KEY;
347 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
348 p->o_key = p->iph.daddr;
349 p->o_flags |= GRE_KEY;
351 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
352 fprintf(stderr, _("Broadcast tunnel requires a source address.\n"));
353 return -1;
355 return 0;
359 static int do_add(int cmd, int argc, char **argv)
361 struct ip_tunnel_parm p;
363 if (parse_args(argc, argv, &p) < 0)
364 return -1;
366 if (p.iph.ttl && p.iph.frag_off == 0) {
367 fprintf(stderr, _("ttl != 0 and noptmudisc are incompatible\n"));
368 return -1;
371 switch (p.iph.protocol) {
372 case IPPROTO_IPIP:
373 return do_add_ioctl(cmd, "tunl0", &p);
374 case IPPROTO_GRE:
375 return do_add_ioctl(cmd, "gre0", &p);
376 case IPPROTO_IPV6:
377 return do_add_ioctl(cmd, "sit0", &p);
378 default:
379 fprintf(stderr, _("cannot determine tunnel mode (ipip, gre or sit)\n"));
380 return -1;
382 return -1;
385 int do_del(int argc, char **argv)
387 struct ip_tunnel_parm p;
389 if (parse_args(argc, argv, &p) < 0)
390 return -1;
392 switch (p.iph.protocol) {
393 case IPPROTO_IPIP:
394 return do_del_ioctl(p.name[0] ? p.name : "tunl0", &p);
395 case IPPROTO_GRE:
396 return do_del_ioctl(p.name[0] ? p.name : "gre0", &p);
397 case IPPROTO_IPV6:
398 return do_del_ioctl(p.name[0] ? p.name : "sit0", &p);
399 default:
400 return do_del_ioctl(p.name, &p);
402 return -1;
405 void print_tunnel(struct ip_tunnel_parm *p)
407 char s1[256];
408 char s2[256];
409 char s3[64];
410 char s4[64];
412 format_host(AF_INET, &p->iph.daddr, s1, sizeof(s1));
413 format_host(AF_INET, &p->iph.saddr, s2, sizeof(s2));
414 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
415 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
417 printf(_("%s: %s/ip remote %s local %s "),
418 p->name,
419 p->iph.protocol == IPPROTO_IPIP ? "ip" :
420 (p->iph.protocol == IPPROTO_GRE ? "gre" :
421 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : _("unknown"))),
422 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
423 if (p->link) {
424 char *n = do_ioctl_get_ifname(p->link);
425 if (n)
426 printf(" dev %s ", n);
428 if (p->iph.ttl)
429 printf(" ttl %d ", p->iph.ttl);
430 else
431 printf(" ttl inherit ");
432 if (p->iph.tos) {
433 printf(" tos");
434 if (p->iph.tos&1)
435 printf(" inherit");
436 if (p->iph.tos&~1)
437 printf("%c%02x ", p->iph.tos&1 ? '/' : ' ', p->iph.tos&~1);
439 if (!(p->iph.frag_off&htons(IP_DF)))
440 printf(" nopmtudisc");
442 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
443 printf(" key %s", s3);
444 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
445 if (p->i_flags&GRE_KEY)
446 printf(" ikey %s ", s3);
447 if (p->o_flags&GRE_KEY)
448 printf(" okey %s ", s4);
450 printf("\n");
452 if (p->i_flags&GRE_SEQ)
453 printf(_(" Drop packets out of sequence.\n"));
454 if (p->i_flags&GRE_CSUM)
455 printf(_(" Checksum in received packet is required.\n"));
456 if (p->o_flags&GRE_SEQ)
457 printf(_(" Sequence packets on output.\n"));
458 if (p->o_flags&GRE_CSUM)
459 printf(_(" Checksum output packets.\n"));
462 static int do_tunnels_list(struct ip_tunnel_parm *p)
464 char name[IFNAMSIZ];
465 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
466 rx_fifo, rx_frame,
467 tx_bytes, tx_packets, tx_errs, tx_drops,
468 tx_fifo, tx_colls, tx_carrier, rx_multi;
469 int type;
470 struct ip_tunnel_parm p1;
472 char buf[512];
473 FILE *fp = fopen("/proc/net/dev", "r");
474 if (fp == NULL) {
475 perror("fopen");
476 return -1;
479 fgets(buf, sizeof(buf), fp);
480 fgets(buf, sizeof(buf), fp);
482 while (fgets(buf, sizeof(buf), fp) != NULL) {
483 char *ptr;
484 buf[sizeof(buf) - 1] = 0;
485 if ((ptr = strchr(buf, ':')) == NULL ||
486 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
487 fprintf(stderr, _("Wrong format of /proc/net/dev. Sorry.\n"));
488 return -1;
490 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
491 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
492 &rx_fifo, &rx_frame, &rx_multi,
493 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
494 &tx_fifo, &tx_colls, &tx_carrier) != 14)
495 continue;
496 if (p->name[0] && strcmp(p->name, name))
497 continue;
498 type = do_ioctl_get_iftype(name);
499 if (type == -1) {
500 fprintf(stderr, _("Failed to get type of [%s]\n"), name);
501 continue;
503 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
504 continue;
505 memset(&p1, 0, sizeof(p1));
506 if (do_get_ioctl(name, &p1))
507 continue;
508 if ((p->link && p1.link != p->link) ||
509 (p->name[0] && strcmp(p1.name, p->name)) ||
510 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
511 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
512 (p->i_key && p1.i_key != p->i_key))
513 continue;
514 print_tunnel(&p1);
515 if (show_stats) {
516 printf(_("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts\n"));
517 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld\n",
518 rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi);
519 printf(_("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs\n"));
520 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld\n\n",
521 tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
524 return 0;
527 static int do_show(int argc, char **argv)
529 int err;
530 struct ip_tunnel_parm p;
532 if (parse_args(argc, argv, &p) < 0)
533 return -1;
535 switch (p.iph.protocol) {
536 case IPPROTO_IPIP:
537 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
538 break;
539 case IPPROTO_GRE:
540 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
541 break;
542 case IPPROTO_IPV6:
543 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
544 break;
545 default:
546 do_tunnels_list(&p);
547 return 0;
549 if (err)
550 return -1;
552 print_tunnel(&p);
553 return 0;
556 int do_iptunnel(int argc, char **argv)
558 if (argc > 0) {
559 if (matches(*argv, "add") == 0)
560 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
561 if (matches(*argv, "change") == 0)
562 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
563 if (matches(*argv, "del") == 0)
564 return do_del(argc-1, argv+1);
565 if (matches(*argv, "show") == 0 ||
566 matches(*argv, "lst") == 0 ||
567 matches(*argv, "list") == 0)
568 return do_show(argc-1, argv+1);
569 } else
570 return do_show(0, NULL);
572 usage();
576 int preferred_family = AF_UNSPEC;
577 int show_stats = 0;
578 int resolve_hosts = 0;
580 int main(int argc, char **argv)
582 char *basename;
584 #if I18N
585 setlocale (LC_ALL, "");
586 bindtextdomain("net-tools", "/usr/share/locale");
587 textdomain("net-tools");
588 #endif
590 basename = strrchr(argv[0], '/');
591 if (basename == NULL)
592 basename = argv[0];
593 else
594 basename++;
596 while (argc > 1) {
597 if (argv[1][0] != '-')
598 break;
599 if (matches(argv[1], "-family") == 0) {
600 argc--;
601 argv++;
602 if (argc <= 1)
603 usage();
604 if (strcmp(argv[1], "inet") == 0)
605 preferred_family = AF_INET;
606 else if (strcmp(argv[1], "inet6") == 0)
607 preferred_family = AF_INET6;
608 else
609 usage();
610 } else if (matches(argv[1], "-stats") == 0 ||
611 matches(argv[1], "-statistics") == 0) {
612 ++show_stats;
613 } else if (matches(argv[1], "-resolve") == 0) {
614 ++resolve_hosts;
615 } else if ((matches(argv[1], "-V") == 0) || (matches(argv[1], "--version") == 0)) {
616 version();
617 } else
618 usage();
619 argc--; argv++;
622 return do_iptunnel(argc-1, argv+1);