-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.y
605 lines (504 loc) · 15.5 KB
/
config.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
%{
#include <stdint.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include "config.h"
#include "config-internal.h"
#include "log.h"
#include "prefix-list.h"
#include "condition.h"
extern int yylineno;
extern int yylex();
extern FILE *yyin;
static const char *_filename;
void yyerror(const char *s);
#define ERR_IF_NULL(x) if ((x) == NULL) { \
store_retval(-1);\
log_error("internal error while parsing config file.\n");\
YYERROR;\
}
%}
%locations
%define parse.error verbose
%union {
double d;
uint64_t u64;
struct in_addr in_addr;
struct in6_addr in6_addr;
char *str;
apermon_config_listens *listen;
apermon_config_agents *agent;
apermon_config_agent_addresses *agent_address;
apermon_config_interfaces *interface;
apermon_config_ifindexes *ifindex;
apermon_config_prefix_lists *prefix_list;
apermon_config_prefix_list_elements *prefix_list_element;
apermon_config_actions *action;
apermon_config_action_scripts *action_script;
apermon_config_triggers *trigger;
apermon_config_prefix_lists_set *prefix_lists_set;
apermon_cond_func_list *cond_func_list_element;
apermon_config_action_set *action_set;
}
%token OPTIONS LISTEN MIN_BAN_TIME BURST_PERIOD STATUS_FILE DUMP_INTERVAL
%token LBRACE RBRACE SEMICOLON LBRACK RBRACK
%token SFLOW V5
%token AGENTS ADDRESSES SAMPLE_RATE_CAP
%token INTERFACES IFINDEXES DOT
%token PREFIXES SLASH
%token ACTIONS SCRIPT EVENTS BAN UNBAN ENV EQUALS
%token TRIGGERS NETWORKS DIRECTIONS INGRESS EGRESS AGGREGATE_TYPE HOST PREFIX NET
%token THRESHOLDS BPS PPS K M G
%token FILTER AND OR NOT SOURCE DESTINATION IN_INTERFACE OUT_INTERFACE PROTOCOL TCP UDP SOURCE_PORT DESTINATION_PORT IS_FRAGMENT
%token <u64> NUMBER
%token <d> DOUBLE
%token <str> IDENT QUOTED_STRING
%token <in_addr> IP
%token <in6_addr> IP6
%type <listen> listen_args option_item_listens option_item_listen
%type <agent> agent agent_list
%type <agent_address> agent_address agent_addresses
%type <interface> iface_options iface iface_list
%type <ifindex> iface_index iface_indexes
%type <prefix_list> prefix_list prefixes
%type <prefix_list_element> prefix prefix_elements
%type <action> action action_list
%type <action_script> action_script action_scripts
%type <str> string
%type <trigger> trigger trigger_list
%type <prefix_lists_set> prefix_lists_set prefix_lists_set_element
%type <cond_func_list_element> filter filter_list
%type <u64> proto_name number_with_unit unit
%type <action_set> action_set action_set_element
%%
config: config_item | config config_item
config_item
: OPTIONS LBRACE options RBRACE
| AGENTS LBRACE agent_list RBRACE {
get_config()->agents = $3;
}
| INTERFACES LBRACE iface_list RBRACE {
get_config()->interfaces = $3;
}
| PREFIXES LBRACE prefix_list RBRACE {
get_config()->prefix_lists = $3;
}
| ACTIONS LBRACE action_list RBRACE {
get_config()->actions = $3;
}
| TRIGGERS LBRACE trigger_list RBRACE {
get_config()->triggers = $3;
}
trigger_list
: trigger
| trigger trigger_list {
$1->next = $2;
}
trigger: IDENT LBRACE trigger_options RBRACE {
$$ = end_trigger($1);
free($1);
}
trigger_options: trigger_option | trigger_options trigger_option
trigger_option
: NETWORKS LBRACK prefix_lists_set RBRACK SEMICOLON {
get_current_trigger()->networks = $3;
}
| MIN_BAN_TIME NUMBER SEMICOLON {
get_current_trigger()->flags |= APERMON_TRIGGER_SET_BAN_TIME;
get_current_trigger()->min_ban_time = $2;
}
| BURST_PERIOD NUMBER SEMICOLON {
get_current_trigger()->flags |= APERMON_TRIGGER_SET_BURST_PERIOD;
get_current_trigger()->burst_period = $2;
}
| DIRECTIONS LBRACK direction_list RBRACK SEMICOLON
| AGGREGATE_TYPE HOST SEMICOLON {
get_current_trigger()->aggregator = APERMON_AGGREGATOR_HOST;
}
| AGGREGATE_TYPE PREFIX SEMICOLON {
get_current_trigger()->aggregator = APERMON_AGGREGATOR_PREFIX;
}
| AGGREGATE_TYPE NET SEMICOLON {
get_current_trigger()->aggregator = APERMON_AGGREGATOR_NET;
}
| THRESHOLDS LBRACE threshold_list RBRACE
| FILTER LBRACE filter_list RBRACE {
apermon_cond_list *l = (apermon_cond_list *) malloc(sizeof(apermon_cond_list));
memset(l, 0, sizeof(apermon_cond_list));
l->funcs = $3;
l->type = APERMON_COND_AND;
get_current_trigger()->conds = l;
}
| ACTIONS LBRACK action_set RBRACK SEMICOLON {
get_current_trigger()->actions = $3;
}
action_set
: action_set_element
| action_set_element action_set {
$1->next = $2;
}
action_set_element: IDENT {
apermon_config_actions *action = get_action($1);
if (action == NULL) {
store_retval(-1);
log_error("unknown action '%s'\n", $1);
YYERROR;
}
$$ = malloc(sizeof(apermon_config_action_set));
memset($$, 0, sizeof(apermon_config_action_set));
$$->action = action;
free($1);
}
prefix_lists_set
: prefix_lists_set_element
| prefix_lists_set_element prefix_lists_set {
$1->next = $2;
}
prefix_lists_set_element: IDENT {
void *pfxlist = get_prefix_list($1);
if (pfxlist == NULL) {
store_retval(-1);
log_error("prefix list '%s' not defined.\n", $1);
YYERROR;
}
$$ = malloc(sizeof(apermon_config_prefix_lists_set));
memset($$, 0, sizeof(apermon_config_prefix_lists_set));
$$->prefix_list = pfxlist;
free($1);
}
direction_list: direction | direction_list direction
direction
: INGRESS {
get_current_trigger()->flags |= APERMON_TRIGGER_CHECK_INGRESS;
}
| EGRESS {
get_current_trigger()->flags |= APERMON_TRIGGER_CHECK_EGRESS;
}
threshold_list: threshold | threshold_list threshold
unit
: { $$ = 1; }
| K { $$ = 1 << 10; }
| M { $$ = 1 << 20; }
| G { $$ = 1 << 30; }
number_with_unit
: NUMBER unit {
$$ = $1 * $2;
}
| DOUBLE unit {
$$ = (uint64_t) ($1 * (double) $2);
}
threshold
: BPS number_with_unit SEMICOLON {
get_current_trigger()->bps = $2;
}
| PPS number_with_unit SEMICOLON {
get_current_trigger()->pps = $2;
}
filter_list
: filter
| filter filter_list {
$1->next = $2;
}
proto_name
: TCP { $$ = IPPROTO_TCP; }
| UDP { $$ = IPPROTO_UDP; }
| NUMBER { $$ = $1; }
filter
: AND LBRACE filter_list RBRACE {
apermon_cond_list *l = (apermon_cond_list *) malloc(sizeof(apermon_cond_list));
memset(l, 0, sizeof(apermon_cond_list));
l->type = APERMON_COND_AND;
l->funcs = $3;
$$ = new_cond_func_list_element(cond_list, l);
}
| OR LBRACE filter_list RBRACE {
apermon_cond_list *l = (apermon_cond_list *) malloc(sizeof(apermon_cond_list));
memset(l, 0, sizeof(apermon_cond_list));
l->type = APERMON_COND_OR;
l->funcs = $3;
$$ = new_cond_func_list_element(cond_list, l);
}
| NOT LBRACE filter_list RBRACE {
apermon_cond_list *l = (apermon_cond_list *) malloc(sizeof(apermon_cond_list));
memset(l, 0, sizeof(apermon_cond_list));
l->type = APERMON_COND_NOT;
l->funcs = $3;
$$ = new_cond_func_list_element(cond_list, l);
}
| SOURCE IDENT SEMICOLON {
apermon_config_prefix_lists *pfxlist = get_prefix_list($2);
apermon_config_prefix_list_elements **arg = malloc(sizeof(void *));
if (pfxlist == NULL) {
store_retval(-1);
log_error("prefix list '%s' not defined.\n", $2);
YYERROR;
}
*arg = pfxlist->elements;
$$ = new_cond_func_list_element(cond_src, arg);
free($2);
}
| DESTINATION IDENT SEMICOLON {
apermon_config_prefix_lists *pfxlist = get_prefix_list($2);
apermon_config_prefix_list_elements **arg = malloc(sizeof(void *));
if (pfxlist == NULL) {
store_retval(-1);
log_error("prefix list '%s' not defined.\n", $2);
YYERROR;
}
*arg = pfxlist->elements;
$$ = new_cond_func_list_element(cond_dst, arg);
free($2);
}
| IN_INTERFACE IDENT SEMICOLON {
apermon_config_interfaces **arg = malloc(sizeof(void *));
*arg = get_interface($2);
if (*arg == NULL) {
store_retval(-1);
log_error("interface '%s' not defined.\n", $2);
YYERROR;
}
$$ = new_cond_func_list_element(cond_in_interface, arg);
free($2);
}
| OUT_INTERFACE IDENT SEMICOLON {
apermon_config_interfaces **arg = malloc(sizeof(void *));
*arg = get_interface($2);
if (*arg == NULL) {
store_retval(-1);
log_error("interface '%s' not defined.\n", $2);
YYERROR;
}
$$ = new_cond_func_list_element(cond_out_interface, arg);
free($2);
}
| PROTOCOL proto_name SEMICOLON {
uint8_t *arg = malloc(sizeof(uint8_t));
*arg = $2;
$$ = new_cond_func_list_element(cond_proto, arg);
}
| SOURCE_PORT NUMBER SEMICOLON {
uint16_t *arg = malloc(sizeof(uint16_t));
*arg = $2;
$$ = new_cond_func_list_element(cond_src_port, arg);
}
| DESTINATION_PORT NUMBER SEMICOLON {
uint16_t *arg = malloc(sizeof(uint16_t));
*arg = $2;
$$ = new_cond_func_list_element(cond_dst_port, arg);
}
| IS_FRAGMENT SEMICOLON {
$$ = new_cond_func_list_element(cond_is_fragment, NULL);
}
action_list
: action
| action action_list {
$1->next = $2;
}
action: IDENT LBRACE action_options RBRACE {
$$ = end_action($1);
free($1);
}
action_options: action_option | action_options action_option
action_option
: action_scripts {
get_current_action()->scripts = $1;
}
action_scripts
: action_script
| action_script action_scripts {
$1->next = $2;
}
action_script
: SCRIPT QUOTED_STRING LBRACE script_options RBRACE {
$$ = end_action_script($2);
free($2);
}
script_options: script_option | script_options script_option
script_option
: EVENTS LBRACK script_events RBRACK SEMICOLON
| ENV LBRACE env_list RBRACE
env_list: env | env_list env
env: string EQUALS string SEMICOLON {
size_t len = strlen($1) + strlen($3) + 2; // +2 for '=' and '\0'
char *env = (char *) malloc(len);
memset(env, 0, len);
snprintf(env, len, "%s=%s", $1, $3);
get_current_action_script()->envs[get_current_action_script()->n_envs++] = env;
log_debug("env: %s\n", env);
free($1);
free($3);
}
string: QUOTED_STRING | IDENT
script_events: script_event | script_events script_event
script_event
: BAN {
get_current_action_script()->flags |= APERMON_SCRIPT_EVENT_BAN;
}
| UNBAN {
get_current_action_script()->flags |= APERMON_SCRIPT_EVENT_UNBAN;
}
prefix_list
: prefixes
| prefixes prefix_list {
$1->next = $2;
}
prefixes: IDENT LBRACE prefix_elements RBRACE {
$$ = (apermon_config_prefix_lists *) malloc(sizeof(apermon_config_prefix_lists));
memset($$, 0, sizeof(apermon_config_prefix_lists));
$$->name = strdup($1);
$$->elements = $3;
free($1);
}
prefix_elements
: prefix
| prefix prefix_elements {
$1->next = $2;
}
prefix
: IP SLASH NUMBER SEMICOLON {
ERR_IF_NULL($$ = new_prefix_inet(&$1, $3));
}
| IP6 SLASH NUMBER SEMICOLON {
ERR_IF_NULL($$ = new_prefix_inet6(&$1, $3));
}
iface_list
: iface
| iface iface_list {
$1->next = $2;
}
iface: IDENT LBRACE iface_options RBRACE {
$$ = end_interface($1);
free($1);
}
iface_options
: IFINDEXES LBRACK iface_indexes RBRACK SEMICOLON {
get_current_interface()->ifindexes = $3;
}
iface_indexes
: iface_index
| iface_index iface_indexes {
$1->next = $2;
}
iface_index: IDENT DOT NUMBER {
$$ = (apermon_config_ifindexes *) malloc(sizeof(apermon_config_ifindexes));
memset($$, 0, sizeof(apermon_config_ifindexes));
const apermon_config_agents *agent = get_agent($1);
if (agent == NULL) {
store_retval(-1);
log_error("agent '%s' not defined.\n", $1);
YYERROR;
}
$$->agent = agent;
$$->ifindex = $3;
free($1);
}
agent_list
: agent
| agent agent_list {
$1->next = $2;
}
agent: IDENT LBRACE agent_options RBRACE {
$$ = end_agent($1);
free($1);
}
agent_options: agent_option | agent_options agent_option
agent_option
: ADDRESSES LBRACK agent_addresses RBRACK SEMICOLON {
get_current_agent()->addresses = $3;
}
| SAMPLE_RATE_CAP NUMBER SEMICOLON {
get_current_agent()->sample_rate_cap = $2;
}
agent_addresses
: agent_address
| agent_address agent_addresses {
$1->next = $2;
}
agent_address
: IP {
$$ = (apermon_config_agent_addresses *) malloc(sizeof(apermon_config_agent_addresses));
memset($$, 0, sizeof(apermon_config_agent_addresses));
$$->af = AF_INET;
$$->inet.s_addr = $1.s_addr;
}
| IP6 {
$$ = (apermon_config_agent_addresses *) malloc(sizeof(apermon_config_agent_addresses));
memset($$, 0, sizeof(apermon_config_agent_addresses));
$$->af = AF_INET6;
memcpy(&$$->inet6, &$1, sizeof($$->inet6));
}
options: option_items | option_items options
option_items
: option_item_listens {
get_config()->listens = $1;
}
| MIN_BAN_TIME NUMBER SEMICOLON {
get_config()->min_ban_time = $2;
}
| BURST_PERIOD NUMBER SEMICOLON {
get_config()->burst_period = $2;
}
| STATUS_FILE QUOTED_STRING DUMP_INTERVAL NUMBER SEMICOLON {
get_config()->status_file = strdup($2);
get_config()->status_dump_interval = $4;
free($2);
}
option_item_listens
: option_item_listen
| option_item_listen option_item_listens {
$1->next = $2;
}
option_item_listen
: LISTEN IDENT NUMBER listen_args SEMICOLON {
ERR_IF_NULL($$ = listen_fill_gai($4, $2, $3));
free($2);
}
| LISTEN QUOTED_STRING NUMBER listen_args SEMICOLON {
ERR_IF_NULL($$ = listen_fill_gai($4, $2, $3));
free($2);
}
| LISTEN IP NUMBER listen_args SEMICOLON {
char addr[INET_ADDRSTRLEN + 1];
memset(addr, 0, sizeof(addr));
if (inet_ntop(AF_INET, &$2, addr, INET_ADDRSTRLEN) == NULL) {
log_fatal("inet_ntop(): %s\n", strerror(errno));
YYERROR;
}
ERR_IF_NULL($$ = listen_fill_gai($4, addr, $3));
}
| LISTEN IP6 NUMBER listen_args SEMICOLON {
char addr[INET6_ADDRSTRLEN + 1];
memset(addr, 0, sizeof(addr));
if (inet_ntop(AF_INET6, &$2, addr, INET6_ADDRSTRLEN) == NULL) {
log_fatal("inet_ntop(): %s\n", strerror(errno));
YYERROR;
}
ERR_IF_NULL($$ = listen_fill_gai($4, addr, $3));
}
listen_args
: SFLOW V5 {
$$ = (apermon_config_listens *) malloc(sizeof(apermon_config_listens));
memset($$, 0, sizeof(apermon_config_listens));
$$->proto = APERMON_LISTEN_SFLOW_V5;
}
%%
int parse_config(const char *filename, apermon_config **config) {
_filename = filename;
FILE *f = fopen(filename, "r");
if (!f) {
log_fatal("failed to open config file %s", filename);
return -1;
}
start_config();
yyin = f;
yyparse();
fclose(f);
end_config();
*config = get_config();
return get_retval();
}
void yyerror(const char *s) {
log_fatal("%s:%d - %s\n", _filename, yylineno, s);
store_retval(-1);
}