]> 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 f3319d55296dc203a1526936ac9cf298601fbb6e..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>
@@ -32,22 +30,22 @@ static void usage(void);
 
 static void usage(void)
 {
-       fprintf(stderr, "Usage: tc filter [ add | del | change | replace ] 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");
        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 {
                struct nlmsghdr         n;
@@ -57,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];
@@ -73,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();
@@ -91,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();
@@ -103,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);
 
@@ -155,7 +159,7 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
 
 
        if (d[0])  {
-               ll_init_map(&rth);
+               ll_init_map(&rth);
 
                if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
                        fprintf(stderr, "Cannot find device \"%s\"\n", d);
@@ -163,7 +167,7 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
                }
        }
 
-       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");
                return 2;
        }
@@ -175,9 +179,10 @@ static __u32 filter_parent;
 static int filter_ifindex;
 static __u32 filter_prio;
 static __u32 filter_protocol;
+__u16 f_proto = 0;
 
 int print_filter(const struct sockaddr_nl *who,
-                       struct nlmsghdr *n, 
+                       struct nlmsghdr *n,
                        void *arg)
 {
        FILE *fp = (FILE*)arg;
@@ -221,13 +226,13 @@ 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) {
@@ -235,7 +240,7 @@ 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)
@@ -255,7 +260,7 @@ 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;
        char d[16];
@@ -285,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();
@@ -298,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;
@@ -306,7 +311,7 @@ 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) {
@@ -321,7 +326,7 @@ int tc_filter_list(int argc, char **argv)
 
        t.tcm_info = TC_H_MAKE(prio<<16, protocol);
 
-       ll_init_map(&rth);
+       ll_init_map(&rth);
 
        if (d[0]) {
                if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
@@ -331,12 +336,12 @@ int tc_filter_list(int argc, char **argv)
                filter_ifindex = t.tcm_ifindex;
        }
 
-       if (rtnl_dump_request(&rth, RTM_GETTFILTER, &t, sizeof(t)) < 0) {
+       if (rtnl_dump_request(&rth, RTM_GETTFILTER, &t, sizeof(t)) < 0) {
                perror("Cannot send dump request");
                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");
                return 1;
        }
@@ -363,8 +368,10 @@ 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);
        return -1;
 }