Skip to contentSkip to conversation

Dynamic hook interface binding

PATCH Superseded Phil Sutter 3 participants 38 messages Sep 12, 2024 – Oct 31, 2024

Subsystems (3)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

v3 cover Dynamic hook interface binding

Changes since v2:
- Practically complete rewrite with wildcard interface spec support

The first two patches of this series are fixes to existing code but cause conflicts if not applied in order. They may go into nf tree as well, though only the first one is a real bug and seems to be of low impact.

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 6 eliminates a quirk in netdev-family chain netdev event handler, aligns behaviour with flowtables and paves the way for following changes.

Patches 7-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: Keep deleted flowtable hooks until after RCU
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: 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                     |   2 +
 include/net/netfilter/nf_tables.h             |  11 +-
 include/uapi/linux/netfilter/nf_tables.h      |   5 +
 net/netfilter/nf_tables_api.c                 | 386 +++++++++++++-----
 net/netfilter/nf_tables_offload.c             |  51 ++-
 net/netfilter/nft_chain_filter.c              |  64 +--
 net/netfilter/nft_flow_offload.c              |   2 +-
 .../testing/selftests/net/netfilter/Makefile  |   1 +
 .../net/netfilter/nft_interface_stress.sh     | 149 +++++++
 9 files changed, 508 insertions(+), 163 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 v3 1/16 netfilter: nf_tables: Keep deleted flowtable hooks until after RCU

Documentation of list_del_rcu() warns callers to not immediately free the deleted list item. While it seems not necessary to use the RCU-variant of list_del() here in the first place, doing so seems to require calling kfree_rcu() on the deleted item as well.

Fixes 3f0465a9ef02 ("netfilter: nf_tables: dynamically allocate hooks per net_device in flowtables")
---
 net/netfilter/nf_tables_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +1 -1
index b6547fe22bd8..2982f49b6d55 100644
@@ -9180,7 +9180,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(hook);
+		kfree_rcu(hook, rcu);
 	}
 	kfree(flowtable->name);
 	module_put(flowtable->data.type->owner);
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [nf-next PATCH v3 01/16] netfilter: nf_tables: Keep deleted flowtable hooks until after RCU

Quoted 16 lines · Phil Sutter

Documentation of list_del_rcu() warns callers to not immediately free the deleted list item. While it seems not necessary to use the RCU-variant of list_del() here in the first place, doing so seems to require calling kfree_rcu() on the deleted item as well.

Fixes 3f0465a9ef02 ("netfilter: nf_tables: dynamically allocate hooks per net_device in flowtables")
---
 net/netfilter/nf_tables_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +0 -0
index b6547fe22bd8..2982f49b6d55 100644

net/netfilter/nf_tables_api.c static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) · line 9186

Quoted 8 lines
net/netfilter/nf_tables_api.c +1 -1
@@ -9180,7 +9180,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(hook);
+		kfree_rcu(hook, rcu);
 	}
 	kfree(flowtable->name);
 	module_put(flowtable->data.type->owner);

AFAICS its safe to use list_del() everywhere, I can't find a single instance where the hooks are iterated without mutex serialization.

nf_tables_flowtable_destroy() is called after the hook has been unregisted (detached from nf_hook list) and rcu grace period elapsed.

author reviewer
#3 lore raw

Re: [nf-next PATCH v3 01/16] netfilter: nf_tables: Keep deleted flowtable hooks until after RCU

Quoted 29 lines · Florian Westphal
Quoted 16 lines · Phil Sutter

Documentation of list_del_rcu() warns callers to not immediately free the deleted list item. While it seems not necessary to use the RCU-variant of list_del() here in the first place, doing so seems to require calling kfree_rcu() on the deleted item as well.

Fixes 3f0465a9ef02 ("netfilter: nf_tables: dynamically allocate hooks per net_device in flowtables")
---
 net/netfilter/nf_tables_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +0 -0
index b6547fe22bd8..2982f49b6d55 100644

net/netfilter/nf_tables_api.c static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) · line 9186

Quoted 8 lines
net/netfilter/nf_tables_api.c +1 -1
@@ -9180,7 +9180,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(hook);
+		kfree_rcu(hook, rcu);
 	}
 	kfree(flowtable->name);
 	module_put(flowtable->data.type->owner);

AFAICS its safe to use list_del() everywhere, I can't find a single instance where the hooks are iterated without mutex serialization.

Quoted 2 lines

nf_tables_flowtable_destroy() is called after the hook has been unregisted (detached from nf_hook list) and rcu grace period elapsed.

Yes, I didn't find a caller which didn't synchronize_rcu() before calling it. Same applies to chain hooks, right? I'd drop all the _rcu() calls and give it a try, but the resulting race conditions may be hard to trigger.

Cheers, Phil

maintainer
#4 lore raw

Re: [nf-next PATCH v3 01/16] netfilter: nf_tables: Keep deleted flowtable hooks until after RCU

Quoted 5 lines · Phil Sutter
Quoted 2 lines

nf_tables_flowtable_destroy() is called after the hook has been unregisted (detached from nf_hook list) and rcu grace period elapsed.

Yes, I didn't find a caller which didn't synchronize_rcu() before calling it. Same applies to chain hooks, right?

Sigh, there is nft_flowtable_find_dev() which iterates the nft_hook list from packet path.

So the syncrhonize_rcu is irrelevant as long as the entry is linked up and this patch is correct as-is.

list_del_rcu(&hook->list);
kfree(hook);

is illegal, and I think this should add a helper that unlinks and then frees the entry via kfree_rcu and converts all instances of this pattern.

#5 lore raw

Re: [nf-next PATCH v3 01/16] netfilter: nf_tables: Keep deleted flowtable hooks until after RCU

In reply to Florian Westphal
Quoted 12 lines · Florian Westphal
Quoted 11 lines · Phil Sutter

Documentation of list_del_rcu() warns callers to not immediately free the deleted list item. While it seems not necessary to use the RCU-variant of list_del() here in the first place, doing so seems to require calling kfree_rcu() on the deleted item as well.

