libiproute: handle table ids larger than 255
[oweals/busybox.git] / networking / libiproute / iptunnel.c
index a2933879c267f2825c2987123709b5601ed1194c..eb136e435d837d9b46ccae1d608aeb887b80a977 100644 (file)
@@ -1,27 +1,66 @@
 /* vi: set sw=4 ts=4: */
 /*
- * iptunnel.c         "ip tunnel"
- *
- * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
- *
- * Authors:    Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  *
+ * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  *
  * Changes:
  *
- * Rani Assaf <rani@magic.metawire.com> 980929:        resolve addresses
- * Rani Assaf <rani@magic.metawire.com> 980930:        do not allow key for ipip/sit
- * Phil Karn <karn@ka9q.ampr.org>      990408: "pmtudisc" flag
+ * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
+ * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
+ * Phil Karn <karn@ka9q.ampr.org>       990408: "pmtudisc" flag
  */
 
 #include <netinet/ip.h>
 #include <net/if.h>
 #include <net/if_arp.h>
 #include <asm/types.h>
+
 #ifndef __constant_htons
 #define __constant_htons htons
 #endif
-#include <linux/if_tunnel.h>
+
+// FYI: #define SIOCDEVPRIVATE 0x89F0
+
+/* From linux/if_tunnel.h. #including it proved troublesome
+ * (redefiniton errors due to name collisions in linux/ and net[inet]/) */
+#define SIOCGETTUNNEL   (SIOCDEVPRIVATE + 0)
+#define SIOCADDTUNNEL   (SIOCDEVPRIVATE + 1)
+#define SIOCDELTUNNEL   (SIOCDEVPRIVATE + 2)
+#define SIOCCHGTUNNEL   (SIOCDEVPRIVATE + 3)
+//#define SIOCGETPRL      (SIOCDEVPRIVATE + 4)
+//#define SIOCADDPRL      (SIOCDEVPRIVATE + 5)
+//#define SIOCDELPRL      (SIOCDEVPRIVATE + 6)
+//#define SIOCCHGPRL      (SIOCDEVPRIVATE + 7)
+#define GRE_CSUM        __constant_htons(0x8000)
+//#define GRE_ROUTING     __constant_htons(0x4000)
+#define GRE_KEY         __constant_htons(0x2000)
+#define GRE_SEQ         __constant_htons(0x1000)
+//#define GRE_STRICT      __constant_htons(0x0800)
+//#define GRE_REC         __constant_htons(0x0700)
+//#define GRE_FLAGS       __constant_htons(0x00F8)
+//#define GRE_VERSION     __constant_htons(0x0007)
+struct ip_tunnel_parm {
+       char            name[IFNAMSIZ];
+       int             link;
+       uint16_t        i_flags;
+       uint16_t        o_flags;
+       uint32_t        i_key;
+       uint32_t        o_key;
+       struct iphdr    iph;
+};
+/* SIT-mode i_flags */
+//#define SIT_ISATAP 0x0001
+//struct ip_tunnel_prl {
+//     uint32_t          addr;
+//     uint16_t          flags;
+//     uint16_t          __reserved;
+//     uint32_t          datalen;
+//     uint32_t          __reserved2;
+//     /* data follows */
+//};
+///* PRL flags */
+//#define PRL_DEFAULT 0x0001
 
 #include "ip_common.h"  /* #include "libbb.h" is inside */
 #include "rt_names.h"
@@ -34,7 +73,7 @@ static int do_ioctl_get_ifindex(char *dev)
        struct ifreq ifr;
        int fd;
 
-       strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
+       strncpy_IFNAMSIZ(ifr.ifr_name, dev);
        fd = xsocket(AF_INET, SOCK_DGRAM, 0);
        xioctl(fd, SIOCGIFINDEX, &ifr);
        close(fd);
@@ -47,7 +86,7 @@ static int do_ioctl_get_iftype(char *dev)
        int fd;
        int err;
 
-       strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
+       strncpy_IFNAMSIZ(ifr.ifr_name, dev);
        fd = xsocket(AF_INET, SOCK_DGRAM, 0);
        err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
        close(fd);
@@ -73,7 +112,7 @@ static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
        int fd;
        int err;
 
-       strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
+       strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
        ifr.ifr_ifru.ifru_data = (void*)p;
        fd = xsocket(AF_INET, SOCK_DGRAM, 0);
        err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
