blob: 58ec7cce67f07d27aa7986e4a72255202e4913fe [file] [log] [blame]
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00001/*
2 * iptunnel.c "ip tunnel"
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 *
12 * Changes:
13 *
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 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
17 */
18
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 <netinet/in.h>
28#include <netinet/ip.h>
29#include <arpa/inet.h>
30
31#include <linux/if.h>
32#include <linux/if_arp.h>
33#include <linux/if_tunnel.h>
34
35#include "rt_names.h"
36#include "utils.h"
37
38#include "busybox.h"
39
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
45 strcpy(ifr.ifr_name, dev);
46 fd = socket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000047 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
48 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
60 strcpy(ifr.ifr_name, dev);
61 fd = socket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000062 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
63 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;
77 fd = socket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000078 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
79 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
94 strcpy(ifr.ifr_name, basedev);
95 ifr.ifr_ifru.ifru_data = (void*)p;
96 fd = socket(AF_INET, SOCK_DGRAM, 0);
97 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +000098 if (err) {
99 perror_msg("ioctl");
100 }
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]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000112 strcpy(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000113 } else {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000114 strcpy(ifr.ifr_name, basedev);
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;
117 fd = socket(AF_INET, SOCK_DGRAM, 0);
118 err = ioctl(fd, cmd, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000119 if (err) {
120 perror_msg("ioctl");
121 }
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]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133 strcpy(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000134 } else {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000135 strcpy(ifr.ifr_name, basedev);
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;
138 fd = socket(AF_INET, SOCK_DGRAM, 0);
139 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000140 if (err) {
141 perror_msg("ioctl");
142 }
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) {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000167 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) {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000174 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) {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000181 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 {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000186 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) {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000198 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) {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000211 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) {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000224 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))
263 invarg("invalid TTL\n", *argv);
264 if (uval > 255)
265 invarg("TTL must be <=255\n", *argv);
266 p->iph.ttl = uval;
267 }
268 } else if (strcmp(*argv, "tos") == 0 ||
269 matches(*argv, "dsfield") == 0) {
270 __u32 uval;
271 NEXT_ARG();
272 if (strcmp(*argv, "inherit") != 0) {
273 if (rtnl_dsfield_a2n(&uval, *argv))
274 invarg("bad TOS value", *argv);
275 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)) {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000309 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) {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000329 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) {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000344 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);
355 default:
Glenn L McGrath8b602442002-11-28 12:19:51 +0000356 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
362int do_del(int argc, char **argv)
363{
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);
376 default:
377 return do_del_ioctl(p.name, &p);
378 }
379 return -1;
380}
381
382void print_tunnel(struct ip_tunnel_parm *p)
383{
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)) {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000465 error_msg("Wrong format of /proc/net/dev. Sorry.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000466 return -1;
467 }
468 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
469 &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) {
Glenn L McGrath8b602442002-11-28 12:19:51 +0000478 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);
493 printf("\n");
494 }
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) {
507 case IPPROTO_IPIP:
508 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);
524 printf("\n");
525 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
Glenn L McGrath8b602442002-11-28 12:19:51 +0000544 error_msg("Command \"%s\" is unknown, try \"ip tunnel help\".", *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000545 exit(-1);
546}