Fixes 3f0465a9ef02 ("netfilter: nf_tables: dynamically allocate hooks per net_device in flowtables")
---
 net/netfilter/nf_tables_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

net/netfilter/nf_tables_api.c static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) · line 9183

Quoted 10 lines
net/netfilter/nf_tables_api.c +1 -1
index b6547fe22bd8..2982f49b6d55 100644
@@ -9180,7 +9180,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(hook);
+		kfree_rcu(hook, rcu);

This looks correct to me.

Quoted 6 lines

net/netfilter/nf_tables_api.c static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) · line 9186

Quoted 3 lines
net/netfilter/nf_tables_api.c +0 -0
 	}
 	kfree(flowtable->name);
 	module_put(flowtable->data.type->owner);

AFAICS its safe to use list_del() everywhere, I can't find a single instance where the hooks are iterated without mutex serialization.

Netlink dump path is lockless.

nft_dump_basechain_hook() is missing list_for_each_entry_rcu() for list iteration, that needs a fix.

nf_tables_fill_flowtable_info() does use list_for_each_entry_rcu().

Quoted 2 lines

nf_tables_flowtable_destroy() is called after the hook has been unregisted (detached from nf_hook list) and rcu grace period elapsed.

#6 lore raw

Re: [nf-next PATCH v3 01/16] netfilter: nf_tables: Keep deleted flowtable hooks until after RCU

Quoted 33 lines · Pablo Neira Ayuso
Quoted 12 lines · Florian Westphal
Quoted 11 lines · Phil Sutter

Documentation of list_del_rcu() warns callers to not immediately free the deleted list item. While it seems not necessary to use the RCU-variant of list_del() here in the first place, doing so seems to require calling kfree_rcu() on the deleted item as well.

Fixes 3f0465a9ef02 ("netfilter: nf_tables: dynamically allocate hooks per net_device in flowtables")
---
 net/netfilter/nf_tables_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

net/netfilter/nf_tables_api.c static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) · line 9183

Quoted 10 lines
net/netfilter/nf_tables_api.c +1 -1
index b6547fe22bd8..2982f49b6d55 100644
@@ -9180,7 +9180,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(hook);
+		kfree_rcu(hook, rcu);

This looks correct to me.

Quoted 6 lines

net/netfilter/nf_tables_api.c static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) · line 9186

Quoted 3 lines
net/netfilter/nf_tables_api.c +0 -0
 	}
 	kfree(flowtable->name);
 	module_put(flowtable->data.type->owner);

AFAICS its safe to use list_del() everywhere, I can't find a single instance where the hooks are iterated without mutex serialization.

Quoted 6 lines

Netlink dump path is lockless.

nft_dump_basechain_hook() is missing list_for_each_entry_rcu() for list iteration, that needs a fix.

nf_tables_fill_flowtable_info() does use list_for_each_entry_rcu().

I'd suggest to start by sending fixes for nf.git to address these two issues.

Quoted 2 lines
Quoted 2 lines

nf_tables_flowtable_destroy() is called after the hook has been unregisted (detached from nf_hook list) and rcu grace period elapsed.

#7 lore raw

Re: [nf-next PATCH v3 01/16] netfilter: nf_tables: Keep deleted flowtable hooks until after RCU

In reply to Phil Sutter
Quoted 4 lines · Phil Sutter

Documentation of list_del_rcu() warns callers to not immediately free the deleted list item. While it seems not necessary to use the RCU-variant of list_del() here in the first place, doing so seems to require calling kfree_rcu() on the deleted item as well.

LGTM, I plan to take this into nf.git

Thanks.

Quoted 22 lines
Fixes 3f0465a9ef02 ("netfilter: nf_tables: dynamically allocate hooks per net_device in flowtables")
---
 net/netfilter/nf_tables_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +1 -1
index b6547fe22bd8..2982f49b6d55 100644
@@ -9180,7 +9180,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(hook);
+		kfree_rcu(hook, rcu);
 	}
 	kfree(flowtable->name);
 	module_put(flowtable->data.type->owner);
Signature · 1 line
2.43.0
author reviewer
#8 lore raw

PATCH v3 2/16 netfilter: nf_tables: Flowtable hook's pf value never varies

In reply to Phil Sutter

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 2982f49b6d55..3ffb728309af 100644
@@ -8544,7 +8544,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;
 
@@ -8553,12 +8553,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
#9 lore raw

PATCH v3 3/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.

---
 include/net/netfilter/nf_tables.h | 2 ++
 net/netfilter/nf_tables_api.c     | 6 +++---
 2 files changed, 5 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
index c302b396e1a7..efd6b55b4914 100644
@@ -1191,6 +1191,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 +3 -3
index 3ffb728309af..f1710aab5188 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,13 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
 		goto err_hook_alloc;
 	}
 
-	nla_strscpy(ifname, attr, IFNAMSIZ);
+	nla_strscpy(hook->ifname, attr, IFNAMSIZ);
+	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
maintainer
#10 lore raw

Re: [nf-next PATCH v3 03/16] netfilter: nf_tables: Store user-defined hook ifname

Quoted 36 lines · 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.

---
 include/net/netfilter/nf_tables.h | 2 ++
 net/netfilter/nf_tables_api.c     | 6 +++---
 2 files changed, 5 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
index c302b396e1a7..efd6b55b4914 100644
@@ -1191,6 +1191,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 +0 -1
index 3ffb728309af..f1710aab5188 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;
 

net/netfilter/nf_tables_api.c static struct nft_hook *nft_netdev_hook_alloc(struct net *net, · line 2186

Quoted 6 lines
net/netfilter/nf_tables_api.c +2 -1
@@ -2183,12 +2182,13 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
 		goto err_hook_alloc;
 	}
 
-	nla_strscpy(ifname, attr, IFNAMSIZ);
+	nla_strscpy(hook->ifname, attr, IFNAMSIZ);
+	hook->ifnamelen = nla_len(attr);

