blob: 3cf5fd561d8649ce9647bce45a0e40dc28124a91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux Socket Filter Data Structures
3 */
4
5#ifndef __LINUX_FILTER_H__
6#define __LINUX_FILTER_H__
7
8#include <linux/compiler.h>
9#include <linux/types.h>
10
11#ifdef __KERNEL__
Arun Sharma600634972011-07-26 16:09:06 -070012#include <linux/atomic.h>
Will Drewry0c5fe1b2012-04-12 16:47:53 -050013#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#endif
15
16/*
17 * Current version of the filter code architecture.
18 */
19#define BPF_MAJOR_VERSION 1
20#define BPF_MINOR_VERSION 1
21
22/*
23 * Try and keep these values and structures similar to BSD, especially
24 * the BPF code definitions which need to match so you can share filters
25 */
26
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080027struct sock_filter { /* Filter block */
Dmitry Mishinfda9ef52006-08-31 15:28:39 -070028 __u16 code; /* Actual filter code */
29 __u8 jt; /* Jump true */
30 __u8 jf; /* Jump false */
31 __u32 k; /* Generic multiuse field */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080034struct sock_fprog { /* Required for SO_ATTACH_FILTER. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 unsigned short len; /* Number of filter blocks */
36 struct sock_filter __user *filter;
37};
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * Instruction classes
41 */
42
43#define BPF_CLASS(code) ((code) & 0x07)
44#define BPF_LD 0x00
45#define BPF_LDX 0x01
46#define BPF_ST 0x02
47#define BPF_STX 0x03
48#define BPF_ALU 0x04
49#define BPF_JMP 0x05
50#define BPF_RET 0x06
51#define BPF_MISC 0x07
52
53/* ld/ldx fields */
54#define BPF_SIZE(code) ((code) & 0x18)
55#define BPF_W 0x00
56#define BPF_H 0x08
57#define BPF_B 0x10
58#define BPF_MODE(code) ((code) & 0xe0)
59#define BPF_IMM 0x00
60#define BPF_ABS 0x20
61#define BPF_IND 0x40
62#define BPF_MEM 0x60
63#define BPF_LEN 0x80
64#define BPF_MSH 0xa0
65
66/* alu/jmp fields */
67#define BPF_OP(code) ((code) & 0xf0)
68#define BPF_ADD 0x00
69#define BPF_SUB 0x10
70#define BPF_MUL 0x20
71#define BPF_DIV 0x30
72#define BPF_OR 0x40
73#define BPF_AND 0x50
74#define BPF_LSH 0x60
75#define BPF_RSH 0x70
76#define BPF_NEG 0x80
Eric Dumazetb6069a92012-09-07 22:03:35 +000077#define BPF_MOD 0x90
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define BPF_JA 0x00
80#define BPF_JEQ 0x10
81#define BPF_JGT 0x20
82#define BPF_JGE 0x30
83#define BPF_JSET 0x40
84#define BPF_SRC(code) ((code) & 0x08)
85#define BPF_K 0x00
86#define BPF_X 0x08
87
88/* ret - BPF_K and BPF_X also apply */
89#define BPF_RVAL(code) ((code) & 0x18)
90#define BPF_A 0x10
91
92/* misc */
93#define BPF_MISCOP(code) ((code) & 0xf8)
94#define BPF_TAX 0x00
95#define BPF_TXA 0x80
96
97#ifndef BPF_MAXINSNS
98#define BPF_MAXINSNS 4096
99#endif
100
101/*
102 * Macros for filter block array initializers.
103 */
104#ifndef BPF_STMT
105#define BPF_STMT(code, k) { (unsigned short)(code), 0, 0, k }
106#endif
107#ifndef BPF_JUMP
108#define BPF_JUMP(code, k, jt, jf) { (unsigned short)(code), jt, jf, k }
109#endif
110
111/*
112 * Number of scratch memory words for: BPF_ST and BPF_STX
113 */
114#define BPF_MEMWORDS 16
115
116/* RATIONALE. Negative offsets are invalid in BPF.
117 We use them to reference ancillary data.
118 Unlike introduction new instructions, it does not break
119 existing compilers/optimizers.
120 */
121#define SKF_AD_OFF (-0x1000)
122#define SKF_AD_PROTOCOL 0
123#define SKF_AD_PKTTYPE 4
124#define SKF_AD_IFINDEX 8
Patrick McHardy4738c1d2008-04-10 02:02:28 -0700125#define SKF_AD_NLATTR 12
Pablo Neira Ayusod214c752008-11-20 00:49:27 -0800126#define SKF_AD_NLATTR_NEST 16
jamal7e75f932009-10-19 02:17:56 +0000127#define SKF_AD_MARK 20
Eric Dumazetd19742f2009-10-20 01:06:22 -0700128#define SKF_AD_QUEUE 24
Paul LeoNerd Evans40eaf962010-04-22 03:32:22 +0000129#define SKF_AD_HATYPE 28
Eric Dumazetda2033c2010-11-30 21:45:56 +0000130#define SKF_AD_RXHASH 32
131#define SKF_AD_CPU 36
Jiri Pirkoffe06c12012-03-31 11:01:20 +0000132#define SKF_AD_ALU_XOR_X 40
133#define SKF_AD_MAX 44
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define SKF_NET_OFF (-0x100000)
135#define SKF_LL_OFF (-0x200000)
136
137#ifdef __KERNEL__
Heiko Carstens792d4b52011-05-22 07:08:11 +0000138
Will Drewry0c5fe1b2012-04-12 16:47:53 -0500139#ifdef CONFIG_COMPAT
140/*
141 * A struct sock_filter is architecture independent.
142 */
143struct compat_sock_fprog {
144 u16 len;
145 compat_uptr_t filter; /* struct sock_filter * */
146};
147#endif
148
Heiko Carstens792d4b52011-05-22 07:08:11 +0000149struct sk_buff;
150struct sock;
151
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700152struct sk_filter
153{
154 atomic_t refcnt;
155 unsigned int len; /* Number of filter blocks */
Eric Dumazet0a148422011-04-20 09:27:32 +0000156 unsigned int (*bpf_func)(const struct sk_buff *skb,
157 const struct sock_filter *filter);
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700158 struct rcu_head rcu;
159 struct sock_filter insns[0];
160};
161
162static inline unsigned int sk_filter_len(const struct sk_filter *fp)
163{
164 return fp->len * sizeof(struct sock_filter) + sizeof(*fp);
165}
166
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700167extern int sk_filter(struct sock *sk, struct sk_buff *skb);
Eric Dumazet62ab0812010-12-06 20:50:09 +0000168extern unsigned int sk_run_filter(const struct sk_buff *skb,
Eric Dumazet93aaae22010-11-19 09:49:59 -0800169 const struct sock_filter *filter);
Jiri Pirko302d6632012-03-31 11:01:19 +0000170extern int sk_unattached_filter_create(struct sk_filter **pfp,
171 struct sock_fprog *fprog);
172extern void sk_unattached_filter_destroy(struct sk_filter *fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173extern int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700174extern int sk_detach_filter(struct sock *sk);
Dan Carpenter4f25af22011-10-17 21:04:20 +0000175extern int sk_chk_filter(struct sock_filter *filter, unsigned int flen);
Eric Dumazet0a148422011-04-20 09:27:32 +0000176
177#ifdef CONFIG_BPF_JIT
178extern void bpf_jit_compile(struct sk_filter *fp);
179extern void bpf_jit_free(struct sk_filter *fp);
180#define SK_RUN_FILTER(FILTER, SKB) (*FILTER->bpf_func)(SKB, FILTER->insns)
181#else
182static inline void bpf_jit_compile(struct sk_filter *fp)
183{
184}
185static inline void bpf_jit_free(struct sk_filter *fp)
186{
187}
188#define SK_RUN_FILTER(FILTER, SKB) sk_run_filter(SKB, FILTER->insns)
189#endif
190
191enum {
192 BPF_S_RET_K = 1,
193 BPF_S_RET_A,
194 BPF_S_ALU_ADD_K,
195 BPF_S_ALU_ADD_X,
196 BPF_S_ALU_SUB_K,
197 BPF_S_ALU_SUB_X,
198 BPF_S_ALU_MUL_K,
199 BPF_S_ALU_MUL_X,
200 BPF_S_ALU_DIV_X,
Eric Dumazetb6069a92012-09-07 22:03:35 +0000201 BPF_S_ALU_MOD_K,
202 BPF_S_ALU_MOD_X,
Eric Dumazet0a148422011-04-20 09:27:32 +0000203 BPF_S_ALU_AND_K,
204 BPF_S_ALU_AND_X,
205 BPF_S_ALU_OR_K,
206 BPF_S_ALU_OR_X,
207 BPF_S_ALU_LSH_K,
208 BPF_S_ALU_LSH_X,
209 BPF_S_ALU_RSH_K,
210 BPF_S_ALU_RSH_X,
211 BPF_S_ALU_NEG,
212 BPF_S_LD_W_ABS,
213 BPF_S_LD_H_ABS,
214 BPF_S_LD_B_ABS,
215 BPF_S_LD_W_LEN,
216 BPF_S_LD_W_IND,
217 BPF_S_LD_H_IND,
218 BPF_S_LD_B_IND,
219 BPF_S_LD_IMM,
220 BPF_S_LDX_W_LEN,
221 BPF_S_LDX_B_MSH,
222 BPF_S_LDX_IMM,
223 BPF_S_MISC_TAX,
224 BPF_S_MISC_TXA,
225 BPF_S_ALU_DIV_K,
226 BPF_S_LD_MEM,
227 BPF_S_LDX_MEM,
228 BPF_S_ST,
229 BPF_S_STX,
230 BPF_S_JMP_JA,
231 BPF_S_JMP_JEQ_K,
232 BPF_S_JMP_JEQ_X,
233 BPF_S_JMP_JGE_K,
234 BPF_S_JMP_JGE_X,
235 BPF_S_JMP_JGT_K,
236 BPF_S_JMP_JGT_X,
237 BPF_S_JMP_JSET_K,
238 BPF_S_JMP_JSET_X,
239 /* Ancillary data */
240 BPF_S_ANC_PROTOCOL,
241 BPF_S_ANC_PKTTYPE,
242 BPF_S_ANC_IFINDEX,
243 BPF_S_ANC_NLATTR,
244 BPF_S_ANC_NLATTR_NEST,
245 BPF_S_ANC_MARK,
246 BPF_S_ANC_QUEUE,
247 BPF_S_ANC_HATYPE,
248 BPF_S_ANC_RXHASH,
249 BPF_S_ANC_CPU,
Jiri Pirkoffe06c12012-03-31 11:01:20 +0000250 BPF_S_ANC_ALU_XOR_X,
Will Drewry46b325c2012-04-12 16:47:52 -0500251 BPF_S_ANC_SECCOMP_LD_W,
Eric Dumazet0a148422011-04-20 09:27:32 +0000252};
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#endif /* __KERNEL__ */
255
256#endif /* __LINUX_FILTER_H__ */