Skip to contentSkip to conversation

parser: get rid of multiton_expr from lhs relational expression

PATCH In review Pablo Neira Ayuso 2 participants 4 messages Dec 29, 2015 – Jan 4, 2016 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/3 parser: get rid of multiton_expr from lhs relational expression

The multiton_expr rule matches range, prefix and wildcard expressions which don't make sense from the non-constant lhs. This rule is there to handle the nat statement case, whose expression may be composed of address and port ranges (hence range expressions).

To resolve this, this patch adds the stmt_expr rule to handle the possible occurrences of map, multiton and primary expressions from statements.

This results in more rules but it narrows down what we can find from expressions that are part of action statements.

---
 src/parser_bison.y | 54 ++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 44 insertions(+), 10 deletions(-)
src/parser_bison.y +44 -10
index d42bd2f..b49eadb 100644
@@ -486,6 +486,10 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %destructor { expr_free($$); }	multiton_expr
 %type <expr>			prefix_expr range_expr wildcard_expr
 %destructor { expr_free($$); }	prefix_expr range_expr wildcard_expr
+
+%type <expr>			stmt_expr concat_stmt_expr map_stmt_expr
+%destructor { expr_free($$); }	stmt_expr concat_stmt_expr map_stmt_expr
+
 %type <expr>			list_expr
 %destructor { expr_free($$); }	list_expr
 %type <expr>			concat_expr
@@ -1577,20 +1581,51 @@ nat_stmt_alloc		:	SNAT
 			}
 			;
 
-nat_stmt_args		:	expr
+concat_stmt_expr	:	primary_expr
+			|	concat_stmt_expr	DOT	primary_expr
+			{
+				if ($$->ops->type != EXPR_CONCAT) {
+					$$ = concat_expr_alloc(&@$);
+					compound_expr_add($$, $1);
+				} else {
+					struct location rhs[] = {
+						[1]	= @2,
+						[2]	= @3,
+					};
+					location_update(&$3->location, rhs, 2);
+
+					$$ = $1;
+					$$->location = @$;
+				}
+				compound_expr_add($$, $3);
+			}
+			;
+
+map_stmt_expr		:	concat_stmt_expr	MAP	rhs_expr
+			{
+				$$ = map_expr_alloc(&@$, $1, $3);
+			}
+			;
+
+stmt_expr		:	map_stmt_expr
+			|	multiton_expr
+			|	primary_expr
+			;
+
+nat_stmt_args		:	stmt_expr
 			{
 				$<stmt>0->nat.addr = $1;
 			}
-			|	expr	COLON	expr
+			|	stmt_expr	COLON	stmt_expr
 			{
 				$<stmt>0->nat.addr = $1;
 				$<stmt>0->nat.proto = $3;
 			}
-			|	COLON	expr
+			|	COLON		stmt_expr
 			{
 				$<stmt>0->nat.proto = $2;
 			}
-			|	nat_stmt_args	nf_nat_flags
+			|       nat_stmt_args   nf_nat_flags
 			{
 				$<stmt>0->nat.flags = $2;
 			}
@@ -1614,7 +1649,7 @@ redir_stmt		:	redir_stmt_alloc	redir_stmt_arg
 redir_stmt_alloc	:	REDIRECT	{ $$ = redir_stmt_alloc(&@$); }
 			;
 
-redir_stmt_arg		:	TO	expr
+redir_stmt_arg		:	TO	stmt_expr
 			{
 				$<stmt>0->redir.proto = $2;
 			}
@@ -1622,19 +1657,19 @@ redir_stmt_arg		:	TO	expr
 			{
 				$<stmt>0->redir.flags = $1;
 			}
-			|	TO	expr	nf_nat_flags
+			|	TO	stmt_expr	nf_nat_flags
 			{
 				$<stmt>0->redir.proto = $2;
 				$<stmt>0->redir.flags = $3;
 			}
 			;
 
-dup_stmt		:	DUP	TO	expr
+dup_stmt		:	DUP	TO	stmt_expr
 			{
 				$$ = dup_stmt_alloc(&@$);
 				$$->dup.to = $3;
 			}
