Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 10435c11 authored by Feng's avatar Feng Committed by Pablo Neira Ayuso
Browse files

netfilter: nf_tables: Eliminate duplicated code in nf_tables_table_enable()



If something fails in nf_tables_table_enable(), it unregisters the
chains. But the rollback code is the same as nf_tables_table_disable()
almostly, except there is one counter check.  Now create one wrapper
function to eliminate the duplicated codes.

Signed-off-by: default avatarFeng <fgao@ikuai8.com>
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent 1a28ad74
Loading
Loading
Loading
Loading
+25 −23
Original line number Diff line number Diff line
@@ -576,48 +576,34 @@ static int nf_tables_gettable(struct net *net, struct sock *nlsk,
	return err;
}

static int nf_tables_table_enable(struct net *net,
static void _nf_tables_table_disable(struct net *net,
				     const struct nft_af_info *afi,
				  struct nft_table *table)
				     struct nft_table *table,
				     u32 cnt)
{
	struct nft_chain *chain;
	int err, i = 0;

	list_for_each_entry(chain, &table->chains, list) {
		if (!nft_is_active_next(net, chain))
			continue;
		if (!(chain->flags & NFT_BASE_CHAIN))
			continue;
	u32 i = 0;

		err = nf_register_net_hooks(net, nft_base_chain(chain)->ops,
					    afi->nops);
		if (err < 0)
			goto err;

		i++;
	}
	return 0;
err:
	list_for_each_entry(chain, &table->chains, list) {
		if (!nft_is_active_next(net, chain))
			continue;
		if (!(chain->flags & NFT_BASE_CHAIN))
			continue;

		if (i-- <= 0)
		if (cnt && i++ == cnt)
			break;

		nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
					afi->nops);
	}
	return err;
}

static void nf_tables_table_disable(struct net *net,
static int nf_tables_table_enable(struct net *net,
				  const struct nft_af_info *afi,
				  struct nft_table *table)
{
	struct nft_chain *chain;
	int err, i = 0;

	list_for_each_entry(chain, &table->chains, list) {
		if (!nft_is_active_next(net, chain))
@@ -625,9 +611,25 @@ static void nf_tables_table_disable(struct net *net,
		if (!(chain->flags & NFT_BASE_CHAIN))
			continue;

		nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
		err = nf_register_net_hooks(net, nft_base_chain(chain)->ops,
					    afi->nops);
		if (err < 0)
			goto err;

		i++;
	}
	return 0;
err:
	if (i)
		_nf_tables_table_disable(net, afi, table, i);
	return err;
}

static void nf_tables_table_disable(struct net *net,
				    const struct nft_af_info *afi,
				    struct nft_table *table)
{
	_nf_tables_table_disable(net, afi, table, 0);
}

static int nf_tables_updtable(struct nft_ctx *ctx)