Merge branch 'cxgb4-next'
[linux-2.6-block.git] / include / linux / filter.h
CommitLineData
1da177e4
LT
1/*
2 * Linux Socket Filter Data Structures
3 */
1da177e4
LT
4#ifndef __LINUX_FILTER_H__
5#define __LINUX_FILTER_H__
6
60063497 7#include <linux/atomic.h>
0c5fe1b4 8#include <linux/compat.h>
9f12fbe6 9#include <linux/skbuff.h>
d45ed4a4 10#include <linux/workqueue.h>
607ca46e 11#include <uapi/linux/filter.h>
60a3b225 12#include <asm/cacheflush.h>
daedfb22 13#include <uapi/linux/bpf.h>
60a3b225
DB
14
15struct sk_buff;
16struct sock;
17struct seccomp_data;
792d4b5c 18
30743837
DB
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
bd4cf0ed
AS
35
36/* BPF program can access up to 512 bytes of stack space. */
37#define MAX_BPF_STACK 512
38
f8f6d679
DB
39/* Helper macros for filter block array initializers. */
40
e430f34e 41/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
f8f6d679 42
e430f34e 43#define BPF_ALU64_REG(OP, DST, SRC) \
2695fb55 44 ((struct bpf_insn) { \
f8f6d679 45 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
e430f34e
AS
46 .dst_reg = DST, \
47 .src_reg = SRC, \
f8f6d679
DB
48 .off = 0, \
49 .imm = 0 })
50
e430f34e 51#define BPF_ALU32_REG(OP, DST, SRC) \
2695fb55 52 ((struct bpf_insn) { \
f8f6d679 53 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
e430f34e
AS
54 .dst_reg = DST, \
55 .src_reg = SRC, \
f8f6d679
DB
56 .off = 0, \
57 .imm = 0 })
58
e430f34e 59/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
f8f6d679 60
e430f34e 61#define BPF_ALU64_IMM(OP, DST, IMM) \
2695fb55 62 ((struct bpf_insn) { \
f8f6d679 63 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
e430f34e
AS
64 .dst_reg = DST, \
65 .src_reg = 0, \
f8f6d679
DB
66 .off = 0, \
67 .imm = IMM })
68
e430f34e 69#define BPF_ALU32_IMM(OP, DST, IMM) \
2695fb55 70 ((struct bpf_insn) { \
f8f6d679 71 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
e430f34e
AS
72 .dst_reg = DST, \
73 .src_reg = 0, \
f8f6d679
DB
74 .off = 0, \
75 .imm = IMM })
76
77/* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
78
e430f34e 79#define BPF_ENDIAN(TYPE, DST, LEN) \
2695fb55 80 ((struct bpf_insn) { \
f8f6d679 81 .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
e430f34e
AS
82 .dst_reg = DST, \
83 .src_reg = 0, \
f8f6d679
DB
84 .off = 0, \
85 .imm = LEN })
86
e430f34e 87/* Short form of mov, dst_reg = src_reg */
f8f6d679 88
e430f34e 89#define BPF_MOV64_REG(DST, SRC) \
2695fb55 90 ((struct bpf_insn) { \
f8f6d679 91 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
e430f34e
AS
92 .dst_reg = DST, \
93 .src_reg = SRC, \
f8f6d679
DB
94 .off = 0, \
95 .imm = 0 })
96
e430f34e 97#define BPF_MOV32_REG(DST, SRC) \
2695fb55 98 ((struct bpf_insn) { \
f8f6d679 99 .code = BPF_ALU | BPF_MOV | BPF_X, \
e430f34e
AS
100 .dst_reg = DST, \
101 .src_reg = SRC, \
f8f6d679
DB
102 .off = 0, \
103 .imm = 0 })
104
e430f34e 105/* Short form of mov, dst_reg = imm32 */
f8f6d679 106
e430f34e 107#define BPF_MOV64_IMM(DST, IMM) \
2695fb55 108 ((struct bpf_insn) { \
f8f6d679 109 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
e430f34e
AS
110 .dst_reg = DST, \
111 .src_reg = 0, \
f8f6d679
DB
112 .off = 0, \
113 .imm = IMM })
114
e430f34e 115#define BPF_MOV32_IMM(DST, IMM) \
2695fb55 116 ((struct bpf_insn) { \
f8f6d679 117 .code = BPF_ALU | BPF_MOV | BPF_K, \
e430f34e
AS
118 .dst_reg = DST, \
119 .src_reg = 0, \
f8f6d679
DB
120 .off = 0, \
121 .imm = IMM })
122
02ab695b
AS
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
e430f34e 141/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
f8f6d679 142
e430f34e 143#define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
2695fb55 144 ((struct bpf_insn) { \
f8f6d679 145 .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
e430f34e
AS
146 .dst_reg = DST, \
147 .src_reg = SRC, \
f8f6d679
DB
148 .off = 0, \
149 .imm = IMM })
150
e430f34e 151#define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
2695fb55 152 ((struct bpf_insn) { \
f8f6d679 153 .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
e430f34e
AS
154 .dst_reg = DST, \
155 .src_reg = SRC, \
f8f6d679
DB
156 .off = 0, \
157 .imm = IMM })
158
e430f34e 159/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
f8f6d679 160
e430f34e 161#define BPF_LD_ABS(SIZE, IMM) \
2695fb55 162 ((struct bpf_insn) { \
f8f6d679 163 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
e430f34e
AS
164 .dst_reg = 0, \
165 .src_reg = 0, \
f8f6d679 166 .off = 0, \
e430f34e 167 .imm = IMM })
f8f6d679 168
e430f34e 169/* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
f8f6d679 170
e430f34e 171#define BPF_LD_IND(SIZE, SRC, IMM) \
2695fb55 172 ((struct bpf_insn) { \
f8f6d679 173 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
e430f34e
AS
174 .dst_reg = 0, \
175 .src_reg = SRC, \
f8f6d679 176 .off = 0, \
e430f34e 177 .imm = IMM })
f8f6d679 178
e430f34e 179/* Memory load, dst_reg = *(uint *) (src_reg + off16) */
f8f6d679 180
e430f34e 181#define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
2695fb55 182 ((struct bpf_insn) { \
f8f6d679 183 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
e430f34e
AS
184 .dst_reg = DST, \
185 .src_reg = SRC, \
f8f6d679
DB
186 .off = OFF, \
187 .imm = 0 })
188
e430f34e
AS
189/* Memory store, *(uint *) (dst_reg + off16) = src_reg */
190
191#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
2695fb55 192 ((struct bpf_insn) { \
f8f6d679 193 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
e430f34e
AS
194 .dst_reg = DST, \
195 .src_reg = SRC, \
f8f6d679
DB
196 .off = OFF, \
197 .imm = 0 })
198
e430f34e
AS
199/* Memory store, *(uint *) (dst_reg + off16) = imm32 */
200
201#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
2695fb55 202 ((struct bpf_insn) { \
e430f34e
AS
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 */
f8f6d679 210
e430f34e 211#define BPF_JMP_REG(OP, DST, SRC, OFF) \
2695fb55 212 ((struct bpf_insn) { \
f8f6d679 213 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
e430f34e
AS
214 .dst_reg = DST, \
215 .src_reg = SRC, \
f8f6d679
DB
216 .off = OFF, \
217 .imm = 0 })
218
e430f34e 219/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
f8f6d679 220
e430f34e 221#define BPF_JMP_IMM(OP, DST, IMM, OFF) \
2695fb55 222 ((struct bpf_insn) { \
f8f6d679 223 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
e430f34e
AS
224 .dst_reg = DST, \
225 .src_reg = 0, \
f8f6d679
DB
226 .off = OFF, \
227 .imm = IMM })
228
229/* Function call */
230
231#define BPF_EMIT_CALL(FUNC) \
2695fb55 232 ((struct bpf_insn) { \
f8f6d679 233 .code = BPF_JMP | BPF_CALL, \
e430f34e
AS
234 .dst_reg = 0, \
235 .src_reg = 0, \
f8f6d679
DB
236 .off = 0, \
237 .imm = ((FUNC) - __bpf_call_base) })
238
239/* Raw code statement block */
240
e430f34e 241#define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
2695fb55 242 ((struct bpf_insn) { \
f8f6d679 243 .code = CODE, \
e430f34e
AS
244 .dst_reg = DST, \
245 .src_reg = SRC, \
f8f6d679
DB
246 .off = OFF, \
247 .imm = IMM })
248
249/* Program exit */
250
251#define BPF_EXIT_INSN() \
2695fb55 252 ((struct bpf_insn) { \
f8f6d679 253 .code = BPF_JMP | BPF_EXIT, \
e430f34e
AS
254 .dst_reg = 0, \
255 .src_reg = 0, \
f8f6d679
DB
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})
9739eef1 274
30743837 275/* Macro to invoke filter function. */
7ae457c1
AS
276#define SK_RUN_FILTER(filter, ctx) \
277 (*filter->prog->bpf_func)(ctx, filter->prog->insnsi)
bd4cf0ed 278
bd4cf0ed
AS
279#ifdef CONFIG_COMPAT
280/* A struct sock_filter is architecture independent. */
0c5fe1b4
WD
281struct compat_sock_fprog {
282 u16 len;
bd4cf0ed 283 compat_uptr_t filter; /* struct sock_filter * */
0c5fe1b4
WD
284};
285#endif
286
a3ea269b
DB
287struct sock_fprog_kern {
288 u16 len;
289 struct sock_filter *filter;
290};
291
738cbe72
DB
292struct bpf_binary_header {
293 unsigned int pages;
294 u8 image[];
295};
296
60a3b225
DB
297struct bpf_work_struct {
298 struct bpf_prog *prog;
299 struct work_struct work;
300};
792d4b5c 301
7ae457c1 302struct bpf_prog {
286aad3c
DB
303 u16 pages; /* Number of allocated pages */
304 bool jited; /* Is our filter JIT'ed? */
305 u32 len; /* Number of filter blocks */
a3ea269b 306 struct sock_fprog_kern *orig_prog; /* Original BPF program */
60a3b225 307 struct bpf_work_struct *work; /* Deferred free work struct */
0a14842f 308 unsigned int (*bpf_func)(const struct sk_buff *skb,
2695fb55 309 const struct bpf_insn *filter);
60a3b225 310 /* Instructions for interpreter */
d45ed4a4 311 union {
bd4cf0ed 312 struct sock_filter insns[0];
2695fb55 313 struct bpf_insn insnsi[0];
d45ed4a4 314 };
b715631f
SH
315};
316
7ae457c1
AS
317struct sk_filter {
318 atomic_t refcnt;
319 struct rcu_head rcu;
320 struct bpf_prog *prog;
321};
322
323#define BPF_PROG_RUN(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
324
325static inline unsigned int bpf_prog_size(unsigned int proglen)
b715631f 326{
7ae457c1
AS
327 return max(sizeof(struct bpf_prog),
328 offsetof(struct bpf_prog, insns[proglen]));
b715631f
SH
329}
330
009937e7 331#define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
a3ea269b 332
60a3b225
DB
333#ifdef CONFIG_DEBUG_SET_MODULE_RONX
334static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
335{
336 set_memory_ro((unsigned long)fp, fp->pages);
337}
338
339static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
340{
341 set_memory_rw((unsigned long)fp, fp->pages);
342}
343#else
344static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
345{
346}
347
348static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
349{
350}
351#endif /* CONFIG_DEBUG_SET_MODULE_RONX */
352
fbc907f0 353int sk_filter(struct sock *sk, struct sk_buff *skb);
bd4cf0ed 354
7ae457c1
AS
355void bpf_prog_select_runtime(struct bpf_prog *fp);
356void bpf_prog_free(struct bpf_prog *fp);
bd4cf0ed 357
8fb575ca
AS
358int bpf_convert_filter(struct sock_filter *prog, int len,
359 struct bpf_insn *new_prog, int *new_len);
a3ea269b 360
60a3b225
DB
361struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
362struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
363 gfp_t gfp_extra_flags);
364void __bpf_prog_free(struct bpf_prog *fp);
365
738cbe72
DB
366typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
367
368struct bpf_binary_header *
369bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
370 unsigned int alignment,
371 bpf_jit_fill_hole_t bpf_fill_ill_insns);
372void bpf_jit_binary_free(struct bpf_binary_header *hdr);
373
60a3b225
DB
374static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
375{
376 bpf_prog_unlock_ro(fp);
377 __bpf_prog_free(fp);
378}
379
7ae457c1
AS
380int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
381void bpf_prog_destroy(struct bpf_prog *fp);
a3ea269b 382
fbc907f0
DB
383int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
384int sk_detach_filter(struct sock *sk);
a3ea269b 385
4df95ff4 386int bpf_check_classic(const struct sock_filter *filter, unsigned int flen);
fbc907f0
DB
387int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
388 unsigned int len);
fbc907f0 389
278571ba 390bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
fbc907f0 391void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
0a14842f 392
62258278 393u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
7ae457c1 394void bpf_int_jit_compile(struct bpf_prog *fp);
62258278 395
34805931
DB
396#define BPF_ANC BIT(15)
397
398static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
399{
400 BUG_ON(ftest->code & BPF_ANC);
401
402 switch (ftest->code) {
403 case BPF_LD | BPF_W | BPF_ABS:
404 case BPF_LD | BPF_H | BPF_ABS:
405 case BPF_LD | BPF_B | BPF_ABS:
406#define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
407 return BPF_ANC | SKF_AD_##CODE
408 switch (ftest->k) {
409 BPF_ANCILLARY(PROTOCOL);
410 BPF_ANCILLARY(PKTTYPE);
411 BPF_ANCILLARY(IFINDEX);
412 BPF_ANCILLARY(NLATTR);
413 BPF_ANCILLARY(NLATTR_NEST);
414 BPF_ANCILLARY(MARK);
415 BPF_ANCILLARY(QUEUE);
416 BPF_ANCILLARY(HATYPE);
417 BPF_ANCILLARY(RXHASH);
418 BPF_ANCILLARY(CPU);
419 BPF_ANCILLARY(ALU_XOR_X);
420 BPF_ANCILLARY(VLAN_TAG);
421 BPF_ANCILLARY(VLAN_TAG_PRESENT);
422 BPF_ANCILLARY(PAY_OFFSET);
423 BPF_ANCILLARY(RANDOM);
424 }
425 /* Fallthrough. */
426 default:
427 return ftest->code;
428 }
429}
430
9f12fbe6
ZSL
431void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
432 int k, unsigned int size);
433
434static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
435 unsigned int size, void *buffer)
436{
437 if (k >= 0)
438 return skb_header_pointer(skb, k, size, buffer);
439
440 return bpf_internal_load_pointer_neg_helper(skb, k, size);
441}
442
0a14842f 443#ifdef CONFIG_BPF_JIT
20074f35 444#include <stdarg.h>
a691ce7f
CG
445#include <linux/linkage.h>
446#include <linux/printk.h>
447
7ae457c1
AS
448void bpf_jit_compile(struct bpf_prog *fp);
449void bpf_jit_free(struct bpf_prog *fp);
79617801
DB
450
451static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
452 u32 pass, void *image)
453{
16495445 454 pr_err("flen=%u proglen=%u pass=%u image=%pK\n",
79617801
DB
455 flen, proglen, pass, image);
456 if (image)
16495445 457 print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
79617801
DB
458 16, 1, image, proglen, false);
459}
0a14842f 460#else
d45ed4a4 461#include <linux/slab.h>
34805931 462
7ae457c1 463static inline void bpf_jit_compile(struct bpf_prog *fp)
0a14842f
ED
464{
465}
34805931 466
7ae457c1 467static inline void bpf_jit_free(struct bpf_prog *fp)
0a14842f 468{
60a3b225 469 bpf_prog_unlock_free(fp);
0a14842f 470}
34805931 471#endif /* CONFIG_BPF_JIT */
0a14842f 472
ea02f941
MS
473static inline int bpf_tell_extensions(void)
474{
37692299 475 return SKF_AD_MAX;
ea02f941
MS
476}
477
1da177e4 478#endif /* __LINUX_FILTER_H__ */