Statistics
| Branch: | Revision:

iof-bird-daemon / filter / config.Y @ 26c09e1d

History | View | Annotate | Download (15 KB)

1
/*
2
 *	BIRD - filters
3
 *
4
 *	Copyright 1998--2000 Pavel Machek
5
 *
6
 *	Can be freely distributed and used under the terms of the GNU GPL.
7
 *
8
 */
9

    
10
CF_HDR
11

    
12
CF_DEFINES
13

    
14
#define P(a,b) ((a<<8) | b)
15

    
16
CF_DECLS
17

    
18
CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN,
19
	ACCEPT, REJECT, ERROR, QUITBIRD,
20
	INT, BOOL, IP, PREFIX, PAIR, SET, STRING, BGPMASK, BGPPATH, CLIST,
21
	IF, THEN, ELSE, CASE,
22
	TRUE, FALSE,
23
	FROM, GW, NET, MASK, SOURCE, SCOPE, CAST, DEST,
24
	LEN,
25
	DEFINED,
26
	ADD, DELETE, CONTAINS, RESET,
27
	PREPEND, MATCH,
28
	EMPTY,
29
	FILTER, WHERE, EVAL)
30

    
31
%nonassoc THEN
32
%nonassoc ELSE
33

    
34
%type <x> term block cmds cmd function_body constant print_one print_list var_list var_listn dynamic_attr static_attr function_call
35
%type <f> filter filter_body where_filter
36
%type <i> type break_command pair
37
%type <e> set_item set_items switch_body
38
%type <v> set_atom fprefix fprefix_s fipa
39
%type <s> decls declsn one_decl function_params 
40
%type <h> bgp_path
41
%type <i> bgp_one
42

    
43
CF_GRAMMAR
44

    
45
CF_ADDTO(conf, filter_def)
46
filter_def:
47
   FILTER SYM { cf_push_scope( $2 ); } filter_body {
48
     cf_define_symbol($2, SYM_FILTER, $4);
49
     $4->name = $2->name;
50
     DBG( "We have new filter defined (%s)\n", $2->name );
51
     cf_pop_scope();
52
   }
53
 ;
54

    
55
CF_ADDTO(conf, filter_eval)
56
filter_eval:
57
   EVAL term { f_eval_int($2); }
58
 ;
59

    
60
type:
61
   INT { $$ = T_INT; }
62
 | BOOL { $$ = T_BOOL; }
63
 | IP { $$ = T_IP; }
64
 | PREFIX { $$ = T_PREFIX; }
65
 | PAIR { $$ = T_PAIR; }
66
 | STRING { $$ = T_STRING; }
67
 | BGPMASK { $$ = T_PATH_MASK; }
68
 | BGPPATH { $$ = T_PATH; }
69
 | CLIST { $$ = T_CLIST; }
70
 | type SET { 
71
	switch ($1) {
72
	  default:
73
		cf_error( "You can not create sets of this type" );
74
	  case T_INT: case T_IP: case T_PREFIX: case T_PAIR:
75
	}
76
	$$ = $1 | T_SET;
77
	}
78
 ;
79

    
80
one_decl:
81
   type SYM {
82
     cf_define_symbol($2, SYM_VARIABLE | $1, NULL);
83
     DBG( "New variable %s type %x\n", $2->name, $1 );
84
     $2->aux = 0;
85
     {
86
       struct f_val * val; 
87
       val = cfg_alloc(sizeof(struct f_val)); 
88
       val->type = $1; 
89
       $2->aux2 = val;
90
     }
91
     $$=$2;
92
   }
93
 ;
94

    
95
/* Decls with ';' at the end */
96
decls: /* EMPTY */ { $$ = NULL; }
97
 | one_decl ';' decls {
98
     $$ = $1;
99
     $$->aux = (int) $3;
100
   }
101
 ;
102

    
103
/* Declarations that have no ';' at the end. */
104
declsn: one_decl { $$ = $1; }
105
 | declsn ';' one_decl {
106
     $$ = $1;
107
     $$->aux = (int) $3;
108
   }
