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