akamatsu@kobedenshi.ac.jp reported a problem with --device without
[oss-qm-packages.git] / iptunnel.c
blob2528eebd26ab44740ffcea658c61bdcea9181e90
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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <syslog.h>
24 #include <fcntl.h>
25 #include <sys/socket.h>
26 #include <sys/ioctl.h>
27 #include <linux/if.h>
28 #include <linux/if_arp.h>
29 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <arpa/inet.h>
32 #include <linux/types.h>
33 #include <linux/if_tunnel.h>
35 #include "config.h"
36 #include "intl.h"
37 #include "net-support.h"
38 #include "version.h"
39 #include "util.h"
41 #undef GRE_CSUM
42 #define GRE_CSUM htons(0x8000)
43 #undef GRE_ROUTING
44 #define GRE_ROUTING htons(0x4000)
45 #undef GRE_KEY
46 #define GRE_KEY htons(0x2000)
47 #undef GRE_SEQ
48 #define GRE_SEQ htons(0x1000)
49 #undef GRE_STRICT
50 #define GRE_STRICT htons(0x0800)
51 #undef GRE_REC
52 #define GRE_REC htons(0x0700)
53 #undef GRE_FLAGS
54 #define GRE_FLAGS htons(0x00F8)
55 #undef GRE_VERSION
56 #define GRE_VERSION htons(0x0007)
58 /* Old versions of glibc do not define this */
59 #if __GLIBC__ == 2 && __GLIBC_MINOR__ == 0
60 #define IPPROTO_GRE 47
61 #endif
63 #include "util-ank.h"
65 char *Release = RELEASE,
66 *Version = "iptunnel 1.01",
67 *Signature = "Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>";
69 static void version(void)
71 printf("%s\n%s\n%s\n", Release, Version, Signature);
72 exit(E_VERSION);
75 static void usage(void) __attribute__((noreturn));
77 static void usage(void)
79 fprintf(stderr, _("Usage: iptunnel { add | change | del | show } [ NAME ]\n"));
80 fprintf(stderr, _(" [ mode { ipip | gre | sit } ] [ remote ADDR ] [ local ADDR ]\n"));
81 fprintf(stderr, _(" [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"));
82 fprintf(stderr, _(" [ ttl TTL ] [ tos TOS ] [ nopmtudisc ] [ dev PHYS_DEV ]\n"));
83 fprintf(stderr, _(" iptunnel -V | --version\n\n"));
84 fprintf(stderr, _("Where: NAME := STRING\n"));
85 fprintf(stderr, _(" ADDR := { IP_ADDRESS | any }\n"));
86 fprintf(stderr, _(" TOS := { NUMBER | inherit }\n"));
87 fprintf(stderr, _(" TTL := { 1..255 | inherit }\n"));
88 fprintf(stderr, _(" KEY := { DOTTED_QUAD | NUMBER }\n"));
89 exit(-1);
92 static int do_ioctl_get_ifindex(char *dev)
94 struct ifreq ifr;
95 int fd;
96 int err;
98 strcpy(ifr.ifr_name, dev);
99 fd = socket(AF_INET, SOCK_DGRAM, 0);
100 err = ioctl(fd, SIOCGIFINDEX, &ifr);
101 if (err) {
102 perror("ioctl");
103 return 0;
105 close(fd);
106 return ifr.ifr_ifindex;
109 static int do_ioctl_get_iftype(char *dev)
111 struct ifreq ifr;
112 int fd;
113 int err;
115 strcpy(ifr.ifr_name, dev);
116 fd = socket(AF_INET, SOCK_DGRAM, 0);
117 err = ioctl(fd, SIOCGIFHWADDR, &ifr);
118 if (err) {
119 perror("ioctl");
120 return -1;
122 close(fd);
123 return ifr.ifr_addr.sa_family;
127 static char * do_ioctl_get_ifname(int idx)
129 static struct ifreq ifr;
130 int fd;
131 int err;
133 ifr.ifr_ifindex = idx;
134 fd = socket(AF_INET, SOCK_DGRAM, 0);
135 err = ioctl(fd, SIOCGIFNAME, &ifr);
136 if (err) {
137 perror("ioctl");
138 return NULL;
140 close(fd);
141 return ifr.ifr_name;
146 static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
148 struct ifreq ifr;
149 int fd;
150 int err;
152 strcpy(ifr.ifr_name, basedev);
153 ifr.ifr_ifru.ifru_data = (void*)p;
154 fd = socket(AF_INET, SOCK_DGRAM, 0);
155 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
156 if (err)
157 perror("ioctl");
158 close(fd);
159 return err;
162 static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
164 struct ifreq ifr;
165 int fd;
166 int err;
168 strcpy(ifr.ifr_name, basedev);
169 ifr.ifr_ifru.ifru_data = (void*)p;
170 fd = socket(AF_INET, SOCK_DGRAM, 0);
171 err = ioctl(fd, cmd, &ifr);
172 if (err)
173 perror("ioctl");
174 close(fd);
175 return err;
178 static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
180 struct ifreq ifr;
181 int fd;
182 int err;
184 strcpy(ifr.ifr_name, basedev);
185 ifr.ifr_ifru.ifru_data = (void*)p;
186 fd = socket(AF_INET, SOCK_DGRAM, 0);
187 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
188 if (err)
189 perror("ioctl");
190 close(fd);
191 return err;
194 static int parse_args(int argc, char **argv, struct ip_tunnel_parm *p)
196 char medium[IFNAMSIZ];
198 memset(p, 0, sizeof(*p));
199 memset(&medium, 0, sizeof(medium));
201 p->iph.version = 4;
202 p->iph.ihl = 5;
203 #ifndef IP_DF
204 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
205 #endif
206 p->iph.frag_off = htons(IP_DF);
208 while (argc > 0) {
209 if (strcmp(*argv, "mode") == 0) {
210 NEXT_ARG();
211 if (strcmp(*argv, "ipip") == 0) {
212 if (p->iph.protocol)
213 usage();
214 p->iph.protocol = IPPROTO_IPIP;
215 } else if (strcmp(*argv, "gre") == 0) {
216 if (p->iph.protocol)
217 usage();
218 p->iph.protocol = IPPROTO_GRE;
219 } else if (strcmp(*argv, "sit") == 0) {
220 if (p->iph.protocol)
221 usage();
222 p->iph.protocol = IPPROTO_IPV6;
223 } else
224 usage();
225 } else if (strcmp(*argv, "key") == 0) {
226 unsigned uval;
227 NEXT_ARG();
228 p->i_flags |= GRE_KEY;
229 p->o_flags |= GRE_KEY;
230 if (strchr(*argv, '.'))
231 p->i_key = p->o_key = get_addr32(*argv);
232 else {
233 if (scan_number(*argv, &uval)<0)
234 usage();
235 p->i_key = p->o_key = htonl(uval);
237 } else if (strcmp(*argv, "ikey") == 0) {
238 unsigned uval;
239 NEXT_ARG();
240 p->i_flags |= GRE_KEY;
241 if (strchr(*argv, '.'))
242 p->o_key = get_addr32(*argv);
243 else {
244 if (scan_number(*argv, &uval)<0)
245 usage();
246 p->i_key = htonl(uval);
248 } else if (strcmp(*argv, "okey") == 0) {
249 unsigned uval;
250 NEXT_ARG();
251 p->o_flags |= GRE_KEY;
252 if (strchr(*argv, '.'))
253 p->o_key = get_addr32(*argv);
254 else {
255 if (scan_number(*argv, &uval)<0)
256 usage();
257 p->o_key = htonl(uval);
259 } else if (strcmp(*argv, "seq") == 0) {
260 p->i_flags |= GRE_SEQ;
261 p->o_flags |= GRE_SEQ;
262 } else if (strcmp(*argv, "iseq") == 0) {
263 p->i_flags |= GRE_SEQ;
264 } else if (strcmp(*argv, "oseq") == 0) {
265 p->o_flags |= GRE_SEQ;
266 } else if (strcmp(*argv, "csum") == 0) {
267 p->i_flags |= GRE_CSUM;
268 p->o_flags |= GRE_CSUM;
269 } else if (strcmp(*argv, "icsum") == 0) {
270 p->i_flags |= GRE_CSUM;
271 } else if (strcmp(*argv, "ocsum") == 0) {
272 p->o_flags |= GRE_CSUM;
273 } else if (strcmp(*argv, "nopmtudisc") == 0) {
274 p->iph.frag_off = 0;
275 } else if (strcmp(*argv, "remote") == 0) {
276 NEXT_ARG();
277 if (strcmp(*argv, "any"))
278 p->iph.daddr = get_addr32(*argv);
279 } else if (strcmp(*argv, "local") == 0) {
280 NEXT_ARG();
281 if (strcmp(*argv, "any"))
282 p->iph.saddr = get_addr32(*argv);
283 } else if (strcmp(*argv, "dev") == 0) {
284 NEXT_ARG();
285 safe_strncpy(medium, *argv, IFNAMSIZ-1);
286 } else if (strcmp(*argv, "ttl") == 0) {
287 unsigned uval;
288 NEXT_ARG();
289 if (strcmp(*argv, "inherit") != 0) {
290 if (scan_number(*argv, &uval)<0)
291 usage();
292 if (uval > 255)
293 usage();
294 p->iph.ttl = uval;
296 } else if (strcmp(*argv, "tos") == 0) {
297 unsigned uval;
298 NEXT_ARG();
299 if (strcmp(*argv, "inherit") != 0) {
300 if (scan_number(*argv, &uval)<0)
301 usage();
302 if (uval > 255)
303 usage();
304 p->iph.tos = uval;
305 } else
306 p->iph.tos = 1;
307 } else {
308 if (p->name[0])
309 usage();
310 safe_strncpy(p->name, *argv, IFNAMSIZ);
312 argc--; argv++;
315 if (p->iph.protocol == 0) {
316 if (memcmp(p->name, "gre", 3) == 0)
317 p->iph.protocol = IPPROTO_GRE;
318 else if (memcmp(p->name, "ipip", 4) == 0)
319 p->iph.protocol = IPPROTO_IPIP;
320 else if (memcmp(p->name, "sit", 3) == 0)
321 p->iph.protocol = IPPROTO_IPV6;
324 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
325 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
326 fprintf(stderr, _("Keys are not allowed with ipip and sit.\n"));
327 return -1;
331 if (medium[0]) {
332 p->link = do_ioctl_get_ifindex(medium);
333 if (p->link == 0)
334 return -1;
337 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
338 p->i_key = p->iph.daddr;
339 p->i_flags |= GRE_KEY;
341 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
342 p->o_key = p->iph.daddr;
343 p->o_flags |= GRE_KEY;
345 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
346 fprintf(stderr, _("Broadcast tunnel requires a source address.\n"));
347 return -1;
349 return 0;
353 static int do_add(int cmd, int argc, char **argv)
355 struct ip_tunnel_parm p;
357 if (parse_args(argc, argv, &p) < 0)
358 return -1;
360 if (p.iph.ttl && p.iph.frag_off == 0) {
361 fprintf(stderr, _("ttl != 0 and noptmudisc are incompatible\n"));
362 return -1;
365 switch (p.iph.protocol) {
366 case IPPROTO_IPIP:
367 return do_add_ioctl(cmd, "tunl0", &p);
368 case IPPROTO_GRE:
369 return do_add_ioctl(cmd, "gre0", &p);
370 case IPPROTO_IPV6:
371 return do_add_ioctl(cmd, "sit0", &p);
372 default:
373 fprintf(stderr, _("cannot determine tunnel mode (ipip, gre or sit)\n"));
374 return -1;
376 return -1;
379 int do_del(int argc, char **argv)
381 struct ip_tunnel_parm p;
383 if (parse_args(argc, argv, &p) < 0)
384 return -1;
386 switch (p.iph.protocol) {
387 case IPPROTO_IPIP:
388 return do_del_ioctl(p.name[0] ? p.name : "tunl0", &p);
389 case IPPROTO_GRE:
390 return do_del_ioctl(p.name[0] ? p.name : "gre0", &p);
391 case IPPROTO_IPV6:
392 return do_del_ioctl(p.name[0] ? p.name : "sit0", &p);
393 default:
394 return do_del_ioctl(p.name, &p);
396 return -1;
399 void print_tunnel(struct ip_tunnel_parm *p)
401 char s1[256];
402 char s2[256];
403 char s3[64];
404 char s4[64];
406 format_host(AF_INET, &p->iph.daddr, s1, sizeof(s1));
407 format_host(AF_INET, &p->iph.saddr, s2, sizeof(s2));
408 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
409 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
411 printf(_("%s: %s/ip remote %s local %s "),
412 p->name,
413 p->iph.protocol == IPPROTO_IPIP ? "ip" :
414 (p->iph.protocol == IPPROTO_GRE ? "gre" :
415 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : _("unknown"))),
416 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
417 if (p->link) {
418 char *n = do_ioctl_get_ifname(p->link);
419 if (n)
420 printf(" dev %s ", n);
422 if (p->iph.ttl)
423 printf(" ttl %d ", p->iph.ttl);
424 else
425 printf(" ttl inherit ");
426 if (p->iph.tos) {
427 printf(" tos");
428 if (p->iph.tos&1)
429 printf(" inherit");
430 if (p->iph.tos&~1)
431 printf("%c%02x ", p->iph.tos&1 ? '/' : ' ', p->iph.tos&~1);
433 if (!(p->iph.frag_off&htons(IP_DF)))
434 printf(" nopmtudisc");
436 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
437 printf(" key %s", s3);
438 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
439 if (p->i_flags&GRE_KEY)
440 printf(" ikey %s ", s3);
441 if (p->o_flags&GRE_KEY)
442 printf(" okey %s ", s4);
444 printf("\n");
446 if (p->i_flags&GRE_SEQ)
447 printf(_(" Drop packets out of sequence.\n"));
448 if (p->i_flags&GRE_CSUM)
449 printf(_(" Checksum in received packet is required.\n"));
450 if (p->o_flags&GRE_SEQ)
451 printf(_(" Sequence packets on output.\n"));
452 if (p->o_flags&GRE_CSUM)
453 printf(_(" Checksum output packets.\n"));
456 static int do_tunnels_list(struct ip_tunnel_parm *p)
458 char name[IFNAMSIZ];
459 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
460 rx_fifo, rx_frame,
461 tx_bytes, tx_packets, tx_errs, tx_drops,
462 tx_fifo, tx_colls, tx_carrier, rx_multi;
463 int type;
464 struct ip_tunnel_parm p1;
466 char buf[512];
467 FILE *fp = fopen("/proc/net/dev", "r");
468 if (fp == NULL) {
469 perror("fopen");
470 return -1;
473 fgets(buf, sizeof(buf), fp);
474 fgets(buf, sizeof(buf), fp);
476 while (fgets(buf, sizeof(buf), fp) != NULL) {
477 char *ptr;
478 buf[sizeof(buf) - 1] = 0;
479 if ((ptr = strchr(buf, ':')) == NULL ||
480 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
481 fprintf(stderr, _("Wrong format of /proc/net/dev. Sorry.\n"));
482 return -1;
484 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
485 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
486 &rx_fifo, &rx_frame, &rx_multi,
487 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
488 &tx_fifo, &tx_colls, &tx_carrier) != 14)
489 continue;
490 if (p->name[0] && strcmp(p->name, name))
491 continue;
492 type = do_ioctl_get_iftype(name);
493 if (type == -1) {
494 fprintf(stderr, _("Failed to get type of [%s]\n"), name);
495 continue;
497 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
498 continue;
499 memset(&p1, 0, sizeof(p1));
500 if (do_get_ioctl(name, &p1))
501 continue;
502 if ((p->link && p1.link != p->link) ||
503 (p->name[0] && strcmp(p1.name, p->name)) ||
504 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
505 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
506 (p->i_key && p1.i_key != p->i_key))
507 continue;
508 print_tunnel(&p1);
509 if (show_stats) {
510 printf(_("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts\n"));
511 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld\n",
512 rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi);
513 printf(_("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs\n"));
514 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld\n\n",
515 tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
518 return 0;
521 static int do_show(int argc, char **argv)
523 int err;
524 struct ip_tunnel_parm p;
526 if (parse_args(argc, argv, &p) < 0)
527 return -1;
529 switch (p.iph.protocol) {
530 case IPPROTO_IPIP:
531 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
532 break;
533 case IPPROTO_GRE:
534 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
535 break;
536 case IPPROTO_IPV6:
537 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
538 break;
539 default:
540 do_tunnels_list(&p);
541 return 0;
543 if (err)
544 return -1;
546 print_tunnel(&p);
547 return 0;
550 int do_iptunnel(int argc, char **argv)
552 if (argc > 0) {
553 if (matches(*argv, "add") == 0)
554 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
555 if (matches(*argv, "change") == 0)
556 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
557 if (matches(*argv, "del") == 0)
558 return do_del(argc-1, argv+1);
559 if (matches(*argv, "show") == 0 ||
560 matches(*argv, "lst") == 0 ||
561 matches(*argv, "list") == 0)
562 return do_show(argc-1, argv+1);
563 } else
564 return do_show(0, NULL);
566 usage();
570 int preferred_family = AF_UNSPEC;
571 int show_stats = 0;
572 int resolve_hosts = 0;
574 int main(int argc, char **argv)
576 char *basename;
578 #if I18N
579 bindtextdomain("net-tools", "/usr/share/locale");
580 textdomain("net-tools");
581 #endif
583 basename = strrchr(argv[0], '/');
584 if (basename == NULL)
585 basename = argv[0];
586 else
587 basename++;
589 while (argc > 1) {
590 if (argv[1][0] != '-')
591 break;
592 if (matches(argv[1], "-family") == 0) {
593 argc--;
594 argv++;
595 if (argc <= 1)
596 usage();
597 if (strcmp(argv[1], "inet") == 0)
598 preferred_family = AF_INET;
599 else if (strcmp(argv[1], "inet6") == 0)
600 preferred_family = AF_INET6;
601 else
602 usage();
603 } else if (matches(argv[1], "-stats") == 0 ||
604 matches(argv[1], "-statistics") == 0) {
605 ++show_stats;
606 } else if (matches(argv[1], "-resolve") == 0) {
607 ++resolve_hosts;
608 } else if ((matches(argv[1], "-V") == 0) || (matches(argv[1], "--version") == 0)) {
609 version();
610 } else
611 usage();
612 argc--; argv++;
615 return do_iptunnel(argc-1, argv+1);