Hmm. nft_netdev_hook_alloc has no netlink attribute policy validation :-/

Can you add another patch that fixes this up?

I'd suggest to move the if (nla_type(tmp) != NFTA_DEVICE_NAME) test from nf_tables_parse_netdev_hooks() into nft_netdev_hook_alloc so nft_chain_parse_netdev() also has this test.

Then,

net/netfilter/nf_tables_api.c static struct nft_hook *nft_netdev_hook_alloc(struct net *net, · line 2186

Quoted 1 line
net/netfilter/nf_tables_api.c +0 -1
-     nla_strscpy(ifname, attr, IFNAMSIZ);

Into:

	err = nla_strscpy(ifname, attr, IFNAMSIZ)
	if (err < 0)
		goto err_hook_dev;

so we validate that
a) attr is NFTA_DEVICE_NAME
b) length doesn't exceed IFNAMSIZ.

ATM this check is implicit because nla_strscpy() always null-terminates and the next line will check that the device with this name actually exists.

But that check is removed later.
This patch can then set

hook->ifnamelen = err

without risk that nla_len() returns 0xfff0 or some other bogus value.

author reviewer
#11 lore raw

Re: [nf-next PATCH v3 03/16] netfilter: nf_tables: Store user-defined hook ifname

Quoted 48 lines · Florian Westphal
Quoted 36 lines · 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.

---
 include/net/netfilter/nf_tables.h | 2 ++
 net/netfilter/nf_tables_api.c     | 6 +++---
 2 files changed, 5 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
index c302b396e1a7..efd6b55b4914 100644
@@ -1191,6 +1191,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 +0 -1
index 3ffb728309af..f1710aab5188 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;
 

net/netfilter/nf_tables_api.c static struct nft_hook *nft_netdev_hook_alloc(struct net *net, · line 2186

Quoted 6 lines
net/netfilter/nf_tables_api.c +2 -1
@@ -2183,12 +2182,13 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
 		goto err_hook_alloc;
 	}
 
-	nla_strscpy(ifname, attr, IFNAMSIZ);
+	nla_strscpy(hook->ifname, attr, IFNAMSIZ);
+	hook->ifnamelen = nla_len(attr);

Hmm. nft_netdev_hook_alloc has no netlink attribute policy validation :-/

Quoted 6 lines

Can you add another patch that fixes this up?

I'd suggest to move the if (nla_type(tmp) != NFTA_DEVICE_NAME) test from nf_tables_parse_netdev_hooks() into nft_netdev_hook_alloc so nft_chain_parse_netdev() also has this test.

I fear this won't work, because nft_chain_parse_netdev() may call nft_netdev_hook_alloc() directly, passing tb[NFTA_HOOK_DEV]. For NFTA_HOOK_DEVS though, it calls nf_tables_parse_netdev_hooks() and thus the nested attribute type check in there applies.

Quoted 4 lines

Then,

net/netfilter/nf_tables_api.c static struct nft_hook *nft_netdev_hook_alloc(struct net *net, · line 2186

Quoted 1 line
net/netfilter/nf_tables_api.c +0 -1
-     nla_strscpy(ifname, attr, IFNAMSIZ);
Quoted 8 lines

Into:

	err = nla_strscpy(ifname, attr, IFNAMSIZ)
	if (err < 0)
		goto err_hook_dev;

so we validate that
a) attr is NFTA_DEVICE_NAME

As said above, it may be NFTA_HOOK_DEV as well.

Quoted 1 line

b) length doesn't exceed IFNAMSIZ.

ACK. I think a) is already being asserted in spots where NFTA_HOOK_DEV is not expected (which in turn has a policy).

Quoted 11 lines

ATM this check is implicit because nla_strscpy() always null-terminates and the next line will check that the device with this name actually exists.

But that check is removed later.
This patch can then set

hook->ifnamelen = err

without risk that nla_len() returns 0xfff0 or some other bogus value.

Ah, nice!

From my perspective, all it takes is the nla_strscpy() return value check in this patch to cover everything. Right?

Thanks, Phil

maintainer
#12 lore raw

Re: [nf-next PATCH v3 03/16] netfilter: nf_tables: Store user-defined hook ifname

Quoted 8 lines · Phil Sutter
Quoted 3 lines

I'd suggest to move the if (nla_type(tmp) != NFTA_DEVICE_NAME) test from nf_tables_parse_netdev_hooks() into nft_netdev_hook_alloc so nft_chain_parse_netdev() also has this test.

I fear this won't work, because nft_chain_parse_netdev() may call nft_netdev_hook_alloc() directly, passing tb[NFTA_HOOK_DEV]. For NFTA_HOOK_DEVS though, it calls nf_tables_parse_netdev_hooks() and thus the nested attribute type check in there applies.

Ouch, you are right, it can be called with either attribute :-(

Quoted 5 lines
Quoted 5 lines

Then,

Quoted 1 line

- nla_strscpy(ifname, attr, IFNAMSIZ);

Into:

Quoted 8 lines
Quoted 6 lines
	err = nla_strscpy(ifname, attr, IFNAMSIZ)
	if (err < 0)
		goto err_hook_dev;

so we validate that
a) attr is NFTA_DEVICE_NAME

As said above, it may be NFTA_HOOK_DEV as well.

Yes, my bad.

Quoted 4 lines
Quoted 1 line

b) length doesn't exceed IFNAMSIZ.

ACK. I think a) is already being asserted in spots where NFTA_HOOK_DEV is not expected (which in turn has a policy).

Yes, I did not see that this is using different attributes.

Quoted 9 lines
Quoted 9 lines

ATM this check is implicit because nla_strscpy() always null-terminates and the next line will check that the device with this name actually exists.

But that check is removed later.
This patch can then set

hook->ifnamelen = err

Quoted 7 lines
Quoted 2 lines

without risk that nla_len() returns 0xfff0 or some other bogus value.

Ah, nice!

From my perspective, all it takes is the nla_strscpy() return value check in this patch to cover everything. Right?

Yes, right. So no extra patch is needd.

