Introduce variables in chain priority and policy
cover Introduce variables in chain priority and policy
This patch series introduces the use of variables in chain priority and policy specification. It also contains tests for invalid cases.
Fernando Fernandez Mancera (2):
src: allow variables in the chain priority specification
src: allow variable in chain policy
include/datatype.h | 1 + include/rule.h | 10 +- src/datatype.c | 26 ++++++ src/evaluate.c | 92 +++++++++++++++++-- src/json.c | 5 +- src/mnl.c | 9 +- src/netlink.c | 8 +- src/parser_bison.y | 45 +++++++-- src/parser_json.c | 17 +++- src/rule.c | 17 +++- .../testcases/nft-f/0021priority_variable_0 | 17 ++++ .../testcases/nft-f/0022priority_variable_0 | 17 ++++ .../testcases/nft-f/0023priority_variable_1 | 18 ++++ .../testcases/nft-f/0024priority_variable_1 | 18 ++++ .../testcases/nft-f/0025policy_variable_0 | 17 ++++ .../testcases/nft-f/0026policy_variable_0 | 17 ++++ .../testcases/nft-f/0027policy_variable_1 | 18 ++++ .../testcases/nft-f/0028policy_variable_1 | 18 ++++ .../nft-f/dumps/0021priority_variable_0.nft | 5 + .../nft-f/dumps/0022priority_variable_0.nft | 5 + .../nft-f/dumps/0025policy_variable_0.nft | 5 + .../nft-f/dumps/0026policy_variable_0.nft | 5 + 22 files changed, 350 insertions(+), 40 deletions(-) mode change 100644 => 100755 src/evaluate.c create mode 100755 tests/shell/testcases/nft-f/0021priority_variable_0 create mode 100755 tests/shell/testcases/nft-f/0022priority_variable_0 create mode 100755 tests/shell/testcases/nft-f/0023priority_variable_1 create mode 100755 tests/shell/testcases/nft-f/0024priority_variable_1 create mode 100755 tests/shell/testcases/nft-f/0025policy_variable_0 create mode 100755 tests/shell/testcases/nft-f/0026policy_variable_0 create mode 100755 tests/shell/testcases/nft-f/0027policy_variable_1 create mode 100755 tests/shell/testcases/nft-f/0028policy_variable_1 create mode 100644 tests/shell/testcases/nft-f/dumps/0021priority_variable_0.nft create mode 100644 tests/shell/testcases/nft-f/dumps/0022priority_variable_0.nft create mode 100644 tests/shell/testcases/nft-f/dumps/0025policy_variable_0.nft create mode 100644 tests/shell/testcases/nft-f/dumps/0026policy_variable_0.nft
Signature · 1 line
2.20.1
PATCH 1/2 src: allow variables in the chain priority specification
This patch introduces the use of nft input files variables in chain priority specification. e.g.
define pri = filter
define prinum = 10
add table ip foo
add chain ip foo bar {type filter hook input priority $pri;} add chain ip foo ber {type filter hook input priority $prinum;} add chain ip foo bor {type filter hook input priority $pri + 10;}
table ip foo {
chain bar {
type filter hook input priority filter; policy accept;
}
chain ber {
type filter hook input priority filter + 10; policy accept;
}
chain bor {
type filter hook input priority filter + 10; policy accept;
}
}--- include/datatype.h | 1 + include/rule.h | 8 ++-- src/datatype.c | 26 ++++++++++++ src/evaluate.c | 41 ++++++++++++++----- src/parser_bison.y | 30 ++++++++++---- src/rule.c | 4 +- .../testcases/nft-f/0021priority_variable_0 | 17 ++++++++ .../testcases/nft-f/0022priority_variable_0 | 17 ++++++++ .../testcases/nft-f/0023priority_variable_1 | 18 ++++++++ .../testcases/nft-f/0024priority_variable_1 | 18 ++++++++ .../nft-f/dumps/0021priority_variable_0.nft | 5 +++ .../nft-f/dumps/0022priority_variable_0.nft | 5 +++ 12 files changed, 166 insertions(+), 24 deletions(-) mode change 100644 => 100755 src/evaluate.c create mode 100755 tests/shell/testcases/nft-f/0021priority_variable_0 create mode 100755 tests/shell/testcases/nft-f/0022priority_variable_0 create mode 100755 tests/shell/testcases/nft-f/0023priority_variable_1 create mode 100755 tests/shell/testcases/nft-f/0024priority_variable_1 create mode 100644 tests/shell/testcases/nft-f/dumps/0021priority_variable_0.nft create mode 100644 tests/shell/testcases/nft-f/dumps/0022priority_variable_0.nft
include/datatype.h +1 -0
@@ -256,6 +256,7 @@ extern const struct datatype icmpx_code_type;extern const struct datatype igmp_type_type;extern const struct datatype time_type;extern const struct datatype boolean_type; +extern const struct datatype priority_type;void inet_service_type_print (const struct expr *expr,struct output_ctx *octx);
include/rule.h +4 -4
@@ -174,13 +174,13 @@ enum chain_flags {* struct prio_spec - extendend priority specification for mixed * textual/numerical parsing. * -* @str: name of the standard priority value -* @num: Numerical value. This MUST contain the parsed value of str after +* @expr: expr of the standard priority value +* @num: Numerical value. This MUST contain the parsed value of expr after * evaluation. */ struct prio_spec { -const char *str; -int num; +struct expr *expr; +int num;struct location loc; };
src/datatype.c +26 -0
@@ -1246,3 +1246,29 @@ const struct datatype boolean_type = { .sym_tbl = &boolean_tbl, .json = boolean_type_json, }; + +static struct error_record *priority_type_parse (const struct expr *sym, +struct expr **res) +{ +int num; + +if (isdigit (sym->identifier[0 ])) { + num =atoi (sym->identifier); + *res =constant_expr_alloc (&sym->location, &integer_type, +BYTEORDER_HOST_ENDIAN , +sizeof (int ) *BITS_PER_BYTE , + &num); + }else + *res =constant_expr_alloc (&sym->location, &string_type, +BYTEORDER_HOST_ENDIAN , +strlen (sym->identifier) * +BITS_PER_BYTE , sym->identifier); +return NULL ; +} + +const struct datatype priority_type = { + .type =TYPE_STRING , + .name ="priority" , + .desc ="priority type" , + .parse = priority_type_parse, +};
src/evaluate.c +31 -10
67 lines not on this page · raw message
src/parser_bison.y +22 -8
@@ -636,8 +636,8 @@ int nft_lex(void *, void *, void *); %type <stmt> meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc %destructor { stmt_free($$); } meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc -%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr -%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr +%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr +%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr %type <expr> primary_expr shift_expr and_expr %destructor { expr_free($$); } primary_expr shift_expr and_expr %type <expr> exclusive_or_expr inclusive_or_expr
@@ -1969,30 +1969,44 @@ extended_prio_name : OUT | STRING ; +prio_expr : variable_expr + { + datatype_set($1->sym->expr, &priority_type); + $$ = $1; + } + | extended_prio_name + { + $$ = constant_expr_alloc(&@$, &string_type, + BYTEORDER_HOST_ENDIAN, + strlen($1) * + BITS_PER_BYTE, $1); + } + ; + extended_prio_spec : int_num { struct prio_spec spec = {0}; spec.num = $1; $$ = spec; } - | extended_prio_name + | prio_expr { struct prio_spec spec = {0}; - spec.str = $1; + spec.expr = $1; $$ = spec; } - | extended_prio_name PLUS NUM + | prio_expr PLUS NUM { struct prio_spec spec = {0}; spec.num = $3; - spec.str = $1; + spec.expr = $1; $$ = spec; } - | extended_prio_name DASH NUM + | prio_expr DASH NUM { struct prio_spec spec = {0}; spec.num = -$3; - spec.str = $1; + spec.expr = $1; $$ = spec; } ;
src/rule.c +2 -2
@@ -823,7 +823,7 @@ void chain_free(struct chain *chain)xfree (chain->type);if (chain->dev !=NULL )xfree (chain->dev); -xfree (chain->priority.str); +expr_free (chain->priority.expr);xfree (chain); }
@@ -2049,7 +2049,7 @@ void flowtable_free(struct flowtable *flowtable)if (--flowtable->refcnt >0 )return ;handle_free (&flowtable->handle); -xfree (flowtable->priority.str); +expr_free (flowtable->priority.expr);xfree (flowtable); }
tests/shell/testcases/nft-f/0021priority_variable_0 new file +17 -0
@@ -0,0 +1,17 @@ +#!/bin/bash + +# Tests use of variables in priority specification + +set -e + +RULESET=" +define pri = filter + +table inet global { + chain prerouting { + type filter hook prerouting priority \$pri + policy accept + } +}" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/nft-f/0022priority_variable_0 new file +17 -0
@@ -0,0 +1,17 @@ +#!/bin/bash + +# Tests use of variables in priority specification + +set -e + +RULESET=" +define pri = 10 + +table inet global { + chain prerouting { + type filter hook prerouting priority \$pri + policy accept + } +}" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/nft-f/0023priority_variable_1 new file +18 -0
@@ -0,0 +1,18 @@ +#!/bin/bash + +# Tests use of variables in priority specification + +set -e + +RULESET=" +define pri = * + +table inet global { + chain prerouting { + type filter hook prerouting priority \$pri + policy accept + } +}" + +$NFT -f - <<< "$RULESET" && exit 1 +exit 0
tests/shell/testcases/nft-f/0024priority_variable_1 new file +18 -0
@@ -0,0 +1,18 @@ +#!/bin/bash + +# Tests use of variables in priority specification + +set -e + +RULESET=" +define pri = { 127.0.0.1 } + +table inet global { + chain prerouting { + type filter hook prerouting priority \$pri + policy accept + } +}" + +$NFT -f - <<< "$RULESET" && exit 1 +exit 0
tests/shell/testcases/nft-f/dumps/0021priority_variable_0.nft new file +5 -0
@@ -0,0 +1,5 @@ +table inet global { + chain prerouting { + type filter hook prerouting priority filter; policy accept; + } +}
tests/shell/testcases/nft-f/dumps/0022priority_variable_0.nft new file +5 -0
@@ -0,0 +1,5 @@ +table inet global { + chain prerouting { + type filter hook prerouting priority filter + 10; policy accept; + } +}
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerRe: [PATCH 1/2 nft] src: allow variables in the chain priority specification
Quoted 2 lines · Fernando Fernandez Mancera
include/rule.h +0
-0
[...]
Quoted 1 line
+const struct datatype
priority_type= {
Please, add here something like on top of the definition:
/* This datatype is not registered via datatype_register()
* since this datatype should not ever be used from either
* rules or elements.
*/
or alike, so we don't forget that missing datatype_register() is not a bug.
[...]
include/rule.h enum chain_flags { · line 179
Quoted 11 lines
include/rule.h +2
-2@@ -174,13 +174,13 @@ enum chain_flags {* struct prio_spec - extendend priority specification for mixed * textual/numerical parsing. * -* @str: name of the standard priority value -* @num: Numerical value. This MUST contain the parsed value of str after +* @expr: expr of the standard priority value +* @num: Numerical value. This MUST contain the parsed value of expr after * evaluation.
There must be a way to avoid this num field.
Quoted 24 lines
include/rule.h +2
-2*/ struct prio_spec { -const char *str; -int num; +struct expr *expr; +int num;struct location loc; };src/datatype.c +7
-0@@ -1246,3 +1246,29 @@ const struct datatype boolean_type = { .sym_tbl = &boolean_tbl, .json = boolean_type_json, }; + +static struct error_record *priority_type_parse (const struct expr *sym, +struct expr **res) +{ +int num; + +if (isdigit (sym->identifier[0 ])) {
src/datatype.c const struct datatype boolean_type = { · line 1265
Quoted 10 lines
src/datatype.c +10
-0+ num =atoi (sym->identifier); + *res =constant_expr_alloc (&sym->location, &integer_type, +BYTEORDER_HOST_ENDIAN , +sizeof (int ) *BITS_PER_BYTE , + &num); + }else + *res =constant_expr_alloc (&sym->location, &string_type, +BYTEORDER_HOST_ENDIAN , +strlen (sym->identifier) * +BITS_PER_BYTE , sym->identifier);
I'd suggest you call integer_datatype_parse() first, check if erec == NULL, then all good, this is an integer. Otherwise, release erec object and parse this as a string via string_datatype_parse().
Quoted 19 lines
src/datatype.c +9
-0+return NULL ; +} + +const struct datatype priority_type = { + .type =TYPE_STRING , + .name ="priority" , + .desc ="priority type" , + .parse = priority_type_parse, +};src/evaluate.c +0
-0@@ -3193,15 +3193,36 @@ static int set_evaluate(struct eval_ctx *ctx, struct set *set)return 0 ; }
src/evaluate.c static int set_evaluate(struct eval_ctx *ctx, struct set *set) · line 3203
Quoted 10 lines
src/evaluate.c +4
-2-static bool evaluate_priority (struct prio_spec *prio,int family,int hook) +static bool evaluate_priority (struct eval_ctx *ctx,struct prio_spec *prio, +int family,int hook) {int priority; +char prio_str[NFT_NAME_MAXLEN ];/* A numeric value has been used to specify priority. */ -if (prio->str ==NULL ) +if (prio->expr ==NULL )
If we always use an expression, as described below, this won't be needed.
Quoted 9 lines
src/evaluate.c +6
-1return true; - priority =std_prio_lookup (prio->str, family, hook); + ctx->ectx.dtype = &priority_type; + ctx->ectx.len =NFT_NAME_MAXLEN *BITS_PER_BYTE ; +if (expr_evaluate (ctx, &prio->expr) <0 ) +return false; +if (prio->expr->etype !=EXPR_VALUE ) { + expr_error(ctx->msgs, prio->expr, "%s is not a valid "
src/evaluate.c static int set_evaluate(struct eval_ctx *ctx, struct set *set) · line 3221
Quoted 10 lines
src/evaluate.c +10
-0+ "priority expression", expr_name(prio->expr)); +return false; + } + +if (prio->expr->dtype->type ==TYPE_INTEGER ) { +mpz_export_data (&prio->num, prio->expr->value, +BYTEORDER_HOST_ENDIAN ,sizeof (int )); +return true; + } + mpz_export_data(prio_str, prio->expr->value,
If you use symbol expression, as I describe below. You don't need to convert this constant expression back to string again. You can just use the symbol identify sym->identifier.
Quoted 53 lines
src/evaluate.c +11
-7+BYTEORDER_HOST_ENDIAN , + NFT_NAME_MAXLEN); + + priority =std_prio_lookup (prio_str, family, hook);if (priority ==NF_IP_PRI_LAST )return false; prio->num += priority;@@ -3223,10 +3244,10 @@ static int flowtable_evaluate(struct eval_ctx *ctx, struct flowtable *ft)if (ft->hooknum ==NF_INET_NUMHOOKS )return chain_error (ctx, ft,"invalid hook %s" , ft->hookstr); -if (!evaluate_priority (&ft->priority,NFPROTO_NETDEV , ft->hooknum)) +if (!evaluate_priority (ctx, &ft->priority,NFPROTO_NETDEV , ft->hooknum))return __stmt_binary_error (ctx, &ft->priority.loc,NULL , -"'%s' is invalid priority." , - ft->priority.str); +"invalid priority expression %s." , +expr_name (ft->priority.expr));if (!ft->dev_expr)return chain_error (ctx, ft,"Unbound flowtable not allowed (must specify devices)" );@@ -3422,11 +3443,11 @@ static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain)return chain_error (ctx, chain,"invalid hook %s" , chain->hookstr); -if (!evaluate_priority (&chain->priority, chain->handle.family, - chain->hooknum)) +if (!evaluate_priority (ctx, &chain->priority, + chain->handle.family, chain->hooknum))return __stmt_binary_error (ctx, &chain->priority.loc,NULL , -"'%s' is invalid priority in this context." , - chain->priority.str); +"invalid priority expression %s in this context." , +expr_name (chain->priority.expr)); }list_for_each_entry (rule, &chain->rules, list) {src/parser_bison.y +2
-2@@ -636,8 +636,8 @@ int nft_lex(void *, void *, void *); %type <stmt> meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc %destructor { stmt_free($$); } meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc -%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr -%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr +%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr +%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr %type <expr> primary_expr shift_expr and_expr %destructor { expr_free($$); } primary_expr shift_expr and_expr %type <expr> exclusive_or_expr inclusive_or_expr
src/parser_bison.y extended_prio_name : OUT · line 1974
Quoted 6 lines
src/parser_bison.y +3
-0@@ -1969,30 +1969,44 @@ extended_prio_name : OUT | STRING ; +prio_expr : variable_expr + { + datatype_set($1->sym->expr, &priority_type);
Can you use symbol_expr_alloc() here, both for variable_expr and extended_prio_name.
Then, from the evaluation phase you can turn this symbol expression into a constant using the priority_type_parse() function.
I think this will allow you to skip the num field in prio_spec.
Thanks!
Re: [PATCH 1/2 nft] src: allow variables in the chain priority specification
Hi Pablo, I have a few comments below.
Quoted 17 lines · Pablo Neira Ayuso
Quoted 2 lines · Fernando Fernandez Mancera
include/rule.h +0
-0[...]
Quoted 1 line
+const struct datatype
priority_type= {Please, add here something like on top of the definition:
/* This datatype is not registered via
datatype_register()
* since this datatype should not ever be used from either
* rules or elements.
*/or alike, so we don't forget that missing
datatype_register()is not a bug.[...]
Quoted 14 lines
include/rule.h enum chain_flags { · line 179
Quoted 11 lines
include/rule.h +2
-2@@ -174,13 +174,13 @@ enum chain_flags {* struct prio_spec - extendend priority specification for mixed * textual/numerical parsing. * -* @str: name of the standard priority value -* @num: Numerical value. This MUST contain the parsed value of str after +* @expr: expr of the standard priority value +* @num: Numerical value. This MUST contain the parsed value of expr after * evaluation. There must be a way to avoid this num field.
I have been thinking about that for a while. Right now it is needed because we are supporting the following priorities:
0 /* only numbers */
filter /* only string */
filter + 10 /* combination of a string plus a number */ filter - 10 /* combination of a string minus a number */
The proposed design will allow us to continue supporting that. Having said that, the only way to get rid of this num field is to parse the string in parsing time and then store the resulting number as a symbol expression in my opinion. Maybe I am missing something here. Have you any idea? Thanks!
Quoted 158 lines
Quoted 24 lines
include/rule.h +2
-2*/ struct prio_spec { -const char *str; -int num; +struct expr *expr; +int num;struct location loc; };src/datatype.c +7
-0@@ -1246,3 +1246,29 @@ const struct datatype boolean_type = { .sym_tbl = &boolean_tbl, .json = boolean_type_json, }; + +static struct error_record *priority_type_parse (const struct expr *sym, +struct expr **res) +{ +int num; + +if (isdigit (sym->identifier[0 ])) {src/datatype.c const struct datatype boolean_type = { · line 1265
Quoted 10 lines
src/datatype.c +10
-0+ num =atoi (sym->identifier); + *res =constant_expr_alloc (&sym->location, &integer_type, +BYTEORDER_HOST_ENDIAN , +sizeof (int ) *BITS_PER_BYTE , + &num); + }else + *res =constant_expr_alloc (&sym->location, &string_type, +BYTEORDER_HOST_ENDIAN , +strlen (sym->identifier) * +BITS_PER_BYTE , sym->identifier);I'd suggest you call
integer_datatype_parse()first, check if erec == NULL, then all good, this is an integer. Otherwise, release erec object and parse this as a string viastring_datatype_parse().Quoted 19 lines
Quoted 1 line
src/datatype.c +1
-0+return NULL ;src/datatype.c +8
-0+} + +const struct datatype priority_type = { + .type =TYPE_STRING , + .name ="priority" , + .desc ="priority type" , + .parse = priority_type_parse, +};src/evaluate.c +0
-0@@ -3193,15 +3193,36 @@ static int set_evaluate(struct eval_ctx *ctx, struct set *set)return 0 ; }src/evaluate.c static int set_evaluate(struct eval_ctx *ctx, struct set *set) · line 3203
Quoted 10 lines
src/evaluate.c +4
-2-static bool evaluate_priority (struct prio_spec *prio,int family,int hook) +static bool evaluate_priority (struct eval_ctx *ctx,struct prio_spec *prio, +int family,int hook) {int priority; +char prio_str[NFT_NAME_MAXLEN ];/* A numeric value has been used to specify priority. */ -if (prio->str ==NULL ) +if (prio->expr ==NULL )If we always use an expression, as described below, this won't be needed.
Quoted 9 lines
src/evaluate.c +6
-1return true; - priority =std_prio_lookup (prio->str, family, hook); + ctx->ectx.dtype = &priority_type; + ctx->ectx.len =NFT_NAME_MAXLEN *BITS_PER_BYTE ; +if (expr_evaluate (ctx, &prio->expr) <0 ) +return false; +if (prio->expr->etype !=EXPR_VALUE ) { + expr_error(ctx->msgs, prio->expr, "%s is not a valid "src/evaluate.c static int set_evaluate(struct eval_ctx *ctx, struct set *set) · line 3221
Quoted 10 lines
src/evaluate.c +10
-0+ "priority expression", expr_name(prio->expr)); +return false; + } + +if (prio->expr->dtype->type ==TYPE_INTEGER ) { +mpz_export_data (&prio->num, prio->expr->value, +BYTEORDER_HOST_ENDIAN ,sizeof (int )); +return true; + } + mpz_export_data(prio_str, prio->expr->value,If you use symbol expression, as I describe below. You don't need to convert this constant expression back to string again. You can just use the symbol identify
sym->identifier.Quoted 53 lines
src/evaluate.c +11
-7+BYTEORDER_HOST_ENDIAN , + NFT_NAME_MAXLEN); + + priority =std_prio_lookup (prio_str, family, hook);if (priority ==NF_IP_PRI_LAST )return false; prio->num += priority;@@ -3223,10 +3244,10 @@ static int flowtable_evaluate(struct eval_ctx *ctx, struct flowtable *ft)if (ft->hooknum ==NF_INET_NUMHOOKS )return chain_error (ctx, ft,"invalid hook %s" , ft->hookstr); -if (!evaluate_priority (&ft->priority,NFPROTO_NETDEV , ft->hooknum)) +if (!evaluate_priority (ctx, &ft->priority,NFPROTO_NETDEV , ft->hooknum))return __stmt_binary_error (ctx, &ft->priority.loc,NULL , -"'%s' is invalid priority." , - ft->priority.str); +"invalid priority expression %s." , +expr_name (ft->priority.expr));if (!ft->dev_expr)return chain_error (ctx, ft,"Unbound flowtable not allowed (must specify devices)" );@@ -3422,11 +3443,11 @@ static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain)return chain_error (ctx, chain,"invalid hook %s" , chain->hookstr); -if (!evaluate_priority (&chain->priority, chain->handle.family, - chain->hooknum)) +if (!evaluate_priority (ctx, &chain->priority, + chain->handle.family, chain->hooknum))return __stmt_binary_error (ctx, &chain->priority.loc,NULL , -"'%s' is invalid priority in this context." , - chain->priority.str); +"invalid priority expression %s in this context." , +expr_name (chain->priority.expr)); }list_for_each_entry (rule, &chain->rules, list) {src/parser_bison.y +2
-2@@ -636,8 +636,8 @@ int nft_lex(void *, void *, void *); %type <stmt> meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc %destructor { stmt_free($$); } meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc -%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr -%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr +%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr +%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr %type <expr> primary_expr shift_expr and_expr %destructor { expr_free($$); } primary_expr shift_expr and_expr %type <expr> exclusive_or_expr inclusive_or_exprsrc/parser_bison.y extended_prio_name : OUT · line 1974
Quoted 6 lines
src/parser_bison.y +3
-0@@ -1969,30 +1969,44 @@ extended_prio_name : OUT | STRING ; +prio_expr : variable_expr + { + datatype_set($1->sym->expr, &priority_type);Can you use
symbol_expr_alloc()here, both forvariable_exprandextended_prio_name.
Quoted 3 lines
Then, from the evaluation phase you can turn this symbol expression into a constant using the
priority_type_parse()function.Quoted 1 line
I think this will allow you to skip the num field in
prio_spec.
Do you mean to store "filter + 10" in the symbol and parse the string manually using priority_type_parse() function? Thanks and sorry if I am making a big mess out of this.
Quoted 2 lines
Thanks!
PATCH 2/2 src: allow variable in chain policy
In reply to Fernando Fernandez ManceraThis patch introduces the use of nft input files variables in chain policy. e.g.
define default_policy = "accept"
add table ip foo
add chain ip foo bar {type filter hook input priority filter; policy $default_policy}
table ip foo {
chain bar {
type filter hook input priority filter; policy accept;
}
}--- include/rule.h | 2 +- src/evaluate.c | 51 +++++++++++++++++++ src/json.c | 5 +- src/mnl.c | 9 ++-- src/netlink.c | 8 ++- src/parser_bison.y | 19 +++++-- src/parser_json.c | 17 +++++-- src/rule.c | 13 +++-- .../testcases/nft-f/0025policy_variable_0 | 17 +++++++ .../testcases/nft-f/0026policy_variable_0 | 17 +++++++ .../testcases/nft-f/0027policy_variable_1 | 18 +++++++ .../testcases/nft-f/0028policy_variable_1 | 18 +++++++ .../nft-f/dumps/0025policy_variable_0.nft | 5 ++ .../nft-f/dumps/0026policy_variable_0.nft | 5 ++ 14 files changed, 186 insertions(+), 18 deletions(-) create mode 100755 tests/shell/testcases/nft-f/0025policy_variable_0 create mode 100755 tests/shell/testcases/nft-f/0026policy_variable_0 create mode 100755 tests/shell/testcases/nft-f/0027policy_variable_1 create mode 100755 tests/shell/testcases/nft-f/0028policy_variable_1 create mode 100644 tests/shell/testcases/nft-f/dumps/0025policy_variable_0.nft create mode 100644 tests/shell/testcases/nft-f/dumps/0026policy_variable_0.nft
include/rule.h +1 -1
@@ -209,7 +209,7 @@ struct chain {const char *hookstr;unsigned int hooknum;struct prio_spec priority; -int policy; +struct expr *policy;const char *type;const char *dev;struct scope scope;
src/evaluate.c +51 -0
63 lines not on this page · raw message
src/json.c +4 -1
@@ -223,6 +223,7 @@ static json_t *rule_print_json(struct output_ctx *octx,static json_t *chain_print_json (const struct chain *chain) {json_t *root, *tmp; +int policy; root = json_pack("{s:s, s:s, s:s, s:I}", "family", family2str(chain->handle.family),
@@ -231,12 +232,14 @@ static json_t *chain_print_json(const struct chain *chain) "handle", chain->handle.handle.id);if (chain->flags &CHAIN_F_BASECHAIN ) { +mpz_export_data (&policy, chain->policy->value, +BYTEORDER_HOST_ENDIAN ,sizeof (int )); tmp =json_pack ("{s:s, s:s, s:i, s:s}" ,"type" , chain->type,"hook" ,hooknum2str (chain->handle.family, chain->hooknum),"prio" , chain->priority.num, -"policy" ,chain_policy2str (chain->policy)); +"policy" ,chain_policy2str (policy));if (chain->dev)json_object_set_new (tmp,"dev" ,json_string (chain->dev));json_object_update (root, tmp);
src/mnl.c +6 -3
@@ -518,6 +518,7 @@ int mnl_nft_chain_add(struct netlink_ctx *ctx, const struct cmd *cmd, {struct nftnl_chain *nlc;struct nlmsghdr *nlh; +int policy; nlc =nftnl_chain_alloc ();if (nlc ==NULL )
@@ -536,9 +537,11 @@ int mnl_nft_chain_add(struct netlink_ctx *ctx, const struct cmd *cmd,nftnl_chain_set_str (nlc,NFTNL_CHAIN_TYPE , cmd->chain->type); } -if (cmd->chain->policy !=-1 ) -nftnl_chain_set_u32 (nlc,NFTNL_CHAIN_POLICY , - cmd->chain->policy); +if (cmd->chain->policy) { +mpz_export_data (&policy, cmd->chain->policy->value, +BYTEORDER_HOST_ENDIAN ,sizeof (int )); +nftnl_chain_set_u32 (nlc,NFTNL_CHAIN_POLICY , policy); + }if (cmd->chain->dev !=NULL )nftnl_chain_set_str (nlc,NFTNL_CHAIN_DEV , cmd->chain->dev);
src/netlink.c +7 -1
@@ -369,6 +369,7 @@ struct chain *netlink_delinearize_chain(struct netlink_ctx *ctx, const struct nftnl_chain *nlc) {struct chain *chain; +int policy; chain =chain_alloc (nftnl_chain_get_str (nlc,NFTNL_CHAIN_NAME )); chain->handle.family =
@@ -390,7 +391,12 @@ struct chain *netlink_delinearize_chain(struct netlink_ctx *ctx,nftnl_chain_get_s32 (nlc,NFTNL_CHAIN_PRIO ); chain->type =xstrdup (nftnl_chain_get_str (nlc,NFTNL_CHAIN_TYPE )); - chain->policy = + policy =nftnl_chain_get_u32 (nlc,NFTNL_CHAIN_POLICY ); + chain->policy =constant_expr_alloc (&netlink_location, + &string_type, +BYTEORDER_HOST_ENDIAN , +sizeof (int ) *BITS_PER_BYTE , + &policy);nftnl_chain_get_u32 (nlc,NFTNL_CHAIN_POLICY );if (nftnl_chain_is_set (nlc,NFTNL_CHAIN_DEV )) { chain->dev =
src/parser_bison.y +15 -4
@@ -636,8 +636,8 @@ int nft_lex(void *, void *, void *); %type <stmt> meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc %destructor { stmt_free($$); } meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc -%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr -%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr +%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr policy_expr +%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr policy_expr %type <expr> primary_expr shift_expr and_expr %destructor { expr_free($$); } primary_expr shift_expr and_expr %type <expr> exclusive_or_expr inclusive_or_expr
@@ -2019,17 +2019,28 @@ dev_spec : DEVICE string { $$ = $2; } | /* empty */ { $$ = NULL; } ; -policy_spec : POLICY chain_policy +policy_spec : POLICY policy_expr { - if ($<chain>0->policy != -1) { + if ($<chain>0->policy) { erec_queue(error(&@$, "you cannot set chain policy twice"), state->msgs); + expr_free($2); YYERROR; } $<chain>0->policy = $2; } ; +policy_expr : variable_expr + | chain_policy + { + $$ = constant_expr_alloc(&@$, &integer_type, + BYTEORDER_HOST_ENDIAN, + sizeof(int) * + BITS_PER_BYTE, &$1); + } + ; + chain_policy : ACCEPT { $$ = NF_ACCEPT; } | DROP { $$ = NF_DROP; } ;
src/parser_json.c +12 -5
@@ -2530,13 +2530,20 @@ static struct cmd *json_parse_cmd_add_table(struct json_ctx *ctx, json_t *root,return cmd_alloc (op, obj, &h, int_loc,NULL ); } -static int parse_policy (const char *policy) +static struct expr *parse_policy (const char *policy) { +int policy_num; +if (!strcmp (policy,"accept" )) -return NF_ACCEPT ; -if (!strcmp (policy,"drop" )) -return NF_DROP ; -return -1 ; + policy_num =NF_ACCEPT ; +else if (!strcmp (policy,"drop" )) + policy_num =NF_DROP ; +else +return NULL ; + +return constant_expr_alloc (int_loc, &integer_type, +BYTEORDER_HOST_ENDIAN ,sizeof (int ) + *BITS_PER_BYTE , &policy_num); } static struct cmd *json_parse_cmd_add_chain(struct json_ctx *ctx, json_t *root,
src/rule.c +10 -3
@@ -800,7 +800,7 @@ struct chain *chain_alloc(const char *name)if (name !=NULL ) chain->handle.chain.name =xstrdup (name); - chain->policy =-1 ; + chain->policy =NULL ;return chain; }
@@ -824,6 +824,7 @@ void chain_free(struct chain *chain)if (chain->dev !=NULL )xfree (chain->dev);expr_free (chain->priority.expr); +expr_free (chain->policy);xfree (chain); }
@@ -1099,12 +1100,15 @@ static void chain_print_declaration(const struct chain *chain, struct output_ctx *octx) {char priobuf[STD_PRIO_BUFSIZE ]; +int policy;nft_print (octx,"\tchain %s {" , chain->handle.chain.name);if (nft_output_handle (octx))nft_print (octx," # handle %" PRIu64, chain->handle.handle.id);nft_print (octx,"\n" );if (chain->flags &CHAIN_F_BASECHAIN ) { +mpz_export_data (&policy, chain->policy->value, +BYTEORDER_HOST_ENDIAN ,sizeof (int ));nft_print (octx,"\t\ttype %s hook %s" , chain->type,hooknum2str (chain->handle.family, chain->hooknum));if (chain->dev !=NULL )
@@ -1113,7 +1117,7 @@ static void chain_print_declaration(const struct chain *chain,prio2str (octx, priobuf,sizeof (priobuf), chain->handle.family, chain->hooknum, chain->priority.num), - chain_policy2str(chain->policy)); + chain_policy2str(policy)); } }
@@ -1134,17 +1138,20 @@ static void chain_print(const struct chain *chain, struct output_ctx *octx)void chain_print_plain (const struct chain *chain,struct output_ctx *octx) {char priobuf[STD_PRIO_BUFSIZE ]; +int policy;nft_print (octx,"chain %s %s %s" ,family2str (chain->handle.family), chain->handle.table.name, chain->handle.chain.name);if (chain->flags &CHAIN_F_BASECHAIN ) { +mpz_export_data (&policy, chain->policy->value, +BYTEORDER_HOST_ENDIAN ,4 );nft_print (octx," { type %s hook %s priority %s; policy %s; }" , chain->type, chain->hookstr,prio2str (octx, priobuf,sizeof (priobuf), chain->handle.family, chain->hooknum, chain->priority.num), -chain_policy2str (chain->policy)); +chain_policy2str (policy)); }if (nft_output_handle (octx))nft_print (octx," # handle %" PRIu64, chain->handle.handle.id);
tests/shell/testcases/nft-f/0025policy_variable_0 new file +17 -0
@@ -0,0 +1,17 @@ +#!/bin/bash + +# Tests use of variables in chain policy + +set -e + +RULESET=" +define default_policy = \"accept\" + +table inet global { + chain prerouting { + type filter hook prerouting priority filter + policy \$default_policy + } +}" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/nft-f/0026policy_variable_0 new file +17 -0
@@ -0,0 +1,17 @@ +#!/bin/bash + +# Tests use of variables in chain policy + +set -e + +RULESET=" +define default_policy = \"drop\" + +table inet global { + chain prerouting { + type filter hook prerouting priority filter + policy \$default_policy + } +}" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/nft-f/0027policy_variable_1 new file +18 -0
@@ -0,0 +1,18 @@ +#!/bin/bash + +# Tests use of variables in chain policy + +set -e + +RULESET=" +define default_policy = { 127.0.0.1 } + +table inet global { + chain prerouting { + type filter hook prerouting priority filter + policy \$default_policy + } +}" + +$NFT -f - <<< "$RULESET" && exit 1 +exit 0
tests/shell/testcases/nft-f/0028policy_variable_1 new file +18 -0
@@ -0,0 +1,18 @@ +#!/bin/bash + +# Tests use of variables in priority specification + +set -e + +RULESET=" +define default_policy = * + +table inet global { + chain prerouting { + type filter hook prerouting priority filter + policy \$default_policy + } +}" + +$NFT -f - <<< "$RULESET" && exit 1 +exit 0
tests/shell/testcases/nft-f/dumps/0025policy_variable_0.nft new file +5 -0
@@ -0,0 +1,5 @@ +table inet global { + chain prerouting { + type filter hook prerouting priority filter; policy accept; + } +}
tests/shell/testcases/nft-f/dumps/0026policy_variable_0.nft new file +5 -0
@@ -0,0 +1,5 @@ +table inet global { + chain prerouting { + type filter hook prerouting priority filter; policy drop; + } +}
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerRe: [PATCH 2/2 nft] src: allow variable in chain policy
Quoted 56 lines · Fernando Fernandez Mancera
This patch introduces the use of nft input files variables in chain policy. e.g.
define
default_policy= "accept"add table ip foo
add chain ip foo bar {type filter hook input priority filter; policy $default_policy}table ip foo { chain bar { type filter hook input priority filter; policy accept; } }Signed-off-by Fernando Fernandez Mancera <[email protected]>--- include/rule.h | 2 +- src/evaluate.c | 51 +++++++++++++++++++ src/json.c | 5 +- src/mnl.c | 9 ++-- src/netlink.c | 8 ++- src/parser_bison.y | 19 +++++-- src/parser_json.c | 17 +++++-- src/rule.c | 13 +++-- .../testcases/nft-f/0025policy_variable_0 | 17 +++++++ .../testcases/nft-f/0026policy_variable_0 | 17 +++++++ .../testcases/nft-f/0027policy_variable_1 | 18 +++++++ .../testcases/nft-f/0028policy_variable_1 | 18 +++++++ .../nft-f/dumps/0025policy_variable_0.nft | 5 ++ .../nft-f/dumps/0026policy_variable_0.nft | 5 ++ 14 files changed, 186 insertions(+), 18 deletions(-) create mode 100755 tests/shell/testcases/nft-f/0025policy_variable_0 create mode 100755 tests/shell/testcases/nft-f/0026policy_variable_0 create mode 100755 tests/shell/testcases/nft-f/0027policy_variable_1 create mode 100755 tests/shell/testcases/nft-f/0028policy_variable_1 create mode 100644 tests/shell/testcases/nft-f/dumps/0025policy_variable_0.nft create mode 100644 tests/shell/testcases/nft-f/dumps/0026policy_variable_0.nftinclude/rule.h +1
-1@@ -209,7 +209,7 @@ struct chain {const char *hookstr;unsigned int hooknum;struct prio_spec priority; -int policy; +struct expr *policy;const char *type;const char *dev;struct scope scope;src/evaluate.c +0
-0
src/evaluate.c static uint32_t str2hooknum(uint32_t family, const char *hook) · line 3418
Quoted 4 lines
src/evaluate.c +1
-0@@ -3415,6 +3415,52 @@ static uint32_t str2hooknum(uint32_t family, const char *hook)return NF_INET_NUMHOOKS ; } +static bool evaluate_policy (struct eval_ctx *ctx,struct expr **policy)
better rename static bool ...(..., struct expr **exprp)
src/evaluate.c static uint32_t str2hooknum(uint32_t family, const char *hook) · line 3422
Quoted 4 lines
src/evaluate.c +4
-0+{ +char policy_str[NFT_NAME_MAXLEN ]; +struct location loc; +int policy_num;
so you can use 'int policy;'.
src/evaluate.c static uint32_t str2hooknum(uint32_t family, const char *hook) · line 3426
Quoted 3 lines
src/evaluate.c +3
-0+ ctx->ectx.len =NFT_NAME_MAXLEN *BITS_PER_BYTE ; +if (expr_evaluate (ctx, policy) <0 ) +return false;
probably cache this here:
expr = *exprp;
so you don't need:
(*expr)->...
in all this code below.
Quoted 17 lines
src/evaluate.c +17
-0+if ((*policy)->etype !=EXPR_VALUE ) { +expr_error (ctx->msgs, *policy,"%s is not a valid " +"policy expression" ,expr_name (*policy)); +return false; + } + +if ((*policy)->dtype->type ==TYPE_STRING ) { +mpz_export_data (policy_str, (*policy)->value, +BYTEORDER_HOST_ENDIAN , +NFT_NAME_MAXLEN ); + loc = (*policy)->location; +if (!strcmp (policy_str,"accept" )) { +expr_free (*policy); + policy_num =NF_ACCEPT ; + *policy = constant_expr_alloc(&loc, + &integer_type, + BYTEORDER_HOST_ENDIAN,
src/evaluate.c static uint32_t str2hooknum(uint32_t family, const char *hook) · line 3453
Quoted 10 lines
src/evaluate.c +10
-0+sizeof (int ) *BITS_PER_BYTE , + &policy_num); + }else if (!strcmp (policy_str,"drop" )) { +expr_free (*policy); + policy_num =NF_DROP ; + *policy =constant_expr_alloc (&(*policy)->location, + &integer_type, +BYTEORDER_HOST_ENDIAN , +sizeof (int ) *BITS_PER_BYTE , + &policy_num);
Probably:
if (!strcmp(policy_str, "accept")) {
policy = NF_ACCEPT;
else (!strmp(policy_str, "drop")) {
policy = NF_DROP;
} else {
...
}
expr = constant_expr_alloc(...);so this code becomes shorter and easier to read.
And I think you should do all this from the new policy_datype, not from the evaluation phase itself.
Thanks.
Re: [PATCH 2/2 nft] src: allow variable in chain policy
Hi Pablo,
Quoted 133 lines · Pablo Neira Ayuso
Quoted 1 line · Fernando Fernandez Mancera
This patch introduces the use of nft input files variables in chain
policy.
Quoted 6 lines
e.g.
define
default_policy= "accept"add table ip foo
add chain ip foo bar {type filter hook input priority filter; policy$default_policy}
Quoted 11 lines
table ip foo { chain bar { type filter hook input priority filter; policy accept; } }Signed-off-by Fernando Fernandez Mancera <[email protected]>--- include/rule.h | 2 +- src/evaluate.c | 51+++++++++++++++++++
Quoted 10 lines
src/json.c | 5 +- src/mnl.c | 9 ++-- src/netlink.c | 8 ++- src/parser_bison.y | 19 +++++-- src/parser_json.c | 17 +++++-- src/rule.c | 13 +++-- .../testcases/nft-f/0025policy_variable_0 | 17 +++++++ .../testcases/nft-f/0026policy_variable_0 | 17 +++++++ .../testcases/nft-f/0027policy_variable_1 | 18 +++++++ .../testcases/nft-f/0028policy_variable_1 | 18 +++++++Quoted 8 lines
.../nft-f/dumps/0025policy_variable_0.nft | 5 ++ .../nft-f/dumps/0026policy_variable_0.nft | 5 ++ 14 files changed, 186 insertions(+), 18 deletions(-) create mode 100755 tests/shell/testcases/nft-f/0025policy_variable_0 create mode 100755 tests/shell/testcases/nft-f/0026policy_variable_0 create mode 100755 tests/shell/testcases/nft-f/0027policy_variable_1 create mode 100755 tests/shell/testcases/nft-f/0028policy_variable_1 create mode 100644tests/shell/testcases/nft-f/dumps/0025policy_variable_0.nft
Quoted 1 line
create mode 100644tests/shell/testcases/nft-f/dumps/0026policy_variable_0.nft
src/evaluate.c static uint32_t str2hooknum(uint32_t family,
Quoted 19 lines
include/rule.h +1
-1@@ -209,7 +209,7 @@ struct chain {const char *hookstr;unsigned int hooknum;struct prio_spec priority; -int policy; +struct expr *policy;const char *type;const char *dev;struct scope scope;src/evaluate.c +0
-0@@ -3415,6 +3415,52 @@ static uint32_t str2hooknum(uint32_t family,const char *hook)
src/evaluate.c static uint32_t str2hooknum(uint32_t family, const char *hook)
Quoted 4 lines
src/evaluate.c +1
-0return NF_INET_NUMHOOKS ; } +static bool evaluate_policy(struct eval_ctx *ctx, struct expr**policy)
better rename static bool ...(..., struct expr **exprp)
src/evaluate.c static uint32_t str2hooknum(uint32_t family, const char *hook) · line 3422
Quoted 4 lines
src/evaluate.c +4
-0+{ +char policy_str[NFT_NAME_MAXLEN ]; +struct location loc; +int policy_num;so you can use 'int policy;'.
src/evaluate.c static uint32_t str2hooknum(uint32_t family, const char *hook) · line 3426
Quoted 3 lines
src/evaluate.c +3
-0+ ctx->ectx.len =NFT_NAME_MAXLEN *BITS_PER_BYTE ; +if (expr_evaluate (ctx, policy) <0 ) +return false;probably cache this here:
expr = *exprp;so you don't need:
(*expr)->...in all this code below.
Quoted 17 lines
src/evaluate.c +17
-0+if ((*policy)->etype !=EXPR_VALUE ) { +expr_error (ctx->msgs, *policy,"%s is not a valid " +"policy expression" ,expr_name (*policy)); +return false; + } + +if ((*policy)->dtype->type ==TYPE_STRING ) { +mpz_export_data (policy_str, (*policy)->value, +BYTEORDER_HOST_ENDIAN , +NFT_NAME_MAXLEN ); + loc = (*policy)->location; +if (!strcmp (policy_str,"accept" )) { +expr_free (*policy); + policy_num =NF_ACCEPT ; + *policy = constant_expr_alloc(&loc, + &integer_type, + BYTEORDER_HOST_ENDIAN,src/evaluate.c static uint32_t str2hooknum(uint32_t family, const char *hook) · line 3453
Quoted 10 lines
src/evaluate.c +10
-0+sizeof (int ) *BITS_PER_BYTE , + &policy_num); + }else if (!strcmp (policy_str,"drop" )) { +expr_free (*policy); + policy_num =NF_DROP ; + *policy =constant_expr_alloc (&(*policy)->location, + &integer_type, +BYTEORDER_HOST_ENDIAN , +sizeof (int ) *BITS_PER_BYTE , + &policy_num);Probably:
if (!strcmp(policy_str, "accept")) { policy = NF_ACCEPT; else (!strmp(policy_str, "drop")) { policy = NF_DROP; } else { ... } expr = constant_expr_alloc(...);
Quoted 6 lines
so this code becomes shorter and easier to read.
And I think you should do all this from the new
policy_datype, not from the evaluation phase itself.Thanks.
I agree with everything except this part. There is no policy datatype. I could create it if you prefer it. Thanks! :-)