]> git.ipfire.org Git - thirdparty/bird.git/blobdiff - filter/config.Y
Filter: Remove quitbird command
[thirdparty/bird.git] / filter / config.Y
index f57575fc84837533a03d6224785e2b714a86030f..77424a8b21c67586003f69cc481b9cdc9470861a 100644 (file)
@@ -15,19 +15,13 @@ CF_HDR
 
 CF_DEFINES
 
-static uint decls_count;
-
 static inline u32 pair(u32 a, u32 b) { return (a << 16) | b; }
 static inline u32 pair_a(u32 p) { return p >> 16; }
 static inline u32 pair_b(u32 p) { return p & 0xFFFF; }
 
-#define f_generate_complex(fi_code, _da, arg) ({ \
-  struct f_inst *fi = f_new_inst(fi_code, f_new_inst(FI_EA_GET, _da), arg); \
-  fi->result.type = F_LVAL_EA; \
-  fi->result.da = _da; \
-  fi; \
-})
-  
+#define f_generate_complex(fi_code, da, arg) \
+  f_new_inst(FI_EA_SET, f_new_inst(fi_code, f_new_inst(FI_EA_GET, da), arg), da)
+
 /*
  * Sets and their items are during parsing handled as lists, linked
  * through left ptr. The first item in a list also contains a pointer
@@ -188,165 +182,9 @@ f_generate_empty(struct f_dynamic_attr dyn)
       cf_error("Can't empty that attribute");
   }
 
-  struct f_inst *fi = f_new_inst(FI_CONSTANT, empty);
-  fi->result.type = F_LVAL_EA;
-  fi->result.da = dyn;
-  return fi;
-}
-
-#if 0
-
-static inline struct f_inst *
-f_generate_dpair(struct f_inst *t1, struct f_inst *t2)
-{
-  struct f_inst *rv;
-
-  if ((t1->fi_code == FI_CONSTANT) && (t2->fi_code == FI_CONSTANT)) {
-    if ((t1->val.type != T_INT) || (t2->val.type != T_INT))
-      cf_error( "Can't operate with value of non-integer type in pair constructor");
-
-    check_u16(t1->a[1].i);
-    check_u16(t2->a[1].i);
-
-    rv = f_new_inst(FI_CONSTANT);
-    rv->val = (struct f_val) {
-      .type = T_PAIR,
-      .val.i = pair(t1->a[1].i, t2->a[1].i),
-    };
-  }
-  else {
-    rv = f_new_inst(FI_PAIR_CONSTRUCT);
-    rv->a[0].p = t1;
-    rv->a[1].p = t2;
-  }
-
-  return rv;
+  return f_new_inst(FI_EA_SET, f_new_inst(FI_CONSTANT, empty), dyn);
 }
 
-static inline struct f_inst *
-f_generate_ec(u16 kind, struct f_inst *tk, struct f_inst *tv)
-{
-  struct f_inst *rv;
-  int c1 = 0, c2 = 0, ipv4_used = 0;
-  u32 key = 0, val2 = 0;
-
-  if (tk->fi_code == FI_CONSTANT) {
-    c1 = 1;
-    struct f_val *val = &(tk->val);
-
-    if (val->type == T_INT) {
-      ipv4_used = 0; key = val->val.i;
-    }
-    else if (tk->val.type == T_QUAD) {
-      ipv4_used = 1; key = val->val.i;
-    }
-    else if ((val->type == T_IP) && ipa_is_ip4(val->val.ip)) {
-      ipv4_used = 1; key = ipa_to_u32(val->val.ip);
-    }
-    else
-      cf_error("Can't operate with key of non-integer/IPv4 type in EC constructor");
-  }
-
-  if (tv->fi_code == FI_CONSTANT) {
-    if (tv->val.type != T_INT)
-      cf_error("Can't operate with value of non-integer type in EC constructor");
-    c2 = 1;
-    val2 = tv->val.val.i;
-  }
-
-  if (c1 && c2) {
-    u64 ec;
-
-    if (kind == EC_GENERIC) {
-      ec = ec_generic(key, val2);
-    }
-    else if (ipv4_used) {
-      check_u16(val2);
-      ec = ec_ip4(kind, key, val2);
-    }
-    else if (key < 0x10000) {
-      ec = ec_as2(kind, key, val2);
-    }
-    else {
-      check_u16(val2);
-      ec = ec_as4(kind, key, val2);
-    }
-
-    rv = f_new_inst(FI_CONSTANT);
-    rv->val = (struct f_val) {
-      .type = T_EC,
-      .val.ec = ec,
-    };
-  }
-  else {
-    rv = f_new_inst(FI_EC_CONSTRUCT);
-    rv->aux = kind;
-    rv->a[0].p = tk;
-    rv->a[1].p = tv;
-  }
-
-  return rv;
-}
-
-static inline struct f_inst *
-f_generate_lc(struct f_inst *t1, struct f_inst *t2, struct f_inst *t3)
-{
-  struct f_inst *rv;
-
-  if ((t1->fi_code == FI_CONSTANT) && (t2->fi_code == FI_CONSTANT) && (t3->fi_code == FI_CONSTANT)) {
-    if ((t1->val.type != T_INT) || (t2->val.type != T_INT) || (t3->val.type != T_INT))
-      cf_error( "LC - Can't operate with value of non-integer type in tuple constructor");
-
-    rv = f_new_inst(FI_CONSTANT);
-    rv->val = (struct f_val) {
-      .type = T_LC,
-      .val.lc = (lcomm) { t1->a[1].i, t2->a[1].i, t3->a[1].i },
-    };
-  }
-  else
-  {
-    rv = f_new_inst(FI_LC_CONSTRUCT);
-    rv->a[0].p = t1;
-    rv->a[1].p = t2;
-    rv->a[2].p = t3;
-  }
-
-  return rv;
-}
-
-static inline struct f_inst *
-f_generate_path_mask(struct f_inst *t)
-{
-  uint len = 0;
-  uint dyn = 0;
-  for (const struct f_inst *tt = t; tt; tt = tt->next) {
-    if (tt->fi_code != FI_CONSTANT)
-      dyn++;
-    len++;
-  }
-
-  if (dyn) {
-    struct f_inst *pmc = f_new_inst(FI_PATHMASK_CONSTRUCT);
-    pmc->a[0].p = t;
-    pmc->a[1].i = len;
-    return pmc;
-  }
-
-  struct f_path_mask *pm = cfg_allocz(sizeof(struct f_path_mask) + len * sizeof(struct f_path_mask_item));
-
-  uint i = 0;
-  for (const struct f_inst *tt = t; tt; tt = tt->next)
-    pm->item[i++] = tt->val.val.pmi;
-
-  pm->len = i;
-  struct f_inst *pmc = f_new_inst(FI_CONSTANT);
-  pmc->val = (struct f_val) { .type = T_PATH_MASK, .val.path_mask = pm, };
-
-  return pmc;
-}
-
-#endif
-
 /*
  * Remove all new lines and doubled whitespaces
  * and convert all tabulators to spaces
@@ -404,48 +242,45 @@ assert_done(struct f_inst *expr, const char *start, const char *end)
 static struct f_inst *
 assert_assign(struct f_lval *lval, struct f_inst *expr, const char *start, const char *end)
 {
-  struct f_inst *getter;
+  struct f_inst *setter, *getter, *checker;
   switch (lval->type) {
-    case F_LVAL_STACK:
-      bug("This shall never happen");
-    case F_LVAL_EXCEPTION:
-      bug("This shall never happen");
     case F_LVAL_VARIABLE:
+      setter = f_new_inst(FI_VAR_SET, expr, lval->sym);
       getter = f_new_inst(FI_VAR_GET, lval->sym);
       break;
     case F_LVAL_PREFERENCE:
+      setter = f_new_inst(FI_PREF_SET, expr);
       getter = f_new_inst(FI_PREF_GET);
       break;
     case F_LVAL_SA:
+      setter = f_new_inst(FI_RTA_SET, expr, lval->sa);
       getter = f_new_inst(FI_RTA_GET, lval->sa);
       break;
     case F_LVAL_EA:
+      setter = f_new_inst(FI_EA_SET, expr, lval->da);
       getter = f_new_inst(FI_EA_GET, lval->da);
       break;
+    default:
+      bug("Unknown lval type");
   }
 
-  struct f_inst *checker = f_new_inst(FI_EQ, expr, getter);
-
-  struct f_inst *setter = cfg_alloc(sizeof(struct f_inst));
-  *setter = *expr;
-
+  checker = f_new_inst(FI_EQ, expr, getter);
   setter->next = checker;
-  setter->result = *lval;
-
+  
   return assert_done(setter, start, end);
 }
 
 CF_DECLS
 
 CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN,
-       ACCEPT, REJECT, ERROR, QUITBIRD,
+       ACCEPT, REJECT, ERROR,
        INT, BOOL, IP, TYPE, PREFIX, RD, PAIR, QUAD, EC, LC,
        SET, STRING, BGPMASK, BGPPATH, CLIST, ECLIST, LCLIST,
        IF, THEN, ELSE, CASE,
        TRUE, FALSE, RT, RO, UNKNOWN, GENERIC,
        FROM, GW, NET, MASK, PROTO, SOURCE, SCOPE, DEST, IFNAME, IFINDEX,
        PREFERENCE,
-       ROA_CHECK, ASN, SRC,
+       ROA_CHECK, ASN, SRC, DST,
        IS_V4, IS_V6,
        LEN, MAXLEN,
        DEFINED,
@@ -458,14 +293,14 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN,
 %nonassoc THEN
 %nonassoc ELSE
 
-%type <xp> cmds_int
-%type <x> term block cmd cmds constant constructor print_one print_list var_list function_call symbol_value bgp_path_expr bgp_path bgp_path_tail
+%type <xp> cmds_int cmd_prep
+%type <x> term block cmd cmds constant constructor print_list var_list function_call symbol_value bgp_path_expr bgp_path bgp_path_tail
 %type <fda> dynamic_attr
 %type <fsa> static_attr
 %type <f> filter where_filter
 %type <fl> filter_body function_body
 %type <flv> lvalue
-%type <i> type function_params
+%type <i> type function_args function_vars
 %type <ecs> ec_kind
 %type <fret> break_command 
 %type <i32> cnum
@@ -479,7 +314,7 @@ CF_GRAMMAR
 
 conf: filter_def ;
 filter_def:
-   FILTER CF_SYM_VOID { $2 = cf_define_symbol($2, SYM_FILTER, filter, NULL); cf_push_scope( $2 ); }
+   FILTER symbol { $2 = cf_define_symbol($2, SYM_FILTER, filter, NULL); cf_push_scope( $2 ); }
      filter_body {
      struct filter *f = cfg_alloc(sizeof(struct filter));
      *f = (struct filter) { .sym = $2, .root = $4 };
@@ -495,7 +330,7 @@ filter_eval:
  ;
 
 conf: custom_attr ;
-custom_attr: ATTRIBUTE type CF_SYM_VOID ';' {
+custom_attr: ATTRIBUTE type symbol ';' {
   cf_define_symbol($3, SYM_ATTRIBUTE, attribute, ca_lookup(new_config->pool, $3->name, $2)->fda);
 };
 
@@ -565,24 +400,31 @@ type:
    }
  ;
 
-/* Declarations with ';' at the end */
-decls:
+function_argsn:
    /* EMPTY */
