Skip to contentSkip to conversation

netfilter: nf_tables: add NFT_HOOK_UNREGISTERED and use it

PATCH New Pablo Neira Ayuso 1 participant 2 messages Nov 9

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 netfilter: nf_tables: add NFT_HOOK_UNREGISTERED and use it

Commit f9a43007d3f7 ("netfilter: nf_tables: double hook unregistration in netns path") fixes a race in the netns exit path with netdev device event handler. The netns pre_exit handler unregisters the hooks, then exit release them. In between this two calls, the netdev event might show up and try to unregister the hooks again. This patch revisit this fix by adding a new NFT_HOOK_UNREGISTERED flag which allows us to skip the hook from the netdevice and flowtable event handlers.

This patch is a prerequisit for another follow up fix for the flowtable hardware offload which allows to unregister the flow_block once the entries in hardware have been removed.

---
 include/net/netfilter/nf_tables.h |  3 +-
 net/netfilter/nf_tables_api.c     | 53 +++++++++++++++----------------
 net/netfilter/nft_chain_filter.c  |  5 ++-
 3 files changed, 29 insertions(+), 32 deletions(-)
include/net/netfilter/nf_tables.h +2 -1
index 9d597482363d..5edff5d7dacd 100644
@@ -1209,7 +1209,8 @@ struct nft_stats {
 	struct u64_stats_sync	syncp;
 };
 
