genetlink: allow dumping command-specific policy
authorJakub Kicinski <kuba@kernel.org>
Sat, 3 Oct 2020 08:44:46 +0000 (10:44 +0200)
committerDavid S. Miller <davem@davemloft.net>
Sat, 3 Oct 2020 21:18:29 +0000 (14:18 -0700)
Right now CTRL_CMD_GETPOLICY can only dump the family-wide
policy. Support dumping policy of a specific op.

v3:
 - rebase after per-op policy export and handle that
v2:
 - make cmd U32, just in case.
v1:
 - don't echo op in the output in a naive way, this should
   make it cleaner to extend the output format for dumping
   policies for all the commands at once in the future.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Link: https://lore.kernel.org/r/20201001225933.1373426-11-kuba@kernel.org
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/uapi/linux/genetlink.h
net/netlink/genetlink.c

index bc9c98e848280ef35ef133d6376fd452bef9c31b..d83f214b4134ab518ecc37318d506df3de987ead 100644 (file)
@@ -65,6 +65,7 @@ enum {
        CTRL_ATTR_MCAST_GROUPS,
        CTRL_ATTR_POLICY,
        CTRL_ATTR_OP_POLICY,
+       CTRL_ATTR_OP,
        __CTRL_ATTR_MAX,
 };
 
index eb916c44884f18dc8edb42928703f868fdd3dd31..c992424e4d632d9f656f603d8ad27639eefb8fba 100644 (file)
@@ -123,7 +123,7 @@ static void genl_op_from_full(const struct genl_family *family,
                op->policy = family->policy;
 }
 
-static int genl_get_cmd_full(u8 cmd, const struct genl_family *family,
+static int genl_get_cmd_full(u32 cmd, const struct genl_family *family,
                             struct genl_ops *op)
 {
        int i;
@@ -152,7 +152,7 @@ static void genl_op_from_small(const struct genl_family *family,
        op->policy = family->policy;
 }
 
-static int genl_get_cmd_small(u8 cmd, const struct genl_family *family,
+static int genl_get_cmd_small(u32 cmd, const struct genl_family *family,
                              struct genl_ops *op)
 {
        int i;
@@ -166,7 +166,7 @@ static int genl_get_cmd_small(u8 cmd, const struct genl_family *family,
        return -ENOENT;
 }
 
-static int genl_get_cmd(u8 cmd, const struct genl_family *family,
+static int genl_get_cmd(u32 cmd, const struct genl_family *family,
                        struct genl_ops *op)
 {
        if (!genl_get_cmd_full(cmd, family, op))
@@ -1114,14 +1114,17 @@ struct ctrl_dump_policy_ctx {
        struct netlink_policy_dump_state *state;
        const struct genl_family *rt;
        unsigned int opidx;
+       u32 op;
        u16 fam_id;
-       u8 policies:1;
+       u8 policies:1,
+          single_op:1;
 };
 
 static const struct nla_policy ctrl_policy_policy[] = {
        [CTRL_ATTR_FAMILY_ID]   = { .type = NLA_U16 },
        [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
                                    .len = GENL_NAMSIZ - 1 },
+       [CTRL_ATTR_OP]          = { .type = NLA_U32 },
 };
 
 static int ctrl_dumppolicy_start(struct netlink_callback *cb)
@@ -1154,6 +1157,23 @@ static int ctrl_dumppolicy_start(struct netlink_callback *cb)
 
        ctx->rt = rt;
 
+       if (tb[CTRL_ATTR_OP]) {
+               ctx->single_op = true;
+               ctx->op = nla_get_u32(tb[CTRL_ATTR_OP]);
+
+               err = genl_get_cmd(ctx->op, rt, &op);
+               if (err) {
+                       NL_SET_BAD_ATTR(cb->extack, tb[CTRL_ATTR_OP]);
+                       return err;
+               }
+
+               if (!op.policy)
+                       return -ENODATA;
+
+               return netlink_policy_dump_add_policy(&ctx->state, op.policy,
+                                                     op.maxattr);
+       }
+
        for (i = 0; i < genl_get_cmd_cnt(rt); i++) {
                genl_get_cmd_by_index(i, rt, &op);
 
@@ -1248,7 +1268,18 @@ static int ctrl_dumppolicy(struct sk_buff *skb, struct netlink_callback *cb)
                while (ctx->opidx < genl_get_cmd_cnt(ctx->rt)) {
                        struct genl_ops op;
 
-                       genl_get_cmd_by_index(ctx->opidx, ctx->rt, &op);
+                       if (ctx->single_op) {
+                               int err;
+
+                               err = genl_get_cmd(ctx->op, ctx->rt, &op);
+                               if (WARN_ON(err))
+                                       return skb->len;
+
+                               /* break out of the loop after this one */
+                               ctx->opidx = genl_get_cmd_cnt(ctx->rt);
+                       } else {
+                               genl_get_cmd_by_index(ctx->opidx, ctx->rt, &op);
+                       }
 
                        if (ctrl_dumppolicy_put_op(skb, cb, &op))
                                return skb->len;