net: filter: split filter.h and expose eBPF to user space
[cascardo/linux.git] / include / linux / filter.h
1 /*
2  * Linux Socket Filter Data Structures
3  */
4 #ifndef __LINUX_FILTER_H__
5 #define __LINUX_FILTER_H__
6
7 #include <linux/atomic.h>
8 #include <linux/compat.h>
9 #include <linux/skbuff.h>
10 #include <linux/workqueue.h>
11 #include <uapi/linux/filter.h>
12 #include <asm/cacheflush.h>
13 #include <uapi/linux/bpf.h>
14
15 struct sk_buff;
16 struct sock;
17 struct seccomp_data;
18
19 /* ArgX, context and stack frame pointer register positions. Note,
20  * Arg1, Arg2, Arg3, etc are used as argument mappings of function
21  * calls in BPF_CALL instruction.
22  */
23 #define BPF_REG_ARG1    BPF_REG_1
24 #define BPF_REG_ARG2    BPF_REG_2
25 #define BPF_REG_ARG3    BPF_REG_3
26 #define BPF_REG_ARG4    BPF_REG_4
27 #define BPF_REG_ARG5    BPF_REG_5
28 #define BPF_REG_CTX     BPF_REG_6
29 #define BPF_REG_FP      BPF_REG_10
30
31 /* Additional register mappings for converted user programs. */
32 #define BPF_REG_A       BPF_REG_0
33 #define BPF_REG_X       BPF_REG_7
34 #define BPF_REG_TMP     BPF_REG_8
35
36 /* BPF program can access up to 512 bytes of stack space. */
37 #define MAX_BPF_STACK   512
38
39 /* Helper macros for filter block array initializers. */
40
41 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
42
43 #define BPF_ALU64_REG(OP, DST, SRC)                             \
44         ((struct bpf_insn) {                                    \
45                 .code  = BPF_ALU64 | BPF_OP(OP) | BPF_X,        \
46                 .dst_reg = DST,                                 \
47                 .src_reg = SRC,                                 \
48                 .off   = 0,                                     \
49                 .imm   = 0 })
50
51 #define BPF_ALU32_REG(OP, DST, SRC)                             \
52         ((struct bpf_insn) {                                    \
53                 .code  = BPF_ALU | BPF_OP(OP) | BPF_X,          \
54                 .dst_reg = DST,                                 \
55                 .src_reg = SRC,                                 \
56                 .off   = 0,                                     \
57                 .imm   = 0 })
58
59 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
60
61 #define BPF_ALU64_IMM(OP, DST, IMM)                             \
62         ((struct bpf_insn) {                                    \
63                 .code  = BPF_ALU64 | BPF_OP(OP) | BPF_K,        \
64                 .dst_reg = DST,                                 \
65                 .src_reg = 0,                                   \
66                 .off   = 0,                                     \
67                 .imm   = IMM })
68
69 #define BPF_ALU32_IMM(OP, DST, IMM)                             \
70         ((struct bpf_insn) {                                    \
71                 .code  = BPF_ALU | BPF_OP(OP) | BPF_K,          \
72                 .dst_reg = DST,                                 \
73                 .src_reg = 0,                                   \
74                 .off   = 0,                                     \
75                 .imm   = IMM })
76
77 /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
78
79 #define BPF_ENDIAN(TYPE, DST, LEN)                              \
80         ((struct bpf_insn) {                                    \
81                 .code  = BPF_ALU | BPF_END | BPF_SRC(TYPE),     \
82                 .dst_reg = DST,                                 \
83                 .src_reg = 0,                                   \
84                 .off   = 0,                                     \
85                 .imm   = LEN })
86
87 /* Short form of mov, dst_reg = src_reg */
88
89 #define BPF_MOV64_REG(DST, SRC)                                 \
90         ((struct bpf_insn) {                                    \
91                 .code  = BPF_ALU64 | BPF_MOV | BPF_X,           \
92                 .dst_reg = DST,                                 \
93                 .src_reg = SRC,                                 \
94                 .off   = 0,                                     \
95                 .imm   = 0 })
96
97 #define BPF_MOV32_REG(DST, SRC)                                 \
98         ((struct bpf_insn) {                                    \
99                 .code  = BPF_ALU | BPF_MOV | BPF_X,             \
100                 .dst_reg = DST,                                 \
101                 .src_reg = SRC,                                 \
102                 .off   = 0,                                     \
103                 .imm   = 0 })
104
105 /* Short form of mov, dst_reg = imm32 */
106
107 #define BPF_MOV64_IMM(DST, IMM)                                 \
108         ((struct bpf_insn) {                                    \
109                 .code  = BPF_ALU64 | BPF_MOV | BPF_K,           \
110                 .dst_reg = DST,                                 \
111                 .src_reg = 0,                                   \
112                 .off   = 0,                                     \
113                 .imm   = IMM })
114
115 #define BPF_MOV32_IMM(DST, IMM)                                 \
116         ((struct bpf_insn) {                                    \
117                 .code  = BPF_ALU | BPF_MOV | BPF_K,             \
118                 .dst_reg = DST,                                 \
119                 .src_reg = 0,                                   \
120                 .off   = 0,                                     \
121                 .imm   = IMM })
122
123 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
124 #define BPF_LD_IMM64(DST, IMM)                                  \
125         BPF_LD_IMM64_RAW(DST, 0, IMM)
126
127 #define BPF_LD_IMM64_RAW(DST, SRC, IMM)                         \
128         ((struct bpf_insn) {                                    \
129                 .code  = BPF_LD | BPF_DW | BPF_IMM,             \
130                 .dst_reg = DST,                                 \
131                 .src_reg = SRC,                                 \
132                 .off   = 0,                                     \
133                 .imm   = (__u32) (IMM) }),                      \
134         ((struct bpf_insn) {                                    \
135                 .code  = 0, /* zero is reserved opcode */       \
136                 .dst_reg = 0,                                   \
137                 .src_reg = 0,                                   \
138                 .off   = 0,                                     \
139                 .imm   = ((__u64) (IMM)) >> 32 })
140
141 /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
142
143 #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM)                      \
144         ((struct bpf_insn) {                                    \
145                 .code  = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE),   \
146                 .dst_reg = DST,                                 \
147                 .src_reg = SRC,                                 \
148                 .off   = 0,                                     \
149                 .imm   = IMM })
150
151 #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM)                      \
152         ((struct bpf_insn) {                                    \
153                 .code  = BPF_ALU | BPF_MOV | BPF_SRC(TYPE),     \
154                 .dst_reg = DST,                                 \
155                 .src_reg = SRC,                                 \
156                 .off   = 0,                                     \
157                 .imm   = IMM })
158
159 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
160
161 #define BPF_LD_ABS(SIZE, IMM)                                   \
162         ((struct bpf_insn) {                                    \
163                 .code  = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS,     \
164                 .dst_reg = 0,                                   \
165                 .src_reg = 0,                                   \
166                 .off   = 0,                                     \
167                 .imm   = IMM })
168
169 /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
170
171 #define BPF_LD_IND(SIZE, SRC, IMM)                              \
172         ((struct bpf_insn) {                                    \
173                 .code  = BPF_LD | BPF_SIZE(SIZE) | BPF_IND,     \
174                 .dst_reg = 0,                                   \
175                 .src_reg = SRC,                                 \
176                 .off   = 0,                                     \
177                 .imm   = IMM })
178
179 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
180
181 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF)                        \
182         ((struct bpf_insn) {                                    \
183                 .code  = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM,    \
184                 .dst_reg = DST,                                 \
185                 .src_reg = SRC,                                 \
186                 .off   = OFF,                                   \
187                 .imm   = 0 })
188
189 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
190
191 #define BPF_STX_MEM(SIZE, DST, SRC, OFF)                        \
192         ((struct bpf_insn) {                                    \
193                 .code  = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM,    \
194                 .dst_reg = DST,                                 \
195                 .src_reg = SRC,                                 \
196                 .off   = OFF,                                   \
197                 .imm   = 0 })
198
199 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
200
201 #define BPF_ST_MEM(SIZE, DST, OFF, IMM)                         \
202         ((struct bpf_insn) {                                    \
203                 .code  = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM,     \
204                 .dst_reg = DST,                                 \
205                 .src_reg = 0,                                   \
206                 .off   = OFF,                                   \
207                 .imm   = IMM })
208
209 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
210
211 #define BPF_JMP_REG(OP, DST, SRC, OFF)                          \
212         ((struct bpf_insn) {                                    \
213                 .code  = BPF_JMP | BPF_OP(OP) | BPF_X,          \
214                 .dst_reg = DST,                                 \
215                 .src_reg = SRC,                                 \
216                 .off   = OFF,                                   \
217                 .imm   = 0 })
218
219 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
220
221 #define BPF_JMP_IMM(OP, DST, IMM, OFF)                          \
222         ((struct bpf_insn) {                                    \
223                 .code  = BPF_JMP | BPF_OP(OP) | BPF_K,          \
224                 .dst_reg = DST,                                 \
225                 .src_reg = 0,                                   \
226                 .off   = OFF,                                   \
227                 .imm   = IMM })
228
229 /* Function call */
230
231 #define BPF_EMIT_CALL(FUNC)                                     \
232         ((struct bpf_insn) {                                    \
233                 .code  = BPF_JMP | BPF_CALL,                    \
234                 .dst_reg = 0,                                   \
235                 .src_reg = 0,                                   \
236                 .off   = 0,                                     \
237                 .imm   = ((FUNC) - __bpf_call_base) })
238
239 /* Raw code statement block */
240
241 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM)                  \
242         ((struct bpf_insn) {                                    \
243                 .code  = CODE,                                  \
244                 .dst_reg = DST,                                 \
245                 .src_reg = SRC,                                 \
246                 .off   = OFF,                                   \
247                 .imm   = IMM })
248
249 /* Program exit */
250
251 #define BPF_EXIT_INSN()                                         \
252         ((struct bpf_insn) {                                    \
253                 .code  = BPF_JMP | BPF_EXIT,                    \
254                 .dst_reg = 0,                                   \
255                 .src_reg = 0,                                   \
256                 .off   = 0,                                     \
257                 .imm   = 0 })
258
259 #define bytes_to_bpf_size(bytes)                                \
260 ({                                                              \
261         int bpf_size = -EINVAL;                                 \
262                                                                 \
263         if (bytes == sizeof(u8))                                \
264                 bpf_size = BPF_B;                               \
265         else if (bytes == sizeof(u16))                          \
266                 bpf_size = BPF_H;                               \
267         else if (bytes == sizeof(u32))                          \
268                 bpf_size = BPF_W;                               \
269         else if (bytes == sizeof(u64))                          \
270                 bpf_size = BPF_DW;                              \
271                                                                 \
272         bpf_size;                                               \
273 })
274
275 /* Macro to invoke filter function. */
276 #define SK_RUN_FILTER(filter, ctx) \
277         (*filter->prog->bpf_func)(ctx, filter->prog->insnsi)
278
279 #ifdef CONFIG_COMPAT
280 /* A struct sock_filter is architecture independent. */
281 struct compat_sock_fprog {
282         u16             len;
283         compat_uptr_t   filter; /* struct sock_filter * */
284 };
285 #endif
286
287 struct sock_fprog_kern {
288         u16                     len;
289         struct sock_filter      *filter;
290 };
291
292 struct bpf_work_struct {
293         struct bpf_prog *prog;
294         struct work_struct work;
295 };
296
297 struct bpf_prog {
298         u32                     pages;          /* Number of allocated pages */
299         u32                     jited:1,        /* Is our filter JIT'ed? */
300                                 len:31;         /* Number of filter blocks */
301         struct sock_fprog_kern  *orig_prog;     /* Original BPF program */
302         struct bpf_work_struct  *work;          /* Deferred free work struct */
303         unsigned int            (*bpf_func)(const struct sk_buff *skb,
304                                             const struct bpf_insn *filter);
305         /* Instructions for interpreter */
306         union {
307                 struct sock_filter      insns[0];
308                 struct bpf_insn         insnsi[0];
309         };
310 };
311
312 struct sk_filter {
313         atomic_t        refcnt;
314         struct rcu_head rcu;
315         struct bpf_prog *prog;
316 };
317
318 #define BPF_PROG_RUN(filter, ctx)  (*filter->bpf_func)(ctx, filter->insnsi)
319
320 static inline unsigned int bpf_prog_size(unsigned int proglen)
321 {
322         return max(sizeof(struct bpf_prog),
323                    offsetof(struct bpf_prog, insns[proglen]));
324 }
325
326 #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
327
328 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
329 static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
330 {
331         set_memory_ro((unsigned long)fp, fp->pages);
332 }
333
334 static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
335 {
336         set_memory_rw((unsigned long)fp, fp->pages);
337 }
338 #else
339 static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
340 {
341 }
342
343 static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
344 {
345 }
346 #endif /* CONFIG_DEBUG_SET_MODULE_RONX */
347
348 int sk_filter(struct sock *sk, struct sk_buff *skb);
349
350 void bpf_prog_select_runtime(struct bpf_prog *fp);
351 void bpf_prog_free(struct bpf_prog *fp);
352
353 int bpf_convert_filter(struct sock_filter *prog, int len,
354                        struct bpf_insn *new_prog, int *new_len);
355
356 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
357 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
358                                   gfp_t gfp_extra_flags);
359 void __bpf_prog_free(struct bpf_prog *fp);
360
361 static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
362 {
363         bpf_prog_unlock_ro(fp);
364         __bpf_prog_free(fp);
365 }
366
367 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
368 void bpf_prog_destroy(struct bpf_prog *fp);
369
370 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
371 int sk_detach_filter(struct sock *sk);
372
373 int bpf_check_classic(const struct sock_filter *filter, unsigned int flen);
374 int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
375                   unsigned int len);
376
377 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
378 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
379
380 u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
381 void bpf_int_jit_compile(struct bpf_prog *fp);
382
383 #define BPF_ANC         BIT(15)
384
385 static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
386 {
387         BUG_ON(ftest->code & BPF_ANC);
388
389         switch (ftest->code) {
390         case BPF_LD | BPF_W | BPF_ABS:
391         case BPF_LD | BPF_H | BPF_ABS:
392         case BPF_LD | BPF_B | BPF_ABS:
393 #define BPF_ANCILLARY(CODE)     case SKF_AD_OFF + SKF_AD_##CODE:        \
394                                 return BPF_ANC | SKF_AD_##CODE
395                 switch (ftest->k) {
396                 BPF_ANCILLARY(PROTOCOL);
397                 BPF_ANCILLARY(PKTTYPE);
398                 BPF_ANCILLARY(IFINDEX);
399                 BPF_ANCILLARY(NLATTR);
400                 BPF_ANCILLARY(NLATTR_NEST);
401                 BPF_ANCILLARY(MARK);
402                 BPF_ANCILLARY(QUEUE);
403                 BPF_ANCILLARY(HATYPE);
404                 BPF_ANCILLARY(RXHASH);
405                 BPF_ANCILLARY(CPU);
406                 BPF_ANCILLARY(ALU_XOR_X);
407                 BPF_ANCILLARY(VLAN_TAG);
408                 BPF_ANCILLARY(VLAN_TAG_PRESENT);
409                 BPF_ANCILLARY(PAY_OFFSET);
410                 BPF_ANCILLARY(RANDOM);
411                 }
412                 /* Fallthrough. */
413         default:
414                 return ftest->code;
415         }
416 }
417
418 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
419                                            int k, unsigned int size);
420
421 static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
422                                      unsigned int size, void *buffer)
423 {
424         if (k >= 0)
425                 return skb_header_pointer(skb, k, size, buffer);
426
427         return bpf_internal_load_pointer_neg_helper(skb, k, size);
428 }
429
430 #ifdef CONFIG_BPF_JIT
431 #include <stdarg.h>
432 #include <linux/linkage.h>
433 #include <linux/printk.h>
434
435 void bpf_jit_compile(struct bpf_prog *fp);
436 void bpf_jit_free(struct bpf_prog *fp);
437
438 static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
439                                 u32 pass, void *image)
440 {
441         pr_err("flen=%u proglen=%u pass=%u image=%pK\n",
442                flen, proglen, pass, image);
443         if (image)
444                 print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
445                                16, 1, image, proglen, false);
446 }
447 #else
448 #include <linux/slab.h>
449
450 static inline void bpf_jit_compile(struct bpf_prog *fp)
451 {
452 }
453
454 static inline void bpf_jit_free(struct bpf_prog *fp)
455 {
456         bpf_prog_unlock_free(fp);
457 }
458 #endif /* CONFIG_BPF_JIT */
459
460 static inline int bpf_tell_extensions(void)
461 {
462         return SKF_AD_MAX;
463 }
464
465 #endif /* __LINUX_FILTER_H__ */