• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 
3 #ifndef __LINUX_FILTER_H
4 #define __LINUX_FILTER_H
5 
6 #include <linux/bpf.h>
7 
8 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM)			\
9 	((struct bpf_insn) {					\
10 		.code = CODE,					\
11 		.dst_reg = DST,					\
12 		.src_reg = SRC,					\
13 		.off = OFF,					\
14 		.imm = IMM })
15 
16 #define BPF_ALU32_IMM(OP, DST, IMM)				\
17 	((struct bpf_insn) {					\
18 		.code  = BPF_ALU | BPF_OP(OP) | BPF_K,		\
19 		.dst_reg = DST,					\
20 		.src_reg = 0,					\
21 		.off   = 0,					\
22 		.imm   = IMM })
23 
24 #define BPF_ALU64_IMM(OP, DST, IMM)				\
25 	((struct bpf_insn) {					\
26 		.code  = BPF_ALU64 | BPF_OP(OP) | BPF_K,	\
27 		.dst_reg = DST,					\
28 		.src_reg = 0,					\
29 		.off   = 0,					\
30 		.imm   = IMM })
31 
32 #define BPF_MOV64_IMM(DST, IMM)					\
33 	((struct bpf_insn) {					\
34 		.code  = BPF_ALU64 | BPF_MOV | BPF_K,		\
35 		.dst_reg = DST,					\
36 		.src_reg = 0,					\
37 		.off   = 0,					\
38 		.imm   = IMM })
39 
40 #define BPF_EXIT_INSN()						\
41 	((struct bpf_insn) {					\
42 		.code  = BPF_JMP | BPF_EXIT,			\
43 		.dst_reg = 0,					\
44 		.src_reg = 0,					\
45 		.off   = 0,					\
46 		.imm   = 0 })
47 
48 #define BPF_EMIT_CALL(FUNC)					\
49 	((struct bpf_insn) {					\
50 		.code  = BPF_JMP | BPF_CALL,			\
51 		.dst_reg = 0,					\
52 		.src_reg = 0,					\
53 		.off   = 0,					\
54 		.imm   = ((FUNC) - BPF_FUNC_unspec) })
55 
56 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF)			\
57 	((struct bpf_insn) {					\
58 		.code  = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM,	\
59 		.dst_reg = DST,					\
60 		.src_reg = SRC,					\
61 		.off   = OFF,					\
62 		.imm   = 0 })
63 
64 #define BPF_STX_MEM(SIZE, DST, SRC, OFF)			\
65 	((struct bpf_insn) {					\
66 		.code  = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM,	\
67 		.dst_reg = DST,					\
68 		.src_reg = SRC,					\
69 		.off   = OFF,					\
70 		.imm   = 0 })
71 
72 #define BPF_ST_MEM(SIZE, DST, OFF, IMM)				\
73 	((struct bpf_insn) {					\
74 		.code  = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM,	\
75 		.dst_reg = DST,					\
76 		.src_reg = 0,					\
77 		.off   = OFF,					\
78 		.imm   = IMM })
79 
80 #define BPF_MOV64_REG(DST, SRC)					\
81 	((struct bpf_insn) {					\
82 		.code  = BPF_ALU64 | BPF_MOV | BPF_X,		\
83 		.dst_reg = DST,					\
84 		.src_reg = SRC,					\
85 		.off   = 0,					\
86 		.imm   = 0 })
87 
88 #define BPF_MOV32_IMM(DST, IMM)					\
89 	((struct bpf_insn) {					\
90 		.code = BPF_ALU | BPF_MOV | BPF_K,		\
91 		.dst_reg = DST,					\
92 		.src_reg = 0,					\
93 		.off   = 0,					\
94 		.imm   = IMM })
95 
96 #define BPF_LD_IMM64_RAW_FULL(DST, SRC, OFF1, OFF2, IMM1, IMM2)	\
97 	((struct bpf_insn) {					\
98 		.code = BPF_LD | BPF_DW | BPF_IMM,		\
99 		.dst_reg = DST,					\
100 		.src_reg = SRC,					\
101 		.off   = OFF1,					\
102 		.imm   = IMM1 }),			\
103 	((struct bpf_insn) {					\
104 		.code  = 0,					\
105 		.dst_reg = 0,					\
106 		.src_reg = 0,					\
107 		.off   = OFF2,					\
108 		.imm   = IMM2 })
109 
110 #define BPF_LD_MAP_FD(DST, MAP_FD)				\
111 	BPF_LD_IMM64_RAW_FULL(DST, BPF_PSEUDO_MAP_FD, 0, 0,	\
112 			      MAP_FD, 0)
113 
114 #define BPF_LD_MAP_VALUE(DST, MAP_FD, VALUE_OFF)		\
115 	BPF_LD_IMM64_RAW_FULL(DST, BPF_PSEUDO_MAP_VALUE, 0, 0,	\
116 			      MAP_FD, VALUE_OFF)
117 
118 #define BPF_JMP_IMM(OP, DST, IMM, OFF)				\
119 	((struct bpf_insn) {					\
120 		.code = BPF_JMP | BPF_OP(OP) | BPF_K,		\
121 		.dst_reg = DST,					\
122 		.src_reg = 0,					\
123 		.off  = OFF,					\
124 		.imm  = IMM })
125 
126 #define BPF_JMP32_IMM(OP, DST, IMM, OFF)			\
127 	((struct bpf_insn) {					\
128 		.code = BPF_JMP32 | BPF_OP(OP) | BPF_K,		\
129 		.dst_reg = DST,					\
130 		.src_reg = 0,					\
131 		.off  = OFF,					\
132 		.imm  = IMM })
133 
134 #endif
135