nslookup: return exitcode 1 on resolution errors
[oweals/busybox.git] / networking / tc.c
index a815b4bd1e8a6070f49bd1c98987cc67ce46f847..3e9808328d8b4ab4071020b8ccc9f74df9110f85 100644 (file)
@@ -1,37 +1,86 @@
 /* vi: set sw=4 ts=4: */
 /*
- * tc.c                "tc" utility frontend.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  *
- * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
+ * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  *
- * Authors:    Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
- *
- * Bernhard Fischer adjusted for busybox
+ * Bernhard Reutner-Fischer adjusted for busybox
  */
+//config:config TC
+//config:      bool "tc (8.3 kb)"
+//config:      default y
+//config:      help
+//config:      Show / manipulate traffic control settings
+//config:
+//config:config FEATURE_TC_INGRESS
+//config:      bool "Enable ingress"
+//config:      default y
+//config:      depends on TC
+
+//applet:IF_TC(APPLET(tc, BB_DIR_SBIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_TC) += tc.o
+
+//usage:#define tc_trivial_usage
+/* //usage: "[OPTIONS] OBJECT CMD [dev STRING]" */
+//usage:       "OBJECT CMD [dev STRING]"
+//usage:#define tc_full_usage "\n\n"
+//usage:       "OBJECT: qdisc|class|filter\n"
+//usage:       "CMD: add|del|change|replace|show\n"
+//usage:       "\n"
+//usage:       "qdisc [handle QHANDLE] [root|"IF_FEATURE_TC_INGRESS("ingress|")"parent CLASSID]\n"
+/* //usage: "[estimator INTERVAL TIME_CONSTANT]\n" */
+//usage:       "       [[QDISC_KIND] [help|OPTIONS]]\n"
+//usage:       "       QDISC_KIND := [p|b]fifo|tbf|prio|cbq|red|etc.\n"
+//usage:       "qdisc show [dev STRING]"IF_FEATURE_TC_INGRESS(" [ingress]")"\n"
+//usage:       "class [classid CLASSID] [root|parent CLASSID]\n"
+//usage:       "       [[QDISC_KIND] [help|OPTIONS] ]\n"
+//usage:       "class show [ dev STRING ] [root|parent CLASSID]\n"
+//usage:       "filter [pref PRIO] [protocol PROTO]\n"
+/* //usage: "\t[estimator INTERVAL TIME_CONSTANT]\n" */
+//usage:       "       [root|classid CLASSID] [handle FILTERID]\n"
+//usage:       "       [[FILTER_TYPE] [help|OPTIONS]]\n"
+//usage:       "filter show [dev STRING] [root|parent CLASSID]"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #include "libiproute/utils.h"
 #include "libiproute/ip_common.h"
 #include "libiproute/rt_names.h"
 #include <linux/pkt_sched.h> /* for the TC_H_* macros */
 
+/* This is the deprecated multiqueue interface */
+#ifndef TCA_PRIO_MAX
+enum
+{
+       TCA_PRIO_UNSPEC,
+       TCA_PRIO_MQ,
+       __TCA_PRIO_MAX
+};
+#define TCA_PRIO_MAX    (__TCA_PRIO_MAX - 1)
+#endif
+
 #define parse_rtattr_nested(tb, max, rta) \
        (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
 
 /* nullifies tb on error */
 #define __parse_rtattr_nested_compat(tb, max, rta, len) \
-       ({if ((RTA_PAYLOAD(rta) >= len) && \
-                (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr))) { \
-                       rta = RTA_DATA(rta) + RTA_ALIGN(len); \
-                       parse_rtattr_nested(tb, max, rta); \
-         } else \
-                       memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); \
-       })
+({ \
+       if ((RTA_PAYLOAD(rta) >= len) \
+        && (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) \
+       ) { \
+               rta = RTA_DATA(rta) + RTA_ALIGN(len); \
+               parse_rtattr_nested(tb, max, rta); \
+       } else \
+               memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); \
+})
 
 #define parse_rtattr_nested_compat(tb, max, rta, data, len) \
