| 1 |
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ |
| 2 |
|
| 3 |
#ifndef __LINUX_FILTER_H |
| 4 |
#define __LINUX_FILTER_H |
| 5 |
|
| 6 |
#include <linux/bpf.h> |
| 7 |
|
| 8 |
#define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \ |
| 9 |
((struct bpf_insn) { \ |
| 10 |
.code = CODE, \ |
| 11 |
.dst_reg = DST, \ |
| 12 |
.src_reg = SRC, \ |
| 13 |
.off = OFF, \ |
| 14 |
.imm = IMM }) |
| 15 |
|
| 16 |
#define BPF_ALU32_IMM(OP, DST, IMM) \ |
| 17 |
((struct bpf_insn) { \ |
| 18 |
.code = BPF_ALU | BPF_OP(OP) | BPF_K, \ |
| 19 |
.dst_reg = DST, \ |
| 20 |
.src_reg = 0, \ |
| 21 |
.off = 0, \ |
| 22 |
.imm = IMM }) |
| 23 |
|
| 24 |
#define BPF_ALU64_IMM(OP, DST, IMM) \ |
| 25 |
((struct bpf_insn) { \ |
| 26 |
.code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \ |
| 27 |
.dst_reg = DST, \ |
| 28 |
.src_reg = 0, \ |
| 29 |
.off = 0, \ |
| 30 |
.imm = IMM }) |
| 31 |
|
| 32 |
#define BPF_MOV64_IMM(DST, IMM) \ |
| 33 |
((struct bpf_insn) { \ |
| 34 |
.code = BPF_ALU64 | BPF_MOV | BPF_K, \ |
| 35 |
.dst_reg = DST, \ |
| 36 |
.src_reg = 0, \ |
| 37 |
.off = 0, \ |
| 38 |
.imm = IMM }) |
| 39 |
|
| 40 |
#define BPF_CALL_REL(DST) \ |
| 41 |
((struct bpf_insn) { \ |
| 42 |
.code = BPF_JMP | BPF_CALL, \ |
| 43 |
.dst_reg = 0, \ |
| 44 |
.src_reg = BPF_PSEUDO_CALL, \ |
| 45 |
.off = 0, \ |
| 46 |
.imm = DST }) |
| 47 |
|
| 48 |
#define BPF_EXIT_INSN() \ |
| 49 |
((struct bpf_insn) { \ |
| 50 |
.code = BPF_JMP | BPF_EXIT, \ |
| 51 |
.dst_reg = 0, \ |
| 52 |
.src_reg = 0, \ |
| 53 |
.off = 0, \ |
| 54 |
.imm = 0 }) |
| 55 |
|
| 56 |
#define BPF_EMIT_CALL(FUNC) \ |
| 57 |
((struct bpf_insn) { \ |
| 58 |
.code = BPF_JMP | BPF_CALL, \ |
| 59 |
.dst_reg = 0, \ |
| 60 |
.src_reg = 0, \ |
| 61 |
.off = 0, \ |
| 62 |
.imm = ((FUNC) - BPF_FUNC_unspec) }) |
| 63 |
|
| 64 |
#define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \ |
| 65 |
((struct bpf_insn) { \ |
| 66 |
.code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ |
| 67 |
.dst_reg = DST, \ |
| 68 |
.src_reg = SRC, \ |
| 69 |
.off = OFF, \ |
| 70 |
.imm = 0 }) |
| 71 |
|
| 72 |
#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \ |
| 73 |
((struct bpf_insn) { \ |
| 74 |
.code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ |
| 75 |
.dst_reg = DST, \ |
| 76 |
.src_reg = SRC, \ |
| 77 |
.off = OFF, \ |
| 78 |
.imm = 0 }) |
| 79 |
|
| 80 |
#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \ |
| 81 |
((struct bpf_insn) { \ |
| 82 |
.code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \ |
| 83 |
.dst_reg = DST, \ |
| 84 |
.src_reg = 0, \ |
| 85 |
.off = OFF, \ |
| 86 |
.imm = IMM }) |
| 87 |
|
| 88 |
#define BPF_MOV64_REG(DST, SRC) \ |
| 89 |
((struct bpf_insn) { \ |
| 90 |
.code = BPF_ALU64 | BPF_MOV | BPF_X, \ |
| 91 |
.dst_reg = DST, \ |
| 92 |
.src_reg = SRC, \ |
| 93 |
.off = 0, \ |
| 94 |
.imm = 0 }) |
| 95 |
|
| 96 |
#define BPF_MOV32_IMM(DST, IMM) \ |
| 97 |
((struct bpf_insn) { \ |
| 98 |
.code = BPF_ALU | BPF_MOV | BPF_K, \ |
| 99 |
.dst_reg = DST, \ |
| 100 |
.src_reg = 0, \ |
| 101 |
.off = 0, \ |
| 102 |
.imm = IMM }) |
| 103 |
|
| 104 |
#define BPF_LD_IMM64_RAW_FULL(DST, SRC, OFF1, OFF2, IMM1, IMM2) \ |
| 105 |
((struct bpf_insn) { \ |
| 106 |
.code = BPF_LD | BPF_DW | BPF_IMM, \ |
| 107 |
.dst_reg = DST, \ |
| 108 |
.src_reg = SRC, \ |
| 109 |
.off = OFF1, \ |
| 110 |
.imm = IMM1 }), \ |
| 111 |
((struct bpf_insn) { \ |
| 112 |
.code = 0, \ |
| 113 |
.dst_reg = 0, \ |
| 114 |
.src_reg = 0, \ |
| 115 |
.off = OFF2, \ |
| 116 |
.imm = IMM2 }) |
| 117 |
|
| 118 |
#define BPF_LD_MAP_FD(DST, MAP_FD) \ |
| 119 |
BPF_LD_IMM64_RAW_FULL(DST, BPF_PSEUDO_MAP_FD, 0, 0, \ |
| 120 |
MAP_FD, 0) |
| 121 |
|
| 122 |
#define BPF_LD_MAP_VALUE(DST, MAP_FD, VALUE_OFF) \ |
| 123 |
BPF_LD_IMM64_RAW_FULL(DST, BPF_PSEUDO_MAP_VALUE, 0, 0, \ |
| 124 |
MAP_FD, VALUE_OFF) |
| 125 |
|
| 126 |
#define BPF_JMP_REG(OP, DST, SRC, OFF) \ |
| 127 |
((struct bpf_insn) { \ |
| 128 |
.code = BPF_JMP | BPF_OP(OP) | BPF_X, \ |
| 129 |
.dst_reg = DST, \ |
| 130 |
.src_reg = SRC, \ |
| 131 |
.off = OFF, \ |
| 132 |
.imm = 0 }) |
| 133 |
|
| 134 |
#define BPF_JMP_IMM(OP, DST, IMM, OFF) \ |
| 135 |
((struct bpf_insn) { \ |
| 136 |
.code = BPF_JMP | BPF_OP(OP) | BPF_K, \ |
| 137 |
.dst_reg = DST, \ |
| 138 |
.src_reg = 0, \ |
| 139 |
.off = OFF, \ |
| 140 |
.imm = IMM }) |
| 141 |
|
| 142 |
#define BPF_JMP32_IMM(OP, DST, IMM, OFF) \ |
| 143 |
((struct bpf_insn) { \ |
| 144 |
.code = BPF_JMP32 | BPF_OP(OP) | BPF_K, \ |
| 145 |
.dst_reg = DST, \ |
| 146 |
.src_reg = 0, \ |
| 147 |
.off = OFF, \ |
| 148 |
.imm = IMM }) |
| 149 |
|
| 150 |
#endif |