path: root/src/mnl.c
diff options
authorPhil Sutter <>2019-06-07 19:21:21 +0200
committerPablo Neira Ayuso <>2019-06-07 23:54:51 +0200
commite5382c0d08e3c6d8246afa95b7380f0d6b8c1826 (patch)
treee7a6973c6354a9bfee9383476dbc7041fc2e27c9 /src/mnl.c
parenteecfd96ea3ca8207a1fc28cd1e845c177be59d85 (diff)
src: Support intra-transaction rule references
A rule may be added before or after another one using index keyword. To support for the other rule being added within the same batch, one has to make use of NFTNL_RULE_ID and NFTNL_RULE_POSITION_ID attributes. This patch does just that among a few more crucial things: * If cache is complete enough to contain rules, update cache when evaluating rule commands so later index references resolve correctly. * Reduce rule_translate_index() to its core code which is the actual linking of rules and consequently rename the function. The removed bits are pulled into the calling rule_evaluate() to reduce code duplication in between cache updates with and without rule reference. * Pass the current command op to rule_evaluate() as indicator whether to insert before or after a referenced rule or at beginning or end of chain in cache. Exploit this from chain_evaluate() to avoid adding the chain's rules a second time. Signed-off-by: Phil Sutter <> Signed-off-by: Pablo Neira Ayuso <>
Diffstat (limited to 'src/mnl.c')
1 files changed, 4 insertions, 0 deletions
diff --git a/src/mnl.c b/src/mnl.c
index 83dfb9d2..6ebad28b 100644
--- a/src/mnl.c
+++ b/src/mnl.c
@@ -378,6 +378,10 @@ int mnl_nft_rule_add(struct netlink_ctx *ctx, const struct cmd *cmd,
nftnl_rule_set_str(nlr, NFTNL_RULE_CHAIN, h->;
if (h->
nftnl_rule_set_u64(nlr, NFTNL_RULE_POSITION, h->;
+ if (h->rule_id)
+ nftnl_rule_set_u32(nlr, NFTNL_RULE_ID, h->rule_id);
+ if (h->position_id)
+ nftnl_rule_set_u32(nlr, NFTNL_RULE_POSITION_ID, h->position_id);
netlink_linearize_rule(ctx, nlr, rule);
nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch),