From f314b4500848d90836cb3d936333bba5a0357ed7 Mon Sep 17 00:00:00 2001 From: Tomasz Bursztyka Date: Wed, 4 Sep 2013 12:50:20 +0300 Subject: src: Better error reporting if chain type is invalid This patch verifies at command line parsing that given chain type is valid. Possibilities are: filter, nat, and route. nft add chain test test { type cheese hook input priority 0 }; :1:28-33: Error: unknown chain type cheese add chain test test { type cheese hook input priority 0 }; ^^^^^^ Signed-off-by: Tomasz Bursztyka Signed-off-by: Pablo Neira Ayuso --- include/rule.h | 1 + src/parser.y | 16 +++++++++++++--- src/rule.c | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/include/rule.h b/include/rule.h index 1de23dce..10cfebde 100644 --- a/include/rule.h +++ b/include/rule.h @@ -117,6 +117,7 @@ struct chain { struct list_head rules; }; +extern const char *chain_type_name_lookup(const char *name); extern const char *chain_hookname_lookup(const char *name); extern struct chain *chain_alloc(const char *name); extern void chain_free(struct chain *chain); diff --git a/src/parser.y b/src/parser.y index a7dfdcc4..074f0758 100644 --- a/src/parser.y +++ b/src/parser.y @@ -769,10 +769,15 @@ map_block : /* empty */ { $$ = $-1; } hook_spec : TYPE STRING HOOK STRING PRIORITY NUM { - $0->type = $2; + $0->type = chain_type_name_lookup($2); + if ($0->type == NULL) { + erec_queue(error(&@2, "unknown chain type %s", $2), + state->msgs); + YYERROR; + } $0->hookstr = chain_hookname_lookup($4); if ($0->hookstr == NULL) { - erec_queue(error(&@4, "unknown hook name %s", $4), + erec_queue(error(&@4, "unknown chain type %s", $4), state->msgs); YYERROR; } @@ -781,7 +786,12 @@ hook_spec : TYPE STRING HOOK STRING PRIORITY NUM } | TYPE STRING HOOK STRING PRIORITY DASH NUM { - $0->type = $2; + $0->type = chain_type_name_lookup($2); + if ($0->type == NULL) { + erec_queue(error(&@2, "unknown type name %s", $2), + state->msgs); + YYERROR; + } $0->hookstr = chain_hookname_lookup($4); if ($0->hookstr == NULL) { erec_queue(error(&@4, "unknown hook name %s", $4), diff --git a/src/rule.c b/src/rule.c index 42ba37f4..2cf024a4 100644 --- a/src/rule.c +++ b/src/rule.c @@ -190,6 +190,25 @@ struct symbol *symbol_lookup(const struct scope *scope, const char *identifier) return NULL; } +static const char *chain_type_str_array[] = { + "filter", + "nat", + "route", + NULL, +}; + +const char *chain_type_name_lookup(const char *name) +{ + int i; + + for (i = 0; chain_type_str_array[i]; i++) { + if (!strcmp(name, chain_type_str_array[i])) + return chain_type_str_array[i]; + } + + return NULL; +} + static const char *chain_hookname_str_array[] = { "prerouting", "input", -- cgit v1.2.3