This commit was manufactured by cvs2svn to create tag 'busybox_1_00'.
[oweals/busybox.git] / busybox / networking / libiproute / iptunnel.c
1 /*
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 <sys/socket.h>
20 #include <sys/ioctl.h>
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <arpa/inet.h>
27 #include <netinet/ip.h>
28 #include <netinet/in.h>
29
30 #include <net/if.h>
31 #include <net/if_arp.h>
32
33 #include <asm/types.h>
34 #define __constant_htons htons
35 #include <linux/if_tunnel.h>
36
37 #include "rt_names.h"
38 #include "utils.h"
39
40 #include "libbb.h"
41
42 static int do_ioctl_get_ifindex(char *dev)
43 {
44         struct ifreq ifr;
45         int fd;
46
47         strcpy(ifr.ifr_name, dev);
48         fd = socket(AF_INET, SOCK_DGRAM, 0);
49         if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
50                 bb_perror_msg("ioctl");
51                 return 0;
52         }
53         close(fd);
54         return ifr.ifr_ifindex;
55 }
56
57 static int do_ioctl_get_iftype(char *dev)
58 {
59         struct ifreq ifr;
60         int fd;
61
62         strcpy(ifr.ifr_name, dev);
63         fd = socket(AF_INET, SOCK_DGRAM, 0);
64         if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
65                 bb_perror_msg("ioctl");
66                 return -1;
67         }
68         close(fd);
69         return ifr.ifr_addr.sa_family;
70 }
71
72
73 static char *do_ioctl_get_ifname(int idx)
74 {
75         static struct ifreq ifr;
76         int fd;
77
78         ifr.ifr_ifindex = idx;
79         fd = socket(AF_INET, SOCK_DGRAM, 0);
80         if (ioctl(fd, SIOCGIFNAME, &ifr)) {
81                 bb_perror_msg("ioctl");
82                 return NULL;
83         }
84         close(fd);
85         return ifr.ifr_name;
86 }
87
88
89
90 static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
91 {
92         struct ifreq ifr;
93         int fd;
94         int err;
95
96         strcpy(ifr.ifr_name, basedev);
97         ifr.ifr_ifru.ifru_data = (void*)p;
98         fd = socket(AF_INET, SOCK_DGRAM, 0);
99         err = ioctl(fd, SIOCGETTUNNEL, &ifr);
100         if (err) {
101                 bb_perror_msg("ioctl");
102         }
103         close(fd);
104         return err;
105 }
106
107 static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
108 {
109         struct ifreq ifr;
110         int fd;
111         int err;
112
113         if (cmd == SIOCCHGTUNNEL && p->name[0]) {
114                 strcpy(ifr.ifr_name, p->name);
115         } else {
116                 strcpy(ifr.ifr_name, basedev);
117         }
118         ifr.ifr_ifru.ifru_data = (void*)p;
119         fd = socket(AF_INET, SOCK_DGRAM, 0);
120         err = ioctl(fd, cmd, &ifr);
121         if (err) {
122                 bb_perror_msg("ioctl");
123         }
124         close(fd);
125         return err;
126 }
127
128 static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
129 {
130         struct ifreq ifr;
131         int fd;
132         int err;
133
134         if (p->name[0]) {
135                 strcpy(ifr.ifr_name, p->name);
136         } else {
137                 strcpy(ifr.ifr_name, basedev);
138         }
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                 bb_perror_msg("ioctl");
144         }
145         close(fd);
146         return err;
147 }
148
149 static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
150 {
151         int count = 0;
152         char medium[IFNAMSIZ];
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                                         bb_error_msg("You managed to ask for more than one tunnel mode.");
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                                         bb_error_msg("You managed to ask for more than one tunnel mode.");
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                                         bb_error_msg("You managed to ask for more than one tunnel mode.");
184                                         exit(-1);
185                                 }
186                                 p->iph.protocol = IPPROTO_IPV6;
187                         } else {
188                                 bb_error_msg("Cannot guess tunnel mode.");
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                                         bb_error_msg("invalid value of \"key\"");
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                                         bb_error_msg("invalid value of \"ikey\"");
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                                         bb_error_msg("invalid value of \"okey\"");
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                         bb_error_msg("Keys are not allowed with ipip and sit.");
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                 bb_error_msg("Broadcast tunnel requires a source address.");
332                 return -1;
333         }
334         return 0;
335 }
336
337
338 static 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                 bb_error_msg("ttl != 0 and noptmudisc are incompatible");
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                 bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
359                 return -1;
360         }
361         return -1;
362 }
363
364 int 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
384 void 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
442 static 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                         bb_error_msg("Wrong format of /proc/net/dev. Sorry.");
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                         bb_error_msg("Failed to get type of [%s]", 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
500 static 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
530 int 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         bb_error_msg("Command \"%s\" is unknown.", *argv);
547         exit(-1);
548 }