2 * Linux Socket Filter Data Structures
4 #ifndef __LINUX_FILTER_H__
5 #define __LINUX_FILTER_H__
9 #include <linux/atomic.h>
10 #include <linux/compat.h>
11 #include <linux/skbuff.h>
12 #include <linux/linkage.h>
13 #include <linux/printk.h>
14 #include <linux/workqueue.h>
15 #include <linux/sched.h>
17 #include <asm/cacheflush.h>
19 #include <uapi/linux/filter.h>
20 #include <uapi/linux/bpf.h>
27 /* ArgX, context and stack frame pointer register positions. Note,
28 * Arg1, Arg2, Arg3, etc are used as argument mappings of function
29 * calls in BPF_CALL instruction.
31 #define BPF_REG_ARG1 BPF_REG_1
32 #define BPF_REG_ARG2 BPF_REG_2
33 #define BPF_REG_ARG3 BPF_REG_3
34 #define BPF_REG_ARG4 BPF_REG_4
35 #define BPF_REG_ARG5 BPF_REG_5
36 #define BPF_REG_CTX BPF_REG_6
37 #define BPF_REG_FP BPF_REG_10
39 /* Additional register mappings for converted user programs. */
40 #define BPF_REG_A BPF_REG_0
41 #define BPF_REG_X BPF_REG_7
42 #define BPF_REG_TMP BPF_REG_8
44 /* BPF program can access up to 512 bytes of stack space. */
45 #define MAX_BPF_STACK 512
47 /* Helper macros for filter block array initializers. */
49 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
51 #define BPF_ALU64_REG(OP, DST, SRC) \
52 ((struct bpf_insn) { \
53 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
59 #define BPF_ALU32_REG(OP, DST, SRC) \
60 ((struct bpf_insn) { \
61 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
67 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
69 #define BPF_ALU64_IMM(OP, DST, IMM) \
70 ((struct bpf_insn) { \
71 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
77 #define BPF_ALU32_IMM(OP, DST, IMM) \
78 ((struct bpf_insn) { \
79 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
85 /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
87 #define BPF_ENDIAN(TYPE, DST, LEN) \
88 ((struct bpf_insn) { \
89 .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
95 /* Short form of mov, dst_reg = src_reg */
97 #define BPF_MOV64_REG(DST, SRC) \
98 ((struct bpf_insn) { \
99 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
105 #define BPF_MOV32_REG(DST, SRC) \
106 ((struct bpf_insn) { \
107 .code = BPF_ALU | BPF_MOV | BPF_X, \
113 /* Short form of mov, dst_reg = imm32 */
115 #define BPF_MOV64_IMM(DST, IMM) \
116 ((struct bpf_insn) { \
117 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
123 #define BPF_MOV32_IMM(DST, IMM) \
124 ((struct bpf_insn) { \
125 .code = BPF_ALU | BPF_MOV | BPF_K, \
131 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
132 #define BPF_LD_IMM64(DST, IMM) \
133 BPF_LD_IMM64_RAW(DST, 0, IMM)
135 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
136 ((struct bpf_insn) { \
137 .code = BPF_LD | BPF_DW | BPF_IMM, \
141 .imm = (__u32) (IMM) }), \
142 ((struct bpf_insn) { \
143 .code = 0, /* zero is reserved opcode */ \
147 .imm = ((__u64) (IMM)) >> 32 })
149 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
150 #define BPF_LD_MAP_FD(DST, MAP_FD) \
151 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
153 /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
155 #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
156 ((struct bpf_insn) { \
157 .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
163 #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
164 ((struct bpf_insn) { \
165 .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
171 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
173 #define BPF_LD_ABS(SIZE, IMM) \
174 ((struct bpf_insn) { \
175 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
181 /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
183 #define BPF_LD_IND(SIZE, SRC, IMM) \
184 ((struct bpf_insn) { \
185 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
191 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
193 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
194 ((struct bpf_insn) { \
195 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
201 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
203 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
204 ((struct bpf_insn) { \
205 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
211 /* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
213 #define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
214 ((struct bpf_insn) { \
215 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
221 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
223 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
224 ((struct bpf_insn) { \
225 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
231 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
233 #define BPF_JMP_REG(OP, DST, SRC, OFF) \
234 ((struct bpf_insn) { \
235 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
241 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
243 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
244 ((struct bpf_insn) { \
245 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
253 #define BPF_EMIT_CALL(FUNC) \
254 ((struct bpf_insn) { \
255 .code = BPF_JMP | BPF_CALL, \
259 .imm = ((FUNC) - __bpf_call_base) })
261 /* Raw code statement block */
263 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
264 ((struct bpf_insn) { \
273 #define BPF_EXIT_INSN() \
274 ((struct bpf_insn) { \
275 .code = BPF_JMP | BPF_EXIT, \
281 /* Internal classic blocks for direct assignment */
283 #define __BPF_STMT(CODE, K) \
284 ((struct sock_filter) BPF_STMT(CODE, K))
286 #define __BPF_JUMP(CODE, K, JT, JF) \
287 ((struct sock_filter) BPF_JUMP(CODE, K, JT, JF))
289 #define bytes_to_bpf_size(bytes) \
291 int bpf_size = -EINVAL; \
293 if (bytes == sizeof(u8)) \
295 else if (bytes == sizeof(u16)) \
297 else if (bytes == sizeof(u32)) \
299 else if (bytes == sizeof(u64)) \
305 /* Macro to invoke filter function. */
306 #define SK_RUN_FILTER(filter, ctx) \
307 (*filter->prog->bpf_func)(ctx, filter->prog->insnsi)
310 /* A struct sock_filter is architecture independent. */
311 struct compat_sock_fprog {
313 compat_uptr_t filter; /* struct sock_filter * */
317 struct sock_fprog_kern {
319 struct sock_filter *filter;
322 struct bpf_binary_header {
328 u16 pages; /* Number of allocated pages */
329 bool jited; /* Is our filter JIT'ed? */
330 bool gpl_compatible; /* Is our filter GPL compatible? */
331 u32 len; /* Number of filter blocks */
332 enum bpf_prog_type type; /* Type of BPF program */
333 struct bpf_prog_aux *aux; /* Auxiliary fields */
334 struct sock_fprog_kern *orig_prog; /* Original BPF program */
335 unsigned int (*bpf_func)(const struct sk_buff *skb,
336 const struct bpf_insn *filter);
337 /* Instructions for interpreter */
339 struct sock_filter insns[0];
340 struct bpf_insn insnsi[0];
347 struct bpf_prog *prog;
350 #define BPF_PROG_RUN(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
352 static inline unsigned int bpf_prog_size(unsigned int proglen)
354 return max(sizeof(struct bpf_prog),
355 offsetof(struct bpf_prog, insns[proglen]));
358 static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
360 /* When classic BPF programs have been loaded and the arch
361 * does not have a classic BPF JIT (anymore), they have been
362 * converted via bpf_migrate_filter() to eBPF and thus always
363 * have an unspec program type.
365 return prog->type == BPF_PROG_TYPE_UNSPEC;
368 #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
370 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
371 static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
373 set_memory_ro((unsigned long)fp, fp->pages);
376 static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
378 set_memory_rw((unsigned long)fp, fp->pages);
381 static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
385 static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
388 #endif /* CONFIG_DEBUG_SET_MODULE_RONX */
390 int sk_filter(struct sock *sk, struct sk_buff *skb);
392 int bpf_prog_select_runtime(struct bpf_prog *fp);
393 void bpf_prog_free(struct bpf_prog *fp);
395 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
396 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
397 gfp_t gfp_extra_flags);
398 void __bpf_prog_free(struct bpf_prog *fp);
400 static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
402 bpf_prog_unlock_ro(fp);
406 typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
409 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
410 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
411 bpf_aux_classic_check_t trans);
412 void bpf_prog_destroy(struct bpf_prog *fp);
414 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
415 int sk_attach_bpf(u32 ufd, struct sock *sk);
416 int sk_detach_filter(struct sock *sk);
417 int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
420 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
421 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
423 u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
424 void bpf_int_jit_compile(struct bpf_prog *fp);
425 bool bpf_helper_changes_skb_data(void *func);
427 #ifdef CONFIG_BPF_JIT
428 typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
430 struct bpf_binary_header *
431 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
432 unsigned int alignment,
433 bpf_jit_fill_hole_t bpf_fill_ill_insns);
434 void bpf_jit_binary_free(struct bpf_binary_header *hdr);
436 void bpf_jit_compile(struct bpf_prog *fp);
437 void bpf_jit_free(struct bpf_prog *fp);
439 static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
440 u32 pass, void *image)
442 pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen,
443 proglen, pass, image, current->comm, task_pid_nr(current));
446 print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
447 16, 1, image, proglen, false);
450 static inline void bpf_jit_compile(struct bpf_prog *fp)
454 static inline void bpf_jit_free(struct bpf_prog *fp)
456 bpf_prog_unlock_free(fp);
458 #endif /* CONFIG_BPF_JIT */
460 #define BPF_ANC BIT(15)
462 static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
464 BUG_ON(ftest->code & BPF_ANC);
466 switch (ftest->code) {
467 case BPF_LD | BPF_W | BPF_ABS:
468 case BPF_LD | BPF_H | BPF_ABS:
469 case BPF_LD | BPF_B | BPF_ABS:
470 #define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
471 return BPF_ANC | SKF_AD_##CODE
473 BPF_ANCILLARY(PROTOCOL);
474 BPF_ANCILLARY(PKTTYPE);
475 BPF_ANCILLARY(IFINDEX);
476 BPF_ANCILLARY(NLATTR);
477 BPF_ANCILLARY(NLATTR_NEST);
479 BPF_ANCILLARY(QUEUE);
480 BPF_ANCILLARY(HATYPE);
481 BPF_ANCILLARY(RXHASH);
483 BPF_ANCILLARY(ALU_XOR_X);
484 BPF_ANCILLARY(VLAN_TAG);
485 BPF_ANCILLARY(VLAN_TAG_PRESENT);
486 BPF_ANCILLARY(PAY_OFFSET);
487 BPF_ANCILLARY(RANDOM);
488 BPF_ANCILLARY(VLAN_TPID);
496 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
497 int k, unsigned int size);
499 static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
500 unsigned int size, void *buffer)
503 return skb_header_pointer(skb, k, size, buffer);
505 return bpf_internal_load_pointer_neg_helper(skb, k, size);
508 static inline int bpf_tell_extensions(void)
513 #endif /* __LINUX_FILTER_H__ */