-			|	DUP	TO	expr 	DEVICE	expr
+			|	DUP	TO	stmt_expr 	DEVICE	stmt_expr
 			{
 				$$ = dup_stmt_alloc(&@$);
 				$$->dup.to = $3;
@@ -1671,7 +1706,7 @@ queue_stmt_args		:	queue_stmt_arg
 			|	queue_stmt_args	queue_stmt_arg
 			;
 
-queue_stmt_arg		:	QUEUENUM	expr
+queue_stmt_arg		:	QUEUENUM	stmt_expr
 			{
 				$<stmt>0->queue.queue = $2;
 			}
@@ -1865,7 +1900,6 @@ map_expr		:	concat_expr	MAP	rhs_expr
 			;
 
 expr			:	concat_expr
-			|	multiton_expr
 			|	set_expr
 			|       map_expr
 			;
Signature · 1 line
2.1.4
author maintainer
#1 lore raw

PATCH 2/3 parser: rename multiton_expr to multiton_rhs_expr

This rule catches occurrences from the constant rhs, rename it for readability reasons.

Note that this rule is still used from the set lhs definition that is always constant (as it represents the key to look up for the corresponding element).

---
 src/parser_bison.y | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
src/parser_bison.y +14 -14
index b49eadb..9f75abe 100644
@@ -482,10 +482,10 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %type <expr>			basic_expr
 %destructor { expr_free($$); }	basic_expr
 
-%type <expr>			multiton_expr
-%destructor { expr_free($$); }	multiton_expr
-%type <expr>			prefix_expr range_expr wildcard_expr
-%destructor { expr_free($$); }	prefix_expr range_expr wildcard_expr
+%type <expr>			multiton_rhs_expr
+%destructor { expr_free($$); }	multiton_rhs_expr
+%type <expr>			prefix_rhs_expr range_rhs_expr wildcard_rhs_expr
+%destructor { expr_free($$); }	prefix_rhs_expr range_rhs_expr wildcard_rhs_expr
 
 %type <expr>			stmt_expr concat_stmt_expr map_stmt_expr
 %destructor { expr_free($$); }	stmt_expr concat_stmt_expr map_stmt_expr
@@ -1608,8 +1608,8 @@ map_stmt_expr		:	concat_stmt_expr	MAP	rhs_expr
 			;
 
 stmt_expr		:	map_stmt_expr
-			|	multiton_expr
-			|	primary_expr
+			|	multiton_rhs_expr
+			|	primary_rhs_expr
 			;
 
 nat_stmt_args		:	stmt_expr
@@ -1865,19 +1865,19 @@ list_expr		:	basic_expr		COMMA		basic_expr
 			}
 			;
 
-prefix_expr		:	basic_rhs_expr		SLASH	NUM
+prefix_rhs_expr		:	basic_rhs_expr	SLASH	NUM
 			{
 				$$ = prefix_expr_alloc(&@$, $1, $3);
 			}
 			;
 
-range_expr		:	basic_rhs_expr		DASH	basic_rhs_expr
+range_rhs_expr		:	basic_rhs_expr	DASH	basic_rhs_expr
 			{
 				$$ = range_expr_alloc(&@$, $1, $3);
 			}
 			;
 
-wildcard_expr		:	ASTERISK
+wildcard_rhs_expr	:	ASTERISK
 	       		{
 				struct expr *expr;
 
@@ -1888,9 +1888,9 @@ wildcard_expr		:	ASTERISK
 			}
 			;
 
-multiton_expr		:	prefix_expr
-			|	range_expr
-			|	wildcard_expr
+multiton_rhs_expr	:	prefix_rhs_expr
+			|	range_rhs_expr
+			|	wildcard_rhs_expr
 			;
 
 map_expr		:	concat_expr	MAP	rhs_expr
@@ -1966,7 +1966,7 @@ set_elem_option		:	TIMEOUT			time_spec
 			;
 
 set_lhs_expr		:	concat_rhs_expr
-			|	multiton_expr
+			|	multiton_rhs_expr
 			;
 
 set_rhs_expr		:	concat_rhs_expr
@@ -2006,7 +2006,7 @@ list_rhs_expr		:	basic_rhs_expr		COMMA		basic_rhs_expr
 			;
 
 rhs_expr		:	concat_rhs_expr		{ $$ = $1; }
-			|	multiton_expr		{ $$ = $1; }
+			|	multiton_rhs_expr	{ $$ = $1; }
 			|	set_expr		{ $$ = $1; }
 			;
 
Signature · 1 line
2.1.4
author maintainer
#2 lore raw

