trafgen: Add checksum helpers for TCP/UDP over IPv6
[netsniff-ng.git] / bpf_parser.y
blob26ec12566450d28c0536bc173c33293c7ad4a2e1
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 '[' '%' '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); }
205 | OP_LDB K_PROTO {
206 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
207 SKF_AD_OFF + SKF_AD_PROTOCOL); }
208 | OP_LDB K_TYPE {
209 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
210 SKF_AD_OFF + SKF_AD_PKTTYPE); }
211 | OP_LDB K_IFIDX {
212 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
213 SKF_AD_OFF + SKF_AD_IFINDEX); }
214 | OP_LDB K_NLATTR {
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); }
220 | OP_LDB K_MARK {
221 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
222 SKF_AD_OFF + SKF_AD_MARK); }
223 | OP_LDB K_QUEUE {
224 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
225 SKF_AD_OFF + SKF_AD_QUEUE); }
226 | OP_LDB K_HATYPE {
227 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
228 SKF_AD_OFF + SKF_AD_HATYPE); }
229 | OP_LDB K_RXHASH {
230 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
231 SKF_AD_OFF + SKF_AD_RXHASH); }
232 | OP_LDB K_CPU {
233 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
234 SKF_AD_OFF + SKF_AD_CPU); }
235 | OP_LDB K_VLANT {
236 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
237 SKF_AD_OFF + SKF_AD_VLAN_TAG); }
238 | OP_LDB K_VLANP {
239 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
240 SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); }
241 | OP_LDB K_POFF {
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); }
253 | OP_LDH K_PROTO {
254 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
255 SKF_AD_OFF + SKF_AD_PROTOCOL); }
256 | OP_LDH K_TYPE {
257 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
258 SKF_AD_OFF + SKF_AD_PKTTYPE); }
259 | OP_LDH K_IFIDX {
260 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
261 SKF_AD_OFF + SKF_AD_IFINDEX); }
262 | OP_LDH K_NLATTR {
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); }
268 | OP_LDH K_MARK {
269 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
270 SKF_AD_OFF + SKF_AD_MARK); }
271 | OP_LDH K_QUEUE {
272 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
273 SKF_AD_OFF + SKF_AD_QUEUE); }
274 | OP_LDH K_HATYPE {
275 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
276 SKF_AD_OFF + SKF_AD_HATYPE); }
277 | OP_LDH K_RXHASH {
278 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
279 SKF_AD_OFF + SKF_AD_RXHASH); }
280 | OP_LDH K_CPU {
281 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
282 SKF_AD_OFF + SKF_AD_CPU); }
283 | OP_LDH K_VLANT {
284 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
285 SKF_AD_OFF + SKF_AD_VLAN_TAG); }
286 | OP_LDH K_VLANP {
287 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
288 SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); }
289 | OP_LDH K_POFF {
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); }
297 | OP_LDI number {
298 set_curr_instr(BPF_LD | BPF_IMM, 0, 0, $2); }
302 : OP_LD '#' number {
303 set_curr_instr(BPF_LD | BPF_IMM, 0, 0, $3); }
304 | OP_LD K_PKT_LEN {
305 set_curr_instr(BPF_LD | BPF_W | BPF_LEN, 0, 0, 0); }
306 | OP_LD K_PROTO {
307 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
308 SKF_AD_OFF + SKF_AD_PROTOCOL); }
309 | OP_LD K_TYPE {
310 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
311 SKF_AD_OFF + SKF_AD_PKTTYPE); }
312 | OP_LD K_IFIDX {
313 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
314 SKF_AD_OFF + SKF_AD_IFINDEX); }
315 | OP_LD K_NLATTR {
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); }
321 | OP_LD K_MARK {
322 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
323 SKF_AD_OFF + SKF_AD_MARK); }
324 | OP_LD K_QUEUE {
325 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
326 SKF_AD_OFF + SKF_AD_QUEUE); }
327 | OP_LD K_HATYPE {
328 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
329 SKF_AD_OFF + SKF_AD_HATYPE); }
330 | OP_LD K_RXHASH {
331 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
332 SKF_AD_OFF + SKF_AD_RXHASH); }
333 | OP_LD K_CPU {
334 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
335 SKF_AD_OFF + SKF_AD_CPU); }
336 | OP_LD K_VLANT {
337 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
338 SKF_AD_OFF + SKF_AD_VLAN_TAG); }
339 | OP_LD K_VLANP {
340 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
341 SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); }
342 | OP_LD K_POFF {
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); }
355 ldxi
356 : OP_LDXI '#' number {
357 set_curr_instr(BPF_LDX | BPF_IMM, 0, 0, $3); }
358 | OP_LDXI number {
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); }
365 | OP_LDX K_PKT_LEN {
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");
372 } else {
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");
377 } else {
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); }
392 : OP_JMP label {
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); }
421 jneq
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); }
505 jset
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); }
532 | OP_ADD 'x' {
533 set_curr_instr(BPF_ALU | BPF_ADD | BPF_X, 0, 0, 0); }
534 | OP_ADD '%' 'x' {
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); }
541 | OP_SUB 'x' {
542 set_curr_instr(BPF_ALU | BPF_SUB | BPF_X, 0, 0, 0); }
543 | OP_SUB '%' 'x' {
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); }
550 | OP_MUL 'x' {
551 set_curr_instr(BPF_ALU | BPF_MUL | BPF_X, 0, 0, 0); }
552 | OP_MUL '%' 'x' {
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); }
559 | OP_DIV 'x' {
560 set_curr_instr(BPF_ALU | BPF_DIV | BPF_X, 0, 0, 0); }
561 | OP_DIV '%' 'x' {
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); }
568 | OP_MOD 'x' {
569 set_curr_instr(BPF_ALU | BPF_MOD | BPF_X, 0, 0, 0); }
570 | OP_MOD '%' 'x' {
571 set_curr_instr(BPF_ALU | BPF_MOD | BPF_X, 0, 0, 0); }
575 : OP_NEG {
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); }
582 | OP_AND 'x' {
583 set_curr_instr(BPF_ALU | BPF_AND | BPF_X, 0, 0, 0); }
584 | OP_AND '%' 'x' {
585 set_curr_instr(BPF_ALU | BPF_AND | BPF_X, 0, 0, 0); }
589 : OP_OR '#' number {
590 set_curr_instr(BPF_ALU | BPF_OR | BPF_K, 0, 0, $3); }
591 | OP_OR 'x' {
592 set_curr_instr(BPF_ALU | BPF_OR | BPF_X, 0, 0, 0); }
593 | OP_OR '%' 'x' {
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); }
600 | OP_XOR 'x' {
601 set_curr_instr(BPF_ALU | BPF_XOR | BPF_X, 0, 0, 0); }
602 | OP_XOR '%' 'x' {
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); }
609 | OP_LSH 'x' {
610 set_curr_instr(BPF_ALU | BPF_LSH | BPF_X, 0, 0, 0); }
611 | OP_LSH '%' 'x' {
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); }
618 | OP_RSH 'x' {
619 set_curr_instr(BPF_ALU | BPF_RSH | BPF_X, 0, 0, 0); }
620 | OP_RSH '%' 'x' {
621 set_curr_instr(BPF_ALU | BPF_RSH | BPF_X, 0, 0, 0); }
625 : OP_RET 'a' {
626 set_curr_instr(BPF_RET | BPF_A, 0, 0, 0); }
627 | OP_RET '%' 'a' {
628 set_curr_instr(BPF_RET | BPF_A, 0, 0, 0); }
629 | OP_RET 'x' {
630 set_curr_instr(BPF_RET | BPF_X, 0, 0, 0); }
631 | OP_RET '%' 'x' {
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); }
638 : OP_TAX {
639 set_curr_instr(BPF_MISC | BPF_TAX, 0, 0, 0); }
643 : OP_TXA {
644 set_curr_instr(BPF_MISC | BPF_TXA, 0, 0, 0); }
649 static void stage_1_inline(void)
651 yyparse();
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)
685 int i;
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)
696 int i;
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);
705 fflush(stdout);
708 static void pretty_printer_tcpdump(const struct sock_fprog *prog)
710 int i;
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)
721 switch (format) {
722 case 0:
723 pretty_printer_c(prog);
724 break;
725 case 1:
726 pretty_printer_xt_bpf(prog);
727 break;
728 case 2:
729 pretty_printer_tcpdump(prog);
730 break;
731 default:
732 bug();
736 int compile_filter(char *file, int verbose, int bypass, int format,
737 bool invoke_cpp)
739 int i;
740 struct sock_fprog res;
741 char tmp_file[128];
743 memset(tmp_file, 0, sizeof(tmp_file));
745 if (invoke_cpp) {
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",
753 file, tmp_file);
754 if (system(cmd) != 0)
755 panic("Failed to invoke C preprocessor!\n");
757 file = tmp_file;
758 xfree(a);
759 xfree(b);
762 if (!strncmp("-", file, strlen("-")))
763 yyin = stdin;
764 else
765 yyin = fopen(file, "r");
766 if (!yyin)
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));
775 stage_1_inline();
776 stage_2_label_reduce();
778 res.filter = out;
779 res.len = curr_instr;
781 if (verbose) {
782 printf("Generated program:\n");
783 bpf_dump_all(&res);
786 if (!bypass) {
787 if (verbose) {
788 printf("Validating: ");
789 fflush(stdout);
792 if (__bpf_validate(&res) == 0) {
793 if (verbose)
794 printf("Semantic error! BPF validation failed!\n");
795 else
796 panic("Semantic error! BPF validation failed! "
797 "Try -V for debugging output!\n");
798 } else if (verbose) {
799 printf("is runnable!\n");
803 if (verbose)
804 printf("Result:\n");
806 pretty_printer(&res, format);
808 for (i = 0; i < res.len; ++i) {
809 free(labels[i]);
810 free(labels_jt[i]);
811 free(labels_jf[i]);
812 free(labels_k[i]);
815 fclose(yyin);
816 if (invoke_cpp)
817 unlink(tmp_file);
819 return 0;
822 void yyerror(const char *err)
824 panic("Syntax error at line %d: %s! %s!\n",
825 yylineno, yytext, err);