From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: phil@nwl.cc
Subject: [PATCH libnftnl 2/3] desc: add datatype description
Date: Thu, 20 Jan 2022 01:04:01 +0100 [thread overview]
Message-ID: <20220120000402.916332-3-pablo@netfilter.org> (raw)
In-Reply-To: <20220120000402.916332-1-pablo@netfilter.org>
Add a new object to describe a datatype. This allows to describe the
set key when type is used to define the set.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
include/desc.h | 6 +++
include/libnftnl/desc.h | 19 ++++++++
src/desc.c | 96 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 121 insertions(+)
diff --git a/include/desc.h b/include/desc.h
index d78af3528118..2f61a1e5963e 100644
--- a/include/desc.h
+++ b/include/desc.h
@@ -16,4 +16,10 @@ struct nftnl_expr_desc {
};
};
+struct nftnl_dtype_desc {
+ uint32_t dtype;
+ uint32_t byteorder;
+ uint32_t len;
+};
+
#endif
diff --git a/include/libnftnl/desc.h b/include/libnftnl/desc.h
index 1202eb9ddf79..cb1cac91f934 100644
--- a/include/libnftnl/desc.h
+++ b/include/libnftnl/desc.h
@@ -50,6 +50,25 @@ int nftnl_expr_desc_build(struct nftnl_udata_buf *udbuf,
int nftnl_expr_desc_parse(const struct nftnl_udata *attr,
struct nftnl_expr_desc *dexpr);
+enum nftnl_dtype_desc_types {
+ NFTNL_DESC_DTYPE_TYPE,
+ NFTNL_DESC_DTYPE_BYTEORDER,
+ NFTNL_DESC_DTYPE_LEN,
+ __NFTNL_DESC_DTYPE_MAX
+};
+#define NFTNL_DESC_DTYPE_MAX (__NFTNL_DESC_DTYPE_MAX - 1)
+
+struct nftnl_dtype_desc;
+
+struct nftnl_dtype_desc *nftnl_dtype_desc_alloc(void);
+void nftnl_dtype_desc_free(struct nftnl_dtype_desc *dtype);
+int nftnl_dtype_desc_set(struct nftnl_dtype_desc *dtype, uint8_t type,
+ const void *data, uint32_t data_len);
+int nftnl_dtype_desc_build(struct nftnl_udata_buf *udbuf,
+ const struct nftnl_dtype_desc *dtype);
+int nftnl_dtype_desc_parse(const struct nftnl_udata *attr,
+ struct nftnl_dtype_desc *dtype);
+
#ifdef __cplusplus
} /* extern "C" */
#endif
diff --git a/src/desc.c b/src/desc.c
index d8566ba2ee38..f73e74c2c7d3 100644
--- a/src/desc.c
+++ b/src/desc.c
@@ -140,3 +140,99 @@ int nftnl_expr_desc_parse(const struct nftnl_udata *attr,
return err;
}
+
+EXPORT_SYMBOL(nftnl_dtype_desc_alloc);
+struct nftnl_dtype_desc *nftnl_dtype_desc_alloc(void)
+{
+ return calloc(1, sizeof(struct nftnl_dtype_desc));
+}
+
+EXPORT_SYMBOL(nftnl_dtype_desc_free);
+void nftnl_dtype_desc_free(struct nftnl_dtype_desc *dtype)
+{
+ free(dtype);
+}
+
+EXPORT_SYMBOL(nftnl_dtype_desc_set);
+int nftnl_dtype_desc_set(struct nftnl_dtype_desc *dtype, uint8_t type,
+ const void *data, uint32_t data_len)
+{
+ int err = 0;
+
+ switch (type) {
+ case NFTNL_DESC_DTYPE_TYPE:
+ memcpy(&dtype->dtype, data, data_len);
+ break;
+ case NFTNL_DESC_DTYPE_BYTEORDER:
+ memcpy(&dtype->byteorder, data, data_len);
+ break;
+ case NFTNL_DESC_DTYPE_LEN:
+ memcpy(&dtype->len, data, data_len);
+ break;
+ default:
+ err = -1;
+ break;
+ }
+
+ return err;
+}
+
+#define NFTNL_UDATA_DTYPE_TYPE 0
+#define NFTNL_UDATA_DTYPE_BYTEORDER 1
+#define NFTNL_UDATA_DTYPE_LEN 2
+#define NFTNL_UDATA_DTYPE_MAX 3
+
+EXPORT_SYMBOL(nftnl_dtype_desc_build);
+int nftnl_dtype_desc_build(struct nftnl_udata_buf *udbuf,
+ const struct nftnl_dtype_desc *dtype)
+{
+ nftnl_udata_put_u32(udbuf, NFTNL_UDATA_DTYPE_TYPE, dtype->dtype);
+ nftnl_udata_put_u32(udbuf, NFTNL_UDATA_DTYPE_BYTEORDER, dtype->byteorder);
+ nftnl_udata_put_u32(udbuf, NFTNL_UDATA_DTYPE_LEN, dtype->len);
+
+ return 0;
+}
+
+static int nftnl_dtype_desc_parse_nested(const struct nftnl_udata *attr, void *data)
+{
+ const struct nftnl_udata **ud = data;
+ uint8_t type = nftnl_udata_type(attr);
+ uint8_t len = nftnl_udata_len(attr);
+
+ switch (type) {
+ case NFTNL_UDATA_DTYPE_TYPE:
+ case NFTNL_UDATA_DTYPE_BYTEORDER:
+ case NFTNL_UDATA_DTYPE_LEN:
+ if (len != sizeof(uint32_t))
+ return -1;
+ break;
+ default:
+ return 0;
+ }
+
+ ud[type] = attr;
+ return 0;
+}
+
+EXPORT_SYMBOL(nftnl_dtype_desc_parse);
+int nftnl_dtype_desc_parse(const struct nftnl_udata *attr,
+ struct nftnl_dtype_desc *dtype)
+{
+ const struct nftnl_udata *ud[NFTNL_UDATA_DTYPE_MAX + 1] = {};
+ int err;
+
+ err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr),
+ nftnl_dtype_desc_parse_nested, ud);
+ if (err < 0)
+ return -1;
+
+ err = 0;
+ if (ud[NFTNL_UDATA_DTYPE_TYPE])
+ dtype->dtype = nftnl_udata_get_u32(ud[NFTNL_UDATA_DTYPE_TYPE]);
+ if (ud[NFTNL_UDATA_DTYPE_BYTEORDER])
+ dtype->byteorder = nftnl_udata_get_u32(ud[NFTNL_UDATA_DTYPE_BYTEORDER]);
+ if (ud[NFTNL_UDATA_DTYPE_LEN])
+ dtype->len = nftnl_udata_get_u32(ud[NFTNL_UDATA_DTYPE_LEN]);
+
+ return err;
+}
--
2.30.2
next prev parent reply other threads:[~2022-01-20 0:04 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-20 0:03 [PATCH libnftnl 0/3] add description infrastructure Pablo Neira Ayuso
2022-01-20 0:04 ` [PATCH libnftnl 1/3] desc: add expression description Pablo Neira Ayuso
2022-01-20 0:04 ` Pablo Neira Ayuso [this message]
2022-01-20 0:04 ` [PATCH libnftnl 3/3] desc: add set description Pablo Neira Ayuso
2022-03-10 11:31 ` Phil Sutter
2022-03-10 23:24 ` Pablo Neira Ayuso
2022-03-11 14:03 ` Phil Sutter
2022-03-10 11:35 ` [PATCH libnftnl 0/3] add description infrastructure Phil Sutter
2022-03-10 23:28 ` Pablo Neira Ayuso
2022-04-06 11:06 ` Phil Sutter
2022-04-06 11:57 ` Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220120000402.916332-3-pablo@netfilter.org \
--to=pablo@netfilter.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=phil@nwl.cc \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.