Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Linux Socket Filter Data Structures |
| 3 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #ifndef __LINUX_FILTER_H__ |
| 5 | #define __LINUX_FILTER_H__ |
| 6 | |
Daniel Borkmann | b954d83 | 2014-09-10 15:01:02 +0200 | [diff] [blame] | 7 | #include <stdarg.h> |
| 8 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 9 | #include <linux/atomic.h> |
Will Drewry | 0c5fe1b | 2012-04-12 16:47:53 -0500 | [diff] [blame] | 10 | #include <linux/compat.h> |
Zi Shen Lim | 9f12fbe | 2014-07-03 07:56:54 -0700 | [diff] [blame] | 11 | #include <linux/skbuff.h> |
Daniel Borkmann | b954d83 | 2014-09-10 15:01:02 +0200 | [diff] [blame] | 12 | #include <linux/linkage.h> |
| 13 | #include <linux/printk.h> |
Alexei Starovoitov | d45ed4a | 2013-10-04 00:14:06 -0700 | [diff] [blame] | 14 | #include <linux/workqueue.h> |
Daniel Borkmann | b954d83 | 2014-09-10 15:01:02 +0200 | [diff] [blame] | 15 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 16 | #include <asm/cacheflush.h> |
Daniel Borkmann | b954d83 | 2014-09-10 15:01:02 +0200 | [diff] [blame] | 17 | |
| 18 | #include <uapi/linux/filter.h> |
Alexei Starovoitov | daedfb2 | 2014-09-04 22:17:18 -0700 | [diff] [blame] | 19 | #include <uapi/linux/bpf.h> |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 20 | |
| 21 | struct sk_buff; |
| 22 | struct sock; |
| 23 | struct seccomp_data; |
Heiko Carstens | 792d4b5 | 2011-05-22 07:08:11 +0000 | [diff] [blame] | 24 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 25 | /* 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 Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 41 | |
| 42 | /* BPF program can access up to 512 bytes of stack space. */ |
| 43 | #define MAX_BPF_STACK 512 |
| 44 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 45 | /* Helper macros for filter block array initializers. */ |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 46 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 47 | /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 48 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 49 | #define BPF_ALU64_REG(OP, DST, SRC) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 50 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 51 | .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 52 | .dst_reg = DST, \ |
| 53 | .src_reg = SRC, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 54 | .off = 0, \ |
| 55 | .imm = 0 }) |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 56 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 57 | #define BPF_ALU32_REG(OP, DST, SRC) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 58 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 59 | .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 60 | .dst_reg = DST, \ |
| 61 | .src_reg = SRC, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 62 | .off = 0, \ |
| 63 | .imm = 0 }) |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 64 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 65 | /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 66 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 67 | #define BPF_ALU64_IMM(OP, DST, IMM) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 68 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 69 | .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 70 | .dst_reg = DST, \ |
| 71 | .src_reg = 0, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 72 | .off = 0, \ |
| 73 | .imm = IMM }) |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 74 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 75 | #define BPF_ALU32_IMM(OP, DST, IMM) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 76 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 77 | .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 78 | .dst_reg = DST, \ |
| 79 | .src_reg = 0, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 80 | .off = 0, \ |
| 81 | .imm = IMM }) |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 82 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 83 | /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */ |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 84 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 85 | #define BPF_ENDIAN(TYPE, DST, LEN) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 86 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 87 | .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 88 | .dst_reg = DST, \ |
| 89 | .src_reg = 0, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 90 | .off = 0, \ |
| 91 | .imm = LEN }) |
| 92 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 93 | /* Short form of mov, dst_reg = src_reg */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 94 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 95 | #define BPF_MOV64_REG(DST, SRC) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 96 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 97 | .code = BPF_ALU64 | BPF_MOV | BPF_X, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 98 | .dst_reg = DST, \ |
| 99 | .src_reg = SRC, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 100 | .off = 0, \ |
| 101 | .imm = 0 }) |
| 102 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 103 | #define BPF_MOV32_REG(DST, SRC) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 104 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 105 | .code = BPF_ALU | BPF_MOV | BPF_X, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 106 | .dst_reg = DST, \ |
| 107 | .src_reg = SRC, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 108 | .off = 0, \ |
| 109 | .imm = 0 }) |
| 110 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 111 | /* Short form of mov, dst_reg = imm32 */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 112 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 113 | #define BPF_MOV64_IMM(DST, IMM) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 114 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 115 | .code = BPF_ALU64 | BPF_MOV | BPF_K, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 116 | .dst_reg = DST, \ |
| 117 | .src_reg = 0, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 118 | .off = 0, \ |
| 119 | .imm = IMM }) |
| 120 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 121 | #define BPF_MOV32_IMM(DST, IMM) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 122 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 123 | .code = BPF_ALU | BPF_MOV | BPF_K, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 124 | .dst_reg = DST, \ |
| 125 | .src_reg = 0, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 126 | .off = 0, \ |
| 127 | .imm = IMM }) |
| 128 | |
Alexei Starovoitov | 02ab695 | 2014-09-04 22:17:17 -0700 | [diff] [blame] | 129 | /* 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 Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 147 | /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 148 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 149 | #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 150 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 151 | .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 152 | .dst_reg = DST, \ |
| 153 | .src_reg = SRC, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 154 | .off = 0, \ |
| 155 | .imm = IMM }) |
| 156 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 157 | #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 158 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 159 | .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 160 | .dst_reg = DST, \ |
| 161 | .src_reg = SRC, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 162 | .off = 0, \ |
| 163 | .imm = IMM }) |
| 164 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 165 | /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 166 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 167 | #define BPF_LD_ABS(SIZE, IMM) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 168 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 169 | .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 170 | .dst_reg = 0, \ |
| 171 | .src_reg = 0, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 172 | .off = 0, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 173 | .imm = IMM }) |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 174 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 175 | /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 176 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 177 | #define BPF_LD_IND(SIZE, SRC, IMM) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 178 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 179 | .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 180 | .dst_reg = 0, \ |
| 181 | .src_reg = SRC, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 182 | .off = 0, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 183 | .imm = IMM }) |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 184 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 185 | /* Memory load, dst_reg = *(uint *) (src_reg + off16) */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 186 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 187 | #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 188 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 189 | .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 190 | .dst_reg = DST, \ |
| 191 | .src_reg = SRC, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 192 | .off = OFF, \ |
| 193 | .imm = 0 }) |
| 194 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 195 | /* Memory store, *(uint *) (dst_reg + off16) = src_reg */ |
| 196 | |
| 197 | #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 198 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 199 | .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 200 | .dst_reg = DST, \ |
| 201 | .src_reg = SRC, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 202 | .off = OFF, \ |
| 203 | .imm = 0 }) |
| 204 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 205 | /* Memory store, *(uint *) (dst_reg + off16) = imm32 */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 206 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 207 | #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 208 | ((struct bpf_insn) { \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 209 | .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 Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 218 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 219 | .code = BPF_JMP | BPF_OP(OP) | BPF_X, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 220 | .dst_reg = DST, \ |
| 221 | .src_reg = SRC, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 222 | .off = OFF, \ |
| 223 | .imm = 0 }) |
| 224 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 225 | /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 226 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 227 | #define BPF_JMP_IMM(OP, DST, IMM, OFF) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 228 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 229 | .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 230 | .dst_reg = DST, \ |
| 231 | .src_reg = 0, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 232 | .off = OFF, \ |
| 233 | .imm = IMM }) |
| 234 | |
| 235 | /* Function call */ |
| 236 | |
| 237 | #define BPF_EMIT_CALL(FUNC) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 238 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 239 | .code = BPF_JMP | BPF_CALL, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 240 | .dst_reg = 0, \ |
| 241 | .src_reg = 0, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 242 | .off = 0, \ |
| 243 | .imm = ((FUNC) - __bpf_call_base) }) |
| 244 | |
| 245 | /* Raw code statement block */ |
| 246 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 247 | #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 248 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 249 | .code = CODE, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 250 | .dst_reg = DST, \ |
| 251 | .src_reg = SRC, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 252 | .off = OFF, \ |
| 253 | .imm = IMM }) |
| 254 | |
| 255 | /* Program exit */ |
| 256 | |
| 257 | #define BPF_EXIT_INSN() \ |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 258 | ((struct bpf_insn) { \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 259 | .code = BPF_JMP | BPF_EXIT, \ |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 260 | .dst_reg = 0, \ |
| 261 | .src_reg = 0, \ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 262 | .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 Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 280 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 281 | /* Macro to invoke filter function. */ |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 282 | #define SK_RUN_FILTER(filter, ctx) \ |
| 283 | (*filter->prog->bpf_func)(ctx, filter->prog->insnsi) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 284 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 285 | #ifdef CONFIG_COMPAT |
| 286 | /* A struct sock_filter is architecture independent. */ |
Will Drewry | 0c5fe1b | 2012-04-12 16:47:53 -0500 | [diff] [blame] | 287 | struct compat_sock_fprog { |
| 288 | u16 len; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 289 | compat_uptr_t filter; /* struct sock_filter * */ |
Will Drewry | 0c5fe1b | 2012-04-12 16:47:53 -0500 | [diff] [blame] | 290 | }; |
| 291 | #endif |
| 292 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 293 | struct sock_fprog_kern { |
| 294 | u16 len; |
| 295 | struct sock_filter *filter; |
| 296 | }; |
| 297 | |
Daniel Borkmann | 738cbe7 | 2014-09-08 08:04:47 +0200 | [diff] [blame] | 298 | struct bpf_binary_header { |
| 299 | unsigned int pages; |
| 300 | u8 image[]; |
| 301 | }; |
| 302 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 303 | struct bpf_work_struct { |
| 304 | struct bpf_prog *prog; |
| 305 | struct work_struct work; |
| 306 | }; |
Heiko Carstens | 792d4b5 | 2011-05-22 07:08:11 +0000 | [diff] [blame] | 307 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 308 | struct bpf_prog { |
Daniel Borkmann | 286aad3 | 2014-09-08 08:04:49 +0200 | [diff] [blame] | 309 | u16 pages; /* Number of allocated pages */ |
| 310 | bool jited; /* Is our filter JIT'ed? */ |
| 311 | u32 len; /* Number of filter blocks */ |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 312 | struct sock_fprog_kern *orig_prog; /* Original BPF program */ |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 313 | struct bpf_work_struct *work; /* Deferred free work struct */ |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 314 | unsigned int (*bpf_func)(const struct sk_buff *skb, |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 315 | const struct bpf_insn *filter); |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 316 | /* Instructions for interpreter */ |
Alexei Starovoitov | d45ed4a | 2013-10-04 00:14:06 -0700 | [diff] [blame] | 317 | union { |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 318 | struct sock_filter insns[0]; |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 319 | struct bpf_insn insnsi[0]; |
Alexei Starovoitov | d45ed4a | 2013-10-04 00:14:06 -0700 | [diff] [blame] | 320 | }; |
Stephen Hemminger | b715631 | 2008-04-10 01:33:47 -0700 | [diff] [blame] | 321 | }; |
| 322 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 323 | struct 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 | |
| 331 | static inline unsigned int bpf_prog_size(unsigned int proglen) |
Stephen Hemminger | b715631 | 2008-04-10 01:33:47 -0700 | [diff] [blame] | 332 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 333 | return max(sizeof(struct bpf_prog), |
| 334 | offsetof(struct bpf_prog, insns[proglen])); |
Stephen Hemminger | b715631 | 2008-04-10 01:33:47 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Alexei Starovoitov | 009937e | 2014-07-30 20:34:13 -0700 | [diff] [blame] | 337 | #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0])) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 338 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 339 | #ifdef CONFIG_DEBUG_SET_MODULE_RONX |
| 340 | static inline void bpf_prog_lock_ro(struct bpf_prog *fp) |
| 341 | { |
| 342 | set_memory_ro((unsigned long)fp, fp->pages); |
| 343 | } |
| 344 | |
| 345 | static inline void bpf_prog_unlock_ro(struct bpf_prog *fp) |
| 346 | { |
| 347 | set_memory_rw((unsigned long)fp, fp->pages); |
| 348 | } |
| 349 | #else |
| 350 | static inline void bpf_prog_lock_ro(struct bpf_prog *fp) |
| 351 | { |
| 352 | } |
| 353 | |
| 354 | static inline void bpf_prog_unlock_ro(struct bpf_prog *fp) |
| 355 | { |
| 356 | } |
| 357 | #endif /* CONFIG_DEBUG_SET_MODULE_RONX */ |
| 358 | |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 359 | int sk_filter(struct sock *sk, struct sk_buff *skb); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 360 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 361 | void bpf_prog_select_runtime(struct bpf_prog *fp); |
| 362 | void bpf_prog_free(struct bpf_prog *fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 363 | |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 364 | int bpf_convert_filter(struct sock_filter *prog, int len, |
| 365 | struct bpf_insn *new_prog, int *new_len); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 366 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 367 | struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags); |
| 368 | struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, |
| 369 | gfp_t gfp_extra_flags); |
| 370 | void __bpf_prog_free(struct bpf_prog *fp); |
| 371 | |
| 372 | static 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 Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 378 | int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog); |
| 379 | void bpf_prog_destroy(struct bpf_prog *fp); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 380 | |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 381 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk); |
| 382 | int sk_detach_filter(struct sock *sk); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 383 | |
Alexei Starovoitov | 4df95ff | 2014-07-30 20:34:14 -0700 | [diff] [blame] | 384 | int bpf_check_classic(const struct sock_filter *filter, unsigned int flen); |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 385 | int sk_get_filter(struct sock *sk, struct sock_filter __user *filter, |
| 386 | unsigned int len); |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 387 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 388 | bool sk_filter_charge(struct sock *sk, struct sk_filter *fp); |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 389 | void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp); |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 390 | |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame] | 391 | u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 392 | void bpf_int_jit_compile(struct bpf_prog *fp); |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame] | 393 | |
Daniel Borkmann | b954d83 | 2014-09-10 15:01:02 +0200 | [diff] [blame] | 394 | #ifdef CONFIG_BPF_JIT |
| 395 | typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size); |
| 396 | |
| 397 | struct bpf_binary_header * |
| 398 | bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, |
| 399 | unsigned int alignment, |
| 400 | bpf_jit_fill_hole_t bpf_fill_ill_insns); |
| 401 | void bpf_jit_binary_free(struct bpf_binary_header *hdr); |
| 402 | |
| 403 | void bpf_jit_compile(struct bpf_prog *fp); |
| 404 | void bpf_jit_free(struct bpf_prog *fp); |
| 405 | |
| 406 | static 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 |
| 416 | static inline void bpf_jit_compile(struct bpf_prog *fp) |
| 417 | { |
| 418 | } |
| 419 | |
| 420 | static inline void bpf_jit_free(struct bpf_prog *fp) |
| 421 | { |
| 422 | bpf_prog_unlock_free(fp); |
| 423 | } |
| 424 | #endif /* CONFIG_BPF_JIT */ |
| 425 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 426 | #define BPF_ANC BIT(15) |
| 427 | |
| 428 | static 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 Lim | 9f12fbe | 2014-07-03 07:56:54 -0700 | [diff] [blame] | 461 | void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, |
| 462 | int k, unsigned int size); |
| 463 | |
| 464 | static 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 Sekletar | ea02f94 | 2014-01-17 17:09:45 +0100 | [diff] [blame] | 473 | static inline int bpf_tell_extensions(void) |
| 474 | { |
Daniel Borkmann | 3769229 | 2014-01-21 00:19:37 +0100 | [diff] [blame] | 475 | return SKF_AD_MAX; |
Michal Sekletar | ea02f94 | 2014-01-17 17:09:45 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | #endif /* __LINUX_FILTER_H__ */ |