filter.h 28 KB

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