Statistics
| Branch: | Revision:

iof-bird-daemon / filter / config.Y @ 72282e2a

History | View | Annotate | Download (14.8 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
	FIXME (nonurgent): define keyword
9
	FIXME (for BGP): whole system of paths, path ~ string, path.prepend(), path.originate
10
	FIXME: create community lists
11
	FIXME: IP addresses in ipv6
12

    
13

    
14
        FIXME: check messages for \n at the end
15
 */
16

    
17
CF_HDR
18

    
19
CF_DEFINES
20

    
21
#define P(a,b) ((a<<8) | b)
22

    
23
CF_DECLS
24

    
25
CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN,
26
	ACCEPT, REJECT, ERROR, QUITBIRD,
27
	INT, BOOL, IP, PREFIX, PAIR, SET, STRING, BGPMASK, BGPPATH, CLIST,
28
	IF, THEN, ELSE, CASE,
29
	TRUE, FALSE,
30
	FROM, GW, NET, MASK, SOURCE,
31
	LEN,
32
	DEFINED,
33
	ADD, DELETE, CONTAINS, RESET,
34
	PREPEND, MATCH,
35
	EMPTY,
36
	FILTER, WHERE, EVAL)
37

    
38
%nonassoc THEN
39
%nonassoc ELSE
40

    
41
%type <x> term block cmds cmd function_body constant print_one print_list var_list var_listn dynamic_attr function_call
42
%type <f> filter filter_body where_filter
43
%type <i> type break_command pair
44
%type <e> set_item set_items switch_body
45
%type <v> set_atom fprefix fprefix_s fipa
46
%type <s> decls declsn one_decl function_params 
47
%type <h> bgp_path
48
%type <i> bgp_one
49

    
50
CF_GRAMMAR
51

    
52
CF_ADDTO(conf, filter_def)
53
filter_def:
54
   FILTER SYM { cf_push_scope( $2 ); } filter_body {
55
     cf_define_symbol($2, SYM_FILTER, $4);
56
     $4->name = $2->name;
57
     DBG( "We have new filter defined (%s)\n", $2->name );
58
     cf_pop_scope();
59
   }
60
 ;
61

    
62
CF_ADDTO(conf, filter_eval)
63
filter_eval:
64
   EVAL term { f_eval_int($2); }
65
 ;
66

    
67
type:
68
   INT { $$ = T_INT; }
69
 | BOOL { $$ = T_BOOL; }
70
 | IP { $$ = T_IP; }
71
 | PREFIX { $$ = T_PREFIX; }
72
 | PAIR { $$ = T_PAIR; }
73
 | STRING { $$ = T_STRING; }
74
 | BGPMASK { $$ = T_PATH_MASK; }
75
 | BGPPATH { $$ = T_PATH; }
76
 | CLIST { $$ = T_CLIST; }
77
 | type SET { 
78
	switch ($1) {
79
	  default:
80
		cf_error( "You can not create sets of this type\n" );
81
	  case T_INT: case T_IP: case T_PREFIX: case T_PAIR:
82
	}
83
	$$ = $1 | T_SET;
84
	}
85
 ;
86

    
87
one_decl:
88
   type SYM {
89
     cf_define_symbol($2, SYM_VARIABLE | $1, NULL);
90
     DBG( "New variable %s type %x\n", $2->name, $1 );
91
     $2->aux = 0;
92
     {
93
       struct f_val * val; 
94
       val = cfg_alloc(sizeof(struct f_val)); 
95
       val->type = $1; 
96
       $2->aux2 = val;
97
     }
98
     $$=$2;
99
   }
100
 ;
101

    
102
/* Decls with ';' at the end */
103
decls: /* EMPTY */ { $$ = NULL; }
104
 | one_decl ';' decls {
105
     $$ = $1;
106
     $$->aux = (int) $3;
107
   }
108
 ;
