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.
9 /* yaac-func-prefix: yy */
20 #include <linux/filter.h>
25 #include "bpf_parser.tab.h"
30 int compile_filter
(char *file
, int verbose
, int bypass
, int format
,
31 bool invoke_cpp
, char *const cpp_argv
[]);
33 static int curr_instr
= 0;
35 static struct sock_filter out
[BPF_MAXINSNS
];
37 static char *labels
[BPF_MAXINSNS
];
38 static char *labels_jt
[BPF_MAXINSNS
];
39 static char *labels_jf
[BPF_MAXINSNS
];
40 static char *labels_k
[BPF_MAXINSNS
];
42 #define YYERROR_VERBOSE 0
44 #define YYENABLE_NLS 1
45 #define YYLTYPE_IS_TRIVIAL 1
49 extern
int yylex(void);
50 extern
void yyerror(const char *);
54 static inline
void check_max_instr
(void)
56 if
(curr_instr
>= BPF_MAXINSNS
)
57 panic
("Exceeded maximal number of instructions!\n");
60 static inline
void set_curr_instr
(uint16_t code
, uint8_t jt
, uint8_t jf
, uint32_t k
)
64 out
[curr_instr
].code
= code
;
65 out
[curr_instr
].jt
= jt
;
66 out
[curr_instr
].jf
= jf
;
67 out
[curr_instr
].k
= k
;
72 static inline
void set_curr_label
(char *label
)
76 labels
[curr_instr
] = label
;
83 static inline
void set_jmp_label
(char *label
, int which
)
89 labels_jt
[curr_instr
] = label
;
92 labels_jf
[curr_instr
] = label
;
95 labels_k
[curr_instr
] = label
;
102 static int find_intr_offset_or_panic
(char *label_to_search
)
104 int i
, max
= curr_instr
, ret
= -ENOENT
;
106 bug_on
(!label_to_search
);
108 for
(i
= 0; i
< max
; ++i
) {
109 if
(labels
[i
] != NULL
) {
110 /* Both are \0-terminated! */
111 if
(!strcmp
(label_to_search
, labels
[i
])) {
119 panic
("No such label!\n");
131 %token OP_LDB OP_LDH OP_LD OP_LDX OP_ST OP_STX OP_JMP OP_JEQ OP_JGT OP_JGE
132 %token OP_JSET OP_ADD OP_SUB OP_MUL OP_DIV OP_AND OP_OR OP_XOR OP_LSH OP_RSH
133 %token OP_RET OP_TAX OP_TXA OP_LDXB OP_MOD OP_NEG OP_JNEQ OP_JLT OP_JLE OP_LDI
136 %token K_PKT_LEN K_PROTO K_TYPE K_NLATTR K_NLATTR_NEST K_MARK K_QUEUE K_HATYPE
137 %token K_RXHASH K_CPU K_IFIDX K_VLANT K_VLANP K_POFF
139 %token
':' ',' '[' ']' '(' ')' 'x' 'a' '+' 'M' '*' '&' '#' '%'
143 %type
<number
> number
196 : label
':' { set_curr_label
($1); }
200 : OP_LDB
'[' 'x' '+' number
']' {
201 set_curr_instr
(BPF_LD | BPF_B | BPF_IND
, 0, 0, $5); }
202 | OP_LDB
'[' '%' 'x' '+' number
']' {
203 set_curr_instr
(BPF_LD | BPF_B | BPF_IND
, 0, 0, $6); }
204 | OP_LDB
'[' number
']' {
205 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0, $3); }
207 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
208 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
210 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
211 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
213 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
214 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
216 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
217 SKF_AD_OFF
+ SKF_AD_NLATTR
); }
218 | OP_LDB K_NLATTR_NEST
{
219 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
220 SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
); }
222 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
223 SKF_AD_OFF
+ SKF_AD_MARK
); }
225 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
226 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
228 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
229 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
231 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
232 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
234 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
235 SKF_AD_OFF
+ SKF_AD_CPU
); }
237 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
238 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
240 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
241 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
243 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
244 SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
); }
248 : OP_LDH
'[' 'x' '+' number
']' {
249 set_curr_instr
(BPF_LD | BPF_H | BPF_IND
, 0, 0, $5); }
250 | OP_LDH
'[' '%' 'x' '+' number
']' {
251 set_curr_instr
(BPF_LD | BPF_H | BPF_IND
, 0, 0, $6); }
252 | OP_LDH
'[' number
']' {
253 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0, $3); }
255 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
256 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
258 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
259 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
261 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
262 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
264 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
265 SKF_AD_OFF
+ SKF_AD_NLATTR
); }
266 | OP_LDH K_NLATTR_NEST
{
267 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
268 SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
); }
270 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
271 SKF_AD_OFF
+ SKF_AD_MARK
); }
273 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
274 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
276 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
277 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
279 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
280 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
282 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
283 SKF_AD_OFF
+ SKF_AD_CPU
); }
285 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
286 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
288 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
289 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
291 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
292 SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
); }
296 : OP_LDI
'#' number
{
297 set_curr_instr
(BPF_LD | BPF_IMM
, 0, 0, $3); }
299 set_curr_instr
(BPF_LD | BPF_IMM
, 0, 0, $2); }
304 set_curr_instr
(BPF_LD | BPF_IMM
, 0, 0, $3); }
306 set_curr_instr
(BPF_LD | BPF_W | BPF_LEN
, 0, 0, 0); }
308 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
309 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
311 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
312 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
314 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
315 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
317 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
318 SKF_AD_OFF
+ SKF_AD_NLATTR
); }
319 | OP_LD K_NLATTR_NEST
{
320 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
321 SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
); }
323 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
324 SKF_AD_OFF
+ SKF_AD_MARK
); }
326 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
327 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
329 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
330 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
332 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
333 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
335 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
336 SKF_AD_OFF
+ SKF_AD_CPU
); }
338 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
339 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
341 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
342 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
344 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
345 SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
); }
346 | OP_LD
'M' '[' number
']' {
347 set_curr_instr
(BPF_LD | BPF_MEM
, 0, 0, $4); }
348 | OP_LD
'[' 'x' '+' number
']' {
349 set_curr_instr
(BPF_LD | BPF_W | BPF_IND
, 0, 0, $5); }
350 | OP_LD
'[' '%' 'x' '+' number
']' {
351 set_curr_instr
(BPF_LD | BPF_W | BPF_IND
, 0, 0, $6); }
352 | OP_LD
'[' number
']' {
353 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0, $3); }
357 : OP_LDXI
'#' number
{
358 set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $3); }
360 set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $2); }
364 : OP_LDX
'#' number
{
365 set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $3); }
367 set_curr_instr
(BPF_LDX | BPF_W | BPF_LEN
, 0, 0, 0); }
368 | OP_LDX
'M' '[' number
']' {
369 set_curr_instr
(BPF_LDX | BPF_MEM
, 0, 0, $4); }
370 | OP_LDXB number
'*' '(' '[' number
']' '&' number
')' {
371 if
($2 != 4 ||
$9 != 0xf) {
372 panic
("ldxb offset not supported!\n");
374 set_curr_instr
(BPF_LDX | BPF_MSH | BPF_B
, 0, 0, $6); } }
375 | OP_LDX number
'*' '(' '[' number
']' '&' number
')' {
376 if
($2 != 4 ||
$9 != 0xf) {
377 panic
("ldxb offset not supported!\n");
379 set_curr_instr
(BPF_LDX | BPF_MSH | BPF_B
, 0, 0, $6); } }
383 : OP_ST
'M' '[' number
']' {
384 set_curr_instr
(BPF_ST
, 0, 0, $4); }
388 : OP_STX
'M' '[' number
']' {
389 set_curr_instr
(BPF_STX
, 0, 0, $4); }
394 set_jmp_label
($2, JKL
);
395 set_curr_instr
(BPF_JMP | BPF_JA
, 0, 0, 0); }
399 : OP_JEQ
'#' number
',' label
',' label
{
400 set_jmp_label
($5, JTL
);
401 set_jmp_label
($7, JFL
);
402 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
403 | OP_JEQ
'x' ',' label
',' label
{
404 set_jmp_label
($4, JTL
);
405 set_jmp_label
($6, JFL
);
406 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
407 | OP_JEQ
'%' 'x' ',' label
',' label
{
408 set_jmp_label
($5, JTL
);
409 set_jmp_label
($7, JFL
);
410 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
411 | OP_JEQ
'#' number
',' label
{
412 set_jmp_label
($5, JTL
);
413 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
414 | OP_JEQ
'x' ',' label
{
415 set_jmp_label
($4, JTL
);
416 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
417 | OP_JEQ
'%' 'x' ',' label
{
418 set_jmp_label
($5, JTL
);
419 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
423 : OP_JNEQ
'#' number
',' label
{
424 set_jmp_label
($5, JFL
);
425 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
426 | OP_JNEQ
'x' ',' label
{
427 set_jmp_label
($4, JFL
);
428 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
429 | OP_JNEQ
'%' 'x' ',' label
{
430 set_jmp_label
($5, JFL
);
431 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
435 : OP_JLT
'#' number
',' label
{
436 set_jmp_label
($5, JFL
);
437 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
438 | OP_JLT
'x' ',' label
{
439 set_jmp_label
($4, JFL
);
440 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
441 | OP_JLT
'%' 'x' ',' label
{
442 set_jmp_label
($5, JFL
);
443 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
447 : OP_JLE
'#' number
',' label
{
448 set_jmp_label
($5, JFL
);
449 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
450 | OP_JLE
'x' ',' label
{
451 set_jmp_label
($4, JFL
);
452 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
453 | OP_JLE
'%' 'x' ',' label
{
454 set_jmp_label
($5, JFL
);
455 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
459 : OP_JGT
'#' number
',' label
',' label
{
460 set_jmp_label
($5, JTL
);
461 set_jmp_label
($7, JFL
);
462 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
463 | OP_JGT
'x' ',' label
',' label
{
464 set_jmp_label
($4, JTL
);
465 set_jmp_label
($6, JFL
);
466 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
467 | OP_JGT
'%' 'x' ',' label
',' label
{
468 set_jmp_label
($5, JTL
);
469 set_jmp_label
($7, JFL
);
470 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
471 | OP_JGT
'#' number
',' label
{
472 set_jmp_label
($5, JTL
);
473 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
474 | OP_JGT
'x' ',' label
{
475 set_jmp_label
($4, JTL
);
476 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
477 | OP_JGT
'%' 'x' ',' label
{
478 set_jmp_label
($5, JTL
);
479 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
483 : OP_JGE
'#' number
',' label
',' label
{
484 set_jmp_label
($5, JTL
);
485 set_jmp_label
($7, JFL
);
486 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
487 | OP_JGE
'x' ',' label
',' label
{
488 set_jmp_label
($4, JTL
);
489 set_jmp_label
($6, JFL
);
490 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
491 | OP_JGE
'%' 'x' ',' label
',' label
{
492 set_jmp_label
($5, JTL
);
493 set_jmp_label
($7, JFL
);
494 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
495 | OP_JGE
'#' number
',' label
{
496 set_jmp_label
($5, JTL
);
497 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
498 | OP_JGE
'x' ',' label
{
499 set_jmp_label
($4, JTL
);
500 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
501 | OP_JGE
'%' 'x' ',' label
{
502 set_jmp_label
($5, JTL
);
503 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
507 : OP_JSET
'#' number
',' label
',' label
{
508 set_jmp_label
($5, JTL
);
509 set_jmp_label
($7, JFL
);
510 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_K
, 0, 0, $3); }
511 | OP_JSET
'x' ',' label
',' label
{
512 set_jmp_label
($4, JTL
);
513 set_jmp_label
($6, JFL
);
514 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
515 | OP_JSET
'%' 'x' ',' label
',' label
{
516 set_jmp_label
($5, JTL
);
517 set_jmp_label
($7, JFL
);
518 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
519 | OP_JSET
'#' number
',' label
{
520 set_jmp_label
($5, JTL
);
521 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_K
, 0, 0, $3); }
522 | OP_JSET
'x' ',' label
{
523 set_jmp_label
($4, JTL
);
524 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
525 | OP_JSET
'%' 'x' ',' label
{
526 set_jmp_label
($5, JTL
);
527 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
531 : OP_ADD
'#' number
{
532 set_curr_instr
(BPF_ALU | BPF_ADD | BPF_K
, 0, 0, $3); }
534 set_curr_instr
(BPF_ALU | BPF_ADD | BPF_X
, 0, 0, 0); }
536 set_curr_instr
(BPF_ALU | BPF_ADD | BPF_X
, 0, 0, 0); }
540 : OP_SUB
'#' number
{
541 set_curr_instr
(BPF_ALU | BPF_SUB | BPF_K
, 0, 0, $3); }
543 set_curr_instr
(BPF_ALU | BPF_SUB | BPF_X
, 0, 0, 0); }
545 set_curr_instr
(BPF_ALU | BPF_SUB | BPF_X
, 0, 0, 0); }
549 : OP_MUL
'#' number
{
550 set_curr_instr
(BPF_ALU | BPF_MUL | BPF_K
, 0, 0, $3); }
552 set_curr_instr
(BPF_ALU | BPF_MUL | BPF_X
, 0, 0, 0); }
554 set_curr_instr
(BPF_ALU | BPF_MUL | BPF_X
, 0, 0, 0); }
558 : OP_DIV
'#' number
{
559 set_curr_instr
(BPF_ALU | BPF_DIV | BPF_K
, 0, 0, $3); }
561 set_curr_instr
(BPF_ALU | BPF_DIV | BPF_X
, 0, 0, 0); }
563 set_curr_instr
(BPF_ALU | BPF_DIV | BPF_X
, 0, 0, 0); }
567 : OP_MOD
'#' number
{
568 set_curr_instr
(BPF_ALU | BPF_MOD | BPF_K
, 0, 0, $3); }
570 set_curr_instr
(BPF_ALU | BPF_MOD | BPF_X
, 0, 0, 0); }
572 set_curr_instr
(BPF_ALU | BPF_MOD | BPF_X
, 0, 0, 0); }
577 set_curr_instr
(BPF_ALU | BPF_NEG
, 0, 0, 0); }
581 : OP_AND
'#' number
{
582 set_curr_instr
(BPF_ALU | BPF_AND | BPF_K
, 0, 0, $3); }
584 set_curr_instr
(BPF_ALU | BPF_AND | BPF_X
, 0, 0, 0); }
586 set_curr_instr
(BPF_ALU | BPF_AND | BPF_X
, 0, 0, 0); }
591 set_curr_instr
(BPF_ALU | BPF_OR | BPF_K
, 0, 0, $3); }
593 set_curr_instr
(BPF_ALU | BPF_OR | BPF_X
, 0, 0, 0); }
595 set_curr_instr
(BPF_ALU | BPF_OR | BPF_X
, 0, 0, 0); }
599 : OP_XOR
'#' number
{
600 set_curr_instr
(BPF_ALU | BPF_XOR | BPF_K
, 0, 0, $3); }
602 set_curr_instr
(BPF_ALU | BPF_XOR | BPF_X
, 0, 0, 0); }
604 set_curr_instr
(BPF_ALU | BPF_XOR | BPF_X
, 0, 0, 0); }
608 : OP_LSH
'#' number
{
609 set_curr_instr
(BPF_ALU | BPF_LSH | BPF_K
, 0, 0, $3); }
611 set_curr_instr
(BPF_ALU | BPF_LSH | BPF_X
, 0, 0, 0); }
613 set_curr_instr
(BPF_ALU | BPF_LSH | BPF_X
, 0, 0, 0); }
617 : OP_RSH
'#' number
{
618 set_curr_instr
(BPF_ALU | BPF_RSH | BPF_K
, 0, 0, $3); }
620 set_curr_instr
(BPF_ALU | BPF_RSH | BPF_X
, 0, 0, 0); }
622 set_curr_instr
(BPF_ALU | BPF_RSH | BPF_X
, 0, 0, 0); }
627 set_curr_instr
(BPF_RET | BPF_A
, 0, 0, 0); }
629 set_curr_instr
(BPF_RET | BPF_A
, 0, 0, 0); }
631 set_curr_instr
(BPF_RET | BPF_X
, 0, 0, 0); }
633 set_curr_instr
(BPF_RET | BPF_X
, 0, 0, 0); }
634 | OP_RET
'#' number
{
635 set_curr_instr
(BPF_RET | BPF_K
, 0, 0, $3); }
640 set_curr_instr
(BPF_MISC | BPF_TAX
, 0, 0, 0); }
645 set_curr_instr
(BPF_MISC | BPF_TXA
, 0, 0, 0); }
650 static void stage_1_inline
(void)
655 static void stage_2_label_reduce
(void)
657 int i
, max
= curr_instr
, off
;
659 /* 1. reduce k jumps */
660 for
(i
= 0; i
< max
; ++i
) {
661 if
(labels_k
[i
] != NULL
) {
662 off
= find_intr_offset_or_panic
(labels_k
[i
]);
663 out
[i
].k
= (uint32_t) (off
- i
- 1);
667 /* 1. reduce jt jumps */
668 for
(i
= 0; i
< max
; ++i
) {
669 if
(labels_jt
[i
] != NULL
) {
670 off
= find_intr_offset_or_panic
(labels_jt
[i
]);
671 out
[i
].jt
= (uint8_t) (off
- i
-1);
675 /* 1. reduce jf jumps */
676 for
(i
= 0; i
< max
; ++i
) {
677 if
(labels_jf
[i
] != NULL
) {
678 off
= find_intr_offset_or_panic
(labels_jf
[i
]);
679 out
[i
].jf
= (uint8_t) (off
- i
- 1);
684 static void pretty_printer_c
(const struct sock_fprog
*prog
)
688 for
(i
= 0; i
< prog
->len
; ++i
) {
689 printf
("{ 0x%x, %u, %u, 0x%08x },\n",
690 prog
->filter
[i
].code
, prog
->filter
[i
].jt
,
691 prog
->filter
[i
].jf
, prog
->filter
[i
].k
);
695 static void pretty_printer_xt_bpf
(const struct sock_fprog
*prog
)
699 printf
("%d,", prog
->len
);
700 for
(i
= 0; i
< prog
->len
; ++i
) {
701 printf
("%u %u %u %u,",
702 prog
->filter
[i
].code
, prog
->filter
[i
].jt
,
703 prog
->filter
[i
].jf
, prog
->filter
[i
].k
);
709 static void pretty_printer_tcpdump
(const struct sock_fprog
*prog
)
713 for
(i
= 0; i
< prog
->len
; ++i
) {
714 printf
("%u %u %u %u\n",
715 prog
->filter
[i
].code
, prog
->filter
[i
].jt
,
716 prog
->filter
[i
].jf
, prog
->filter
[i
].k
);
720 static void pretty_printer
(const struct sock_fprog
*prog
, int format
)
724 pretty_printer_c
(prog
);
727 pretty_printer_xt_bpf
(prog
);
730 pretty_printer_tcpdump
(prog
);
737 int compile_filter
(char *file
, int verbose
, int bypass
, int format
,
738 bool invoke_cpp
, char *const cpp_argv
[])
741 struct sock_fprog res
;
745 memset
(tmp_file
, 0, sizeof
(tmp_file
));
748 ret
= cpp_exec
(file
, tmp_file
, sizeof
(tmp_file
), cpp_argv
);
750 fprintf
(stderr
, "Failed to invoke C preprocessor!\n");
757 if
(!strncmp
("-", file
, strlen
("-")))
760 yyin
= fopen
(file
, "r");
762 panic
("Cannot open file!\n");
764 memset
(out
, 0, sizeof
(out
));
765 memset
(labels
, 0, sizeof
(labels
));
766 memset
(labels_jf
, 0, sizeof
(labels_jf
));
767 memset
(labels_jt
, 0, sizeof
(labels_jt
));
768 memset
(labels_k
, 0, sizeof
(labels_k
));
771 stage_2_label_reduce
();
774 res.len
= curr_instr
;
777 printf
("Generated program:\n");
783 printf
("Validating: ");
787 if
(__bpf_validate
(&res
) == 0) {
789 printf
("Semantic error! BPF validation failed!\n");
791 printf
("Semantic error! BPF validation failed! "
792 "Try -V for debugging output!\n");
796 } else if
(verbose
) {
797 printf
("is runnable!\n");
804 pretty_printer
(&res
, format
);
806 for
(i
= 0; i
< res.len
; ++i
) {
813 if
(yyin
&& yyin
!= stdin
)
823 void yyerror(const char *err
)
825 panic
("Syntax error at line %d: %s! %s!\n",
826 yylineno
, yytext
, err
);