Skip to content

Commit

Permalink
netfilter: nf_tables: Implement table adoption support
Browse files Browse the repository at this point in the history
Allow a new process to take ownership of a previously owned table,
useful mostly for firewall management services restarting or suspending
when idle.

By extending __NFT_TABLE_F_UPDATE, the on/off/on check in
nf_tables_updtable() also covers table adoption, although it is actually
not needed: Table adoption is irreversible because nf_tables_updtable()
rejects attempts to drop NFT_TABLE_F_OWNER so table->nlpid setting can
happen just once within the transaction.

If the transaction commences, table's nlpid and flags fields are already
set and no further action is required. If it aborts, the table returns
to orphaned state.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
  • Loading branch information
Phil Sutter authored and Florian Westphal committed Jan 29, 2024
1 parent da5141b commit 31bf508
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 6 additions & 0 deletions include/net/netfilter/nf_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,12 @@ static inline bool nft_table_has_owner(const struct nft_table *table)
return table->flags & NFT_TABLE_F_OWNER;
}

static inline bool nft_table_is_orphan(const struct nft_table *table)
{
return (table->flags & (NFT_TABLE_F_OWNER | NFT_TABLE_F_PERSIST)) ==
NFT_TABLE_F_PERSIST;
}

static inline bool nft_base_chain_netdev(int family, u32 hooknum)
{
return family == NFPROTO_NETDEV ||
Expand Down
19 changes: 16 additions & 3 deletions net/netfilter/nf_tables_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1194,8 +1194,10 @@ static void nf_tables_table_disable(struct net *net, struct nft_table *table)
#define __NFT_TABLE_F_INTERNAL (NFT_TABLE_F_MASK + 1)
#define __NFT_TABLE_F_WAS_DORMANT (__NFT_TABLE_F_INTERNAL << 0)
#define __NFT_TABLE_F_WAS_AWAKEN (__NFT_TABLE_F_INTERNAL << 1)
#define __NFT_TABLE_F_WAS_ORPHAN (__NFT_TABLE_F_INTERNAL << 2)
#define __NFT_TABLE_F_UPDATE (__NFT_TABLE_F_WAS_DORMANT | \
__NFT_TABLE_F_WAS_AWAKEN)
__NFT_TABLE_F_WAS_AWAKEN | \
__NFT_TABLE_F_WAS_ORPHAN)

static int nf_tables_updtable(struct nft_ctx *ctx)
{
Expand All @@ -1215,8 +1217,8 @@ static int nf_tables_updtable(struct nft_ctx *ctx)

if ((nft_table_has_owner(ctx->table) &&
!(flags & NFT_TABLE_F_OWNER)) ||
(!nft_table_has_owner(ctx->table) &&
flags & NFT_TABLE_F_OWNER))
(flags & NFT_TABLE_F_OWNER &&
!nft_table_is_orphan(ctx->table)))
return -EOPNOTSUPP;

if ((flags ^ ctx->table->flags) & NFT_TABLE_F_PERSIST)
Expand Down Expand Up @@ -1248,6 +1250,13 @@ static int nf_tables_updtable(struct nft_ctx *ctx)
}
}

if ((flags & NFT_TABLE_F_OWNER) &&
!nft_table_has_owner(ctx->table)) {
ctx->table->nlpid = ctx->portid;
ctx->table->flags |= NFT_TABLE_F_OWNER |
__NFT_TABLE_F_WAS_ORPHAN;
}

nft_trans_table_update(trans) = true;
nft_trans_commit_list_add_tail(ctx->net, trans);

Expand Down Expand Up @@ -10423,6 +10432,10 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
} else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) {
trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT;
}
if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_ORPHAN) {
trans->ctx.table->flags &= ~NFT_TABLE_F_OWNER;
trans->ctx.table->nlpid = 0;
}
trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
nft_trans_destroy(trans);
} else {
Expand Down

0 comments on commit 31bf508

Please sign in to comment.