xref: /linux/include/linux/filter.h (revision 5a8cd539ac19f7a68e68e1d25ef9ca2ff55b8500)
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 <linux/atomic.h>
9 #include <linux/bpf.h>
10 #include <linux/refcount.h>
11 #include <linux/compat.h>
12 #include <linux/skbuff.h>
13 #include <linux/linkage.h>
14 #include <linux/printk.h>
15 #include <linux/workqueue.h>
16 #include <linux/sched.h>
17 #include <linux/sched/clock.h>
18 #include <linux/capability.h>
19 #include <linux/set_memory.h>
20 #include <linux/kallsyms.h>
21 #include <linux/if_vlan.h>
22 #include <linux/vmalloc.h>
23 #include <linux/sockptr.h>
24 #include <linux/static_call.h>
25 #include <linux/u64_stats_sync.h>
26 
27 #include <net/sch_generic.h>
28 
29 #include <asm/byteorder.h>
30 #include <uapi/linux/filter.h>
31 
32 struct sk_buff;
33 struct sock;
34 struct seccomp_data;
35 struct bpf_prog_aux;
36 struct xdp_rxq_info;
37 struct xdp_buff;
38 struct sock_reuseport;
39 struct ctl_table;
40 struct ctl_table_header;
41 
42 /* ArgX, context and stack frame pointer register positions. Note,
43  * Arg1, Arg2, Arg3, etc are used as argument mappings of function
44  * calls in BPF_CALL instruction.
45  */
46 #define BPF_REG_ARG1	BPF_REG_1
47 #define BPF_REG_ARG2	BPF_REG_2
48 #define BPF_REG_ARG3	BPF_REG_3
49 #define BPF_REG_ARG4	BPF_REG_4
50 #define BPF_REG_ARG5	BPF_REG_5
51 #define BPF_REG_CTX	BPF_REG_6
52 #define BPF_REG_FP	BPF_REG_10
53 
54 /* Additional register mappings for converted user programs. */
55 #define BPF_REG_A	BPF_REG_0
56 #define BPF_REG_X	BPF_REG_7
57 #define BPF_REG_TMP	BPF_REG_2	/* scratch reg */
58 #define BPF_REG_D	BPF_REG_8	/* data, callee-saved */
59 #define BPF_REG_H	BPF_REG_9	/* hlen, callee-saved */
60 
61 /* Kernel hidden auxiliary/helper register. */
62 #define BPF_REG_PARAMS		MAX_BPF_REG
63 #define BPF_REG_AX		(MAX_BPF_REG + 1)
64 #define MAX_BPF_EXT_REG		(MAX_BPF_REG + 2)
65 #define MAX_BPF_JIT_REG		MAX_BPF_EXT_REG
66 
67 /* unused opcode to mark special call to bpf_tail_call() helper */
68 #define BPF_TAIL_CALL	0xf0
69 
70 /* unused opcode to mark special load instruction. Same as BPF_ABS */
71 #define BPF_PROBE_MEM	0x20
72 
73 /* unused opcode to mark special ldsx instruction. Same as BPF_IND */
74 #define BPF_PROBE_MEMSX	0x40
75 
76 /* unused opcode to mark special load instruction. Same as BPF_MSH */
77 #define BPF_PROBE_MEM32	0xa0
78 
79 /* unused opcode to mark special atomic instruction */
80 #define BPF_PROBE_ATOMIC 0xe0
81 
82 /* unused opcode to mark special ldsx instruction. Same as BPF_NOSPEC */
83 #define BPF_PROBE_MEM32SX 0xc0
84 
85 /* unused opcode to mark call to interpreter with arguments */
86 #define BPF_CALL_ARGS	0xe0
87 
88 /* unused opcode to mark speculation barrier for mitigating
89  * Spectre v1 and v4
90  */
91 #define BPF_NOSPEC	0xc0
92 
93 /* As per nm, we expose JITed images as text (code) section for
94  * kallsyms. That way, tools like perf can find it to match
95  * addresses.
96  */
97 #define BPF_SYM_ELF_TYPE	't'
98 
99 /* BPF program can access up to 512 bytes of stack space. */
100 #define MAX_BPF_STACK	512
101 
102 /* Helper macros for filter block array initializers. */
103 
104 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
105 
106 #define BPF_ALU64_REG_OFF(OP, DST, SRC, OFF)			\
107 	((struct bpf_insn) {					\
108 		.code  = BPF_ALU64 | BPF_OP(OP) | BPF_X,	\
109 		.dst_reg = DST,					\
110 		.src_reg = SRC,					\
111 		.off   = OFF,					\
112 		.imm   = 0 })
113 
114 #define BPF_ALU64_REG(OP, DST, SRC)				\
115 	BPF_ALU64_REG_OFF(OP, DST, SRC, 0)
116 
117 #define BPF_ALU32_REG_OFF(OP, DST, SRC, OFF)			\
118 	((struct bpf_insn) {					\
119 		.code  = BPF_ALU | BPF_OP(OP) | BPF_X,		\
120 		.dst_reg = DST,					\
121 		.src_reg = SRC,					\
122 		.off   = OFF,					\
123 		.imm   = 0 })
124 
125 #define BPF_ALU32_REG(OP, DST, SRC)				\
126 	BPF_ALU32_REG_OFF(OP, DST, SRC, 0)
127 
128 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
129 
130 #define BPF_ALU64_IMM_OFF(OP, DST, IMM, OFF)			\
131 	((struct bpf_insn) {					\
132 		.code  = BPF_ALU64 | BPF_OP(OP) | BPF_K,	\
133 		.dst_reg = DST,					\
134 		.src_reg = 0,					\
135 		.off   = OFF,					\
136 		.imm   = IMM })
137 #define BPF_ALU64_IMM(OP, DST, IMM)				\
138 	BPF_ALU64_IMM_OFF(OP, DST, IMM, 0)
139 
140 #define BPF_ALU32_IMM_OFF(OP, DST, IMM, OFF)			\
141 	((struct bpf_insn) {					\
142 		.code  = BPF_ALU | BPF_OP(OP) | BPF_K,		\
143 		.dst_reg = DST,					\
144 		.src_reg = 0,					\
145 		.off   = OFF,					\
146 		.imm   = IMM })
147 #define BPF_ALU32_IMM(OP, DST, IMM)				\
148 	BPF_ALU32_IMM_OFF(OP, DST, IMM, 0)
149 
150 /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
151 
152 #define BPF_ENDIAN(TYPE, DST, LEN)				\
153 	((struct bpf_insn) {					\
154 		.code  = BPF_ALU | BPF_END | BPF_SRC(TYPE),	\
155 		.dst_reg = DST,					\
156 		.src_reg = 0,					\
157 		.off   = 0,					\
158 		.imm   = LEN })
159 
160 /* Byte Swap, bswap16/32/64 */
161 
162 #define BPF_BSWAP(DST, LEN)					\
163 	((struct bpf_insn) {					\
164 		.code  = BPF_ALU64 | BPF_END | BPF_SRC(BPF_TO_LE),	\
165 		.dst_reg = DST,					\
166 		.src_reg = 0,					\
167 		.off   = 0,					\
168 		.imm   = LEN })
169 
170 /* Short form of mov, dst_reg = src_reg */
171 
172 #define BPF_MOV64_REG(DST, SRC)					\
173 	((struct bpf_insn) {					\
174 		.code  = BPF_ALU64 | BPF_MOV | BPF_X,		\
175 		.dst_reg = DST,					\
176 		.src_reg = SRC,					\
177 		.off   = 0,					\
178 		.imm   = 0 })
179 
180 #define BPF_MOV32_REG(DST, SRC)					\
181 	((struct bpf_insn) {					\
182 		.code  = BPF_ALU | BPF_MOV | BPF_X,		\
183 		.dst_reg = DST,					\
184 		.src_reg = SRC,					\
185 		.off   = 0,					\
186 		.imm   = 0 })
187 
188 /* Special (internal-only) form of mov, used to resolve per-CPU addrs:
189  * dst_reg = src_reg + <percpu_base_off>
190  * BPF_ADDR_PERCPU is used as a special insn->off value.
191  */
192 #define BPF_ADDR_PERCPU	(-1)
193 
194 #define BPF_MOV64_PERCPU_REG(DST, SRC)				\
195 	((struct bpf_insn) {					\
196 		.code  = BPF_ALU64 | BPF_MOV | BPF_X,		\
197 		.dst_reg = DST,					\
198 		.src_reg = SRC,					\
199 		.off   = BPF_ADDR_PERCPU,			\
200 		.imm   = 0 })
201 
insn_is_mov_percpu_addr(const struct bpf_insn * insn)202 static inline bool insn_is_mov_percpu_addr(const struct bpf_insn *insn)
203 {
204 	return insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) && insn->off == BPF_ADDR_PERCPU;
205 }
206 
207 /* Short form of mov, dst_reg = imm32 */
208 
209 #define BPF_MOV64_IMM(DST, IMM)					\
210 	((struct bpf_insn) {					\
211 		.code  = BPF_ALU64 | BPF_MOV | BPF_K,		\
212 		.dst_reg = DST,					\
213 		.src_reg = 0,					\
214 		.off   = 0,					\
215 		.imm   = IMM })
216 
217 #define BPF_MOV32_IMM(DST, IMM)					\
218 	((struct bpf_insn) {					\
219 		.code  = BPF_ALU | BPF_MOV | BPF_K,		\
220 		.dst_reg = DST,					\
221 		.src_reg = 0,					\
222 		.off   = 0,					\
223 		.imm   = IMM })
224 
225 /* Short form of movsx, dst_reg = (s8,s16,s32)src_reg */
226 
227 #define BPF_MOVSX64_REG(DST, SRC, OFF)				\
228 	((struct bpf_insn) {					\
229 		.code  = BPF_ALU64 | BPF_MOV | BPF_X,		\
230 		.dst_reg = DST,					\
231 		.src_reg = SRC,					\
232 		.off   = OFF,					\
233 		.imm   = 0 })
234 
235 #define BPF_MOVSX32_REG(DST, SRC, OFF)				\
236 	((struct bpf_insn) {					\
237 		.code  = BPF_ALU | BPF_MOV | BPF_X,		\
238 		.dst_reg = DST,					\
239 		.src_reg = SRC,					\
240 		.off   = OFF,					\
241 		.imm   = 0 })
242 
243 /* Special form of mov32, used for doing explicit zero extension on dst. */
244 #define BPF_ZEXT_REG(DST)					\
245 	((struct bpf_insn) {					\
246 		.code  = BPF_ALU | BPF_MOV | BPF_X,		\
247 		.dst_reg = DST,					\
248 		.src_reg = DST,					\
249 		.off   = 0,					\
250 		.imm   = 1 })
251 
insn_is_zext(const struct bpf_insn * insn)252 static inline bool insn_is_zext(const struct bpf_insn *insn)
253 {
254 	return insn->code == (BPF_ALU | BPF_MOV | BPF_X) && insn->imm == 1;
255 }
256 
257 /* addr_space_cast from as(0) to as(1) is for converting bpf arena pointers
258  * to pointers in user vma.
259  */
insn_is_cast_user(const struct bpf_insn * insn)260 static inline bool insn_is_cast_user(const struct bpf_insn *insn)
261 {
262 	return insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) &&
263 			      insn->off == BPF_ADDR_SPACE_CAST &&
264 			      insn->imm == 1U << 16;
265 }
266 
267 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
268 #define BPF_LD_IMM64(DST, IMM)					\
269 	BPF_LD_IMM64_RAW(DST, 0, IMM)
270 
271 #define BPF_LD_IMM64_RAW(DST, SRC, IMM)				\
272 	((struct bpf_insn) {					\
273 		.code  = BPF_LD | BPF_DW | BPF_IMM,		\
274 		.dst_reg = DST,					\
275 		.src_reg = SRC,					\
276 		.off   = 0,					\
277 		.imm   = (__u32) (IMM) }),			\
278 	((struct bpf_insn) {					\
279 		.code  = 0, /* zero is reserved opcode */	\
280 		.dst_reg = 0,					\
281 		.src_reg = 0,					\
282 		.off   = 0,					\
283 		.imm   = ((__u64) (IMM)) >> 32 })
284 
285 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
286 #define BPF_LD_MAP_FD(DST, MAP_FD)				\
287 	BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
288 
289 /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
290 
291 #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM)			\
292 	((struct bpf_insn) {					\
293 		.code  = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE),	\
294 		.dst_reg = DST,					\
295 		.src_reg = SRC,					\
296 		.off   = 0,					\
297 		.imm   = IMM })
298 
299 #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM)			\
300 	((struct bpf_insn) {					\
301 		.code  = BPF_ALU | BPF_MOV | BPF_SRC(TYPE),	\
302 		.dst_reg = DST,					\
303 		.src_reg = SRC,					\
304 		.off   = 0,					\
305 		.imm   = IMM })
306 
307 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
308 
309 #define BPF_LD_ABS(SIZE, IMM)					\
310 	((struct bpf_insn) {					\
311 		.code  = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS,	\
312 		.dst_reg = 0,					\
313 		.src_reg = 0,					\
314 		.off   = 0,					\
315 		.imm   = IMM })
316 
317 /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
318 
319 #define BPF_LD_IND(SIZE, SRC, IMM)				\
320 	((struct bpf_insn) {					\
321 		.code  = BPF_LD | BPF_SIZE(SIZE) | BPF_IND,	\
322 		.dst_reg = 0,					\
323 		.src_reg = SRC,					\
324 		.off   = 0,					\
325 		.imm   = IMM })
326 
327 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
328 
329 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF)			\
330 	((struct bpf_insn) {					\
331 		.code  = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM,	\
332 		.dst_reg = DST,					\
333 		.src_reg = SRC,					\
334 		.off   = OFF,					\
335 		.imm   = 0 })
336 
337 /* Memory load, dst_reg = *(signed size *) (src_reg + off16) */
338 
339 #define BPF_LDX_MEMSX(SIZE, DST, SRC, OFF)			\
340 	((struct bpf_insn) {					\
341 		.code  = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEMSX,	\
342 		.dst_reg = DST,					\
343 		.src_reg = SRC,					\
344 		.off   = OFF,					\
345 		.imm   = 0 })
346 
347 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
348 
349 #define BPF_STX_MEM(SIZE, DST, SRC, OFF)			\
350 	((struct bpf_insn) {					\
351 		.code  = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM,	\
352 		.dst_reg = DST,					\
353 		.src_reg = SRC,					\
354 		.off   = OFF,					\
355 		.imm   = 0 })
356 
357 
358 /*
359  * Atomic operations:
360  *
361  *   BPF_ADD                  *(uint *) (dst_reg + off16) += src_reg
362  *   BPF_AND                  *(uint *) (dst_reg + off16) &= src_reg
363  *   BPF_OR                   *(uint *) (dst_reg + off16) |= src_reg
364  *   BPF_XOR                  *(uint *) (dst_reg + off16) ^= src_reg
365  *   BPF_ADD | BPF_FETCH      src_reg = atomic_fetch_add(dst_reg + off16, src_reg);
366  *   BPF_AND | BPF_FETCH      src_reg = atomic_fetch_and(dst_reg + off16, src_reg);
367  *   BPF_OR | BPF_FETCH       src_reg = atomic_fetch_or(dst_reg + off16, src_reg);
368  *   BPF_XOR | BPF_FETCH      src_reg = atomic_fetch_xor(dst_reg + off16, src_reg);
369  *   BPF_XCHG                 src_reg = atomic_xchg(dst_reg + off16, src_reg)
370  *   BPF_CMPXCHG              r0 = atomic_cmpxchg(dst_reg + off16, r0, src_reg)
371  *   BPF_LOAD_ACQ             dst_reg = smp_load_acquire(src_reg + off16)
372  *   BPF_STORE_REL            smp_store_release(dst_reg + off16, src_reg)
373  */
374 
375 #define BPF_ATOMIC_OP(SIZE, OP, DST, SRC, OFF)			\
376 	((struct bpf_insn) {					\
377 		.code  = BPF_STX | BPF_SIZE(SIZE) | BPF_ATOMIC,	\
378 		.dst_reg = DST,					\
379 		.src_reg = SRC,					\
380 		.off   = OFF,					\
381 		.imm   = OP })
382 
383 /* Legacy alias */
384 #define BPF_STX_XADD(SIZE, DST, SRC, OFF) BPF_ATOMIC_OP(SIZE, BPF_ADD, DST, SRC, OFF)
385 
386 /*
387  * Given a BPF_ATOMIC instruction @atomic_insn, return true if it is an
388  * atomic load or store, and false if it is a read-modify-write instruction.
389  */
390 static inline bool
bpf_atomic_is_load_store(const struct bpf_insn * atomic_insn)391 bpf_atomic_is_load_store(const struct bpf_insn *atomic_insn)
392 {
393 	switch (atomic_insn->imm) {
394 	case BPF_LOAD_ACQ:
395 	case BPF_STORE_REL:
396 		return true;
397 	default:
398 		return false;
399 	}
400 }
401 
402 /*
403  * A load-acquire is the only BPF_STX class instruction that reads into
404  * dst_reg from src_reg + off16, i.e. it has the operand roles of a BPF_LDX.
405  * Unlike bpf_atomic_is_load_store(), @insn is not assumed to be a BPF_ATOMIC
406  * instruction here, so that callers which walk all instruction classes can
407  * use this directly.
408  */
bpf_atomic_is_load_acq(const struct bpf_insn * insn)409 static inline bool bpf_atomic_is_load_acq(const struct bpf_insn *insn)
410 {
411 	return BPF_CLASS(insn->code) == BPF_STX &&
412 	       (BPF_MODE(insn->code) == BPF_ATOMIC ||
413 		BPF_MODE(insn->code) == BPF_PROBE_ATOMIC) &&
414 	       insn->imm == BPF_LOAD_ACQ;
415 }
416 
417 /*
418  * Given an instruction @insn, return the number of the BPF register that a
419  * BPF_ATOMIC reads the value at its memory operand into, or -1 if there is
420  * no such register. That is the register a BPF_PROBE_ATOMIC has to clear when
421  * the access faults. Like bpf_atomic_is_load_acq(), @insn is not assumed to
422  * be a BPF_ATOMIC here.
423  */
bpf_atomic_load_reg(const struct bpf_insn * insn)424 static inline int bpf_atomic_load_reg(const struct bpf_insn *insn)
425 {
426 	if (BPF_CLASS(insn->code) != BPF_STX ||
427 	    (BPF_MODE(insn->code) != BPF_ATOMIC &&
428 	     BPF_MODE(insn->code) != BPF_PROBE_ATOMIC))
429 		return -1;
430 
431 	switch (insn->imm) {
432 	case BPF_LOAD_ACQ:
433 		return insn->dst_reg;
434 	case BPF_CMPXCHG:
435 		return BPF_REG_0;
436 	default:
437 		return (insn->imm & BPF_FETCH) ? insn->src_reg : -1;
438 	}
439 }
440 
441 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
442 
443 #define BPF_ST_MEM(SIZE, DST, OFF, IMM)				\
444 	((struct bpf_insn) {					\
445 		.code  = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM,	\
446 		.dst_reg = DST,					\
447 		.src_reg = 0,					\
448 		.off   = OFF,					\
449 		.imm   = IMM })
450 
451 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
452 
453 #define BPF_JMP_REG(OP, DST, SRC, OFF)				\
454 	((struct bpf_insn) {					\
455 		.code  = BPF_JMP | BPF_OP(OP) | BPF_X,		\
456 		.dst_reg = DST,					\
457 		.src_reg = SRC,					\
458 		.off   = OFF,					\
459 		.imm   = 0 })
460 
461 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
462 
463 #define BPF_JMP_IMM(OP, DST, IMM, OFF)				\
464 	((struct bpf_insn) {					\
465 		.code  = BPF_JMP | BPF_OP(OP) | BPF_K,		\
466 		.dst_reg = DST,					\
467 		.src_reg = 0,					\
468 		.off   = OFF,					\
469 		.imm   = IMM })
470 
471 /* Like BPF_JMP_REG, but with 32-bit wide operands for comparison. */
472 
473 #define BPF_JMP32_REG(OP, DST, SRC, OFF)			\
474 	((struct bpf_insn) {					\
475 		.code  = BPF_JMP32 | BPF_OP(OP) | BPF_X,	\
476 		.dst_reg = DST,					\
477 		.src_reg = SRC,					\
478 		.off   = OFF,					\
479 		.imm   = 0 })
480 
481 /* Like BPF_JMP_IMM, but with 32-bit wide operands for comparison. */
482 
483 #define BPF_JMP32_IMM(OP, DST, IMM, OFF)			\
484 	((struct bpf_insn) {					\
485 		.code  = BPF_JMP32 | BPF_OP(OP) | BPF_K,	\
486 		.dst_reg = DST,					\
487 		.src_reg = 0,					\
488 		.off   = OFF,					\
489 		.imm   = IMM })
490 
491 /* Unconditional jumps, goto pc + off16 */
492 
493 #define BPF_JMP_A(OFF)						\
494 	((struct bpf_insn) {					\
495 		.code  = BPF_JMP | BPF_JA,			\
496 		.dst_reg = 0,					\
497 		.src_reg = 0,					\
498 		.off   = OFF,					\
499 		.imm   = 0 })
500 
501 /* Unconditional jumps, gotol pc + imm32 */
502 
503 #define BPF_JMP32_A(IMM)					\
504 	((struct bpf_insn) {					\
505 		.code  = BPF_JMP32 | BPF_JA,			\
506 		.dst_reg = 0,					\
507 		.src_reg = 0,					\
508 		.off   = 0,					\
509 		.imm   = IMM })
510 
511 /* Relative call */
512 
513 #define BPF_CALL_REL(TGT)					\
514 	((struct bpf_insn) {					\
515 		.code  = BPF_JMP | BPF_CALL,			\
516 		.dst_reg = 0,					\
517 		.src_reg = BPF_PSEUDO_CALL,			\
518 		.off   = 0,					\
519 		.imm   = TGT })
520 
521 /* Convert function address to BPF immediate */
522 
523 #define BPF_CALL_IMM(x)	((void *)(x) - (void *)__bpf_call_base)
524 
525 #define BPF_EMIT_CALL(FUNC)					\
526 	((struct bpf_insn) {					\
527 		.code  = BPF_JMP | BPF_CALL,			\
528 		.dst_reg = 0,					\
529 		.src_reg = 0,					\
530 		.off   = 0,					\
531 		.imm   = BPF_CALL_IMM(FUNC) })
532 
533 /* Kfunc call */
534 
535 #define BPF_CALL_KFUNC(OFF, IMM)				\
536 	((struct bpf_insn) {					\
537 		.code  = BPF_JMP | BPF_CALL,			\
538 		.dst_reg = 0,					\
539 		.src_reg = BPF_PSEUDO_KFUNC_CALL,		\
540 		.off   = OFF,					\
541 		.imm   = IMM })
542 
543 /* Raw code statement block */
544 
545 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM)			\
546 	((struct bpf_insn) {					\
547 		.code  = CODE,					\
548 		.dst_reg = DST,					\
549 		.src_reg = SRC,					\
550 		.off   = OFF,					\
551 		.imm   = IMM })
552 
553 /* Program exit */
554 
555 #define BPF_EXIT_INSN()						\
556 	((struct bpf_insn) {					\
557 		.code  = BPF_JMP | BPF_EXIT,			\
558 		.dst_reg = 0,					\
559 		.src_reg = 0,					\
560 		.off   = 0,					\
561 		.imm   = 0 })
562 
563 /* Speculation barrier */
564 
565 #define BPF_ST_NOSPEC()						\
566 	((struct bpf_insn) {					\
567 		.code  = BPF_ST | BPF_NOSPEC,			\
568 		.dst_reg = 0,					\
569 		.src_reg = 0,					\
570 		.off   = 0,					\
571 		.imm   = 0 })
572 
573 /* Internal classic blocks for direct assignment */
574 
575 #define __BPF_STMT(CODE, K)					\
576 	((struct sock_filter) BPF_STMT(CODE, K))
577 
578 #define __BPF_JUMP(CODE, K, JT, JF)				\
579 	((struct sock_filter) BPF_JUMP(CODE, K, JT, JF))
580 
581 #define bytes_to_bpf_size(bytes)				\
582 ({								\
583 	int bpf_size = -EINVAL;					\
584 								\
585 	if (bytes == sizeof(u8))				\
586 		bpf_size = BPF_B;				\
587 	else if (bytes == sizeof(u16))				\
588 		bpf_size = BPF_H;				\
589 	else if (bytes == sizeof(u32))				\
590 		bpf_size = BPF_W;				\
591 	else if (bytes == sizeof(u64))				\
592 		bpf_size = BPF_DW;				\
593 								\
594 	bpf_size;						\
595 })
596 
597 #define bpf_size_to_bytes(bpf_size)				\
598 ({								\
599 	int bytes = -EINVAL;					\
600 								\
601 	if (bpf_size == BPF_B)					\
602 		bytes = sizeof(u8);				\
603 	else if (bpf_size == BPF_H)				\
604 		bytes = sizeof(u16);				\
605 	else if (bpf_size == BPF_W)				\
606 		bytes = sizeof(u32);				\
607 	else if (bpf_size == BPF_DW)				\
608 		bytes = sizeof(u64);				\
609 								\
610 	bytes;							\
611 })
612 
613 #define BPF_SIZEOF(type)					\
614 	({							\
615 		const int __size = bytes_to_bpf_size(sizeof(type)); \
616 		BUILD_BUG_ON(__size < 0);			\
617 		__size;						\
618 	})
619 
620 #define BPF_FIELD_SIZEOF(type, field)				\
621 	({							\
622 		const int __size = bytes_to_bpf_size(sizeof_field(type, field)); \
623 		BUILD_BUG_ON(__size < 0);			\
624 		__size;						\
625 	})
626 
627 #define BPF_LDST_BYTES(insn)					\
628 	({							\
629 		const int __size = bpf_size_to_bytes(BPF_SIZE((insn)->code)); \
630 		WARN_ON(__size < 0);				\
631 		__size;						\
632 	})
633 
634 #define __BPF_MAP_0(m, v, ...) v
635 #define __BPF_MAP_1(m, v, t, a, ...) m(t, a)
636 #define __BPF_MAP_2(m, v, t, a, ...) m(t, a), __BPF_MAP_1(m, v, __VA_ARGS__)
637 #define __BPF_MAP_3(m, v, t, a, ...) m(t, a), __BPF_MAP_2(m, v, __VA_ARGS__)
638 #define __BPF_MAP_4(m, v, t, a, ...) m(t, a), __BPF_MAP_3(m, v, __VA_ARGS__)
639 #define __BPF_MAP_5(m, v, t, a, ...) m(t, a), __BPF_MAP_4(m, v, __VA_ARGS__)
640 
641 #define __BPF_REG_0(...) __BPF_PAD(5)
642 #define __BPF_REG_1(...) __BPF_MAP(1, __VA_ARGS__), __BPF_PAD(4)
643 #define __BPF_REG_2(...) __BPF_MAP(2, __VA_ARGS__), __BPF_PAD(3)
644 #define __BPF_REG_3(...) __BPF_MAP(3, __VA_ARGS__), __BPF_PAD(2)
645 #define __BPF_REG_4(...) __BPF_MAP(4, __VA_ARGS__), __BPF_PAD(1)
646 #define __BPF_REG_5(...) __BPF_MAP(5, __VA_ARGS__)
647 
648 #define __BPF_MAP(n, ...) __BPF_MAP_##n(__VA_ARGS__)
649 #define __BPF_REG(n, ...) __BPF_REG_##n(__VA_ARGS__)
650 
651 #define __BPF_CAST(t, a)						       \
652 	(__force t)							       \
653 	(__force							       \
654 	 typeof(__builtin_choose_expr(sizeof(t) == sizeof(unsigned long),      \
655 				      (unsigned long)0, (t)0))) a
656 #define __BPF_V void
657 #define __BPF_N
658 
659 #define __BPF_DECL_ARGS(t, a) t   a
660 #define __BPF_DECL_REGS(t, a) u64 a
661 
662 #define __BPF_PAD(n)							       \
663 	__BPF_MAP(n, __BPF_DECL_ARGS, __BPF_N, u64, __ur_1, u64, __ur_2,       \
664 		  u64, __ur_3, u64, __ur_4, u64, __ur_5)
665 
666 #define BPF_CALL_x(x, attr, name, ...)					       \
667 	static __always_inline						       \
668 	u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__));   \
669 	typedef u64 (*btf_##name)(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \
670 	attr u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__));    \
671 	attr u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__))     \
672 	{								       \
673 		return ((btf_##name)____##name)(__BPF_MAP(x,__BPF_CAST,__BPF_N,__VA_ARGS__));\
674 	}								       \
675 	static __always_inline						       \
676 	u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__))
677 
678 #define __NOATTR
679 #define BPF_CALL_0(name, ...)	BPF_CALL_x(0, __NOATTR, name, __VA_ARGS__)
680 #define BPF_CALL_1(name, ...)	BPF_CALL_x(1, __NOATTR, name, __VA_ARGS__)
681 #define BPF_CALL_2(name, ...)	BPF_CALL_x(2, __NOATTR, name, __VA_ARGS__)
682 #define BPF_CALL_3(name, ...)	BPF_CALL_x(3, __NOATTR, name, __VA_ARGS__)
683 #define BPF_CALL_4(name, ...)	BPF_CALL_x(4, __NOATTR, name, __VA_ARGS__)
684 #define BPF_CALL_5(name, ...)	BPF_CALL_x(5, __NOATTR, name, __VA_ARGS__)
685 
686 #define NOTRACE_BPF_CALL_1(name, ...)	BPF_CALL_x(1, notrace, name, __VA_ARGS__)
687 
688 #define bpf_ctx_range(TYPE, MEMBER)						\
689 	offsetof(TYPE, MEMBER) ... offsetofend(TYPE, MEMBER) - 1
690 #define bpf_ctx_range_till(TYPE, MEMBER1, MEMBER2)				\
691 	offsetof(TYPE, MEMBER1) ... offsetofend(TYPE, MEMBER2) - 1
692 #if BITS_PER_LONG == 64
693 # define bpf_ctx_range_ptr(TYPE, MEMBER)					\
694 	offsetof(TYPE, MEMBER) ... offsetofend(TYPE, MEMBER) - 1
695 #else
696 # define bpf_ctx_range_ptr(TYPE, MEMBER)					\
697 	offsetof(TYPE, MEMBER) ... offsetof(TYPE, MEMBER) + 8 - 1
698 #endif /* BITS_PER_LONG == 64 */
699 
700 #define bpf_target_off(TYPE, MEMBER, SIZE, PTR_SIZE)				\
701 	({									\
702 		BUILD_BUG_ON(sizeof_field(TYPE, MEMBER) != (SIZE));		\
703 		*(PTR_SIZE) = (SIZE);						\
704 		offsetof(TYPE, MEMBER);						\
705 	})
706 
707 /* A struct sock_filter is architecture independent. */
708 struct compat_sock_fprog {
709 	u16		len;
710 	compat_uptr_t	filter;	/* struct sock_filter * */
711 };
712 
713 struct sock_fprog_kern {
714 	u16			len;
715 	struct sock_filter	*filter;
716 };
717 
718 /* Some arches need doubleword alignment for their instructions and/or data */
719 #define BPF_IMAGE_ALIGNMENT 8
720 
721 struct bpf_binary_header {
722 	u32 size;
723 	u8 image[] __aligned(BPF_IMAGE_ALIGNMENT);
724 };
725 
726 struct bpf_prog_stats {
727 	u64_stats_t cnt;
728 	u64_stats_t nsecs;
729 	u64_stats_t misses;
730 	struct u64_stats_sync syncp;
731 } __aligned(2 * sizeof(u64));
732 
733 struct bpf_timed_may_goto {
734 	u64 count;
735 	u64 timestamp;
736 };
737 
738 struct sk_filter {
739 	refcount_t	refcnt;
740 	struct rcu_head	rcu;
741 	struct bpf_prog	*prog;
742 };
743 
744 DECLARE_STATIC_KEY_FALSE(bpf_stats_enabled_key);
745 
746 extern struct mutex nf_conn_btf_access_lock;
747 extern int (*nfct_btf_struct_access)(struct bpf_verifier_log *log,
748 				     const struct bpf_reg_state *reg,
749 				     int off, int size);
750 
751 typedef unsigned int (*bpf_dispatcher_fn)(const void *ctx,
752 					  const struct bpf_insn *insnsi,
753 					  unsigned int (*bpf_func)(const void *,
754 								   const struct bpf_insn *));
755 
__bpf_prog_run(const struct bpf_prog * prog,const void * ctx,bpf_dispatcher_fn dfunc)756 static __always_inline u32 __bpf_prog_run(const struct bpf_prog *prog,
757 					  const void *ctx,
758 					  bpf_dispatcher_fn dfunc)
759 {
760 	u32 ret;
761 
762 	cant_migrate();
763 	if (static_branch_unlikely(&bpf_stats_enabled_key)) {
764 		struct bpf_prog_stats *stats;
765 		u64 duration, start = sched_clock();
766 		unsigned long flags;
767 
768 		ret = dfunc(ctx, prog->insnsi, prog->bpf_func);
769 
770 		duration = sched_clock() - start;
771 		if (likely(prog->stats)) {
772 			stats = this_cpu_ptr(prog->stats);
773 			flags = u64_stats_update_begin_irqsave(&stats->syncp);
774 			u64_stats_inc(&stats->cnt);
775 			u64_stats_add(&stats->nsecs, duration);
776 			u64_stats_update_end_irqrestore(&stats->syncp, flags);
777 		}
778 	} else {
779 		ret = dfunc(ctx, prog->insnsi, prog->bpf_func);
780 	}
781 	return ret;
782 }
783 
bpf_prog_run(const struct bpf_prog * prog,const void * ctx)784 static __always_inline u32 bpf_prog_run(const struct bpf_prog *prog, const void *ctx)
785 {
786 	return __bpf_prog_run(prog, ctx, bpf_dispatcher_nop_func);
787 }
788 
789 /*
790  * Use in preemptible and therefore migratable context to make sure that
791  * the execution of the BPF program runs on one CPU.
792  *
793  * This uses migrate_disable/enable() explicitly to document that the
794  * invocation of a BPF program does not require reentrancy protection
795  * against a BPF program which is invoked from a preempting task.
796  */
bpf_prog_run_pin_on_cpu(const struct bpf_prog * prog,const void * ctx)797 static inline u32 bpf_prog_run_pin_on_cpu(const struct bpf_prog *prog,
798 					  const void *ctx)
799 {
800 	u32 ret;
801 
802 	migrate_disable();
803 	ret = bpf_prog_run(prog, ctx);
804 	migrate_enable();
805 	return ret;
806 }
807 
is_stack_arg_ldx(const struct bpf_insn * insn)808 static inline bool is_stack_arg_ldx(const struct bpf_insn *insn)
809 {
810 	return insn->code == (BPF_LDX | BPF_MEM | BPF_DW) &&
811 	       insn->src_reg == BPF_REG_PARAMS &&
812 	       insn->off > 0 && insn->off % 8 == 0;
813 }
814 
is_stack_arg_st(const struct bpf_insn * insn)815 static inline bool is_stack_arg_st(const struct bpf_insn *insn)
816 {
817 	return insn->code == (BPF_ST | BPF_MEM | BPF_DW) &&
818 	       insn->dst_reg == BPF_REG_PARAMS &&
819 	       insn->off < 0 && insn->off % 8 == 0;
820 }
821 
is_stack_arg_stx(const struct bpf_insn * insn)822 static inline bool is_stack_arg_stx(const struct bpf_insn *insn)
823 {
824 	return insn->code == (BPF_STX | BPF_MEM | BPF_DW) &&
825 	       insn->dst_reg == BPF_REG_PARAMS &&
826 	       insn->off < 0 && insn->off % 8 == 0;
827 }
828 
829 #define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN
830 
831 struct bpf_skb_data_end {
832 	struct qdisc_skb_cb qdisc_cb;
833 	void *data_meta;
834 	void *data_end;
835 };
836 
837 struct bpf_nh_params {
838 	u32 nh_family;
839 	union {
840 		u32 ipv4_nh;
841 		struct in6_addr ipv6_nh;
842 	};
843 };
844 
845 /* flags for bpf_redirect_info kern_flags */
846 #define BPF_RI_F_RF_NO_DIRECT	BIT(0)	/* no napi_direct on return_frame */
847 #define BPF_RI_F_RI_INIT	BIT(1)
848 #define BPF_RI_F_CPU_MAP_INIT	BIT(2)
849 #define BPF_RI_F_DEV_MAP_INIT	BIT(3)
850 #define BPF_RI_F_XSK_MAP_INIT	BIT(4)
851 
852 struct bpf_redirect_info {
853 	u64 tgt_index;
854 	void *tgt_value;
855 	struct bpf_map *map;
856 	u32 flags;
857 	u32 map_id;
858 	enum bpf_map_type map_type;
859 	struct bpf_nh_params nh;
860 	u32 kern_flags;
861 };
862 
863 struct bpf_net_context {
864 	struct bpf_redirect_info ri;
865 	struct list_head cpu_map_flush_list;
866 	struct list_head dev_map_flush_list;
867 	struct list_head xskmap_map_flush_list;
868 };
869 
bpf_net_ctx_set(struct bpf_net_context * bpf_net_ctx)870 static inline struct bpf_net_context *bpf_net_ctx_set(struct bpf_net_context *bpf_net_ctx)
871 {
872 	struct task_struct *tsk = current;
873 
874 	if (tsk->bpf_net_context != NULL)
875 		return NULL;
876 	bpf_net_ctx->ri.kern_flags = 0;
877 
878 	tsk->bpf_net_context = bpf_net_ctx;
879 	return bpf_net_ctx;
880 }
881 
bpf_net_ctx_clear(struct bpf_net_context * bpf_net_ctx)882 static inline void bpf_net_ctx_clear(struct bpf_net_context *bpf_net_ctx)
883 {
884 	if (bpf_net_ctx)
885 		current->bpf_net_context = NULL;
886 }
887 
bpf_net_ctx_get(void)888 static inline struct bpf_net_context *bpf_net_ctx_get(void)
889 {
890 	return current->bpf_net_context;
891 }
892 
bpf_net_ctx_get_ri(void)893 static inline struct bpf_redirect_info *bpf_net_ctx_get_ri(void)
894 {
895 	struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get();
896 
897 	if (!(bpf_net_ctx->ri.kern_flags & BPF_RI_F_RI_INIT)) {
898 		memset(&bpf_net_ctx->ri, 0, offsetof(struct bpf_net_context, ri.nh));
899 		bpf_net_ctx->ri.kern_flags |= BPF_RI_F_RI_INIT;
900 	}
901 
902 	return &bpf_net_ctx->ri;
903 }
904 
bpf_net_ctx_get_cpu_map_flush_list(void)905 static inline struct list_head *bpf_net_ctx_get_cpu_map_flush_list(void)
906 {
907 	struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get();
908 
909 	if (!(bpf_net_ctx->ri.kern_flags & BPF_RI_F_CPU_MAP_INIT)) {
910 		INIT_LIST_HEAD(&bpf_net_ctx->cpu_map_flush_list);
911 		bpf_net_ctx->ri.kern_flags |= BPF_RI_F_CPU_MAP_INIT;
912 	}
913 
914 	return &bpf_net_ctx->cpu_map_flush_list;
915 }
916 
bpf_net_ctx_get_dev_flush_list(void)917 static inline struct list_head *bpf_net_ctx_get_dev_flush_list(void)
918 {
919 	struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get();
920 
921 	if (!(bpf_net_ctx->ri.kern_flags & BPF_RI_F_DEV_MAP_INIT)) {
922 		INIT_LIST_HEAD(&bpf_net_ctx->dev_map_flush_list);
923 		bpf_net_ctx->ri.kern_flags |= BPF_RI_F_DEV_MAP_INIT;
924 	}
925 
926 	return &bpf_net_ctx->dev_map_flush_list;
927 }
928 
bpf_net_ctx_get_xskmap_flush_list(void)929 static inline struct list_head *bpf_net_ctx_get_xskmap_flush_list(void)
930 {
931 	struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get();
932 
933 	if (!(bpf_net_ctx->ri.kern_flags & BPF_RI_F_XSK_MAP_INIT)) {
934 		INIT_LIST_HEAD(&bpf_net_ctx->xskmap_map_flush_list);
935 		bpf_net_ctx->ri.kern_flags |= BPF_RI_F_XSK_MAP_INIT;
936 	}
937 
938 	return &bpf_net_ctx->xskmap_map_flush_list;
939 }
940 
bpf_net_ctx_get_all_used_flush_lists(struct list_head ** lh_map,struct list_head ** lh_dev,struct list_head ** lh_xsk)941 static inline void bpf_net_ctx_get_all_used_flush_lists(struct list_head **lh_map,
942 							struct list_head **lh_dev,
943 							struct list_head **lh_xsk)
944 {
945 	struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get();
946 	u32 kern_flags = bpf_net_ctx->ri.kern_flags;
947 	struct list_head *lh;
948 
949 	*lh_map = *lh_dev = *lh_xsk = NULL;
950 
951 	if (!IS_ENABLED(CONFIG_BPF_SYSCALL))
952 		return;
953 
954 	lh = &bpf_net_ctx->dev_map_flush_list;
955 	if (kern_flags & BPF_RI_F_DEV_MAP_INIT && !list_empty(lh))
956 		*lh_dev = lh;
957 
958 	lh = &bpf_net_ctx->cpu_map_flush_list;
959 	if (kern_flags & BPF_RI_F_CPU_MAP_INIT && !list_empty(lh))
960 		*lh_map = lh;
961 
962 	lh = &bpf_net_ctx->xskmap_map_flush_list;
963 	if (IS_ENABLED(CONFIG_XDP_SOCKETS) &&
964 	    kern_flags & BPF_RI_F_XSK_MAP_INIT && !list_empty(lh))
965 		*lh_xsk = lh;
966 }
967 
968 /* Compute the linear packet data range [data, data_end) which
969  * will be accessed by various program types (cls_bpf, act_bpf,
970  * lwt, ...). Subsystems allowing direct data access must (!)
971  * ensure that cb[] area can be written to when BPF program is
972  * invoked (otherwise cb[] save/restore is necessary).
973  */
bpf_compute_data_pointers(struct sk_buff * skb)974 static inline void bpf_compute_data_pointers(struct sk_buff *skb)
975 {
976 	struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
977 
978 	BUILD_BUG_ON(sizeof(*cb) > sizeof_field(struct sk_buff, cb));
979 	cb->data_meta = skb->data - skb_metadata_len(skb);
980 	cb->data_end  = skb->data + skb_headlen(skb);
981 }
982 
bpf_prog_run_data_pointers(const struct bpf_prog * prog,struct sk_buff * skb)983 static inline int bpf_prog_run_data_pointers(
984 	const struct bpf_prog *prog,
985 	struct sk_buff *skb)
986 {
987 	struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
988 	void *save_data_meta, *save_data_end;
989 	int res;
990 
991 	save_data_meta = cb->data_meta;
992 	save_data_end = cb->data_end;
993 
994 	bpf_compute_data_pointers(skb);
995 	res = bpf_prog_run(prog, skb);
996 
997 	cb->data_meta = save_data_meta;
998 	cb->data_end = save_data_end;
999 
1000 	return res;
1001 }
1002 
1003 /* Similar to bpf_compute_data_pointers(), except that save orginal
1004  * data in cb->data and cb->meta_data for restore.
1005  */
bpf_compute_and_save_data_end(struct sk_buff * skb,void ** saved_data_end)1006 static inline void bpf_compute_and_save_data_end(
1007 	struct sk_buff *skb, void **saved_data_end)
1008 {
1009 	struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
1010 
1011 	*saved_data_end = cb->data_end;
1012 	cb->data_end  = skb->data + skb_headlen(skb);
1013 }
1014 
1015 /* Restore data saved by bpf_compute_and_save_data_end(). */
bpf_restore_data_end(struct sk_buff * skb,void * saved_data_end)1016 static inline void bpf_restore_data_end(
1017 	struct sk_buff *skb, void *saved_data_end)
1018 {
1019 	struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
1020 
1021 	cb->data_end = saved_data_end;
1022 }
1023 
bpf_skb_cb(const struct sk_buff * skb)1024 static inline u8 *bpf_skb_cb(const struct sk_buff *skb)
1025 {
1026 	/* eBPF programs may read/write skb->cb[] area to transfer meta
1027 	 * data between tail calls. Since this also needs to work with
1028 	 * tc, that scratch memory is mapped to qdisc_skb_cb's data area.
1029 	 *
1030 	 * In some socket filter cases, the cb unfortunately needs to be
1031 	 * saved/restored so that protocol specific skb->cb[] data won't
1032 	 * be lost. In any case, due to unpriviledged eBPF programs
1033 	 * attached to sockets, we need to clear the bpf_skb_cb() area
1034 	 * to not leak previous contents to user space.
1035 	 */
1036 	BUILD_BUG_ON(sizeof_field(struct __sk_buff, cb) != BPF_SKB_CB_LEN);
1037 	BUILD_BUG_ON(sizeof_field(struct __sk_buff, cb) !=
1038 		     sizeof_field(struct qdisc_skb_cb, data));
1039 
1040 	return qdisc_skb_cb(skb)->data;
1041 }
1042 
1043 /* Must be invoked with migration disabled */
__bpf_prog_run_save_cb(const struct bpf_prog * prog,const void * ctx)1044 static inline u32 __bpf_prog_run_save_cb(const struct bpf_prog *prog,
1045 					 const void *ctx)
1046 {
1047 	const struct sk_buff *skb = ctx;
1048 	u8 *cb_data = bpf_skb_cb(skb);
1049 	u8 cb_saved[BPF_SKB_CB_LEN];
1050 	u32 res;
1051 
1052 	if (unlikely(prog->cb_access)) {
1053 		memcpy(cb_saved, cb_data, sizeof(cb_saved));
1054 		memset(cb_data, 0, sizeof(cb_saved));
1055 	}
1056 
1057 	res = bpf_prog_run(prog, skb);
1058 
1059 	if (unlikely(prog->cb_access))
1060 		memcpy(cb_data, cb_saved, sizeof(cb_saved));
1061 
1062 	return res;
1063 }
1064 
bpf_prog_run_save_cb(const struct bpf_prog * prog,struct sk_buff * skb)1065 static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
1066 				       struct sk_buff *skb)
1067 {
1068 	u32 res;
1069 
1070 	migrate_disable();
1071 	res = __bpf_prog_run_save_cb(prog, skb);
1072 	migrate_enable();
1073 	return res;
1074 }
1075 
bpf_prog_run_clear_cb(const struct bpf_prog * prog,struct sk_buff * skb)1076 static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog,
1077 					struct sk_buff *skb)
1078 {
1079 	u8 *cb_data = bpf_skb_cb(skb);
1080 	u32 res;
1081 
1082 	if (unlikely(prog->cb_access))
1083 		memset(cb_data, 0, BPF_SKB_CB_LEN);
1084 
1085 	res = bpf_prog_run_pin_on_cpu(prog, skb);
1086 	return res;
1087 }
1088 
1089 DECLARE_BPF_DISPATCHER(xdp)
1090 
1091 DECLARE_STATIC_KEY_FALSE(bpf_master_redirect_enabled_key);
1092 
1093 u32 xdp_master_redirect(struct xdp_buff *xdp);
1094 
1095 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog);
1096 
bpf_prog_insn_size(const struct bpf_prog * prog)1097 static inline u32 bpf_prog_insn_size(const struct bpf_prog *prog)
1098 {
1099 	return prog->len * sizeof(struct bpf_insn);
1100 }
1101 
bpf_prog_size(unsigned int proglen)1102 static inline unsigned int bpf_prog_size(unsigned int proglen)
1103 {
1104 	return max(sizeof(struct bpf_prog),
1105 		   offsetof(struct bpf_prog, insns[proglen]));
1106 }
1107 
bpf_prog_was_classic(const struct bpf_prog * prog)1108 static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
1109 {
1110 	/* When classic BPF programs have been loaded and the arch
1111 	 * does not have a classic BPF JIT (anymore), they have been
1112 	 * converted via bpf_migrate_filter() to eBPF and thus always
1113 	 * have an unspec program type.
1114 	 */
1115 	return prog->type == BPF_PROG_TYPE_UNSPEC;
1116 }
1117 
bpf_ctx_off_adjust_machine(u32 size)1118 static inline u32 bpf_ctx_off_adjust_machine(u32 size)
1119 {
1120 	const u32 size_machine = sizeof(unsigned long);
1121 
1122 	if (size > size_machine && size % size_machine == 0)
1123 		size = size_machine;
1124 
1125 	return size;
1126 }
1127 
1128 static inline bool
bpf_ctx_narrow_access_ok(u32 off,u32 size,u32 size_default)1129 bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
1130 {
1131 	return size <= size_default && (size & (size - 1)) == 0;
1132 }
1133 
1134 static inline u8
bpf_ctx_narrow_access_offset(u32 off,u32 size,u32 size_default)1135 bpf_ctx_narrow_access_offset(u32 off, u32 size, u32 size_default)
1136 {
1137 	u8 access_off = off & (size_default - 1);
1138 
1139 #ifdef __LITTLE_ENDIAN
1140 	return access_off;
1141 #else
1142 	return size_default - (access_off + size);
1143 #endif
1144 }
1145 
1146 #define bpf_ctx_wide_access_ok(off, size, type, field)			\
1147 	(size == sizeof(__u64) &&					\
1148 	off >= offsetof(type, field) &&					\
1149 	off + sizeof(__u64) <= offsetofend(type, field) &&		\
1150 	off % sizeof(__u64) == 0)
1151 
1152 #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
1153 
bpf_prog_lock_ro(struct bpf_prog * fp)1154 static inline int __must_check bpf_prog_lock_ro(struct bpf_prog *fp)
1155 {
1156 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1157 	if (!fp->jited) {
1158 		set_vm_flush_reset_perms(fp);
1159 		return set_memory_ro((unsigned long)fp, fp->pages);
1160 	}
1161 #endif
1162 	return 0;
1163 }
1164 
1165 static inline int __must_check
bpf_jit_binary_lock_ro(struct bpf_binary_header * hdr)1166 bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
1167 {
1168 	set_vm_flush_reset_perms(hdr);
1169 	return set_memory_rox((unsigned long)hdr, hdr->size >> PAGE_SHIFT);
1170 }
1171 
1172 enum skb_drop_reason
1173 sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
1174 
sk_filter(struct sock * sk,struct sk_buff * skb)1175 static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
1176 {
1177 	enum skb_drop_reason drop_reason;
1178 
1179 	drop_reason = sk_filter_trim_cap(sk, skb, 1);
1180 	return drop_reason ? -EPERM : 0;
1181 }
1182 
1183 static inline enum skb_drop_reason
sk_filter_reason(struct sock * sk,struct sk_buff * skb)1184 sk_filter_reason(struct sock *sk, struct sk_buff *skb)
1185 {
1186 	return sk_filter_trim_cap(sk, skb, 1);
1187 }
1188 
1189 struct bpf_prog *__bpf_prog_select_runtime(struct bpf_verifier_env *env, struct bpf_prog *fp,
1190 					   int *err);
1191 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err);
1192 void bpf_prog_free(struct bpf_prog *fp);
1193 
1194 bool bpf_opcode_in_insntable(u8 code);
1195 
1196 void bpf_prog_fill_jited_linfo(struct bpf_prog *prog,
1197 			       const u32 *insn_to_jit_off);
1198 int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog);
1199 void bpf_prog_jit_attempt_done(struct bpf_prog *prog);
1200 
1201 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
1202 struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags);
1203 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
1204 				  gfp_t gfp_extra_flags);
1205 void __bpf_prog_free(struct bpf_prog *fp);
1206 
bpf_prog_unlock_free(struct bpf_prog * fp)1207 static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
1208 {
1209 	__bpf_prog_free(fp);
1210 }
1211 
1212 typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
1213 				       unsigned int flen);
1214 
1215 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
1216 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1217 			      bpf_aux_classic_check_t trans, bool save_orig);
1218 void bpf_prog_destroy(struct bpf_prog *fp);
1219 
1220 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
1221 int sk_attach_bpf(u32 ufd, struct sock *sk);
1222 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk);
1223 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk);
1224 void sk_reuseport_prog_free(struct bpf_prog *prog);
1225 int sk_detach_filter(struct sock *sk);
1226 int sk_get_filter(struct sock *sk, sockptr_t optval, unsigned int len);
1227 
1228 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
1229 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
1230 
1231 u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1232 
1233 struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog);
1234 void bpf_jit_compile(struct bpf_prog *prog);
1235 bool bpf_jit_needs_zext(void);
1236 bool bpf_jit_inlines_helper_call(s32 imm);
1237 bool bpf_jit_supports_subprog_tailcalls(void);
1238 bool bpf_jit_supports_percpu_insn(void);
1239 bool bpf_jit_supports_kfunc_call(void);
1240 bool bpf_jit_supports_stack_args(void);
1241 bool bpf_jit_supports_arena_args(void);
1242 bool bpf_jit_supports_far_kfunc_call(void);
1243 bool bpf_jit_supports_exceptions(void);
1244 bool bpf_jit_supports_ptr_xchg(void);
1245 bool bpf_jit_supports_arena(void);
1246 bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);
1247 bool bpf_jit_supports_private_stack(void);
1248 bool bpf_jit_supports_timed_may_goto(void);
1249 bool bpf_jit_supports_fsession(void);
1250 u64 bpf_arch_uaddress_limit(void);
1251 void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie);
1252 u64 arch_bpf_timed_may_goto(void);
1253 u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *);
1254 bool bpf_helper_changes_pkt_data(enum bpf_func_id func_id);
1255 
bpf_dump_raw_ok(const struct cred * cred)1256 static inline bool bpf_dump_raw_ok(const struct cred *cred)
1257 {
1258 	/* Reconstruction of call-sites is dependent on kallsyms,
1259 	 * thus make dump the same restriction.
1260 	 */
1261 	return kallsyms_show_value(cred);
1262 }
1263 
1264 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
1265 				       const struct bpf_insn *patch, u32 len);
1266 
1267 #ifdef CONFIG_BPF_SYSCALL
1268 struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
1269 				     const struct bpf_insn *patch, u32 len);
1270 #else
bpf_patch_insn_data(struct bpf_verifier_env * env,u32 off,const struct bpf_insn * patch,u32 len)1271 static inline struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
1272 						   const struct bpf_insn *patch, u32 len)
1273 {
1274 	return ERR_PTR(-ENOTSUPP);
1275 }
1276 #endif /* CONFIG_BPF_SYSCALL */
1277 
1278 int bpf_remove_insns(struct bpf_prog *prog, u32 off, u32 cnt);
1279 
xdp_return_frame_no_direct(void)1280 static inline bool xdp_return_frame_no_direct(void)
1281 {
1282 	struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
1283 
1284 	return ri->kern_flags & BPF_RI_F_RF_NO_DIRECT;
1285 }
1286 
xdp_set_return_frame_no_direct(void)1287 static inline void xdp_set_return_frame_no_direct(void)
1288 {
1289 	struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
1290 
1291 	ri->kern_flags |= BPF_RI_F_RF_NO_DIRECT;
1292 }
1293 
xdp_clear_return_frame_no_direct(void)1294 static inline void xdp_clear_return_frame_no_direct(void)
1295 {
1296 	struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
1297 
1298 	ri->kern_flags &= ~BPF_RI_F_RF_NO_DIRECT;
1299 }
1300 
xdp_ok_fwd_dev(const struct net_device * fwd,unsigned int pktlen)1301 static inline int xdp_ok_fwd_dev(const struct net_device *fwd,
1302 				 unsigned int pktlen)
1303 {
1304 	unsigned int len;
1305 
1306 	if (unlikely(!(fwd->flags & IFF_UP)))
1307 		return -ENETDOWN;
1308 
1309 	len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
1310 	if (pktlen > len)
1311 		return -EMSGSIZE;
1312 
1313 	return 0;
1314 }
1315 
1316 /* The pair of xdp_do_redirect and xdp_do_flush MUST be called in the
1317  * same cpu context. Further for best results no more than a single map
1318  * for the do_redirect/do_flush pair should be used. This limitation is
1319  * because we only track one map and force a flush when the map changes.
1320  * This does not appear to be a real limitation for existing software.
1321  */
1322 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
1323 			    struct xdp_buff *xdp, const struct bpf_prog *prog);
1324 int xdp_do_redirect(struct net_device *dev,
1325 		    struct xdp_buff *xdp,
1326 		    const struct bpf_prog *prog);
1327 int xdp_do_redirect_frame(struct net_device *dev,
1328 			  struct xdp_buff *xdp,
1329 			  struct xdp_frame *xdpf,
1330 			  const struct bpf_prog *prog);
1331 void xdp_do_flush(void);
1332 
1333 void bpf_warn_invalid_xdp_action(const struct net_device *dev,
1334 				 const struct bpf_prog *prog, u32 act);
1335 
1336 #ifdef CONFIG_INET
1337 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
1338 				  struct bpf_prog *prog, struct sk_buff *skb,
1339 				  struct sock *migrating_sk,
1340 				  u32 hash);
1341 #else
1342 static inline struct sock *
bpf_run_sk_reuseport(struct sock_reuseport * reuse,struct sock * sk,struct bpf_prog * prog,struct sk_buff * skb,struct sock * migrating_sk,u32 hash)1343 bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
1344 		     struct bpf_prog *prog, struct sk_buff *skb,
1345 		     struct sock *migrating_sk,
1346 		     u32 hash)
1347 {
1348 	return NULL;
1349 }
1350 #endif
1351 
1352 #ifdef CONFIG_BPF_JIT
1353 extern int bpf_jit_enable;
1354 extern int bpf_jit_harden;
1355 extern int bpf_jit_kallsyms;
1356 extern long bpf_jit_limit;
1357 extern long bpf_jit_limit_max;
1358 
1359 typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
1360 
1361 /*
1362  * Flush the indirect branch predictors before reusing JIT memory, so that
1363  * indirect jumps into a newly written program don't reuse predictions left
1364  * behind by an old program that occupied the same space.
1365  */
1366 void bpf_arch_pred_flush(void);
1367 DECLARE_STATIC_CALL(bpf_arch_pred_flush, bpf_arch_pred_flush);
1368 DECLARE_STATIC_KEY_FALSE(bpf_pred_flush_enabled);
1369 
1370 void bpf_jit_fill_hole_with_zero(void *area, unsigned int size);
1371 
1372 struct bpf_binary_header *
1373 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
1374 		     unsigned int alignment,
1375 		     bpf_jit_fill_hole_t bpf_fill_ill_insns);
1376 void bpf_jit_binary_free(struct bpf_binary_header *hdr);
1377 u64 bpf_jit_alloc_exec_limit(void);
1378 void *bpf_jit_alloc_exec(unsigned long size);
1379 void *bpf_jit_alloc_exec_rw(unsigned long size);
1380 void bpf_jit_free_exec(void *addr);
1381 void bpf_jit_free(struct bpf_prog *fp);
1382 struct bpf_binary_header *
1383 bpf_jit_binary_pack_hdr(const struct bpf_prog *fp);
1384 
1385 void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool was_classic);
1386 void bpf_prog_pack_free(void *ptr, u32 size);
1387 
bpf_prog_kallsyms_verify_off(const struct bpf_prog * fp)1388 static inline bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
1389 {
1390 	return list_empty(&fp->aux->ksym.lnode) ||
1391 	       fp->aux->ksym.lnode.prev == LIST_POISON2;
1392 }
1393 
1394 struct bpf_binary_header *
1395 bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **ro_image,
1396 			  unsigned int alignment,
1397 			  struct bpf_binary_header **rw_hdr,
1398 			  u8 **rw_image,
1399 			  bpf_jit_fill_hole_t bpf_fill_ill_insns,
1400 			  bool was_classic);
1401 int bpf_jit_binary_pack_finalize(struct bpf_binary_header *ro_header,
1402 				 struct bpf_binary_header *rw_header);
1403 void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header,
1404 			      struct bpf_binary_header *rw_header);
1405 
1406 int bpf_jit_add_poke_descriptor(struct bpf_prog *prog,
1407 				struct bpf_jit_poke_descriptor *poke);
1408 
1409 int bpf_jit_get_func_addr(const struct bpf_prog *prog,
1410 			  const struct bpf_insn *insn, bool extra_pass,
1411 			  u64 *func_addr, bool *func_addr_fixed);
1412 
1413 const char *bpf_jit_get_prog_name(struct bpf_prog *prog);
1414 
1415 struct bpf_prog *bpf_jit_blind_constants(struct bpf_verifier_env *env, struct bpf_prog *prog);
1416 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);
1417 
bpf_prog_need_blind(const struct bpf_prog * prog)1418 static inline bool bpf_prog_need_blind(const struct bpf_prog *prog)
1419 {
1420 	return prog->blinding_requested && !prog->blinded;
1421 }
1422 
bpf_jit_dump(unsigned int flen,unsigned int proglen,u32 pass,void * image)1423 static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
1424 				u32 pass, void *image)
1425 {
1426 	pr_err("flen=%u proglen=%u pass=%u image=%p from=%s pid=%d\n", flen,
1427 	       proglen, pass, image, current->comm, task_pid_nr(current));
1428 
1429 	if (image)
1430 		print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
1431 			       16, 1, image, proglen, false);
1432 }
1433 
bpf_jit_is_ebpf(void)1434 static inline bool bpf_jit_is_ebpf(void)
1435 {
1436 # ifdef CONFIG_HAVE_EBPF_JIT
1437 	return true;
1438 # else
1439 	return false;
1440 # endif
1441 }
1442 
ebpf_jit_enabled(void)1443 static inline bool ebpf_jit_enabled(void)
1444 {
1445 	return bpf_jit_enable && bpf_jit_is_ebpf();
1446 }
1447 
bpf_prog_ebpf_jited(const struct bpf_prog * fp)1448 static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
1449 {
1450 	return fp->jited && bpf_jit_is_ebpf();
1451 }
1452 
bpf_jit_blinding_enabled(struct bpf_prog * prog)1453 static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
1454 {
1455 	/* These are the prerequisites, should someone ever have the
1456 	 * idea to call blinding outside of them, we make sure to
1457 	 * bail out.
1458 	 */
1459 	if (!bpf_jit_is_ebpf())
1460 		return false;
1461 	if (!prog->jit_requested)
1462 		return false;
1463 	if (!bpf_jit_harden)
1464 		return false;
1465 	if (bpf_jit_harden == 1 && bpf_token_capable(prog->aux->token, CAP_BPF))
1466 		return false;
1467 
1468 	return true;
1469 }
1470 
bpf_jit_kallsyms_enabled(void)1471 static inline bool bpf_jit_kallsyms_enabled(void)
1472 {
1473 	/* There are a couple of corner cases where kallsyms should
1474 	 * not be enabled f.e. on hardening.
1475 	 */
1476 	if (bpf_jit_harden)
1477 		return false;
1478 	if (!bpf_jit_kallsyms)
1479 		return false;
1480 	if (bpf_jit_kallsyms == 1)
1481 		return true;
1482 
1483 	return false;
1484 }
1485 
1486 int bpf_address_lookup(unsigned long addr, unsigned long *size,
1487 		       unsigned long *off, char *sym);
1488 bool is_bpf_text_address(unsigned long addr);
1489 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
1490 		    char *sym);
1491 struct bpf_prog *bpf_prog_ksym_find(unsigned long addr);
1492 
1493 void bpf_prog_kallsyms_add(struct bpf_prog *fp);
1494 void bpf_prog_kallsyms_del(struct bpf_prog *fp);
1495 
1496 #else /* CONFIG_BPF_JIT */
1497 
ebpf_jit_enabled(void)1498 static inline bool ebpf_jit_enabled(void)
1499 {
1500 	return false;
1501 }
1502 
bpf_jit_blinding_enabled(struct bpf_prog * prog)1503 static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
1504 {
1505 	return false;
1506 }
1507 
bpf_prog_ebpf_jited(const struct bpf_prog * fp)1508 static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
1509 {
1510 	return false;
1511 }
1512 
1513 static inline int
bpf_jit_add_poke_descriptor(struct bpf_prog * prog,struct bpf_jit_poke_descriptor * poke)1514 bpf_jit_add_poke_descriptor(struct bpf_prog *prog,
1515 			    struct bpf_jit_poke_descriptor *poke)
1516 {
1517 	return -ENOTSUPP;
1518 }
1519 
bpf_jit_free(struct bpf_prog * fp)1520 static inline void bpf_jit_free(struct bpf_prog *fp)
1521 {
1522 	bpf_prog_unlock_free(fp);
1523 }
1524 
bpf_jit_kallsyms_enabled(void)1525 static inline bool bpf_jit_kallsyms_enabled(void)
1526 {
1527 	return false;
1528 }
1529 
1530 static inline int
bpf_address_lookup(unsigned long addr,unsigned long * size,unsigned long * off,char * sym)1531 bpf_address_lookup(unsigned long addr, unsigned long *size,
1532 		   unsigned long *off, char *sym)
1533 {
1534 	return 0;
1535 }
1536 
is_bpf_text_address(unsigned long addr)1537 static inline bool is_bpf_text_address(unsigned long addr)
1538 {
1539 	return false;
1540 }
1541 
bpf_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * sym)1542 static inline int bpf_get_kallsym(unsigned int symnum, unsigned long *value,
1543 				  char *type, char *sym)
1544 {
1545 	return -ERANGE;
1546 }
1547 
bpf_prog_ksym_find(unsigned long addr)1548 static inline struct bpf_prog *bpf_prog_ksym_find(unsigned long addr)
1549 {
1550 	return NULL;
1551 }
1552 
bpf_prog_kallsyms_add(struct bpf_prog * fp)1553 static inline void bpf_prog_kallsyms_add(struct bpf_prog *fp)
1554 {
1555 }
1556 
bpf_prog_kallsyms_del(struct bpf_prog * fp)1557 static inline void bpf_prog_kallsyms_del(struct bpf_prog *fp)
1558 {
1559 }
1560 
bpf_prog_need_blind(const struct bpf_prog * prog)1561 static inline bool bpf_prog_need_blind(const struct bpf_prog *prog)
1562 {
1563 	return false;
1564 }
1565 
1566 static inline
bpf_jit_blind_constants(struct bpf_verifier_env * env,struct bpf_prog * prog)1567 struct bpf_prog *bpf_jit_blind_constants(struct bpf_verifier_env *env, struct bpf_prog *prog)
1568 {
1569 	return prog;
1570 }
1571 
bpf_jit_prog_release_other(struct bpf_prog * fp,struct bpf_prog * fp_other)1572 static inline void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
1573 {
1574 }
1575 #endif /* CONFIG_BPF_JIT */
1576 
1577 void bpf_prog_kallsyms_del_all(struct bpf_prog *fp);
1578 
1579 #define BPF_ANC		BIT(15)
1580 
bpf_needs_clear_a(const struct sock_filter * first)1581 static inline bool bpf_needs_clear_a(const struct sock_filter *first)
1582 {
1583 	switch (first->code) {
1584 	case BPF_RET | BPF_K:
1585 	case BPF_LD | BPF_W | BPF_LEN:
1586 		return false;
1587 
1588 	case BPF_LD | BPF_W | BPF_ABS:
1589 	case BPF_LD | BPF_H | BPF_ABS:
1590 	case BPF_LD | BPF_B | BPF_ABS:
1591 		if (first->k == SKF_AD_OFF + SKF_AD_ALU_XOR_X)
1592 			return true;
1593 		return false;
1594 
1595 	default:
1596 		return true;
1597 	}
1598 }
1599 
bpf_anc_helper(const struct sock_filter * ftest)1600 static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
1601 {
1602 	BUG_ON(ftest->code & BPF_ANC);
1603 
1604 	switch (ftest->code) {
1605 	case BPF_LD | BPF_W | BPF_ABS:
1606 	case BPF_LD | BPF_H | BPF_ABS:
1607 	case BPF_LD | BPF_B | BPF_ABS:
1608 #define BPF_ANCILLARY(CODE)	case SKF_AD_OFF + SKF_AD_##CODE:	\
1609 				return BPF_ANC | SKF_AD_##CODE
1610 		switch (ftest->k) {
1611 		BPF_ANCILLARY(PROTOCOL);
1612 		BPF_ANCILLARY(PKTTYPE);
1613 		BPF_ANCILLARY(IFINDEX);
1614 		BPF_ANCILLARY(NLATTR);
1615 		BPF_ANCILLARY(NLATTR_NEST);
1616 		BPF_ANCILLARY(MARK);
1617 		BPF_ANCILLARY(QUEUE);
1618 		BPF_ANCILLARY(HATYPE);
1619 		BPF_ANCILLARY(RXHASH);
1620 		BPF_ANCILLARY(CPU);
1621 		BPF_ANCILLARY(ALU_XOR_X);
1622 		BPF_ANCILLARY(VLAN_TAG);
1623 		BPF_ANCILLARY(VLAN_TAG_PRESENT);
1624 		BPF_ANCILLARY(PAY_OFFSET);
1625 		BPF_ANCILLARY(RANDOM);
1626 		BPF_ANCILLARY(VLAN_TPID);
1627 		}
1628 		fallthrough;
1629 	default:
1630 		return ftest->code;
1631 	}
1632 }
1633 
1634 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
1635 					   int k, unsigned int size);
1636 
bpf_tell_extensions(void)1637 static inline int bpf_tell_extensions(void)
1638 {
1639 	return SKF_AD_MAX;
1640 }
1641 
1642 struct bpf_sock_addr_kern {
1643 	struct sock *sk;
1644 	struct sockaddr_unsized *uaddr;
1645 	/* Temporary "register" to make indirect stores to nested structures
1646 	 * defined above. We need three registers to make such a store, but
1647 	 * only two (src and dst) are available at convert_ctx_access time
1648 	 */
1649 	u64 tmp_reg;
1650 	void *t_ctx;	/* Attach type specific context. */
1651 	u32 uaddrlen;
1652 };
1653 
1654 struct bpf_sock_ops_kern {
1655 	struct	sock *sk;
1656 	union {
1657 		u32 args[4];
1658 		u32 reply;
1659 		u32 replylong[4];
1660 	};
1661 	struct sk_buff	*syn_skb;
1662 	struct sk_buff	*skb;
1663 	void	*skb_data_end;
1664 	u8	op;
1665 	u8	is_fullsock;
1666 	u8	is_locked_tcp_sock;
1667 	u8	remaining_opt_len;
1668 	u64	temp;			/* temp and everything after is not
1669 					 * initialized to 0 before calling
1670 					 * the BPF program. New fields that
1671 					 * should be initialized to 0 should
1672 					 * be inserted before temp.
1673 					 * temp is scratch storage used by
1674 					 * sock_ops_convert_ctx_access
1675 					 * as temporary storage of a register.
1676 					 */
1677 };
1678 
1679 struct bpf_sysctl_kern {
1680 	struct ctl_table_header *head;
1681 	const struct ctl_table *table;
1682 	void *cur_val;
1683 	size_t cur_len;
1684 	void *new_val;
1685 	size_t new_len;
1686 	int new_updated;
1687 	int write;
1688 	loff_t *ppos;
1689 	/* Temporary "register" for indirect stores to ppos. */
1690 	u64 tmp_reg;
1691 };
1692 
1693 #define BPF_SOCKOPT_KERN_BUF_SIZE	32
1694 struct bpf_sockopt_buf {
1695 	u8		data[BPF_SOCKOPT_KERN_BUF_SIZE];
1696 };
1697 
1698 struct bpf_sockopt_kern {
1699 	struct sock	*sk;
1700 	u8		*optval;
1701 	u8		*optval_end;
1702 	s32		level;
1703 	s32		optname;
1704 	s32		optlen;
1705 	/* for retval in struct bpf_cg_run_ctx */
1706 	struct task_struct *current_task;
1707 	/* Temporary "register" for indirect stores to ppos. */
1708 	u64		tmp_reg;
1709 };
1710 
1711 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len);
1712 
1713 struct bpf_sk_lookup_kern {
1714 	u16		family;
1715 	u16		protocol;
1716 	__be16		sport;
1717 	u16		dport;
1718 	struct {
1719 		__be32 saddr;
1720 		__be32 daddr;
1721 	} v4;
1722 	struct {
1723 		const struct in6_addr *saddr;
1724 		const struct in6_addr *daddr;
1725 	} v6;
1726 	struct sock	*selected_sk;
1727 	u32		ingress_ifindex;
1728 	bool		no_reuseport;
1729 };
1730 
1731 extern struct static_key_false bpf_sk_lookup_enabled;
1732 
1733 /* Runners for BPF_SK_LOOKUP programs to invoke on socket lookup.
1734  *
1735  * Allowed return values for a BPF SK_LOOKUP program are SK_PASS and
1736  * SK_DROP. Their meaning is as follows:
1737  *
1738  *  SK_PASS && ctx.selected_sk != NULL: use selected_sk as lookup result
1739  *  SK_PASS && ctx.selected_sk == NULL: continue to htable-based socket lookup
1740  *  SK_DROP                           : terminate lookup with -ECONNREFUSED
1741  *
1742  * This macro aggregates return values and selected sockets from
1743  * multiple BPF programs according to following rules in order:
1744  *
1745  *  1. If any program returned SK_PASS and a non-NULL ctx.selected_sk,
1746  *     macro result is SK_PASS and last ctx.selected_sk is used.
1747  *  2. If any program returned SK_DROP return value,
1748  *     macro result is SK_DROP.
1749  *  3. Otherwise result is SK_PASS and ctx.selected_sk is NULL.
1750  *
1751  * Caller must ensure that the prog array is non-NULL, and that the
1752  * array as well as the programs it contains remain valid.
1753  */
1754 #define BPF_PROG_SK_LOOKUP_RUN_ARRAY(array, ctx, func)			\
1755 	({								\
1756 		struct bpf_sk_lookup_kern *_ctx = &(ctx);		\
1757 		struct bpf_prog_array_item *_item;			\
1758 		struct sock *_selected_sk = NULL;			\
1759 		bool _no_reuseport = false;				\
1760 		struct bpf_prog *_prog;					\
1761 		bool _all_pass = true;					\
1762 		u32 _ret;						\
1763 									\
1764 		migrate_disable();					\
1765 		_item = &(array)->items[0];				\
1766 		while ((_prog = READ_ONCE(_item->prog))) {		\
1767 			/* restore most recent selection */		\
1768 			_ctx->selected_sk = _selected_sk;		\
1769 			_ctx->no_reuseport = _no_reuseport;		\
1770 									\
1771 			_ret = func(_prog, _ctx);			\
1772 			if (_ret == SK_PASS && _ctx->selected_sk) {	\
1773 				/* remember last non-NULL socket */	\
1774 				_selected_sk = _ctx->selected_sk;	\
1775 				_no_reuseport = _ctx->no_reuseport;	\
1776 			} else if (_ret == SK_DROP && _all_pass) {	\
1777 				_all_pass = false;			\
1778 			}						\
1779 			_item++;					\
1780 		}							\
1781 		_ctx->selected_sk = _selected_sk;			\
1782 		_ctx->no_reuseport = _no_reuseport;			\
1783 		migrate_enable();					\
1784 		_all_pass || _selected_sk ? SK_PASS : SK_DROP;		\
1785 	 })
1786 
bpf_sk_lookup_run_v4(const struct net * net,int protocol,const __be32 saddr,const __be16 sport,const __be32 daddr,const u16 dport,const int ifindex,struct sock ** psk)1787 static inline bool bpf_sk_lookup_run_v4(const struct net *net, int protocol,
1788 					const __be32 saddr, const __be16 sport,
1789 					const __be32 daddr, const u16 dport,
1790 					const int ifindex, struct sock **psk)
1791 {
1792 	struct bpf_prog_array *run_array;
1793 	struct sock *selected_sk = NULL;
1794 	bool no_reuseport = false;
1795 
1796 	rcu_read_lock();
1797 	run_array = rcu_dereference(net->bpf.run_array[NETNS_BPF_SK_LOOKUP]);
1798 	if (run_array) {
1799 		struct bpf_sk_lookup_kern ctx = {
1800 			.family		= AF_INET,
1801 			.protocol	= protocol,
1802 			.v4.saddr	= saddr,
1803 			.v4.daddr	= daddr,
1804 			.sport		= sport,
1805 			.dport		= dport,
1806 			.ingress_ifindex	= ifindex,
1807 		};
1808 		u32 act;
1809 
1810 		act = BPF_PROG_SK_LOOKUP_RUN_ARRAY(run_array, ctx, bpf_prog_run);
1811 		if (act == SK_PASS) {
1812 			selected_sk = ctx.selected_sk;
1813 			no_reuseport = ctx.no_reuseport;
1814 		} else {
1815 			selected_sk = ERR_PTR(-ECONNREFUSED);
1816 		}
1817 	}
1818 	rcu_read_unlock();
1819 	*psk = selected_sk;
1820 	return no_reuseport;
1821 }
1822 
1823 #if IS_ENABLED(CONFIG_IPV6)
bpf_sk_lookup_run_v6(const struct net * net,int protocol,const struct in6_addr * saddr,const __be16 sport,const struct in6_addr * daddr,const u16 dport,const int ifindex,struct sock ** psk)1824 static inline bool bpf_sk_lookup_run_v6(const struct net *net, int protocol,
1825 					const struct in6_addr *saddr,
1826 					const __be16 sport,
1827 					const struct in6_addr *daddr,
1828 					const u16 dport,
1829 					const int ifindex, struct sock **psk)
1830 {
1831 	struct bpf_prog_array *run_array;
1832 	struct sock *selected_sk = NULL;
1833 	bool no_reuseport = false;
1834 
1835 	rcu_read_lock();
1836 	run_array = rcu_dereference(net->bpf.run_array[NETNS_BPF_SK_LOOKUP]);
1837 	if (run_array) {
1838 		struct bpf_sk_lookup_kern ctx = {
1839 			.family		= AF_INET6,
1840 			.protocol	= protocol,
1841 			.v6.saddr	= saddr,
1842 			.v6.daddr	= daddr,
1843 			.sport		= sport,
1844 			.dport		= dport,
1845 			.ingress_ifindex	= ifindex,
1846 		};
1847 		u32 act;
1848 
1849 		act = BPF_PROG_SK_LOOKUP_RUN_ARRAY(run_array, ctx, bpf_prog_run);
1850 		if (act == SK_PASS) {
1851 			selected_sk = ctx.selected_sk;
1852 			no_reuseport = ctx.no_reuseport;
1853 		} else {
1854 			selected_sk = ERR_PTR(-ECONNREFUSED);
1855 		}
1856 	}
1857 	rcu_read_unlock();
1858 	*psk = selected_sk;
1859 	return no_reuseport;
1860 }
1861 #endif /* IS_ENABLED(CONFIG_IPV6) */
1862 
__bpf_xdp_redirect_map(struct bpf_map * map,u64 index,u64 flags,const u64 flag_mask,void * lookup_elem (struct bpf_map * map,u32 key))1863 static __always_inline long __bpf_xdp_redirect_map(struct bpf_map *map, u64 index,
1864 						   u64 flags, const u64 flag_mask,
1865 						   void *lookup_elem(struct bpf_map *map, u32 key))
1866 {
1867 	struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
1868 	const u64 action_mask = XDP_ABORTED | XDP_DROP | XDP_PASS | XDP_TX;
1869 
1870 	/* Lower bits of the flags are used as return code on lookup failure */
1871 	if (unlikely(flags & ~(action_mask | flag_mask)))
1872 		return XDP_ABORTED;
1873 
1874 	ri->tgt_value = lookup_elem(map, index);
1875 	if (unlikely(!ri->tgt_value) && !(flags & BPF_F_BROADCAST)) {
1876 		/* If the lookup fails we want to clear out the state in the
1877 		 * redirect_info struct completely, so that if an eBPF program
1878 		 * performs multiple lookups, the last one always takes
1879 		 * precedence.
1880 		 */
1881 		ri->map_id = INT_MAX; /* Valid map id idr range: [1,INT_MAX[ */
1882 		ri->map_type = BPF_MAP_TYPE_UNSPEC;
1883 		return flags & action_mask;
1884 	}
1885 
1886 	ri->tgt_index = index;
1887 	ri->map_id = map->id;
1888 	ri->map_type = map->map_type;
1889 
1890 	if (flags & BPF_F_BROADCAST) {
1891 		WRITE_ONCE(ri->map, map);
1892 		ri->flags = flags;
1893 	} else {
1894 		WRITE_ONCE(ri->map, NULL);
1895 		ri->flags = 0;
1896 	}
1897 
1898 	return XDP_REDIRECT;
1899 }
1900 
1901 #ifdef CONFIG_NET
1902 int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len);
1903 int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
1904 			  u32 len, u64 flags);
1905 int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
1906 int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
1907 void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len);
1908 void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
1909 		      void *buf, unsigned long len, bool flush);
1910 int __bpf_skb_meta_store_bytes(struct sk_buff *skb, u32 offset,
1911 			       const void *from, u32 len, u64 flags);
1912 void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset);
1913 #else /* CONFIG_NET */
__bpf_skb_load_bytes(const struct sk_buff * skb,u32 offset,void * to,u32 len)1914 static inline int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset,
1915 				       void *to, u32 len)
1916 {
1917 	return -EOPNOTSUPP;
1918 }
1919 
__bpf_skb_store_bytes(struct sk_buff * skb,u32 offset,const void * from,u32 len,u64 flags)1920 static inline int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset,
1921 					const void *from, u32 len, u64 flags)
1922 {
1923 	return -EOPNOTSUPP;
1924 }
1925 
__bpf_xdp_load_bytes(struct xdp_buff * xdp,u32 offset,void * buf,u32 len)1926 static inline int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset,
1927 				       void *buf, u32 len)
1928 {
1929 	return -EOPNOTSUPP;
1930 }
1931 
__bpf_xdp_store_bytes(struct xdp_buff * xdp,u32 offset,void * buf,u32 len)1932 static inline int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset,
1933 					void *buf, u32 len)
1934 {
1935 	return -EOPNOTSUPP;
1936 }
1937 
bpf_xdp_pointer(struct xdp_buff * xdp,u32 offset,u32 len)1938 static inline void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
1939 {
1940 	return NULL;
1941 }
1942 
bpf_xdp_copy_buf(struct xdp_buff * xdp,unsigned long off,void * buf,unsigned long len,bool flush)1943 static inline void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off, void *buf,
1944 				    unsigned long len, bool flush)
1945 {
1946 }
1947 
__bpf_skb_meta_store_bytes(struct sk_buff * skb,u32 offset,const void * from,u32 len,u64 flags)1948 static inline int __bpf_skb_meta_store_bytes(struct sk_buff *skb, u32 offset,
1949 					     const void *from, u32 len,
1950 					     u64 flags)
1951 {
1952 	return -EOPNOTSUPP;
1953 }
1954 
bpf_skb_meta_pointer(struct sk_buff * skb,u32 offset)1955 static inline void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset)
1956 {
1957 	return ERR_PTR(-EOPNOTSUPP);
1958 }
1959 #endif /* CONFIG_NET */
1960 
1961 #endif /* __LINUX_FILTER_H__ */
1962