/* * netsniff-ng - the packet sniffing beast * By Daniel Borkmann * Copyright 2011 Daniel Borkmann , * Swiss federal institute of technology (ETH Zurich) * Subject to the GPL, version 2. */ /* yacc-func-prefix: yy */ %{ #include #include #include #include #include #include #include #include #include "bpf.h" #include "str.h" #include "xmalloc.h" #include "bpf_parser.tab.h" #include "built_in.h" #include "die.h" #include "cpp.h" static int curr_instr = 0; static struct sock_filter out[BPF_MAXINSNS]; static char *labels[BPF_MAXINSNS]; static char *labels_jt[BPF_MAXINSNS]; static char *labels_jf[BPF_MAXINSNS]; static char *labels_k[BPF_MAXINSNS]; #define YYERROR_VERBOSE 0 #define YYDEBUG 0 #define YYLTYPE_IS_TRIVIAL 1 extern FILE *yyin; extern int yylex(void); extern void yyerror(const char *); extern int yylineno; extern char *yytext; static inline void check_max_instr(void) { if (curr_instr >= BPF_MAXINSNS) panic("Exceeded maximal number of instructions!\n"); } static inline void set_curr_instr(uint16_t code, uint8_t jt, uint8_t jf, uint32_t k) { check_max_instr(); out[curr_instr].code = code; out[curr_instr].jt = jt; out[curr_instr].jf = jf; out[curr_instr].k = k; curr_instr++; } static inline void set_curr_label(char *label) { check_max_instr(); labels[curr_instr] = label; } #define JTL 1 #define JFL 2 #define JKL 3 static inline void set_jmp_label(char *label, int which) { check_max_instr(); switch (which) { case JTL: labels_jt[curr_instr] = label; break; case JFL: labels_jf[curr_instr] = label; break; case JKL: labels_k[curr_instr] = label; break; default: bug(); } } static int find_intr_offset_or_panic(char *label_to_search) { int i, max = curr_instr, ret = -ENOENT; bug_on(!label_to_search); for (i = 0; i < max; ++i) { if (labels[i] != NULL) { /* Both are \0-terminated! */ if (!strcmp(label_to_search, labels[i])) { ret = i; break; } } } if (ret == -ENOENT) panic("No such label!\n"); return ret; } %} %union { char *label; long int number; } %token OP_LDB OP_LDH OP_LD OP_LDX OP_ST OP_STX OP_JMP OP_JEQ OP_JGT OP_JGE %token OP_JSET OP_ADD OP_SUB OP_MUL OP_DIV OP_AND OP_OR OP_XOR OP_LSH OP_RSH %token OP_RET OP_TAX OP_TXA OP_LDXB OP_MOD OP_NEG OP_JNEQ OP_JLT OP_JLE OP_LDI %token OP_LDXI %token K_PKT_LEN K_PROTO K_TYPE K_NLATTR K_NLATTR_NEST K_MARK K_QUEUE K_HATYPE %token K_RXHASH K_CPU K_IFIDX K_VLANT K_VLANP K_POFF %token ':' ',' '[' ']' '(' ')' 'x' 'a' '+' 'M' '*' '&' '#' '%' %token number label %type number %type