filter.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Linux Socket Filter Data Structures
  3. */
  4. #ifndef __TOOLS_LINUX_FILTER_H
  5. #define __TOOLS_LINUX_FILTER_H
  6. #include <linux/bpf.h>
  7. /* ArgX, context and stack frame pointer register positions. Note,
  8. * Arg1, Arg2, Arg3, etc are used as argument mappings of function
  9. * calls in BPF_CALL instruction.
  10. */
  11. #define BPF_REG_ARG1 BPF_REG_1
  12. #define BPF_REG_ARG2 BPF_REG_2
  13. #define BPF_REG_ARG3 BPF_REG_3
  14. #define BPF_REG_ARG4 BPF_REG_4
  15. #define BPF_REG_ARG5 BPF_REG_5
  16. #define BPF_REG_CTX BPF_REG_6
  17. #define BPF_REG_FP BPF_REG_10
  18. /* Additional register mappings for converted user programs. */
  19. #define BPF_REG_A BPF_REG_0
  20. #define BPF_REG_X BPF_REG_7
  21. #define BPF_REG_TMP BPF_REG_8
  22. /* BPF program can access up to 512 bytes of stack space. */
  23. #define MAX_BPF_STACK 512
  24. /* Helper macros for filter block array initializers. */
  25. /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
  26. #define BPF_ALU64_REG(OP, DST, SRC) \
  27. ((struct bpf_insn) { \
  28. .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
  29. .dst_reg = DST, \
  30. .src_reg = SRC, \
  31. .off = 0, \
  32. .imm = 0 })
  33. #define BPF_ALU32_REG(OP, DST, SRC) \
  34. ((struct bpf_insn) { \
  35. .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
  36. .dst_reg = DST, \
  37. .src_reg = SRC, \
  38. .off = 0, \
  39. .imm = 0 })
  40. /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
  41. #define BPF_ALU64_IMM(OP, DST, IMM) \
  42. ((struct bpf_insn) { \
  43. .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
  44. .dst_reg = DST, \
  45. .src_reg = 0, \
  46. .off = 0, \
  47. .imm = IMM })
  48. #define BPF_ALU32_IMM(OP, DST, IMM) \
  49. ((struct bpf_insn) { \
  50. .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
  51. .dst_reg = DST, \
  52. .src_reg = 0, \
  53. .off = 0, \
  54. .imm = IMM })
  55. /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
  56. #define BPF_ENDIAN(TYPE, DST, LEN) \
  57. ((struct bpf_insn) { \
  58. .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
  59. .dst_reg = DST, \
  60. .src_reg = 0, \
  61. .off = 0, \
  62. .imm = LEN })
  63. /* Short form of mov, dst_reg = src_reg */
  64. #define BPF_MOV64_REG(DST, SRC) \
  65. ((struct bpf_insn) { \
  66. .code = BPF_ALU64 | BPF_MOV | BPF_X, \
  67. .dst_reg = DST, \
  68. .src_reg = SRC, \
  69. .off = 0, \
  70. .imm = 0 })
  71. #define BPF_MOV32_REG(DST, SRC) \
  72. ((struct bpf_insn) { \
  73. .code = BPF_ALU | BPF_MOV | BPF_X, \
  74. .dst_reg = DST, \
  75. .src_reg = SRC, \
  76. .off = 0, \
  77. .imm = 0 })
  78. /* Short form of mov, dst_reg = imm32 */
  79. #define BPF_MOV64_IMM(DST, IMM) \
  80. ((struct bpf_insn) { \
  81. .code = BPF_ALU64 | BPF_MOV | BPF_K, \
  82. .dst_reg = DST, \
  83. .src_reg = 0, \
  84. .off = 0, \
  85. .imm = IMM })
  86. #define BPF_MOV32_IMM(DST, IMM) \
  87. ((struct bpf_insn) { \
  88. .code = BPF_ALU | BPF_MOV | BPF_K, \
  89. .dst_reg = DST, \
  90. .src_reg = 0, \
  91. .off = 0, \
  92. .imm = IMM })
  93. /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
  94. #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
  95. ((struct bpf_insn) { \
  96. .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
  97. .dst_reg = DST, \
  98. .src_reg = SRC, \
  99. .off = 0, \
  100. .imm = IMM })
  101. #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
  102. ((struct bpf_insn) { \
  103. .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
  104. .dst_reg = DST, \
  105. .src_reg = SRC, \
  106. .off = 0, \
  107. .imm = IMM })
  108. /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
  109. #define BPF_LD_ABS(SIZE, IMM) \
  110. ((struct bpf_insn) { \
  111. .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
  112. .dst_reg = 0, \
  113. .src_reg = 0, \
  114. .off = 0, \
  115. .imm = IMM })
  116. /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
  117. #define BPF_LD_IND(SIZE, SRC, IMM) \
  118. ((struct bpf_insn) { \
  119. .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
  120. .dst_reg = 0, \
  121. .src_reg = SRC, \
  122. .off = 0, \
  123. .imm = IMM })
  124. /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
  125. #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
  126. ((struct bpf_insn) { \
  127. .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
  128. .dst_reg = DST, \
  129. .src_reg = SRC, \
  130. .off = OFF, \
  131. .imm = 0 })
  132. /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
  133. #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
  134. ((struct bpf_insn) { \
  135. .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
  136. .dst_reg = DST, \
  137. .src_reg = SRC, \
  138. .off = OFF, \
  139. .imm = 0 })
  140. /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
  141. #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
  142. ((struct bpf_insn) { \
  143. .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
  144. .dst_reg = DST, \
  145. .src_reg = 0, \
  146. .off = OFF, \
  147. .imm = IMM })
  148. /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
  149. #define BPF_JMP_REG(OP, DST, SRC, OFF) \
  150. ((struct bpf_insn) { \
  151. .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
  152. .dst_reg = DST, \
  153. .src_reg = SRC, \
  154. .off = OFF, \
  155. .imm = 0 })
  156. /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
  157. #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
  158. ((struct bpf_insn) { \
  159. .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
  160. .dst_reg = DST, \
  161. .src_reg = 0, \
  162. .off = OFF, \
  163. .imm = IMM })
  164. /* Function call */
  165. #define BPF_EMIT_CALL(FUNC) \
  166. ((struct bpf_insn) { \
  167. .code = BPF_JMP | BPF_CALL, \
  168. .dst_reg = 0, \
  169. .src_reg = 0, \
  170. .off = 0, \
  171. .imm = ((FUNC) - BPF_FUNC_unspec) })
  172. /* Raw code statement block */
  173. #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
  174. ((struct bpf_insn) { \
  175. .code = CODE, \
  176. .dst_reg = DST, \
  177. .src_reg = SRC, \
  178. .off = OFF, \
  179. .imm = IMM })
  180. /* Program exit */
  181. #define BPF_EXIT_INSN() \
  182. ((struct bpf_insn) { \
  183. .code = BPF_JMP | BPF_EXIT, \
  184. .dst_reg = 0, \
  185. .src_reg = 0, \
  186. .off = 0, \
  187. .imm = 0 })
  188. #endif /* __TOOLS_LINUX_FILTER_H */