netfilter: nf_tables: add math expression support
Subsystems (2)
RFC PATCH 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. In addition, it takes into account the byteorder of the value stored in the source register, so there is no need to do a byteorder conversion before and after the math expression.
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.
Payload set operations sometimes need 16 bits for checksum recalculation. Even it is a 8 bit operation, 16 bits are loaded in the source register. Handle such cases applying a bitmask when operating with 8 bits length.
As a last detail, nft_math prevents overflow of the field. If the value is already at its limit, do nothing.
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 mangle inet flags 0 use 1 handle 5
inet mangle output use 1 type filter hook output prio 0 policy accept packets 0 bytes 0 flags 1 inet mangle output 2
[ payload load 2b @ network header + 8 => reg 1 ]
[ math math 8 bits host 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 mangle inet flags 0 use 1 handle 7
inet mangle output use 1 type filter hook output prio 0 policy accept packets 0 bytes 0 flags 1 inet mangle output 2
[ payload load 2b @ network header + 8 => reg 1 ]
[ math math 8 bits host 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 math 32 bits host reg 1 + 1 => 1]
[ meta set mark with reg 1 ]
--- include/uapi/linux/netfilter/nf_tables.h | 34 +++ net/netfilter/Kconfig | 7 + net/netfilter/Makefile | 1 + net/netfilter/nft_math.c | 267 +++++++++++++++++++++++ 4 files changed, 309 insertions(+) create mode 100644 net/netfilter/nft_math.c
include/uapi/linux/netfilter/nf_tables.h +34 -0
@@ -2015,4 +2015,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 */
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 +267 -0
267 lines not on this page · raw message
Signature · 1 line
2.51.0
Florian Westphal
maintainerQuoted 2 lines · Fernando Fernandez Mancera
Historically, users have requested support for increasing and decreasing TTL value in nftables in order to migrate from iptables.
Right.
Quoted 3 lines
In addition, it takes into account the byteorder of the value stored in the source register, so there is no need to do a byteorder conversion before and after the math expression.
Why? Any particular reason for this?
I would have expected to have ntf insert the needed byteorder conversions.
Quoted 3 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.
Not so sure. We already have nft_bitwise. Or what bitfield operations are you considering?
You mean 'add' to a non-byte part, like e.g. as in iph->ihl?
Quoted 7 lines
Payload set operations sometimes need 16 bits for checksum recalculation. Even it is a 8 bit operation, 16 bits are loaded in the source register. Handle such cases applying a bitmask when operating with 8 bits length.
As a last detail,
nft_mathprevents overflow of the field. If the value is already at its limit, do nothing.
Should it set NFT_BREAK?
Quoted 6 lines
table mangle inet flags 0 use 1 handle 5
inet mangle output use 1 type filter hook output prio 0 policy accept packets 0 bytes 0 flags 1 inet mangle output 2
[ payload load 2b @ network header + 8 => reg 1 ]
[ math math 8 bits host reg 1 + 1 => 1]
[ payload write reg 1 => 2b @ network header + 8csum_type1csum_off10csum_flags0x0]
Thanks for including these examples.
You can drop the 'math' from the snprintf callback in libnftnl to avoid this 'math math'.
I assume this says 'host' because its limited to 8 bits?
net/netfilter/nft_math.c line 30
Quoted 3 lines
net/netfilter/nft_math.c new file +3
-0+/* For payload set if checksum needs to be adjusted 16 bits are stored +* in the source register instead of 8. Therefore, use a bitmask to +* operate with the less significant byte. */
I don't think this works. You don't know if the add should be done on the less significant byte order the MSB one.
(If you do... how?)
AFAICS you need to add support for a displacement offset inside the register to support this (or I should write:
work-around-align-fetch-mangling ...)
Quoted 5 lines
net/netfilter/nft_math.c new file +5
-0+static int nft_math_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]) +{ +struct nft_math *priv =nft_expr_priv (expr);
net/netfilter/nft_math.c line 184
Quoted 10 lines
net/netfilter/nft_math.c new file +9
-0+int err; + +if (tb[NFTA_MATH_SREG ] ==NULL || + tb[NFTA_MATH_DREG ] ==NULL || + tb[NFTA_MATH_LEN ] ==NULL || + tb[NFTA_MATH_OP ] ==NULL || + tb[NFTA_MATH_BYTEORDER ] ==NULL ) +return -EINVAL ; + priv->op =nla_get_u8 (tb[NFTA_MATH_OP ]);
Can you make it NLA_U32? NLA_U8 doesn't buy anything except limiting us to 255 supported options (i don't see a use case for ever having so many, but if we ever have we don't need new OP32 attribute).
I wonder if we really want this as a module, it seems rather small.
I would also be open to just extending bitwise expression with this inc/dec, both bitwise & math expr do register manipulations.
Quoted 4 lines · Florian Westphal
Quoted 2 lines · Fernando Fernandez Mancera
Historically, users have requested support for increasing and decreasing TTL value in nftables in order to migrate from iptables.
Quoted 8 lines
Right.
Quoted 3 lines
In addition, it takes into account the byteorder of the value stored in the source register, so there is no need to do a byteorder conversion before and after the math expression.
Why? Any particular reason for this?
It is for simplicity reasons. Just to avoid a big payload with byteorder conversions per math expression.
Quoted 12 lines
I would have expected to have ntf insert the needed byteorder conversions.
Quoted 3 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.
Not so sure. We already have
nft_bitwise. Or what bitfield operations are you considering?You mean 'add' to a non-byte part, like e.g. as in
iph->ihl?
Yes, I do not have a clear use-case for it right now, but I just wanted to keep it as flexible as possible.
Quoted 10 lines
Quoted 7 lines
Payload set operations sometimes need 16 bits for checksum recalculation. Even it is a 8 bit operation, 16 bits are loaded in the source register. Handle such cases applying a bitmask when operating with 8 bits length.
As a last detail,
nft_mathprevents overflow of the field. If the value is already at its limit, do nothing.Should it set
NFT_BREAK?
I don't think so. If the user wants to increase or decrease a field but it already reached the limit IMO it shouldn't do anything but continue with the next expression instead of setting NFT_BREAK. Anyway, not a deal breaker for me..
Quoted 11 lines
Quoted 6 lines
table mangle inet flags 0 use 1 handle 5
inet mangle output use 1 type filter hook output prio 0 policy accept packets 0 bytes 0 flags 1 inet mangle output 2
[ payload load 2b @ network header + 8 => reg 1 ]
[ math math 8 bits host reg 1 + 1 => 1]
[ payload write reg 1 => 2b @ network header + 8csum_type1csum_off10csum_flags0x0]Thanks for including these examples.
You can drop the 'math' from the snprintf callback in libnftnl to avoid this 'math math'.
Ugh, sorry I missed that detail.
Quoted 2 lines
I assume this says 'host' because its limited to 8 bits?
Yes, well it says "host" because I used NFT_MATH_BYTEORDER_HOST to generate it, anyway it doesn't matter as it is 8 bits. Would it be better to hide byteorder if it doesn't matter?
Quoted 7 lines
net/netfilter/nft_math.c line 30
Quoted 3 lines
net/netfilter/nft_math.c new file +3
-0+/* For payload set if checksum needs to be adjusted 16 bits are stored +* in the source register instead of 8. Therefore, use a bitmask to +* operate with the less significant byte. */ I don't think this works. You don't know if the add should be done on the less significant byte order the MSB one.
Quoted 6 lines
(If you do... how?)
AFAICS you need to add support for a displacement offset inside the register to support this (or I should write:
work-around-align-fetch-mangling ...)
So I was aware of this. But after reading nft_payload.c expression I thought the desired value was always stored in the less significant byte. Anyway, this assumption was wrong and you are right.
What would be the best way to implement this? A pure bit offset (NFT_MATH_OFFSET) or a bitmask (NFT_MATH_BITMASK)?
Quoted 14 lines
Quoted 13 lines
net/netfilter/nft_math.c new file +13
-0+static int nft_math_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]) +{ +struct nft_math *priv =nft_expr_priv (expr); +int err; + +if (tb[NFTA_MATH_SREG ] ==NULL || + tb[NFTA_MATH_DREG ] ==NULL || + tb[NFTA_MATH_LEN ] ==NULL || + tb[NFTA_MATH_OP ] ==NULL || + tb[NFTA_MATH_BYTEORDER ] ==NULL ) +return -EINVAL ;
Quoted 6 lines
net/netfilter/nft_math.c line 184
Quoted 1 line
net/netfilter/nft_math.c new file +1
-0+ priv->op =nla_get_u8 (tb[NFTA_MATH_OP ]);Can you make it
NLA_U32?NLA_U8doesn't buy anything except limiting us to 255 supported options (i don't see a use case for ever having so many, but if we ever have we don't need new OP32 attribute).
Sure, no problems about that. Also NFTA_MATH_LEN? I do not see a U32 bits operation happening in the future tho.
Quoted 3 lines
I wonder if we really want this as a module, it seems rather small.
By default I thought that a module made sense.. but it is true it is "general" purpose code and small. I don't really mind.
Quoted 2 lines
I would also be open to just extending bitwise expression with this inc/dec, both bitwise & math expr do register manipulations.
While both do register manipulation, I do not think they fit together from a user perspective. Especially if we extend the number of math operations in the future.
Florian Westphal
maintainerQuoted 5 lines · Fernando Fernandez Mancera
Quoted 2 lines
Why? Any particular reason for this?
It is for simplicity reasons. Just to avoid a big payload with byteorder conversions per math expression.
Not sold yet. I'd like to hear Pablos opinion.
Quoted 6 lines
Quoted 2 lines
Should it set
NFT_BREAK?I don't think so. If the user wants to increase or decrease a field but it already reached the limit IMO it shouldn't do anything but continue with the next expression instead of setting
NFT_BREAK. Anyway, not a deal breaker for me..
I was just curious. No objections from me.
Quoted 5 lines
Quoted 1 line
I assume this says 'host' because its limited to 8 bits?
Yes, well it says "host" because I used
NFT_MATH_BYTEORDER_HOSTto generate it, anyway it doesn't matter as it is 8 bits. Would it be better to hide byteorder if it doesn't matter?
No, I would prefer simpler code, no need to special case this IMO.
Quoted 3 lines
Quoted 3 lines
AFAICS you need to add support for a displacement offset inside the register to support this (or I should write:
work-around-align-fetch-mangling ...)
[..]
Quoted 2 lines
What would be the best way to implement this? A pure bit offset (
NFT_MATH_OFFSET) or a bitmask (NFT_MATH_BITMASK)?
Bitmask would allow to remove MATH_LEN, correct?
Users that want pure U8 increment can do 0xff000000 resp. 0x000000ff.
Same for U16. So I'd say a mask would be better.
Quoted 7 lines
Quoted 4 lines
Can you make it
NLA_U32?NLA_U8doesn't buy anything except limiting us to 255 supported options (i don't see a use case for ever having so many, but if we ever have we don't need new OP32 attribute).Sure, no problems about that. Also
NFTA_MATH_LEN? I do not see a U32 bits operation happening in the future tho.
Yes, good question. Due to netlink padding neither u8 nor u16 saves space in the message encoding. I'll leave it up to you, I don't see 2**32 math len making any sense.
Quoted 9 lines
By default I thought that a module made sense.. but it is true it is "general" purpose code and small. I don't really mind.
Quoted 2 lines
I would also be open to just extending bitwise expression with this inc/dec, both bitwise & math expr do register manipulations.
While both do register manipulation, I do not think they fit together from a user perspective. Especially if we extend the number of math operations in the future.
Users don't interact with the expressions directly. But I understand your point.
Pablo, whats your take?
Quoted 10 lines · Florian Westphal
[..]
Quoted 3 lines
Quoted 3 lines
AFAICS you need to add support for a displacement offset inside the register to support this (or I should write:
work-around-align-fetch-mangling ...)[..]
Quoted 2 lines
What would be the best way to implement this? A pure bit offset (
NFT_MATH_OFFSET) or a bitmask (NFT_MATH_BITMASK)?
Quoted 7 lines
Bitmask would allow to remove
MATH_LEN, correct?Users that want pure U8 increment can do
0xff000000resp.0x000000ff.Same for U16. So I'd say a mask would be better.
Well, I would prefer to keep the MATH_LEN in order to keep this flexible in the future for bitfield math operations unless we discard them completely.
Anyway, I also prefer the bitmask approach.
Quoted 18 lines
Quoted 7 lines
Quoted 4 lines
Can you make it
NLA_U32?NLA_U8doesn't buy anything except limiting us to 255 supported options (i don't see a use case for ever having so many, but if we ever have we don't need new OP32 attribute).Sure, no problems about that. Also
NFTA_MATH_LEN? I do not see a U32 bits operation happening in the future tho.Yes, good question. Due to netlink padding neither u8 nor u16 saves space in the message encoding. I'll leave it up to you, I don't see 2**32 math len making any sense.
Quoted 6 lines
By default I thought that a module made sense.. but it is true it is "general" purpose code and small. I don't really mind.
Quoted 2 lines
I would also be open to just extending bitwise expression with this inc/dec, both bitwise & math expr do register manipulations.
Quoted 7 lines
Quoted 3 lines
While both do register manipulation, I do not think they fit together from a user perspective. Especially if we extend the number of math operations in the future.
Users don't interact with the expressions directly. But I understand your point.
Nft command line tool users do not interact with the expression directly but libnftnl users do. I have found some users using custom C programs to configure rules with libnftnl.
It is not common of course, but I think it is good to keep them on mind.
Thanks for all this feedback Florian!
Quoted 2 lines
Pablo, whats your take?