source: MondoRescue/branches/stable/mindi-busybox/networking/libiproute/iptunnel.c@ 821

Last change on this file since 821 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 12.8 KB
RevLine 
[821]1/*
2 * iptunnel.c "ip tunnel"
3 *
4 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
5 *
6 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
7 *
8 *
9 * Changes:
10 *
11 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
12 * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
13 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
14 */
15
16#include "libbb.h"
17#include <sys/socket.h>
18#include <sys/ioctl.h>
19
20#include <string.h>
21#include <unistd.h>
22
23#include <netinet/ip.h>
24
25#include <net/if.h>
26#include <net/if_arp.h>
27
28#include <asm/types.h>
29#ifndef __constant_htons
30#define __constant_htons htons
31#endif
32#include <linux/if_tunnel.h>
33
34#include "rt_names.h"
35#include "utils.h"
36#include "ip_common.h"
37
38
39static int do_ioctl_get_ifindex(char *dev)
40{
41 struct ifreq ifr;
42 int fd;
43
44 strcpy(ifr.ifr_name, dev);
45 fd = socket(AF_INET, SOCK_DGRAM, 0);
46 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
47 bb_perror_msg("ioctl");
48 return 0;
49 }
50 close(fd);
51 return ifr.ifr_ifindex;
52}
53
54static int do_ioctl_get_iftype(char *dev)
55{
56 struct ifreq ifr;
57 int fd;
58
59 strcpy(ifr.ifr_name, dev);
60 fd = socket(AF_INET, SOCK_DGRAM, 0);
61 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
62 bb_perror_msg("ioctl");
63 return -1;
64 }
65 close(fd);
66 return ifr.ifr_addr.sa_family;
67}
68
69
70static char *do_ioctl_get_ifname(int idx)
71{
72 static struct ifreq ifr;
73 int fd;
74
75 ifr.ifr_ifindex = idx;
76 fd = socket(AF_INET, SOCK_DGRAM, 0);
77 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
78 bb_perror_msg("ioctl");
79 return NULL;
80 }
81 close(fd);
82 return ifr.ifr_name;
83}
84
85
86
87static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
88{
89 struct ifreq ifr;
90 int fd;
91 int err;
92
93 strcpy(ifr.ifr_name, basedev);
94 ifr.ifr_ifru.ifru_data = (void*)p;
95 fd = socket(AF_INET, SOCK_DGRAM, 0);
96 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
97 if (err) {
98 bb_perror_msg("ioctl");
99 }
100 close(fd);
101 return err;
102}
103
104static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
105{
106 struct ifreq ifr;
107 int fd;
108 int err;
109
110 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
111 strcpy(ifr.ifr_name, p->name);
112 } else {
113 strcpy(ifr.ifr_name, basedev);
114 }
115 ifr.ifr_ifru.ifru_data = (void*)p;
116 fd = socket(AF_INET, SOCK_DGRAM, 0);
117 err = ioctl(fd, cmd, &ifr);
118 if (err) {
119 bb_perror_msg("ioctl");
120 }
121 close(fd);
122 return err;
123}
124
125static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
126{
127 struct ifreq ifr;
128 int fd;
129 int err;
130
131 if (p->name[0]) {
132 strcpy(ifr.ifr_name, p->name);
133 } else {
134 strcpy(ifr.ifr_name, basedev);
135 }
136 ifr.ifr_ifru.ifru_data = (void*)p;
137 fd = socket(AF_INET, SOCK_DGRAM, 0);
138 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
139 if (err) {
140 bb_perror_msg("ioctl");
141 }
142 close(fd);
143 return err;
144}
145
146static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
147{
148 int count = 0;
149 char medium[IFNAMSIZ];
150 memset(p, 0, sizeof(*p));
151 memset(&medium, 0, sizeof(medium));
152
153 p->iph.version = 4;
154 p->iph.ihl = 5;
155#ifndef IP_DF
156#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
157#endif
158 p->iph.frag_off = htons(IP_DF);
159
160 while (argc > 0) {
161 if (strcmp(*argv, "mode") == 0) {
162 NEXT_ARG();
163 if (strcmp(*argv, "ipip") == 0 ||
164 strcmp(*argv, "ip/ip") == 0) {
165 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
166 bb_error_msg("You managed to ask for more than one tunnel mode.");
167 exit(-1);
168 }
169 p->iph.protocol = IPPROTO_IPIP;
170 } else if (strcmp(*argv, "gre") == 0 ||
171 strcmp(*argv, "gre/ip") == 0) {
172 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
173 bb_error_msg("You managed to ask for more than one tunnel mode.");
174 exit(-1);
175 }
176 p->iph.protocol = IPPROTO_GRE;
177 } else if (strcmp(*argv, "sit") == 0 ||
178 strcmp(*argv, "ipv6/ip") == 0) {
179 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
180 bb_error_msg("You managed to ask for more than one tunnel mode.");
181 exit(-1);
182 }
183 p->iph.protocol = IPPROTO_IPV6;
184 } else {
185 bb_error_msg("Cannot guess tunnel mode.");
186 exit(-1);
187 }
188 } else if (strcmp(*argv, "key") == 0) {
189 unsigned uval;
190 NEXT_ARG();
191 p->i_flags |= GRE_KEY;
192 p->o_flags |= GRE_KEY;
193 if (strchr(*argv, '.'))
194 p->i_key = p->o_key = get_addr32(*argv);
195 else {
196 if (get_unsigned(&uval, *argv, 0)<0) {
197 bb_error_msg("invalid value of \"key\"");
198 exit(-1);
199 }
200 p->i_key = p->o_key = htonl(uval);
201 }
202 } else if (strcmp(*argv, "ikey") == 0) {
203 unsigned uval;
204 NEXT_ARG();
205 p->i_flags |= GRE_KEY;
206 if (strchr(*argv, '.'))
207 p->o_key = get_addr32(*argv);
208 else {
209 if (get_unsigned(&uval, *argv, 0)<0) {
210 bb_error_msg("invalid value of \"ikey\"");
211 exit(-1);
212 }
213 p->i_key = htonl(uval);
214 }
215 } else if (strcmp(*argv, "okey") == 0) {
216 unsigned uval;
217 NEXT_ARG();
218 p->o_flags |= GRE_KEY;
219 if (strchr(*argv, '.'))
220 p->o_key = get_addr32(*argv);
221 else {
222 if (get_unsigned(&uval, *argv, 0)<0) {
223 bb_error_msg("invalid value of \"okey\"");
224 exit(-1);
225 }
226 p->o_key = htonl(uval);
227 }
228 } else if (strcmp(*argv, "seq") == 0) {
229 p->i_flags |= GRE_SEQ;
230 p->o_flags |= GRE_SEQ;
231 } else if (strcmp(*argv, "iseq") == 0) {
232 p->i_flags |= GRE_SEQ;
233 } else if (strcmp(*argv, "oseq") == 0) {
234 p->o_flags |= GRE_SEQ;
235 } else if (strcmp(*argv, "csum") == 0) {
236 p->i_flags |= GRE_CSUM;
237 p->o_flags |= GRE_CSUM;
238 } else if (strcmp(*argv, "icsum") == 0) {
239 p->i_flags |= GRE_CSUM;
240 } else if (strcmp(*argv, "ocsum") == 0) {
241 p->o_flags |= GRE_CSUM;
242 } else if (strcmp(*argv, "nopmtudisc") == 0) {
243 p->iph.frag_off = 0;
244 } else if (strcmp(*argv, "pmtudisc") == 0) {
245 p->iph.frag_off = htons(IP_DF);
246 } else if (strcmp(*argv, "remote") == 0) {
247 NEXT_ARG();
248 if (strcmp(*argv, "any"))
249 p->iph.daddr = get_addr32(*argv);
250 } else if (strcmp(*argv, "local") == 0) {
251 NEXT_ARG();
252 if (strcmp(*argv, "any"))
253 p->iph.saddr = get_addr32(*argv);
254 } else if (strcmp(*argv, "dev") == 0) {
255 NEXT_ARG();
256 strncpy(medium, *argv, IFNAMSIZ-1);
257 } else if (strcmp(*argv, "ttl") == 0) {
258 unsigned uval;
259 NEXT_ARG();
260 if (strcmp(*argv, "inherit") != 0) {
261 if (get_unsigned(&uval, *argv, 0))
262 invarg(*argv, "TTL");
263 if (uval > 255)
264 invarg(*argv, "TTL must be <=255");
265 p->iph.ttl = uval;
266 }
267 } else if (strcmp(*argv, "tos") == 0 ||
268 matches(*argv, "dsfield") == 0) {
269 __u32 uval;
270 NEXT_ARG();
271 if (strcmp(*argv, "inherit") != 0) {
272 if (rtnl_dsfield_a2n(&uval, *argv))
273 invarg(*argv, "TOS");
274 p->iph.tos = uval;
275 } else
276 p->iph.tos = 1;
277 } else {
278 if (strcmp(*argv, "name") == 0) {
279 NEXT_ARG();
280 }
281 if (p->name[0])
282 duparg2("name", *argv);
283 strncpy(p->name, *argv, IFNAMSIZ);
284 if (cmd == SIOCCHGTUNNEL && count == 0) {
285 struct ip_tunnel_parm old_p;
286 memset(&old_p, 0, sizeof(old_p));
287 if (do_get_ioctl(*argv, &old_p))
288 return -1;
289 *p = old_p;
290 }
291 }
292 count++;
293 argc--; argv++;
294 }
295
296
297 if (p->iph.protocol == 0) {
298 if (memcmp(p->name, "gre", 3) == 0)
299 p->iph.protocol = IPPROTO_GRE;
300 else if (memcmp(p->name, "ipip", 4) == 0)
301 p->iph.protocol = IPPROTO_IPIP;
302 else if (memcmp(p->name, "sit", 3) == 0)
303 p->iph.protocol = IPPROTO_IPV6;
304 }
305
306 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
307 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
308 bb_error_msg("Keys are not allowed with ipip and sit.");
309 return -1;
310 }
311 }
312
313 if (medium[0]) {
314 p->link = do_ioctl_get_ifindex(medium);
315 if (p->link == 0)
316 return -1;
317 }
318
319 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
320 p->i_key = p->iph.daddr;
321 p->i_flags |= GRE_KEY;
322 }
323 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
324 p->o_key = p->iph.daddr;
325 p->o_flags |= GRE_KEY;
326 }
327 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
328 bb_error_msg("Broadcast tunnel requires a source address.");
329 return -1;
330 }
331 return 0;
332}
333
334
335static int do_add(int cmd, int argc, char **argv)
336{
337 struct ip_tunnel_parm p;
338
339 if (parse_args(argc, argv, cmd, &p) < 0)
340 return -1;
341
342 if (p.iph.ttl && p.iph.frag_off == 0) {
343 bb_error_msg("ttl != 0 and noptmudisc are incompatible");
344 return -1;
345 }
346
347 switch (p.iph.protocol) {
348 case IPPROTO_IPIP:
349 return do_add_ioctl(cmd, "tunl0", &p);
350 case IPPROTO_GRE:
351 return do_add_ioctl(cmd, "gre0", &p);
352 case IPPROTO_IPV6:
353 return do_add_ioctl(cmd, "sit0", &p);
354 default:
355 bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
356 return -1;
357 }
358 return -1;
359}
360
361static int do_del(int argc, char **argv)
362{
363 struct ip_tunnel_parm p;
364
365 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
366 return -1;
367
368 switch (p.iph.protocol) {
369 case IPPROTO_IPIP:
370 return do_del_ioctl("tunl0", &p);
371 case IPPROTO_GRE:
372 return do_del_ioctl("gre0", &p);
373 case IPPROTO_IPV6:
374 return do_del_ioctl("sit0", &p);
375 default:
376 return do_del_ioctl(p.name, &p);
377 }
378 return -1;
379}
380
381static void print_tunnel(struct ip_tunnel_parm *p)
382{
383 char s1[256];
384 char s2[256];
385 char s3[64];
386 char s4[64];
387
388 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
389 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
390 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
391 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
392
393 printf("%s: %s/ip remote %s local %s ",
394 p->name,
395 p->iph.protocol == IPPROTO_IPIP ? "ip" :
396 (p->iph.protocol == IPPROTO_GRE ? "gre" :
397 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
398 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
399 if (p->link) {
400 char *n = do_ioctl_get_ifname(p->link);
401 if (n)
402 printf(" dev %s ", n);
403 }
404 if (p->iph.ttl)
405 printf(" ttl %d ", p->iph.ttl);
406 else
407 printf(" ttl inherit ");
408 if (p->iph.tos) {
409 SPRINT_BUF(b1);
410 printf(" tos");
411 if (p->iph.tos&1)
412 printf(" inherit");
413 if (p->iph.tos&~1)
414 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
415 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
416 }
417 if (!(p->iph.frag_off&htons(IP_DF)))
418 printf(" nopmtudisc");
419
420 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
421 printf(" key %s", s3);
422 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
423 if (p->i_flags&GRE_KEY)
424 printf(" ikey %s ", s3);
425 if (p->o_flags&GRE_KEY)
426 printf(" okey %s ", s4);
427 }
428
429 if (p->i_flags&GRE_SEQ)
430 printf("%s Drop packets out of sequence.\n", _SL_);
431 if (p->i_flags&GRE_CSUM)
432 printf("%s Checksum in received packet is required.", _SL_);
433 if (p->o_flags&GRE_SEQ)
434 printf("%s Sequence packets on output.", _SL_);
435 if (p->o_flags&GRE_CSUM)
436 printf("%s Checksum output packets.", _SL_);
437}
438
439static int do_tunnels_list(struct ip_tunnel_parm *p)
440{
441 char name[IFNAMSIZ];
442 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
443 rx_fifo, rx_frame,
444 tx_bytes, tx_packets, tx_errs, tx_drops,
445 tx_fifo, tx_colls, tx_carrier, rx_multi;
446 int type;
447 struct ip_tunnel_parm p1;
448
449 char buf[512];
450 FILE *fp = fopen("/proc/net/dev", "r");
451 if (fp == NULL) {
452 perror("fopen");
453 return -1;
454 }
455
456 fgets(buf, sizeof(buf), fp);
457 fgets(buf, sizeof(buf), fp);
458
459 while (fgets(buf, sizeof(buf), fp) != NULL) {
460 char *ptr;
461 buf[sizeof(buf) - 1] = 0;
462 if ((ptr = strchr(buf, ':')) == NULL ||
463 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
464 bb_error_msg("Wrong format of /proc/net/dev. Sorry.");
465 return -1;
466 }
467 if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
468 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
469 &rx_fifo, &rx_frame, &rx_multi,
470 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
471 &tx_fifo, &tx_colls, &tx_carrier) != 14)
472 continue;
473 if (p->name[0] && strcmp(p->name, name))
474 continue;
475 type = do_ioctl_get_iftype(name);
476 if (type == -1) {
477 bb_error_msg("Failed to get type of [%s]", name);
478 continue;
479 }
480 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
481 continue;
482 memset(&p1, 0, sizeof(p1));
483 if (do_get_ioctl(name, &p1))
484 continue;
485 if ((p->link && p1.link != p->link) ||
486 (p->name[0] && strcmp(p1.name, p->name)) ||
487 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
488 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
489 (p->i_key && p1.i_key != p->i_key))
490 continue;
491 print_tunnel(&p1);
492 printf("\n");
493 }
494 return 0;
495}
496
497static int do_show(int argc, char **argv)
498{
499 int err;
500 struct ip_tunnel_parm p;
501
502 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
503 return -1;
504
505 switch (p.iph.protocol) {
506 case IPPROTO_IPIP:
507 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
508 break;
509 case IPPROTO_GRE:
510 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
511 break;
512 case IPPROTO_IPV6:
513 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
514 break;
515 default:
516 do_tunnels_list(&p);
517 return 0;
518 }
519 if (err)
520 return -1;
521
522 print_tunnel(&p);
523 printf("\n");
524 return 0;
525}
526
527int do_iptunnel(int argc, char **argv)
528{
529 if (argc > 0) {
530 if (matches(*argv, "add") == 0)
531 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
532 if (matches(*argv, "change") == 0)
533 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
534 if (matches(*argv, "del") == 0)
535 return do_del(argc-1, argv+1);
536 if (matches(*argv, "show") == 0 ||
537 matches(*argv, "lst") == 0 ||
538 matches(*argv, "list") == 0)
539 return do_show(argc-1, argv+1);
540 } else
541 return do_show(0, NULL);
542
543 bb_error_msg("Command \"%s\" is unknown.", *argv);
544 exit(-1);
545}
Note: See TracBrowser for help on using the repository browser.