vrf: fix a kernel warning
[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
b954d834
DB
7#include <stdarg.h>
8
60063497 9#include <linux/atomic.h>
0c5fe1b4 10#include <linux/compat.h>
9f12fbe6 11#include <linux/skbuff.h>
b954d834
DB
12#include <linux/linkage.h>
13#include <linux/printk.h>
d45ed4a4 14#include <linux/workqueue.h>
b13138ef 15#include <linux/sched.h>
b954d834 16
60a3b225 17#include <asm/cacheflush.h>
b954d834
DB
18
19#include <uapi/linux/filter.h>
daedfb22 20#include <uapi/linux/bpf.h>
60a3b225
DB
21
22struct sk_buff;
23struct sock;
24struct seccomp_data;
09756af4 25struct bpf_prog_aux;
792d4b5c 26
30743837
DB
27/* ArgX, context and stack frame pointer register positions. Note,
28 * Arg1, Arg2, Arg3, etc are used as argument mappings of function
29 * calls in BPF_CALL instruction.
30 */
31#define BPF_REG_ARG1 BPF_REG_1
32#define BPF_REG_ARG2 BPF_REG_2
33#define BPF_REG_ARG3 BPF_REG_3
34#define BPF_REG_ARG4 BPF_REG_4
35#define BPF_REG_ARG5 BPF_REG_5
36#define BPF_REG_CTX BPF_REG_6
37#define BPF_REG_FP BPF_REG_10
38
39/* Additional register mappings for converted user programs. */
40#define BPF_REG_A BPF_REG_0
41#define BPF_REG_X BPF_REG_7
42#define BPF_REG_TMP BPF_REG_8
bd4cf0ed
AS
43
44/* BPF program can access up to 512 bytes of stack space. */
45#define MAX_BPF_STACK 512
46
f8f6d679
DB
47/* Helper macros for filter block array initializers. */
48
e430f34e 49/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
f8f6d679 50
e430f34e 51#define BPF_ALU64_REG(OP, DST, SRC) \
2695fb55 52 ((struct bpf_insn) { \
f8f6d679 53 .code = BPF_ALU64 | 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#define BPF_ALU32_REG(OP, DST, SRC) \
2695fb55 60 ((struct bpf_insn) { \
f8f6d679 61 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
e430f34e
AS
62 .dst_reg = DST, \
63 .src_reg = SRC, \
f8f6d679
DB
64 .off = 0, \
65 .imm = 0 })
66
e430f34e 67/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
f8f6d679 68
e430f34e 69#define BPF_ALU64_IMM(OP, DST, IMM) \
2695fb55 70 ((struct bpf_insn) { \
f8f6d679 71 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
e430f34e
AS
72 .dst_reg = DST, \
73 .src_reg = 0, \
f8f6d679
DB
74 .off = 0, \
75 .imm = IMM })
76
e430f34e 77#define BPF_ALU32_IMM(OP, DST, IMM) \
2695fb55 78 ((struct bpf_insn) { \
f8f6d679 79 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
e430f34e
AS
80 .dst_reg = DST, \
81 .src_reg = 0, \
f8f6d679
DB
82 .off = 0, \
83 .imm = IMM })
84
85/* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
86
e430f34e 87#define BPF_ENDIAN(TYPE, DST, LEN) \
2695fb55 88 ((struct bpf_insn) { \
f8f6d679 89 .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
e430f34e
AS
90 .dst_reg = DST, \
91 .src_reg = 0, \
f8f6d679
DB
92 .off = 0, \
93 .imm = LEN })
94
e430f34e 95/* Short form of mov, dst_reg = src_reg */
f8f6d679 96
e430f34e 97#define BPF_MOV64_REG(DST, SRC) \
2695fb55 98 ((struct bpf_insn) { \
f8f6d679 99 .code = BPF_ALU64 | 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#define BPF_MOV32_REG(DST, SRC) \
2695fb55 106 ((struct bpf_insn) { \
f8f6d679 107 .code = BPF_ALU | BPF_MOV | BPF_X, \
e430f34e
AS
108 .dst_reg = DST, \
109 .src_reg = SRC, \
f8f6d679
DB
110 .off = 0, \
111 .imm = 0 })
112
e430f34e 113/* Short form of mov, dst_reg = imm32 */
f8f6d679 114
e430f34e 115#define BPF_MOV64_IMM(DST, IMM) \
2695fb55 116 ((struct bpf_insn) { \
f8f6d679 117 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
e430f34e
AS
118 .dst_reg = DST, \
119 .src_reg = 0, \
f8f6d679
DB
120 .off = 0, \
121 .imm = IMM })
122
e430f34e 123#define BPF_MOV32_IMM(DST, IMM) \
2695fb55 124 ((struct bpf_insn) { \
f8f6d679 125 .code = BPF_ALU | BPF_MOV | BPF_K, \
e430f34e
AS
126 .dst_reg = DST, \
127 .src_reg = 0, \
f8f6d679
DB
128 .off = 0, \
129 .imm = IMM })
130
02ab695b
AS
131/* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
132#define BPF_LD_IMM64(DST, IMM) \
133 BPF_LD_IMM64_RAW(DST, 0, IMM)
134
135#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
136 ((struct bpf_insn) { \
137 .code = BPF_LD | BPF_DW | BPF_IMM, \
138 .dst_reg = DST, \
139 .src_reg = SRC, \
140 .off = 0, \
141 .imm = (__u32) (IMM) }), \
142 ((struct bpf_insn) { \
143 .code = 0, /* zero is reserved opcode */ \
144 .dst_reg = 0, \
145 .src_reg = 0, \
146 .off = 0, \
147 .imm = ((__u64) (IMM)) >> 32 })
148
0246e64d
AS
149/* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
150#define BPF_LD_MAP_FD(DST, MAP_FD) \
151 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
152
e430f34e 153/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
f8f6d679 154
e430f34e 155#define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
2695fb55 156 ((struct bpf_insn) { \
f8f6d679 157 .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
e430f34e
AS
158 .dst_reg = DST, \
159 .src_reg = SRC, \
f8f6d679
DB
160 .off = 0, \
161 .imm = IMM })
162
e430f34e 163#define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
2695fb55 164 ((struct bpf_insn) { \
f8f6d679 165 .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
e430f34e
AS
166 .dst_reg = DST, \
167 .src_reg = SRC, \
f8f6d679
DB
168 .off = 0, \
169 .imm = IMM })
170
e430f34e 171/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
f8f6d679 172
e430f34e 173#define BPF_LD_ABS(SIZE, IMM) \
2695fb55 174 ((struct bpf_insn) { \
f8f6d679 175 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
e430f34e
AS
176 .dst_reg = 0, \
177 .src_reg = 0, \
f8f6d679 178 .off = 0, \
e430f34e 179 .imm = IMM })
f8f6d679 180
e430f34e 181/* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
f8f6d679 182
e430f34e 183#define BPF_LD_IND(SIZE, SRC, IMM) \
2695fb55 184 ((struct bpf_insn) { \
f8f6d679 185 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
e430f34e
AS
186 .dst_reg = 0, \
187 .src_reg = SRC, \
f8f6d679 188 .off = 0, \
e430f34e 189 .imm = IMM })
f8f6d679 190
e430f34e 191/* Memory load, dst_reg = *(uint *) (src_reg + off16) */
f8f6d679 192
e430f34e 193#define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
2695fb55 194 ((struct bpf_insn) { \
f8f6d679 195 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
e430f34e
AS
196 .dst_reg = DST, \
197 .src_reg = SRC, \
f8f6d679
DB
198 .off = OFF, \
199 .imm = 0 })
200
e430f34e
AS
201/* Memory store, *(uint *) (dst_reg + off16) = src_reg */
202
203#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
2695fb55 204 ((struct bpf_insn) { \
f8f6d679 205 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
e430f34e
AS
206 .dst_reg = DST, \
207 .src_reg = SRC, \
f8f6d679
DB
208 .off = OFF, \
209 .imm = 0 })
210
cffc642d
MH
211/* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
212
213#define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
214 ((struct bpf_insn) { \
215 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
216 .dst_reg = DST, \
217 .src_reg = SRC, \
218 .off = OFF, \
219 .imm = 0 })
220
e430f34e
AS
221/* Memory store, *(uint *) (dst_reg + off16) = imm32 */
222
223#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
2695fb55 224 ((struct bpf_insn) { \
e430f34e
AS
225 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
226 .dst_reg = DST, \
227 .src_reg = 0, \
228 .off = OFF, \
229 .imm = IMM })
230
231/* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
f8f6d679 232
e430f34e 233#define BPF_JMP_REG(OP, DST, SRC, OFF) \
2695fb55 234 ((struct bpf_insn) { \
f8f6d679 235 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
e430f34e
AS
236 .dst_reg = DST, \
237 .src_reg = SRC, \
f8f6d679
DB
238 .off = OFF, \
239 .imm = 0 })
240
e430f34e 241/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
f8f6d679 242
e430f34e 243#define BPF_JMP_IMM(OP, DST, IMM, OFF) \
2695fb55 244 ((struct bpf_insn) { \
f8f6d679 245 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
e430f34e
AS
246 .dst_reg = DST, \
247 .src_reg = 0, \
f8f6d679
DB
248 .off = OFF, \
249 .imm = IMM })
250
251/* Function call */
252
253#define BPF_EMIT_CALL(FUNC) \
2695fb55 254 ((struct bpf_insn) { \
f8f6d679 255 .code = BPF_JMP | BPF_CALL, \
e430f34e
AS
256 .dst_reg = 0, \
257 .src_reg = 0, \
f8f6d679
DB
258 .off = 0, \
259 .imm = ((FUNC) - __bpf_call_base) })
260
261/* Raw code statement block */
262
e430f34e 263#define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
2695fb55 264 ((struct bpf_insn) { \
f8f6d679 265 .code = CODE, \
e430f34e
AS
266 .dst_reg = DST, \
267 .src_reg = SRC, \
f8f6d679
DB
268 .off = OFF, \
269 .imm = IMM })
270
271/* Program exit */
272
273#define BPF_EXIT_INSN() \
2695fb55 274 ((struct bpf_insn) { \
f8f6d679 275 .code = BPF_JMP | BPF_EXIT, \
e430f34e
AS
276 .dst_reg = 0, \
277 .src_reg = 0, \
f8f6d679
DB
278 .off = 0, \
279 .imm = 0 })
280
a4afd37b
DB
281/* Internal classic blocks for direct assignment */
282
283#define __BPF_STMT(CODE, K) \
284 ((struct sock_filter) BPF_STMT(CODE, K))
285
286#define __BPF_JUMP(CODE, K, JT, JF) \
287 ((struct sock_filter) BPF_JUMP(CODE, K, JT, JF))
288
f8f6d679
DB
289#define bytes_to_bpf_size(bytes) \
290({ \
291 int bpf_size = -EINVAL; \
292 \
293 if (bytes == sizeof(u8)) \
294 bpf_size = BPF_B; \
295 else if (bytes == sizeof(u16)) \
296 bpf_size = BPF_H; \
297 else if (bytes == sizeof(u32)) \
298 bpf_size = BPF_W; \
299 else if (bytes == sizeof(u64)) \
300 bpf_size = BPF_DW; \
301 \
302 bpf_size; \
303})
9739eef1 304
30743837 305/* Macro to invoke filter function. */
7ae457c1
AS
306#define SK_RUN_FILTER(filter, ctx) \
307 (*filter->prog->bpf_func)(ctx, filter->prog->insnsi)
bd4cf0ed 308
bd4cf0ed
AS
309#ifdef CONFIG_COMPAT
310/* A struct sock_filter is architecture independent. */
0c5fe1b4
WD
311struct compat_sock_fprog {
312 u16 len;
bd4cf0ed 313 compat_uptr_t filter; /* struct sock_filter * */
0c5fe1b4
WD
314};
315#endif
316
a3ea269b
DB
317struct sock_fprog_kern {
318 u16 len;
319 struct sock_filter *filter;
320};
321
738cbe72
DB
322struct bpf_binary_header {
323 unsigned int pages;
324 u8 image[];
325};
326
7ae457c1 327struct bpf_prog {
286aad3c 328 u16 pages; /* Number of allocated pages */
a91263d5
DB
329 kmemcheck_bitfield_begin(meta);
330 u16 jited:1, /* Is our filter JIT'ed? */
c46646d0
DB
331 gpl_compatible:1, /* Is filter GPL compatible? */
332 dst_needed:1; /* Do we need dst entry? */
a91263d5 333 kmemcheck_bitfield_end(meta);
286aad3c 334 u32 len; /* Number of filter blocks */
24701ece 335 enum bpf_prog_type type; /* Type of BPF program */
09756af4 336 struct bpf_prog_aux *aux; /* Auxiliary fields */
24701ece 337 struct sock_fprog_kern *orig_prog; /* Original BPF program */
0a14842f 338 unsigned int (*bpf_func)(const struct sk_buff *skb,
2695fb55 339 const struct bpf_insn *filter);
60a3b225 340 /* Instructions for interpreter */
d45ed4a4 341 union {
bd4cf0ed 342 struct sock_filter insns[0];
2695fb55 343 struct bpf_insn insnsi[0];
d45ed4a4 344 };
b715631f
SH
345};
346
7ae457c1
AS
347struct sk_filter {
348 atomic_t refcnt;
349 struct rcu_head rcu;
350 struct bpf_prog *prog;
351};
352
353#define BPF_PROG_RUN(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
354
355static inline unsigned int bpf_prog_size(unsigned int proglen)
b715631f 356{
7ae457c1
AS
357 return max(sizeof(struct bpf_prog),
358 offsetof(struct bpf_prog, insns[proglen]));
b715631f
SH
359}
360
7b36f929
DB
361static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
362{
363 /* When classic BPF programs have been loaded and the arch
364 * does not have a classic BPF JIT (anymore), they have been
365 * converted via bpf_migrate_filter() to eBPF and thus always
366 * have an unspec program type.
367 */
368 return prog->type == BPF_PROG_TYPE_UNSPEC;
369}
370
009937e7 371#define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
a3ea269b 372
60a3b225
DB
373#ifdef CONFIG_DEBUG_SET_MODULE_RONX
374static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
375{
376 set_memory_ro((unsigned long)fp, fp->pages);
377}
378
379static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
380{
381 set_memory_rw((unsigned long)fp, fp->pages);
382}
383#else
384static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
385{
386}
387
388static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
389{
390}
391#endif /* CONFIG_DEBUG_SET_MODULE_RONX */
392
fbc907f0 393int sk_filter(struct sock *sk, struct sk_buff *skb);
bd4cf0ed 394
04fd61ab 395int bpf_prog_select_runtime(struct bpf_prog *fp);
7ae457c1 396void bpf_prog_free(struct bpf_prog *fp);
bd4cf0ed 397
60a3b225
DB
398struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
399struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
400 gfp_t gfp_extra_flags);
401void __bpf_prog_free(struct bpf_prog *fp);
402
403static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
404{
405 bpf_prog_unlock_ro(fp);
406 __bpf_prog_free(fp);
407}
408
ac67eb2c
DB
409typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
410 unsigned int flen);
411
7ae457c1 412int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
ac67eb2c
DB
413int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
414 bpf_aux_classic_check_t trans);
7ae457c1 415void bpf_prog_destroy(struct bpf_prog *fp);
a3ea269b 416
fbc907f0 417int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
89aa0758 418int sk_attach_bpf(u32 ufd, struct sock *sk);
fbc907f0 419int sk_detach_filter(struct sock *sk);
fbc907f0
DB
420int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
421 unsigned int len);
fbc907f0 422
278571ba 423bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
fbc907f0 424void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
0a14842f 425
62258278 426u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
7ae457c1 427void bpf_int_jit_compile(struct bpf_prog *fp);
4e10df9a 428bool bpf_helper_changes_skb_data(void *func);
62258278 429
b954d834
DB
430#ifdef CONFIG_BPF_JIT
431typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
432
433struct bpf_binary_header *
434bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
435 unsigned int alignment,
436 bpf_jit_fill_hole_t bpf_fill_ill_insns);
437void bpf_jit_binary_free(struct bpf_binary_header *hdr);
438
439void bpf_jit_compile(struct bpf_prog *fp);
440void bpf_jit_free(struct bpf_prog *fp);
441
442static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
443 u32 pass, void *image)
444{
b13138ef
DB
445 pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen,
446 proglen, pass, image, current->comm, task_pid_nr(current));
447
b954d834
DB
448 if (image)
449 print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
450 16, 1, image, proglen, false);
451}
452#else
453static inline void bpf_jit_compile(struct bpf_prog *fp)
454{
455}
456
457static inline void bpf_jit_free(struct bpf_prog *fp)
458{
459 bpf_prog_unlock_free(fp);
460}
461#endif /* CONFIG_BPF_JIT */
462
34805931
DB
463#define BPF_ANC BIT(15)
464
465static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
466{
467 BUG_ON(ftest->code & BPF_ANC);
468
469 switch (ftest->code) {
470 case BPF_LD | BPF_W | BPF_ABS:
471 case BPF_LD | BPF_H | BPF_ABS:
472 case BPF_LD | BPF_B | BPF_ABS:
473#define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
474 return BPF_ANC | SKF_AD_##CODE
475 switch (ftest->k) {
476 BPF_ANCILLARY(PROTOCOL);
477 BPF_ANCILLARY(PKTTYPE);
478 BPF_ANCILLARY(IFINDEX);
479 BPF_ANCILLARY(NLATTR);
480 BPF_ANCILLARY(NLATTR_NEST);
481 BPF_ANCILLARY(MARK);
482 BPF_ANCILLARY(QUEUE);
483 BPF_ANCILLARY(HATYPE);
484 BPF_ANCILLARY(RXHASH);
485 BPF_ANCILLARY(CPU);
486 BPF_ANCILLARY(ALU_XOR_X);
487 BPF_ANCILLARY(VLAN_TAG);
488 BPF_ANCILLARY(VLAN_TAG_PRESENT);
489 BPF_ANCILLARY(PAY_OFFSET);
490 BPF_ANCILLARY(RANDOM);
27cd5452 491 BPF_ANCILLARY(VLAN_TPID);
34805931
DB
492 }
493 /* Fallthrough. */
494 default:
495 return ftest->code;
496 }
497}
498
9f12fbe6
ZSL
499void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
500 int k, unsigned int size);
501
502static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
503 unsigned int size, void *buffer)
504{
505 if (k >= 0)
506 return skb_header_pointer(skb, k, size, buffer);
507
508 return bpf_internal_load_pointer_neg_helper(skb, k, size);
509}
510
ea02f941
MS
511static inline int bpf_tell_extensions(void)
512{
37692299 513 return SKF_AD_MAX;
ea02f941
MS
514}
515
1da177e4 516#endif /* __LINUX_FILTER_H__ */