PATCH 3/3 parser: restore bitwise operations from the rhs of relational expressions

In reply to Pablo Neira Ayuso

Reintroduce bitwise operation on constants that was removed in ("parser:
restrict relational rhs expression recursion") since we support this since the beginning.

---
 src/parser_bison.y | 47 +++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 41 insertions(+), 6 deletions(-)
src/parser_bison.y +41 -6
index 9f75abe..af51e4d 100644
@@ -514,8 +514,11 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 
 %type <expr>			rhs_expr concat_rhs_expr basic_rhs_expr
 %destructor { expr_free($$); }	rhs_expr concat_rhs_expr basic_rhs_expr
-%type <expr>			primary_rhs_expr list_rhs_expr
-%destructor { expr_free($$); }	primary_rhs_expr list_rhs_expr
+%type <expr>			primary_rhs_expr list_rhs_expr shift_rhs_expr
+%destructor { expr_free($$); }	primary_rhs_expr list_rhs_expr shift_rhs_expr
+%type <expr>			and_rhs_expr exclusive_or_rhs_expr inclusive_or_rhs_expr
+%destructor { expr_free($$); }	and_rhs_expr exclusive_or_rhs_expr inclusive_or_rhs_expr
+
 
 %type <expr>			relational_expr
 %destructor { expr_free($$); }	relational_expr
@@ -2010,7 +2013,42 @@ rhs_expr		:	concat_rhs_expr		{ $$ = $1; }
 			|	set_expr		{ $$ = $1; }
 			;
 
-concat_rhs_expr		:	basic_rhs_expr		{ $$ = $1; }
+shift_rhs_expr		:	primary_rhs_expr
+			|	shift_rhs_expr		LSHIFT		primary_rhs_expr
+			{
+				$$ = binop_expr_alloc(&@$, OP_LSHIFT, $1, $3);
+			}
+			|	shift_rhs_expr		RSHIFT		primary_rhs_expr
+			{
+				$$ = binop_expr_alloc(&@$, OP_RSHIFT, $1, $3);
+			}
+			;
+
+and_rhs_expr		:	shift_rhs_expr
+			|	and_rhs_expr		AMPERSAND	shift_rhs_expr
+			{
+				$$ = binop_expr_alloc(&@$, OP_AND, $1, $3);
+			}
+			;
+
+exclusive_or_rhs_expr	:	and_rhs_expr
+			|	exclusive_or_rhs_expr	CARET		and_rhs_expr
+			{
+				$$ = binop_expr_alloc(&@$, OP_XOR, $1, $3);
+			}
+			;
+
+inclusive_or_rhs_expr	:	exclusive_or_rhs_expr
+			|	inclusive_or_rhs_expr	'|'		exclusive_or_rhs_expr
+			{
+				$$ = binop_expr_alloc(&@$, OP_OR, $1, $3);
+			}
+			;
+
+basic_rhs_expr		:	inclusive_or_rhs_expr
+			;
+
+concat_rhs_expr		:	basic_rhs_expr
 			|	concat_rhs_expr	DOT	basic_rhs_expr
 			{
 				if ($$->ops->type != EXPR_CONCAT) {
@@ -2030,9 +2068,6 @@ concat_rhs_expr		:	basic_rhs_expr		{ $$ = $1; }
 			}
 			;
 
-basic_rhs_expr		:	primary_rhs_expr	{ $$ = $1; }
-			;
-
 primary_rhs_expr	:	symbol_expr		{ $$ = $1; }
 			|	integer_expr		{ $$ = $1; }
 			|	ETHER
Signature · 1 line
2.1.4
#3 lore raw
In reply to Pablo Neira Ayuso
Quoted 4 lines · Pablo Neira Ayuso

The multiton_expr rule matches range, prefix and wildcard expressions which don't make sense from the non-constant lhs. This rule is there to handle the nat statement case, whose expression may be composed of address and port ranges (hence range expressions).

Its actually originates from set expressions. As long as we can still use it there and can use bitwise and similar expressions for both sets and maps this seems fine.

Quoted 6 lines

To resolve this, this patch adds the stmt_expr rule to handle the possible occurrences of map, multiton and primary expressions from statements.

This results in more rules but it narrows down what we can find from expressions that are part of action statements.

I had a similar patch which simply split it up into lhs and rhs expressions. Using stmt_expr seems a bit limited considering that we already use it for other things than statements.

Quoted 129 lines
---
 src/parser_bison.y | 54 ++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 44 insertions(+), 10 deletions(-)
src/parser_bison.y +44 -10
index d42bd2f..b49eadb 100644
@@ -486,6 +486,10 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %destructor { expr_free($$); }	multiton_expr
 %type <expr>			prefix_expr range_expr wildcard_expr
 %destructor { expr_free($$); }	prefix_expr range_expr wildcard_expr
+
+%type <expr>			stmt_expr concat_stmt_expr map_stmt_expr
+%destructor { expr_free($$); }	stmt_expr concat_stmt_expr map_stmt_expr
+
 %type <expr>			list_expr
 %destructor { expr_free($$); }	list_expr
 %type <expr>			concat_expr
@@ -1577,20 +1581,51 @@ nat_stmt_alloc		:	SNAT
 			}
 			;
 
-nat_stmt_args		:	expr
+concat_stmt_expr	:	primary_expr
+			|	concat_stmt_expr	DOT	primary_expr
+			{
+				if ($$->ops->type != EXPR_CONCAT) {
+					$$ = concat_expr_alloc(&@$);
+					compound_expr_add($$, $1);
+				} else {
+					struct location rhs[] = {
+						[1]	= @2,
+						[2]	= @3,
+					};
+					location_update(&$3->location, rhs, 2);
+
+					$$ = $1;
+					$$->location = @$;
+				}
+				compound_expr_add($$, $3);
+			}
+			;
+
+map_stmt_expr		:	concat_stmt_expr	MAP	rhs_expr
+			{
+				$$ = map_expr_alloc(&@$, $1, $3);
+			}
+			;
+
+stmt_expr		:	map_stmt_expr
+			|	multiton_expr
+			|	primary_expr
+			;
+
+nat_stmt_args		:	stmt_expr
 			{
 				$<stmt>0->nat.addr = $1;
 			}
-			|	expr	COLON	expr
+			|	stmt_expr	COLON	stmt_expr
 			{
 				$<stmt>0->nat.addr = $1;
 				$<stmt>0->nat.proto = $3;
 			}
-			|	COLON	expr
+			|	COLON		stmt_expr
 			{
 				$<stmt>0->nat.proto = $2;
 			}
-			|	nat_stmt_args	nf_nat_flags
+			|       nat_stmt_args   nf_nat_flags
 			{
 				$<stmt>0->nat.flags = $2;
 			}
@@ -1614,7 +1649,7 @@ redir_stmt		:	redir_stmt_alloc	redir_stmt_arg
 redir_stmt_alloc	:	REDIRECT	{ $$ = redir_stmt_alloc(&@$); }
 			;
 
-redir_stmt_arg		:	TO	expr
+redir_stmt_arg		:	TO	stmt_expr
 			{
 				$<stmt>0->redir.proto = $2;
 			}
@@ -1622,19 +1657,19 @@ redir_stmt_arg		:	TO	expr
 			{
 				$<stmt>0->redir.flags = $1;
 			}
-			|	TO	expr	nf_nat_flags
+			|	TO	stmt_expr	nf_nat_flags
 			{
 				$<stmt>0->redir.proto = $2;
 				$<stmt>0->redir.flags = $3;
 			}
 			;
 
-dup_stmt		:	DUP	TO	expr
+dup_stmt		:	DUP	TO	stmt_expr
 			{
 				$$ = dup_stmt_alloc(&@$);
 				$$->dup.to = $3;
 			}
-			|	DUP	TO	expr 	DEVICE	expr
+			|	DUP	TO	stmt_expr 	DEVICE	stmt_expr
 			{
 				$$ = dup_stmt_alloc(&@$);
 				$$->dup.to = $3;
@@ -1671,7 +1706,7 @@ queue_stmt_args		:	queue_stmt_arg
 			|	queue_stmt_args	queue_stmt_arg
 			;
 
-queue_stmt_arg		:	QUEUENUM	expr
+queue_stmt_arg		:	QUEUENUM	stmt_expr
 			{
 				$<stmt>0->queue.queue = $2;
 			}
@@ -1865,7 +1900,6 @@ map_expr		:	concat_expr	MAP	rhs_expr
 			;
 
 expr			:	concat_expr
-			|	multiton_expr
 			|	set_expr
 			|       map_expr
 			;
Signature · 1 line
2.1.4