]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
utils: extract CTRL_ATTR_MAXATTR and save it
authorJacob Keller <jacob.e.keller@intel.com>
Fri, 26 Aug 2022 18:17:41 +0000 (11:17 -0700)
committerDavid Ahern <dsahern@kernel.org>
Thu, 1 Sep 2022 02:59:03 +0000 (20:59 -0600)
mnlu_gen_socket_open opens a socket and configures it for use with a
generic netlink family. As part of this process it sends a
CTRL_CMD_GETFAMILY to get the ID for the family name requested.

In addition to the family id, this command reports a few other useful
values including the maximum attribute. The maximum attribute is useful in
order to know whether a given attribute is supported and for knowing the
necessary size to allocate for other operations such as policy dumping.

Since we already have to issue a CTRL_CMD_GETFAMILY to get the id, we can
also store the maximum attribute as well. Modify the callback functions to
parse the maximum attribute NLA and store it in the mnlu_gen_socket
structure.

Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
include/mnl_utils.h
lib/mnl_utils.c

index aa5f0a9b17c72228f1997478a6b3e9fd09bff927..2193934849e1923f67d38674cdf53eb7c5e99cbb 100644 (file)
@@ -6,6 +6,7 @@ struct mnlu_gen_socket {
        struct mnl_socket *nl;
        char *buf;
        uint32_t family;
+       uint32_t maxattr;
        unsigned int seq;
        uint8_t version;
 };
index d5abff58d816ceb92971df8e61b0e19fd7451ed7..79bac5cfd4de3d3607cb1fb0221d9d1b618bfa1c 100644 (file)
@@ -110,7 +110,7 @@ int mnlu_socket_recv_run(struct mnl_socket *nl, unsigned int seq, void *buf, siz
        return err;
 }
 
-static int get_family_id_attr_cb(const struct nlattr *attr, void *data)
+static int get_family_attrs_cb(const struct nlattr *attr, void *data)
 {
        int type = mnl_attr_get_type(attr);
        const struct nlattr **tb = data;
@@ -121,20 +121,26 @@ static int get_family_id_attr_cb(const struct nlattr *attr, void *data)
        if (type == CTRL_ATTR_FAMILY_ID &&
            mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
                return MNL_CB_ERROR;
+       if (type == CTRL_ATTR_MAXATTR &&
+           mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+               return MNL_CB_ERROR;
        tb[type] = attr;
        return MNL_CB_OK;
 }
 
-static int get_family_id_cb(const struct nlmsghdr *nlh, void *data)
+static int get_family_cb(const struct nlmsghdr *nlh, void *data)
 {
        struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
        struct nlattr *tb[CTRL_ATTR_MAX + 1] = {};
-       uint32_t *p_id = data;
+       struct mnlu_gen_socket *nlg = data;
 
-       mnl_attr_parse(nlh, sizeof(*genl), get_family_id_attr_cb, tb);
+       mnl_attr_parse(nlh, sizeof(*genl), get_family_attrs_cb, tb);
        if (!tb[CTRL_ATTR_FAMILY_ID])
                return MNL_CB_ERROR;
-       *p_id = mnl_attr_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
+       if (!tb[CTRL_ATTR_MAXATTR])
+               return MNL_CB_ERROR;
+       nlg->family = mnl_attr_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
+       nlg->maxattr = mnl_attr_get_u32(tb[CTRL_ATTR_MAXATTR]);
        return MNL_CB_OK;
 }
 
@@ -159,7 +165,7 @@ static int family_get(struct mnlu_gen_socket *nlg, const char *family_name)
 
        err = mnlu_socket_recv_run(nlg->nl, nlh->nlmsg_seq, nlg->buf,
                                   MNL_SOCKET_BUFFER_SIZE,
-                                  get_family_id_cb, &nlg->family);
+                                  get_family_cb, nlg);
        return err;
 }