109
 ;
110

    
111
filter_body:
112
   function_body {
113
     struct filter *f = cfg_alloc(sizeof(struct filter));
114
     f->name = NULL;
115
     f->root = $1;
116
     $$ = f;
117
   }
118
 ;
119

    
120
filter:
121
   SYM {
122
     if ($1->class != SYM_FILTER) cf_error("No such filter");
123
     $$ = $1->def;
124
   }
125
 | filter_body
126
 ;
127

    
128
where_filter:
129
   WHERE term {
130
     /* Construct 'IF term THEN ACCEPT; REJECT;' */
131
     struct filter *f = cfg_alloc(sizeof(struct filter));
132
     struct f_inst *i, *acc, *rej;
133
     acc = f_new_inst();		/* ACCEPT */
134
     acc->code = P('p',',');
135
     acc->a1.p = NULL;
136
     acc->a2.i = F_ACCEPT;
137
     rej = f_new_inst();		/* REJECT */
138
     rej->code = P('p',',');
139
     rej->a1.p = NULL;
140
     rej->a2.i = F_REJECT;
141
     i = f_new_inst();			/* IF */
142
     i->code = '?';
143
     i->a1.p = $2;
144
     i->a2.p = acc;
145
     i->next = rej;
146
     f->name = NULL;
147
     f->root = i;
148
     $$ = f;
149
  }
150
 ;
151

    
152
function_params:
153
   '(' declsn ')' { DBG( "Have function parameters\n" ); $$=$2; }
154
 | '(' ')' { $$=NULL; }
155
 ;
156

    
157
function_body:
158
   decls '{' cmds '}' {
159
     $$ = $3;
160
   }
161
 ;
162

    
163
CF_ADDTO(conf, function_def)
164
function_def:
165
   FUNCTION SYM { DBG( "Beginning of function %s\n", $2->name ); cf_push_scope($2); } function_params function_body {
166
     cf_define_symbol($2, SYM_FUNCTION, $5);
167
     $2->aux = (int) $4;
168
     $2->aux2 = $5;
169
     DBG("Hmm, we've got one function here - %s\n", $2->name); 
170
     cf_pop_scope();
171
   }
172
 ;
173

    
174
/* Programs */
175

    
176
cmds: /* EMPTY */ { $$ = NULL; }
177
 | cmd cmds {
178
     if ($1) {
179
       if ($1->next)
180
	 bug("Command has next already set");
181
       $1->next = $2;
182
       $$ = $1;
183
     } else $$ = $2;
184
   }
185
 ;
186

    
187
block:
188
   cmd {
189
     $$=$1;
190
   }
191
 | '{' cmds '}' {
192
     $$=$2;
193
   }
194
 ;
195

    
196
/*
197
 * Simple types, their bison value is int
198
 */
199
pair:
200
   '(' NUM ',' NUM ')' { $$ = $2 << 16 | $4; }
201
 ;
202

    
203
/*
204
 * Complex types, their bison value is struct f_val
205
 */
206
fprefix_s:
207
   IPA '/' NUM %prec '/' {
208
     if (!ip_is_prefix($1, $3)) cf_error("Invalid network prefix: %I/%d", $1, $3);
209
     $$.type = T_PREFIX; $$.val.px.ip = $1; $$.val.px.len = $3;
210
   }
211
 ;
212

    
213
fprefix:
214
   fprefix_s { $$ = $1; }
215
 | fprefix_s '+' { $$ = $1; $$.val.px.len |= LEN_PLUS; }
216
 | fprefix_s '-' { $$ = $1; $$.val.px.len |= LEN_MINUS; }
217
 | fprefix_s '{' NUM ',' NUM '}' { $$ = $1; $$.val.px.len |= LEN_RANGE | ($3 << 16) | ($5 << 8); }
218
 ;
219

    
220
fipa:
221
   IPA %prec PREFIX_DUMMY { $$.type = T_IP; $$.val.px.ip = $1; }
222
 ;
223

    
224
set_atom:
225
   NUM  { $$.type = T_INT; $$.val.i = $1; }