author reviewer
#13 lore raw

PATCH v3 4/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 f1710aab5188..4fb230e4afe3 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);
@@ -8968,7 +8969,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
#14 lore raw

PATCH v3 5/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 4fb230e4afe3..457696f55003 100644
@@ -2210,7 +2210,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
#15 lore raw

PATCH v3 6/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 efd6b55b4914..16daffcee0e1 100644
@@ -1228,8 +1228,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 457696f55003..46d4e9056bf1 100644
@@ -11386,27 +11386,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
maintainer
#16 lore raw

Re: [nf-next PATCH v3 06/16] netfilter: nf_tables: Tolerate chains with no remaining hooks

Quoted 9 lines · 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.

This "breaks"
W: [DUMP FAIL] 1/2 tests/shell/testcases/json/netdev
W: [DUMP FAIL] 2/2 tests/shell/testcases/chains/netdev_chain_0

any suggestions on how to handle this?

We can't fix the dump because old kernel will axe the empty basechain. Should the dump files be removed?

author reviewer
#17 lore raw

Re: [nf-next PATCH v3 06/16] netfilter: nf_tables: Tolerate chains with no remaining hooks

Quoted 11 lines · Florian Westphal
Quoted 9 lines · 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.

Quoted 7 lines

This "breaks"
W: [DUMP FAIL] 1/2 tests/shell/testcases/json/netdev
W: [DUMP FAIL] 2/2 tests/shell/testcases/chains/netdev_chain_0

any suggestions on how to handle this?

We can't fix the dump because old kernel will axe the empty basechain.

AFAIR, we did just that in the past with such cases. I agree, it pretty much breaks any efforts at making the testsuite usable with stable kernels.

Quoted 1 line

Should the dump files be removed?

Maybe "feature flag" it and introduce a mechanism for test cases to revert to a different dump file?

Or we convince Pablo to axe his efforts at fixing chain deletion in stable kernels and instead backport my "zombie chain" feature. ;)

Cheers, Phil

maintainer
#18 lore raw

Re: [nf-next PATCH v3 06/16] netfilter: nf_tables: Tolerate chains with no remaining hooks

Quoted 8 lines · Phil Sutter

AFAIR, we did just that in the past with such cases. I agree, it pretty much breaks any efforts at making the testsuite usable with stable kernels.

Quoted 1 line

Should the dump files be removed?

Maybe "feature flag" it and introduce a mechanism for test cases to revert to a different dump file?

What could be good enough:
1. fix dump files
2. add feature flag, if feature not present -> exit 77/skip to elide dump compare.

Would you explore this?
Thanks!

author reviewer
#19 lore raw

Re: [nf-next PATCH v3 06/16] netfilter: nf_tables: Tolerate chains with no remaining hooks

Quoted 7 lines · Florian Westphal
Quoted 6 lines · Phil Sutter

AFAIR, we did just that in the past with such cases. I agree, it pretty much breaks any efforts at making the testsuite usable with stable kernels.

Quoted 1 line

Should the dump files be removed?

Quoted 7 lines
Quoted 2 lines

Maybe "feature flag" it and introduce a mechanism for test cases to revert to a different dump file?

What could be good enough:
1. fix dump files
2. add feature flag, if feature not present -> exit 77/skip to elide dump compare.

Reasonably simple to provide backwards compat (and still keeping the actual test active)!

Quoted 1 line

Would you explore this?

Will do!

Thanks, Phil

author reviewer
#20 lore raw

PATCH v3 7/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.

---
 net/netfilter/nf_tables_api.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
net/netfilter/nf_tables_api.c +22 -12
index 46d4e9056bf1..dedf50ba266c 100644
@@ -2134,6 +2134,16 @@ static void nf_tables_chain_free_chain_rules(struct nft_chain *chain)
 	kvfree(chain->blob_next);
 }
 
+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);
+}
+
 void nf_tables_chain_destroy(struct nft_chain *chain)
 {
 	const struct nft_table *table = chain->table;
@@ -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);
@@ -2240,7 +2250,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;
 		}
@@ -2258,7 +2268,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;
 }
@@ -2401,7 +2411,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);
 }
@@ -2691,7 +2701,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 {
@@ -2812,7 +2822,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);
 	}
@@ -8586,7 +8596,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;
@@ -8598,7 +8608,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);
 	}
 }
 
@@ -8622,7 +8632,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);
 		}
 	}
 
@@ -8669,7 +8679,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;
@@ -8815,7 +8825,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);
 	}
 }
 
@@ -9179,7 +9189,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
#21 lore raw

PATCH v3 8/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 16daffcee0e1..be11518646a3 100644
@@ -1195,6 +1195,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 dedf50ba266c..65db4c54cfae 100644
@@ -9222,13 +9222,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 9dcd1548df9d..646192321265 100644
@@ -174,7 +174,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
#22 lore raw

PATCH v3 9/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 65db4c54cfae..dc30d2be09fb 100644
@@ -8550,6 +8550,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,
@@ -8570,20 +8590,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
#23 lore raw

PATCH v3 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 potentially 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.

---
 include/linux/netfilter.h         |   2 +
 include/net/netfilter/nf_tables.h |   2 +-
 net/netfilter/nf_tables_api.c     | 155 +++++++++++++++++++++---------
 net/netfilter/nf_tables_offload.c |  49 ++++++----
 net/netfilter/nft_chain_filter.c  |   6 +-
 5 files changed, 143 insertions(+), 71 deletions(-)
include/linux/netfilter.h +2 -0
index 2683b2b77612..1318f18784ab 100644
@@ -95,6 +95,8 @@ enum nf_hook_ops_type {
 };
 
 struct nf_hook_ops {
+	struct list_head	list;
+
 	/* User fills in from here down. */
 	nf_hookfn		*hook;
 	struct net_device	*dev;
include/net/netfilter/nf_tables.h +1 -1
index be11518646a3..991b8d5e52f1 100644
@@ -1189,7 +1189,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 +110 -45

319 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..2507e3beac5c 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(&ops->list);
+		kfree(ops);
 	}
 }
 
