filter.h 28 KB

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