Skip to contentSkip to conversation

Dynamic hook interface binding

PATCH Superseded Phil Sutter 2 participants 24 messages Sep 20, 2024 – Sep 25, 2024

Subsystems (3)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

v4 cover Dynamic hook interface binding

Changes since v3:
- Introduce RCU for nf_hook_ops: Readers iterate through each nft_hook's ops_list and we want to remove individual list entries in netdev event handlers. This requires to extend nf_hook_ops by an rcu_head if we don't want to call synchronize_rcu() in netdev notifier callbacks.
- RCU-free an nft_hook and ops using call_rcu() and a callback.
- Check nla_strscpy() return value to make sure user-defined ifname string length is less than IFNAMSIZ.
- Drop __nft_unregister_flowtable_net_hooks(), it's redundant (noticed while reviewing nft_hook free sites).
- Return error in netdev notifier callbacks if hook registration fails.
- Upon NETDEV_CHANGENAME, try to register the new name first before unregistering the old one.
- Use kmemdup() instead of kzalloc() && memcpy().

Patch 1 eliminates a pointless check and allows for some code consolidation.

The next three patches introduce external storing of the user-supplied interface name in nft_hook structs to decouple code from values in ->ops.dev or ->ops value in general.

Patch 5 eliminates a quirk in netdev-family chain netdev event handler, aligns behaviour with flowtables and paves the way for following changes.

Patches 6-10 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 11-13 leverage the new infrastructure to correctly handle NETDEV_REGISTER and NETDEV_CHANGENAME events.

Patch 14 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 (16):
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: Introduce functions freeing nft_hook objects
netfilter: nf_tables: Introduce nft_hook_find_ops()
netfilter: nf_tables: Introduce nft_register_flowtable_ops()
netfilter: nf_tables: Drop __nft_unregister_flowtable_net_hooks()
netfilter: nf_tables: Have a list of nf_hook_ops in nft_hook
netfilter: nf_tables: chain: Respect NETDEV_REGISTER events
netfilter: nf_tables: flowtable: Respect NETDEV_REGISTER events
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             |  11 +-
 include/uapi/linux/netfilter/nf_tables.h      |   5 +
 net/netfilter/nf_tables_api.c                 | 411 ++++++++++++------
 net/netfilter/nf_tables_offload.c             |  51 ++-
 net/netfilter/nft_chain_filter.c              |  73 ++--
 net/netfilter/nft_flow_offload.c              |   2 +-
 .../testing/selftests/net/netfilter/Makefile  |   1 +
 .../net/netfilter/nft_interface_stress.sh     | 149 +++++++
 9 files changed, 521 insertions(+), 185 deletions(-)
 create mode 100755 tools/testing/selftests/net/netfilter/nft_interface_stress.sh
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH v4 1/16 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.

Fixes 3f0465a9ef02 ("netfilter: nf_tables: dynamically allocate hooks per net_device in flowtables")
---
 net/netfilter/nf_tables_api.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
net/netfilter/nf_tables_api.c +4 -7
index 042080aeb46c..b85f15ed77ed 100644
@@ -8571,7 +8571,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;
 
@@ -8580,12 +8580,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.43.0
author reviewer
#2 lore raw

PATCH v4 2/16 netfilter: nf_tables: Store user-defined hook ifname

In reply to Phil Sutter

Prepare 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.

---
Changes since v3:
- Assert max attribute length to avoid bogus values in hook->ifnamelen as per Florian's suggestion.

---
 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
index 49708e7e1339..73714e9d9392 100644
@@ -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
index b85f15ed77ed..2ec3e407f91f 100644
@@ -2173,7 +2173,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;
 
@@ -2183,12 +2182,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.43.0
author reviewer
#3 lore raw

PATCH v4 3/16 netfilter: nf_tables: Use stored ifname in netdev hook dumps

In reply to Phil Sutter

The 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
index 2ec3e407f91f..c53afdecef24 100644
@@ -1853,15 +1853,16 @@ static int nft_dump_basechain_hook(struct sk_buff *skb, int family,
 			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);
