blob: d44b25cbe4607401655a35d5b06d2d7e8541f3e5 [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 Starovoitovf5bffec2014-07-22 23:01:58 -070029#include <asm/unaligned.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070030#include <linux/bpf.h>
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -070031
32/* Registers */
33#define BPF_R0 regs[BPF_REG_0]
34#define BPF_R1 regs[BPF_REG_1]
35#define BPF_R2 regs[BPF_REG_2]
36#define BPF_R3 regs[BPF_REG_3]
37#define BPF_R4 regs[BPF_REG_4]
38#define BPF_R5 regs[BPF_REG_5]
39#define BPF_R6 regs[BPF_REG_6]
40#define BPF_R7 regs[BPF_REG_7]
41#define BPF_R8 regs[BPF_REG_8]
42#define BPF_R9 regs[BPF_REG_9]
43#define BPF_R10 regs[BPF_REG_10]
44
45/* Named registers */
46#define DST regs[insn->dst_reg]
47#define SRC regs[insn->src_reg]
48#define FP regs[BPF_REG_FP]
49#define ARG1 regs[BPF_REG_ARG1]
50#define CTX regs[BPF_REG_CTX]
51#define IMM insn->imm
52
53/* No hurry in this branch
54 *
55 * Exported for the bpf jit load helper.
56 */
57void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
58{
59 u8 *ptr = NULL;
60
61 if (k >= SKF_NET_OFF)
62 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
63 else if (k >= SKF_LL_OFF)
64 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
65 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
66 return ptr;
67
68 return NULL;
69}
70
Daniel Borkmann60a3b222014-09-02 22:53:44 +020071struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
72{
73 gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
74 gfp_extra_flags;
Alexei Starovoitov09756af2014-09-26 00:17:00 -070075 struct bpf_prog_aux *aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +020076 struct bpf_prog *fp;
77
78 size = round_up(size, PAGE_SIZE);
79 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
80 if (fp == NULL)
81 return NULL;
82
Alexei Starovoitov09756af2014-09-26 00:17:00 -070083 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
84 if (aux == NULL) {
Daniel Borkmann60a3b222014-09-02 22:53:44 +020085 vfree(fp);
86 return NULL;
87 }
88
89 fp->pages = size / PAGE_SIZE;
Alexei Starovoitov09756af2014-09-26 00:17:00 -070090 fp->aux = aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +020091
92 return fp;
93}
94EXPORT_SYMBOL_GPL(bpf_prog_alloc);
95
96struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
97 gfp_t gfp_extra_flags)
98{
99 gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
100 gfp_extra_flags;
101 struct bpf_prog *fp;
102
103 BUG_ON(fp_old == NULL);
104
105 size = round_up(size, PAGE_SIZE);
106 if (size <= fp_old->pages * PAGE_SIZE)
107 return fp_old;
108
109 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
110 if (fp != NULL) {
111 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
112 fp->pages = size / PAGE_SIZE;
113
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700114 /* We keep fp->aux from fp_old around in the new
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200115 * reallocated structure.
116 */
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700117 fp_old->aux = NULL;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200118 __bpf_prog_free(fp_old);
119 }
120
121 return fp;
122}
123EXPORT_SYMBOL_GPL(bpf_prog_realloc);
124
125void __bpf_prog_free(struct bpf_prog *fp)
126{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700127 kfree(fp->aux);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200128 vfree(fp);
129}
130EXPORT_SYMBOL_GPL(__bpf_prog_free);
131
Daniel Borkmannb954d832014-09-10 15:01:02 +0200132#ifdef CONFIG_BPF_JIT
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200133struct bpf_binary_header *
134bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
135 unsigned int alignment,
136 bpf_jit_fill_hole_t bpf_fill_ill_insns)
137{
138 struct bpf_binary_header *hdr;
139 unsigned int size, hole, start;
140
141 /* Most of BPF filters are really small, but if some of them
142 * fill a page, allow at least 128 extra bytes to insert a
143 * random section of illegal instructions.
144 */
145 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
146 hdr = module_alloc(size);
147 if (hdr == NULL)
148 return NULL;
149
150 /* Fill space with illegal/arch-dep instructions. */
151 bpf_fill_ill_insns(hdr, size);
152
153 hdr->pages = size / PAGE_SIZE;
154 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
155 PAGE_SIZE - sizeof(*hdr));
156 start = (prandom_u32() % hole) & ~(alignment - 1);
157
158 /* Leave a random number of instructions before BPF code. */
159 *image_ptr = &hdr->image[start];
160
161 return hdr;
162}
163
164void bpf_jit_binary_free(struct bpf_binary_header *hdr)
165{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030166 module_memfree(hdr);
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200167}
Daniel Borkmannb954d832014-09-10 15:01:02 +0200168#endif /* CONFIG_BPF_JIT */
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200169
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700170/* Base function for offset calculation. Needs to go into .text section,
171 * therefore keeping it non-static as well; will also be used by JITs
172 * anyway later on, so do not let the compiler omit it.
173 */
174noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
175{
176 return 0;
177}
178
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700179const struct bpf_func_proto bpf_tail_call_proto = {
180 .func = NULL,
181 .gpl_only = false,
182 .ret_type = RET_VOID,
183 .arg1_type = ARG_PTR_TO_CTX,
184 .arg2_type = ARG_CONST_MAP_PTR,
185 .arg3_type = ARG_ANYTHING,
186};
187
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700188/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700189 * __bpf_prog_run - run eBPF program on a given context
190 * @ctx: is the data we are operating on
191 * @insn: is the array of eBPF instructions
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700192 *
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700193 * Decode and execute eBPF instructions.
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700194 */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700195static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700196{
197 u64 stack[MAX_BPF_STACK / sizeof(u64)];
198 u64 regs[MAX_BPF_REG], tmp;
199 static const void *jumptable[256] = {
200 [0 ... 255] = &&default_label,
201 /* Now overwrite non-defaults ... */
202 /* 32 bit ALU operations */
203 [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
204 [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
205 [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
206 [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
207 [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
208 [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
209 [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
210 [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
211 [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
212 [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
213 [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
214 [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
215 [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
216 [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
217 [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
218 [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
219 [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
220 [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
221 [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
222 [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
223 [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
224 [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
225 [BPF_ALU | BPF_NEG] = &&ALU_NEG,
226 [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
227 [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
228 /* 64 bit ALU operations */
229 [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
230 [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
231 [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
232 [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
233 [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
234 [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
235 [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
236 [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
237 [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
238 [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
239 [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
240 [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
241 [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
242 [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
243 [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
244 [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
245 [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
246 [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
247 [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
248 [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
249 [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
250 [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
251 [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
252 [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
253 [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
254 /* Call instruction */
255 [BPF_JMP | BPF_CALL] = &&JMP_CALL,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700256 [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700257 /* Jumps */
258 [BPF_JMP | BPF_JA] = &&JMP_JA,
259 [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
260 [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
261 [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
262 [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
263 [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
264 [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
265 [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
266 [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
267 [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
268 [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
269 [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
270 [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
271 [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
272 [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
273 /* Program return */
274 [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
275 /* Store instructions */
276 [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
277 [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
278 [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
279 [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
280 [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
281 [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
282 [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
283 [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
284 [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
285 [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
286 /* Load instructions */
287 [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
288 [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
289 [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
290 [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
291 [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
292 [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
293 [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
294 [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
295 [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
296 [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700297 [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700298 };
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700299 u32 tail_call_cnt = 0;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700300 void *ptr;
301 int off;
302
303#define CONT ({ insn++; goto select_insn; })
304#define CONT_JMP ({ insn++; goto select_insn; })
305
306 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
307 ARG1 = (u64) (unsigned long) ctx;
308
309 /* Registers used in classic BPF programs need to be reset first. */
310 regs[BPF_REG_A] = 0;
311 regs[BPF_REG_X] = 0;
312
313select_insn:
314 goto *jumptable[insn->code];
315
316 /* ALU */
317#define ALU(OPCODE, OP) \
318 ALU64_##OPCODE##_X: \
319 DST = DST OP SRC; \
320 CONT; \
321 ALU_##OPCODE##_X: \
322 DST = (u32) DST OP (u32) SRC; \
323 CONT; \
324 ALU64_##OPCODE##_K: \
325 DST = DST OP IMM; \
326 CONT; \
327 ALU_##OPCODE##_K: \
328 DST = (u32) DST OP (u32) IMM; \
329 CONT;
330
331 ALU(ADD, +)
332 ALU(SUB, -)
333 ALU(AND, &)
334 ALU(OR, |)
335 ALU(LSH, <<)
336 ALU(RSH, >>)
337 ALU(XOR, ^)
338 ALU(MUL, *)
339#undef ALU
340 ALU_NEG:
341 DST = (u32) -DST;
342 CONT;
343 ALU64_NEG:
344 DST = -DST;
345 CONT;
346 ALU_MOV_X:
347 DST = (u32) SRC;
348 CONT;
349 ALU_MOV_K:
350 DST = (u32) IMM;
351 CONT;
352 ALU64_MOV_X:
353 DST = SRC;
354 CONT;
355 ALU64_MOV_K:
356 DST = IMM;
357 CONT;
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700358 LD_IMM_DW:
359 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
360 insn++;
361 CONT;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700362 ALU64_ARSH_X:
363 (*(s64 *) &DST) >>= SRC;
364 CONT;
365 ALU64_ARSH_K:
366 (*(s64 *) &DST) >>= IMM;
367 CONT;
368 ALU64_MOD_X:
369 if (unlikely(SRC == 0))
370 return 0;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700371 div64_u64_rem(DST, SRC, &tmp);
372 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700373 CONT;
374 ALU_MOD_X:
375 if (unlikely(SRC == 0))
376 return 0;
377 tmp = (u32) DST;
378 DST = do_div(tmp, (u32) SRC);
379 CONT;
380 ALU64_MOD_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700381 div64_u64_rem(DST, IMM, &tmp);
382 DST = tmp;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700383 CONT;
384 ALU_MOD_K:
385 tmp = (u32) DST;
386 DST = do_div(tmp, (u32) IMM);
387 CONT;
388 ALU64_DIV_X:
389 if (unlikely(SRC == 0))
390 return 0;
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700391 DST = div64_u64(DST, SRC);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700392 CONT;
393 ALU_DIV_X:
394 if (unlikely(SRC == 0))
395 return 0;
396 tmp = (u32) DST;
397 do_div(tmp, (u32) SRC);
398 DST = (u32) tmp;
399 CONT;
400 ALU64_DIV_K:
Alexei Starovoitov876a7ae2015-04-27 14:40:37 -0700401 DST = div64_u64(DST, IMM);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700402 CONT;
403 ALU_DIV_K:
404 tmp = (u32) DST;
405 do_div(tmp, (u32) IMM);
406 DST = (u32) tmp;
407 CONT;
408 ALU_END_TO_BE:
409 switch (IMM) {
410 case 16:
411 DST = (__force u16) cpu_to_be16(DST);
412 break;
413 case 32:
414 DST = (__force u32) cpu_to_be32(DST);
415 break;
416 case 64:
417 DST = (__force u64) cpu_to_be64(DST);
418 break;
419 }
420 CONT;
421 ALU_END_TO_LE:
422 switch (IMM) {
423 case 16:
424 DST = (__force u16) cpu_to_le16(DST);
425 break;
426 case 32:
427 DST = (__force u32) cpu_to_le32(DST);
428 break;
429 case 64:
430 DST = (__force u64) cpu_to_le64(DST);
431 break;
432 }
433 CONT;
434
435 /* CALL */
436 JMP_CALL:
437 /* Function call scratches BPF_R1-BPF_R5 registers,
438 * preserves BPF_R6-BPF_R9, and stores return value
439 * into BPF_R0.
440 */
441 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
442 BPF_R4, BPF_R5);
443 CONT;
444
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700445 JMP_TAIL_CALL: {
446 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
447 struct bpf_array *array = container_of(map, struct bpf_array, map);
448 struct bpf_prog *prog;
449 u64 index = BPF_R3;
450
451 if (unlikely(index >= array->map.max_entries))
452 goto out;
453
454 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
455 goto out;
456
457 tail_call_cnt++;
458
459 prog = READ_ONCE(array->prog[index]);
460 if (unlikely(!prog))
461 goto out;
462
463 ARG1 = BPF_R1;
464 insn = prog->insnsi;
465 goto select_insn;
466out:
467 CONT;
468 }
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700469 /* JMP */
470 JMP_JA:
471 insn += insn->off;
472 CONT;
473 JMP_JEQ_X:
474 if (DST == SRC) {
475 insn += insn->off;
476 CONT_JMP;
477 }
478 CONT;
479 JMP_JEQ_K:
480 if (DST == IMM) {
481 insn += insn->off;
482 CONT_JMP;
483 }
484 CONT;
485 JMP_JNE_X:
486 if (DST != SRC) {
487 insn += insn->off;
488 CONT_JMP;
489 }
490 CONT;
491 JMP_JNE_K:
492 if (DST != IMM) {
493 insn += insn->off;
494 CONT_JMP;
495 }
496 CONT;
497 JMP_JGT_X:
498 if (DST > SRC) {
499 insn += insn->off;
500 CONT_JMP;
501 }
502 CONT;
503 JMP_JGT_K:
504 if (DST > IMM) {
505 insn += insn->off;
506 CONT_JMP;
507 }
508 CONT;
509 JMP_JGE_X:
510 if (DST >= SRC) {
511 insn += insn->off;
512 CONT_JMP;
513 }
514 CONT;
515 JMP_JGE_K:
516 if (DST >= IMM) {
517 insn += insn->off;
518 CONT_JMP;
519 }
520 CONT;
521 JMP_JSGT_X:
522 if (((s64) DST) > ((s64) SRC)) {
523 insn += insn->off;
524 CONT_JMP;
525 }
526 CONT;
527 JMP_JSGT_K:
528 if (((s64) DST) > ((s64) IMM)) {
529 insn += insn->off;
530 CONT_JMP;
531 }
532 CONT;
533 JMP_JSGE_X:
534 if (((s64) DST) >= ((s64) SRC)) {
535 insn += insn->off;
536 CONT_JMP;
537 }
538 CONT;
539 JMP_JSGE_K:
540 if (((s64) DST) >= ((s64) IMM)) {
541 insn += insn->off;
542 CONT_JMP;
543 }
544 CONT;
545 JMP_JSET_X:
546 if (DST & SRC) {
547 insn += insn->off;
548 CONT_JMP;
549 }
550 CONT;
551 JMP_JSET_K:
552 if (DST & IMM) {
553 insn += insn->off;
554 CONT_JMP;
555 }
556 CONT;
557 JMP_EXIT:
558 return BPF_R0;
559
560 /* STX and ST and LDX*/
561#define LDST(SIZEOP, SIZE) \
562 STX_MEM_##SIZEOP: \
563 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
564 CONT; \
565 ST_MEM_##SIZEOP: \
566 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
567 CONT; \
568 LDX_MEM_##SIZEOP: \
569 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
570 CONT;
571
572 LDST(B, u8)
573 LDST(H, u16)
574 LDST(W, u32)
575 LDST(DW, u64)
576#undef LDST
577 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
578 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
579 (DST + insn->off));
580 CONT;
581 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
582 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
583 (DST + insn->off));
584 CONT;
585 LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
586 off = IMM;
587load_word:
588 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
589 * only appearing in the programs where ctx ==
590 * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700591 * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700592 * internal BPF verifier will check that BPF_R6 ==
593 * ctx.
594 *
595 * BPF_ABS and BPF_IND are wrappers of function calls,
596 * so they scratch BPF_R1-BPF_R5 registers, preserve
597 * BPF_R6-BPF_R9, and store return value into BPF_R0.
598 *
599 * Implicit input:
600 * ctx == skb == BPF_R6 == CTX
601 *
602 * Explicit input:
603 * SRC == any register
604 * IMM == 32-bit immediate
605 *
606 * Output:
607 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
608 */
609
610 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
611 if (likely(ptr != NULL)) {
612 BPF_R0 = get_unaligned_be32(ptr);
613 CONT;
614 }
615
616 return 0;
617 LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
618 off = IMM;
619load_half:
620 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
621 if (likely(ptr != NULL)) {
622 BPF_R0 = get_unaligned_be16(ptr);
623 CONT;
624 }
625
626 return 0;
627 LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
628 off = IMM;
629load_byte:
630 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
631 if (likely(ptr != NULL)) {
632 BPF_R0 = *(u8 *)ptr;
633 CONT;
634 }
635
636 return 0;
637 LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
638 off = IMM + SRC;
639 goto load_word;
640 LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
641 off = IMM + SRC;
642 goto load_half;
643 LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
644 off = IMM + SRC;
645 goto load_byte;
646
647 default_label:
648 /* If we ever reach this, we have a bug somewhere. */
649 WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
650 return 0;
651}
652
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700653void __weak bpf_int_jit_compile(struct bpf_prog *prog)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700654{
655}
656
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700657bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp)
658{
659 if (array->owner_prog_type) {
660 if (array->owner_prog_type != fp->type)
661 return false;
662 if (array->owner_jited != fp->jited)
663 return false;
664 } else {
665 array->owner_prog_type = fp->type;
666 array->owner_jited = fp->jited;
667 }
668 return true;
669}
670
671static int check_tail_call(const struct bpf_prog *fp)
672{
673 struct bpf_prog_aux *aux = fp->aux;
674 int i;
675
676 for (i = 0; i < aux->used_map_cnt; i++) {
677 struct bpf_array *array;
678 struct bpf_map *map;
679
680 map = aux->used_maps[i];
681 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
682 continue;
683 array = container_of(map, struct bpf_array, map);
684 if (!bpf_prog_array_compatible(array, fp))
685 return -EINVAL;
686 }
687
688 return 0;
689}
690
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700691/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700692 * bpf_prog_select_runtime - select execution runtime for BPF program
693 * @fp: bpf_prog populated with internal BPF program
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700694 *
695 * try to JIT internal BPF program, if JIT is not available select interpreter
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700696 * BPF program will be executed via BPF_PROG_RUN() macro
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700697 */
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700698int bpf_prog_select_runtime(struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700699{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700700 fp->bpf_func = (void *) __bpf_prog_run;
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700701
702 /* Probe if internal BPF can be JITed */
703 bpf_int_jit_compile(fp);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200704 /* Lock whole bpf_prog as read-only */
705 bpf_prog_lock_ro(fp);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700706
707 return check_tail_call(fp);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700708}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700709EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700710
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200711static void bpf_prog_free_deferred(struct work_struct *work)
712{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700713 struct bpf_prog_aux *aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200714
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700715 aux = container_of(work, struct bpf_prog_aux, work);
716 bpf_jit_free(aux->prog);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200717}
718
719/* Free internal BPF program */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700720void bpf_prog_free(struct bpf_prog *fp)
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700721{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700722 struct bpf_prog_aux *aux = fp->aux;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200723
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700724 INIT_WORK(&aux->work, bpf_prog_free_deferred);
725 aux->prog = fp;
726 schedule_work(&aux->work);
Alexei Starovoitovf5bffec2014-07-22 23:01:58 -0700727}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700728EXPORT_SYMBOL_GPL(bpf_prog_free);
Alexei Starovoitovf89b7752014-10-23 18:41:08 -0700729
Daniel Borkmann3ba67da2015-03-05 23:27:51 +0100730/* Weak definitions of helper functions in case we don't have bpf syscall. */
731const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
732const struct bpf_func_proto bpf_map_update_elem_proto __weak;
733const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
734
Daniel Borkmann03e69b52015-03-14 02:27:16 +0100735const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
Daniel Borkmannc04167c2015-03-14 02:27:17 +0100736const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
Daniel Borkmann03e69b52015-03-14 02:27:16 +0100737
Alexei Starovoitovf89b7752014-10-23 18:41:08 -0700738/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
739 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
740 */
741int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
742 int len)
743{
744 return -EFAULT;
745}