Netfilter updates for net-next
Subsystems (4)
Pablo Neira Ayuso
maintainercover Netfilter updates for net-next
@netdev maintainers: Please, kindly allow me to exceed in one patch the maximum series length, there are a few trivial oneliners in this series.
-o-
Hi,
The following patchset contains Netfilter updates for net-next:
Patch #1 adds ctnetlink support for kernel side filtering for
deletions, from Changliang Wu.
Patch #2 updates nft_counter support to Use u64_stats_t,
from Sebastian Andrzej Siewior.
Patch #3 uses kmemdup_array() in all xtables frontends,
from Yan Zhen.
Patch #4 is a oneliner to use ERR_CAST() in nf_conntrack instead
opencoded casting, from Shen Lichuan.
Patch #5 removes unused argument in nftables .validate interface,
from Florian Westphal.
Patch #6 is a oneliner to correct a typo in nftables kdoc,
from Simon Horman.
Patch #7 fixes missing kdoc in nftables, also from Simon.
Patch #8 updates nftables to handle timeout less than CONFIG_HZ.
Patch #9 rejects element expiration if timeout is zero,
otherwise it is silently ignored.
Patch #10 disallows element expiration larger than timeout.
Patch #11 removes unnecessary READ_ONCE annotation while mutex is held.
Patch #12 adds missing READ_ONCE/WRITE_ONCE annotation in dynset.
Patch #13 annotates data-races around element expiration.
Patch #14 allocates timeout and expiration in one single set element
extension, they are tighly couple, no reason to keep them separated anymore.
Patch #15 updates nftables to interpret zero timeout element as never
times out. Note that it is already possible to declare sets with elements that never time out but this generalizes to all kind of set with timeouts.
Patch #16 supports for element timeout and expiration updates.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git nf-next-24-09-06
Thanks.
----------------------------------------------------------------
The following changes since commit 55ddb6c5a3aef8d8658fe31b1ddda007693ae797:
net: stmmac: drop the ethtool begin() callback (2024-09-02 13:44:09 +0100)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git tags/nf-next-24-09-06
for you to fetch changes up to 4201f3938914d8df3c761754b9726770c4225d66:
netfilter: nf_tables: set element timeout update support (2024-09-03 18:19:44 +0200)
----------------------------------------------------------------
netfilter pull request 24-09-06
----------------------------------------------------------------
Changliang Wu (1):
netfilter: ctnetlink: support CTA_FILTER for flush
Florian Westphal (1):
netfilter: nf_tables: drop unused 3rd argument from validate callback ops
Pablo Neira Ayuso (9):
netfilter: nf_tables: elements with timeout below CONFIG_HZ never expire
netfilter: nf_tables: reject element expiration with no timeout
netfilter: nf_tables: reject expiration higher than timeout
netfilter: nf_tables: remove annotation to access set timeout while holding lock
netfilter: nft_dynset: annotate data-races around set timeout
netfilter: nf_tables: annotate data-races around element expiration
netfilter: nf_tables: consolidate timeout extension for elements
netfilter: nf_tables: zero timeout means element never times out
netfilter: nf_tables: set element timeout update support
Sebastian Andrzej Siewior (1):
netfilter: nft_counter: Use u64_stats_t for statistic.
Shen Lichuan (1):
netfilter: conntrack: Convert to use ERR_CAST()
Simon Horman (2):
netfilter: nf_tables: Correct spelling in nf_tables.h
netfilter: nf_tables: Add missing Kernel doc
Yan Zhen (1):
netfilter: Use kmemdup_array instead of kmemdup for multiple allocation
include/net/netfilter/nf_tables.h | 42 +++++++---- include/net/netfilter/nf_tproxy.h | 1 + include/net/netfilter/nft_fib.h | 4 +- include/net/netfilter/nft_meta.h | 3 +- include/net/netfilter/nft_reject.h | 3 +- include/uapi/linux/netfilter/nf_tables.h | 2 +- net/bridge/netfilter/ebtables.c | 2 +- net/bridge/netfilter/nft_meta_bridge.c | 5 +- net/bridge/netfilter/nft_reject_bridge.c | 3 +- net/ipv4/netfilter/arp_tables.c | 2 +- net/ipv4/netfilter/ip_tables.c | 2 +- net/ipv6/netfilter/ip6_tables.c | 2 +- net/netfilter/nf_conntrack_core.c | 2 +- net/netfilter/nf_conntrack_netlink.c | 9 +-- net/netfilter/nf_nat_core.c | 2 +- net/netfilter/nf_tables_api.c | 126 ++++++++++++++++++++----------- net/netfilter/nft_compat.c | 6 +- net/netfilter/nft_counter.c | 90 +++++++++++----------- net/netfilter/nft_dynset.c | 18 ++--- net/netfilter/nft_fib.c | 3 +- net/netfilter/nft_flow_offload.c | 3 +- net/netfilter/nft_fwd_netdev.c | 3 +- net/netfilter/nft_immediate.c | 3 +- net/netfilter/nft_lookup.c | 3 +- net/netfilter/nft_masq.c | 3 +- net/netfilter/nft_meta.c | 6 +- net/netfilter/nft_nat.c | 3 +- net/netfilter/nft_osf.c | 3 +- net/netfilter/nft_queue.c | 3 +- net/netfilter/nft_redir.c | 3 +- net/netfilter/nft_reject.c | 3 +- net/netfilter/nft_reject_inet.c | 3 +- net/netfilter/nft_reject_netdev.c | 3 +- net/netfilter/nft_rt.c | 3 +- net/netfilter/nft_socket.c | 3 +- net/netfilter/nft_synproxy.c | 3 +- net/netfilter/nft_tproxy.c | 3 +- net/netfilter/nft_xfrm.c | 3 +- 38 files changed, 206 insertions(+), 178 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/16 netfilter: ctnetlink: support CTA_FILTER for flush
From: Changliang Wu <[email protected]>
From cb8aa9a, we can use kernel side filtering for dump, but this capability is not available for flush.
This Patch allows advanced filter with CTA_FILTER for flush
Performace
1048576 ct flows in total, delete 50,000 flows by origin src ip 3.06s -> dump all, compare and delete
584ms -> directly flush with filter
--- net/netfilter/nf_conntrack_netlink.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
net/netfilter/nf_conntrack_netlink.c +3 -6
@@ -1579,9 +1579,6 @@ static int ctnetlink_flush_conntrack(struct net *net, };if (ctnetlink_needs_filter (family, cda)) { -if (cda[CTA_FILTER ]) -return -EOPNOTSUPP ; - filter =ctnetlink_alloc_filter (cda, family);if (IS_ERR (filter))return PTR_ERR (filter);
@@ -1610,14 +1607,14 @@ static int ctnetlink_del_conntrack(struct sk_buff *skb,if (err <0 )return err; -if (cda[CTA_TUPLE_ORIG ]) +if (cda[CTA_TUPLE_ORIG ] && !cda[CTA_FILTER ]) err =ctnetlink_parse_tuple (cda, &tuple,CTA_TUPLE_ORIG , family, &zone); -else if (cda[CTA_TUPLE_REPLY ]) +else if (cda[CTA_TUPLE_REPLY ] && !cda[CTA_FILTER ]) err =ctnetlink_parse_tuple (cda, &tuple,CTA_TUPLE_REPLY , family, &zone);else { -u_int8_t u3 = info->nfmsg->version ? family :AF_UNSPEC ; +u8 u3 = info->nfmsg->version || cda[CTA_FILTER ] ? family :AF_UNSPEC ; return ctnetlink_flush_conntrack(info->net, cda, NETLINK_CB(skb).portid,
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/16 netfilter: nft_counter: Use u64_stats_t for statistic.
In reply to Pablo Neira AyusoFrom: Sebastian Andrzej Siewior <[email protected]>
The nft_counter uses two s64 counters for statistics. Those two are protected by a seqcount to ensure that the 64bit variable is always properly seen during updates even on 32bit architectures where the store is performed by two writes. A side effect is that the two counter (bytes and packet) are written and read together in the same window.
This can be replaced with u64_stats_t. write_seqcount_begin()/ end() is replaced with u64_stats_update_begin()/ end() and behaves the same way as with seqcount_t on 32bit architectures. Additionally there is a preempt_disable on PREEMPT_RT to ensure that a reader does not preempt a writer.
On 64bit architectures the macros are removed and the reads happen without any retries. This also means that the reader can observe one counter (bytes) from before the update and the other counter (packets) but that is okay since there is no requirement to have both counter from the same update window.
Convert the statistic to u64_stats_t. There is one optimisation:nft_counter_do_init() and nft_counter_clone() allocate a new per-CPU counter and assign a value to it. During this assignment preemption is disabled which is not needed because the counter is not yet exposed to the system so there can not be another writer or reader. Therefore disabling preemption is omitted and raw_cpu_ptr() is used to obtain a pointer to a counter for the assignment.
--- net/netfilter/nft_counter.c | 90 +++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 44 deletions(-)
net/netfilter/nft_counter.c +46 -44
@@ -8,7 +8,7 @@#include <linux/kernel.h> #include <linux/init.h> #include <linux/module.h> -#include <linux/seqlock.h> +#include <linux/u64_stats_sync.h> #include <linux/netlink.h> #include <linux/netfilter.h> #include <linux/netfilter/nf_tables.h>
@@ -17,6 +17,11 @@#include <net/netfilter/nf_tables_offload.h> struct nft_counter { +u64_stats_t bytes; +u64_stats_t packets; +}; + +struct nft_counter_tot {s64 bytes;s64 packets; };
@@ -25,25 +30,24 @@ struct nft_counter_percpu_priv {struct nft_counter __percpu *counter; }; -static DEFINE_PER_CPU(seqcount_t, nft_counter_seq); +static DEFINE_PER_CPU(struct u64_stats_sync, nft_counter_sync);static inline void nft_counter_do_eval (struct nft_counter_percpu_priv *priv,struct nft_regs *regs,const struct nft_pktinfo *pkt) { +struct u64_stats_sync *nft_sync;struct nft_counter *this_cpu; -seqcount_t *myseq;local_bh_disable (); this_cpu =this_cpu_ptr (priv->counter); - myseq =this_cpu_ptr (&nft_counter_seq); - -write_seqcount_begin (myseq); + nft_sync =this_cpu_ptr (&nft_counter_sync); - this_cpu->bytes += pkt->skb->len; - this_cpu->packets++; +u64_stats_update_begin (nft_sync); +u64_stats_add (&this_cpu->bytes, pkt->skb->len); +u64_stats_inc (&this_cpu->packets); +u64_stats_update_end (nft_sync); -write_seqcount_end (myseq);local_bh_enable (); }
@@ -66,17 +70,16 @@ static int nft_counter_do_init(const struct nlattr * const tb[],if (cpu_stats ==NULL )return -ENOMEM ; -preempt_disable (); - this_cpu =this_cpu_ptr (cpu_stats); + this_cpu =raw_cpu_ptr (cpu_stats);if (tb[NFTA_COUNTER_PACKETS ]) { - this_cpu->packets = -be64_to_cpu (nla_get_be64 (tb[NFTA_COUNTER_PACKETS ])); +u64_stats_set (&this_cpu->packets, +be64_to_cpu (nla_get_be64 (tb[NFTA_COUNTER_PACKETS ]))); }if (tb[NFTA_COUNTER_BYTES ]) { - this_cpu->bytes = -be64_to_cpu (nla_get_be64 (tb[NFTA_COUNTER_BYTES ])); +u64_stats_set (&this_cpu->bytes, +be64_to_cpu (nla_get_be64 (tb[NFTA_COUNTER_BYTES ]))); } -preempt_enable (); + priv->counter = cpu_stats;return 0 ; }
@@ -104,40 +107,41 @@ static void nft_counter_obj_destroy(const struct nft_ctx *ctx, }static void nft_counter_reset (struct nft_counter_percpu_priv *priv, -struct nft_counter *total) +struct nft_counter_tot *total) { +struct u64_stats_sync *nft_sync;struct nft_counter *this_cpu; -seqcount_t *myseq;local_bh_disable (); this_cpu =this_cpu_ptr (priv->counter); - myseq =this_cpu_ptr (&nft_counter_seq); + nft_sync =this_cpu_ptr (&nft_counter_sync); + +u64_stats_update_begin (nft_sync); +u64_stats_add (&this_cpu->packets, -total->packets); +u64_stats_add (&this_cpu->bytes, -total->bytes); +u64_stats_update_end (nft_sync); -write_seqcount_begin (myseq); - this_cpu->packets -= total->packets; - this_cpu->bytes -= total->bytes; -write_seqcount_end (myseq);local_bh_enable (); }static void nft_counter_fetch (struct nft_counter_percpu_priv *priv, -struct nft_counter *total) +struct nft_counter_tot *total) {struct nft_counter *this_cpu; -const seqcount_t *myseq;u64 bytes, packets;unsigned int seq;int cpu;memset (total,0 ,sizeof (*total));for_each_possible_cpu (cpu) { - myseq =per_cpu_ptr (&nft_counter_seq, cpu); +struct u64_stats_sync *nft_sync =per_cpu_ptr (&nft_counter_sync, cpu); + this_cpu =per_cpu_ptr (priv->counter, cpu);do { - seq =read_seqcount_begin (myseq); - bytes = this_cpu->bytes; - packets = this_cpu->packets; - }while (read_seqcount_retry (myseq, seq)); + seq =u64_stats_fetch_begin (nft_sync); + bytes =u64_stats_read (&this_cpu->bytes); + packets =u64_stats_read (&this_cpu->packets); + }while (u64_stats_fetch_retry (nft_sync, seq)); total->bytes += bytes; total->packets += packets;
@@ -148,7 +152,7 @@ static int nft_counter_do_dump(struct sk_buff *skb, struct nft_counter_percpu_priv *priv, bool reset) { -struct nft_counter total; +struct nft_counter_tot total;nft_counter_fetch (priv, &total);
@@ -237,7 +241,7 @@ static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src, gstruct nft_counter_percpu_priv *priv_clone =nft_expr_priv (dst);struct nft_counter __percpu *cpu_stats;struct nft_counter *this_cpu; -struct nft_counter total; +struct nft_counter_tot total;nft_counter_fetch (priv, &total);
@@ -245,11 +249,9 @@ static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src, gif (cpu_stats ==NULL )return -ENOMEM ; -preempt_disable (); - this_cpu =this_cpu_ptr (cpu_stats); - this_cpu->packets = total.packets; - this_cpu->bytes = total.bytes; -preempt_enable (); + this_cpu =raw_cpu_ptr (cpu_stats); +u64_stats_set (&this_cpu->packets, total.packets); +u64_stats_set (&this_cpu->bytes, total.bytes); priv_clone->counter = cpu_stats;return 0 ;
@@ -267,17 +269,17 @@ static void nft_counter_offload_stats(struct nft_expr *expr, const struct flow_stats *stats) {struct nft_counter_percpu_priv *priv =nft_expr_priv (expr); +struct u64_stats_sync *nft_sync;struct nft_counter *this_cpu; -seqcount_t *myseq;local_bh_disable (); this_cpu =this_cpu_ptr (priv->counter); - myseq =this_cpu_ptr (&nft_counter_seq); + nft_sync =this_cpu_ptr (&nft_counter_sync); -write_seqcount_begin (myseq); - this_cpu->packets += stats->pkts; - this_cpu->bytes += stats->bytes; -write_seqcount_end (myseq); +u64_stats_update_begin (nft_sync); +u64_stats_add (&this_cpu->packets, stats->pkts); +u64_stats_add (&this_cpu->bytes, stats->bytes); +u64_stats_update_end (nft_sync);local_bh_enable (); }
@@ -286,7 +288,7 @@ void nft_counter_init_seqcount(void)int cpu;for_each_possible_cpu (cpu) -seqcount_init (per_cpu_ptr (&nft_counter_seq, cpu)); +u64_stats_init (per_cpu_ptr (&nft_counter_sync, cpu)); }struct nft_expr_type nft_counter_type;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/16 netfilter: Use kmemdup_array instead of kmemdup for multiple allocation
In reply to Pablo Neira AyusoFrom: Yan Zhen <[email protected]>
When we are allocating an array, using kmemdup_array() to take care about multiplication and possible overflows.
Also it makes auditing the code easier.
--- net/bridge/netfilter/ebtables.c | 2 +- net/ipv4/netfilter/arp_tables.c | 2 +- net/ipv4/netfilter/ip_tables.c | 2 +- net/ipv6/netfilter/ip6_tables.c | 2 +- net/netfilter/nf_nat_core.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-)
net/bridge/netfilter/ebtables.c +1 -1
@@ -1256,7 +1256,7 @@ int ebt_register_table(struct net *net, const struct ebt_table *input_table, goto free_unlock; } - ops =kmemdup (template_ops,sizeof (*ops) * num_ops,GFP_KERNEL ); + ops =kmemdup_array (template_ops, num_ops,sizeof (*ops),GFP_KERNEL );if (!ops) { ret = -ENOMEM ;if (newinfo->nentries)
net/ipv4/netfilter/arp_tables.c +1 -1
@@ -1547,7 +1547,7 @@ int arpt_register_table(struct net *net, goto out_free; } - ops =kmemdup (template_ops,sizeof (*ops) * num_ops,GFP_KERNEL ); + ops =kmemdup_array (template_ops, num_ops,sizeof (*ops),GFP_KERNEL );if (!ops) { ret = -ENOMEM ; goto out_free;
net/ipv4/netfilter/ip_tables.c +1 -1
@@ -1767,7 +1767,7 @@ int ipt_register_table(struct net *net, const struct xt_table *table, goto out_free; } - ops =kmemdup (template_ops,sizeof (*ops) * num_ops,GFP_KERNEL ); + ops =kmemdup_array (template_ops, num_ops,sizeof (*ops),GFP_KERNEL );if (!ops) { ret = -ENOMEM ; goto out_free;
net/ipv6/netfilter/ip6_tables.c +1 -1
@@ -1773,7 +1773,7 @@ int ip6t_register_table(struct net *net, const struct xt_table *table, goto out_free; } - ops =kmemdup (template_ops,sizeof (*ops) * num_ops,GFP_KERNEL ); + ops =kmemdup_array (template_ops, num_ops,sizeof (*ops),GFP_KERNEL );if (!ops) { ret = -ENOMEM ; goto out_free;
net/netfilter/nf_nat_core.c +1 -1
@@ -1104,7 +1104,7 @@ int nf_nat_register_fn(struct net *net, u8 pf, const struct nf_hook_ops *ops,if (!nat_proto_net->nat_hook_ops) {WARN_ON (nat_proto_net->users !=0 ); - nat_ops =kmemdup (orig_nat_ops,sizeof (*orig_nat_ops) * ops_count,GFP_KERNEL ); + nat_ops =kmemdup_array (orig_nat_ops, ops_count,sizeof (*orig_nat_ops),GFP_KERNEL );if (!nat_ops) {mutex_unlock (&nf_nat_proto_mutex);return -ENOMEM ;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/16 netfilter: conntrack: Convert to use ERR_CAST()
In reply to Pablo Neira AyusoFrom: Shen Lichuan <[email protected]>
Use the ERR_CAST macro to clearly indicate that this is a pointer to an error value and that a type conversion was performed.
--- net/netfilter/nf_conntrack_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_conntrack_core.c +1 -1
@@ -1722,7 +1722,7 @@ init_conntrack(struct net *net, struct nf_conn *tmpl, ct =__nf_conntrack_alloc (net, zone, tuple, &repl_tuple,GFP_ATOMIC , hash);if (IS_ERR (ct)) -return (struct nf_conntrack_tuple_hash *)ct; +return ERR_CAST (ct);if (!nf_ct_add_synproxy (ct, tmpl)) {nf_conntrack_free (ct);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/16 netfilter: nf_tables: drop unused 3rd argument from validate callback ops
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Since commit a654de8fdc18 ("netfilter: nf_tables: fix chain dependency validation") the validate() callback no longer needs the return pointer argument.
--- include/net/netfilter/nf_tables.h | 3 +-- include/net/netfilter/nft_fib.h | 4 +--- include/net/netfilter/nft_meta.h | 3 +-- include/net/netfilter/nft_reject.h | 3 +-- net/bridge/netfilter/nft_meta_bridge.c | 5 ++--- net/bridge/netfilter/nft_reject_bridge.c | 3 +-- net/netfilter/nf_tables_api.c | 3 +-- net/netfilter/nft_compat.c | 6 ++---- net/netfilter/nft_fib.c | 3 +-- net/netfilter/nft_flow_offload.c | 3 +-- net/netfilter/nft_fwd_netdev.c | 3 +-- net/netfilter/nft_immediate.c | 3 +-- net/netfilter/nft_lookup.c | 3 +-- net/netfilter/nft_masq.c | 3 +-- net/netfilter/nft_meta.c | 6 ++---- net/netfilter/nft_nat.c | 3 +-- net/netfilter/nft_osf.c | 3 +-- net/netfilter/nft_queue.c | 3 +-- net/netfilter/nft_redir.c | 3 +-- net/netfilter/nft_reject.c | 3 +-- net/netfilter/nft_reject_inet.c | 3 +-- net/netfilter/nft_reject_netdev.c | 3 +-- net/netfilter/nft_rt.c | 3 +-- net/netfilter/nft_socket.c | 3 +-- net/netfilter/nft_synproxy.c | 3 +-- net/netfilter/nft_tproxy.c | 3 +-- net/netfilter/nft_xfrm.c | 3 +-- 27 files changed, 30 insertions(+), 60 deletions(-)
include/net/netfilter/nf_tables.h +1 -2
@@ -961,8 +961,7 @@ struct nft_expr_ops {const struct nft_expr *expr, bool reset);int (*validate)(const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data); +const struct nft_expr *expr);bool (*reduce)(struct nft_regs_track *track,const struct nft_expr *expr); bool (*gc)(struct net *net,
include/net/netfilter/nft_fib.h +1 -3
@@ -21,9 +21,7 @@ nft_fib_is_loopback(const struct sk_buff *skb, const struct net_device *in)int nft_fib_dump (struct sk_buff *skb,const struct nft_expr *expr,bool reset);int nft_fib_init (const struct nft_ctx *ctx,const struct nft_expr *expr,const struct nlattr *const tb[]); -int nft_fib_validate (const struct nft_ctx *ctx,const struct nft_expr *expr, -const struct nft_data **data); - +int nft_fib_validate (const struct nft_ctx *ctx,const struct nft_expr *expr);void nft_fib4_eval_type (const struct nft_expr *expr,struct nft_regs *regs,const struct nft_pktinfo *pkt);
include/net/netfilter/nft_meta.h +1 -2
@@ -41,8 +41,7 @@ void nft_meta_set_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr);int nft_meta_set_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data); +const struct nft_expr *expr);bool nft_meta_get_reduce (struct nft_regs_track *track,const struct nft_expr *expr);
include/net/netfilter/nft_reject.h +1 -2
@@ -15,8 +15,7 @@ struct nft_reject {extern const struct nla_policy nft_reject_policy[];int nft_reject_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data); +const struct nft_expr *expr); int nft_reject_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
net/bridge/netfilter/nft_meta_bridge.c +2 -3
@@ -168,8 +168,7 @@ static bool nft_meta_bridge_set_reduce(struct nft_regs_track *track, }static int nft_meta_bridge_set_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {struct nft_meta *priv =nft_expr_priv (expr);unsigned int hooks;
@@ -179,7 +178,7 @@ static int nft_meta_bridge_set_validate(const struct nft_ctx *ctx, hooks =1 <<NF_BR_PRE_ROUTING ;break ;default : -return nft_meta_set_validate (ctx, expr, data); +return nft_meta_set_validate (ctx, expr); }return nft_chain_validate_hooks (ctx->chain, hooks);
net/bridge/netfilter/nft_reject_bridge.c +1 -2
@@ -170,8 +170,7 @@ static void nft_reject_bridge_eval(const struct nft_expr *expr, }static int nft_reject_bridge_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {return nft_chain_validate_hooks (ctx->chain, (1 <<NF_BR_PRE_ROUTING ) | (1 <<NF_BR_LOCAL_IN ));
net/netfilter/nf_tables_api.c +1 -2
@@ -3886,7 +3886,6 @@ static void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rint nft_chain_validate (const struct nft_ctx *ctx,const struct nft_chain *chain) {struct nft_expr *expr, *last; -const struct nft_data *data;struct nft_rule *rule;int err;
@@ -3907,7 +3906,7 @@ int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain)/* This may call nft_chain_validate() recursively, * callers that do so must increment ctx->level. */ - err = expr->ops->validate (ctx, expr, &data); + err = expr->ops->validate (ctx, expr);if (err <0 )return err; }
net/netfilter/nft_compat.c +2 -4
@@ -350,8 +350,7 @@ static int nft_target_dump(struct sk_buff *skb, }static int nft_target_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {struct xt_target *target = expr->ops->data;unsigned int hook_mask =0 ;
@@ -611,8 +610,7 @@ static int nft_match_large_dump(struct sk_buff *skb, }static int nft_match_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {struct xt_match *match = expr->ops->data;unsigned int hook_mask =0 ;
net/netfilter/nft_fib.c +1 -2
@@ -26,8 +26,7 @@ const struct nla_policy nft_fib_policy[NFTA_FIB_MAX + 1] = { };EXPORT_SYMBOL (nft_fib_policy); -int nft_fib_validate (const struct nft_ctx *ctx,const struct nft_expr *expr, -const struct nft_data **data) +int nft_fib_validate (const struct nft_ctx *ctx,const struct nft_expr *expr) {const struct nft_fib *priv =nft_expr_priv (expr);unsigned int hooks;
net/netfilter/nft_flow_offload.c +1 -2
@@ -380,8 +380,7 @@ static void nft_flow_offload_eval(const struct nft_expr *expr, }static int nft_flow_offload_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {unsigned int hook_mask = (1 <<NF_INET_FORWARD );
net/netfilter/nft_fwd_netdev.c +1 -2
@@ -204,8 +204,7 @@ static int nft_fwd_neigh_dump(struct sk_buff *skb, }static int nft_fwd_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {return nft_chain_validate_hooks (ctx->chain, (1 <<NF_NETDEV_INGRESS ) | (1 <<NF_NETDEV_EGRESS ));
net/netfilter/nft_immediate.c +1 -2
@@ -244,8 +244,7 @@ static int nft_immediate_dump(struct sk_buff *skb, }static int nft_immediate_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **d) +const struct nft_expr *expr) {const struct nft_immediate_expr *priv =nft_expr_priv (expr);struct nft_ctx *pctx = (struct nft_ctx *)ctx;
net/netfilter/nft_lookup.c +1 -2
@@ -206,8 +206,7 @@ static int nft_lookup_dump(struct sk_buff *skb, }static int nft_lookup_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **d) +const struct nft_expr *expr) {const struct nft_lookup *priv =nft_expr_priv (expr);struct nft_set_iter iter;
net/netfilter/nft_masq.c +1 -2
@@ -27,8 +27,7 @@ static const struct nla_policy nft_masq_policy[NFTA_MASQ_MAX + 1] = { };static int nft_masq_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {int err;
net/netfilter/nft_meta.c +2 -4
@@ -581,8 +581,7 @@ static int nft_meta_get_validate_xfrm(const struct nft_ctx *ctx) }static int nft_meta_get_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {const struct nft_meta *priv =nft_expr_priv (expr);
@@ -600,8 +599,7 @@ static int nft_meta_get_validate(const struct nft_ctx *ctx, }int nft_meta_set_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {struct nft_meta *priv =nft_expr_priv (expr);unsigned int hooks;
net/netfilter/nft_nat.c +1 -2
@@ -137,8 +137,7 @@ static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = { };static int nft_nat_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {struct nft_nat *priv =nft_expr_priv (expr);int err;
net/netfilter/nft_osf.c +1 -2
@@ -108,8 +108,7 @@ static int nft_osf_dump(struct sk_buff *skb, }static int nft_osf_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {unsigned int hooks;
net/netfilter/nft_queue.c +1 -2
@@ -69,8 +69,7 @@ static void nft_queue_sreg_eval(const struct nft_expr *expr, }static int nft_queue_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) { static const unsigned int supported_hooks = ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_IN) |
net/netfilter/nft_redir.c +1 -2
@@ -27,8 +27,7 @@ static const struct nla_policy nft_redir_policy[NFTA_REDIR_MAX + 1] = { };static int nft_redir_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {int err;
net/netfilter/nft_reject.c +1 -2
@@ -24,8 +24,7 @@ const struct nla_policy nft_reject_policy[NFTA_REJECT_MAX + 1] = {EXPORT_SYMBOL_GPL (nft_reject_policy);int nft_reject_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) { return nft_chain_validate_hooks(ctx->chain, (1 << NF_INET_LOCAL_IN) |
net/netfilter/nft_reject_inet.c +1 -2
@@ -61,8 +61,7 @@ static void nft_reject_inet_eval(const struct nft_expr *expr, }static int nft_reject_inet_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) { return nft_chain_validate_hooks(ctx->chain, (1 << NF_INET_LOCAL_IN) |
net/netfilter/nft_reject_netdev.c +1 -2
@@ -145,8 +145,7 @@ static void nft_reject_netdev_eval(const struct nft_expr *expr, }static int nft_reject_netdev_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) {return nft_chain_validate_hooks (ctx->chain, (1 <<NF_NETDEV_INGRESS )); }
net/netfilter/nft_rt.c +1 -2
@@ -160,8 +160,7 @@ static int nft_rt_get_dump(struct sk_buff *skb,return -1 ; } -static int nft_rt_validate (const struct nft_ctx *ctx,const struct nft_expr *expr, -const struct nft_data **data) +static int nft_rt_validate (const struct nft_ctx *ctx,const struct nft_expr *expr) {const struct nft_rt *priv =nft_expr_priv (expr);unsigned int hooks;
net/netfilter/nft_socket.c +1 -2
@@ -239,8 +239,7 @@ static bool nft_socket_reduce(struct nft_regs_track *track, }static int nft_socket_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) { if (ctx->family != NFPROTO_IPV4 && ctx->family != NFPROTO_IPV6 &&
net/netfilter/nft_synproxy.c +1 -2
@@ -248,8 +248,7 @@ static void nft_synproxy_eval(const struct nft_expr *expr, }static int nft_synproxy_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) { if (ctx->family != NFPROTO_IPV4 && ctx->family != NFPROTO_IPV6 &&
net/netfilter/nft_tproxy.c +1 -2
@@ -313,8 +313,7 @@ static int nft_tproxy_dump(struct sk_buff *skb, }static int nft_tproxy_validate (const struct nft_ctx *ctx, -const struct nft_expr *expr, -const struct nft_data **data) +const struct nft_expr *expr) { if (ctx->family != NFPROTO_IPV4 && ctx->family != NFPROTO_IPV6 &&
net/netfilter/nft_xfrm.c +1 -2
@@ -229,8 +229,7 @@ static int nft_xfrm_get_dump(struct sk_buff *skb,return 0 ; } -static int nft_xfrm_validate (const struct nft_ctx *ctx,const struct nft_expr *expr, -const struct nft_data **data) +static int nft_xfrm_validate (const struct nft_ctx *ctx,const struct nft_expr *expr) {const struct nft_xfrm *priv =nft_expr_priv (expr);unsigned int hooks;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 6/16 netfilter: nf_tables: Correct spelling in nf_tables.h
In reply to Pablo Neira AyusoFrom: Simon Horman <[email protected]>
Correct spelling in nf_tables.h.
As reported by codespell.
--- include/net/netfilter/nf_tables.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/net/netfilter/nf_tables.h +1 -1
@@ -687,7 +687,7 @@ void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);* @NFT_SET_EXT_TIMEOUT: element timeout * @NFT_SET_EXT_EXPIRATION: element expiration time * @NFT_SET_EXT_USERDATA: user data associated with the element -* @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element +* @NFT_SET_EXT_EXPRESSIONS: expressions associated with the element * @NFT_SET_EXT_OBJREF: stateful object reference associated with element * @NFT_SET_EXT_NUM: number of extension types */
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 7/16 netfilter: nf_tables: Add missing Kernel doc
In reply to Pablo Neira AyusoFrom: Simon Horman <[email protected]>
- Add missing documentation of struct field and enum items.
- Add missing documentation of function parameter.
Flagged by ./scripts/kernel-doc -none.
No functional change intended.
Compile tested only.
--- include/net/netfilter/nf_tables.h | 2 ++ include/net/netfilter/nf_tproxy.h | 1 + 2 files changed, 3 insertions(+)
include/net/netfilter/nf_tables.h +2 -0
@@ -209,6 +209,7 @@ static inline void nft_data_copy(u32 *dst, const struct nft_data *src,* @family: protocol family * @level: depth of the chains * @report: notify via unicast netlink message +* @reg_inited: bitmap of initialised registers */ struct nft_ctx {struct net *net;
@@ -313,6 +314,7 @@ static inline void *nft_elem_priv_cast(const struct nft_elem_priv *priv)/** * enum nft_iter_type - nftables set iterator type * +* @NFT_ITER_UNSPEC: unspecified, to catch errors * @NFT_ITER_READ: read-only iteration over set elements * @NFT_ITER_UPDATE: iteration under mutex to update set element state */
include/net/netfilter/nf_tproxy.h +1 -0
@@ -36,6 +36,7 @@ __be32 nf_tproxy_laddr4(struct sk_buff *skb, __be32 user_laddr, __be32 daddr);/** * nf_tproxy_handle_time_wait4 - handle IPv4 TCP TIME_WAIT reopen redirections +* @net: The network namespace. * @skb: The skb being processed. * @laddr: IPv4 address to redirect to or zero. * @lport: TCP port to redirect to or zero.
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 8/16 netfilter: nf_tables: elements with timeout below CONFIG_HZ never expire
In reply to Pablo Neira AyusoElement timeout that is below CONFIG_HZ never expires because the timeout extension is not allocated given that nf_msecs_to_jiffies64() returns 0. Set timeout to the minimum value to honor timeout.
--- net/netfilter/nf_tables_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +1 -1
@@ -4593,7 +4593,7 @@ int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result)return -ERANGE ; ms *=NSEC_PER_MSEC ; - *result =nsecs_to_jiffies64 (ms); + *result =nsecs_to_jiffies64 (ms) ? : !!ms;return 0 ; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 9/16 netfilter: nf_tables: reject element expiration with no timeout
In reply to Pablo Neira AyusoIf element timeout is unset and set provides no default timeout, the element expiration is silently ignored, reject this instead to let user know this is unsupported.
Also prepare for supporting timeout that never expire, where zero timeout and expiration must be also rejected.
--- net/netfilter/nf_tables_api.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/nf_tables_api.c +3 -0
@@ -6923,6 +6923,9 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,if (nla[NFTA_SET_ELEM_EXPIRATION ] !=NULL ) {if (!(set->flags &NFT_SET_TIMEOUT ))return -EINVAL ; +if (timeout ==0 ) +return -EOPNOTSUPP ; + err =nf_msecs_to_jiffies64 (nla[NFTA_SET_ELEM_EXPIRATION ], &expiration);if (err)
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 10/16 netfilter: nf_tables: reject expiration higher than timeout
In reply to Pablo Neira AyusoReport ERANGE to userspace if user specifies an expiration larger than the timeout.
--- net/netfilter/nf_tables_api.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/nf_tables_api.c +3 -0
@@ -6930,6 +6930,9 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, &expiration);if (err)return err; + +if (expiration > timeout) +return -ERANGE ; }if (nla[NFTA_SET_ELEM_EXPR ]) {
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 11/16 netfilter: nf_tables: remove annotation to access set timeout while holding lock
In reply to Pablo Neira AyusoMutex is held when adding an element, no need for READ_ONCE, remove it.
--- net/netfilter/nf_tables_api.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nf_tables_api.c +2 -2
@@ -6916,7 +6916,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,return err; }else if (set->flags &NFT_SET_TIMEOUT && !(flags &NFT_SET_ELEM_INTERVAL_END )) { - timeout =READ_ONCE (set->timeout); + timeout = set->timeout; } expiration =0 ;
@@ -7023,7 +7023,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,if (err <0 ) goto err_parse_key_end; -if (timeout !=READ_ONCE (set->timeout)) { +if (timeout != set->timeout) { err =nft_set_ext_add (&tmpl,NFT_SET_EXT_TIMEOUT );if (err <0 ) goto err_parse_key_end;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 12/16 netfilter: nft_dynset: annotate data-races around set timeout
In reply to Pablo Neira Ayusoset timeout can be read locklessly while being updated from control plane, add annotation.
--- net/netfilter/nft_dynset.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
net/netfilter/nft_dynset.c +3 -3
@@ -56,7 +56,7 @@ static struct nft_elem_priv *nft_dynset_new(struct nft_set *set,if (!atomic_add_unless (&set->nelems,1 , set->size))return NULL ; - timeout = priv->timeout ? : set->timeout; + timeout = priv->timeout ? :READ_ONCE (set->timeout); elem_priv = nft_set_elem_init(set, &priv->tmpl, ®s->data[priv->sreg_key], NULL, ®s->data[priv->sreg_data],
@@ -95,7 +95,7 @@ void nft_dynset_eval(const struct nft_expr *expr, expr, regs, &ext)) {if (priv->op ==NFT_DYNSET_OP_UPDATE &&nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION )) { - timeout = priv->timeout ? : set->timeout; + timeout = priv->timeout ? :READ_ONCE (set->timeout); *nft_set_ext_expiration (ext) =get_jiffies_64 () + timeout; }
@@ -313,7 +313,7 @@ static int nft_dynset_init(const struct nft_ctx *ctx,nft_dynset_ext_add_expr (priv);if (set->flags &NFT_SET_TIMEOUT ) { -if (timeout || set->timeout) { +if (timeout ||READ_ONCE (set->timeout)) {nft_set_ext_add (&priv->tmpl,NFT_SET_EXT_TIMEOUT );nft_set_ext_add (&priv->tmpl,NFT_SET_EXT_EXPIRATION ); }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 13/16 netfilter: nf_tables: annotate data-races around element expiration
In reply to Pablo Neira Ayusoelement expiration can be read-write locklessly, it can be written by dynset and read from netlink dump, add annotation.
--- include/net/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 2 +- net/netfilter/nft_dynset.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +1 -1
@@ -835,7 +835,7 @@ static inline bool __nft_set_elem_expired(const struct nft_set_ext *ext, u64 tstamp) {return nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION ) && -time_after_eq64 (tstamp, *nft_set_ext_expiration (ext)); +time_after_eq64 (tstamp,READ_ONCE (*nft_set_ext_expiration (ext))); }static inline bool nft_set_elem_expired (const struct nft_set_ext *ext)
net/netfilter/nf_tables_api.c +1 -1
@@ -5827,7 +5827,7 @@ static int nf_tables_fill_setelem(struct sk_buff *skb,if (nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION )) {u64 expires, now =get_jiffies_64 (); - expires = *nft_set_ext_expiration (ext); + expires =READ_ONCE (*nft_set_ext_expiration (ext));if (time_before64 (now, expires)) expires -= now;else
net/netfilter/nft_dynset.c +1 -1
@@ -96,7 +96,7 @@ void nft_dynset_eval(const struct nft_expr *expr,if (priv->op ==NFT_DYNSET_OP_UPDATE &&nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION )) { timeout = priv->timeout ? :READ_ONCE (set->timeout); - *nft_set_ext_expiration (ext) =get_jiffies_64 () + timeout; +WRITE_ONCE (*nft_set_ext_expiration (ext),get_jiffies_64 () + timeout); }nft_set_elem_update_expr (ext, regs, pkt);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 14/16 netfilter: nf_tables: consolidate timeout extension for elements
In reply to Pablo Neira AyusoExpiration and timeout are stored in separated set element extensions, but they are tightly coupled. Consolidate them in a single extension to simplify and prepare for set element updates.
--- include/net/netfilter/nf_tables.h | 18 ++++++------- net/netfilter/nf_tables_api.c | 43 ++++++++++++------------------- net/netfilter/nft_dynset.c | 13 ++++------ 3 files changed, 30 insertions(+), 44 deletions(-)
include/net/netfilter/nf_tables.h +8 -10
@@ -687,7 +687,6 @@ void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);* @NFT_SET_EXT_DATA: mapping data * @NFT_SET_EXT_FLAGS: element flags * @NFT_SET_EXT_TIMEOUT: element timeout -* @NFT_SET_EXT_EXPIRATION: element expiration time * @NFT_SET_EXT_USERDATA: user data associated with the element * @NFT_SET_EXT_EXPRESSIONS: expressions associated with the element * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
@@ -699,7 +698,6 @@ enum nft_set_extensions {NFT_SET_EXT_DATA ,NFT_SET_EXT_FLAGS ,NFT_SET_EXT_TIMEOUT , -NFT_SET_EXT_EXPIRATION ,NFT_SET_EXT_USERDATA ,NFT_SET_EXT_EXPRESSIONS , NFT_SET_EXT_OBJREF,
@@ -811,14 +809,14 @@ static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)return nft_set_ext (ext,NFT_SET_EXT_FLAGS ); } -static inline u64 *nft_set_ext_timeout (const struct nft_set_ext *ext) -{ -return nft_set_ext (ext,NFT_SET_EXT_TIMEOUT ); -} +struct nft_timeout { +u64 timeout; +u64 expiration; +}; -static inline u64 *nft_set_ext_expiration (const struct nft_set_ext *ext) +static inline struct nft_timeout *nft_set_ext_timeout (const struct nft_set_ext *ext) { -return nft_set_ext (ext,NFT_SET_EXT_EXPIRATION ); +return nft_set_ext (ext,NFT_SET_EXT_TIMEOUT ); }static inline struct nft_userdata *nft_set_ext_userdata (const struct nft_set_ext *ext)
@@ -834,8 +832,8 @@ static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_exstatic inline bool __nft_set_elem_expired (const struct nft_set_ext *ext,u64 tstamp) { -return nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION ) && -time_after_eq64 (tstamp,READ_ONCE (*nft_set_ext_expiration (ext))); +return nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) && +time_after_eq64 (tstamp,READ_ONCE (nft_set_ext_timeout (ext)->expiration)); }static inline bool nft_set_elem_expired (const struct nft_set_ext *ext)
net/netfilter/nf_tables_api.c +17 -26
73 lines not on this page · raw message
net/netfilter/nft_dynset.c +5 -8
@@ -94,9 +94,9 @@ void nft_dynset_eval(const struct nft_expr *expr,if (set->ops->update (set, ®s->data[priv->sreg_key], nft_dynset_new, expr, regs, &ext)) {if (priv->op ==NFT_DYNSET_OP_UPDATE && -nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION )) { +nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT )) { timeout = priv->timeout ? :READ_ONCE (set->timeout); -WRITE_ONCE (*nft_set_ext_expiration (ext),get_jiffies_64 () + timeout); +WRITE_ONCE (nft_set_ext_timeout (ext)->expiration,get_jiffies_64 () + timeout); }nft_set_elem_update_expr (ext, regs, pkt);
@@ -312,12 +312,9 @@ static int nft_dynset_init(const struct nft_ctx *ctx,if (priv->num_exprs)nft_dynset_ext_add_expr (priv); -if (set->flags &NFT_SET_TIMEOUT ) { -if (timeout ||READ_ONCE (set->timeout)) { -nft_set_ext_add (&priv->tmpl,NFT_SET_EXT_TIMEOUT ); -nft_set_ext_add (&priv->tmpl,NFT_SET_EXT_EXPIRATION ); - } - } +if (set->flags &NFT_SET_TIMEOUT && + (timeout ||READ_ONCE (set->timeout))) +nft_set_ext_add (&priv->tmpl,NFT_SET_EXT_TIMEOUT ); priv->timeout = timeout;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 15/16 netfilter: nf_tables: zero timeout means element never times out
In reply to Pablo Neira AyusoThis patch uses zero as timeout marker for those elements that never expire when the element is created.
If userspace provides no timeout for an element, then the default set timeout applies. However, if no default set timeout is specified and timeout flag is set on, then timeout extension is allocated and timeout is set to zero to allow for future updates.
Use of zero a never timeout marker has been suggested by Phil Sutter.
Note that, in older kernels, it is already possible to define elements that never expire by declaring a set with the set timeout flag set on and no global set timeout, in this case, new element with no explicit timeout never expire do not allocate the timeout extension, hence, they never expire. This approach makes it complicated to accomodate element timeout update, because element extensions do not support reallocations. Therefore, allocate the timeout extension and use the new marker for this case, but do not expose it to userspace to retain backward compatibility in the set listing.
--- include/net/netfilter/nf_tables.h | 7 +++-- include/uapi/linux/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 39 ++++++++++++++---------- net/netfilter/nft_dynset.c | 3 +- 4 files changed, 31 insertions(+), 20 deletions(-)
include/net/netfilter/nf_tables.h +5 -2
@@ -832,8 +832,11 @@ static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_exstatic inline bool __nft_set_elem_expired (const struct nft_set_ext *ext,u64 tstamp) { -return nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) && -time_after_eq64 (tstamp,READ_ONCE (nft_set_ext_timeout (ext)->expiration)); +if (!nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) || +nft_set_ext_timeout (ext)->timeout ==0 ) +return false; + +return time_after_eq64 (tstamp,READ_ONCE (nft_set_ext_timeout (ext)->expiration)); }static inline bool nft_set_elem_expired (const struct nft_set_ext *ext)
include/uapi/linux/netfilter/nf_tables.h +1 -1
@@ -436,7 +436,7 @@ enum nft_set_elem_flags {* @NFTA_SET_ELEM_KEY: key value (NLA_NESTED: nft_data) * @NFTA_SET_ELEM_DATA: data value of mapping (NLA_NESTED: nft_data_attributes) * @NFTA_SET_ELEM_FLAGS: bitmask of nft_set_elem_flags (NLA_U32) -* @NFTA_SET_ELEM_TIMEOUT: timeout value (NLA_U64) +* @NFTA_SET_ELEM_TIMEOUT: timeout value, zero means never times out (NLA_U64) * @NFTA_SET_ELEM_EXPIRATION: expiration time (NLA_U64) * @NFTA_SET_ELEM_USERDATA: user data (NLA_BINARY) * @NFTA_SET_ELEM_EXPR: expression (NLA_NESTED: nft_expr_attributes)
net/netfilter/nf_tables_api.c +23 -16
@@ -5815,24 +5815,31 @@ static int nf_tables_fill_setelem(struct sk_buff *skb, goto nla_put_failure;if (nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT )) { -u64 expires, now =get_jiffies_64 (); +u64 timeout =nft_set_ext_timeout (ext)->timeout; +u64 set_timeout =READ_ONCE (set->timeout); +__be64 msecs =0 ; + +if (set_timeout != timeout) { + msecs =nf_jiffies64_to_msecs (timeout); +if (nla_put_be64 (skb,NFTA_SET_ELEM_TIMEOUT , msecs, +NFTA_SET_ELEM_PAD )) + goto nla_put_failure; + } -if (nft_set_ext_timeout (ext)->timeout !=READ_ONCE (set->timeout) && -nla_put_be64 (skb,NFTA_SET_ELEM_TIMEOUT , -nf_jiffies64_to_msecs (nft_set_ext_timeout (ext)->timeout), -NFTA_SET_ELEM_PAD )) - goto nla_put_failure; +if (timeout >0 ) { +u64 expires, now =get_jiffies_64 (); - expires =READ_ONCE (nft_set_ext_timeout (ext)->expiration); -if (time_before64 (now, expires)) - expires -= now; -else - expires =0 ; + expires =READ_ONCE (nft_set_ext_timeout (ext)->expiration); +if (time_before64 (now, expires)) + expires -= now; +else + expires =0 ; -if (nla_put_be64 (skb,NFTA_SET_ELEM_EXPIRATION , -nf_jiffies64_to_msecs (expires), -NFTA_SET_ELEM_PAD )) - goto nla_put_failure; +if (nla_put_be64 (skb,NFTA_SET_ELEM_EXPIRATION , +nf_jiffies64_to_msecs (expires), +NFTA_SET_ELEM_PAD )) + goto nla_put_failure; + } }if (nft_set_ext_exists (ext,NFT_SET_EXT_USERDATA )) {
@@ -7015,7 +7022,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, goto err_parse_key_end; } -if (timeout >0 ) { +if (set->flags &NFT_SET_TIMEOUT ) { err =nft_set_ext_add (&tmpl,NFT_SET_EXT_TIMEOUT );if (err <0 ) goto err_parse_key_end;
net/netfilter/nft_dynset.c +2 -1
@@ -94,7 +94,8 @@ void nft_dynset_eval(const struct nft_expr *expr,if (set->ops->update (set, ®s->data[priv->sreg_key], nft_dynset_new, expr, regs, &ext)) {if (priv->op ==NFT_DYNSET_OP_UPDATE && -nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT )) { +nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) && +nft_set_ext_timeout (ext)->timeout !=0 ) { timeout = priv->timeout ? :READ_ONCE (set->timeout);WRITE_ONCE (nft_set_ext_timeout (ext)->expiration,get_jiffies_64 () + timeout); }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 16/16 netfilter: nf_tables: set element timeout update support
In reply to Pablo Neira AyusoStore new timeout and expiration in transaction object, use them to update elements from .commit path. Otherwise, discard update if .abort path is exercised.
Use update_flags in the transaction to note whether the timeout, expiration, or both need to be updated.
Annotate access to timeout extension now that it can be updated while lockless read access is possible.
Reject timeout updates on elements with no timeout extension.
Element transaction remains in the 96 bytes kmalloc slab on x86_64 after this update.
This patch requires ("netfilter: nf_tables: use timestamp to check for set element timeout") to make sure an element does not expire while transaction is ongoing.
--- include/net/netfilter/nf_tables.h | 16 ++++++++++- net/netfilter/nf_tables_api.c | 47 ++++++++++++++++++++++++++++--- net/netfilter/nft_dynset.c | 2 +- 3 files changed, 59 insertions(+), 6 deletions(-)
include/net/netfilter/nf_tables.h +15 -1
@@ -833,7 +833,7 @@ static inline bool __nft_set_elem_expired(const struct nft_set_ext *ext, u64 tstamp) {if (!nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) || -nft_set_ext_timeout (ext)->timeout ==0 ) +READ_ONCE (nft_set_ext_timeout (ext)->timeout) ==0 )return false;return time_after_eq64 (tstamp,READ_ONCE (nft_set_ext_timeout (ext)->expiration));
@@ -1749,10 +1749,18 @@ struct nft_trans_table {#define nft_trans_table_update (trans) \ nft_trans_container_table(trans)->update +enum nft_trans_elem_flags { +NFT_TRANS_UPD_TIMEOUT = (1 <<0 ), +NFT_TRANS_UPD_EXPIRATION = (1 <<1 ), +}; +struct nft_trans_elem {struct nft_trans nft_trans;struct nft_set *set;struct nft_elem_priv *elem_priv; +u64 timeout; +u64 expiration; +u8 update_flags;bool bound; };
@@ -1762,6 +1770,12 @@ struct nft_trans_elem { nft_trans_container_elem(trans)->set#define nft_trans_elem_priv (trans) \ nft_trans_container_elem(trans)->elem_priv +#define nft_trans_elem_update_flags (trans) \ + nft_trans_container_elem(trans)->update_flags +#define nft_trans_elem_timeout (trans) \ + nft_trans_container_elem(trans)->timeout +#define nft_trans_elem_expiration (trans) \ + nft_trans_container_elem(trans)->expiration#define nft_trans_elem_set_bound (trans) \ nft_trans_container_elem(trans)->bound
net/netfilter/nf_tables_api.c +43 -4
78 lines not on this page · raw message
net/netfilter/nft_dynset.c +1 -1
@@ -95,7 +95,7 @@ void nft_dynset_eval(const struct nft_expr *expr, expr, regs, &ext)) {if (priv->op ==NFT_DYNSET_OP_UPDATE &&nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) && -nft_set_ext_timeout (ext)->timeout !=0 ) { +READ_ONCE (nft_set_ext_timeout (ext)->timeout) !=0 ) { timeout = priv->timeout ? :READ_ONCE (set->timeout);WRITE_ONCE (nft_set_ext_timeout (ext)->expiration,get_jiffies_64 () + timeout); }
Signature · 1 line
2.30.2
Re: [PATCH net-next 01/16] netfilter: ctnetlink: support CTA_FILTER for flush
In reply to Pablo Neira AyusoThis series was applied to netdev/net-next.git (main) by Pablo Neira Ayuso <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Pablo Neira Ayuso <[email protected]>:
Quoted 8 lines · On Fri, 6 Sep 2024 01:29:05 +0200 you wrote:
From: Changliang Wu <[email protected]>
From cb8aa9a, we can use kernel side filtering for dump, but this capability is not available for flush.
This Patch allows advanced filter with
CTA_FILTERfor flush[...]
Here is the summary with links:
- [net-next,01/16] netfilter: ctnetlink: support CTA_FILTER for flush
https://git.kernel.org/netdev/net-next/c/1ef7f50ccc6e - [net-next,02/16] netfilter: nft_counter: Use u64_stats_t for statistic. https://git.kernel.org/netdev/net-next/c/4a1d3acd6ea8 - [net-next,03/16] netfilter: Use kmemdup_array instead of kmemdup for multiple allocation https://git.kernel.org/netdev/net-next/c/20eb5e7cb78c - [net-next,04/16] netfilter: conntrack: Convert to use ERR_CAST() https://git.kernel.org/netdev/net-next/c/09c0d0aef56b - [net-next,05/16] netfilter: nf_tables: drop unused 3rd argument from validate callback ops https://git.kernel.org/netdev/net-next/c/eaf9b2c875ec - [net-next,06/16] netfilter: nf_tables: Correct spelling in nf_tables.h https://git.kernel.org/netdev/net-next/c/85dfb34bb7d2 - [net-next,07/16] netfilter: nf_tables: Add missing Kernel doc https://git.kernel.org/netdev/net-next/c/c362646b6fc1 - [net-next,08/16] netfilter: nf_tables: elements with timeout below CONFIG_HZ never expire https://git.kernel.org/netdev/net-next/c/e0c47281723f - [net-next,09/16] netfilter: nf_tables: reject element expiration with no timeout https://git.kernel.org/netdev/net-next/c/d2dc429ecb4e - [net-next,10/16] netfilter: nf_tables: reject expiration higher than timeout https://git.kernel.org/netdev/net-next/c/c0f38a8c6017 - [net-next,11/16] netfilter: nf_tables: remove annotation to access set timeout while holding lock https://git.kernel.org/netdev/net-next/c/15d8605c0cf4 - [net-next,12/16] netfilter: nft_dynset: annotate data-races around set timeout https://git.kernel.org/netdev/net-next/c/c5ad8ed61fa8 - [net-next,13/16] netfilter: nf_tables: annotate data-races around element expiration https://git.kernel.org/netdev/net-next/c/73d3c04b710f - [net-next,14/16] netfilter: nf_tables: consolidate timeout extension for elements https://git.kernel.org/netdev/net-next/c/4c5daea9af4f - [net-next,15/16] netfilter: nf_tables: zero timeout means element never times out https://git.kernel.org/netdev/net-next/c/8bfb74ae12fa - [net-next,16/16] netfilter: nf_tables: set element timeout update support https://git.kernel.org/netdev/net-next/c/4201f3938914
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html