filter.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Linux Socket Filter Data Structures
  4. */
  5. #ifndef __LINUX_FILTER_H__
  6. #define __LINUX_FILTER_H__
  7. #include <stdarg.h>
  8. #include <linux/atomic.h>
  9. #include <linux/refcount.h>
  10. #include <linux/compat.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/linkage.h>
  13. #include <linux/printk.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/sched.h>
  16. #include <linux/capability.h>
  17. #include <linux/cryptohash.h>
  18. #include <linux/set_memory.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/if_vlan.h>
  21. #include <net/sch_generic.h>
  22. #include <uapi/linux/filter.h>
  23. #include <uapi/linux/bpf.h>
  24. struct sk_buff;
  25. struct sock;
  26. struct seccomp_data;
  27. struct bpf_prog_aux;
  28. struct xdp_rxq_info;
  29. struct xdp_buff;
  30. struct sock_reuseport;
  31. /* ArgX, context and stack frame pointer register positions. Note,
  32. * Arg1, Arg2, Arg3, etc are used as argument mappings of function
  33. * calls in BPF_CALL instruction.
  34. */
  35. #define BPF_REG_ARG1 BPF_REG_1
  36. #define BPF_REG_ARG2 BPF_REG_2
  37. #define BPF_REG_ARG3 BPF_REG_3
  38. #define BPF_REG_ARG4 BPF_REG_4
  39. #define BPF_REG_ARG5 BPF_REG_5
  40. #define BPF_REG_CTX BPF_REG_6
  41. #define BPF_REG_FP BPF_REG_10
  42. /* Additional register mappings for converted user programs. */
  43. #define BPF_REG_A BPF_REG_0
  44. #define BPF_REG_X BPF_REG_7
  45. #define BPF_REG_TMP BPF_REG_2 /* scratch reg */
  46. #define BPF_REG_D BPF_REG_8 /* data, callee-saved */
  47. #define BPF_REG_H BPF_REG_9 /* hlen, callee-saved */
  48. /* Kernel hidden auxiliary/helper register for hardening step.
  49. * Only used by eBPF JITs. It's nothing more than a temporary
  50. * register that JITs use internally, only that here it's part
  51. * of eBPF instructions that have been rewritten for blinding
  52. * constants. See JIT pre-step in bpf_jit_blind_constants().
  53. */
  54. #define BPF_REG_AX MAX_BPF_REG
  55. #define MAX_BPF_JIT_REG (MAX_BPF_REG + 1)
  56. /* unused opcode to mark special call to bpf_tail_call() helper */
  57. #define BPF_TAIL_CALL 0xf0
  58. /* unused opcode to mark call to interpreter with arguments */
  59. #define BPF_CALL_ARGS 0xe0
  60. /* As per nm, we expose JITed images as text (code) section for
  61. * kallsyms. That way, tools like perf can find it to match
  62. * addresses.
  63. */
  64. #define BPF_SYM_ELF_TYPE 't'
  65. /* BPF program can access up to 512 bytes of stack space. */
  66. #define MAX_BPF_STACK 512
  67. /* Helper macros for filter block array initializers. */
  68. /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
  69. #define BPF_ALU64_REG(OP, DST, SRC) \
  70. ((struct bpf_insn) { \
  71. .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
  72. .dst_reg = DST, \
  73. .src_reg = SRC, \
  74. .off = 0, \
  75. .imm = 0 })
  76. #define BPF_ALU32_REG(OP, DST, SRC) \
  77. ((struct bpf_insn) { \
  78. .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
  79. .dst_reg = DST, \
  80. .src_reg = SRC, \
  81. .off = 0, \
  82. .imm = 0 })
  83. /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
  84. #define BPF_ALU64_IMM(OP, DST, IMM) \
  85. ((struct bpf_insn) { \
  86. .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
  87. .dst_reg = DST, \
  88. .src_reg = 0, \
  89. .off = 0, \
  90. .imm = IMM })
  91. #define BPF_ALU32_IMM(OP, DST, IMM) \
  92. ((struct bpf_insn) { \
  93. .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
  94. .dst_reg = DST, \
  95. .src_reg = 0, \
  96. .off = 0, \
  97. .imm = IMM })
  98. /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
  99. #define BPF_ENDIAN(TYPE, DST, LEN) \
  100. ((struct bpf_insn) { \
  101. .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
  102. .dst_reg = DST, \
  103. .src_reg = 0, \
  104. .off = 0, \
  105. .imm = LEN })
  106. /* Short form of mov, dst_reg = src_reg */
  107. #define BPF_MOV64_REG(DST, SRC) \
  108. ((struct bpf_insn) { \
  109. .code = BPF_ALU64 | BPF_MOV | BPF_X, \
  110. .dst_reg = DST, \
  111. .src_reg = SRC, \
  112. .off = 0, \
  113. .imm = 0 })
  114. #define BPF_MOV32_REG(DST, SRC) \
  115. ((struct bpf_insn) { \
  116. .code = BPF_ALU | BPF_MOV | BPF_X, \
  117. .dst_reg = DST, \
  118. .src_reg = SRC, \
  119. .off = 0, \
  120. .imm = 0 })
  121. /* Short form of mov, dst_reg = imm32 */
  122. #define BPF_MOV64_IMM(DST, IMM) \
  123. ((struct bpf_insn) { \
  124. .code = BPF_ALU64 | BPF_MOV | BPF_K, \
  125. .dst_reg = DST, \
  126. .src_reg = 0, \
  127. .off = 0, \
  128. .imm = IMM })
  129. #define BPF_MOV32_IMM(DST, IMM) \
  130. ((struct bpf_insn) { \
  131. .code = BPF_ALU | BPF_MOV | BPF_K, \
  132. .dst_reg = DST, \
  133. .src_reg = 0, \
  134. .off = 0, \
  135. .imm = IMM })
  136. /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
  137. #define BPF_LD_IMM64(DST, IMM) \
  138. BPF_LD_IMM64_RAW(DST, 0, IMM)
  139. #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
  140. ((struct bpf_insn) { \
  141. .code = BPF_LD | BPF_DW | BPF_IMM, \
  142. .dst_reg = DST, \
  143. .src_reg = SRC, \
  144. .off = 0, \
  145. .imm = (__u32) (IMM) }), \
  146. ((struct bpf_insn) { \
  147. .code = 0, /* zero is reserved opcode */ \
  148. .dst_reg = 0, \
  149. .src_reg = 0, \
  150. .off = 0, \
  151. .imm = ((__u64) (IMM)) >> 32 })
  152. /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
  153. #define BPF_LD_MAP_FD(DST, MAP_FD) \
  154. BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
  155. /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
  156. #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
  157. ((struct bpf_insn) { \
  158. .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
  159. .dst_reg = DST, \
  160. .src_reg = SRC, \
  161. .off = 0, \
  162. .imm = IMM })
  163. #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
  164. ((struct bpf_insn) { \
  165. .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
  166. .dst_reg = DST, \
  167. .src_reg = SRC, \
  168. .off = 0, \
  169. .imm = IMM })
  170. /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
  171. #define BPF_LD_ABS(SIZE, IMM) \
  172. ((struct bpf_insn) { \
  173. .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
  174. .dst_reg = 0, \
  175. .src_reg = 0, \
  176. .off = 0, \
  177. .imm = IMM })
  178. /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
  179. #define BPF_LD_IND(SIZE, SRC, IMM) \
  180. ((struct bpf_insn) { \
  181. .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
  182. .dst_reg = 0, \
  183. .src_reg = SRC, \
  184. .off = 0, \
  185. .imm = IMM })
  186. /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
  187. #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
  188. ((struct bpf_insn) { \
  189. .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
  190. .dst_reg = DST, \
  191. .src_reg = SRC, \
  192. .off = OFF, \
  193. .imm = 0 })
  194. /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
  195. #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
  196. ((struct bpf_insn) { \
  197. .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
  198. .dst_reg = DST, \
  199. .src_reg = SRC, \
  200. .off = OFF, \
  201. .imm = 0 })
  202. /* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
  203. #define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
  204. ((struct bpf_insn) { \
  205. .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
  206. .dst_reg = DST, \
  207. .src_reg = SRC, \
  208. .off = OFF, \
  209. .imm = 0 })
  210. /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
  211. #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
  212. ((struct bpf_insn) { \
  213. .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
  214. .dst_reg = DST, \
  215. .src_reg = 0, \
  216. .off = OFF, \
  217. .imm = IMM })
  218. /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
  219. #define BPF_JMP_REG(OP, DST, SRC, OFF) \
  220. ((struct bpf_insn) { \
  221. .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
  222. .dst_reg = DST, \
  223. .src_reg = SRC, \
  224. .off = OFF, \
  225. .imm = 0 })
  226. /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
  227. #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
  228. ((struct bpf_insn) { \
  229. .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
  230. .dst_reg = DST, \
  231. .src_reg = 0, \
  232. .off = OFF, \
  233. .imm = IMM })
  234. /* Unconditional jumps, goto pc + off16 */
  235. #define BPF_JMP_A(OFF) \
  236. ((struct bpf_insn) { \
  237. .code = BPF_JMP | BPF_JA, \
  238. .dst_reg = 0, \
  239. .src_reg = 0, \
  240. .off = OFF, \
  241. .imm = 0 })
  242. /* Relative call */
  243. #define BPF_CALL_REL(TGT) \
  244. ((struct bpf_insn) { \
  245. .code = BPF_JMP | BPF_CALL, \
  246. .dst_reg = 0, \
  247. .src_reg = BPF_PSEUDO_CALL, \
  248. .off = 0, \
  249. .imm = TGT })
  250. /* Function call */
  251. #define BPF_CAST_CALL(x) \
  252. ((u64 (*)(u64, u64, u64, u64, u64))(x))
  253. #define BPF_EMIT_CALL(FUNC) \
  254. ((struct bpf_insn) { \
  255. .code = BPF_JMP | BPF_CALL, \
  256. .dst_reg = 0, \
  257. .src_reg = 0, \
  258. .off = 0, \
  259. .imm = ((FUNC) - __bpf_call_base) })
  260. /* Raw code statement block */
  261. #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
  262. ((struct bpf_insn) { \
  263. .code = CODE, \
  264. .dst_reg = DST, \
  265. .src_reg = SRC, \
  266. .off = OFF, \
  267. .imm = IMM })
  268. /* Program exit */
  269. #define BPF_EXIT_INSN() \
  270. ((struct bpf_insn) { \
  271. .code = BPF_JMP | BPF_EXIT, \
  272. .dst_reg = 0, \
  273. .src_reg = 0, \
  274. .off = 0, \
  275. .imm = 0 })
  276. /* Internal classic blocks for direct assignment */
  277. #define __BPF_STMT(CODE, K) \
  278. ((struct sock_filter) BPF_STMT(CODE, K))
  279. #define __BPF_JUMP(CODE, K, JT, JF) \
  280. ((struct sock_filter) BPF_JUMP(CODE, K, JT, JF))
  281. #define bytes_to_bpf_size(bytes) \
  282. ({ \
  283. int bpf_size = -EINVAL; \
  284. \
  285. if (bytes == sizeof(u8)) \
  286. bpf_size = BPF_B; \
  287. else if (bytes == sizeof(u16)) \
  288. bpf_size = BPF_H; \
  289. else if (bytes == sizeof(u32)) \
  290. bpf_size = BPF_W; \
  291. else if (bytes == sizeof(u64)) \
  292. bpf_size = BPF_DW; \
  293. \
  294. bpf_size; \
  295. })
  296. #define bpf_size_to_bytes(bpf_size) \
  297. ({ \
  298. int bytes = -EINVAL; \
  299. \
  300. if (bpf_size == BPF_B) \
  301. bytes = sizeof(u8); \
  302. else if (bpf_size == BPF_H) \
  303. bytes = sizeof(u16); \
  304. else if (bpf_size == BPF_W) \
  305. bytes = sizeof(u32); \
  306. else if (bpf_size == BPF_DW) \
  307. bytes = sizeof(u64); \
  308. \
  309. bytes; \
  310. })
  311. #define BPF_SIZEOF(type) \
  312. ({ \
  313. const int __size = bytes_to_bpf_size(sizeof(type)); \
  314. BUILD_BUG_ON(__size < 0); \
  315. __size; \
  316. })
  317. #define BPF_FIELD_SIZEOF(type, field) \
  318. ({ \
  319. const int __size = bytes_to_bpf_size(FIELD_SIZEOF(type, field)); \
  320. BUILD_BUG_ON(__size < 0); \
  321. __size; \
  322. })
  323. #define BPF_LDST_BYTES(insn) \
  324. ({ \
  325. const int __size = bpf_size_to_bytes(BPF_SIZE((insn)->code)); \
  326. WARN_ON(__size < 0); \
  327. __size; \
  328. })
  329. #define __BPF_MAP_0(m, v, ...) v
  330. #define __BPF_MAP_1(m, v, t, a, ...) m(t, a)
  331. #define __BPF_MAP_2(m, v, t, a, ...) m(t, a), __BPF_MAP_1(m, v, __VA_ARGS__)
  332. #define __BPF_MAP_3(m, v, t, a, ...) m(t, a), __BPF_MAP_2(m, v, __VA_ARGS__)
  333. #define __BPF_MAP_4(m, v, t, a, ...) m(t, a), __BPF_MAP_3(m, v, __VA_ARGS__)
  334. #define __BPF_MAP_5(m, v, t, a, ...) m(t, a), __BPF_MAP_4(m, v, __VA_ARGS__)
  335. #define __BPF_REG_0(...) __BPF_PAD(5)
  336. #define __BPF_REG_1(...) __BPF_MAP(1, __VA_ARGS__), __BPF_PAD(4)
  337. #define __BPF_REG_2(...) __BPF_MAP(2, __VA_ARGS__), __BPF_PAD(3)
  338. #define __BPF_REG_3(...) __BPF_MAP(3, __VA_ARGS__), __BPF_PAD(2)
  339. #define __BPF_REG_4(...) __BPF_MAP(4, __VA_ARGS__), __BPF_PAD(1)
  340. #define __BPF_REG_5(...) __BPF_MAP(5, __VA_ARGS__)
  341. #define __BPF_MAP(n, ...) __BPF_MAP_##n(__VA_ARGS__)
  342. #define __BPF_REG(n, ...) __BPF_REG_##n(__VA_ARGS__)
  343. #define __BPF_CAST(t, a) \
  344. (__force t) \
  345. (__force \
  346. typeof(__builtin_choose_expr(sizeof(t) == sizeof(unsigned long), \
  347. (unsigned long)0, (t)0))) a
  348. #define __BPF_V void
  349. #define __BPF_N
  350. #define __BPF_DECL_ARGS(t, a) t a
  351. #define __BPF_DECL_REGS(t, a) u64 a
  352. #define __BPF_PAD(n) \
  353. __BPF_MAP(n, __BPF_DECL_ARGS, __BPF_N, u64, __ur_1, u64, __ur_2, \
  354. u64, __ur_3, u64, __ur_4, u64, __ur_5)
  355. #define BPF_CALL_x(x, name, ...) \
  356. static __always_inline \
  357. u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \
  358. u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)); \
  359. u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)) \
  360. { \
  361. return ____##name(__BPF_MAP(x,__BPF_CAST,__BPF_N,__VA_ARGS__));\
  362. } \
  363. static __always_inline \
  364. u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__))
  365. #define BPF_CALL_0(name, ...) BPF_CALL_x(0, name, __VA_ARGS__)
  366. #define BPF_CALL_1(name, ...) BPF_CALL_x(1, name, __VA_ARGS__)
  367. #define BPF_CALL_2(name, ...) BPF_CALL_x(2, name, __VA_ARGS__)
  368. #define BPF_CALL_3(name, ...) BPF_CALL_x(3, name, __VA_ARGS__)
  369. #define BPF_CALL_4(name, ...) BPF_CALL_x(4, name, __VA_ARGS__)
  370. #define BPF_CALL_5(name, ...) BPF_CALL_x(5, name, __VA_ARGS__)
  371. #define bpf_ctx_range(TYPE, MEMBER) \
  372. offsetof(TYPE, MEMBER) ... offsetofend(TYPE, MEMBER) - 1
  373. #define bpf_ctx_range_till(TYPE, MEMBER1, MEMBER2) \
  374. offsetof(TYPE, MEMBER1) ... offsetofend(TYPE, MEMBER2) - 1
  375. #define bpf_target_off(TYPE, MEMBER, SIZE, PTR_SIZE) \
  376. ({ \
  377. BUILD_BUG_ON(FIELD_SIZEOF(TYPE, MEMBER) != (SIZE)); \
  378. *(PTR_SIZE) = (SIZE); \
  379. offsetof(TYPE, MEMBER); \
  380. })
  381. #ifdef CONFIG_COMPAT
  382. /* A struct sock_filter is architecture independent. */
  383. struct compat_sock_fprog {
  384. u16 len;
  385. compat_uptr_t filter; /* struct sock_filter * */
  386. };
  387. #endif
  388. struct sock_fprog_kern {
  389. u16 len;
  390. struct sock_filter *filter;
  391. };
  392. struct bpf_binary_header {
  393. u32 pages;
  394. /* Some arches need word alignment for their instructions */
  395. u8 image[] __aligned(4);
  396. };
  397. struct bpf_prog {
  398. u16 pages; /* Number of allocated pages */
  399. u16 jited:1, /* Is our filter JIT'ed? */
  400. jit_requested:1,/* archs need to JIT the prog */
  401. undo_set_mem:1, /* Passed set_memory_ro() checkpoint */
  402. gpl_compatible:1, /* Is filter GPL compatible? */
  403. cb_access:1, /* Is control block accessed? */
  404. dst_needed:1, /* Do we need dst entry? */
  405. blinded:1, /* Was blinded */
  406. is_func:1, /* program is a bpf function */
  407. kprobe_override:1, /* Do we override a kprobe? */
  408. has_callchain_buf:1; /* callchain buffer allocated? */
  409. enum bpf_prog_type type; /* Type of BPF program */
  410. enum bpf_attach_type expected_attach_type; /* For some prog types */
  411. u32 len; /* Number of filter blocks */
  412. u32 jited_len; /* Size of jited insns in bytes */
  413. u8 tag[BPF_TAG_SIZE];
  414. struct bpf_prog_aux *aux; /* Auxiliary fields */
  415. struct sock_fprog_kern *orig_prog; /* Original BPF program */
  416. unsigned int (*bpf_func)(const void *ctx,
  417. const struct bpf_insn *insn);
  418. /* Instructions for interpreter */
  419. union {
  420. struct sock_filter insns[0];
  421. struct bpf_insn insnsi[0];
  422. };
  423. };
  424. struct sk_filter {
  425. refcount_t refcnt;
  426. struct rcu_head rcu;
  427. struct bpf_prog *prog;
  428. };
  429. #define BPF_PROG_RUN(filter, ctx) (*(filter)->bpf_func)(ctx, (filter)->insnsi)
  430. #define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN
  431. struct bpf_skb_data_end {
  432. struct qdisc_skb_cb qdisc_cb;
  433. void *data_meta;
  434. void *data_end;
  435. };
  436. struct bpf_redirect_info {
  437. u32 ifindex;
  438. u32 flags;
  439. struct bpf_map *map;
  440. struct bpf_map *map_to_flush;
  441. u32 kern_flags;
  442. };
  443. DECLARE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
  444. /* flags for bpf_redirect_info kern_flags */
  445. #define BPF_RI_F_RF_NO_DIRECT BIT(0) /* no napi_direct on return_frame */
  446. /* Compute the linear packet data range [data, data_end) which
  447. * will be accessed by various program types (cls_bpf, act_bpf,
  448. * lwt, ...). Subsystems allowing direct data access must (!)
  449. * ensure that cb[] area can be written to when BPF program is
  450. * invoked (otherwise cb[] save/restore is necessary).
  451. */
  452. static inline void bpf_compute_data_pointers(struct sk_buff *skb)
  453. {
  454. struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
  455. BUILD_BUG_ON(sizeof(*cb) > FIELD_SIZEOF(struct sk_buff, cb));
  456. cb->data_meta = skb->data - skb_metadata_len(skb);
  457. cb->data_end = skb->data + skb_headlen(skb);
  458. }
  459. /* Similar to bpf_compute_data_pointers(), except that save orginal
  460. * data in cb->data and cb->meta_data for restore.
  461. */
  462. static inline void bpf_compute_and_save_data_end(
  463. struct sk_buff *skb, void **saved_data_end)
  464. {
  465. struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
  466. *saved_data_end = cb->data_end;
  467. cb->data_end = skb->data + skb_headlen(skb);
  468. }
  469. /* Restore data saved by bpf_compute_data_pointers(). */
  470. static inline void bpf_restore_data_end(
  471. struct sk_buff *skb, void *saved_data_end)
  472. {
  473. struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
  474. cb->data_end = saved_data_end;
  475. }
  476. static inline u8 *bpf_skb_cb(struct sk_buff *skb)
  477. {
  478. /* eBPF programs may read/write skb->cb[] area to transfer meta
  479. * data between tail calls. Since this also needs to work with
  480. * tc, that scratch memory is mapped to qdisc_skb_cb's data area.
  481. *
  482. * In some socket filter cases, the cb unfortunately needs to be
  483. * saved/restored so that protocol specific skb->cb[] data won't
  484. * be lost. In any case, due to unpriviledged eBPF programs
  485. * attached to sockets, we need to clear the bpf_skb_cb() area
  486. * to not leak previous contents to user space.
  487. */
  488. BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) != BPF_SKB_CB_LEN);
  489. BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) !=
  490. FIELD_SIZEOF(struct qdisc_skb_cb, data));
  491. return qdisc_skb_cb(skb)->data;
  492. }
  493. static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
  494. struct sk_buff *skb)
  495. {
  496. u8 *cb_data = bpf_skb_cb(skb);
  497. u8 cb_saved[BPF_SKB_CB_LEN];
  498. u32 res;
  499. if (unlikely(prog->cb_access)) {
  500. memcpy(cb_saved, cb_data, sizeof(cb_saved));
  501. memset(cb_data, 0, sizeof(cb_saved));
  502. }
  503. res = BPF_PROG_RUN(prog, skb);
  504. if (unlikely(prog->cb_access))
  505. memcpy(cb_data, cb_saved, sizeof(cb_saved));
  506. return res;
  507. }
  508. static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog,
  509. struct sk_buff *skb)
  510. {
  511. u8 *cb_data = bpf_skb_cb(skb);
  512. if (unlikely(prog->cb_access))
  513. memset(cb_data, 0, BPF_SKB_CB_LEN);
  514. return BPF_PROG_RUN(prog, skb);
  515. }
  516. static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,
  517. struct xdp_buff *xdp)
  518. {
  519. /* Caller needs to hold rcu_read_lock() (!), otherwise program
  520. * can be released while still running, or map elements could be
  521. * freed early while still having concurrent users. XDP fastpath
  522. * already takes rcu_read_lock() when fetching the program, so
  523. * it's not necessary here anymore.
  524. */
  525. return BPF_PROG_RUN(prog, xdp);
  526. }
  527. static inline u32 bpf_prog_insn_size(const struct bpf_prog *prog)
  528. {
  529. return prog->len * sizeof(struct bpf_insn);
  530. }
  531. static inline u32 bpf_prog_tag_scratch_size(const struct bpf_prog *prog)
  532. {
  533. return round_up(bpf_prog_insn_size(prog) +
  534. sizeof(__be64) + 1, SHA_MESSAGE_BYTES);
  535. }
  536. static inline unsigned int bpf_prog_size(unsigned int proglen)
  537. {
  538. return max(sizeof(struct bpf_prog),
  539. offsetof(struct bpf_prog, insns[proglen]));
  540. }
  541. static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
  542. {
  543. /* When classic BPF programs have been loaded and the arch
  544. * does not have a classic BPF JIT (anymore), they have been
  545. * converted via bpf_migrate_filter() to eBPF and thus always
  546. * have an unspec program type.
  547. */
  548. return prog->type == BPF_PROG_TYPE_UNSPEC;
  549. }
  550. static inline u32 bpf_ctx_off_adjust_machine(u32 size)
  551. {
  552. const u32 size_machine = sizeof(unsigned long);
  553. if (size > size_machine && size % size_machine == 0)
  554. size = size_machine;
  555. return size;
  556. }
  557. static inline bool bpf_ctx_narrow_align_ok(u32 off, u32 size_access,
  558. u32 size_default)
  559. {
  560. size_default = bpf_ctx_off_adjust_machine(size_default);
  561. size_access = bpf_ctx_off_adjust_machine(size_access);
  562. #ifdef __LITTLE_ENDIAN
  563. return (off & (size_default - 1)) == 0;
  564. #else
  565. return (off & (size_default - 1)) + size_access == size_default;
  566. #endif
  567. }
  568. static inline bool
  569. bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
  570. {
  571. return bpf_ctx_narrow_align_ok(off, size, size_default) &&
  572. size <= size_default && (size & (size - 1)) == 0;
  573. }
  574. #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
  575. static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
  576. {
  577. fp->undo_set_mem = 1;
  578. set_memory_ro((unsigned long)fp, fp->pages);
  579. }
  580. static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
  581. {
  582. if (fp->undo_set_mem)
  583. set_memory_rw((unsigned long)fp, fp->pages);
  584. }
  585. static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
  586. {
  587. set_memory_ro((unsigned long)hdr, hdr->pages);
  588. }
  589. static inline void bpf_jit_binary_unlock_ro(struct bpf_binary_header *hdr)
  590. {
  591. set_memory_rw((unsigned long)hdr, hdr->pages);
  592. }
  593. static inline struct bpf_binary_header *
  594. bpf_jit_binary_hdr(const struct bpf_prog *fp)
  595. {
  596. unsigned long real_start = (unsigned long)fp->bpf_func;
  597. unsigned long addr = real_start & PAGE_MASK;
  598. return (void *)addr;
  599. }
  600. int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
  601. static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
  602. {
  603. return sk_filter_trim_cap(sk, skb, 1);
  604. }
  605. struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err);
  606. void bpf_prog_free(struct bpf_prog *fp);
  607. bool bpf_opcode_in_insntable(u8 code);
  608. struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
  609. struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
  610. gfp_t gfp_extra_flags);
  611. void __bpf_prog_free(struct bpf_prog *fp);
  612. static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
  613. {
  614. bpf_prog_unlock_ro(fp);
  615. __bpf_prog_free(fp);
  616. }
  617. typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
  618. unsigned int flen);
  619. int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
  620. int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
  621. bpf_aux_classic_check_t trans, bool save_orig);
  622. void bpf_prog_destroy(struct bpf_prog *fp);
  623. int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  624. int sk_attach_bpf(u32 ufd, struct sock *sk);
  625. int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  626. int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk);
  627. void sk_reuseport_prog_free(struct bpf_prog *prog);
  628. int sk_detach_filter(struct sock *sk);
  629. int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
  630. unsigned int len);
  631. bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
  632. void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
  633. u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  634. #define __bpf_call_base_args \
  635. ((u64 (*)(u64, u64, u64, u64, u64, const struct bpf_insn *)) \
  636. __bpf_call_base)
  637. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog);
  638. void bpf_jit_compile(struct bpf_prog *prog);
  639. bool bpf_helper_changes_pkt_data(void *func);
  640. static inline bool bpf_dump_raw_ok(void)
  641. {
  642. /* Reconstruction of call-sites is dependent on kallsyms,
  643. * thus make dump the same restriction.
  644. */
  645. return kallsyms_show_value() == 1;
  646. }
  647. struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
  648. const struct bpf_insn *patch, u32 len);
  649. void bpf_clear_redirect_map(struct bpf_map *map);
  650. static inline bool xdp_return_frame_no_direct(void)
  651. {
  652. struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
  653. return ri->kern_flags & BPF_RI_F_RF_NO_DIRECT;
  654. }
  655. static inline void xdp_set_return_frame_no_direct(void)
  656. {
  657. struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
  658. ri->kern_flags |= BPF_RI_F_RF_NO_DIRECT;
  659. }
  660. static inline void xdp_clear_return_frame_no_direct(void)
  661. {
  662. struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
  663. ri->kern_flags &= ~BPF_RI_F_RF_NO_DIRECT;
  664. }
  665. static inline int xdp_ok_fwd_dev(const struct net_device *fwd,
  666. unsigned int pktlen)
  667. {
  668. unsigned int len;
  669. if (unlikely(!(fwd->flags & IFF_UP)))
  670. return -ENETDOWN;
  671. len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
  672. if (pktlen > len)
  673. return -EMSGSIZE;
  674. return 0;
  675. }
  676. /* The pair of xdp_do_redirect and xdp_do_flush_map MUST be called in the
  677. * same cpu context. Further for best results no more than a single map
  678. * for the do_redirect/do_flush pair should be used. This limitation is
  679. * because we only track one map and force a flush when the map changes.
  680. * This does not appear to be a real limitation for existing software.
  681. */
  682. int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
  683. struct xdp_buff *xdp, struct bpf_prog *prog);
  684. int xdp_do_redirect(struct net_device *dev,
  685. struct xdp_buff *xdp,
  686. struct bpf_prog *prog);
  687. void xdp_do_flush_map(void);
  688. void bpf_warn_invalid_xdp_action(u32 act);
  689. #ifdef CONFIG_INET
  690. struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
  691. struct bpf_prog *prog, struct sk_buff *skb,
  692. u32 hash);
  693. #else
  694. static inline struct sock *
  695. bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
  696. struct bpf_prog *prog, struct sk_buff *skb,
  697. u32 hash)
  698. {
  699. return NULL;
  700. }
  701. #endif
  702. #ifdef CONFIG_BPF_JIT
  703. extern int bpf_jit_enable;
  704. extern int bpf_jit_harden;
  705. extern int bpf_jit_kallsyms;
  706. typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
  707. struct bpf_binary_header *
  708. bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
  709. unsigned int alignment,
  710. bpf_jit_fill_hole_t bpf_fill_ill_insns);
  711. void bpf_jit_binary_free(struct bpf_binary_header *hdr);
  712. void bpf_jit_free(struct bpf_prog *fp);
  713. struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *fp);
  714. void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);
  715. static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
  716. u32 pass, void *image)
  717. {
  718. pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen,
  719. proglen, pass, image, current->comm, task_pid_nr(current));
  720. if (image)
  721. print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
  722. 16, 1, image, proglen, false);
  723. }
  724. static inline bool bpf_jit_is_ebpf(void)
  725. {
  726. # ifdef CONFIG_HAVE_EBPF_JIT
  727. return true;
  728. # else
  729. return false;
  730. # endif
  731. }
  732. static inline bool ebpf_jit_enabled(void)
  733. {
  734. return bpf_jit_enable && bpf_jit_is_ebpf();
  735. }
  736. static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
  737. {
  738. return fp->jited && bpf_jit_is_ebpf();
  739. }
  740. static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
  741. {
  742. /* These are the prerequisites, should someone ever have the
  743. * idea to call blinding outside of them, we make sure to
  744. * bail out.
  745. */
  746. if (!bpf_jit_is_ebpf())
  747. return false;
  748. if (!prog->jit_requested)
  749. return false;
  750. if (!bpf_jit_harden)
  751. return false;
  752. if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
  753. return false;
  754. return true;
  755. }
  756. static inline bool bpf_jit_kallsyms_enabled(void)
  757. {
  758. /* There are a couple of corner cases where kallsyms should
  759. * not be enabled f.e. on hardening.
  760. */
  761. if (bpf_jit_harden)
  762. return false;
  763. if (!bpf_jit_kallsyms)
  764. return false;
  765. if (bpf_jit_kallsyms == 1)
  766. return true;
  767. return false;
  768. }
  769. const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
  770. unsigned long *off, char *sym);
  771. bool is_bpf_text_address(unsigned long addr);
  772. int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  773. char *sym);
  774. static inline const char *
  775. bpf_address_lookup(unsigned long addr, unsigned long *size,
  776. unsigned long *off, char **modname, char *sym)
  777. {
  778. const char *ret = __bpf_address_lookup(addr, size, off, sym);
  779. if (ret && modname)
  780. *modname = NULL;
  781. return ret;
  782. }
  783. void bpf_prog_kallsyms_add(struct bpf_prog *fp);
  784. void bpf_prog_kallsyms_del(struct bpf_prog *fp);
  785. #else /* CONFIG_BPF_JIT */
  786. static inline bool ebpf_jit_enabled(void)
  787. {
  788. return false;
  789. }
  790. static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
  791. {
  792. return false;
  793. }
  794. static inline void bpf_jit_free(struct bpf_prog *fp)
  795. {
  796. bpf_prog_unlock_free(fp);
  797. }
  798. static inline bool bpf_jit_kallsyms_enabled(void)
  799. {
  800. return false;
  801. }
  802. static inline const char *
  803. __bpf_address_lookup(unsigned long addr, unsigned long *size,
  804. unsigned long *off, char *sym)
  805. {
  806. return NULL;
  807. }
  808. static inline bool is_bpf_text_address(unsigned long addr)
  809. {
  810. return false;
  811. }
  812. static inline int bpf_get_kallsym(unsigned int symnum, unsigned long *value,
  813. char *type, char *sym)
  814. {
  815. return -ERANGE;
  816. }
  817. static inline const char *
  818. bpf_address_lookup(unsigned long addr, unsigned long *size,
  819. unsigned long *off, char **modname, char *sym)
  820. {
  821. return NULL;
  822. }
  823. static inline void bpf_prog_kallsyms_add(struct bpf_prog *fp)
  824. {
  825. }
  826. static inline void bpf_prog_kallsyms_del(struct bpf_prog *fp)
  827. {
  828. }
  829. #endif /* CONFIG_BPF_JIT */
  830. void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp);
  831. void bpf_prog_kallsyms_del_all(struct bpf_prog *fp);
  832. #define BPF_ANC BIT(15)
  833. static inline bool bpf_needs_clear_a(const struct sock_filter *first)
  834. {
  835. switch (first->code) {
  836. case BPF_RET | BPF_K:
  837. case BPF_LD | BPF_W | BPF_LEN:
  838. return false;
  839. case BPF_LD | BPF_W | BPF_ABS:
  840. case BPF_LD | BPF_H | BPF_ABS:
  841. case BPF_LD | BPF_B | BPF_ABS:
  842. if (first->k == SKF_AD_OFF + SKF_AD_ALU_XOR_X)
  843. return true;
  844. return false;
  845. default:
  846. return true;
  847. }
  848. }
  849. static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
  850. {
  851. BUG_ON(ftest->code & BPF_ANC);
  852. switch (ftest->code) {
  853. case BPF_LD | BPF_W | BPF_ABS:
  854. case BPF_LD | BPF_H | BPF_ABS:
  855. case BPF_LD | BPF_B | BPF_ABS:
  856. #define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
  857. return BPF_ANC | SKF_AD_##CODE
  858. switch (ftest->k) {
  859. BPF_ANCILLARY(PROTOCOL);
  860. BPF_ANCILLARY(PKTTYPE);
  861. BPF_ANCILLARY(IFINDEX);
  862. BPF_ANCILLARY(NLATTR);
  863. BPF_ANCILLARY(NLATTR_NEST);
  864. BPF_ANCILLARY(MARK);
  865. BPF_ANCILLARY(QUEUE);
  866. BPF_ANCILLARY(HATYPE);
  867. BPF_ANCILLARY(RXHASH);
  868. BPF_ANCILLARY(CPU);
  869. BPF_ANCILLARY(ALU_XOR_X);
  870. BPF_ANCILLARY(VLAN_TAG);
  871. BPF_ANCILLARY(VLAN_TAG_PRESENT);
  872. BPF_ANCILLARY(PAY_OFFSET);
  873. BPF_ANCILLARY(RANDOM);
  874. BPF_ANCILLARY(VLAN_TPID);
  875. }
  876. /* Fallthrough. */
  877. default:
  878. return ftest->code;
  879. }
  880. }
  881. void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
  882. int k, unsigned int size);
  883. static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
  884. unsigned int size, void *buffer)
  885. {
  886. if (k >= 0)
  887. return skb_header_pointer(skb, k, size, buffer);
  888. return bpf_internal_load_pointer_neg_helper(skb, k, size);
  889. }
  890. static inline int bpf_tell_extensions(void)
  891. {
  892. return SKF_AD_MAX;
  893. }
  894. struct bpf_sock_addr_kern {
  895. struct sock *sk;
  896. struct sockaddr *uaddr;
  897. /* Temporary "register" to make indirect stores to nested structures
  898. * defined above. We need three registers to make such a store, but
  899. * only two (src and dst) are available at convert_ctx_access time
  900. */
  901. u64 tmp_reg;
  902. void *t_ctx; /* Attach type specific context. */
  903. };
  904. struct bpf_sock_ops_kern {
  905. struct sock *sk;
  906. u32 op;
  907. union {
  908. u32 args[4];
  909. u32 reply;
  910. u32 replylong[4];
  911. };
  912. u32 is_fullsock;
  913. u64 temp; /* temp and everything after is not
  914. * initialized to 0 before calling
  915. * the BPF program. New fields that
  916. * should be initialized to 0 should
  917. * be inserted before temp.
  918. * temp is scratch storage used by
  919. * sock_ops_convert_ctx_access
  920. * as temporary storage of a register.
  921. */
  922. };
  923. #endif /* __LINUX_FILTER_H__ */