Skip to contentSkip to conversation

arbirary table/chain names

PATCH In review Florian Westphal 2 participants 12 messages Mar 16, 2021 – Mar 24, 2021 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover arbirary table/chain names

This series allows (almost) arbitrary chain names.

Unsolved problem:
nft has implict 'rule add' behaviour, e.g.

'nft add rule ip filter input foo ip saddr 1.2.3.4 drop' can be written like 'nft ip filter input foo ip saddr 1.2.3.4 drop' or even
'nft filter input foo ip saddr 1.2.3.4 drop'.

IOW, the scanner cannot switch to the exclusive rule scope added in patch 5 to allow for arbitrary names.

Patch 6 resolves this by switching state from bison, but this requires to add future tokens to a special whitelist.

It might be better to omit patch 6 and/or deprecate the implicit rule add behaviour. See patch 6 for details.

Florian Westphal (6):
scanner: add support for scope nesting
scanner: counter: move to own scope
scanner: log: move to own scope
scanner: support arbitary table names
scanner: support arbitrary chain names
src: allow arbitary chain name in implicit rule add case

 include/parser.h   |  12 ++++
 src/parser_bison.y |  97 ++++++++++++++++++-------
 src/scanner.l      | 173 +++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 241 insertions(+), 41 deletions(-)
Signature · 1 line
2.26.2
author maintainer
#1 lore raw

PATCH 1/6 scanner: add support for scope nesting

Adding a COUNTER scope introduces parsing errors. Example:

add rule ... counter ip saddr 1.2.3.4

This is supposed to be

COUNTER IP SADDR SYMBOL

but it will be parsed as

COUNTER IP STRING SYMBOL

... and rule fails with unknown saddr.
This is because IP state change gets popped right after it was pushed.

bison parser invokes scanner_pop_start_cond() helper via 'close_scope_counter' rule after it has processed the entire 'counter' rule. But that happens *after* flex has executed the 'IP' rule.

IOW, the sequence of events is not the exepcted
"COUNTER close_scope_counter IP SADDR SYMBOL close_scope_ip", it is "COUNTER IP close_scope_counter".

close_scope_counter pops the just-pushed SCANSTATE_IP and returns the scanner to SCANSTATE_COUNTER, so next input token (saddr) gets parsed as a string, which gets then rejected from bison.

To resolve this, defer the pop operation until the current state is done. scanner_pop_start_cond() already gets the scope that it has been completed as an argument, so we can compare it to the active state.

If those are not the same, just defer the pop operation until the bison reports its done with the active flex scope.

This leads to following sequence of events:
1. flex switches to SCANSTATE_COUNTER
2. flex switches to SCANSTATE_IP
3. bison calls scanner_pop_start_cond(SCANSTATE_COUNTER)
4. flex remains in SCANSTATE_IP, bison continues
5. bison calls scanner_pop_start_cond(SCANSTATE_IP) once the entire ip rule has completed: this pops both IP and COUNTER.

---
 include/parser.h |  2 ++
 src/scanner.l    | 17 +++++++++++++++++
 2 files changed, 19 insertions(+)
include/parser.h +2 -0
index 9fdebcd11dd2..0c229963d3be 100644
@@ -23,6 +23,8 @@ struct parser_state {
 	struct scope			*scopes[SCOPE_NEST_MAX];
 	unsigned int			scope;
 
+	unsigned int			flex_state_pop;
+	unsigned int			startcond_type;
 	struct list_head		*cmds;
 };
 
src/scanner.l +17 -0
index a73ce1b819d8..01e1dca52fdd 100644
@@ -1017,11 +1017,28 @@ void scanner_destroy(struct nft_ctx *nft)
 
 static void scanner_push_start_cond(void *scanner, enum startcond_type type)
 {
+	struct parser_state *state = yyget_extra(scanner);
+
+	state->startcond_type = type;
 	yy_push_state((int)type, scanner);
 }
 
 void scanner_pop_start_cond(void *scanner, enum startcond_type t)
 {
+	struct parser_state *state = yyget_extra(scanner);
+
+	if (state->startcond_type != t) {
+		state->flex_state_pop++;
+		return; /* Can't pop just yet! */
+	}
+
+	while (state->flex_state_pop) {
+		state->flex_state_pop--;
+		state->startcond_type = yy_top_state(scanner);
+		yy_pop_state(scanner);
+	}
+
+	state->startcond_type = yy_top_state(scanner);
 	yy_pop_state(scanner);
 	(void)yy_top_state(scanner); /* suppress gcc warning wrt. unused function */
 }
