Skip to content

Commit

Permalink
Merge branch 'net-genetlink-parse-attrs-for-dumpit-callback'
Browse files Browse the repository at this point in the history
Jiri Pirko says:

====================
net: genetlink: parse attrs for dumpit() callback

In generic netlink, parsing attributes for doit() callback is already
implemented. They are available in info->attrs.

For dumpit() however, each user which is interested in attributes have to
parse it manually. Even though the attributes may be (depending on flag)
already validated (by parse function).

Make usage of attributes in dumpit() more convenient and prepare
info->attrs too.

Patchset also make the existing users of genl_family_attrbuf() converted
to use info->attrs and removes the helper.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
David S. Miller committed Oct 6, 2019
2 parents 248d45f + ee85da5 commit d44dc74
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 225 deletions.
20 changes: 18 additions & 2 deletions include/net/genetlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ struct genl_family {
struct module *module;
};

struct nlattr **genl_family_attrbuf(const struct genl_family *family);

/**
* struct genl_info - receiving information
* @snd_seq: sending sequence number
Expand Down Expand Up @@ -127,6 +125,24 @@ enum genl_validate_flags {
GENL_DONT_VALIDATE_DUMP_STRICT = BIT(2),
};

/**
* struct genl_info - info that is available during dumpit op call
* @family: generic netlink family - for internal genl code usage
* @ops: generic netlink ops - for internal genl code usage
* @attrs: netlink attributes
*/
struct genl_dumpit_info {
const struct genl_family *family;
const struct genl_ops *ops;
struct nlattr **attrs;
};

static inline const struct genl_dumpit_info *
genl_dumpit_info(struct netlink_callback *cb)
{
return cb->data;
}

/**
* struct genl_ops - generic netlink operations
* @cmd: command identifier
Expand Down
38 changes: 6 additions & 32 deletions net/core/devlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -3943,29 +3943,19 @@ static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
struct netlink_callback *cb)
{
const struct genl_dumpit_info *info = genl_dumpit_info(cb);
u64 ret_offset, start_offset, end_offset = 0;
struct nlattr **attrs = info->attrs;
struct devlink_region *region;
struct nlattr *chunks_attr;
const char *region_name;
struct devlink *devlink;
struct nlattr **attrs;
bool dump = true;
void *hdr;
int err;

start_offset = *((u64 *)&cb->args[0]);

attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL);
if (!attrs)
return -ENOMEM;

err = nlmsg_parse_deprecated(cb->nlh,
GENL_HDRLEN + devlink_nl_family.hdrsize,
attrs, DEVLINK_ATTR_MAX,
devlink_nl_family.policy, cb->extack);
if (err)
goto out_free;

mutex_lock(&devlink_mutex);
devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
if (IS_ERR(devlink)) {
Expand Down Expand Up @@ -4042,7 +4032,6 @@ static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
genlmsg_end(skb, hdr);
mutex_unlock(&devlink->lock);
mutex_unlock(&devlink_mutex);
kfree(attrs);

return skb->len;

Expand All @@ -4052,8 +4041,6 @@ static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
mutex_unlock(&devlink->lock);
out_dev:
mutex_unlock(&devlink_mutex);
out_free:
kfree(attrs);
return err;
}

Expand Down Expand Up @@ -4995,21 +4982,10 @@ devlink_health_reporter_get_from_info(struct devlink *devlink,
static struct devlink_health_reporter *
devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
{
const struct genl_dumpit_info *info = genl_dumpit_info(cb);
struct devlink_health_reporter *reporter;
struct nlattr **attrs = info->attrs;
struct devlink *devlink;
struct nlattr **attrs;
int err;

attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL);
if (!attrs)
return NULL;

err = nlmsg_parse_deprecated(cb->nlh,
GENL_HDRLEN + devlink_nl_family.hdrsize,
attrs, DEVLINK_ATTR_MAX,
devlink_nl_family.policy, cb->extack);
if (err)
goto free;

mutex_lock(&devlink_mutex);
devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
Expand All @@ -5018,12 +4994,9 @@ devlink_health_reporter_get_from_cb(struct netlink_callback *cb)

reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
mutex_unlock(&devlink_mutex);
kfree(attrs);
return reporter;
unlock:
mutex_unlock(&devlink_mutex);
free:
kfree(attrs);
return NULL;
}

Expand Down Expand Up @@ -6154,7 +6127,8 @@ static const struct genl_ops devlink_nl_ops[] = {
},
{
.cmd = DEVLINK_CMD_REGION_READ,
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
.validate = GENL_DONT_VALIDATE_STRICT |
GENL_DONT_VALIDATE_DUMP_STRICT,
.dumpit = devlink_nl_cmd_region_read_dumpit,
.flags = GENL_ADMIN_PERM,
.internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Expand Down
39 changes: 14 additions & 25 deletions net/ieee802154/nl802154.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,21 +236,14 @@ nl802154_prepare_wpan_dev_dump(struct sk_buff *skb,
struct cfg802154_registered_device **rdev,
struct wpan_dev **wpan_dev)
{
const struct genl_dumpit_info *info = genl_dumpit_info(cb);
int err;

rtnl_lock();

if (!cb->args[0]) {
err = nlmsg_parse_deprecated(cb->nlh,
GENL_HDRLEN + nl802154_fam.hdrsize,
genl_family_attrbuf(&nl802154_fam),
nl802154_fam.maxattr,
nl802154_policy, NULL);
if (err)
goto out_unlock;

*wpan_dev = __cfg802154_wpan_dev_from_attrs(sock_net(skb->sk),
genl_family_attrbuf(&nl802154_fam));
info->attrs);
if (IS_ERR(*wpan_dev)) {
err = PTR_ERR(*wpan_dev);
goto out_unlock;
Expand Down Expand Up @@ -557,17 +550,8 @@ static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb,
struct netlink_callback *cb,
struct nl802154_dump_wpan_phy_state *state)
{
struct nlattr **tb = genl_family_attrbuf(&nl802154_fam);
int ret = nlmsg_parse_deprecated(cb->nlh,
GENL_HDRLEN + nl802154_fam.hdrsize,
tb, nl802154_fam.maxattr,
nl802154_policy, NULL);

/* TODO check if we can handle error here,
* we have no backward compatibility
*/
if (ret)
return 0;
const struct genl_dumpit_info *info = genl_dumpit_info(cb);
struct nlattr **tb = info->attrs;

