Branch data Line data Source code
1 : : /*
2 : : * Linux Socket Filter Data Structures
3 : : */
4 : : #ifndef __LINUX_FILTER_H__
5 : : #define __LINUX_FILTER_H__
6 : :
7 : : #include <linux/atomic.h>
8 : : #include <linux/compat.h>
9 : : #include <linux/workqueue.h>
10 : : #include <uapi/linux/filter.h>
11 : :
12 : : #ifdef CONFIG_COMPAT
13 : : /*
14 : : * A struct sock_filter is architecture independent.
15 : : */
16 : : struct compat_sock_fprog {
17 : : u16 len;
18 : : compat_uptr_t filter; /* struct sock_filter * */
19 : : };
20 : : #endif
21 : :
22 : : struct sk_buff;
23 : : struct sock;
24 : :
25 : : struct sk_filter
26 : : {
27 : : atomic_t refcnt;
28 : : unsigned int len; /* Number of filter blocks */
29 : : struct rcu_head rcu;
30 : : unsigned int (*bpf_func)(const struct sk_buff *skb,
31 : : const struct sock_filter *filter);
32 : : union {
33 : : struct sock_filter insns[0];
34 : : struct work_struct work;
35 : : };
36 : : };
37 : :
38 : : static inline unsigned int sk_filter_size(unsigned int proglen)
39 : : {
40 : 8 : return max(sizeof(struct sk_filter),
41 : : offsetof(struct sk_filter, insns[proglen]));
42 : : }
43 : :
44 : : extern int sk_filter(struct sock *sk, struct sk_buff *skb);
45 : : extern unsigned int sk_run_filter(const struct sk_buff *skb,
46 : : const struct sock_filter *filter);
47 : : extern int sk_unattached_filter_create(struct sk_filter **pfp,
48 : : struct sock_fprog *fprog);
49 : : extern void sk_unattached_filter_destroy(struct sk_filter *fp);
50 : : extern int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
51 : : extern int sk_detach_filter(struct sock *sk);
52 : : extern int sk_chk_filter(struct sock_filter *filter, unsigned int flen);
53 : : extern int sk_get_filter(struct sock *sk, struct sock_filter __user *filter, unsigned len);
54 : : extern void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to);
55 : :
56 : : #ifdef CONFIG_BPF_JIT
57 : : #include <stdarg.h>
58 : : #include <linux/linkage.h>
59 : : #include <linux/printk.h>
60 : :
61 : : extern void bpf_jit_compile(struct sk_filter *fp);
62 : : extern void bpf_jit_free(struct sk_filter *fp);
63 : :
64 : : static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
65 : : u32 pass, void *image)
66 : : {
67 : : pr_err("flen=%u proglen=%u pass=%u image=%pK\n",
68 : : flen, proglen, pass, image);
69 : : if (image)
70 : : print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
71 : : 16, 1, image, proglen, false);
72 : : }
73 : : #define SK_RUN_FILTER(FILTER, SKB) (*FILTER->bpf_func)(SKB, FILTER->insns)
74 : : #else
75 : : #include <linux/slab.h>
76 : : static inline void bpf_jit_compile(struct sk_filter *fp)
77 : : {
78 : : }
79 : : static inline void bpf_jit_free(struct sk_filter *fp)
80 : : {
81 : 4 : kfree(fp);
82 : : }
83 : : #define SK_RUN_FILTER(FILTER, SKB) sk_run_filter(SKB, FILTER->insns)
84 : : #endif
85 : :
86 : : static inline int bpf_tell_extensions(void)
87 : : {
88 : : return SKF_AD_MAX;
89 : : }
90 : :
91 : : enum {
92 : : BPF_S_RET_K = 1,
93 : : BPF_S_RET_A,
94 : : BPF_S_ALU_ADD_K,
95 : : BPF_S_ALU_ADD_X,
96 : : BPF_S_ALU_SUB_K,
97 : : BPF_S_ALU_SUB_X,
98 : : BPF_S_ALU_MUL_K,
99 : : BPF_S_ALU_MUL_X,
100 : : BPF_S_ALU_DIV_X,
101 : : BPF_S_ALU_MOD_K,
102 : : BPF_S_ALU_MOD_X,
103 : : BPF_S_ALU_AND_K,
104 : : BPF_S_ALU_AND_X,
105 : : BPF_S_ALU_OR_K,
106 : : BPF_S_ALU_OR_X,
107 : : BPF_S_ALU_XOR_K,
108 : : BPF_S_ALU_XOR_X,
109 : : BPF_S_ALU_LSH_K,
110 : : BPF_S_ALU_LSH_X,
111 : : BPF_S_ALU_RSH_K,
112 : : BPF_S_ALU_RSH_X,
113 : : BPF_S_ALU_NEG,
114 : : BPF_S_LD_W_ABS,
115 : : BPF_S_LD_H_ABS,
116 : : BPF_S_LD_B_ABS,
117 : : BPF_S_LD_W_LEN,
118 : : BPF_S_LD_W_IND,
119 : : BPF_S_LD_H_IND,
120 : : BPF_S_LD_B_IND,
121 : : BPF_S_LD_IMM,
122 : : BPF_S_LDX_W_LEN,
123 : : BPF_S_LDX_B_MSH,
124 : : BPF_S_LDX_IMM,
125 : : BPF_S_MISC_TAX,
126 : : BPF_S_MISC_TXA,
127 : : BPF_S_ALU_DIV_K,
128 : : BPF_S_LD_MEM,
129 : : BPF_S_LDX_MEM,
130 : : BPF_S_ST,
131 : : BPF_S_STX,
132 : : BPF_S_JMP_JA,
133 : : BPF_S_JMP_JEQ_K,
134 : : BPF_S_JMP_JEQ_X,
135 : : BPF_S_JMP_JGE_K,
136 : : BPF_S_JMP_JGE_X,
137 : : BPF_S_JMP_JGT_K,
138 : : BPF_S_JMP_JGT_X,
139 : : BPF_S_JMP_JSET_K,
140 : : BPF_S_JMP_JSET_X,
141 : : /* Ancillary data */
142 : : BPF_S_ANC_PROTOCOL,
143 : : BPF_S_ANC_PKTTYPE,
144 : : BPF_S_ANC_IFINDEX,
145 : : BPF_S_ANC_NLATTR,
146 : : BPF_S_ANC_NLATTR_NEST,
147 : : BPF_S_ANC_MARK,
148 : : BPF_S_ANC_QUEUE,
149 : : BPF_S_ANC_HATYPE,
150 : : BPF_S_ANC_RXHASH,
151 : : BPF_S_ANC_CPU,
152 : : BPF_S_ANC_ALU_XOR_X,
153 : : BPF_S_ANC_SECCOMP_LD_W,
154 : : BPF_S_ANC_VLAN_TAG,
155 : : BPF_S_ANC_VLAN_TAG_PRESENT,
156 : : BPF_S_ANC_PAY_OFFSET,
157 : : };
158 : :
159 : : #endif /* __LINUX_FILTER_H__ */
|