Skip to contentSkip to conversation

netfilter: nf_tables: add math expression support

PATCH New Fernando Fernandez Mancera 1 participant 1 message Sep 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 netfilter: nf_tables: add math expression support

Historically, users have requested support for increasing and decreasing TTL value in nftables in order to migrate from iptables.

Following the nftables spirit of flexible and multipurpose expressions, this patch introduces "nft_math" expression. This expression allows to increase and decrease values stored in the register.

The math expression intends to be flexible enough in case it needs to be extended in the future, e.g implement non-contiguous bitfields operations. But for now, non-contiguous payloads are not supported.

When loading a u8 or u16 payload into a register we don't know if the value is stored at least significant byte or most significant byte. In order to handle such cases, introduce a bitmask indicating what is the target bit and also use it to handle limits to prevent overflow or underflow. Keep in mind that math expression expects that the sreg and dreg are in host byteorder, so the user must use nft_byteorder expressions as needed.

This implementation comes with a libnftnl patch that allows the user to generate the following example bytecodes:

- Bytecode to increase the TTL of a packet

table filter inet flags 0 use 1 handle 1
inet filter input use 1 type filter hook input prio 0 policy accept packets 0 bytes 0 flags 1 inet filter input 4
[ payload load 2b @ network header + 8 => reg 1 ]
[ math mask 0x000000ff reg 1 + 1 => 1]
[ payload write reg 1 => 2b @ network header + 8 csum_type 1 csum_off 10 csum_flags 0x0 ]

- Bytecode to decrease the TTL of a packet

table filter inet flags 0 use 1 handle 1
inet filter input use 1 type filter hook input prio 0 policy accept packets 0 bytes 0 flags 1 inet filter input 4
[ payload load 2b @ network header + 8 => reg 1 ]
[ math mask 0x000000ff reg 1 - 1 => 1]
[ payload write reg 1 => 2b @ network header + 8 csum_type 1 csum_off 10 csum_flags 0x0 ]

- Bytecode to increase the meta mark of a packet

table mangle inet flags 0 use 1 handle 6
inet mangle output use 1 type filter hook output prio 0 policy accept packets 0 bytes 0 flags 1 inet mangle output 2
[ meta load mark => reg 1 ]
[ math mask 0xffffffff reg 1 + 1 => 1]
[ meta set mark with reg 1 ]

---
v2: dropped the byteorder netlink attribute, added bitmask to handle LSB/MSB when dealing with u8 and u16, simplified eval logic. I've kept nft_math as module, IMHO it would be too much to make it built-in.
v3: fixed checkpatch warnings, improved Kconfig description and fixed a wrong EINVAL return.
v4: removed NFTA_MATH_LEN as discussed with Phil, added a non-contiguous bitmask check
v5: fixed check on op max value, fixed byteorder inconsistency now NLA_U32 for both BITMASK and OP attributes
v6: rebased in top of recent changes, simplified eval logic and made all netlink attributes be32 instead of u32

---
 include/uapi/linux/netfilter/nf_tables.h |  25 ++++
 net/netfilter/Kconfig                    |   9 ++
 net/netfilter/Makefile                   |   1 +
 net/netfilter/nft_math.c                 | 150 +++++++++++++++++++++++
 4 files changed, 185 insertions(+)
 create mode 100644 net/netfilter/nft_math.c
include/uapi/linux/netfilter/nf_tables.h +25 -0
index 0b708153469c..36a5b42c3ddf 100644
@@ -2019,4 +2019,29 @@ 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)
+ */
+enum nft_math_attributes {
+	NFTA_MATH_UNSPEC,
+	NFTA_MATH_SREG,
+	NFTA_MATH_DREG,
+	NFTA_MATH_OP,
+	NFTA_MATH_BITMASK,
+	__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 */
net/netfilter/Kconfig +9 -0
index 09874c26fd13..b2203d7ac08b 100644
@@ -657,6 +657,15 @@ config NFT_SYNPROXY
 	  server. This allows to avoid conntrack and server resource usage
 	  during SYN-flood attacks.
 
+config NFT_MATH
+	tristate "Netfilter nf_tables math expression support"
+	depends on NETFILTER_ADVANCED
+	help
+	  This option enables support for the nftables math expression.
+	  It allows arithmetic operations to be performed on nft registers
+	  content, such as incrementing or decrementing values. Math
+	  expressions can be used in nftables rules to modify packet fields.
+
 if NF_TABLES_NETDEV
 
 config NF_DUP_NETDEV
net/netfilter/Makefile +1 -0
index 6bf74d488a29..ee50c2dd1f29 100644
@@ -131,6 +131,7 @@ obj-$(CONFIG_NFT_OSF)		+= nft_osf.o
 obj-$(CONFIG_NFT_TPROXY)	+= nft_tproxy.o
 obj-$(CONFIG_NFT_XFRM)		+= nft_xfrm.o
 obj-$(CONFIG_NFT_SYNPROXY)	+= nft_synproxy.o
+obj-$(CONFIG_NFT_MATH)		+= nft_math.o
 
 obj-$(CONFIG_NFT_NAT)		+= nft_chain_nat.o
 
net/netfilter/nft_math.c new file +150 -0

150 lines not on this page · raw message

Signature · 1 line
2.55.0