if (tb[NL802154_ATTR_WPAN_PHY])
state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]);
Expand Down Expand Up @@ -2203,7 +2187,8 @@ static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
static const struct genl_ops nl802154_ops[] = {
{
.cmd = NL802154_CMD_GET_WPAN_PHY,
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
.validate = GENL_DONT_VALIDATE_STRICT |
GENL_DONT_VALIDATE_DUMP_STRICT,
.doit = nl802154_get_wpan_phy,
.dumpit = nl802154_dump_wpan_phy,
.done = nl802154_dump_wpan_phy_done,
Expand Down Expand Up @@ -2343,7 +2328,8 @@ static const struct genl_ops nl802154_ops[] = {
},
{
.cmd = NL802154_CMD_GET_SEC_KEY,
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
.validate = GENL_DONT_VALIDATE_STRICT |
GENL_DONT_VALIDATE_DUMP_STRICT,
/* TODO .doit by matching key id? */
.dumpit = nl802154_dump_llsec_key,
.flags = GENL_ADMIN_PERM,
Expand All @@ -2369,7 +2355,8 @@ static const struct genl_ops nl802154_ops[] = {
/* TODO unique identifier must short+pan OR extended_addr */
{
.cmd = NL802154_CMD_GET_SEC_DEV,
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
.validate = GENL_DONT_VALIDATE_STRICT |
GENL_DONT_VALIDATE_DUMP_STRICT,
/* TODO .doit by matching extended_addr? */
.dumpit = nl802154_dump_llsec_dev,
.flags = GENL_ADMIN_PERM,
Expand All @@ -2395,7 +2382,8 @@ static const struct genl_ops nl802154_ops[] = {
/* TODO remove complete devkey, put it as nested? */
{
.cmd = NL802154_CMD_GET_SEC_DEVKEY,
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
.validate = GENL_DONT_VALIDATE_STRICT |
GENL_DONT_VALIDATE_DUMP_STRICT,
/* TODO doit by matching ??? */
.dumpit = nl802154_dump_llsec_devkey,
.flags = GENL_ADMIN_PERM,
Expand All @@ -2420,7 +2408,8 @@ static const struct genl_ops nl802154_ops[] = {
},
{
.cmd = NL802154_CMD_GET_SEC_LEVEL,
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
.validate = GENL_DONT_VALIDATE_STRICT |
GENL_DONT_VALIDATE_DUMP_STRICT,
/* TODO .doit by matching frame_type? */
.dumpit = nl802154_dump_llsec_seclevel,
.flags = GENL_ADMIN_PERM,
Expand Down
Loading

0 comments on commit d44dc74

Please sign in to comment.