filter.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. #if BITS_PER_LONG == 64
  376. # define bpf_ctx_range_ptr(TYPE, MEMBER) \
  377. offsetof(TYPE, MEMBER) ... offsetofend(TYPE, MEMBER) - 1
  378. #else
  379. # define bpf_ctx_range_ptr(TYPE, MEMBER) \
  380. offsetof(TYPE, MEMBER) ... offsetof(TYPE, MEMBER) + 8 - 1
  381. #endif /* BITS_PER_LONG == 64 */
  382. #define bpf_target_off(TYPE, MEMBER, SIZE, PTR_SIZE) \
  383. ({ \
  384. BUILD_BUG_ON(FIELD_SIZEOF(TYPE, MEMBER) != (SIZE)); \
  385. *(PTR_SIZE) = (SIZE); \
  386. offsetof(TYPE, MEMBER); \
  387. })
  388. #ifdef CONFIG_COMPAT
  389. /* A struct sock_filter is architecture independent. */
  390. struct compat_sock_fprog {
  391. u16 len;
  392. compat_uptr_t filter; /* struct sock_filter * */
  393. };
  394. #endif
  395. struct sock_fprog_kern {
  396. u16 len;
  397. struct sock_filter *filter;
  398. };
  399. struct bpf_binary_header {
  400. u32 pages;
  401. /* Some arches need word alignment for their instructions */
  402. u8 image[] __aligned(4);
  403. };
  404. struct bpf_prog {
  405. u16 pages; /* Number of allocated pages */
  406. u16 jited:1, /* Is our filter JIT'ed? */
  407. jit_requested:1,/* archs need to JIT the prog */
  408. undo_set_mem:1, /* Passed set_memory_ro() checkpoint */
  409. gpl_compatible:1, /* Is filter GPL compatible? */
  410. cb_access:1, /* Is control block accessed? */
  411. dst_needed:1, /* Do we need dst entry? */
  412. blinded:1, /* Was blinded */
  413. is_func:1, /* program is a bpf function */
  414. kprobe_override:1, /* Do we override a kprobe? */
  415. has_callchain_buf:1; /* callchain buffer allocated? */
  416. enum bpf_prog_type type; /* Type of BPF program */
  417. enum bpf_attach_type expected_attach_type; /* For some prog types */
  418. u32 len; /* Number of filter blocks */
  419. u32 jited_len; /* Size of jited insns in bytes */
  420. u8 tag[BPF_TAG_SIZE];
  421. struct bpf_prog_aux *aux; /* Auxiliary fields */
  422. struct sock_fprog_kern *orig_prog; /* Original BPF program */
  423. unsigned int (*bpf_func)(const void *ctx,
  424. const struct bpf_insn *insn);
  425. /* Instructions for interpreter */
  426. union {
  427. struct sock_filter insns[0];
  428. struct bpf_insn insnsi[0];
  429. };
  430. };
  431. struct sk_filter {
  432. refcount_t refcnt;
  433. struct rcu_head rcu;
  434. struct bpf_prog *prog;
  435. };
  436. #define BPF_PROG_RUN(filter, ctx) (*(filter)->bpf_func)(ctx, (filter)->insnsi)
  437. #define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN
  438. struct bpf_skb_data_end {
  439. struct qdisc_skb_cb qdisc_cb;
  440. void *data_meta;
  441. void *data_end;
  442. };
  443. struct bpf_redirect_info {
  444. u32 ifindex;
  445. u32 flags;
  446. struct bpf_map *map;
  447. struct bpf_map *map_to_flush;
  448. u32 kern_flags;
  449. };
  450. DECLARE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
  451. /* flags for bpf_redirect_info kern_flags */
  452. #define BPF_RI_F_RF_NO_DIRECT BIT(0) /* no napi_direct on return_frame */
  453. /* Compute the linear packet data range [data, data_end) which
  454. * will be accessed by various program types (cls_bpf, act_bpf,
  455. * lwt, ...). Subsystems allowing direct data access must (!)
  456. * ensure that cb[] area can be written to when BPF program is
  457. * invoked (otherwise cb[] save/restore is necessary).
  458. */
  459. static inline void bpf_compute_data_pointers(struct sk_buff *skb)
  460. {
  461. struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
  462. BUILD_BUG_ON(sizeof(*cb) > FIELD_SIZEOF(struct sk_buff, cb));
  463. cb->data_meta = skb->data - skb_metadata_len(skb);
  464. cb->data_end = skb->data + skb_headlen(skb);
  465. }
  466. /* Similar to bpf_compute_data_pointers(), except that save orginal
  467. * data in cb->data and cb->meta_data for restore.
  468. */
  469. static inline void bpf_compute_and_save_data_end(
  470. struct sk_buff *skb, void **saved_data_end)
  471. {
  472. struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
  473. *saved_data_end = cb->data_end;
  474. cb->data_end = skb->data + skb_headlen(skb);
  475. }
  476. /* Restore data saved by bpf_compute_data_pointers(). */
  477. static inline void bpf_restore_data_end(
  478. struct sk_buff *skb, void *saved_data_end)
  479. {
  480. struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
  481. cb->data_end = saved_data_end;
  482. }
  483. static inline u8 *bpf_skb_cb(struct sk_buff *skb)
  484. {
  485. /* eBPF programs may read/write skb->cb[] area to transfer meta
  486. * data between tail calls. Since this also needs to work with
  487. * tc, that scratch memory is mapped to qdisc_skb_cb's data area.
  488. *
  489. * In some socket filter cases, the cb unfortunately needs to be
  490. * saved/restored so that protocol specific skb->cb[] data won't
  491. * be lost. In any case, due to unpriviledged eBPF programs
  492. * attached to sockets, we need to clear the bpf_skb_cb() area
  493. * to not leak previous contents to user space.
  494. */
  495. BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) != BPF_SKB_CB_LEN);
  496. BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) !=
  497. FIELD_SIZEOF(struct qdisc_skb_cb, data));
  498. return qdisc_skb_cb(skb)->data;
  499. }
  500. static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
  501. struct sk_buff *skb)
  502. {
  503. u8 *cb_data = bpf_skb_cb(skb);
  504. u8 cb_saved[BPF_SKB_CB_LEN];
  505. u32 res;
  506. if (unlikely(prog->cb_access)) {
  507. memcpy(cb_saved, cb_data, sizeof(cb_saved));
  508. memset(cb_data, 0, sizeof(cb_saved));
  509. }
  510. res = BPF_PROG_RUN(prog, skb);
  511. if (unlikely(prog->cb_access))
  512. memcpy(cb_data, cb_saved, sizeof(cb_saved));
  513. return res;
  514. }
  515. static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog,
  516. struct sk_buff *skb)
  517. {
  518. u8 *cb_data = bpf_skb_cb(skb);
  519. if (unlikely(prog->cb_access))
  520. memset(cb_data, 0, BPF_SKB_CB_LEN);
  521. return BPF_PROG_RUN(prog, skb);
  522. }
  523. static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,
  524. struct xdp_buff *xdp)
  525. {
  526. /* Caller needs to hold rcu_read_lock() (!), otherwise program
  527. * can be released while still running, or map elements could be
  528. * freed early while still having concurrent users. XDP fastpath
  529. * already takes rcu_read_lock() when fetching the program, so
  530. * it's not necessary here anymore.
  531. */
  532. return BPF_PROG_RUN(prog, xdp);
  533. }
  534. static inline u32 bpf_prog_insn_size(const struct bpf_prog *prog)
  535. {
  536. return prog->len * sizeof(struct bpf_insn);
  537. }
  538. static inline u32 bpf_prog_tag_scratch_size(const struct bpf_prog *prog)
  539. {
  540. return round_up(bpf_prog_insn_size(prog) +
  541. sizeof(__be64) + 1, SHA_MESSAGE_BYTES);
  542. }
  543. static inline unsigned int bpf_prog_size(unsigned int proglen)
  544. {
  545. return max(sizeof(struct bpf_prog),
  546. offsetof(struct bpf_prog, insns[proglen]));
  547. }
  548. static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
  549. {
  550. /* When classic BPF programs have been loaded and the arch
  551. * does not have a classic BPF JIT (anymore), they have been
  552. * converted via bpf_migrate_filter() to eBPF and thus always
  553. * have an unspec program type.
  554. */
  555. return prog->type == BPF_PROG_TYPE_UNSPEC;
  556. }
  557. static inline u32 bpf_ctx_off_adjust_machine(u32 size)
  558. {
  559. const u32 size_machine = sizeof(unsigned long);
  560. if (size > size_machine && size % size_machine == 0)
  561. size = size_machine;
  562. return size;
  563. }
  564. static inline bool bpf_ctx_narrow_align_ok(u32 off, u32 size_access,
  565. u32 size_default)
  566. {
  567. size_default = bpf_ctx_off_adjust_machine(size_default);
  568. size_access = bpf_ctx_off_adjust_machine(size_access);
  569. #ifdef __LITTLE_ENDIAN
  570. return (off & (size_default - 1)) == 0;
  571. #else
  572. return (off & (size_default - 1)) + size_access == size_default;
  573. #endif
  574. }
  575. static inline bool
  576. bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
  577. {
  578. return bpf_ctx_narrow_align_ok(off, size, size_default) &&
  579. size <= size_default && (size & (size - 1)) == 0;
  580. }
  581. #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
  582. static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
  583. {
  584. fp->undo_set_mem = 1;
  585. set_memory_ro((unsigned long)fp, fp->pages);
  586. }
  587. static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
  588. {
  589. if (fp->undo_set_mem)
  590. set_memory_rw((unsigned long)fp, fp->pages);
  591. }
  592. static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
  593. {
  594. set_memory_ro((unsigned long)hdr, hdr->pages);
  595. }
  596. static inline void bpf_jit_binary_unlock_ro(struct bpf_binary_header *hdr)
  597. {
  598. set_memory_rw((unsigned long)hdr, hdr->pages);
  599. }
  600. static inline struct bpf_binary_header *
  601. bpf_jit_binary_hdr(const struct bpf_prog *fp)
  602. {
  603. unsigned long real_start = (unsigned long)fp->bpf_func;
  604. unsigned long addr = real_start & PAGE_MASK;
  605. return (void *)addr;
  606. }
  607. int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
  608. static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
  609. {
  610. return sk_filter_trim_cap(sk, skb, 1);
  611. }
  612. struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err);
  613. void bpf_prog_free(struct bpf_prog *fp);
  614. bool bpf_opcode_in_insntable(u8 code);
  615. struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
  616. struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
  617. gfp_t gfp_extra_flags);
  618. void __bpf_prog_free(struct bpf_prog *fp);
  619. static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
  620. {
  621. bpf_prog_unlock_ro(fp);
  622. __bpf_prog_free(fp);
  623. }
  624. typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
  625. unsigned int flen);
  626. int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
  627. int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
  628. bpf_aux_classic_check_t trans, bool save_orig);
  629. void bpf_prog_destroy(struct bpf_prog *fp);
  630. int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  631. int sk_attach_bpf(u32 ufd, struct sock *sk);
  632. int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  633. int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk);
  634. void sk_reuseport_prog_free(struct bpf_prog *prog);
  635. int sk_detach_filter(struct sock *sk);
  636. int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
  637. unsigned int len);
  638. bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
  639. void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
  640. u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  641. #define __bpf_call_base_args \
  642. ((u64 (*)(u64, u64, u64, u64, u64, const struct bpf_insn *)) \
  643. __bpf_call_base)
  644. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog);
  645. void bpf_jit_compile(struct bpf_prog *prog);
  646. bool bpf_helper_changes_pkt_data(void *func);
  647. static inline bool bpf_dump_raw_ok(void)
  648. {
  649. /* Reconstruction of call-sites is dependent on kallsyms,
  650. * thus make dump the same restriction.
  651. */
  652. return kallsyms_show_value() == 1;
  653. }
  654. struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
  655. const struct bpf_insn *patch, u32 len);
  656. void bpf_clear_redirect_map(struct bpf_map *map);
  657. static inline bool xdp_return_frame_no_direct(void)
  658. {
  659. struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
  660. return ri->kern_flags & BPF_RI_F_RF_NO_DIRECT;
  661. }
  662. static inline void xdp_set_return_frame_no_direct(void)
  663. {
  664. struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
  665. ri->kern_flags |= BPF_RI_F_RF_NO_DIRECT;
  666. }
  667. static inline void xdp_clear_return_frame_no_direct(void)
  668. {
  669. struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
  670. ri->kern_flags &= ~BPF_RI_F_RF_NO_DIRECT;
  671. }
  672. static inline int xdp_ok_fwd_dev(const struct net_device *fwd,
  673. unsigned int pktlen)
  674. {
  675. unsigned int len;
  676. if (unlikely(!(fwd->flags & IFF_UP)))
  677. return -ENETDOWN;
  678. len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
  679. if (pktlen > len)
  680. return -EMSGSIZE;
  681. return 0;
  682. }
  683. /* The pair of xdp_do_redirect and xdp_do_flush_map MUST be called in the
  684. * same cpu context. Further for best results no more than a single map
  685. * for the do_redirect/do_flush pair should be used. This limitation is
  686. * because we only track one map and force a flush when the map changes.
  687. * This does not appear to be a real limitation for existing software.
  688. */
  689. int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
  690. struct xdp_buff *xdp, struct bpf_prog *prog);
  691. int xdp_do_redirect(struct net_device *dev,
  692. struct xdp_buff *xdp,
  693. struct bpf_prog *prog);
  694. void xdp_do_flush_map(void);
  695. void bpf_warn_invalid_xdp_action(u32 act);
  696. #ifdef CONFIG_INET
  697. struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
  698. struct bpf_prog *prog, struct sk_buff *skb,
  699. u32 hash);
  700. #else
  701. static inline struct sock *
  702. bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
  703. struct bpf_prog *prog, struct sk_buff *skb,
  704. u32 hash)
  705. {
  706. return NULL;
  707. }
  708. #endif
  709. #ifdef CONFIG_BPF_JIT
  710. extern int bpf_jit_enable;
  711. extern int bpf_jit_harden;
  712. extern int bpf_jit_kallsyms;
  713. extern int bpf_jit_limit;
  714. typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
  715. struct bpf_binary_header *
  716. bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
  717. unsigned int alignment,
  718. bpf_jit_fill_hole_t bpf_fill_ill_insns);
  719. void bpf_jit_binary_free(struct bpf_binary_header *hdr);
  720. void bpf_jit_free(struct bpf_prog *fp);
  721. int bpf_jit_get_func_addr(const struct bpf_prog *prog,
  722. const struct bpf_insn *insn, bool extra_pass,
  723. u64 *func_addr, bool *func_addr_fixed);
  724. struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *fp);
  725. void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);
  726. static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
  727. u32 pass, void *image)
  728. {
  729. pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen,
  730. proglen, pass, image, current->comm, task_pid_nr(current));
  731. if (image)
  732. print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
  733. 16, 1, image, proglen, false);
  734. }
  735. static inline bool bpf_jit_is_ebpf(void)
  736. {
  737. # ifdef CONFIG_HAVE_EBPF_JIT
  738. return true;
  739. # else
  740. return false;
  741. # endif
  742. }
  743. static inline bool ebpf_jit_enabled(void)
  744. {
  745. return bpf_jit_enable && bpf_jit_is_ebpf();
  746. }
  747. static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
  748. {
  749. return fp->jited && bpf_jit_is_ebpf();
  750. }
  751. static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
  752. {
  753. /* These are the prerequisites, should someone ever have the
  754. * idea to call blinding outside of them, we make sure to
  755. * bail out.
  756. */
  757. if (!bpf_jit_is_ebpf())
  758. return false;
  759. if (!prog->jit_requested)
  760. return false;
  761. if (!bpf_jit_harden)
  762. return false;
  763. if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
  764. return false;
  765. return true;
  766. }
  767. static inline bool bpf_jit_kallsyms_enabled(void)
  768. {
  769. /* There are a couple of corner cases where kallsyms should
  770. * not be enabled f.e. on hardening.
  771. */
  772. if (bpf_jit_harden)
  773. return false;
  774. if (!bpf_jit_kallsyms)
  775. return false;
  776. if (bpf_jit_kallsyms == 1)
  777. return true;
  778. return false;
  779. }
  780. const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
  781. unsigned long *off, char *sym);
  782. bool is_bpf_text_address(unsigned long addr);
  783. int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  784. char *sym);
  785. static inline const char *
  786. bpf_address_lookup(unsigned long addr, unsigned long *size,
  787. unsigned long *off, char **modname, char *sym)
  788. {
  789. const char *ret = __bpf_address_lookup(addr, size, off, sym);
  790. if (ret && modname)
  791. *modname = NULL;
  792. return ret;
  793. }
  794. void bpf_prog_kallsyms_add(struct bpf_prog *fp);
  795. void bpf_prog_kallsyms_del(struct bpf_prog *fp);
  796. #else /* CONFIG_BPF_JIT */
  797. static inline bool ebpf_jit_enabled(void)
  798. {
  799. return false;
  800. }
  801. static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
  802. {
  803. return false;
  804. }
  805. static inline void bpf_jit_free(struct bpf_prog *fp)
  806. {
  807. bpf_prog_unlock_free(fp);
  808. }
  809. static inline bool bpf_jit_kallsyms_enabled(void)
  810. {
  811. return false;
  812. }
  813. static inline const char *
  814. __bpf_address_lookup(unsigned long addr, unsigned long *size,
  815. unsigned long *off, char *sym)
  816. {
  817. return NULL;
  818. }
  819. static inline bool is_bpf_text_address(unsigned long addr)
  820. {
  821. return false;
  822. }
  823. static inline int bpf_get_kallsym(unsigned int symnum, unsigned long *value,
  824. char *type, char *sym)
  825. {
  826. return -ERANGE;
  827. }
  828. static inline const char *
  829. bpf_address_lookup(unsigned long addr, unsigned long *size,
  830. unsigned long *off, char **modname, char *sym)
  831. {
  832. return NULL;
  833. }
  834. static inline void bpf_prog_kallsyms_add(struct bpf_prog *fp)
  835. {
  836. }
  837. static inline void bpf_prog_kallsyms_del(struct bpf_prog *fp)
  838. {
  839. }
  840. #endif /* CONFIG_BPF_JIT */
  841. void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp);
  842. void bpf_prog_kallsyms_del_all(struct bpf_prog *fp);
  843. #define BPF_ANC BIT(15)
  844. static inline bool bpf_needs_clear_a(const struct sock_filter *first)
  845. {
  846. switch (first->code) {
  847. case BPF_RET | BPF_K:
  848. case BPF_LD | BPF_W | BPF_LEN:
  849. return false;
  850. case BPF_LD | BPF_W | BPF_ABS:
  851. case BPF_LD | BPF_H | BPF_ABS:
  852. case BPF_LD | BPF_B | BPF_ABS:
  853. if (first->k == SKF_AD_OFF + SKF_AD_ALU_XOR_X)
  854. return true;
  855. return false;
  856. default:
  857. return true;
  858. }
  859. }
  860. static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
  861. {
  862. BUG_ON(ftest->code & BPF_ANC);
  863. switch (ftest->code) {
  864. case BPF_LD | BPF_W | BPF_ABS:
  865. case BPF_LD | BPF_H | BPF_ABS:
  866. case BPF_LD | BPF_B | BPF_ABS:
  867. #define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
  868. return BPF_ANC | SKF_AD_##CODE
  869. switch (ftest->k) {
  870. BPF_ANCILLARY(PROTOCOL);
  871. BPF_ANCILLARY(PKTTYPE);
  872. BPF_ANCILLARY(IFINDEX);
  873. BPF_ANCILLARY(NLATTR);
  874. BPF_ANCILLARY(NLATTR_NEST);
  875. BPF_ANCILLARY(MARK);
  876. BPF_ANCILLARY(QUEUE);
  877. BPF_ANCILLARY(HATYPE);
  878. BPF_ANCILLARY(RXHASH);
  879. BPF_ANCILLARY(CPU);
  880. BPF_ANCILLARY(ALU_XOR_X);
  881. BPF_ANCILLARY(VLAN_TAG);
  882. BPF_ANCILLARY(VLAN_TAG_PRESENT);
  883. BPF_ANCILLARY(PAY_OFFSET);
  884. BPF_ANCILLARY(RANDOM);
  885. BPF_ANCILLARY(VLAN_TPID);
  886. }
  887. /* Fallthrough. */
  888. default:
  889. return ftest->code;
  890. }
  891. }
  892. void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
  893. int k, unsigned int size);
  894. static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
  895. unsigned int size, void *buffer)
  896. {
  897. if (k >= 0)
  898. return skb_header_pointer(skb, k, size, buffer);
  899. return bpf_internal_load_pointer_neg_helper(skb, k, size);
  900. }
  901. static inline int bpf_tell_extensions(void)
  902. {
  903. return SKF_AD_MAX;
  904. }
  905. struct bpf_sock_addr_kern {
  906. struct sock *sk;
  907. struct sockaddr *uaddr;
  908. /* Temporary "register" to make indirect stores to nested structures
  909. * defined above. We need three registers to make such a store, but
  910. * only two (src and dst) are available at convert_ctx_access time
  911. */
  912. u64 tmp_reg;
  913. void *t_ctx; /* Attach type specific context. */
  914. };
  915. struct bpf_sock_ops_kern {
  916. struct sock *sk;
  917. u32 op;
  918. union {
  919. u32 args[4];
  920. u32 reply;
  921. u32 replylong[4];
  922. };
  923. u32 is_fullsock;
  924. u64 temp; /* temp and everything after is not
  925. * initialized to 0 before calling
  926. * the BPF program. New fields that
  927. * should be initialized to 0 should
  928. * be inserted before temp.
  929. * temp is scratch storage used by
  930. * sock_ops_convert_ctx_access
  931. * as temporary storage of a register.
  932. */
  933. };
  934. #endif /* __LINUX_FILTER_H__ */