- | declsn ';'
+ | function_argsn type symbol ';' {
+     if ($3->scope->slots >= 0xfe) cf_error("Too many declarations, at most 255 allowed");
+     cf_define_symbol($3, SYM_VARIABLE | $2, offset, $3->scope->slots++);
+   }
  ;
 
-/* Declarations that have no ';' at the end. */
-declsn:
-   type CF_SYM_VOID {
-     cf_define_symbol($2, SYM_VARIABLE | $1, offset, decls_count++);
+function_args:
+   '(' ')' { $$ = 0; }
+ | '(' function_argsn type symbol ')' {
+     cf_define_symbol($4, SYM_VARIABLE | $3, offset, $4->scope->slots++);
+     $$ = $4->scope->slots;
    }
- | declsn ';' type CF_SYM_VOID {
-     if (decls_count >= 0xff) cf_error("Too many declarations, at most 255 allowed");
-     cf_define_symbol($4, SYM_VARIABLE | $3, offset, decls_count++);
+ ;
+
+function_vars:
+   /* EMPTY */ { $$ = 0; }
+ | function_vars type symbol ';' {
+     cf_define_symbol($3, SYM_VARIABLE | $2, offset, $3->scope->slots++);
+     $$ = $1 + 1;
    }
  ;
 
-filter_body: { decls_count = 0; } function_body { $$ = $2; } ;
+filter_body: function_body ;
 
 filter:
    CF_SYM_KNOWN {
@@ -603,26 +445,20 @@ where_filter:
    }
  ;
 
-function_params:
-   '(' declsn ')' { $$ = decls_count; }
- | '(' ')' { $$ = 0; }
- ;
-
 function_body:
-   decls '{' cmds '}' {
+   function_vars '{' cmds '}' {
      $$ = f_linearize($3);
-     $$->vars = decls_count;
+     $$->vars = $1;
    }
  ;
 
 conf: function_def ;
 function_def:
-   FUNCTION CF_SYM_VOID { DBG( "Beginning of function %s\n", $2->name );
+   FUNCTION symbol { DBG( "Beginning of function %s\n", $2->name );
      $2 = cf_define_symbol($2, SYM_FUNCTION, function, NULL);
      cf_push_scope($2);
-     decls_count = 0;
-   } function_params function_body {
-     $5->vars -= $4;
+   } function_args function_body {
+     DBG("Definition of function %s with %u args and %u local vars.\n", $2->name, $4, $5->vars);
      $5->args = $4;
      $2->function = $5;
      cf_pop_scope();
@@ -632,11 +468,29 @@ function_def:
 /* Programs */
 
 cmds: /* EMPTY */ { $$ = NULL; }
- | cmds_int { $$ = $1[0]; }
+ | cmds_int { $$ = $1.begin; }
  ;
 
-cmds_int: cmd { $$[0] = $$[1] = $1; }
- | cmds_int cmd { $$[1] = $2; $1[1]->next = $2; $$[0] = $1[0]; }
+cmd_prep: cmd {
+  $$.begin = $$.end = $1;
+  if ($1)
+    while ($$.end->next)
+      $$.end = $$.end->next;
+}
+ ;
+
+cmds_int: cmd_prep
+ | cmds_int cmd_prep {
+  if (!$1.begin)
+    $$ = $2;
+  else if (!$2.begin)
+    $$ = $1;
+  else {
+    $$.begin = $1.begin;
+    $$.end = $2.end;
+    $1.end->next = $2.begin;
+  }
+ }
  ;
 
 block:
@@ -768,8 +622,8 @@ fprefix:
  ;
 
 fprefix_set:
-   fprefix { $$ = f_new_trie(cfg_mem, sizeof(struct f_trie_node)); trie_add_prefix($$, &($1.net), $1.lo, $1.hi); }
- | fprefix_set ',' fprefix { $$ = $1; trie_add_prefix($$, &($3.net), $3.lo, $3.hi); }
+   fprefix { $$ = f_new_trie(cfg_mem, 0); trie_add_prefix($$, &($1.net), $1.lo, $1.hi); }
+ | fprefix_set ',' fprefix { $$ = $1; if (!trie_add_prefix($$, &($3.net), $3.lo, $3.hi)) cf_error("Mixed IPv4/IPv6 prefixes in prefix set"); }
  ;
 
 switch_body: /* EMPTY */ { $$ = NULL; }
@@ -802,6 +656,10 @@ bgp_path:
 bgp_path_tail:
    NUM bgp_path_tail           { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PATH_MASK_ITEM, .val.pmi = { .asn = $1, .kind = PM_ASN, }, }); $$->next = $2;  }
  | NUM DDOT NUM bgp_path_tail  { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PATH_MASK_ITEM, .val.pmi = { .from = $1, .to = $3, .kind = PM_ASN_RANGE }, }); $$->next = $4; }
+ | '[' set_items ']' bgp_path_tail {
+   if ($2->from.type != T_INT) cf_error("Only integer sets allowed in path mask");
+   $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PATH_MASK_ITEM, .val.pmi = { .set = build_tree($2), .kind = PM_ASN_SET }, }); $$->next = $4;
+ }
  | '*' bgp_path_tail           { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PATH_MASK_ITEM, .val.pmi = { .kind = PM_ASTERISK }, }); $$->next = $2; }
  | '?' bgp_path_tail           { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PATH_MASK_ITEM, .val.pmi = { .kind = PM_QUESTION }, }); $$->next = $2; }
  | bgp_path_expr bgp_path_tail { $$ = $1; $$->next = $2; }
