Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Linux Socket Filter Data Structures | |
3 | */ | |
1da177e4 LT |
4 | #ifndef __LINUX_FILTER_H__ |
5 | #define __LINUX_FILTER_H__ | |
6 | ||
60063497 | 7 | #include <linux/atomic.h> |
0c5fe1b4 | 8 | #include <linux/compat.h> |
9f12fbe6 | 9 | #include <linux/skbuff.h> |
d45ed4a4 | 10 | #include <linux/workqueue.h> |
607ca46e | 11 | #include <uapi/linux/filter.h> |
60a3b225 DB |
12 | #include <asm/cacheflush.h> |
13 | ||
14 | struct sk_buff; | |
15 | struct sock; | |
16 | struct seccomp_data; | |
792d4b5c | 17 | |
bd4cf0ed AS |
18 | /* Internally used and optimized filter representation with extended |
19 | * instruction set based on top of classic BPF. | |
0c5fe1b4 | 20 | */ |
bd4cf0ed AS |
21 | |
22 | /* instruction classes */ | |
23 | #define BPF_ALU64 0x07 /* alu mode in double word width */ | |
24 | ||
25 | /* ld/ldx fields */ | |
26 | #define BPF_DW 0x18 /* double word */ | |
27 | #define BPF_XADD 0xc0 /* exclusive add */ | |
28 | ||
29 | /* alu/jmp fields */ | |
30 | #define BPF_MOV 0xb0 /* mov reg to reg */ | |
31 | #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */ | |
32 | ||
33 | /* change endianness of a register */ | |
34 | #define BPF_END 0xd0 /* flags for endianness conversion: */ | |
35 | #define BPF_TO_LE 0x00 /* convert to little-endian */ | |
36 | #define BPF_TO_BE 0x08 /* convert to big-endian */ | |
37 | #define BPF_FROM_LE BPF_TO_LE | |
38 | #define BPF_FROM_BE BPF_TO_BE | |
39 | ||
40 | #define BPF_JNE 0x50 /* jump != */ | |
41 | #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */ | |
42 | #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */ | |
43 | #define BPF_CALL 0x80 /* function call */ | |
44 | #define BPF_EXIT 0x90 /* function return */ | |
45 | ||
30743837 DB |
46 | /* Register numbers */ |
47 | enum { | |
48 | BPF_REG_0 = 0, | |
49 | BPF_REG_1, | |
50 | BPF_REG_2, | |
51 | BPF_REG_3, | |
52 | BPF_REG_4, | |
53 | BPF_REG_5, | |
54 | BPF_REG_6, | |
55 | BPF_REG_7, | |
56 | BPF_REG_8, | |
57 | BPF_REG_9, | |
58 | BPF_REG_10, | |
59 | __MAX_BPF_REG, | |
60 | }; | |
61 | ||
bd4cf0ed | 62 | /* BPF has 10 general purpose 64-bit registers and stack frame. */ |
30743837 DB |
63 | #define MAX_BPF_REG __MAX_BPF_REG |
64 | ||
65 | /* ArgX, context and stack frame pointer register positions. Note, | |
66 | * Arg1, Arg2, Arg3, etc are used as argument mappings of function | |
67 | * calls in BPF_CALL instruction. | |
68 | */ | |
69 | #define BPF_REG_ARG1 BPF_REG_1 | |
70 | #define BPF_REG_ARG2 BPF_REG_2 | |
71 | #define BPF_REG_ARG3 BPF_REG_3 | |
72 | #define BPF_REG_ARG4 BPF_REG_4 | |
73 | #define BPF_REG_ARG5 BPF_REG_5 | |
74 | #define BPF_REG_CTX BPF_REG_6 | |
75 | #define BPF_REG_FP BPF_REG_10 | |
76 | ||
77 | /* Additional register mappings for converted user programs. */ | |
78 | #define BPF_REG_A BPF_REG_0 | |
79 | #define BPF_REG_X BPF_REG_7 | |
80 | #define BPF_REG_TMP BPF_REG_8 | |
bd4cf0ed AS |
81 | |
82 | /* BPF program can access up to 512 bytes of stack space. */ | |
83 | #define MAX_BPF_STACK 512 | |
84 | ||
f8f6d679 DB |
85 | /* Helper macros for filter block array initializers. */ |
86 | ||
e430f34e | 87 | /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ |
f8f6d679 | 88 | |
e430f34e | 89 | #define BPF_ALU64_REG(OP, DST, SRC) \ |
2695fb55 | 90 | ((struct bpf_insn) { \ |
f8f6d679 | 91 | .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \ |
e430f34e AS |
92 | .dst_reg = DST, \ |
93 | .src_reg = SRC, \ | |
f8f6d679 DB |
94 | .off = 0, \ |
95 | .imm = 0 }) | |
96 | ||
e430f34e | 97 | #define BPF_ALU32_REG(OP, DST, SRC) \ |
2695fb55 | 98 | ((struct bpf_insn) { \ |
f8f6d679 | 99 | .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ |
e430f34e AS |
100 | .dst_reg = DST, \ |
101 | .src_reg = SRC, \ | |
f8f6d679 DB |
102 | .off = 0, \ |
103 | .imm = 0 }) | |
104 | ||
e430f34e | 105 | /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ |
f8f6d679 | 106 | |
e430f34e | 107 | #define BPF_ALU64_IMM(OP, DST, IMM) \ |
2695fb55 | 108 | ((struct bpf_insn) { \ |
f8f6d679 | 109 | .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \ |
e430f34e AS |
110 | .dst_reg = DST, \ |
111 | .src_reg = 0, \ | |
f8f6d679 DB |
112 | .off = 0, \ |
113 | .imm = IMM }) | |
114 | ||
e430f34e | 115 | #define BPF_ALU32_IMM(OP, DST, IMM) \ |
2695fb55 | 116 | ((struct bpf_insn) { \ |
f8f6d679 | 117 | .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ |
e430f34e AS |
118 | .dst_reg = DST, \ |
119 | .src_reg = 0, \ | |
f8f6d679 DB |
120 | .off = 0, \ |
121 | .imm = IMM }) | |
122 | ||
123 | /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */ | |
124 | ||
e430f34e | 125 | #define BPF_ENDIAN(TYPE, DST, LEN) \ |
2695fb55 | 126 | ((struct bpf_insn) { \ |
f8f6d679 | 127 | .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \ |
e430f34e AS |
128 | .dst_reg = DST, \ |
129 | .src_reg = 0, \ | |
f8f6d679 DB |
130 | .off = 0, \ |
131 | .imm = LEN }) | |
132 | ||
e430f34e | 133 | /* Short form of mov, dst_reg = src_reg */ |
f8f6d679 | 134 | |
e430f34e | 135 | #define BPF_MOV64_REG(DST, SRC) \ |
2695fb55 | 136 | ((struct bpf_insn) { \ |
f8f6d679 | 137 | .code = BPF_ALU64 | BPF_MOV | BPF_X, \ |
e430f34e AS |
138 | .dst_reg = DST, \ |
139 | .src_reg = SRC, \ | |
f8f6d679 DB |
140 | .off = 0, \ |
141 | .imm = 0 }) | |
142 | ||
e430f34e | 143 | #define BPF_MOV32_REG(DST, SRC) \ |
2695fb55 | 144 | ((struct bpf_insn) { \ |
f8f6d679 | 145 | .code = BPF_ALU | BPF_MOV | BPF_X, \ |
e430f34e AS |
146 | .dst_reg = DST, \ |
147 | .src_reg = SRC, \ | |
f8f6d679 DB |
148 | .off = 0, \ |
149 | .imm = 0 }) | |
150 | ||
e430f34e | 151 | /* Short form of mov, dst_reg = imm32 */ |
f8f6d679 | 152 | |
e430f34e | 153 | #define BPF_MOV64_IMM(DST, IMM) \ |
2695fb55 | 154 | ((struct bpf_insn) { \ |
f8f6d679 | 155 | .code = BPF_ALU64 | BPF_MOV | BPF_K, \ |
e430f34e AS |
156 | .dst_reg = DST, \ |
157 | .src_reg = 0, \ | |
f8f6d679 DB |
158 | .off = 0, \ |
159 | .imm = IMM }) | |
160 | ||
e430f34e | 161 | #define BPF_MOV32_IMM(DST, IMM) \ |
2695fb55 | 162 | ((struct bpf_insn) { \ |
f8f6d679 | 163 | .code = BPF_ALU | BPF_MOV | BPF_K, \ |
e430f34e AS |
164 | .dst_reg = DST, \ |
165 | .src_reg = 0, \ | |
f8f6d679 DB |
166 | .off = 0, \ |
167 | .imm = IMM }) | |
168 | ||
02ab695b AS |
169 | /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */ |
170 | #define BPF_LD_IMM64(DST, IMM) \ | |
171 | BPF_LD_IMM64_RAW(DST, 0, IMM) | |
172 | ||
173 | #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \ | |
174 | ((struct bpf_insn) { \ | |
175 | .code = BPF_LD | BPF_DW | BPF_IMM, \ | |
176 | .dst_reg = DST, \ | |
177 | .src_reg = SRC, \ | |
178 | .off = 0, \ | |
179 | .imm = (__u32) (IMM) }), \ | |
180 | ((struct bpf_insn) { \ | |
181 | .code = 0, /* zero is reserved opcode */ \ | |
182 | .dst_reg = 0, \ | |
183 | .src_reg = 0, \ | |
184 | .off = 0, \ | |
185 | .imm = ((__u64) (IMM)) >> 32 }) | |
186 | ||
e430f34e | 187 | /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */ |
f8f6d679 | 188 | |
e430f34e | 189 | #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \ |
2695fb55 | 190 | ((struct bpf_insn) { \ |
f8f6d679 | 191 | .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \ |
e430f34e AS |
192 | .dst_reg = DST, \ |
193 | .src_reg = SRC, \ | |
f8f6d679 DB |
194 | .off = 0, \ |
195 | .imm = IMM }) | |
196 | ||
e430f34e | 197 | #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \ |
2695fb55 | 198 | ((struct bpf_insn) { \ |
f8f6d679 | 199 | .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \ |
e430f34e AS |
200 | .dst_reg = DST, \ |
201 | .src_reg = SRC, \ | |
f8f6d679 DB |
202 | .off = 0, \ |
203 | .imm = IMM }) | |
204 | ||
e430f34e | 205 | /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ |
f8f6d679 | 206 | |
e430f34e | 207 | #define BPF_LD_ABS(SIZE, IMM) \ |
2695fb55 | 208 | ((struct bpf_insn) { \ |
f8f6d679 | 209 | .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ |
e430f34e AS |
210 | .dst_reg = 0, \ |
211 | .src_reg = 0, \ | |
f8f6d679 | 212 | .off = 0, \ |
e430f34e | 213 | .imm = IMM }) |
f8f6d679 | 214 | |
e430f34e | 215 | /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */ |
f8f6d679 | 216 | |
e430f34e | 217 | #define BPF_LD_IND(SIZE, SRC, IMM) \ |
2695fb55 | 218 | ((struct bpf_insn) { \ |
f8f6d679 | 219 | .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \ |
e430f34e AS |
220 | .dst_reg = 0, \ |
221 | .src_reg = SRC, \ | |
f8f6d679 | 222 | .off = 0, \ |
e430f34e | 223 | .imm = IMM }) |
f8f6d679 | 224 | |
e430f34e | 225 | /* Memory load, dst_reg = *(uint *) (src_reg + off16) */ |
f8f6d679 | 226 | |
e430f34e | 227 | #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \ |
2695fb55 | 228 | ((struct bpf_insn) { \ |
f8f6d679 | 229 | .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ |
e430f34e AS |
230 | .dst_reg = DST, \ |
231 | .src_reg = SRC, \ | |
f8f6d679 DB |
232 | .off = OFF, \ |
233 | .imm = 0 }) | |
234 | ||
e430f34e AS |
235 | /* Memory store, *(uint *) (dst_reg + off16) = src_reg */ |
236 | ||
237 | #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \ | |
2695fb55 | 238 | ((struct bpf_insn) { \ |
f8f6d679 | 239 | .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ |
e430f34e AS |
240 | .dst_reg = DST, \ |
241 | .src_reg = SRC, \ | |
f8f6d679 DB |
242 | .off = OFF, \ |
243 | .imm = 0 }) | |
244 | ||
e430f34e AS |
245 | /* Memory store, *(uint *) (dst_reg + off16) = imm32 */ |
246 | ||
247 | #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \ | |
2695fb55 | 248 | ((struct bpf_insn) { \ |
e430f34e AS |
249 | .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \ |
250 | .dst_reg = DST, \ | |
251 | .src_reg = 0, \ | |
252 | .off = OFF, \ | |
253 | .imm = IMM }) | |
254 | ||
255 | /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */ | |
f8f6d679 | 256 | |
e430f34e | 257 | #define BPF_JMP_REG(OP, DST, SRC, OFF) \ |
2695fb55 | 258 | ((struct bpf_insn) { \ |
f8f6d679 | 259 | .code = BPF_JMP | BPF_OP(OP) | BPF_X, \ |
e430f34e AS |
260 | .dst_reg = DST, \ |
261 | .src_reg = SRC, \ | |
f8f6d679 DB |
262 | .off = OFF, \ |
263 | .imm = 0 }) | |
264 | ||
e430f34e | 265 | /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ |
f8f6d679 | 266 | |
e430f34e | 267 | #define BPF_JMP_IMM(OP, DST, IMM, OFF) \ |
2695fb55 | 268 | ((struct bpf_insn) { \ |
f8f6d679 | 269 | .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ |
e430f34e AS |
270 | .dst_reg = DST, \ |
271 | .src_reg = 0, \ | |
f8f6d679 DB |
272 | .off = OFF, \ |
273 | .imm = IMM }) | |
274 | ||
275 | /* Function call */ | |
276 | ||
277 | #define BPF_EMIT_CALL(FUNC) \ | |
2695fb55 | 278 | ((struct bpf_insn) { \ |
f8f6d679 | 279 | .code = BPF_JMP | BPF_CALL, \ |
e430f34e AS |
280 | .dst_reg = 0, \ |
281 | .src_reg = 0, \ | |
f8f6d679 DB |
282 | .off = 0, \ |
283 | .imm = ((FUNC) - __bpf_call_base) }) | |
284 | ||
285 | /* Raw code statement block */ | |
286 | ||
e430f34e | 287 | #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \ |
2695fb55 | 288 | ((struct bpf_insn) { \ |
f8f6d679 | 289 | .code = CODE, \ |
e430f34e AS |
290 | .dst_reg = DST, \ |
291 | .src_reg = SRC, \ | |
f8f6d679 DB |
292 | .off = OFF, \ |
293 | .imm = IMM }) | |
294 | ||
295 | /* Program exit */ | |
296 | ||
297 | #define BPF_EXIT_INSN() \ | |
2695fb55 | 298 | ((struct bpf_insn) { \ |
f8f6d679 | 299 | .code = BPF_JMP | BPF_EXIT, \ |
e430f34e AS |
300 | .dst_reg = 0, \ |
301 | .src_reg = 0, \ | |
f8f6d679 DB |
302 | .off = 0, \ |
303 | .imm = 0 }) | |
304 | ||
305 | #define bytes_to_bpf_size(bytes) \ | |
306 | ({ \ | |
307 | int bpf_size = -EINVAL; \ | |
308 | \ | |
309 | if (bytes == sizeof(u8)) \ | |
310 | bpf_size = BPF_B; \ | |
311 | else if (bytes == sizeof(u16)) \ | |
312 | bpf_size = BPF_H; \ | |
313 | else if (bytes == sizeof(u32)) \ | |
314 | bpf_size = BPF_W; \ | |
315 | else if (bytes == sizeof(u64)) \ | |
316 | bpf_size = BPF_DW; \ | |
317 | \ | |
318 | bpf_size; \ | |
319 | }) | |
9739eef1 | 320 | |
30743837 | 321 | /* Macro to invoke filter function. */ |
7ae457c1 AS |
322 | #define SK_RUN_FILTER(filter, ctx) \ |
323 | (*filter->prog->bpf_func)(ctx, filter->prog->insnsi) | |
bd4cf0ed | 324 | |
2695fb55 | 325 | struct bpf_insn { |
bd4cf0ed | 326 | __u8 code; /* opcode */ |
e430f34e AS |
327 | __u8 dst_reg:4; /* dest register */ |
328 | __u8 src_reg:4; /* source register */ | |
bd4cf0ed AS |
329 | __s16 off; /* signed offset */ |
330 | __s32 imm; /* signed immediate constant */ | |
331 | }; | |
332 | ||
333 | #ifdef CONFIG_COMPAT | |
334 | /* A struct sock_filter is architecture independent. */ | |
0c5fe1b4 WD |
335 | struct compat_sock_fprog { |
336 | u16 len; | |
bd4cf0ed | 337 | compat_uptr_t filter; /* struct sock_filter * */ |
0c5fe1b4 WD |
338 | }; |
339 | #endif | |
340 | ||
a3ea269b DB |
341 | struct sock_fprog_kern { |
342 | u16 len; | |
343 | struct sock_filter *filter; | |
344 | }; | |
345 | ||
60a3b225 DB |
346 | struct bpf_work_struct { |
347 | struct bpf_prog *prog; | |
348 | struct work_struct work; | |
349 | }; | |
792d4b5c | 350 | |
7ae457c1 | 351 | struct bpf_prog { |
60a3b225 | 352 | u32 pages; /* Number of allocated pages */ |
f8bbbfc3 DB |
353 | u32 jited:1, /* Is our filter JIT'ed? */ |
354 | len:31; /* Number of filter blocks */ | |
a3ea269b | 355 | struct sock_fprog_kern *orig_prog; /* Original BPF program */ |
60a3b225 | 356 | struct bpf_work_struct *work; /* Deferred free work struct */ |
0a14842f | 357 | unsigned int (*bpf_func)(const struct sk_buff *skb, |
2695fb55 | 358 | const struct bpf_insn *filter); |
60a3b225 | 359 | /* Instructions for interpreter */ |
d45ed4a4 | 360 | union { |
bd4cf0ed | 361 | struct sock_filter insns[0]; |
2695fb55 | 362 | struct bpf_insn insnsi[0]; |
d45ed4a4 | 363 | }; |
b715631f SH |
364 | }; |
365 | ||
7ae457c1 AS |
366 | struct sk_filter { |
367 | atomic_t refcnt; | |
368 | struct rcu_head rcu; | |
369 | struct bpf_prog *prog; | |
370 | }; | |
371 | ||
372 | #define BPF_PROG_RUN(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi) | |
373 | ||
374 | static inline unsigned int bpf_prog_size(unsigned int proglen) | |
b715631f | 375 | { |
7ae457c1 AS |
376 | return max(sizeof(struct bpf_prog), |
377 | offsetof(struct bpf_prog, insns[proglen])); | |
b715631f SH |
378 | } |
379 | ||
009937e7 | 380 | #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0])) |
a3ea269b | 381 | |
60a3b225 DB |
382 | #ifdef CONFIG_DEBUG_SET_MODULE_RONX |
383 | static inline void bpf_prog_lock_ro(struct bpf_prog *fp) | |
384 | { | |
385 | set_memory_ro((unsigned long)fp, fp->pages); | |
386 | } | |
387 | ||
388 | static inline void bpf_prog_unlock_ro(struct bpf_prog *fp) | |
389 | { | |
390 | set_memory_rw((unsigned long)fp, fp->pages); | |
391 | } | |
392 | #else | |
393 | static inline void bpf_prog_lock_ro(struct bpf_prog *fp) | |
394 | { | |
395 | } | |
396 | ||
397 | static inline void bpf_prog_unlock_ro(struct bpf_prog *fp) | |
398 | { | |
399 | } | |
400 | #endif /* CONFIG_DEBUG_SET_MODULE_RONX */ | |
401 | ||
fbc907f0 | 402 | int sk_filter(struct sock *sk, struct sk_buff *skb); |
bd4cf0ed | 403 | |
7ae457c1 AS |
404 | void bpf_prog_select_runtime(struct bpf_prog *fp); |
405 | void bpf_prog_free(struct bpf_prog *fp); | |
bd4cf0ed | 406 | |
8fb575ca AS |
407 | int bpf_convert_filter(struct sock_filter *prog, int len, |
408 | struct bpf_insn *new_prog, int *new_len); | |
a3ea269b | 409 | |
60a3b225 DB |
410 | struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags); |
411 | struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, | |
412 | gfp_t gfp_extra_flags); | |
413 | void __bpf_prog_free(struct bpf_prog *fp); | |
414 | ||
415 | static inline void bpf_prog_unlock_free(struct bpf_prog *fp) | |
416 | { | |
417 | bpf_prog_unlock_ro(fp); | |
418 | __bpf_prog_free(fp); | |
419 | } | |
420 | ||
7ae457c1 AS |
421 | int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog); |
422 | void bpf_prog_destroy(struct bpf_prog *fp); | |
a3ea269b | 423 | |
fbc907f0 DB |
424 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk); |
425 | int sk_detach_filter(struct sock *sk); | |
a3ea269b | 426 | |
4df95ff4 | 427 | int bpf_check_classic(const struct sock_filter *filter, unsigned int flen); |
fbc907f0 DB |
428 | int sk_get_filter(struct sock *sk, struct sock_filter __user *filter, |
429 | unsigned int len); | |
fbc907f0 | 430 | |
278571ba | 431 | bool sk_filter_charge(struct sock *sk, struct sk_filter *fp); |
fbc907f0 | 432 | void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp); |
0a14842f | 433 | |
62258278 | 434 | u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
7ae457c1 | 435 | void bpf_int_jit_compile(struct bpf_prog *fp); |
62258278 | 436 | |
34805931 DB |
437 | #define BPF_ANC BIT(15) |
438 | ||
439 | static inline u16 bpf_anc_helper(const struct sock_filter *ftest) | |
440 | { | |
441 | BUG_ON(ftest->code & BPF_ANC); | |
442 | ||
443 | switch (ftest->code) { | |
444 | case BPF_LD | BPF_W | BPF_ABS: | |
445 | case BPF_LD | BPF_H | BPF_ABS: | |
446 | case BPF_LD | BPF_B | BPF_ABS: | |
447 | #define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \ | |
448 | return BPF_ANC | SKF_AD_##CODE | |
449 | switch (ftest->k) { | |
450 | BPF_ANCILLARY(PROTOCOL); | |
451 | BPF_ANCILLARY(PKTTYPE); | |
452 | BPF_ANCILLARY(IFINDEX); | |
453 | BPF_ANCILLARY(NLATTR); | |
454 | BPF_ANCILLARY(NLATTR_NEST); | |
455 | BPF_ANCILLARY(MARK); | |
456 | BPF_ANCILLARY(QUEUE); | |
457 | BPF_ANCILLARY(HATYPE); | |
458 | BPF_ANCILLARY(RXHASH); | |
459 | BPF_ANCILLARY(CPU); | |
460 | BPF_ANCILLARY(ALU_XOR_X); | |
461 | BPF_ANCILLARY(VLAN_TAG); | |
462 | BPF_ANCILLARY(VLAN_TAG_PRESENT); | |
463 | BPF_ANCILLARY(PAY_OFFSET); | |
464 | BPF_ANCILLARY(RANDOM); | |
465 | } | |
466 | /* Fallthrough. */ | |
467 | default: | |
468 | return ftest->code; | |
469 | } | |
470 | } | |
471 | ||
9f12fbe6 ZSL |
472 | void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, |
473 | int k, unsigned int size); | |
474 | ||
475 | static inline void *bpf_load_pointer(const struct sk_buff *skb, int k, | |
476 | unsigned int size, void *buffer) | |
477 | { | |
478 | if (k >= 0) | |
479 | return skb_header_pointer(skb, k, size, buffer); | |
480 | ||
481 | return bpf_internal_load_pointer_neg_helper(skb, k, size); | |
482 | } | |
483 | ||
0a14842f | 484 | #ifdef CONFIG_BPF_JIT |
20074f35 | 485 | #include <stdarg.h> |
a691ce7f CG |
486 | #include <linux/linkage.h> |
487 | #include <linux/printk.h> | |
488 | ||
7ae457c1 AS |
489 | void bpf_jit_compile(struct bpf_prog *fp); |
490 | void bpf_jit_free(struct bpf_prog *fp); | |
79617801 DB |
491 | |
492 | static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen, | |
493 | u32 pass, void *image) | |
494 | { | |
16495445 | 495 | pr_err("flen=%u proglen=%u pass=%u image=%pK\n", |
79617801 DB |
496 | flen, proglen, pass, image); |
497 | if (image) | |
16495445 | 498 | print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET, |
79617801 DB |
499 | 16, 1, image, proglen, false); |
500 | } | |
0a14842f | 501 | #else |
d45ed4a4 | 502 | #include <linux/slab.h> |
34805931 | 503 | |
7ae457c1 | 504 | static inline void bpf_jit_compile(struct bpf_prog *fp) |
0a14842f ED |
505 | { |
506 | } | |
34805931 | 507 | |
7ae457c1 | 508 | static inline void bpf_jit_free(struct bpf_prog *fp) |
0a14842f | 509 | { |
60a3b225 | 510 | bpf_prog_unlock_free(fp); |
0a14842f | 511 | } |
34805931 | 512 | #endif /* CONFIG_BPF_JIT */ |
0a14842f | 513 | |
ea02f941 MS |
514 | static inline int bpf_tell_extensions(void) |
515 | { | |
37692299 | 516 | return SKF_AD_MAX; |
ea02f941 MS |
517 | } |
518 | ||
1da177e4 | 519 | #endif /* __LINUX_FILTER_H__ */ |