2 * Linux Socket Filter - Kernel level socket filtering
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
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.
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
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>
31 #include <asm/unaligned.h>
34 #define BPF_R0 regs[BPF_REG_0]
35 #define BPF_R1 regs[BPF_REG_1]
36 #define BPF_R2 regs[BPF_REG_2]
37 #define BPF_R3 regs[BPF_REG_3]
38 #define BPF_R4 regs[BPF_REG_4]
39 #define BPF_R5 regs[BPF_REG_5]
40 #define BPF_R6 regs[BPF_REG_6]
41 #define BPF_R7 regs[BPF_REG_7]
42 #define BPF_R8 regs[BPF_REG_8]
43 #define BPF_R9 regs[BPF_REG_9]
44 #define BPF_R10 regs[BPF_REG_10]
47 #define DST regs[insn->dst_reg]
48 #define SRC regs[insn->src_reg]
49 #define FP regs[BPF_REG_FP]
50 #define ARG1 regs[BPF_REG_ARG1]
51 #define CTX regs[BPF_REG_CTX]
54 /* No hurry in this branch
56 * Exported for the bpf jit load helper.
58 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
63 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
64 else if (k >= SKF_LL_OFF)
65 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
67 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
73 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
75 gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
77 struct bpf_prog_aux *aux;
80 size = round_up(size, PAGE_SIZE);
81 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
85 kmemcheck_annotate_bitfield(fp, meta);
87 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
93 fp->pages = size / PAGE_SIZE;
99 EXPORT_SYMBOL_GPL(bpf_prog_alloc);
101 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
102 gfp_t gfp_extra_flags)
104 gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
108 BUG_ON(fp_old == NULL);
110 size = round_up(size, PAGE_SIZE);
111 if (size <= fp_old->pages * PAGE_SIZE)
114 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
116 kmemcheck_annotate_bitfield(fp, meta);
118 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
119 fp->pages = size / PAGE_SIZE;
122 /* We keep fp->aux from fp_old around in the new
123 * reallocated structure.
126 __bpf_prog_free(fp_old);
131 EXPORT_SYMBOL_GPL(bpf_prog_realloc);
133 void __bpf_prog_free(struct bpf_prog *fp)
138 EXPORT_SYMBOL_GPL(__bpf_prog_free);
140 static bool bpf_is_jmp_and_has_target(const struct bpf_insn *insn)
142 return BPF_CLASS(insn->code) == BPF_JMP &&
143 /* Call and Exit are both special jumps with no
144 * target inside the BPF instruction image.
146 BPF_OP(insn->code) != BPF_CALL &&
147 BPF_OP(insn->code) != BPF_EXIT;
150 static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
152 struct bpf_insn *insn = prog->insnsi;
153 u32 i, insn_cnt = prog->len;
155 for (i = 0; i < insn_cnt; i++, insn++) {
156 if (!bpf_is_jmp_and_has_target(insn))
159 /* Adjust offset of jmps if we cross boundaries. */
160 if (i < pos && i + insn->off + 1 > pos)
162 else if (i > pos + delta && i + insn->off + 1 <= pos + delta)
167 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
168 const struct bpf_insn *patch, u32 len)
170 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
171 struct bpf_prog *prog_adj;
173 /* Since our patchlet doesn't expand the image, we're done. */
174 if (insn_delta == 0) {
175 memcpy(prog->insnsi + off, patch, sizeof(*patch));
179 insn_adj_cnt = prog->len + insn_delta;
181 /* Several new instructions need to be inserted. Make room
182 * for them. Likely, there's no need for a new allocation as
183 * last page could have large enough tailroom.
185 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
190 prog_adj->len = insn_adj_cnt;
192 /* Patching happens in 3 steps:
194 * 1) Move over tail of insnsi from next instruction onwards,
195 * so we can patch the single target insn with one or more
196 * new ones (patching is always from 1 to n insns, n > 0).
197 * 2) Inject new instructions at the target location.
198 * 3) Adjust branch offsets if necessary.
200 insn_rest = insn_adj_cnt - off - len;
202 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
203 sizeof(*patch) * insn_rest);
204 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
206 bpf_adj_branches(prog_adj, off, insn_delta);
211 #ifdef CONFIG_BPF_JIT
212 struct bpf_binary_header *
213 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
214 unsigned int alignment,
215 bpf_jit_fill_hole_t bpf_fill_ill_insns)
217 struct bpf_binary_header *hdr;
218 unsigned int size, hole, start;
220 /* Most of BPF filters are really small, but if some of them
221 * fill a page, allow at least 128 extra bytes to insert a
222 * random section of illegal instructions.
224 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
225 hdr = module_alloc(size);
229 /* Fill space with illegal/arch-dep instructions. */
230 bpf_fill_ill_insns(hdr, size);
232 hdr->pages = size / PAGE_SIZE;
233 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
234 PAGE_SIZE - sizeof(*hdr));
235 start = (prandom_u32() % hole) & ~(alignment - 1);
237 /* Leave a random number of instructions before BPF code. */
238 *image_ptr = &hdr->image[start];
243 void bpf_jit_binary_free(struct bpf_binary_header *hdr)
247 #endif /* CONFIG_BPF_JIT */
249 /* Base function for offset calculation. Needs to go into .text section,
250 * therefore keeping it non-static as well; will also be used by JITs
251 * anyway later on, so do not let the compiler omit it.
253 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
257 EXPORT_SYMBOL_GPL(__bpf_call_base);
260 * __bpf_prog_run - run eBPF program on a given context
261 * @ctx: is the data we are operating on
262 * @insn: is the array of eBPF instructions
264 * Decode and execute eBPF instructions.
266 static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
268 u64 stack[MAX_BPF_STACK / sizeof(u64)];
269 u64 regs[MAX_BPF_REG], tmp;
270 static const void *jumptable[256] = {
271 [0 ... 255] = &&default_label,
272 /* Now overwrite non-defaults ... */
273 /* 32 bit ALU operations */
274 [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
275 [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
276 [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
277 [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
278 [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
279 [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
280 [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
281 [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
282 [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
283 [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
284 [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
285 [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
286 [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
287 [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
288 [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
289 [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
290 [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
291 [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
292 [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
293 [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
294 [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
295 [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
296 [BPF_ALU | BPF_NEG] = &&ALU_NEG,
297 [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
298 [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
299 /* 64 bit ALU operations */
300 [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
301 [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
302 [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
303 [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
304 [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
305 [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
306 [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
307 [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
308 [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
309 [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
310 [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
311 [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
312 [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
313 [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
314 [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
315 [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
316 [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
317 [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
318 [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
319 [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
320 [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
321 [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
322 [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
323 [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
324 [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
325 /* Call instruction */
326 [BPF_JMP | BPF_CALL] = &&JMP_CALL,
327 [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
329 [BPF_JMP | BPF_JA] = &&JMP_JA,
330 [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
331 [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
332 [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
333 [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
334 [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
335 [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
336 [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
337 [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
338 [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
339 [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
340 [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
341 [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
342 [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
343 [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
345 [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
346 /* Store instructions */
347 [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
348 [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
349 [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
350 [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
351 [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
352 [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
353 [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
354 [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
355 [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
356 [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
357 /* Load instructions */
358 [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
359 [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
360 [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
361 [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
362 [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
363 [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
364 [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
365 [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
366 [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
367 [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
368 [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
370 u32 tail_call_cnt = 0;
374 #define CONT ({ insn++; goto select_insn; })
375 #define CONT_JMP ({ insn++; goto select_insn; })
377 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
378 ARG1 = (u64) (unsigned long) ctx;
380 /* Registers used in classic BPF programs need to be reset first. */
385 goto *jumptable[insn->code];
388 #define ALU(OPCODE, OP) \
389 ALU64_##OPCODE##_X: \
393 DST = (u32) DST OP (u32) SRC; \
395 ALU64_##OPCODE##_K: \
399 DST = (u32) DST OP (u32) IMM; \
430 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
434 (*(s64 *) &DST) >>= SRC;
437 (*(s64 *) &DST) >>= IMM;
440 if (unlikely(SRC == 0))
442 div64_u64_rem(DST, SRC, &tmp);
446 if (unlikely(SRC == 0))
449 DST = do_div(tmp, (u32) SRC);
452 div64_u64_rem(DST, IMM, &tmp);
457 DST = do_div(tmp, (u32) IMM);
460 if (unlikely(SRC == 0))
462 DST = div64_u64(DST, SRC);
465 if (unlikely(SRC == 0))
468 do_div(tmp, (u32) SRC);
472 DST = div64_u64(DST, IMM);
476 do_div(tmp, (u32) IMM);
482 DST = (__force u16) cpu_to_be16(DST);
485 DST = (__force u32) cpu_to_be32(DST);
488 DST = (__force u64) cpu_to_be64(DST);
495 DST = (__force u16) cpu_to_le16(DST);
498 DST = (__force u32) cpu_to_le32(DST);
501 DST = (__force u64) cpu_to_le64(DST);
508 /* Function call scratches BPF_R1-BPF_R5 registers,
509 * preserves BPF_R6-BPF_R9, and stores return value
512 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
517 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
518 struct bpf_array *array = container_of(map, struct bpf_array, map);
519 struct bpf_prog *prog;
522 if (unlikely(index >= array->map.max_entries))
525 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
530 prog = READ_ONCE(array->ptrs[index]);
534 /* ARG1 at this point is guaranteed to point to CTX from
535 * the verifier side due to the fact that the tail call is
536 * handeled like a helper, that is, bpf_tail_call_proto,
537 * where arg1_type is ARG_PTR_TO_CTX.
597 if (((s64) DST) > ((s64) SRC)) {
603 if (((s64) DST) > ((s64) IMM)) {
609 if (((s64) DST) >= ((s64) SRC)) {
615 if (((s64) DST) >= ((s64) IMM)) {
635 /* STX and ST and LDX*/
636 #define LDST(SIZEOP, SIZE) \
638 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
641 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
644 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
652 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
653 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
656 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
657 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
660 LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
663 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
664 * only appearing in the programs where ctx ==
665 * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
666 * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
667 * internal BPF verifier will check that BPF_R6 ==
670 * BPF_ABS and BPF_IND are wrappers of function calls,
671 * so they scratch BPF_R1-BPF_R5 registers, preserve
672 * BPF_R6-BPF_R9, and store return value into BPF_R0.
675 * ctx == skb == BPF_R6 == CTX
678 * SRC == any register
679 * IMM == 32-bit immediate
682 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
685 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
686 if (likely(ptr != NULL)) {
687 BPF_R0 = get_unaligned_be32(ptr);
692 LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
695 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
696 if (likely(ptr != NULL)) {
697 BPF_R0 = get_unaligned_be16(ptr);
702 LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
705 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
706 if (likely(ptr != NULL)) {
712 LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
715 LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
718 LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
723 /* If we ever reach this, we have a bug somewhere. */
724 WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
728 bool bpf_prog_array_compatible(struct bpf_array *array,
729 const struct bpf_prog *fp)
731 if (!array->owner_prog_type) {
732 /* There's no owner yet where we could check for
735 array->owner_prog_type = fp->type;
736 array->owner_jited = fp->jited;
741 return array->owner_prog_type == fp->type &&
742 array->owner_jited == fp->jited;
745 static int bpf_check_tail_call(const struct bpf_prog *fp)
747 struct bpf_prog_aux *aux = fp->aux;
750 for (i = 0; i < aux->used_map_cnt; i++) {
751 struct bpf_map *map = aux->used_maps[i];
752 struct bpf_array *array;
754 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
757 array = container_of(map, struct bpf_array, map);
758 if (!bpf_prog_array_compatible(array, fp))
766 * bpf_prog_select_runtime - select exec runtime for BPF program
767 * @fp: bpf_prog populated with internal BPF program
769 * Try to JIT eBPF program, if JIT is not available, use interpreter.
770 * The BPF program will be executed via BPF_PROG_RUN() macro.
772 int bpf_prog_select_runtime(struct bpf_prog *fp)
774 fp->bpf_func = (void *) __bpf_prog_run;
776 bpf_int_jit_compile(fp);
777 bpf_prog_lock_ro(fp);
779 /* The tail call compatibility check can only be done at
780 * this late stage as we need to determine, if we deal
781 * with JITed or non JITed program concatenations and not
782 * all eBPF JITs might immediately support all features.
784 return bpf_check_tail_call(fp);
786 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
788 static void bpf_prog_free_deferred(struct work_struct *work)
790 struct bpf_prog_aux *aux;
792 aux = container_of(work, struct bpf_prog_aux, work);
793 bpf_jit_free(aux->prog);
796 /* Free internal BPF program */
797 void bpf_prog_free(struct bpf_prog *fp)
799 struct bpf_prog_aux *aux = fp->aux;
801 INIT_WORK(&aux->work, bpf_prog_free_deferred);
802 schedule_work(&aux->work);
804 EXPORT_SYMBOL_GPL(bpf_prog_free);
806 /* RNG for unpriviledged user space with separated state from prandom_u32(). */
807 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
809 void bpf_user_rnd_init_once(void)
811 prandom_init_once(&bpf_user_rnd_state);
814 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
816 /* Should someone ever have the rather unwise idea to use some
817 * of the registers passed into this function, then note that
818 * this function is called from native eBPF and classic-to-eBPF
819 * transformations. Register assignments from both sides are
820 * different, f.e. classic always sets fn(ctx, A, X) here.
822 struct rnd_state *state;
825 state = &get_cpu_var(bpf_user_rnd_state);
826 res = prandom_u32_state(state);
832 /* Weak definitions of helper functions in case we don't have bpf syscall. */
833 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
834 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
835 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
837 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
838 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
839 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
840 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
841 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
842 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
843 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
848 /* Always built-in helper functions. */
849 const struct bpf_func_proto bpf_tail_call_proto = {
852 .ret_type = RET_VOID,
853 .arg1_type = ARG_PTR_TO_CTX,
854 .arg2_type = ARG_CONST_MAP_PTR,
855 .arg3_type = ARG_ANYTHING,
858 /* For classic BPF JITs that don't implement bpf_int_jit_compile(). */
859 void __weak bpf_int_jit_compile(struct bpf_prog *prog)
863 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
864 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
866 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,