2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
9 /* yaac-func-prefix: zz */
13 - intermediate representation
14 - code optimization (symbolic reduction?)
15 - linearization (jumps, etc)
28 #include "bpf_hla_parser.tab.h"
29 #include "bpf_symtab.h"
35 #define YYERROR_VERBOSE 0
37 #define YYENABLE_NLS 1
38 #define YYLTYPE_IS_TRIVIAL 1
42 extern
int zzlex
(void);
43 extern
void zzerror
(const char *);
47 int compile_hla_filter
(char *file
, int verbose
, int debug
);
49 static unsigned int num_vars
= 0;
50 static unsigned int num_ifs
= 0;
59 %token K_NAME K_DEF K_PKT K_RET K_IF K_ELIF K_ELSE
60 %token K_MACRO_IPV4 K_MACRO_IPV6 K_MACRO_IP K_MACRO_UDP K_MACRO_TCP
64 %token
'(' ')' '{' '}' '=' ';' '+' '-' '&' '|' '^' '!' '<' '>' '*' '/' '%' ','
66 %type
<idx
> K_NAME var
72 : declaration_list
{ printf
("_entry:\n"); } statement_list
73 | short_ret
/* for short filter statements */
77 : declaration
{ num_vars
++; } declaration_list
82 : statement statement_list
88 if
(bpf_symtab_declared
($2)) {
89 panic
("Variable \"%s\" already declared (l%d)\n",
90 bpf_symtab_name
($2), zzlineno
);
92 printf
("; @var %s\n", bpf_symtab_name
($2));
93 bpf_symtab_declare
($2);
112 : K_RET
{ printf
(" ret a\n"); }
113 | K_RET number
{ printf
(" ret #%ld\n", $2); }
114 | K_RET var
{ printf
(" ret a\n"); }
115 | K_RET expression
{ printf
(" ret macro\n"); }
119 : K_MACRO_IPV4
{ printf
("ipv4\n"); }
120 | K_MACRO_IPV6
{ printf
("ipv6\n"); }
121 | K_MACRO_IP
{ printf
("ip\n"); }
122 | K_MACRO_UDP
{ printf
("udp\n"); }
123 | K_MACRO_TCP
{ printf
("tcp\n"); }
127 : { num_ifs
++; } K_IF
'(' expression
')' '{'
128 { printf
("jpt_f%u:\n", num_ifs
); }
129 statement_list
'}' condition_contd
133 : K_ELIF
'(' expression
')' '{' statement_list
'}' condition_contd
134 | K_ELSE
'{' { printf
("jpt_e%u:\n", num_ifs
); } statement_list
'}'
139 : var
'=' expression
{ printf
("; @asn %s\n", bpf_symtab_name
($1)); }
140 | var
'=' K_PKT
'(' number
',' number
')' {
143 printf
(" ldb [%ld]\n", $5);
146 printf
(" ldh [%ld]\n", $5);
149 printf
(" ld [%ld]\n", $5);
152 panic
("Invalid argument (l%d)\n", zzlineno
);
159 |
'!' term
{ printf
("; @!\n"); }
160 | term
'+' term
{ printf
("; @+\n"); }
161 | term
'-' term
{ printf
("; @-\n"); }
162 | term
'/' term
{ printf
("; @/\n"); }
163 | term
'*' term
{ printf
("; @*\n"); }
164 | term
'%' term
{ printf
("; @\n"); }
165 | term
'&' term
{ printf
("; @&\n"); }
166 | term
'|' term
{ printf
("; @|\n"); }
167 | term
'^' term
{ printf
("; @^\n"); }
168 | term
'<' term
{ printf
("; @<\n"); }
169 | term
'>' term
{ printf
("; @>\n"); }
170 | term
'=' '=' term
{ printf
("; @==\n"); }
171 | term
'&' '&' term
{ printf
("; @&&\n"); }
172 | term
'|' '|' term
{ printf
("; @||\n"); }
173 | term
'<' '<' term
{ printf
("; @<<\n"); }
174 | term
'>' '>' term
{ printf
("; @>>\n"); }
178 : number
{ printf
("; @num %ld\n", $1); }
179 | var
{ printf
("; @var %s\n", bpf_symtab_name
($1)); }
186 if
(!bpf_symtab_declared
($1))
187 panic
("Variable \"%s\" not declared (l%d)\n",
188 bpf_symtab_name
($1), zzlineno
);
194 static void stage_1_compile
(void)
199 int compile_hla_filter
(char *file
, int verbose
, int debug
)
205 if
(!strncmp
("-", file
, strlen
("-")))
208 zzin
= fopen
(file
, "r");
210 panic
("Cannot open file!\n");
212 fd
= dup
(fileno
(stdout
));
214 slprintf
(file_tmp
, sizeof
(file_tmp
), ".%s", file
);
215 if
(freopen
(file_tmp
, "w", stdout
) == NULL
)
216 panic
("Cannot reopen file!\n");
223 dup2
(fd
, fileno
(stdout
));
227 fsetpos
(stdout
, &pos
);
232 bpf_symtab_cleanup
();
239 void zzerror
(const char *err
)
241 panic
("Syntax error at line %d: %s! %s!\n",
242 zzlineno
, zztext
, err
);