]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - include/linux/filter.h
bpf: Add BPF_MAP_GET_FD_BY_ID
[mirror_ubuntu-eoan-kernel.git] / include / linux / filter.h
CommitLineData
1da177e4
LT
1/*
2 * Linux Socket Filter Data Structures
3 */
1da177e4
LT
4#ifndef __LINUX_FILTER_H__
5#define __LINUX_FILTER_H__
6
b954d834
DB
7#include <stdarg.h>
8
60063497 9#include <linux/atomic.h>
4c355cdf 10#include <linux/refcount.h>
0c5fe1b4 11#include <linux/compat.h>
9f12fbe6 12#include <linux/skbuff.h>
b954d834
DB
13#include <linux/linkage.h>
14#include <linux/printk.h>
d45ed4a4 15#include <linux/workqueue.h>
b13138ef 16#include <linux/sched.h>
4f3446bb 17#include <linux/capability.h>
7bd509e3 18#include <linux/cryptohash.h>
4f3446bb 19
ff936a04 20#include <net/sch_generic.h>
b954d834 21
2d0bde57
LA
22#ifdef CONFIG_ARCH_HAS_SET_MEMORY
23#include <asm/set_memory.h>
24#endif
b954d834
DB
25
26#include <uapi/linux/filter.h>
daedfb22 27#include <uapi/linux/bpf.h>
60a3b225
DB
28
29struct sk_buff;
30struct sock;
31struct seccomp_data;
09756af4 32struct bpf_prog_aux;
792d4b5c 33
30743837
DB
34/* ArgX, context and stack frame pointer register positions. Note,
35 * Arg1, Arg2, Arg3, etc are used as argument mappings of function
36 * calls in BPF_CALL instruction.
37 */
38#define BPF_REG_ARG1 BPF_REG_1
39#define BPF_REG_ARG2 BPF_REG_2
40#define BPF_REG_ARG3 BPF_REG_3
41#define BPF_REG_ARG4 BPF_REG_4
42#define BPF_REG_ARG5 BPF_REG_5
43#define BPF_REG_CTX BPF_REG_6
44#define BPF_REG_FP BPF_REG_10
45
46/* Additional register mappings for converted user programs. */
47#define BPF_REG_A BPF_REG_0
48#define BPF_REG_X BPF_REG_7
49#define BPF_REG_TMP BPF_REG_8
bd4cf0ed 50
4f3446bb
DB
51/* Kernel hidden auxiliary/helper register for hardening step.
52 * Only used by eBPF JITs. It's nothing more than a temporary
53 * register that JITs use internally, only that here it's part
54 * of eBPF instructions that have been rewritten for blinding
55 * constants. See JIT pre-step in bpf_jit_blind_constants().
56 */
57#define BPF_REG_AX MAX_BPF_REG
58#define MAX_BPF_JIT_REG (MAX_BPF_REG + 1)
59
71189fa9
AS
60/* unused opcode to mark special call to bpf_tail_call() helper */
61#define BPF_TAIL_CALL 0xf0
62
74451e66
DB
63/* As per nm, we expose JITed images as text (code) section for
64 * kallsyms. That way, tools like perf can find it to match
65 * addresses.
66 */
67#define BPF_SYM_ELF_TYPE 't'
68
bd4cf0ed
AS
69/* BPF program can access up to 512 bytes of stack space. */
70#define MAX_BPF_STACK 512
71
f1f7714e
DB
72#define BPF_TAG_SIZE 8
73
f8f6d679
DB
74/* Helper macros for filter block array initializers. */
75
e430f34e 76/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
f8f6d679 77
e430f34e 78#define BPF_ALU64_REG(OP, DST, SRC) \
2695fb55 79 ((struct bpf_insn) { \
f8f6d679 80 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
e430f34e
AS
81 .dst_reg = DST, \
82 .src_reg = SRC, \
f8f6d679
DB
83 .off = 0, \
84 .imm = 0 })
85
e430f34e 86#define BPF_ALU32_REG(OP, DST, SRC) \
2695fb55 87 ((struct bpf_insn) { \
f8f6d679 88 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
e430f34e
AS
89 .dst_reg = DST, \
90 .src_reg = SRC, \
f8f6d679
DB
91 .off = 0, \
92 .imm = 0 })
93
e430f34e 94/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
f8f6d679 95
e430f34e 96#define BPF_ALU64_IMM(OP, DST, IMM) \
2695fb55 97 ((struct bpf_insn) { \
f8f6d679 98 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
e430f34e
AS
99 .dst_reg = DST, \
100 .src_reg = 0, \
f8f6d679
DB
101 .off = 0, \
102 .imm = IMM })
103
e430f34e 104#define BPF_ALU32_IMM(OP, DST, IMM) \
2695fb55 105 ((struct bpf_insn) { \
f8f6d679 106 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
e430f34e
AS
107 .dst_reg = DST, \
108 .src_reg = 0, \
f8f6d679
DB
109 .off = 0, \
110 .imm = IMM })
111
112/* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
113
e430f34e 114#define BPF_ENDIAN(TYPE, DST, LEN) \
2695fb55 115 ((struct bpf_insn) { \
f8f6d679 116 .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
e430f34e
AS
117 .dst_reg = DST, \
118 .src_reg = 0, \
f8f6d679
DB
119 .off = 0, \
120 .imm = LEN })
121
e430f34e 122/* Short form of mov, dst_reg = src_reg */
f8f6d679 123
e430f34e 124#define BPF_MOV64_REG(DST, SRC) \
2695fb55 125 ((struct bpf_insn) { \
f8f6d679 126 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
e430f34e
AS
127 .dst_reg = DST, \
128 .src_reg = SRC, \
f8f6d679
DB
129 .off = 0, \
130 .imm = 0 })
131
e430f34e 132#define BPF_MOV32_REG(DST, SRC) \
2695fb55 133 ((struct bpf_insn) { \
f8f6d679 134 .code = BPF_ALU | BPF_MOV | BPF_X, \
e430f34e
AS
135 .dst_reg = DST, \
136 .src_reg = SRC, \
f8f6d679
DB
137 .off = 0, \
138 .imm = 0 })
139
e430f34e 140/* Short form of mov, dst_reg = imm32 */
f8f6d679 141
e430f34e 142#define BPF_MOV64_IMM(DST, IMM) \
2695fb55 143 ((struct bpf_insn) { \
f8f6d679 144 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
e430f34e
AS
145 .dst_reg = DST, \
146 .src_reg = 0, \
f8f6d679
DB
147 .off = 0, \
148 .imm = IMM })
149
e430f34e 150#define BPF_MOV32_IMM(DST, IMM) \
2695fb55 151 ((struct bpf_insn) { \
f8f6d679 152 .code = BPF_ALU | BPF_MOV | BPF_K, \
e430f34e
AS
153 .dst_reg = DST, \
154 .src_reg = 0, \
f8f6d679
DB
155 .off = 0, \
156 .imm = IMM })
157
02ab695b
AS
158/* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
159#define BPF_LD_IMM64(DST, IMM) \
160 BPF_LD_IMM64_RAW(DST, 0, IMM)
161
162#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
163 ((struct bpf_insn) { \
164 .code = BPF_LD | BPF_DW | BPF_IMM, \
165 .dst_reg = DST, \
166 .src_reg = SRC, \
167 .off = 0, \
168 .imm = (__u32) (IMM) }), \
169 ((struct bpf_insn) { \
170 .code = 0, /* zero is reserved opcode */ \
171 .dst_reg = 0, \
172 .src_reg = 0, \
173 .off = 0, \
174 .imm = ((__u64) (IMM)) >> 32 })
175
0246e64d
AS
176/* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
177#define BPF_LD_MAP_FD(DST, MAP_FD) \
178 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
179
e430f34e 180/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
f8f6d679 181
e430f34e 182#define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
2695fb55 183 ((struct bpf_insn) { \
f8f6d679 184 .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
e430f34e
AS
185 .dst_reg = DST, \
186 .src_reg = SRC, \
f8f6d679
DB
187 .off = 0, \
188 .imm = IMM })
189
e430f34e 190#define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
2695fb55 191 ((struct bpf_insn) { \
f8f6d679 192 .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
e430f34e
AS
193 .dst_reg = DST, \
194 .src_reg = SRC, \
f8f6d679
DB
195 .off = 0, \
196 .imm = IMM })
197
e430f34e 198/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
f8f6d679 199
e430f34e 200#define BPF_LD_ABS(SIZE, IMM) \
2695fb55 201 ((struct bpf_insn) { \
f8f6d679 202 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
e430f34e
AS
203 .dst_reg = 0, \
204 .src_reg = 0, \
f8f6d679 205 .off = 0, \
e430f34e 206 .imm = IMM })
f8f6d679 207
e430f34e 208/* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
f8f6d679 209
e430f34e 210#define BPF_LD_IND(SIZE, SRC, IMM) \
2695fb55 211 ((struct bpf_insn) { \
f8f6d679 212 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
e430f34e
AS
213 .dst_reg = 0, \
214 .src_reg = SRC, \
f8f6d679 215 .off = 0, \
e430f34e 216 .imm = IMM })
f8f6d679 217
e430f34e 218/* Memory load, dst_reg = *(uint *) (src_reg + off16) */
f8f6d679 219
e430f34e 220#define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
2695fb55 221 ((struct bpf_insn) { \
f8f6d679 222 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
e430f34e
AS
223 .dst_reg = DST, \
224 .src_reg = SRC, \
f8f6d679
DB
225 .off = OFF, \
226 .imm = 0 })
227
e430f34e
AS
228/* Memory store, *(uint *) (dst_reg + off16) = src_reg */
229
230#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
2695fb55 231 ((struct bpf_insn) { \
f8f6d679 232 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
e430f34e
AS
233 .dst_reg = DST, \
234 .src_reg = SRC, \
f8f6d679
DB
235 .off = OFF, \
236 .imm = 0 })
237
cffc642d
MH
238/* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
239
240#define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
241 ((struct bpf_insn) { \
242 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
243 .dst_reg = DST, \
244 .src_reg = SRC, \
245 .off = OFF, \
246 .imm = 0 })
247
e430f34e
AS
248/* Memory store, *(uint *) (dst_reg + off16) = imm32 */
249
250#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
2695fb55 251 ((struct bpf_insn) { \
e430f34e
AS
252 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
253 .dst_reg = DST, \
254 .src_reg = 0, \
255 .off = OFF, \
256 .imm = IMM })
257
258/* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
f8f6d679 259
e430f34e 260#define BPF_JMP_REG(OP, DST, SRC, OFF) \
2695fb55 261 ((struct bpf_insn) { \
f8f6d679 262 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
e430f34e
AS
263 .dst_reg = DST, \
264 .src_reg = SRC, \
f8f6d679
DB
265 .off = OFF, \
266 .imm = 0 })
267
e430f34e 268/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
f8f6d679 269
e430f34e 270#define BPF_JMP_IMM(OP, DST, IMM, OFF) \
2695fb55 271 ((struct bpf_insn) { \
f8f6d679 272 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
e430f34e
AS
273 .dst_reg = DST, \
274 .src_reg = 0, \
f8f6d679
DB
275 .off = OFF, \
276 .imm = IMM })
277
614d0d77
DB
278/* Unconditional jumps, goto pc + off16 */
279
280#define BPF_JMP_A(OFF) \
281 ((struct bpf_insn) { \
282 .code = BPF_JMP | BPF_JA, \
283 .dst_reg = 0, \
284 .src_reg = 0, \
285 .off = OFF, \
286 .imm = 0 })
287
f8f6d679
DB
288/* Function call */
289
290#define BPF_EMIT_CALL(FUNC) \
2695fb55 291 ((struct bpf_insn) { \
f8f6d679 292 .code = BPF_JMP | BPF_CALL, \
e430f34e
AS
293 .dst_reg = 0, \
294 .src_reg = 0, \
f8f6d679
DB
295 .off = 0, \
296 .imm = ((FUNC) - __bpf_call_base) })
297
298/* Raw code statement block */
299
e430f34e 300#define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
2695fb55 301 ((struct bpf_insn) { \
f8f6d679 302 .code = CODE, \
e430f34e
AS
303 .dst_reg = DST, \
304 .src_reg = SRC, \
f8f6d679
DB
305 .off = OFF, \
306 .imm = IMM })
307
308/* Program exit */
309
310#define BPF_EXIT_INSN() \
2695fb55 311 ((struct bpf_insn) { \
f8f6d679 312 .code = BPF_JMP | BPF_EXIT, \
e430f34e
AS
313 .dst_reg = 0, \
314 .src_reg = 0, \
f8f6d679
DB
315 .off = 0, \
316 .imm = 0 })
317
a4afd37b
DB
318/* Internal classic blocks for direct assignment */
319
320#define __BPF_STMT(CODE, K) \
321 ((struct sock_filter) BPF_STMT(CODE, K))
322
323#define __BPF_JUMP(CODE, K, JT, JF) \
324 ((struct sock_filter) BPF_JUMP(CODE, K, JT, JF))
325
f8f6d679
DB
326#define bytes_to_bpf_size(bytes) \
327({ \
328 int bpf_size = -EINVAL; \
329 \
330 if (bytes == sizeof(u8)) \
331 bpf_size = BPF_B; \
332 else if (bytes == sizeof(u16)) \
333 bpf_size = BPF_H; \
334 else if (bytes == sizeof(u32)) \
335 bpf_size = BPF_W; \
336 else if (bytes == sizeof(u64)) \
337 bpf_size = BPF_DW; \
338 \
339 bpf_size; \
340})
9739eef1 341
f035a515
DB
342#define BPF_SIZEOF(type) \
343 ({ \
344 const int __size = bytes_to_bpf_size(sizeof(type)); \
345 BUILD_BUG_ON(__size < 0); \
346 __size; \
347 })
348
349#define BPF_FIELD_SIZEOF(type, field) \
350 ({ \
351 const int __size = bytes_to_bpf_size(FIELD_SIZEOF(type, field)); \
352 BUILD_BUG_ON(__size < 0); \
353 __size; \
354 })
355
f3694e00
DB
356#define __BPF_MAP_0(m, v, ...) v
357#define __BPF_MAP_1(m, v, t, a, ...) m(t, a)
358#define __BPF_MAP_2(m, v, t, a, ...) m(t, a), __BPF_MAP_1(m, v, __VA_ARGS__)
359#define __BPF_MAP_3(m, v, t, a, ...) m(t, a), __BPF_MAP_2(m, v, __VA_ARGS__)
360#define __BPF_MAP_4(m, v, t, a, ...) m(t, a), __BPF_MAP_3(m, v, __VA_ARGS__)
361#define __BPF_MAP_5(m, v, t, a, ...) m(t, a), __BPF_MAP_4(m, v, __VA_ARGS__)
362
363#define __BPF_REG_0(...) __BPF_PAD(5)
364#define __BPF_REG_1(...) __BPF_MAP(1, __VA_ARGS__), __BPF_PAD(4)
365#define __BPF_REG_2(...) __BPF_MAP(2, __VA_ARGS__), __BPF_PAD(3)
366#define __BPF_REG_3(...) __BPF_MAP(3, __VA_ARGS__), __BPF_PAD(2)
367#define __BPF_REG_4(...) __BPF_MAP(4, __VA_ARGS__), __BPF_PAD(1)
368#define __BPF_REG_5(...) __BPF_MAP(5, __VA_ARGS__)
369
370#define __BPF_MAP(n, ...) __BPF_MAP_##n(__VA_ARGS__)
371#define __BPF_REG(n, ...) __BPF_REG_##n(__VA_ARGS__)
372
373#define __BPF_CAST(t, a) \
374 (__force t) \
375 (__force \
376 typeof(__builtin_choose_expr(sizeof(t) == sizeof(unsigned long), \
377 (unsigned long)0, (t)0))) a
378#define __BPF_V void
379#define __BPF_N
380
381#define __BPF_DECL_ARGS(t, a) t a
382#define __BPF_DECL_REGS(t, a) u64 a
383
384#define __BPF_PAD(n) \
385 __BPF_MAP(n, __BPF_DECL_ARGS, __BPF_N, u64, __ur_1, u64, __ur_2, \
386 u64, __ur_3, u64, __ur_4, u64, __ur_5)
387
388#define BPF_CALL_x(x, name, ...) \
389 static __always_inline \
390 u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \
391 u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)); \
392 u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)) \
393 { \
394 return ____##name(__BPF_MAP(x,__BPF_CAST,__BPF_N,__VA_ARGS__));\
395 } \
396 static __always_inline \
397 u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__))
398
399#define BPF_CALL_0(name, ...) BPF_CALL_x(0, name, __VA_ARGS__)
400#define BPF_CALL_1(name, ...) BPF_CALL_x(1, name, __VA_ARGS__)
401#define BPF_CALL_2(name, ...) BPF_CALL_x(2, name, __VA_ARGS__)
402#define BPF_CALL_3(name, ...) BPF_CALL_x(3, name, __VA_ARGS__)
403#define BPF_CALL_4(name, ...) BPF_CALL_x(4, name, __VA_ARGS__)
404#define BPF_CALL_5(name, ...) BPF_CALL_x(5, name, __VA_ARGS__)
405
bd4cf0ed
AS
406#ifdef CONFIG_COMPAT
407/* A struct sock_filter is architecture independent. */
0c5fe1b4
WD
408struct compat_sock_fprog {
409 u16 len;
bd4cf0ed 410 compat_uptr_t filter; /* struct sock_filter * */
0c5fe1b4
WD
411};
412#endif
413
a3ea269b
DB
414struct sock_fprog_kern {
415 u16 len;
416 struct sock_filter *filter;
417};
418
738cbe72
DB
419struct bpf_binary_header {
420 unsigned int pages;
421 u8 image[];
422};
423
7ae457c1 424struct bpf_prog {
286aad3c 425 u16 pages; /* Number of allocated pages */
a91263d5
DB
426 kmemcheck_bitfield_begin(meta);
427 u16 jited:1, /* Is our filter JIT'ed? */
65869a47 428 locked:1, /* Program image locked? */
c46646d0 429 gpl_compatible:1, /* Is filter GPL compatible? */
ff936a04 430 cb_access:1, /* Is control block accessed? */
e390b55d 431 dst_needed:1; /* Do we need dst entry? */
a91263d5 432 kmemcheck_bitfield_end(meta);
24701ece 433 enum bpf_prog_type type; /* Type of BPF program */
7bd509e3 434 u32 len; /* Number of filter blocks */
f1f7714e 435 u8 tag[BPF_TAG_SIZE];
09756af4 436 struct bpf_prog_aux *aux; /* Auxiliary fields */
24701ece 437 struct sock_fprog_kern *orig_prog; /* Original BPF program */
88575199
DB
438 unsigned int (*bpf_func)(const void *ctx,
439 const struct bpf_insn *insn);
60a3b225 440 /* Instructions for interpreter */
d45ed4a4 441 union {
bd4cf0ed 442 struct sock_filter insns[0];
2695fb55 443 struct bpf_insn insnsi[0];
d45ed4a4 444 };
b715631f
SH
445};
446
7ae457c1 447struct sk_filter {
4c355cdf 448 refcount_t refcnt;
7ae457c1
AS
449 struct rcu_head rcu;
450 struct bpf_prog *prog;
451};
452
453#define BPF_PROG_RUN(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
454
01dd194c
DB
455#define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN
456
db58ba45
AS
457struct bpf_skb_data_end {
458 struct qdisc_skb_cb qdisc_cb;
459 void *data_end;
460};
461
6a773a15
BB
462struct xdp_buff {
463 void *data;
464 void *data_end;
17bedab2 465 void *data_hard_start;
6a773a15
BB
466};
467
db58ba45 468/* compute the linear packet data range [data, data_end) which
3a0af8fd 469 * will be accessed by cls_bpf, act_bpf and lwt programs
db58ba45
AS
470 */
471static inline void bpf_compute_data_end(struct sk_buff *skb)
472{
473 struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
474
475 BUILD_BUG_ON(sizeof(*cb) > FIELD_SIZEOF(struct sk_buff, cb));
476 cb->data_end = skb->data + skb_headlen(skb);
477}
478
01dd194c
DB
479static inline u8 *bpf_skb_cb(struct sk_buff *skb)
480{
481 /* eBPF programs may read/write skb->cb[] area to transfer meta
482 * data between tail calls. Since this also needs to work with
483 * tc, that scratch memory is mapped to qdisc_skb_cb's data area.
484 *
485 * In some socket filter cases, the cb unfortunately needs to be
486 * saved/restored so that protocol specific skb->cb[] data won't
487 * be lost. In any case, due to unpriviledged eBPF programs
488 * attached to sockets, we need to clear the bpf_skb_cb() area
489 * to not leak previous contents to user space.
490 */
491 BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) != BPF_SKB_CB_LEN);
492 BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) !=
493 FIELD_SIZEOF(struct qdisc_skb_cb, data));
494
495 return qdisc_skb_cb(skb)->data;
496}
497
ff936a04
AS
498static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
499 struct sk_buff *skb)
500{
01dd194c
DB
501 u8 *cb_data = bpf_skb_cb(skb);
502 u8 cb_saved[BPF_SKB_CB_LEN];
ff936a04
AS
503 u32 res;
504
ff936a04 505 if (unlikely(prog->cb_access)) {
01dd194c
DB
506 memcpy(cb_saved, cb_data, sizeof(cb_saved));
507 memset(cb_data, 0, sizeof(cb_saved));
ff936a04
AS
508 }
509
510 res = BPF_PROG_RUN(prog, skb);
511
512 if (unlikely(prog->cb_access))
01dd194c 513 memcpy(cb_data, cb_saved, sizeof(cb_saved));
ff936a04
AS
514
515 return res;
516}
517
518static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog,
519 struct sk_buff *skb)
520{
01dd194c 521 u8 *cb_data = bpf_skb_cb(skb);
ff936a04
AS
522
523 if (unlikely(prog->cb_access))
01dd194c
DB
524 memset(cb_data, 0, BPF_SKB_CB_LEN);
525
ff936a04
AS
526 return BPF_PROG_RUN(prog, skb);
527}
528
366cbf2f
DB
529static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,
530 struct xdp_buff *xdp)
6a773a15 531{
366cbf2f
DB
532 /* Caller needs to hold rcu_read_lock() (!), otherwise program
533 * can be released while still running, or map elements could be
534 * freed early while still having concurrent users. XDP fastpath
535 * already takes rcu_read_lock() when fetching the program, so
536 * it's not necessary here anymore.
537 */
538 return BPF_PROG_RUN(prog, xdp);
6a773a15
BB
539}
540
aafe6ae9
DB
541static inline u32 bpf_prog_insn_size(const struct bpf_prog *prog)
542{
543 return prog->len * sizeof(struct bpf_insn);
544}
545
f1f7714e 546static inline u32 bpf_prog_tag_scratch_size(const struct bpf_prog *prog)
aafe6ae9
DB
547{
548 return round_up(bpf_prog_insn_size(prog) +
549 sizeof(__be64) + 1, SHA_MESSAGE_BYTES);
550}
551
7ae457c1 552static inline unsigned int bpf_prog_size(unsigned int proglen)
b715631f 553{
7ae457c1
AS
554 return max(sizeof(struct bpf_prog),
555 offsetof(struct bpf_prog, insns[proglen]));
b715631f
SH
556}
557
7b36f929
DB
558static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
559{
560 /* When classic BPF programs have been loaded and the arch
561 * does not have a classic BPF JIT (anymore), they have been
562 * converted via bpf_migrate_filter() to eBPF and thus always
563 * have an unspec program type.
564 */
565 return prog->type == BPF_PROG_TYPE_UNSPEC;
566}
567
009937e7 568#define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
a3ea269b 569
9d876e79 570#ifdef CONFIG_ARCH_HAS_SET_MEMORY
60a3b225
DB
571static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
572{
65869a47
DB
573 fp->locked = 1;
574 WARN_ON_ONCE(set_memory_ro((unsigned long)fp, fp->pages));
60a3b225
DB
575}
576
577static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
578{
65869a47
DB
579 if (fp->locked) {
580 WARN_ON_ONCE(set_memory_rw((unsigned long)fp, fp->pages));
581 /* In case set_memory_rw() fails, we want to be the first
582 * to crash here instead of some random place later on.
583 */
584 fp->locked = 0;
585 }
60a3b225 586}
74451e66 587
9d876e79
DB
588static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
589{
65869a47 590 WARN_ON_ONCE(set_memory_ro((unsigned long)hdr, hdr->pages));
9d876e79
DB
591}
592
74451e66
DB
593static inline void bpf_jit_binary_unlock_ro(struct bpf_binary_header *hdr)
594{
65869a47 595 WARN_ON_ONCE(set_memory_rw((unsigned long)hdr, hdr->pages));
74451e66 596}
60a3b225
DB
597#else
598static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
599{
600}
601
602static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
603{
604}
74451e66 605
9d876e79
DB
606static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
607{
608}
609
74451e66
DB
610static inline void bpf_jit_binary_unlock_ro(struct bpf_binary_header *hdr)
611{
612}
9d876e79 613#endif /* CONFIG_ARCH_HAS_SET_MEMORY */
60a3b225 614
74451e66
DB
615static inline struct bpf_binary_header *
616bpf_jit_binary_hdr(const struct bpf_prog *fp)
617{
618 unsigned long real_start = (unsigned long)fp->bpf_func;
619 unsigned long addr = real_start & PAGE_MASK;
620
621 return (void *)addr;
622}
623
f4979fce
WB
624int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
625static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
626{
627 return sk_filter_trim_cap(sk, skb, 1);
628}
bd4cf0ed 629
d1c55ab5 630struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err);
7ae457c1 631void bpf_prog_free(struct bpf_prog *fp);
bd4cf0ed 632
60a3b225
DB
633struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
634struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
635 gfp_t gfp_extra_flags);
636void __bpf_prog_free(struct bpf_prog *fp);
637
638static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
639{
640 bpf_prog_unlock_ro(fp);
641 __bpf_prog_free(fp);
642}
643
ac67eb2c
DB
644typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
645 unsigned int flen);
646
7ae457c1 647int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
ac67eb2c 648int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
bab18991 649 bpf_aux_classic_check_t trans, bool save_orig);
7ae457c1 650void bpf_prog_destroy(struct bpf_prog *fp);
a3ea269b 651
fbc907f0 652int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
89aa0758 653int sk_attach_bpf(u32 ufd, struct sock *sk);
538950a1
CG
654int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk);
655int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk);
fbc907f0 656int sk_detach_filter(struct sock *sk);
fbc907f0
DB
657int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
658 unsigned int len);
fbc907f0 659
278571ba 660bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
fbc907f0 661void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
0a14842f 662
62258278 663u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
d1c55ab5
DB
664
665struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog);
9383191d 666void bpf_jit_compile(struct bpf_prog *prog);
17bedab2 667bool bpf_helper_changes_pkt_data(void *func);
62258278 668
c237ee5e
DB
669struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
670 const struct bpf_insn *patch, u32 len);
6a773a15 671void bpf_warn_invalid_xdp_action(u32 act);
c237ee5e 672
b954d834 673#ifdef CONFIG_BPF_JIT
c94987e4 674extern int bpf_jit_enable;
4f3446bb 675extern int bpf_jit_harden;
74451e66 676extern int bpf_jit_kallsyms;
c94987e4 677
b954d834
DB
678typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
679
680struct bpf_binary_header *
681bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
682 unsigned int alignment,
683 bpf_jit_fill_hole_t bpf_fill_ill_insns);
684void bpf_jit_binary_free(struct bpf_binary_header *hdr);
685
b954d834
DB
686void bpf_jit_free(struct bpf_prog *fp);
687
4f3446bb
DB
688struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *fp);
689void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);
690
b954d834
DB
691static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
692 u32 pass, void *image)
693{
b13138ef
DB
694 pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen,
695 proglen, pass, image, current->comm, task_pid_nr(current));
696
b954d834
DB
697 if (image)
698 print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
699 16, 1, image, proglen, false);
700}
4f3446bb
DB
701
702static inline bool bpf_jit_is_ebpf(void)
703{
704# ifdef CONFIG_HAVE_EBPF_JIT
705 return true;
706# else
707 return false;
708# endif
709}
710
81ed18ab
AS
711static inline bool ebpf_jit_enabled(void)
712{
713 return bpf_jit_enable && bpf_jit_is_ebpf();
714}
715
74451e66
DB
716static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
717{
718 return fp->jited && bpf_jit_is_ebpf();
719}
720
4f3446bb
DB
721static inline bool bpf_jit_blinding_enabled(void)
722{
723 /* These are the prerequisites, should someone ever have the
724 * idea to call blinding outside of them, we make sure to
725 * bail out.
726 */
727 if (!bpf_jit_is_ebpf())
728 return false;
729 if (!bpf_jit_enable)
730 return false;
731 if (!bpf_jit_harden)
732 return false;
733 if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
734 return false;
735
736 return true;
737}
74451e66
DB
738
739static inline bool bpf_jit_kallsyms_enabled(void)
740{
741 /* There are a couple of corner cases where kallsyms should
742 * not be enabled f.e. on hardening.
743 */
744 if (bpf_jit_harden)
745 return false;
746 if (!bpf_jit_kallsyms)
747 return false;
748 if (bpf_jit_kallsyms == 1)
749 return true;
750
751 return false;
752}
753
754const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
755 unsigned long *off, char *sym);
756bool is_bpf_text_address(unsigned long addr);
757int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
758 char *sym);
759
760static inline const char *
761bpf_address_lookup(unsigned long addr, unsigned long *size,
762 unsigned long *off, char **modname, char *sym)
763{
764 const char *ret = __bpf_address_lookup(addr, size, off, sym);
765
766 if (ret && modname)
767 *modname = NULL;
768 return ret;
769}
770
771void bpf_prog_kallsyms_add(struct bpf_prog *fp);
772void bpf_prog_kallsyms_del(struct bpf_prog *fp);
773
774#else /* CONFIG_BPF_JIT */
775
81ed18ab
AS
776static inline bool ebpf_jit_enabled(void)
777{
778 return false;
779}
780
74451e66
DB
781static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
782{
783 return false;
784}
785
b954d834
DB
786static inline void bpf_jit_free(struct bpf_prog *fp)
787{
788 bpf_prog_unlock_free(fp);
789}
74451e66
DB
790
791static inline bool bpf_jit_kallsyms_enabled(void)
792{
793 return false;
794}
795
796static inline const char *
797__bpf_address_lookup(unsigned long addr, unsigned long *size,
798 unsigned long *off, char *sym)
799{
800 return NULL;
801}
802
803static inline bool is_bpf_text_address(unsigned long addr)
804{
805 return false;
806}
807
808static inline int bpf_get_kallsym(unsigned int symnum, unsigned long *value,
809 char *type, char *sym)
810{
811 return -ERANGE;
812}
813
814static inline const char *
815bpf_address_lookup(unsigned long addr, unsigned long *size,
816 unsigned long *off, char **modname, char *sym)
817{
818 return NULL;
819}
820
821static inline void bpf_prog_kallsyms_add(struct bpf_prog *fp)
822{
823}
824
825static inline void bpf_prog_kallsyms_del(struct bpf_prog *fp)
826{
827}
b954d834
DB
828#endif /* CONFIG_BPF_JIT */
829
34805931
DB
830#define BPF_ANC BIT(15)
831
55795ef5
RV
832static inline bool bpf_needs_clear_a(const struct sock_filter *first)
833{
834 switch (first->code) {
835 case BPF_RET | BPF_K:
836 case BPF_LD | BPF_W | BPF_LEN:
837 return false;
838
839 case BPF_LD | BPF_W | BPF_ABS:
840 case BPF_LD | BPF_H | BPF_ABS:
841 case BPF_LD | BPF_B | BPF_ABS:
842 if (first->k == SKF_AD_OFF + SKF_AD_ALU_XOR_X)
843 return true;
844 return false;
845
846 default:
847 return true;
848 }
849}
850
34805931
DB
851static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
852{
853 BUG_ON(ftest->code & BPF_ANC);
854
855 switch (ftest->code) {
856 case BPF_LD | BPF_W | BPF_ABS:
857 case BPF_LD | BPF_H | BPF_ABS:
858 case BPF_LD | BPF_B | BPF_ABS:
859#define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
860 return BPF_ANC | SKF_AD_##CODE
861 switch (ftest->k) {
862 BPF_ANCILLARY(PROTOCOL);
863 BPF_ANCILLARY(PKTTYPE);
864 BPF_ANCILLARY(IFINDEX);
865 BPF_ANCILLARY(NLATTR);
866 BPF_ANCILLARY(NLATTR_NEST);
867 BPF_ANCILLARY(MARK);
868 BPF_ANCILLARY(QUEUE);
869 BPF_ANCILLARY(HATYPE);
870 BPF_ANCILLARY(RXHASH);
871 BPF_ANCILLARY(CPU);
872 BPF_ANCILLARY(ALU_XOR_X);
873 BPF_ANCILLARY(VLAN_TAG);
874 BPF_ANCILLARY(VLAN_TAG_PRESENT);
875 BPF_ANCILLARY(PAY_OFFSET);
876 BPF_ANCILLARY(RANDOM);
27cd5452 877 BPF_ANCILLARY(VLAN_TPID);
34805931
DB
878 }
879 /* Fallthrough. */
880 default:
881 return ftest->code;
882 }
883}
884
9f12fbe6
ZSL
885void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
886 int k, unsigned int size);
887
888static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
889 unsigned int size, void *buffer)
890{
891 if (k >= 0)
892 return skb_header_pointer(skb, k, size, buffer);
893
894 return bpf_internal_load_pointer_neg_helper(skb, k, size);
895}
896
ea02f941
MS
897static inline int bpf_tell_extensions(void)
898{
37692299 899 return SKF_AD_MAX;
ea02f941
MS
900}
901
1da177e4 902#endif /* __LINUX_FILTER_H__ */