Signature · 1 line
2.43.0
author reviewer
#24 lore raw

PATCH v3 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.

---
 net/netfilter/nft_chain_filter.c | 40 +++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 8 deletions(-)
net/netfilter/nft_chain_filter.c +32 -8
index 2507e3beac5c..ec44c27a9d91 100644
@@ -326,14 +326,37 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,
 	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(&ops->list);
-		kfree(ops);
+			if (!(ctx->chain->table->flags & NFT_TABLE_F_DORMANT))
+				nf_unregister_net_hook(ctx->net, ops);
+			list_del(&ops->list);
+			kfree(ops);
+			break;
+		case NETDEV_REGISTER:
+			if (strcmp(hook->ifname, dev->name))
+				continue;
+			ops = kzalloc(sizeof(struct nf_hook_ops),
+				      GFP_KERNEL_ACCOUNT);
+			if (ops) {
+				memcpy(ops, &basechain->ops, sizeof(*ops));
+				ops->dev = dev;
+			}
+			if (ops &&
+			    (ctx->chain->table->flags & NFT_TABLE_F_DORMANT ||
+			     !nf_register_net_hook(dev_net(dev), ops))) {
+				list_add_tail(&ops->list, &hook->ops_list);
+				break;
+			}
+			printk(KERN_ERR "chain %s: Can't hook into device %s\n",
+			       ctx->chain->name, dev->name);
+			kfree(ops);
+			continue;
+		}
 	}
 }
 
@@ -349,7 +372,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);
Signature · 1 line
2.43.0
maintainer
#25 lore raw

Re: [nf-next PATCH v3 11/16] netfilter: nf_tables: chain: Respect NETDEV_REGISTER events

Quoted 22 lines · Phil Sutter

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

---
 net/netfilter/nft_chain_filter.c | 40 +++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 8 deletions(-)
net/netfilter/nft_chain_filter.c +4 -3
index 2507e3beac5c..ec44c27a9d91 100644
@@ -326,14 +326,37 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,
 	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)