Signature · 1 line
2.26.2
author maintainer
#2 lore raw

PATCH 2/6 scanner: counter: move to own scope

In reply to Florian Westphal

move bytes/packets away from initial state.

---
 include/parser.h   |  1 +
 src/parser_bison.y | 31 ++++++++++++++++---------------
 src/scanner.l      |  7 ++++---
 3 files changed, 21 insertions(+), 18 deletions(-)
include/parser.h +1 -0
index 0c229963d3be..59eff16eac20 100644
@@ -32,6 +32,7 @@ enum startcond_type {
 	PARSER_SC_BEGIN,
 	PARSER_SC_ARP,
 	PARSER_SC_CT,
+	PARSER_SC_COUNTER,
 	PARSER_SC_ETH,
 	PARSER_SC_IP,
 	PARSER_SC_IP6,
src/parser_bison.y +16 -15
index 08a2599e5374..805a38ab22ed 100644
@@ -863,6 +863,7 @@ opt_newline		:	NEWLINE
 
 close_scope_arp		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_ARP); };
 close_scope_ct		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_CT); };
+close_scope_counter	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_COUNTER); };
 close_scope_eth		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_ETH); };
 close_scope_fib		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_EXPR_FIB); };
 close_scope_hash	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_EXPR_HASH); };
@@ -1023,7 +1024,7 @@ add_cmd			:	TABLE		table_spec
 				handle_merge(&$3->handle, &$2);
 				$$ = cmd_alloc(CMD_ADD, CMD_OBJ_FLOWTABLE, &$2, &@$, $5);
 			}
-			|	COUNTER		obj_spec
+			|	COUNTER		obj_spec	close_scope_counter
 			{
 				struct obj *obj;
 
@@ -1032,11 +1033,11 @@ add_cmd			:	TABLE		table_spec
 				handle_merge(&obj->handle, &$2);
 				$$ = cmd_alloc(CMD_ADD, CMD_OBJ_COUNTER, &$2, &@$, obj);
 			}
-			|	COUNTER		obj_spec	counter_obj	counter_config
+			|	COUNTER		obj_spec	counter_obj	counter_config	close_scope_counter
 			{
 				$$ = cmd_alloc(CMD_ADD, CMD_OBJ_COUNTER, &$2, &@$, $3);
 			}
-			|	COUNTER		obj_spec	counter_obj	'{' counter_block '}'
+			|	COUNTER		obj_spec	counter_obj	'{' counter_block '}'	close_scope_counter
 			{
 				$$ = cmd_alloc(CMD_ADD, CMD_OBJ_COUNTER, &$2, &@$, $3);
 			}
@@ -1140,7 +1141,7 @@ create_cmd		:	TABLE		table_spec
 				handle_merge(&$3->handle, &$2);
 				$$ = cmd_alloc(CMD_CREATE, CMD_OBJ_FLOWTABLE, &$2, &@$, $5);
 			}
-			|	COUNTER		obj_spec
+			|	COUNTER		obj_spec	close_scope_counter
 			{
 				struct obj *obj;
 
@@ -1149,7 +1150,7 @@ create_cmd		:	TABLE		table_spec
 				handle_merge(&obj->handle, &$2);
 				$$ = cmd_alloc(CMD_CREATE, CMD_OBJ_COUNTER, &$2, &@$, obj);
 			}
-			|	COUNTER		obj_spec	counter_obj	counter_config
+			|	COUNTER		obj_spec	counter_obj	counter_config	close_scope_counter
 			{
 				$$ = cmd_alloc(CMD_CREATE, CMD_OBJ_COUNTER, &$2, &@$, $3);
 			}
