ebpf.rs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. // Copyright 2016 6WIND S.A. <[email protected]>
  3. //! This module contains all the definitions related to eBPF, and some functions permitting to
  4. //! manipulate eBPF instructions.
  5. //!
  6. //! The number of bytes in an instruction, the maximum number of instructions in a program, and
  7. //! also all operation codes are defined here as constants.
  8. //!
  9. //! The structure for an instruction used by this crate, as well as the function to extract it from
  10. //! a program, is also defined in the module.
  11. //!
  12. //! To learn more about these instructions, see the Linux kernel documentation:
  13. //! <https://www.kernel.org/doc/Documentation/networking/filter.txt>, or for a shorter version of
  14. //! the list of the operation codes: <https://github.com/iovisor/bpf-docs/blob/master/eBPF.md>
  15. use alloc::{vec, vec::Vec};
  16. use byteorder::{ByteOrder, LittleEndian};
  17. /// The maximum call depth is 8
  18. pub const RBPF_MAX_CALL_DEPTH: usize = 8;
  19. /// Maximum number of instructions in an eBPF program.
  20. pub const PROG_MAX_INSNS: usize = 1000000;
  21. /// Size of an eBPF instructions, in bytes.
  22. pub const INSN_SIZE: usize = 8;
  23. /// Maximum size of an eBPF program, in bytes.
  24. pub const PROG_MAX_SIZE: usize = PROG_MAX_INSNS * INSN_SIZE;
  25. /// Stack for the eBPF stack, in bytes.
  26. pub const STACK_SIZE: usize = 512;
  27. // eBPF op codes.
  28. // See also https://www.kernel.org/doc/Documentation/networking/filter.txt
  29. // Three least significant bits are operation class:
  30. /// BPF operation class: load from immediate.
  31. pub const BPF_LD: u8 = 0x00;
  32. /// BPF operation class: load from register.
  33. pub const BPF_LDX: u8 = 0x01;
  34. /// BPF operation class: store immediate.
  35. pub const BPF_ST: u8 = 0x02;
  36. /// BPF operation class: store value from register.
  37. pub const BPF_STX: u8 = 0x03;
  38. /// BPF operation class: 32 bits arithmetic operation.
  39. pub const BPF_ALU: u8 = 0x04;
  40. /// BPF operation class: jump (64-bit wide operands for comparisons).
  41. pub const BPF_JMP: u8 = 0x05;
  42. /// BPF operation class: jump (32-bit wide operands for comparisons).
  43. pub const BPF_JMP32: u8 = 0x06;
  44. // [ class 6 unused, reserved for future use ]
  45. /// BPF operation class: 64 bits arithmetic operation.
  46. pub const BPF_ALU64: u8 = 0x07;
  47. // For load and store instructions:
  48. // +------------+--------+------------+
  49. // | 3 bits | 2 bits | 3 bits |
  50. // | mode | size | insn class |
  51. // +------------+--------+------------+
  52. // (MSB) (LSB)
  53. // Size modifiers:
  54. /// BPF size modifier: word (4 bytes).
  55. pub const BPF_W: u8 = 0x00;
  56. /// BPF size modifier: half-word (2 bytes).
  57. pub const BPF_H: u8 = 0x08;
  58. /// BPF size modifier: byte (1 byte).
  59. pub const BPF_B: u8 = 0x10;
  60. /// BPF size modifier: double word (8 bytes).
  61. pub const BPF_DW: u8 = 0x18;
  62. // Mode modifiers:
  63. /// BPF mode modifier: immediate value.
  64. pub const BPF_IMM: u8 = 0x00;
  65. /// BPF mode modifier: absolute load.
  66. pub const BPF_ABS: u8 = 0x20;
  67. /// BPF mode modifier: indirect load.
  68. pub const BPF_IND: u8 = 0x40;
  69. /// BPF mode modifier: load from / store to memory.
  70. pub const BPF_MEM: u8 = 0x60;
  71. // [ 0x80 reserved ]
  72. // [ 0xa0 reserved ]
  73. /// BPF mode modifier: exclusive add.
  74. pub const BPF_XADD: u8 = 0xc0;
  75. // For arithmetic (BPF_ALU/BPF_ALU64) and jump (BPF_JMP) instructions:
  76. // +----------------+--------+--------+
  77. // | 4 bits |1 b.| 3 bits |
  78. // | operation code | src| insn class |
  79. // +----------------+----+------------+
  80. // (MSB) (LSB)
  81. // Source modifiers:
  82. /// BPF source operand modifier: 32-bit immediate value.
  83. pub const BPF_K: u8 = 0x00;
  84. /// BPF source operand modifier: `src` register.
  85. pub const BPF_X: u8 = 0x08;
  86. // Operation codes -- BPF_ALU or BPF_ALU64 classes:
  87. /// BPF ALU/ALU64 operation code: addition.
  88. pub const BPF_ADD: u8 = 0x00;
  89. /// BPF ALU/ALU64 operation code: subtraction.
  90. pub const BPF_SUB: u8 = 0x10;
  91. /// BPF ALU/ALU64 operation code: multiplication.
  92. pub const BPF_MUL: u8 = 0x20;
  93. /// BPF ALU/ALU64 operation code: division.
  94. pub const BPF_DIV: u8 = 0x30;
  95. /// BPF ALU/ALU64 operation code: or.
  96. pub const BPF_OR: u8 = 0x40;
  97. /// BPF ALU/ALU64 operation code: and.
  98. pub const BPF_AND: u8 = 0x50;
  99. /// BPF ALU/ALU64 operation code: left shift.
  100. pub const BPF_LSH: u8 = 0x60;
  101. /// BPF ALU/ALU64 operation code: right shift.
  102. pub const BPF_RSH: u8 = 0x70;
  103. /// BPF ALU/ALU64 operation code: negation.
  104. pub const BPF_NEG: u8 = 0x80;
  105. /// BPF ALU/ALU64 operation code: modulus.
  106. pub const BPF_MOD: u8 = 0x90;
  107. /// BPF ALU/ALU64 operation code: exclusive or.
  108. pub const BPF_XOR: u8 = 0xa0;
  109. /// BPF ALU/ALU64 operation code: move.
  110. pub const BPF_MOV: u8 = 0xb0;
  111. /// BPF ALU/ALU64 operation code: sign extending right shift.
  112. pub const BPF_ARSH: u8 = 0xc0;
  113. /// BPF ALU/ALU64 operation code: endianness conversion.
  114. pub const BPF_END: u8 = 0xd0;
  115. // Operation codes -- BPF_JMP or BPF_JMP32 classes:
  116. /// BPF JMP operation code: jump.
  117. pub const BPF_JA: u8 = 0x00;
  118. /// BPF JMP operation code: jump if equal.
  119. pub const BPF_JEQ: u8 = 0x10;
  120. /// BPF JMP operation code: jump if greater than.
  121. pub const BPF_JGT: u8 = 0x20;
  122. /// BPF JMP operation code: jump if greater or equal.
  123. pub const BPF_JGE: u8 = 0x30;
  124. /// BPF JMP operation code: jump if `src` & `reg`.
  125. pub const BPF_JSET: u8 = 0x40;
  126. /// BPF JMP operation code: jump if not equal.
  127. pub const BPF_JNE: u8 = 0x50;
  128. /// BPF JMP operation code: jump if greater than (signed).
  129. pub const BPF_JSGT: u8 = 0x60;
  130. /// BPF JMP operation code: jump if greater or equal (signed).
  131. pub const BPF_JSGE: u8 = 0x70;
  132. /// BPF JMP operation code: helper function call.
  133. pub const BPF_CALL: u8 = 0x80;
  134. /// BPF JMP operation code: return from program.
  135. pub const BPF_EXIT: u8 = 0x90;
  136. /// BPF JMP operation code: jump if lower than.
  137. pub const BPF_JLT: u8 = 0xa0;
  138. /// BPF JMP operation code: jump if lower or equal.
  139. pub const BPF_JLE: u8 = 0xb0;
  140. /// BPF JMP operation code: jump if lower than (signed).
  141. pub const BPF_JSLT: u8 = 0xc0;
  142. /// BPF JMP operation code: jump if lower or equal (signed).
  143. pub const BPF_JSLE: u8 = 0xd0;
  144. // Op codes
  145. // (Following operation names are not “official”, but may be proper to rbpf; Linux kernel only
  146. // combines above flags and does not attribute a name per operation.)
  147. /// BPF opcode: `ldabsb src, dst, imm`.
  148. pub const LD_ABS_B: u8 = BPF_LD | BPF_ABS | BPF_B;
  149. /// BPF opcode: `ldabsh src, dst, imm`.
  150. pub const LD_ABS_H: u8 = BPF_LD | BPF_ABS | BPF_H;
  151. /// BPF opcode: `ldabsw src, dst, imm`.
  152. pub const LD_ABS_W: u8 = BPF_LD | BPF_ABS | BPF_W;
  153. /// BPF opcode: `ldabsdw src, dst, imm`.
  154. pub const LD_ABS_DW: u8 = BPF_LD | BPF_ABS | BPF_DW;
  155. /// BPF opcode: `ldindb src, dst, imm`.
  156. pub const LD_IND_B: u8 = BPF_LD | BPF_IND | BPF_B;
  157. /// BPF opcode: `ldindh src, dst, imm`.
  158. pub const LD_IND_H: u8 = BPF_LD | BPF_IND | BPF_H;
  159. /// BPF opcode: `ldindw src, dst, imm`.
  160. pub const LD_IND_W: u8 = BPF_LD | BPF_IND | BPF_W;
  161. /// BPF opcode: `ldinddw src, dst, imm`.
  162. pub const LD_IND_DW: u8 = BPF_LD | BPF_IND | BPF_DW;
  163. #[allow(unknown_lints)]
  164. #[allow(clippy::eq_op)]
  165. /// BPF opcode: `lddw dst, imm` /// `dst = imm`.
  166. pub const LD_DW_IMM: u8 = BPF_LD | BPF_IMM | BPF_DW;
  167. /// BPF opcode: `ldxb dst, [src + off]` /// `dst = (src + off) as u8`.
  168. pub const LD_B_REG: u8 = BPF_LDX | BPF_MEM | BPF_B;
  169. /// BPF opcode: `ldxh dst, [src + off]` /// `dst = (src + off) as u16`.
  170. pub const LD_H_REG: u8 = BPF_LDX | BPF_MEM | BPF_H;
  171. /// BPF opcode: `ldxw dst, [src + off]` /// `dst = (src + off) as u32`.
  172. pub const LD_W_REG: u8 = BPF_LDX | BPF_MEM | BPF_W;
  173. /// BPF opcode: `ldxdw dst, [src + off]` /// `dst = (src + off) as u64`.
  174. pub const LD_DW_REG: u8 = BPF_LDX | BPF_MEM | BPF_DW;
  175. /// BPF opcode: `stb [dst + off], imm` /// `(dst + offset) as u8 = imm`.
  176. pub const ST_B_IMM: u8 = BPF_ST | BPF_MEM | BPF_B;
  177. /// BPF opcode: `sth [dst + off], imm` /// `(dst + offset) as u16 = imm`.
  178. pub const ST_H_IMM: u8 = BPF_ST | BPF_MEM | BPF_H;
  179. /// BPF opcode: `stw [dst + off], imm` /// `(dst + offset) as u32 = imm`.
  180. pub const ST_W_IMM: u8 = BPF_ST | BPF_MEM | BPF_W;
  181. /// BPF opcode: `stdw [dst + off], imm` /// `(dst + offset) as u64 = imm`.
  182. pub const ST_DW_IMM: u8 = BPF_ST | BPF_MEM | BPF_DW;
  183. /// BPF opcode: `stxb [dst + off], src` /// `(dst + offset) as u8 = src`.
  184. pub const ST_B_REG: u8 = BPF_STX | BPF_MEM | BPF_B;
  185. /// BPF opcode: `stxh [dst + off], src` /// `(dst + offset) as u16 = src`.
  186. pub const ST_H_REG: u8 = BPF_STX | BPF_MEM | BPF_H;
  187. /// BPF opcode: `stxw [dst + off], src` /// `(dst + offset) as u32 = src`.
  188. pub const ST_W_REG: u8 = BPF_STX | BPF_MEM | BPF_W;
  189. /// BPF opcode: `stxdw [dst + off], src` /// `(dst + offset) as u64 = src`.
  190. pub const ST_DW_REG: u8 = BPF_STX | BPF_MEM | BPF_DW;
  191. /// BPF opcode: `stxxaddw [dst + off], src`.
  192. pub const ST_W_XADD: u8 = BPF_STX | BPF_XADD | BPF_W;
  193. /// BPF opcode: `stxxadddw [dst + off], src`.
  194. pub const ST_DW_XADD: u8 = BPF_STX | BPF_XADD | BPF_DW;
  195. /// BPF opcode: `add32 dst, imm` /// `dst += imm`.
  196. pub const ADD32_IMM: u8 = BPF_ALU | BPF_K | BPF_ADD;
  197. /// BPF opcode: `add32 dst, src` /// `dst += src`.
  198. pub const ADD32_REG: u8 = BPF_ALU | BPF_X | BPF_ADD;
  199. /// BPF opcode: `sub32 dst, imm` /// `dst -= imm`.
  200. pub const SUB32_IMM: u8 = BPF_ALU | BPF_K | BPF_SUB;
  201. /// BPF opcode: `sub32 dst, src` /// `dst -= src`.
  202. pub const SUB32_REG: u8 = BPF_ALU | BPF_X | BPF_SUB;
  203. /// BPF opcode: `mul32 dst, imm` /// `dst *= imm`.
  204. pub const MUL32_IMM: u8 = BPF_ALU | BPF_K | BPF_MUL;
  205. /// BPF opcode: `mul32 dst, src` /// `dst *= src`.
  206. pub const MUL32_REG: u8 = BPF_ALU | BPF_X | BPF_MUL;
  207. /// BPF opcode: `div32 dst, imm` /// `dst /= imm`.
  208. pub const DIV32_IMM: u8 = BPF_ALU | BPF_K | BPF_DIV;
  209. /// BPF opcode: `div32 dst, src` /// `dst /= src`.
  210. pub const DIV32_REG: u8 = BPF_ALU | BPF_X | BPF_DIV;
  211. /// BPF opcode: `or32 dst, imm` /// `dst |= imm`.
  212. pub const OR32_IMM: u8 = BPF_ALU | BPF_K | BPF_OR;
  213. /// BPF opcode: `or32 dst, src` /// `dst |= src`.
  214. pub const OR32_REG: u8 = BPF_ALU | BPF_X | BPF_OR;
  215. /// BPF opcode: `and32 dst, imm` /// `dst &= imm`.
  216. pub const AND32_IMM: u8 = BPF_ALU | BPF_K | BPF_AND;
  217. /// BPF opcode: `and32 dst, src` /// `dst &= src`.
  218. pub const AND32_REG: u8 = BPF_ALU | BPF_X | BPF_AND;
  219. /// BPF opcode: `lsh32 dst, imm` /// `dst <<= imm`.
  220. pub const LSH32_IMM: u8 = BPF_ALU | BPF_K | BPF_LSH;
  221. /// BPF opcode: `lsh32 dst, src` /// `dst <<= src`.
  222. pub const LSH32_REG: u8 = BPF_ALU | BPF_X | BPF_LSH;
  223. /// BPF opcode: `rsh32 dst, imm` /// `dst >>= imm`.
  224. pub const RSH32_IMM: u8 = BPF_ALU | BPF_K | BPF_RSH;
  225. /// BPF opcode: `rsh32 dst, src` /// `dst >>= src`.
  226. pub const RSH32_REG: u8 = BPF_ALU | BPF_X | BPF_RSH;
  227. /// BPF opcode: `neg32 dst` /// `dst = -dst`.
  228. pub const NEG32: u8 = BPF_ALU | BPF_NEG;
  229. /// BPF opcode: `mod32 dst, imm` /// `dst %= imm`.
  230. pub const MOD32_IMM: u8 = BPF_ALU | BPF_K | BPF_MOD;
  231. /// BPF opcode: `mod32 dst, src` /// `dst %= src`.
  232. pub const MOD32_REG: u8 = BPF_ALU | BPF_X | BPF_MOD;
  233. /// BPF opcode: `xor32 dst, imm` /// `dst ^= imm`.
  234. pub const XOR32_IMM: u8 = BPF_ALU | BPF_K | BPF_XOR;
  235. /// BPF opcode: `xor32 dst, src` /// `dst ^= src`.
  236. pub const XOR32_REG: u8 = BPF_ALU | BPF_X | BPF_XOR;
  237. /// BPF opcode: `mov32 dst, imm` /// `dst = imm`.
  238. pub const MOV32_IMM: u8 = BPF_ALU | BPF_K | BPF_MOV;
  239. /// BPF opcode: `mov32 dst, src` /// `dst = src`.
  240. pub const MOV32_REG: u8 = BPF_ALU | BPF_X | BPF_MOV;
  241. /// BPF opcode: `arsh32 dst, imm` /// `dst >>= imm (arithmetic)`.
  242. ///
  243. /// <https://en.wikipedia.org/wiki/Arithmetic_shift>
  244. pub const ARSH32_IMM: u8 = BPF_ALU | BPF_K | BPF_ARSH;
  245. /// BPF opcode: `arsh32 dst, src` /// `dst >>= src (arithmetic)`.
  246. ///
  247. /// <https://en.wikipedia.org/wiki/Arithmetic_shift>
  248. pub const ARSH32_REG: u8 = BPF_ALU | BPF_X | BPF_ARSH;
  249. /// BPF opcode: `le dst` /// `dst = htole<imm>(dst), with imm in {16, 32, 64}`.
  250. pub const LE: u8 = BPF_ALU | BPF_K | BPF_END;
  251. /// BPF opcode: `be dst` /// `dst = htobe<imm>(dst), with imm in {16, 32, 64}`.
  252. pub const BE: u8 = BPF_ALU | BPF_X | BPF_END;
  253. /// BPF opcode: `add64 dst, imm` /// `dst += imm`.
  254. pub const ADD64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_ADD;
  255. /// BPF opcode: `add64 dst, src` /// `dst += src`.
  256. pub const ADD64_REG: u8 = BPF_ALU64 | BPF_X | BPF_ADD;
  257. /// BPF opcode: `sub64 dst, imm` /// `dst -= imm`.
  258. pub const SUB64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_SUB;
  259. /// BPF opcode: `sub64 dst, src` /// `dst -= src`.
  260. pub const SUB64_REG: u8 = BPF_ALU64 | BPF_X | BPF_SUB;
  261. /// BPF opcode: `div64 dst, imm` /// `dst /= imm`.
  262. pub const MUL64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_MUL;
  263. /// BPF opcode: `div64 dst, src` /// `dst /= src`.
  264. pub const MUL64_REG: u8 = BPF_ALU64 | BPF_X | BPF_MUL;
  265. /// BPF opcode: `div64 dst, imm` /// `dst /= imm`.
  266. pub const DIV64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_DIV;
  267. /// BPF opcode: `div64 dst, src` /// `dst /= src`.
  268. pub const DIV64_REG: u8 = BPF_ALU64 | BPF_X | BPF_DIV;
  269. /// BPF opcode: `or64 dst, imm` /// `dst |= imm`.
  270. pub const OR64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_OR;
  271. /// BPF opcode: `or64 dst, src` /// `dst |= src`.
  272. pub const OR64_REG: u8 = BPF_ALU64 | BPF_X | BPF_OR;
  273. /// BPF opcode: `and64 dst, imm` /// `dst &= imm`.
  274. pub const AND64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_AND;
  275. /// BPF opcode: `and64 dst, src` /// `dst &= src`.
  276. pub const AND64_REG: u8 = BPF_ALU64 | BPF_X | BPF_AND;
  277. /// BPF opcode: `lsh64 dst, imm` /// `dst <<= imm`.
  278. pub const LSH64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_LSH;
  279. /// BPF opcode: `lsh64 dst, src` /// `dst <<= src`.
  280. pub const LSH64_REG: u8 = BPF_ALU64 | BPF_X | BPF_LSH;
  281. /// BPF opcode: `rsh64 dst, imm` /// `dst >>= imm`.
  282. pub const RSH64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_RSH;
  283. /// BPF opcode: `rsh64 dst, src` /// `dst >>= src`.
  284. pub const RSH64_REG: u8 = BPF_ALU64 | BPF_X | BPF_RSH;
  285. /// BPF opcode: `neg64 dst, imm` /// `dst = -dst`.
  286. pub const NEG64: u8 = BPF_ALU64 | BPF_NEG;
  287. /// BPF opcode: `mod64 dst, imm` /// `dst %= imm`.
  288. pub const MOD64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_MOD;
  289. /// BPF opcode: `mod64 dst, src` /// `dst %= src`.
  290. pub const MOD64_REG: u8 = BPF_ALU64 | BPF_X | BPF_MOD;
  291. /// BPF opcode: `xor64 dst, imm` /// `dst ^= imm`.
  292. pub const XOR64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_XOR;
  293. /// BPF opcode: `xor64 dst, src` /// `dst ^= src`.
  294. pub const XOR64_REG: u8 = BPF_ALU64 | BPF_X | BPF_XOR;
  295. /// BPF opcode: `mov64 dst, imm` /// `dst = imm`.
  296. pub const MOV64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_MOV;
  297. /// BPF opcode: `mov64 dst, src` /// `dst = src`.
  298. pub const MOV64_REG: u8 = BPF_ALU64 | BPF_X | BPF_MOV;
  299. /// BPF opcode: `arsh64 dst, imm` /// `dst >>= imm (arithmetic)`.
  300. ///
  301. /// <https://en.wikipedia.org/wiki/Arithmetic_shift>
  302. pub const ARSH64_IMM: u8 = BPF_ALU64 | BPF_K | BPF_ARSH;
  303. /// BPF opcode: `arsh64 dst, src` /// `dst >>= src (arithmetic)`.
  304. ///
  305. /// <https://en.wikipedia.org/wiki/Arithmetic_shift>
  306. pub const ARSH64_REG: u8 = BPF_ALU64 | BPF_X | BPF_ARSH;
  307. /// BPF opcode: `ja +off` /// `PC += off`.
  308. pub const JA: u8 = BPF_JMP | BPF_JA;
  309. /// BPF opcode: `jeq dst, imm, +off` /// `PC += off if dst == imm`.
  310. pub const JEQ_IMM: u8 = BPF_JMP | BPF_K | BPF_JEQ;
  311. /// BPF opcode: `jeq dst, src, +off` /// `PC += off if dst == src`.
  312. pub const JEQ_REG: u8 = BPF_JMP | BPF_X | BPF_JEQ;
  313. /// BPF opcode: `jgt dst, imm, +off` /// `PC += off if dst > imm`.
  314. pub const JGT_IMM: u8 = BPF_JMP | BPF_K | BPF_JGT;
  315. /// BPF opcode: `jgt dst, src, +off` /// `PC += off if dst > src`.
  316. pub const JGT_REG: u8 = BPF_JMP | BPF_X | BPF_JGT;
  317. /// BPF opcode: `jge dst, imm, +off` /// `PC += off if dst >= imm`.
  318. pub const JGE_IMM: u8 = BPF_JMP | BPF_K | BPF_JGE;
  319. /// BPF opcode: `jge dst, src, +off` /// `PC += off if dst >= src`.
  320. pub const JGE_REG: u8 = BPF_JMP | BPF_X | BPF_JGE;
  321. /// BPF opcode: `jlt dst, imm, +off` /// `PC += off if dst < imm`.
  322. pub const JLT_IMM: u8 = BPF_JMP | BPF_K | BPF_JLT;
  323. /// BPF opcode: `jlt dst, src, +off` /// `PC += off if dst < src`.
  324. pub const JLT_REG: u8 = BPF_JMP | BPF_X | BPF_JLT;
  325. /// BPF opcode: `jle dst, imm, +off` /// `PC += off if dst <= imm`.
  326. pub const JLE_IMM: u8 = BPF_JMP | BPF_K | BPF_JLE;
  327. /// BPF opcode: `jle dst, src, +off` /// `PC += off if dst <= src`.
  328. pub const JLE_REG: u8 = BPF_JMP | BPF_X | BPF_JLE;
  329. /// BPF opcode: `jset dst, imm, +off` /// `PC += off if dst & imm`.
  330. pub const JSET_IMM: u8 = BPF_JMP | BPF_K | BPF_JSET;
  331. /// BPF opcode: `jset dst, src, +off` /// `PC += off if dst & src`.
  332. pub const JSET_REG: u8 = BPF_JMP | BPF_X | BPF_JSET;
  333. /// BPF opcode: `jne dst, imm, +off` /// `PC += off if dst != imm`.
  334. pub const JNE_IMM: u8 = BPF_JMP | BPF_K | BPF_JNE;
  335. /// BPF opcode: `jne dst, src, +off` /// `PC += off if dst != src`.
  336. pub const JNE_REG: u8 = BPF_JMP | BPF_X | BPF_JNE;
  337. /// BPF opcode: `jsgt dst, imm, +off` /// `PC += off if dst > imm (signed)`.
  338. pub const JSGT_IMM: u8 = BPF_JMP | BPF_K | BPF_JSGT;
  339. /// BPF opcode: `jsgt dst, src, +off` /// `PC += off if dst > src (signed)`.
  340. pub const JSGT_REG: u8 = BPF_JMP | BPF_X | BPF_JSGT;
  341. /// BPF opcode: `jsge dst, imm, +off` /// `PC += off if dst >= imm (signed)`.
  342. pub const JSGE_IMM: u8 = BPF_JMP | BPF_K | BPF_JSGE;
  343. /// BPF opcode: `jsge dst, src, +off` /// `PC += off if dst >= src (signed)`.
  344. pub const JSGE_REG: u8 = BPF_JMP | BPF_X | BPF_JSGE;
  345. /// BPF opcode: `jslt dst, imm, +off` /// `PC += off if dst < imm (signed)`.
  346. pub const JSLT_IMM: u8 = BPF_JMP | BPF_K | BPF_JSLT;
  347. /// BPF opcode: `jslt dst, src, +off` /// `PC += off if dst < src (signed)`.
  348. pub const JSLT_REG: u8 = BPF_JMP | BPF_X | BPF_JSLT;
  349. /// BPF opcode: `jsle dst, imm, +off` /// `PC += off if dst <= imm (signed)`.
  350. pub const JSLE_IMM: u8 = BPF_JMP | BPF_K | BPF_JSLE;
  351. /// BPF opcode: `jsle dst, src, +off` /// `PC += off if dst <= src (signed)`.
  352. pub const JSLE_REG: u8 = BPF_JMP | BPF_X | BPF_JSLE;
  353. /// BPF opcode: `jeq dst, imm, +off` /// `PC += off if (dst as u32) == imm`.
  354. pub const JEQ_IMM32: u8 = BPF_JMP32 | BPF_K | BPF_JEQ;
  355. /// BPF opcode: `jeq dst, src, +off` /// `PC += off if (dst as u32) == (src as u32)`.
  356. pub const JEQ_REG32: u8 = BPF_JMP32 | BPF_X | BPF_JEQ;
  357. /// BPF opcode: `jgt dst, imm, +off` /// `PC += off if (dst as u32) > imm`.
  358. pub const JGT_IMM32: u8 = BPF_JMP32 | BPF_K | BPF_JGT;
  359. /// BPF opcode: `jgt dst, src, +off` /// `PC += off if (dst as u32) > (src as u32)`.
  360. pub const JGT_REG32: u8 = BPF_JMP32 | BPF_X | BPF_JGT;
  361. /// BPF opcode: `jge dst, imm, +off` /// `PC += off if (dst as u32) >= imm`.
  362. pub const JGE_IMM32: u8 = BPF_JMP32 | BPF_K | BPF_JGE;
  363. /// BPF opcode: `jge dst, src, +off` /// `PC += off if (dst as u32) >= (src as u32)`.
  364. pub const JGE_REG32: u8 = BPF_JMP32 | BPF_X | BPF_JGE;
  365. /// BPF opcode: `jlt dst, imm, +off` /// `PC += off if (dst as u32) < imm`.
  366. pub const JLT_IMM32: u8 = BPF_JMP32 | BPF_K | BPF_JLT;
  367. /// BPF opcode: `jlt dst, src, +off` /// `PC += off if (dst as u32) < (src as u32)`.
  368. pub const JLT_REG32: u8 = BPF_JMP32 | BPF_X | BPF_JLT;
  369. /// BPF opcode: `jle dst, imm, +off` /// `PC += off if (dst as u32) <= imm`.
  370. pub const JLE_IMM32: u8 = BPF_JMP32 | BPF_K | BPF_JLE;
  371. /// BPF opcode: `jle dst, src, +off` /// `PC += off if (dst as u32) <= (src as u32)`.
  372. pub const JLE_REG32: u8 = BPF_JMP32 | BPF_X | BPF_JLE;
  373. /// BPF opcode: `jset dst, imm, +off` /// `PC += off if (dst as u32) & imm`.
  374. pub const JSET_IMM32: u8 = BPF_JMP32 | BPF_K | BPF_JSET;
  375. /// BPF opcode: `jset dst, src, +off` /// `PC += off if (dst as u32) & (src as u32)`.
  376. pub const JSET_REG32: u8 = BPF_JMP32 | BPF_X | BPF_JSET;
  377. /// BPF opcode: `jne dst, imm, +off` /// `PC += off if (dst as u32) != imm`.
  378. pub const JNE_IMM32: u8 = BPF_JMP32 | BPF_K | BPF_JNE;
  379. /// BPF opcode: `jne dst, src, +off` /// `PC += off if (dst as u32) != (src as u32)`.
  380. pub const JNE_REG32: u8 = BPF_JMP32 | BPF_X | BPF_JNE;
  381. /// BPF opcode: `jsgt dst, imm, +off` /// `PC += off if (dst as i32) > imm (signed)`.
  382. pub const JSGT_IMM32: u8 = BPF_JMP32 | BPF_K | BPF_JSGT;
  383. /// BPF opcode: `jsgt dst, src, +off` /// `PC += off if (dst as i32) > (src as i32) (signed)`.
  384. pub const JSGT_REG32: u8 = BPF_JMP32 | BPF_X | BPF_JSGT;
  385. /// BPF opcode: `jsge dst, imm, +off` /// `PC += off if (dst as i32) >= imm (signed)`.
  386. pub const JSGE_IMM32: u8 = BPF_JMP32 | BPF_K | BPF_JSGE;
  387. /// BPF opcode: `jsge dst, src, +off` /// `PC += off if (dst as i32) >= (src as i32) (signed)`.
  388. pub const JSGE_REG32: u8 = BPF_JMP32 | BPF_X | BPF_JSGE;
  389. /// BPF opcode: `jslt dst, imm, +off` /// `PC += off if (dst as i32) < imm (signed)`.
  390. pub const JSLT_IMM32: u8 = BPF_JMP32 | BPF_K | BPF_JSLT;
  391. /// BPF opcode: `jslt dst, src, +off` /// `PC += off if (dst as i32) < (src as i32) (signed)`.
  392. pub const JSLT_REG32: u8 = BPF_JMP32 | BPF_X | BPF_JSLT;
  393. /// BPF opcode: `jsle dst, imm, +off` /// `PC += off if (dst as i32) <= imm (signed)`.
  394. pub const JSLE_IMM32: u8 = BPF_JMP32 | BPF_K | BPF_JSLE;
  395. /// BPF opcode: `jsle dst, src, +off` /// `PC += off if (dst as i32) <= (src as i32) (signed)`.
  396. pub const JSLE_REG32: u8 = BPF_JMP32 | BPF_X | BPF_JSLE;
  397. /// BPF opcode: `call imm` /// helper function call to helper with key `imm`.
  398. pub const CALL: u8 = BPF_JMP | BPF_CALL;
  399. /// BPF opcode: tail call.
  400. pub const TAIL_CALL: u8 = BPF_JMP | BPF_X | BPF_CALL;
  401. /// BPF opcode: `exit` /// `return r0`.
  402. pub const EXIT: u8 = BPF_JMP | BPF_EXIT;
  403. // Used in JIT
  404. /// Mask to extract the operation class from an operation code.
  405. pub const BPF_CLS_MASK: u8 = 0x07;
  406. /// Mask to extract the arithmetic operation code from an instruction operation code.
  407. pub const BPF_ALU_OP_MASK: u8 = 0xf0;
  408. /// Prototype of an eBPF helper function.
  409. pub type Helper = fn(u64, u64, u64, u64, u64) -> u64;
  410. /// An eBPF instruction.
  411. ///
  412. /// See <https://www.kernel.org/doc/Documentation/networking/filter.txt> for the Linux kernel
  413. /// documentation about eBPF, or <https://github.com/iovisor/bpf-docs/blob/master/eBPF.md> for a
  414. /// more concise version.
  415. #[derive(Debug, PartialEq, Eq, Clone)]
  416. pub struct Insn {
  417. /// Operation code.
  418. pub opc: u8,
  419. /// Destination register operand.
  420. pub dst: u8,
  421. /// Source register operand.
  422. pub src: u8,
  423. /// Offset operand.
  424. pub off: i16,
  425. /// Immediate value operand.
  426. pub imm: i32,
  427. }
  428. impl Insn {
  429. /// Turn an `Insn` back into an array of bytes.
  430. ///
  431. /// # Examples
  432. ///
  433. /// ```
  434. /// use rbpf::ebpf;
  435. ///
  436. /// let prog: &[u8] = &[
  437. /// 0xb7, 0x12, 0x56, 0x34, 0xde, 0xbc, 0x9a, 0x78,
  438. /// ];
  439. /// let insn = ebpf::Insn {
  440. /// opc: 0xb7,
  441. /// dst: 2,
  442. /// src: 1,
  443. /// off: 0x3456,
  444. /// imm: 0x789abcde
  445. /// };
  446. /// assert_eq!(insn.to_array(), prog);
  447. /// ```
  448. pub fn to_array(&self) -> [u8; INSN_SIZE] {
  449. [
  450. self.opc,
  451. self.src.wrapping_shl(4) | self.dst,
  452. (self.off & 0xff) as u8,
  453. self.off.wrapping_shr(8) as u8,
  454. (self.imm & 0xff) as u8,
  455. (self.imm & 0xff_00).wrapping_shr(8) as u8,
  456. (self.imm as u32 & 0xff_00_00).wrapping_shr(16) as u8,
  457. (self.imm as u32 & 0xff_00_00_00).wrapping_shr(24) as u8,
  458. ]
  459. }
  460. /// Turn an `Insn` into an vector of bytes.
  461. ///
  462. /// # Examples
  463. ///
  464. /// ```
  465. /// use rbpf::ebpf;
  466. ///
  467. /// let prog: Vec<u8> = vec![
  468. /// 0xb7, 0x12, 0x56, 0x34, 0xde, 0xbc, 0x9a, 0x78,
  469. /// ];
  470. /// let insn = ebpf::Insn {
  471. /// opc: 0xb7,
  472. /// dst: 2,
  473. /// src: 1,
  474. /// off: 0x3456,
  475. /// imm: 0x789abcde
  476. /// };
  477. /// assert_eq!(insn.to_vec(), prog);
  478. /// ```
  479. pub fn to_vec(&self) -> Vec<u8> {
  480. vec![
  481. self.opc,
  482. self.src.wrapping_shl(4) | self.dst,
  483. (self.off & 0xff) as u8,
  484. self.off.wrapping_shr(8) as u8,
  485. (self.imm & 0xff) as u8,
  486. (self.imm & 0xff_00).wrapping_shr(8) as u8,
  487. (self.imm as u32 & 0xff_00_00).wrapping_shr(16) as u8,
  488. (self.imm as u32 & 0xff_00_00_00).wrapping_shr(24) as u8,
  489. ]
  490. }
  491. }
  492. /// Get the instruction at `idx` of an eBPF program. `idx` is the index (number) of the
  493. /// instruction (not a byte offset). The first instruction has index 0.
  494. ///
  495. /// # Panics
  496. ///
  497. /// Panics if it is not possible to get the instruction (if idx is too high, or last instruction is
  498. /// incomplete).
  499. ///
  500. /// # Examples
  501. ///
  502. /// ```
  503. /// use rbpf::ebpf;
  504. ///
  505. /// let prog = &[
  506. /// 0xb7, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  507. /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  508. /// ];
  509. /// let insn = ebpf::get_insn(prog, 1);
  510. /// assert_eq!(insn.opc, 0x95);
  511. /// ```
  512. ///
  513. /// The example below will panic, since the last instruction is not complete and cannot be loaded.
  514. ///
  515. /// ```rust,should_panic
  516. /// use rbpf::ebpf;
  517. ///
  518. /// let prog = &[
  519. /// 0xb7, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  520. /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00 // two bytes missing
  521. /// ];
  522. /// let insn = ebpf::get_insn(prog, 1);
  523. /// ```
  524. pub fn get_insn(prog: &[u8], idx: usize) -> Insn {
  525. // This guard should not be needed in most cases, since the verifier already checks the program
  526. // size, and indexes should be fine in the interpreter/JIT. But this function is publicly
  527. // available and user can call it with any `idx`, so we have to check anyway.
  528. if (idx + 1) * INSN_SIZE > prog.len() {
  529. panic!(
  530. "Error: cannot reach instruction at index {:?} in program containing {:?} bytes",
  531. idx,
  532. prog.len()
  533. );
  534. }
  535. Insn {
  536. opc: prog[INSN_SIZE * idx],
  537. dst: prog[INSN_SIZE * idx + 1] & 0x0f,
  538. src: (prog[INSN_SIZE * idx + 1] & 0xf0) >> 4,
  539. off: LittleEndian::read_i16(&prog[(INSN_SIZE * idx + 2)..]),
  540. imm: LittleEndian::read_i32(&prog[(INSN_SIZE * idx + 4)..]),
  541. }
  542. }
  543. /// Return a vector of `struct Insn` built from a program.
  544. ///
  545. /// This is provided as a convenience for users wishing to manipulate a vector of instructions, for
  546. /// example for dumping the program instruction after instruction with a custom format.
  547. ///
  548. /// Note that the two parts of `LD_DW_IMM` instructions (spanning on 64 bits) are considered as two
  549. /// distinct instructions.
  550. ///
  551. /// # Examples
  552. ///
  553. /// ```
  554. /// use rbpf::ebpf;
  555. ///
  556. /// let prog = &[
  557. /// 0x18, 0x00, 0x00, 0x00, 0x88, 0x77, 0x66, 0x55,
  558. /// 0x00, 0x00, 0x00, 0x00, 0x44, 0x33, 0x22, 0x11,
  559. /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  560. /// ];
  561. ///
  562. /// let v = ebpf::to_insn_vec(prog);
  563. /// assert_eq!(v, vec![
  564. /// ebpf::Insn {
  565. /// opc: 0x18,
  566. /// dst: 0,
  567. /// src: 0,
  568. /// off: 0,
  569. /// imm: 0x55667788
  570. /// },
  571. /// ebpf::Insn {
  572. /// opc: 0,
  573. /// dst: 0,
  574. /// src: 0,
  575. /// off: 0,
  576. /// imm: 0x11223344
  577. /// },
  578. /// ebpf::Insn {
  579. /// opc: 0x95,
  580. /// dst: 0,
  581. /// src: 0,
  582. /// off: 0,
  583. /// imm: 0
  584. /// },
  585. /// ]);
  586. /// ```
  587. pub fn to_insn_vec(prog: &[u8]) -> Vec<Insn> {
  588. if prog.len() % INSN_SIZE != 0 {
  589. panic!(
  590. "Error: eBPF program length must be a multiple of {:?} octets",
  591. INSN_SIZE
  592. );
  593. }
  594. let mut res = vec![];
  595. let mut insn_ptr: usize = 0;
  596. while insn_ptr * INSN_SIZE < prog.len() {
  597. let insn = get_insn(prog, insn_ptr);
  598. res.push(insn);
  599. insn_ptr += 1;
  600. }
  601. res
  602. }