net/netfilter/nft_chain_filter.c static void nft_netdev_event(unsigned long event, struct net_device *dev, · line 338

Quoted 10 lines
net/netfilter/nft_chain_filter.c +5 -4
+				continue;
 
-		if (!(ctx->chain->table->flags & NFT_TABLE_F_DORMANT))
-			nf_unregister_net_hook(ctx->net, ops);
-		list_del(&ops->list);
-		kfree(ops);
+			if (!(ctx->chain->table->flags & NFT_TABLE_F_DORMANT))
+				nf_unregister_net_hook(ctx->net, ops);
+			list_del(&ops->list);
+			kfree(ops);

This needs to use kfree_rcu() + list_del_rcu, nf_unregister_net_hook only stops new packets from executing for dev, it doesn't stop new packets.

Or is this guaranteed by UNREGISTER event already? If so, please add a comment.

net/netfilter/nft_chain_filter.c static void nft_netdev_event(unsigned long event, struct net_device *dev, · line 344

Quoted 6 lines
net/netfilter/nft_chain_filter.c +6 -0
+			break;
+		case NETDEV_REGISTER:
+			if (strcmp(hook->ifname, dev->name))
+				continue;
+			ops = kzalloc(sizeof(struct nf_hook_ops),
+				      GFP_KERNEL_ACCOUNT);

ops = kmemdup(&basechain->ops, .. ?

net/netfilter/nft_chain_filter.c static void nft_netdev_event(unsigned long event, struct net_device *dev, · line 356

Quoted 12 lines
net/netfilter/nft_chain_filter.c +12 -0
+			if (ops) {
+				memcpy(ops, &basechain->ops, sizeof(*ops));
+				ops->dev = dev;
+			}
+			if (ops &&
+			    (ctx->chain->table->flags & NFT_TABLE_F_DORMANT ||
+			     !nf_register_net_hook(dev_net(dev), ops))) {
+				list_add_tail(&ops->list, &hook->ops_list);
+				break;
+			}
+			printk(KERN_ERR "chain %s: Can't hook into device %s\n",
+			       ctx->chain->name, dev->name);

I think its better to -ENOMEM and veto the netdevice register request in this case.

I also think this needs extra handling for NETDEV_CHANGENAME rather than the 'treat as UNREG+REG' trick.

Else we may unregister and then fail to re-register which leaves the device without the registered hook op.

author reviewer
#26 lore raw

Re: [nf-next PATCH v3 11/16] netfilter: nf_tables: chain: Respect NETDEV_REGISTER events

Quoted 38 lines · Florian Westphal
Quoted 22 lines · Phil Sutter

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

---
 net/netfilter/nft_chain_filter.c | 40 +++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 8 deletions(-)
net/netfilter/nft_chain_filter.c +4 -3
index 2507e3beac5c..ec44c27a9d91 100644
@@ -326,14 +326,37 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,
 	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)

net/netfilter/nft_chain_filter.c static void nft_netdev_event(unsigned long event, struct net_device *dev, · line 338

Quoted 10 lines
net/netfilter/nft_chain_filter.c +5 -4
+				continue;
 
-		if (!(ctx->chain->table->flags & NFT_TABLE_F_DORMANT))
-			nf_unregister_net_hook(ctx->net, ops);
-		list_del(&ops->list);
-		kfree(ops);
+			if (!(ctx->chain->table->flags & NFT_TABLE_F_DORMANT))
+				nf_unregister_net_hook(ctx->net, ops);
+			list_del(&ops->list);
+			kfree(ops);

This needs to use kfree_rcu() + list_del_rcu, nf_unregister_net_hook only stops new packets from executing for dev, it doesn't stop new packets.

Quoted 2 lines

Or is this guaranteed by UNREGISTER event already? If so, please add a comment.

Are packets relevant here? The question should be whether another CPU traverses hook->ops_list at the same time, no? Looking at nft_flowtable_find_dev() mentioned in your other mail, there seems to be a case which doesn't synchronize on commit_mutex. So same rules apply to ops_list as for hook_list and thus I need to add an rcu_head to nf_hook_ops as well?

Quoted 8 lines

net/netfilter/nft_chain_filter.c static void nft_netdev_event(unsigned long event, struct net_device *dev, · line 344

Quoted 6 lines
net/netfilter/nft_chain_filter.c +6 -0
+			break;
+		case NETDEV_REGISTER:
+			if (strcmp(hook->ifname, dev->name))
+				continue;
+			ops = kzalloc(sizeof(struct nf_hook_ops),
+				      GFP_KERNEL_ACCOUNT);

ops = kmemdup(&basechain->ops, .. ?

Oh, sure!

Quoted 6 lines
Quoted 6 lines
net/netfilter/nft_chain_filter.c +6 -0
+			if (ops) {
+				memcpy(ops, &basechain->ops, sizeof(*ops));
+				ops->dev = dev;
+			}
+			if (ops &&
+			    (ctx->chain->table->flags & NFT_TABLE_F_DORMANT ||
Quoted 8 lines

net/netfilter/nft_chain_filter.c static void nft_netdev_event(unsigned long event, struct net_device *dev, · line 356

Quoted 6 lines
net/netfilter/nft_chain_filter.c +6 -0
+			     !nf_register_net_hook(dev_net(dev), ops))) {
+				list_add_tail(&ops->list, &hook->ops_list);
+				break;
+			}
+			printk(KERN_ERR "chain %s: Can't hook into device %s\n",
+			       ctx->chain->name, dev->name);

I think its better to -ENOMEM and veto the netdevice register request in this case.

Ah, I wasn't aware we may influence netdev registration from a notifier. So I'll change the callbacks to return NOTIFY_BAD in error case.

Quoted 5 lines

I also think this needs extra handling for NETDEV_CHANGENAME rather than the 'treat as UNREG+REG' trick.

Else we may unregister and then fail to re-register which leaves the device without the registered hook op.

So search for another flowtable/chain with a hook matching the new name first, then unregister, try to register in the new spot and undo on failure? Sounds doable. :)

Thanks, Phil

maintainer
#27 lore raw

Re: [nf-next PATCH v3 11/16] netfilter: nf_tables: chain: Respect NETDEV_REGISTER events

Quoted 5 lines · Phil Sutter
Quoted 2 lines

Or is this guaranteed by UNREGISTER event already? If so, please add a comment.

Are packets relevant here? The question should be whether another CPU traverses hook->ops_list at the same time, no?

Yes, but also if anyone else can look at the structure in parallel.

Quoted 5 lines

Looking at
nft_flowtable_find_dev() mentioned in your other mail, there seems to be a case which doesn't synchronize on commit_mutex. So same rules apply to ops_list as for hook_list and thus I need to add an rcu_head to nf_hook_ops as well?

I will need to apply your series locally first to get the full picture, sorry.

Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
net/netfilter/nft_chain_filter.c +6 -0
+			if (ops) {
+				memcpy(ops, &basechain->ops, sizeof(*ops));
+				ops->dev = dev;
+			}
+			if (ops &&
+			    (ctx->chain->table->flags & NFT_TABLE_F_DORMANT ||
Quoted 8 lines

net/netfilter/nft_chain_filter.c static void nft_netdev_event(unsigned long event, struct net_device *dev, · line 356

Quoted 6 lines
net/netfilter/nft_chain_filter.c +6 -0
+			     !nf_register_net_hook(dev_net(dev), ops))) {
+				list_add_tail(&ops->list, &hook->ops_list);
+				break;
+			}
+			printk(KERN_ERR "chain %s: Can't hook into device %s\n",
+			       ctx->chain->name, dev->name);

I think its better to -ENOMEM and veto the netdevice register request in this case.

Ah, I wasn't aware we may influence netdev registration from a notifier. So I'll change the callbacks to return NOTIFY_BAD in error case.

Quoted 3 lines

I also think this needs extra handling for NETDEV_CHANGENAME rather than the 'treat as UNREG+REG' trick.

Quoted 6 lines
Quoted 2 lines

Else we may unregister and then fail to re-register which leaves the device without the registered hook op.

So search for another flowtable/chain with a hook matching the new name first, then unregister, try to register in the new spot and undo on failure? Sounds doable. :)

If possible i'd register new, then unreg old. But, do you need to do anything on CHANGENAME at all?

Device is the same, so maybe its enough to update the name in nft_hooks structure?

author reviewer
#28 lore raw

Re: [nf-next PATCH v3 11/16] netfilter: nf_tables: chain: Respect NETDEV_REGISTER events

Quoted 8 lines · Florian Westphal
Quoted 5 lines · Phil Sutter
Quoted 2 lines

Or is this guaranteed by UNREGISTER event already? If so, please add a comment.

Are packets relevant here? The question should be whether another CPU traverses hook->ops_list at the same time, no?

Yes, but also if anyone else can look at the structure in parallel.

So it is possible that ops are still used somewhere after nf_unregister_net_hook() returns? I don't quite get that code, with all the RCU-bells and ONCE-whistles.

Quoted 8 lines
Quoted 5 lines

Looking at
nft_flowtable_find_dev() mentioned in your other mail, there seems to be a case which doesn't synchronize on commit_mutex. So same rules apply to ops_list as for hook_list and thus I need to add an rcu_head to nf_hook_ops as well?

I will need to apply your series locally first to get the full picture, sorry.

No sorry, thanks for your review so far!

Quoted 28 lines
Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
net/netfilter/nft_chain_filter.c +6 -0
+			if (ops) {
+				memcpy(ops, &basechain->ops, sizeof(*ops));
+				ops->dev = dev;
+			}
+			if (ops &&
+			    (ctx->chain->table->flags & NFT_TABLE_F_DORMANT ||
Quoted 8 lines

net/netfilter/nft_chain_filter.c static void nft_netdev_event(unsigned long event, struct net_device *dev, · line 356

Quoted 6 lines
net/netfilter/nft_chain_filter.c +6 -0
+			     !nf_register_net_hook(dev_net(dev), ops))) {
+				list_add_tail(&ops->list, &hook->ops_list);
+				break;
+			}
+			printk(KERN_ERR "chain %s: Can't hook into device %s\n",
+			       ctx->chain->name, dev->name);

I think its better to -ENOMEM and veto the netdevice register request in this case.

Ah, I wasn't aware we may influence netdev registration from a notifier. So I'll change the callbacks to return NOTIFY_BAD in error case.

Quoted 3 lines

I also think this needs extra handling for NETDEV_CHANGENAME rather than the 'treat as UNREG+REG' trick.

Quoted 6 lines
Quoted 2 lines

Else we may unregister and then fail to re-register which leaves the device without the registered hook op.

So search for another flowtable/chain with a hook matching the new name first, then unregister, try to register in the new spot and undo on failure? Sounds doable. :)

Quoted 5 lines

If possible i'd register new, then unreg old. But, do you need to do anything on CHANGENAME at all?

Device is the same, so maybe its enough to update the name in nft_hooks structure?

You're putting the cart before the horse here: The user sets hook->ifname and we bind to whatever device matches that.

Now with a device being renamed, there are two options:

A) Unbind if the name doesn't match hook->ifname anymore and search for another, matching hook. This is what I had (tried to) implement.

B) Just leave the interface in place as long as it exists. This is how the old code behaves.

For users, I find (A) more intuitive. Also, consider netdevs being renamed by udev: Users may have a flowtable which matches the initial name by accident. If it doesn't unbind them upon being renamed, they all remain in there and may block the right flowtable from binding to them.

Cheers, Phil

maintainer
#29 lore raw

Re: [nf-next PATCH v3 11/16] netfilter: nf_tables: chain: Respect NETDEV_REGISTER events

Quoted 3 lines · Phil Sutter

So it is possible that ops are still used somewhere after nf_unregister_net_hook() returns? I don't quite get that code, with all the RCU-bells and ONCE-whistles.

Not ops, but other core could still run the hookfn that is being unregistered, i.e.:

cpu 0
unreg hookfn nf_hook_slow
runs hookfn
if nf_hook() fetched the hook blob (struct nf_hook_entries *) before unreg path called its rcu_assign_pointer with the updated incarnation.

That said, this might be a special case as entire nf_hook_entries blob is tied to device that is going down, so packet flow might have stopped already.

From a brief glance the device is already disabled at NETDEV_UNREGISTER time, no packets are flowing anymore.

Quoted 8 lines
Quoted 2 lines

Device is the same, so maybe its enough to update the name in nft_hooks structure?

You're putting the cart before the horse here: The user sets hook->ifname and we bind to whatever device matches that.

Now with a device being renamed, there are two options:

Quoted 7 lines

A) Unbind if the name doesn't match hook->ifname anymore and search for another, matching hook. This is what I had (tried to) implement.

B) Just leave the interface in place as long as it exists. This is how the old code behaves.

For users, I find (A) more intuitive.

Yes, that makes sense to me. But can't you defer the unbind until after you've figured out if there is a matching hook or not?

I.e., if no matching new hook, just unreg, else register new/unregister old.

Otherwise, we might unreg, then fail to re-register?

author reviewer
#30 lore raw

Re: [nf-next PATCH v3 11/16] netfilter: nf_tables: chain: Respect NETDEV_REGISTER events

Quoted 15 lines · Florian Westphal
Quoted 3 lines · Phil Sutter

So it is possible that ops are still used somewhere after nf_unregister_net_hook() returns? I don't quite get that code, with all the RCU-bells and ONCE-whistles.

Not ops, but other core could still run the hookfn that is being unregistered, i.e.:

cpu 0
unreg hookfn nf_hook_slow
runs hookfn
if nf_hook() fetched the hook blob (struct nf_hook_entries *) before unreg path called its rcu_assign_pointer with the updated incarnation.

Quoted 6 lines

That said, this might be a special case as entire nf_hook_entries blob is tied to device that is going down, so packet flow might have stopped already.

From a brief glance the device is already disabled at NETDEV_UNREGISTER time, no packets are flowing anymore.

This seems like a relief, but doesn't apply to NETDEV_CHANGENAME case. At least I can't handle it like UNREG && REG - or vice versa, have to consider the above for UNREG.

Quoted 14 lines
Quoted 14 lines
Quoted 2 lines

Device is the same, so maybe its enough to update the name in nft_hooks structure?

You're putting the cart before the horse here: The user sets hook->ifname and we bind to whatever device matches that.

Now with a device being renamed, there are two options:

A) Unbind if the name doesn't match hook->ifname anymore and search for another, matching hook. This is what I had (tried to) implement.

B) Just leave the interface in place as long as it exists. This is how the old code behaves.

