blob: beb8bb6757dccda71f7a6c1581f8a794d2d47dcf [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;
44 int err;
45
46 strcpy(ifr.ifr_name, dev);
47 fd = socket(AF_INET, SOCK_DGRAM, 0);
48 err = ioctl(fd, SIOCGIFINDEX, &ifr);
49 if (err) {
50 perror("ioctl");
51 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;
61 int err;
62
63 strcpy(ifr.ifr_name, dev);
64 fd = socket(AF_INET, SOCK_DGRAM, 0);
65 err = ioctl(fd, SIOCGIFHWADDR, &ifr);
66 if (err) {
67 perror("ioctl");
68 return -1;
69 }
70 close(fd);
71 return ifr.ifr_addr.sa_family;
72}
73
74
75static char * do_ioctl_get_ifname(int idx)
76{
77 static struct ifreq ifr;
78 int fd;
79 int err;
80
81 ifr.ifr_ifindex = idx;
82 fd = socket(AF_INET, SOCK_DGRAM, 0);
83 err = ioctl(fd, SIOCGIFNAME, &ifr);
84 if (err) {
85 perror("ioctl");
86 return NULL;
87 }
88 close(fd);
89 return ifr.ifr_name;
90}
91
92
93
94static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
95{
96 struct ifreq ifr;
97 int fd;
98 int err;
99
100 strcpy(ifr.ifr_name, basedev);
101 ifr.ifr_ifru.ifru_data = (void*)p;
102 fd = socket(AF_INET, SOCK_DGRAM, 0);
103 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
104 if (err)
105 perror("ioctl");
106 close(fd);
107 return err;
108}
109
110static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
111{
112 struct ifreq ifr;
113 int fd;
114 int err;
115
116 if (cmd == SIOCCHGTUNNEL && p->name[0])
117 strcpy(ifr.ifr_name, p->name);
118 else
119 strcpy(ifr.ifr_name, basedev);
120 ifr.ifr_ifru.ifru_data = (void*)p;
121 fd = socket(AF_INET, SOCK_DGRAM, 0);
122 err = ioctl(fd, cmd, &ifr);
123 if (err)
124 perror("ioctl");
125 close(fd);
126 return err;
127}
128
129static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
130{
131 struct ifreq ifr;
132 int fd;
133 int err;
134
135 if (p->name[0])
136 strcpy(ifr.ifr_name, p->name);
137 else
138 strcpy(ifr.ifr_name, basedev);
139 ifr.ifr_ifru.ifru_data = (void*)p;
140 fd = socket(AF_INET, SOCK_DGRAM, 0);
141 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
142 if (err)
143 perror("ioctl");
144 close(fd);
145 return err;
146}
147
148static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
149{
150 int count = 0;
151 char medium[IFNAMSIZ];
152
153 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) {
169 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
170 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) {
176 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
177 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) {
183 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
184 exit(-1);
185 }
186 p->iph.protocol = IPPROTO_IPV6;
187 } else {
188 fprintf(stderr,"Cannot guess tunnel mode.\n");
189 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) {
200 fprintf(stderr, "invalid value of \"key\"\n");
201 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) {
213 fprintf(stderr, "invalid value of \"ikey\"\n");
214 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) {
226 fprintf(stderr, "invalid value of \"okey\"\n");
227 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)) {
311 fprintf(stderr, "Keys are not allowed with ipip and sit.\n");
312 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) {
331 fprintf(stderr, "Broadcast tunnel requires a source address.\n");
332 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) {
346 fprintf(stderr, "ttl != 0 and noptmudisc are incompatible\n");
347 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);
357 default:
358 fprintf(stderr, "cannot determine tunnel mode (ipip, gre or sit)\n");
359 return -1;
360 }
361 return -1;
362}
363
364int do_del(int argc, char **argv)
365{
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);
378 default:
379 return do_del_ioctl(p.name, &p);
380 }
381 return -1;
382}
383
384void print_tunnel(struct ip_tunnel_parm *p)
385{
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)) {
467 fprintf(stderr, "Wrong format of /proc/net/dev. Sorry.\n");
468 return -1;
469 }
470 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
471 &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) {
480 fprintf(stderr, "Failed to get type of [%s]\n", name);
481 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) {
509 case IPPROTO_IPIP:
510 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
546 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv);
547 exit(-1);
548}