Skip to contentSkip to conversation

expr: add support to math expression

RFC PATCH Superseded Fernando Fernandez Mancera 1 participant 1 message Sep 23, 2025 netfilter

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 expr: add support to math expression

---
 include/libnftnl/expr.h             |   9 ++
 include/linux/netfilter/nf_tables.h |  34 +++++
 src/Makefile.am                     |   1 +
 src/expr/math.c                     | 207 ++++++++++++++++++++++++++++
 src/expr_ops.c                      |   2 +
 5 files changed, 253 insertions(+)
 create mode 100644 src/expr/math.c
include/libnftnl/expr.h +9 -0
index 1c07b54..b0c36b9 100644
@@ -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_BYTEORDER,
+	__NFTNL_EXPR_MATH_MAX
+};
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
include/linux/netfilter/nf_tables.h +34 -0
index 2beb30b..40bca48 100644
@@ -2011,4 +2011,38 @@ 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_U8)
+ * @NFTA_MATH_LEN: value length in bits (NLA_U8)
+ * @NFTA_MATH_BYTEORDER: byteorder of the value passed to the SREG (NLA_U8)
+ */
+enum nft_math_attributes {
+	NFTA_MATH_UNSPEC,
+	NFTA_MATH_SREG,
+	NFTA_MATH_DREG,
+	NFTA_MATH_OP,
+	NFTA_MATH_LEN,
+	NFTA_MATH_BYTEORDER,
+	__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)
+
+enum nft_math_byteorder {
+	NFT_MATH_BYTEORDER_HOST,
+	NFT_MATH_BYTEORDER_BIG,
+	__NFT_MATH_BYTEORDER_MAX,
+};
+#define NFT_MATH_BYTEORDER_MAX (__NFT_MATH_BYTEORDER_MAX - 1)
+
 #endif /* _LINUX_NF_TABLES_H */
src/Makefile.am +1 -0
index 1c38d00..90eafc4 100644
@@ -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	\
src/expr/math.c new file +207 -0

207 lines not on this page · raw message

src/expr_ops.c +2 -0
index b85f472..b654fa0 100644
@@ -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,
 };
 
Signature · 1 line
2.51.0