Quoted 7 lines
Quoted 1 line

For users, I find (A) more intuitive.

Yes, that makes sense to me. But can't you defer the unbind until after you've figured out if there is a matching hook or not?

I.e., if no matching new hook, just unreg, else register new/unregister old.

I can't bind a device to multiple flowtables of the same family, so I can't bind first, then unbind.

Quoted 1 line

Otherwise, we might unreg, then fail to re-register?

Assuming I can't bind to the new flowtable while still bound to the old one, I can only try to roll back. Which means trying to bind to the old flowtable again in error case which might still fail. I could return NOTIFY_BAD then, but it won't help - the transaction is broken.

Cheers, Phil

maintainer
#31 lore raw

Re: [nf-next PATCH v3 11/16] netfilter: nf_tables: chain: Respect NETDEV_REGISTER events

Quoted 5 lines · Phil Sutter
Quoted 2 lines

I.e., if no matching new hook, just unreg, else register new/unregister old.

I can't bind a device to multiple flowtables of the same family, so I can't bind first, then unbind.

I'm dense, why does that not work?

author reviewer
#32 lore raw

Re: [nf-next PATCH v3 11/16] netfilter: nf_tables: chain: Respect NETDEV_REGISTER events

Quoted 8 lines · Florian Westphal
Quoted 5 lines · Phil Sutter
Quoted 2 lines

