blob: 7977b3958e25321435fc5fad48c66ace242215a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux Socket Filter Data Structures
3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#ifndef __LINUX_FILTER_H__
5#define __LINUX_FILTER_H__
6
Arun Sharma600634972011-07-26 16:09:06 -07007#include <linux/atomic.h>
Will Drewry0c5fe1b2012-04-12 16:47:53 -05008#include <linux/compat.h>
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -07009#include <linux/workqueue.h>
David Howells607ca462012-10-13 10:46:48 +010010#include <uapi/linux/filter.h>
Heiko Carstens792d4b52011-05-22 07:08:11 +000011
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010012/* Internally used and optimized filter representation with extended
13 * instruction set based on top of classic BPF.
Will Drewry0c5fe1b2012-04-12 16:47:53 -050014 */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010015
16/* instruction classes */
17#define BPF_ALU64 0x07 /* alu mode in double word width */
18
19/* ld/ldx fields */
20#define BPF_DW 0x18 /* double word */
21#define BPF_XADD 0xc0 /* exclusive add */
22
23/* alu/jmp fields */
24#define BPF_MOV 0xb0 /* mov reg to reg */
25#define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */
26
27/* change endianness of a register */
28#define BPF_END 0xd0 /* flags for endianness conversion: */
29#define BPF_TO_LE 0x00 /* convert to little-endian */
30#define BPF_TO_BE 0x08 /* convert to big-endian */
31#define BPF_FROM_LE BPF_TO_LE
32#define BPF_FROM_BE BPF_TO_BE
33
34#define BPF_JNE 0x50 /* jump != */
35#define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */
36#define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */
37#define BPF_CALL 0x80 /* function call */
38#define BPF_EXIT 0x90 /* function return */
39
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +020040/* Placeholder/dummy for 0 */
41#define BPF_0 0
42
Daniel Borkmann30743832014-05-01 18:34:19 +020043/* Register numbers */
44enum {
45 BPF_REG_0 = 0,
46 BPF_REG_1,
47 BPF_REG_2,
48 BPF_REG_3,
49 BPF_REG_4,
50 BPF_REG_5,
51 BPF_REG_6,
52 BPF_REG_7,
53 BPF_REG_8,
54 BPF_REG_9,
55 BPF_REG_10,
56 __MAX_BPF_REG,
57};
58
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010059/* BPF has 10 general purpose 64-bit registers and stack frame. */
Daniel Borkmann30743832014-05-01 18:34:19 +020060#define MAX_BPF_REG __MAX_BPF_REG
61
62/* ArgX, context and stack frame pointer register positions. Note,
63 * Arg1, Arg2, Arg3, etc are used as argument mappings of function
64 * calls in BPF_CALL instruction.
65 */
66#define BPF_REG_ARG1 BPF_REG_1
67#define BPF_REG_ARG2 BPF_REG_2
68#define BPF_REG_ARG3 BPF_REG_3
69#define BPF_REG_ARG4 BPF_REG_4
70#define BPF_REG_ARG5 BPF_REG_5
71#define BPF_REG_CTX BPF_REG_6
72#define BPF_REG_FP BPF_REG_10
73
74/* Additional register mappings for converted user programs. */
75#define BPF_REG_A BPF_REG_0
76#define BPF_REG_X BPF_REG_7
77#define BPF_REG_TMP BPF_REG_8
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010078
79/* BPF program can access up to 512 bytes of stack space. */
80#define MAX_BPF_STACK 512
81
Alexei Starovoitov9739eef2014-05-08 14:10:51 -070082/* bpf_add|sub|...: a += x, bpf_mov: a = x */
83#define BPF_ALU64_REG(op, a, x) \
84 ((struct sock_filter_int) {BPF_ALU64|BPF_OP(op)|BPF_X, a, x, 0, 0})
85#define BPF_ALU32_REG(op, a, x) \
86 ((struct sock_filter_int) {BPF_ALU|BPF_OP(op)|BPF_X, a, x, 0, 0})
87
88/* bpf_add|sub|...: a += imm, bpf_mov: a = imm */
89#define BPF_ALU64_IMM(op, a, imm) \
90 ((struct sock_filter_int) {BPF_ALU64|BPF_OP(op)|BPF_K, a, 0, 0, imm})
91#define BPF_ALU32_IMM(op, a, imm) \
92 ((struct sock_filter_int) {BPF_ALU|BPF_OP(op)|BPF_K, a, 0, 0, imm})
93
94/* R0 = *(uint *) (skb->data + off) */
95#define BPF_LD_ABS(size, off) \
96 ((struct sock_filter_int) {BPF_LD|BPF_SIZE(size)|BPF_ABS, 0, 0, 0, off})
97
98/* R0 = *(uint *) (skb->data + x + off) */
99#define BPF_LD_IND(size, x, off) \
100 ((struct sock_filter_int) {BPF_LD|BPF_SIZE(size)|BPF_IND, 0, x, 0, off})
101
102/* a = *(uint *) (x + off) */
103#define BPF_LDX_MEM(sz, a, x, off) \
104 ((struct sock_filter_int) {BPF_LDX|BPF_SIZE(sz)|BPF_MEM, a, x, off, 0})
105
106/* if (a 'op' x) goto pc+off */
107#define BPF_JMP_REG(op, a, x, off) \
108 ((struct sock_filter_int) {BPF_JMP|BPF_OP(op)|BPF_X, a, x, off, 0})
109
110/* if (a 'op' imm) goto pc+off */
111#define BPF_JMP_IMM(op, a, imm, off) \
112 ((struct sock_filter_int) {BPF_JMP|BPF_OP(op)|BPF_K, a, 0, off, imm})
113
114#define BPF_EXIT_INSN() \
115 ((struct sock_filter_int) {BPF_JMP|BPF_EXIT, 0, 0, 0, 0})
116
117static inline int size_to_bpf(int size)
118{
119 switch (size) {
120 case 1:
121 return BPF_B;
122 case 2:
123 return BPF_H;
124 case 4:
125 return BPF_W;
126 case 8:
127 return BPF_DW;
128 default:
129 return -EINVAL;
130 }
131}
132
Daniel Borkmann30743832014-05-01 18:34:19 +0200133/* Macro to invoke filter function. */
134#define SK_RUN_FILTER(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100135
136struct sock_filter_int {
137 __u8 code; /* opcode */
138 __u8 a_reg:4; /* dest register */
139 __u8 x_reg:4; /* source register */
140 __s16 off; /* signed offset */
141 __s32 imm; /* signed immediate constant */
142};
143
144#ifdef CONFIG_COMPAT
145/* A struct sock_filter is architecture independent. */
Will Drewry0c5fe1b2012-04-12 16:47:53 -0500146struct compat_sock_fprog {
147 u16 len;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100148 compat_uptr_t filter; /* struct sock_filter * */
Will Drewry0c5fe1b2012-04-12 16:47:53 -0500149};
150#endif
151
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100152struct sock_fprog_kern {
153 u16 len;
154 struct sock_filter *filter;
155};
156
Heiko Carstens792d4b52011-05-22 07:08:11 +0000157struct sk_buff;
158struct sock;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100159struct seccomp_data;
Heiko Carstens792d4b52011-05-22 07:08:11 +0000160
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100161struct sk_filter {
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700162 atomic_t refcnt;
Daniel Borkmannf8bbbfc2014-03-28 18:58:18 +0100163 u32 jited:1, /* Is our filter JIT'ed? */
164 len:31; /* Number of filter blocks */
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100165 struct sock_fprog_kern *orig_prog; /* Original BPF program */
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700166 struct rcu_head rcu;
Eric Dumazet0a148422011-04-20 09:27:32 +0000167 unsigned int (*bpf_func)(const struct sk_buff *skb,
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100168 const struct sock_filter_int *filter);
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700169 union {
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100170 struct sock_filter insns[0];
171 struct sock_filter_int insnsi[0];
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700172 struct work_struct work;
173 };
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700174};
175
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700176static inline unsigned int sk_filter_size(unsigned int proglen)
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700177{
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700178 return max(sizeof(struct sk_filter),
179 offsetof(struct sk_filter, insns[proglen]));
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700180}
181
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100182#define sk_filter_proglen(fprog) \
183 (fprog->len * sizeof(fprog->filter[0]))
184
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100185int sk_filter(struct sock *sk, struct sk_buff *skb);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100186
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -0700187void sk_filter_select_runtime(struct sk_filter *fp);
188void sk_filter_free(struct sk_filter *fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100189
190int sk_convert_filter(struct sock_filter *prog, int len,
191 struct sock_filter_int *new_prog, int *new_len);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100192
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100193int sk_unattached_filter_create(struct sk_filter **pfp,
194 struct sock_fprog *fprog);
195void sk_unattached_filter_destroy(struct sk_filter *fp);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100196
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100197int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
198int sk_detach_filter(struct sock *sk);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100199
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100200int sk_chk_filter(struct sock_filter *filter, unsigned int flen);
201int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
202 unsigned int len);
203void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to);
204
205void sk_filter_charge(struct sock *sk, struct sk_filter *fp);
206void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
Eric Dumazet0a148422011-04-20 09:27:32 +0000207
Alexei Starovoitov62258272014-05-13 19:50:46 -0700208u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
209void bpf_int_jit_compile(struct sk_filter *fp);
210
Eric Dumazet0a148422011-04-20 09:27:32 +0000211#ifdef CONFIG_BPF_JIT
Xi Wang20074f32013-05-01 16:24:08 -0400212#include <stdarg.h>
Chen Ganga691ce72013-03-28 15:24:53 +0000213#include <linux/linkage.h>
214#include <linux/printk.h>
215
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100216void bpf_jit_compile(struct sk_filter *fp);
217void bpf_jit_free(struct sk_filter *fp);
Daniel Borkmann79617802013-03-21 22:22:03 +0100218
219static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
220 u32 pass, void *image)
221{
Eric Dumazet16495442013-05-17 16:57:37 +0000222 pr_err("flen=%u proglen=%u pass=%u image=%pK\n",
Daniel Borkmann79617802013-03-21 22:22:03 +0100223 flen, proglen, pass, image);
224 if (image)
Eric Dumazet16495442013-05-17 16:57:37 +0000225 print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
Daniel Borkmann79617802013-03-21 22:22:03 +0100226 16, 1, image, proglen, false);
227}
Eric Dumazet0a148422011-04-20 09:27:32 +0000228#else
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700229#include <linux/slab.h>
Eric Dumazet0a148422011-04-20 09:27:32 +0000230static inline void bpf_jit_compile(struct sk_filter *fp)
231{
232}
233static inline void bpf_jit_free(struct sk_filter *fp)
234{
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700235 kfree(fp);
Eric Dumazet0a148422011-04-20 09:27:32 +0000236}
Eric Dumazet0a148422011-04-20 09:27:32 +0000237#endif
238
Michal Sekletarea02f942014-01-17 17:09:45 +0100239static inline int bpf_tell_extensions(void)
240{
Daniel Borkmann37692292014-01-21 00:19:37 +0100241 return SKF_AD_MAX;
Michal Sekletarea02f942014-01-17 17:09:45 +0100242}
243
Eric Dumazet0a148422011-04-20 09:27:32 +0000244enum {
245 BPF_S_RET_K = 1,
246 BPF_S_RET_A,
247 BPF_S_ALU_ADD_K,
248 BPF_S_ALU_ADD_X,
249 BPF_S_ALU_SUB_K,
250 BPF_S_ALU_SUB_X,
251 BPF_S_ALU_MUL_K,
252 BPF_S_ALU_MUL_X,
253 BPF_S_ALU_DIV_X,
Eric Dumazetb6069a92012-09-07 22:03:35 +0000254 BPF_S_ALU_MOD_K,
255 BPF_S_ALU_MOD_X,
Eric Dumazet0a148422011-04-20 09:27:32 +0000256 BPF_S_ALU_AND_K,
257 BPF_S_ALU_AND_X,
258 BPF_S_ALU_OR_K,
259 BPF_S_ALU_OR_X,
Daniel Borkmann9e49e882012-09-24 02:23:59 +0000260 BPF_S_ALU_XOR_K,
261 BPF_S_ALU_XOR_X,
Eric Dumazet0a148422011-04-20 09:27:32 +0000262 BPF_S_ALU_LSH_K,
263 BPF_S_ALU_LSH_X,
264 BPF_S_ALU_RSH_K,
265 BPF_S_ALU_RSH_X,
266 BPF_S_ALU_NEG,
267 BPF_S_LD_W_ABS,
268 BPF_S_LD_H_ABS,
269 BPF_S_LD_B_ABS,
270 BPF_S_LD_W_LEN,
271 BPF_S_LD_W_IND,
272 BPF_S_LD_H_IND,
273 BPF_S_LD_B_IND,
274 BPF_S_LD_IMM,
275 BPF_S_LDX_W_LEN,
276 BPF_S_LDX_B_MSH,
277 BPF_S_LDX_IMM,
278 BPF_S_MISC_TAX,
279 BPF_S_MISC_TXA,
280 BPF_S_ALU_DIV_K,
281 BPF_S_LD_MEM,
282 BPF_S_LDX_MEM,
283 BPF_S_ST,
284 BPF_S_STX,
285 BPF_S_JMP_JA,
286 BPF_S_JMP_JEQ_K,
287 BPF_S_JMP_JEQ_X,
288 BPF_S_JMP_JGE_K,
289 BPF_S_JMP_JGE_X,
290 BPF_S_JMP_JGT_K,
291 BPF_S_JMP_JGT_X,
292 BPF_S_JMP_JSET_K,
293 BPF_S_JMP_JSET_X,
294 /* Ancillary data */
295 BPF_S_ANC_PROTOCOL,
296 BPF_S_ANC_PKTTYPE,
297 BPF_S_ANC_IFINDEX,
298 BPF_S_ANC_NLATTR,
299 BPF_S_ANC_NLATTR_NEST,
300 BPF_S_ANC_MARK,
301 BPF_S_ANC_QUEUE,
302 BPF_S_ANC_HATYPE,
303 BPF_S_ANC_RXHASH,
304 BPF_S_ANC_CPU,
Jiri Pirkoffe06c12012-03-31 11:01:20 +0000305 BPF_S_ANC_ALU_XOR_X,
Eric Dumazetf3335032012-10-27 02:26:17 +0000306 BPF_S_ANC_VLAN_TAG,
307 BPF_S_ANC_VLAN_TAG_PRESENT,
Daniel Borkmann3e5289d2013-03-19 06:39:31 +0000308 BPF_S_ANC_PAY_OFFSET,
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700309 BPF_S_ANC_RANDOM,
Eric Dumazet0a148422011-04-20 09:27:32 +0000310};
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312#endif /* __LINUX_FILTER_H__ */