genetlink: bring back per op policy

Add policy to the struct genl_ops structure, this time
with maxattr, so it can be used properly.

Propagate .policy and .maxattr from the family
in genl_get_cmd() if needed, this way the rest of the
code does not have to worry if the policy is per op
or global.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Reviewed-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Jakub Kicinski 2020-10-02 14:49:57 -07:00 committed by David S. Miller
parent 78ade619c1
commit 48526a0f4c
2 changed files with 17 additions and 5 deletions

View File

@ -137,6 +137,8 @@ struct genl_small_ops {
* @cmd: command identifier * @cmd: command identifier
* @internal_flags: flags used by the family * @internal_flags: flags used by the family
* @flags: flags * @flags: flags
* @maxattr: maximum number of attributes supported
* @policy: netlink policy (takes precedence over family policy)
* @doit: standard command callback * @doit: standard command callback
* @start: start callback for dumps * @start: start callback for dumps
* @dumpit: callback for dumpers * @dumpit: callback for dumpers
@ -149,6 +151,8 @@ struct genl_ops {
int (*dumpit)(struct sk_buff *skb, int (*dumpit)(struct sk_buff *skb,
struct netlink_callback *cb); struct netlink_callback *cb);
int (*done)(struct netlink_callback *cb); int (*done)(struct netlink_callback *cb);
const struct nla_policy *policy;
unsigned int maxattr;
u8 cmd; u8 cmd;
u8 internal_flags; u8 internal_flags;
u8 flags; u8 flags;

View File

@ -116,6 +116,11 @@ static void genl_op_from_full(const struct genl_family *family,
unsigned int i, struct genl_ops *op) unsigned int i, struct genl_ops *op)
{ {
*op = family->ops[i]; *op = family->ops[i];
if (!op->maxattr)
op->maxattr = family->maxattr;
if (!op->policy)
op->policy = family->policy;
} }
static int genl_get_cmd_full(u8 cmd, const struct genl_family *family, static int genl_get_cmd_full(u8 cmd, const struct genl_family *family,
@ -142,6 +147,9 @@ static void genl_op_from_small(const struct genl_family *family,
op->internal_flags = family->small_ops[i].internal_flags; op->internal_flags = family->small_ops[i].internal_flags;
op->flags = family->small_ops[i].flags; op->flags = family->small_ops[i].flags;
op->validate = family->small_ops[i].validate; op->validate = family->small_ops[i].validate;
op->maxattr = family->maxattr;
op->policy = family->policy;
} }
static int genl_get_cmd_small(u8 cmd, const struct genl_family *family, static int genl_get_cmd_small(u8 cmd, const struct genl_family *family,
@ -529,16 +537,16 @@ genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
struct nlattr **attrbuf; struct nlattr **attrbuf;
int err; int err;
if (!family->maxattr) if (!ops->maxattr)
return NULL; return NULL;
attrbuf = kmalloc_array(family->maxattr + 1, attrbuf = kmalloc_array(ops->maxattr + 1,
sizeof(struct nlattr *), GFP_KERNEL); sizeof(struct nlattr *), GFP_KERNEL);
if (!attrbuf) if (!attrbuf)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
err = __nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr, err = __nlmsg_parse(nlh, hdrlen, attrbuf, ops->maxattr, ops->policy,
family->policy, validate, extack); validate, extack);
if (err) { if (err) {
kfree(attrbuf); kfree(attrbuf);
return ERR_PTR(err); return ERR_PTR(err);
@ -845,7 +853,7 @@ static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
op_flags |= GENL_CMD_CAP_DUMP; op_flags |= GENL_CMD_CAP_DUMP;
if (op.doit) if (op.doit)
op_flags |= GENL_CMD_CAP_DO; op_flags |= GENL_CMD_CAP_DO;
if (family->policy) if (op.policy)
op_flags |= GENL_CMD_CAP_HASPOL; op_flags |= GENL_CMD_CAP_HASPOL;
nest = nla_nest_start_noflag(skb, i + 1); nest = nla_nest_start_noflag(skb, i + 1);