]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - include/linux/filter.h
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 503
[thirdparty/kernel/stable.git] / include / linux / filter.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Linux Socket Filter Data Structures
4 */
5 #ifndef __LINUX_FILTER_H__
6 #define __LINUX_FILTER_H__
7
8 #include <stdarg.h>
9
10 #include <linux/atomic.h>
11 #include <linux/refcount.h>
12 #include <linux/compat.h>
13 #include <linux/skbuff.h>
14 #include <linux/linkage.h>
15 #include <linux/printk.h>
16 #include <linux/workqueue.h>
17 #include <linux/sched.h>
18 #include <linux/capability.h>
19 #include <linux/cryptohash.h>
20 #include <linux/set_memory.h>
21 #include <linux/kallsyms.h>
22 #include <linux/if_vlan.h>
23 #include <linux/vmalloc.h>
24
25 #include <net/sch_generic.h>
26
27 #include <uapi/linux/filter.h>
28 #include <uapi/linux/bpf.h>
29
30 struct sk_buff;
31 struct sock;
32 struct seccomp_data;
33 struct bpf_prog_aux;
34 struct xdp_rxq_info;
35 struct xdp_buff;
36 struct sock_reuseport;
37 struct ctl_table;
38 struct ctl_table_header;
39
40 /* ArgX, context and stack frame pointer register positions. Note,
41 * Arg1, Arg2, Arg3, etc are used as argument mappings of function
42 * calls in BPF_CALL instruction.
43 */
44 #define BPF_REG_ARG1 BPF_REG_1
45 #define BPF_REG_ARG2 BPF_REG_2
46 #define BPF_REG_ARG3 BPF_REG_3
47 #define BPF_REG_ARG4 BPF_REG_4
48 #define BPF_REG_ARG5 BPF_REG_5
49 #define BPF_REG_CTX BPF_REG_6
50 #define BPF_REG_FP BPF_REG_10
51
52 /* Additional register mappings for converted user programs. */
53 #define BPF_REG_A BPF_REG_0
54 #define BPF_REG_X BPF_REG_7
55 #define BPF_REG_TMP BPF_REG_2 /* scratch reg */
56 #define BPF_REG_D BPF_REG_8 /* data, callee-saved */
57 #define BPF_REG_H BPF_REG_9 /* hlen, callee-saved */
58
59 /* Kernel hidden auxiliary/helper register. */
60 #define BPF_REG_AX MAX_BPF_REG
61 #define MAX_BPF_EXT_REG (MAX_BPF_REG + 1)
62 #define MAX_BPF_JIT_REG MAX_BPF_EXT_REG
63
64 /* unused opcode to mark special call to bpf_tail_call() helper */
65 #define BPF_TAIL_CALL 0xf0
66
67 /* unused opcode to mark call to interpreter with arguments */
68 #define BPF_CALL_ARGS 0xe0
69
70 /* As per nm, we expose JITed images as text (code) section for
71 * kallsyms. That way, tools like perf can find it to match
72 * addresses.
73 */
74 #define BPF_SYM_ELF_TYPE 't'
75
76 /* BPF program can access up to 512 bytes of stack space. */
77 #define MAX_BPF_STACK 512
78
79 /* Helper macros for filter block array initializers. */
80
81 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
82
83 #define BPF_ALU64_REG(OP, DST, SRC) \
84 ((struct bpf_insn) { \
85 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
86 .dst_reg = DST, \
87 .src_reg = SRC, \
88 .off = 0, \
89 .imm = 0 })
90
91 #define BPF_ALU32_REG(OP, DST, SRC) \
92 ((struct bpf_insn) { \
93 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
94 .dst_reg = DST, \
95 .src_reg = SRC, \
96 .off = 0, \
97 .imm = 0 })
98
99 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
100
101 #define BPF_ALU64_IMM(OP, DST, IMM) \
102 ((struct bpf_insn) { \
103 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
104 .dst_reg = DST, \
105 .src_reg = 0, \
106 .off = 0, \
107 .imm = IMM })
108
109 #define BPF_ALU32_IMM(OP, DST, IMM) \
110 ((struct bpf_insn) { \
111 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
112 .dst_reg = DST, \
113 .src_reg = 0, \
114 .off = 0, \
115 .imm = IMM })
116
117 /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
118
119 #define BPF_ENDIAN(TYPE, DST, LEN) \
120 ((struct bpf_insn) { \
121 .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
122 .dst_reg = DST, \
123 .src_reg = 0, \
124 .off = 0, \
125 .imm = LEN })
126
127 /* Short form of mov, dst_reg = src_reg */
128
129 #define BPF_MOV64_REG(DST, SRC) \
130 ((struct bpf_insn) { \
131 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
132 .dst_reg = DST, \
133 .src_reg = SRC, \
134 .off = 0, \
135 .imm = 0 })
136
137 #define BPF_MOV32_REG(DST, SRC) \
138 ((struct bpf_insn) { \
139 .code = BPF_ALU | BPF_MOV | BPF_X, \
140 .dst_reg = DST, \
141 .src_reg = SRC, \
142 .off = 0, \
143 .imm = 0 })
144
145 /* Short form of mov, dst_reg = imm32 */
146
147 #define BPF_MOV64_IMM(DST, IMM) \
148 ((struct bpf_insn) { \
149 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
150 .dst_reg = DST, \
151 .src_reg = 0, \
152 .off = 0, \
153 .imm = IMM })
154
155 #define BPF_MOV32_IMM(DST, IMM) \
156 ((struct bpf_insn) { \
157 .code = BPF_ALU | BPF_MOV | BPF_K, \
158 .dst_reg = DST, \
159 .src_reg = 0, \
160 .off = 0, \
161 .imm = IMM })
162
163 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
164 #define BPF_LD_IMM64(DST, IMM) \
165 BPF_LD_IMM64_RAW(DST, 0, IMM)
166
167 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
168 ((struct bpf_insn) { \
169 .code = BPF_LD | BPF_DW | BPF_IMM, \
170 .dst_reg = DST, \
171 .src_reg = SRC, \
172 .off = 0, \
173 .imm = (__u32) (IMM) }), \
174 ((struct bpf_insn) { \
175 .code = 0, /* zero is reserved opcode */ \
176 .dst_reg = 0, \
177 .src_reg = 0, \
178 .off = 0, \
179 .imm = ((__u64) (IMM)) >> 32 })
180
181 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
182 #define BPF_LD_MAP_FD(DST, MAP_FD) \
183 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
184
185 /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
186
187 #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
188 ((struct bpf_insn) { \
189 .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
190 .dst_reg = DST, \
191 .src_reg = SRC, \
192 .off = 0, \
193 .imm = IMM })
194
195 #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
196 ((struct bpf_insn) { \
197 .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
198 .dst_reg = DST, \
199 .src_reg = SRC, \
200 .off = 0, \
201 .imm = IMM })
202
203 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
204
205 #define BPF_LD_ABS(SIZE, IMM) \
206 ((struct bpf_insn) { \
207 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
208 .dst_reg = 0, \
209 .src_reg = 0, \
210 .off = 0, \
211 .imm = IMM })
212
213 /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
214
215 #define BPF_LD_IND(SIZE, SRC, IMM) \
216 ((struct bpf_insn) { \
217 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
218 .dst_reg = 0, \
219 .src_reg = SRC, \
220 .off = 0, \
221 .imm = IMM })
222
223 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
224
225 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
226 ((struct bpf_insn) { \
227 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
228 .dst_reg = DST, \
229 .src_reg = SRC, \
230 .off = OFF, \
231 .imm = 0 })
232
233 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
234
235 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
236 ((struct bpf_insn) { \
237 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
238 .dst_reg = DST, \
239 .src_reg = SRC, \
240 .off = OFF, \
241 .imm = 0 })
242
243 /* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
244
245 #define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
246 ((struct bpf_insn) { \
247 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
248 .dst_reg = DST, \
249 .src_reg = SRC, \
250 .off = OFF, \
251 .imm = 0 })
252
253 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
254
255 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
256 ((struct bpf_insn) { \
257 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
258 .dst_reg = DST, \
259 .src_reg = 0, \
260 .off = OFF, \
261 .imm = IMM })
262
263 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
264
265 #define BPF_JMP_REG(OP, DST, SRC, OFF) \
266 ((struct bpf_insn) { \
267 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
268 .dst_reg = DST, \
269 .src_reg = SRC, \
270 .off = OFF, \
271 .imm = 0 })
272
273 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
274
275 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
276 ((struct bpf_insn) { \
277 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
278 .dst_reg = DST, \
279 .src_reg = 0, \
280 .off = OFF, \
281 .imm = IMM })
282
283 /* Like BPF_JMP_REG, but with 32-bit wide operands for comparison. */
284
285 #define BPF_JMP32_REG(OP, DST, SRC, OFF) \
286 ((struct bpf_insn) { \
287 .code = BPF_JMP32 | BPF_OP(OP) | BPF_X, \
288 .dst_reg = DST, \
289 .src_reg = SRC, \
290 .off = OFF, \
291 .imm = 0 })
292
293 /* Like BPF_JMP_IMM, but with 32-bit wide operands for comparison. */
294
295 #define BPF_JMP32_IMM(OP, DST, IMM, OFF) \
296 ((struct bpf_insn) { \
297 .code = BPF_JMP32 | BPF_OP(OP) | BPF_K, \
298 .dst_reg = DST, \
299 .src_reg = 0, \
300 .off = OFF, \
301 .imm = IMM })
302
303 /* Unconditional jumps, goto pc + off16 */
304
305 #define BPF_JMP_A(OFF) \
306 ((struct bpf_insn) { \
307 .code = BPF_JMP | BPF_JA, \
308 .dst_reg = 0, \
309 .src_reg = 0, \
310 .off = OFF, \
311 .imm = 0 })
312
313 /* Relative call */
314
315 #define BPF_CALL_REL(TGT) \
316 ((struct bpf_insn) { \
317 .code = BPF_JMP | BPF_CALL, \
318 .dst_reg = 0, \
319 .src_reg = BPF_PSEUDO_CALL, \
320 .off = 0, \
321 .imm = TGT })
322
323 /* Function call */
324
325 #define BPF_CAST_CALL(x) \
326 ((u64 (*)(u64, u64, u64, u64, u64))(x))
327
328 #define BPF_EMIT_CALL(FUNC) \
329 ((struct bpf_insn) { \
330 .code = BPF_JMP | BPF_CALL, \
331 .dst_reg = 0, \
332 .src_reg = 0, \
333 .off = 0, \
334 .imm = ((FUNC) - __bpf_call_base) })
335
336 /* Raw code statement block */
337
338 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
339 ((struct bpf_insn) { \
340 .code = CODE, \
341 .dst_reg = DST, \
342 .src_reg = SRC, \
343 .off = OFF, \
344 .imm = IMM })
345
346 /* Program exit */
347
348 #define BPF_EXIT_INSN() \
349 ((struct bpf_insn) { \
350 .code = BPF_JMP | BPF_EXIT, \
351 .dst_reg = 0, \
352 .src_reg = 0, \
353 .off = 0, \
354 .imm = 0 })
355
356 /* Internal classic blocks for direct assignment */
357
358 #define __BPF_STMT(CODE, K) \
359 ((struct sock_filter) BPF_STMT(CODE, K))
360
361 #define __BPF_JUMP(CODE, K, JT, JF) \
362 ((struct sock_filter) BPF_JUMP(CODE, K, JT, JF))
363
364 #define bytes_to_bpf_size(bytes) \
365 ({ \
366 int bpf_size = -EINVAL; \
367 \
368 if (bytes == sizeof(u8)) \
369 bpf_size = BPF_B; \
370 else if (bytes == sizeof(u16)) \
371 bpf_size = BPF_H; \
372 else if (bytes == sizeof(u32)) \
373 bpf_size = BPF_W; \
374 else if (bytes == sizeof(u64)) \
375 bpf_size = BPF_DW; \
376 \
377 bpf_size; \
378 })
379
380 #define bpf_size_to_bytes(bpf_size) \
381 ({ \
382 int bytes = -EINVAL; \
383 \
384 if (bpf_size == BPF_B) \
385 bytes = sizeof(u8); \
386 else if (bpf_size == BPF_H) \
387 bytes = sizeof(u16); \
388 else if (bpf_size == BPF_W) \
389 bytes = sizeof(u32); \
390 else if (bpf_size == BPF_DW) \
391 bytes = sizeof(u64); \
392 \
393 bytes; \
394 })
395
396 #define BPF_SIZEOF(type) \
397 ({ \
398 const int __size = bytes_to_bpf_size(sizeof(type)); \
399 BUILD_BUG_ON(__size < 0); \
400 __size; \
401 })
402
403 #define BPF_FIELD_SIZEOF(type, field) \
404 ({ \
405 const int __size = bytes_to_bpf_size(FIELD_SIZEOF(type, field)); \
406 BUILD_BUG_ON(__size < 0); \
407 __size; \
408 })
409
410 #define BPF_LDST_BYTES(insn) \
411 ({ \
412 const int __size = bpf_size_to_bytes(BPF_SIZE((insn)->code)); \
413 WARN_ON(__size < 0); \
414 __size; \
415 })
416
417 #define __BPF_MAP_0(m, v, ...) v
418 #define __BPF_MAP_1(m, v, t, a, ...) m(t, a)
419 #define __BPF_MAP_2(m, v, t, a, ...) m(t, a), __BPF_MAP_1(m, v, __VA_ARGS__)
420 #define __BPF_MAP_3(m, v, t, a, ...) m(t, a), __BPF_MAP_2(m, v, __VA_ARGS__)
421 #define __BPF_MAP_4(m, v, t, a, ...) m(t, a), __BPF_MAP_3(m, v, __VA_ARGS__)
422 #define __BPF_MAP_5(m, v, t, a, ...) m(t, a), __BPF_MAP_4(m, v, __VA_ARGS__)
423
424 #define __BPF_REG_0(...) __BPF_PAD(5)
425 #define __BPF_REG_1(...) __BPF_MAP(1, __VA_ARGS__), __BPF_PAD(4)
426 #define __BPF_REG_2(...) __BPF_MAP(2, __VA_ARGS__), __BPF_PAD(3)
427 #define __BPF_REG_3(...) __BPF_MAP(3, __VA_ARGS__), __BPF_PAD(2)
428 #define __BPF_REG_4(...) __BPF_MAP(4, __VA_ARGS__), __BPF_PAD(1)
429 #define __BPF_REG_5(...) __BPF_MAP(5, __VA_ARGS__)
430
431 #define __BPF_MAP(n, ...) __BPF_MAP_##n(__VA_ARGS__)
432 #define __BPF_REG(n, ...) __BPF_REG_##n(__VA_ARGS__)
433
434 #define __BPF_CAST(t, a) \
435 (__force t) \
436 (__force \
437 typeof(__builtin_choose_expr(sizeof(t) == sizeof(unsigned long), \
438 (unsigned long)0, (t)0))) a
439 #define __BPF_V void
440 #define __BPF_N
441
442 #define __BPF_DECL_ARGS(t, a) t a
443 #define __BPF_DECL_REGS(t, a) u64 a
444
445 #define __BPF_PAD(n) \
446 __BPF_MAP(n, __BPF_DECL_ARGS, __BPF_N, u64, __ur_1, u64, __ur_2, \
447 u64, __ur_3, u64, __ur_4, u64, __ur_5)
448
449 #define BPF_CALL_x(x, name, ...) \
450 static __always_inline \
451 u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \
452 u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)); \
453 u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)) \
454 { \
455 return ____##name(__BPF_MAP(x,__BPF_CAST,__BPF_N,__VA_ARGS__));\
456 } \
457 static __always_inline \
458 u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__))
459
460 #define BPF_CALL_0(name, ...) BPF_CALL_x(0, name, __VA_ARGS__)
461 #define BPF_CALL_1(name, ...) BPF_CALL_x(1, name, __VA_ARGS__)
462 #define BPF_CALL_2(name, ...) BPF_CALL_x(2, name, __VA_ARGS__)
463 #define BPF_CALL_3(name, ...) BPF_CALL_x(3, name, __VA_ARGS__)
464 #define BPF_CALL_4(name, ...) BPF_CALL_x(4, name, __VA_ARGS__)
465 #define BPF_CALL_5(name, ...) BPF_CALL_x(5, name, __VA_ARGS__)
466
467 #define bpf_ctx_range(TYPE, MEMBER) \
468 offsetof(TYPE, MEMBER) ... offsetofend(TYPE, MEMBER) - 1
469 #define bpf_ctx_range_till(TYPE, MEMBER1, MEMBER2) \
470 offsetof(TYPE, MEMBER1) ... offsetofend(TYPE, MEMBER2) - 1
471 #if BITS_PER_LONG == 64
472 # define bpf_ctx_range_ptr(TYPE, MEMBER) \
473 offsetof(TYPE, MEMBER) ... offsetofend(TYPE, MEMBER) - 1
474 #else
475 # define bpf_ctx_range_ptr(TYPE, MEMBER) \
476 offsetof(TYPE, MEMBER) ... offsetof(TYPE, MEMBER) + 8 - 1
477 #endif /* BITS_PER_LONG == 64 */
478
479 #define bpf_target_off(TYPE, MEMBER, SIZE, PTR_SIZE) \
480 ({ \
481 BUILD_BUG_ON(FIELD_SIZEOF(TYPE, MEMBER) != (SIZE)); \
482 *(PTR_SIZE) = (SIZE); \
483 offsetof(TYPE, MEMBER); \
484 })
485
486 #ifdef CONFIG_COMPAT
487 /* A struct sock_filter is architecture independent. */
488 struct compat_sock_fprog {
489 u16 len;
490 compat_uptr_t filter; /* struct sock_filter * */
491 };
492 #endif
493
494 struct sock_fprog_kern {
495 u16 len;
496 struct sock_filter *filter;
497 };
498
499 struct bpf_binary_header {
500 u32 pages;
501 /* Some arches need word alignment for their instructions */
502 u8 image[] __aligned(4);
503 };
504
505 struct bpf_prog {
506 u16 pages; /* Number of allocated pages */
507 u16 jited:1, /* Is our filter JIT'ed? */
508 jit_requested:1,/* archs need to JIT the prog */
509 gpl_compatible:1, /* Is filter GPL compatible? */
510 cb_access:1, /* Is control block accessed? */
511 dst_needed:1, /* Do we need dst entry? */
512 blinded:1, /* Was blinded */
513 is_func:1, /* program is a bpf function */
514 kprobe_override:1, /* Do we override a kprobe? */
515 has_callchain_buf:1; /* callchain buffer allocated? */
516 enum bpf_prog_type type; /* Type of BPF program */
517 enum bpf_attach_type expected_attach_type; /* For some prog types */
518 u32 len; /* Number of filter blocks */
519 u32 jited_len; /* Size of jited insns in bytes */
520 u8 tag[BPF_TAG_SIZE];
521 struct bpf_prog_aux *aux; /* Auxiliary fields */
522 struct sock_fprog_kern *orig_prog; /* Original BPF program */
523 unsigned int (*bpf_func)(const void *ctx,
524 const struct bpf_insn *insn);
525 /* Instructions for interpreter */
526 union {
527 struct sock_filter insns[0];
528 struct bpf_insn insnsi[0];
529 };
530 };
531
532 struct sk_filter {
533 refcount_t refcnt;
534 struct rcu_head rcu;
535 struct bpf_prog *prog;
536 };
537
538 DECLARE_STATIC_KEY_FALSE(bpf_stats_enabled_key);
539
540 #define BPF_PROG_RUN(prog, ctx) ({ \
541 u32 ret; \
542 cant_sleep(); \
543 if (static_branch_unlikely(&bpf_stats_enabled_key)) { \
544 struct bpf_prog_stats *stats; \
545 u64 start = sched_clock(); \
546 ret = (*(prog)->bpf_func)(ctx, (prog)->insnsi); \
547 stats = this_cpu_ptr(prog->aux->stats); \
548 u64_stats_update_begin(&stats->syncp); \
549 stats->cnt++; \
550 stats->nsecs += sched_clock() - start; \
551 u64_stats_update_end(&stats->syncp); \
552 } else { \
553 ret = (*(prog)->bpf_func)(ctx, (prog)->insnsi); \
554 } \
555 ret; })
556
557 #define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN
558
559 struct bpf_skb_data_end {
560 struct qdisc_skb_cb qdisc_cb;
561 void *data_meta;
562 void *data_end;
563 };
564
565 struct bpf_redirect_info {
566 u32 ifindex;
567 u32 flags;
568 struct bpf_map *map;
569 struct bpf_map *map_to_flush;
570 u32 kern_flags;
571 };
572
573 DECLARE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
574
575 /* flags for bpf_redirect_info kern_flags */
576 #define BPF_RI_F_RF_NO_DIRECT BIT(0) /* no napi_direct on return_frame */
577
578 /* Compute the linear packet data range [data, data_end) which
579 * will be accessed by various program types (cls_bpf, act_bpf,
580 * lwt, ...). Subsystems allowing direct data access must (!)
581 * ensure that cb[] area can be written to when BPF program is
582 * invoked (otherwise cb[] save/restore is necessary).
583 */
584 static inline void bpf_compute_data_pointers(struct sk_buff *skb)
585 {
586 struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
587
588 BUILD_BUG_ON(sizeof(*cb) > FIELD_SIZEOF(struct sk_buff, cb));
589 cb->data_meta = skb->data - skb_metadata_len(skb);
590 cb->data_end = skb->data + skb_headlen(skb);
591 }
592
593 /* Similar to bpf_compute_data_pointers(), except that save orginal
594 * data in cb->data and cb->meta_data for restore.
595 */
596 static inline void bpf_compute_and_save_data_end(
597 struct sk_buff *skb, void **saved_data_end)
598 {
599 struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
600
601 *saved_data_end = cb->data_end;
602 cb->data_end = skb->data + skb_headlen(skb);
603 }
604
605 /* Restore data saved by bpf_compute_data_pointers(). */
606 static inline void bpf_restore_data_end(
607 struct sk_buff *skb, void *saved_data_end)
608 {
609 struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
610
611 cb->data_end = saved_data_end;
612 }
613
614 static inline u8 *bpf_skb_cb(struct sk_buff *skb)
615 {
616 /* eBPF programs may read/write skb->cb[] area to transfer meta
617 * data between tail calls. Since this also needs to work with
618 * tc, that scratch memory is mapped to qdisc_skb_cb's data area.
619 *
620 * In some socket filter cases, the cb unfortunately needs to be
621 * saved/restored so that protocol specific skb->cb[] data won't
622 * be lost. In any case, due to unpriviledged eBPF programs
623 * attached to sockets, we need to clear the bpf_skb_cb() area
624 * to not leak previous contents to user space.
625 */
626 BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) != BPF_SKB_CB_LEN);
627 BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) !=
628 FIELD_SIZEOF(struct qdisc_skb_cb, data));
629
630 return qdisc_skb_cb(skb)->data;
631 }
632
633 static inline u32 __bpf_prog_run_save_cb(const struct bpf_prog *prog,
634 struct sk_buff *skb)
635 {
636 u8 *cb_data = bpf_skb_cb(skb);
637 u8 cb_saved[BPF_SKB_CB_LEN];
638 u32 res;
639
640 if (unlikely(prog->cb_access)) {
641 memcpy(cb_saved, cb_data, sizeof(cb_saved));
642 memset(cb_data, 0, sizeof(cb_saved));
643 }
644
645 res = BPF_PROG_RUN(prog, skb);
646
647 if (unlikely(prog->cb_access))
648 memcpy(cb_data, cb_saved, sizeof(cb_saved));
649
650 return res;
651 }
652
653 static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
654 struct sk_buff *skb)
655 {
656 u32 res;
657
658 preempt_disable();
659 res = __bpf_prog_run_save_cb(prog, skb);
660 preempt_enable();
661 return res;
662 }
663
664 static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog,
665 struct sk_buff *skb)
666 {
667 u8 *cb_data = bpf_skb_cb(skb);
668 u32 res;
669
670 if (unlikely(prog->cb_access))
671 memset(cb_data, 0, BPF_SKB_CB_LEN);
672
673 preempt_disable();
674 res = BPF_PROG_RUN(prog, skb);
675 preempt_enable();
676 return res;
677 }
678
679 static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,
680 struct xdp_buff *xdp)
681 {
682 /* Caller needs to hold rcu_read_lock() (!), otherwise program
683 * can be released while still running, or map elements could be
684 * freed early while still having concurrent users. XDP fastpath
685 * already takes rcu_read_lock() when fetching the program, so
686 * it's not necessary here anymore.
687 */
688 return BPF_PROG_RUN(prog, xdp);
689 }
690
691 static inline u32 bpf_prog_insn_size(const struct bpf_prog *prog)
692 {
693 return prog->len * sizeof(struct bpf_insn);
694 }
695
696 static inline u32 bpf_prog_tag_scratch_size(const struct bpf_prog *prog)
697 {
698 return round_up(bpf_prog_insn_size(prog) +
699 sizeof(__be64) + 1, SHA_MESSAGE_BYTES);
700 }
701
702 static inline unsigned int bpf_prog_size(unsigned int proglen)
703 {
704 return max(sizeof(struct bpf_prog),
705 offsetof(struct bpf_prog, insns[proglen]));
706 }
707
708 static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
709 {
710 /* When classic BPF programs have been loaded and the arch
711 * does not have a classic BPF JIT (anymore), they have been
712 * converted via bpf_migrate_filter() to eBPF and thus always
713 * have an unspec program type.
714 */
715 return prog->type == BPF_PROG_TYPE_UNSPEC;
716 }
717
718 static inline u32 bpf_ctx_off_adjust_machine(u32 size)
719 {
720 const u32 size_machine = sizeof(unsigned long);
721
722 if (size > size_machine && size % size_machine == 0)
723 size = size_machine;
724
725 return size;
726 }
727
728 static inline bool
729 bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
730 {
731 return size <= size_default && (size & (size - 1)) == 0;
732 }
733
734 #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
735
736 static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
737 {
738 set_vm_flush_reset_perms(fp);
739 set_memory_ro((unsigned long)fp, fp->pages);
740 }
741
742 static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
743 {
744 set_vm_flush_reset_perms(hdr);
745 set_memory_ro((unsigned long)hdr, hdr->pages);
746 set_memory_x((unsigned long)hdr, hdr->pages);
747 }
748
749 static inline struct bpf_binary_header *
750 bpf_jit_binary_hdr(const struct bpf_prog *fp)
751 {
752 unsigned long real_start = (unsigned long)fp->bpf_func;
753 unsigned long addr = real_start & PAGE_MASK;
754
755 return (void *)addr;
756 }
757
758 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
759 static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
760 {
761 return sk_filter_trim_cap(sk, skb, 1);
762 }
763
764 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err);
765 void bpf_prog_free(struct bpf_prog *fp);
766
767 bool bpf_opcode_in_insntable(u8 code);
768
769 void bpf_prog_free_linfo(struct bpf_prog *prog);
770 void bpf_prog_fill_jited_linfo(struct bpf_prog *prog,
771 const u32 *insn_to_jit_off);
772 int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog);
773 void bpf_prog_free_jited_linfo(struct bpf_prog *prog);
774 void bpf_prog_free_unused_jited_linfo(struct bpf_prog *prog);
775
776 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
777 struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags);
778 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
779 gfp_t gfp_extra_flags);
780 void __bpf_prog_free(struct bpf_prog *fp);
781
782 static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
783 {
784 __bpf_prog_free(fp);
785 }
786
787 typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
788 unsigned int flen);
789
790 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
791 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
792 bpf_aux_classic_check_t trans, bool save_orig);
793 void bpf_prog_destroy(struct bpf_prog *fp);
794
795 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
796 int sk_attach_bpf(u32 ufd, struct sock *sk);
797 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk);
798 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk);
799 void sk_reuseport_prog_free(struct bpf_prog *prog);
800 int sk_detach_filter(struct sock *sk);
801 int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
802 unsigned int len);
803
804 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
805 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
806
807 u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
808 #define __bpf_call_base_args \
809 ((u64 (*)(u64, u64, u64, u64, u64, const struct bpf_insn *)) \
810 __bpf_call_base)
811
812 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog);
813 void bpf_jit_compile(struct bpf_prog *prog);
814 bool bpf_helper_changes_pkt_data(void *func);
815
816 static inline bool bpf_dump_raw_ok(void)
817 {
818 /* Reconstruction of call-sites is dependent on kallsyms,
819 * thus make dump the same restriction.
820 */
821 return kallsyms_show_value() == 1;
822 }
823
824 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
825 const struct bpf_insn *patch, u32 len);
826 int bpf_remove_insns(struct bpf_prog *prog, u32 off, u32 cnt);
827
828 void bpf_clear_redirect_map(struct bpf_map *map);
829
830 static inline bool xdp_return_frame_no_direct(void)
831 {
832 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
833
834 return ri->kern_flags & BPF_RI_F_RF_NO_DIRECT;
835 }
836
837 static inline void xdp_set_return_frame_no_direct(void)
838 {
839 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
840
841 ri->kern_flags |= BPF_RI_F_RF_NO_DIRECT;
842 }
843
844 static inline void xdp_clear_return_frame_no_direct(void)
845 {
846 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
847
848 ri->kern_flags &= ~BPF_RI_F_RF_NO_DIRECT;
849 }
850
851 static inline int xdp_ok_fwd_dev(const struct net_device *fwd,
852 unsigned int pktlen)
853 {
854 unsigned int len;
855
856 if (unlikely(!(fwd->flags & IFF_UP)))
857 return -ENETDOWN;
858
859 len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
860 if (pktlen > len)
861 return -EMSGSIZE;
862
863 return 0;
864 }
865
866 /* The pair of xdp_do_redirect and xdp_do_flush_map MUST be called in the
867 * same cpu context. Further for best results no more than a single map
868 * for the do_redirect/do_flush pair should be used. This limitation is
869 * because we only track one map and force a flush when the map changes.
870 * This does not appear to be a real limitation for existing software.
871 */
872 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
873 struct xdp_buff *xdp, struct bpf_prog *prog);
874 int xdp_do_redirect(struct net_device *dev,
875 struct xdp_buff *xdp,
876 struct bpf_prog *prog);
877 void xdp_do_flush_map(void);
878
879 void bpf_warn_invalid_xdp_action(u32 act);
880
881 #ifdef CONFIG_INET
882 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
883 struct bpf_prog *prog, struct sk_buff *skb,
884 u32 hash);
885 #else
886 static inline struct sock *
887 bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
888 struct bpf_prog *prog, struct sk_buff *skb,
889 u32 hash)
890 {
891 return NULL;
892 }
893 #endif
894
895 #ifdef CONFIG_BPF_JIT
896 extern int bpf_jit_enable;
897 extern int bpf_jit_harden;
898 extern int bpf_jit_kallsyms;
899 extern long bpf_jit_limit;
900
901 typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
902
903 struct bpf_binary_header *
904 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
905 unsigned int alignment,
906 bpf_jit_fill_hole_t bpf_fill_ill_insns);
907 void bpf_jit_binary_free(struct bpf_binary_header *hdr);
908 u64 bpf_jit_alloc_exec_limit(void);
909 void *bpf_jit_alloc_exec(unsigned long size);
910 void bpf_jit_free_exec(void *addr);
911 void bpf_jit_free(struct bpf_prog *fp);
912
913 int bpf_jit_get_func_addr(const struct bpf_prog *prog,
914 const struct bpf_insn *insn, bool extra_pass,
915 u64 *func_addr, bool *func_addr_fixed);
916
917 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *fp);
918 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);
919
920 static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
921 u32 pass, void *image)
922 {
923 pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen,
924 proglen, pass, image, current->comm, task_pid_nr(current));
925
926 if (image)
927 print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
928 16, 1, image, proglen, false);
929 }
930
931 static inline bool bpf_jit_is_ebpf(void)
932 {
933 # ifdef CONFIG_HAVE_EBPF_JIT
934 return true;
935 # else
936 return false;
937 # endif
938 }
939
940 static inline bool ebpf_jit_enabled(void)
941 {
942 return bpf_jit_enable && bpf_jit_is_ebpf();
943 }
944
945 static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
946 {
947 return fp->jited && bpf_jit_is_ebpf();
948 }
949
950 static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
951 {
952 /* These are the prerequisites, should someone ever have the
953 * idea to call blinding outside of them, we make sure to
954 * bail out.
955 */
956 if (!bpf_jit_is_ebpf())
957 return false;
958 if (!prog->jit_requested)
959 return false;
960 if (!bpf_jit_harden)
961 return false;
962 if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
963 return false;
964
965 return true;
966 }
967
968 static inline bool bpf_jit_kallsyms_enabled(void)
969 {
970 /* There are a couple of corner cases where kallsyms should
971 * not be enabled f.e. on hardening.
972 */
973 if (bpf_jit_harden)
974 return false;
975 if (!bpf_jit_kallsyms)
976 return false;
977 if (bpf_jit_kallsyms == 1)
978 return true;
979
980 return false;
981 }
982
983 const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
984 unsigned long *off, char *sym);
985 bool is_bpf_text_address(unsigned long addr);
986 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
987 char *sym);
988
989 static inline const char *
990 bpf_address_lookup(unsigned long addr, unsigned long *size,
991 unsigned long *off, char **modname, char *sym)
992 {
993 const char *ret = __bpf_address_lookup(addr, size, off, sym);
994
995 if (ret && modname)
996 *modname = NULL;
997 return ret;
998 }
999
1000 void bpf_prog_kallsyms_add(struct bpf_prog *fp);
1001 void bpf_prog_kallsyms_del(struct bpf_prog *fp);
1002 void bpf_get_prog_name(const struct bpf_prog *prog, char *sym);
1003
1004 #else /* CONFIG_BPF_JIT */
1005
1006 static inline bool ebpf_jit_enabled(void)
1007 {
1008 return false;
1009 }
1010
1011 static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
1012 {
1013 return false;
1014 }
1015
1016 static inline void bpf_jit_free(struct bpf_prog *fp)
1017 {
1018 bpf_prog_unlock_free(fp);
1019 }
1020
1021 static inline bool bpf_jit_kallsyms_enabled(void)
1022 {
1023 return false;
1024 }
1025
1026 static inline const char *
1027 __bpf_address_lookup(unsigned long addr, unsigned long *size,
1028 unsigned long *off, char *sym)
1029 {
1030 return NULL;
1031 }
1032
1033 static inline bool is_bpf_text_address(unsigned long addr)
1034 {
1035 return false;
1036 }
1037
1038 static inline int bpf_get_kallsym(unsigned int symnum, unsigned long *value,
1039 char *type, char *sym)
1040 {
1041 return -ERANGE;
1042 }
1043
1044 static inline const char *
1045 bpf_address_lookup(unsigned long addr, unsigned long *size,
1046 unsigned long *off, char **modname, char *sym)
1047 {
1048 return NULL;
1049 }
1050
1051 static inline void bpf_prog_kallsyms_add(struct bpf_prog *fp)
1052 {
1053 }
1054
1055 static inline void bpf_prog_kallsyms_del(struct bpf_prog *fp)
1056 {
1057 }
1058
1059 static inline void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
1060 {
1061 sym[0] = '\0';
1062 }
1063
1064 #endif /* CONFIG_BPF_JIT */
1065
1066 void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp);
1067 void bpf_prog_kallsyms_del_all(struct bpf_prog *fp);
1068
1069 #define BPF_ANC BIT(15)
1070
1071 static inline bool bpf_needs_clear_a(const struct sock_filter *first)
1072 {
1073 switch (first->code) {
1074 case BPF_RET | BPF_K:
1075 case BPF_LD | BPF_W | BPF_LEN:
1076 return false;
1077
1078 case BPF_LD | BPF_W | BPF_ABS:
1079 case BPF_LD | BPF_H | BPF_ABS:
1080 case BPF_LD | BPF_B | BPF_ABS:
1081 if (first->k == SKF_AD_OFF + SKF_AD_ALU_XOR_X)
1082 return true;
1083 return false;
1084
1085 default:
1086 return true;
1087 }
1088 }
1089
1090 static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
1091 {
1092 BUG_ON(ftest->code & BPF_ANC);
1093
1094 switch (ftest->code) {
1095 case BPF_LD | BPF_W | BPF_ABS:
1096 case BPF_LD | BPF_H | BPF_ABS:
1097 case BPF_LD | BPF_B | BPF_ABS:
1098 #define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
1099 return BPF_ANC | SKF_AD_##CODE
1100 switch (ftest->k) {
1101 BPF_ANCILLARY(PROTOCOL);
1102 BPF_ANCILLARY(PKTTYPE);
1103 BPF_ANCILLARY(IFINDEX);
1104 BPF_ANCILLARY(NLATTR);
1105 BPF_ANCILLARY(NLATTR_NEST);
1106 BPF_ANCILLARY(MARK);
1107 BPF_ANCILLARY(QUEUE);
1108 BPF_ANCILLARY(HATYPE);
1109 BPF_ANCILLARY(RXHASH);
1110 BPF_ANCILLARY(CPU);
1111 BPF_ANCILLARY(ALU_XOR_X);
1112 BPF_ANCILLARY(VLAN_TAG);
1113 BPF_ANCILLARY(VLAN_TAG_PRESENT);
1114 BPF_ANCILLARY(PAY_OFFSET);
1115 BPF_ANCILLARY(RANDOM);
1116 BPF_ANCILLARY(VLAN_TPID);
1117 }
1118 /* Fallthrough. */
1119 default:
1120 return ftest->code;
1121 }
1122 }
1123
1124 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
1125 int k, unsigned int size);
1126
1127 static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
1128 unsigned int size, void *buffer)
1129 {
1130 if (k >= 0)
1131 return skb_header_pointer(skb, k, size, buffer);
1132
1133 return bpf_internal_load_pointer_neg_helper(skb, k, size);
1134 }
1135
1136 static inline int bpf_tell_extensions(void)
1137 {
1138 return SKF_AD_MAX;
1139 }
1140
1141 struct bpf_sock_addr_kern {
1142 struct sock *sk;
1143 struct sockaddr *uaddr;
1144 /* Temporary "register" to make indirect stores to nested structures
1145 * defined above. We need three registers to make such a store, but
1146 * only two (src and dst) are available at convert_ctx_access time
1147 */
1148 u64 tmp_reg;
1149 void *t_ctx; /* Attach type specific context. */
1150 };
1151
1152 struct bpf_sock_ops_kern {
1153 struct sock *sk;
1154 u32 op;
1155 union {
1156 u32 args[4];
1157 u32 reply;
1158 u32 replylong[4];
1159 };
1160 u32 is_fullsock;
1161 u64 temp; /* temp and everything after is not
1162 * initialized to 0 before calling
1163 * the BPF program. New fields that
1164 * should be initialized to 0 should
1165 * be inserted before temp.
1166 * temp is scratch storage used by
1167 * sock_ops_convert_ctx_access
1168 * as temporary storage of a register.
1169 */
1170 };
1171
1172 struct bpf_sysctl_kern {
1173 struct ctl_table_header *head;
1174 struct ctl_table *table;
1175 void *cur_val;
1176 size_t cur_len;
1177 void *new_val;
1178 size_t new_len;
1179 int new_updated;
1180 int write;
1181 loff_t *ppos;
1182 /* Temporary "register" for indirect stores to ppos. */
1183 u64 tmp_reg;
1184 };
1185
1186 #endif /* __LINUX_FILTER_H__ */