Dynamic hook interface binding part 1
Subsystems (2)
Phil Sutter
reviewerv7 cover Dynamic hook interface binding part 1
Changes since v6:
- Rebase onto "netfilter: nf_tables: imbalance in flowtable binding" patch which is in nf-25-01-09 tag but missing in nf-next
- Drop patch 7 which removed __nft_unregister_flowtable_net_hooks(): The function is no longer a duplicate of nft_netdev_unregister_hooks()
This series makes netdev hooks store the interface name spec they were created for and establishes this stored name as the key identifier. The previous one which is the hook's 'ops.dev' pointer is thereby freed to vanish, so a vanishing netdev no longer has to drag the hook along with it. (Patches 2-4)
Furthermore, it aligns behaviour of netdev-family chains with that of flowtables in situations of vanishing interfaces. When previously a chain losing its last interface was torn down and deleted, it may now remain in place (albeit with no remaining interfaces). (Patch 5)
Patch 6 is a cleanup following patch 5, patch 1 is an independent code simplification.
Phil Sutter (6):
netfilter: nf_tables: Flowtable hook's pf value never varies
netfilter: nf_tables: Store user-defined hook ifname
netfilter: nf_tables: Use stored ifname in netdev hook dumps
netfilter: nf_tables: Compare netdev hooks based on stored name
netfilter: nf_tables: Tolerate chains with no remaining hooks
netfilter: nf_tables: Simplify chain netdev notifier
include/net/netfilter/nf_tables.h | 4 +- net/netfilter/nf_tables_api.c | 74 ++++++++----------------------- net/netfilter/nft_chain_filter.c | 48 ++++++-------------- 3 files changed, 33 insertions(+), 93 deletions(-)
Signature · 1 line
2.47.1
Phil Sutter
reviewerPATCH v7 1/6 netfilter: nf_tables: Flowtable hook's pf value never varies
When checking for duplicate hooks in nft_register_flowtable_net_hooks(), comparing ops.pf value is pointless as it is always NFPROTO_NETDEV with flowtable hooks.
Dropping the check leaves the search identical to the one in nft_hook_list_find() so call that function instead of open coding.
--- net/netfilter/nf_tables_api.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
net/netfilter/nf_tables_api.c +4 -7
@@ -8851,7 +8851,7 @@ static int nft_register_flowtable_net_hooks(struct net *net,struct list_head *hook_list, struct nft_flowtable *flowtable) { - struct nft_hook *hook, *hook2, *next; + struct nft_hook *hook, *next;struct nft_flowtable *ft;int err, i =0 ;
@@ -8860,12 +8860,9 @@ static int nft_register_flowtable_net_hooks(struct net *net,if (!nft_is_active_next (net, ft))continue ; -list_for_each_entry (hook2, &ft->hook_list, list) { -if (hook->ops.dev == hook2->ops.dev && - hook->ops.pf == hook2->ops.pf) { - err = -EEXIST ; - goto err_unregister_net_hooks; - } +if (nft_hook_list_find (&ft->hook_list, hook)) { + err = -EEXIST ; + goto err_unregister_net_hooks; } }
Signature · 1 line
2.47.1
Phil Sutter
reviewerPATCH v7 2/6 netfilter: nf_tables: Store user-defined hook ifname
In reply to Phil SutterPrepare for hooks with NULL ops.dev pointer (due to non-existent device) and store the interface name and length as specified by the user upon creation. No functional change intended.
--- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
@@ -1192,6 +1192,8 @@ struct nft_hook {struct list_head list;struct nf_hook_ops ops;struct rcu_head rcu; +char ifname[IFNAMSIZ ]; +u8 ifnamelen; };/**
net/netfilter/nf_tables_api.c +7 -3
@@ -2276,7 +2276,6 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net, const struct nlattr *attr) {struct net_device *dev; -char ifname[IFNAMSIZ ];struct nft_hook *hook;int err;
@@ -2286,12 +2285,17 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net, goto err_hook_alloc; } -nla_strscpy (ifname, attr,IFNAMSIZ ); + err =nla_strscpy (hook->ifname, attr,IFNAMSIZ ); +if (err <0 ) + goto err_hook_dev; + + hook->ifnamelen =nla_len (attr); +/* nf_tables_netdev_event() is called under rtnl_mutex, this is * indirectly serializing all the other holders of the commit_mutex with * the rtnl_mutex. */ - dev =__dev_get_by_name (net, ifname); + dev =__dev_get_by_name (net, hook->ifname);if (!dev) { err = -ENOENT ; goto err_hook_dev;
Signature · 1 line
2.47.1
Phil Sutter
reviewerPATCH v7 3/6 netfilter: nf_tables: Use stored ifname in netdev hook dumps
In reply to Phil SutterThe stored ifname and ops.dev->name may deviate after creation due to interface name changes. Prefer the more deterministic stored name in dumps which also helps avoiding inadvertent changes to stored ruleset dumps.
--- net/netfilter/nf_tables_api.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +6 -4
@@ -1956,15 +1956,16 @@ static int nft_dump_basechain_hook(struct sk_buff *skb,if (!first) first = hook; -if (nla_put_string (skb,NFTA_DEVICE_NAME , - hook->ops.dev->name)) +if (nla_put (skb,NFTA_DEVICE_NAME , + hook->ifnamelen, hook->ifname)) goto nla_put_failure; n++; }nla_nest_end (skb, nest_devs);if (n ==1 && -nla_put_string (skb,NFTA_HOOK_DEV , first->ops.dev->name)) +nla_put (skb,NFTA_HOOK_DEV , + first->ifnamelen, first->ifname)) goto nla_put_failure; }nla_nest_end (skb, nest);
@@ -9280,7 +9281,8 @@ static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,list_for_each_entry_rcu (hook, hook_list, list,lockdep_commit_lock_is_held (net)) { -if (nla_put_string (skb,NFTA_DEVICE_NAME , hook->ops.dev->name)) +if (nla_put (skb,NFTA_DEVICE_NAME , + hook->ifnamelen, hook->ifname)) goto nla_put_failure; }nla_nest_end (skb, nest_devs);
Signature · 1 line
2.47.1
Phil Sutter
reviewerPATCH v7 4/6 netfilter: nf_tables: Compare netdev hooks based on stored name
In reply to Phil SutterThe 1:1 relationship between nft_hook and nf_hook_ops is about to break, so choose the stored ifname to uniquely identify hooks.
--- net/netfilter/nf_tables_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +1 -1
@@ -2317,7 +2317,7 @@ static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,struct nft_hook *hook;list_for_each_entry (hook, hook_list, list) { -if (this->ops.dev == hook->ops.dev) +if (!strcmp (hook->ifname, this->ifname))return hook; }
Signature · 1 line
2.47.1
Phil Sutter
reviewerPATCH v7 5/6 netfilter: nf_tables: Tolerate chains with no remaining hooks
In reply to Phil SutterDo not drop a netdev-family chain if the last interface it is registered for vanishes. Users dumping and storing the ruleset upon shutdown to restore it upon next boot may otherwise lose the chain and all contained rules. They will still lose the list of devices, a later patch will fix that. For now, this aligns the event handler's behaviour with that for flowtables.
The controversal situation at netns exit should be no problem here:
event handler will unregister the hooks, core nftables cleanup code will drop the chain itself.
--- include/net/netfilter/nf_tables.h | 2 -- net/netfilter/nf_tables_api.c | 41 ------------------------------- net/netfilter/nft_chain_filter.c | 29 ++++++---------------- 3 files changed, 7 insertions(+), 65 deletions(-)
include/net/netfilter/nf_tables.h +0 -2
@@ -1229,8 +1229,6 @@ static inline bool nft_is_base_chain(const struct nft_chain *chain)return chain->flags &NFT_CHAIN_BASE ; } -int __nft_release_basechain (struct nft_ctx *ctx); -unsigned int nft_do_chain (struct nft_pktinfo *pkt,void *priv);static inline bool nft_use_inc (u32 *use)
net/netfilter/nf_tables_api.c +0 -41
47 lines not on this page · raw message
net/netfilter/nft_chain_filter.c +7 -22
@@ -322,34 +322,19 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev, struct nft_ctx *ctx) {struct nft_base_chain *basechain =nft_base_chain (ctx->chain); -struct nft_hook *hook, *found =NULL ; -int n =0 ; +struct nft_hook *hook;list_for_each_entry (hook, &basechain->hook_list, list) { -if (hook->ops.dev == dev) - found = hook; - - n++; - } -if (!found) -return ; +if (hook->ops.dev != dev) +continue ; -if (n >1 ) {if (!(ctx->chain->table->flags &NFT_TABLE_F_DORMANT )) -nf_unregister_net_hook (ctx->net, &found->ops); +nf_unregister_net_hook (ctx->net, &hook->ops); -list_del_rcu (&found->list); -kfree_rcu (found, rcu); -return ; +list_del_rcu (&hook->list); +kfree_rcu (hook, rcu); +break ; } - -/* UNREGISTER events are also happening on netns exit. -* -* Although nf_tables core releases all tables/chains, only this event -* handler provides guarantee that hook->ops.dev is still accessible, -* so we cannot skip exiting net namespaces. -*/ -__nft_release_basechain (ctx); } static int nf_tables_netdev_event(struct notifier_block *this,
Signature · 1 line
2.47.1
Phil Sutter
reviewerPATCH v7 6/6 netfilter: nf_tables: Simplify chain netdev notifier
In reply to Phil SutterWith conditional chain deletion gone, callback code simplifies: Instead of filling an nft_ctx object, just pass basechain to the per-chain function. Also plain list_for_each_entry() is safe now.
--- net/netfilter/nft_chain_filter.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-)
net/netfilter/nft_chain_filter.c +7 -14
@@ -319,17 +319,16 @@ static const struct nft_chain_type nft_chain_filter_netdev = { };static void nft_netdev_event (unsigned long event,struct net_device *dev, -struct nft_ctx *ctx) +struct nft_base_chain *basechain) { -struct nft_base_chain *basechain =nft_base_chain (ctx->chain);struct nft_hook *hook;list_for_each_entry (hook, &basechain->hook_list, list) {if (hook->ops.dev != dev)continue ; -if (!(ctx->chain->table->flags &NFT_TABLE_F_DORMANT )) -nf_unregister_net_hook (ctx->net, &hook->ops); +if (!(basechain->chain.table->flags &NFT_TABLE_F_DORMANT )) +nf_unregister_net_hook (dev_net (dev), &hook->ops);list_del_rcu (&hook->list);kfree_rcu (hook, rcu);
@@ -343,25 +342,20 @@ static int nf_tables_netdev_event(struct notifier_block *this,struct net_device *dev =netdev_notifier_info_to_dev (ptr);struct nft_base_chain *basechain;struct nftables_pernet *nft_net; -struct nft_chain *chain, *nr; +struct nft_chain *chain;struct nft_table *table; -struct nft_ctx ctx = { - .net =dev_net (dev), - };if (event !=NETDEV_UNREGISTER )return NOTIFY_DONE ; - nft_net =nft_pernet (ctx.net); + nft_net =nft_pernet (dev_net (dev));mutex_lock (&nft_net->commit_mutex);list_for_each_entry (table, &nft_net->tables, list) {if (table->family !=NFPROTO_NETDEV && table->family !=NFPROTO_INET )continue ; - ctx.family = table->family; - ctx.table = table; -list_for_each_entry_safe (chain, nr, &table->chains, list) { +list_for_each_entry (chain, &table->chains, list) {if (!nft_is_base_chain (chain))continue ;
@@ -370,8 +364,7 @@ static int nf_tables_netdev_event(struct notifier_block *this, basechain->ops.hooknum != NF_INET_INGRESS)continue ; - ctx.chain = chain; -nft_netdev_event (event, dev, &ctx); +nft_netdev_event (event, dev, basechain); } }mutex_unlock (&nft_net->commit_mutex);
Signature · 1 line
2.47.1
Pablo Neira Ayuso
maintainerQuoted 12 lines · Phil Sutter
Changes since v6:
- Rebase onto "netfilter:nf_tables: imbalance in flowtable binding" patch which is in nf-25-01-09 tag but missing in nf-next
- Drop patch 7 which removed__nft_unregister_flowtable_net_hooks(): The function is no longer a duplicate ofnft_netdev_unregister_hooks()This series makes netdev hooks store the interface name spec they were created for and establishes this stored name as the key identifier. The previous one which is the hook's 'ops.dev' pointer is thereby freed to vanish, so a vanishing netdev no longer has to drag the hook along with it. (Patches 2-4)
Quoted 7 lines
Furthermore, it aligns behaviour of netdev-family chains with that of flowtables in situations of vanishing interfaces. When previously a chain losing its last interface was torn down and deleted, it may now remain in place (albeit with no remaining interfaces). (Patch 5)
Patch 6 is a cleanup following patch 5, patch 1 is an independent code simplification.
Series applied to nf-next, thanks Phil.