226
 | pair { $$.type = T_PAIR; $$.val.i = $1; }
227
 | fipa { $$ = $1; }
228
 | fprefix { $$ = $1; }
229
 ; 
230

    
231
set_item:
232
   set_atom { 
233
	$$ = f_new_tree(); 
234
	$$->from = $1; 
235
	if ($1.type != T_PREFIX)
236
		$$->to = $1;
237
	else {
238
		$$->to = $1;
239
		$$->to.val.px.ip = ipa_or( $$->to.val.px.ip, ipa_not( ipa_mkmask( $$->to.val.px.len ) ));
240
	}
241
   }
242
 | set_atom '.' '.' set_atom { 
243
	$$ = f_new_tree(); 
244
	$$->from = $1; 
245
	$$->to = $4; 
246
	if (($1.type == T_PREFIX) || ($4.type == T_PREFIX)) cf_error( "You can not use prefixes for range" ); 
247
   }
248
 ;
249

    
250
set_items:
251
   set_item { $$ = $1; }
252
 | set_items ',' set_item { $$ = $3; $$->left = $1; }
253
 ;
254

    
255
switch_body: /* EMPTY */ { $$ = NULL; }
256
 | set_item ':' cmds switch_body {
257
     $$ = $1;
258
     $$->data = $3;
259
     $$->left = $4;
260
   }
261
 | ELSE ':' cmds {
262
     $$ = f_new_tree(); 
263
     $$->from.type = T_VOID; 
264
     $$->to.type = T_VOID;
265
     $$->data = $3;
266
   }
267
 ;
268

    
269
/* CONST '(' expr ')' { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_INT; $$->a2.i = $3; } */
270

    
271
bgp_one:
272
   NUM { $$ = $1; }
273
 | '?' { $$ = PM_ANY; }
274
 ;
275

    
276
bgp_path: 
277
   bgp_one          { $$ = cfg_alloc(sizeof(struct f_path_mask)); $$->next = NULL; $$->val  = $1; }
278
 | bgp_one bgp_path { $$ = cfg_alloc(sizeof(struct f_path_mask)); $$->next = $2;   $$->val  = $1; }
279
 ;
280

    
281
constant:
282
   NUM    { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_INT;  $$->a2.i = $1; }
283
 | TRUE   { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_BOOL; $$->a2.i = 1;  }
284
 | FALSE  { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_BOOL; $$->a2.i = 0;  }
285
 | TEXT   { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_STRING; $$->a2.p = $1; }
286
 | pair   { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_PAIR;  $$->a2.i = $1; }
287
 | fipa	   { NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; $$->a1.p = val; *val = $1; }
288
 | fprefix_s {NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; $$->a1.p = val; *val = $1; }
289
 | '[' set_items ']' { DBG( "We've got a set here..." ); $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_SET; $$->a2.p = build_tree($2); DBG( "ook\n" ); }
290
 | ENUM	  { $$ = f_new_inst(); $$->code = 'c'; $$->aux = $1 >> 16; $$->a2.i = $1 & 0xffff; }
291
 | '/' bgp_path '/' { NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; val->type = T_PATH_MASK; val->val.path_mask = $2; $$->a1.p = val; }
292
 ;
293

    
294
/*
295
 *  Maybe there are no dynamic attributes defined by protocols.
296
 *  For such cases, we force the dynamic_attr list to contain
297
 *  at least an invalid token, so it's syntantically correct.
298
 */
299
CF_ADDTO(dynamic_attr, INVALID_TOKEN { $$ = NULL; })
300

    
301
rtadot: /* EMPTY, we are not permitted RTA. prefix */
302
 ;
