]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - tc/tc_filter.c
libnetlink: add size argument to rtnl_talk
[mirror_iproute2.git] / tc / tc_filter.c
index 74e996955ed655a8e2bd8572a3a4c701519b9f49..9e416008eaa9b708142faf3e644db440ce699bde 100644 (file)
@@ -15,8 +15,6 @@
 #include <unistd.h>
 #include <syslog.h>
 #include <fcntl.h>
-#include <net/if.h>
-#include <net/if_arp.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include "tc_util.h"
 #include "tc_common.h"
 
-static void usage(void) __attribute__((noreturn));
+static void usage(void);
 
 static void usage(void)
 {
-       fprintf(stderr, "Usage: tc filter [ add | del | change | get ] dev STRING\n");
-       fprintf(stderr, "       [ pref PRIO ] [ protocol PROTO ]\n");
+       fprintf(stderr, "Usage: tc filter [ add | del | change | replace | show ] dev STRING\n");
+       fprintf(stderr, "       [ pref PRIO ] protocol PROTO\n");
        fprintf(stderr, "       [ estimator INTERVAL TIME_CONSTANT ]\n");
        fprintf(stderr, "       [ root | classid CLASSID ] [ handle FILTERID ]\n");
        fprintf(stderr, "       [ [ FILTER_TYPE ] [ help | OPTIONS ] ]\n");
        fprintf(stderr, "\n");
        fprintf(stderr, "       tc filter show [ dev STRING ] [ root | parent CLASSID ]\n");
        fprintf(stderr, "Where:\n");
-       fprintf(stderr, "FILTER_TYPE := { rsvp | u32 | fw | route | etc. }\n");
+       fprintf(stderr, "FILTER_TYPE := { rsvp | u32 | bpf | fw | route | etc. }\n");
        fprintf(stderr, "FILTERID := ... format depends on classifier, see there\n");
        fprintf(stderr, "OPTIONS := ... try tc filter add <desired FILTER_KIND> help\n");
-       exit(-1);
+       return;
 }
 
 
-int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
+static int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
 {
-       struct rtnl_handle rth;
        struct {
                struct nlmsghdr         n;
                struct tcmsg            t;
@@ -58,6 +55,7 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
        struct filter_util *q = NULL;
        __u32 prio = 0;
        __u32 protocol = 0;
+       int protocol_set = 0;
        char *fhandle = NULL;
        char  d[16];
        char  k[16];
@@ -74,6 +72,9 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
        req.n.nlmsg_type = cmd;
        req.t.tcm_family = AF_UNSPEC;
 
+       if (cmd == RTM_NEWTFILTER && flags & NLM_F_CREATE)
+               protocol = htons(ETH_P_ALL);
+
        while (argc > 0) {
                if (strcmp(*argv, "dev") == 0) {
                        NEXT_ARG();
@@ -83,7 +84,7 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
                } else if (strcmp(*argv, "root") == 0) {
                        if (req.t.tcm_parent) {
                                fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
-                               exit(-1);
+                               return -1;
                        }
                        req.t.tcm_parent = TC_H_ROOT;
                } else if (strcmp(*argv, "parent") == 0) {
@@ -92,7 +93,7 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
                        if (req.t.tcm_parent)
                                duparg("parent", *argv);
                        if (get_tc_classid(&handle, *argv))
-                               invarg(*argv, "Invalid parent ID");
+                               invarg("Invalid parent ID", *argv);
                        req.t.tcm_parent = handle;
                } else if (strcmp(*argv, "handle") == 0) {
                        NEXT_ARG();
@@ -104,21 +105,23 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
                        NEXT_ARG();
                        if (prio)
                                duparg("priority", *argv);
-                       if (get_u32(&prio, *argv, 0))
-                               invarg(*argv, "invalid prpriority value");
+                       if (get_u32(&prio, *argv, 0) || prio > 0xFFFF)
+                               invarg("invalid priority value", *argv);
                } else if (matches(*argv, "protocol") == 0) {
                        __u16 id;
                        NEXT_ARG();
-                       if (protocol)
+                       if (protocol_set)
                                duparg("protocol", *argv);
                        if (ll_proto_a2n(&id, *argv))
-                               invarg(*argv, "invalid protocol");
+                               invarg("invalid protocol", *argv);
                        protocol = id;
+                       protocol_set = 1;
                } else if (matches(*argv, "estimator") == 0) {
                        if (parse_estimator(&argc, &argv, &est) < 0)
                                return -1;
                } else if (matches(*argv, "help") == 0) {
                        usage();
+                       return 0;
                } else {
                        strncpy(k, *argv, sizeof(k)-1);
 
@@ -137,46 +140,38 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
 
        if (q) {
                if (q->parse_fopt(q, fhandle, argc, argv, &req.n))
-                       exit(1);
+                       return 1;
        } else {
                if (fhandle) {
                        fprintf(stderr, "Must specify filter type when using "
                                "\"handle\"\n");
-                       exit(-1);
+                       return -1;
                }
                if (argc) {
                        if (matches(*argv, "help") == 0)
                                usage();
                        fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc filter help\".\n", *argv);
-                       exit(-1);
+                       return -1;
                }
        }
        if (est.ewma_log)
                addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
 
 
-       if (rtnl_open(&rth, 0) < 0) {
-               fprintf(stderr, "Cannot open rtnetlink\n");
-               exit(1);
-       }
-
        if (d[0])  {
                ll_init_map(&rth);
 
                if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
                        fprintf(stderr, "Cannot find device \"%s\"\n", d);
-                       rtnl_close(&rth);
-                       exit(1);
+                       return 1;
                }
        }
 
-       if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
+       if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
                fprintf(stderr, "We have an error talking to the kernel\n");
-               rtnl_close(&rth);
-               exit(2);
+               return 2;
        }
 
-       rtnl_close(&rth);
        return 0;
 }
 