@@ -1244,7 +1245,7 @@ delete_cmd		:	TABLE		table_or_id_spec
 				handle_merge(&$3->handle, &$2);
 				$$ = cmd_alloc(CMD_DELETE, CMD_OBJ_FLOWTABLE, &$2, &@$, $5);
 			}
-			|	COUNTER		obj_or_id_spec
+			|	COUNTER		obj_or_id_spec	close_scope_counter
 			{
 				$$ = cmd_alloc(CMD_DELETE, CMD_OBJ_COUNTER, &$2, &@$, NULL);
 			}
@@ -1312,7 +1313,7 @@ list_cmd		:	TABLE		table_spec
 			{
 				$$ = cmd_alloc(CMD_LIST, CMD_OBJ_COUNTERS, &$3, &@$, NULL);
 			}
-			|	COUNTER		obj_spec
+			|	COUNTER		obj_spec	close_scope_counter
 			{
 				$$ = cmd_alloc(CMD_LIST, CMD_OBJ_COUNTER, &$2, &@$, NULL);
 			}
@@ -1418,7 +1419,7 @@ reset_cmd		:	COUNTERS	ruleset_spec
 			{
 				$$ = cmd_alloc(CMD_RESET, CMD_OBJ_COUNTERS, &$3, &@$, NULL);
 			}
-			|       COUNTER         obj_spec
+			|       COUNTER         obj_spec	close_scope_counter
 			{
 				$$ = cmd_alloc(CMD_RESET, CMD_OBJ_COUNTER, &$2,&@$, NULL);
 			}
@@ -1621,7 +1622,7 @@ table_block		:	/* empty */	{ $$ = $<table>-1; }
 			}
 			|	table_block	COUNTER		obj_identifier
 					obj_block_alloc	'{'	counter_block	'}'
-					stmt_separator
+					stmt_separator	close_scope_counter
 			{
 				$4->location = @3;
 				$4->type = NFT_OBJECT_COUNTER;
@@ -1881,7 +1882,7 @@ map_block_alloc		:	/* empty */
 			}
 			;
 
-map_block_obj_type	:	COUNTER	{ $$ = NFT_OBJECT_COUNTER; }
+map_block_obj_type	:	COUNTER	close_scope_counter { $$ = NFT_OBJECT_COUNTER; }
 			|	QUOTA	close_scope_quota { $$ = NFT_OBJECT_QUOTA; }
 			|	LIMIT	close_scope_limit { $$ = NFT_OBJECT_LIMIT; }
 			|	SECMARK close_scope_secmark { $$ = NFT_OBJECT_SECMARK; }
@@ -2011,7 +2012,7 @@ flowtable_block		:	/* empty */	{ $$ = $<flowtable>-1; }
 			{
 				$$->dev_expr = $4;
 			}
-			|	flowtable_block COUNTER
+			|	flowtable_block COUNTER	close_scope_counter
 			{
 				$$->flags |= NFT_FLOWTABLE_COUNTER;
 			}
@@ -2782,11 +2783,11 @@ connlimit_stmt		:	CT	COUNT	NUM	close_scope_ct
 counter_stmt		:	counter_stmt_alloc
 			|	counter_stmt_alloc	counter_args
 
-counter_stmt_alloc	:	COUNTER
+counter_stmt_alloc	:	COUNTER	close_scope_counter
 			{
 				$$ = counter_stmt_alloc(&@$);
 			}
-			|	COUNTER		NAME	stmt_expr
+			|	COUNTER		NAME	stmt_expr	close_scope_counter
 			{
 				$$ = objref_stmt_alloc(&@$);
 				$$->objref.type = NFT_OBJECT_COUNTER;
@@ -4133,11 +4134,11 @@ set_elem_stmt_list	:	set_elem_stmt
 			}
 			;
 
-set_elem_stmt		:	COUNTER
+set_elem_stmt		:	COUNTER	close_scope_counter
 			{
 				$$ = counter_stmt_alloc(&@$);
 			}