303

    
304
function_call:
305
   SYM '(' var_list ')' {
306
     struct symbol *sym;
307
     struct f_inst *inst = $3;
308
     if ($1->class != SYM_FUNCTION)
309
       cf_error("You can not call something which is not function. Really.");
310
     DBG("You are calling function %s\n", $1->name);
311
     $$ = f_new_inst();
312
     $$->code = P('c','a');
313
     $$->a1.p = inst;
314
     $$->a2.p = $1->aux2;
315
     sym = (void *) $1->aux;
316
     while (sym || inst) {
317
       if (!sym || !inst)
318
	 cf_error("wrong number of arguments for function %s.", $1->name);
319
       DBG( "You should pass parameter called %s\n", sym->name);
320
       inst->a1.p = sym;
321
       sym = (void *) sym->aux;
322
       inst = inst->next;
323
     }
324
   }
325
 ;
326

    
327
static_attr:
328
/* FIXME (maybe?) rta.from read/write */
329
   FROM    { $$ = f_new_inst(); $$->aux = T_IP;         $$->a2.i = OFFSETOF(struct rta, from); }
330

    
331
 | GW      { $$ = f_new_inst(); $$->aux = T_IP;         $$->a2.i = OFFSETOF(struct rta, gw); }    /* FIXME: want it read/write */
332
 | NET     { $$ = f_new_inst(); $$->aux = T_PREFIX;     $$->a2.i = 0x12345678; /* This is actually ok - T_PREFIX is special-cased. */ }
333
 | SOURCE  { $$ = f_new_inst(); $$->aux = T_ENUM_RTS;   $$->a2.i = OFFSETOF(struct rta, source); }
334
 | SCOPE   { $$ = f_new_inst(); $$->aux = T_ENUM_SCOPE; $$->a2.i = OFFSETOF(struct rta, scope); } /* FIXME: want it read/write */
335
 | CAST    { $$ = f_new_inst(); $$->aux = T_ENUM_RTC;   $$->a2.i = OFFSETOF(struct rta, cast); }
336
 | DEST    { $$ = f_new_inst(); $$->aux = T_ENUM_RTD;   $$->a2.i = OFFSETOF(struct rta, dest); }
337
 ;
338

    
339
term:
340
   '(' term ')'      { $$ = $2; }
341
 | term '+' term     { $$ = f_new_inst(); $$->code = '+';        $$->a1.p = $1; $$->a2.p = $3; }
342
 | term '-' term     { $$ = f_new_inst(); $$->code = '-';        $$->a1.p = $1; $$->a2.p = $3; }
343
 | term '*' term     { $$ = f_new_inst(); $$->code = '*';        $$->a1.p = $1; $$->a2.p = $3; }
344
 | term '/' term     { $$ = f_new_inst(); $$->code = '/';        $$->a1.p = $1; $$->a2.p = $3; }
345
 | term '=' term     { $$ = f_new_inst(); $$->code = P('=','='); $$->a1.p = $1; $$->a2.p = $3; }
346
 | term NEQ term { $$ = f_new_inst(); $$->code = P('!','=');     $$->a1.p = $1; $$->a2.p = $3; }
347
 | term '<' term     { $$ = f_new_inst(); $$->code = '<';        $$->a1.p = $1; $$->a2.p = $3; }
348
 | term LEQ term { $$ = f_new_inst(); $$->code = P('<','=');     $$->a1.p = $1; $$->a2.p = $3; }
349
 | term '>' term     { $$ = f_new_inst(); $$->code = '<';        $$->a1.p = $3; $$->a2.p = $1; }
350
 | term GEQ term { $$ = f_new_inst(); $$->code = P('<','=');     $$->a1.p = $3; $$->a2.p = $1; }
351
 | term '~' term     { $$ = f_new_inst(); $$->code = '~';        $$->a1.p = $1; $$->a2.p = $3; }
352
 | '!' term { $$ = f_new_inst(); $$->code = '!'; $$->a1.p = $2; }
353
 | DEFINED '(' term ')' { $$ = f_new_inst(); $$->code = P('d','e');  $$->a1.p = $3; }
354

    
355
 | constant { $$ = $1; }
