blob: 5313d09d4b62f2e940ab4b2e05c2880ad9d92b7c [file] [log] [blame]
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -07001/*
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.
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -070021 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070022 */
Daniel Borkmann738cbe72014-09-08 08:04:47 +020023
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070024#include <linux/filter.h>
25#include <linux/skbuff.h>
Daniel Borkmann60a3b222014-09-02 22:53:44 +020026#include <linux/vmalloc.h>
Daniel Borkmann738cbe72014-09-08 08:04:47 +020027#include <linux/random.h>
28#include <linux/moduleloader.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070029#include <linux/bpf.h>
Josh Poimboeuf39853cc2016-02-28 22:22:37 -060030#include <linux/frame.h>
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070031
Daniel Borkmann3324b582015-05-29 23:23:07 +020032#include <asm/unaligned.h>
33
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070034/* 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 */
59void *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;
Daniel Borkmann3324b582015-05-29 23:23:07 +020067
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070068 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
69 return ptr;
70
71 return NULL;
72}
73
Daniel Borkmann60a3b222014-09-02 22:53:44 +020074struct 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;
Alexei Starovoitov09756af2014-09-26 00:17:00 -070078 struct bpf_prog_aux *aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +020079 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
Daniel Borkmanna91263d2015-09-30 01:41:50 +020086 kmemcheck_annotate_bitfield(fp, meta);
87
Alexei Starovoitov09756af2014-09-26 00:17:00 -070088 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
89 if (aux == NULL) {
Daniel Borkmann60a3b222014-09-02 22:53:44 +020090 vfree(fp);
91 return NULL;
92 }
93
94 fp->pages = size / PAGE_SIZE;
Alexei Starovoitov09756af2014-09-26 00:17:00 -070095 fp->aux = aux;
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +010096 fp->aux->prog = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +020097
98 return fp;
99}
100EXPORT_SYMBOL_GPL(bpf_prog_alloc);
101
102struct 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) {
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200117 kmemcheck_annotate_bitfield(fp, meta);
118
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200119 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
120 fp->pages = size / PAGE_SIZE;
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100121 fp->aux->prog = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200122
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700123 /* We keep fp->aux from fp_old around in the new
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200124 * reallocated structure.
125 */
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700126 fp_old->aux = NULL;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200127 __bpf_prog_free(fp_old);
128 }
129
130 return fp;
131}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200132
133void __bpf_prog_free(struct bpf_prog *fp)
134{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700135 kfree(fp->aux);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200136 vfree(fp);
137}
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200138
Daniel Borkmannb954d832014-09-10 15:01:02 +0200139#ifdef CONFIG_BPF_JIT
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200140struct bpf_binary_header *
141bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
142 unsigned int alignment,
143 bpf_jit_fill_hole_t bpf_fill_ill_insns)
144{
145 struct bpf_binary_header *hdr;
146 unsigned int size, hole, start;
147
148 /* Most of BPF filters are really small, but if some of them
149 * fill a page, allow at least 128 extra bytes to insert a
150 * random section of illegal instructions.
151 */
152 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
153 hdr = module_alloc(size);
154 if (hdr == NULL)
155 return NULL;
156
157 /* Fill space with illegal/arch-dep instructions. */
158 bpf_fill_ill_insns(hdr, size);
159
160 hdr->pages = size / PAGE_SIZE;
161 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
162 PAGE_SIZE - sizeof(*hdr));
163 start = (prandom_u32() % hole) & ~(alignment - 1);
164
165 /* Leave a random number of instructions before BPF code. */
166 *image_ptr = &hdr->image[start];
167
168 return hdr;
169}
170
171void bpf_jit_binary_free(struct bpf_binary_header *hdr)
172{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030173 module_memfree(hdr);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200174}
Daniel Borkmannb954d832014-09-10 15:01:02 +0200175#endif /* CONFIG_BPF_JIT */
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200176
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700177/* Base function for offset calculation. Needs to go into .text section,
178 * therefore keeping it non-static as well; will also be used by JITs
179 * anyway later on, so do not let the compiler omit it.
180 */
181noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
182{
183 return 0;
184}
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -0700185EXPORT_SYMBOL_GPL(__bpf_call_base);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700186
187/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700188 * __bpf_prog_run - run eBPF program on a given context
189 * @ctx: is the data we are operating on
190 * @insn: is the array of eBPF instructions
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700191 *
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700192 * Decode and execute eBPF instructions.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700193 */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700194static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700195{
196 u64 stack[MAX_BPF_STACK / sizeof(u64)];
197 u64 regs[MAX_BPF_REG], tmp;
198 static const void *jumptable[256] = {
199 [0 ... 255] = &&default_label,
200 /* Now overwrite non-defaults ... */
201 /* 32 bit ALU operations */
202 [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
203 [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
204 [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
205 [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
206 [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
207 [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
208 [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
209 [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
210 [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
211 [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
212 [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
213 [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
214 [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
215 [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
216 [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
217 [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
218 [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
219 [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
220 [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
221 [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
222 [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
223 [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
224 [BPF_ALU | BPF_NEG] = &&ALU_NEG,
225 [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
226 [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
227 /* 64 bit ALU operations */
228 [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
229 [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
230 [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
231 [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
232 [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
233 [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
234 [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
235 [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
236 [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
237 [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
238 [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
239 [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
240 [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
241 [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
242 [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
243 [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
244 [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
245 [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
246 [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
247 [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
248 [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
249 [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
250 [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
251 [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
252 [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
253 /* Call instruction */
254 [BPF_JMP | BPF_CALL] = &&JMP_CALL,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700255 [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700256 /* Jumps */
257 [BPF_JMP | BPF_JA] = &&JMP_JA,
258 [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
259 [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
260 [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
261 [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
262 [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
263 [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
264 [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
265 [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
266 [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
267 [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
268 [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
269 [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
270 [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
271 [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
272 /* Program return */
273 [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
274 /* Store instructions */
275 [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
276 [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
277 [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
278 [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
279 [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
280 [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
281 [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
282 [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
283 [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
284 [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
285 /* Load instructions */
286 [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
287 [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
288 [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
289 [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
290 [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
291 [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
292 [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
293 [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
294 [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
295 [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700296 [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700297 };
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700298 u32 tail_call_cnt = 0;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700299 void *ptr;
300 int off;
301
302#define CONT ({ insn++; goto select_insn; })
303#define CONT_JMP ({ insn++; goto select_insn; })
304
305 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
306 ARG1 = (u64) (unsigned long) ctx;
307
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700308select_insn:
309 goto *jumptable[insn->code];
310
311 /* ALU */
312#define ALU(OPCODE, OP) \
313 ALU64_##OPCODE##_X: \
314 DST = DST OP SRC; \
315 CONT; \
316 ALU_##OPCODE##_X: \
317 DST = (u32) DST OP (u32) SRC; \
318 CONT; \
319 ALU64_##OPCODE##_K: \
320 DST = DST OP IMM; \
321 CONT; \
322 ALU_##OPCODE##_K: \
323 DST = (u32) DST OP (u32) IMM; \
324 CONT;
325
326 ALU(ADD, +)
327 ALU(SUB, -)
328 ALU(AND, &)
329 ALU(OR, |)
330 ALU(LSH, <<)
331 ALU(RSH, >>)
332 ALU(XOR, ^)
333 ALU(MUL, *)
334#undef ALU
335 ALU_NEG:
336 DST = (u32) -DST;
337 CONT;
338 ALU64_NEG:
339 DST = -DST;
340 CONT;
341 ALU_MOV_X:
342 DST = (u32) SRC;
343 CONT;
344 ALU_MOV_K:
345 DST = (u32) IMM;
346 CONT;
347 ALU64_MOV_X:
348 DST = SRC;
349 CONT;
350 ALU64_MOV_K:
351 DST = IMM;
352 CONT;
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700353 LD_IMM_DW:
354 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
355 insn++;
356 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700357 ALU64_ARSH_X:
358 (*(s64 *) &DST) >>= SRC;
359 CONT;
360 ALU64_ARSH_K:
361 (*(s64 *) &DST) >>= IMM;
362 CONT;
363 ALU64_MOD_X:
364 if (unlikely(SRC == 0))
365 return 0;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700366 div64_u64_rem(DST, SRC, &tmp);
367 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700368 CONT;
369 ALU_MOD_X:
370 if (unlikely(SRC == 0))
371 return 0;
372 tmp = (u32) DST;
373 DST = do_div(tmp, (u32) SRC);
374 CONT;
375 ALU64_MOD_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700376 div64_u64_rem(DST, IMM, &tmp);
377 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700378 CONT;
379 ALU_MOD_K:
380 tmp = (u32) DST;
381 DST = do_div(tmp, (u32) IMM);
382 CONT;
383 ALU64_DIV_X:
384 if (unlikely(SRC == 0))
385 return 0;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700386 DST = div64_u64(DST, SRC);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700387 CONT;
388 ALU_DIV_X:
389 if (unlikely(SRC == 0))
390 return 0;
391 tmp = (u32) DST;
392 do_div(tmp, (u32) SRC);
393 DST = (u32) tmp;
394 CONT;
395 ALU64_DIV_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700396 DST = div64_u64(DST, IMM);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700397 CONT;
398 ALU_DIV_K:
399 tmp = (u32) DST;
400 do_div(tmp, (u32) IMM);
401 DST = (u32) tmp;
402 CONT;
403 ALU_END_TO_BE:
404 switch (IMM) {
405 case 16:
406 DST = (__force u16) cpu_to_be16(DST);
407 break;
408 case 32:
409 DST = (__force u32) cpu_to_be32(DST);
410 break;
411 case 64:
412 DST = (__force u64) cpu_to_be64(DST);
413 break;
414 }
415 CONT;
416 ALU_END_TO_LE:
417 switch (IMM) {
418 case 16:
419 DST = (__force u16) cpu_to_le16(DST);
420 break;
421 case 32:
422 DST = (__force u32) cpu_to_le32(DST);
423 break;
424 case 64:
425 DST = (__force u64) cpu_to_le64(DST);
426 break;
427 }
428 CONT;
429
430 /* CALL */
431 JMP_CALL:
432 /* Function call scratches BPF_R1-BPF_R5 registers,
433 * preserves BPF_R6-BPF_R9, and stores return value
434 * into BPF_R0.
435 */
436 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
437 BPF_R4, BPF_R5);
438 CONT;
439
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700440 JMP_TAIL_CALL: {
441 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
442 struct bpf_array *array = container_of(map, struct bpf_array, map);
443 struct bpf_prog *prog;
444 u64 index = BPF_R3;
445
446 if (unlikely(index >= array->map.max_entries))
447 goto out;
448
449 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
450 goto out;
451
452 tail_call_cnt++;
453
Wang Nan2a36f0b2015-08-06 07:02:33 +0000454 prog = READ_ONCE(array->ptrs[index]);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700455 if (unlikely(!prog))
456 goto out;
457
Daniel Borkmannc4675f92015-07-13 20:49:32 +0200458 /* ARG1 at this point is guaranteed to point to CTX from
459 * the verifier side due to the fact that the tail call is
460 * handeled like a helper, that is, bpf_tail_call_proto,
461 * where arg1_type is ARG_PTR_TO_CTX.
462 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700463 insn = prog->insnsi;
464 goto select_insn;
465out:
466 CONT;
467 }
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700468 /* JMP */
469 JMP_JA:
470 insn += insn->off;
471 CONT;
472 JMP_JEQ_X:
473 if (DST == SRC) {
474 insn += insn->off;
475 CONT_JMP;
476 }
477 CONT;
478 JMP_JEQ_K:
479 if (DST == IMM) {
480 insn += insn->off;
481 CONT_JMP;
482 }
483 CONT;
484 JMP_JNE_X:
485 if (DST != SRC) {
486 insn += insn->off;
487 CONT_JMP;
488 }
489 CONT;
490 JMP_JNE_K:
491 if (DST != IMM) {
492 insn += insn->off;
493 CONT_JMP;
494 }
495 CONT;
496 JMP_JGT_X:
497 if (DST > SRC) {
498 insn += insn->off;
499 CONT_JMP;
500 }
501 CONT;
502 JMP_JGT_K:
503 if (DST > IMM) {
504 insn += insn->off;
505 CONT_JMP;
506 }
507 CONT;
508 JMP_JGE_X:
509 if (DST >= SRC) {
510 insn += insn->off;
511 CONT_JMP;
512 }
513 CONT;
514 JMP_JGE_K:
515 if (DST >= IMM) {
516 insn += insn->off;
517 CONT_JMP;
518 }
519 CONT;
520 JMP_JSGT_X:
521 if (((s64) DST) > ((s64) SRC)) {
522 insn += insn->off;
523 CONT_JMP;
524 }
525 CONT;
526 JMP_JSGT_K:
527 if (((s64) DST) > ((s64) IMM)) {
528 insn += insn->off;
529 CONT_JMP;
530 }
531 CONT;
532 JMP_JSGE_X:
533 if (((s64) DST) >= ((s64) SRC)) {
534 insn += insn->off;
535 CONT_JMP;
536 }
537 CONT;
538 JMP_JSGE_K:
539 if (((s64) DST) >= ((s64) IMM)) {
540 insn += insn->off;
541 CONT_JMP;
542 }
543 CONT;
544 JMP_JSET_X:
545 if (DST & SRC) {
546 insn += insn->off;
547 CONT_JMP;
548 }
549 CONT;
550 JMP_JSET_K:
551 if (DST & IMM) {
552 insn += insn->off;
553 CONT_JMP;
554 }
555 CONT;
556 JMP_EXIT:
557 return BPF_R0;
558
559 /* STX and ST and LDX*/
560#define LDST(SIZEOP, SIZE) \
561 STX_MEM_##SIZEOP: \
562 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
563 CONT; \
564 ST_MEM_##SIZEOP: \
565 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
566 CONT; \
567 LDX_MEM_##SIZEOP: \
568 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
569 CONT;
570
571 LDST(B, u8)
572 LDST(H, u16)
573 LDST(W, u32)
574 LDST(DW, u64)
575#undef LDST
576 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
577 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
578 (DST + insn->off));
579 CONT;
580 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
581 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
582 (DST + insn->off));
583 CONT;
584 LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
585 off = IMM;
586load_word:
587 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
588 * only appearing in the programs where ctx ==
589 * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700590 * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700591 * internal BPF verifier will check that BPF_R6 ==
592 * ctx.
593 *
594 * BPF_ABS and BPF_IND are wrappers of function calls,
595 * so they scratch BPF_R1-BPF_R5 registers, preserve
596 * BPF_R6-BPF_R9, and store return value into BPF_R0.
597 *
598 * Implicit input:
599 * ctx == skb == BPF_R6 == CTX
600 *
601 * Explicit input:
602 * SRC == any register
603 * IMM == 32-bit immediate
604 *
605 * Output:
606 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
607 */
608
609 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
610 if (likely(ptr != NULL)) {
611 BPF_R0 = get_unaligned_be32(ptr);
612 CONT;
613 }
614
615 return 0;
616 LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
617 off = IMM;
618load_half:
619 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
620 if (likely(ptr != NULL)) {
621 BPF_R0 = get_unaligned_be16(ptr);
622 CONT;
623 }
624
625 return 0;
626 LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
627 off = IMM;
628load_byte:
629 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
630 if (likely(ptr != NULL)) {
631 BPF_R0 = *(u8 *)ptr;
632 CONT;
633 }
634
635 return 0;
636 LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
637 off = IMM + SRC;
638 goto load_word;
639 LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
640 off = IMM + SRC;
641 goto load_half;
642 LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
643 off = IMM + SRC;
644 goto load_byte;
645
646 default_label:
647 /* If we ever reach this, we have a bug somewhere. */
648 WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
649 return 0;
650}
Josh Poimboeuf39853cc2016-02-28 22:22:37 -0600651STACK_FRAME_NON_STANDARD(__bpf_prog_run); /* jump table */
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700652
Daniel Borkmann3324b582015-05-29 23:23:07 +0200653bool bpf_prog_array_compatible(struct bpf_array *array,
654 const struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700655{
Daniel Borkmann3324b582015-05-29 23:23:07 +0200656 if (!array->owner_prog_type) {
657 /* There's no owner yet where we could check for
658 * compatibility.
659 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700660 array->owner_prog_type = fp->type;
661 array->owner_jited = fp->jited;
Daniel Borkmann3324b582015-05-29 23:23:07 +0200662
663 return true;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700664 }
Daniel Borkmann3324b582015-05-29 23:23:07 +0200665
666 return array->owner_prog_type == fp->type &&
667 array->owner_jited == fp->jited;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700668}
669
Daniel Borkmann3324b582015-05-29 23:23:07 +0200670static int bpf_check_tail_call(const struct bpf_prog *fp)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700671{
672 struct bpf_prog_aux *aux = fp->aux;
673 int i;
674
675 for (i = 0; i < aux->used_map_cnt; i++) {
Daniel Borkmann3324b582015-05-29 23:23:07 +0200676 struct bpf_map *map = aux->used_maps[i];
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700677 struct bpf_array *array;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700678
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700679 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
680 continue;
Daniel Borkmann3324b582015-05-29 23:23:07 +0200681
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700682 array = container_of(map, struct bpf_array, map);
683 if (!bpf_prog_array_compatible(array, fp))
684 return -EINVAL;
685 }
686
687 return 0;
688}
689
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700690/**
Daniel Borkmann3324b582015-05-29 23:23:07 +0200691 * bpf_prog_select_runtime - select exec runtime for BPF program
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700692 * @fp: bpf_prog populated with internal BPF program
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700693 *
Daniel Borkmann3324b582015-05-29 23:23:07 +0200694 * Try to JIT eBPF program, if JIT is not available, use interpreter.
695 * The BPF program will be executed via BPF_PROG_RUN() macro.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700696 */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700697int bpf_prog_select_runtime(struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700698{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700699 fp->bpf_func = (void *) __bpf_prog_run;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700700
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700701 bpf_int_jit_compile(fp);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200702 bpf_prog_lock_ro(fp);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700703
Daniel Borkmann3324b582015-05-29 23:23:07 +0200704 /* The tail call compatibility check can only be done at
705 * this late stage as we need to determine, if we deal
706 * with JITed or non JITed program concatenations and not
707 * all eBPF JITs might immediately support all features.
708 */
709 return bpf_check_tail_call(fp);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700710}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700711EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700712
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200713static void bpf_prog_free_deferred(struct work_struct *work)
714{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700715 struct bpf_prog_aux *aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200716
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700717 aux = container_of(work, struct bpf_prog_aux, work);
718 bpf_jit_free(aux->prog);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200719}
720
721/* Free internal BPF program */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700722void bpf_prog_free(struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700723{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700724 struct bpf_prog_aux *aux = fp->aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200725
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700726 INIT_WORK(&aux->work, bpf_prog_free_deferred);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700727 schedule_work(&aux->work);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700728}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700729EXPORT_SYMBOL_GPL(bpf_prog_free);
Alexei Starovoitovf89b7752014-10-23 18:41:08 -0700730
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200731/* RNG for unpriviledged user space with separated state from prandom_u32(). */
732static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
733
734void bpf_user_rnd_init_once(void)
735{
736 prandom_init_once(&bpf_user_rnd_state);
737}
738
739u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
740{
741 /* Should someone ever have the rather unwise idea to use some
742 * of the registers passed into this function, then note that
743 * this function is called from native eBPF and classic-to-eBPF
744 * transformations. Register assignments from both sides are
745 * different, f.e. classic always sets fn(ctx, A, X) here.
746 */
747 struct rnd_state *state;
748 u32 res;
749
750 state = &get_cpu_var(bpf_user_rnd_state);
751 res = prandom_u32_state(state);
752 put_cpu_var(state);
753
754 return res;
755}
756
Daniel Borkmann3ba67da2015-03-05 23:27:51 +0100757/* Weak definitions of helper functions in case we don't have bpf syscall. */
758const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
759const struct bpf_func_proto bpf_map_update_elem_proto __weak;
760const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
761
Daniel Borkmann03e69b52015-03-14 02:27:16 +0100762const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
Daniel Borkmannc04167c2015-03-14 02:27:17 +0100763const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +0200764const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200765
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700766const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
767const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
768const struct bpf_func_proto bpf_get_current_comm_proto __weak;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200769
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700770const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
771{
772 return NULL;
773}
Daniel Borkmann03e69b52015-03-14 02:27:16 +0100774
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200775const struct bpf_func_proto * __weak bpf_get_event_output_proto(void)
776{
777 return NULL;
778}
779
Daniel Borkmann3324b582015-05-29 23:23:07 +0200780/* Always built-in helper functions. */
781const struct bpf_func_proto bpf_tail_call_proto = {
782 .func = NULL,
783 .gpl_only = false,
784 .ret_type = RET_VOID,
785 .arg1_type = ARG_PTR_TO_CTX,
786 .arg2_type = ARG_CONST_MAP_PTR,
787 .arg3_type = ARG_ANYTHING,
788};
789
790/* For classic BPF JITs that don't implement bpf_int_jit_compile(). */
791void __weak bpf_int_jit_compile(struct bpf_prog *prog)
792{
793}
794
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700795bool __weak bpf_helper_changes_skb_data(void *func)
796{
797 return false;
798}
799
Alexei Starovoitovf89b7752014-10-23 18:41:08 -0700800/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
801 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
802 */
803int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
804 int len)
805{
806 return -EFAULT;
807}