@@ -88,9 +127,9 @@ static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
        int fd;
 
        if (cmd == SIOCCHGTUNNEL && p->name[0]) {
-               strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
+               strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
        } else {
-               strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
+               strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
        }
        ifr.ifr_ifru.ifru_data = (void*)p;
        fd = xsocket(AF_INET, SOCK_DGRAM, 0);
@@ -114,9 +153,9 @@ static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
        int fd;
 
        if (p->name[0]) {
-               strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
+               strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
        } else {
-               strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
+               strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
        }
        ifr.ifr_ifru.ifru_data = (void*)p;
        fd = xsocket(AF_INET, SOCK_DGRAM, 0);
@@ -126,9 +165,9 @@ static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
 }
 
 /* Dies on error */
-static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
+static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
 {
-       static const char keywords[] =
+       static const char keywords[] ALIGN1 =
                "mode\0""ipip\0""ip/ip\0""gre\0""gre/ip\0""sit\0""ipv6/ip\0"
                "key\0""ikey\0""okey\0""seq\0""iseq\0""oseq\0"
                "csum\0""icsum\0""ocsum\0""nopmtudisc\0""pmtudisc\0"
@@ -148,7 +187,7 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
        int key;
 
        memset(p, 0, sizeof(*p));
-       memset(&medium, 0, sizeof(medium));
+       medium[0] = '\0';
 
        p->iph.version = 4;
        p->iph.ihl = 5;
@@ -157,31 +196,34 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
 #endif
        p->iph.frag_off = htons(IP_DF);
 
-       while (argc > 0) {
+       while (*argv) {
                key = index_in_strings(keywords, *argv);
                if (key == ARG_mode) {
                        NEXT_ARG();
                        key = index_in_strings(keywords, *argv);
                        if (key == ARG_ipip ||
-                           key == ARG_ip_ip) {
+                           key == ARG_ip_ip
+                       ) {
                                if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
-                                       bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
+                                       bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
                                }
                                p->iph.protocol = IPPROTO_IPIP;
                        } else if (key == ARG_gre ||
-                                  key == ARG_gre_ip) {
+                                  key == ARG_gre_ip
+                       ) {
                                if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
-                                       bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
+                                       bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
                                }
                                p->iph.protocol = IPPROTO_GRE;
                        } else if (key == ARG_sit ||
-                                  key == ARG_ip6_ip) {
+                                  key == ARG_ip6_ip
+                       ) {
                                if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
-                                       bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
+                                       bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
                                }
                                p->iph.protocol = IPPROTO_IPV6;
                        } else {
-                               bb_error_msg_and_die("cannot guess tunnel mode");
+                               bb_error_msg_and_die("%s tunnel mode", "can't guess");
                        }
                } else if (key == ARG_key) {
                        unsigned uval;
@@ -191,9 +233,7 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
                        if (strchr(*argv, '.'))
                                p->i_key = p->o_key = get_addr32(*argv);
                        else {
-                               if (get_unsigned(&uval, *argv, 0)<0) {
-                                       bb_error_msg_and_die("invalid value of \"key\"");
-                               }
+                               uval = get_unsigned(*argv, "key");
                                p->i_key = p->o_key = htonl(uval);
                        }
                } else if (key == ARG_ikey) {
@@ -203,9 +243,7 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
                        if (strchr(*argv, '.'))
                                p->o_key = get_addr32(*argv);
                        else {
-                               if (get_unsigned(&uval, *argv, 0)<0) {
-                                       bb_error_msg_and_die("invalid value of \"ikey\"");
-                               }
+                               uval = get_unsigned(*argv, "ikey");
                                p->i_key = htonl(uval);
                        }
                } else if (key == ARG_okey) {
@@ -215,9 +253,7 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
                        if (strchr(*argv, '.'))
                                p->o_key = get_addr32(*argv);
                        else {
-                               if (get_unsigned(&uval, *argv, 0)<0) {
-                                       bb_error_msg_and_die("invalid value of \"okey\"");
-                               }
+                               uval = get_unsigned(*argv, "okey");
                                p->o_key = htonl(uval);
                        }
                } else if (key == ARG_seq) {
@@ -241,35 +277,35 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
                } else if (key == ARG_remote) {
                        NEXT_ARG();
                        key = index_in_strings(keywords, *argv);
-                       if (key == ARG_any)
+                       if (key != ARG_any)
                                p->iph.daddr = get_addr32(*argv);
                } else if (key == ARG_local) {
                        NEXT_ARG();
                        key = index_in_strings(keywords, *argv);
-                       if (key == ARG_any)
+                       if (key != ARG_any)
                                p->iph.saddr = get_addr32(*argv);
                } else if (key == ARG_dev) {
                        NEXT_ARG();
-                       strncpy(medium, *argv, IFNAMSIZ-1);
+                       strncpy_IFNAMSIZ(medium, *argv);
                } else if (key == ARG_ttl) {
                        unsigned uval;
                        NEXT_ARG();
                        key = index_in_strings(keywords, *argv);
                        if (key != ARG_inherit) {
-                               if (get_unsigned(&uval, *argv, 0))
-                                       invarg(*argv, "TTL");
+                               uval = get_unsigned(*argv, "TTL");
                                if (uval > 255)
-                                       invarg(*argv, "TTL must be <=255");
+                                       invarg_1_to_2(*argv, "TTL");
                                p->iph.ttl = uval;
                        }
                } else if (key == ARG_tos ||
-                          key == ARG_dsfield) {
+                          key == ARG_dsfield
+               ) {
                        uint32_t uval;
                        NEXT_ARG();
                        key = index_in_strings(keywords, *argv);
                        if (key != ARG_inherit) {
                                if (rtnl_dsfield_a2n(&uval, *argv))
-                                       invarg(*argv, "TOS");
+                                       invarg_1_to_2(*argv, "TOS");
                                p->iph.tos = uval;
                        } else
                                p->iph.tos = 1;
@@ -279,17 +315,16 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
                        }
                        if (p->name[0])
                                duparg2("name", *argv);
