diff options
Diffstat (limited to 'conf')
-rw-r--r-- | conf/Makefile | 10 | ||||
-rw-r--r-- | conf/cf-lex.l | 201 | ||||
-rw-r--r-- | conf/conf.c | 3 | ||||
-rw-r--r-- | conf/conf.h | 59 | ||||
-rw-r--r-- | conf/confbase.Y | 49 |
5 files changed, 211 insertions, 111 deletions
diff --git a/conf/Makefile b/conf/Makefile index 5c10bbcb..19db9c2c 100644 --- a/conf/Makefile +++ b/conf/Makefile @@ -10,12 +10,12 @@ BISON_DEBUG=-t #FLEX_DEBUG=-d endif -$(conf-y-targets): $(s)confbase.Y $(s)flowspec.Y - $(M4) $(M4FLAGS) -P $| $^ >$@ +$(o)cf-parse.y: $(s)gen_parser.m4 +$(o)keywords.h: $(s)gen_keywords.m4 +$(o)commands.h: $(s)gen_commands.m4 -$(o)cf-parse.y: | $(s)gen_parser.m4 -$(o)keywords.h: | $(s)gen_keywords.m4 -$(o)commands.h: | $(s)gen_commands.m4 $(srcdir)/client/cmds.m4 +$(conf-y-targets): $(s)confbase.Y $(s)flowspec.Y + $(M4) $(M4FLAGS) -P $(if $(word 2,$(filter %.m4,$^)),$(error "Too many M4 scripts for one target"),$(filter %.m4,$^)) $(filter %.Y,$^) >$@ $(o)cf-parse.tab.h: $(o)cf-parse.tab.c diff --git a/conf/cf-lex.l b/conf/cf-lex.l index 9bbb3660..4f69993e 100644 --- a/conf/cf-lex.l +++ b/conf/cf-lex.l @@ -45,6 +45,7 @@ #include "nest/route.h" #include "nest/protocol.h" #include "filter/filter.h" +#include "filter/f-inst.h" #include "conf/conf.h" #include "conf/cf-parse.tab.h" #include "lib/string.h" @@ -64,7 +65,7 @@ struct keyword { #endif -static uint cf_hash(byte *c); +static uint cf_hash(const byte *c); #define KW_KEY(n) n->name #define KW_NEXT(n) n->next @@ -95,6 +96,15 @@ int (*cf_read_hook)(byte *buf, unsigned int max, int fd); struct include_file_stack *ifs; static struct include_file_stack *ifs_head; +#define QUOTED_BUFFER_SIZE 4096 +static BUFFER_(char) quoted_buffer; +static char quoted_buffer_data[QUOTED_BUFFER_SIZE]; +static inline void quoted_buffer_init(void) { + quoted_buffer.used = 0; + quoted_buffer.size = QUOTED_BUFFER_SIZE; + quoted_buffer.data = quoted_buffer_data; +} + #define MAX_INCLUDE_DEPTH 8 #define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->fd); @@ -105,6 +115,8 @@ static struct include_file_stack *ifs_head; static void cf_include(char *arg, int alen); static int check_eof(void); +static enum yytokentype cf_lex_symbol(const char *data); + %} %option noyywrap @@ -112,24 +124,26 @@ static int check_eof(void); %option nounput %option noreject -%x COMMENT CCOMM CLI +%x COMMENT CCOMM CLI QUOTED APOSTROPHED INCLUDE ALPHA [a-zA-Z_] DIGIT [0-9] XIGIT [0-9a-fA-F] ALNUM [a-zA-Z_0-9] WHITE [ \t] -include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*; %% -{include} { - char *start, *end; - +^{WHITE}*include{WHITE}*\" { if (!ifs->depth) cf_error("Include not allowed in CLI"); - start = strchr(yytext, '"'); - start++; + BEGIN(INCLUDE); +} + +<INCLUDE>[^"\n]+["]{WHITE}*; { + char *start, *end; + + start = yytext; end = strchr(start, '"'); *end = 0; @@ -138,15 +152,23 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*; cf_error("Include with empty argument"); cf_include(start, end-start); + + BEGIN(INITIAL); } +<INCLUDE>["] cf_error("Include with empty argument"); +<INCLUDE>. cf_error("Unterminated include"); +<INCLUDE>\n cf_error("Unterminated include"); +<INCLUDE><<EOF>> cf_error("Unterminated include"); + + {DIGIT}+:{DIGIT}+ { uint len1 UNUSED, len2; u64 l; char *e; errno = 0; - l = strtoul(yytext, &e, 10); + l = bstrtoul(yytext, &e, 10); if (e && (*e != ':') || (errno == ERANGE) || (l >> 32)) cf_error("ASN out of range"); @@ -164,7 +186,7 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*; } errno = 0; - l = strtoul(e+1, &e, 10); + l = bstrtoul(e+1, &e, 10); if (e && *e || (errno == ERANGE) || (l >> len2)) cf_error("Number out of range"); cf_lval.i64 |= l; @@ -191,13 +213,13 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*; } errno = 0; - l = strtoul(yytext+2, &e, 10); + l = bstrtoul(yytext+2, &e, 10); if (e && (*e != ':') || (errno == ERANGE) || (l >> len1)) cf_error("ASN out of range"); cf_lval.i64 |= ((u64) l) << len2; errno = 0; - l = strtoul(e+1, &e, 10); + l = bstrtoul(e+1, &e, 10); if (e && *e || (errno == ERANGE) || (l >> len2)) cf_error("Number out of range"); cf_lval.i64 |= l; @@ -219,7 +241,7 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*; cf_lval.i64 |= ((u64) ip4_to_u32(ip4)) << 16; errno = 0; - l = strtoul(e, &e, 10); + l = bstrtoul(e, &e, 10); if (e && *e || (errno == ERANGE) || (l >> 16)) cf_error("Number out of range"); cf_lval.i64 |= l; @@ -243,7 +265,7 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*; char *e; unsigned long int l; errno = 0; - l = strtoul(yytext+2, &e, 16); + l = bstrtoul(yytext+2, &e, 16); if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l) cf_error("Number out of range"); cf_lval.i = l; @@ -254,7 +276,7 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*; char *e; unsigned long int l; errno = 0; - l = strtoul(yytext, &e, 10); + l = bstrtoul(yytext, &e, 10); if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l) cf_error("Number out of range"); cf_lval.i = l; @@ -266,26 +288,23 @@ else: { return ELSECOL; } -({ALPHA}{ALNUM}*|[']({ALNUM}|[-]|[\.]|[:])*[']) { - if(*yytext == '\'') { - yytext[yyleng-1] = 0; - yytext++; - } +['] { + BEGIN(APOSTROPHED); + quoted_buffer_init(); +} - struct keyword *k = HASH_FIND(kw_hash, KW, yytext); - if (k) - { - if (k->value > 0) - return k->value; - else - { - cf_lval.i = -k->value; - return ENUM; - } - } +<APOSTROPHED>{ALNUM}|[-]|[.:] BUFFER_PUSH(quoted_buffer) = yytext[0]; +<APOSTROPHED>\n cf_error("Unterminated symbol"); +<APOSTROPHED><<EOF>> cf_error("Unterminated symbol"); +<APOSTROPHED>['] { + BEGIN(INITIAL); + BUFFER_PUSH(quoted_buffer) = 0; + return cf_lex_symbol(quoted_buffer_data); +} +<APOSTROPHED>. cf_error("Invalid character in apostrophed symbol"); - cf_lval.s = cf_get_symbol(yytext); - return SYM; +({ALPHA}{ALNUM}*) { + return cf_lex_symbol(yytext); } <CLI>(.|\n) { @@ -301,14 +320,21 @@ else: { return yytext[0]; } -["][^"\n]*["] { - yytext[yyleng-1] = 0; - cf_lval.t = cfg_strdup(yytext+1); - yytext[yyleng-1] = '"'; +["] { + BEGIN(QUOTED); + quoted_buffer_init(); +} + +<QUOTED>\n cf_error("Unterminated string"); +<QUOTED><<EOF>> cf_error("Unterminated string"); +<QUOTED>["] { + BEGIN(INITIAL); + BUFFER_PUSH(quoted_buffer) = 0; + cf_lval.t = cfg_strdup(quoted_buffer_data); return TEXT; } -["][^"\n]*\n cf_error("Unterminated string"); +<QUOTED>. BUFFER_PUSH(quoted_buffer) = yytext[0]; <INITIAL,COMMENT><<EOF>> { if (check_eof()) return END; } @@ -349,7 +375,7 @@ else: { %% static uint -cf_hash(byte *c) +cf_hash(const byte *c) { uint h = 13 << 24; @@ -358,7 +384,6 @@ cf_hash(byte *c) return h; } - /* * IFS stack - it contains structures needed for recursive processing * of include in config files. On the top of the stack is a structure @@ -519,7 +544,7 @@ check_eof(void) } static struct symbol * -cf_new_symbol(byte *c) +cf_new_symbol(const byte *c) { struct symbol *s; @@ -527,11 +552,8 @@ cf_new_symbol(byte *c) if (l > SYM_MAX_LEN) cf_error("Symbol too long"); - s = cfg_alloc(sizeof(struct symbol) + l); - s->scope = conf_this_scope; - s->class = SYM_VOID; - s->def = NULL; - s->aux = 0; + s = cfg_allocz(sizeof(struct symbol) + l + 1); + *s = (struct symbol) { .scope = conf_this_scope, .class = SYM_VOID, }; strcpy(s->name, c); if (!new_config->sym_hash.data) @@ -539,6 +561,8 @@ cf_new_symbol(byte *c) HASH_INSERT2(new_config->sym_hash, SYM, new_config->pool, s); + add_tail(&(new_config->symbols), &(s->n)); + return s; } @@ -554,7 +578,7 @@ cf_new_symbol(byte *c) * signify no match. */ struct symbol * -cf_find_symbol(struct config *cfg, byte *c) +cf_find_symbol(const struct config *cfg, const byte *c) { struct symbol *s; @@ -562,6 +586,7 @@ cf_find_symbol(struct config *cfg, byte *c) (s = HASH_FIND(cfg->sym_hash, SYM, c, 1))) return s; + /* In CLI command parsing, fallback points to the current config, otherwise it is NULL. */ if (cfg->fallback && cfg->fallback->sym_hash.data && (s = HASH_FIND(cfg->fallback->sym_hash, SYM, c, 1))) @@ -580,11 +605,33 @@ cf_find_symbol(struct config *cfg, byte *c) * existing symbol is found. */ struct symbol * -cf_get_symbol(byte *c) +cf_get_symbol(const byte *c) { return cf_find_symbol(new_config, c) ?: cf_new_symbol(c); } +/** + * cf_localize_symbol - get the local instance of given symbol + * @sym: the symbol to localize + * + * This functions finds the symbol that is local to current scope + * for purposes of cf_define_symbol(). + */ +struct symbol * +cf_localize_symbol(struct symbol *sym) +{ + /* If the symbol type is void, it has been recently allocated just in this scope. */ + if (!sym->class) + return sym; + + /* If the scope is the current, it is already defined in this scope. */ + if (sym->scope == conf_this_scope) + cf_error("Symbol already defined"); + + /* Not allocated here yet, doing it now. */ + return cf_new_symbol(sym->name); +} + struct symbol * cf_default_name(char *template, int *counter) { @@ -604,33 +651,34 @@ cf_default_name(char *template, int *counter) cf_error("Unable to generate default name"); } -/** - * cf_define_symbol - define meaning of a symbol - * @sym: symbol to be defined - * @type: symbol class to assign - * @def: class dependent data - * - * Defines new meaning of a symbol. If the symbol is an undefined - * one (%SYM_VOID), it's just re-defined to the new type. If it's defined - * in different scope, a new symbol in current scope is created and the - * meaning is assigned to it. If it's already defined in the current scope, - * an error is reported via cf_error(). - * - * Result: Pointer to the newly defined symbol. If we are in the top-level - * scope, it's the same @sym as passed to the function. - */ -struct symbol * -cf_define_symbol(struct symbol *sym, int type, void *def) +static enum yytokentype +cf_lex_symbol(const char *data) { - if (sym->class) + struct keyword *k = HASH_FIND(kw_hash, KW, data); + if (k) + { + if (k->value > 0) + return k->value; + else { - if (sym->scope == conf_this_scope) - cf_error("Symbol already defined"); - sym = cf_new_symbol(sym->name); + cf_lval.i = -k->value; + return ENUM; } - sym->class = type; - sym->def = def; - return sym; + } + + cf_lval.s = cf_get_symbol(data); + switch (cf_lval.s->class) { + case SYM_VOID: return CF_SYM_VOID; + case SYM_PROTO: return CF_SYM_PROTO; + case SYM_TEMPLATE: return CF_SYM_TEMPLATE; + case SYM_FUNCTION: return CF_SYM_FUNCTION; + case SYM_FILTER: return CF_SYM_FILTER; + case SYM_TABLE: return CF_SYM_TABLE; + case SYM_ATTRIBUTE: return CF_SYM_ATTRIBUTE; + case SYM_VARIABLE_RANGE: return CF_SYM_VARIABLE; + case SYM_CONSTANT_RANGE: return CF_SYM_CONSTANT; + default: bug("Unknown symbol class %d", cf_lval.s->class); + } } static void @@ -723,9 +771,6 @@ cf_pop_scope(void) char * cf_symbol_class_name(struct symbol *sym) { - if (cf_symbol_is_constant(sym)) - return "constant"; - switch (sym->class) { case SYM_VOID: @@ -740,6 +785,12 @@ cf_symbol_class_name(struct symbol *sym) return "filter"; case SYM_TABLE: return "routing table"; + case SYM_ATTRIBUTE: + return "custom attribute"; + case SYM_CONSTANT_RANGE: + return "constant"; + case SYM_VARIABLE_RANGE: + return "variable"; default: return "unknown type"; } diff --git a/conf/conf.c b/conf/conf.c index 439aa41d..b0980d7e 100644 --- a/conf/conf.c +++ b/conf/conf.c @@ -98,6 +98,7 @@ config_alloc(const char *name) memcpy(ndup, name, nlen); init_list(&c->tests); + init_list(&c->symbols); c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */ c->pool = p; c->mem = l; @@ -258,6 +259,8 @@ config_do_commit(struct config *c, int type) if (old_config) old_config->obstacle_count++; + DBG("filter_commit\n"); + filter_commit(c, old_config); DBG("sysdep_commit\n"); int force_restart = sysdep_commit(c, old_config); DBG("global_commit\n"); diff --git a/conf/conf.h b/conf/conf.h index 427569fd..f14c0e21 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -24,6 +24,7 @@ struct config { list tables; /* Configured routing tables (struct rtable_config) */ list logfiles; /* Configured log files (sysdep) */ list tests; /* Configured unit tests (f_bt_test_suite) */ + list symbols; /* Configured symbols in config order */ int mrtdump_file; /* Configured MRTDump file (sysdep, fd in unix) */ char *syslog_name; /* Name used for syslog (NULL -> no syslog) */ @@ -103,13 +104,22 @@ void cfg_copy_list(list *dest, list *src, unsigned node_size); extern int (*cf_read_hook)(byte *buf, uint max, int fd); struct symbol { + node n; /* In list of symbols in config */ struct symbol *next; struct sym_scope *scope; - int class; - int aux; - void *aux2; - void *def; - char name[1]; + int class; /* SYM_* */ + uint flags; /* SYM_FLAG_* */ + + union { + struct proto_config *proto; /* For SYM_PROTO and SYM_TEMPLATE */ + const struct f_line *function; /* For SYM_FUNCTION */ + const struct filter *filter; /* For SYM_FILTER */ + struct rtable_config *table; /* For SYM_TABLE */ + struct f_dynamic_attr *attribute; /* For SYM_ATTRIBUTE */ + struct f_val *val; /* For SYM_CONSTANT or SYM_VARIABLE */ + }; + + char name[0]; }; struct sym_scope { @@ -134,8 +144,11 @@ struct sym_scope { #define SYM_CONSTANT 0x200 /* 0x200-0x2ff are variable types */ #define SYM_CONSTANT_RANGE SYM_CONSTANT ... (SYM_CONSTANT | 0xff) -#define SYM_TYPE(s) (((struct f_val *) (s)->def)->type) -#define SYM_VAL(s) (((struct f_val *) (s)->def)->val) +#define SYM_TYPE(s) ((s)->val->type) +#define SYM_VAL(s) ((s)->val->val) + +/* Symbol flags */ +#define SYM_FLAG_SAME 0x1 /* For SYM_FUNCTION and SYM_FILTER */ struct include_file_stack { void *buffer; /* Internal lexer state */ @@ -156,19 +169,37 @@ int cf_lex(void); void cf_lex_init(int is_cli, struct config *c); void cf_lex_unwind(void); -struct symbol *cf_find_symbol(struct config *cfg, byte *c); +struct symbol *cf_find_symbol(const struct config *cfg, const byte *c); -struct symbol *cf_get_symbol(byte *c); +struct symbol *cf_get_symbol(const byte *c); struct symbol *cf_default_name(char *template, int *counter); -struct symbol *cf_define_symbol(struct symbol *symbol, int type, void *def); +struct symbol *cf_localize_symbol(struct symbol *sym); + +/** + * cf_define_symbol - define meaning of a symbol + * @sym: symbol to be defined + * @type: symbol class to assign + * @def: class dependent data + * + * Defines new meaning of a symbol. If the symbol is an undefined + * one (%SYM_VOID), it's just re-defined to the new type. If it's defined + * in different scope, a new symbol in current scope is created and the + * meaning is assigned to it. If it's already defined in the current scope, + * an error is reported via cf_error(). + * + * Result: Pointer to the newly defined symbol. If we are in the top-level + * scope, it's the same @sym as passed to the function. + */ +#define cf_define_symbol(sym_, type_, var_, def_) ({ \ + struct symbol *sym = cf_localize_symbol(sym_); \ + sym->class = type_; \ + sym->var_ = def_; \ + sym; }) + void cf_push_scope(struct symbol *); void cf_pop_scope(void); char *cf_symbol_class_name(struct symbol *sym); -static inline int cf_symbol_is_constant(struct symbol *sym) -{ return (sym->class & 0xff00) == SYM_CONSTANT; } - - /* Parser */ extern char *cf_text; diff --git a/conf/confbase.Y b/conf/confbase.Y index 3fd034db..dcc92365 100644 --- a/conf/confbase.Y +++ b/conf/confbase.Y @@ -49,13 +49,17 @@ CF_DECLS struct rtable_config *r; struct channel_config *cc; struct f_inst *x; + struct f_inst *xp[2]; + enum filter_return fret; + enum ec_subtype ecs; struct f_dynamic_attr fda; struct f_static_attr fsa; - struct filter *f; + struct f_lval flv; + const struct f_line *fl; + const struct filter *f; struct f_tree *e; struct f_trie *trie; struct f_val v; - struct f_path_mask *h; struct password_item *p; struct rt_show_data *ra; struct sym_show_data *sd; @@ -78,7 +82,7 @@ CF_DECLS %token <ip4> IP4 %token <ip6> IP6 %token <i64> VPN_RD -%token <s> SYM +%token <s> CF_SYM_VOID CF_SYM_PROTO CF_SYM_TEMPLATE CF_SYM_FUNCTION CF_SYM_FILTER CF_SYM_TABLE CF_SYM_ATTRIBUTE CF_SYM_VARIABLE CF_SYM_CONSTANT %token <t> TEXT %type <iface> ipa_scope @@ -90,6 +94,7 @@ CF_DECLS %type <mls> label_stack_start label_stack %type <t> text opttext +%type <s> symbol %nonassoc PREFIX_DUMMY %left AND OR @@ -122,29 +127,39 @@ conf: ';' ; conf: definition ; definition: - DEFINE SYM '=' term ';' { + DEFINE CF_SYM_VOID '=' term ';' { struct f_val *val = cfg_alloc(sizeof(struct f_val)); - *val = f_eval($4, cfg_mem); - if (val->type == T_RETURN) cf_error("Runtime error"); - cf_define_symbol($2, SYM_CONSTANT | val->type, val); + if (f_eval(f_postfixify($4), cfg_mem, val) > F_RETURN) cf_error("Runtime error"); + cf_define_symbol($2, SYM_CONSTANT | val->type, val, val); } ; expr: NUM - | '(' term ')' { $$ = f_eval_int($2); } - | SYM { + | '(' term ')' { $$ = f_eval_int(f_postfixify($2)); } + | CF_SYM_CONSTANT { if ($1->class != (SYM_CONSTANT | T_INT)) cf_error("Number expected"); $$ = SYM_VAL($1).i; } ; - expr_us: expr S { $$ = $1 S_; } | expr MS { $$ = $1 MS_; } | expr US { $$ = $1 US_; } ; +symbol: + CF_SYM_VOID + | CF_SYM_PROTO + | CF_SYM_TEMPLATE + | CF_SYM_FUNCTION + | CF_SYM_FILTER + | CF_SYM_TABLE + | CF_SYM_ATTRIBUTE + | CF_SYM_VARIABLE + | CF_SYM_CONSTANT + ; + /* Switches */ bool: @@ -162,7 +177,7 @@ bool: ipa: IP4 { $$ = ipa_from_ip4($1); } | IP6 { $$ = ipa_from_ip6($1); } - | SYM { + | CF_SYM_CONSTANT { if ($1->class != (SYM_CONSTANT | T_IP)) cf_error("IP address expected"); $$ = SYM_VAL($1).ip; } @@ -170,7 +185,7 @@ ipa: ipa_scope: /* empty */ { $$ = NULL; } - | '%' SYM { $$ = if_get_by_name($2->name); } + | '%' symbol { $$ = if_get_by_name($2->name); } ; @@ -277,7 +292,7 @@ net_: net_ip6: net_ip6_ - | SYM { + | CF_SYM_CONSTANT { if (($1->class != (SYM_CONSTANT | T_NET)) || (SYM_VAL($1).net->type != NET_IP6)) cf_error("IPv6 network expected"); $$ = * SYM_VAL($1).net; @@ -286,7 +301,7 @@ net_ip6: net_ip: net_ip_ - | SYM { + | CF_SYM_CONSTANT { if (($1->class != (SYM_CONSTANT | T_NET)) || !net_is_ip(SYM_VAL($1).net)) cf_error("IP network expected"); $$ = * SYM_VAL($1).net; @@ -295,7 +310,7 @@ net_ip: net_any: net_ - | SYM { + | CF_SYM_CONSTANT { if ($1->class != (SYM_CONSTANT | T_NET)) cf_error("Network expected"); $$ = (net_addr *) SYM_VAL($1).net; /* Avoid const warning */ @@ -307,7 +322,7 @@ net_or_ipa: | net_ip6_ | IP4 { net_fill_ip4(&($$), $1, IP4_MAX_PREFIX_LENGTH); } | IP6 { net_fill_ip6(&($$), $1, IP6_MAX_PREFIX_LENGTH); } - | SYM { + | CF_SYM_CONSTANT { if ($1->class == (SYM_CONSTANT | T_IP)) net_fill_ip_host(&($$), SYM_VAL($1).ip); else if (($1->class == (SYM_CONSTANT | T_NET)) && net_is_ip(SYM_VAL($1).net)) @@ -344,7 +359,7 @@ time: text: TEXT - | SYM { + | CF_SYM_CONSTANT { if ($1->class != (SYM_CONSTANT | T_STRING)) cf_error("String expected"); $$ = SYM_VAL($1).s; } |