-#define NFT_HOOK_REMOVE	(1 << 0)
+#define NFT_HOOK_REMOVE		(1 << 0)
+#define NFT_HOOK_UNREGISTERED	(1 << 1)
 
 struct nft_hook {
 	struct list_head	list;
net/netfilter/nf_tables_api.c +25 -28

142 lines not on this page · raw message

net/netfilter/nft_chain_filter.c +2 -3
index e04e689f2228..bf066ec82694 100644
@@ -322,7 +322,6 @@ static const struct nft_chain_type nft_chain_filter_netdev = {
 static int nft_netdev_event(unsigned long event, struct net_device *dev,
 			    struct nft_base_chain *basechain, bool changename)
 {
-	struct nft_table *table = basechain->chain.table;
 	struct nf_hook_ops *ops;
 	struct nft_hook *hook;
 	bool match;
@@ -337,7 +336,7 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,
 			if (!ops || (changename && match))
 				continue;
 
-			if (!(table->flags & NFT_TABLE_F_DORMANT))
+			if (!(hook->flags & NFT_HOOK_UNREGISTERED))
 				nf_unregister_net_hook(dev_net(dev), ops);
 
 			list_del_rcu(&ops->list);
@@ -356,7 +355,7 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,
 
 			ops->dev = dev;
 
-			if (!(table->flags & NFT_TABLE_F_DORMANT) &&
+			if (!(hook->flags & NFT_HOOK_UNREGISTERED) &&
 			    nf_register_net_hook(dev_net(dev), ops)) {
 				kfree(ops);
 				return 1;
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 2/2 netfilter: nf_tables: perform unbind callback on hook release

Commit 13210fc63f35 ("netfilter: nf_tables: imbalance in flowtable binding") breaks hardware offload by unbinding the flow_block too early, this results in leaving entries in the hardware. Any pending offload work will be ignored because there is no flow_block anymore in place.

This patch restores the strategy followed by commit 5acab91458ce ("netfilter: nf_tables: unbind callbacks from flowtable destroy path") by performing the flow_block unbind command from the destroy path, right before the hook is released.

Fixes 13210fc63f35 ("netfilter: nf_tables: imbalance in flowtable binding")
---
 net/netfilter/nf_tables_api.c | 63 +++++++++++++++++++++++++----------
 1 file changed, 45 insertions(+), 18 deletions(-)
net/netfilter/nf_tables_api.c +45 -18
index d3c04a71c924..0d50540fb04c 100644
@@ -380,6 +380,22 @@ static void nft_netdev_hook_unlink_free_rcu(struct nft_hook *hook)
 	nft_netdev_hook_free_rcu(hook);
 }
 
+static void nft_flowtable_hook_unlink_free_rcu(struct nft_flowtable *flowtable,
+					       struct nft_hook *hook)
+{
+	struct nf_hook_ops *ops;
+
+	/* This unbind command needs to come this late, otherwise offload
+	 * worker finds no flow_block and entries are not cleaned up from
+	 * hardware.
+	 */
+	list_for_each_entry(ops, &hook->ops_list, list) {
+		flowtable->data.type->setup(&flowtable->data, ops->dev,
+					    FLOW_BLOCK_UNBIND);
+	}
+	nft_netdev_hook_unlink_free_rcu(hook);
+}
+
 static void nft_trans_hook_destroy(struct nft_trans_hook *trans_hook)
 {
 	list_del(&trans_hook->list);
@@ -9026,12 +9042,9 @@ nft_flowtable_type_get(struct net *net, u8 family)
 
 /* Only called from error and netdev event paths. */
 static void nft_unregister_flowtable_ops(struct net *net,
-					 struct nft_flowtable *flowtable,
 					 struct nf_hook_ops *ops)
 {
 	nf_unregister_net_hook(net, ops);
-	flowtable->data.type->setup(&flowtable->data, ops->dev,
-				    FLOW_BLOCK_UNBIND);
 }
 
 static void __nft_unregister_flowtable_net_hooks(struct net *net,
@@ -9043,7 +9056,7 @@ static void __nft_unregister_flowtable_net_hooks(struct net *net,
 
 	list_for_each_entry_safe(hook, next, hook_list, list) {
 		list_for_each_entry(ops, &hook->ops_list, list)
-			nft_unregister_flowtable_ops(net, flowtable, ops);
+			nft_unregister_flowtable_ops(net, ops);
 
 		hook->flags |= NFT_HOOK_UNREGISTERED;
 	}
@@ -9114,7 +9127,9 @@ static int nft_register_flowtable_net_hooks(struct net *net,
 			if (i-- <= 0)
 				break;
 
-			nft_unregister_flowtable_ops(net, flowtable, ops);
+			nft_unregister_flowtable_ops(net, ops);
+			flowtable->data.type->setup(&flowtable->data, ops->dev,
+						    FLOW_BLOCK_UNBIND);
 		}
 		nft_netdev_hook_unlink_free_rcu(hook);
 	}
@@ -9141,9 +9156,9 @@ static void nft_flowtable_unregister_trans_hook(struct net *net,
 	list_for_each_entry_safe(trans_hook, next, hook_list, list) {
 		hook = trans_hook->hook;
 		list_for_each_entry(ops, &hook->ops_list, list)
-			nft_unregister_flowtable_ops(net, flowtable, ops);
+			nft_unregister_flowtable_ops(net, ops);
 
-		nft_netdev_hook_unlink_free_rcu(hook);
+		nft_flowtable_hook_unlink_free_rcu(flowtable, trans_hook->hook);
 		nft_trans_hook_destroy(trans_hook);
 	}
 }
@@ -9229,9 +9244,11 @@ static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,
 err_flowtable_update_hook:
 	list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
 		if (unregister) {
-			list_for_each_entry(ops, &hook->ops_list, list)
-				nft_unregister_flowtable_ops(ctx->net,
-							     flowtable, ops);
+			list_for_each_entry(ops, &hook->ops_list, list) {
+				nft_unregister_flowtable_ops(ctx->net, ops);
+				flowtable->data.type->setup(&flowtable->data, ops->dev,
+							    FLOW_BLOCK_UNBIND);
+			}
 		}
 		nft_netdev_hook_unlink_free_rcu(hook);
 	}
@@ -9752,10 +9769,19 @@ static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
 	nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
 }
 
+static void nft_flowtable_hooks_destroy(struct nft_flowtable *flowtable,
+					struct list_head *hook_list)
+{
+	struct nft_hook *hook, *next;
+
+	list_for_each_entry_safe(hook, next, hook_list, list)
+		nft_flowtable_hook_unlink_free_rcu(flowtable, hook);
+}
+
 static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
 {
 	flowtable->data.type->free(&flowtable->data);
-	nft_hooks_destroy(&flowtable->hook_list);
+	nft_flowtable_hooks_destroy(flowtable, &flowtable->hook_list);
 	kfree(flowtable->name);
 	module_put(flowtable->data.type->owner);
 	kfree(flowtable);
@@ -9830,10 +9856,9 @@ static int nft_flowtable_event(unsigned long event, struct net_device *dev,
 				continue;
 
 			/* flow_offload_netdev_event() cleans up entries for us. */
-			if (!(hook->flags & NFT_HOOK_UNREGISTERED)) {
-				nft_unregister_flowtable_ops(dev_net(dev),
-							     flowtable, ops);
-			}
+			if (!(hook->flags & NFT_HOOK_UNREGISTERED))
+				nft_unregister_flowtable_ops(dev_net(dev), ops);
+
 			list_del_rcu(&ops->list);
 			kfree_rcu(ops, rcu);
 			break;
@@ -11311,10 +11336,12 @@ static void nf_tables_abort_release(struct nft_trans *trans)
 		nft_obj_destroy(&ctx, nft_trans_obj(trans));
 		break;
 	case NFT_MSG_NEWFLOWTABLE:
-		if (nft_trans_flowtable_update(trans))
-			nft_hooks_destroy(&nft_trans_flowtable_hooks(trans));
-		else
+		if (nft_trans_flowtable_update(trans)) {
+			nft_flowtable_hooks_destroy(nft_trans_flowtable(trans),
+						    &nft_trans_flowtable_hooks(trans));
+		} else {
 			nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
+		}
 		break;
 	}
 	kfree(trans);
Signature · 1 line
2.47.3