bpf_exp.l 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * BPF asm code lexer
  3. *
  4. * This program is free software; you can distribute it and/or modify
  5. * it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * Syntax kept close to:
  10. *
  11. * Steven McCanne and Van Jacobson. 1993. The BSD packet filter: a new
  12. * architecture for user-level packet capture. In Proceedings of the
  13. * USENIX Winter 1993 Conference Proceedings on USENIX Winter 1993
  14. * Conference Proceedings (USENIX'93). USENIX Association, Berkeley,
  15. * CA, USA, 2-2.
  16. *
  17. * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
  18. * Licensed under the GNU General Public License, version 2.0 (GPLv2)
  19. */
  20. %{
  21. #include <stdio.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include "bpf_exp.yacc.h"
  25. extern void yyerror(const char *str);
  26. %}
  27. %option align
  28. %option ecs
  29. %option nounput
  30. %option noreject
  31. %option noinput
  32. %option noyywrap
  33. %option 8bit
  34. %option caseless
  35. %option yylineno
  36. %%
  37. "ldb" { return OP_LDB; }
  38. "ldh" { return OP_LDH; }
  39. "ld" { return OP_LD; }
  40. "ldi" { return OP_LDI; }
  41. "ldx" { return OP_LDX; }
  42. "ldxi" { return OP_LDXI; }
  43. "ldxb" { return OP_LDXB; }
  44. "st" { return OP_ST; }
  45. "stx" { return OP_STX; }
  46. "jmp" { return OP_JMP; }
  47. "ja" { return OP_JMP; }
  48. "jeq" { return OP_JEQ; }
  49. "jneq" { return OP_JNEQ; }
  50. "jne" { return OP_JNEQ; }
  51. "jlt" { return OP_JLT; }
  52. "jle" { return OP_JLE; }
  53. "jgt" { return OP_JGT; }
  54. "jge" { return OP_JGE; }
  55. "jset" { return OP_JSET; }
  56. "add" { return OP_ADD; }
  57. "sub" { return OP_SUB; }
  58. "mul" { return OP_MUL; }
  59. "div" { return OP_DIV; }
  60. "mod" { return OP_MOD; }
  61. "neg" { return OP_NEG; }
  62. "and" { return OP_AND; }
  63. "xor" { return OP_XOR; }
  64. "or" { return OP_OR; }
  65. "lsh" { return OP_LSH; }
  66. "rsh" { return OP_RSH; }
  67. "ret" { return OP_RET; }
  68. "tax" { return OP_TAX; }
  69. "txa" { return OP_TXA; }
  70. "#"?("len") { return K_PKT_LEN; }
  71. "#"?("proto") { return K_PROTO; }
  72. "#"?("type") { return K_TYPE; }
  73. "#"?("poff") { return K_POFF; }
  74. "#"?("ifidx") { return K_IFIDX; }
  75. "#"?("nla") { return K_NLATTR; }
  76. "#"?("nlan") { return K_NLATTR_NEST; }
  77. "#"?("mark") { return K_MARK; }
  78. "#"?("queue") { return K_QUEUE; }
  79. "#"?("hatype") { return K_HATYPE; }
  80. "#"?("rxhash") { return K_RXHASH; }
  81. "#"?("cpu") { return K_CPU; }
  82. "#"?("vlan_tci") { return K_VLAN_TCI; }
  83. "#"?("vlan_pr") { return K_VLAN_AVAIL; }
  84. "#"?("vlan_avail") { return K_VLAN_AVAIL; }
  85. "#"?("vlan_tpid") { return K_VLAN_TPID; }
  86. "#"?("rand") { return K_RAND; }
  87. ":" { return ':'; }
  88. "," { return ','; }
  89. "#" { return '#'; }
  90. "%" { return '%'; }
  91. "[" { return '['; }
  92. "]" { return ']'; }
  93. "(" { return '('; }
  94. ")" { return ')'; }
  95. "x" { return 'x'; }
  96. "a" { return 'a'; }
  97. "+" { return '+'; }
  98. "M" { return 'M'; }
  99. "*" { return '*'; }
  100. "&" { return '&'; }
  101. ([0][x][a-fA-F0-9]+) {
  102. yylval.number = strtoul(yytext, NULL, 16);
  103. return number;
  104. }
  105. ([0][b][0-1]+) {
  106. yylval.number = strtol(yytext + 2, NULL, 2);
  107. return number;
  108. }
  109. (([0])|([-+]?[1-9][0-9]*)) {
  110. yylval.number = strtol(yytext, NULL, 10);
  111. return number;
  112. }
  113. ([0][0-9]+) {
  114. yylval.number = strtol(yytext + 1, NULL, 8);
  115. return number;
  116. }
  117. [a-zA-Z_][a-zA-Z0-9_]+ {
  118. yylval.label = strdup(yytext);
  119. return label;
  120. }
  121. "/*"([^\*]|\*[^/])*"*/" { /* NOP */ }
  122. ";"[^\n]* { /* NOP */ }
  123. ^#.* { /* NOP */ }
  124. [ \t]+ { /* NOP */ }
  125. [ \n]+ { /* NOP */ }
  126. . {
  127. printf("unknown character \'%s\'", yytext);
  128. yyerror("lex unknown character");
  129. }
  130. %%