356
 | SYM {
357
     $$ = f_new_inst();
358
     switch ($1->class) {
359
       case SYM_NUMBER:
360
	$$ = f_new_inst();
361
	$$->code = 'c'; 
362
	$$->aux = T_INT; 
363
	$$->a2.i = $1->aux;
364
	break;
365
       case SYM_IPA:
366
	{ NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; $$->a1.p = val; val->type = T_IP; val->val.px.ip = * (ip_addr *) ($1->def); }
367
	break;
368
       case SYM_VARIABLE | T_INT:
369
       case SYM_VARIABLE | T_PAIR:
370
       case SYM_VARIABLE | T_PREFIX:
371
       case SYM_VARIABLE | T_IP:
372
       case SYM_VARIABLE | T_PATH_MASK:
373
       case SYM_VARIABLE | T_PATH:
374
       case SYM_VARIABLE | T_CLIST:
375
	 $$->code = 'C';
376
	 $$->a1.p = $1->aux2;
377
	 break;
378
       default:
379
	 cf_error("Can not use this class of symbol (%s,%x) as variable.", $1->name, $1->class );
380
     }
381
   }
382

    
383
/* FIXME rte.Preference - read/write */
384

    
385
 | rtadot static_attr { $$ = $2; $$->code = 'a'; }
386

    
387
 | rtadot dynamic_attr { $$ = $2; $$->code = P('e','a'); }
388

    
389
 | term '.' IP { $$ = f_new_inst(); $$->code = P('c','p'); $$->a1.p = $1; $$->aux = T_IP; }
390
 | term '.' LEN { $$ = f_new_inst(); $$->code = 'L'; $$->a1.p = $1; }
391
 | term '.' MASK '(' term ')' { $$ = f_new_inst(); $$->code = P('i','M'); $$->a1.p = $1; $$->a2.p = $5; }
392

    
393
/* Communities */
394
/* This causes one shift/reduce conflict
395
 | rtadot dynamic_attr '.' ADD '(' term ')' { }
396
 | rtadot dynamic_attr '.' DELETE '(' term ')' { }
397
 | rtadot dynamic_attr '.' CONTAINS '(' term ')' { }
398
 | rtadot dynamic_attr '.' RESET{ }
399
*/
400

    
401
 | '+' EMPTY '+' { $$ = f_new_inst(); $$->code = 'E'; $$->aux = T_PATH; }
402
 | '-' EMPTY '-' { $$ = f_new_inst(); $$->code = 'E'; $$->aux = T_CLIST; }
403
 | PREPEND '(' term ',' term ')' { $$ = f_new_inst(); $$->code = P('A','p'); $$->a1.p = $3; $$->a2.p = $5; } 
404
 | ADD '(' term ',' term ')' { $$ = f_new_inst(); $$->code = P('C','a'); $$->a1.p = $3; $$->a2.p = $5; $$->aux = 'a'; } 
405
 | DELETE '(' term ',' term ')' { $$ = f_new_inst(); $$->code = P('C','a'); $$->a1.p = $3; $$->a2.p = $5; $$->aux = 'd'; }
406

    
407
/* | term '.' LEN { $$->code = P('P','l'); } */
408

    
409
/* function_call is inlined here */
410
 | SYM '(' var_list ')' {
411
     struct symbol *sym;
412
     struct f_inst *inst = $3;
413
     if ($1->class != SYM_FUNCTION)
414
       cf_error("You can not call something which is not function. Really.");
415
     DBG("You are calling function %s\n", $1->name);
416
     $$ = f_new_inst();
417
     $$->code = P('c','a');
418
     $$->a1.p = inst;
419
     $$->a2.p = $1->aux2;
420
     sym = (void *) $1->aux;
421
     while (sym || inst) {
422
       if (!sym || !inst)
423
	 cf_error("wrong number of arguments for function %s.", $1->name);
424
       DBG( "You should pass parameter called %s\n", sym->name);
425
       inst->a1.p = sym;
426
       sym = (void *) sym->aux;
427
       inst = inst->next;
428
     }
429
   }
430
 ;
431

    
432
break_command:
433
   QUITBIRD { $$ = F_QUITBIRD }
434
 | ACCEPT { $$ = F_ACCEPT }
435
 | REJECT { $$ = F_REJECT }
436
 | ERROR { $$ = F_ERROR }
437
 | PRINT { $$ = F_NOP }