-			|	COUNTER	PACKETS	NUM	BYTES	NUM
+			|	COUNTER	PACKETS	NUM	BYTES	NUM	close_scope_counter
 			{
 				$$ = counter_stmt_alloc(&@$);
 				$$->counter.packets = $3;
src/scanner.l +4 -3
index 01e1dca52fdd..783436504326 100644
@@ -198,6 +198,7 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 %option stack
 %s SCANSTATE_ARP
 %s SCANSTATE_CT
+%s SCANSTATE_COUNTER
 %s SCANSTATE_ETH
 %s SCANSTATE_IP
 %s SCANSTATE_IP6
@@ -343,10 +344,10 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 
 "flowtables"		{ return FLOWTABLES; }
 
-"counter"		{ return COUNTER; }
+"counter"		{ scanner_push_start_cond(yyscanner, SCANSTATE_COUNTER); return COUNTER; }
 "name"			{ return NAME; }
-"packets"		{ return PACKETS; }
-"bytes"			{ return BYTES; }
+<SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT>"packets"		{ return PACKETS; }
+<SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT,SCANSTATE_QUOTA>"bytes"	{ return BYTES; }
 
 "counters"		{ return COUNTERS; }
 "quotas"		{ return QUOTAS; }
Signature · 1 line
2.26.2
author maintainer
#3 lore raw

PATCH 3/6 scanner: log: move to own scope

In reply to Florian Westphal

GROUP and PREFIX are used by igmp and nat, so they can't be moved out of INITIAL scope yet.

---
 include/parser.h   |  2 ++
 src/parser_bison.y |  4 +++-
 src/scanner.l      | 12 ++++++++----
 3 files changed, 13 insertions(+), 5 deletions(-)
include/parser.h +2 -0
index 59eff16eac20..d890ab223c52 100644
@@ -47,6 +47,8 @@ enum startcond_type {
 	PARSER_SC_EXPR_QUEUE,
 	PARSER_SC_EXPR_RT,
 	PARSER_SC_EXPR_SOCKET,
+
+	PARSER_SC_STMT_LOG,
 };
 
 struct mnl_socket;
src/parser_bison.y +3 -1
index 805a38ab22ed..98fe4431c4f4 100644
@@ -879,6 +879,8 @@ close_scope_rt		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_EXPR_RT); };
 close_scope_secmark	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_SECMARK); };
 close_scope_socket	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_EXPR_SOCKET); }
 
