geoip: Allow to get country 3-code
[netsniff-ng-new.git] / bpf_parser.y
blob6f5f2b0bd126f11a432abb4d8c7c15b731cf0036
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"
28 #include "cpp.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
43 #define YYDEBUG 0
44 #define YYENABLE_NLS 1
45 #define YYLTYPE_IS_TRIVIAL 1
46 #define ENABLE_NLS 1
48 extern FILE *yyin;
49 extern int yylex(void);
50 extern void yyerror(const char *);
51 extern int yylineno;
52 extern char *yytext;
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)
62 check_max_instr();
64 out[curr_instr].code = code;
65 out[curr_instr].jt = jt;
66 out[curr_instr].jf = jf;
67 out[curr_instr].k = k;
69 curr_instr++;
72 static inline void set_curr_label(char *label)
74 check_max_instr();
76 labels[curr_instr] = label;
79 #define JTL 1
80 #define JFL 2
81 #define JKL 3
83 static inline void set_jmp_label(char *label, int which)
85 check_max_instr();
87 switch (which) {
88 case JTL:
89 labels_jt[curr_instr] = label;
90 break;
91 case JFL:
92 labels_jf[curr_instr] = label;
93 break;
94 case JKL:
95 labels_k[curr_instr] = label;
96 break;
97 default:
98 bug();
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])) {
112 ret = i;
113 break;
118 if (ret == -ENOENT)
119 panic("No such label!\n");
121 return ret;
126 %union {
127 char *label;
128 long int number;
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
134 %token OP_LDXI
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' '*' '&' '#' '%'
141 %token number label
143 %type <number> number
144 %type <label> label
148 prog
149 : line
150 | prog line
153 line
154 : instr
155 | labelled_instr
158 labelled_instr
159 : labelled instr
162 instr
163 : ldb
164 | ldh
165 | ld
166 | ldi
167 | ldx
168 | ldxi
169 | st
170 | stx
171 | jmp
172 | jeq
173 | jneq
174 | jlt
175 | jle
176 | jgt
177 | jge
178 | jset
179 | add
180 | sub
181 | mul
182 | div
183 | mod
184 | neg
185 | and
186 | or
187 | xor
188 | lsh
189 | rsh
190 | ret
191 | tax
192 | txa
195 labelled
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); }
206 | OP_LDB K_PROTO {
207 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
208 SKF_AD_OFF + SKF_AD_PROTOCOL); }
209 | OP_LDB K_TYPE {
210 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
211 SKF_AD_OFF + SKF_AD_PKTTYPE); }
212 | OP_LDB K_IFIDX {
213 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
214 SKF_AD_OFF + SKF_AD_IFINDEX); }
215 | OP_LDB K_NLATTR {
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); }
221 | OP_LDB K_MARK {
222 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
223 SKF_AD_OFF + SKF_AD_MARK); }
224 | OP_LDB K_QUEUE {
225 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
226 SKF_AD_OFF + SKF_AD_QUEUE); }
227 | OP_LDB K_HATYPE {
228 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
229 SKF_AD_OFF + SKF_AD_HATYPE); }
230 | OP_LDB K_RXHASH {
231 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
232 SKF_AD_OFF + SKF_AD_RXHASH); }
233 | OP_LDB K_CPU {
234 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
235 SKF_AD_OFF + SKF_AD_CPU); }
236 | OP_LDB K_VLANT {
237 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
238 SKF_AD_OFF + SKF_AD_VLAN_TAG); }
239 | OP_LDB K_VLANP {
240 set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
241 SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); }
242 | OP_LDB K_POFF {
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); }
254 | OP_LDH K_PROTO {
255 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
256 SKF_AD_OFF + SKF_AD_PROTOCOL); }
257 | OP_LDH K_TYPE {
258 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
259 SKF_AD_OFF + SKF_AD_PKTTYPE); }
260 | OP_LDH K_IFIDX {
261 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
262 SKF_AD_OFF + SKF_AD_IFINDEX); }
263 | OP_LDH K_NLATTR {
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); }
269 | OP_LDH K_MARK {
270 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
271 SKF_AD_OFF + SKF_AD_MARK); }
272 | OP_LDH K_QUEUE {
273 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
274 SKF_AD_OFF + SKF_AD_QUEUE); }
275 | OP_LDH K_HATYPE {
276 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
277 SKF_AD_OFF + SKF_AD_HATYPE); }
278 | OP_LDH K_RXHASH {
279 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
280 SKF_AD_OFF + SKF_AD_RXHASH); }
281 | OP_LDH K_CPU {
282 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
283 SKF_AD_OFF + SKF_AD_CPU); }
284 | OP_LDH K_VLANT {
285 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
286 SKF_AD_OFF + SKF_AD_VLAN_TAG); }
287 | OP_LDH K_VLANP {
288 set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
289 SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); }
290 | OP_LDH K_POFF {
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); }
298 | OP_LDI number {
299 set_curr_instr(BPF_LD | BPF_IMM, 0, 0, $2); }
303 : OP_LD '#' number {
304 set_curr_instr(BPF_LD | BPF_IMM, 0, 0, $3); }
305 | OP_LD K_PKT_LEN {
306 set_curr_instr(BPF_LD | BPF_W | BPF_LEN, 0, 0, 0); }
307 | OP_LD K_PROTO {
308 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
309 SKF_AD_OFF + SKF_AD_PROTOCOL); }
310 | OP_LD K_TYPE {
311 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
312 SKF_AD_OFF + SKF_AD_PKTTYPE); }
313 | OP_LD K_IFIDX {
314 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
315 SKF_AD_OFF + SKF_AD_IFINDEX); }
316 | OP_LD K_NLATTR {
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); }
322 | OP_LD K_MARK {
323 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
324 SKF_AD_OFF + SKF_AD_MARK); }
325 | OP_LD K_QUEUE {
326 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
327 SKF_AD_OFF + SKF_AD_QUEUE); }
328 | OP_LD K_HATYPE {
329 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
330 SKF_AD_OFF + SKF_AD_HATYPE); }
331 | OP_LD K_RXHASH {
332 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
333 SKF_AD_OFF + SKF_AD_RXHASH); }
334 | OP_LD K_CPU {
335 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
336 SKF_AD_OFF + SKF_AD_CPU); }
337 | OP_LD K_VLANT {
338 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
339 SKF_AD_OFF + SKF_AD_VLAN_TAG); }
340 | OP_LD K_VLANP {
341 set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
342 SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); }
343 | OP_LD K_POFF {
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); }
356 ldxi
357 : OP_LDXI '#' number {
358 set_curr_instr(BPF_LDX | BPF_IMM, 0, 0, $3); }
359 | OP_LDXI number {
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); }
366 | OP_LDX K_PKT_LEN {
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");
373 } else {
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");
378 } else {
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); }
393 : OP_JMP label {
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); }
422 jneq
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); }
506 jset
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); }
533 | OP_ADD 'x' {
534 set_curr_instr(BPF_ALU | BPF_ADD | BPF_X, 0, 0, 0); }
535 | OP_ADD '%' 'x' {
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); }
542 | OP_SUB 'x' {
543 set_curr_instr(BPF_ALU | BPF_SUB | BPF_X, 0, 0, 0); }
544 | OP_SUB '%' 'x' {
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); }
551 | OP_MUL 'x' {
552 set_curr_instr(BPF_ALU | BPF_MUL | BPF_X, 0, 0, 0); }
553 | OP_MUL '%' 'x' {
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); }
560 | OP_DIV 'x' {
561 set_curr_instr(BPF_ALU | BPF_DIV | BPF_X, 0, 0, 0); }
562 | OP_DIV '%' 'x' {
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); }
569 | OP_MOD 'x' {
570 set_curr_instr(BPF_ALU | BPF_MOD | BPF_X, 0, 0, 0); }
571 | OP_MOD '%' 'x' {
572 set_curr_instr(BPF_ALU | BPF_MOD | BPF_X, 0, 0, 0); }
576 : OP_NEG {
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); }
583 | OP_AND 'x' {
584 set_curr_instr(BPF_ALU | BPF_AND | BPF_X, 0, 0, 0); }
585 | OP_AND '%' 'x' {
586 set_curr_instr(BPF_ALU | BPF_AND | BPF_X, 0, 0, 0); }
590 : OP_OR '#' number {
591 set_curr_instr(BPF_ALU | BPF_OR | BPF_K, 0, 0, $3); }
592 | OP_OR 'x' {
593 set_curr_instr(BPF_ALU | BPF_OR | BPF_X, 0, 0, 0); }
594 | OP_OR '%' 'x' {
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); }
601 | OP_XOR 'x' {
602 set_curr_instr(BPF_ALU | BPF_XOR | BPF_X, 0, 0, 0); }
603 | OP_XOR '%' 'x' {
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); }
610 | OP_LSH 'x' {
611 set_curr_instr(BPF_ALU | BPF_LSH | BPF_X, 0, 0, 0); }
612 | OP_LSH '%' 'x' {
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); }
619 | OP_RSH 'x' {
620 set_curr_instr(BPF_ALU | BPF_RSH | BPF_X, 0, 0, 0); }
621 | OP_RSH '%' 'x' {
622 set_curr_instr(BPF_ALU | BPF_RSH | BPF_X, 0, 0, 0); }
626 : OP_RET 'a' {
627 set_curr_instr(BPF_RET | BPF_A, 0, 0, 0); }
628 | OP_RET '%' 'a' {
629 set_curr_instr(BPF_RET | BPF_A, 0, 0, 0); }
630 | OP_RET 'x' {
631 set_curr_instr(BPF_RET | BPF_X, 0, 0, 0); }
632 | OP_RET '%' 'x' {
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); }
639 : OP_TAX {
640 set_curr_instr(BPF_MISC | BPF_TAX, 0, 0, 0); }
644 : OP_TXA {
645 set_curr_instr(BPF_MISC | BPF_TXA, 0, 0, 0); }
650 static void stage_1_inline(void)
652 yyparse();
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)
686 int i;
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)
697 int i;
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);
706 fflush(stdout);
709 static void pretty_printer_tcpdump(const struct sock_fprog *prog)
711 int i;
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)
722 switch (format) {
723 case 0:
724 pretty_printer_c(prog);
725 break;
726 case 1:
727 pretty_printer_xt_bpf(prog);
728 break;
729 case 2:
730 pretty_printer_tcpdump(prog);
731 break;
732 default:
733 bug();
737 int compile_filter(char *file, int verbose, int bypass, int format,
738 bool invoke_cpp, char *const cpp_argv[])
740 int i;
741 struct sock_fprog res;
742 char tmp_file[128];
743 int ret = 0;
745 memset(tmp_file, 0, sizeof(tmp_file));
747 if (invoke_cpp) {
748 ret = cpp_exec(file, tmp_file, sizeof(tmp_file), cpp_argv);
749 if (ret) {
750 fprintf(stderr, "Failed to invoke C preprocessor!\n");
751 goto exit;
754 file = tmp_file;
757 if (!strncmp("-", file, strlen("-")))
758 yyin = stdin;
759 else
760 yyin = fopen(file, "r");
761 if (!yyin)
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));
770 stage_1_inline();
771 stage_2_label_reduce();
773 res.filter = out;
774 res.len = curr_instr;
776 if (verbose) {
777 printf("Generated program:\n");
778 bpf_dump_all(&res);
781 if (!bypass) {
782 if (verbose) {
783 printf("Validating: ");
784 fflush(stdout);
787 if (__bpf_validate(&res) == 0) {
788 if (verbose)
789 printf("Semantic error! BPF validation failed!\n");
790 else {
791 printf("Semantic error! BPF validation failed! "
792 "Try -V for debugging output!\n");
793 ret = 1;
794 goto exit;
796 } else if (verbose) {
797 printf("is runnable!\n");
801 if (verbose)
802 printf("Result:\n");
804 pretty_printer(&res, format);
806 for (i = 0; i < res.len; ++i) {
807 free(labels[i]);
808 free(labels_jt[i]);
809 free(labels_jf[i]);
810 free(labels_k[i]);
813 if (yyin && yyin != stdin)
814 fclose(yyin);
816 exit:
817 if (invoke_cpp)
818 unlink(tmp_file);
820 return ret;
823 void yyerror(const char *err)
825 panic("Syntax error at line %d: %s! %s!\n",
826 yylineno, yytext, err);