@@ -8999,7 +9000,8 @@ static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
 		hook_list = &flowtable->hook_list;
 
 	list_for_each_entry_rcu(hook, hook_list, list) {
-		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.43.0
author reviewer
#4 lore raw

PATCH v4 4/16 netfilter: nf_tables: Compare netdev hooks based on stored name

In reply to Phil Sutter

The 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
index c53afdecef24..f6e28a6ac9b0 100644
@@ -2214,7 +2214,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.43.0
author reviewer
#5 lore raw

PATCH v4 5/16 netfilter: nf_tables: Tolerate chains with no remaining hooks

In reply to Phil Sutter

Do not drop a netdev-family chain if the last interface it is registered for vanishes. Users dumping and storing the ruleset upon shutdown for restore 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     | 21 ---------------------
 net/netfilter/nft_chain_filter.c  | 29 +++++++----------------------
 3 files changed, 7 insertions(+), 45 deletions(-)
include/net/netfilter/nf_tables.h +0 -2
index 73714e9d9392..6aa39c4a8c3c 100644
@@ -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 -21
index f6e28a6ac9b0..f77ba323a906 100644
@@ -11433,27 +11433,6 @@ int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
 }
 EXPORT_SYMBOL_GPL(nft_data_dump);
 
-int __nft_release_basechain(struct nft_ctx *ctx)
-{
-	struct nft_rule *rule, *nr;
-
-	if (WARN_ON(!nft_is_base_chain(ctx->chain)))
-		return 0;
-
-	nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain);
-	list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
-		list_del(&rule->list);
-		nft_use_dec(&ctx->chain->use);
-		nf_tables_rule_release(ctx, rule);
-	}
-	nft_chain_del(ctx->chain);
-	nft_use_dec(&ctx->table->use);
-	nf_tables_chain_destroy(ctx->chain);
-
-	return 0;
-}
-EXPORT_SYMBOL_GPL(__nft_release_basechain);
-
 static void __nft_release_hook(struct net *net, struct nft_table *table)
 {
 	struct nft_flowtable *flowtable;
net/netfilter/nft_chain_filter.c +7 -22
index 7010541fcca6..543f258b7c6b 100644
@@ -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.43.0
author reviewer
#6 lore raw

PATCH v4 6/16 netfilter: nf_tables: Introduce functions freeing nft_hook objects

In reply to Phil Sutter

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 | 36 ++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)
net/netfilter/nf_tables_api.c +23 -13
index f77ba323a906..a0482c7fc659 100644
@@ -322,6 +322,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)
@@ -332,7 +342,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);
 		}
 	}
 }
@@ -2152,7 +2162,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);
@@ -2244,7 +2254,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;
 		}
@@ -2262,7 +2272,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;
 }
@@ -2405,7 +2415,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);
 }
@@ -2695,7 +2705,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 {
@@ -2816,7 +2826,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);
 	}
@@ -8617,7 +8627,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;
@@ -8629,7 +8639,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);
 	}
 }
 
@@ -8653,7 +8663,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);
 		}
 	}
 
@@ -8700,7 +8710,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;
@@ -8846,7 +8856,7 @@ static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook
 
 	list_for_each_entry_safe(this, next, &flowtable_hook->list, list) {
 		list_del(&this->list);
-		kfree(this);
+		nft_netdev_hook_free(this);
 	}
 }
 
@@ -9210,7 +9220,7 @@ static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
 		flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
 					    FLOW_BLOCK_UNBIND);
 		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.43.0
author reviewer
#7 lore raw

PATCH v4 7/16 netfilter: nf_tables: Introduce nft_hook_find_ops()

In reply to Phil Sutter

Also 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.

---
 include/net/netfilter/nf_tables.h |  3 +++
 net/netfilter/nf_tables_api.c     | 14 +++++++++++++-
 net/netfilter/nf_tables_offload.c |  2 +-
 net/netfilter/nft_chain_filter.c  |  6 ++++--
 net/netfilter/nft_flow_offload.c  |  2 +-
 5 files changed, 22 insertions(+), 5 deletions(-)
include/net/netfilter/nf_tables.h +3 -0
index 6aa39c4a8c3c..37d1110ccfd9 100644
@@ -1196,6 +1196,9 @@ struct nft_hook {
 	u8			ifnamelen;
 };
 
