bpf: use Linux' define of BPF_MAXINSNS
[netsniff-ng.git] / bpf_parser.y
bloba101057b90e935c6427decdcd5f66b8cac8d5a4f
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
7 */
9 /* yaac-func-prefix: yy */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <signal.h>
17 #include <stdint.h>
18 #include <errno.h>
19 #include <libgen.h>
20 #include <linux/filter.h>
22 #include "bpf.h"
23 #include "str.h"
24 #include "xmalloc.h"
25 #include "bpf_parser.tab.h"
26 #include "built_in.h"
27 #include "die.h"
29 int compile_filter(char *file, int verbose, int bypass, int format,
30 bool invoke_cpp);
32 static int curr_instr = 0;
34 static struct sock_filter out[BPF_MAXINSNS];
36 static char *labels[BPF_MAXINSNS];
37 static char *labels_jt[BPF_MAXINSNS];
38 static char *labels_jf[BPF_MAXINSNS];
39 static char *labels_k[BPF_MAXINSNS];
41 #define YYERROR_VERBOSE 0
42 #define YYDEBUG 0
43 #define YYENABLE_NLS 1
44 #define YYLTYPE_IS_TRIVIAL 1
45 #define ENABLE_NLS 1
47 extern FILE *yyin;
48 extern int yylex(void);
49 extern void yyerror(const char *);
50 extern int yylineno;
51 extern char *yytext;
53 static inline void check_max_instr(void)
55 if (curr_instr >= BPF_MAXINSNS)
56 panic("Exceeded maximal number of instructions!\n");
59 static inline void set_curr_instr(uint16_t code, uint8_t jt, uint8_t jf, uint32_t k)
61 check_max_instr();
63 out[curr_instr].code = code;
64 out[curr_instr].jt = jt;
65 out[curr_instr].jf = jf;
66 out[curr_instr].k = k;
68 curr_instr++;
71 static inline void set_curr_label(char *label)
73 check_max_instr();
75 labels[curr_instr] = label;
78 #define JTL 1
79 #define JFL 2
80 #define JKL 3
82 static inline void set_jmp_label(char *label, int which)
84 check_max_instr();
86 switch (which) {
87 case JTL:
88 labels_jt[curr_instr] = label;
89 break;
90 case JFL:
91 labels_jf[curr_instr] = label;
92 break;
93 case JKL:
94 labels_k[curr_instr] = label;
95 break;
96 default:
97 bug();
101 static int find_intr_offset_or_panic(char *label_to_search)
103 int i, max = curr_instr, ret = -ENOENT;
105 bug_on(!label_to_search);
107 for (i = 0; i < max; ++i) {
108 if (labels[i] != NULL) {
109 /* Both are \0-terminated! */
110 if (!strcmp(label_to_search, labels[i])) {
111 ret = i;
112 break;
117 if (ret == -ENOENT)
118 panic("No such label!\n");
120 return ret;
125 %union {
126 char *label;
127 long int number;
130 %token OP_LDB OP_LDH OP_LD OP_LDX OP_ST OP_STX OP_JMP OP_JEQ OP_JGT OP_JGE
131 %token OP_JSET OP_ADD OP_SUB OP_MUL OP_DIV OP_AND OP_OR OP_XOR OP_LSH OP_RSH
132 %token OP_RET OP_TAX OP_TXA OP_LDXB OP_MOD OP_NEG OP_JNEQ OP_JLT OP_JLE OP_LDI
133 %token OP_LDXI
135 %token K_PKT_LEN K_PROTO K_TYPE K_NLATTR K_NLATTR_NEST K_MARK K_QUEUE K_HATYPE
136 %token K_RXHASH K_CPU K_IFIDX K_VLANT K_VLANP K_POFF
138 %token ':' ',' '[' ']' '(' ')' 'x' 'a' '+' 'M' '*' '&' '#'
140 %token number label
142 %type <number> number
143 %type <label> label
147 prog
148 : line
149 | prog line
152 line
153 : instr
154 | labelled_instr
157 labelled_instr
158 : labelled instr
161 instr
162 : ldb
163 | ldh
164 | ld
165 | ldi
166 | ldx
167 | ldxi
168 | st
169 | stx
170 | jmp
171 | jeq
172 | jneq
173 | jlt
174 | jle
175 | jgt
176 | jge
177 | jset
178 | add
179 | sub
180 | mul
181 | div
182 | mod
183 | neg
184 | and
185 | or
186 | xor
187 | lsh
188 | rsh
189 | ret
190 | tax
191 | txa
194 labelled
195 : label ':' { set_curr_label($1); }
199 : OP_LDB '[' 'x' '+' number ']' {
200 set_curr_instr(BPF_LD | BPF_B | BPF_IND, 0, 0, $5); }
201 | OP_LDB '[' number ']' {
202 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0, $3); }
203 | OP_LDB K_PROTO {
204 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
205 SKF_AD_OFF + SKF_AD_PROTOCOL); }
206 | OP_LDB K_TYPE {
207 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
208 SKF_AD_OFF + SKF_AD_PKTTYPE); }
209 | OP_LDB K_IFIDX {
210 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
211 SKF_AD_OFF + SKF_AD_IFINDEX); }
212 | OP_LDB K_NLATTR {
213 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
214 SKF_AD_OFF + SKF_AD_NLATTR); }
215 | OP_LDB K_NLATTR_NEST {
216 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
217 SKF_AD_OFF + SKF_AD_NLATTR_NEST); }
218 | OP_LDB K_MARK {
219 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
220 SKF_AD_OFF + SKF_AD_MARK); }
221 | OP_LDB K_QUEUE {
222 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
223 SKF_AD_OFF + SKF_AD_QUEUE); }
224 | OP_LDB K_HATYPE {
225 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
226 SKF_AD_OFF + SKF_AD_HATYPE); }
227 | OP_LDB K_RXHASH {
228 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
229 SKF_AD_OFF + SKF_AD_RXHASH); }
230 | OP_LDB K_CPU {
231 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
232 SKF_AD_OFF + SKF_AD_CPU); }
233 | OP_LDB K_VLANT {
234 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
235 SKF_AD_OFF + SKF_AD_VLAN_TAG); }
236 | OP_LDB K_VLANP {
237 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
238 SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); }
239 | OP_LDB K_POFF {
240 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
241 SKF_AD_OFF + SKF_AD_PAY_OFFSET); }
245 : OP_LDH '[' 'x' '+' number ']' {
246 set_curr_instr(BPF_LD | BPF_H | BPF_IND, 0, 0, $5); }
247 | OP_LDH '[' number ']' {
248 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0, $3); }
249 | OP_LDH K_PROTO {
250 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
251 SKF_AD_OFF + SKF_AD_PROTOCOL); }
252 | OP_LDH K_TYPE {
253 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
254 SKF_AD_OFF + SKF_AD_PKTTYPE); }
255 | OP_LDH K_IFIDX {
256 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
257 SKF_AD_OFF + SKF_AD_IFINDEX); }
258 | OP_LDH K_NLATTR {
259 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
260 SKF_AD_OFF + SKF_AD_NLATTR); }
261 | OP_LDH K_NLATTR_NEST {
262 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
263 SKF_AD_OFF + SKF_AD_NLATTR_NEST); }
264 | OP_LDH K_MARK {
265 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
266 SKF_AD_OFF + SKF_AD_MARK); }
267 | OP_LDH K_QUEUE {
268 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
269 SKF_AD_OFF + SKF_AD_QUEUE); }
270 | OP_LDH K_HATYPE {
271 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
272 SKF_AD_OFF + SKF_AD_HATYPE); }
273 | OP_LDH K_RXHASH {
274 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
275 SKF_AD_OFF + SKF_AD_RXHASH); }
276 | OP_LDH K_CPU {
277 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
278 SKF_AD_OFF + SKF_AD_CPU); }
279 | OP_LDH K_VLANT {
280 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
281 SKF_AD_OFF + SKF_AD_VLAN_TAG); }
282 | OP_LDH K_VLANP {
283 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
284 SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); }
285 | OP_LDH K_POFF {
286 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
287 SKF_AD_OFF + SKF_AD_PAY_OFFSET); }
291 : OP_LDI number {
292 set_curr_instr(BPF_LD | BPF_IMM, 0, 0, $2); }
296 : OP_LD '#' number {
297 set_curr_instr(BPF_LD | BPF_IMM, 0, 0, $3); }
298 | OP_LD K_PKT_LEN {
299 set_curr_instr(BPF_LD | BPF_W | BPF_LEN, 0, 0, 0); }
300 | OP_LD K_PROTO {
301 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
302 SKF_AD_OFF + SKF_AD_PROTOCOL); }
303 | OP_LD K_TYPE {
304 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
305 SKF_AD_OFF + SKF_AD_PKTTYPE); }
306 | OP_LD K_IFIDX {
307 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
308 SKF_AD_OFF + SKF_AD_IFINDEX); }
309 | OP_LD K_NLATTR {
310 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
311 SKF_AD_OFF + SKF_AD_NLATTR); }
312 | OP_LD K_NLATTR_NEST {
313 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
314 SKF_AD_OFF + SKF_AD_NLATTR_NEST); }
315 | OP_LD K_MARK {
316 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
317 SKF_AD_OFF + SKF_AD_MARK); }
318 | OP_LD K_QUEUE {
319 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
320 SKF_AD_OFF + SKF_AD_QUEUE); }
321 | OP_LD K_HATYPE {
322 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
323 SKF_AD_OFF + SKF_AD_HATYPE); }
324 | OP_LD K_RXHASH {
325 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
326 SKF_AD_OFF + SKF_AD_RXHASH); }
327 | OP_LD K_CPU {
328 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
329 SKF_AD_OFF + SKF_AD_CPU); }
330 | OP_LD K_VLANT {
331 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
332 SKF_AD_OFF + SKF_AD_VLAN_TAG); }
333 | OP_LD K_VLANP {
334 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
335 SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); }
336 | OP_LD K_POFF {
337 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
338 SKF_AD_OFF + SKF_AD_PAY_OFFSET); }
339 | OP_LD 'M' '[' number ']' {
340 set_curr_instr(BPF_LD | BPF_MEM, 0, 0, $4); }
341 | OP_LD '[' 'x' '+' number ']' {
342 set_curr_instr(BPF_LD | BPF_W | BPF_IND, 0, 0, $5); }
343 | OP_LD '[' number ']' {
344 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0, $3); }
347 ldxi
348 : OP_LDXI number {
349 set_curr_instr(BPF_LDX | BPF_IMM, 0, 0, $2); }
353 : OP_LDX '#' number {
354 set_curr_instr(BPF_LDX | BPF_IMM, 0, 0, $3); }
355 | OP_LDX K_PKT_LEN {
356 set_curr_instr(BPF_LDX | BPF_W | BPF_LEN, 0, 0, 0); }
357 | OP_LDX 'M' '[' number ']' {
358 set_curr_instr(BPF_LDX | BPF_MEM, 0, 0, $4); }
359 | OP_LDXB number '*' '(' '[' number ']' '&' number ')' {
360 if ($2 != 4 || $9 != 0xf) {
361 panic("ldxb offset not supported!\n");
362 } else {
363 set_curr_instr(BPF_LDX | BPF_MSH | BPF_B, 0, 0, $6); } }
364 | OP_LDX number '*' '(' '[' number ']' '&' number ')' {
365 if ($2 != 4 || $9 != 0xf) {
366 panic("ldxb offset not supported!\n");
367 } else {
368 set_curr_instr(BPF_LDX | BPF_MSH | BPF_B, 0, 0, $6); } }
372 : OP_ST 'M' '[' number ']' {
373 set_curr_instr(BPF_ST, 0, 0, $4); }
377 : OP_STX 'M' '[' number ']' {
378 set_curr_instr(BPF_STX, 0, 0, $4); }
382 : OP_JMP label {
383 set_jmp_label($2, JKL);
384 set_curr_instr(BPF_JMP | BPF_JA, 0, 0, 0); }
388 : OP_JEQ '#' number ',' label ',' label {
389 set_jmp_label($5, JTL);
390 set_jmp_label($7, JFL);
391 set_curr_instr(BPF_JMP | BPF_JEQ | BPF_K, 0, 0, $3); }
392 | OP_JEQ 'x' ',' label ',' label {
393 set_jmp_label($4, JTL);
394 set_jmp_label($6, JFL);
395 set_curr_instr(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 0); }
396 | OP_JEQ '#' number ',' label {
397 set_jmp_label($5, JTL);
398 set_curr_instr(BPF_JMP | BPF_JEQ | BPF_K, 0, 0, $3); }
399 | OP_JEQ 'x' ',' label {
400 set_jmp_label($4, JTL);
401 set_curr_instr(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 0); }
404 jneq
405 : OP_JNEQ '#' number ',' label {
406 set_jmp_label($5, JFL);
407 set_curr_instr(BPF_JMP | BPF_JEQ | BPF_K, 0, 0, $3); }
408 | OP_JNEQ 'x' ',' label {
409 set_jmp_label($4, JFL);
410 set_curr_instr(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 0); }
414 : OP_JLT '#' number ',' label {
415 set_jmp_label($5, JFL);
416 set_curr_instr(BPF_JMP | BPF_JGE | BPF_K, 0, 0, $3); }
417 | OP_JLT 'x' ',' label {
418 set_jmp_label($4, JFL);
419 set_curr_instr(BPF_JMP | BPF_JGE | BPF_X, 0, 0, 0); }
423 : OP_JLE '#' number ',' label {
424 set_jmp_label($5, JFL);
425 set_curr_instr(BPF_JMP | BPF_JGT | BPF_K, 0, 0, $3); }
426 | OP_JLE 'x' ',' label {
427 set_jmp_label($4, JFL);
428 set_curr_instr(BPF_JMP | BPF_JGT | BPF_X, 0, 0, 0); }
432 : OP_JGT '#' number ',' label ',' label {
433 set_jmp_label($5, JTL);
434 set_jmp_label($7, JFL);
435 set_curr_instr(BPF_JMP | BPF_JGT | BPF_K, 0, 0, $3); }
436 | OP_JGT 'x' ',' label ',' label {
437 set_jmp_label($4, JTL);
438 set_jmp_label($6, JFL);
439 set_curr_instr(BPF_JMP | BPF_JGT | BPF_X, 0, 0, 0); }
440 | OP_JGT '#' number ',' label {
441 set_jmp_label($5, JTL);
442 set_curr_instr(BPF_JMP | BPF_JGT | BPF_K, 0, 0, $3); }
443 | OP_JGT 'x' ',' label {
444 set_jmp_label($4, JTL);
445 set_curr_instr(BPF_JMP | BPF_JGT | BPF_X, 0, 0, 0); }
449 : OP_JGE '#' number ',' label ',' label {
450 set_jmp_label($5, JTL);
451 set_jmp_label($7, JFL);
452 set_curr_instr(BPF_JMP | BPF_JGE | BPF_K, 0, 0, $3); }
453 | OP_JGE 'x' ',' label ',' label {
454 set_jmp_label($4, JTL);
455 set_jmp_label($6, JFL);
456 set_curr_instr(BPF_JMP | BPF_JGE | BPF_X, 0, 0, 0); }
457 | OP_JGE '#' number ',' label {
458 set_jmp_label($5, JTL);
459 set_curr_instr(BPF_JMP | BPF_JGE | BPF_K, 0, 0, $3); }
460 | OP_JGE 'x' ',' label {
461 set_jmp_label($4, JTL);
462 set_curr_instr(BPF_JMP | BPF_JGE | BPF_X, 0, 0, 0); }
465 jset
466 : OP_JSET '#' number ',' label ',' label {
467 set_jmp_label($5, JTL);
468 set_jmp_label($7, JFL);
469 set_curr_instr(BPF_JMP | BPF_JSET | BPF_K, 0, 0, $3); }
470 | OP_JSET 'x' ',' label ',' label {
471 set_jmp_label($4, JTL);
472 set_jmp_label($6, JFL);
473 set_curr_instr(BPF_JMP | BPF_JSET | BPF_X, 0, 0, 0); }
474 | OP_JSET '#' number ',' label {
475 set_jmp_label($5, JTL);
476 set_curr_instr(BPF_JMP | BPF_JSET | BPF_K, 0, 0, $3); }
477 | OP_JSET 'x' ',' label {
478 set_jmp_label($4, JTL);
479 set_curr_instr(BPF_JMP | BPF_JSET | BPF_X, 0, 0, 0); }
483 : OP_ADD '#' number {
484 set_curr_instr(BPF_ALU | BPF_ADD | BPF_K, 0, 0, $3); }
485 | OP_ADD 'x' {
486 set_curr_instr(BPF_ALU | BPF_ADD | BPF_X, 0, 0, 0); }
490 : OP_SUB '#' number {
491 set_curr_instr(BPF_ALU | BPF_SUB | BPF_K, 0, 0, $3); }
492 | OP_SUB 'x' {
493 set_curr_instr(BPF_ALU | BPF_SUB | BPF_X, 0, 0, 0); }
497 : OP_MUL '#' number {
498 set_curr_instr(BPF_ALU | BPF_MUL | BPF_K, 0, 0, $3); }
499 | OP_MUL 'x' {
500 set_curr_instr(BPF_ALU | BPF_MUL | BPF_X, 0, 0, 0); }
504 : OP_DIV '#' number {
505 set_curr_instr(BPF_ALU | BPF_DIV | BPF_K, 0, 0, $3); }
506 | OP_DIV 'x' {
507 set_curr_instr(BPF_ALU | BPF_DIV | BPF_X, 0, 0, 0); }
511 : OP_MOD '#' number {
512 set_curr_instr(BPF_ALU | BPF_MOD | BPF_K, 0, 0, $3); }
513 | OP_MOD 'x' {
514 set_curr_instr(BPF_ALU | BPF_MOD | BPF_X, 0, 0, 0); }
518 : OP_NEG {
519 set_curr_instr(BPF_ALU | BPF_NEG, 0, 0, 0); }
523 : OP_AND '#' number {
524 set_curr_instr(BPF_ALU | BPF_AND | BPF_K, 0, 0, $3); }
525 | OP_AND 'x' {
526 set_curr_instr(BPF_ALU | BPF_AND | BPF_X, 0, 0, 0); }
530 : OP_OR '#' number {
531 set_curr_instr(BPF_ALU | BPF_OR | BPF_K, 0, 0, $3); }
532 | OP_OR 'x' {
533 set_curr_instr(BPF_ALU | BPF_OR | BPF_X, 0, 0, 0); }
537 : OP_XOR '#' number {
538 set_curr_instr(BPF_ALU | BPF_XOR | BPF_K, 0, 0, $3); }
539 | OP_XOR 'x' {
540 set_curr_instr(BPF_ALU | BPF_XOR | BPF_X, 0, 0, 0); }
544 : OP_LSH '#' number {
545 set_curr_instr(BPF_ALU | BPF_LSH | BPF_K, 0, 0, $3); }
546 | OP_LSH 'x' {
547 set_curr_instr(BPF_ALU | BPF_LSH | BPF_X, 0, 0, 0); }
551 : OP_RSH '#' number {
552 set_curr_instr(BPF_ALU | BPF_RSH | BPF_K, 0, 0, $3); }
553 | OP_RSH 'x' {
554 set_curr_instr(BPF_ALU | BPF_RSH | BPF_X, 0, 0, 0); }
558 : OP_RET 'a' {
559 set_curr_instr(BPF_RET | BPF_A, 0, 0, 0); }
560 | OP_RET 'x' {
561 set_curr_instr(BPF_RET | BPF_X, 0, 0, 0); }
562 | OP_RET '#' number {
563 set_curr_instr(BPF_RET | BPF_K, 0, 0, $3); }
567 : OP_TAX {
568 set_curr_instr(BPF_MISC | BPF_TAX, 0, 0, 0); }
572 : OP_TXA {
573 set_curr_instr(BPF_MISC | BPF_TXA, 0, 0, 0); }
578 static void stage_1_inline(void)
580 yyparse();
583 static void stage_2_label_reduce(void)
585 int i, max = curr_instr, off;
587 /* 1. reduce k jumps */
588 for (i = 0; i < max; ++i) {
589 if (labels_k[i] != NULL) {
590 off = find_intr_offset_or_panic(labels_k[i]);
591 out[i].k = (uint32_t) (off - i - 1);
595 /* 1. reduce jt jumps */
596 for (i = 0; i < max; ++i) {
597 if (labels_jt[i] != NULL) {
598 off = find_intr_offset_or_panic(labels_jt[i]);
599 out[i].jt = (uint8_t) (off - i -1);
603 /* 1. reduce jf jumps */
604 for (i = 0; i < max; ++i) {
605 if (labels_jf[i] != NULL) {
606 off = find_intr_offset_or_panic(labels_jf[i]);
607 out[i].jf = (uint8_t) (off - i - 1);
612 static void pretty_printer_c(const struct sock_fprog *prog)
614 int i;
616 for (i = 0; i < prog->len; ++i) {
617 printf("{ 0x%x, %u, %u, 0x%08x },\n",
618 prog->filter[i].code, prog->filter[i].jt,
619 prog->filter[i].jf, prog->filter[i].k);
623 static void pretty_printer_xt_bpf(const struct sock_fprog *prog)
625 int i;
627 printf("%d,", prog->len);
628 for (i = 0; i < prog->len; ++i) {
629 printf("%u %u %u %u,",
630 prog->filter[i].code, prog->filter[i].jt,
631 prog->filter[i].jf, prog->filter[i].k);
634 fflush(stdout);
637 static void pretty_printer_tcpdump(const struct sock_fprog *prog)
639 int i;
641 for (i = 0; i < prog->len; ++i) {
642 printf("%u %u %u %u\n",
643 prog->filter[i].code, prog->filter[i].jt,
644 prog->filter[i].jf, prog->filter[i].k);
648 static void pretty_printer(const struct sock_fprog *prog, int format)
650 switch (format) {
651 case 0:
652 pretty_printer_c(prog);
653 break;
654 case 1:
655 pretty_printer_xt_bpf(prog);
656 break;
657 case 2:
658 pretty_printer_tcpdump(prog);
659 break;
660 default:
661 bug();
665 int compile_filter(char *file, int verbose, int bypass, int format,
666 bool invoke_cpp)
668 int i;
669 struct sock_fprog res;
670 char tmp_file[128];
672 memset(tmp_file, 0, sizeof(tmp_file));
674 if (invoke_cpp) {
675 char cmd[256], *dir, *base, *a, *b;
677 dir = dirname((a = xstrdup(file)));
678 base = basename((b = xstrdup(file)));
680 slprintf(tmp_file, sizeof(tmp_file), "%s/.tmp-%u-%s", dir, rand(), base);
681 slprintf(cmd, sizeof(cmd), "cpp -I" PREFIX_STRING
682 "/etc/netsniff-ng/ %s > %s", file, tmp_file);
683 system(cmd);
685 file = tmp_file;
686 xfree(a);
687 xfree(b);
690 if (!strncmp("-", file, strlen("-")))
691 yyin = stdin;
692 else
693 yyin = fopen(file, "r");
694 if (!yyin)
695 panic("Cannot open file!\n");
697 memset(out, 0, sizeof(out));
698 memset(labels, 0, sizeof(labels));
699 memset(labels_jf, 0, sizeof(labels_jf));
700 memset(labels_jt, 0, sizeof(labels_jt));
701 memset(labels_k, 0, sizeof(labels_k));
703 stage_1_inline();
704 stage_2_label_reduce();
706 res.filter = out;
707 res.len = curr_instr;
709 if (verbose) {
710 printf("Generated program:\n");
711 bpf_dump_all(&res);
714 if (!bypass) {
715 if (verbose) {
716 printf("Validating: ");
717 fflush(stdout);
720 if (__bpf_validate(&res) == 0) {
721 if (verbose)
722 printf("Semantic error! BPF validation failed!\n");
723 else
724 panic("Semantic error! BPF validation failed! "
725 "Try -V for debugging output!\n");
726 } else if (verbose) {
727 printf("is runnable!\n");
731 if (verbose)
732 printf("Result:\n");
734 pretty_printer(&res, format);
736 for (i = 0; i < res.len; ++i) {
737 free(labels[i]);
738 free(labels_jt[i]);
739 free(labels_jf[i]);
740 free(labels_k[i]);
743 fclose(yyin);
744 if (invoke_cpp)
745 unlink(tmp_file);
747 return 0;
750 void yyerror(const char *err)
752 panic("Syntax error at line %d: %s! %s!\n",
753 yylineno, yytext, err);