]> 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 d70c656dcfe561757a91c284d5e16c9e5523d916..9e416008eaa9b708142faf3e644db440ce699bde 100644 (file)
@@ -38,14 +38,14 @@ static void usage(void)
        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;
@@ -55,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];
@@ -71,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();
@@ -89,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();
@@ -101,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 priority 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);
 
@@ -127,11 +133,6 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
                argc--; argv++;
        }
 
-       if (!protocol) {
-               fprintf(stderr, "\"protocol\" is required.\n");
-               return -1;
-       }
-
        req.t.tcm_info = TC_H_MAKE(prio<<16, protocol);
 
        if (k[0])
@@ -158,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);
@@ -166,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;
        }
@@ -178,6 +179,7 @@ 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,
@@ -224,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) {
@@ -238,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)
@@ -258,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];
@@ -288,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();
@@ -301,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;
@@ -309,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) {
@@ -324,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) {
@@ -334,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;
        }