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 /* yacc-func-prefix: yy */
20 #include <linux/filter.h>
25 #include "bpf_parser.tab.h"
30 static int curr_instr
= 0;
32 static struct sock_filter out
[BPF_MAXINSNS
];
34 static char *labels
[BPF_MAXINSNS
];
35 static char *labels_jt
[BPF_MAXINSNS
];
36 static char *labels_jf
[BPF_MAXINSNS
];
37 static char *labels_k
[BPF_MAXINSNS
];
39 #define YYERROR_VERBOSE 0
41 #define YYLTYPE_IS_TRIVIAL 1
44 extern
int yylex(void);
45 extern
void yyerror(const char *);
49 static inline
void check_max_instr
(void)
51 if
(curr_instr
>= BPF_MAXINSNS
)
52 panic
("Exceeded maximal number of instructions!\n");
55 static inline
void set_curr_instr
(uint16_t code
, uint8_t jt
, uint8_t jf
, uint32_t k
)
59 out
[curr_instr
].code
= code
;
60 out
[curr_instr
].jt
= jt
;
61 out
[curr_instr
].jf
= jf
;
62 out
[curr_instr
].k
= k
;
67 static inline
void set_curr_label
(char *label
)
71 labels
[curr_instr
] = label
;
78 static inline
void set_jmp_label
(char *label
, int which
)
84 labels_jt
[curr_instr
] = label
;
87 labels_jf
[curr_instr
] = label
;
90 labels_k
[curr_instr
] = label
;
97 static int find_intr_offset_or_panic
(char *label_to_search
)
99 int i
, max
= curr_instr
, ret
= -ENOENT
;
101 bug_on
(!label_to_search
);
103 for
(i
= 0; i
< max
; ++i
) {
104 if
(labels
[i
] != NULL
) {
105 /* Both are \0-terminated! */
106 if
(!strcmp
(label_to_search
, labels
[i
])) {
114 panic
("No such label!\n");
126 %token OP_LDB OP_LDH OP_LD OP_LDX OP_ST OP_STX OP_JMP OP_JEQ OP_JGT OP_JGE
127 %token OP_JSET OP_ADD OP_SUB OP_MUL OP_DIV OP_AND OP_OR OP_XOR OP_LSH OP_RSH
128 %token OP_RET OP_TAX OP_TXA OP_LDXB OP_MOD OP_NEG OP_JNEQ OP_JLT OP_JLE OP_LDI
131 %token K_PKT_LEN K_PROTO K_TYPE K_NLATTR K_NLATTR_NEST K_MARK K_QUEUE K_HATYPE
132 %token K_RXHASH K_CPU K_IFIDX K_VLANT K_VLANP K_POFF
134 %token
':' ',' '[' ']' '(' ')' 'x' 'a' '+' 'M' '*' '&' '#' '%'
138 %type
<number
> number
191 : label
':' { set_curr_label
($1); }
195 : OP_LDB
'[' 'x' '+' number
']' {
196 set_curr_instr
(BPF_LD | BPF_B | BPF_IND
, 0, 0, $5); }
197 | OP_LDB
'[' '%' 'x' '+' number
']' {
198 set_curr_instr
(BPF_LD | BPF_B | BPF_IND
, 0, 0, $6); }
199 | OP_LDB
'[' number
']' {
200 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0, $3); }
202 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
203 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
205 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
206 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
208 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
209 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
211 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
212 SKF_AD_OFF
+ SKF_AD_NLATTR
); }
213 | OP_LDB K_NLATTR_NEST
{
214 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
215 SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
); }
217 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
218 SKF_AD_OFF
+ SKF_AD_MARK
); }
220 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
221 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
223 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
224 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
226 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
227 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
229 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
230 SKF_AD_OFF
+ SKF_AD_CPU
); }
232 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
233 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
235 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
236 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
238 set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
239 SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
); }
243 : OP_LDH
'[' 'x' '+' number
']' {
244 set_curr_instr
(BPF_LD | BPF_H | BPF_IND
, 0, 0, $5); }
245 | OP_LDH
'[' '%' 'x' '+' number
']' {
246 set_curr_instr
(BPF_LD | BPF_H | BPF_IND
, 0, 0, $6); }
247 | OP_LDH
'[' number
']' {
248 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0, $3); }
250 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
251 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
253 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
254 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
256 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
257 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
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
); }
265 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
266 SKF_AD_OFF
+ SKF_AD_MARK
); }
268 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
269 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
271 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
272 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
274 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
275 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
277 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
278 SKF_AD_OFF
+ SKF_AD_CPU
); }
280 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
281 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
283 set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
284 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
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, $3); }
294 set_curr_instr
(BPF_LD | BPF_IMM
, 0, 0, $2); }
299 set_curr_instr
(BPF_LD | BPF_IMM
, 0, 0, $3); }
301 set_curr_instr
(BPF_LD | BPF_W | BPF_LEN
, 0, 0, 0); }
303 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
304 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
306 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
307 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
309 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
310 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
312 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
313 SKF_AD_OFF
+ SKF_AD_NLATTR
); }
314 | OP_LD K_NLATTR_NEST
{
315 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
316 SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
); }
318 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
319 SKF_AD_OFF
+ SKF_AD_MARK
); }
321 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
322 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
324 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
325 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
327 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
328 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
330 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
331 SKF_AD_OFF
+ SKF_AD_CPU
); }
333 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
334 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
336 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
337 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
339 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
340 SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
); }
341 | OP_LD
'M' '[' number
']' {
342 set_curr_instr
(BPF_LD | BPF_MEM
, 0, 0, $4); }
343 | OP_LD
'[' 'x' '+' number
']' {
344 set_curr_instr
(BPF_LD | BPF_W | BPF_IND
, 0, 0, $5); }
345 | OP_LD
'[' '%' 'x' '+' number
']' {
346 set_curr_instr
(BPF_LD | BPF_W | BPF_IND
, 0, 0, $6); }
347 | OP_LD
'[' number
']' {
348 set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0, $3); }
352 : OP_LDXI
'#' number
{
353 set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $3); }
355 set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $2); }
359 : OP_LDX
'#' number
{
360 set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $3); }
362 set_curr_instr
(BPF_LDX | BPF_W | BPF_LEN
, 0, 0, 0); }
363 | OP_LDX
'M' '[' number
']' {
364 set_curr_instr
(BPF_LDX | BPF_MEM
, 0, 0, $4); }
365 | OP_LDXB number
'*' '(' '[' number
']' '&' number
')' {
366 if
($2 != 4 ||
$9 != 0xf) {
367 panic
("ldxb offset not supported!\n");
369 set_curr_instr
(BPF_LDX | BPF_MSH | BPF_B
, 0, 0, $6); } }
370 | OP_LDX 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); } }
378 : OP_ST
'M' '[' number
']' {
379 set_curr_instr
(BPF_ST
, 0, 0, $4); }
383 : OP_STX
'M' '[' number
']' {
384 set_curr_instr
(BPF_STX
, 0, 0, $4); }
389 set_jmp_label
($2, JKL
);
390 set_curr_instr
(BPF_JMP | BPF_JA
, 0, 0, 0); }
394 : OP_JEQ
'#' number
',' label
',' label
{
395 set_jmp_label
($5, JTL
);
396 set_jmp_label
($7, JFL
);
397 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
398 | OP_JEQ
'x' ',' label
',' label
{
399 set_jmp_label
($4, JTL
);
400 set_jmp_label
($6, JFL
);
401 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
402 | OP_JEQ
'%' 'x' ',' label
',' label
{
403 set_jmp_label
($5, JTL
);
404 set_jmp_label
($7, JFL
);
405 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
406 | OP_JEQ
'#' number
',' label
{
407 set_jmp_label
($5, JTL
);
408 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
409 | OP_JEQ
'x' ',' label
{
410 set_jmp_label
($4, JTL
);
411 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
412 | OP_JEQ
'%' 'x' ',' label
{
413 set_jmp_label
($5, JTL
);
414 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
418 : OP_JNEQ
'#' number
',' label
{
419 set_jmp_label
($5, JFL
);
420 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
421 | OP_JNEQ
'x' ',' label
{
422 set_jmp_label
($4, JFL
);
423 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
424 | OP_JNEQ
'%' 'x' ',' label
{
425 set_jmp_label
($5, JFL
);
426 set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
430 : OP_JLT
'#' number
',' label
{
431 set_jmp_label
($5, JFL
);
432 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
433 | OP_JLT
'x' ',' label
{
434 set_jmp_label
($4, JFL
);
435 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
436 | OP_JLT
'%' 'x' ',' label
{
437 set_jmp_label
($5, JFL
);
438 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
442 : OP_JLE
'#' number
',' label
{
443 set_jmp_label
($5, JFL
);
444 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
445 | OP_JLE
'x' ',' label
{
446 set_jmp_label
($4, JFL
);
447 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
448 | OP_JLE
'%' 'x' ',' label
{
449 set_jmp_label
($5, JFL
);
450 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
454 : OP_JGT
'#' number
',' label
',' label
{
455 set_jmp_label
($5, JTL
);
456 set_jmp_label
($7, JFL
);
457 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
458 | OP_JGT
'x' ',' label
',' label
{
459 set_jmp_label
($4, JTL
);
460 set_jmp_label
($6, JFL
);
461 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
462 | OP_JGT
'%' 'x' ',' label
',' label
{
463 set_jmp_label
($5, JTL
);
464 set_jmp_label
($7, JFL
);
465 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
466 | OP_JGT
'#' number
',' label
{
467 set_jmp_label
($5, JTL
);
468 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
469 | OP_JGT
'x' ',' label
{
470 set_jmp_label
($4, JTL
);
471 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
472 | OP_JGT
'%' 'x' ',' label
{
473 set_jmp_label
($5, JTL
);
474 set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
478 : OP_JGE
'#' number
',' label
',' label
{
479 set_jmp_label
($5, JTL
);
480 set_jmp_label
($7, JFL
);
481 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
482 | OP_JGE
'x' ',' label
',' label
{
483 set_jmp_label
($4, JTL
);
484 set_jmp_label
($6, JFL
);
485 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
486 | OP_JGE
'%' 'x' ',' label
',' label
{
487 set_jmp_label
($5, JTL
);
488 set_jmp_label
($7, JFL
);
489 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
490 | OP_JGE
'#' number
',' label
{
491 set_jmp_label
($5, JTL
);
492 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
493 | OP_JGE
'x' ',' label
{
494 set_jmp_label
($4, JTL
);
495 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
496 | OP_JGE
'%' 'x' ',' label
{
497 set_jmp_label
($5, JTL
);
498 set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
502 : OP_JSET
'#' number
',' label
',' label
{
503 set_jmp_label
($5, JTL
);
504 set_jmp_label
($7, JFL
);
505 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_K
, 0, 0, $3); }
506 | OP_JSET
'x' ',' label
',' label
{
507 set_jmp_label
($4, JTL
);
508 set_jmp_label
($6, JFL
);
509 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
510 | OP_JSET
'%' 'x' ',' label
',' label
{
511 set_jmp_label
($5, JTL
);
512 set_jmp_label
($7, JFL
);
513 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
514 | OP_JSET
'#' number
',' label
{
515 set_jmp_label
($5, JTL
);
516 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_K
, 0, 0, $3); }
517 | OP_JSET
'x' ',' label
{
518 set_jmp_label
($4, JTL
);
519 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
520 | OP_JSET
'%' 'x' ',' label
{
521 set_jmp_label
($5, JTL
);
522 set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
526 : OP_ADD
'#' number
{
527 set_curr_instr
(BPF_ALU | BPF_ADD | BPF_K
, 0, 0, $3); }
529 set_curr_instr
(BPF_ALU | BPF_ADD | BPF_X
, 0, 0, 0); }
531 set_curr_instr
(BPF_ALU | BPF_ADD | BPF_X
, 0, 0, 0); }
535 : OP_SUB
'#' number
{
536 set_curr_instr
(BPF_ALU | BPF_SUB | BPF_K
, 0, 0, $3); }
538 set_curr_instr
(BPF_ALU | BPF_SUB | BPF_X
, 0, 0, 0); }
540 set_curr_instr
(BPF_ALU | BPF_SUB | BPF_X
, 0, 0, 0); }
544 : OP_MUL
'#' number
{
545 set_curr_instr
(BPF_ALU | BPF_MUL | BPF_K
, 0, 0, $3); }
547 set_curr_instr
(BPF_ALU | BPF_MUL | BPF_X
, 0, 0, 0); }
549 set_curr_instr
(BPF_ALU | BPF_MUL | BPF_X
, 0, 0, 0); }
553 : OP_DIV
'#' number
{
554 set_curr_instr
(BPF_ALU | BPF_DIV | BPF_K
, 0, 0, $3); }
556 set_curr_instr
(BPF_ALU | BPF_DIV | BPF_X
, 0, 0, 0); }
558 set_curr_instr
(BPF_ALU | BPF_DIV | BPF_X
, 0, 0, 0); }
562 : OP_MOD
'#' number
{
563 set_curr_instr
(BPF_ALU | BPF_MOD | BPF_K
, 0, 0, $3); }
565 set_curr_instr
(BPF_ALU | BPF_MOD | BPF_X
, 0, 0, 0); }
567 set_curr_instr
(BPF_ALU | BPF_MOD | BPF_X
, 0, 0, 0); }
572 set_curr_instr
(BPF_ALU | BPF_NEG
, 0, 0, 0); }
576 : OP_AND
'#' number
{
577 set_curr_instr
(BPF_ALU | BPF_AND | BPF_K
, 0, 0, $3); }
579 set_curr_instr
(BPF_ALU | BPF_AND | BPF_X
, 0, 0, 0); }
581 set_curr_instr
(BPF_ALU | BPF_AND | BPF_X
, 0, 0, 0); }
586 set_curr_instr
(BPF_ALU | BPF_OR | BPF_K
, 0, 0, $3); }
588 set_curr_instr
(BPF_ALU | BPF_OR | BPF_X
, 0, 0, 0); }
590 set_curr_instr
(BPF_ALU | BPF_OR | BPF_X
, 0, 0, 0); }
594 : OP_XOR
'#' number
{
595 set_curr_instr
(BPF_ALU | BPF_XOR | BPF_K
, 0, 0, $3); }
597 set_curr_instr
(BPF_ALU | BPF_XOR | BPF_X
, 0, 0, 0); }
599 set_curr_instr
(BPF_ALU | BPF_XOR | BPF_X
, 0, 0, 0); }
603 : OP_LSH
'#' number
{
604 set_curr_instr
(BPF_ALU | BPF_LSH | BPF_K
, 0, 0, $3); }
606 set_curr_instr
(BPF_ALU | BPF_LSH | BPF_X
, 0, 0, 0); }
608 set_curr_instr
(BPF_ALU | BPF_LSH | BPF_X
, 0, 0, 0); }
612 : OP_RSH
'#' number
{
613 set_curr_instr
(BPF_ALU | BPF_RSH | BPF_K
, 0, 0, $3); }
615 set_curr_instr
(BPF_ALU | BPF_RSH | BPF_X
, 0, 0, 0); }
617 set_curr_instr
(BPF_ALU | BPF_RSH | BPF_X
, 0, 0, 0); }
622 set_curr_instr
(BPF_RET | BPF_A
, 0, 0, 0); }
624 set_curr_instr
(BPF_RET | BPF_A
, 0, 0, 0); }
626 set_curr_instr
(BPF_RET | BPF_X
, 0, 0, 0); }
628 set_curr_instr
(BPF_RET | BPF_X
, 0, 0, 0); }
629 | OP_RET
'#' number
{
630 set_curr_instr
(BPF_RET | BPF_K
, 0, 0, $3); }
635 set_curr_instr
(BPF_MISC | BPF_TAX
, 0, 0, 0); }
640 set_curr_instr
(BPF_MISC | BPF_TXA
, 0, 0, 0); }
645 static void stage_1_inline
(void)
650 static void stage_2_label_reduce
(void)
652 int i
, max
= curr_instr
, off
;
654 /* 1. reduce k jumps */
655 for
(i
= 0; i
< max
; ++i
) {
656 if
(labels_k
[i
] != NULL
) {
657 off
= find_intr_offset_or_panic
(labels_k
[i
]);
658 out
[i
].k
= (uint32_t) (off
- i
- 1);
662 /* 1. reduce jt jumps */
663 for
(i
= 0; i
< max
; ++i
) {
664 if
(labels_jt
[i
] != NULL
) {
665 off
= find_intr_offset_or_panic
(labels_jt
[i
]);
666 out
[i
].jt
= (uint8_t) (off
- i
-1);
670 /* 1. reduce jf jumps */
671 for
(i
= 0; i
< max
; ++i
) {
672 if
(labels_jf
[i
] != NULL
) {
673 off
= find_intr_offset_or_panic
(labels_jf
[i
]);
674 out
[i
].jf
= (uint8_t) (off
- i
- 1);
679 static void pretty_printer_c
(const struct sock_fprog
*prog
)
683 for
(i
= 0; i
< prog
->len
; ++i
) {
684 printf
("{ 0x%x, %u, %u, 0x%08x },\n",
685 prog
->filter
[i
].code
, prog
->filter
[i
].jt
,
686 prog
->filter
[i
].jf
, prog
->filter
[i
].k
);
690 static void pretty_printer_xt_bpf
(const struct sock_fprog
*prog
)
694 printf
("%d,", prog
->len
);
695 for
(i
= 0; i
< prog
->len
; ++i
) {
696 printf
("%u %u %u %u,",
697 prog
->filter
[i
].code
, prog
->filter
[i
].jt
,
698 prog
->filter
[i
].jf
, prog
->filter
[i
].k
);
704 static void pretty_printer_tcpdump
(const struct sock_fprog
*prog
)
708 for
(i
= 0; i
< prog
->len
; ++i
) {
709 printf
("%u %u %u %u\n",
710 prog
->filter
[i
].code
, prog
->filter
[i
].jt
,
711 prog
->filter
[i
].jf
, prog
->filter
[i
].k
);
715 static void pretty_printer
(const struct sock_fprog
*prog
, int format
)
719 pretty_printer_c
(prog
);
722 pretty_printer_xt_bpf
(prog
);
725 pretty_printer_tcpdump
(prog
);
732 int compile_filter
(char *file
, bool verbose
, int bypass
, int format
,
733 bool invoke_cpp
, char *const cpp_argv
[])
736 struct sock_fprog res
;
740 memset
(tmp_file
, 0, sizeof
(tmp_file
));
743 ret
= cpp_exec
(file
, tmp_file
, sizeof
(tmp_file
), cpp_argv
);
745 fprintf
(stderr
, "Failed to invoke C preprocessor!\n");
752 if
(!strncmp
("-", file
, strlen
("-")))
755 yyin
= fopen
(file
, "r");
757 panic
("Cannot open file!\n");
759 memset
(out
, 0, sizeof
(out
));
760 memset
(labels
, 0, sizeof
(labels
));
761 memset
(labels_jf
, 0, sizeof
(labels_jf
));
762 memset
(labels_jt
, 0, sizeof
(labels_jt
));
763 memset
(labels_k
, 0, sizeof
(labels_k
));
766 stage_2_label_reduce
();
769 res.len
= curr_instr
;
772 printf
("Generated program:\n");
778 printf
("Validating: ");
782 if
(__bpf_validate
(&res
) == 0) {
784 printf
("Semantic error! BPF validation failed!\n");
786 printf
("Semantic error! BPF validation failed! "
787 "Try -V for debugging output!\n");
791 } else if
(verbose
) {
792 printf
("is runnable!\n");
799 pretty_printer
(&res
, format
);
801 for
(i
= 0; i
< res.len
; ++i
) {
808 if
(yyin
&& yyin
!= stdin
)
818 void yyerror(const char *err
)
820 panic
("Syntax error at line %d: %s! %s!\n",
821 yylineno
, yytext
, err
);