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