-                       strncpy(p->name, *argv, IFNAMSIZ);
+                       strncpy_IFNAMSIZ(p->name, *argv);
                        if (cmd == SIOCCHGTUNNEL && count == 0) {
                                struct ip_tunnel_parm old_p;
                                memset(&old_p, 0, sizeof(old_p));
                                if (do_get_ioctl(*argv, &old_p))
-                                       exit(1);
+                                       exit(EXIT_FAILURE);
                                *p = old_p;
                        }
                }
                count++;
-               argc--;
                argv++;
        }
 
@@ -325,13 +360,12 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
        }
 }
 
-
 /* Return value becomes exitcode. It's okay to not return at all */
-static int do_add(int cmd, int argc, char **argv)
+static int do_add(int cmd, char **argv)
 {
        struct ip_tunnel_parm p;
 
-       parse_args(argc, argv, cmd, &p);
+       parse_args(argv, cmd, &p);
 
        if (p.iph.ttl && p.iph.frag_off == 0) {
                bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
@@ -345,16 +379,16 @@ static int do_add(int cmd, int argc, char **argv)
        case IPPROTO_IPV6:
                return do_add_ioctl(cmd, "sit0", &p);
        default:
-               bb_error_msg_and_die("cannot determine tunnel mode (ipip, gre or sit)");
+               bb_error_msg_and_die("can't determine tunnel mode (ipip, gre or sit)");
        }
 }
 
 /* Return value becomes exitcode. It's okay to not return at all */
