From e5382c0d08e3c6d8246afa95b7380f0d6b8c1826 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Fri, 7 Jun 2019 19:21:21 +0200 Subject: 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 --- tests/shell/testcases/transactions/0024rule_0 | 17 +++++++++++++++++ tests/shell/testcases/transactions/0025rule_0 | 21 +++++++++++++++++++++ .../testcases/transactions/dumps/0024rule_0.nft | 8 ++++++++ .../testcases/transactions/dumps/0025rule_0.nft | 6 ++++++ 4 files changed, 52 insertions(+) create mode 100755 tests/shell/testcases/transactions/0024rule_0 create mode 100755 tests/shell/testcases/transactions/0025rule_0 create mode 100644 tests/shell/testcases/transactions/dumps/0024rule_0.nft create mode 100644 tests/shell/testcases/transactions/dumps/0025rule_0.nft (limited to 'tests/shell/testcases/transactions') diff --git a/tests/shell/testcases/transactions/0024rule_0 b/tests/shell/testcases/transactions/0024rule_0 new file mode 100755 index 00000000..4c1ac41d --- /dev/null +++ b/tests/shell/testcases/transactions/0024rule_0 @@ -0,0 +1,17 @@ +#!/bin/bash + +RULESET="flush ruleset +add table x +add chain x y +add rule x y accept comment rule1 +add rule x y accept comment rule4 +add rule x y index 0 accept comment rule2 +insert rule x y index 2 accept comment rule3" + +$NFT -f - <<< "$RULESET" && \ + $NFT -f - <<< "$RULESET" && \ + echo "$RULESET" | tr '\n' ';' | $NFT -i >/dev/null && \ + exit 0 +echo "E: intra-transaction rule reference failed" +exit 1 + diff --git a/tests/shell/testcases/transactions/0025rule_0 b/tests/shell/testcases/transactions/0025rule_0 new file mode 100755 index 00000000..d72d5cfc --- /dev/null +++ b/tests/shell/testcases/transactions/0025rule_0 @@ -0,0 +1,21 @@ +#!/bin/bash + +# make sure stored delete/replace rule commands are correctly applied + +set -e + +$NFT -f - <