109

    
110
/* Declarations that have no ';' at the end. */
111
declsn: one_decl { $$ = $1; }
112
 | declsn ';' one_decl {
113
     $$ = $1;
114
     $$->aux = (int) $3;
115
   }
116
 ;
117

    
118
filter_body:
119
   function_body {
120
     struct filter *f = cfg_alloc(sizeof(struct filter));
121
     f->name = NULL;
122
     f->root = $1;
123
     $$ = f;
124
   }
125
 ;
126

    
127
filter:
128
   SYM {
129
     if ($1->class != SYM_FILTER) cf_error("No such filter");
130
     $$ = $1->def;
131
   }
132
 | filter_body
133
 ;
134

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

    
159
function_params:
160
   '(' declsn ')' { DBG( "Have function parameters\n" ); $$=$2; }
161
 | '(' ')' { $$=NULL; }
162
 ;
163

    
164
function_body:
165
   decls '{' cmds '}' {
166
     $$ = $3;
167
   }
168
 ;
169

    
170
CF_ADDTO(conf, function_def)
171
function_def:
172
   FUNCTION SYM { DBG( "Beginning of function %s\n", $2->name ); cf_push_scope($2); } function_params function_body {
173
     cf_define_symbol($2, SYM_FUNCTION, $5);
174
     $2->aux = (int) $4;
175
     $2->aux2 = $5;
176
     DBG("Hmm, we've got one function here - %s\n", $2->name); 
177
     cf_pop_scope();
178
   }
179
 ;
180

    
181
/* Programs */
182

    
183
cmds: /* EMPTY */ { $$ = NULL; }
184
 | cmd cmds {
185
     if ($1) {
186
       if ($1->next)
187
	 bug("Command has next already set\n");
188
       $1->next = $2;
189
       $$ = $1;
190
     } else $$ = $2;
191
   }
192
 ;
193

    
194
block:
195
   cmd {
196
     $$=$1;
197
   }
198
 | '{' cmds '}' {
199
     $$=$2;
200
   }
201
 ;
202

    
203
/*
204
 * Simple types, their bison value is int
205
 */
206
pair:
207
   '(' NUM ',' NUM ')' { $$ = $2 << 16 | $4; }
208
 ;
209

    
210
/*
211
 * Complex types, their bison value is struct f_val
212
 */
213
fprefix_s:
214
   IPA '/' NUM %prec '/' {
215
     if (!ip_is_prefix($1, $3)) cf_error("Invalid network prefix: %I/%d", $1, $3);
216
     $$.type = T_PREFIX; $$.val.px.ip = $1; $$.val.px.len = $3;
217
   }
218
 ;
219

    
220
fprefix:
221
   fprefix_s { $$ = $1; }
222
 | fprefix_s '+' { $$ = $1; $$.val.px.len |= LEN_PLUS; }
223
 | fprefix_s '-' { $$ = $1; $$.val.px.len |= LEN_MINUS; }
224
 | fprefix_s '{' NUM ',' NUM '}' { $$ = $1; $$.val.px.len |= LEN_RANGE | ($3 << 16) | ($5 << 8); }
225
 ;
226

    
227
fipa:
228
   IPA %prec PREFIX_DUMMY { $$.type = T_IP; $$.val.px.ip = $1; }
229
 ;
230

    
231
set_atom:
232
   NUM  { $$.type = T_INT; $$.val.i = $1; }
233
 | pair { $$.type = T_PAIR; $$.val.i = $1; }
234
 | fipa { $$ = $1; }
235
 | fprefix { $$ = $1; }
236
 ; 
237

    
238
set_item:
239
   set_atom { 
240
	$$ = f_new_tree(); 
241
	$$->from = $1; 
242
	if ($1.type != T_PREFIX)
243
		$$->to = $1;
244
	else {
245
		$$->to = $1;
246
		$$->to.val.px.ip = ipa_or( $$->to.val.px.ip, ipa_not( ipa_mkmask( $$->to.val.px.len ) ));
247
	}
248
   }
