Skip to content

Commit 74cccc3

Browse files
committed
netfilter: nf_tables: add NFTA_CHAIN_ID attribute
This netlink attribute allows you to refer to chains inside a transaction as an alternative to the name and the handle. The chain binding support requires this new chain ID approach. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent f0a5e4d commit 74cccc3

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,7 @@ struct nft_trans_chain {
14331433
char *name;
14341434
struct nft_stats __percpu *stats;
14351435
u8 policy;
1436+
u32 chain_id;
14361437
};
14371438

14381439
#define nft_trans_chain_update(trans) \
@@ -1443,6 +1444,8 @@ struct nft_trans_chain {
14431444
(((struct nft_trans_chain *)trans->data)->stats)
14441445
#define nft_trans_chain_policy(trans) \
14451446
(((struct nft_trans_chain *)trans->data)->policy)
1447+
#define nft_trans_chain_id(trans) \
1448+
(((struct nft_trans_chain *)trans->data)->chain_id)
14461449

14471450
struct nft_trans_table {
14481451
bool update;

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ enum nft_table_attributes {
196196
* @NFTA_CHAIN_TYPE: type name of the string (NLA_NUL_STRING)
197197
* @NFTA_CHAIN_COUNTERS: counter specification of the chain (NLA_NESTED: nft_counter_attributes)
198198
* @NFTA_CHAIN_FLAGS: chain flags
199+
* @NFTA_CHAIN_ID: uniquely identifies a chain in a transaction (NLA_U32)
199200
*/
200201
enum nft_chain_attributes {
201202
NFTA_CHAIN_UNSPEC,
@@ -209,6 +210,7 @@ enum nft_chain_attributes {
209210
NFTA_CHAIN_COUNTERS,
210211
NFTA_CHAIN_PAD,
211212
NFTA_CHAIN_FLAGS,
213+
NFTA_CHAIN_ID,
212214
__NFTA_CHAIN_MAX
213215
};
214216
#define NFTA_CHAIN_MAX (__NFTA_CHAIN_MAX - 1)

net/netfilter/nf_tables_api.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,15 @@ static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
280280
if (trans == NULL)
281281
return ERR_PTR(-ENOMEM);
282282

283-
if (msg_type == NFT_MSG_NEWCHAIN)
283+
if (msg_type == NFT_MSG_NEWCHAIN) {
284284
nft_activate_next(ctx->net, ctx->chain);
285285

286+
if (ctx->nla[NFTA_CHAIN_ID]) {
287+
nft_trans_chain_id(trans) =
288+
ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID]));
289+
}
290+
}
291+
286292
list_add_tail(&trans->list, &ctx->net->nft.commit_list);
287293
return trans;
288294
}
@@ -1274,6 +1280,7 @@ static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
12741280
.len = NFT_MODULE_AUTOLOAD_LIMIT },
12751281
[NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
12761282
[NFTA_CHAIN_FLAGS] = { .type = NLA_U32 },
1283+
[NFTA_CHAIN_ID] = { .type = NLA_U32 },
12771284
};
12781285

12791286
static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
@@ -2154,9 +2161,9 @@ static int nf_tables_newchain(struct net *net, struct sock *nlsk,
21542161
const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
21552162
u8 genmask = nft_genmask_next(net);
21562163
int family = nfmsg->nfgen_family;
2164+
struct nft_chain *chain = NULL;
21572165
const struct nlattr *attr;
21582166
struct nft_table *table;
2159-
struct nft_chain *chain;
21602167
u8 policy = NF_ACCEPT;
21612168
struct nft_ctx ctx;
21622169
u64 handle = 0;
@@ -2181,7 +2188,7 @@ static int nf_tables_newchain(struct net *net, struct sock *nlsk,
21812188
return PTR_ERR(chain);
21822189
}
21832190
attr = nla[NFTA_CHAIN_HANDLE];
2184-
} else {
2191+
} else if (nla[NFTA_CHAIN_NAME]) {
21852192
chain = nft_chain_lookup(net, table, attr, genmask);
21862193
if (IS_ERR(chain)) {
21872194
if (PTR_ERR(chain) != -ENOENT) {
@@ -2190,6 +2197,8 @@ static int nf_tables_newchain(struct net *net, struct sock *nlsk,
21902197
}
21912198
chain = NULL;
21922199
}
2200+
} else if (!nla[NFTA_CHAIN_ID]) {
2201+
return -EINVAL;
21932202
}
21942203

21952204
if (nla[NFTA_CHAIN_POLICY]) {

0 commit comments

Comments
 (0)