netfilter: nft_nth: match every n packets
Subsystems (2)
PATCH v2 1/1 netfilter: nft_nth: match every n packets
Add support for the nth expression in netfilter.
A nft_nth structure is created with dreg (to store the result into a given register), every (to store the input value that indicates when the counter is going to be reset) and the counter (to store atomically the current counter value).
--- include/uapi/linux/netfilter/nf_tables.h | 15 ++++ net/netfilter/Kconfig | 6 ++ net/netfilter/Makefile | 1 + net/netfilter/nft_nth.c | 123 +++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 net/netfilter/nft_nth.c
include/uapi/linux/netfilter/nf_tables.h +15 -0
@@ -1076,4 +1076,19 @@ enum nft_trace_types {__NFT_TRACETYPE_MAX };#define NFT_TRACETYPE_MAX (__NFT_TRACETYPE_MAX - 1) + +/** +* enum nft_nth_attributes - nf_tables nth expression netlink attributes +* @NFTA_NTH_UNSPEC: unspecified attribute +* @NFTA_NTH_DREG: destination register (NLA_U32) +* @NFTA_NTH_EVERY: source value for every counter reset (NLA_U32) +*/ +enum nft_nth_attributes { +NFTA_NTH_UNSPEC , +NFTA_NTH_DREG , +NFTA_NTH_EVERY , + __NFTA_NTH_MAX +}; +#define NFTA_NTH_MAX (__NFTA_NTH_MAX - 1) +#endif /* _LINUX_NF_TABLES_H */
net/netfilter/Kconfig +6 -0
@@ -474,6 +474,12 @@ config NFT_META This option adds the "meta" expression that you can use to match and to set packet metainformation such as the packet mark. +config NFT_NTH + tristate "Netfilter nf_tables nth module" + help + This option adds the "nth" expression that you can use to match a + packet every a specific given value. + config NFT_CT depends on NF_CONNTRACK tristate "Netfilter nf_tables conntrack module"
net/netfilter/Makefile +1 -0
@@ -80,6 +80,7 @@ obj-$(CONFIG_NF_TABLES_NETDEV) += nf_tables_netdev.o obj-$(CONFIG_NFT_COMPAT) += nft_compat.o obj-$(CONFIG_NFT_EXTHDR) += nft_exthdr.o obj-$(CONFIG_NFT_META) += nft_meta.o +obj-$(CONFIG_NFT_NTH) += nft_nth.o obj-$(CONFIG_NFT_CT) += nft_ct.o obj-$(CONFIG_NFT_LIMIT) += nft_limit.o obj-$(CONFIG_NFT_NAT) += nft_nat.o
net/netfilter/nft_nth.c new file +123 -0
123 lines not on this page · raw message
Signature · 1 line
2.8.1
Florian Westphal
maintainerQuoted 9 lines · Laura Garcia Liebana
net/netfilter/nft_nth.c new file +9
-0+struct nft_nth { +enum nft_registers dreg:8 ; +u32 every; +atomic_t counter; +}; + +static void nft_nth_eval (const struct nft_expr *expr, +struct nft_regs *regs, +const struct nft_pktinfo *pkt)
net/netfilter/nft_nth.c line 42
Quoted 10 lines
net/netfilter/nft_nth.c new file +10
-0+{ +struct nft_nth *nth =nft_expr_priv (expr); +u32 nval, oval; + +do { + oval =atomic_read (&nth->counter); + nval = (oval+1 < nth->every) ? oval+1 :0 ; + }while (atomic_cmpxchg (&nth->counter, oval, nval) != oval); + +memcpy (®s->data[nth->dreg], &nth->counter,sizeof (u32));
So this places current counter value in the dreg.
How exactly is this used by nftables?
AFAIU usespace will check if ->dreg is 0 or not, but does that make sense?
Seems to me it would be more straightforward to not use a dreg at all and just NFT_BREAK if nval != 0?
net/netfilter/nft_nth.c line 56
Quoted 7 lines
net/netfilter/nft_nth.c new file +7
-0+static int nft_nth_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]) +{ +struct nft_nth *nth =nft_expr_priv (expr); + + nth->every =ntohl (nla_get_be32 (tb[NFTA_NTH_EVERY ]));
I think you have to check if tb[NFTA_NTH_EVERY] is not NULL first.
net/netfilter/nft_nth.c line 60
Quoted 1 line
net/netfilter/nft_nth.c new file +1
-0+ nth->dreg =nft_parse_register (tb[NFTA_NTH_DREG ]);
same here.
net/netfilter/nft_nth.c line 100
Quoted 10 lines
net/netfilter/nft_nth.c new file +10
-0+static const struct nft_expr_ops * +nft_nth_select_ops (const struct nft_ctx *ctx, +const struct nlattr *const tb[]) +{ +if (!tb[NFTA_NTH_DREG ] || + !tb[NFTA_NTH_EVERY ]) +return ERR_PTR (-EINVAL ); + +return &nft_nth_ops; +}
Oh, I see -- its already checked here.
But why does nth implement a select_ops in the first place?
Otherwise this looks good to me, except that I think we should consider putting this in nft_meta.c instead of a new module.
Quoted 23 lines · Florian Westphal
Quoted 9 lines · Laura Garcia Liebana
net/netfilter/nft_nth.c new file +9
-0+struct nft_nth { +enum nft_registers dreg:8 ; +u32 every; +atomic_t counter; +}; + +static void nft_nth_eval (const struct nft_expr *expr, +struct nft_regs *regs, +const struct nft_pktinfo *pkt)net/netfilter/nft_nth.c line 42
Quoted 10 lines
net/netfilter/nft_nth.c new file +10
-0+{ +struct nft_nth *nth =nft_expr_priv (expr); +u32 nval, oval; + +do { + oval =atomic_read (&nth->counter); + nval = (oval+1 < nth->every) ? oval+1 :0 ; + }while (atomic_cmpxchg (&nth->counter, oval, nval) != oval); + +memcpy (®s->data[nth->dreg], &nth->counter,sizeof (u32));So this places current counter value in the dreg.
Quoted 8 lines
How exactly is this used by nftables?
AFAIU usespace will check if ->dreg is 0 or not, but does that make sense?
Seems to me it would be more straightforward to not use a dreg at all and just
NFT_BREAKif nval != 0?
The main idea is to provide a round robin like scheduling method, for example:
ip daddr <ipsaddr> dnat nth 3 map {
0: <ipdaddrA>,
1: <ipdaddrB>,
2: <ipdaddrC>
}It's a port of the nth mode in the iptables statistic extension module:
http://ipset.netfilter.org/iptables-extensions.man.html#lbCD
Quoted 10 lines
net/netfilter/nft_nth.c line 56
Quoted 7 lines
net/netfilter/nft_nth.c new file +7
-0+static int nft_nth_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]) +{ +struct nft_nth *nth =nft_expr_priv (expr); + + nth->every =ntohl (nla_get_be32 (tb[NFTA_NTH_EVERY ]));I think you have to check if tb[NFTA_NTH_EVERY] is not NULL first.
Quoted 4 lines
net/netfilter/nft_nth.c line 60
Quoted 1 line
net/netfilter/nft_nth.c new file +1
-0+ nth->dreg =nft_parse_register (tb[NFTA_NTH_DREG ]);same here.
It's checked below.
Quoted 5 lines
Quoted 5 lines
net/netfilter/nft_nth.c new file +5
-0+static const struct nft_expr_ops * +nft_nth_select_ops (const struct nft_ctx *ctx, +const struct nlattr *const tb[]) +{ + if (!tb[NFTA_NTH_DREG] ||
Quoted 9 lines
net/netfilter/nft_nth.c line 100
Quoted 5 lines
net/netfilter/nft_nth.c new file +5
-0+ !tb[NFTA_NTH_EVERY]) +return ERR_PTR (-EINVAL ); + +return &nft_nth_ops; +}Oh, I see -- its already checked here.
But why does nth implement aselect_opsin the first place?
In the future we can include a sreg to set a counter initialization, but currently there is only one ops structure.
Quoted 2 lines
Otherwise this looks good to me, except that I think we should consider putting this in
nft_meta.cinstead of a new module.
AFAIK meta is more to set or get metainformation from a certain packet. I consider this expression is closer to counter, but with a resetting value.
Thank you.
Florian Westphal
maintainerQuoted 10 lines · Laura Garcia
Quoted 8 lines · Florian Westphal
How exactly is this used by nftables?
AFAIU usespace will check if ->dreg is 0 or not, but does that make sense?
Seems to me it would be more straightforward to not use a dreg at all and just
NFT_BREAKif nval != 0?
Quoted 9 lines
The main idea is to provide a round robin like scheduling method, for example:
ip daddr <ipsaddr> dnat nth 3 map {
0: <ipdaddrA>, 1: <ipdaddrB>, 2: <ipdaddrC> }
That makes sense, would be nice to place a small blurb in the commit message.
Quoted 6 lines
Quoted 2 lines
Otherwise this looks good to me, except that I think we should consider putting this in
nft_meta.cinstead of a new module.AFAIK meta is more to set or get metainformation from a certain packet. I consider this expression is closer to counter, but with a resetting value.
Ok, fair enough.
Thanks,
Florian
Pablo Neira Ayuso
maintainerQuoted 21 lines · Florian Westphal
Quoted 10 lines · Laura Garcia
Quoted 8 lines · Florian Westphal
How exactly is this used by nftables?
AFAIU usespace will check if ->dreg is 0 or not, but does that make sense?
Seems to me it would be more straightforward to not use a dreg at all and just
NFT_BREAKif nval != 0?Quoted 9 lines
The main idea is to provide a round robin like scheduling method, for example:
ip daddr <ipsaddr> dnat nth 3 map {
0: <ipdaddrA>, 1: <ipdaddrB>, 2: <ipdaddrC> }
Quoted 2 lines
That makes sense, would be nice to place a small blurb in the commit message.
I'd suggest you rename this to nft_numgen.c where numgen stands for 'number generator', then rename 'every' to 'until' (this sets the upper limit in the generator) and add support for random too, so we provide incremental and random number generators to start with and we leave room to extend this with more number generators in the future if needed.
Florian added random to meta, but I don't see an easy way to reuse this with maps unless we introduce another modulus/scale expression, and we should skip oversplitting expressions in way too basic operations.
Quoted 32 lines · Pablo Neira Ayuso
Quoted 21 lines · Florian Westphal
Quoted 10 lines · Laura Garcia
Quoted 8 lines · Florian Westphal
How exactly is this used by nftables?
AFAIU usespace will check if ->dreg is 0 or not, but does that make sense?
Seems to me it would be more straightforward to not use a dreg at all and just
NFT_BREAKif nval != 0?Quoted 9 lines
The main idea is to provide a round robin like scheduling method, for example:
ip daddr <ipsaddr> dnat nth 3 map {
0: <ipdaddrA>, 1: <ipdaddrB>, 2: <ipdaddrC> }Quoted 2 lines
That makes sense, would be nice to place a small blurb in the commit message.
I'd suggest you rename this to
nft_numgen.cwhere numgen stands for 'number generator', then rename 'every' to 'until' (this sets the upper limit in the generator) and add support for random too, so we provide incremental and random number generators to start with and we leave room to extend this with more number generators in the future if needed.
Quoted 4 lines
Florian added random to meta, but I don't see an easy way to reuse this with maps unless we introduce another modulus/scale expression, and we should skip oversplitting expressions in way too basic operations.
So, do you mean something like this?
ip daddr <ipsaddr> dnat numgen nth 3 map {
0: <ipdaddrA>,
1: <ipdaddrB>,
2: <ipdaddrC>
}and
ip daddr <ipsaddr> dnat numgen random 3 map {
0: <ipdaddrA>,
1: <ipdaddrB>,
2: <ipdaddrC>
}Maybe _math_ could be a better name?
The counter expression could be included as well.
Pablo Neira Ayuso
maintainerQuoted 46 lines · Laura Garcia
Quoted 32 lines · Pablo Neira Ayuso
Quoted 21 lines · Florian Westphal
Quoted 10 lines · Laura Garcia
Quoted 8 lines · Florian Westphal
How exactly is this used by nftables?
AFAIU usespace will check if ->dreg is 0 or not, but does that make sense?
Seems to me it would be more straightforward to not use a dreg at all and just
NFT_BREAKif nval != 0?Quoted 9 lines
The main idea is to provide a round robin like scheduling method, for example:
ip daddr <ipsaddr> dnat nth 3 map {
0: <ipdaddrA>, 1: <ipdaddrB>, 2: <ipdaddrC> }Quoted 2 lines
That makes sense, would be nice to place a small blurb in the commit message.
I'd suggest you rename this to
nft_numgen.cwhere numgen stands for 'number generator', then rename 'every' to 'until' (this sets the upper limit in the generator) and add support for random too, so we provide incremental and random number generators to start with and we leave room to extend this with more number generators in the future if needed.Quoted 4 lines
Florian added random to meta, but I don't see an easy way to reuse this with maps unless we introduce another modulus/scale expression, and we should skip oversplitting expressions in way too basic operations.
So, do you mean something like this?
ip daddr <ipsaddr> dnat numgen nth 3 map {
0: <ipdaddrA>, 1: <ipdaddrB>, 2: <ipdaddrC> }
Quoted 7 lines
and
ip daddr <ipsaddr> dnat numgen random 3 map {
0: <ipdaddrA>, 1: <ipdaddrB>, 2: <ipdaddrC> }
Something like this, but I would like to have a better syntax for this.
Quoted 2 lines
Maybe _math_ could be a better name?
The counter expression could be included as well.
We already have a counter expression ;-) So what counter expression are you refering to?