@@ -829,7 +687,7 @@ constructor:
    '(' term ',' term ')' { $$ = f_new_inst(FI_PAIR_CONSTRUCT, $2, $4); }
  | '(' ec_kind ',' term ',' term ')' { $$ = f_new_inst(FI_EC_CONSTRUCT, $4, $6, $2); }
  | '(' term ',' term ',' term ')' { $$ = f_new_inst(FI_LC_CONSTRUCT, $2, $4, $6); }
- | bgp_path { $$ = f_new_inst(FI_PATHMASK_CONSTRUCT, $1, 0); }
+ | bgp_path { $$ = f_new_inst(FI_PATHMASK_CONSTRUCT, $1); }
  ;
 
 
@@ -867,7 +725,7 @@ symbol_value: CF_SYM_KNOWN
   {
     switch ($1->class) {
       case SYM_CONSTANT_RANGE:
-       $$ = f_new_inst(FI_CONSTANT_DEFINED, $1);
+       $$ = f_new_inst(FI_CONSTANT, *($1->val));
        break;
       case SYM_VARIABLE_RANGE:
        $$ = f_new_inst(FI_VAR_GET, $1);
@@ -929,7 +787,8 @@ term:
  | term '.' LEN { $$ = f_new_inst(FI_LENGTH, $1); }
  | term '.' MAXLEN { $$ = f_new_inst(FI_ROA_MAXLEN, $1); }
  | term '.' ASN { $$ = f_new_inst(FI_ROA_ASN, $1); }
- | term '.' SRC { $$ = f_new_inst(FI_SADR_SRC, $1); }
+ | term '.' SRC { $$ = f_new_inst(FI_NET_SRC, $1); }
+ | term '.' DST { $$ = f_new_inst(FI_NET_DST, $1); }
  | term '.' MASK '(' term ')' { $$ = f_new_inst(FI_IP_MASK, $1, $5); }
  | term '.' FIRST { $$ = f_new_inst(FI_AS_PATH_FIRST, $1); }
  | term '.' LAST  { $$ = f_new_inst(FI_AS_PATH_LAST, $1); }
@@ -963,25 +822,18 @@ term:
  ;
 
 break_command:
-   QUITBIRD { $$ = F_QUITBIRD; }
- | ACCEPT { $$ = F_ACCEPT; }
+   ACCEPT { $$ = F_ACCEPT; }
  | REJECT { $$ = F_REJECT; }
  | ERROR { $$ = F_ERROR; }
- | PRINT { $$ = F_NOP; }
- | PRINTN { $$ = F_NONL; }
- ;
-
-print_one:
-   term { $$ = f_new_inst(FI_PRINT, $1); }
  ;
 
 print_list: /* EMPTY */ { $$ = NULL; }
- | print_one { $$ = $1; }
- | print_one ',' print_list {
-     if ($1) {
-       $1->next = $3;
-       $$ = $1;
-     } else $$ = $3;
+ | term { $$ = $1; }
+ | term ',' print_list {
+     ASSERT($1);
+     ASSERT($1->next == NULL);
+     $1->next = $3;
+     $$ = $1;
    }
  ;
 
@@ -995,45 +847,51 @@ cmd:
  | CF_SYM_KNOWN '=' term ';' {
      switch ($1->class) {
        case SYM_VARIABLE_RANGE:
-        $3->result.type = F_LVAL_VARIABLE;
-        $3->result.sym = $1;
+        $$ = f_new_inst(FI_VAR_SET, $3, $1);
         break;
        case SYM_ATTRIBUTE:
-        $3->result.type = F_LVAL_EA;
-        $3->result.da = *$1->attribute;
+        $$ = f_new_inst(FI_EA_SET, $3, *$1->attribute);
         break;
        default:
         cf_error("Can't assign to symbol %s", $1->name);
      }
-     $$ = $3;
    }
  | RETURN term ';' {
      DBG( "Ook, we'll return the value\n" );
-     $2->result.type = F_LVAL_EXCEPTION;
-     $2->result.exception = FE_RETURN;
-     $$ = $2;
+     $$ = f_new_inst(FI_RETURN, $2);
    }
  | dynamic_attr '=' term ';' {
-     $3->result.type = F_LVAL_EA;
-     $3->result.da = $1;
-     $$ = $3;
+     $$ = f_new_inst(FI_EA_SET, $3, $1);
    }
  | static_attr '=' term ';' {
      if ($1.readonly)
        cf_error( "This static attribute is read-only.");
-
-     $3->result.type = F_LVAL_SA;
-     $3->result.sa = $1;
-     $$ = $3;
+     $$ = f_new_inst(FI_RTA_SET, $3, $1);
    }
  | PREFERENCE '=' term ';' {
-     $3->result.type = F_LVAL_PREFERENCE;
-     $$ = $3;
+     $$ = f_new_inst(FI_PREF_SET, $3);
    }
  | UNSET '(' dynamic_attr ')' ';' {
      $$ = f_new_inst(FI_EA_UNSET, $3);
    }
- | break_command print_list ';' { $$ = f_new_inst(FI_PRINT_AND_DIE, $2, $1); }
+ | break_command print_list ';' {
+    struct f_inst *breaker = f_new_inst(FI_DIE, $1);
+    if ($2) {
+      struct f_inst *printer = f_new_inst(FI_PRINT, $2);
+      struct f_inst *flusher = f_new_inst(FI_FLUSH);
+      printer->next = flusher;
+      flusher->next = breaker;
+      $$ = printer;
+    } else
+      $$ = breaker;
+   }
+ | PRINT print_list ';' {
+    $$ = f_new_inst(FI_PRINT, $2);
+    $$->next = f_new_inst(FI_FLUSH);
+   }
+ | PRINTN print_list ';' {
+    $$ = f_new_inst(FI_PRINT, $2);
+   }
  | function_call ';' { $$ = f_new_inst(FI_DROP_RESULT, $1); } 
  | CASE term '{' switch_body '}' {
       $$ = f_new_inst(FI_SWITCH, $2, build_tree($4));