GNU Linux-libre 4.9.317-gnu1
[releases.git] / kernel / bpf / core.c
1 /*
2  * Linux Socket Filter - Kernel level socket filtering
3  *
4  * Based on the design of the Berkeley Packet Filter. The new
5  * internal format has been designed by PLUMgrid:
6  *
7  *      Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
8  *
9  * Authors:
10  *
11  *      Jay Schulist <jschlst@samba.org>
12  *      Alexei Starovoitov <ast@plumgrid.com>
13  *      Daniel Borkmann <dborkman@redhat.com>
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version
18  * 2 of the License, or (at your option) any later version.
19  *
20  * Andi Kleen - Fix a few bad bugs and races.
21  * Kris Katterjohn - Added many additional checks in bpf_check_classic()
22  */
23
24 #include <linux/filter.h>
25 #include <linux/skbuff.h>
26 #include <linux/vmalloc.h>
27 #include <linux/random.h>
28 #include <linux/moduleloader.h>
29 #include <linux/bpf.h>
30 #include <linux/frame.h>
31
32 #include <asm/unaligned.h>
33
34 /* Registers */
35 #define BPF_R0  regs[BPF_REG_0]
36 #define BPF_R1  regs[BPF_REG_1]
37 #define BPF_R2  regs[BPF_REG_2]
38 #define BPF_R3  regs[BPF_REG_3]
39 #define BPF_R4  regs[BPF_REG_4]
40 #define BPF_R5  regs[BPF_REG_5]
41 #define BPF_R6  regs[BPF_REG_6]
42 #define BPF_R7  regs[BPF_REG_7]
43 #define BPF_R8  regs[BPF_REG_8]
44 #define BPF_R9  regs[BPF_REG_9]
45 #define BPF_R10 regs[BPF_REG_10]
46
47 /* Named registers */
48 #define DST     regs[insn->dst_reg]
49 #define SRC     regs[insn->src_reg]
50 #define FP      regs[BPF_REG_FP]
51 #define ARG1    regs[BPF_REG_ARG1]
52 #define CTX     regs[BPF_REG_CTX]
53 #define IMM     insn->imm
54
55 /* No hurry in this branch
56  *
57  * Exported for the bpf jit load helper.
58  */
59 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
60 {
61         u8 *ptr = NULL;
62
63         if (k >= SKF_NET_OFF)
64                 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
65         else if (k >= SKF_LL_OFF)
66                 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
67
68         if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
69                 return ptr;
70
71         return NULL;
72 }
73
74 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
75 {
76         gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
77                           gfp_extra_flags;
78         struct bpf_prog_aux *aux;
79         struct bpf_prog *fp;
80
81         size = round_up(size, PAGE_SIZE);
82         fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
83         if (fp == NULL)
84                 return NULL;
85
86         kmemcheck_annotate_bitfield(fp, meta);
87
88         aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
89         if (aux == NULL) {
90                 vfree(fp);
91                 return NULL;
92         }
93
94         fp->pages = size / PAGE_SIZE;
95         fp->aux = aux;
96         fp->aux->prog = fp;
97
98         return fp;
99 }
100 EXPORT_SYMBOL_GPL(bpf_prog_alloc);
101
102 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
103                                   gfp_t gfp_extra_flags)
104 {
105         gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
106                           gfp_extra_flags;
107         struct bpf_prog *fp;
108
109         BUG_ON(fp_old == NULL);
110
111         size = round_up(size, PAGE_SIZE);
112         if (size <= fp_old->pages * PAGE_SIZE)
113                 return fp_old;
114
115         fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
116         if (fp != NULL) {
117                 kmemcheck_annotate_bitfield(fp, meta);
118
119                 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
120                 fp->pages = size / PAGE_SIZE;
121                 fp->aux->prog = fp;
122
123                 /* We keep fp->aux from fp_old around in the new
124                  * reallocated structure.
125                  */
126                 fp_old->aux = NULL;
127                 __bpf_prog_free(fp_old);
128         }
129
130         return fp;
131 }
132
133 void __bpf_prog_free(struct bpf_prog *fp)
134 {
135         kfree(fp->aux);
136         vfree(fp);
137 }
138
139 static bool bpf_is_jmp_and_has_target(const struct bpf_insn *insn)
140 {
141         return BPF_CLASS(insn->code) == BPF_JMP  &&
142                /* Call and Exit are both special jumps with no
143                 * target inside the BPF instruction image.
144                 */
145                BPF_OP(insn->code) != BPF_CALL &&
146                BPF_OP(insn->code) != BPF_EXIT;
147 }
148
149 static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
150 {
151         struct bpf_insn *insn = prog->insnsi;
152         u32 i, insn_cnt = prog->len;
153
154         for (i = 0; i < insn_cnt; i++, insn++) {
155                 if (!bpf_is_jmp_and_has_target(insn))
156                         continue;
157
158                 /* Adjust offset of jmps if we cross boundaries. */
159                 if (i < pos && i + insn->off + 1 > pos)
160                         insn->off += delta;
161                 else if (i > pos + delta && i + insn->off + 1 <= pos + delta)
162                         insn->off -= delta;
163         }
164 }
165
166 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
167                                        const struct bpf_insn *patch, u32 len)
168 {
169         u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
170         struct bpf_prog *prog_adj;
171
172         /* Since our patchlet doesn't expand the image, we're done. */
173         if (insn_delta == 0) {
174                 memcpy(prog->insnsi + off, patch, sizeof(*patch));
175                 return prog;
176         }
177
178         insn_adj_cnt = prog->len + insn_delta;
179
180         /* Several new instructions need to be inserted. Make room
181          * for them. Likely, there's no need for a new allocation as
182          * last page could have large enough tailroom.
183          */
184         prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
185                                     GFP_USER);
186         if (!prog_adj)
187                 return NULL;
188
189         prog_adj->len = insn_adj_cnt;
190
191         /* Patching happens in 3 steps:
192          *
193          * 1) Move over tail of insnsi from next instruction onwards,
194          *    so we can patch the single target insn with one or more
195          *    new ones (patching is always from 1 to n insns, n > 0).
196          * 2) Inject new instructions at the target location.
197          * 3) Adjust branch offsets if necessary.
198          */
199         insn_rest = insn_adj_cnt - off - len;
200
201         memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
202                 sizeof(*patch) * insn_rest);
203         memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
204
205         bpf_adj_branches(prog_adj, off, insn_delta);
206
207         return prog_adj;
208 }
209
210 #ifdef CONFIG_BPF_JIT
211 /* All BPF JIT sysctl knobs here. */
212 int bpf_jit_enable   __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
213 int bpf_jit_harden   __read_mostly;
214 long bpf_jit_limit   __read_mostly;
215 long bpf_jit_limit_max __read_mostly;
216
217 static atomic_long_t bpf_jit_current;
218
219 /* Can be overridden by an arch's JIT compiler if it has a custom,
220  * dedicated BPF backend memory area, or if neither of the two
221  * below apply.
222  */
223 u64 __weak bpf_jit_alloc_exec_limit(void)
224 {
225 #if defined(MODULES_VADDR)
226         return MODULES_END - MODULES_VADDR;
227 #else
228         return VMALLOC_END - VMALLOC_START;
229 #endif
230 }
231
232 static int __init bpf_jit_charge_init(void)
233 {
234         /* Only used as heuristic here to derive limit. */
235         bpf_jit_limit_max = bpf_jit_alloc_exec_limit();
236         bpf_jit_limit = min_t(u64, round_up(bpf_jit_limit_max >> 2,
237                                             PAGE_SIZE), LONG_MAX);
238         return 0;
239 }
240 pure_initcall(bpf_jit_charge_init);
241
242 static int bpf_jit_charge_modmem(u32 pages)
243 {
244         if (atomic_long_add_return(pages, &bpf_jit_current) >
245             (bpf_jit_limit >> PAGE_SHIFT)) {
246                 if (!capable(CAP_SYS_ADMIN)) {
247                         atomic_long_sub(pages, &bpf_jit_current);
248                         return -EPERM;
249                 }
250         }
251
252         return 0;
253 }
254
255 static void bpf_jit_uncharge_modmem(u32 pages)
256 {
257         atomic_long_sub(pages, &bpf_jit_current);
258 }
259
260 struct bpf_binary_header *
261 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
262                      unsigned int alignment,
263                      bpf_jit_fill_hole_t bpf_fill_ill_insns)
264 {
265         struct bpf_binary_header *hdr;
266         u32 size, hole, start, pages;
267
268         /* Most of BPF filters are really small, but if some of them
269          * fill a page, allow at least 128 extra bytes to insert a
270          * random section of illegal instructions.
271          */
272         size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
273         pages = size / PAGE_SIZE;
274
275         if (bpf_jit_charge_modmem(pages))
276                 return NULL;
277         hdr = module_alloc(size);
278         if (!hdr) {
279                 bpf_jit_uncharge_modmem(pages);
280                 return NULL;
281         }
282
283         /* Fill space with illegal/arch-dep instructions. */
284         bpf_fill_ill_insns(hdr, size);
285
286         hdr->pages = pages;
287         hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
288                      PAGE_SIZE - sizeof(*hdr));
289         start = (get_random_int() % hole) & ~(alignment - 1);
290
291         /* Leave a random number of instructions before BPF code. */
292         *image_ptr = &hdr->image[start];
293
294         return hdr;
295 }
296
297 void bpf_jit_binary_free(struct bpf_binary_header *hdr)
298 {
299         u32 pages = hdr->pages;
300
301         module_memfree(hdr);
302         bpf_jit_uncharge_modmem(pages);
303 }
304
305 static int bpf_jit_blind_insn(const struct bpf_insn *from,
306                               const struct bpf_insn *aux,
307                               struct bpf_insn *to_buff)
308 {
309         struct bpf_insn *to = to_buff;
310         u32 imm_rnd = get_random_int();
311         s16 off;
312
313         BUILD_BUG_ON(BPF_REG_AX  + 1 != MAX_BPF_JIT_REG);
314         BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
315
316         if (from->imm == 0 &&
317             (from->code == (BPF_ALU   | BPF_MOV | BPF_K) ||
318              from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
319                 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
320                 goto out;
321         }
322
323         switch (from->code) {
324         case BPF_ALU | BPF_ADD | BPF_K:
325         case BPF_ALU | BPF_SUB | BPF_K:
326         case BPF_ALU | BPF_AND | BPF_K:
327         case BPF_ALU | BPF_OR  | BPF_K:
328         case BPF_ALU | BPF_XOR | BPF_K:
329         case BPF_ALU | BPF_MUL | BPF_K:
330         case BPF_ALU | BPF_MOV | BPF_K:
331         case BPF_ALU | BPF_DIV | BPF_K:
332         case BPF_ALU | BPF_MOD | BPF_K:
333                 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
334                 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
335                 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
336                 break;
337
338         case BPF_ALU64 | BPF_ADD | BPF_K:
339         case BPF_ALU64 | BPF_SUB | BPF_K:
340         case BPF_ALU64 | BPF_AND | BPF_K:
341         case BPF_ALU64 | BPF_OR  | BPF_K:
342         case BPF_ALU64 | BPF_XOR | BPF_K:
343         case BPF_ALU64 | BPF_MUL | BPF_K:
344         case BPF_ALU64 | BPF_MOV | BPF_K:
345         case BPF_ALU64 | BPF_DIV | BPF_K:
346         case BPF_ALU64 | BPF_MOD | BPF_K:
347                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
348                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
349                 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
350                 break;
351
352         case BPF_JMP | BPF_JEQ  | BPF_K:
353         case BPF_JMP | BPF_JNE  | BPF_K:
354         case BPF_JMP | BPF_JGT  | BPF_K:
355         case BPF_JMP | BPF_JGE  | BPF_K:
356         case BPF_JMP | BPF_JSGT | BPF_K:
357         case BPF_JMP | BPF_JSGE | BPF_K:
358         case BPF_JMP | BPF_JSET | BPF_K:
359                 /* Accommodate for extra offset in case of a backjump. */
360                 off = from->off;
361                 if (off < 0)
362                         off -= 2;
363                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
364                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
365                 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
366                 break;
367
368         case BPF_LD | BPF_ABS | BPF_W:
369         case BPF_LD | BPF_ABS | BPF_H:
370         case BPF_LD | BPF_ABS | BPF_B:
371                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
372                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
373                 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
374                 break;
375
376         case BPF_LD | BPF_IND | BPF_W:
377         case BPF_LD | BPF_IND | BPF_H:
378         case BPF_LD | BPF_IND | BPF_B:
379                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
380                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
381                 *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
382                 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
383                 break;
384
385         case BPF_LD | BPF_IMM | BPF_DW:
386                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
387                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
388                 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
389                 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
390                 break;
391         case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
392                 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
393                 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
394                 *to++ = BPF_ALU64_REG(BPF_OR,  aux[0].dst_reg, BPF_REG_AX);
395                 break;
396
397         case BPF_ST | BPF_MEM | BPF_DW:
398         case BPF_ST | BPF_MEM | BPF_W:
399         case BPF_ST | BPF_MEM | BPF_H:
400         case BPF_ST | BPF_MEM | BPF_B:
401                 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
402                 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
403                 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
404                 break;
405         }
406 out:
407         return to - to_buff;
408 }
409
410 static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
411                                               gfp_t gfp_extra_flags)
412 {
413         gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
414                           gfp_extra_flags;
415         struct bpf_prog *fp;
416
417         fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
418         if (fp != NULL) {
419                 kmemcheck_annotate_bitfield(fp, meta);
420
421                 /* aux->prog still points to the fp_other one, so
422                  * when promoting the clone to the real program,
423                  * this still needs to be adapted.
424                  */
425                 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
426         }
427
428         return fp;
429 }
430
431 static void bpf_prog_clone_free(struct bpf_prog *fp)
432 {
433         /* aux was stolen by the other clone, so we cannot free
434          * it from this path! It will be freed eventually by the
435          * other program on release.
436          *
437          * At this point, we don't need a deferred release since
438          * clone is guaranteed to not be locked.
439          */
440         fp->aux = NULL;
441         __bpf_prog_free(fp);
442 }
443
444 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
445 {
446         /* We have to repoint aux->prog to self, as we don't
447          * know whether fp here is the clone or the original.
448          */
449         fp->aux->prog = fp;
450         bpf_prog_clone_free(fp_other);
451 }
452
453 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
454 {
455         struct bpf_insn insn_buff[16], aux[2];
456         struct bpf_prog *clone, *tmp;
457         int insn_delta, insn_cnt;
458         struct bpf_insn *insn;
459         int i, rewritten;
460
461         if (!bpf_jit_blinding_enabled())
462                 return prog;
463
464         clone = bpf_prog_clone_create(prog, GFP_USER);
465         if (!clone)
466                 return ERR_PTR(-ENOMEM);
467
468         insn_cnt = clone->len;
469         insn = clone->insnsi;
470
471         for (i = 0; i < insn_cnt; i++, insn++) {
472                 /* We temporarily need to hold the original ld64 insn
473                  * so that we can still access the first part in the
474                  * second blinding run.
475                  */
476                 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
477                     insn[1].code == 0)
478                         memcpy(aux, insn, sizeof(aux));
479
480                 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
481                 if (!rewritten)
482                         continue;
483
484                 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
485                 if (!tmp) {
486                         /* Patching may have repointed aux->prog during
487                          * realloc from the original one, so we need to
488                          * fix it up here on error.
489                          */
490                         bpf_jit_prog_release_other(prog, clone);
491                         return ERR_PTR(-ENOMEM);
492                 }
493
494                 clone = tmp;
495                 insn_delta = rewritten - 1;
496
497                 /* Walk new program and skip insns we just inserted. */
498                 insn = clone->insnsi + i + insn_delta;
499                 insn_cnt += insn_delta;
500                 i        += insn_delta;
501         }
502
503         return clone;
504 }
505 #endif /* CONFIG_BPF_JIT */
506
507 /* Base function for offset calculation. Needs to go into .text section,
508  * therefore keeping it non-static as well; will also be used by JITs
509  * anyway later on, so do not let the compiler omit it.
510  */
511 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
512 {
513         return 0;
514 }
515 EXPORT_SYMBOL_GPL(__bpf_call_base);
516
517 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
518 /**
519  *      __bpf_prog_run - run eBPF program on a given context
520  *      @ctx: is the data we are operating on
521  *      @insn: is the array of eBPF instructions
522  *
523  * Decode and execute eBPF instructions.
524  */
525 static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
526 {
527         u64 stack[MAX_BPF_STACK / sizeof(u64)];
528         u64 regs[MAX_BPF_REG], tmp;
529         static const void *jumptable[256] = {
530                 [0 ... 255] = &&default_label,
531                 /* Now overwrite non-defaults ... */
532                 /* 32 bit ALU operations */
533                 [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
534                 [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
535                 [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
536                 [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
537                 [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
538                 [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
539                 [BPF_ALU | BPF_OR | BPF_X]  = &&ALU_OR_X,
540                 [BPF_ALU | BPF_OR | BPF_K]  = &&ALU_OR_K,
541                 [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
542                 [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
543                 [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
544                 [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
545                 [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
546                 [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
547                 [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
548                 [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
549                 [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
550                 [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
551                 [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
552                 [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
553                 [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
554                 [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
555                 [BPF_ALU | BPF_NEG] = &&ALU_NEG,
556                 [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
557                 [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
558                 /* 64 bit ALU operations */
559                 [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
560                 [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
561                 [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
562                 [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
563                 [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
564                 [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
565                 [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
566                 [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
567                 [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
568                 [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
569                 [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
570                 [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
571                 [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
572                 [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
573                 [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
574                 [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
575                 [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
576                 [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
577                 [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
578                 [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
579                 [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
580                 [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
581                 [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
582                 [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
583                 [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
584                 /* Call instruction */
585                 [BPF_JMP | BPF_CALL] = &&JMP_CALL,
586                 [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
587                 /* Jumps */
588                 [BPF_JMP | BPF_JA] = &&JMP_JA,
589                 [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
590                 [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
591                 [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
592                 [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
593                 [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
594                 [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
595                 [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
596                 [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
597                 [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
598                 [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
599                 [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
600                 [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
601                 [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
602                 [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
603                 /* Program return */
604                 [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
605                 /* Store instructions */
606                 [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
607                 [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
608                 [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
609                 [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
610                 [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
611                 [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
612                 [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
613                 [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
614                 [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
615                 [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
616                 /* Load instructions */
617                 [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
618                 [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
619                 [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
620                 [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
621                 [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
622                 [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
623                 [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
624                 [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
625                 [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
626                 [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
627                 [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
628         };
629         u32 tail_call_cnt = 0;
630         void *ptr;
631         int off;
632
633 #define CONT     ({ insn++; goto select_insn; })
634 #define CONT_JMP ({ insn++; goto select_insn; })
635
636         FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
637         ARG1 = (u64) (unsigned long) ctx;
638
639 select_insn:
640         goto *jumptable[insn->code];
641
642         /* ALU */
643 #define ALU(OPCODE, OP)                 \
644         ALU64_##OPCODE##_X:             \
645                 DST = DST OP SRC;       \
646                 CONT;                   \
647         ALU_##OPCODE##_X:               \
648                 DST = (u32) DST OP (u32) SRC;   \
649                 CONT;                   \
650         ALU64_##OPCODE##_K:             \
651                 DST = DST OP IMM;               \
652                 CONT;                   \
653         ALU_##OPCODE##_K:               \
654                 DST = (u32) DST OP (u32) IMM;   \
655                 CONT;
656
657         ALU(ADD,  +)
658         ALU(SUB,  -)
659         ALU(AND,  &)
660         ALU(OR,   |)
661         ALU(LSH, <<)
662         ALU(RSH, >>)
663         ALU(XOR,  ^)
664         ALU(MUL,  *)
665 #undef ALU
666         ALU_NEG:
667                 DST = (u32) -DST;
668                 CONT;
669         ALU64_NEG:
670                 DST = -DST;
671                 CONT;
672         ALU_MOV_X:
673                 DST = (u32) SRC;
674                 CONT;
675         ALU_MOV_K:
676                 DST = (u32) IMM;
677                 CONT;
678         ALU64_MOV_X:
679                 DST = SRC;
680                 CONT;
681         ALU64_MOV_K:
682                 DST = IMM;
683                 CONT;
684         LD_IMM_DW:
685                 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
686                 insn++;
687                 CONT;
688         ALU64_ARSH_X:
689                 (*(s64 *) &DST) >>= SRC;
690                 CONT;
691         ALU64_ARSH_K:
692                 (*(s64 *) &DST) >>= IMM;
693                 CONT;
694         ALU64_MOD_X:
695                 if (unlikely(SRC == 0))
696                         return 0;
697                 div64_u64_rem(DST, SRC, &tmp);
698                 DST = tmp;
699                 CONT;
700         ALU_MOD_X:
701                 if (unlikely((u32)SRC == 0))
702                         return 0;
703                 tmp = (u32) DST;
704                 DST = do_div(tmp, (u32) SRC);
705                 CONT;
706         ALU64_MOD_K:
707                 div64_u64_rem(DST, IMM, &tmp);
708                 DST = tmp;
709                 CONT;
710         ALU_MOD_K:
711                 tmp = (u32) DST;
712                 DST = do_div(tmp, (u32) IMM);
713                 CONT;
714         ALU64_DIV_X:
715                 if (unlikely(SRC == 0))
716                         return 0;
717                 DST = div64_u64(DST, SRC);
718                 CONT;
719         ALU_DIV_X:
720                 if (unlikely((u32)SRC == 0))
721                         return 0;
722                 tmp = (u32) DST;
723                 do_div(tmp, (u32) SRC);
724                 DST = (u32) tmp;
725                 CONT;
726         ALU64_DIV_K:
727                 DST = div64_u64(DST, IMM);
728                 CONT;
729         ALU_DIV_K:
730                 tmp = (u32) DST;
731                 do_div(tmp, (u32) IMM);
732                 DST = (u32) tmp;
733                 CONT;
734         ALU_END_TO_BE:
735                 switch (IMM) {
736                 case 16:
737                         DST = (__force u16) cpu_to_be16(DST);
738                         break;
739                 case 32:
740                         DST = (__force u32) cpu_to_be32(DST);
741                         break;
742                 case 64:
743                         DST = (__force u64) cpu_to_be64(DST);
744                         break;
745                 }
746                 CONT;
747         ALU_END_TO_LE:
748                 switch (IMM) {
749                 case 16:
750                         DST = (__force u16) cpu_to_le16(DST);
751                         break;
752                 case 32:
753                         DST = (__force u32) cpu_to_le32(DST);
754                         break;
755                 case 64:
756                         DST = (__force u64) cpu_to_le64(DST);
757                         break;
758                 }
759                 CONT;
760
761         /* CALL */
762         JMP_CALL:
763                 /* Function call scratches BPF_R1-BPF_R5 registers,
764                  * preserves BPF_R6-BPF_R9, and stores return value
765                  * into BPF_R0.
766                  */
767                 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
768                                                        BPF_R4, BPF_R5);
769                 CONT;
770
771         JMP_TAIL_CALL: {
772                 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
773                 struct bpf_array *array = container_of(map, struct bpf_array, map);
774                 struct bpf_prog *prog;
775                 u32 index = BPF_R3;
776
777                 if (unlikely(index >= array->map.max_entries))
778                         goto out;
779                 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
780                         goto out;
781
782                 tail_call_cnt++;
783
784                 prog = READ_ONCE(array->ptrs[index]);
785                 if (!prog)
786                         goto out;
787
788                 /* ARG1 at this point is guaranteed to point to CTX from
789                  * the verifier side due to the fact that the tail call is
790                  * handeled like a helper, that is, bpf_tail_call_proto,
791                  * where arg1_type is ARG_PTR_TO_CTX.
792                  */
793                 insn = prog->insnsi;
794                 goto select_insn;
795 out:
796                 CONT;
797         }
798         /* JMP */
799         JMP_JA:
800                 insn += insn->off;
801                 CONT;
802         JMP_JEQ_X:
803                 if (DST == SRC) {
804                         insn += insn->off;
805                         CONT_JMP;
806                 }
807                 CONT;
808         JMP_JEQ_K:
809                 if (DST == IMM) {
810                         insn += insn->off;
811                         CONT_JMP;
812                 }
813                 CONT;
814         JMP_JNE_X:
815                 if (DST != SRC) {
816                         insn += insn->off;
817                         CONT_JMP;
818                 }
819                 CONT;
820         JMP_JNE_K:
821                 if (DST != IMM) {
822                         insn += insn->off;
823                         CONT_JMP;
824                 }
825                 CONT;
826         JMP_JGT_X:
827                 if (DST > SRC) {
828                         insn += insn->off;
829                         CONT_JMP;
830                 }
831                 CONT;
832         JMP_JGT_K:
833                 if (DST > IMM) {
834                         insn += insn->off;
835                         CONT_JMP;
836                 }
837                 CONT;
838         JMP_JGE_X:
839                 if (DST >= SRC) {
840                         insn += insn->off;
841                         CONT_JMP;
842                 }
843                 CONT;
844         JMP_JGE_K:
845                 if (DST >= IMM) {
846                         insn += insn->off;
847                         CONT_JMP;
848                 }
849                 CONT;
850         JMP_JSGT_X:
851                 if (((s64) DST) > ((s64) SRC)) {
852                         insn += insn->off;
853                         CONT_JMP;
854                 }
855                 CONT;
856         JMP_JSGT_K:
857                 if (((s64) DST) > ((s64) IMM)) {
858                         insn += insn->off;
859                         CONT_JMP;
860                 }
861                 CONT;
862         JMP_JSGE_X:
863                 if (((s64) DST) >= ((s64) SRC)) {
864                         insn += insn->off;
865                         CONT_JMP;
866                 }
867                 CONT;
868         JMP_JSGE_K:
869                 if (((s64) DST) >= ((s64) IMM)) {
870                         insn += insn->off;
871                         CONT_JMP;
872                 }
873                 CONT;
874         JMP_JSET_X:
875                 if (DST & SRC) {
876                         insn += insn->off;
877                         CONT_JMP;
878                 }
879                 CONT;
880         JMP_JSET_K:
881                 if (DST & IMM) {
882                         insn += insn->off;
883                         CONT_JMP;
884                 }
885                 CONT;
886         JMP_EXIT:
887                 return BPF_R0;
888
889         /* STX and ST and LDX*/
890 #define LDST(SIZEOP, SIZE)                                              \
891         STX_MEM_##SIZEOP:                                               \
892                 *(SIZE *)(unsigned long) (DST + insn->off) = SRC;       \
893                 CONT;                                                   \
894         ST_MEM_##SIZEOP:                                                \
895                 *(SIZE *)(unsigned long) (DST + insn->off) = IMM;       \
896                 CONT;                                                   \
897         LDX_MEM_##SIZEOP:                                               \
898                 DST = *(SIZE *)(unsigned long) (SRC + insn->off);       \
899                 CONT;
900
901         LDST(B,   u8)
902         LDST(H,  u16)
903         LDST(W,  u32)
904         LDST(DW, u64)
905 #undef LDST
906         STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
907                 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
908                            (DST + insn->off));
909                 CONT;
910         STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
911                 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
912                              (DST + insn->off));
913                 CONT;
914         LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
915                 off = IMM;
916 load_word:
917                 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
918                  * only appearing in the programs where ctx ==
919                  * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
920                  * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
921                  * internal BPF verifier will check that BPF_R6 ==
922                  * ctx.
923                  *
924                  * BPF_ABS and BPF_IND are wrappers of function calls,
925                  * so they scratch BPF_R1-BPF_R5 registers, preserve
926                  * BPF_R6-BPF_R9, and store return value into BPF_R0.
927                  *
928                  * Implicit input:
929                  *   ctx == skb == BPF_R6 == CTX
930                  *
931                  * Explicit input:
932                  *   SRC == any register
933                  *   IMM == 32-bit immediate
934                  *
935                  * Output:
936                  *   BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
937                  */
938
939                 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
940                 if (likely(ptr != NULL)) {
941                         BPF_R0 = get_unaligned_be32(ptr);
942                         CONT;
943                 }
944
945                 return 0;
946         LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
947                 off = IMM;
948 load_half:
949                 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
950                 if (likely(ptr != NULL)) {
951                         BPF_R0 = get_unaligned_be16(ptr);
952                         CONT;
953                 }
954
955                 return 0;
956         LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
957                 off = IMM;
958 load_byte:
959                 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
960                 if (likely(ptr != NULL)) {
961                         BPF_R0 = *(u8 *)ptr;
962                         CONT;
963                 }
964
965                 return 0;
966         LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
967                 off = IMM + SRC;
968                 goto load_word;
969         LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
970                 off = IMM + SRC;
971                 goto load_half;
972         LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
973                 off = IMM + SRC;
974                 goto load_byte;
975
976         default_label:
977                 /* If we ever reach this, we have a bug somewhere. */
978                 WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
979                 return 0;
980 }
981 STACK_FRAME_NON_STANDARD(__bpf_prog_run); /* jump table */
982
983 #else
984 static unsigned int __bpf_prog_ret0_warn(void *ctx,
985                                          const struct bpf_insn *insn)
986 {
987         /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
988          * is not working properly, so warn about it!
989          */
990         WARN_ON_ONCE(1);
991         return 0;
992 }
993 #endif
994
995 bool bpf_prog_array_compatible(struct bpf_array *array,
996                                const struct bpf_prog *fp)
997 {
998         if (!array->owner_prog_type) {
999                 /* There's no owner yet where we could check for
1000                  * compatibility.
1001                  */
1002                 array->owner_prog_type = fp->type;
1003                 array->owner_jited = fp->jited;
1004
1005                 return true;
1006         }
1007
1008         return array->owner_prog_type == fp->type &&
1009                array->owner_jited == fp->jited;
1010 }
1011
1012 static int bpf_check_tail_call(const struct bpf_prog *fp)
1013 {
1014         struct bpf_prog_aux *aux = fp->aux;
1015         int i;
1016
1017         for (i = 0; i < aux->used_map_cnt; i++) {
1018                 struct bpf_map *map = aux->used_maps[i];
1019                 struct bpf_array *array;
1020
1021                 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1022                         continue;
1023
1024                 array = container_of(map, struct bpf_array, map);
1025                 if (!bpf_prog_array_compatible(array, fp))
1026                         return -EINVAL;
1027         }
1028
1029         return 0;
1030 }
1031
1032 /**
1033  *      bpf_prog_select_runtime - select exec runtime for BPF program
1034  *      @fp: bpf_prog populated with internal BPF program
1035  *      @err: pointer to error variable
1036  *
1037  * Try to JIT eBPF program, if JIT is not available, use interpreter.
1038  * The BPF program will be executed via BPF_PROG_RUN() macro.
1039  */
1040 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
1041 {
1042 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1043         fp->bpf_func = (void *) __bpf_prog_run;
1044 #else
1045         fp->bpf_func = (void *) __bpf_prog_ret0_warn;
1046 #endif
1047
1048         /* eBPF JITs can rewrite the program in case constant
1049          * blinding is active. However, in case of error during
1050          * blinding, bpf_int_jit_compile() must always return a
1051          * valid program, which in this case would simply not
1052          * be JITed, but falls back to the interpreter.
1053          */
1054         fp = bpf_int_jit_compile(fp);
1055 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
1056         if (!fp->jited) {
1057                 *err = -ENOTSUPP;
1058                 return fp;
1059         }
1060 #endif
1061         bpf_prog_lock_ro(fp);
1062
1063         /* The tail call compatibility check can only be done at
1064          * this late stage as we need to determine, if we deal
1065          * with JITed or non JITed program concatenations and not
1066          * all eBPF JITs might immediately support all features.
1067          */
1068         *err = bpf_check_tail_call(fp);
1069
1070         return fp;
1071 }
1072 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
1073
1074 static void bpf_prog_free_deferred(struct work_struct *work)
1075 {
1076         struct bpf_prog_aux *aux;
1077
1078         aux = container_of(work, struct bpf_prog_aux, work);
1079         bpf_jit_free(aux->prog);
1080 }
1081
1082 /* Free internal BPF program */
1083 void bpf_prog_free(struct bpf_prog *fp)
1084 {
1085         struct bpf_prog_aux *aux = fp->aux;
1086
1087         INIT_WORK(&aux->work, bpf_prog_free_deferred);
1088         schedule_work(&aux->work);
1089 }
1090 EXPORT_SYMBOL_GPL(bpf_prog_free);
1091
1092 /* RNG for unpriviledged user space with separated state from prandom_u32(). */
1093 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1094
1095 void bpf_user_rnd_init_once(void)
1096 {
1097         prandom_init_once(&bpf_user_rnd_state);
1098 }
1099
1100 BPF_CALL_0(bpf_user_rnd_u32)
1101 {
1102         /* Should someone ever have the rather unwise idea to use some
1103          * of the registers passed into this function, then note that
1104          * this function is called from native eBPF and classic-to-eBPF
1105          * transformations. Register assignments from both sides are
1106          * different, f.e. classic always sets fn(ctx, A, X) here.
1107          */
1108         struct rnd_state *state;
1109         u32 res;
1110
1111         state = &get_cpu_var(bpf_user_rnd_state);
1112         res = prandom_u32_state(state);
1113         put_cpu_var(bpf_user_rnd_state);
1114
1115         return res;
1116 }
1117
1118 /* Weak definitions of helper functions in case we don't have bpf syscall. */
1119 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1120 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1121 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1122
1123 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
1124 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
1125 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
1126
1127 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1128 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1129 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
1130
1131 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1132 {
1133         return NULL;
1134 }
1135
1136 u64 __weak
1137 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1138                  void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
1139 {
1140         return -ENOTSUPP;
1141 }
1142
1143 /* Always built-in helper functions. */
1144 const struct bpf_func_proto bpf_tail_call_proto = {
1145         .func           = NULL,
1146         .gpl_only       = false,
1147         .ret_type       = RET_VOID,
1148         .arg1_type      = ARG_PTR_TO_CTX,
1149         .arg2_type      = ARG_CONST_MAP_PTR,
1150         .arg3_type      = ARG_ANYTHING,
1151 };
1152
1153 /* For classic BPF JITs that don't implement bpf_int_jit_compile(). */
1154 struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
1155 {
1156         return prog;
1157 }
1158
1159 bool __weak bpf_helper_changes_skb_data(void *func)
1160 {
1161         return false;
1162 }
1163
1164 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1165  * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1166  */
1167 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1168                          int len)
1169 {
1170         return -EFAULT;
1171 }