2 * Linux Socket Filter Data Structures
4 #ifndef __LINUX_FILTER_H__
5 #define __LINUX_FILTER_H__
7 #include <linux/atomic.h>
8 #include <linux/compat.h>
9 #include <linux/workqueue.h>
10 #include <uapi/linux/filter.h>
12 /* Internally used and optimized filter representation with extended
13 * instruction set based on top of classic BPF.
16 /* instruction classes */
17 #define BPF_ALU64 0x07 /* alu mode in double word width */
20 #define BPF_DW 0x18 /* double word */
21 #define BPF_XADD 0xc0 /* exclusive add */
24 #define BPF_MOV 0xb0 /* mov reg to reg */
25 #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */
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
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 */
40 /* Placeholder/dummy for 0 */
43 /* Register numbers */
59 /* BPF has 10 general purpose 64-bit registers and stack frame. */
60 #define MAX_BPF_REG __MAX_BPF_REG
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.
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
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
79 /* BPF program can access up to 512 bytes of stack space. */
80 #define MAX_BPF_STACK 512
82 /* Macro to invoke filter function. */
83 #define SK_RUN_FILTER(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
85 struct sock_filter_int
{
86 __u8 code
; /* opcode */
87 __u8 a_reg
:4; /* dest register */
88 __u8 x_reg
:4; /* source register */
89 __s16 off
; /* signed offset */
90 __s32 imm
; /* signed immediate constant */
94 /* A struct sock_filter is architecture independent. */
95 struct compat_sock_fprog
{
97 compat_uptr_t filter
; /* struct sock_filter * */
101 struct sock_fprog_kern
{
103 struct sock_filter
*filter
;
112 u32 jited
:1, /* Is our filter JIT'ed? */
113 len
:31; /* Number of filter blocks */
114 struct sock_fprog_kern
*orig_prog
; /* Original BPF program */
116 unsigned int (*bpf_func
)(const struct sk_buff
*skb
,
117 const struct sock_filter_int
*filter
);
119 struct sock_filter insns
[0];
120 struct sock_filter_int insnsi
[0];
121 struct work_struct work
;
125 static inline unsigned int sk_filter_size(unsigned int proglen
)
127 return max(sizeof(struct sk_filter
),
128 offsetof(struct sk_filter
, insns
[proglen
]));
131 #define sk_filter_proglen(fprog) \
132 (fprog->len * sizeof(fprog->filter[0]))
134 int sk_filter(struct sock
*sk
, struct sk_buff
*skb
);
136 u32
sk_run_filter_int_seccomp(const struct seccomp_data
*ctx
,
137 const struct sock_filter_int
*insni
);
138 u32
sk_run_filter_int_skb(const struct sk_buff
*ctx
,
139 const struct sock_filter_int
*insni
);
141 int sk_convert_filter(struct sock_filter
*prog
, int len
,
142 struct sock_filter_int
*new_prog
, int *new_len
);
144 int sk_unattached_filter_create(struct sk_filter
**pfp
,
145 struct sock_fprog
*fprog
);
146 void sk_unattached_filter_destroy(struct sk_filter
*fp
);
148 int sk_attach_filter(struct sock_fprog
*fprog
, struct sock
*sk
);
149 int sk_detach_filter(struct sock
*sk
);
151 int sk_chk_filter(struct sock_filter
*filter
, unsigned int flen
);
152 int sk_get_filter(struct sock
*sk
, struct sock_filter __user
*filter
,
154 void sk_decode_filter(struct sock_filter
*filt
, struct sock_filter
*to
);
156 void sk_filter_charge(struct sock
*sk
, struct sk_filter
*fp
);
157 void sk_filter_uncharge(struct sock
*sk
, struct sk_filter
*fp
);
159 #ifdef CONFIG_BPF_JIT
161 #include <linux/linkage.h>
162 #include <linux/printk.h>
164 void bpf_jit_compile(struct sk_filter
*fp
);
165 void bpf_jit_free(struct sk_filter
*fp
);
167 static inline void bpf_jit_dump(unsigned int flen
, unsigned int proglen
,
168 u32 pass
, void *image
)
170 pr_err("flen=%u proglen=%u pass=%u image=%pK\n",
171 flen
, proglen
, pass
, image
);
173 print_hex_dump(KERN_ERR
, "JIT code: ", DUMP_PREFIX_OFFSET
,
174 16, 1, image
, proglen
, false);
177 #include <linux/slab.h>
178 static inline void bpf_jit_compile(struct sk_filter
*fp
)
181 static inline void bpf_jit_free(struct sk_filter
*fp
)
187 static inline int bpf_tell_extensions(void)
247 BPF_S_ANC_NLATTR_NEST
,
255 BPF_S_ANC_VLAN_TAG_PRESENT
,
256 BPF_S_ANC_PAY_OFFSET
,
260 #endif /* __LINUX_FILTER_H__ */