Commit | Line | Data |
---|---|---|
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> |
d45ed4a4 | 9 | #include <linux/workqueue.h> |
607ca46e | 10 | #include <uapi/linux/filter.h> |
792d4b5c | 11 | |
bd4cf0ed AS |
12 | /* Internally used and optimized filter representation with extended |
13 | * instruction set based on top of classic BPF. | |
0c5fe1b4 | 14 | */ |
bd4cf0ed AS |
15 | |
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 | ||
5bcfedf0 DB |
40 | /* Placeholder/dummy for 0 */ |
41 | #define BPF_0 0 | |
42 | ||
30743837 DB |
43 | /* Register numbers */ |
44 | enum { | |
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 | ||
bd4cf0ed | 59 | /* BPF has 10 general purpose 64-bit registers and stack frame. */ |
30743837 DB |
60 | #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 | |
bd4cf0ed AS |
78 | |
79 | /* BPF program can access up to 512 bytes of stack space. */ | |
80 | #define MAX_BPF_STACK 512 | |
81 | ||
9739eef1 AS |
82 | /* 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 | ||
117 | static 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 | ||
30743837 DB |
133 | /* Macro to invoke filter function. */ |
134 | #define SK_RUN_FILTER(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi) | |
bd4cf0ed AS |
135 | |
136 | struct 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. */ | |
0c5fe1b4 WD |
146 | struct compat_sock_fprog { |
147 | u16 len; | |
bd4cf0ed | 148 | compat_uptr_t filter; /* struct sock_filter * */ |
0c5fe1b4 WD |
149 | }; |
150 | #endif | |
151 | ||
a3ea269b DB |
152 | struct sock_fprog_kern { |
153 | u16 len; | |
154 | struct sock_filter *filter; | |
155 | }; | |
156 | ||
792d4b5c HC |
157 | struct sk_buff; |
158 | struct sock; | |
bd4cf0ed | 159 | struct seccomp_data; |
792d4b5c | 160 | |
a3ea269b | 161 | struct sk_filter { |
b715631f | 162 | atomic_t refcnt; |
f8bbbfc3 DB |
163 | u32 jited:1, /* Is our filter JIT'ed? */ |
164 | len:31; /* Number of filter blocks */ | |
a3ea269b | 165 | struct sock_fprog_kern *orig_prog; /* Original BPF program */ |
d45ed4a4 | 166 | struct rcu_head rcu; |
0a14842f | 167 | unsigned int (*bpf_func)(const struct sk_buff *skb, |
bd4cf0ed | 168 | const struct sock_filter_int *filter); |
d45ed4a4 | 169 | union { |
bd4cf0ed AS |
170 | struct sock_filter insns[0]; |
171 | struct sock_filter_int insnsi[0]; | |
d45ed4a4 AS |
172 | struct work_struct work; |
173 | }; | |
b715631f SH |
174 | }; |
175 | ||
d45ed4a4 | 176 | static inline unsigned int sk_filter_size(unsigned int proglen) |
b715631f | 177 | { |
d45ed4a4 AS |
178 | return max(sizeof(struct sk_filter), |
179 | offsetof(struct sk_filter, insns[proglen])); | |
b715631f SH |
180 | } |
181 | ||
a3ea269b DB |
182 | #define sk_filter_proglen(fprog) \ |
183 | (fprog->len * sizeof(fprog->filter[0])) | |
184 | ||
fbc907f0 | 185 | int sk_filter(struct sock *sk, struct sk_buff *skb); |
bd4cf0ed AS |
186 | |
187 | u32 sk_run_filter_int_seccomp(const struct seccomp_data *ctx, | |
188 | const struct sock_filter_int *insni); | |
189 | u32 sk_run_filter_int_skb(const struct sk_buff *ctx, | |
190 | const struct sock_filter_int *insni); | |
191 | ||
192 | int sk_convert_filter(struct sock_filter *prog, int len, | |
193 | struct sock_filter_int *new_prog, int *new_len); | |
a3ea269b | 194 | |
fbc907f0 DB |
195 | int sk_unattached_filter_create(struct sk_filter **pfp, |
196 | struct sock_fprog *fprog); | |
197 | void sk_unattached_filter_destroy(struct sk_filter *fp); | |
a3ea269b | 198 | |
fbc907f0 DB |
199 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk); |
200 | int sk_detach_filter(struct sock *sk); | |
a3ea269b | 201 | |
fbc907f0 DB |
202 | int sk_chk_filter(struct sock_filter *filter, unsigned int flen); |
203 | int sk_get_filter(struct sock *sk, struct sock_filter __user *filter, | |
204 | unsigned int len); | |
205 | void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to); | |
206 | ||
207 | void sk_filter_charge(struct sock *sk, struct sk_filter *fp); | |
208 | void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp); | |
0a14842f ED |
209 | |
210 | #ifdef CONFIG_BPF_JIT | |
20074f35 | 211 | #include <stdarg.h> |
a691ce7f CG |
212 | #include <linux/linkage.h> |
213 | #include <linux/printk.h> | |
214 | ||
fbc907f0 DB |
215 | void bpf_jit_compile(struct sk_filter *fp); |
216 | void bpf_jit_free(struct sk_filter *fp); | |
79617801 DB |
217 | |
218 | static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen, | |
219 | u32 pass, void *image) | |
220 | { | |
16495445 | 221 | pr_err("flen=%u proglen=%u pass=%u image=%pK\n", |
79617801 DB |
222 | flen, proglen, pass, image); |
223 | if (image) | |
16495445 | 224 | print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET, |
79617801 DB |
225 | 16, 1, image, proglen, false); |
226 | } | |
0a14842f | 227 | #else |
d45ed4a4 | 228 | #include <linux/slab.h> |
0a14842f ED |
229 | static inline void bpf_jit_compile(struct sk_filter *fp) |
230 | { | |
231 | } | |
232 | static inline void bpf_jit_free(struct sk_filter *fp) | |
233 | { | |
d45ed4a4 | 234 | kfree(fp); |
0a14842f | 235 | } |
0a14842f ED |
236 | #endif |
237 | ||
ea02f941 MS |
238 | static inline int bpf_tell_extensions(void) |
239 | { | |
37692299 | 240 | return SKF_AD_MAX; |
ea02f941 MS |
241 | } |
242 | ||
0a14842f ED |
243 | enum { |
244 | BPF_S_RET_K = 1, | |
245 | BPF_S_RET_A, | |
246 | BPF_S_ALU_ADD_K, | |
247 | BPF_S_ALU_ADD_X, | |
248 | BPF_S_ALU_SUB_K, | |
249 | BPF_S_ALU_SUB_X, | |
250 | BPF_S_ALU_MUL_K, | |
251 | BPF_S_ALU_MUL_X, | |
252 | BPF_S_ALU_DIV_X, | |
b6069a95 ED |
253 | BPF_S_ALU_MOD_K, |
254 | BPF_S_ALU_MOD_X, | |
0a14842f ED |
255 | BPF_S_ALU_AND_K, |
256 | BPF_S_ALU_AND_X, | |
257 | BPF_S_ALU_OR_K, | |
258 | BPF_S_ALU_OR_X, | |
9e49e889 DB |
259 | BPF_S_ALU_XOR_K, |
260 | BPF_S_ALU_XOR_X, | |
0a14842f ED |
261 | BPF_S_ALU_LSH_K, |
262 | BPF_S_ALU_LSH_X, | |
263 | BPF_S_ALU_RSH_K, | |
264 | BPF_S_ALU_RSH_X, | |
265 | BPF_S_ALU_NEG, | |
266 | BPF_S_LD_W_ABS, | |
267 | BPF_S_LD_H_ABS, | |
268 | BPF_S_LD_B_ABS, | |
269 | BPF_S_LD_W_LEN, | |
270 | BPF_S_LD_W_IND, | |
271 | BPF_S_LD_H_IND, | |
272 | BPF_S_LD_B_IND, | |
273 | BPF_S_LD_IMM, | |
274 | BPF_S_LDX_W_LEN, | |
275 | BPF_S_LDX_B_MSH, | |
276 | BPF_S_LDX_IMM, | |
277 | BPF_S_MISC_TAX, | |
278 | BPF_S_MISC_TXA, | |
279 | BPF_S_ALU_DIV_K, | |
280 | BPF_S_LD_MEM, | |
281 | BPF_S_LDX_MEM, | |
282 | BPF_S_ST, | |
283 | BPF_S_STX, | |
284 | BPF_S_JMP_JA, | |
285 | BPF_S_JMP_JEQ_K, | |
286 | BPF_S_JMP_JEQ_X, | |
287 | BPF_S_JMP_JGE_K, | |
288 | BPF_S_JMP_JGE_X, | |
289 | BPF_S_JMP_JGT_K, | |
290 | BPF_S_JMP_JGT_X, | |
291 | BPF_S_JMP_JSET_K, | |
292 | BPF_S_JMP_JSET_X, | |
293 | /* Ancillary data */ | |
294 | BPF_S_ANC_PROTOCOL, | |
295 | BPF_S_ANC_PKTTYPE, | |
296 | BPF_S_ANC_IFINDEX, | |
297 | BPF_S_ANC_NLATTR, | |
298 | BPF_S_ANC_NLATTR_NEST, | |
299 | BPF_S_ANC_MARK, | |
300 | BPF_S_ANC_QUEUE, | |
301 | BPF_S_ANC_HATYPE, | |
302 | BPF_S_ANC_RXHASH, | |
303 | BPF_S_ANC_CPU, | |
ffe06c17 | 304 | BPF_S_ANC_ALU_XOR_X, |
f3335031 ED |
305 | BPF_S_ANC_VLAN_TAG, |
306 | BPF_S_ANC_VLAN_TAG_PRESENT, | |
3e5289d5 | 307 | BPF_S_ANC_PAY_OFFSET, |
4cd3675e | 308 | BPF_S_ANC_RANDOM, |
0a14842f ED |
309 | }; |
310 | ||
1da177e4 | 311 | #endif /* __LINUX_FILTER_H__ */ |