-       ({data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
-       __parse_rtattr_nested_compat(tb, max, rta, len); })
+({ \
+       data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
+       __parse_rtattr_nested_compat(tb, max, rta, len); \
+})
 
 #define show_details (0) /* not implemented. Does anyone need it? */
 #define use_iec (0) /* not currently documented in the upstream manpage */
 
 struct globals {
        int filter_ifindex;
-       __u32 filter_qdisc;
-       __u32 filter_parent;
-       __u32 filter_prio;
-       __u32 filter_proto;
-};
-
-#define G (*(struct globals*)&bb_common_bufsiz1)
+       uint32_t filter_qdisc;
+       uint32_t filter_parent;
+       uint32_t filter_prio;
+       uint32_t filter_proto;
+} FIX_ALIASING;
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define filter_ifindex (G.filter_ifindex)
 #define filter_qdisc (G.filter_qdisc)
 #define filter_parent (G.filter_parent)
 #define filter_prio (G.filter_prio)
 #define filter_proto (G.filter_proto)
-
-void BUG_tc_globals_too_big(void);
 #define INIT_G() do { \
-       if (sizeof(G) > COMMON_BUFSIZE) \
-               BUG_tc_globals_too_big(); \
+       setup_common_bufsiz(); \
+       BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
 } while (0)
 
 /* Allocates a buffer containing the name of a class id.
@@ -78,18 +124,18 @@ static char* print_tc_classid(uint32_t cid)
 }
 
 /* Get a qdisc handle.  Return 0 on success, !0 otherwise.  */