@@ -184,9 +179,10 @@ static __u32 filter_parent;
 static int filter_ifindex;
 static __u32 filter_prio;
 static __u32 filter_protocol;
+__u16 f_proto = 0;
 
-static int print_filter(const struct sockaddr_nl *who,
-                       const struct nlmsghdr *n, 
+int print_filter(const struct sockaddr_nl *who,
+                       struct nlmsghdr *n,
                        void *arg)
 {
        FILE *fp = (FILE*)arg;
@@ -230,13 +226,13 @@ static int print_filter(const struct sockaddr_nl *who,
                }
        }
        if (t->tcm_info) {
-               __u32 protocol = TC_H_MIN(t->tcm_info);
+               f_proto = TC_H_MIN(t->tcm_info);
                __u32 prio = TC_H_MAJ(t->tcm_info)>>16;
-               if (!filter_protocol || filter_protocol != protocol) {
-                       if (protocol) {
+               if (!filter_protocol || filter_protocol != f_proto) {
+                       if (f_proto) {
                                SPRINT_BUF(b1);
                                fprintf(fp, "protocol %s ",
-                                       ll_proto_n2a(protocol, b1, sizeof(b1)));
+                                       ll_proto_n2a(f_proto, b1, sizeof(b1)));
                        }
                }
                if (!filter_prio || filter_prio != prio) {
@@ -244,7 +240,7 @@ static int print_filter(const struct sockaddr_nl *who,
                                fprintf(fp, "pref %u ", prio);
                }
        }
-       fprintf(fp, "%s ", (char*)RTA_DATA(tb[TCA_KIND]));
+       fprintf(fp, "%s ", rta_getattr_str(tb[TCA_KIND]));
        q = get_filter_kind(RTA_DATA(tb[TCA_KIND]));
        if (tb[TCA_OPTIONS]) {
                if (q)
@@ -254,8 +250,8 @@ static int print_filter(const struct sockaddr_nl *who,
        }
        fprintf(fp, "\n");
 
-       if (show_stats && tb[TCA_STATS]) {
-               print_tcstats_attr(fp, tb[TCA_STATS]);
+       if (show_stats && (tb[TCA_STATS] || tb[TCA_STATS2])) {
+               print_tcstats_attr(fp, tb, " ", NULL);
                fprintf(fp, "\n");
        }
 
@@ -264,10 +260,9 @@ static int print_filter(const struct sockaddr_nl *who,
 }
 
 
-int tc_filter_list(int argc, char **argv)
+static int tc_filter_list(int argc, char **argv)
 {
        struct tcmsg t;
-       struct rtnl_handle rth;
        char d[16];
        __u32 prio = 0;
        __u32 protocol = 0;
@@ -286,7 +281,7 @@ int tc_filter_list(int argc, char **argv)
                } else if (strcmp(*argv, "root") == 0) {
                        if (t.tcm_parent) {
                                fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
-                               exit(-1);
+                               return -1;
                        }
                        filter_parent = t.tcm_parent = TC_H_ROOT;
                } else if (strcmp(*argv, "parent") == 0) {
@@ -295,7 +290,7 @@ int tc_filter_list(int argc, char **argv)
                        if (t.tcm_parent)
                                duparg("parent", *argv);
                        if (get_tc_classid(&handle, *argv))
-                               invarg(*argv, "invalid parent ID");
+                               invarg("invalid parent ID", *argv);
                        filter_parent = t.tcm_parent = handle;
                } else if (strcmp(*argv, "handle") == 0) {
                        NEXT_ARG();
@@ -308,7 +303,7 @@ int tc_filter_list(int argc, char **argv)
                        if (prio)
                                duparg("priority", *argv);
                        if (get_u32(&prio, *argv, 0))
-                               invarg(*argv, "invalid preference");
+                               invarg("invalid preference", *argv);
                        filter_prio = prio;
                } else if (matches(*argv, "protocol") == 0) {
                        __u16 res;
@@ -316,14 +311,14 @@ int tc_filter_list(int argc, char **argv)
                        if (protocol)
                                duparg("protocol", *argv);
                        if (ll_proto_a2n(&res, *argv))
-                               invarg(*argv, "invalid protocol");
+                               invarg("invalid protocol", *argv);
                        protocol = res;
                        filter_protocol = protocol;
                } else if (matches(*argv, "help") == 0) {
                        usage();
                } else {
                        fprintf(stderr, " What is \"%s\"? Try \"tc filter help\"\n", *argv);
-                       exit(-1);
+                       return -1;
                }
 
                argc--; argv++;
@@ -331,34 +326,26 @@ int tc_filter_list(int argc, char **argv)
 
        t.tcm_info = TC_H_MAKE(prio<<16, protocol);
 
-       if (rtnl_open(&rth, 0) < 0) {
-               fprintf(stderr, "Cannot open rtnetlink\n");
-               exit(1);
-       }
-
        ll_init_map(&rth);
 
        if (d[0]) {
                if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
                        fprintf(stderr, "Cannot find device \"%s\"\n", d);
-                       rtnl_close(&rth);
-                       exit(1);
+                       return 1;
                }
                filter_ifindex = t.tcm_ifindex;
        }
 
        if (rtnl_dump_request(&rth, RTM_GETTFILTER, &t, sizeof(t)) < 0) {
                perror("Cannot send dump request");
-               rtnl_close(&rth);
-               exit(1);
+               return 1;
        }
 
-       if (rtnl_dump_filter(&rth, print_filter, stdout, NULL, NULL) < 0) {
+       if (rtnl_dump_filter(&rth, print_filter, stdout) < 0) {
                fprintf(stderr, "Dump terminated\n");
-               exit(1);
+               return 1;
        }
 
-       rtnl_close(&rth);
        return 0;
 }
 
@@ -381,9 +368,11 @@ int do_filter(int argc, char **argv)
        if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
            || matches(*argv, "lst") == 0)
                return tc_filter_list(argc-1, argv+1);
-       if (matches(*argv, "help") == 0)
+       if (matches(*argv, "help") == 0) {
                usage();
+               return 0;
+        }
        fprintf(stderr, "Command \"%s\" is unknown, try \"tc filter help\".\n", *argv);
-       exit(-1);
+       return -1;
 }