ebpf.rs 24 KB

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