+close_scope_log		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_STMT_LOG); }
+
 common_block		:	INCLUDE		QUOTED_STRING	stmt_separator
 			{
 				if (scanner_include_file(nft, scanner, $2, &@$) < 0) {
@@ -2695,7 +2697,7 @@ stmt			:	verdict_stmt
 			|	payload_stmt
 			|	stateful_stmt
 			|	meta_stmt
-			|	log_stmt
+			|	log_stmt	close_scope_log
 			|	reject_stmt
 			|	nat_stmt
 			|	tproxy_stmt
src/scanner.l +8 -4
index 783436504326..0082b3eeca29 100644
@@ -214,6 +214,8 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 %s SCANSTATE_EXPR_RT
 %s SCANSTATE_EXPR_SOCKET
 
+%s SCANSTATE_STMT_LOG
+
 %%
 
 "=="			{ return EQ; }
@@ -354,12 +356,14 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 "limits"		{ return LIMITS; }
 "synproxys"		{ return SYNPROXYS; }
 
-"log"			{ return LOG; }
+"log"			{ scanner_push_start_cond(yyscanner, SCANSTATE_STMT_LOG); return LOG; }
 "prefix"		{ return PREFIX; }
 "group"			{ return GROUP; }
-"snaplen"		{ return SNAPLEN; }
-"queue-threshold"	{ return QUEUE_THRESHOLD; }
-"level"			{ return LEVEL; }
+<SCANSTATE_STMT_LOG>{
+	"snaplen"		{ return SNAPLEN; }
+	"queue-threshold"	{ return QUEUE_THRESHOLD; }
+	"level"			{ return LEVEL; }
+}
 
 "queue"			{ scanner_push_start_cond(yyscanner, SCANSTATE_EXPR_QUEUE); return QUEUE;}
 <SCANSTATE_EXPR_QUEUE>{
Signature · 1 line
2.26.2
author maintainer
#4 lore raw

PATCH 4/6 scanner: support arbitary table names

In reply to Florian Westphal

Add exclusive start condition that only recognizes strings, then switch to it from table keyword.

This prevents

table foo {

... from breaking when a foo expression keyword would be added to nft in the future.

---
 include/parser.h |  3 ++
 src/scanner.l    | 72 +++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 74 insertions(+), 1 deletion(-)
include/parser.h +3 -0
index d890ab223c52..0843aa1adb6a 100644
@@ -25,6 +25,7 @@ struct parser_state {
 
 	unsigned int			flex_state_pop;
 	unsigned int			startcond_type;
+	unsigned int			saw_family:1;
 	struct list_head		*cmds;
 };
 
@@ -49,6 +50,8 @@ enum startcond_type {
 	PARSER_SC_EXPR_SOCKET,
 
 	PARSER_SC_STMT_LOG,
+
+	PARSER_SC_STRING_TABLE,
 };
 
 struct mnl_socket;
src/scanner.l +71 -1

105 lines not on this page · raw message

Signature · 1 line
2.26.2
author maintainer
#5 lore raw

PATCH 5/6 scanner: support arbitrary chain names

In reply to Florian Westphal

Previous change removed constraints on table naming, this extends it to handle chain names as well.

The new exclusive scope also needs to permit '{' to handle bounded chains, e.g.

.... jump { ip ...

and "handle", to deal with: 'delete chain test-ip handle 2' -- in both cases flex needs to leave the chain scope immediately.

The parser_state struct is used to record if we saw a family or table name:

chain tname cname
chain ip tname cname
chain cname {
chain tname handle 1
chain ip tname handle 1

... are all valid. We can't exit after the second name, first name could have been the family name, not the table.

This change allows nft to parse:

  table ip dup {
        chain fwd {
        }
 }
 table inet dup {
        chain fwd {
        }

}

The point here is not to allow aribitrary names (for instance 'table ip handle' won't work), its to make sure a new/future expression keyword won't break an existing ruleset where the 'new keyword' happened to be the name of a chain/table.

Remaining issue:

   |       RULE            rule_position   rule
   {
       $$ = cmd_alloc(CMD_ADD, CMD_OBJ_RULE, &$2, &@$, $3);
   }
   |       /* empty */     rule_position   rule

so, implicit 'add rule', e.g. "nft ip tname cname ip saddr ..." will not work when either tname or cname would be added as token/expression in a future release.

This can't be fully resolved, there is no context (from scanner) to know that 'ip tname cname' need to be parsed as 'IP STRING STRING'.

With the long form (nft add rule ip tname cname ...), the RULE token allows to switch scanner start condition.

Switching state from bison doesn't work reliably as the switch rule will be evaluated too late, e.g. 'nft add dup ...'.

---
 include/parser.h   |  3 ++
 src/parser_bison.y |  5 +--
 src/scanner.l      | 81 ++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 74 insertions(+), 15 deletions(-)
include/parser.h +3 -0
index 0843aa1adb6a..d6cf20729421 100644
@@ -26,6 +26,8 @@ struct parser_state {
 	unsigned int			flex_state_pop;
 	unsigned int			startcond_type;
 	unsigned int			saw_family:1;
+	unsigned int			saw_table:1;
+	unsigned int			saw_chain:1;
 	struct list_head		*cmds;
 };
 
@@ -52,6 +54,7 @@ enum startcond_type {
 	PARSER_SC_STMT_LOG,
 
 	PARSER_SC_STRING_TABLE,
+	PARSER_SC_STRING_CHAIN,
 };
 
 struct mnl_socket;
src/parser_bison.y +3 -2
index 98fe4431c4f4..bbac85fd35ce 100644
@@ -864,6 +864,7 @@ opt_newline		:	NEWLINE
 close_scope_arp		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_ARP); };
 close_scope_ct		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_CT); };
 close_scope_counter	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_COUNTER); };
+close_scope_chain	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_STRING_CHAIN); }
 close_scope_eth		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_ETH); };
 close_scope_fib		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_EXPR_FIB); };
 close_scope_hash	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_EXPR_HASH); };
