Skip to contentSkip to conversation

evaluate: expand variable containing set into multiple mappings

PATCH New Pablo Neira Ayuso 1 participant 1 message Aug 12, 2021 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 evaluate: expand variable containing set into multiple mappings

 # cat x.nft
 define interfaces = { eth0, eth1 }
 table ip x {
        chain y {
		type filter hook input priority 0; policy accept;
                iifname vmap { lo : accept, $interfaces : drop }
        }
 }
 # nft -f x.nft
 # nft list ruleset
 table ip x {
        chain y {
		type filter hook input priority 0; policy accept;
                iifname vmap { "lo" : accept, "eth0" : drop, "eth1" : drop }
        }
 }

---
supersedes: https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

 src/evaluate.c                                 | 17 +++++++++++++++++
 tests/shell/testcases/maps/0012map_0           | 17 +++++++++++++++++
 tests/shell/testcases/maps/dumps/0012map_0.nft | 12 ++++++++++++
 3 files changed, 46 insertions(+)
 create mode 100755 tests/shell/testcases/maps/0012map_0
 create mode 100644 tests/shell/testcases/maps/dumps/0012map_0.nft
src/evaluate.c +17 -0
index 8b5f51cee01c..8ebc75617b1c 100644
@@ -1431,6 +1431,23 @@ static int expr_evaluate_set(struct eval_ctx *ctx, struct expr **expr)
 		if (list_member_evaluate(ctx, &i) < 0)
 			return -1;
 
+		if (i->etype == EXPR_MAPPING &&
+		    i->left->etype == EXPR_SET_ELEM &&
+		    i->left->key->etype == EXPR_SET) {
+			struct expr *new, *j;
+
+			list_for_each_entry(j, &i->left->key->expressions, list) {
+				new = mapping_expr_alloc(&i->location,
+							 expr_get(j),
+							 expr_clone(i->right));
+				list_add_tail(&new->list, &set->expressions);
+				set->size++;
+			}
+			list_del(&i->list);
+			expr_free(i);
+			continue;
+		}
+
 		elem = expr_set_elem(i);
 
 		if (elem->etype == EXPR_SET_ELEM &&
tests/shell/testcases/maps/0012map_0 new file +17 -0
new file mode 100755
index 000000000000..dd93c482f441
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+set -e
+
+EXPECTED="define interfaces = { eth0, eth1 }
+
+table ip x {
+	map z {
+		type ifname : verdict
+		elements = { \$interfaces : drop, lo : accept }
+	}
+	chain y {
+		iifname vmap { lo : accept, \$interfaces : drop }
+	}
+}"
+
+$NFT -f - <<< "$EXPECTED"
tests/shell/testcases/maps/dumps/0012map_0.nft new file +12 -0
new file mode 100644
index 000000000000..e734fc1c70b9
@@ -0,0 +1,12 @@
+table ip x {
+	map z {
+		type ifname : verdict
+		elements = { "lo" : accept,
+			     "eth0" : drop,
+			     "eth1" : drop }
+	}
+
+	chain y {
+		iifname vmap { "lo" : accept, "eth0" : drop, "eth1" : drop }
+	}
+}
Signature · 1 line
2.20.1