I.e., if no matching new hook, just unreg, else register new/unregister old.

I can't bind a device to multiple flowtables of the same family, so I can't bind first, then unbind.

I'm dense, why does that not work?

Well, nft_register_flowtable_net_hooks() searches for a same hook in other flowtables of the same table ("same" as in hook->ops.dev and hook->ops.pf values match) and returns -EEXIST if found.

Originally this check was added by Pablo:

| commit 32fc71875127498bf99cc648e96400ee0895edf7
| Author: Pablo Neira Ayuso <[email protected]>
| Date:   Mon Feb 26 13:16:04 2018 +0100
| 
|     netfilter: nf_tables: return EBUSY if device already belongs to flowtable
|     
|     If the netdevice is already part of a flowtable, return EBUSY. I cannot
|     find a valid usecase for having two flowtables bound to the same
|     netdevice. We can still have two flowtable where the device set is
|     disjoint.
|     
|     Signed-off-by: Pablo Neira Ayuso <[email protected]>

The comment luckily indicates there's no technical reason, so the reg first approach may fly. Apart from that, I'll try getting rid of this because it prevents things like ft1(eth0, eth1) && ft2(eth1, eth2) which seems like a valid use-case to me.

Thanks for questioning the basics here! :)

Cheers, Phil

author reviewer
#33 lore raw

PATCH v3 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.

---
 net/netfilter/nf_tables_api.c | 47 +++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 10 deletions(-)
net/netfilter/nf_tables_api.c +37 -10
index 64f8305189f1..40cff8539c74 100644
@@ -9314,15 +9314,41 @@ 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(&ops->list);
-		kfree(ops);
-		break;
+			/* flow_offload_netdev_event() cleans up entries for us. */
+			nft_unregister_flowtable_ops(dev_net(dev),
+						     flowtable, ops);
+			list_del(&ops->list);
+			kfree(ops);
+			break;
+		case NETDEV_REGISTER:
+			if (strcmp(hook->ifname, dev->name))
+				continue;
+			ops = kzalloc(sizeof(struct nf_hook_ops),
+				      GFP_KERNEL_ACCOUNT);
+			if (ops) {
+				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 (ops && !nft_register_flowtable_ops(dev_net(dev),
+							       flowtable, ops)) {
+				list_add_tail(&ops->list, &hook->ops_list);
+				break;
+			}
+			printk(KERN_ERR "flowtable %s: Can't hook into device %s\n",
+			       flowtable->name, dev->name);
+			kfree(ops);
+			continue;
+		}
 	}
 }
 
@@ -9335,8 +9361,9 @@ 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);
Signature · 1 line
2.43.0
author reviewer
#34 lore raw

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

In reply to Phil Sutter

For the sake of simplicity, treat them like consecutive NETDEV_UNREGISTER and NETDEV_REGISTER events.

---
 net/netfilter/nf_tables_api.c    | 4 ++++
 net/netfilter/nft_chain_filter.c | 4 ++++
 2 files changed, 8 insertions(+)
net/netfilter/nf_tables_api.c +4 -0
index 40cff8539c74..88528775732a 100644
@@ -9361,6 +9361,10 @@ static int nf_tables_flowtable_event(struct notifier_block *this,
 	struct nft_table *table;
 	struct net *net;
 
+	if (event == NETDEV_CHANGENAME) {
+		nf_tables_flowtable_event(this, NETDEV_UNREGISTER, ptr);
+		event = NETDEV_REGISTER;
+	}
 	if (event != NETDEV_REGISTER &&
 	    event != NETDEV_UNREGISTER)
 		return NOTIFY_DONE;
net/netfilter/nft_chain_filter.c +4 -0
index ec44c27a9d91..4f13591e5cd1 100644
@@ -372,6 +372,10 @@ static int nf_tables_netdev_event(struct notifier_block *this,
 		.net	= dev_net(dev),
 	};
 
+	if (event == NETDEV_CHANGENAME) {
+		nf_tables_netdev_event(this, NETDEV_UNREGISTER, ptr);
+		event = NETDEV_REGISTER;
+	}
 	if (event != NETDEV_REGISTER &&
 	    event != NETDEV_UNREGISTER)
 		return NOTIFY_DONE;
Signature · 1 line
2.43.0
author reviewer
#35 lore raw

PATCH v3 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    | 31 +++++++++++++++----------------
 net/netfilter/nft_chain_filter.c |  2 +-
 2 files changed, 16 insertions(+), 17 deletions(-)
net/netfilter/nf_tables_api.c +15 -16
index 88528775732a..3632be26d73a 100644
@@ -2225,24 +2225,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);
 }
@@ -2253,7 +2251,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;
 	}
 
@@ -9327,7 +9326,7 @@ static void nft_flowtable_event(unsigned long event, struct net_device *dev,
 			kfree(ops);
 			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 4f13591e5cd1..d691f8354049 100644
@@ -338,7 +338,7 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,
 			kfree(ops);
 			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);
Signature · 1 line
2.43.0
author reviewer
#36 lore raw

PATCH v3 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            | 57 ++++++++++++++++++++++++
 net/netfilter/nft_chain_filter.c         |  1 +
 4 files changed, 65 insertions(+)
include/net/netfilter/nf_tables.h +2 -0
index 991b8d5e52f1..3ebdb46b3993 100644
@@ -1131,6 +1131,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 639894ed1b97..0f350f2fc807 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 +57 -0

69 lines not on this page · raw message

net/netfilter/nft_chain_filter.c +1 -0
index d691f8354049..c9de9b12f772 100644
@@ -357,6 +357,7 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,
 			kfree(ops);
 			continue;
 		}
+		nf_tables_chain_device_notify(ctx->chain, dev, event);
 	}
 }
 
Signature · 1 line
2.43.0
author reviewer
#37 lore raw

PATCH v3 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