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