Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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__ | |
12 | #include <asm/atomic.h> | |
13 | #endif | |
14 | ||
15 | /* | |
16 | * Current version of the filter code architecture. | |
17 | */ | |
18 | #define BPF_MAJOR_VERSION 1 | |
19 | #define BPF_MINOR_VERSION 1 | |
20 | ||
21 | /* | |
22 | * Try and keep these values and structures similar to BSD, especially | |
23 | * the BPF code definitions which need to match so you can share filters | |
24 | */ | |
25 | ||
26 | struct sock_filter /* Filter block */ | |
27 | { | |
fda9ef5d DM |
28 | __u16 code; /* Actual filter code */ |
29 | __u8 jt; /* Jump true */ | |
30 | __u8 jf; /* Jump false */ | |
31 | __u32 k; /* Generic multiuse field */ | |
1da177e4 LT |
32 | }; |
33 | ||
34 | struct sock_fprog /* Required for SO_ATTACH_FILTER. */ | |
35 | { | |
36 | unsigned short len; /* Number of filter blocks */ | |
37 | struct sock_filter __user *filter; | |
38 | }; | |
39 | ||
40 | #ifdef __KERNEL__ | |
41 | struct sk_filter | |
42 | { | |
43 | atomic_t refcnt; | |
fda9ef5d DM |
44 | unsigned int len; /* Number of filter blocks */ |
45 | struct rcu_head rcu; | |
46 | struct sock_filter insns[0]; | |
1da177e4 LT |
47 | }; |
48 | ||
49 | static inline unsigned int sk_filter_len(struct sk_filter *fp) | |
50 | { | |
51 | return fp->len*sizeof(struct sock_filter) + sizeof(*fp); | |
52 | } | |
53 | #endif | |
54 | ||
55 | /* | |
56 | * Instruction classes | |
57 | */ | |
58 | ||
59 | #define BPF_CLASS(code) ((code) & 0x07) | |
60 | #define BPF_LD 0x00 | |
61 | #define BPF_LDX 0x01 | |
62 | #define BPF_ST 0x02 | |
63 | #define BPF_STX 0x03 | |
64 | #define BPF_ALU 0x04 | |
65 | #define BPF_JMP 0x05 | |
66 | #define BPF_RET 0x06 | |
67 | #define BPF_MISC 0x07 | |
68 | ||
69 | /* ld/ldx fields */ | |
70 | #define BPF_SIZE(code) ((code) & 0x18) | |
71 | #define BPF_W 0x00 | |
72 | #define BPF_H 0x08 | |
73 | #define BPF_B 0x10 | |
74 | #define BPF_MODE(code) ((code) & 0xe0) | |
75 | #define BPF_IMM 0x00 | |
76 | #define BPF_ABS 0x20 | |
77 | #define BPF_IND 0x40 | |
78 | #define BPF_MEM 0x60 | |
79 | #define BPF_LEN 0x80 | |
80 | #define BPF_MSH 0xa0 | |
81 | ||
82 | /* alu/jmp fields */ | |
83 | #define BPF_OP(code) ((code) & 0xf0) | |
84 | #define BPF_ADD 0x00 | |
85 | #define BPF_SUB 0x10 | |
86 | #define BPF_MUL 0x20 | |
87 | #define BPF_DIV 0x30 | |
88 | #define BPF_OR 0x40 | |
89 | #define BPF_AND 0x50 | |
90 | #define BPF_LSH 0x60 | |
91 | #define BPF_RSH 0x70 | |
92 | #define BPF_NEG 0x80 | |
93 | #define BPF_JA 0x00 | |
94 | #define BPF_JEQ 0x10 | |
95 | #define BPF_JGT 0x20 | |
96 | #define BPF_JGE 0x30 | |
97 | #define BPF_JSET 0x40 | |
98 | #define BPF_SRC(code) ((code) & 0x08) | |
99 | #define BPF_K 0x00 | |
100 | #define BPF_X 0x08 | |
101 | ||
102 | /* ret - BPF_K and BPF_X also apply */ | |
103 | #define BPF_RVAL(code) ((code) & 0x18) | |
104 | #define BPF_A 0x10 | |
105 | ||
106 | /* misc */ | |
107 | #define BPF_MISCOP(code) ((code) & 0xf8) | |
108 | #define BPF_TAX 0x00 | |
109 | #define BPF_TXA 0x80 | |
110 | ||
111 | #ifndef BPF_MAXINSNS | |
112 | #define BPF_MAXINSNS 4096 | |
113 | #endif | |
114 | ||
115 | /* | |
116 | * Macros for filter block array initializers. | |
117 | */ | |
118 | #ifndef BPF_STMT | |
119 | #define BPF_STMT(code, k) { (unsigned short)(code), 0, 0, k } | |
120 | #endif | |
121 | #ifndef BPF_JUMP | |
122 | #define BPF_JUMP(code, k, jt, jf) { (unsigned short)(code), jt, jf, k } | |
123 | #endif | |
124 | ||
125 | /* | |
126 | * Number of scratch memory words for: BPF_ST and BPF_STX | |
127 | */ | |
128 | #define BPF_MEMWORDS 16 | |
129 | ||
130 | /* RATIONALE. Negative offsets are invalid in BPF. | |
131 | We use them to reference ancillary data. | |
132 | Unlike introduction new instructions, it does not break | |
133 | existing compilers/optimizers. | |
134 | */ | |
135 | #define SKF_AD_OFF (-0x1000) | |
136 | #define SKF_AD_PROTOCOL 0 | |
137 | #define SKF_AD_PKTTYPE 4 | |
138 | #define SKF_AD_IFINDEX 8 | |
139 | #define SKF_AD_MAX 12 | |
140 | #define SKF_NET_OFF (-0x100000) | |
141 | #define SKF_LL_OFF (-0x200000) | |
142 | ||
143 | #ifdef __KERNEL__ | |
144 | struct sk_buff; | |
145 | struct sock; | |
146 | ||
4bad4dc9 | 147 | extern unsigned int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen); |
1da177e4 | 148 | extern int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk); |
55b33325 | 149 | extern int sk_detach_filter(struct sock *sk); |
1da177e4 LT |
150 | extern int sk_chk_filter(struct sock_filter *filter, int flen); |
151 | #endif /* __KERNEL__ */ | |
152 | ||
153 | #endif /* __LINUX_FILTER_H__ */ |