+struct nf_hook_ops *nft_hook_find_ops(const struct nft_hook *hook,
+				      const struct net_device *dev);
+
 /**
  *	struct nft_base_chain - nf_tables base chain
  *
net/netfilter/nf_tables_api.c +13 -1
index a0482c7fc659..8326395c5752 100644
@@ -9253,13 +9253,25 @@ 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);
+
 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
index 64675f1c7f29..75b756f0b9f0 100644
@@ -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
index 543f258b7c6b..d34c6fe7ba72 100644
@@ -322,14 +322,16 @@ 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 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 (!(ctx->chain->table->flags & NFT_TABLE_F_DORMANT))
-			nf_unregister_net_hook(ctx->net, &hook->ops);
+			nf_unregister_net_hook(ctx->net, ops);
 
 		list_del_rcu(&hook->list);
 		kfree_rcu(hook, rcu);
net/netfilter/nft_flow_offload.c +1 -1
index 2f732fae5a83..83415d7aadda 100644
@@ -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(hook, dev))
 			continue;
 
 		found = true;
Signature · 1 line
2.43.0
author reviewer
#8 lore raw

PATCH v4 8/16 netfilter: nf_tables: Introduce nft_register_flowtable_ops()

In reply to Phil Sutter

Facilitate 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
index 8326395c5752..3721f4636e0a 100644
@@ -8581,6 +8581,26 @@ static void nft_unregister_flowtable_net_hooks(struct net *net,
 	__nft_unregister_flowtable_net_hooks(net, 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,
@@ -8601,20 +8621,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.43.0
author reviewer
#9 lore raw

PATCH v4 9/16 netfilter: nf_tables: Drop __nft_unregister_flowtable_net_hooks()

In reply to Phil Sutter

The function is a 1:1 copy of nft_netdev_unregister_hooks(), use the latter in its place.

---
 net/netfilter/nf_tables_api.c | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)
net/netfilter/nf_tables_api.c +2 -18
index 3721f4636e0a..7a721df27f12 100644
@@ -8560,25 +8560,10 @@ static void nft_unregister_flowtable_hook(struct net *net,
 				    FLOW_BLOCK_UNBIND);
 }
 
-static void __nft_unregister_flowtable_net_hooks(struct net *net,
-						 struct list_head *hook_list,
-					         bool release_netdev)
-{
-	struct nft_hook *hook, *next;
-
-	list_for_each_entry_safe(hook, next, hook_list, list) {
-		nf_unregister_net_hook(net, &hook->ops);
-		if (release_netdev) {
-			list_del(&hook->list);
-			kfree_rcu(hook, rcu);
-		}
-	}
-}
-
 static void nft_unregister_flowtable_net_hooks(struct net *net,
 					       struct list_head *hook_list)
 {
-	__nft_unregister_flowtable_net_hooks(net, hook_list, false);
+	nft_netdev_unregister_hooks(net, hook_list, false);
 }
 
 static int nft_register_flowtable_ops(struct net *net,
@@ -11473,8 +11458,7 @@ static void __nft_release_hook(struct net *net, struct nft_table *table)
 	list_for_each_entry(chain, &table->chains, list)
 		__nf_tables_unregister_hook(net, table, chain, true);
 	list_for_each_entry(flowtable, &table->flowtables, list)
-		__nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list,
-						     true);
+		nft_netdev_unregister_hooks(net, &flowtable->hook_list, true);
 }
 
 static void __nft_release_hooks(struct net *net)
Signature · 1 line
2.43.0
author reviewer
#10 lore raw

PATCH v4 10/16 netfilter: nf_tables: Have a list of nf_hook_ops in nft_hook

In reply to Phil Sutter

Supporting 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     | 149 +++++++++++++++++++++---------
 net/netfilter/nf_tables_offload.c |  49 +++++-----
 net/netfilter/nft_chain_filter.c  |   6 +-
 5 files changed, 138 insertions(+), 71 deletions(-)
include/linux/netfilter.h +3 -0
index 2683b2b77612..18372de587b4 100644
@@ -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
index 37d1110ccfd9..eaf2f5184bdf 100644
@@ -1190,7 +1190,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 +104 -45

302 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 -4
index d34c6fe7ba72..f8c69d28d656 100644
@@ -332,10 +332,8 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,
 
 		if (!(ctx->chain->table->flags & NFT_TABLE_F_DORMANT))
 			nf_unregister_net_hook(ctx->net, ops);
-
-		list_del_rcu(&hook->list);
-		kfree_rcu(hook, rcu);
-		break;
+		list_del_rcu(&ops->list);
+		kfree_rcu(ops, rcu);
 	}
 }
 
Signature · 1 line
2.43.0
author reviewer
#11 lore raw

PATCH v4 11/16 netfilter: nf_tables: chain: Respect NETDEV_REGISTER events

In reply to Phil Sutter

Hook into new devices if their name matches the hook spec.

---
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/nft_chain_filter.c | 50 +++++++++++++++++++++++++-------
 1 file changed, 39 insertions(+), 11 deletions(-)
net/netfilter/nft_chain_filter.c +39 -11
index f8c69d28d656..562af2773a66 100644
@@ -318,23 +318,47 @@ 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)
+static int 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 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 (!(ctx->chain->table->flags & NFT_TABLE_F_DORMANT))
+				nf_unregister_net_hook(ctx->net, ops);
+			list_del_rcu(&ops->list);
+			kfree_rcu(ops, rcu);
+			break;
+		case NETDEV_REGISTER:
+			if (strcmp(hook->ifname, dev->name))
+				continue;
 
-		if (!(ctx->chain->table->flags & NFT_TABLE_F_DORMANT))
-			nf_unregister_net_hook(ctx->net, ops);
-		list_del_rcu(&ops->list);
-		kfree_rcu(ops, rcu);
+			ops = kmemdup(&basechain->ops,
+				      sizeof(struct nf_hook_ops),
+				      GFP_KERNEL_ACCOUNT);
+			if (!ops)
+				return 1;
+
+			ops->dev = dev;
+
+			if (!(ctx->chain->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,
@@ -349,7 +373,8 @@ static int nf_tables_netdev_event(struct notifier_block *this,
 		.net	= dev_net(dev),
 	};
 
-	if (event != NETDEV_UNREGISTER)
+	if (event != NETDEV_REGISTER &&
+	    event != NETDEV_UNREGISTER)
 		return NOTIFY_DONE;
 
 	nft_net = nft_pernet(ctx.net);
@@ -371,7 +396,10 @@ static int nf_tables_netdev_event(struct notifier_block *this,
 				continue;
 
 			ctx.chain = chain;
-			nft_netdev_event(event, dev, &ctx);
+			if (nft_netdev_event(event, dev, &ctx)) {
+				mutex_unlock(&nft_net->commit_mutex);
+				return NOTIFY_BAD;
+			}
 		}
 	}
 	mutex_unlock(&nft_net->commit_mutex);
Signature · 1 line
2.43.0
author reviewer
#12 lore raw

PATCH v4 12/16 netfilter: nf_tables: flowtable: Respect NETDEV_REGISTER events

In reply to Phil Sutter

Hook into new devices if their name matches the hook spec.

---
Changes since v3:
- Use list_add_tail_rcu() to avoid breaking readers.
- Return NOTIFY_BAD upon error instead of printing an error message as per Florian.

---
 net/netfilter/nf_tables_api.c | 56 +++++++++++++++++++++++++++--------
 1 file changed, 43 insertions(+), 13 deletions(-)
net/netfilter/nf_tables_api.c +43 -13
index f3b0bc2fe0e3..2684990dd3dc 100644
@@ -9317,23 +9317,49 @@ struct nf_hook_ops *nft_hook_find_ops(const struct nft_hook *hook,
 }
 EXPORT_SYMBOL_GPL(nft_hook_find_ops);
 
-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;
 
 	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);
-		break;
+			/* 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;
+		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;
+		}
 	}
+	return 0;
 }
 
 static int nf_tables_flowtable_event(struct notifier_block *this,
@@ -9345,15 +9371,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);
Signature · 1 line
2.43.0
author reviewer
#13 lore raw

PATCH v4 13/16 netfilter: nf_tables: Handle NETDEV_CHANGENAME events

In reply to Phil Sutter

For 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.

---
Changes since v3:
- Register first and handle errors to avoid having unregistered the device but registration fails.

---
 net/netfilter/nf_tables_api.c    | 5 +++++
 net/netfilter/nft_chain_filter.c | 5 +++++
 2 files changed, 10 insertions(+)
net/netfilter/nf_tables_api.c +5 -0
index 2684990dd3dc..4d40c1905735 100644
@@ -9371,6 +9371,11 @@ static int nf_tables_flowtable_event(struct notifier_block *this,
 	struct nft_table *table;
 	struct net *net;
 
+	if (event == NETDEV_CHANGENAME) {
+		if (nf_tables_flowtable_event(this, NETDEV_REGISTER, ptr))
+			return NOTIFY_BAD;
+		event = NETDEV_UNREGISTER;
+	}
 	if (event != NETDEV_REGISTER &&
 	    event != NETDEV_UNREGISTER)
 		return NOTIFY_DONE;
net/netfilter/nft_chain_filter.c +5 -0
index 562af2773a66..0f5706addfcb 100644
@@ -373,6 +373,11 @@ static int nf_tables_netdev_event(struct notifier_block *this,
 		.net	= dev_net(dev),
 	};
 
+	if (event == NETDEV_CHANGENAME) {
+		if (nf_tables_netdev_event(this, NETDEV_REGISTER, ptr))
+			return NOTIFY_BAD;
+		event = NETDEV_UNREGISTER;
+	}
 	if (event != NETDEV_REGISTER &&
 	    event != NETDEV_UNREGISTER)
 		return NOTIFY_DONE;
Signature · 1 line
2.43.0
maintainer
#14 lore raw

Re: [nf-next PATCH v4 13/16] netfilter: nf_tables: Handle NETDEV_CHANGENAME events

Quoted 19 lines · Phil Sutter

For 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.

---
Changes since v3:
- Register first and handle errors to avoid having unregistered the device but registration fails.

---
 net/netfilter/nf_tables_api.c    | 5 +++++
 net/netfilter/nft_chain_filter.c | 5 +++++
 2 files changed, 10 insertions(+)
net/netfilter/nf_tables_api.c +0 -0
index 2684990dd3dc..4d40c1905735 100644

net/netfilter/nf_tables_api.c static int nf_tables_flowtable_event(struct notifier_block *this, · line 9378

Quoted 8 lines
net/netfilter/nf_tables_api.c +5 -0
@@ -9371,6 +9371,11 @@ static int nf_tables_flowtable_event(struct notifier_block *this,
 	struct nft_table *table;
 	struct net *net;
 
+	if (event == NETDEV_CHANGENAME) {
+		if (nf_tables_flowtable_event(this, NETDEV_REGISTER, ptr))
+			return NOTIFY_BAD;
+		event = NETDEV_UNREGISTER;
+	}

Consider flowtable that should claim devices "pv*". You get CHANGENAME, device name is, say, pv5.

Device name is registered in nf_tables_flowtable_event(). Then, event is set to UNREGISTER.

AFAICS this may unreg the device again immediately, as unreg part only compares device pointer and we can't be sure the device was part of any flowtable when CHANGENAME was triggered.

So I think nf_tables_flowtable_event() must handle CHANGENAME directly, first check if any flowtable holds the device at this time, then check if we need to register it with a new name, and do unreg only if it was previously part of any flowtable.

Same logic needed for netdev chains.

Does that make sense?

author reviewer
#15 lore raw

Re: [nf-next PATCH v4 13/16] netfilter: nf_tables: Handle NETDEV_CHANGENAME events

Quoted 39 lines · Florian Westphal
Quoted 19 lines · Phil Sutter

For 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.

---
Changes since v3:
- Register first and handle errors to avoid having unregistered the device but registration fails.

---
 net/netfilter/nf_tables_api.c    | 5 +++++
 net/netfilter/nft_chain_filter.c | 5 +++++
 2 files changed, 10 insertions(+)
net/netfilter/nf_tables_api.c +0 -0
index 2684990dd3dc..4d40c1905735 100644

net/netfilter/nf_tables_api.c static int nf_tables_flowtable_event(struct notifier_block *this, · line 9378

Quoted 8 lines
net/netfilter/nf_tables_api.c +5 -0
@@ -9371,6 +9371,11 @@ static int nf_tables_flowtable_event(struct notifier_block *this,
 	struct nft_table *table;
 	struct net *net;
 
+	if (event == NETDEV_CHANGENAME) {
+		if (nf_tables_flowtable_event(this, NETDEV_REGISTER, ptr))
+			return NOTIFY_BAD;
+		event = NETDEV_UNREGISTER;
+	}

Consider flowtable that should claim devices "pv*". You get CHANGENAME, device name is, say, pv5.

Device name is registered in nf_tables_flowtable_event(). Then, event is set to UNREGISTER.

AFAICS this may unreg the device again immediately, as unreg part only compares device pointer and we can't be sure the device was part of any flowtable when CHANGENAME was triggered.

Quoted 8 lines

So I think nf_tables_flowtable_event() must handle CHANGENAME directly, first check if any flowtable holds the device at this time, then check if we need to register it with a new name, and do unreg only if it was previously part of any flowtable.

Same logic needed for netdev chains.

Does that make sense?

Oh, you're right: Registering the device again (with new name) then searching *all* flowtables for the device and unregistering it will undo the previous registration, too! This obviously needs proper testing, too.

Thanks, Phil

author reviewer
#16 lore raw

PATCH v4 14/16 netfilter: nf_tables: Support wildcard netdev hook specs

In reply to Phil Sutter

User 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
index 4d40c1905735..ba2038ea56d2 100644
@@ -2222,7 +2222,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);
 
@@ -2230,24 +2230,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);
 }
@@ -2258,7 +2256,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;
 	}
 
@@ -9337,7 +9336,7 @@ 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))
+			if (strncmp(hook->ifname, dev->name, hook->ifnamelen))
 				continue;
 			ops = kzalloc(sizeof(struct nf_hook_ops),
 				      GFP_KERNEL_ACCOUNT);
net/netfilter/nft_chain_filter.c +1 -1
index 0f5706addfcb..f7290dc20a53 100644
@@ -338,7 +338,7 @@ 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))
+			if (strncmp(hook->ifname, dev->name, hook->ifnamelen))
 				continue;
 
 			ops = kmemdup(&basechain->ops,
Signature · 1 line
2.43.0
author reviewer
#17 lore raw

PATCH v4 15/16 netfilter: nf_tables: Add notications for hook changes

In reply to Phil Sutter

Notify 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.

---
 include/net/netfilter/nf_tables.h        |  2 +
 include/uapi/linux/netfilter/nf_tables.h |  5 +++
 net/netfilter/nf_tables_api.c            | 56 ++++++++++++++++++++++++
 net/netfilter/nft_chain_filter.c         |  1 +
 4 files changed, 64 insertions(+)
include/net/netfilter/nf_tables.h +2 -0
index eaf2f5184bdf..f8da38e45277 100644
@@ -1132,6 +1132,8 @@ int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
 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);
+void nf_tables_chain_device_notify(const struct nft_chain *chain,
+				   const struct net_device *dev, int event);
 
 enum nft_chain_types {
 	NFT_CHAIN_T_DEFAULT = 0,
include/uapi/linux/netfilter/nf_tables.h +5 -0
index d6476ca5d7a6..3a874febf1ac 100644
@@ -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,
 };
 
@@ -1772,6 +1774,9 @@ enum nft_synproxy_attributes {
 enum nft_devices_attributes {
 	NFTA_DEVICE_UNSPEC,
 	NFTA_DEVICE_NAME,
+	NFTA_DEVICE_TABLE,
+	NFTA_DEVICE_FLOWTABLE,
+	NFTA_DEVICE_CHAIN,
 	__NFTA_DEVICE_MAX
 };
 #define NFTA_DEVICE_MAX		(__NFTA_DEVICE_MAX - 1)
net/netfilter/nf_tables_api.c +56 -0

68 lines not on this page · raw message

net/netfilter/nft_chain_filter.c +1 -0
index f7290dc20a53..7a35b034d0c6 100644
@@ -357,6 +357,7 @@ 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(ctx->chain, dev, event);
 	}
 	return 0;
 }
Signature · 1 line
2.43.0
maintainer
#18 lore raw

Re: [nf-next PATCH v4 15/16] netfilter: nf_tables: Add notications for hook changes

Quoted 30 lines · Phil Sutter

Notify 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.

---
 include/net/netfilter/nf_tables.h        |  2 +
 include/uapi/linux/netfilter/nf_tables.h |  5 +++
 net/netfilter/nf_tables_api.c            | 56 ++++++++++++++++++++++++
 net/netfilter/nft_chain_filter.c         |  1 +
 4 files changed, 64 insertions(+)
include/net/netfilter/nf_tables.h +2 -0
index eaf2f5184bdf..f8da38e45277 100644
@@ -1132,6 +1132,8 @@ int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
 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);
+void nf_tables_chain_device_notify(const struct nft_chain *chain,
+				   const struct net_device *dev, int event);
 
 enum nft_chain_types {
 	NFT_CHAIN_T_DEFAULT = 0,
include/uapi/linux/netfilter/nf_tables.h +0 -0
index d6476ca5d7a6..3a874febf1ac 100644

include/uapi/linux/netfilter/nf_tables.h enum nf_tables_msg_types { · line 146

Quoted 5 lines
include/uapi/linux/netfilter/nf_tables.h +2 -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,

This relies on implicit NFNL_CB_UNSPEC == 0 and nfnetlink bailing out whe NFT_MSG_NEWDEV appears in a netlink message coming from userspace.

Is there precedence for this?
If not, maybe better to add explicit entries to the nf_tables_cb[] array?

AFAICS its fine as-is, nfnetlink won't blindly invoke NULL ->call() pointer, but I'm not sure this was designed to be this way or if this is a coincidence.

author reviewer
#19 lore raw

Re: [nf-next PATCH v4 15/16] netfilter: nf_tables: Add notications for hook changes

Quoted 24 lines · Florian Westphal
Quoted 23 lines · Phil Sutter

Notify 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.

---
 include/net/netfilter/nf_tables.h        |  2 +
 include/uapi/linux/netfilter/nf_tables.h |  5 +++
 net/netfilter/nf_tables_api.c            | 56 ++++++++++++++++++++++++
 net/netfilter/nft_chain_filter.c         |  1 +
 4 files changed, 64 insertions(+)
include/net/netfilter/nf_tables.h +2 -0
index eaf2f5184bdf..f8da38e45277 100644
@@ -1132,6 +1132,8 @@ int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
 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);
+void nf_tables_chain_device_notify(const struct nft_chain *chain,
+				   const struct net_device *dev, int event);
 
Quoted 16 lines

include/uapi/linux/netfilter/nf_tables.h enum nf_tables_msg_types { · line 146

Quoted 12 lines
include/net/netfilter/nf_tables.h +0 -0
 enum nft_chain_types {
 	NFT_CHAIN_T_DEFAULT = 0,
include/uapi/linux/netfilter/nf_tables.h +2 -0
index d6476ca5d7a6..3a874febf1ac 100644
@@ -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,

This relies on implicit NFNL_CB_UNSPEC == 0 and nfnetlink bailing out whe NFT_MSG_NEWDEV appears in a netlink message coming from userspace.

I guess with 'implicit NFNL_CB_UNSPEC == 0' you mean the extra nf_tables_cb array fields' 'type' value being 0 (nfnetlink.h explicitly defines NFNL_CB_UNSPEC value as 0). I don't see the connection here though, probably I miss nfnetlink_rcv_msg() relying on that field value or so.

I do see implicit dependency on attr_count field being 0 via nla_parse_deprecated().

Quoted 7 lines

Is there precedence for this?
If not, maybe better to add explicit entries to the nf_tables_cb[] array?

AFAICS its fine as-is, nfnetlink won't blindly invoke NULL ->call() pointer, but I'm not sure this was designed to be this way or if this is a coincidence.

I see at least NFNL_MSG_ACCT_OVERQUOTA missing from nfnl_acct_cb. The former was introduced in 2014. May I claim grandfathering? ;)

Cheers, Phil

maintainer
#20 lore raw

Re: [nf-next PATCH v4 15/16] netfilter: nf_tables: Add notications for hook changes

Quoted 9 lines · Phil Sutter
Quoted 3 lines

This relies on implicit NFNL_CB_UNSPEC == 0 and nfnetlink bailing out whe NFT_MSG_NEWDEV appears in a netlink message coming from userspace.

I guess with 'implicit NFNL_CB_UNSPEC == 0' you mean the extra nf_tables_cb array fields' 'type' value being 0 (nfnetlink.h explicitly defines NFNL_CB_UNSPEC value as 0). I don't see the connection here though, probably I miss nfnetlink_rcv_msg() relying on that field value or so.

I should have been more clear, I was wondering if we need/want an -EOPNOTSUPP stub callback rather than reliance of nfnetlink to detect it.

Quoted 2 lines

I see at least NFNL_MSG_ACCT_OVERQUOTA missing from nfnl_acct_cb. The former was introduced in 2014. May I claim grandfathering? ;)

I guess it just means "no we don't worry about it".

author reviewer
#21 lore raw

Re: [nf-next PATCH v4 15/16] netfilter: nf_tables: Add notications for hook changes

Quoted 11 lines · Florian Westphal
Quoted 9 lines · Phil Sutter
Quoted 3 lines

This relies on implicit NFNL_CB_UNSPEC == 0 and nfnetlink bailing out whe NFT_MSG_NEWDEV appears in a netlink message coming from userspace.

I guess with 'implicit NFNL_CB_UNSPEC == 0' you mean the extra nf_tables_cb array fields' 'type' value being 0 (nfnetlink.h explicitly defines NFNL_CB_UNSPEC value as 0). I don't see the connection here though, probably I miss nfnetlink_rcv_msg() relying on that field value or so.

Quoted 3 lines

I should have been more clear, I was wondering if we need/want an -EOPNOTSUPP stub callback rather than reliance of nfnetlink to detect it.

Sure, I got your point. The NFNL_CB_UNSPEC reference was just a bit confusing.

Quoted 4 lines
Quoted 2 lines

I see at least NFNL_MSG_ACCT_OVERQUOTA missing from nfnl_acct_cb. The former was introduced in 2014. May I claim grandfathering? ;)

I guess it just means "no we don't worry about it".

Maybe. At least we rely upon the behaviour for a while now, possibly by accident.

We could get rid of the nc->call != NULL check by assigning such stub in nfnetlink_subsys_register(). OK, technically it would just move the NULL check. Without such stunts, nfnetlink_rcv_msg() would have to remain as-is to cover for future users with holes, right?

Cheers, Phil

maintainer
#22 lore raw

Re: [nf-next PATCH v4 15/16] netfilter: nf_tables: Add notications for hook changes

Quoted 4 lines · Phil Sutter

We could get rid of the nc->call != NULL check by assigning such stub in nfnetlink_subsys_register(). OK, technically it would just move the NULL check. Without such stunts, nfnetlink_rcv_msg() would have to remain as-is to cover for future users with holes, right?

I think we should leave it as-is, I thought this was the first use of NULL .call but as you found thats not the case.

author reviewer
#23 lore raw

PATCH v4 16/16 selftests: netfilter: Torture nftables netdev hooks

In reply to Phil Sutter

Add 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.

---
 .../testing/selftests/net/netfilter/Makefile  |   1 +
 .../net/netfilter/nft_interface_stress.sh     | 149 ++++++++++++++++++
 2 files changed, 150 insertions(+)
 create mode 100755 tools/testing/selftests/net/netfilter/nft_interface_stress.sh
tools/testing/selftests/net/netfilter/Makefile +1 -0
index d13fb5ea3e89..823e0acf7171 100644
@@ -21,6 +21,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 +149 -0

149 lines not on this page · raw message

Signature · 1 line
2.43.0