netfilter: nf_tables: add math expression support
Subsystems (2)
PATCH v3 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.
v3: fixed checkpatch warnings, improved Kconfig description and fixed a wrong EINVAL return.
--- include/uapi/linux/netfilter/nf_tables.h | 27 +++ net/netfilter/Kconfig | 9 + net/netfilter/Makefile | 1 + net/netfilter/nft_math.c | 201 +++++++++++++++++++++++ 4 files changed, 238 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 +9 -0
@@ -671,6 +671,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 operation to be performed on nft registers, + 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
@@ -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.52.0
Phil Sutter
reviewerHi Fernando,
Quoted 8 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.
Quoted 8 lines
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 part puzzles me. IMO byteorder conversion is needed for arithmetic on multi-byte values in non-host byte order.
Since nftables only knows host byte order and network byte order, the only case to consider is Little Endian host with NBO data. Registers are filled "from left to right", so (u16)0x1337 and (u32)0xfeedbabe look like this when stored in Network Byte Order in registers:
{ 0x13, 0x37, 0x00, 0x00 }
{ 0xfe, 0xed, 0xba, 0xbe }
Interpreting those buffers as u16/u32 on Little Endian results in values 0x3713 and 0xbebaedfe. Any increment/decrement on those values leads to wrong results.
Maybe there's a hidden secret in 'bit_unit', but even if you calculate the right value to add/subtract from the right byte (0x100 and 0x1000000 in our examples), a possible carry-over bumps the wrong byte.
Assuming that I didn't entirely miss the point you might want to have a look at recent libnftnl/nftables commits informing libnftnl of payload byteorder for host byteorder independent debug output (with correct values). Particularly interesting are nftnl_*_set_imm() functions. Maybe this allows you to annotate math expression with data byteorder so it may perform byteorder conversion as needed.
Cheers, Phil
Quoted 31 lines · Phil Sutter
Hi Fernando,
Quoted 8 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.Quoted 8 lines
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 part puzzles me. IMO byteorder conversion is needed for arithmetic on multi-byte values in non-host byte order.
Since nftables only knows host byte order and network byte order, the only case to consider is Little Endian host with NBO data. Registers are filled "from left to right", so (u16)0x1337 and (u32)0xfeedbabe look like this when stored in Network Byte Order in registers:
{
0x13,0x37,0x00,0x00}
{0xfe,0xed,0xba,0xbe}
Quoted 8 lines
Interpreting those buffers as u16/u32 on Little Endian results in values
0x3713and0xbebaedfe. Any increment/decrement on those values leads to wrong results.Maybe there's a hidden secret in '
bit_unit', but even if you calculate the right value to add/subtract from the right byte (0x100and0x1000000in our examples), a possible carry-over bumps the wrong byte.
Hi!
This is correct. In the initial implementation [1] I included a NFTA_MATH_BYTEORDER attribute but after discussing with Florian we decided to drop it. Of course, in order to make this work correctly, nft byteorder expression must be used to perform the conversion when needed.
I believe that nft command line tool can figure out when a byteorder conversion is needed as I noticed this is already done for other expressions.
My initial idea was to keep the bytecode as smaller as possible but it is true that it makes sense to use the existing byteorder operations.
Does this make sense or am I missing something?
Thanks for the feedback!
[1] https://lore.kernel.org/netfilter-devel/[email protected]/
Quoted 9 lines
Assuming that I didn't entirely miss the point you might want to have a look at recent libnftnl/nftables commits informing libnftnl of payload byteorder for host byteorder independent debug output (with correct values). Particularly interesting are nftnl_*_set_imm() functions. Maybe this allows you to annotate math expression with data byteorder so it may perform byteorder conversion as needed.
Cheers, Phil
Phil Sutter
reviewerQuoted 48 lines · Fernando Fernandez Mancera
Quoted 31 lines · Phil Sutter
Hi Fernando,
Quoted 8 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.Quoted 8 lines
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 part puzzles me. IMO byteorder conversion is needed for arithmetic on multi-byte values in non-host byte order.
Since nftables only knows host byte order and network byte order, the only case to consider is Little Endian host with NBO data. Registers are filled "from left to right", so (u16)0x1337 and (u32)0xfeedbabe look like this when stored in Network Byte Order in registers:
{
0x13,0x37,0x00,0x00}
{0xfe,0xed,0xba,0xbe}Quoted 8 lines
Interpreting those buffers as u16/u32 on Little Endian results in values
0x3713and0xbebaedfe. Any increment/decrement on those values leads to wrong results.Maybe there's a hidden secret in '
bit_unit', but even if you calculate the right value to add/subtract from the right byte (0x100and0x1000000in our examples), a possible carry-over bumps the wrong byte.Hi!
This is correct. In the initial implementation [1] I included a
NFTA_MATH_BYTEORDERattribute but after discussing with Florian we decided to drop it. Of course, in order to make this work correctly, nft byteorder expression must be used to perform the conversion when needed.
Quoted 8 lines
I believe that nft command line tool can figure out when a byteorder conversion is needed as I noticed this is already done for other expressions.
My initial idea was to keep the bytecode as smaller as possible but it is true that it makes sense to use the existing byteorder operations.
Does this make sense or am I missing something?
Ah, got it! So nft_math simply assumes sreg and dreg are in host byte order and nftables is supposed to add nft_byteorder expressions as needed. That should do and is indeed easier than dealing with data byteorder within nft_math itself!
The only odd thing that remains is the combined use of mask and length. Typically one would either use length and offset or mask alone because the former two values may be extracted from the latter. Also, why does nft_math_init() restrict masks to align at start or end of register?
Thanks, Phil
Quoted 63 lines · Phil Sutter
Quoted 48 lines · Fernando Fernandez Mancera
Quoted 31 lines · Phil Sutter
Hi Fernando,
Quoted 8 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.Quoted 8 lines
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 part puzzles me. IMO byteorder conversion is needed for arithmetic on multi-byte values in non-host byte order.
Since nftables only knows host byte order and network byte order, the only case to consider is Little Endian host with NBO data. Registers are filled "from left to right", so (u16)0x1337 and (u32)0xfeedbabe look like this when stored in Network Byte Order in registers:
{
0x13,0x37,0x00,0x00}
{0xfe,0xed,0xba,0xbe}Quoted 8 lines
Interpreting those buffers as u16/u32 on Little Endian results in values
0x3713and0xbebaedfe. Any increment/decrement on those values leads to wrong results.Maybe there's a hidden secret in '
bit_unit', but even if you calculate the right value to add/subtract from the right byte (0x100and0x1000000in our examples), a possible carry-over bumps the wrong byte.Hi!
This is correct. In the initial implementation [1] I included a
NFTA_MATH_BYTEORDERattribute but after discussing with Florian we decided to drop it. Of course, in order to make this work correctly, nft byteorder expression must be used to perform the conversion when needed.Quoted 8 lines
I believe that nft command line tool can figure out when a byteorder conversion is needed as I noticed this is already done for other expressions.
My initial idea was to keep the bytecode as smaller as possible but it is true that it makes sense to use the existing byteorder operations.
Does this make sense or am I missing something?
Ah, got it! So
nft_mathsimply assumes sreg and dreg are in host byte order and nftables is supposed to addnft_byteorderexpressions as needed. That should do and is indeed easier than dealing with data byteorder withinnft_mathitself!
Quoted 5 lines
The only odd thing that remains is the combined use of mask and length. Typically one would either use length and offset or mask alone because the former two values may be extracted from the latter. Also, why does
nft_math_init()restrict masks to align at start or end of register?
Hm. I have been thinking about this. So the initial reason for this was to simplify everything so if u32 length is used we don't need to use a bitmask. But tbh, it doesn't make much sense.
I will get rid of the length attribute on the v4. Thank you Phil! :)
Quoted 2 lines
Thanks, Phil