@@ -2462,7 +2463,7 @@ tableid_spec 		: 	family_spec 	HANDLE NUM
 			}
 			;
 
-chain_spec		:	table_spec	identifier
+chain_spec		:	table_spec	identifier	close_scope_chain
 			{
 				$$		= $1;
 				$$.chain.name	= $2;
@@ -2470,7 +2471,7 @@ chain_spec		:	table_spec	identifier
 			}
 			;
 
-chainid_spec 		: 	table_spec 	HANDLE NUM
+chainid_spec 		: 	table_spec 	HANDLE NUM	close_scope_chain
 			{
 				$$ 			= $1;
 				$$.handle.location 	= @3;
src/scanner.l +68 -13

144 lines not on this page · raw message

Signature · 1 line
2.26.2
author maintainer
#6 lore raw

PATCH 6/6 src: allow arbitary chain name in implicit rule add case

In reply to Florian Westphal

Allow switch of the flex state from bison parser. Note that this switch will happen too late to cover all cases:

nft add ip dup fwd ip saddr ... # adds a rule to chain fwd in table dup nft add dup fwd ... # syntax error (flex parses dup as expression keyword)

to solve this, bison must carry a list of keywords that are allowed to be used as table names.

This adds FWD as an example. When new keywords are added, this can then be extended as needed.

Another alternative is to deprecate implicit rule add altogether so users would have to move to 'nft add rule ...'.

---
 include/parser.h   |  1 +
 src/parser_bison.y | 57 ++++++++++++++++++++++++++++++++++++++--------
 src/scanner.l      |  4 +---
 3 files changed, 50 insertions(+), 12 deletions(-)
include/parser.h +1 -0
index d6cf20729421..35117acc977f 100644
@@ -77,5 +77,6 @@ extern void scanner_push_buffer(void *scanner,
 				const char *buffer);
 
 extern void scanner_pop_start_cond(void *scanner, enum startcond_type sc);
+extern void scanner_push_start_cond(void *scanner, enum startcond_type sc);
 
 #endif /* NFTABLES_PARSER_H */
src/parser_bison.y +48 -9

88 lines not on this page · raw message

src/scanner.l +1 -3
index a156accaa944..a4747b39b314 100644
@@ -101,8 +101,6 @@ static void reset_pos(struct parser_state *state, struct location *loc)
 static int scanner_handle_tablename(void *scanner, const char *token);
 static int scanner_handle_chainname(void *scanner, const char *token);
 
-static void scanner_push_start_cond(void *scanner, enum startcond_type type);
-
 #define YY_USER_ACTION {					\
 	update_pos(yyget_extra(yyscanner), yylloc, yyleng);	\
 	update_offset(yyget_extra(yyscanner), yylloc, yyleng);	\
@@ -1087,7 +1085,7 @@ void scanner_destroy(struct nft_ctx *nft)
 	yylex_destroy(nft->scanner);
 }
 
-static void scanner_push_start_cond(void *scanner, enum startcond_type type)
+void scanner_push_start_cond(void *scanner, enum startcond_type type)
 {
 	struct parser_state *state = yyget_extra(scanner);
 
Signature · 1 line
2.26.2
reviewer
#7 lore raw

Re: [PATCH nft 6/6] src: allow arbitary chain name in implicit rule add case

Hi,

On Wed, Mar 17, 2021 at 12:40:39AM +0100, Florian Westphal wrote:
[...]

Quoted 2 lines

Another alternative is to deprecate implicit rule add altogether so users would have to move to 'nft add rule ...'.

Isn't this required for nested syntax? I didn't check, but does your arbitrary table/chain name support work also when restoring a ruleset in that nested syntax? Another interesting aspect might be arbitrary set names - 'set' is also a valid keyword used in rules, this fact killed my approach with start conditions. ;)

Cheers, Phil

author maintainer
#8 lore raw

Re: [PATCH nft 6/6] src: allow arbitary chain name in implicit rule add case

Quoted 6 lines · Phil Sutter
Quoted 2 lines

Another alternative is to deprecate implicit rule add altogether so users would have to move to 'nft add rule ...'.

