bpf_hla: update in parser
[netsniff-ng.git] / src / bpf_hla_parser.y
blob2e5506706299973fb9c93afd4b39fbbdbb62aa4c
1 /*
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.
7 */
9 /*
10 TODO:
11 - intermediate representation
12 - code optimization (symbolic reduction?)
13 - linearization (jumps, etc)
14 - bpf emitter
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <errno.h>
25 #include "bpf.h"
26 #include "bpf_hla_parser.tab.h"
27 #include "bpf_symtab.h"
28 #include "xmalloc.h"
29 #include "xutils.h"
30 #include "built_in.h"
31 #include "die.h"
33 #define YYERROR_VERBOSE 0
34 #define YYDEBUG 0
35 #define YYENABLE_NLS 1
36 #define YYLTYPE_IS_TRIVIAL 1
37 #define ENABLE_NLS 1
39 extern FILE *zzin;
40 extern int zzlex(void);
41 extern void zzerror(const char *);
42 extern int zzlineno;
43 extern char *zztext;
45 int compile_hla_filter(char *file, int verbose, int debug);
47 static unsigned int num_vars = 0;
48 static unsigned int num_ifs = 0;
52 %union {
53 int idx;
54 long int number;
57 %token K_NAME K_DEF K_PKT K_RET K_IF K_ELIF K_ELSE
58 %token K_MACRO_IPV4 K_MACRO_IPV6 K_MACRO_IP K_MACRO_UDP K_MACRO_TCP
60 %token '(' ')' '{' '}' '=' ';' '+' '-' '&' '|' '^' '!' '<' '>' '*' '/' '%' ','
62 %token number_hex number_dec number_oct number_bin
64 %type <idx> K_NAME var
65 %type <number> number_hex number_dec number_oct number_bin number
69 program
70 : declaration_list { printf("_entry:\n"); } statement_list
71 | short_ret /* for short filter statements */
74 declaration_list
75 : declaration { num_vars++; } declaration_list
76 | /* empty */
79 statement_list
80 : statement statement_list
81 | statement
84 declaration
85 : K_DEF K_NAME ';' {
86 if (bpf_symtab_declared($2)) {
87 panic("Variable \"%s\" already declared (l%d)\n",
88 bpf_symtab_name($2), zzlineno);
89 } else {
90 printf("; @var %s\n", bpf_symtab_name($2));
91 bpf_symtab_declare($2);
95 block
96 : condition
99 statement
100 : assignment ';'
101 | return ';'
102 | block
105 short_ret
106 : expression
109 return
110 : K_RET { printf(" ret a\n"); }
111 | K_RET number { printf(" ret #%ld\n", $2); }
112 | K_RET var { printf(" ret a\n"); }
113 | K_RET expression { printf(" ret macro\n"); }
116 macro
117 : K_MACRO_IPV4 { printf("ipv4\n"); }
118 | K_MACRO_IPV6 { printf("ipv6\n"); }
119 | K_MACRO_IP { printf("ip\n"); }
120 | K_MACRO_UDP { printf("udp\n"); }
121 | K_MACRO_TCP { printf("tcp\n"); }
124 condition
125 : { num_ifs++; } K_IF '(' expression ')' '{'
126 { printf("jpt_f%ld:\n", num_ifs); }
127 statement_list '}' condition_contd
130 condition_contd
131 : K_ELIF '(' expression ')' '{' statement_list '}' condition_contd
132 | K_ELSE '{' { printf("jpt_e%ld:\n", num_ifs); } statement_list '}'
133 | /* None */
136 assignment
137 : var '=' expression { printf("; @asn %s\n", bpf_symtab_name($1)); }
138 | var '=' K_PKT '(' number ',' number ')' {
139 switch ($7) {
140 case 1:
141 printf(" ldb [%ld]\n", $5);
142 break;
143 case 2:
144 printf(" ldh [%ld]\n", $5);
145 break;
146 case 4:
147 printf(" ld [%ld]\n", $5);
148 break;
149 default:
150 panic("Invalid argument (l%d)\n", zzlineno);
155 expression
156 : term
157 | '!' term { printf("; @!\n"); }
158 | term '+' term { printf("; @+\n"); }
159 | term '-' 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"); }
175 term
176 : number { printf("; @num %ld\n", $1); }
177 | var { printf("; @var %s\n", bpf_symtab_name($1)); }
178 | macro
179 | '(' expression ')'
183 : K_NAME {
184 if (!bpf_symtab_declared($1))
185 panic("Variable \"%s\" not declared (l%d)\n",
186 bpf_symtab_name($1), zzlineno);
187 $$ = $1; }
190 number
191 : number_dec { $$ = $1; }
192 | number_hex { $$ = $1; }
193 | number_oct { $$ = $1; }
194 | number_bin { $$ = $1; }
199 static void stage_1_compile(void)
201 zzparse();
204 int compile_hla_filter(char *file, int verbose, int debug)
206 int fd;
207 fpos_t pos;
208 char file_tmp[128];
210 if (!strncmp("-", file, strlen("-")))
211 zzin = stdin;
212 else
213 zzin = fopen(file, "r");
214 if (!zzin)
215 panic("Cannot open file!\n");
216 if (!debug) {
217 fd = dup(fileno(stdout));
219 slprintf(file_tmp, sizeof(file_tmp), ".%s", file);
220 if (freopen(file_tmp, "w", stdout) == NULL)
221 panic("Cannot reopen file!\n");
224 stage_1_compile();
226 if (!debug) {
227 fflush(stdout);
228 dup2(fd, fileno(stdout));
230 close(fd);
231 clearerr(stdout);
232 fsetpos(stdout, &pos);
235 fclose(zzin);
237 bpf_symtab_cleanup();
238 if (debug)
239 die();
241 return 0;
244 void zzerror(const char *err)
246 panic("Syntax error at line %d: %s! %s!\n",
247 zzlineno, zztext, err);