From: Fernando Fernandez Mancera <fmancera@suse.de>
To: netfilter-devel@vger.kernel.org
Cc: coreteam@netfilter.org, fw@strlen.de, pablo@netfilter.org,
Fernando Fernandez Mancera <fmancera@suse.de>
Subject: [PATCH libnftnl v2] expr: add support to math expression
Date: Mon, 3 Nov 2025 15:28:49 +0100 [thread overview]
Message-ID: <20251103142849.23240-1-fmancera@suse.de> (raw)
Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
---
v2: adjusted the new fields and fixed the duplicated "math" on print
---
include/libnftnl/expr.h | 9 ++
include/linux/netfilter/nf_tables.h | 27 ++++
src/Makefile.am | 1 +
src/expr/math.c | 206 ++++++++++++++++++++++++++++
src/expr_ops.c | 2 +
5 files changed, 245 insertions(+)
create mode 100644 src/expr/math.c
diff --git a/include/libnftnl/expr.h b/include/libnftnl/expr.h
index 1c07b54..779ad5a 100644
--- a/include/libnftnl/expr.h
+++ b/include/libnftnl/expr.h
@@ -363,6 +363,15 @@ enum {
__NFTNL_EXPR_INNER_MAX
};
+enum {
+ NFTNL_EXPR_MATH_SREG = NFTNL_EXPR_BASE,
+ NFTNL_EXPR_MATH_DREG,
+ NFTNL_EXPR_MATH_OP,
+ NFTNL_EXPR_MATH_LEN,
+ NFTNL_EXPR_MATH_BITMASK,
+ __NFTNL_EXPR_MATH_MAX
+};
+
#ifdef __cplusplus
} /* extern "C" */
#endif
diff --git a/include/linux/netfilter/nf_tables.h b/include/linux/netfilter/nf_tables.h
index 7c0c915..06e91cd 100644
--- a/include/linux/netfilter/nf_tables.h
+++ b/include/linux/netfilter/nf_tables.h
@@ -2015,4 +2015,31 @@ enum nft_tunnel_attributes {
};
#define NFTA_TUNNEL_MAX (__NFTA_TUNNEL_MAX - 1)
+/**
+ * enum nft_math_attributes - nftables math expression netlink attributes
+ *
+ * @NFTA_MATH_SREG: source register (NLA_U32: nft_registers)
+ * @NFTA_MATH_DREG: destination register (NLA_U32: nft_registers)
+ * @NFTA_MATH_OP: operation to be performed (NLA_U32)
+ * @NFTA_MATH_BITMASK: bitmask to be applied on the operation (NLA_U32)
+ * @NFTA_MATH_LEN: value length in bits (NLA_U32)
+ */
+enum nft_math_attributes {
+ NFTA_MATH_UNSPEC,
+ NFTA_MATH_SREG,
+ NFTA_MATH_DREG,
+ NFTA_MATH_OP,
+ NFTA_MATH_BITMASK,
+ NFTA_MATH_LEN,
+ __NFTA_MATH_MAX,
+};
+#define NFTA_MATH_MAX (__NFTA_MATH_MAX - 1)
+
+enum nft_math_op {
+ NFT_MATH_OP_INC,
+ NFT_MATH_OP_DEC,
+ __NFT_MATH_OP_MAX,
+};
+#define NFT_MATH_OP_MAX (__NFT_MATH_OP_MAX - 1)
+
#endif /* _LINUX_NF_TABLES_H */
diff --git a/src/Makefile.am b/src/Makefile.am
index 1c38d00..90eafc4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -62,6 +62,7 @@ libnftnl_la_SOURCES = utils.c \
expr/synproxy.c \
expr/osf.c \
expr/xfrm.c \
+ expr/math.c \
obj/counter.c \
obj/ct_helper.c \
obj/quota.c \
diff --git a/src/expr/math.c b/src/expr/math.c
new file mode 100644
index 0000000..48eba71
--- /dev/null
+++ b/src/expr/math.c
@@ -0,0 +1,206 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * (C) 2025 by Fernando Fernandez Mancera <fmancera@suse.de>
+ */
+
+#include <arpa/inet.h>
+#include <linux/netfilter/nf_tables.h>
+
+#include "internal.h"
+#include <libmnl/libmnl.h>
+#include <libnftnl/expr.h>
+#include <libnftnl/rule.h>
+
+struct nftnl_expr_math {
+ enum nft_registers sreg;
+ enum nft_registers dreg;
+ enum nft_math_op op;
+ uint32_t bitmask;
+ uint32_t len;
+};
+
+static int nftnl_expr_math_set(struct nftnl_expr *e, uint16_t type,
+ const void *data, uint32_t data_len)
+{
+ struct nftnl_expr_math *math = nftnl_expr_data(e);
+
+ switch(type) {
+ case NFTNL_EXPR_MATH_SREG:
+ memcpy(&math->sreg, data, data_len);
+ break;
+ case NFTNL_EXPR_MATH_DREG:
+ memcpy(&math->dreg, data, data_len);
+ break;
+ case NFTNL_EXPR_MATH_OP:
+ memcpy(&math->op, data, data_len);
+ break;
+ case NFTNL_EXPR_MATH_LEN:
+ memcpy(&math->len, data, data_len);
+ break;
+ case NFTNL_EXPR_MATH_BITMASK:
+ memcpy(&math->bitmask, data, data_len);
+ break;
+ }
+ return 0;
+}
+
+static const void *
+nftnl_expr_math_get(const struct nftnl_expr *e, uint16_t type,
+ uint32_t *data_len)
+{
+ struct nftnl_expr_math *math = nftnl_expr_data(e);
+
+ switch(type) {
+ case NFTNL_EXPR_MATH_SREG:
+ *data_len = sizeof(math->sreg);
+ return &math->sreg;
+ case NFTNL_EXPR_MATH_DREG:
+ *data_len = sizeof(math->dreg);
+ return &math->dreg;
+ case NFTNL_EXPR_MATH_OP:
+ *data_len = sizeof(math->op);
+ return &math->op;
+ case NFTNL_EXPR_MATH_LEN:
+ *data_len = sizeof(math->len);
+ return &math->len;
+ case NFTNL_EXPR_MATH_BITMASK:
+ *data_len = sizeof(math->bitmask);
+ return &math->bitmask;
+ }
+ return NULL;
+}
+
+static void
+nftnl_expr_math_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
+{
+ struct nftnl_expr_math *math = nftnl_expr_data(e);
+
+ if ((e->flags & (1 << NFTNL_EXPR_MATH_SREG)) &&
+ (e->flags & (1 << NFTNL_EXPR_MATH_DREG)) &&
+ (e->flags & (1 << NFTNL_EXPR_MATH_LEN)) &&
+ (e->flags & (1 << NFTNL_EXPR_MATH_OP))) {
+ if (e->flags & (1 << NFTNL_EXPR_MATH_BITMASK))
+ mnl_attr_put_u32(nlh, NFTA_MATH_BITMASK, htonl(math->bitmask));
+
+ mnl_attr_put_u32(nlh, NFTA_MATH_SREG, htonl(math->sreg));
+ mnl_attr_put_u32(nlh, NFTA_MATH_DREG, htonl(math->dreg));
+ mnl_attr_put_u32(nlh, NFTA_MATH_OP, htonl(math->op));
+ mnl_attr_put_u32(nlh, NFTA_MATH_LEN, htonl(math->len));
+ }
+
+}
+
+static int nftnl_expr_math_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ if (mnl_attr_type_valid(attr, NFTA_MATH_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case NFTNL_EXPR_MATH_SREG:
+ case NFTNL_EXPR_MATH_DREG:
+ case NFTNL_EXPR_MATH_BITMASK:
+ case NFTNL_EXPR_MATH_OP:
+ case NFTNL_EXPR_MATH_LEN:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static int
+nftnl_expr_math_parse(struct nftnl_expr *e, struct nlattr *attr)
+{
+ struct nftnl_expr_math *math = nftnl_expr_data(e);
+ struct nlattr *tb[NFTA_MATH_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_expr_math_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_MATH_SREG]) {
+ math->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_MATH_SREG]));
+ e->flags |= (1 << NFTNL_EXPR_MATH_SREG);
+ }
+
+ if (tb[NFTA_MATH_DREG]) {
+ math->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_MATH_DREG]));
+ e->flags |= (1 << NFTNL_EXPR_MATH_DREG);
+ }
+
+ if (tb[NFTA_MATH_LEN]) {
+ math->len = ntohl(mnl_attr_get_u32(tb[NFTA_MATH_LEN]));
+ e->flags |= (1 << NFTNL_EXPR_MATH_LEN);
+ }
+
+ if (tb[NFTA_MATH_OP]) {
+ math->op = ntohl(mnl_attr_get_u32(tb[NFTA_MATH_OP]));
+ e->flags |= (1 << NFTNL_EXPR_MATH_OP);
+ }
+
+ if (tb[NFTA_MATH_BITMASK]) {
+ math->bitmask = ntohl(mnl_attr_get_u32(tb[NFTA_MATH_BITMASK]));
+ e->flags |= (1 << NFTNL_EXPR_MATH_BITMASK);
+ }
+
+ return 0;
+}
+
+static const char op2char(enum nft_math_op op)
+{
+ switch (op) {
+ case NFT_MATH_OP_INC:
+ return '+';
+ case NFT_MATH_OP_DEC:
+ return '-';
+ default:
+ return '?';
+ }
+}
+
+static int
+nftnl_expr_math_snprintf(char *buf, size_t len,
+ uint32_t flags, const struct nftnl_expr *e)
+{
+ struct nftnl_expr_math *math = nftnl_expr_data(e);
+ int ret, offset = 0;
+
+ ret = snprintf(buf, len, "%u bits", math->len);
+ SNPRINTF_BUFFER_SIZE(ret, len, offset);
+
+ if (e->flags & (1 << NFTNL_EXPR_MATH_BITMASK)) {
+ ret = snprintf(buf + offset, len, " mask 0x%.8x", math->bitmask);
+ SNPRINTF_BUFFER_SIZE(ret, len, offset);
+ }
+
+ ret = snprintf(buf + offset, len, " reg %u %c 1 => %u",
+ math->sreg, op2char(math->op), math->dreg);
+ SNPRINTF_BUFFER_SIZE(ret, len, offset);
+
+ return offset;
+}
+
+static struct attr_policy math_attr_policy[__NFTNL_EXPR_MATH_MAX] = {
+ [NFTNL_EXPR_MATH_SREG] = { .maxlen = sizeof(uint32_t) },
+ [NFTNL_EXPR_MATH_DREG] = { .maxlen = sizeof(uint32_t) },
+ [NFTNL_EXPR_MATH_BITMASK] = { .maxlen = sizeof(uint32_t) },
+ [NFTNL_EXPR_MATH_OP] = { .maxlen = sizeof(uint32_t) },
+ [NFTNL_EXPR_MATH_LEN] = { .maxlen = sizeof(uint32_t) },
+
+};
+
+struct expr_ops expr_ops_math = {
+ .name = "math",
+ .alloc_len = sizeof(struct nftnl_expr_math),
+ .nftnl_max_attr = __NFTNL_EXPR_MATH_MAX - 1,
+ .attr_policy = math_attr_policy,
+ .set = nftnl_expr_math_set,
+ .get = nftnl_expr_math_get,
+ .parse = nftnl_expr_math_parse,
+ .build = nftnl_expr_math_build,
+ .output = nftnl_expr_math_snprintf,
+};
diff --git a/src/expr_ops.c b/src/expr_ops.c
index b85f472..b654fa0 100644
--- a/src/expr_ops.c
+++ b/src/expr_ops.c
@@ -43,6 +43,7 @@ extern struct expr_ops expr_ops_synproxy;
extern struct expr_ops expr_ops_tunnel;
extern struct expr_ops expr_ops_osf;
extern struct expr_ops expr_ops_xfrm;
+extern struct expr_ops expr_ops_math;
static struct expr_ops expr_ops_notrack = {
.name = "notrack",
@@ -89,6 +90,7 @@ static struct expr_ops *expr_ops[] = {
&expr_ops_tunnel,
&expr_ops_osf,
&expr_ops_xfrm,
+ &expr_ops_math,
NULL,
};
--
2.51.0
reply other threads:[~2025-11-03 14:29 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20251103142849.23240-1-fmancera@suse.de \
--to=fmancera@suse.de \
--cc=coreteam@netfilter.org \
--cc=fw@strlen.de \
--cc=netfilter-devel@vger.kernel.org \
--cc=pablo@netfilter.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).