Dynamic hook interface binding part 2
Subsystems (3)
Phil Sutter
reviewerv6 cover Dynamic hook interface binding part 2
Changes since v5:
- First part split into separate series (applied and present in Linus' git already).
- Add nft_hook_find_ops_rcu() in patch 2 already to reduce size of patch 5.
- New patch 4 to reduce size of patch 5.
- New patch 6 preparing for patch 7 which in turn combines identical changes to both flowtables and netdev chains.
Patches 1-5 prepare for and implement nf_hook_ops lists in nft_hook objects. This is crucial for wildcard interface specs and convenient with dynamic netdev hook registration upon NETDEV_REGISTER events.
Patches 6-9 leverage the new infrastructure to correctly handle NETDEV_REGISTER and NETDEV_CHANGENAME events.
Patch 10 prepares the code for non-NUL-terminated interface names passed by user space which resemble prefixes to match on. As a side-effect, hook allocation code becomes tolerant to non-matching interface specs.
The final two patches implement netlink notifications for netdev add/remove events and add a kselftest.
Phil Sutter (12):
netfilter: nf_tables: Introduce functions freeing nft_hook objects
netfilter: nf_tables: Introduce nft_hook_find_ops{,_rcu}()
netfilter: nf_tables: Introduce nft_register_flowtable_ops()
netfilter: nf_tables: Pass nf_hook_ops to
nft_unregister_flowtable_hook()
netfilter: nf_tables: Have a list of nf_hook_ops in nft_hook
netfilter: nf_tables: Prepare for handling NETDEV_REGISTER events
netfilter: nf_tables: Respect NETDEV_REGISTER events
netfilter: nf_tables: Wrap netdev notifiers
netfilter: nf_tables: Handle NETDEV_CHANGENAME events
netfilter: nf_tables: Support wildcard netdev hook specs
netfilter: nf_tables: Add notications for hook changes
selftests: netfilter: Torture nftables netdev hooks
include/linux/netfilter.h | 3 + include/net/netfilter/nf_tables.h | 12 +- include/uapi/linux/netfilter/nf_tables.h | 10 + net/netfilter/nf_tables_api.c | 394 ++++++++++++++---- net/netfilter/nf_tables_offload.c | 51 ++- net/netfilter/nft_chain_filter.c | 95 ++++- net/netfilter/nft_flow_offload.c | 2 +- .../testing/selftests/net/netfilter/Makefile | 1 + .../net/netfilter/nft_interface_stress.sh | 151 +++++++ 9 files changed, 587 insertions(+), 132 deletions(-) create mode 100755 tools/testing/selftests/net/netfilter/nft_interface_stress.sh
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 1/12 netfilter: nf_tables: Introduce functions freeing nft_hook objects
Pointless wrappers around kfree() for now, prep work for an embedded list of nf_hook_ops.
---
Changes since v3:
- Move function declarations up and use the wrapper in nft_netdev_unregister_hooks(), too.
--- net/netfilter/nf_tables_api.c | 38 ++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-)
net/netfilter/nf_tables_api.c +24 -14
@@ -323,6 +323,16 @@ static int nft_netdev_register_hooks(struct net *net,return err; } +static void nft_netdev_hook_free (struct nft_hook *hook) +{ +kfree (hook); +} + +static void nft_netdev_hook_free_rcu (struct nft_hook *hook) +{ +kfree_rcu (hook, rcu); +} +static void nft_netdev_unregister_hooks (struct net *net,struct list_head *hook_list,bool release_netdev)
@@ -333,7 +343,7 @@ static void nft_netdev_unregister_hooks(struct net *net,nf_unregister_net_hook (net, &hook->ops);if (release_netdev) {list_del (&hook->list); -kfree_rcu (hook, rcu); +nft_netdev_hook_free_rcu (hook); } } }
@@ -2253,7 +2263,7 @@ void nf_tables_chain_destroy(struct nft_chain *chain)list_for_each_entry_safe (hook, next, &basechain->hook_list, list) {list_del_rcu (&hook->list); -kfree_rcu (hook, rcu); +nft_netdev_hook_free_rcu (hook); } }module_put (basechain->type->owner);
@@ -2345,7 +2355,7 @@ static int nf_tables_parse_netdev_hooks(struct net *net, }if (nft_hook_list_find (hook_list, hook)) {NL_SET_BAD_ATTR (extack, tmp); -kfree (hook); +nft_netdev_hook_free (hook); err = -EEXIST ; goto err_hook; }
@@ -2363,7 +2373,7 @@ static int nf_tables_parse_netdev_hooks(struct net *net, err_hook:list_for_each_entry_safe (hook, next, hook_list, list) {list_del (&hook->list); -kfree (hook); +nft_netdev_hook_free (hook); }return err; }
@@ -2506,7 +2516,7 @@ static void nft_chain_release_hook(struct nft_chain_hook *hook)list_for_each_entry_safe (h, next, &hook->list, list) {list_del (&h->list); -kfree (h); +nft_netdev_hook_free (h); }module_put (hook->type->owner); }
@@ -2795,7 +2805,7 @@ static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy,if (nft_hook_list_find (&basechain->hook_list, h)) {list_del (&h->list); -kfree (h); +nft_netdev_hook_free (h); } } }else {
@@ -2916,7 +2926,7 @@ static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy,if (unregister)nf_unregister_net_hook (ctx->net, &h->ops);list_del (&h->list); -kfree_rcu (h, rcu); +nft_netdev_hook_free_rcu (h); }module_put (hook.type->owner); }
@@ -8881,7 +8891,7 @@ static void __nft_unregister_flowtable_net_hooks(struct net *net, FLOW_BLOCK_UNBIND);if (release_netdev) {list_del (&hook->list); -kfree_rcu (hook, rcu); +nft_netdev_hook_free_rcu (hook); } } }
@@ -8939,7 +8949,7 @@ static int nft_register_flowtable_net_hooks(struct net *net,nft_unregister_flowtable_hook (net, flowtable, hook);list_del_rcu (&hook->list); -kfree_rcu (hook, rcu); +nft_netdev_hook_free_rcu (hook); }return err;
@@ -8951,7 +8961,7 @@ static void nft_hooks_destroy(struct list_head *hook_list)list_for_each_entry_safe (hook, next, hook_list, list) {list_del_rcu (&hook->list); -kfree_rcu (hook, rcu); +nft_netdev_hook_free_rcu (hook); } }
@@ -8975,7 +8985,7 @@ static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,list_for_each_entry_safe (hook, next, &flowtable_hook.list, list) {if (nft_hook_list_find (&flowtable->hook_list, hook)) {list_del (&hook->list); -kfree (hook); +nft_netdev_hook_free (hook); } }
@@ -9022,7 +9032,7 @@ static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,if (unregister)nft_unregister_flowtable_hook (ctx->net, flowtable, hook);list_del_rcu (&hook->list); -kfree_rcu (hook, rcu); +nft_netdev_hook_free_rcu (hook); }return err;
@@ -9168,7 +9178,7 @@ static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hooklist_for_each_entry_safe (this, next, &flowtable_hook->list, list) {list_del (&this->list); -kfree (this); +nft_netdev_hook_free (this); } }
@@ -9531,7 +9541,7 @@ static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) flowtable->data.type->free (&flowtable->data);list_for_each_entry_safe (hook, next, &flowtable->hook_list, list) {list_del_rcu (&hook->list); -kfree_rcu (hook, rcu); +nft_netdev_hook_free_rcu (hook); }kfree (flowtable->name);module_put (flowtable->data.type->owner);
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 2/12 netfilter: nf_tables: Introduce nft_hook_find_ops{,_rcu}()
In reply to Phil SutterAlso a pretty dull wrapper around the hook->ops.dev comparison for now. Will search the embedded nf_hook_ops list in future. The ugly cast to eliminate the const qualifier will vanish then, too.
Since this future list will be RCU-protected, also introduce an _rcu() variant here.
---
Changes since v5:
- Add nft_hook_find_ops_rcu() now rather than later although it is merely a wrapper around nft_hook_find_ops() for now.
--- include/net/netfilter/nf_tables.h | 5 +++++ net/netfilter/nf_tables_api.c | 21 ++++++++++++++++++++- net/netfilter/nf_tables_offload.c | 2 +- net/netfilter/nft_chain_filter.c | 6 ++++-- net/netfilter/nft_flow_offload.c | 2 +- 5 files changed, 31 insertions(+), 5 deletions(-)
include/net/netfilter/nf_tables.h +5 -0
@@ -1205,6 +1205,11 @@ struct nft_hook {u8 ifnamelen; }; +struct nf_hook_ops *nft_hook_find_ops (const struct nft_hook *hook, +const struct net_device *dev); +struct nf_hook_ops *nft_hook_find_ops_rcu (const struct nft_hook *hook, +const struct net_device *dev); +/** * struct nft_base_chain - nf_tables base chain *
net/netfilter/nf_tables_api.c +20 -1
@@ -9574,13 +9574,32 @@ static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,return -EMSGSIZE ; } +struct nf_hook_ops *nft_hook_find_ops (const struct nft_hook *hook, +const struct net_device *dev) +{ +if (hook->ops.dev == dev) +return (struct nf_hook_ops *)&hook->ops; + +return NULL ; +} +EXPORT_SYMBOL_GPL (nft_hook_find_ops); + +struct nf_hook_ops *nft_hook_find_ops_rcu (const struct nft_hook *hook, +const struct net_device *dev) +{ +return nft_hook_find_ops (hook, dev); +} +EXPORT_SYMBOL_GPL (nft_hook_find_ops_rcu); +static void nft_flowtable_event (unsigned long event,struct net_device *dev,struct nft_flowtable *flowtable) { +struct nf_hook_ops *ops;struct nft_hook *hook;list_for_each_entry (hook, &flowtable->hook_list, list) { -if (hook->ops.dev != dev) + ops =nft_hook_find_ops (hook, dev); +if (!ops)continue ;/* flow_offload_netdev_event() cleans up entries for us. */
net/netfilter/nf_tables_offload.c +1 -1
@@ -638,7 +638,7 @@ static struct nft_chain *__nft_offload_get_chain(const struct nftables_pernet *n found =NULL ; basechain =nft_base_chain (chain);list_for_each_entry (hook, &basechain->hook_list, list) { -if (hook->ops.dev != dev) +if (!nft_hook_find_ops (hook, dev))continue ; found = hook;
net/netfilter/nft_chain_filter.c +4 -2
@@ -321,14 +321,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_base_chain *basechain) { +struct nf_hook_ops *ops;struct nft_hook *hook;list_for_each_entry (hook, &basechain->hook_list, list) { -if (hook->ops.dev != dev) + ops =nft_hook_find_ops (hook, dev); +if (!ops)continue ;if (!(basechain->chain.table->flags &NFT_TABLE_F_DORMANT )) -nf_unregister_net_hook (dev_net (dev), &hook->ops); +nf_unregister_net_hook (dev_net (dev), ops);list_del_rcu (&hook->list);kfree_rcu (hook, rcu);
net/netfilter/nft_flow_offload.c +1 -1
@@ -175,7 +175,7 @@ static bool nft_flowtable_find_dev(const struct net_device *dev,bool found = false;list_for_each_entry_rcu (hook, &ft->hook_list, list) { -if (hook->ops.dev != dev) +if (!nft_hook_find_ops_rcu (hook, dev))continue ; found = true;
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 3/12 netfilter: nf_tables: Introduce nft_register_flowtable_ops()
In reply to Phil SutterFacilitate binding and registering of a flowtable hook via a single function call.
--- net/netfilter/nf_tables_api.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-)
net/netfilter/nf_tables_api.c +21 -11
@@ -8903,6 +8903,26 @@ static void nft_unregister_flowtable_net_hooks(struct net *net,__nft_unregister_flowtable_net_hooks (net, flowtable, hook_list, false); } +static int nft_register_flowtable_ops (struct net *net, +struct nft_flowtable *flowtable, +struct nf_hook_ops *ops) +{ +int err; + + err = flowtable->data.type->setup (&flowtable->data, + ops->dev,FLOW_BLOCK_BIND ); +if (err <0 ) +return err; + + err =nf_register_net_hook (net, ops); +if (!err) +return 0 ; + + flowtable->data.type->setup (&flowtable->data, + ops->dev,FLOW_BLOCK_UNBIND ); +return err; +} + static int nft_register_flowtable_net_hooks(struct net *net, struct nft_table *table, struct list_head *hook_list,
@@ -8923,20 +8943,10 @@ static int nft_register_flowtable_net_hooks(struct net *net, } } - err = flowtable->data.type->setup (&flowtable->data, - hook->ops.dev, -FLOW_BLOCK_BIND ); + err =nft_register_flowtable_ops (net, flowtable, &hook->ops);if (err <0 ) goto err_unregister_net_hooks; - err =nf_register_net_hook (net, &hook->ops); -if (err <0 ) { - flowtable->data.type->setup (&flowtable->data, - hook->ops.dev, -FLOW_BLOCK_UNBIND ); - goto err_unregister_net_hooks; - } - i++; }
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 4/12 netfilter: nf_tables: Pass nf_hook_ops to nft_unregister_flowtable_hook()
In reply to Phil SutterThe function accesses only the hook's ops field, pass it directly. This prepares for nft_hooks holding a list of nf_hook_ops in future.
While at it, make use of the function in__nft_unregister_flowtable_net_hooks() as well.
---
Changes since v5:
- New patch.
--- net/netfilter/nf_tables_api.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
net/netfilter/nf_tables_api.c +9 -11
@@ -8869,12 +8869,12 @@ nft_flowtable_type_get(struct net *net, u8 family) }/* Only called from error and netdev event paths. */ -static void nft_unregister_flowtable_hook (struct net *net, -struct nft_flowtable *flowtable, -struct nft_hook *hook) +static void nft_unregister_flowtable_ops (struct net *net, +struct nft_flowtable *flowtable, +struct nf_hook_ops *ops) { -nf_unregister_net_hook (net, &hook->ops); - flowtable->data.type->setup (&flowtable->data, hook->ops.dev, +nf_unregister_net_hook (net, ops); + flowtable->data.type->setup (&flowtable->data, ops->dev,FLOW_BLOCK_UNBIND ); }
@@ -8886,9 +8886,7 @@ static void __nft_unregister_flowtable_net_hooks(struct net *net,struct nft_hook *hook, *next;list_for_each_entry_safe (hook, next, hook_list, list) { -nf_unregister_net_hook (net, &hook->ops); - flowtable->data.type->setup (&flowtable->data, hook->ops.dev, -FLOW_BLOCK_UNBIND ); +nft_unregister_flowtable_ops (net, flowtable, &hook->ops);if (release_netdev) {list_del (&hook->list);nft_netdev_hook_free_rcu (hook);
@@ -8957,7 +8955,7 @@ static int nft_register_flowtable_net_hooks(struct net *net,if (i-- <=0 )break ; -nft_unregister_flowtable_hook (net, flowtable, hook); +nft_unregister_flowtable_ops (net, flowtable, &hook->ops);list_del_rcu (&hook->list);nft_netdev_hook_free_rcu (hook); }
@@ -9040,7 +9038,7 @@ static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh, err_flowtable_update_hook:list_for_each_entry_safe (hook, next, &flowtable_hook.list, list) {if (unregister) -nft_unregister_flowtable_hook (ctx->net, flowtable, hook); +nft_unregister_flowtable_ops (ctx->net, flowtable, &hook->ops);list_del_rcu (&hook->list);nft_netdev_hook_free_rcu (hook); }
@@ -9613,7 +9611,7 @@ static void nft_flowtable_event(unsigned long event, struct net_device *dev,continue ;/* flow_offload_netdev_event() cleans up entries for us. */ -nft_unregister_flowtable_hook (dev_net (dev), flowtable, hook); +nft_unregister_flowtable_ops (dev_net (dev), flowtable, ops);list_del_rcu (&hook->list);kfree_rcu (hook, rcu);break ;
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 5/12 netfilter: nf_tables: Have a list of nf_hook_ops in nft_hook
In reply to Phil SutterSupporting a 1:n relationship between nft_hook and nf_hook_ops is convenient since a chain's or flowtable's nft_hooks may remain in place despite matching interfaces disappearing. This stabilizes ruleset dumps in that regard and opens the possibility to claim newly added interfaces which match the spec. Also it prepares for wildcard interface specs since these will potentially match multiple interfaces.
All spots dealing with hook registration are updated to handle a list of multiple nf_hook_ops, but nft_netdev_hook_alloc() only adds a single item for now to retain the old behaviour. The only expected functional change here is how vanishing interfaces are handled: Instead of dropping the respective nft_hook, only the matching nf_hook_ops are dropped.
To safely remove individual ops from the list in netdev handlers, an rcu_head is added to struct nf_hook_ops so kfree_rcu() may be used. There is at least nft_flowtable_find_dev() which may be iterating through the list at the same time.
---
Changes since v3:
- Add an rcu_head to nf_hook_ops.
- RCU-free an nft_hook along with its ops_list via call_rcu() and a callback.
--- include/linux/netfilter.h | 3 + include/net/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 142 ++++++++++++++++++++++-------- net/netfilter/nf_tables_offload.c | 49 ++++++----- net/netfilter/nft_chain_filter.c | 5 +- 5 files changed, 137 insertions(+), 64 deletions(-)
include/linux/netfilter.h +3 -0
@@ -95,6 +95,9 @@ enum nf_hook_ops_type { };struct nf_hook_ops { +struct list_head list; +struct rcu_head rcu; +/* User fills in from here down. */ nf_hookfn *hook;struct net_device *dev;
include/net/netfilter/nf_tables.h +1 -1
@@ -1199,7 +1199,7 @@ struct nft_stats {struct nft_hook {struct list_head list; -struct nf_hook_ops ops; +struct list_head ops_list;struct rcu_head rcu;char ifname[IFNAMSIZ ];u8 ifnamelen;
net/netfilter/nf_tables_api.c +103 -39
293 lines not on this page · raw message
net/netfilter/nf_tables_offload.c +28 -21
81 lines not on this page · raw message
net/netfilter/nft_chain_filter.c +2 -3
@@ -332,9 +332,8 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,if (!(basechain->chain.table->flags &NFT_TABLE_F_DORMANT ))nf_unregister_net_hook (dev_net (dev), ops); -list_del_rcu (&hook->list); -kfree_rcu (hook, rcu); -break ; +list_del_rcu (&ops->list); +kfree_rcu (ops, rcu); } }
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 6/12 netfilter: nf_tables: Prepare for handling NETDEV_REGISTER events
In reply to Phil SutterPut NETDEV_UNREGISTER handling code into a switch, no functional change intended as the function is only called for that event yet.
---
Changes since v5:
- New patch.
--- net/netfilter/nf_tables_api.c | 19 ++++++++++++------- net/netfilter/nft_chain_filter.c | 19 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-)
net/netfilter/nf_tables_api.c +12 -7
@@ -9670,14 +9670,19 @@ static void nft_flowtable_event(unsigned long event, struct net_device *dev,struct nft_hook *hook;list_for_each_entry (hook, &flowtable->hook_list, list) { - ops =nft_hook_find_ops (hook, dev); -if (!ops) -continue ; +switch (event) { +case NETDEV_UNREGISTER : + ops =nft_hook_find_ops (hook, dev); +if (!ops) +continue ; -/* flow_offload_netdev_event() cleans up entries for us. */ -nft_unregister_flowtable_ops (dev_net (dev), flowtable, ops); -list_del_rcu (&ops->list); -kfree_rcu (ops, rcu); +/* flow_offload_netdev_event() cleans up entries for us. */ +nft_unregister_flowtable_ops (dev_net (dev), + flowtable, ops); +list_del_rcu (&ops->list); +kfree_rcu (ops, rcu); +break ; + }break ; } }
net/netfilter/nft_chain_filter.c +12 -7
@@ -321,19 +321,24 @@ static const struct nft_chain_type nft_chain_filter_netdev = {static void nft_netdev_event (unsigned long event,struct net_device *dev,struct nft_base_chain *basechain) { +struct nft_table *table = basechain->chain.table;struct nf_hook_ops *ops;struct nft_hook *hook;list_for_each_entry (hook, &basechain->hook_list, list) { - ops =nft_hook_find_ops (hook, dev); -if (!ops) -continue ; +switch (event) { +case NETDEV_UNREGISTER : + ops =nft_hook_find_ops (hook, dev); +if (!ops) +continue ; -if (!(basechain->chain.table->flags &NFT_TABLE_F_DORMANT )) -nf_unregister_net_hook (dev_net (dev), ops); +if (!(table->flags &NFT_TABLE_F_DORMANT )) +nf_unregister_net_hook (dev_net (dev), ops); -list_del_rcu (&ops->list); -kfree_rcu (ops, rcu); +list_del_rcu (&ops->list); +kfree_rcu (ops, rcu); +break ; + } } }
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 7/12 netfilter: nf_tables: Respect NETDEV_REGISTER events
In reply to Phil SutterHook into new devices if their name matches the hook spec.
---
Changes since v5:
- Prep split into separate patch.
- Merged separate patches for netdev chains and flowtables.
Changes since v3:
- Use list_add_tail_rcu() to avoid breaking readers.
- Use kmemdup() instead of kzalloc() && memcpy() as per Florian.
- Return NOTIFY_BAD upon error instead of printing an error message, also suggested by Florian.
--- net/netfilter/nf_tables_api.c | 37 +++++++++++++++++++++++++++----- net/netfilter/nft_chain_filter.c | 32 +++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 9 deletions(-)
net/netfilter/nf_tables_api.c +32 -5
@@ -9663,8 +9663,8 @@ struct nf_hook_ops *nft_hook_find_ops_rcu(const struct nft_hook *hook, }EXPORT_SYMBOL_GPL (nft_hook_find_ops_rcu); -static void nft_flowtable_event (unsigned long event,struct net_device *dev, -struct nft_flowtable *flowtable) +static int nft_flowtable_event (unsigned long event,struct net_device *dev, +struct nft_flowtable *flowtable) {struct nf_hook_ops *ops;struct nft_hook *hook;
@@ -9682,9 +9682,32 @@ static void nft_flowtable_event(unsigned long event, struct net_device *dev,list_del_rcu (&ops->list);kfree_rcu (ops, rcu);break ; +case NETDEV_REGISTER : +if (strcmp (hook->ifname, dev->name)) +continue ; + + ops =kzalloc (sizeof (struct nf_hook_ops ), +GFP_KERNEL_ACCOUNT ); +if (!ops) +return 1 ; + + ops->pf =NFPROTO_NETDEV ; + ops->hooknum = flowtable->hooknum; + ops->priority = flowtable->data.priority; + ops->priv = &flowtable->data; + ops->hook = flowtable->data.type->hook; + ops->dev = dev; +if (nft_register_flowtable_ops (dev_net (dev), + flowtable, ops)) { +kfree (ops); +return 1 ; + } +list_add_tail_rcu (&ops->list, &hook->ops_list); +break ; }break ; } +return 0 ; } static int nf_tables_flowtable_event(struct notifier_block *this,
@@ -9696,15 +9719,19 @@ static int nf_tables_flowtable_event(struct notifier_block *this,struct nft_table *table;struct net *net; -if (event !=NETDEV_UNREGISTER ) -return 0 ; +if (event !=NETDEV_REGISTER && + event !=NETDEV_UNREGISTER ) +return NOTIFY_DONE ; net =dev_net (dev); nft_net =nft_pernet (net);mutex_lock (&nft_net->commit_mutex);list_for_each_entry (table, &nft_net->tables, list) {list_for_each_entry (flowtable, &table->flowtables, list) { -nft_flowtable_event (event, dev, flowtable); +if (nft_flowtable_event (event, dev, flowtable)) { +mutex_unlock (&nft_net->commit_mutex); +return NOTIFY_BAD ; + } } }mutex_unlock (&nft_net->commit_mutex);
net/netfilter/nft_chain_filter.c +28 -4
@@ -318,8 +318,8 @@ static const struct nft_chain_type nft_chain_filter_netdev = { }, }; -static void nft_netdev_event (unsigned long event,struct net_device *dev, -struct nft_base_chain *basechain) +static int nft_netdev_event (unsigned long event,struct net_device *dev, +struct nft_base_chain *basechain) {struct nft_table *table = basechain->chain.table;struct nf_hook_ops *ops;
@@ -338,8 +338,28 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,list_del_rcu (&ops->list);kfree_rcu (ops, rcu);break ; +case NETDEV_REGISTER : +if (strcmp (hook->ifname, dev->name)) +continue ; + + ops =kmemdup (&basechain->ops, +sizeof (struct nf_hook_ops ), +GFP_KERNEL_ACCOUNT ); +if (!ops) +return 1 ; + + ops->dev = dev; + +if (!(table->flags &NFT_TABLE_F_DORMANT ) && +nf_register_net_hook (dev_net (dev), ops)) { +kfree (ops); +return 1 ; + } +list_add_tail_rcu (&ops->list, &hook->ops_list); +break ; } } +return 0 ; } static int nf_tables_netdev_event(struct notifier_block *this,
@@ -351,7 +371,8 @@ static int nf_tables_netdev_event(struct notifier_block *this,struct nft_chain *chain;struct nft_table *table; -if (event !=NETDEV_UNREGISTER ) +if (event !=NETDEV_REGISTER && + event !=NETDEV_UNREGISTER )return NOTIFY_DONE ; nft_net =nft_pernet (dev_net (dev));
@@ -370,7 +391,10 @@ static int nf_tables_netdev_event(struct notifier_block *this, basechain->ops.hooknum != NF_INET_INGRESS)continue ; -nft_netdev_event (event, dev, basechain); +if (nft_netdev_event (event, dev, basechain)) { +mutex_unlock (&nft_net->commit_mutex); +return NOTIFY_BAD ; + } } }mutex_unlock (&nft_net->commit_mutex);
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 8/12 netfilter: nf_tables: Wrap netdev notifiers
In reply to Phil SutterHandling NETDEV_CHANGENAME events has to traverse all chains/flowtables twice, prepare for this. No functional change intended.
--- net/netfilter/nf_tables_api.c | 34 ++++++++++++++++++---------- net/netfilter/nft_chain_filter.c | 38 ++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 26 deletions(-)
net/netfilter/nf_tables_api.c +22 -12
@@ -9710,13 +9710,28 @@ static int nft_flowtable_event(unsigned long event, struct net_device *dev,return 0 ; } +static int __nf_tables_flowtable_event (unsigned long event, +struct net_device *dev) +{ +struct nftables_pernet *nft_net =nft_pernet (dev_net (dev)); +struct nft_flowtable *flowtable; +struct nft_table *table; + +list_for_each_entry (table, &nft_net->tables, list) { +list_for_each_entry (flowtable, &table->flowtables, list) { +if (nft_flowtable_event (event, dev, flowtable)) +return 1 ; + } + } +return 0 ; +} +static int nf_tables_flowtable_event (struct notifier_block *this,unsigned long event,void *ptr) {struct net_device *dev =netdev_notifier_info_to_dev (ptr); -struct nft_flowtable *flowtable;struct nftables_pernet *nft_net; -struct nft_table *table; +int ret =NOTIFY_DONE ;struct net *net; if (event != NETDEV_REGISTER &&
@@ -9726,17 +9741,12 @@ static int nf_tables_flowtable_event(struct notifier_block *this, net =dev_net (dev); nft_net =nft_pernet (net);mutex_lock (&nft_net->commit_mutex); -list_for_each_entry (table, &nft_net->tables, list) { -list_for_each_entry (flowtable, &table->flowtables, list) { -if (nft_flowtable_event (event, dev, flowtable)) { -mutex_unlock (&nft_net->commit_mutex); -return NOTIFY_BAD ; - } - } - } -mutex_unlock (&nft_net->commit_mutex); -return NOTIFY_DONE ; +if (__nf_tables_flowtable_event (event, dev)) + ret =NOTIFY_BAD ; + +mutex_unlock (&nft_net->commit_mutex); +return ret; }static struct notifier_block nf_tables_flowtable_notifier = {
net/netfilter/nft_chain_filter.c +24 -14
@@ -362,21 +362,14 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,return 0 ; } -static int nf_tables_netdev_event (struct notifier_block *this, -unsigned long event,void *ptr) +static int __nf_tables_netdev_event (unsigned long event,struct net_device *dev) { -struct net_device *dev =netdev_notifier_info_to_dev (ptr);struct nft_base_chain *basechain;struct nftables_pernet *nft_net;struct nft_chain *chain;struct nft_table *table; -if (event !=NETDEV_REGISTER && - event !=NETDEV_UNREGISTER ) -return NOTIFY_DONE ; - 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 )
@@ -391,15 +384,32 @@ static int nf_tables_netdev_event(struct notifier_block *this, basechain->ops.hooknum != NF_INET_INGRESS)continue ; -if (nft_netdev_event (event, dev, basechain)) { -mutex_unlock (&nft_net->commit_mutex); -return NOTIFY_BAD ; - } +if (nft_netdev_event (event, dev, basechain)) +return 1 ; } } -mutex_unlock (&nft_net->commit_mutex); +return 0 ; +} + +static int nf_tables_netdev_event (struct notifier_block *this, +unsigned long event,void *ptr) +{ +struct net_device *dev =netdev_notifier_info_to_dev (ptr); +struct nftables_pernet *nft_net; +int ret =NOTIFY_DONE ; + +if (event !=NETDEV_REGISTER && + event !=NETDEV_UNREGISTER ) +return NOTIFY_DONE ; -return NOTIFY_DONE ; + nft_net =nft_pernet (dev_net (dev)); +mutex_lock (&nft_net->commit_mutex); + +if (__nf_tables_netdev_event (event, dev)) + ret =NOTIFY_BAD ; + +mutex_unlock (&nft_net->commit_mutex); +return ret; }static struct notifier_block nf_tables_netdev_notifier = {
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 9/12 netfilter: nf_tables: Handle NETDEV_CHANGENAME events
In reply to Phil SutterFor the sake of simplicity, treat them like consecutive NETDEV_REGISTER and NETDEV_UNREGISTER events. If the new name matches a hook spec and registration fails, escalate the error and keep things as they are.
To avoid unregistering the newly registered hook again during the following fake NETDEV_UNREGISTER event, leave hooks alone if their interface spec matches the new name.
Note how this patch also skips for NETDEV_REGISTER if the device is already registered. This is not yet possible as the new name would have to match the old one. This will change with wildcard interface specs, though.
---
Changes since v4:
- Avoid unregistering the new ops along with the old one(s) by accident.
Changes since v3:
- Register first and handle errors to avoid having unregistered the device but registration fails.
--- net/netfilter/nf_tables_api.c | 33 +++++++++++++++++++++++--------- net/netfilter/nft_chain_filter.c | 33 +++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 18 deletions(-)
net/netfilter/nf_tables_api.c +24 -9
@@ -9664,16 +9664,20 @@ struct nf_hook_ops *nft_hook_find_ops_rcu(const struct nft_hook *hook,EXPORT_SYMBOL_GPL (nft_hook_find_ops_rcu);static int nft_flowtable_event (unsigned long event,struct net_device *dev, -struct nft_flowtable *flowtable) +struct nft_flowtable *flowtable,bool changename) {struct nf_hook_ops *ops;struct nft_hook *hook; +bool match;list_for_each_entry (hook, &flowtable->hook_list, list) { + ops =nft_hook_find_ops (hook, dev); + match = !strcmp (hook->ifname, dev->name); +switch (event) {case NETDEV_UNREGISTER : - ops =nft_hook_find_ops (hook, dev); -if (!ops) +/* NOP if not found or new name still matching */ +if (!ops || (changename && match))continue ;/* flow_offload_netdev_event() cleans up entries for us. */
@@ -9683,7 +9687,8 @@ static int nft_flowtable_event(unsigned long event, struct net_device *dev,kfree_rcu (ops, rcu);break ;case NETDEV_REGISTER : -if (strcmp (hook->ifname, dev->name)) +/* NOP if not matching or already registered */ +if (!match || (changename && ops))continue ; ops = kzalloc(sizeof(struct nf_hook_ops),
@@ -9711,7 +9716,8 @@ static int nft_flowtable_event(unsigned long event, struct net_device *dev, }static int __nf_tables_flowtable_event (unsigned long event, -struct net_device *dev) +struct net_device *dev, +bool changename) {struct nftables_pernet *nft_net =nft_pernet (dev_net (dev));struct nft_flowtable *flowtable;
@@ -9719,7 +9725,8 @@ static int __nf_tables_flowtable_event(unsigned long event,list_for_each_entry (table, &nft_net->tables, list) {list_for_each_entry (flowtable, &table->flowtables, list) { -if (nft_flowtable_event (event, dev, flowtable)) +if (nft_flowtable_event (event, dev, + flowtable, changename))return 1 ; } }
@@ -9735,16 +9742,24 @@ static int nf_tables_flowtable_event(struct notifier_block *this,struct net *net;if (event !=NETDEV_REGISTER && - event !=NETDEV_UNREGISTER ) + event !=NETDEV_UNREGISTER && + event !=NETDEV_CHANGENAME )return NOTIFY_DONE ; net =dev_net (dev); nft_net =nft_pernet (net);mutex_lock (&nft_net->commit_mutex); -if (__nf_tables_flowtable_event (event, dev)) +if (event ==NETDEV_CHANGENAME ) { +if (__nf_tables_flowtable_event (NETDEV_REGISTER , dev, true)) { + ret =NOTIFY_BAD ; + goto out_unlock; + } +__nf_tables_flowtable_event (NETDEV_UNREGISTER , dev, true); + }else if (__nf_tables_flowtable_event (event, dev, false)) { ret =NOTIFY_BAD ; - + } +out_unlock:mutex_unlock (&nft_net->commit_mutex);return ret; }
net/netfilter/nft_chain_filter.c +24 -9
@@ -319,17 +319,21 @@ static const struct nft_chain_type nft_chain_filter_netdev = { };static int nft_netdev_event (unsigned long event,struct net_device *dev, -struct nft_base_chain *basechain) +struct nft_base_chain *basechain,bool changename) {struct nft_table *table = basechain->chain.table;struct nf_hook_ops *ops;struct nft_hook *hook; +bool match;list_for_each_entry (hook, &basechain->hook_list, list) { + ops =nft_hook_find_ops (hook, dev); + match = !strcmp (hook->ifname, dev->name); +switch (event) {case NETDEV_UNREGISTER : - ops =nft_hook_find_ops (hook, dev); -if (!ops) +/* NOP if not found or new name still matching */ +if (!ops || (changename && match))continue ;if (!(table->flags &NFT_TABLE_F_DORMANT ))
@@ -339,7 +343,8 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,kfree_rcu (ops, rcu);break ;case NETDEV_REGISTER : -if (strcmp (hook->ifname, dev->name)) +/* NOP if not matching or already registered */ +if (!match || (changename && ops))continue ; ops = kmemdup(&basechain->ops,
@@ -362,7 +367,9 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,return 0 ; } -static int __nf_tables_netdev_event (unsigned long event,struct net_device *dev) +static int __nf_tables_netdev_event (unsigned long event, +struct net_device *dev, +bool changename) {struct nft_base_chain *basechain;struct nftables_pernet *nft_net;
@@ -384,7 +391,7 @@ static int __nf_tables_netdev_event(unsigned long event, struct net_device *dev) basechain->ops.hooknum != NF_INET_INGRESS)continue ; -if (nft_netdev_event (event, dev, basechain)) +if (nft_netdev_event (event, dev, basechain, changename))return 1 ; } }
@@ -399,15 +406,23 @@ static int nf_tables_netdev_event(struct notifier_block *this,int ret =NOTIFY_DONE ;if (event !=NETDEV_REGISTER && - event !=NETDEV_UNREGISTER ) + event !=NETDEV_UNREGISTER && + event !=NETDEV_CHANGENAME )return NOTIFY_DONE ; nft_net =nft_pernet (dev_net (dev));mutex_lock (&nft_net->commit_mutex); -if (__nf_tables_netdev_event (event, dev)) +if (event ==NETDEV_CHANGENAME ) { +if (__nf_tables_netdev_event (NETDEV_REGISTER , dev, true)) { + ret =NOTIFY_BAD ; + goto out_unlock; + } +__nf_tables_netdev_event (NETDEV_UNREGISTER , dev, true); + }else if (__nf_tables_netdev_event (event, dev, false)) { ret =NOTIFY_BAD ; - + } +out_unlock:mutex_unlock (&nft_net->commit_mutex);return ret; }
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 10/12 netfilter: nf_tables: Support wildcard netdev hook specs
In reply to Phil SutterUser space may pass non-nul-terminated NFTA_DEVICE_NAME attribute values to indicate a suffix wildcard.
Expect for multiple devices to match the given prefix in nft_netdev_hook_alloc() and populate 'ops_list' with them all. When checking for duplicate hooks, compare the shortest prefix so a device may never match more than a single hook spec.
Finally respect the stored prefix length when hooking into new devices from event handlers.
--- net/netfilter/nf_tables_api.c | 33 ++++++++++++++++---------------- net/netfilter/nft_chain_filter.c | 2 +- 2 files changed, 17 insertions(+), 18 deletions(-)
net/netfilter/nf_tables_api.c +16 -17
@@ -2323,7 +2323,7 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net, err =nla_strscpy (hook->ifname, attr,IFNAMSIZ );if (err <0 ) - goto err_hook_dev; + goto err_ops_alloc; hook->ifnamelen =nla_len (attr);
@@ -2331,24 +2331,22 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net,* indirectly serializing all the other holders of the commit_mutex with * the rtnl_mutex. */ - dev =__dev_get_by_name (net, hook->ifname); -if (!dev) { - err = -ENOENT ; - goto err_hook_dev; - } +for_each_netdev (net, dev) { +if (strncmp (dev->name, hook->ifname, hook->ifnamelen)) +continue ; - ops =kzalloc (sizeof (struct nf_hook_ops ),GFP_KERNEL_ACCOUNT ); -if (!ops) { - err = -ENOMEM ; - goto err_hook_dev; + ops =kzalloc (sizeof (struct nf_hook_ops ),GFP_KERNEL_ACCOUNT ); +if (!ops) { + err = -ENOMEM ; + goto err_ops_alloc; + } + ops->dev = dev; +list_add_tail (&ops->list, &hook->ops_list); } - ops->dev = dev; -list_add_tail (&ops->list, &hook->ops_list); -return hook; -err_hook_dev: -kfree (hook); +err_ops_alloc: +nft_netdev_hook_free (hook); err_hook_alloc:return ERR_PTR (err); }
@@ -2359,7 +2357,8 @@ 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 (!strcmp (hook->ifname, this->ifname)) +if (!strncmp (hook->ifname, this->ifname, +min (hook->ifnamelen, this->ifnamelen)))return hook; }
@@ -9672,7 +9671,7 @@ static int nft_flowtable_event(unsigned long event, struct net_device *dev,list_for_each_entry (hook, &flowtable->hook_list, list) { ops =nft_hook_find_ops (hook, dev); - match = !strcmp (hook->ifname, dev->name); + match = !strncmp (hook->ifname, dev->name, hook->ifnamelen);switch (event) {case NETDEV_UNREGISTER :
net/netfilter/nft_chain_filter.c +1 -1
@@ -328,7 +328,7 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,list_for_each_entry (hook, &basechain->hook_list, list) { ops =nft_hook_find_ops (hook, dev); - match = !strcmp (hook->ifname, dev->name); + match = !strncmp (hook->ifname, dev->name, hook->ifnamelen);switch (event) {case NETDEV_UNREGISTER :
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 11/12 netfilter: nf_tables: Add notications for hook changes
In reply to Phil SutterNotify user space if netdev hooks are updated due to netdev add/remove events. Send minimal notification messages by introducing NFT_MSG_NEWDEV/DELDEV message types describing a single device only.
Upon NETDEV_CHANGENAME, the callback has no information about the interface's old name. To provide a clear message to user space, include the hook's stored interface name in the notification.
---
Changes since v4:
- Introduce NFTA_DEVICE_SPEC to contain the hook's stored ifname
--- include/net/netfilter/nf_tables.h | 5 ++ include/uapi/linux/netfilter/nf_tables.h | 10 ++++ net/netfilter/nf_tables_api.c | 59 ++++++++++++++++++++++++ net/netfilter/nft_chain_filter.c | 2 + 4 files changed, 76 insertions(+)
include/net/netfilter/nf_tables.h +5 -0
@@ -1142,6 +1142,11 @@ 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); +struct nft_hook ; +void nf_tables_chain_device_notify (const struct nft_chain *chain, +const struct nft_hook *hook, +const struct net_device *dev,int event); +enum nft_chain_types {NFT_CHAIN_T_DEFAULT =0 ,NFT_CHAIN_T_ROUTE ,
include/uapi/linux/netfilter/nf_tables.h +10 -0
@@ -142,6 +142,8 @@ enum nf_tables_msg_types {NFT_MSG_DESTROYOBJ ,NFT_MSG_DESTROYFLOWTABLE ,NFT_MSG_GETSETELEM_RESET , +NFT_MSG_NEWDEV , +NFT_MSG_DELDEV , NFT_MSG_MAX, };
@@ -1780,10 +1782,18 @@ enum nft_synproxy_attributes {* enum nft_device_attributes - nf_tables device netlink attributes * * @NFTA_DEVICE_NAME: name of this device (NLA_STRING) +* @NFTA_DEVICE_TABLE: table containing the flowtable or chain hooking into the device (NLA_STRING) +* @NFTA_DEVICE_FLOWTABLE: flowtable hooking into the device (NLA_STRING) +* @NFTA_DEVICE_CHAIN: chain hooking into the device (NLA_STRING) +* @NFTA_DEVICE_SPEC: hook spec matching the device (NLA_STRING) */ enum nft_devices_attributes {NFTA_DEVICE_UNSPEC ,NFTA_DEVICE_NAME , +NFTA_DEVICE_TABLE , +NFTA_DEVICE_FLOWTABLE , +NFTA_DEVICE_CHAIN , +NFTA_DEVICE_SPEC , __NFTA_DEVICE_MAX };#define NFTA_DEVICE_MAX (__NFTA_DEVICE_MAX - 1)
net/netfilter/nf_tables_api.c +59 -0
71 lines not on this page · raw message
net/netfilter/nft_chain_filter.c +2 -0
@@ -363,6 +363,8 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,list_add_tail_rcu (&ops->list, &hook->ops_list);break ; } +nf_tables_chain_device_notify (&basechain->chain, + hook, dev, event); }return 0 ; }
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v6 12/12 selftests: netfilter: Torture nftables netdev hooks
In reply to Phil SutterAdd a ruleset which binds to various interface names via netdev-family chains and flowtables and massage the notifiers by frequently renaming interfaces to match these names. While doing so:
- Keep an 'nft monitor' running in background to receive the notifications
- Loop over 'nft list ruleset' to exercise ruleset dump codepath
- Have iperf running so the involved chains/flowtables see traffic
If supported, also test interface wildcard support separately by creating a flowtable with 'wild*' interface spec and quickly add/remove matching dummy interfaces.
---
Changes since v5:
- Have iperf report rates in Kbits/s, the value in Mbits/s may contain a decimal point and therefore break the following zero value check
Changes since v4:
- Limit maximum run-time to 48s.
--- .../testing/selftests/net/netfilter/Makefile | 1 + .../net/netfilter/nft_interface_stress.sh | 151 ++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100755 tools/testing/selftests/net/netfilter/nft_interface_stress.sh
tools/testing/selftests/net/netfilter/Makefile +1 -0
@@ -23,6 +23,7 @@ TEST_PROGS += nft_concat_range.sh TEST_PROGS += nft_conntrack_helper.sh TEST_PROGS += nft_fib.sh TEST_PROGS += nft_flowtable.sh +TEST_PROGS += nft_interface_stress.sh TEST_PROGS += nft_meta.sh TEST_PROGS += nft_nat.sh TEST_PROGS += nft_nat_zones.sh
tools/testing/selftests/net/netfilter/nft_interface_stress.sh new file +151 -0
151 lines not on this page · raw message
Signature · 1 line
2.49.0
Phil Sutter
reviewerBump!
Anything I can do to help push this forward? The series I submitted to add support for this to libnftnl and nftables should still apply as-is. Anything else missing on my end? Or should I try to break this down into smaller patches/chunks?
Thanks, Phil
Quoted 55 lines · Phil Sutter
Changes since v5:
- First part split into separate series (applied and present in Linus' git already).
- Addnft_hook_find_ops_rcu()in patch 2 already to reduce size of patch 5.
- New patch 4 to reduce size of patch 5.
- New patch 6 preparing for patch 7 which in turn combines identical changes to both flowtables and netdev chains.Patches 1-5 prepare for and implement
nf_hook_opslists innft_hookobjects. This is crucial for wildcard interface specs and convenient with dynamic netdev hook registration uponNETDEV_REGISTERevents.Patches 6-9 leverage the new infrastructure to correctly handle
NETDEV_REGISTERandNETDEV_CHANGENAMEevents.Patch 10 prepares the code for non-NUL-terminated interface names passed by user space which resemble prefixes to match on. As a side-effect, hook allocation code becomes tolerant to non-matching interface specs.
The final two patches implement netlink notifications for netdev add/remove events and add a kselftest.
Phil Sutter (12):
netfilter:nf_tables: Introduce functions freeingnft_hookobjects
netfilter:nf_tables: Introduce nft_hook_find_ops{,_rcu}()
netfilter:nf_tables: Introducenft_register_flowtable_ops()
netfilter:nf_tables: Passnf_hook_opsto
nft_unregister_flowtable_hook()
netfilter:nf_tables: Have a list ofnf_hook_opsinnft_hook
netfilter:nf_tables: Prepare for handlingNETDEV_REGISTERevents
netfilter:nf_tables: RespectNETDEV_REGISTERevents
netfilter:nf_tables: Wrap netdev notifiers
netfilter:nf_tables: HandleNETDEV_CHANGENAMEevents
netfilter:nf_tables: Support wildcard netdev hook specs
netfilter:nf_tables: Add notications for hook changes
selftests: netfilter: Torture nftables netdev hooksinclude/linux/netfilter.h | 3 + include/net/netfilter/nf_tables.h | 12 +- include/uapi/linux/netfilter/nf_tables.h | 10 + net/netfilter/nf_tables_api.c | 394 ++++++++++++++---- net/netfilter/nf_tables_offload.c | 51 ++- net/netfilter/nft_chain_filter.c | 95 ++++- net/netfilter/nft_flow_offload.c | 2 +- .../testing/selftests/net/netfilter/Makefile | 1 + .../net/netfilter/nft_interface_stress.sh | 151 +++++++ 9 files changed, 587 insertions(+), 132 deletions(-) create mode 100755 tools/testing/selftests/net/netfilter/nft_interface_stress.shSignature · 1 line
2.49.0
Pablo Neira Ayuso
maintainerQuoted 6 lines · Phil Sutter
Bump!
Anything I can do to help push this forward? The series I submitted to add support for this to libnftnl and nftables should still apply as-is. Anything else missing on my end? Or should I try to break this down into smaller patches/chunks?
I was exactly now looking into integrating this into nf-next, sorry for the slow turn around.
Quoted 58 lines
Thanks, Phil
Quoted 55 lines · Phil Sutter
Changes since v5:
- First part split into separate series (applied and present in Linus' git already).
- Addnft_hook_find_ops_rcu()in patch 2 already to reduce size of patch 5.
- New patch 4 to reduce size of patch 5.
- New patch 6 preparing for patch 7 which in turn combines identical changes to both flowtables and netdev chains.Patches 1-5 prepare for and implement
nf_hook_opslists innft_hookobjects. This is crucial for wildcard interface specs and convenient with dynamic netdev hook registration uponNETDEV_REGISTERevents.Patches 6-9 leverage the new infrastructure to correctly handle
NETDEV_REGISTERandNETDEV_CHANGENAMEevents.Patch 10 prepares the code for non-NUL-terminated interface names passed by user space which resemble prefixes to match on. As a side-effect, hook allocation code becomes tolerant to non-matching interface specs.
The final two patches implement netlink notifications for netdev add/remove events and add a kselftest.
Phil Sutter (12):
netfilter:nf_tables: Introduce functions freeingnft_hookobjects
netfilter:nf_tables: Introduce nft_hook_find_ops{,_rcu}()
netfilter:nf_tables: Introducenft_register_flowtable_ops()
netfilter:nf_tables: Passnf_hook_opsto
nft_unregister_flowtable_hook()
netfilter:nf_tables: Have a list ofnf_hook_opsinnft_hook
netfilter:nf_tables: Prepare for handlingNETDEV_REGISTERevents
netfilter:nf_tables: RespectNETDEV_REGISTERevents
netfilter:nf_tables: Wrap netdev notifiers
netfilter:nf_tables: HandleNETDEV_CHANGENAMEevents
netfilter:nf_tables: Support wildcard netdev hook specs
netfilter:nf_tables: Add notications for hook changes
selftests: netfilter: Torture nftables netdev hooksinclude/linux/netfilter.h | 3 + include/net/netfilter/nf_tables.h | 12 +- include/uapi/linux/netfilter/nf_tables.h | 10 + net/netfilter/nf_tables_api.c | 394 ++++++++++++++---- net/netfilter/nf_tables_offload.c | 51 ++- net/netfilter/nft_chain_filter.c | 95 ++++- net/netfilter/nft_flow_offload.c | 2 +- .../testing/selftests/net/netfilter/Makefile | 1 + .../net/netfilter/nft_interface_stress.sh | 151 +++++++ 9 files changed, 587 insertions(+), 132 deletions(-) create mode 100755 tools/testing/selftests/net/netfilter/nft_interface_stress.shSignature · 1 line
2.49.0
Phil Sutter
reviewerQuoted 10 lines · Pablo Neira Ayuso
Quoted 6 lines · Phil Sutter
Bump!
Anything I can do to help push this forward? The series I submitted to add support for this to libnftnl and nftables should still apply as-is. Anything else missing on my end? Or should I try to break this down into smaller patches/chunks?
I was exactly now looking into integrating this into nf-next, sorry for the slow turn around.
Nice! I obviously start to sense when someone reviews a patch of mine. :D
Thanks, Phil
Pablo Neira Ayuso
maintainerHi Phil,
This looks very good, I still have a few comments, related to three patches:
== netfilter: nf_tables: Have a list of nf_hook_ops in nft_hook
1) There's a possible inconsistent use of list_for_each_entry{_safe} while calling nf_unregister_net_hook().
static void nft_netdev_unregister_hooks(struct net *net,
struct list_head *hook_list, bool release_netdev)
{
+ struct nf_hook_ops *ops, *nextops;
struct nft_hook *hook, *next;
list_for_each_entry_safe(hook, next, hook_list, list) {- nf_unregister_net_hook(net, &hook->ops);
+ list_for_each_entry_safe(ops, nextops, &hook->ops_list, list) <--- HERE
+ nf_unregister_net_hook(net, ops);
[...]
@@ -2923,8 +2962,10 @@ static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy,
err_hooks:
if (nla[NFTA_CHAIN_HOOK]) {
list_for_each_entry_safe(h, next, &hook.list, list) {- if (unregister)
- nf_unregister_net_hook(ctx->net, &h->ops);
+ if (unregister) {
+ list_for_each_entry(ops, &h->ops_list, list) <--- HERE
+ nf_unregister_net_hook(ctx->net, ops);
Which one should be adjusted? I think _safe can be removed?
Maybe add nf_unregister_net_hook_list() helper? It helps to avoid future similar issues.
2) I wonder if nft_hook_find_ops() will need a hashtable sooner or later. With the wildcard, the number of devices could be significantly large in this list lookup.
@@ -9611,9 +9666,12 @@ static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
struct nf_hook_ops *nft_hook_find_ops(const struct nft_hook *hook,
const struct net_device *dev)
{
- if (hook->ops.dev == dev)
- return (struct nf_hook_ops *)&hook->ops;
+ struct nf_hook_ops *ops;
+ list_for_each_entry(ops, &hook->ops_list, list) {
+ if (ops->dev == dev)
+ return ops;
+ }
return NULL;
}
3) Maybe move struct rcu_head at the end of struct nf_hook_ops?
struct nf_hook_ops {
+ struct list_head list; + struct rcu_head rcu; <--- move it at the end of this struct?
This is a control plane object, but still it is common to place this at the end. But not a deal breaker.
4) nft_netdev_event() is missing a break; I think it is an overlook?
net/netfilter/nft_chain_filter.c +2 -3
@@ -332,9 +332,8 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,if (!(basechain->chain.table->flags &NFT_TABLE_F_DORMANT ))nf_unregister_net_hook (dev_net (dev), ops); -list_del_rcu (&hook->list); -kfree_rcu (hook, rcu); - break; <------------------------- this is gone! +list_del_rcu (&ops->list); +kfree_rcu (ops, rcu); } }
but I can still see break; in the flowtable event handler.
So nft_netdev_event() shows no break;
But nft_flowtable_event() still has a break;
== Support wildcard netdev hook specs
Nitpick: the err_ops_alloc: tag takes me to nft_netdev_hook_free(hook); maybe better rename it to err_hook_free: ? Because currently err_ops_alloc takes me to nft_netdev_hook_free(hook);
@@ -2323,7 +2323,7 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
err = nla_strscpy(hook->ifname, attr, IFNAMSIZ);
if (err < 0)- goto err_hook_dev;
+ goto err_ops_alloc;
but takes you to free the hook:
-err_hook_dev:
- kfree(hook);
+err_ops_alloc:
+ nft_netdev_hook_free(hook);
== netfilter: nf_tables: Add "notications" <-- typo: "notifications"
I suggest you add a new NFNLGRP_NFT_DEV group for these notifications, so NFNLGRP_NFTABLES is only used for control plane updates via nfnetlink API. In this case, these events are triggered by rtnetlink when a new device is registered and it matches the existing an existing device if I understood the rationale.
Thanks.
Phil Sutter
reviewerHi Pablo,
Quoted 32 lines · Pablo Neira Ayuso
This looks very good, I still have a few comments, related to three patches:
== netfilter:
nf_tables: Have a list ofnf_hook_opsinnft_hook1) There's a possible inconsistent use of list_for_each_entry{_safe} while calling
nf_unregister_net_hook().static void nft_netdev_unregister_hooks(struct net *net,
structlist_head*hook_list, boolrelease_netdev)
{(quoted diff) +6
-3+ struct nf_hook_ops *ops, *nextops; struct nft_hook *hook, *next; list_for_each_entry_safe(hook, next, hook_list, list) { - nf_unregister_net_hook(net, &hook->ops); + list_for_each_entry_safe(ops, nextops, &hook->ops_list, list) <--- HERE + nf_unregister_net_hook(net, ops);@@ -2923,8 +2962,10 @@ static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, err_hooks: if (nla[NFTA_CHAIN_HOOK]) { list_for_each_entry_safe(h, next, &hook.list, list) { - if (unregister) - nf_unregister_net_hook(ctx->net, &h->ops); + if (unregister) { + list_for_each_entry(ops, &h->ops_list, list) <--- HERE + nf_unregister_net_hook(ctx->net, ops);
Quoted 1 line
Which one should be adjusted? I think
_safecan be removed?
Oh, right. Yes, the _save is pointless since no items are removed inside the loop.
Quoted 2 lines
Maybe add
nf_unregister_net_hook_list()helper? It helps to avoid future similar issues.
ACK, will do!
Quoted 3 lines
2) I wonder if
nft_hook_find_ops()will need a hashtable sooner or later. With the wildcard, the number of devices could be significantly large in this list lookup.
Maybe, yes. Is it useful to have a single flowtable for all virtual functions (e.g.) on a hypervisor? Or would one rather have distinct flowtables for each VM/container?
Quoted 4 lines
(quoted diff) +0
-0@@ -9611,9 +9666,12 @@ static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, struct nf_hook_ops *nft_hook_find_ops(const struct nft_hook *hook, const struct net_device *dev) {
Review static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, · line 9676
Quoted 10 lines
(quoted diff) +5
-2- if (hook->ops.dev == dev) - return (struct nf_hook_ops *)&hook->ops; + struct nf_hook_ops *ops; + list_for_each_entry(ops, &hook->ops_list, list) { + if (ops->dev == dev) + return ops; + } return NULL; }
Callers are:
- nft_{flowtable,netdev}_event(): Interface is added or removed
- nft_flow_offload_eval(): New flow being offloaded
- nft_offload_netdev_event(): Interface is removed
All these are "slow path" at least. I could try building a test case to see how performance scales, but since we hit the function just once for each new connection, I guess it's hard to get significant data out of it.
Review
Quoted 8 lines
3) Maybe move struct
rcu_headat the end of structnf_hook_ops?struct
nf_hook_ops{(quoted diff) +2
-0+ struct list_head list; + struct rcu_head rcu; <--- move it at the end of this struct? This is a control plane object, but still it is common to place this at the end. But not a deal breaker.
I tried to copy struct nft_hook in that regard which has the rcu_head right after the list_heads. AIUI, both these types are "handles" to carry the object around, having the actual payload follow them feels more natural to me. If you prefer the "append new fields" approach, fine with me though!
Quoted 20 lines
4)
nft_netdev_event()is missing a break; I think it is an overlook?net/netfilter/nft_chain_filter.c +2
-3@@ -332,9 +332,8 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,if (!(basechain->chain.table->flags &NFT_TABLE_F_DORMANT ))nf_unregister_net_hook (dev_net (dev), ops); -list_del_rcu (&hook->list); -kfree_rcu (hook, rcu); - break; <------------------------- this is gone! +list_del_rcu (&ops->list); +kfree_rcu (ops, rcu); } }but I can still see break; in the flowtable event handler.
Quoted 2 lines
So
nft_netdev_event()shows no break;
Butnft_flowtable_event()still has a break;
I recall being undecided about this. A relevant difference between netdev chains and flowtables is that a device may have multiple netdev chain hooks but only a single flowtable one[1]. This might have confused me, because the break above effectively means "stop searching for a matching hook in this chain if one was found for the given interface already". Since neither chains nor flowtables allow for overlapping hooks (e.g. { eth1*, eth11 }), a given device matches at most a single hook. I'll adjust the series to keep this break, then. Thanks!
Quoted 15 lines
== Support wildcard netdev hook specs
Nitpick: the
err_ops_alloc: tag takes me tonft_netdev_hook_free(hook); maybe better rename it toerr_hook_free: ? Because currentlyerr_ops_alloctakes me tonft_netdev_hook_free(hook);net/netfilter/nft_chain_filter.c +1
-1@@ -2323,7 +2323,7 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net, err =nla_strscpy (hook->ifname, attr,IFNAMSIZ );if (err <0 ) - goto err_hook_dev; + goto err_ops_alloc;but takes you to free the hook:
net/netfilter/nft_chain_filter.c
Quoted 4 lines
net/netfilter/nft_chain_filter.c +2
-2-err_hook_dev: -kfree (hook); +err_ops_alloc: +nft_netdev_hook_free (hook);
Oh yes, this is a mess. Also, the err_hook_alloc tag is pointless as it leads to an immediate return.
Quoted 7 lines
== netfilter:
nf_tables: Add "notications" <-- typo: "notifications"I suggest you add a new
NFNLGRP_NFT_DEVgroup for these notifications, soNFNLGRP_NFTABLESis only used for control plane updates via nfnetlink API. In this case, these events are triggered by rtnetlink when a new device is registered and it matches the existing an existing device if I understood the rationale.
Yes, MSG_NEWDEV and MSG_DELDEV are triggered if a new device matches a hook or if a hooked device is removed (or renamed, so the hook won't match anymore).
Having a distinct NFNLGRP for them requires a new 'nft monitor' mode, right? So we can't have a single monitor process for ruleset changes and these device events. Should not be a problem, though.
Thanks, Phil
[1] nft_register_flowtable_net_hooks() searches for a matching hook in other flowtables of the same table.
Phil Sutter
reviewerQuoted 1 line · Phil Sutter
On Wed, May 21, 2025 at 12:28:01AM +0200, Pablo Neira Ayuso wrote:
[...]
Quoted 4 lines
Quoted 2 lines
Maybe add
nf_unregister_net_hook_list()helper? It helps to avoid future similar issues.ACK, will do!
Scratch that, I was too quick and didn't check: There are exactly two spots which run 'list_for_each_entry(...) { nf_register_net_hook() }' without any extras. I'll just fix the one spot instead.
Cheers, Phil
Pablo Neira Ayuso
maintainerHi Phil,
Quoted 3 lines · Phil Sutter
Hi Pablo,
On Wed, May 21, 2025 at 12:28:01AM +0200, Pablo Neira Ayuso wrote:
[...]
Quoted 7 lines
Quoted 3 lines
2) I wonder if
nft_hook_find_ops()will need a hashtable sooner or later. With the wildcard, the number of devices could be significantly large in this list lookup.Maybe, yes. Is it useful to have a single flowtable for all virtual functions (e.g.) on a hypervisor? Or would one rather have distinct flowtables for each VM/container?
[...]
Quoted 10 lines
Callers are:
- nft_{flowtable,netdev}_event(): Interface is added or removed
-nft_flow_offload_eval(): New flow being offloaded
-nft_offload_netdev_event(): Interface is removedAll these are "slow path" at least. I could try building a test case to see how performance scales, but since we hit the function just once for each new connection, I guess it's hard to get significant data out of it.
This can be added later, not a deal breaker.
This is event path which might slow down adding new entries via rtnetlink maybe, but I would need to have a closer look.
[...]
Quoted 8 lines
Quoted 7 lines
== netfilter:
nf_tables: Add "notications" <-- typo: "notifications"I suggest you add a new
NFNLGRP_NFT_DEVgroup for these notifications, soNFNLGRP_NFTABLESis only used for control plane updates via nfnetlink API. In this case, these events are triggered by rtnetlink when a new device is registered and it matches the existing an existing device if I understood the rationale.
Quoted 7 lines
Yes,
MSG_NEWDEVandMSG_DELDEVare triggered if a new device matches a hook or if a hooked device is removed (or renamed, so the hook won't match anymore).Having a distinct NFNLGRP for them requires a new 'nft monitor' mode, right? So we can't have a single monitor process for ruleset changes and these device events. Should not be a problem, though.
Having a different group allows to filter out events you might not care about, it is a simple netlink event filtering facility.
I think this feature is for debugging purpose only, correct? So a separated group should be fine. IIUC, this event does not modify the ruleset, it only tells us a hook for a matching device is being registered.
Thanks.
Phil Sutter
reviewerQuoted 21 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 3 lines · Phil Sutter
Hi Pablo,
On Wed, May 21, 2025 at 12:28:01AM +0200, Pablo Neira Ayuso wrote:
[...]
Quoted 7 lines
Quoted 3 lines
2) I wonder if
nft_hook_find_ops()will need a hashtable sooner or later. With the wildcard, the number of devices could be significantly large in this list lookup.Maybe, yes. Is it useful to have a single flowtable for all virtual functions (e.g.) on a hypervisor? Or would one rather have distinct flowtables for each VM/container?
[...]
Quoted 6 lines
Callers are:
- nft_{flowtable,netdev}_event(): Interface is added or removed
-nft_flow_offload_eval(): New flow being offloaded
-nft_offload_netdev_event(): Interface is removed
Quoted 6 lines
Quoted 4 lines
All these are "slow path" at least. I could try building a test case to see how performance scales, but since we hit the function just once for each new connection, I guess it's hard to get significant data out of it.
This can be added later, not a deal breaker.
ACK.
Quoted 2 lines
This is event path which might slow down adding new entries via rtnetlink maybe, but I would need to have a closer look.
We'd optimize for flowtables with many devices. The same system could also have many flowtables with few devices each, then the hash table adds to the overhead. A global table for all flowtables could serve both. We could also have the same device in multiple chains, so each hash table entry needs a list of ops pointers?
Quoted 17 lines
[...]
Quoted 8 lines
Quoted 7 lines
== netfilter:
nf_tables: Add "notications" <-- typo: "notifications"I suggest you add a new
NFNLGRP_NFT_DEVgroup for these notifications, soNFNLGRP_NFTABLESis only used for control plane updates via nfnetlink API. In this case, these events are triggered by rtnetlink when a new device is registered and it matches the existing an existing device if I understood the rationale.Quoted 7 lines
Yes,
MSG_NEWDEVandMSG_DELDEVare triggered if a new device matches a hook or if a hooked device is removed (or renamed, so the hook won't match anymore).Having a distinct NFNLGRP for them requires a new 'nft monitor' mode, right? So we can't have a single monitor process for ruleset changes and these device events. Should not be a problem, though.
Quoted 7 lines
Having a different group allows to filter out events you might not care about, it is a simple netlink event filtering facility.
I think this feature is for debugging purpose only, correct? So a separated group should be fine. IIUC, this event does not modify the ruleset, it only tells us a hook for a matching device is being registered.
Yes, you're right. Strictly speaking, these events are different from NFNLGRP_NFTABLES as they don't indicate a ruleset change. Their actual purpose is to satisfy the requirement of hook reg/unreg being visible to user space. :)
nft monitor needs some work, though: Right now it's in trace or !trace mode depending on whether NFT_MSG_TRACE is present in monitor_flags. Now there will be a third modus operandi. OK, we could hide that internally and enable the new group automatically if not tracing, then add a 'hooks' keyword so users may 'nft monitor (new|destroy) hooks'.
Cheers, Phil