Skip to contentSkip to conversation

src: Support maps as left side expressions

PATCH Superseded Brett Mastbergen 2 participants 2 messages Jul 29, 2019 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 src: Support maps as left side expressions

This change allows map expressions on the left side of comparisons:

nft add rule foo bar ip saddr map @map_a == 22 counter

It also allows map expressions as the left side expression of other map expressions:

nft add rule foo bar ip saddr map @map_a map @map_b == 22 counter

To accomplish this, some additional context needs to be set during evaluation and delinearization. A tweak is also make to the parser logic to allow map expressions as the left hand expression to other map expressions.

By allowing maps as left side comparison expressions one can map information in the packet to some arbitrary piece of data and use the equality (or inequality) to make some decision about the traffic, unlike today where the result of a map lookup is only usable as the right side of a statement (like dnat or snat) that actually uses the value as input.

---
 src/evaluate.c            |  2 +-
 src/expression.c          | 12 +++++++++++-
 src/netlink_delinearize.c |  2 ++
 src/parser_bison.y        | 10 +++++++---
 4 files changed, 21 insertions(+), 5 deletions(-)
src/evaluate.c +1 -1
index 48c65cd2..59538f27 100644
@@ -1385,6 +1385,7 @@ static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)
 		    !set_is_datamap(map->mappings->set->flags))
 			return expr_error(ctx->msgs, map->mappings,
 					  "Expression is not a map");
+		expr_set_context(&ctx->ectx, map->mappings->set->datatype, map->mappings->set->datalen);
 		break;
 	default:
 		BUG("invalid mapping expression %s\n",
@@ -1399,7 +1400,6 @@ static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)
 					 map->map->dtype->desc);
 
 	datatype_set(map, map->mappings->set->datatype);
-	map->flags |= EXPR_F_CONSTANT;
 
 	/* Data for range lookups needs to be in big endian order */
 	if (map->mappings->set->flags & NFT_SET_INTERVAL &&
src/expression.c +11 -1
index cb49e0b7..e5979394 100644
@@ -1067,8 +1067,18 @@ static const struct expr_ops set_ref_expr_ops = {
 struct expr *set_ref_expr_alloc(const struct location *loc, struct set *set)
 {
 	struct expr *expr;
+	const struct datatype *dtype;
+	unsigned int len;
 
-	expr = expr_alloc(loc, EXPR_SET_REF, set->key->dtype, 0, 0);
+	if (set->flags & NFT_SET_MAP) {
+		dtype = set->datatype;
+		len = set->datalen;
+	} else {
+		dtype = set->key->dtype;
+		len = set->key->len;
+	}
+
+	expr = expr_alloc(loc, EXPR_SET_REF, dtype, 0, len);
 	expr->set = set_get(set);
 	expr->flags |= EXPR_F_CONSTANT;
 	return expr;
src/netlink_delinearize.c +2 -0
index fc2574b1..b70433f0 100644
@@ -341,6 +341,8 @@ static void netlink_parse_lookup(struct netlink_parse_ctx *ctx,
 	if (nftnl_expr_is_set(nle, NFTNL_EXPR_LOOKUP_DREG)) {
 		dreg = netlink_parse_register(nle, NFTNL_EXPR_LOOKUP_DREG);
 		expr = map_expr_alloc(loc, left, right);
+		expr_set_type(expr, right->dtype, right->byteorder);
+		expr->len = right->len;
 		if (dreg != NFT_REG_VERDICT)
 			return netlink_set_register(ctx, dreg, expr);
 	} else {
src/parser_bison.y +7 -3
index 53e66952..9b998d65 100644
@@ -668,8 +668,8 @@ int nft_lex(void *, void *, void *);
 %type <expr>			concat_expr
 %destructor { expr_free($$); }	concat_expr
 
-%type <expr>			map_expr
-%destructor { expr_free($$); }	map_expr
+%type <expr>			map_expr map_lhs_expr
+%destructor { expr_free($$); }	map_expr map_lhs_expr
 
 %type <expr>			verdict_map_stmt
 %destructor { expr_free($$); }	verdict_map_stmt
@@ -3378,7 +3378,11 @@ multiton_rhs_expr	:	prefix_rhs_expr
 			|	wildcard_expr
 			;
 
-map_expr		:	concat_expr	MAP	rhs_expr
+map_lhs_expr		:	concat_expr
+			|	map_expr
+			;
+
+map_expr		:	map_lhs_expr	MAP	rhs_expr
 			{
 				$$ = map_expr_alloc(&@$, $1, $3);
 			}
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 14 lines · Brett Mastbergen

This change allows map expressions on the left side of comparisons:

nft add rule foo bar ip saddr map @map_a == 22 counter

It also allows map expressions as the left side expression of other map expressions:

nft add rule foo bar ip saddr map @map_a map @map_b == 22 counter

To accomplish this, some additional context needs to be set during evaluation and delinearization. A tweak is also make to the parser logic to allow map expressions as the left hand expression to other map expressions.

Quoted 6 lines

By allowing maps as left side comparison expressions one can map information in the packet to some arbitrary piece of data and use the equality (or inequality) to make some decision about the traffic, unlike today where the result of a map lookup is only usable as the right side of a statement (like dnat or snat) that actually uses the value as input.

Can you add a test case for this?

FWIW, this appears to work fine:

table inet filter {
        map map_a {
                type ipv4_addr : mark
                elements = { 127.0.0.1 : 0x0000002a }
        }

        map map_b {
                type mark : inet_service
                elements = { 0x0000002a : 22 }
        }

        chain input {
                type filter hook input priority filter; policy accept;
                ip saddr map @map_a map @map_b 22 counter
        }
}

inet filter input
[ meta load nfproto => reg 1 ]
[ cmp eq reg 1 0x00000002 ]
[ payload load 4b @ network header + 12 => reg 1 ]
[ lookup reg 1 set map_a dreg 1 ] # looks up ipv4 addr in map_a [ lookup reg 1 set map_b dreg 1 ] # looks up mark in map_b
[ cmp eq reg 1 0x00001600 ] # cmp port number from map_b with 22
[ counter pkts 0 bytes 0 ]

... so this looks good to me.

Guess its time for me to work on typeof() keyword again so we can have strings in named sets.