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"
29 int compile_filter
(char *file
, int verbose
, int bypass
, int format
,
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
43 #define YYENABLE_NLS 1
44 #define YYLTYPE_IS_TRIVIAL 1
48 extern
int yylex(void);
49 extern
void yyerror(const char *);
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
)
63 out
[curr_instr
].code
= code
;
64 out
[curr_instr
].jt
= jt
;
65 out
[curr_instr
].jf
= jf
;
66 out
[curr_instr
].k
= k
;
71 static inline
void set_curr_label
(char *label
)
75 labels
[curr_instr
] = label
;
82 static inline
void set_jmp_label
(char *label
, int which
)
88 labels_jt
[curr_instr
] = label
;
91 labels_jf
[curr_instr
] = label
;
94 labels_k
[curr_instr
] = label
;
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
])) {
118 panic
("No such label!\n");
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
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' '*' '&' '#' '%'
142 %type
<number
> number
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
'[' '%' 'x' '+' number
']' {
202 set_curr_instr
(BPF_LD | BPF_B | BPF_IND
, 0, 0, $6); }
203 | OP_LDB
'[' number
']' {
204 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0, $3); }
206 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
207 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
209 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
210 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
212 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
213 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
215 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
216 SKF_AD_OFF
+ SKF_AD_NLATTR
); }
217 | OP_LDB K_NLATTR_NEST
{
218 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
219 SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
); }
221 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
222 SKF_AD_OFF
+ SKF_AD_MARK
); }
224 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
225 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
227 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
228 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
230 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
231 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
233 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
234 SKF_AD_OFF
+ SKF_AD_CPU
); }
236 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
237 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
239 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
240 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
242 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
243 SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
); }
247 : OP_LDH
'[' 'x' '+' number
']' {
248 set_curr_instr
(BPF_LD | BPF_H | BPF_IND
, 0, 0, $5); }
249 | OP_LDH
'[' '%' 'x' '+' number
']' {
250 set_curr_instr
(BPF_LD | BPF_H | BPF_IND
, 0, 0, $6); }
251 | OP_LDH
'[' number
']' {
252 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0, $3); }
254 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
255 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
257 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
258 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
260 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
261 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
263 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
264 SKF_AD_OFF
+ SKF_AD_NLATTR
); }
265 | OP_LDH K_NLATTR_NEST
{
266 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
267 SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
); }
269 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
270 SKF_AD_OFF
+ SKF_AD_MARK
); }
272 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
273 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
275 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
276 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
278 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
279 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
281 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
282 SKF_AD_OFF
+ SKF_AD_CPU
); }
284 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
285 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
287 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
288 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
290 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
291 SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
); }
295 : OP_LDI
'#' number
{
296 set_curr_instr
(BPF_LD | BPF_IMM
, 0, 0, $3); }
298 set_curr_instr
(BPF_LD | BPF_IMM
, 0, 0, $2); }
303 set_curr_instr
(BPF_LD | BPF_IMM
, 0, 0, $3); }
305 set_curr_instr
(BPF_LD | BPF_W | BPF_LEN
, 0, 0, 0); }
307 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
308 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
310 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
311 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
313 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
314 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
316 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
317 SKF_AD_OFF
+ SKF_AD_NLATTR
); }
318 | OP_LD K_NLATTR_NEST
{
319 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
320 SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
); }
322 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
323 SKF_AD_OFF
+ SKF_AD_MARK
); }
325 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
326 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
328 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
329 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
331 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
332 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
334 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
335 SKF_AD_OFF
+ SKF_AD_CPU
); }
337 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
338 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
340 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
341 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
343 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
344 SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
); }
345 | OP_LD
'M' '[' number
']' {
346 set_curr_instr
(BPF_LD | BPF_MEM
, 0, 0, $4); }
347 | OP_LD
'[' 'x' '+' number
']' {
348 set_curr_instr
(BPF_LD | BPF_W | BPF_IND
, 0, 0, $5); }
349 | OP_LD
'[' '%' 'x' '+' number
']' {
350 set_curr_instr
(BPF_LD | BPF_W | BPF_IND
, 0, 0, $6); }
351 | OP_LD
'[' number
']' {
352 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0, $3); }
356 : OP_LDXI
'#' number
{
357 set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $3); }
359 set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $2); }
363 : OP_LDX
'#' number
{
364 set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $3); }
366 set_curr_instr
(BPF_LDX | BPF_W | BPF_LEN
, 0, 0, 0); }
367 | OP_LDX
'M' '[' number
']' {
368 set_curr_instr
(BPF_LDX | BPF_MEM
, 0, 0, $4); }
369 | OP_LDXB number
'*' '(' '[' number
']' '&' number
')' {
370 if
($2 != 4 ||
$9 != 0xf) {
371 panic
("ldxb offset not supported!\n");
373 set_curr_instr
(BPF_LDX | BPF_MSH | BPF_B
, 0, 0, $6); } }
374 | OP_LDX number
'*' '(' '[' number
']' '&' number
')' {
375 if
($2 != 4 ||
$9 != 0xf) {
376 panic
("ldxb offset not supported!\n");
378 set_curr_instr
(BPF_LDX | BPF_MSH | BPF_B
, 0, 0, $6); } }
382 : OP_ST
'M' '[' number
']' {
383 set_curr_instr
(BPF_ST
, 0, 0, $4); }
387 : OP_STX
'M' '[' number
']' {
388 set_curr_instr
(BPF_STX
, 0, 0, $4); }
393 set_jmp_label
($2, JKL
);
394 set_curr_instr
(BPF_JMP | BPF_JA
, 0, 0, 0); }
398 : OP_JEQ
'#' number
',' label
',' label
{
399 set_jmp_label
($5, JTL
);
400 set_jmp_label
($7, JFL
);
401 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
402 | OP_JEQ
'x' ',' label
',' label
{
403 set_jmp_label
($4, JTL
);
404 set_jmp_label
($6, JFL
);
405 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
406 | OP_JEQ
'%' 'x' ',' label
',' label
{
407 set_jmp_label
($5, JTL
);
408 set_jmp_label
($7, JFL
);
409 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
410 | OP_JEQ
'#' number
',' label
{
411 set_jmp_label
($5, JTL
);
412 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
413 | OP_JEQ
'x' ',' label
{
414 set_jmp_label
($4, JTL
);
415 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
416 | OP_JEQ
'%' 'x' ',' label
{
417 set_jmp_label
($5, JTL
);
418 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
422 : OP_JNEQ
'#' number
',' label
{
423 set_jmp_label
($5, JFL
);
424 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
425 | OP_JNEQ
'x' ',' label
{
426 set_jmp_label
($4, JFL
);
427 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
428 | OP_JNEQ
'%' 'x' ',' label
{
429 set_jmp_label
($5, JFL
);
430 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
434 : OP_JLT
'#' number
',' label
{
435 set_jmp_label
($5, JFL
);
436 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
437 | OP_JLT
'x' ',' label
{
438 set_jmp_label
($4, JFL
);
439 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
440 | OP_JLT
'%' 'x' ',' label
{
441 set_jmp_label
($5, JFL
);
442 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
446 : OP_JLE
'#' number
',' label
{
447 set_jmp_label
($5, JFL
);
448 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
449 | OP_JLE
'x' ',' label
{
450 set_jmp_label
($4, JFL
);
451 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
452 | OP_JLE
'%' 'x' ',' label
{
453 set_jmp_label
($5, JFL
);
454 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
458 : OP_JGT
'#' number
',' label
',' label
{
459 set_jmp_label
($5, JTL
);
460 set_jmp_label
($7, JFL
);
461 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
462 | OP_JGT
'x' ',' label
',' label
{
463 set_jmp_label
($4, JTL
);
464 set_jmp_label
($6, JFL
);
465 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
466 | OP_JGT
'%' 'x' ',' label
',' label
{
467 set_jmp_label
($5, JTL
);
468 set_jmp_label
($7, JFL
);
469 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
470 | OP_JGT
'#' number
',' label
{
471 set_jmp_label
($5, JTL
);
472 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
473 | OP_JGT
'x' ',' label
{
474 set_jmp_label
($4, JTL
);
475 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
476 | OP_JGT
'%' 'x' ',' label
{
477 set_jmp_label
($5, JTL
);
478 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
482 : OP_JGE
'#' number
',' label
',' label
{
483 set_jmp_label
($5, JTL
);
484 set_jmp_label
($7, JFL
);
485 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
486 | OP_JGE
'x' ',' label
',' label
{
487 set_jmp_label
($4, JTL
);
488 set_jmp_label
($6, JFL
);
489 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
490 | OP_JGE
'%' 'x' ',' label
',' label
{
491 set_jmp_label
($5, JTL
);
492 set_jmp_label
($7, JFL
);
493 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
494 | OP_JGE
'#' number
',' label
{
495 set_jmp_label
($5, JTL
);
496 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
497 | OP_JGE
'x' ',' label
{
498 set_jmp_label
($4, JTL
);
499 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
500 | OP_JGE
'%' 'x' ',' label
{
501 set_jmp_label
($5, JTL
);
502 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
506 : OP_JSET
'#' number
',' label
',' label
{
507 set_jmp_label
($5, JTL
);
508 set_jmp_label
($7, JFL
);
509 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_K
, 0, 0, $3); }
510 | OP_JSET
'x' ',' label
',' label
{
511 set_jmp_label
($4, JTL
);
512 set_jmp_label
($6, JFL
);
513 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
514 | OP_JSET
'%' 'x' ',' label
',' label
{
515 set_jmp_label
($5, JTL
);
516 set_jmp_label
($7, JFL
);
517 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
518 | OP_JSET
'#' number
',' label
{
519 set_jmp_label
($5, JTL
);
520 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_K
, 0, 0, $3); }
521 | OP_JSET
'x' ',' label
{
522 set_jmp_label
($4, JTL
);
523 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
524 | OP_JSET
'%' 'x' ',' label
{
525 set_jmp_label
($5, JTL
);
526 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
530 : OP_ADD
'#' number
{
531 set_curr_instr
(BPF_ALU | BPF_ADD | BPF_K
, 0, 0, $3); }
533 set_curr_instr
(BPF_ALU | BPF_ADD | BPF_X
, 0, 0, 0); }
535 set_curr_instr
(BPF_ALU | BPF_ADD | BPF_X
, 0, 0, 0); }
539 : OP_SUB
'#' number
{
540 set_curr_instr
(BPF_ALU | BPF_SUB | BPF_K
, 0, 0, $3); }
542 set_curr_instr
(BPF_ALU | BPF_SUB | BPF_X
, 0, 0, 0); }
544 set_curr_instr
(BPF_ALU | BPF_SUB | BPF_X
, 0, 0, 0); }
548 : OP_MUL
'#' number
{
549 set_curr_instr
(BPF_ALU | BPF_MUL | BPF_K
, 0, 0, $3); }
551 set_curr_instr
(BPF_ALU | BPF_MUL | BPF_X
, 0, 0, 0); }
553 set_curr_instr
(BPF_ALU | BPF_MUL | BPF_X
, 0, 0, 0); }
557 : OP_DIV
'#' number
{
558 set_curr_instr
(BPF_ALU | BPF_DIV | BPF_K
, 0, 0, $3); }
560 set_curr_instr
(BPF_ALU | BPF_DIV | BPF_X
, 0, 0, 0); }
562 set_curr_instr
(BPF_ALU | BPF_DIV | BPF_X
, 0, 0, 0); }
566 : OP_MOD
'#' number
{
567 set_curr_instr
(BPF_ALU | BPF_MOD | BPF_K
, 0, 0, $3); }
569 set_curr_instr
(BPF_ALU | BPF_MOD | BPF_X
, 0, 0, 0); }
571 set_curr_instr
(BPF_ALU | BPF_MOD | BPF_X
, 0, 0, 0); }
576 set_curr_instr
(BPF_ALU | BPF_NEG
, 0, 0, 0); }
580 : OP_AND
'#' number
{
581 set_curr_instr
(BPF_ALU | BPF_AND | BPF_K
, 0, 0, $3); }
583 set_curr_instr
(BPF_ALU | BPF_AND | BPF_X
, 0, 0, 0); }
585 set_curr_instr
(BPF_ALU | BPF_AND | BPF_X
, 0, 0, 0); }
590 set_curr_instr
(BPF_ALU | BPF_OR | BPF_K
, 0, 0, $3); }
592 set_curr_instr
(BPF_ALU | BPF_OR | BPF_X
, 0, 0, 0); }
594 set_curr_instr
(BPF_ALU | BPF_OR | BPF_X
, 0, 0, 0); }
598 : OP_XOR
'#' number
{
599 set_curr_instr
(BPF_ALU | BPF_XOR | BPF_K
, 0, 0, $3); }
601 set_curr_instr
(BPF_ALU | BPF_XOR | BPF_X
, 0, 0, 0); }
603 set_curr_instr
(BPF_ALU | BPF_XOR | BPF_X
, 0, 0, 0); }
607 : OP_LSH
'#' number
{
608 set_curr_instr
(BPF_ALU | BPF_LSH | BPF_K
, 0, 0, $3); }
610 set_curr_instr
(BPF_ALU | BPF_LSH | BPF_X
, 0, 0, 0); }
612 set_curr_instr
(BPF_ALU | BPF_LSH | BPF_X
, 0, 0, 0); }
616 : OP_RSH
'#' number
{
617 set_curr_instr
(BPF_ALU | BPF_RSH | BPF_K
, 0, 0, $3); }
619 set_curr_instr
(BPF_ALU | BPF_RSH | BPF_X
, 0, 0, 0); }
621 set_curr_instr
(BPF_ALU | BPF_RSH | BPF_X
, 0, 0, 0); }
626 set_curr_instr
(BPF_RET | BPF_A
, 0, 0, 0); }
628 set_curr_instr
(BPF_RET | BPF_A
, 0, 0, 0); }
630 set_curr_instr
(BPF_RET | BPF_X
, 0, 0, 0); }
632 set_curr_instr
(BPF_RET | BPF_X
, 0, 0, 0); }
633 | OP_RET
'#' number
{
634 set_curr_instr
(BPF_RET | BPF_K
, 0, 0, $3); }
639 set_curr_instr
(BPF_MISC | BPF_TAX
, 0, 0, 0); }
644 set_curr_instr
(BPF_MISC | BPF_TXA
, 0, 0, 0); }
649 static void stage_1_inline
(void)
654 static void stage_2_label_reduce
(void)
656 int i
, max
= curr_instr
, off
;
658 /* 1. reduce k jumps */
659 for
(i
= 0; i
< max
; ++i
) {
660 if
(labels_k
[i
] != NULL
) {
661 off
= find_intr_offset_or_panic
(labels_k
[i
]);
662 out
[i
].k
= (uint32_t) (off
- i
- 1);
666 /* 1. reduce jt jumps */
667 for
(i
= 0; i
< max
; ++i
) {
668 if
(labels_jt
[i
] != NULL
) {
669 off
= find_intr_offset_or_panic
(labels_jt
[i
]);
670 out
[i
].jt
= (uint8_t) (off
- i
-1);
674 /* 1. reduce jf jumps */
675 for
(i
= 0; i
< max
; ++i
) {
676 if
(labels_jf
[i
] != NULL
) {
677 off
= find_intr_offset_or_panic
(labels_jf
[i
]);
678 out
[i
].jf
= (uint8_t) (off
- i
- 1);
683 static void pretty_printer_c
(const struct sock_fprog
*prog
)
687 for
(i
= 0; i
< prog
->len
; ++i
) {
688 printf
("{ 0x%x, %u, %u, 0x%08x },\n",
689 prog
->filter
[i
].code
, prog
->filter
[i
].jt
,
690 prog
->filter
[i
].jf
, prog
->filter
[i
].k
);
694 static void pretty_printer_xt_bpf
(const struct sock_fprog
*prog
)
698 printf
("%d,", prog
->len
);
699 for
(i
= 0; i
< prog
->len
; ++i
) {
700 printf
("%u %u %u %u,",
701 prog
->filter
[i
].code
, prog
->filter
[i
].jt
,
702 prog
->filter
[i
].jf
, prog
->filter
[i
].k
);
708 static void pretty_printer_tcpdump
(const struct sock_fprog
*prog
)
712 for
(i
= 0; i
< prog
->len
; ++i
) {
713 printf
("%u %u %u %u\n",
714 prog
->filter
[i
].code
, prog
->filter
[i
].jt
,
715 prog
->filter
[i
].jf
, prog
->filter
[i
].k
);
719 static void pretty_printer
(const struct sock_fprog
*prog
, int format
)
723 pretty_printer_c
(prog
);
726 pretty_printer_xt_bpf
(prog
);
729 pretty_printer_tcpdump
(prog
);
736 int compile_filter
(char *file
, int verbose
, int bypass
, int format
,
740 struct sock_fprog res
;
743 memset
(tmp_file
, 0, sizeof
(tmp_file
));
746 char cmd
[256], *dir
, *base
, *a
, *b
;
748 dir
= dirname
((a
= xstrdup
(file
)));
749 base
= basename
((b
= xstrdup
(file
)));
751 slprintf
(tmp_file
, sizeof
(tmp_file
), "%s/.tmp-%u-%s", dir
, rand
(), base
);
752 slprintf
(cmd
, sizeof
(cmd
), "cpp -I" ETCDIRE_STRING
" %s > %s",
754 if
(system
(cmd
) != 0)
755 panic
("Failed to invoke C preprocessor!\n");
762 if
(!strncmp
("-", file
, strlen
("-")))
765 yyin
= fopen
(file
, "r");
767 panic
("Cannot open file!\n");
769 memset
(out
, 0, sizeof
(out
));
770 memset
(labels
, 0, sizeof
(labels
));
771 memset
(labels_jf
, 0, sizeof
(labels_jf
));
772 memset
(labels_jt
, 0, sizeof
(labels_jt
));
773 memset
(labels_k
, 0, sizeof
(labels_k
));
776 stage_2_label_reduce
();
779 res.len
= curr_instr
;
782 printf
("Generated program:\n");
788 printf
("Validating: ");
792 if
(__bpf_validate
(&res
) == 0) {
794 printf
("Semantic error! BPF validation failed!\n");
796 panic
("Semantic error! BPF validation failed! "
797 "Try -V for debugging output!\n");
798 } else if
(verbose
) {
799 printf
("is runnable!\n");
806 pretty_printer
(&res
, format
);
808 for
(i
= 0; i
< res.len
; ++i
) {
822 void yyerror(const char *err
)
824 panic
("Syntax error at line %d: %s! %s!\n",
825 yylineno
, yytext
, err
);