438
 | PRINTN { $$ = F_NONL }
439
 ;
440

    
441
print_one:
442
   term { $$ = f_new_inst(); $$->code = 'p'; $$->a1.p = $1; $$->a2.p = NULL; }
443
 ;
444

    
445
print_list: /* EMPTY */ { $$ = NULL; }
446
 | print_one { $$ = $1; }
447
 | print_one ',' print_list {
448
     if ($1) {
449
       $1->next = $3;
450
       $$ = $1;
451
     } else $$ = $3;
452
   }
453
 
454
 ;
455

    
456
var_listn: term { 
457
     $$ = f_new_inst();
458
     $$->code = 's';
459
     $$->a1.p = NULL;
460
     $$->a2.p = $1;
461
     $$->next = NULL;
462
   }
463
 | term ',' var_listn {
464
     $$ = f_new_inst();
465
     $$->code = 's';
466
     $$->a1.p = NULL;
467
     $$->a2.p = $1;
468
     $$->next = $3;
469
   }
470
 ;
471

    
472
var_list: /* EMPTY */ { $$ = NULL; }
473
 | var_listn { $$ = $1; }
474
 ;
475

    
476
cmd:
477
   IF term THEN block {
478
     $$ = f_new_inst();
479
     $$->code = '?';
480
     $$->a1.p = $2;
481
     $$->a2.p = $4;
482
   }
483
 | IF term THEN block ELSE block {
484
     struct f_inst *i = f_new_inst();
485
     i->code = '?';
486
     i->a1.p = $2;
487
     i->a2.p = $4;
488
     $$ = f_new_inst();
489
     $$->code = '?';
490
     $$->a1.p = i;
491
     $$->a2.p = $6;
492
   }
493
 | SYM '=' term ';' {
494
     $$ = f_new_inst();
495
     DBG( "Ook, we'll set value\n" );
496
     if (($1->class & ~T_MASK) != SYM_VARIABLE)
497
       cf_error( "You may only set variables, and this is %x.", $1->class );
498
     $$->code = 's';
499
     $$->a1.p = $1;
500
     $$->a2.p = $3;
501
   }
502
 | RETURN term ';' {
503
     $$ = f_new_inst();
504
     DBG( "Ook, we'll return the value\n" );
505
     $$->code = 'r';
506
     $$->a1.p = $2;
507
   }
508
 | rtadot dynamic_attr '=' term ';' {
509
     $$ = $2;
510
     $$->code  = P('e','S');
511
     $$->a1.p = $4;
512
   }
513
 | UNSET '(' rtadot dynamic_attr ')' ';' {
514
     $$ = $4;
515
     $$->aux = EAF_TYPE_UNDEF | EAF_TEMP;
516
     $$->code = P('e','S');
517
     $$->a1.p = NULL;
518
   }
519
 | break_command print_list ';' { $$ = f_new_inst(); $$->code = P('p',','); $$->a1.p = $2; $$->a2.i = $1; }
520
 | function_call ';' { $$ = $1; }
521
 | CASE term '{' switch_body '}' {
522
      $$ = f_new_inst();
523
      $$->code = P('S','W');
524
      $$->a1.p = $2;
525
      $$->a2.p = build_tree( $4 );
526
   }
527

    
528

    
529
 | rtadot dynamic_attr '.' EMPTY ';' 
530
  { struct f_inst *i = f_new_inst(); i->code = 'E'; i->aux = T_CLIST; $$ = $2; $$->code = P('e','S'); $$->a1.p = i; }
531
 | rtadot dynamic_attr '.' PREPEND '(' term ')' ';'   { $$ = f_generate_complex( P('A','p'), 'x', $2, $6 ); }
532
 | rtadot dynamic_attr '.' ADD '(' term ')' ';'       { $$ = f_generate_complex( P('C','a'), 'a', $2, $6 ); } 
533
 | rtadot dynamic_attr '.' DELETE '(' term ')' ';'    { $$ = f_generate_complex( P('C','a'), 'd', $2, $6 ); } 
534
 ;
535

    
536
CF_END