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