Isn't this required for nested syntax? I didn't check, but does your arbitrary table/chain name support work also when restoring a ruleset in that nested syntax?

Whats 'nested syntax'?

You mean "table bla { chain foo {"?

Quoted 3 lines

Another interesting aspect might be arbitrary set
names - 'set' is also a valid keyword used in rules, this fact killed my approach with start conditions. ;)

Right, arbitrary set names are needed as well, I forgot about them.

It should be possible by using two "set" rules in flex.

One in the INITIAL scope (to handle set bla {), and one in 'rule' or 'expression scope'.

The former would switch to an exclusive start condition (expect STRING, close condition on '{', just like CHAIN is handled here.

The latter would not change state and just return SET token.

reviewer
#9 lore raw

Re: [PATCH nft 6/6] src: allow arbitary chain name in implicit rule add case

Quoted 11 lines · Florian Westphal
Quoted 6 lines · Phil Sutter
Quoted 2 lines

Another alternative is to deprecate implicit rule add altogether so users would have to move to 'nft add rule ...'.

Isn't this required for nested syntax? I didn't check, but does your arbitrary table/chain name support work also when restoring a ruleset in that nested syntax?

Whats 'nested syntax'?

You mean "table bla { chain foo {"?

Yes, exactly.

Quoted 8 lines
Quoted 3 lines

Another interesting aspect might be arbitrary set
names - 'set' is also a valid keyword used in rules, this fact killed my approach with start conditions. ;)

Right, arbitrary set names are needed as well, I forgot about them.

It should be possible by using two "set" rules in flex.

Quoted 7 lines

One in the INITIAL scope (to handle set bla {), and one in 'rule' or 'expression scope'.

The former would switch to an exclusive start condition (expect STRING, close condition on '{', just like CHAIN is handled here.

The latter would not change state and just return SET token.

Yes, that might work.

Thanks, Phil

author maintainer
#10 lore raw

Re: [PATCH nft 6/6] src: allow arbitary chain name in implicit rule add case

In reply to Florian Westphal
Quoted 6 lines · Florian Westphal

Allow switch of the flex state from bison parser. Note that this switch will happen too late to cover all cases:

nft add ip dup fwd ip saddr ... # adds a rule to chain fwd in table dup nft add dup fwd ... # syntax error (flex parses dup as expression keyword)

Quoted 8 lines

to solve this, bison must carry a list of keywords that are allowed to be used as table names.

This adds FWD as an example. When new keywords are added, this can then be extended as needed.

Another alternative is to deprecate implicit rule add altogether so users would have to move to 'nft add rule ...'.

... and another alternative is to not allow arbitrary table/chain/set names after all.

We could just say that all future tokens that could break existing table/chain/set name need to be added to the 'identifier' in parser_bison.y.

Provided new expressions with args use start conditionals the list of tokens would probably stay short.

Given the 'set' complication Phil mentioned that might be the best way forward.

author maintainer
#11 lore raw

Re: [PATCH nft 6/6] src: allow arbitary chain name in implicit rule add case

Quoted 23 lines · Florian Westphal
Quoted 6 lines · Florian Westphal

Allow switch of the flex state from bison parser. Note that this switch will happen too late to cover all cases:

nft add ip dup fwd ip saddr ... # adds a rule to chain fwd in table dup nft add dup fwd ... # syntax error (flex parses dup as expression keyword)

Quoted 8 lines

to solve this, bison must carry a list of keywords that are allowed to be used as table names.

This adds FWD as an example. When new keywords are added, this can then be extended as needed.

Another alternative is to deprecate implicit rule add altogether so users would have to move to 'nft add rule ...'.

... and another alternative is to not allow arbitrary table/chain/set names after all.

We could just say that all future tokens that could break existing table/chain/set name need to be added to the 'identifier' in parser_bison.y.

Quoted 5 lines

Provided new expressions with args use start conditionals the list of tokens would probably stay short.

Given the 'set' complication Phil mentioned that might be the best way forward.

I've pushed the first 3 patches and marked the last 3 as deferred -- lets first try conservative approach first before attempting to support arbitrary names.