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