filter.h 29 KB

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