blob: 1a0bc6d134d7981f9f0cdd5aff2d2a9471be8777 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux Socket Filter Data Structures
3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#ifndef __LINUX_FILTER_H__
5#define __LINUX_FILTER_H__
6
Daniel Borkmannb954d832014-09-10 15:01:02 +02007#include <stdarg.h>
8
Arun Sharma600634972011-07-26 16:09:06 -07009#include <linux/atomic.h>
Will Drewry0c5fe1b2012-04-12 16:47:53 -050010#include <linux/compat.h>
Zi Shen Lim9f12fbe2014-07-03 07:56:54 -070011#include <linux/skbuff.h>
Daniel Borkmannb954d832014-09-10 15:01:02 +020012#include <linux/linkage.h>
13#include <linux/printk.h>
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -070014#include <linux/workqueue.h>
Daniel Borkmannb954d832014-09-10 15:01:02 +020015
Daniel Borkmann60a3b222014-09-02 22:53:44 +020016#include <asm/cacheflush.h>
Daniel Borkmannb954d832014-09-10 15:01:02 +020017
18#include <uapi/linux/filter.h>
Alexei Starovoitovdaedfb22014-09-04 22:17:18 -070019#include <uapi/linux/bpf.h>
Daniel Borkmann60a3b222014-09-02 22:53:44 +020020
21struct sk_buff;
22struct sock;
23struct seccomp_data;
Heiko Carstens792d4b52011-05-22 07:08:11 +000024
Daniel Borkmann30743832014-05-01 18:34:19 +020025/* ArgX, context and stack frame pointer register positions. Note,
26 * Arg1, Arg2, Arg3, etc are used as argument mappings of function
27 * calls in BPF_CALL instruction.
28 */
29#define BPF_REG_ARG1 BPF_REG_1
30#define BPF_REG_ARG2 BPF_REG_2
31#define BPF_REG_ARG3 BPF_REG_3
32#define BPF_REG_ARG4 BPF_REG_4
33#define BPF_REG_ARG5 BPF_REG_5
34#define BPF_REG_CTX BPF_REG_6
35#define BPF_REG_FP BPF_REG_10
36
37/* Additional register mappings for converted user programs. */
38#define BPF_REG_A BPF_REG_0
39#define BPF_REG_X BPF_REG_7
40#define BPF_REG_TMP BPF_REG_8
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010041
42/* BPF program can access up to 512 bytes of stack space. */
43#define MAX_BPF_STACK 512
44
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020045/* Helper macros for filter block array initializers. */
Alexei Starovoitov9739eef2014-05-08 14:10:51 -070046
Alexei Starovoitove430f342014-06-06 14:46:06 -070047/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
Alexei Starovoitov9739eef2014-05-08 14:10:51 -070048
Alexei Starovoitove430f342014-06-06 14:46:06 -070049#define BPF_ALU64_REG(OP, DST, SRC) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -070050 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020051 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
Alexei Starovoitove430f342014-06-06 14:46:06 -070052 .dst_reg = DST, \
53 .src_reg = SRC, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020054 .off = 0, \
55 .imm = 0 })
Alexei Starovoitov9739eef2014-05-08 14:10:51 -070056
Alexei Starovoitove430f342014-06-06 14:46:06 -070057#define BPF_ALU32_REG(OP, DST, SRC) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -070058 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020059 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
Alexei Starovoitove430f342014-06-06 14:46:06 -070060 .dst_reg = DST, \
61 .src_reg = SRC, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020062 .off = 0, \
63 .imm = 0 })
Alexei Starovoitov9739eef2014-05-08 14:10:51 -070064
Alexei Starovoitove430f342014-06-06 14:46:06 -070065/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
Alexei Starovoitov9739eef2014-05-08 14:10:51 -070066
Alexei Starovoitove430f342014-06-06 14:46:06 -070067#define BPF_ALU64_IMM(OP, DST, IMM) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -070068 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020069 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
Alexei Starovoitove430f342014-06-06 14:46:06 -070070 .dst_reg = DST, \
71 .src_reg = 0, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020072 .off = 0, \
73 .imm = IMM })
Alexei Starovoitov9739eef2014-05-08 14:10:51 -070074
Alexei Starovoitove430f342014-06-06 14:46:06 -070075#define BPF_ALU32_IMM(OP, DST, IMM) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -070076 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020077 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
Alexei Starovoitove430f342014-06-06 14:46:06 -070078 .dst_reg = DST, \
79 .src_reg = 0, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020080 .off = 0, \
81 .imm = IMM })
Alexei Starovoitov9739eef2014-05-08 14:10:51 -070082
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020083/* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
Alexei Starovoitov9739eef2014-05-08 14:10:51 -070084
Alexei Starovoitove430f342014-06-06 14:46:06 -070085#define BPF_ENDIAN(TYPE, DST, LEN) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -070086 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020087 .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
Alexei Starovoitove430f342014-06-06 14:46:06 -070088 .dst_reg = DST, \
89 .src_reg = 0, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020090 .off = 0, \
91 .imm = LEN })
92
Alexei Starovoitove430f342014-06-06 14:46:06 -070093/* Short form of mov, dst_reg = src_reg */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020094
Alexei Starovoitove430f342014-06-06 14:46:06 -070095#define BPF_MOV64_REG(DST, SRC) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -070096 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +020097 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
Alexei Starovoitove430f342014-06-06 14:46:06 -070098 .dst_reg = DST, \
99 .src_reg = SRC, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200100 .off = 0, \
101 .imm = 0 })
102
Alexei Starovoitove430f342014-06-06 14:46:06 -0700103#define BPF_MOV32_REG(DST, SRC) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700104 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200105 .code = BPF_ALU | BPF_MOV | BPF_X, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700106 .dst_reg = DST, \
107 .src_reg = SRC, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200108 .off = 0, \
109 .imm = 0 })
110
Alexei Starovoitove430f342014-06-06 14:46:06 -0700111/* Short form of mov, dst_reg = imm32 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200112
Alexei Starovoitove430f342014-06-06 14:46:06 -0700113#define BPF_MOV64_IMM(DST, IMM) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700114 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200115 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700116 .dst_reg = DST, \
117 .src_reg = 0, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200118 .off = 0, \
119 .imm = IMM })
120
Alexei Starovoitove430f342014-06-06 14:46:06 -0700121#define BPF_MOV32_IMM(DST, IMM) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700122 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200123 .code = BPF_ALU | BPF_MOV | BPF_K, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700124 .dst_reg = DST, \
125 .src_reg = 0, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200126 .off = 0, \
127 .imm = IMM })
128
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700129/* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
130#define BPF_LD_IMM64(DST, IMM) \
131 BPF_LD_IMM64_RAW(DST, 0, IMM)
132
133#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
134 ((struct bpf_insn) { \
135 .code = BPF_LD | BPF_DW | BPF_IMM, \
136 .dst_reg = DST, \
137 .src_reg = SRC, \
138 .off = 0, \
139 .imm = (__u32) (IMM) }), \
140 ((struct bpf_insn) { \
141 .code = 0, /* zero is reserved opcode */ \
142 .dst_reg = 0, \
143 .src_reg = 0, \
144 .off = 0, \
145 .imm = ((__u64) (IMM)) >> 32 })
146
Alexei Starovoitove430f342014-06-06 14:46:06 -0700147/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200148
Alexei Starovoitove430f342014-06-06 14:46:06 -0700149#define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700150 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200151 .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700152 .dst_reg = DST, \
153 .src_reg = SRC, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200154 .off = 0, \
155 .imm = IMM })
156
Alexei Starovoitove430f342014-06-06 14:46:06 -0700157#define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700158 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200159 .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700160 .dst_reg = DST, \
161 .src_reg = SRC, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200162 .off = 0, \
163 .imm = IMM })
164
Alexei Starovoitove430f342014-06-06 14:46:06 -0700165/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200166
Alexei Starovoitove430f342014-06-06 14:46:06 -0700167#define BPF_LD_ABS(SIZE, IMM) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700168 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200169 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700170 .dst_reg = 0, \
171 .src_reg = 0, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200172 .off = 0, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700173 .imm = IMM })
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200174
Alexei Starovoitove430f342014-06-06 14:46:06 -0700175/* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200176
Alexei Starovoitove430f342014-06-06 14:46:06 -0700177#define BPF_LD_IND(SIZE, SRC, IMM) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700178 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200179 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700180 .dst_reg = 0, \
181 .src_reg = SRC, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200182 .off = 0, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700183 .imm = IMM })
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200184
Alexei Starovoitove430f342014-06-06 14:46:06 -0700185/* Memory load, dst_reg = *(uint *) (src_reg + off16) */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200186
Alexei Starovoitove430f342014-06-06 14:46:06 -0700187#define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700188 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200189 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700190 .dst_reg = DST, \
191 .src_reg = SRC, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200192 .off = OFF, \
193 .imm = 0 })
194
Alexei Starovoitove430f342014-06-06 14:46:06 -0700195/* Memory store, *(uint *) (dst_reg + off16) = src_reg */
196
197#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700198 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200199 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700200 .dst_reg = DST, \
201 .src_reg = SRC, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200202 .off = OFF, \
203 .imm = 0 })
204
Alexei Starovoitove430f342014-06-06 14:46:06 -0700205/* Memory store, *(uint *) (dst_reg + off16) = imm32 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200206
Alexei Starovoitove430f342014-06-06 14:46:06 -0700207#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700208 ((struct bpf_insn) { \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700209 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
210 .dst_reg = DST, \
211 .src_reg = 0, \
212 .off = OFF, \
213 .imm = IMM })
214
215/* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
216
217#define BPF_JMP_REG(OP, DST, SRC, OFF) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700218 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200219 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700220 .dst_reg = DST, \
221 .src_reg = SRC, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200222 .off = OFF, \
223 .imm = 0 })
224
Alexei Starovoitove430f342014-06-06 14:46:06 -0700225/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200226
Alexei Starovoitove430f342014-06-06 14:46:06 -0700227#define BPF_JMP_IMM(OP, DST, IMM, OFF) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700228 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200229 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700230 .dst_reg = DST, \
231 .src_reg = 0, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200232 .off = OFF, \
233 .imm = IMM })
234
235/* Function call */
236
237#define BPF_EMIT_CALL(FUNC) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700238 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200239 .code = BPF_JMP | BPF_CALL, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700240 .dst_reg = 0, \
241 .src_reg = 0, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200242 .off = 0, \
243 .imm = ((FUNC) - __bpf_call_base) })
244
245/* Raw code statement block */
246
Alexei Starovoitove430f342014-06-06 14:46:06 -0700247#define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700248 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200249 .code = CODE, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700250 .dst_reg = DST, \
251 .src_reg = SRC, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200252 .off = OFF, \
253 .imm = IMM })
254
255/* Program exit */
256
257#define BPF_EXIT_INSN() \
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700258 ((struct bpf_insn) { \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200259 .code = BPF_JMP | BPF_EXIT, \
Alexei Starovoitove430f342014-06-06 14:46:06 -0700260 .dst_reg = 0, \
261 .src_reg = 0, \
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200262 .off = 0, \
263 .imm = 0 })
264
265#define bytes_to_bpf_size(bytes) \
266({ \
267 int bpf_size = -EINVAL; \
268 \
269 if (bytes == sizeof(u8)) \
270 bpf_size = BPF_B; \
271 else if (bytes == sizeof(u16)) \
272 bpf_size = BPF_H; \
273 else if (bytes == sizeof(u32)) \
274 bpf_size = BPF_W; \
275 else if (bytes == sizeof(u64)) \
276 bpf_size = BPF_DW; \
277 \
278 bpf_size; \
279})
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700280
Daniel Borkmann30743832014-05-01 18:34:19 +0200281/* Macro to invoke filter function. */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700282#define SK_RUN_FILTER(filter, ctx) \
283 (*filter->prog->bpf_func)(ctx, filter->prog->insnsi)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100284
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100285#ifdef CONFIG_COMPAT
286/* A struct sock_filter is architecture independent. */
Will Drewry0c5fe1b2012-04-12 16:47:53 -0500287struct compat_sock_fprog {
288 u16 len;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100289 compat_uptr_t filter; /* struct sock_filter * */
Will Drewry0c5fe1b2012-04-12 16:47:53 -0500290};
291#endif
292
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100293struct sock_fprog_kern {
294 u16 len;
295 struct sock_filter *filter;
296};
297
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200298struct bpf_binary_header {
299 unsigned int pages;
300 u8 image[];
301};
302
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200303struct bpf_work_struct {
304 struct bpf_prog *prog;
305 struct work_struct work;
306};
Heiko Carstens792d4b52011-05-22 07:08:11 +0000307
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700308struct bpf_prog {
Daniel Borkmann286aad32014-09-08 08:04:49 +0200309 u16 pages; /* Number of allocated pages */
310 bool jited; /* Is our filter JIT'ed? */
311 u32 len; /* Number of filter blocks */
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100312 struct sock_fprog_kern *orig_prog; /* Original BPF program */
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200313 struct bpf_work_struct *work; /* Deferred free work struct */
Eric Dumazet0a148422011-04-20 09:27:32 +0000314 unsigned int (*bpf_func)(const struct sk_buff *skb,
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700315 const struct bpf_insn *filter);
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200316 /* Instructions for interpreter */
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700317 union {
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100318 struct sock_filter insns[0];
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700319 struct bpf_insn insnsi[0];
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700320 };
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700321};
322
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700323struct sk_filter {
324 atomic_t refcnt;
325 struct rcu_head rcu;
326 struct bpf_prog *prog;
327};
328
329#define BPF_PROG_RUN(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
330
331static inline unsigned int bpf_prog_size(unsigned int proglen)
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700332{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700333 return max(sizeof(struct bpf_prog),
334 offsetof(struct bpf_prog, insns[proglen]));
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700335}
336
Alexei Starovoitov009937e2014-07-30 20:34:13 -0700337#define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100338
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200339#ifdef CONFIG_DEBUG_SET_MODULE_RONX
340static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
341{
342 set_memory_ro((unsigned long)fp, fp->pages);
343}
344
345static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
346{
347 set_memory_rw((unsigned long)fp, fp->pages);
348}
349#else
350static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
351{
352}
353
354static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
355{
356}
357#endif /* CONFIG_DEBUG_SET_MODULE_RONX */
358
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100359int sk_filter(struct sock *sk, struct sk_buff *skb);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100360
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700361void bpf_prog_select_runtime(struct bpf_prog *fp);
362void bpf_prog_free(struct bpf_prog *fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100363
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700364int bpf_convert_filter(struct sock_filter *prog, int len,
365 struct bpf_insn *new_prog, int *new_len);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100366
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200367struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
368struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
369 gfp_t gfp_extra_flags);
370void __bpf_prog_free(struct bpf_prog *fp);
371
372static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
373{
374 bpf_prog_unlock_ro(fp);
375 __bpf_prog_free(fp);
376}
377
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700378int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
379void bpf_prog_destroy(struct bpf_prog *fp);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100380
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100381int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
382int sk_detach_filter(struct sock *sk);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100383
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -0700384int bpf_check_classic(const struct sock_filter *filter, unsigned int flen);
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100385int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
386 unsigned int len);
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100387
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700388bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100389void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
Eric Dumazet0a148422011-04-20 09:27:32 +0000390
Alexei Starovoitov62258272014-05-13 19:50:46 -0700391u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700392void bpf_int_jit_compile(struct bpf_prog *fp);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700393
Daniel Borkmannb954d832014-09-10 15:01:02 +0200394#ifdef CONFIG_BPF_JIT
395typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
396
397struct bpf_binary_header *
398bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
399 unsigned int alignment,
400 bpf_jit_fill_hole_t bpf_fill_ill_insns);
401void bpf_jit_binary_free(struct bpf_binary_header *hdr);
402
403void bpf_jit_compile(struct bpf_prog *fp);
404void bpf_jit_free(struct bpf_prog *fp);
405
406static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
407 u32 pass, void *image)
408{
409 pr_err("flen=%u proglen=%u pass=%u image=%pK\n",
410 flen, proglen, pass, image);
411 if (image)
412 print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
413 16, 1, image, proglen, false);
414}
415#else
416static inline void bpf_jit_compile(struct bpf_prog *fp)
417{
418}
419
420static inline void bpf_jit_free(struct bpf_prog *fp)
421{
422 bpf_prog_unlock_free(fp);
423}
424#endif /* CONFIG_BPF_JIT */
425
Daniel Borkmann34805932014-05-29 10:22:50 +0200426#define BPF_ANC BIT(15)
427
428static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
429{
430 BUG_ON(ftest->code & BPF_ANC);
431
432 switch (ftest->code) {
433 case BPF_LD | BPF_W | BPF_ABS:
434 case BPF_LD | BPF_H | BPF_ABS:
435 case BPF_LD | BPF_B | BPF_ABS:
436#define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
437 return BPF_ANC | SKF_AD_##CODE
438 switch (ftest->k) {
439 BPF_ANCILLARY(PROTOCOL);
440 BPF_ANCILLARY(PKTTYPE);
441 BPF_ANCILLARY(IFINDEX);
442 BPF_ANCILLARY(NLATTR);
443 BPF_ANCILLARY(NLATTR_NEST);
444 BPF_ANCILLARY(MARK);
445 BPF_ANCILLARY(QUEUE);
446 BPF_ANCILLARY(HATYPE);
447 BPF_ANCILLARY(RXHASH);
448 BPF_ANCILLARY(CPU);
449 BPF_ANCILLARY(ALU_XOR_X);
450 BPF_ANCILLARY(VLAN_TAG);
451 BPF_ANCILLARY(VLAN_TAG_PRESENT);
452 BPF_ANCILLARY(PAY_OFFSET);
453 BPF_ANCILLARY(RANDOM);
454 }
455 /* Fallthrough. */
456 default:
457 return ftest->code;
458 }
459}
460
Zi Shen Lim9f12fbe2014-07-03 07:56:54 -0700461void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
462 int k, unsigned int size);
463
464static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
465 unsigned int size, void *buffer)
466{
467 if (k >= 0)
468 return skb_header_pointer(skb, k, size, buffer);
469
470 return bpf_internal_load_pointer_neg_helper(skb, k, size);
471}
472
Michal Sekletarea02f942014-01-17 17:09:45 +0100473static inline int bpf_tell_extensions(void)
474{
Daniel Borkmann37692292014-01-21 00:19:37 +0100475 return SKF_AD_MAX;
Michal Sekletarea02f942014-01-17 17:09:45 +0100476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#endif /* __LINUX_FILTER_H__ */