rule: add helper function to expand chain rules into commands
Pablo Neira Ayuso
maintainerPATCH 1/2 rule: add helper function to expand chain rules into commands
This patch adds a helper function to expand chain rules into commands. This comes in preparation for the follow up patch.
--- src/rule.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-)
src/rule.c +22 -17
@@ -1310,13 +1310,31 @@ void cmd_add_loc(struct cmd *cmd, uint16_t offset, const struct location *loc) cmd->num_attrs++; } +static void nft_cmd_expand_chain (struct chain *chain,struct list_head *new_cmds) +{ +struct rule *rule; +struct handle h; +struct cmd *new; + +list_for_each_entry (rule, &chain->rules, list) { +memset (&h,0 ,sizeof (h)); +handle_merge (&h, &rule->handle); +if (chain->flags &CHAIN_F_BINDING ) { + rule->handle.chain_id = chain->handle.chain_id; + rule->handle.chain.location = chain->location; + } + new =cmd_alloc (CMD_ADD ,CMD_OBJ_RULE , &h, + &rule->location,rule_get (rule)); +list_add_tail (&new->list, new_cmds); + } +} +void nft_cmd_expand (struct cmd *cmd) {struct list_head new_cmds;struct flowtable *ft;struct table *table;struct chain *chain; -struct rule *rule;struct set *set;struct obj *obj;struct cmd *new;
@@ -1362,22 +1380,9 @@ void nft_cmd_expand(struct cmd *cmd) &ft->location, flowtable_get(ft));list_add_tail (&new->list, &new_cmds); } -list_for_each_entry (chain, &table->chains, list) { -list_for_each_entry (rule, &chain->rules, list) { -memset (&h,0 ,sizeof (h)); -handle_merge (&h, &rule->handle); -if (chain->flags &CHAIN_F_BINDING ) { - rule->handle.chain_id = - chain->handle.chain_id; - rule->handle.chain.location = - chain->location; - } - new =cmd_alloc (CMD_ADD ,CMD_OBJ_RULE , &h, - &rule->location, -rule_get (rule)); -list_add_tail (&new->list, &new_cmds); - } - } +list_for_each_entry (chain, &table->chains, list) +nft_cmd_expand_chain (chain, &new_cmds); +list_splice (&new_cmds, &cmd->list);break ;case CMD_OBJ_SET :
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/2 rule: expand standalone chain that contains rules
Otherwise rules that this chain contains are ignored when expressed using the following syntax:
chain inet filter input2 {
type filter hook input priority filter; policy accept;
ip saddr 1.2.3.4 tcp dport { 22, 443, 123 } drop
}and importing chain definitions like these from another file.
When expanding the chain, remove the rule so the new CMD_OBJ_CHAIN case does not expand it again.
--- src/rule.c | 15 +++++++++--- .../testcases/include/0020include_chain_0 | 23 +++++++++++++++++++ .../include/dumps/0020include_chain_0.nft | 6 +++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100755 tests/shell/testcases/include/0020include_chain_0 create mode 100644 tests/shell/testcases/include/dumps/0020include_chain_0.nft
src/rule.c +12 -3
@@ -1312,11 +1312,12 @@ void cmd_add_loc(struct cmd *cmd, uint16_t offset, const struct location *loc)static void nft_cmd_expand_chain (struct chain *chain,struct list_head *new_cmds) { -struct rule *rule; +struct rule *rule, *next;struct handle h;struct cmd *new; -list_for_each_entry (rule, &chain->rules, list) { +list_for_each_entry_safe (rule, next, &chain->rules, list) { +list_del (&rule->list);memset (&h,0 ,sizeof (h));handle_merge (&h, &rule->handle);if (chain->flags &CHAIN_F_BINDING ) {
@@ -1324,7 +1325,7 @@ static void nft_cmd_expand_chain(struct chain *chain, struct list_head *new_cmds rule->handle.chain.location = chain->location; } new =cmd_alloc (CMD_ADD ,CMD_OBJ_RULE , &h, - &rule->location,rule_get (rule)); + &rule->location, rule);list_add_tail (&new->list, new_cmds); } }
@@ -1385,6 +1386,14 @@ void nft_cmd_expand(struct cmd *cmd)list_splice (&new_cmds, &cmd->list);break ; +case CMD_OBJ_CHAIN : + chain = cmd->chain; +if (!chain ||list_empty (&chain->rules)) +break ; + +nft_cmd_expand_chain (chain, &new_cmds); +list_splice (&new_cmds, &cmd->list); +break ;case CMD_OBJ_SET :case CMD_OBJ_MAP : set = cmd->set;
tests/shell/testcases/include/0020include_chain_0 new file +23 -0
@@ -0,0 +1,23 @@ +#!/bin/bash + +set -e + +tmpfile1=$(mktemp -p .) +if [ ! -w $tmpfile1 ] ; then + echo "Failed to create tmp file" >&2 + exit 0 +fi + +trap "rm -rf $tmpfile1" EXIT # cleanup if aborted + +RULESET="table inet filter { } +include \"$tmpfile1\"" + +RULESET2="chain inet filter input2 { + type filter hook input priority filter; policy accept; + ip saddr 1.2.3.4 tcp dport { 22, 443, 123 } drop +}" + +echo "$RULESET2" > $tmpfile1 + +$NFT -o -f - <<< $RULESET
tests/shell/testcases/include/dumps/0020include_chain_0.nft new file +6 -0
@@ -0,0 +1,6 @@ +table inet filter { + chain input2 { + type filter hook input priority filter; policy accept; + ip saddr 1.2.3.4 tcp dport { 22, 123, 443 } drop + } +}
Signature · 1 line
2.30.2
Phil Sutter
reviewerRe: [PATCH nft 2/2] rule: expand standalone chain that contains rules
Hi Pablo,
Quoted 8 lines · Pablo Neira Ayuso
Otherwise rules that this chain contains are ignored when expressed using the following syntax:
chain inet filter input2 { type filter hook input priority filter; policy accept; ip saddr 1.2.3.4 tcp dport { 22, 443, 123 } drop }
Quoted 7 lines
and importing chain definitions like these from another file.
When expanding the chain, remove the rule so the new
CMD_OBJ_CHAINcase does not expand it again.
This commit happens to break a pretty simple use-case:
# nft -f - <<EOF
flush ruleset
add table inet t
add chain inet t c { type filter hook input priority 0 ; }
add rule inet t c tcp dport 1234 accept
add rule inet t c accept
insert rule inet t c index 1 udp dport 4321 accept
EOF
/dev/stdin:6:30-50: Error: Could not process rule: No such file or directory
insert rule inet t c index 1 udp dport 4321 accept
^^^^^^^^^^^^^^^^^^^^^