- remove matches() from networking/. Untested.
[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         static const char * const keywords[] = {
141                 "mode", "ipip", "ip/ip", "gre", "gre/ip", "sit", "ipv6/ip",
142                 "key", "ikey", "okey", "seq", "iseq", "oseq",
143                 "csum", "icsum", "ocsum", "nopmtudisc", "pmtudisc",
144                 "remote", "any", "local", "dev",
145                 "ttl", "inherit", "tos", "dsfield",
146                 "name", NULL
147         };
148         enum {
149                 ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip,
150                 ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq,
151                 ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc,
152                 ARG_remote, ARG_any, ARG_local, ARG_dev,
153                 ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield,
154                 ARG_name
155         };
156         int key;
157         memset(p, 0, sizeof(*p));
158         memset(&medium, 0, sizeof(medium));
159
160         p->iph.version = 4;
161         p->iph.ihl = 5;
162 #ifndef IP_DF
163 #define IP_DF           0x4000          /* Flag: "Don't Fragment"       */
164 #endif
165         p->iph.frag_off = htons(IP_DF);
166
167         while (argc > 0) {
168                 key = index_in_str_array(keywords, *argv);
169                 if (key == ARG_mode) {
170                         NEXT_ARG();
171                         key = index_in_str_array(keywords, *argv);
172                         if (key == ARG_ipip ||
173                             key == ARG_ip_ip) {
174                                 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
175                                         bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
176                                 }
177                                 p->iph.protocol = IPPROTO_IPIP;
178                         } else if (key == ARG_gre ||
179                                    key == ARG_gre_ip) {
180                                 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
181                                         bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
182                                 }
183                                 p->iph.protocol = IPPROTO_GRE;
184                         } else if (key == ARG_sit ||
185                                    key == ARG_ip6_ip) {
186                                 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
187                                         bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
188                                 }
189                                 p->iph.protocol = IPPROTO_IPV6;
190                         } else {
191                                 bb_error_msg_and_die("cannot guess tunnel mode");
192                         }
193                 } else if (key == ARG_key) {
194                         unsigned uval;
195                         NEXT_ARG();
196                         p->i_flags |= GRE_KEY;
197                         p->o_flags |= GRE_KEY;
198                         if (strchr(*argv, '.'))
199                                 p->i_key = p->o_key = get_addr32(*argv);
200                         else {
201                                 if (get_unsigned(&uval, *argv, 0)<0) {
202                                         bb_error_msg_and_die("invalid value of \"key\"");
203                                 }
204                                 p->i_key = p->o_key = htonl(uval);
205                         }
206                 } else if (key == ARG_ikey) {
207                         unsigned uval;
208                         NEXT_ARG();
209                         p->i_flags |= GRE_KEY;
210                         if (strchr(*argv, '.'))
211                                 p->o_key = get_addr32(*argv);
212                         else {
213                                 if (get_unsigned(&uval, *argv, 0)<0) {
214                                         bb_error_msg_and_die("invalid value of \"ikey\"");
215                                 }
216                                 p->i_key = htonl(uval);
217                         }
218                 } else if (key == ARG_okey) {
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_and_die("invalid value of \"okey\"");
227                                 }
228                                 p->o_key = htonl(uval);
229                         }
230                 } else if (key == ARG_seq) {
231                         p->i_flags |= GRE_SEQ;
232                         p->o_flags |= GRE_SEQ;
233                 } else if (key == ARG_iseq) {
234                         p->i_flags |= GRE_SEQ;
235                 } else if (key == ARG_oseq) {
236                         p->o_flags |= GRE_SEQ;
237                 } else if (key == ARG_csum) {
238                         p->i_flags |= GRE_CSUM;
239                         p->o_flags |= GRE_CSUM;
240                 } else if (key == ARG_icsum) {
241                         p->i_flags |= GRE_CSUM;
242                 } else if (key == ARG_ocsum) {
243                         p->o_flags |= GRE_CSUM;
244                 } else if (key == ARG_nopmtudisc) {
245                         p->iph.frag_off = 0;
246                 } else if (key == ARG_pmtudisc) {
247                         p->iph.frag_off = htons(IP_DF);
248                 } else if (key == ARG_remote) {
249                         NEXT_ARG();
250                         key = index_in_str_array(keywords, *argv);
251                         if (key == ARG_any)
252                                 p->iph.daddr = get_addr32(*argv);
253                 } else if (key == ARG_local) {
254                         NEXT_ARG();
255                         key = index_in_str_array(keywords, *argv);
256                         if (key == ARG_any)
257                                 p->iph.saddr = get_addr32(*argv);
258                 } else if (key == ARG_dev) {
259                         NEXT_ARG();
260                         strncpy(medium, *argv, IFNAMSIZ-1);
261                 } else if (key == ARG_ttl) {
262                         unsigned uval;
263                         NEXT_ARG();
264                         key = index_in_str_array(keywords, *argv);
265                         if (key != ARG_inherit) {
266                                 if (get_unsigned(&uval, *argv, 0))
267                                         invarg(*argv, "TTL");
268                                 if (uval > 255)
269                                         invarg(*argv, "TTL must be <=255");
270                                 p->iph.ttl = uval;
271                         }
272                 } else if (key == ARG_tos ||
273                            key == ARG_dsfield) {
274                         uint32_t uval;
275                         NEXT_ARG();
276                         key = index_in_str_array(keywords, *argv);
277                         if (key != ARG_inherit) {
278                                 if (rtnl_dsfield_a2n(&uval, *argv))
279                                         invarg(*argv, "TOS");
280                                 p->iph.tos = uval;
281                         } else
282                                 p->iph.tos = 1;
283                 } else {
284                         if (key == ARG_name) {
285                                 NEXT_ARG();
286                         }
287                         if (p->name[0])
288                                 duparg2("name", *argv);
289                         strncpy(p->name, *argv, IFNAMSIZ);
290                         if (cmd == SIOCCHGTUNNEL && count == 0) {
291                                 struct ip_tunnel_parm old_p;
292                                 memset(&old_p, 0, sizeof(old_p));
293                                 if (do_get_ioctl(*argv, &old_p))
294                                         exit(1);
295                                 *p = old_p;
296                         }
297                 }
298                 count++;
299                 argc--;
300                 argv++;
301         }
302
303         if (p->iph.protocol == 0) {
304                 if (memcmp(p->name, "gre", 3) == 0)
305                         p->iph.protocol = IPPROTO_GRE;
306                 else if (memcmp(p->name, "ipip", 4) == 0)
307                         p->iph.protocol = IPPROTO_IPIP;
308                 else if (memcmp(p->name, "sit", 3) == 0)
309                         p->iph.protocol = IPPROTO_IPV6;
310         }
311
312         if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
313                 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
314                         bb_error_msg_and_die("keys are not allowed with ipip and sit");
315                 }
316         }
317
318         if (medium[0]) {
319                 p->link = do_ioctl_get_ifindex(medium);
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_and_die("broadcast tunnel requires a source address");
332         }
333 }
334
335
336 /* Return value becomes exitcode. It's okay to not return at all */
337 static int do_add(int cmd, int argc, char **argv)
338 {
339         struct ip_tunnel_parm p;
340
341         parse_args(argc, argv, cmd, &p);
342
343         if (p.iph.ttl && p.iph.frag_off == 0) {
344                 bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
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_and_die("cannot determine tunnel mode (ipip, gre or sit)");
356         }
357 }
358
359 /* Return value becomes exitcode. It's okay to not return at all */
360 static int do_del(int argc, char **argv)
361 {
362         struct ip_tunnel_parm p;
363
364         parse_args(argc, argv, SIOCDELTUNNEL, &p);
365
366         switch (p.iph.protocol) {
367         case IPPROTO_IPIP:
368                 return do_del_ioctl("tunl0", &p);
369         case IPPROTO_GRE:
370                 return do_del_ioctl("gre0", &p);
371         case IPPROTO_IPV6:
372                 return do_del_ioctl("sit0", &p);
373         default:
374                 return do_del_ioctl(p.name, &p);
375         }
376 }
377
378 static void print_tunnel(struct ip_tunnel_parm *p)
379 {
380         char s1[256];
381         char s2[256];
382         char s3[64];
383         char s4[64];
384
385         format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
386         format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
387         inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
388         inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
389
390         printf("%s: %s/ip  remote %s  local %s ",
391                p->name,
392                p->iph.protocol == IPPROTO_IPIP ? "ip" :
393                (p->iph.protocol == IPPROTO_GRE ? "gre" :
394                 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
395                p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
396         if (p->link) {
397                 char *n = do_ioctl_get_ifname(p->link);
398                 if (n) {
399                         printf(" dev %s ", n);
400                         free(n);
401                 }
402         }
403         if (p->iph.ttl)
404                 printf(" ttl %d ", p->iph.ttl);
405         else
406                 printf(" ttl inherit ");
407         if (p->iph.tos) {
408                 SPRINT_BUF(b1);
409                 printf(" tos");
410                 if (p->iph.tos & 1)
411                         printf(" inherit");
412                 if (p->iph.tos & ~1)
413                         printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
414                                rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
415         }
416         if (!(p->iph.frag_off & htons(IP_DF)))
417                 printf(" nopmtudisc");
418
419         if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
420                 printf(" key %s", s3);
421         else if ((p->i_flags | p->o_flags) & GRE_KEY) {
422                 if (p->i_flags & GRE_KEY)
423                         printf(" ikey %s ", s3);
424                 if (p->o_flags & GRE_KEY)
425                         printf(" okey %s ", s4);
426         }
427
428         if (p->i_flags & GRE_SEQ)
429                 printf("%c  Drop packets out of sequence.\n", _SL_);
430         if (p->i_flags & GRE_CSUM)
431                 printf("%c  Checksum in received packet is required.", _SL_);
432         if (p->o_flags & GRE_SEQ)
433                 printf("%c  Sequence packets on output.", _SL_);
434         if (p->o_flags & GRE_CSUM)
435                 printf("%c  Checksum output packets.", _SL_);
436 }
437
438 static void do_tunnels_list(struct ip_tunnel_parm *p)
439 {
440         char name[IFNAMSIZ];
441         unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
442                 rx_fifo, rx_frame,
443                 tx_bytes, tx_packets, tx_errs, tx_drops,
444                 tx_fifo, tx_colls, tx_carrier, rx_multi;
445         int type;
446         struct ip_tunnel_parm p1;
447         char buf[512];
448         FILE *fp = fopen_or_warn("/proc/net/dev", "r");
449
450         if (fp == NULL) {
451                 return;
452         }
453
454         fgets(buf, sizeof(buf), fp);
455         fgets(buf, sizeof(buf), fp);
456
457         while (fgets(buf, sizeof(buf), fp) != NULL) {
458                 char *ptr;
459
460                 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
461                 ptr = strchr(buf, ':');
462                 if (ptr == NULL ||
463                     (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
464                         bb_error_msg("wrong format of /proc/net/dev");
465                         return;
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("cannot 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                 puts("");
493         }
494 }
495
496 /* Return value becomes exitcode. It's okay to not return at all */
497 static int do_show(int argc, char **argv)
498 {
499         int err;
500         struct ip_tunnel_parm p;
501
502         parse_args(argc, argv, SIOCGETTUNNEL, &p);
503
504         switch (p.iph.protocol) {
505         case IPPROTO_IPIP:
506                 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
507                 break;
508         case IPPROTO_GRE:
509                 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
510                 break;
511         case IPPROTO_IPV6:
512                 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
513                 break;
514         default:
515                 do_tunnels_list(&p);
516                 return 0;
517         }
518         if (err)
519                 return -1;
520
521         print_tunnel(&p);
522         puts("");
523         return 0;
524 }
525
526 /* Return value becomes exitcode. It's okay to not return at all */
527 int do_iptunnel(int argc, char **argv)
528 {
529         static const char * const keywords[] = {
530                 "add", "change", "delete", "show", "list", "lst", NULL
531         };
532         enum {ARG_add = 1, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst};
533         smalluint key = 4; /* show */
534         if (argc > 0) {
535                 key = index_in_substr_array(keywords, *argv) +1;
536                 --argc;
537                 ++argv;
538         } else
539                 return do_show(0, NULL);
540         if (key < ARG_add)
541  bail:
542                 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
543
544         if (key == ARG_add)
545                 return do_add(SIOCADDTUNNEL, argc, argv);
546         if (key == ARG_change)
547                 return do_add(SIOCCHGTUNNEL, argc, argv);
548         if (key == ARG_del)
549                 return do_del(argc, argv);
550         if (key == ARG_show || key == ARG_list || key == ARG_lst)
551                 return do_show(argc, argv);
552         /* be gentle to gcc; avoid warning about non returning */
553         goto bail; /* never reached */
554 }