249
 | set_atom '.' '.' set_atom { 
250
	$$ = f_new_tree(); 
251
	$$->from = $1; 
252
	$$->to = $4; 
253
	if (($1.type == T_PREFIX) || ($4.type == T_PREFIX)) cf_error( "You can not use prefixes for range" ); 
254
   }
255
 ;
256

    
257
set_items:
258
   set_item { $$ = $1; }
259
 | set_items ',' set_item { $$ = $3; $$->left = $1; }
260
 ;
261

    
262
switch_body: /* EMPTY */ { $$ = NULL; }
263
 | set_item ':' cmds switch_body {
264
     $$ = $1;
265
     $$->data = $3;
266
     $$->left = $4;
267
   }
268
 | ELSE ':' cmds {
269
     $$ = f_new_tree(); 
270
     $$->from.type = T_VOID; 
271
     $$->to.type = T_VOID;
272
     $$->data = $3;
273
   }
274
 ;
275

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

    
278
bgp_one:
279
   NUM { $$ = $1; }
280
 | '*' { $$ = PM_ANY; }
281
 ;
282

    
283
bgp_path: 
284
   bgp_one          { $$ = cfg_alloc(sizeof(struct f_path_mask)); $$->next = NULL; $$->val  = $1; }
285
 | bgp_one bgp_path { $$ = cfg_alloc(sizeof(struct f_path_mask)); $$->next = $2;   $$->val  = $1; }
286
 ;
287

    
288
constant:
289
   NUM    { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_INT;  $$->a2.i = $1; }
290
 | TRUE   { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_BOOL; $$->a2.i = 1;  }
291
 | FALSE  { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_BOOL; $$->a2.i = 0;  }
292
 | TEXT   { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_STRING; $$->a2.p = $1; }
293
 | pair   { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_PAIR;  $$->a2.i = $1; }
294
 | fipa	   { NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; $$->a1.p = val; *val = $1; }
295
 | fprefix_s {NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; $$->a1.p = val; *val = $1; }
296
 | '[' 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" ); }
297
 | ENUM	  { $$ = f_new_inst(); $$->code = 'c'; $$->aux = $1 >> 16; $$->a2.i = $1 & 0xffff; }
298
 | '/' bgp_path '/' { NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; val->type = T_PATH_MASK; val->val.path_mask = $2; $$->a1.p = val; }
299
 ;
300

    
301
/*
302
 *  Maybe there are no dynamic attributes defined by protocols.
303
 *  For such cases, we force the dynamic_attr list to contain
304
 *  at least an invalid token, so it's syntantically correct.
305
 */
306
CF_ADDTO(dynamic_attr, INVALID_TOKEN { $$ = NULL; })
307

    
308
rtadot: /* EMPTY, we are not permitted RTA. prefix */
309
 ;
310

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

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

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

    
378
 | rtadot FROM    { $$ = f_new_inst(); $$->code = 'a'; $$->aux = T_IP; $$->a2.i = OFFSETOF(struct rta, from); }
379

    
380
 | rtadot GW      { $$ = f_new_inst(); $$->code = 'a'; $$->aux = T_IP; $$->a2.i = OFFSETOF(struct rta, gw); }
381
 | rtadot NET     { $$ = f_new_inst(); $$->code = 'a'; $$->aux = T_PREFIX; $$->a2.i = 0x12345678; /* This is actually ok - T_PREFIX is special-cased. */ }
382
 | rtadot SOURCE  { $$ = f_new_inst(); $$->code = 'a'; $$->aux = T_ENUM_RTS; $$->a2.i = OFFSETOF(struct rta, source); }
383

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

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

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

    
398
/* Paths */
399
 | term '.' PREPEND '(' term ')' { }
400
 | term '.' RESET { }
401

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

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

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

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

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

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

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

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

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

    
530

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

    
538
CF_END