1
2
3
4#ifndef __TOOLS_LINUX_FILTER_H
5#define __TOOLS_LINUX_FILTER_H
6
7#include <linux/bpf.h>
8
9
10
11
12
13#define BPF_REG_ARG1 BPF_REG_1
14#define BPF_REG_ARG2 BPF_REG_2
15#define BPF_REG_ARG3 BPF_REG_3
16#define BPF_REG_ARG4 BPF_REG_4
17#define BPF_REG_ARG5 BPF_REG_5
18#define BPF_REG_CTX BPF_REG_6
19#define BPF_REG_FP BPF_REG_10
20
21
22#define BPF_REG_A BPF_REG_0
23#define BPF_REG_X BPF_REG_7
24#define BPF_REG_TMP BPF_REG_8
25
26
27#define MAX_BPF_STACK 512
28
29
30
31
32
33#define BPF_ALU64_REG(OP, DST, SRC) \
34 ((struct bpf_insn) { \
35 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
36 .dst_reg = DST, \
37 .src_reg = SRC, \
38 .off = 0, \
39 .imm = 0 })
40
41#define BPF_ALU32_REG(OP, DST, SRC) \
42 ((struct bpf_insn) { \
43 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
44 .dst_reg = DST, \
45 .src_reg = SRC, \
46 .off = 0, \
47 .imm = 0 })
48
49
50
51#define BPF_ALU64_IMM(OP, DST, IMM) \
52 ((struct bpf_insn) { \
53 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
54 .dst_reg = DST, \
55 .src_reg = 0, \
56 .off = 0, \
57 .imm = IMM })
58
59#define BPF_ALU32_IMM(OP, DST, IMM) \
60 ((struct bpf_insn) { \
61 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
62 .dst_reg = DST, \
63 .src_reg = 0, \
64 .off = 0, \
65 .imm = IMM })
66
67
68
69#define BPF_ENDIAN(TYPE, DST, LEN) \
70 ((struct bpf_insn) { \
71 .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
72 .dst_reg = DST, \
73 .src_reg = 0, \
74 .off = 0, \
75 .imm = LEN })
76
77
78
79#define BPF_MOV64_REG(DST, SRC) \
80 ((struct bpf_insn) { \
81 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
82 .dst_reg = DST, \
83 .src_reg = SRC, \
84 .off = 0, \
85 .imm = 0 })
86
87#define BPF_MOV32_REG(DST, SRC) \
88 ((struct bpf_insn) { \
89 .code = BPF_ALU | BPF_MOV | BPF_X, \
90 .dst_reg = DST, \
91 .src_reg = SRC, \
92 .off = 0, \
93 .imm = 0 })
94
95
96
97#define BPF_MOV64_IMM(DST, IMM) \
98 ((struct bpf_insn) { \
99 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
100 .dst_reg = DST, \
101 .src_reg = 0, \
102 .off = 0, \
103 .imm = IMM })
104
105#define BPF_MOV32_IMM(DST, IMM) \
106 ((struct bpf_insn) { \
107 .code = BPF_ALU | BPF_MOV | BPF_K, \
108 .dst_reg = DST, \
109 .src_reg = 0, \
110 .off = 0, \
111 .imm = IMM })
112
113
114#define BPF_LD_IMM64(DST, IMM) \
115 BPF_LD_IMM64_RAW(DST, 0, IMM)
116
117#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
118 ((struct bpf_insn) { \
119 .code = BPF_LD | BPF_DW | BPF_IMM, \
120 .dst_reg = DST, \
121 .src_reg = SRC, \
122 .off = 0, \
123 .imm = (__u32) (IMM) }), \
124 ((struct bpf_insn) { \
125 .code = 0, \
126 .dst_reg = 0, \
127 .src_reg = 0, \
128 .off = 0, \
129 .imm = ((__u64) (IMM)) >> 32 })
130
131#define BPF_PSEUDO_MAP_FD 1
132
133
134#define BPF_LD_MAP_FD(DST, MAP_FD) \
135 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
136
137
138
139#define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
140 ((struct bpf_insn) { \
141 .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
142 .dst_reg = DST, \
143 .src_reg = SRC, \
144 .off = 0, \
145 .imm = IMM })
146
147#define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
148 ((struct bpf_insn) { \
149 .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
150 .dst_reg = DST, \
151 .src_reg = SRC, \
152 .off = 0, \
153 .imm = IMM })
154
155
156
157#define BPF_LD_ABS(SIZE, IMM) \
158 ((struct bpf_insn) { \
159 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
160 .dst_reg = 0, \
161 .src_reg = 0, \
162 .off = 0, \
163 .imm = IMM })
164
165
166
167#define BPF_LD_IND(SIZE, SRC, IMM) \
168 ((struct bpf_insn) { \
169 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
170 .dst_reg = 0, \
171 .src_reg = SRC, \
172 .off = 0, \
173 .imm = IMM })
174
175
176
177#define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
178 ((struct bpf_insn) { \
179 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
180 .dst_reg = DST, \
181 .src_reg = SRC, \
182 .off = OFF, \
183 .imm = 0 })
184
185
186
187#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
188 ((struct bpf_insn) { \
189 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
190 .dst_reg = DST, \
191 .src_reg = SRC, \
192 .off = OFF, \
193 .imm = 0 })
194
195
196
197#define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
198 ((struct bpf_insn) { \
199 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
200 .dst_reg = DST, \
201 .src_reg = SRC, \
202 .off = OFF, \
203 .imm = 0 })
204
205
206
207#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
208 ((struct bpf_insn) { \
209 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
210 .dst_reg = DST, \
211 .src_reg = 0, \
212 .off = OFF, \
213 .imm = IMM })
214
215
216
217#define BPF_JMP_REG(OP, DST, SRC, OFF) \
218 ((struct bpf_insn) { \
219 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
220 .dst_reg = DST, \
221 .src_reg = SRC, \
222 .off = OFF, \
223 .imm = 0 })
224
225
226
227#define BPF_JMP_IMM(OP, DST, IMM, OFF) \
228 ((struct bpf_insn) { \
229 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
230 .dst_reg = DST, \
231 .src_reg = 0, \
232 .off = OFF, \
233 .imm = IMM })
234
235
236
237#define BPF_JMP_A(OFF) \
238 ((struct bpf_insn) { \
239 .code = BPF_JMP | BPF_JA, \
240 .dst_reg = 0, \
241 .src_reg = 0, \
242 .off = OFF, \
243 .imm = 0 })
244
245
246
247#define BPF_EMIT_CALL(FUNC) \
248 ((struct bpf_insn) { \
249 .code = BPF_JMP | BPF_CALL, \
250 .dst_reg = 0, \
251 .src_reg = 0, \
252 .off = 0, \
253 .imm = ((FUNC) - BPF_FUNC_unspec) })
254
255
256
257#define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
258 ((struct bpf_insn) { \
259 .code = CODE, \
260 .dst_reg = DST, \
261 .src_reg = SRC, \
262 .off = OFF, \
263 .imm = IMM })
264
265
266
267#define BPF_EXIT_INSN() \
268 ((struct bpf_insn) { \
269 .code = BPF_JMP | BPF_EXIT, \
270 .dst_reg = 0, \
271 .src_reg = 0, \
272 .off = 0, \
273 .imm = 0 })
274
275#endif
276