Netfilter updates for net-next
Subsystems (4)
Pablo Neira Ayuso
maintainercover Netfilter updates for net-next
Hi,
The following patchset contains Netfilter updates for net-next. Mostly nf_tables updates with two patches for connlabel and br_netfilter.
1) Rename function name to perform on-demand GC for rbtree elements, and replace async GC in rbtree by sync GC. Patches from Florian Westphal.
2) Use commit_mutex for NFT_MSG_GETRULE_RESET to ensure that two concurrent threads invoking this command do not underrun stateful objects. Patches from Phil Sutter.
3) Use single hook to deal with IP and ARP packets in br_netfilter. Patch from Florian Westphal.
4) Use atomic_t in netns->connlabel use counter instead of using a spinlock, also patch from Florian.
5) Cleanups for stateful objects infrastructure in nf_tables. Patches from Phil Sutter.
6) Flush path uses opaque set element offered by the iterator, instead of calling pipapo_deactivate() which looks up for it again.
7) Set backend .flush interface always succeeds, make it return void instead.
8) Add struct nft_elem_priv placeholder structure and use it by replacing void * to pass opaque set element representation from backend to frontend which defeats compiler type checks.
9) Shrink memory consumption of set element transactions, by reducing struct nft_trans_elem object size and reducing stack memory usage.
10) Use struct nft_elem_priv also for set backend .insert operation too.
11) Carry reset flag in nft_set_dump_ctx structure, instead of passing it as a function argument, from Phil Sutter.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git nf-next-23-10-25
Thanks.
----------------------------------------------------------------
The following changes since commit 5e3704030b240ab6878c32abdc2e38b6bac9dfb8:
Merge branch 'bnxt_en-next' (2023-10-22 11:41:46 +0100)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git nf-next-23-10-25
for you to fetch changes up to 9cdee063476988102bbc5e0e9551e10c5ed00d3e:
netfilter: nf_tables: Carry reset boolean in nft_set_dump_ctx (2023-10-24 15:48:30 +0200)
----------------------------------------------------------------
netfilter pull request 23-10-25
----------------------------------------------------------------
Florian Westphal (4):
netfilter: nft_set_rbtree: rename gc deactivate+erase function
netfilter: nft_set_rbtree: prefer sync gc to async worker
br_netfilter: use single forward hook for ip and arp
netfilter: conntrack: switch connlabels to atomic_t
Pablo Neira Ayuso (5):
netfilter: nft_set_pipapo: no need to call pipapo_deactivate() from flush
netfilter: nf_tables: set backend .flush always succeeds
netfilter: nf_tables: expose opaque set element as struct nft_elem_priv
netfilter: nf_tables: shrink memory consumption of set elements
netfilter: nf_tables: set->ops->insert returns opaque set element in case of EEXIST
Phil Sutter (10):
netfilter: nf_tables: Open-code audit log call in nf_tables_getrule()
netfilter: nf_tables: Introduce nf_tables_getrule_single()
netfilter: nf_tables: Add locking for NFT_MSG_GETRULE_RESET requests
netfilter: nf_tables: Drop pointless memset in nf_tables_dump_obj
netfilter: nf_tables: Unconditionally allocate nft_obj_filter
netfilter: nf_tables: A better name for nft_obj_filter
netfilter: nf_tables: Carry s_idx in nft_obj_dump_ctx
netfilter: nf_tables: nft_obj_filter fits into cb->ctx
netfilter: nf_tables: Carry reset boolean in nft_obj_dump_ctx
netfilter: nf_tables: Carry reset boolean in nft_set_dump_ctx
include/net/netfilter/nf_conntrack_labels.h | 2 +- include/net/netfilter/nf_tables.h | 60 ++-- include/net/netns/conntrack.h | 2 +- net/bridge/br_netfilter_hooks.c | 72 +++-- net/netfilter/nf_conntrack_labels.c | 17 +- net/netfilter/nf_tables_api.c | 445 +++++++++++++++------------- net/netfilter/nft_dynset.c | 23 +- net/netfilter/nft_set_bitmap.c | 53 ++-- net/netfilter/nft_set_hash.c | 109 +++---- net/netfilter/nft_set_pipapo.c | 73 ++--- net/netfilter/nft_set_pipapo.h | 4 +- net/netfilter/nft_set_rbtree.c | 200 ++++++------- 12 files changed, 558 insertions(+), 502 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/19 netfilter: nft_set_rbtree: rename gc deactivate+erase function
From: Florian Westphal <[email protected]>
Next patch adds a cllaer that doesn't hold the priv->write lock and will need a similar function.
Rename the existing function to make it clear that it can only be used for opportunistic gc during insertion.
--- net/netfilter/nft_set_rbtree.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
net/netfilter/nft_set_rbtree.c +6 -5
@@ -221,14 +221,15 @@ static void *nft_rbtree_get(const struct net *net, const struct nft_set *set,return rbe; } -static void nft_rbtree_gc_remove (struct net *net,struct nft_set *set, -struct nft_rbtree *priv, -struct nft_rbtree_elem *rbe) +static void nft_rbtree_gc_elem_remove (struct net *net,struct nft_set *set, +struct nft_rbtree *priv, +struct nft_rbtree_elem *rbe) {struct nft_set_elem elem = { .priv = rbe, }; +lockdep_assert_held_write (&priv->lock);nft_setelem_data_deactivate (net, set, &elem);rb_erase (&rbe->node, &priv->root); }
@@ -263,7 +264,7 @@ nft_rbtree_gc_elem(const struct nft_set *__set, struct nft_rbtree *priv, rbe_prev =NULL ;if (prev) { rbe_prev = rb_entry(prev, struct nft_rbtree_elem, node); -nft_rbtree_gc_remove (net, set, priv, rbe_prev); +nft_rbtree_gc_elem_remove (net, set, priv, rbe_prev);/* There is always room in this trans gc for this element, * memory allocation never actually happens, hence, the warning
@@ -277,7 +278,7 @@ nft_rbtree_gc_elem(const struct nft_set *__set, struct nft_rbtree *priv,nft_trans_gc_elem_add (gc, rbe_prev); } -nft_rbtree_gc_remove (net, set, priv, rbe); +nft_rbtree_gc_elem_remove (net, set, priv, rbe); gc =nft_trans_gc_queue_sync (gc,GFP_ATOMIC );if (WARN_ON_ONCE (!gc))return ERR_PTR (-ENOMEM );
Signature · 1 line
2.30.2
Re: [PATCH net-next 01/19] netfilter: nft_set_rbtree: rename gc deactivate+erase function
This 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 9 lines · On Wed, 25 Oct 2023 23:25:37 +0200 you wrote:
From: Florian Westphal <[email protected]>
Next patch adds a cllaer that doesn't hold the
priv->writelock and will need a similar function.Rename the existing function to make it clear that it can only be used for opportunistic gc during insertion.
[...]
Here is the summary with links:
- [net-next,01/19] netfilter: nft_set_rbtree: rename gc deactivate+erase function
https://git.kernel.org/netdev/net-next/c/8079fc30f797 - [net-next,02/19] netfilter: nft_set_rbtree: prefer sync gc to async worker https://git.kernel.org/netdev/net-next/c/7d259f021aaa - [net-next,03/19] netfilter: nf_tables: Open-code audit log call in nf_tables_getrule() https://git.kernel.org/netdev/net-next/c/8877393029e7 - [net-next,04/19] netfilter: nf_tables: Introduce nf_tables_getrule_single() https://git.kernel.org/netdev/net-next/c/1578c3287719 - [net-next,05/19] netfilter: nf_tables: Add locking for NFT_MSG_GETRULE_RESET requests https://git.kernel.org/netdev/net-next/c/3cb03edb4de3 - [net-next,06/19] br_netfilter: use single forward hook for ip and arp https://git.kernel.org/netdev/net-next/c/ee6f05dcd672 - [net-next,07/19] netfilter: conntrack: switch connlabels to atomic_t https://git.kernel.org/netdev/net-next/c/643d12603664 - [net-next,08/19] netfilter: nf_tables: Drop pointless memset in nf_tables_dump_obj https://git.kernel.org/netdev/net-next/c/ff16111cc10c - [net-next,09/19] netfilter: nf_tables: Unconditionally allocate nft_obj_filter https://git.kernel.org/netdev/net-next/c/4279cc60b354 - [net-next,10/19] netfilter: nf_tables: A better name for nft_obj_filter https://git.kernel.org/netdev/net-next/c/ecf49cad8070 - [net-next,11/19] netfilter: nf_tables: Carry s_idx in nft_obj_dump_ctx https://git.kernel.org/netdev/net-next/c/2eda95cfa2fc - [net-next,12/19] netfilter: nf_tables: nft_obj_filter fits into cb->ctx https://git.kernel.org/netdev/net-next/c/5a893b9cdf6f - [net-next,13/19] netfilter: nf_tables: Carry reset boolean in nft_obj_dump_ctx https://git.kernel.org/netdev/net-next/c/a552339063d3 - [net-next,14/19] netfilter: nft_set_pipapo: no need to call pipapo_deactivate() from flush https://git.kernel.org/netdev/net-next/c/26cec9d4144e - [net-next,15/19] netfilter: nf_tables: set backend .flush always succeeds https://git.kernel.org/netdev/net-next/c/6509a2e410c3 - [net-next,16/19] netfilter: nf_tables: expose opaque set element as struct nft_elem_priv https://git.kernel.org/netdev/net-next/c/9dad402b89e8 - [net-next,17/19] netfilter: nf_tables: shrink memory consumption of set elements https://git.kernel.org/netdev/net-next/c/0e1ea651c971 - [net-next,18/19] netfilter: nf_tables: set->ops->insert returns opaque set element in case of EEXIST https://git.kernel.org/netdev/net-next/c/078996fcd657 - [net-next,19/19] netfilter: nf_tables: Carry reset boolean in nft_set_dump_ctx https://git.kernel.org/netdev/net-next/c/9cdee0634769
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Pablo Neira Ayuso
maintainerPATCH 2/19 netfilter: nft_set_rbtree: prefer sync gc to async worker
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
There is no need for asynchronous garbage collection, rbtree inserts can only happen from the netlink control plane.
We already perform on-demand gc on insertion, in the area of the tree where the insertion takes place, but we don't do a full tree walk there for performance reasons.
Do a full gc walk at the end of the transaction instead and remove the async worker.
--- net/netfilter/nft_set_rbtree.c | 124 +++++++++++++++++---------------- 1 file changed, 65 insertions(+), 59 deletions(-)
net/netfilter/nft_set_rbtree.c +65 -59
@@ -19,7 +19,7 @@ struct nft_rbtree {struct rb_root root;rwlock_t lock;seqcount_rwlock_t count; -struct delayed_work gc_work; +unsigned long last_gc; };struct nft_rbtree_elem {
@@ -48,8 +48,7 @@ static int nft_rbtree_cmp(const struct nft_set *set,static bool nft_rbtree_elem_expired (const struct nft_rbtree_elem *rbe) { -return nft_set_elem_expired (&rbe->ext) || -nft_set_elem_is_dead (&rbe->ext); +return nft_set_elem_expired (&rbe->ext); } static bool __nft_rbtree_lookup(const struct net *net, const struct nft_set *set,
@@ -508,6 +507,15 @@ static int nft_rbtree_insert(const struct net *net, const struct nft_set *set,return err; } +static void nft_rbtree_erase (struct nft_rbtree *priv,struct nft_rbtree_elem *rbe) +{ +write_lock_bh (&priv->lock); +write_seqcount_begin (&priv->count); +rb_erase (&rbe->node, &priv->root); +write_seqcount_end (&priv->count); +write_unlock_bh (&priv->lock); +} +static void nft_rbtree_remove (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem)
@@ -515,11 +523,7 @@ static void nft_rbtree_remove(const struct net *net,struct nft_rbtree *priv =nft_set_priv (set);struct nft_rbtree_elem *rbe = elem->priv; -write_lock_bh (&priv->lock); -write_seqcount_begin (&priv->count); -rb_erase (&rbe->node, &priv->root); -write_seqcount_end (&priv->count); -write_unlock_bh (&priv->lock); +nft_rbtree_erase (priv, rbe); } static void nft_rbtree_activate(const struct net *net,
@@ -613,45 +617,40 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,read_unlock_bh (&priv->lock); } -static void nft_rbtree_gc (struct work_struct *work) +static void nft_rbtree_gc_remove (struct net *net,struct nft_set *set, +struct nft_rbtree *priv, +struct nft_rbtree_elem *rbe) { +struct nft_set_elem elem = { + .priv = rbe, + }; + +nft_setelem_data_deactivate (net, set, &elem); +nft_rbtree_erase (priv, rbe); +} + +static void nft_rbtree_gc (struct nft_set *set) +{ +struct nft_rbtree *priv =nft_set_priv (set);struct nft_rbtree_elem *rbe, *rbe_end =NULL ;struct nftables_pernet *nft_net; -struct nft_rbtree *priv; +struct rb_node *node, *next;struct nft_trans_gc *gc; -struct rb_node *node; -struct nft_set *set; -unsigned int gc_seq;struct net *net; - priv = container_of(work, struct nft_rbtree, gc_work.work); set =nft_set_container_of (priv); net =read_pnet (&set->net); nft_net =nft_pernet (net); - gc_seq =READ_ONCE (nft_net->gc_seq); -if (nft_set_gc_is_pending (set)) - goto done; - - gc =nft_trans_gc_alloc (set, gc_seq,GFP_KERNEL ); + gc =nft_trans_gc_alloc (set,0 ,GFP_KERNEL );if (!gc) - goto done; - -read_lock_bh (&priv->lock); -for (node =rb_first (&priv->root); node !=NULL ; node =rb_next (node)) { +return ; -/* Ruleset has been updated, try later. */ -if (READ_ONCE (nft_net->gc_seq) != gc_seq) { -nft_trans_gc_destroy (gc); - gc =NULL ; - goto try_later; - } +for (node =rb_first (&priv->root); node ; node = next) { + next =rb_next (node); rbe = rb_entry(node, struct nft_rbtree_elem, node); -if (nft_set_elem_is_dead (&rbe->ext)) - goto dead_elem; -/* elements are reversed in the rbtree for historical reasons, * from highest to lowest value, that is why end element is * always visited before the start element.
@@ -663,37 +662,34 @@ static void nft_rbtree_gc(struct work_struct *work)if (!nft_set_elem_expired (&rbe->ext))continue ; -nft_set_elem_dead (&rbe->ext); - -if (!rbe_end) -continue ; - -nft_set_elem_dead (&rbe_end->ext); - - gc =nft_trans_gc_queue_async (gc, gc_seq,GFP_ATOMIC ); + gc =nft_trans_gc_queue_sync (gc,GFP_KERNEL );if (!gc) goto try_later; -nft_trans_gc_elem_add (gc, rbe_end); - rbe_end =NULL ; -dead_elem: - gc =nft_trans_gc_queue_async (gc, gc_seq,GFP_ATOMIC ); +/* end element needs to be removed first, it has +* no timeout extension. +*/ +if (rbe_end) { +nft_rbtree_gc_remove (net, set, priv, rbe_end); +nft_trans_gc_elem_add (gc, rbe_end); + rbe_end =NULL ; + } + + gc =nft_trans_gc_queue_sync (gc,GFP_KERNEL );if (!gc) goto try_later; +nft_rbtree_gc_remove (net, set, priv, rbe);nft_trans_gc_elem_add (gc, rbe); } - gc =nft_trans_gc_catchall_async (gc, gc_seq); - try_later: -read_unlock_bh (&priv->lock); -if (gc) -nft_trans_gc_queue_async_done (gc); -done: -queue_delayed_work (system_power_efficient_wq, &priv->gc_work, -nft_set_gc_interval (set)); +if (gc) { + gc =nft_trans_gc_catchall_sync (gc); +nft_trans_gc_queue_sync_done (gc); + priv->last_gc = jiffies; + } } static u64 nft_rbtree_privsize(const struct nlattr * const nla[],
@@ -712,11 +708,6 @@ static int nft_rbtree_init(const struct nft_set *set,seqcount_rwlock_init (&priv->count, &priv->lock); priv->root =RB_ROOT ; -INIT_DEFERRABLE_WORK (&priv->gc_work, nft_rbtree_gc); -if (set->flags &NFT_SET_TIMEOUT ) -queue_delayed_work (system_power_efficient_wq, &priv->gc_work, -nft_set_gc_interval (set)); -return 0 ; }
@@ -727,8 +718,6 @@ static void nft_rbtree_destroy(const struct nft_ctx *ctx,struct nft_rbtree_elem *rbe;struct rb_node *node; -cancel_delayed_work_sync (&priv->gc_work); -rcu_barrier ();while ((node = priv->root.rb_node) !=NULL ) {rb_erase (node, &priv->root); rbe = rb_entry(node, struct nft_rbtree_elem, node);
@@ -754,6 +743,21 @@ static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features,return true; } +static void nft_rbtree_commit (struct nft_set *set) +{ +struct nft_rbtree *priv =nft_set_priv (set); + +if (time_after_eq (jiffies, priv->last_gc +nft_set_gc_interval (set))) +nft_rbtree_gc (set); +} + +static void nft_rbtree_gc_init (const struct nft_set *set) +{ +struct nft_rbtree *priv =nft_set_priv (set); + + priv->last_gc = jiffies; +} +const struct nft_set_type nft_set_rbtree_type = { .features =NFT_SET_INTERVAL |NFT_SET_MAP |NFT_SET_OBJECT |NFT_SET_TIMEOUT , .ops = {
@@ -767,6 +771,8 @@ const struct nft_set_type nft_set_rbtree_type = { .deactivate = nft_rbtree_deactivate, .flush = nft_rbtree_flush, .activate = nft_rbtree_activate, + .commit = nft_rbtree_commit, + .gc_init = nft_rbtree_gc_init, .lookup = nft_rbtree_lookup, .walk = nft_rbtree_walk, .get = nft_rbtree_get,
Signature · 1 line
2.30.2
Simon Horman
reviewerRe: [PATCH net-next 02/19] netfilter: nft_set_rbtree: prefer sync gc to async worker
Quoted 14 lines · Pablo Neira Ayuso
From: Florian Westphal <[email protected]>
There is no need for asynchronous garbage collection, rbtree inserts can only happen from the netlink control plane.
We already perform on-demand gc on insertion, in the area of the tree where the insertion takes place, but we don't do a full tree walk there for performance reasons.
Do a full gc walk at the end of the transaction instead and remove the async worker.
Signed-off-by Florian Westphal <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>
...
Quoted 25 lines
net/netfilter/nft_set_rbtree.c +7
-6@@ -515,11 +523,7 @@ static void nft_rbtree_remove(const struct net *net,struct nft_rbtree *priv =nft_set_priv (set);struct nft_rbtree_elem *rbe = elem->priv; -write_lock_bh (&priv->lock); -write_seqcount_begin (&priv->count); -rb_erase (&rbe->node, &priv->root); -write_seqcount_end (&priv->count); -write_unlock_bh (&priv->lock); +nft_rbtree_erase (priv, rbe); } static void nft_rbtree_activate(const struct net *net,@@ -613,45 +617,40 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,read_unlock_bh (&priv->lock); } -static void nft_rbtree_gc (struct work_struct *work) +static void nft_rbtree_gc_remove (struct net *net,struct nft_set *set, +struct nft_rbtree *priv, +struct nft_rbtree_elem *rbe) { +struct nft_set_elem elem = { + .priv = rbe, + };
net/netfilter/nft_set_rbtree.c static void nft_rbtree_walk(const struct nft_ctx *ctx, · line 636
Quoted 10 lines
net/netfilter/nft_set_rbtree.c +8
-0+ +nft_setelem_data_deactivate (net, set, &elem); +nft_rbtree_erase (priv, rbe); +} + +static void nft_rbtree_gc (struct nft_set *set) +{ +struct nft_rbtree *priv =nft_set_priv (set);struct nft_rbtree_elem *rbe, *rbe_end =NULL ;struct nftables_pernet *nft_net;
Hi Florian and Pablo,
I understand that this patch has been accepted upstream, and that by implication this feedback is rather slow, but I noticed that with this patch nft_net is now set but otherwise unused in this function.
As flagged by clang-16 and gcc-13 W=1 builds.
Quoted 43 lines
net/netfilter/nft_set_rbtree.c +5
-23-struct nft_rbtree *priv; +struct rb_node *node, *next;struct nft_trans_gc *gc; -struct rb_node *node; -struct nft_set *set; -unsigned int gc_seq;struct net *net; - priv = container_of(work, struct nft_rbtree, gc_work.work); set =nft_set_container_of (priv); net =read_pnet (&set->net); nft_net =nft_pernet (net); - gc_seq =READ_ONCE (nft_net->gc_seq); -if (nft_set_gc_is_pending (set)) - goto done; - - gc =nft_trans_gc_alloc (set, gc_seq,GFP_KERNEL ); + gc =nft_trans_gc_alloc (set,0 ,GFP_KERNEL );if (!gc) - goto done; - -read_lock_bh (&priv->lock); -for (node =rb_first (&priv->root); node !=NULL ; node =rb_next (node)) { +return ; -/* Ruleset has been updated, try later. */ -if (READ_ONCE (nft_net->gc_seq) != gc_seq) { -nft_trans_gc_destroy (gc); - gc =NULL ; - goto try_later; - } +for (node =rb_first (&priv->root); node ; node = next) { + next =rb_next (node); rbe = rb_entry(node, struct nft_rbtree_elem, node); -if (nft_set_elem_is_dead (&rbe->ext)) - goto dead_elem; -/* elements are reversed in the rbtree for historical reasons, * from highest to lowest value, that is why end element is * always visited before the start element.
...
Florian Westphal
maintainerRe: [PATCH net-next 02/19] netfilter: nft_set_rbtree: prefer sync gc to async worker
Quoted 6 lines · Simon Horman
Hi Florian and Pablo,
I understand that this patch has been accepted upstream, and that by implication this feedback is rather slow, but I noticed that with this patch
nft_netis now set but otherwise unused in this function.
There is a patch queued for this, we'll pass this to -net next week.
Pablo Neira Ayuso
maintainerPATCH 3/19 netfilter: nf_tables: Open-code audit log call in nf_tables_getrule()
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
The table lookup will be dropped from that function, so remove that dependency from audit logging code. Using whatever is in nla[NFTA_RULE_TABLE] is sufficient as long as the previous rule info filling succeded.
--- net/netfilter/nf_tables_api.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +15 -4
@@ -3589,15 +3589,18 @@ static int nf_tables_dump_rules_done(struct netlink_callback *cb)static int nf_tables_getrule (struct sk_buff *skb,const struct nfnl_info *info,const struct nlattr *const nla[]) { +struct nftables_pernet *nft_net =nft_pernet (info->net);struct netlink_ext_ack *extack = info->extack;u8 genmask =nft_genmask_cur (info->net);u8 family = info->nfmsg->nfgen_family; +u32 portid =NETLINK_CB (skb).portid;const struct nft_chain *chain;const struct nft_rule *rule;struct net *net = info->net;struct nft_table *table;struct sk_buff *skb2;bool reset = false; +char *buf;int err;if (info->nlh->nlmsg_flags &NLM_F_DUMP ) {
@@ -3637,16 +3640,24 @@ static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info,if (NFNL_MSG_TYPE (info->nlh->nlmsg_type) ==NFT_MSG_GETRULE_RESET ) reset = true; - err =nf_tables_fill_rule_info (skb2, net,NETLINK_CB (skb).portid, + err =nf_tables_fill_rule_info (skb2, net, portid, info->nlh->nlmsg_seq,NFT_MSG_NEWRULE ,0 , family, table, chain, rule,0 , reset);if (err <0 ) goto err_fill_rule_info; -if (reset) -audit_log_rule_reset (table,nft_pernet (net)->base_seq,1 ); +if (!reset) +return nfnetlink_unicast (skb2, net, portid); -return nfnetlink_unicast (skb2, net,NETLINK_CB (skb).portid); + buf =kasprintf (GFP_ATOMIC ,"%.*s:%u" , +nla_len (nla[NFTA_RULE_TABLE ]), + (char *)nla_data (nla[NFTA_RULE_TABLE ]), + nft_net->base_seq); +audit_log_nfcfg (buf, info->nfmsg->nfgen_family,1 , +AUDIT_NFT_OP_RULE_RESET ,GFP_ATOMIC ); +kfree (buf); + +return nfnetlink_unicast (skb2, net, portid); err_fill_rule_info:kfree_skb (skb2);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/19 netfilter: nf_tables: Introduce nf_tables_getrule_single()
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
Outsource the reply skb preparation for non-dump getrule requests into a distinct function. Prep work for rule reset locking.
--- net/netfilter/nf_tables_api.c | 74 ++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 31 deletions(-)
net/netfilter/nf_tables_api.c +43 -31
@@ -3586,65 +3586,81 @@ static int nf_tables_dump_rules_done(struct netlink_callback *cb) }/* called with rcu_read_lock held */ -static int nf_tables_getrule (struct sk_buff *skb,const struct nfnl_info *info, -const struct nlattr *const nla[]) +static struct sk_buff * +nf_tables_getrule_single (u32 portid,const struct nfnl_info *info, +const struct nlattr *const nla[],bool reset) { -struct nftables_pernet *nft_net =nft_pernet (info->net);struct netlink_ext_ack *extack = info->extack;u8 genmask =nft_genmask_cur (info->net);u8 family = info->nfmsg->nfgen_family; -u32 portid =NETLINK_CB (skb).portid;const struct nft_chain *chain;const struct nft_rule *rule;struct net *net = info->net;struct nft_table *table;struct sk_buff *skb2; -bool reset = false; -char *buf;int err; -if (info->nlh->nlmsg_flags &NLM_F_DUMP ) { -struct netlink_dump_control c = { - .start= nf_tables_dump_rules_start, - .dump = nf_tables_dump_rules, - .done = nf_tables_dump_rules_done, - .module =THIS_MODULE , - .data = (void *)nla, - }; - -return nft_netlink_dump_start_rcu (info->sk, skb, info->nlh, &c); - } - table =nft_table_lookup (net, nla[NFTA_RULE_TABLE ], family, genmask,0 );if (IS_ERR (table)) {NL_SET_BAD_ATTR (extack, nla[NFTA_RULE_TABLE ]); -return PTR_ERR (table); +return ERR_CAST (table); } chain =nft_chain_lookup (net, table, nla[NFTA_RULE_CHAIN ], genmask);if (IS_ERR (chain)) {NL_SET_BAD_ATTR (extack, nla[NFTA_RULE_CHAIN ]); -return PTR_ERR (chain); +return ERR_CAST (chain); } rule =nft_rule_lookup (chain, nla[NFTA_RULE_HANDLE ]);if (IS_ERR (rule)) {NL_SET_BAD_ATTR (extack, nla[NFTA_RULE_HANDLE ]); -return PTR_ERR (rule); +return ERR_CAST (rule); } skb2 =alloc_skb (NLMSG_GOODSIZE ,GFP_ATOMIC );if (!skb2) -return -ENOMEM ; - -if (NFNL_MSG_TYPE (info->nlh->nlmsg_type) ==NFT_MSG_GETRULE_RESET ) - reset = true; +return ERR_PTR (-ENOMEM ); err =nf_tables_fill_rule_info (skb2, net, portid, info->nlh->nlmsg_seq,NFT_MSG_NEWRULE ,0 , family, table, chain, rule,0 , reset); -if (err <0 ) - goto err_fill_rule_info; +if (err <0 ) { +kfree_skb (skb2); +return ERR_PTR (err); + } + +return skb2; +} + +static int nf_tables_getrule (struct sk_buff *skb,const struct nfnl_info *info, +const struct nlattr *const nla[]) +{ +struct nftables_pernet *nft_net =nft_pernet (info->net); +u32 portid =NETLINK_CB (skb).portid; +struct net *net = info->net; +struct sk_buff *skb2; +bool reset = false; +char *buf; + +if (info->nlh->nlmsg_flags &NLM_F_DUMP ) { +struct netlink_dump_control c = { + .start= nf_tables_dump_rules_start, + .dump = nf_tables_dump_rules, + .done = nf_tables_dump_rules_done, + .module =THIS_MODULE , + .data = (void *)nla, + }; + +return nft_netlink_dump_start_rcu (info->sk, skb, info->nlh, &c); + } + +if (NFNL_MSG_TYPE (info->nlh->nlmsg_type) ==NFT_MSG_GETRULE_RESET ) + reset = true; + + skb2 =nf_tables_getrule_single (portid, info, nla, reset); +if (IS_ERR (skb2)) +return PTR_ERR (skb2);if (!reset)return nfnetlink_unicast (skb2, net, portid);
@@ -3658,10 +3674,6 @@ static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info,kfree (buf);return nfnetlink_unicast (skb2, net, portid); - -err_fill_rule_info: -kfree_skb (skb2); -return err; }void nf_tables_rule_destroy (const struct nft_ctx *ctx,struct nft_rule *rule)
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/19 netfilter: nf_tables: Add locking for NFT_MSG_GETRULE_RESET requests
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
Rule reset is not concurrency-safe per-se, so multiple CPUs may reset the same rule at the same time. At least counter and quota expressions will suffer from value underruns in this case.
Prevent this by introducing dedicated locking callbacks for nfnetlink and the asynchronous dump handling to serialize access.
--- net/netfilter/nf_tables_api.c | 77 +++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 13 deletions(-)
net/netfilter/nf_tables_api.c +64 -13
@@ -3551,6 +3551,23 @@ static int nf_tables_dump_rules(struct sk_buff *skb,return skb->len; } +static int nf_tables_dumpreset_rules (struct sk_buff *skb, +struct netlink_callback *cb) +{ +struct nftables_pernet *nft_net =nft_pernet (sock_net (skb->sk)); +int ret; + +/* Mutex is held is to prevent that two concurrent dump-and-reset calls +* do not underrun counters and quotas. The commit_mutex is used for +* the lack a better lock, this is not transaction path. +*/ +mutex_lock (&nft_net->commit_mutex); + ret =nf_tables_dump_rules (skb, cb); +mutex_unlock (&nft_net->commit_mutex); + +return ret; +} +static int nf_tables_dump_rules_start (struct netlink_callback *cb) {struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;
@@ -3570,12 +3587,18 @@ static int nf_tables_dump_rules_start(struct netlink_callback *cb)return -ENOMEM ; } } -if (NFNL_MSG_TYPE (cb->nlh->nlmsg_type) ==NFT_MSG_GETRULE_RESET ) - ctx->reset = true; -return 0 ; } +static int nf_tables_dumpreset_rules_start (struct netlink_callback *cb) +{ +struct nft_rule_dump_ctx *ctx = (void *)cb->ctx; + + ctx->reset = true; + +return nf_tables_dump_rules_start (cb); +} +static int nf_tables_dump_rules_done (struct netlink_callback *cb) {struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;
@@ -3636,12 +3659,9 @@ nf_tables_getrule_single(u32 portid, const struct nfnl_info *info,static int nf_tables_getrule (struct sk_buff *skb,const struct nfnl_info *info,const struct nlattr *const nla[]) { -struct nftables_pernet *nft_net =nft_pernet (info->net);u32 portid =NETLINK_CB (skb).portid;struct net *net = info->net;struct sk_buff *skb2; -bool reset = false; -char *buf;if (info->nlh->nlmsg_flags &NLM_F_DUMP ) { struct netlink_dump_control c = {
@@ -3655,15 +3675,46 @@ static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info,return nft_netlink_dump_start_rcu (info->sk, skb, info->nlh, &c); } -if (NFNL_MSG_TYPE (info->nlh->nlmsg_type) ==NFT_MSG_GETRULE_RESET ) - reset = true; - - skb2 =nf_tables_getrule_single (portid, info, nla, reset); + skb2 =nf_tables_getrule_single (portid, info, nla, false);if (IS_ERR (skb2))return PTR_ERR (skb2); -if (!reset) -return nfnetlink_unicast (skb2, net, portid); +return nfnetlink_unicast (skb2, net, portid); +} + +static int nf_tables_getrule_reset (struct sk_buff *skb, +const struct nfnl_info *info, +const struct nlattr *const nla[]) +{ +struct nftables_pernet *nft_net =nft_pernet (info->net); +u32 portid =NETLINK_CB (skb).portid; +struct net *net = info->net; +struct sk_buff *skb2; +char *buf; + +if (info->nlh->nlmsg_flags &NLM_F_DUMP ) { +struct netlink_dump_control c = { + .start= nf_tables_dumpreset_rules_start, + .dump = nf_tables_dumpreset_rules, + .done = nf_tables_dump_rules_done, + .module =THIS_MODULE , + .data = (void *)nla, + }; + +return nft_netlink_dump_start_rcu (info->sk, skb, info->nlh, &c); + } + +if (!try_module_get (THIS_MODULE )) +return -EINVAL ; +rcu_read_unlock (); +mutex_lock (&nft_net->commit_mutex); + skb2 =nf_tables_getrule_single (portid, info, nla, true); +mutex_unlock (&nft_net->commit_mutex); +rcu_read_lock (); +module_put (THIS_MODULE ); + +if (IS_ERR (skb2)) +return PTR_ERR (skb2); buf = kasprintf(GFP_ATOMIC, "%.*s:%u", nla_len(nla[NFTA_RULE_TABLE]),
@@ -8995,7 +9046,7 @@ static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { .policy = nft_rule_policy, }, [NFT_MSG_GETRULE_RESET] = { - .call = nf_tables_getrule, + .call = nf_tables_getrule_reset, .type = NFNL_CB_RCU, .attr_count = NFTA_RULE_MAX, .policy = nft_rule_policy,
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 6/19 br_netfilter: use single forward hook for ip and arp
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
br_netfilter registers two forward hooks, one for ip and one for arp.
Just use a common function for both and then call the arp/ip helper as needed.
--- net/bridge/br_netfilter_hooks.c | 72 ++++++++++++++++----------------- 1 file changed, 34 insertions(+), 38 deletions(-)
net/bridge/br_netfilter_hooks.c +34 -38
@@ -570,18 +570,12 @@ static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff } -/* This is the 'purely bridged' case. For IP, we pass the packet to -* netfilter with indev and outdev set to the bridge device, -* but we are still able to filter on the 'real' indev/outdev -* because of the physdev module. For ARP, indev and outdev are the -* bridge ports. */ -static unsigned int br_nf_forward_ip (void *priv, -struct sk_buff *skb, -const struct nf_hook_state *state) +static unsigned int br_nf_forward_ip (struct sk_buff *skb, +const struct nf_hook_state *state, +u8 pf) {struct nf_bridge_info *nf_bridge;struct net_device *parent; -u_int8_t pf; nf_bridge =nf_bridge_info_get (skb);if (!nf_bridge)
@@ -600,15 +594,6 @@ static unsigned int br_nf_forward_ip(void *priv,if (!parent)return NF_DROP_REASON (skb,SKB_DROP_REASON_DEV_READY ,0 ); -if (IS_IP (skb) ||is_vlan_ip (skb, state->net) || -is_pppoe_ip (skb, state->net)) - pf =NFPROTO_IPV4 ; -else if (IS_IPV6 (skb) ||is_vlan_ipv6 (skb, state->net) || -is_pppoe_ipv6 (skb, state->net)) - pf =NFPROTO_IPV6 ; -else -return NF_ACCEPT ; -nf_bridge_pull_encap_header (skb);if (skb->pkt_type ==PACKET_OTHERHOST ) {
@@ -620,19 +605,18 @@ static unsigned int br_nf_forward_ip(void *priv,if (br_validate_ipv4 (state->net, skb))return NF_DROP_REASON (skb,SKB_DROP_REASON_IP_INHDR ,0 );IPCB (skb)->frag_max_size = nf_bridge->frag_max_size; - } - -if (pf ==NFPROTO_IPV6 ) { + skb->protocol =htons (ETH_P_IP ); + }else if (pf ==NFPROTO_IPV6 ) {if (br_validate_ipv6 (state->net, skb))return NF_DROP_REASON (skb,SKB_DROP_REASON_IP_INHDR ,0 );IP6CB (skb)->frag_max_size = nf_bridge->frag_max_size; + skb->protocol =htons (ETH_P_IPV6 ); + }else { +WARN_ON_ONCE (1 ); +return NF_DROP ; } nf_bridge->physoutdev = skb->dev; -if (pf ==NFPROTO_IPV4 ) - skb->protocol =htons (ETH_P_IP ); -else - skb->protocol =htons (ETH_P_IPV6 ); NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb, brnf_get_logical_dev(skb, state->in, state->net),
@@ -641,8 +625,7 @@ static unsigned int br_nf_forward_ip(void *priv,return NF_STOLEN ; } -static unsigned int br_nf_forward_arp (void *priv, -struct sk_buff *skb, +static unsigned int br_nf_forward_arp (struct sk_buff *skb,const struct nf_hook_state *state) {struct net_bridge_port *p;
@@ -659,11 +642,8 @@ static unsigned int br_nf_forward_arp(void *priv,if (!brnet->call_arptables && !br_opt_get (br,BROPT_NF_CALL_ARPTABLES ))return NF_ACCEPT ; -if (!IS_ARP (skb)) { -if (!is_vlan_arp (skb, state->net)) -return NF_ACCEPT ; +if (is_vlan_arp (skb, state->net))nf_bridge_pull_encap_header (skb); - }if (unlikely (!pskb_may_pull (skb,sizeof (struct arphdr ))))return NF_DROP_REASON (skb,SKB_DROP_REASON_PKT_TOO_SMALL ,0 );
@@ -680,6 +660,28 @@ static unsigned int br_nf_forward_arp(void *priv,return NF_STOLEN ; } +/* This is the 'purely bridged' case. For IP, we pass the packet to +* netfilter with indev and outdev set to the bridge device, +* but we are still able to filter on the 'real' indev/outdev +* because of the physdev module. For ARP, indev and outdev are the +* bridge ports. +*/ +static unsigned int br_nf_forward (void *priv, +struct sk_buff *skb, +const struct nf_hook_state *state) +{ +if (IS_IP (skb) ||is_vlan_ip (skb, state->net) || +is_pppoe_ip (skb, state->net)) +return br_nf_forward_ip (skb, state,NFPROTO_IPV4 ); +if (IS_IPV6 (skb) ||is_vlan_ipv6 (skb, state->net) || +is_pppoe_ipv6 (skb, state->net)) +return br_nf_forward_ip (skb, state,NFPROTO_IPV6 ); +if (IS_ARP (skb) ||is_vlan_arp (skb, state->net)) +return br_nf_forward_arp (skb, state); + +return NF_ACCEPT ; +} +static int br_nf_push_frag_xmit (struct net *net,struct sock *sk,struct sk_buff *skb) {struct brnf_frag_data *data;
@@ -937,13 +939,7 @@ static const struct nf_hook_ops br_nf_ops[] = { .priority = NF_BR_PRI_BRNF, }, { - .hook = br_nf_forward_ip, - .pf = NFPROTO_BRIDGE, - .hooknum = NF_BR_FORWARD, - .priority = NF_BR_PRI_BRNF - 1, - }, - { - .hook = br_nf_forward_arp, + .hook = br_nf_forward, .pf = NFPROTO_BRIDGE, .hooknum = NF_BR_FORWARD, .priority = NF_BR_PRI_BRNF,
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 7/19 netfilter: conntrack: switch connlabels to atomic_t
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
The spinlock is back from the day when connabels did not have a fixed size and reallocation had to be supported.
Remove it. This change also allows to call the helpers from softirq or timers without deadlocks.
Also add WARN()s to catch refcounting imbalances.
--- include/net/netfilter/nf_conntrack_labels.h | 2 +- include/net/netns/conntrack.h | 2 +- net/netfilter/nf_conntrack_labels.c | 17 ++++++++--------- 3 files changed, 10 insertions(+), 11 deletions(-)
include/net/netfilter/nf_conntrack_labels.h +1 -1
@@ -39,7 +39,7 @@ static inline struct nf_conn_labels *nf_ct_labels_ext_add(struct nf_conn *ct)#ifdef CONFIG_NF_CONNTRACK_LABELS struct net *net =nf_ct_net (ct); -if (net->ct.labels_used ==0 ) +if (atomic_read (&net->ct.labels_used) ==0 )return NULL ;return nf_ct_ext_add (ct,NF_CT_EXT_LABELS ,GFP_ATOMIC );
include/net/netns/conntrack.h +1 -1
@@ -107,7 +107,7 @@ struct netns_ct {struct nf_ct_event_notifier __rcu *nf_conntrack_event_cb;struct nf_ip_net nf_ct_proto;#if defined(CONFIG_NF_CONNTRACK_LABELS ) -unsigned int labels_used; +atomic_t labels_used; #endif };#endif
net/netfilter/nf_conntrack_labels.c +8 -9
@@ -11,8 +11,6 @@#include <net/netfilter/nf_conntrack_ecache.h> #include <net/netfilter/nf_conntrack_labels.h> -static DEFINE_SPINLOCK (nf_connlabels_lock); -static int replace_u32 (u32 *address,u32 mask,u32 new) {u32 old, tmp;
@@ -60,23 +58,24 @@ EXPORT_SYMBOL_GPL(nf_connlabels_replace);int nf_connlabels_get (struct net *net,unsigned int bits) { +int v; +if (BIT_WORD (bits) >=NF_CT_LABELS_MAX_SIZE /sizeof (long ))return -ERANGE ; -spin_lock (&nf_connlabels_lock); - net->ct.labels_used++; -spin_unlock (&nf_connlabels_lock); -BUILD_BUG_ON (NF_CT_LABELS_MAX_SIZE /sizeof (long ) >=U8_MAX ); + v =atomic_inc_return_relaxed (&net->ct.labels_used); +WARN_ON_ONCE (v <=0 ); +return 0 ; }EXPORT_SYMBOL_GPL (nf_connlabels_get);void nf_connlabels_put (struct net *net) { -spin_lock (&nf_connlabels_lock); - net->ct.labels_used--; -spin_unlock (&nf_connlabels_lock); +int v =atomic_dec_return_relaxed (&net->ct.labels_used); + +WARN_ON_ONCE (v <0 ); }EXPORT_SYMBOL_GPL (nf_connlabels_put);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 8/19 netfilter: nf_tables: Drop pointless memset in nf_tables_dump_obj
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
The code does not make use of cb->args fields past the first one, no need to zero them.
--- net/netfilter/nf_tables_api.c | 3 --- 1 file changed, 3 deletions(-)
net/netfilter/nf_tables_api.c +0 -3
@@ -7717,9 +7717,6 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) goto cont;if (idx < s_idx) goto cont; -if (idx > s_idx) -memset (&cb->args[1 ],0 , -sizeof (cb->args) -sizeof (cb->args[0 ]));if (filter && filter->table &&strcmp (filter->table, table->name)) goto cont;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 9/19 netfilter: nf_tables: Unconditionally allocate nft_obj_filter
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
Prep work for moving the filter into struct netlink_callback's scratch area.
--- net/netfilter/nf_tables_api.c | 36 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 21 deletions(-)
net/netfilter/nf_tables_api.c +15 -21
@@ -7717,11 +7717,9 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) goto cont;if (idx < s_idx) goto cont; -if (filter && filter->table && -strcmp (filter->table, table->name)) +if (filter->table &&strcmp (filter->table, table->name)) goto cont; -if (filter && - filter->type !=NFT_OBJECT_UNSPEC && +if (filter->type !=NFT_OBJECT_UNSPEC && obj->ops->type->type != filter->type) goto cont;
@@ -7756,23 +7754,21 @@ static int nf_tables_dump_obj_start(struct netlink_callback *cb)const struct nlattr *const *nla = cb->data;struct nft_obj_filter *filter =NULL ; -if (nla[NFTA_OBJ_TABLE ] || nla[NFTA_OBJ_TYPE ]) { - filter =kzalloc (sizeof (*filter),GFP_ATOMIC ); -if (!filter) -return -ENOMEM ; + filter =kzalloc (sizeof (*filter),GFP_ATOMIC ); +if (!filter) +return -ENOMEM ; -if (nla[NFTA_OBJ_TABLE ]) { - filter->table =nla_strdup (nla[NFTA_OBJ_TABLE ],GFP_ATOMIC ); -if (!filter->table) { -kfree (filter); -return -ENOMEM ; - } +if (nla[NFTA_OBJ_TABLE ]) { + filter->table =nla_strdup (nla[NFTA_OBJ_TABLE ],GFP_ATOMIC ); +if (!filter->table) { +kfree (filter); +return -ENOMEM ; } - -if (nla[NFTA_OBJ_TYPE ]) - filter->type =ntohl (nla_get_be32 (nla[NFTA_OBJ_TYPE ])); } +if (nla[NFTA_OBJ_TYPE ]) + filter->type =ntohl (nla_get_be32 (nla[NFTA_OBJ_TYPE ])); + cb->data = filter;return 0 ; }
@@ -7781,10 +7777,8 @@ static int nf_tables_dump_obj_done(struct netlink_callback *cb) {struct nft_obj_filter *filter = cb->data; -if (filter) { -kfree (filter->table); -kfree (filter); - } +kfree (filter->table); +kfree (filter);return 0 ; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 10/19 netfilter: nf_tables: A better name for nft_obj_filter
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
Name it for what it is supposed to become, a real nft_obj_dump_ctx. No functional change intended.
--- net/netfilter/nf_tables_api.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)
net/netfilter/nf_tables_api.c +16 -16
@@ -7681,7 +7681,7 @@ static void audit_log_obj_reset(const struct nft_table *table,kfree (buf); } -struct nft_obj_filter { +struct nft_obj_dump_ctx {char *table;u32 type; };
@@ -7691,7 +7691,7 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)const struct nfgenmsg *nfmsg =nlmsg_data (cb->nlh);const struct nft_table *table;unsigned int idx =0 , s_idx = cb->args[0 ]; -struct nft_obj_filter *filter = cb->data; +struct nft_obj_dump_ctx *ctx = cb->data;struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family;struct nftables_pernet *nft_net;
@@ -7717,10 +7717,10 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) goto cont;if (idx < s_idx) goto cont; -if (filter->table &&strcmp (filter->table, table->name)) +if (ctx->table &&strcmp (ctx->table, table->name)) goto cont; -if (filter->type !=NFT_OBJECT_UNSPEC && - obj->ops->type->type != filter->type) +if (ctx->type !=NFT_OBJECT_UNSPEC && + obj->ops->type->type != ctx->type) goto cont; rc = nf_tables_fill_obj_info(skb, net,
@@ -7752,33 +7752,33 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)static int nf_tables_dump_obj_start (struct netlink_callback *cb) {const struct nlattr *const *nla = cb->data; -struct nft_obj_filter *filter =NULL ; +struct nft_obj_dump_ctx *ctx =NULL ; - filter =kzalloc (sizeof (*filter),GFP_ATOMIC ); -if (!filter) + ctx =kzalloc (sizeof (*ctx),GFP_ATOMIC ); +if (!ctx)return -ENOMEM ;if (nla[NFTA_OBJ_TABLE ]) { - filter->table =nla_strdup (nla[NFTA_OBJ_TABLE ],GFP_ATOMIC ); -if (!filter->table) { -kfree (filter); + ctx->table =nla_strdup (nla[NFTA_OBJ_TABLE ],GFP_ATOMIC ); +if (!ctx->table) { +kfree (ctx);return -ENOMEM ; } }if (nla[NFTA_OBJ_TYPE ]) - filter->type =ntohl (nla_get_be32 (nla[NFTA_OBJ_TYPE ])); + ctx->type =ntohl (nla_get_be32 (nla[NFTA_OBJ_TYPE ])); - cb->data = filter; + cb->data = ctx;return 0 ; }static int nf_tables_dump_obj_done (struct netlink_callback *cb) { -struct nft_obj_filter *filter = cb->data; +struct nft_obj_dump_ctx *ctx = cb->data; -kfree (filter->table); -kfree (filter); +kfree (ctx->table); +kfree (ctx);return 0 ; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 11/19 netfilter: nf_tables: Carry s_idx in nft_obj_dump_ctx
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
Prep work for moving the context into struct netlink_callback scratch area.
--- net/netfilter/nf_tables_api.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +5 -4
@@ -7682,6 +7682,7 @@ static void audit_log_obj_reset(const struct nft_table *table, }struct nft_obj_dump_ctx { +unsigned int s_idx;char *table;u32 type; };
@@ -7689,14 +7690,14 @@ struct nft_obj_dump_ctx {static int nf_tables_dump_obj (struct sk_buff *skb,struct netlink_callback *cb) {const struct nfgenmsg *nfmsg =nlmsg_data (cb->nlh); -const struct nft_table *table; -unsigned int idx =0 , s_idx = cb->args[0 ];struct nft_obj_dump_ctx *ctx = cb->data;struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family;struct nftables_pernet *nft_net; +const struct nft_table *table;unsigned int entries =0 ;struct nft_object *obj; +unsigned int idx =0 ;bool reset = false;int rc =0 ;
@@ -7715,7 +7716,7 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)list_for_each_entry_rcu (obj, &table->objects, list) {if (!nft_is_active (net, obj)) goto cont; -if (idx < s_idx) +if (idx < ctx->s_idx) goto cont;if (ctx->table &&strcmp (ctx->table, table->name)) goto cont;
@@ -7745,7 +7746,7 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) }rcu_read_unlock (); - cb->args[0 ] = idx; + ctx->s_idx = idx;return skb->len; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 12/19 netfilter: nf_tables: nft_obj_filter fits into cb->ctx
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
No need to allocate it if one may just use struct netlink_callback's scratch area for it.
--- net/netfilter/nf_tables_api.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)
net/netfilter/nf_tables_api.c +5 -11
@@ -7690,7 +7690,7 @@ struct nft_obj_dump_ctx {static int nf_tables_dump_obj (struct sk_buff *skb,struct netlink_callback *cb) {const struct nfgenmsg *nfmsg =nlmsg_data (cb->nlh); -struct nft_obj_dump_ctx *ctx = cb->data; +struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family;struct nftables_pernet *nft_net;
@@ -7752,34 +7752,28 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)static int nf_tables_dump_obj_start (struct netlink_callback *cb) { +struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;const struct nlattr *const *nla = cb->data; -struct nft_obj_dump_ctx *ctx =NULL ; - ctx =kzalloc (sizeof (*ctx),GFP_ATOMIC ); -if (!ctx) -return -ENOMEM ; +BUILD_BUG_ON (sizeof (*ctx) >sizeof (cb->ctx));if (nla[NFTA_OBJ_TABLE ]) { ctx->table =nla_strdup (nla[NFTA_OBJ_TABLE ],GFP_ATOMIC ); -if (!ctx->table) { -kfree (ctx); +if (!ctx->table)return -ENOMEM ; - } }if (nla[NFTA_OBJ_TYPE ]) ctx->type =ntohl (nla_get_be32 (nla[NFTA_OBJ_TYPE ])); - cb->data = ctx;return 0 ; }static int nf_tables_dump_obj_done (struct netlink_callback *cb) { -struct nft_obj_dump_ctx *ctx = cb->data; +struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;kfree (ctx->table); -kfree (ctx);return 0 ; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 13/19 netfilter: nf_tables: Carry reset boolean in nft_obj_dump_ctx
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
Relieve the dump callback from having to inspect nlmsg_type upon each call, just do it once at start of the dump.
--- net/netfilter/nf_tables_api.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
net/netfilter/nf_tables_api.c +6 -6
@@ -7685,6 +7685,7 @@ struct nft_obj_dump_ctx {unsigned int s_idx;char *table;u32 type; +bool reset; };static int nf_tables_dump_obj (struct sk_buff *skb,struct netlink_callback *cb)
@@ -7698,12 +7699,8 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)unsigned int entries =0 ;struct nft_object *obj;unsigned int idx =0 ; -bool reset = false;int rc =0 ; -if (NFNL_MSG_TYPE (cb->nlh->nlmsg_type) ==NFT_MSG_GETOBJ_RESET ) - reset = true; -rcu_read_lock (); nft_net =nft_pernet (net); cb->seq =READ_ONCE (nft_net->base_seq);
@@ -7730,7 +7727,7 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)NFT_MSG_NEWOBJ ,NLM_F_MULTI |NLM_F_APPEND , table->family, table, - obj, reset); + obj, ctx->reset);if (rc <0 )break ;
@@ -7739,7 +7736,7 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) cont: idx++; } -if (reset && entries) +if (ctx->reset && entries)audit_log_obj_reset (table, nft_net->base_seq, entries);if (rc <0 )break ;
@@ -7766,6 +7763,9 @@ static int nf_tables_dump_obj_start(struct netlink_callback *cb)if (nla[NFTA_OBJ_TYPE ]) ctx->type =ntohl (nla_get_be32 (nla[NFTA_OBJ_TYPE ])); +if (NFNL_MSG_TYPE (cb->nlh->nlmsg_type) ==NFT_MSG_GETOBJ_RESET ) + ctx->reset = true; +return 0 ; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 14/19 netfilter: nft_set_pipapo: no need to call pipapo_deactivate() from flush
In reply to Pablo Neira AyusoUse the element object that is already offered instead.
--- net/netfilter/nft_set_pipapo.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
net/netfilter/nft_set_pipapo.c +3 -2
@@ -1814,8 +1814,9 @@ static bool nft_pipapo_flush(const struct net *net, const struct nft_set *set, {struct nft_pipapo_elem *e = elem; -return pipapo_deactivate (net, set, (const u8 *)nft_set_ext_key (&e->ext), - &e->ext); +nft_set_elem_change_active (net, set, &e->ext); + +return true; }/**
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 15/19 netfilter: nf_tables: set backend .flush always succeeds
In reply to Pablo Neira Ayuso.flush is always successful since this results from iterating over the set elements to toggle mark the element as inactive in the next generation.
--- include/net/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 9 +-------- net/netfilter/nft_set_bitmap.c | 4 +--- net/netfilter/nft_set_hash.c | 7 ++----- net/netfilter/nft_set_pipapo.c | 4 +--- net/netfilter/nft_set_rbtree.c | 4 +--- 6 files changed, 7 insertions(+), 23 deletions(-)
include/net/netfilter/nf_tables.h +1 -1
@@ -449,7 +449,7 @@ struct nft_set_ops {void * (*deactivate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem); -bool (*flush)(const struct net *net, +void (*flush)(const struct net *net,const struct nft_set *set,void *priv); void (*remove)(const struct net *net,
net/netfilter/nf_tables_api.c +1 -8
@@ -7146,17 +7146,13 @@ static int nft_setelem_flush(const struct nft_ctx *ctx, struct nft_set_elem *elem) {struct nft_trans *trans; -int err; trans =nft_trans_alloc_gfp (ctx,NFT_MSG_DELSETELEM ,sizeof (struct nft_trans_elem ),GFP_ATOMIC );if (!trans)return -ENOMEM ; -if (!set->ops->flush (ctx->net, set, elem->priv)) { - err = -ENOENT ; - goto err1; - } + set->ops->flush (ctx->net, set, elem->priv); set->ndeact++;nft_setelem_data_deactivate (ctx->net, set, elem);
@@ -7165,9 +7161,6 @@ static int nft_setelem_flush(const struct nft_ctx *ctx,nft_trans_commit_list_add_tail (ctx->net, trans);return 0 ; -err1: -kfree (trans); -return err; } static int __nft_set_catchall_flush(const struct nft_ctx *ctx,
net/netfilter/nft_set_bitmap.c +1 -3
@@ -174,7 +174,7 @@ static void nft_bitmap_activate(const struct net *net,nft_set_elem_change_active (net, set, &be->ext); } -static bool nft_bitmap_flush (const struct net *net, +static void nft_bitmap_flush (const struct net *net,const struct nft_set *set,void *_be) {struct nft_bitmap *priv =nft_set_priv (set);
@@ -186,8 +186,6 @@ static bool nft_bitmap_flush(const struct net *net,/* Enter 10 state, similar to deactivation. */ priv->bitmap[idx] &= ~(genmask << off);nft_set_elem_change_active (net, set, &be->ext); - -return true; } static void *nft_bitmap_deactivate(const struct net *net,
net/netfilter/nft_set_hash.c +2 -5
@@ -192,14 +192,12 @@ static void nft_rhash_activate(const struct net *net, const struct nft_set *set,nft_set_elem_change_active (net, set, &he->ext); } -static bool nft_rhash_flush (const struct net *net, +static void nft_rhash_flush (const struct net *net,const struct nft_set *set,void *priv) {struct nft_rhash_elem *he = priv;nft_set_elem_change_active (net, set, &he->ext); - -return true; } static void *nft_rhash_deactivate(const struct net *net,
@@ -590,13 +588,12 @@ static void nft_hash_activate(const struct net *net, const struct nft_set *set,nft_set_elem_change_active (net, set, &he->ext); } -static bool nft_hash_flush (const struct net *net, +static void nft_hash_flush (const struct net *net,const struct nft_set *set,void *priv) {struct nft_hash_elem *he = priv;nft_set_elem_change_active (net, set, &he->ext); -return true; } static void *nft_hash_deactivate(const struct net *net,
net/netfilter/nft_set_pipapo.c +1 -3
@@ -1809,14 +1809,12 @@ static void *nft_pipapo_deactivate(const struct net *net,* * Return: true if element was found and deactivated. */ -static bool nft_pipapo_flush (const struct net *net,const struct nft_set *set, +static void nft_pipapo_flush (const struct net *net,const struct nft_set *set,void *elem) {struct nft_pipapo_elem *e = elem;nft_set_elem_change_active (net, set, &e->ext); - -return true; }/**
net/netfilter/nft_set_rbtree.c +1 -3
@@ -535,14 +535,12 @@ static void nft_rbtree_activate(const struct net *net,nft_set_elem_change_active (net, set, &rbe->ext); } -static bool nft_rbtree_flush (const struct net *net, +static void nft_rbtree_flush (const struct net *net,const struct nft_set *set,void *priv) {struct nft_rbtree_elem *rbe = priv;nft_set_elem_change_active (net, set, &rbe->ext); - -return true; } static void *nft_rbtree_deactivate(const struct net *net,
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 16/19 netfilter: nf_tables: expose opaque set element as struct nft_elem_priv
In reply to Pablo Neira AyusoAdd placeholder structure and place it at the beginning of each struct nft_*_elem for each existing set backend, instead of exposing elements as void type to the frontend which defeats compiler type checks. Use this pointer to this new type to replace void *.
This patch updates the following set backend API to use this new struct nft_elem_priv placeholder structure:
- update
- deactivate
- flush
- get
as well as the following helper functions:
- nft_set_elem_ext()
- nft_set_elem_init()
- nft_set_elem_destroy()
- nf_tables_set_elem_destroy()
This patch adds nft_elem_priv_cast() to cast struct nft_elem_priv to native element representation from the corresponding set backend. BUILD_BUG_ON() makes sure this .priv placeholder is always at the top of the opaque set element representation.
--- include/net/netfilter/nf_tables.h | 38 ++++++++++----- net/netfilter/nf_tables_api.c | 27 ++++++----- net/netfilter/nft_dynset.c | 23 ++++----- net/netfilter/nft_set_bitmap.c | 35 ++++++++------ net/netfilter/nft_set_hash.c | 80 ++++++++++++++++++------------- net/netfilter/nft_set_pipapo.c | 41 ++++++++++------ net/netfilter/nft_set_pipapo.h | 4 +- net/netfilter/nft_set_rbtree.c | 46 ++++++++++-------- 8 files changed, 173 insertions(+), 121 deletions(-)
include/net/netfilter/nf_tables.h +25 -13
@@ -274,6 +274,9 @@ struct nft_userdata {unsigned char data[]; }; +/* placeholder structure for opaque set element backend representation. */ +struct nft_elem_priv { }; +/** * struct nft_set_elem - generic representation of set elements *
@@ -294,9 +297,14 @@ struct nft_set_elem {u32 buf[NFT_DATA_VALUE_MAXLEN /sizeof (u32)];struct nft_data val; } data; -void *priv; +struct nft_elem_priv *priv; }; +static inline void *nft_elem_priv_cast (const struct nft_elem_priv *priv) +{ +return (void *)priv; +} +struct nft_set ;struct nft_set_iter {u8 genmask;
@@ -430,7 +438,8 @@ struct nft_set_ops { const struct nft_set_ext **ext); bool (*update)(struct nft_set *set, const u32 *key, - void *(*new)(struct nft_set *, + struct nft_elem_priv * + (*new)(struct nft_set *, const struct nft_expr *, struct nft_regs *), const struct nft_expr *expr,
@@ -446,19 +455,19 @@ struct nft_set_ops {void (*activate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem); -void * (*deactivate)(const struct net *net, +struct nft_elem_priv * (*deactivate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem);void (*flush)(const struct net *net,const struct nft_set *set, -void *priv); +struct nft_elem_priv *priv);void (*remove)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem);void (*walk)(const struct nft_ctx *ctx,struct nft_set *set,struct nft_set_iter *iter); -void * (*get)(const struct net *net, +struct nft_elem_priv * (*get)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem,unsigned int flags);
@@ -796,9 +805,9 @@ static inline bool nft_set_elem_expired(const struct nft_set_ext *ext) }static inline struct nft_set_ext *nft_set_elem_ext (const struct nft_set *set, -void *elem) +const struct nft_elem_priv *elem_priv) { -return elem + set->ops->elemsize; +return (void *)elem_priv + set->ops->elemsize; }static inline struct nft_object **nft_set_ext_obj (const struct nft_set_ext *ext)
@@ -810,16 +819,19 @@ struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,const struct nft_set *set, const struct nlattr *attr); -void *nft_set_elem_init (const struct nft_set *set, -const struct nft_set_ext_tmpl *tmpl, -const u32 *key,const u32 *key_end,const u32 *data, -u64 timeout,u64 expiration,gfp_t gfp); +struct nft_elem_priv *nft_set_elem_init (const struct nft_set *set, +const struct nft_set_ext_tmpl *tmpl, +const u32 *key,const u32 *key_end, +const u32 *data, +u64 timeout,u64 expiration,gfp_t gfp);int nft_set_elem_expr_clone (const struct nft_ctx *ctx,struct nft_set *set,struct nft_expr *expr_array[]); -void nft_set_elem_destroy (const struct nft_set *set,void *elem, +void nft_set_elem_destroy (const struct nft_set *set, +const struct nft_elem_priv *elem_priv,bool destroy_expr);void nf_tables_set_elem_destroy (const struct nft_ctx *ctx, -const struct nft_set *set,void *elem); +const struct nft_set *set, +const struct nft_elem_priv *elem_priv);struct nft_expr_ops ;/**
net/netfilter/nf_tables_api.c +15 -12
@@ -601,7 +601,7 @@ static int nft_mapelem_deactivate(const struct nft_ctx *ctx,struct nft_set_elem_catchall {struct list_head list;struct rcu_head rcu; -void *elem; +struct nft_elem_priv *elem; }; static void nft_map_catchall_deactivate(const struct nft_ctx *ctx,
@@ -6218,10 +6218,11 @@ static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id,return 0 ; } -void *nft_set_elem_init (const struct nft_set *set, -const struct nft_set_ext_tmpl *tmpl, -const u32 *key,const u32 *key_end, -const u32 *data,u64 timeout,u64 expiration,gfp_t gfp) +struct nft_elem_priv *nft_set_elem_init (const struct nft_set *set, +const struct nft_set_ext_tmpl *tmpl, +const u32 *key,const u32 *key_end, +const u32 *data, +u64 timeout,u64 expiration,gfp_t gfp) {struct nft_set_ext *ext;void *elem;
@@ -6286,10 +6287,11 @@ static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, }/* Drop references and destroy. Called from gc, dynset and abort path. */ -void nft_set_elem_destroy (const struct nft_set *set,void *elem, +void nft_set_elem_destroy (const struct nft_set *set, +const struct nft_elem_priv *elem_priv,bool destroy_expr) { -struct nft_set_ext *ext =nft_set_elem_ext (set, elem); +struct nft_set_ext *ext =nft_set_elem_ext (set, elem_priv);struct nft_ctx ctx = { .net =read_pnet (&set->net), .family = set->table->family,
@@ -6300,10 +6302,10 @@ void nft_set_elem_destroy(const struct nft_set *set, void *elem,nft_data_release (nft_set_ext_data (ext), set->dtype);if (destroy_expr &&nft_set_ext_exists (ext,NFT_SET_EXT_EXPRESSIONS ))nft_set_elem_expr_destroy (&ctx,nft_set_ext_expr (ext)); -if (nft_set_ext_exists (ext,NFT_SET_EXT_OBJREF ))nft_use_dec (&(*nft_set_ext_obj (ext))->use); -kfree (elem); + +kfree (elem_priv); }EXPORT_SYMBOL_GPL (nft_set_elem_destroy);
@@ -6311,14 +6313,15 @@ EXPORT_SYMBOL_GPL(nft_set_elem_destroy);* path via nft_setelem_data_deactivate(). */ void nf_tables_set_elem_destroy (const struct nft_ctx *ctx, -const struct nft_set *set,void *elem) +const struct nft_set *set, +const struct nft_elem_priv *elem_priv) { -struct nft_set_ext *ext =nft_set_elem_ext (set, elem); +struct nft_set_ext *ext =nft_set_elem_ext (set, elem_priv);if (nft_set_ext_exists (ext,NFT_SET_EXT_EXPRESSIONS ))nft_set_elem_expr_destroy (ctx,nft_set_ext_expr (ext)); -kfree (elem); +kfree (elem_priv); } int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
net/netfilter/nft_dynset.c +12 -11
@@ -44,33 +44,34 @@ static int nft_dynset_expr_setup(const struct nft_dynset *priv,return 0 ; } -static void *nft_dynset_new (struct nft_set *set,const struct nft_expr *expr, -struct nft_regs *regs) +static struct nft_elem_priv *nft_dynset_new (struct nft_set *set, +const struct nft_expr *expr, +struct nft_regs *regs) {const struct nft_dynset *priv =nft_expr_priv (expr);struct nft_set_ext *ext; +void *elem_priv;u64 timeout; -void *elem;if (!atomic_add_unless (&set->nelems,1 , set->size))return NULL ; timeout = priv->timeout ? : set->timeout; - elem =nft_set_elem_init (set, &priv->tmpl, - ®s->data[priv->sreg_key],NULL , - ®s->data[priv->sreg_data], - timeout,0 ,GFP_ATOMIC ); -if (IS_ERR (elem)) + elem_priv =nft_set_elem_init (set, &priv->tmpl, + ®s->data[priv->sreg_key],NULL , + ®s->data[priv->sreg_data], + timeout,0 ,GFP_ATOMIC ); +if (IS_ERR (elem_priv)) goto err1; - ext =nft_set_elem_ext (set, elem); + ext =nft_set_elem_ext (set, elem_priv);if (priv->num_exprs &&nft_dynset_expr_setup (priv, ext) <0 ) goto err2; -return elem; +return elem_priv; err2: -nft_set_elem_destroy (set, elem, false); +nft_set_elem_destroy (set, elem_priv, false); err1:if (set->size)atomic_dec (&set->nelems);
net/netfilter/nft_set_bitmap.c +20 -15
@@ -13,6 +13,7 @@#include <net/netfilter/nf_tables_core.h> struct nft_bitmap_elem { +struct nft_elem_priv priv;struct list_head head;struct nft_set_ext ext; };
@@ -104,8 +105,9 @@ nft_bitmap_elem_find(const struct nft_set *set, struct nft_bitmap_elem *this,return NULL ; } -static void *nft_bitmap_get (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem,unsigned int flags) +static struct nft_elem_priv * +nft_bitmap_get (const struct net *net,const struct nft_set *set, +const struct nft_set_elem *elem,unsigned int flags) {const struct nft_bitmap *priv =nft_set_priv (set);u8 genmask =nft_genmask_cur (net);
@@ -116,7 +118,7 @@ static void *nft_bitmap_get(const struct net *net, const struct nft_set *set, !nft_set_elem_active(&be->ext, genmask))continue ; -return be; +return &be->priv; }return ERR_PTR (-ENOENT ); }
@@ -125,8 +127,8 @@ static int nft_bitmap_insert(const struct net *net, const struct nft_set *set,const struct nft_set_elem *elem, struct nft_set_ext **ext) { + struct nft_bitmap_elem *new = nft_elem_priv_cast(elem->priv), *be;struct nft_bitmap *priv =nft_set_priv (set); -struct nft_bitmap_elem *new = elem->priv, *be;u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -148,8 +150,8 @@ static void nft_bitmap_remove(const struct net *net,const struct nft_set *set, const struct nft_set_elem *elem) { + struct nft_bitmap_elem *be = nft_elem_priv_cast(elem->priv);struct nft_bitmap *priv =nft_set_priv (set); -struct nft_bitmap_elem *be = elem->priv;u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -163,8 +165,8 @@ static void nft_bitmap_activate(const struct net *net,const struct nft_set *set, const struct nft_set_elem *elem) { + struct nft_bitmap_elem *be = nft_elem_priv_cast(elem->priv);struct nft_bitmap *priv =nft_set_priv (set); -struct nft_bitmap_elem *be = elem->priv;u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -175,11 +177,12 @@ static void nft_bitmap_activate(const struct net *net, }static void nft_bitmap_flush (const struct net *net, -const struct nft_set *set,void *_be) +const struct nft_set *set, +struct nft_elem_priv *elem_priv) { +struct nft_bitmap_elem *be =nft_elem_priv_cast (elem_priv);struct nft_bitmap *priv =nft_set_priv (set);u8 genmask =nft_genmask_next (net); -struct nft_bitmap_elem *be = _be;u32 idx, off;nft_bitmap_location (set,nft_set_ext_key (&be->ext), &idx, &off);
@@ -188,12 +191,12 @@ static void nft_bitmap_flush(const struct net *net,nft_set_elem_change_active (net, set, &be->ext); } -static void *nft_bitmap_deactivate (const struct net *net, -const struct nft_set *set, -const struct nft_set_elem *elem) +static struct nft_elem_priv * +nft_bitmap_deactivate (const struct net *net,const struct nft_set *set, +const struct nft_set_elem *elem) { +struct nft_bitmap_elem *this =nft_elem_priv_cast (elem->priv), *be;struct nft_bitmap *priv =nft_set_priv (set); -struct nft_bitmap_elem *this = elem->priv, *be;u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -207,7 +210,7 @@ static void *nft_bitmap_deactivate(const struct net *net, priv->bitmap[idx] &= ~(genmask << off);nft_set_elem_change_active (net, set, &be->ext); -return be; +return &be->priv; } static void nft_bitmap_walk(const struct nft_ctx *ctx,
@@ -224,7 +227,7 @@ static void nft_bitmap_walk(const struct nft_ctx *ctx,if (!nft_set_elem_active (&be->ext, iter->genmask)) goto cont; - elem.priv = be; + elem.priv = &be->priv; iter->err = iter->fn (ctx, set, iter, &elem);
@@ -263,6 +266,8 @@ static int nft_bitmap_init(const struct nft_set *set, {struct nft_bitmap *priv =nft_set_priv (set); +BUILD_BUG_ON (offsetof(struct nft_bitmap_elem , priv) !=0 ); +INIT_LIST_HEAD (&priv->list); priv->bitmap_size =nft_bitmap_size (set->klen);
@@ -276,7 +281,7 @@ static void nft_bitmap_destroy(const struct nft_ctx *ctx,struct nft_bitmap_elem *be, *n;list_for_each_entry_safe (be, n, &priv->list, head) -nf_tables_set_elem_destroy (ctx, set, be); +nf_tables_set_elem_destroy (ctx, set, &be->priv); } static bool nft_bitmap_estimate(const struct nft_set_desc *desc, u32 features,
net/netfilter/nft_set_hash.c +46 -34
250 lines not on this page · raw message
net/netfilter/nft_set_pipapo.c +26 -15
115 lines not on this page · raw message
net/netfilter/nft_set_pipapo.h +3 -1
@@ -170,10 +170,12 @@ struct nft_pipapo_elem;/** * struct nft_pipapo_elem - API-facing representation of single set element +* @priv: element placeholder * @ext: nftables API extensions */ struct nft_pipapo_elem { -struct nft_set_ext ext; +struct nft_elem_priv priv; +struct nft_set_ext ext; }; int pipapo_refill(unsigned long *map, int len, int rules, unsigned long *dst,
net/netfilter/nft_set_rbtree.c +26 -20
133 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 17/19 netfilter: nf_tables: shrink memory consumption of set elements
In reply to Pablo Neira AyusoInstead of copying struct nft_set_elem into struct nft_trans_elem, store the pointer to the opaque set element object in the transaction. Adapt set backend API (and set backend implementations) to take the pointer to opaque set element representation whenever required.
This patch deconstifies .remove() and .activate() set backend API since these modify the set element opaque object. And it also constify nft_set_elem_ext() this provides access to the nft_set_ext struct without updating the object.
According to pahole on x86_64, this patch shrinks struct nft_trans_elem size from 216 to 24 bytes.
This patch also reduces stack memory consumption by removing the template struct nft_set_elem object, using the opaque set element object instead such as from the set iterator API, catchall elements and the get element command.
--- include/net/netfilter/nf_tables.h | 18 ++-- net/netfilter/nf_tables_api.c | 166 ++++++++++++++---------------- net/netfilter/nft_set_bitmap.c | 16 ++- net/netfilter/nft_set_hash.c | 26 ++--- net/netfilter/nft_set_pipapo.c | 25 ++--- net/netfilter/nft_set_rbtree.c | 25 ++--- 6 files changed, 116 insertions(+), 160 deletions(-)
include/net/netfilter/nf_tables.h +9 -9
@@ -314,7 +314,7 @@ struct nft_set_iter {int (*fn)(const struct nft_ctx *ctx,struct nft_set *set,const struct nft_set_iter *iter, -struct nft_set_elem *elem); +struct nft_elem_priv *elem_priv); };/**
@@ -454,7 +454,7 @@ struct nft_set_ops { struct nft_set_ext **ext);void (*activate)(const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem); +struct nft_elem_priv *elem_priv);struct nft_elem_priv * (*deactivate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem);
@@ -463,7 +463,7 @@ struct nft_set_ops { struct nft_elem_priv *priv);void (*remove)(const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem); +struct nft_elem_priv *elem_priv);void (*walk)(const struct nft_ctx *ctx,struct nft_set *set,struct nft_set_iter *iter);
@@ -1073,7 +1073,7 @@ struct nft_chain {int nft_chain_validate (const struct nft_ctx *ctx,const struct nft_chain *chain);int nft_setelem_validate (const struct nft_ctx *ctx,struct nft_set *set,const struct nft_set_iter *iter, -struct nft_set_elem *elem); +struct nft_elem_priv *elem_priv);int nft_set_catchall_validate (const struct nft_ctx *ctx,struct nft_set *set);int nf_tables_bind_chain (const struct nft_ctx *ctx,struct nft_chain *chain);void nf_tables_unbind_chain (const struct nft_ctx *ctx,struct nft_chain *chain);
@@ -1650,14 +1650,14 @@ struct nft_trans_table {struct nft_trans_elem {struct nft_set *set; -struct nft_set_elem elem; +struct nft_elem_priv *elem_priv;bool bound; };#define nft_trans_elem_set (trans) \ (((struct nft_trans_elem *)trans->data)->set) -#define nft_trans_elem (trans) \ - (((struct nft_trans_elem *)trans->data)->elem) +#define nft_trans_elem_priv (trans) \ + (((struct nft_trans_elem *)trans->data)->elem_priv)#define nft_trans_elem_set_bound (trans) \ (((struct nft_trans_elem *)trans->data)->bound)
@@ -1698,7 +1698,7 @@ struct nft_trans_gc {struct nft_set *set;u32 seq;u16 count; -void *priv[NFT_TRANS_GC_BATCHCOUNT ]; +struct nft_elem_priv *priv[NFT_TRANS_GC_BATCHCOUNT ];struct rcu_head rcu; };
@@ -1721,7 +1721,7 @@ struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc);void nft_setelem_data_deactivate (const struct net *net,const struct nft_set *set, -struct nft_set_elem *elem); +struct nft_elem_priv *elem_priv);int __init nft_chain_filter_init (void );void nft_chain_filter_fini (void );
net/netfilter/nf_tables_api.c +75 -91
490 lines not on this page · raw message
net/netfilter/nft_set_bitmap.c +6 -10
@@ -146,11 +146,10 @@ static int nft_bitmap_insert(const struct net *net, const struct nft_set *set,return 0 ; } -static void nft_bitmap_remove (const struct net *net, -const struct nft_set *set, -const struct nft_set_elem *elem) +static void nft_bitmap_remove (const struct net *net,const struct nft_set *set, +struct nft_elem_priv *elem_priv) { -struct nft_bitmap_elem *be =nft_elem_priv_cast (elem->priv); +struct nft_bitmap_elem *be =nft_elem_priv_cast (elem_priv);struct nft_bitmap *priv =nft_set_priv (set);u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -163,9 +162,9 @@ static void nft_bitmap_remove(const struct net *net,static void nft_bitmap_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_bitmap_elem *be =nft_elem_priv_cast (elem->priv); +struct nft_bitmap_elem *be =nft_elem_priv_cast (elem_priv);struct nft_bitmap *priv =nft_set_priv (set);u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -219,7 +218,6 @@ static void nft_bitmap_walk(const struct nft_ctx *ctx, {const struct nft_bitmap *priv =nft_set_priv (set);struct nft_bitmap_elem *be; -struct nft_set_elem elem;list_for_each_entry_rcu (be, &priv->list, head) {if (iter->count < iter->skip)
@@ -227,9 +225,7 @@ static void nft_bitmap_walk(const struct nft_ctx *ctx,if (!nft_set_elem_active (&be->ext, iter->genmask)) goto cont; - elem.priv = &be->priv; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, &be->priv);if (iter->err <0 )return ;
net/netfilter/nft_set_hash.c +10 -16
@@ -190,9 +190,9 @@ static int nft_rhash_insert(const struct net *net, const struct nft_set *set, }static void nft_rhash_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_rhash_elem *he =nft_elem_priv_cast (elem->priv); +struct nft_rhash_elem *he =nft_elem_priv_cast (elem_priv);nft_set_elem_change_active (net, set, &he->ext); }
@@ -230,9 +230,9 @@ nft_rhash_deactivate(const struct net *net, const struct nft_set *set,static void nft_rhash_remove (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_rhash_elem *he =nft_elem_priv_cast (elem->priv); +struct nft_rhash_elem *he =nft_elem_priv_cast (elem_priv);struct nft_rhash *priv =nft_set_priv (set);rhashtable_remove_fast (&priv->ht, &he->node, nft_rhash_params);
@@ -264,7 +264,6 @@ static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,struct nft_rhash *priv =nft_set_priv (set);struct nft_rhash_elem *he;struct rhashtable_iter hti; -struct nft_set_elem elem;rhashtable_walk_enter (&priv->ht, &hti);rhashtable_walk_start (&hti);
@@ -284,9 +283,7 @@ static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,if (!nft_set_elem_active (&he->ext, iter->genmask)) goto cont; - elem.priv = &he->priv; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, &he->priv);if (iter->err <0 )break ;
@@ -592,9 +589,9 @@ static int nft_hash_insert(const struct net *net, const struct nft_set *set, }static void nft_hash_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_hash_elem *he =nft_elem_priv_cast (elem->priv); +struct nft_hash_elem *he =nft_elem_priv_cast (elem_priv);nft_set_elem_change_active (net, set, &he->ext); }
@@ -631,9 +628,9 @@ nft_hash_deactivate(const struct net *net, const struct nft_set *set,static void nft_hash_remove (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_hash_elem *he =nft_elem_priv_cast (elem->priv); +struct nft_hash_elem *he =nft_elem_priv_cast (elem_priv);hlist_del_rcu (&he->node); }
@@ -643,7 +640,6 @@ static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set, {struct nft_hash *priv =nft_set_priv (set);struct nft_hash_elem *he; -struct nft_set_elem elem;int i;for (i =0 ; i < priv->buckets; i++) {
@@ -653,9 +649,7 @@ static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,if (!nft_set_elem_active (&he->ext, iter->genmask)) goto cont; - elem.priv = &he->priv; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, &he->priv);if (iter->err <0 )return ; cont:
net/netfilter/nft_set_pipapo.c +9 -16
@@ -1548,11 +1548,7 @@ static void nft_pipapo_gc_deactivate(struct net *net, struct nft_set *set, struct nft_pipapo_elem *e) { -struct nft_set_elem elem = { - .priv = &e->priv, - }; - -nft_setelem_data_deactivate (net, set, &elem); +nft_setelem_data_deactivate (net, set, &e->priv); }/**
@@ -1739,7 +1735,7 @@ static void nft_pipapo_abort(const struct nft_set *set)* nft_pipapo_activate() - Mark element reference as active given key, commit * @net: Network namespace * @set: nftables API set representation -* @elem: nftables API element representation containing key data +* @elem_priv: nftables API element representation containing key data * * On insertion, elements are added to a copy of the matching data currently * in use for lookups, and not directly inserted into current lookup data. Both
@@ -1748,9 +1744,9 @@ static void nft_pipapo_abort(const struct nft_set *set)*/ static void nft_pipapo_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_pipapo_elem *e =nft_elem_priv_cast (elem->priv); +struct nft_pipapo_elem *e =nft_elem_priv_cast (elem_priv);nft_set_elem_change_active (net, set, &e->ext); }
@@ -1803,7 +1799,7 @@ nft_pipapo_deactivate(const struct net *net, const struct nft_set *set,* nft_pipapo_flush() - Call pipapo_deactivate() to make element inactive * @net: Network namespace * @set: nftables API set representation -* @elem: nftables API element representation containing key data +* @elem_priv: nftables API element representation containing key data * * This is functionally the same as nft_pipapo_deactivate(), with a slightly * different interface, and it's also called once for each element in a set
@@ -1945,7 +1941,7 @@ static bool pipapo_match_field(struct nft_pipapo_field *f,* nft_pipapo_remove() - Remove element given key, commit * @net: Network namespace * @set: nftables API set representation -* @elem: nftables API element representation containing key data +* @elem_priv: nftables API element representation containing key data * * Similarly to nft_pipapo_activate(), this is used as commit operation by the * API, but it's called once per element in the pending transaction, so we can't
@@ -1953,7 +1949,7 @@ static bool pipapo_match_field(struct nft_pipapo_field *f,* the matched element here, if any, and commit the updated matching data. */ static void nft_pipapo_remove (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) {struct nft_pipapo *priv =nft_set_priv (set);struct nft_pipapo_match *m = priv->clone;
@@ -1961,7 +1957,7 @@ static void nft_pipapo_remove(const struct net *net, const struct nft_set *set,struct nft_pipapo_elem *e;const u8 *data; - e =nft_elem_priv_cast (elem->priv); + e =nft_elem_priv_cast (elem_priv); data = (const u8 *)nft_set_ext_key (&e->ext);while ((rules_f0 =pipapo_rules_same_key (m->f, first_rule))) {
@@ -2038,7 +2034,6 @@ static void nft_pipapo_walk(const struct nft_ctx *ctx, struct nft_set *set,for (r =0 ; r < f->rules; r++) {struct nft_pipapo_elem *e; -struct nft_set_elem elem;if (r < f->rules -1 && f->mt[r +1 ].e == f->mt[r].e)continue ;
@@ -2048,9 +2043,7 @@ static void nft_pipapo_walk(const struct nft_ctx *ctx, struct nft_set *set, e = f->mt[r].e; - elem.priv = &e->priv; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, &e->priv);if (iter->err <0 ) goto out;
net/netfilter/nft_set_rbtree.c +7 -18
@@ -227,12 +227,8 @@ static void nft_rbtree_gc_elem_remove(struct net *net, struct nft_set *set,struct nft_rbtree *priv, struct nft_rbtree_elem *rbe) { - struct nft_set_elem elem = { - .priv = &rbe->priv, - }; - lockdep_assert_held_write(&priv->lock); -nft_setelem_data_deactivate (net, set, &elem); +nft_setelem_data_deactivate (net, set, &rbe->priv);rb_erase (&rbe->node, &priv->root); }
@@ -521,9 +517,9 @@ static void nft_rbtree_erase(struct nft_rbtree *priv, struct nft_rbtree_elem *rbstatic void nft_rbtree_remove (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_rbtree_elem *rbe =nft_elem_priv_cast (elem->priv); +struct nft_rbtree_elem *rbe =nft_elem_priv_cast (elem_priv);struct nft_rbtree *priv =nft_set_priv (set);nft_rbtree_erase (priv, rbe);
@@ -531,9 +527,9 @@ static void nft_rbtree_remove(const struct net *net,static void nft_rbtree_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +struct nft_elem_priv *elem_priv) { -struct nft_rbtree_elem *rbe =nft_elem_priv_cast (elem->priv); +struct nft_rbtree_elem *rbe =nft_elem_priv_cast (elem_priv);nft_set_elem_change_active (net, set, &rbe->ext); }
@@ -594,7 +590,6 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx, {struct nft_rbtree *priv =nft_set_priv (set);struct nft_rbtree_elem *rbe; -struct nft_set_elem elem;struct rb_node *node;read_lock_bh (&priv->lock);
@@ -606,9 +601,7 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,if (!nft_set_elem_active (&rbe->ext, iter->genmask)) goto cont; - elem.priv = &rbe->priv; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, &rbe->priv);if (iter->err <0 ) {read_unlock_bh (&priv->lock);return ;
@@ -623,11 +616,7 @@ static void nft_rbtree_gc_remove(struct net *net, struct nft_set *set,struct nft_rbtree *priv, struct nft_rbtree_elem *rbe) { - struct nft_set_elem elem = { - .priv = rbe, - }; - -nft_setelem_data_deactivate (net, set, &elem); + nft_setelem_data_deactivate(net, set, &rbe->priv);nft_rbtree_erase (priv, rbe); }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 18/19 netfilter: nf_tables: set->ops->insert returns opaque set element in case of EEXIST
In reply to Pablo Neira AyusoReturn struct nft_elem_priv instead of struct nft_set_ext for consistency with ("netfilter: nf_tables: expose opaque set element as struct nft_elem_priv") and to prepare the introduction of element timeout updates from control path.
--- include/net/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 17 ++++++++++------- net/netfilter/nft_set_bitmap.c | 4 ++-- net/netfilter/nft_set_hash.c | 8 ++++---- net/netfilter/nft_set_pipapo.c | 10 +++++----- net/netfilter/nft_set_rbtree.c | 10 +++++----- 6 files changed, 27 insertions(+), 24 deletions(-)
include/net/netfilter/nf_tables.h +1 -1
@@ -451,7 +451,7 @@ struct nft_set_ops {int (*insert)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext); +struct nft_elem_priv **priv);void (*activate)(const struct net *net,const struct nft_set *set,struct nft_elem_priv *elem_priv);
net/netfilter/nf_tables_api.c +10 -7
@@ -6406,7 +6406,7 @@ EXPORT_SYMBOL_GPL(nft_set_catchall_lookup);static int nft_setelem_catchall_insert (const struct net *net,struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **pext) +struct nft_elem_priv **priv) {struct nft_set_elem_catchall *catchall;u8 genmask =nft_genmask_next (net);
@@ -6415,7 +6415,7 @@ static int nft_setelem_catchall_insert(const struct net *net,list_for_each_entry (catchall, &set->catchall_list, list) { ext =nft_set_elem_ext (set, catchall->elem);if (nft_set_elem_active (ext, genmask)) { - *pext = ext; + *priv = catchall->elem;return -EEXIST ; } }
@@ -6433,14 +6433,15 @@ static int nft_setelem_catchall_insert(const struct net *net,static int nft_setelem_insert (const struct net *net,struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext,unsigned int flags) +struct nft_elem_priv **elem_priv, +unsigned int flags) {int ret;if (flags &NFT_SET_ELEM_CATCHALL ) - ret =nft_setelem_catchall_insert (net, set, elem, ext); + ret =nft_setelem_catchall_insert (net, set, elem, elem_priv);else - ret = set->ops->insert (net, set, elem, ext); + ret = set->ops->insert (net, set, elem, elem_priv);return ret; }
@@ -6576,13 +6577,14 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,struct nft_set_ext *ext, *ext2;struct nft_set_elem elem;struct nft_set_binding *binding; +struct nft_elem_priv *elem_priv;struct nft_object *obj =NULL ;struct nft_userdata *udata;struct nft_data_desc desc;enum nft_registers dreg;struct nft_trans *trans; -u64 timeout;u64 expiration; +u64 timeout;int err, i;u8 ulen;
@@ -6875,9 +6877,10 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, ext->genmask =nft_genmask_cur (ctx->net); - err =nft_setelem_insert (ctx->net, set, &elem, &ext2, flags); + err =nft_setelem_insert (ctx->net, set, &elem, &elem_priv, flags); if (err) { if (err == -EEXIST) { + ext2 = nft_set_elem_ext(set, elem_priv); if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
net/netfilter/nft_set_bitmap.c +2 -2
@@ -125,7 +125,7 @@ nft_bitmap_get(const struct net *net, const struct nft_set *set,static int nft_bitmap_insert (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext) +struct nft_elem_priv **elem_priv) {struct nft_bitmap_elem *new =nft_elem_priv_cast (elem->priv), *be;struct nft_bitmap *priv =nft_set_priv (set);
@@ -134,7 +134,7 @@ static int nft_bitmap_insert(const struct net *net, const struct nft_set *set, be =nft_bitmap_elem_find (set, new, genmask);if (be) { - *ext = &be->ext; + *elem_priv = &be->priv;return -EEXIST ; }
net/netfilter/nft_set_hash.c +4 -4
@@ -167,7 +167,7 @@ static bool nft_rhash_update(struct nft_set *set, const u32 *key,static int nft_rhash_insert (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext) +struct nft_elem_priv **elem_priv) {struct nft_rhash_elem *he =nft_elem_priv_cast (elem->priv);struct nft_rhash *priv =nft_set_priv (set);
@@ -183,7 +183,7 @@ static int nft_rhash_insert(const struct net *net, const struct nft_set *set,if (IS_ERR (prev))return PTR_ERR (prev);if (prev) { - *ext = &prev->ext; + *elem_priv = &prev->priv;return -EEXIST ; }return 0 ;
@@ -568,7 +568,7 @@ static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv,static int nft_hash_insert (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext) +struct nft_elem_priv **elem_priv) {struct nft_hash_elem *this =nft_elem_priv_cast (elem->priv), *he;struct nft_hash *priv =nft_set_priv (set);
@@ -580,7 +580,7 @@ static int nft_hash_insert(const struct net *net, const struct nft_set *set,if (!memcmp (nft_set_ext_key (&this->ext),nft_set_ext_key (&he->ext), set->klen) &&nft_set_elem_active (&he->ext, genmask)) { - *ext = &he->ext; + *elem_priv = &he->priv;return -EEXIST ; } }
net/netfilter/nft_set_pipapo.c +5 -5
@@ -1158,13 +1158,13 @@ static int pipapo_realloc_scratch(struct nft_pipapo_match *clone,* @net: Network namespace * @set: nftables API set representation * @elem: nftables API element representation containing key data -* @ext2: Filled with pointer to &struct nft_set_ext in inserted element +* @elem_priv: Filled with pointer to &struct nft_set_ext in inserted element * * Return: 0 on success, error pointer on failure. */ static int nft_pipapo_insert (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext2) +struct nft_elem_priv **elem_priv) {const struct nft_set_ext *ext =nft_set_elem_ext (set, elem->priv);union nft_pipapo_map_bucket rulemap[NFT_PIPAPO_MAX_FIELDS ];
@@ -1195,7 +1195,7 @@ static int nft_pipapo_insert(const struct net *net, const struct nft_set *set,if (!memcmp (start, dup_key->data,sizeof (*dup_key->data)) && !memcmp (end, dup_end->data,sizeof (*dup_end->data))) { - *ext2 = &dup->ext; + *elem_priv = &dup->priv;return -EEXIST ; }
@@ -1210,7 +1210,7 @@ static int nft_pipapo_insert(const struct net *net, const struct nft_set *set,if (PTR_ERR (dup) != -ENOENT ) {if (IS_ERR (dup))return PTR_ERR (dup); - *ext2 = &dup->ext; + *elem_priv = &dup->priv;return -ENOTEMPTY ; }
@@ -1271,7 +1271,7 @@ static int nft_pipapo_insert(const struct net *net, const struct nft_set *set, } e =nft_elem_priv_cast (elem->priv); - *ext2 = &e->ext; + *elem_priv = &e->priv;pipapo_map (m, rulemap, e);
net/netfilter/nft_set_rbtree.c +5 -5
@@ -306,7 +306,7 @@ static bool nft_rbtree_update_first(const struct nft_set *set,static int __nft_rbtree_insert (const struct net *net,const struct nft_set *set,struct nft_rbtree_elem *new, -struct nft_set_ext **ext) +struct nft_elem_priv **elem_priv) {struct nft_rbtree_elem *rbe, *rbe_le =NULL , *rbe_ge =NULL ;struct rb_node *node, *next, *parent, **p, *first =NULL ;
@@ -423,7 +423,7 @@ static int __nft_rbtree_insert(const struct net *net, const struct nft_set *set,*/ if (rbe_ge && !nft_rbtree_cmp (set, new, rbe_ge) &&nft_rbtree_interval_start (rbe_ge) ==nft_rbtree_interval_start (new)) { - *ext = &rbe_ge->ext; + *elem_priv = &rbe_ge->priv;return -EEXIST ; }
@@ -432,7 +432,7 @@ static int __nft_rbtree_insert(const struct net *net, const struct nft_set *set,*/ if (rbe_le && !nft_rbtree_cmp (set, new, rbe_le) &&nft_rbtree_interval_end (rbe_le) ==nft_rbtree_interval_end (new)) { - *ext = &rbe_le->ext; + *elem_priv = &rbe_le->priv;return -EEXIST ; }
@@ -484,7 +484,7 @@ static int __nft_rbtree_insert(const struct net *net, const struct nft_set *set,static int nft_rbtree_insert (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem, -struct nft_set_ext **ext) +struct nft_elem_priv **elem_priv) {struct nft_rbtree_elem *rbe =nft_elem_priv_cast (elem->priv);struct nft_rbtree *priv =nft_set_priv (set);
@@ -498,7 +498,7 @@ static int nft_rbtree_insert(const struct net *net, const struct nft_set *set,write_lock_bh (&priv->lock);write_seqcount_begin (&priv->count); - err =__nft_rbtree_insert (net, set, rbe, ext); + err =__nft_rbtree_insert (net, set, rbe, elem_priv);write_seqcount_end (&priv->count);write_unlock_bh (&priv->lock); }while (err == -EAGAIN );
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 19/19 netfilter: nf_tables: Carry reset boolean in nft_set_dump_ctx
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
Relieve the dump callback from having to check nlmsg_type upon each call. Prep work for set element reset locking.
--- net/netfilter/nf_tables_api.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
net/netfilter/nf_tables_api.c +8 -10
@@ -5715,6 +5715,7 @@ static void audit_log_nft_set_reset(const struct nft_table *table,struct nft_set_dump_ctx {const struct nft_set *set;struct nft_ctx ctx; +bool reset; }; static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb,
@@ -5752,7 +5753,6 @@ static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)bool set_found = false;struct nlmsghdr *nlh;struct nlattr *nest; -bool reset = false;u32 portid, seq;int event;
@@ -5800,12 +5800,9 @@ static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)if (nest ==NULL ) goto nla_put_failure; -if (NFNL_MSG_TYPE (cb->nlh->nlmsg_type) ==NFT_MSG_GETSETELEM_RESET ) - reset = true; - args.cb = cb; args.skb = skb; - args.reset = reset; + args.reset = dump_ctx->reset; args.iter.genmask =nft_genmask_cur (net); args.iter.skip = cb->args[0 ]; args.iter.count =0 ;
@@ -5815,11 +5812,11 @@ static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)if (!args.iter.err && args.iter.count == cb->args[0 ]) args.iter.err =nft_set_catchall_dump (net, skb, set, - reset, cb->seq); + dump_ctx->reset, cb->seq);nla_nest_end (skb, nest);nlmsg_end (skb, nlh); -if (reset && args.iter.count > args.iter.skip) +if (dump_ctx->reset && args.iter.count > args.iter.skip)audit_log_nft_set_reset (table, cb->seq, args.iter.count - args.iter.skip);
@@ -6072,6 +6069,9 @@ static int nf_tables_getsetelem(struct sk_buff *skb,nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla); +if (NFNL_MSG_TYPE (info->nlh->nlmsg_type) ==NFT_MSG_GETSETELEM_RESET ) + reset = true; +if (info->nlh->nlmsg_flags &NLM_F_DUMP ) {struct netlink_dump_control c = { .start = nf_tables_dump_set_start,
@@ -6082,6 +6082,7 @@ static int nf_tables_getsetelem(struct sk_buff *skb,struct nft_set_dump_ctx dump_ctx = { .set = set, .ctx = ctx, + .reset = reset, }; c.data = &dump_ctx;
@@ -6091,9 +6092,6 @@ static int nf_tables_getsetelem(struct sk_buff *skb,if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS ])return -EINVAL ; -if (NFNL_MSG_TYPE (info->nlh->nlmsg_type) ==NFT_MSG_GETSETELEM_RESET ) - reset = true; -nla_for_each_nested (attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS ], rem) { err =nft_get_set_elem (&ctx, set, attr, reset);if (err <0 ) {
Signature · 1 line
2.30.2