filter.h 30 KB

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