filter.h 28 KB

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