-static int do_del(int argc, char **argv)
+static int do_del(char **argv)
 {
        struct ip_tunnel_parm p;
 
-       parse_args(argc, argv, SIOCDELTUNNEL, &p);
+       parse_args(argv, SIOCDELTUNNEL, &p);
 
        switch (p.iph.protocol) {
        case IPPROTO_IPIP:
@@ -370,22 +404,18 @@ static int do_del(int argc, char **argv)
 
 static void print_tunnel(struct ip_tunnel_parm *p)
 {
-       char s1[256];
-       char s2[256];
-       char s3[64];
-       char s4[64];
-
-       format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
-       format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
-       inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
-       inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
+       char s3[INET_ADDRSTRLEN];
+       char s4[INET_ADDRSTRLEN];
 
        printf("%s: %s/ip  remote %s  local %s ",
-              p->name,
-              p->iph.protocol == IPPROTO_IPIP ? "ip" :
-              (p->iph.protocol == IPPROTO_GRE ? "gre" :
-               (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
-              p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
+               p->name,
+               p->iph.protocol == IPPROTO_IPIP ? "ip" :
+                       p->iph.protocol == IPPROTO_GRE ? "gre" :
+                       p->iph.protocol == IPPROTO_IPV6 ? "ipv6" :
+                       "unknown",
+               p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr) : "any",
+               p->iph.saddr ? format_host(AF_INET, 4, &p->iph.saddr) : "any"
+       );
        if (p->link) {
                char *n = do_ioctl_get_ifname(p->link);
                if (n) {
@@ -398,20 +428,21 @@ static void print_tunnel(struct ip_tunnel_parm *p)
        else
                printf(" ttl inherit ");
        if (p->iph.tos) {
-               SPRINT_BUF(b1);
                printf(" tos");
                if (p->iph.tos & 1)
                        printf(" inherit");
                if (p->iph.tos & ~1)
                        printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
-                              rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
+                               rtnl_dsfield_n2a(p->iph.tos & ~1));
        }
        if (!(p->iph.frag_off & htons(IP_DF)))
                printf(" nopmtudisc");
 
+       inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
+       inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
        if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
                printf(" key %s", s3);
-       else if ((p->i_flags | p->o_flags) & GRE_KEY) {
+       else {
                if (p->i_flags & GRE_KEY)
                        printf(" ikey %s ", s3);
                if (p->o_flags & GRE_KEY)
@@ -443,7 +474,7 @@ static void do_tunnels_list(struct ip_tunnel_parm *p)
        if (fp == NULL) {
                return;
        }
-
+       /* skip headers */
        fgets(buf, sizeof(buf), fp);
        fgets(buf, sizeof(buf), fp);
 
@@ -453,7 +484,8 @@ static void do_tunnels_list(struct ip_tunnel_parm *p)
                /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
                ptr = strchr(buf, ':');
                if (ptr == NULL ||
-                   (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
+                   (*ptr++ = 0, sscanf(buf, "%s", name) != 1)
+               ) {
                        bb_error_msg("wrong format of /proc/net/dev");
                        return;
                }
@@ -467,7 +499,7 @@ static void do_tunnels_list(struct ip_tunnel_parm *p)
                        continue;
                type = do_ioctl_get_iftype(name);
                if (type == -1) {
-                       bb_error_msg("cannot get type of [%s]", name);
+                       bb_error_msg("can't get type of [%s]", name);
                        continue;
                }
                if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
@@ -479,20 +511,22 @@ static void do_tunnels_list(struct ip_tunnel_parm *p)
                    (p->name[0] && strcmp(p1.name, p->name)) ||
                    (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
                    (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
-                   (p->i_key && p1.i_key != p->i_key))
+                   (p->i_key && p1.i_key != p->i_key)
+               ) {
                        continue;
+               }
                print_tunnel(&p1);
-               puts("");
+               bb_putchar('\n');
        }
 }
 
 /* Return value becomes exitcode. It's okay to not return at all */
-static int do_show(int argc, char **argv)
+static int do_show(char **argv)
 {
        int err;
        struct ip_tunnel_parm p;
 
-       parse_args(argc, argv, SIOCGETTUNNEL, &p);
+       parse_args(argv, SIOCGETTUNNEL, &p);
 
        switch (p.iph.protocol) {
        case IPPROTO_IPIP:
@@ -512,30 +546,28 @@ static int do_show(int argc, char **argv)
                return -1;
 
        print_tunnel(&p);
-       puts("");
+       bb_putchar('\n');
        return 0;
 }
 
 /* Return value becomes exitcode. It's okay to not return at all */
-int do_iptunnel(int argc, char **argv)
+int FAST_FUNC do_iptunnel(char **argv)
 {
-       static const char keywords[] =
+       static const char keywords[] ALIGN1 =
                "add\0""change\0""delete\0""show\0""list\0""lst\0";
        enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst };
-       int key;
 
-       if (argc) {
-               key = index_in_substrings(keywords, *argv);
+       if (*argv) {
+               int key = index_in_substrings(keywords, *argv);
                if (key < 0)
-                       bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
-               --argc;
-               ++argv;
+                       invarg_1_to_2(*argv, applet_name);
+               argv++;
                if (key == ARG_add)
-                       return do_add(SIOCADDTUNNEL, argc, argv);
+                       return do_add(SIOCADDTUNNEL, argv);
                if (key == ARG_change)
-                       return do_add(SIOCCHGTUNNEL, argc, argv);
+                       return do_add(SIOCCHGTUNNEL, argv);
                if (key == ARG_del)
-                       return do_del(argc, argv);
+                       return do_del(argv);
        }
-       return do_show(argc, argv);
+       return do_show(argv);
 }