]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - include/linux/filter.h
net: filter: get rid of BPF_S_* enum
[mirror_ubuntu-hirsute-kernel.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>
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
30743837
DB
40/* Register numbers */
41enum {
42 BPF_REG_0 = 0,
43 BPF_REG_1,
44 BPF_REG_2,
45 BPF_REG_3,
46 BPF_REG_4,
47 BPF_REG_5,
48 BPF_REG_6,
49 BPF_REG_7,
50 BPF_REG_8,
51 BPF_REG_9,
52 BPF_REG_10,
53 __MAX_BPF_REG,
54};
55
bd4cf0ed 56/* BPF has 10 general purpose 64-bit registers and stack frame. */
30743837
DB
57#define MAX_BPF_REG __MAX_BPF_REG
58
59/* ArgX, context and stack frame pointer register positions. Note,
60 * Arg1, Arg2, Arg3, etc are used as argument mappings of function
61 * calls in BPF_CALL instruction.
62 */
63#define BPF_REG_ARG1 BPF_REG_1
64#define BPF_REG_ARG2 BPF_REG_2
65#define BPF_REG_ARG3 BPF_REG_3
66#define BPF_REG_ARG4 BPF_REG_4
67#define BPF_REG_ARG5 BPF_REG_5
68#define BPF_REG_CTX BPF_REG_6
69#define BPF_REG_FP BPF_REG_10
70
71/* Additional register mappings for converted user programs. */
72#define BPF_REG_A BPF_REG_0
73#define BPF_REG_X BPF_REG_7
74#define BPF_REG_TMP BPF_REG_8
bd4cf0ed
AS
75
76/* BPF program can access up to 512 bytes of stack space. */
77#define MAX_BPF_STACK 512
78
9739eef1
AS
79/* bpf_add|sub|...: a += x, bpf_mov: a = x */
80#define BPF_ALU64_REG(op, a, x) \
81 ((struct sock_filter_int) {BPF_ALU64|BPF_OP(op)|BPF_X, a, x, 0, 0})
82#define BPF_ALU32_REG(op, a, x) \
83 ((struct sock_filter_int) {BPF_ALU|BPF_OP(op)|BPF_X, a, x, 0, 0})
84
85/* bpf_add|sub|...: a += imm, bpf_mov: a = imm */
86#define BPF_ALU64_IMM(op, a, imm) \
87 ((struct sock_filter_int) {BPF_ALU64|BPF_OP(op)|BPF_K, a, 0, 0, imm})
88#define BPF_ALU32_IMM(op, a, imm) \
89 ((struct sock_filter_int) {BPF_ALU|BPF_OP(op)|BPF_K, a, 0, 0, imm})
90
91/* R0 = *(uint *) (skb->data + off) */
92#define BPF_LD_ABS(size, off) \
93 ((struct sock_filter_int) {BPF_LD|BPF_SIZE(size)|BPF_ABS, 0, 0, 0, off})
94
95/* R0 = *(uint *) (skb->data + x + off) */
96#define BPF_LD_IND(size, x, off) \
97 ((struct sock_filter_int) {BPF_LD|BPF_SIZE(size)|BPF_IND, 0, x, 0, off})
98
99/* a = *(uint *) (x + off) */
100#define BPF_LDX_MEM(sz, a, x, off) \
101 ((struct sock_filter_int) {BPF_LDX|BPF_SIZE(sz)|BPF_MEM, a, x, off, 0})
102
103/* if (a 'op' x) goto pc+off */
104#define BPF_JMP_REG(op, a, x, off) \
105 ((struct sock_filter_int) {BPF_JMP|BPF_OP(op)|BPF_X, a, x, off, 0})
106
107/* if (a 'op' imm) goto pc+off */
108#define BPF_JMP_IMM(op, a, imm, off) \
109 ((struct sock_filter_int) {BPF_JMP|BPF_OP(op)|BPF_K, a, 0, off, imm})
110
111#define BPF_EXIT_INSN() \
112 ((struct sock_filter_int) {BPF_JMP|BPF_EXIT, 0, 0, 0, 0})
113
114static inline int size_to_bpf(int size)
115{
116 switch (size) {
117 case 1:
118 return BPF_B;
119 case 2:
120 return BPF_H;
121 case 4:
122 return BPF_W;
123 case 8:
124 return BPF_DW;
125 default:
126 return -EINVAL;
127 }
128}
129
30743837
DB
130/* Macro to invoke filter function. */
131#define SK_RUN_FILTER(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
bd4cf0ed
AS
132
133struct sock_filter_int {
134 __u8 code; /* opcode */
135 __u8 a_reg:4; /* dest register */
136 __u8 x_reg:4; /* source register */
137 __s16 off; /* signed offset */
138 __s32 imm; /* signed immediate constant */
139};
140
141#ifdef CONFIG_COMPAT
142/* A struct sock_filter is architecture independent. */
0c5fe1b4
WD
143struct compat_sock_fprog {
144 u16 len;
bd4cf0ed 145 compat_uptr_t filter; /* struct sock_filter * */
0c5fe1b4
WD
146};
147#endif
148
a3ea269b
DB
149struct sock_fprog_kern {
150 u16 len;
151 struct sock_filter *filter;
152};
153
792d4b5c
HC
154struct sk_buff;
155struct sock;
bd4cf0ed 156struct seccomp_data;
792d4b5c 157
a3ea269b 158struct sk_filter {
b715631f 159 atomic_t refcnt;
f8bbbfc3
DB
160 u32 jited:1, /* Is our filter JIT'ed? */
161 len:31; /* Number of filter blocks */
a3ea269b 162 struct sock_fprog_kern *orig_prog; /* Original BPF program */
d45ed4a4 163 struct rcu_head rcu;
0a14842f 164 unsigned int (*bpf_func)(const struct sk_buff *skb,
bd4cf0ed 165 const struct sock_filter_int *filter);
d45ed4a4 166 union {
bd4cf0ed
AS
167 struct sock_filter insns[0];
168 struct sock_filter_int insnsi[0];
d45ed4a4
AS
169 struct work_struct work;
170 };
b715631f
SH
171};
172
d45ed4a4 173static inline unsigned int sk_filter_size(unsigned int proglen)
b715631f 174{
d45ed4a4
AS
175 return max(sizeof(struct sk_filter),
176 offsetof(struct sk_filter, insns[proglen]));
b715631f
SH
177}
178
a3ea269b
DB
179#define sk_filter_proglen(fprog) \
180 (fprog->len * sizeof(fprog->filter[0]))
181
fbc907f0 182int sk_filter(struct sock *sk, struct sk_buff *skb);
bd4cf0ed 183
5fe821a9
AS
184void sk_filter_select_runtime(struct sk_filter *fp);
185void sk_filter_free(struct sk_filter *fp);
bd4cf0ed
AS
186
187int sk_convert_filter(struct sock_filter *prog, int len,
188 struct sock_filter_int *new_prog, int *new_len);
a3ea269b 189
fbc907f0 190int sk_unattached_filter_create(struct sk_filter **pfp,
b1fcd35c 191 struct sock_fprog_kern *fprog);
fbc907f0 192void sk_unattached_filter_destroy(struct sk_filter *fp);
a3ea269b 193
fbc907f0
DB
194int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
195int sk_detach_filter(struct sock *sk);
a3ea269b 196
fbc907f0
DB
197int sk_chk_filter(struct sock_filter *filter, unsigned int flen);
198int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
199 unsigned int len);
fbc907f0
DB
200
201void sk_filter_charge(struct sock *sk, struct sk_filter *fp);
202void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
0a14842f 203
62258278
AS
204u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
205void bpf_int_jit_compile(struct sk_filter *fp);
206
34805931
DB
207#define BPF_ANC BIT(15)
208
209static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
210{
211 BUG_ON(ftest->code & BPF_ANC);
212
213 switch (ftest->code) {
214 case BPF_LD | BPF_W | BPF_ABS:
215 case BPF_LD | BPF_H | BPF_ABS:
216 case BPF_LD | BPF_B | BPF_ABS:
217#define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
218 return BPF_ANC | SKF_AD_##CODE
219 switch (ftest->k) {
220 BPF_ANCILLARY(PROTOCOL);
221 BPF_ANCILLARY(PKTTYPE);
222 BPF_ANCILLARY(IFINDEX);
223 BPF_ANCILLARY(NLATTR);
224 BPF_ANCILLARY(NLATTR_NEST);
225 BPF_ANCILLARY(MARK);
226 BPF_ANCILLARY(QUEUE);
227 BPF_ANCILLARY(HATYPE);
228 BPF_ANCILLARY(RXHASH);
229 BPF_ANCILLARY(CPU);
230 BPF_ANCILLARY(ALU_XOR_X);
231 BPF_ANCILLARY(VLAN_TAG);
232 BPF_ANCILLARY(VLAN_TAG_PRESENT);
233 BPF_ANCILLARY(PAY_OFFSET);
234 BPF_ANCILLARY(RANDOM);
235 }
236 /* Fallthrough. */
237 default:
238 return ftest->code;
239 }
240}
241
0a14842f 242#ifdef CONFIG_BPF_JIT
20074f35 243#include <stdarg.h>
a691ce7f
CG
244#include <linux/linkage.h>
245#include <linux/printk.h>
246
fbc907f0
DB
247void bpf_jit_compile(struct sk_filter *fp);
248void bpf_jit_free(struct sk_filter *fp);
79617801
DB
249
250static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
251 u32 pass, void *image)
252{
16495445 253 pr_err("flen=%u proglen=%u pass=%u image=%pK\n",
79617801
DB
254 flen, proglen, pass, image);
255 if (image)
16495445 256 print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
79617801
DB
257 16, 1, image, proglen, false);
258}
0a14842f 259#else
d45ed4a4 260#include <linux/slab.h>
34805931 261
0a14842f
ED
262static inline void bpf_jit_compile(struct sk_filter *fp)
263{
264}
34805931 265
0a14842f
ED
266static inline void bpf_jit_free(struct sk_filter *fp)
267{
d45ed4a4 268 kfree(fp);
0a14842f 269}
34805931 270#endif /* CONFIG_BPF_JIT */
0a14842f 271
ea02f941
MS
272static inline int bpf_tell_extensions(void)
273{
37692299 274 return SKF_AD_MAX;
ea02f941
MS
275}
276
1da177e4 277#endif /* __LINUX_FILTER_H__ */