-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.y
320 lines (251 loc) · 6.95 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
%{
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "log.h"
#include "trace.h"
extern int yylineno;
extern int yylex();
extern FILE *yyin;
static int _retval;
static rule_t *_rules;
static const char *_filename;
static rule_t *_current_rule;
static hop_t *_current_hops;
static stack_t *_pending_stack;
static stack_t *_prev_stack;
static size_t _current_hop_index;
#define _current_hop (_current_hops[_current_hop_index])
static const uint32_t CIDR_MASK_MAP[33] = {
0x00000000, 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000, 0xf8000000,
0xfc000000, 0xfe000000, 0xff000000, 0xff800000, 0xffc00000, 0xffe00000,
0xfff00000, 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000, 0xffff8000,
0xffffc000, 0xffffe000, 0xfffff000, 0xfffff800, 0xfffffc00, 0xfffffe00,
0xffffff00, 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0, 0xfffffff8,
0xfffffffc, 0xfffffffe, 0xffffffff
};
static void yyerror(const char *s);
static int new_rule();
static int new_hop();
static int save_stack();
static int set_rule_from(uint32_t ip, uint32_t mask);
static int set_rule_to(uint32_t ip, uint32_t mask);
%}
%locations
%define parse.error verbose
%union {
uint32_t u32;
}
%token SLASH
%token LBRACE
%token RBRACE
%token SEMICOLON
%token RULE
%token FROM
%token TO
%token RANDOM_IP
%token RANDOM_UINT
%token LPAREN
%token RPAREN
%token COMMA
%token HOP
%token SRC
%token DST
%token LABEL
%token EXP
%token S
%token TTL
%token DEFAULT
%token <u32> IP
%token <u32> NUMBER
%%
rule_list: rule | rule_list rule
rule: RULE selectors LBRACE hop_list RBRACE
selectors
: FROM IP SLASH NUMBER {
new_rule();
if (set_rule_from($2, $4) < 0) {
YYERROR;
}
}
| TO IP SLASH NUMBER {
new_rule();
if (set_rule_to($2, $4) < 0) {
YYERROR;
}
}
| FROM IP SLASH NUMBER TO IP SLASH NUMBER {
new_rule();
if (set_rule_from($2, $4) < 0) {
YYERROR;
}
if (set_rule_to($6, $8) < 0) {
YYERROR;
}
}
| DEFAULT {
new_rule();
}
hop_list: hop | hop_list hop
hop: HOP hop_src hop_def
hop_src
: IP {
if (new_hop() < 0) {
YYERROR;
}
_current_hop.type = HOP_TTPE_LITERAL;
_current_hop.address = $1;
}
| RANDOM_IP LPAREN IP COMMA IP RPAREN {
if (new_hop() < 0) {
YYERROR;
}
_current_hop.type = HOP_TYPE_RANDOM;
_current_hop.address_rand_min = $3;
_current_hop.address_rand_max = $5;
}
| SRC {
if (new_hop() < 0) {
YYERROR;
}
_current_hop.type = HOP_TYPE_SRC;
}
| DST {
if (new_hop() < 0) {
YYERROR;
}
_current_hop.type = HOP_TYPE_DST;
}
hop_def: LBRACE label_list RBRACE | SEMICOLON
label_list: label | label_list label
label
: LABEL NUMBER LBRACE label_options RBRACE {
_pending_stack->label_type = VAL_TYPE_LITERAL;
_pending_stack->label = $2;
save_stack();
}
| LABEL RANDOM_UINT LPAREN NUMBER COMMA NUMBER RPAREN LBRACE label_options RBRACE {
_pending_stack->label_type = VAL_TYPE_RANDOM;
_pending_stack->label_rand_min = $4;
_pending_stack->label_rand_max = $6;
save_stack();
}
label_options: label_option | label_options label_option
label_option
: TTL NUMBER SEMICOLON {
_pending_stack->ttl_type = VAL_TYPE_LITERAL;
_pending_stack->ttl = $2;
}
| EXP NUMBER SEMICOLON {
_pending_stack->exp_type = VAL_TYPE_LITERAL;
_pending_stack->exp = $2;
}
| S NUMBER SEMICOLON {
_pending_stack->s_type = VAL_TYPE_LITERAL;
_pending_stack->s = $2;
}
| TTL RANDOM_UINT LPAREN NUMBER COMMA NUMBER RPAREN SEMICOLON {
_pending_stack->ttl_type = VAL_TYPE_RANDOM;
_pending_stack->ttl_rand_min = $4;
_pending_stack->ttl_rand_max = $6;
}
| EXP RANDOM_UINT LPAREN NUMBER COMMA NUMBER RPAREN SEMICOLON {
_pending_stack->exp_type = VAL_TYPE_RANDOM;
_pending_stack->exp_rand_min = $4;
_pending_stack->exp_rand_max = $6;
}
| S RANDOM_UINT LPAREN NUMBER COMMA NUMBER RPAREN SEMICOLON {
_pending_stack->s_type = VAL_TYPE_RANDOM;
_pending_stack->s_rand_min = $4;
_pending_stack->s_rand_max = $6;
}
%%
int parse_rules(const char *filename, rule_t **rules) {
_filename = filename;
_rules = NULL;
_retval = 0;
FILE *f = fopen(filename, "r");
if (!f) {
log_fatal("failed to open config file %s", filename);
return -1;
}
yyin = f;
_pending_stack = (stack_t *) malloc(sizeof(stack_t));
memset(_pending_stack, 0, sizeof(stack_t));
_current_rule = NULL;
_current_hops = NULL;
_prev_stack = NULL;
_current_hop_index = 1000; // magic value for "no hops defined"
yyparse();
fclose(f);
free(_pending_stack);
*rules = _rules;
return _retval;
}
void yyerror(const char *s) {
log_fatal("%s:%d - %s\n", _filename, yylineno, s);
_retval = -1;
}
int new_rule() {
_current_hop_index = 1000; // magic value for "no hops defined"
rule_t *prev_rule = _current_rule;
_current_rule = (rule_t *) malloc(sizeof(rule_t));
memset(_current_rule, 0, sizeof(rule_t));
if (_rules == NULL) {
_rules = _current_rule;
}
if (prev_rule != NULL) {
prev_rule->next = _current_rule;
}
return 0;
}
int new_hop() {
_prev_stack = NULL;
if (_current_hop_index == 1000) {
_current_hops = calloc(255, sizeof(hop_t));
_current_rule->hops = _current_hops;
_current_hop_index = 0;
} else {
++_current_hop_index;
}
_current_rule->nhops = _current_hop_index + 1;
if (_current_hop_index > 255) {
log_fatal("too many hops defined (255 max)");
return -1;
}
return 0;
}
int save_stack() {
stack_t *new_stack = (stack_t *) malloc(sizeof(stack_t));
memcpy(new_stack, _pending_stack, sizeof(stack_t));
memset(_pending_stack, 0, sizeof(stack_t));
if (_prev_stack != NULL) {
_prev_stack->next = new_stack;
_prev_stack = new_stack;
} else {
_current_hop.stack = new_stack;
_prev_stack = new_stack;
}
return 0;
}
int set_rule_from(uint32_t ip, uint32_t mask) {
if (mask > 32) {
log_error("invalid mask: '/%d'\n", mask);
yyerror("invalid value for mask, aborting.");
return -1;
}
_current_rule->from = ip;
_current_rule->from_mask = CIDR_MASK_MAP[mask];
return 0;
}
int set_rule_to(uint32_t ip, uint32_t mask) {
if (mask > 32) {
log_error("invalid mask: '/%d'\n", mask);
yyerror("invalid value for mask, aborting.");
return -1;
}
_current_rule->to = ip;
_current_rule->to_mask = CIDR_MASK_MAP[mask];
return 0;
}