netfilter: nf_tables: add math expression support
Subsystems (2)
PATCH v2 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 u32, u16 and u8 values stored in the nftables registers.
The math expression intends to be flexible enough in case it needs to be extended in the future, e.g implement bitfields operations. For this reason, the length of the data is indicated in bits instead of bytes.
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.
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 3
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 8 bits 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 3
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 8 bits 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 32 bits 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.
--- include/uapi/linux/netfilter/nf_tables.h | 27 +++ net/netfilter/Kconfig | 7 + net/netfilter/Makefile | 1 + net/netfilter/nft_math.c | 201 +++++++++++++++++++++++ 4 files changed, 236 insertions(+) create mode 100644 net/netfilter/nft_math.c
include/uapi/linux/netfilter/nf_tables.h +27 -0
@@ -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 */
net/netfilter/Kconfig +7 -0
@@ -671,6 +671,13 @@ 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 + The math expression allows performing mathematical operations like + increase to packet fields. + if NF_TABLES_NETDEV config NF_DUP_NETDEV
net/netfilter/Makefile +1 -0
@@ -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 +201 -0
201 lines not on this page · raw message
Signature · 1 line
2.51.0
Quoted 47 lines · Fernando Fernandez Mancera
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 u32, u16 and u8 values stored in the nftables registers.The math expression intends to be flexible enough in case it needs to be extended in the future, e.g implement bitfields operations. For this reason, the length of the data is indicated in bits instead of bytes.
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.
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 3
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 8 bits mask0x000000ffreg 1 + 1 => 1]
[ payload write reg 1 => 2b @ network header + 8csum_type1csum_off10csum_flags0x0]- Bytecode to decrease the TTL of a packet
table filter inet flags 0 use 1 handle 3
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 8 bits mask0x000000ffreg 1 - 1 => 1]
[ payload write reg 1 => 2b @ network header + 8csum_type1csum_off10csum_flags0x0]- 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 32 bits reg 1 + 1 => 1]
[ meta set mark with reg 1 ]
Quoted 6 lines
Signed-off-by Fernando Fernandez Mancera <[email protected]>---
v2: dropped the byteorder netlink attribute, added bitmask to handle LSB/MSB when dealing with u8 and u16, simplified eval logic. I've keptnft_mathas module, IMHO it would be too much to make it built-in.
---
Ugh, I forgot to run ./scripts/checkpatch.pl against this patch, there are several warnings. No need to go over them during review, I fixed them already and will be included in a v3 :-)
Thanks,
Fernando.