-static int get_qdisc_handle(__u32 *h, const char *str) {
-       __u32 maj;
+static int get_qdisc_handle(uint32_t *h, const char *str) {
+       uint32_t maj;
        char *p;
 
        maj = TC_H_UNSPEC;
-       if (!strcmp(str, "none"))
+       if (strcmp(str, "none") == 0)
                goto ok;
        maj = strtoul(str, &p, 16);
        if (p == str)
                return 1;
        maj <<= 16;
-       if (*p != ':' && *p!=0)
+       if (*p != ':' && *p != '\0')
                return 1;
  ok:
        *h = maj;
@@ -97,15 +143,15 @@ static int get_qdisc_handle(__u32 *h, const char *str) {
 }
 
 /* Get class ID.  Return 0 on success, !0 otherwise.  */
-static int get_tc_classid(__u32 *h, const char *str) {
-       __u32 maj, min;
+static int get_tc_classid(uint32_t *h, const char *str) {
+       uint32_t maj, min;
        char *p;
 
        maj = TC_H_ROOT;
-       if (!strcmp(str, "root"))
+       if (strcmp(str, "root") == 0)
                goto ok;
        maj = TC_H_UNSPEC;
-       if (!strcmp(str, "none"))
+       if (strcmp(str, "none") == 0)
                goto ok;
        maj = strtoul(str, &p, 16);
        if (p == str) {
@@ -119,7 +165,8 @@ static int get_tc_classid(__u32 *h, const char *str) {
                maj <<= 16;
                str = p + 1;
                min = strtoul(str, &p, 16);
-               if (*p != 0 || min >= (1<<16))
+//FIXME: check for "" too?
+               if (*p != '\0' || min >= (1<<16))
                        return 1;
                maj |= min;
        } else if (*p != 0)
@@ -134,27 +181,29 @@ static void print_rate(char *buf, int len, uint32_t rate)
        double tmp = (double)rate*8;
 
        if (use_iec) {
-               if (tmp >= 1000.0*1024.0*1024.0)
-                       snprintf(buf, len, "%.0fMibit", tmp/1024.0*1024.0);
-               else if (tmp >= 1000.0*1024)
+               if (tmp >= 1000*1024*1024)
+                       snprintf(buf, len, "%.0fMibit", tmp/(1024*1024));
+               else if (tmp >= 1000*1024)
                        snprintf(buf, len, "%.0fKibit", tmp/1024);
                else
                        snprintf(buf, len, "%.0fbit", tmp);
        } else {
-               if (tmp >= 1000.0*1000000.0)
-                       snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
-               else if (tmp >= 1000.0 * 1000.0)
-                       snprintf(buf, len, "%.0fKbit", tmp/1000.0);
+               if (tmp >= 1000*1000000)
+                       snprintf(buf, len, "%.0fMbit", tmp/1000000);
+               else if (tmp >= 1000*1000)
+                       snprintf(buf, len, "%.0fKbit", tmp/1000);
                else
                        snprintf(buf, len, "%.0fbit",  tmp);
        }
 }
 
+#if 0
 /* This is "pfifo_fast".  */
 static int prio_parse_opt(int argc, char **argv, struct nlmsghdr *n)
 {
        return 0;
 }
+#endif
 static int prio_print_opt(struct rtattr *opt)
 {
        int i;
@@ -177,11 +226,13 @@ static int prio_print_opt(struct rtattr *opt)
        return 0;
 }
 
+#if 0
 /* Class Based Queue */
 static int cbq_parse_opt(int argc, char **argv, struct nlmsghdr *n)
 {
        return 0;
 }
+#endif
 static int cbq_print_opt(struct rtattr *opt)
 {
        struct rtattr *tb[TCA_CBQ_MAX+1];
@@ -190,8 +241,8 @@ static int cbq_print_opt(struct rtattr *opt)
        struct tc_cbq_wrropt *wrr = NULL;
        struct tc_cbq_fopt *fopt = NULL;
        struct tc_cbq_ovl *ovl = NULL;
-       const char * const error = "CBQ: too short %s opt";
-       RESERVE_CONFIG_BUFFER(buf, 64);
+       const char *const error = "CBQ: too short %s opt";
+       char buf[64];
 
        if (opt == NULL)
                goto done;
@@ -271,12 +322,13 @@ static int cbq_print_opt(struct rtattr *opt)
                }
        }
  done:
-       RELEASE_CONFIG_BUFFER(buf);
        return 0;
 }
 
-static int print_qdisc(const struct sockaddr_nl *who UNUSED_PARAM,
-                                               struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
+static FAST_FUNC int print_qdisc(
+               const struct sockaddr_nl *who UNUSED_PARAM,
+               struct nlmsghdr *hdr,
+               void *arg UNUSED_PARAM)
 {
        struct tcmsg *msg = NLMSG_DATA(hdr);
        int len = hdr->nlmsg_len;
@@ -284,12 +336,12 @@ static int print_qdisc(const struct sockaddr_nl *who UNUSED_PARAM,
        char *name;
 
        if (hdr->nlmsg_type != RTM_NEWQDISC && hdr->nlmsg_type != RTM_DELQDISC) {
-               /* bb_error_msg("Not a qdisc"); */
+               /* bb_error_msg("not a qdisc"); */
                return 0; /* ??? mimic upstream; should perhaps return -1 */
        }
        len -= NLMSG_LENGTH(sizeof(*msg));
        if (len < 0) {
-               /* bb_error_msg("Wrong len %d", len); */
+               /* bb_error_msg("wrong len %d", len); */
                return -1;
        }
        /* not the desired interface? */
@@ -322,7 +374,7 @@ static int print_qdisc(const struct sockaddr_nl *who UNUSED_PARAM,
                int qqq = index_in_strings(_q_, name);
                if (qqq == 0) { /* pfifo_fast aka prio */
                        prio_print_opt(tb[TCA_OPTIONS]);
-               } else if (qqq == 1) { /* class based queueing */
+               } else if (qqq == 1) { /* class based queuing */
                        cbq_print_opt(tb[TCA_OPTIONS]);
                } else
                        bb_error_msg("unknown %s", name);
@@ -331,8 +383,10 @@ static int print_qdisc(const struct sockaddr_nl *who UNUSED_PARAM,
        return 0;
 }
 
-static int print_class(const struct sockaddr_nl *who UNUSED_PARAM,
-                                               struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
+static FAST_FUNC int print_class(
+               const struct sockaddr_nl *who UNUSED_PARAM,
+               struct nlmsghdr *hdr,
+               void *arg UNUSED_PARAM)
 {
        struct tcmsg *msg = NLMSG_DATA(hdr);
        int len = hdr->nlmsg_len;
@@ -342,12 +396,12 @@ static int print_class(const struct sockaddr_nl *who UNUSED_PARAM,
        /*XXX Eventually factor out common code */
 
        if (hdr->nlmsg_type != RTM_NEWTCLASS && hdr->nlmsg_type != RTM_DELTCLASS) {
-               /* bb_error_msg("Not a class"); */
+               /* bb_error_msg("not a class"); */
                return 0; /* ??? mimic upstream; should perhaps return -1 */
        }
        len -= NLMSG_LENGTH(sizeof(*msg));
        if (len < 0) {
-               /* bb_error_msg("Wrong len %d", len); */
+               /* bb_error_msg("wrong len %d", len); */
                return -1;
        }
        /* not the desired interface? */
@@ -375,7 +429,7 @@ static int print_class(const struct sockaddr_nl *who UNUSED_PARAM,
                printf("root ");
        else if (msg->tcm_parent) {
                classid = print_tc_classid(filter_qdisc ?
-                                                                  TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
+                               TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
                printf("parent %s ", classid);
                if (ENABLE_FEATURE_CLEAN_UP)
                        free(classid);
@@ -388,7 +442,7 @@ static int print_class(const struct sockaddr_nl *who UNUSED_PARAM,
                int qqq = index_in_strings(_q_, name);
                if (qqq == 0) { /* pfifo_fast aka prio */
                        /* nothing. */ /*prio_print_opt(tb[TCA_OPTIONS]);*/
-               } else if (qqq == 1) { /* class based queueing */
+               } else if (qqq == 1) { /* class based queuing */
                        /* cbq_print_copt() is identical to cbq_print_opt(). */
                        cbq_print_opt(tb[TCA_OPTIONS]);
                } else
@@ -399,12 +453,11 @@ static int print_class(const struct sockaddr_nl *who UNUSED_PARAM,
        return 0;
 }
 
-static int print_filter(const struct sockaddr_nl *who UNUSED_PARAM,
-                                               struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
+static FAST_FUNC int print_filter(
+               const struct sockaddr_nl *who UNUSED_PARAM,
+               struct nlmsghdr *hdr UNUSED_PARAM,
+               void *arg UNUSED_PARAM)
 {
-       struct tcmsg *msg = NLMSG_DATA(hdr);
-       int len = hdr->nlmsg_len;
-       struct rtattr * tb[TCA_MAX+1];
        return 0;
 }
 
@@ -421,6 +474,12 @@ int tc_main(int argc UNUSED_PARAM, char **argv)
                "replace\0"
                "show\0""list\0"
                ;
+       enum {
+               CMD_add = 0, CMD_del, CMD_change,
+               CMD_link,
+               CMD_replace,
+               CMD_show
+       };
        static const char args[] ALIGN1 =
                "dev\0" /* qdisc, class, filter */
                "root\0" /* class, filter */
@@ -430,9 +489,15 @@ int tc_main(int argc UNUSED_PARAM, char **argv)
                "classid\0" /* change: for class use "handle" */
                "preference\0""priority\0""protocol\0" /* filter */
                ;
-       enum { CMD_add = 0, CMD_del, CMD_change, CMD_link, CMD_replace, CMD_show };
-       enum { ARG_dev = 0, ARG_root, ARG_parent, ARG_qdisc,
-                       ARG_handle, ARG_classid, ARG_pref, ARG_prio, ARG_proto};
+       enum {
+               ARG_dev = 0,
+               ARG_root,
+               ARG_parent,
+               ARG_qdisc,
+               ARG_handle,
+               ARG_classid,
+               ARG_pref, ARG_prio, ARG_proto
+       };
        struct rtnl_handle rth;
        struct tcmsg msg;
        int ret, obj, cmd, arg;
@@ -447,19 +512,22 @@ int tc_main(int argc UNUSED_PARAM, char **argv)
 
        obj = index_in_substrings(objects, *argv++);
 
-       if (obj < OBJ_qdisc)
+       if (obj < 0)
                bb_show_usage();
        if (!*argv)
                cmd = CMD_show; /* list is the default */
        else {
                cmd = index_in_substrings(commands, *argv);
                if (cmd < 0)
-                       bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
+                       invarg_1_to_2(*argv, argv[-1]);
                argv++;
        }
+
        memset(&msg, 0, sizeof(msg));
-       msg.tcm_family = AF_UNSPEC;
+       if (AF_UNSPEC != 0)
+               msg.tcm_family = AF_UNSPEC;
        ll_init_map(&rth);
+
        while (*argv) {
                arg = index_in_substrings(args, *argv);
                if (arg == ARG_dev) {
@@ -470,19 +538,24 @@ int tc_main(int argc UNUSED_PARAM, char **argv)
                        msg.tcm_ifindex = xll_name_to_index(dev);
                        if (cmd >= CMD_show)
                                filter_ifindex = msg.tcm_ifindex;
-               } else if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show)
-                                  || (arg == ARG_handle && obj == OBJ_qdisc
-                                          && cmd == CMD_change)) {
+               } else
+               if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show)
+                || (arg == ARG_handle && obj == OBJ_qdisc && cmd == CMD_change)
+               ) {
                        NEXT_ARG();
                        /* We don't care about duparg2("qdisc handle",*argv) for now */
                        if (get_qdisc_handle(&filter_qdisc, *argv))
-                               invarg(*argv, "qdisc");
-               } else if (obj != OBJ_qdisc &&
-                                  (arg == ARG_root
-                                        || arg == ARG_parent
-                                        || (obj == OBJ_filter && arg >= ARG_pref))) {
+                               invarg_1_to_2(*argv, "qdisc");
+               } else
+               if (obj != OBJ_qdisc
+                && (arg == ARG_root
+                   || arg == ARG_parent
+                   || (obj == OBJ_filter && arg >= ARG_pref)
+                   )
+               ) {
+                       /* nothing */
                } else {
-                       invarg(*argv, "command");
+                       invarg_1_to_2(*argv, "command");
                }
                NEXT_ARG();
                if (arg == ARG_root) {
@@ -491,48 +564,57 @@ int tc_main(int argc UNUSED_PARAM, char **argv)
                        msg.tcm_parent = TC_H_ROOT;
                        if (obj == OBJ_filter)
                                filter_parent = TC_H_ROOT;
-               } else if (arg == ARG_parent) {
-                       __u32 handle;
+               } else
+               if (arg == ARG_parent) {
+                       uint32_t handle;
                        if (msg.tcm_parent)
                                duparg(*argv, "parent");
                        if (get_tc_classid(&handle, *argv))
-                               invarg(*argv, "parent");
+                               invarg_1_to_2(*argv, "parent");
                        msg.tcm_parent = handle;
                        if (obj == OBJ_filter)
                                filter_parent = handle;
-               } else if (arg == ARG_handle) { /* filter::list */
+               } else
+               if (arg == ARG_handle) { /* filter::list */
                        if (msg.tcm_handle)
                                duparg(*argv, "handle");
                        /* reject LONG_MIN || LONG_MAX */
                        /* TODO: for fw
-                          if ((slash = strchr(handle, '/')) != NULL)
-                                  *slash = '\0';
+                       slash = strchr(handle, '/');
+                       if (slash != NULL)
+                               *slash = '\0';
                         */
-                       if (get_u32(&msg.tcm_handle, *argv, 0))
-                               invarg(*argv, "handle");
-                       /* if (slash) {if (get_u32(__u32 &mask, slash+1,0)) inv mask;addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */
-               } else if (arg == ARG_classid && obj == OBJ_class && cmd == CMD_change){
-               } else if (arg == ARG_pref || arg == ARG_prio) { /* filter::list */
+                       msg.tcm_handle = get_u32(*argv, "handle");
+                       /* if (slash) {if (get_u32(uint32_t &mask, slash+1, NULL)) inv mask; addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */
+               } else
+               if (arg == ARG_classid
+                && obj == OBJ_class
+                && cmd == CMD_change
+               ) {
+                       /* TODO */
+               } else
+               if (arg == ARG_pref || arg == ARG_prio) { /* filter::list */
                        if (filter_prio)
                                duparg(*argv, "priority");
-                       if (get_u32(&filter_prio, *argv, 0))
-                               invarg(*argv, "priority");
-               } else if (arg == ARG_proto) { /* filter::list */
-                       __u16 tmp;
+                       filter_prio = get_u32(*argv, "priority");
+               } else
+               if (arg == ARG_proto) { /* filter::list */
+                       uint16_t tmp;
                        if (filter_proto)
                                duparg(*argv, "protocol");
                        if (ll_proto_a2n(&tmp, *argv))
-                               invarg(*argv, "protocol");
+                               invarg_1_to_2(*argv, "protocol");
                        filter_proto = tmp;
                }
        }
+
        if (cmd >= CMD_show) { /* show or list */
                if (obj == OBJ_filter)
                        msg.tcm_info = TC_H_MAKE(filter_prio<<16, filter_proto);
                if (rtnl_dump_request(&rth, obj == OBJ_qdisc ? RTM_GETQDISC :
                                                obj == OBJ_class ? RTM_GETTCLASS : RTM_GETTFILTER,
                                                &msg, sizeof(msg)) < 0)
-                       bb_simple_perror_msg_and_die("cannot send dump request");
+                       bb_simple_perror_msg_and_die("can't send dump request");
 
                xrtnl_dump_filter(&rth, obj == OBJ_qdisc ? print_qdisc :
                                                obj == OBJ_class ? print_class : print_filter,