^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * BPF asm code parser
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * This program is free software; you can distribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * it under the terms of the GNU General Public License as published
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * by the Free Software Foundation; either version 2 of the License,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * or (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Syntax kept close to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Steven McCanne and Van Jacobson. 1993. The BSD packet filter: a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * architecture for user-level packet capture. In Proceedings of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * USENIX Winter 1993 Conference Proceedings on USENIX Winter 1993
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Conference Proceedings (USENIX'93). USENIX Association, Berkeley,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * CA, USA, 2-2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Licensed under the GNU General Public License, version 2.0 (GPLv2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) %{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/filter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "bpf_exp.yacc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) enum jmp_type { JTL, JFL, JKL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) extern FILE *yyin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) extern int yylineno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) extern int yylex(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) extern void yyerror(const char *str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) extern void bpf_asm_compile(FILE *fp, bool cstyle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void bpf_set_curr_instr(uint16_t op, uint8_t jt, uint8_t jf, uint32_t k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static void bpf_set_curr_label(char *label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static void bpf_set_jmp_label(char *label, enum jmp_type type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) %}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) %union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) char *label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) uint32_t number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) %token OP_LDB OP_LDH OP_LD OP_LDX OP_ST OP_STX OP_JMP OP_JEQ OP_JGT OP_JGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) %token OP_JSET OP_ADD OP_SUB OP_MUL OP_DIV OP_AND OP_OR OP_XOR OP_LSH OP_RSH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) %token OP_RET OP_TAX OP_TXA OP_LDXB OP_MOD OP_NEG OP_JNEQ OP_JLT OP_JLE OP_LDI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) %token OP_LDXI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) %token K_PKT_LEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) %token ':' ',' '[' ']' '(' ')' 'x' 'a' '+' 'M' '*' '&' '#' '%'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) %token extension number label
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) %type <label> label
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) %type <number> extension
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) %type <number> number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) %%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) prog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) : line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) | prog line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) : instr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) | labelled_instr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) labelled_instr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) : labelled instr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) instr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) : ldb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) | ldh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) | ld
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) | ldi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) | ldx
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) | ldxi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) | st
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) | stx
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) | jmp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) | jeq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) | jneq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) | jlt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) | jle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) | jgt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) | jge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) | jset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) | add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) | sub
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) | mul
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) | div
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) | mod
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) | neg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) | and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) | or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) | xor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) | lsh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) | rsh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) | ret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) | tax
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) | txa
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) labelled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) : label ':' { bpf_set_curr_label($1); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ldb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) : OP_LDB '[' 'x' '+' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) bpf_set_curr_instr(BPF_LD | BPF_B | BPF_IND, 0, 0, $5); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) | OP_LDB '[' '%' 'x' '+' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) bpf_set_curr_instr(BPF_LD | BPF_B | BPF_IND, 0, 0, $6); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) | OP_LDB '[' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) bpf_set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) | OP_LDB extension {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) bpf_set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) SKF_AD_OFF + $2); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ldh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) : OP_LDH '[' 'x' '+' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) bpf_set_curr_instr(BPF_LD | BPF_H | BPF_IND, 0, 0, $5); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) | OP_LDH '[' '%' 'x' '+' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) bpf_set_curr_instr(BPF_LD | BPF_H | BPF_IND, 0, 0, $6); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) | OP_LDH '[' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) bpf_set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) | OP_LDH extension {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) bpf_set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) SKF_AD_OFF + $2); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ldi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) : OP_LDI '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) bpf_set_curr_instr(BPF_LD | BPF_IMM, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) | OP_LDI number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) bpf_set_curr_instr(BPF_LD | BPF_IMM, 0, 0, $2); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ld
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) : OP_LD '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) bpf_set_curr_instr(BPF_LD | BPF_IMM, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) | OP_LD K_PKT_LEN {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) bpf_set_curr_instr(BPF_LD | BPF_W | BPF_LEN, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) | OP_LD extension {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) bpf_set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) SKF_AD_OFF + $2); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) | OP_LD 'M' '[' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) bpf_set_curr_instr(BPF_LD | BPF_MEM, 0, 0, $4); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) | OP_LD '[' 'x' '+' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) bpf_set_curr_instr(BPF_LD | BPF_W | BPF_IND, 0, 0, $5); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) | OP_LD '[' '%' 'x' '+' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) bpf_set_curr_instr(BPF_LD | BPF_W | BPF_IND, 0, 0, $6); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) | OP_LD '[' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) bpf_set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ldxi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) : OP_LDXI '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) bpf_set_curr_instr(BPF_LDX | BPF_IMM, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) | OP_LDXI number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) bpf_set_curr_instr(BPF_LDX | BPF_IMM, 0, 0, $2); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ldx
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) : OP_LDX '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) bpf_set_curr_instr(BPF_LDX | BPF_IMM, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) | OP_LDX K_PKT_LEN {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) bpf_set_curr_instr(BPF_LDX | BPF_W | BPF_LEN, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) | OP_LDX 'M' '[' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) bpf_set_curr_instr(BPF_LDX | BPF_MEM, 0, 0, $4); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) | OP_LDXB number '*' '(' '[' number ']' '&' number ')' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if ($2 != 4 || $9 != 0xf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) fprintf(stderr, "ldxb offset not supported!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) bpf_set_curr_instr(BPF_LDX | BPF_MSH | BPF_B, 0, 0, $6); } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) | OP_LDX number '*' '(' '[' number ']' '&' number ')' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if ($2 != 4 || $9 != 0xf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) fprintf(stderr, "ldxb offset not supported!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) bpf_set_curr_instr(BPF_LDX | BPF_MSH | BPF_B, 0, 0, $6); } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) st
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) : OP_ST 'M' '[' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) bpf_set_curr_instr(BPF_ST, 0, 0, $4); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) stx
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) : OP_STX 'M' '[' number ']' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) bpf_set_curr_instr(BPF_STX, 0, 0, $4); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) jmp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) : OP_JMP label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) bpf_set_jmp_label($2, JKL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) bpf_set_curr_instr(BPF_JMP | BPF_JA, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) jeq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) : OP_JEQ '#' number ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) bpf_set_jmp_label($7, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) bpf_set_curr_instr(BPF_JMP | BPF_JEQ | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) | OP_JEQ 'x' ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) bpf_set_jmp_label($4, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) bpf_set_jmp_label($6, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) bpf_set_curr_instr(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) | OP_JEQ '%' 'x' ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) bpf_set_jmp_label($7, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) bpf_set_curr_instr(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) | OP_JEQ '#' number ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) bpf_set_curr_instr(BPF_JMP | BPF_JEQ | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) | OP_JEQ 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) bpf_set_jmp_label($4, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) bpf_set_curr_instr(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) | OP_JEQ '%' 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) bpf_set_curr_instr(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) jneq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) : OP_JNEQ '#' number ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) bpf_set_jmp_label($5, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) bpf_set_curr_instr(BPF_JMP | BPF_JEQ | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) | OP_JNEQ 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) bpf_set_jmp_label($4, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) bpf_set_curr_instr(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) | OP_JNEQ '%' 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) bpf_set_jmp_label($5, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) bpf_set_curr_instr(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) jlt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) : OP_JLT '#' number ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) bpf_set_jmp_label($5, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) bpf_set_curr_instr(BPF_JMP | BPF_JGE | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) | OP_JLT 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) bpf_set_jmp_label($4, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) bpf_set_curr_instr(BPF_JMP | BPF_JGE | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) | OP_JLT '%' 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) bpf_set_jmp_label($5, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) bpf_set_curr_instr(BPF_JMP | BPF_JGE | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) jle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) : OP_JLE '#' number ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) bpf_set_jmp_label($5, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) bpf_set_curr_instr(BPF_JMP | BPF_JGT | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) | OP_JLE 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) bpf_set_jmp_label($4, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) bpf_set_curr_instr(BPF_JMP | BPF_JGT | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) | OP_JLE '%' 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) bpf_set_jmp_label($5, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) bpf_set_curr_instr(BPF_JMP | BPF_JGT | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) jgt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) : OP_JGT '#' number ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) bpf_set_jmp_label($7, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) bpf_set_curr_instr(BPF_JMP | BPF_JGT | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) | OP_JGT 'x' ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) bpf_set_jmp_label($4, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) bpf_set_jmp_label($6, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) bpf_set_curr_instr(BPF_JMP | BPF_JGT | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) | OP_JGT '%' 'x' ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) bpf_set_jmp_label($7, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) bpf_set_curr_instr(BPF_JMP | BPF_JGT | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) | OP_JGT '#' number ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) bpf_set_curr_instr(BPF_JMP | BPF_JGT | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) | OP_JGT 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) bpf_set_jmp_label($4, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) bpf_set_curr_instr(BPF_JMP | BPF_JGT | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) | OP_JGT '%' 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) bpf_set_curr_instr(BPF_JMP | BPF_JGT | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) jge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) : OP_JGE '#' number ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) bpf_set_jmp_label($7, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) bpf_set_curr_instr(BPF_JMP | BPF_JGE | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) | OP_JGE 'x' ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) bpf_set_jmp_label($4, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) bpf_set_jmp_label($6, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) bpf_set_curr_instr(BPF_JMP | BPF_JGE | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) | OP_JGE '%' 'x' ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) bpf_set_jmp_label($7, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) bpf_set_curr_instr(BPF_JMP | BPF_JGE | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) | OP_JGE '#' number ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) bpf_set_curr_instr(BPF_JMP | BPF_JGE | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) | OP_JGE 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) bpf_set_jmp_label($4, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) bpf_set_curr_instr(BPF_JMP | BPF_JGE | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) | OP_JGE '%' 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) bpf_set_curr_instr(BPF_JMP | BPF_JGE | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) jset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) : OP_JSET '#' number ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) bpf_set_jmp_label($7, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) bpf_set_curr_instr(BPF_JMP | BPF_JSET | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) | OP_JSET 'x' ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) bpf_set_jmp_label($4, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) bpf_set_jmp_label($6, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) bpf_set_curr_instr(BPF_JMP | BPF_JSET | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) | OP_JSET '%' 'x' ',' label ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) bpf_set_jmp_label($7, JFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) bpf_set_curr_instr(BPF_JMP | BPF_JSET | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) | OP_JSET '#' number ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) bpf_set_curr_instr(BPF_JMP | BPF_JSET | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) | OP_JSET 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) bpf_set_jmp_label($4, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) bpf_set_curr_instr(BPF_JMP | BPF_JSET | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) | OP_JSET '%' 'x' ',' label {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) bpf_set_jmp_label($5, JTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) bpf_set_curr_instr(BPF_JMP | BPF_JSET | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) : OP_ADD '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) bpf_set_curr_instr(BPF_ALU | BPF_ADD | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) | OP_ADD 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) bpf_set_curr_instr(BPF_ALU | BPF_ADD | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) | OP_ADD '%' 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) bpf_set_curr_instr(BPF_ALU | BPF_ADD | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) sub
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) : OP_SUB '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) bpf_set_curr_instr(BPF_ALU | BPF_SUB | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) | OP_SUB 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) bpf_set_curr_instr(BPF_ALU | BPF_SUB | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) | OP_SUB '%' 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) bpf_set_curr_instr(BPF_ALU | BPF_SUB | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) mul
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) : OP_MUL '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) bpf_set_curr_instr(BPF_ALU | BPF_MUL | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) | OP_MUL 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) bpf_set_curr_instr(BPF_ALU | BPF_MUL | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) | OP_MUL '%' 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) bpf_set_curr_instr(BPF_ALU | BPF_MUL | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) div
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) : OP_DIV '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) bpf_set_curr_instr(BPF_ALU | BPF_DIV | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) | OP_DIV 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) bpf_set_curr_instr(BPF_ALU | BPF_DIV | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) | OP_DIV '%' 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) bpf_set_curr_instr(BPF_ALU | BPF_DIV | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) mod
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) : OP_MOD '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) bpf_set_curr_instr(BPF_ALU | BPF_MOD | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) | OP_MOD 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) bpf_set_curr_instr(BPF_ALU | BPF_MOD | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) | OP_MOD '%' 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) bpf_set_curr_instr(BPF_ALU | BPF_MOD | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) neg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) : OP_NEG {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) bpf_set_curr_instr(BPF_ALU | BPF_NEG, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) : OP_AND '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) bpf_set_curr_instr(BPF_ALU | BPF_AND | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) | OP_AND 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) bpf_set_curr_instr(BPF_ALU | BPF_AND | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) | OP_AND '%' 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) bpf_set_curr_instr(BPF_ALU | BPF_AND | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) : OP_OR '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) bpf_set_curr_instr(BPF_ALU | BPF_OR | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) | OP_OR 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) bpf_set_curr_instr(BPF_ALU | BPF_OR | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) | OP_OR '%' 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) bpf_set_curr_instr(BPF_ALU | BPF_OR | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) xor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) : OP_XOR '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) bpf_set_curr_instr(BPF_ALU | BPF_XOR | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) | OP_XOR 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) bpf_set_curr_instr(BPF_ALU | BPF_XOR | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) | OP_XOR '%' 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) bpf_set_curr_instr(BPF_ALU | BPF_XOR | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) lsh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) : OP_LSH '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) bpf_set_curr_instr(BPF_ALU | BPF_LSH | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) | OP_LSH 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) bpf_set_curr_instr(BPF_ALU | BPF_LSH | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) | OP_LSH '%' 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) bpf_set_curr_instr(BPF_ALU | BPF_LSH | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) rsh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) : OP_RSH '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) bpf_set_curr_instr(BPF_ALU | BPF_RSH | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) | OP_RSH 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) bpf_set_curr_instr(BPF_ALU | BPF_RSH | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) | OP_RSH '%' 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) bpf_set_curr_instr(BPF_ALU | BPF_RSH | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) ret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) : OP_RET 'a' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) bpf_set_curr_instr(BPF_RET | BPF_A, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) | OP_RET '%' 'a' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) bpf_set_curr_instr(BPF_RET | BPF_A, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) | OP_RET 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) bpf_set_curr_instr(BPF_RET | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) | OP_RET '%' 'x' {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) bpf_set_curr_instr(BPF_RET | BPF_X, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) | OP_RET '#' number {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) bpf_set_curr_instr(BPF_RET | BPF_K, 0, 0, $3); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) tax
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) : OP_TAX {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) bpf_set_curr_instr(BPF_MISC | BPF_TAX, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) txa
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) : OP_TXA {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) bpf_set_curr_instr(BPF_MISC | BPF_TXA, 0, 0, 0); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) %%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) static int curr_instr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static struct sock_filter out[BPF_MAXINSNS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static char **labels, **labels_jt, **labels_jf, **labels_k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static void bpf_assert_max(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (curr_instr >= BPF_MAXINSNS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) fprintf(stderr, "only max %u insns allowed!\n", BPF_MAXINSNS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) static void bpf_set_curr_instr(uint16_t code, uint8_t jt, uint8_t jf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) uint32_t k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) bpf_assert_max();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) out[curr_instr].code = code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) out[curr_instr].jt = jt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) out[curr_instr].jf = jf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) out[curr_instr].k = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) curr_instr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static void bpf_set_curr_label(char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) bpf_assert_max();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) labels[curr_instr] = label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static void bpf_set_jmp_label(char *label, enum jmp_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) bpf_assert_max();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) case JTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) labels_jt[curr_instr] = label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) case JFL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) labels_jf[curr_instr] = label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) case JKL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) labels_k[curr_instr] = label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) static int bpf_find_insns_offset(const char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) int i, max = curr_instr, ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) for (i = 0; i < max; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) if (labels[i] && !strcmp(label, labels[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) ret = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (ret == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) fprintf(stderr, "no such label \'%s\'!\n", label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static void bpf_stage_1_insert_insns(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) yyparse();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) static void bpf_reduce_k_jumps(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) for (i = 0; i < curr_instr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (labels_k[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) int off = bpf_find_insns_offset(labels_k[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) out[i].k = (uint32_t) (off - i - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) static uint8_t bpf_encode_jt_jf_offset(int off, int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) int delta = off - i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (delta < 0 || delta > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) fprintf(stderr, "warning: insn #%d jumps to insn #%d, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) "which is out of range\n", i, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return (uint8_t) delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static void bpf_reduce_jt_jumps(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) for (i = 0; i < curr_instr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (labels_jt[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) int off = bpf_find_insns_offset(labels_jt[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) out[i].jt = bpf_encode_jt_jf_offset(off, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) static void bpf_reduce_jf_jumps(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) for (i = 0; i < curr_instr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (labels_jf[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) int off = bpf_find_insns_offset(labels_jf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) out[i].jf = bpf_encode_jt_jf_offset(off, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static void bpf_stage_2_reduce_labels(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) bpf_reduce_k_jumps();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) bpf_reduce_jt_jumps();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) bpf_reduce_jf_jumps();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) static void bpf_pretty_print_c(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) for (i = 0; i < curr_instr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) printf("{ %#04x, %2u, %2u, %#010x },\n", out[i].code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) out[i].jt, out[i].jf, out[i].k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) static void bpf_pretty_print(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) printf("%u,", curr_instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) for (i = 0; i < curr_instr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) printf("%u %u %u %u,", out[i].code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) out[i].jt, out[i].jf, out[i].k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) static void bpf_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) memset(out, 0, sizeof(out));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) labels = calloc(BPF_MAXINSNS, sizeof(*labels));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) assert(labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) labels_jt = calloc(BPF_MAXINSNS, sizeof(*labels_jt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) assert(labels_jt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) labels_jf = calloc(BPF_MAXINSNS, sizeof(*labels_jf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) assert(labels_jf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) labels_k = calloc(BPF_MAXINSNS, sizeof(*labels_k));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) assert(labels_k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) static void bpf_destroy_labels(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) for (i = 0; i < curr_instr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) free(labels_jf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) free(labels_jt[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) free(labels_k[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) free(labels[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) static void bpf_destroy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) bpf_destroy_labels();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) free(labels_jt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) free(labels_jf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) free(labels_k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) free(labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) void bpf_asm_compile(FILE *fp, bool cstyle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) yyin = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) bpf_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) bpf_stage_1_insert_insns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) bpf_stage_2_reduce_labels();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) bpf_destroy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (cstyle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) bpf_pretty_print_c();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) bpf_pretty_print();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (fp != stdin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) fclose(yyin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) void yyerror(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) fprintf(stderr, "error: %s at line %d\n", str, yylineno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }