filter.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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. */
  49. #define BPF_REG_AX MAX_BPF_REG
  50. #define MAX_BPF_EXT_REG (MAX_BPF_REG + 1)
  51. #define MAX_BPF_JIT_REG MAX_BPF_EXT_REG
  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. /* Relative call */
  239. #define BPF_CALL_REL(TGT) \
  240. ((struct bpf_insn) { \
  241. .code = BPF_JMP | BPF_CALL, \
  242. .dst_reg = 0, \
  243. .src_reg = BPF_PSEUDO_CALL, \
  244. .off = 0, \
  245. .imm = TGT })
  246. /* Function call */
  247. #define BPF_CAST_CALL(x) \
  248. ((u64 (*)(u64, u64, u64, u64, u64))(x))
  249. #define BPF_EMIT_CALL(FUNC) \
  250. ((struct bpf_insn) { \
  251. .code = BPF_JMP | BPF_CALL, \
  252. .dst_reg = 0, \
  253. .src_reg = 0, \
  254. .off = 0, \
  255. .imm = ((FUNC) - __bpf_call_base) })
  256. /* Raw code statement block */
  257. #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
  258. ((struct bpf_insn) { \
  259. .code = CODE, \
  260. .dst_reg = DST, \
  261. .src_reg = SRC, \
  262. .off = OFF, \
  263. .imm = IMM })
  264. /* Program exit */
  265. #define BPF_EXIT_INSN() \
  266. ((struct bpf_insn) { \
  267. .code = BPF_JMP | BPF_EXIT, \
  268. .dst_reg = 0, \
  269. .src_reg = 0, \
  270. .off = 0, \
  271. .imm = 0 })
  272. /* Internal classic blocks for direct assignment */
  273. #define __BPF_STMT(CODE, K) \
  274. ((struct sock_filter) BPF_STMT(CODE, K))
  275. #define __BPF_JUMP(CODE, K, JT, JF) \
  276. ((struct sock_filter) BPF_JUMP(CODE, K, JT, JF))
  277. #define bytes_to_bpf_size(bytes) \
  278. ({ \
  279. int bpf_size = -EINVAL; \
  280. \
  281. if (bytes == sizeof(u8)) \
  282. bpf_size = BPF_B; \
  283. else if (bytes == sizeof(u16)) \
  284. bpf_size = BPF_H; \
  285. else if (bytes == sizeof(u32)) \
  286. bpf_size = BPF_W; \
  287. else if (bytes == sizeof(u64)) \
  288. bpf_size = BPF_DW; \
  289. \
  290. bpf_size; \
  291. })
  292. #define bpf_size_to_bytes(bpf_size) \
  293. ({ \
  294. int bytes = -EINVAL; \
  295. \
  296. if (bpf_size == BPF_B) \
  297. bytes = sizeof(u8); \
  298. else if (bpf_size == BPF_H) \
  299. bytes = sizeof(u16); \
  300. else if (bpf_size == BPF_W) \
  301. bytes = sizeof(u32); \
  302. else if (bpf_size == BPF_DW) \
  303. bytes = sizeof(u64); \
  304. \
  305. bytes; \
  306. })
  307. #define BPF_SIZEOF(type) \
  308. ({ \
  309. const int __size = bytes_to_bpf_size(sizeof(type)); \
  310. BUILD_BUG_ON(__size < 0); \
  311. __size; \
  312. })
  313. #define BPF_FIELD_SIZEOF(type, field) \
  314. ({ \
  315. const int __size = bytes_to_bpf_size(FIELD_SIZEOF(type, field)); \
  316. BUILD_BUG_ON(__size < 0); \
  317. __size; \
  318. })
  319. #define BPF_LDST_BYTES(insn) \
  320. ({ \
  321. const int __size = bpf_size_to_bytes(BPF_SIZE((insn)->code)); \
  322. WARN_ON(__size < 0); \
  323. __size; \
  324. })
  325. #define __BPF_MAP_0(m, v, ...) v
  326. #define __BPF_MAP_1(m, v, t, a, ...) m(t, a)
  327. #define __BPF_MAP_2(m, v, t, a, ...) m(t, a), __BPF_MAP_1(m, v, __VA_ARGS__)
  328. #define __BPF_MAP_3(m, v, t, a, ...) m(t, a), __BPF_MAP_2(m, v, __VA_ARGS__)
  329. #define __BPF_MAP_4(m, v, t, a, ...) m(t, a), __BPF_MAP_3(m, v, __VA_ARGS__)
  330. #define __BPF_MAP_5(m, v, t, a, ...) m(t, a), __BPF_MAP_4(m, v, __VA_ARGS__)
  331. #define __BPF_REG_0(...) __BPF_PAD(5)
  332. #define __BPF_REG_1(...) __BPF_MAP(1, __VA_ARGS__), __BPF_PAD(4)
  333. #define __BPF_REG_2(...) __BPF_MAP(2, __VA_ARGS__), __BPF_PAD(3)
  334. #define __BPF_REG_3(...) __BPF_MAP(3, __VA_ARGS__), __BPF_PAD(2)
  335. #define __BPF_REG_4(...) __BPF_MAP(4, __VA_ARGS__), __BPF_PAD(1)
  336. #define __BPF_REG_5(...) __BPF_MAP(5, __VA_ARGS__)
  337. #define __BPF_MAP(n, ...) __BPF_MAP_##n(__VA_ARGS__)
  338. #define __BPF_REG(n, ...) __BPF_REG_##n(__VA_ARGS__)
  339. #define __BPF_CAST(t, a) \
  340. (__force t) \
  341. (__force \
  342. typeof(__builtin_choose_expr(sizeof(t) == sizeof(unsigned long), \
  343. (unsigned long)0, (t)0))) a
  344. #define __BPF_V void
  345. #define __BPF_N
  346. #define __BPF_DECL_ARGS(t, a) t a
  347. #define __BPF_DECL_REGS(t, a) u64 a
  348. #define __BPF_PAD(n) \
  349. __BPF_MAP(n, __BPF_DECL_ARGS, __BPF_N, u64, __ur_1, u64, __ur_2, \
  350. u64, __ur_3, u64, __ur_4, u64, __ur_5)
  351. #define BPF_CALL_x(x, name, ...) \
  352. static __always_inline \
  353. u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \
  354. u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)); \
  355. u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)) \
  356. { \
  357. return ____##name(__BPF_MAP(x,__BPF_CAST,__BPF_N,__VA_ARGS__));\
  358. } \
  359. static __always_inline \
  360. u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__))
  361. #define BPF_CALL_0(name, ...) BPF_CALL_x(0, name, __VA_ARGS__)
  362. #define BPF_CALL_1(name, ...) BPF_CALL_x(1, name, __VA_ARGS__)
  363. #define BPF_CALL_2(name, ...) BPF_CALL_x(2, name, __VA_ARGS__)
  364. #define BPF_CALL_3(name, ...) BPF_CALL_x(3, name, __VA_ARGS__)
  365. #define BPF_CALL_4(name, ...) BPF_CALL_x(4, name, __VA_ARGS__)
  366. #define BPF_CALL_5(name, ...) BPF_CALL_x(5, name, __VA_ARGS__)
  367. #define bpf_ctx_range(TYPE, MEMBER) \
  368. offsetof(TYPE, MEMBER) ... offsetofend(TYPE, MEMBER) - 1
  369. #define bpf_ctx_range_till(TYPE, MEMBER1, MEMBER2) \
  370. offsetof(TYPE, MEMBER1) ... offsetofend(TYPE, MEMBER2) - 1
  371. #define bpf_target_off(TYPE, MEMBER, SIZE, PTR_SIZE) \
  372. ({ \
  373. BUILD_BUG_ON(FIELD_SIZEOF(TYPE, MEMBER) != (SIZE)); \
  374. *(PTR_SIZE) = (SIZE); \
  375. offsetof(TYPE, MEMBER); \
  376. })
  377. #ifdef CONFIG_COMPAT
  378. /* A struct sock_filter is architecture independent. */
  379. struct compat_sock_fprog {
  380. u16 len;
  381. compat_uptr_t filter; /* struct sock_filter * */
  382. };
  383. #endif
  384. struct sock_fprog_kern {
  385. u16 len;
  386. struct sock_filter *filter;
  387. };
  388. struct bpf_binary_header {
  389. u32 pages;
  390. /* Some arches need word alignment for their instructions */
  391. u8 image[] __aligned(4);
  392. };
  393. struct bpf_prog {
  394. u16 pages; /* Number of allocated pages */
  395. u16 jited:1, /* Is our filter JIT'ed? */
  396. jit_requested:1,/* archs need to JIT the prog */
  397. undo_set_mem:1, /* Passed set_memory_ro() checkpoint */
  398. gpl_compatible:1, /* Is filter GPL compatible? */
  399. cb_access:1, /* Is control block accessed? */
  400. dst_needed:1, /* Do we need dst entry? */
  401. blinded:1, /* Was blinded */
  402. is_func:1, /* program is a bpf function */
  403. kprobe_override:1, /* Do we override a kprobe? */
  404. has_callchain_buf:1; /* callchain buffer allocated? */
  405. enum bpf_prog_type type; /* Type of BPF program */
  406. enum bpf_attach_type expected_attach_type; /* For some prog types */
  407. u32 len; /* Number of filter blocks */
  408. u32 jited_len; /* Size of jited insns in bytes */
  409. u8 tag[BPF_TAG_SIZE];
  410. struct bpf_prog_aux *aux; /* Auxiliary fields */
  411. struct sock_fprog_kern *orig_prog; /* Original BPF program */
  412. unsigned int (*bpf_func)(const void *ctx,
  413. const struct bpf_insn *insn);
  414. /* Instructions for interpreter */
  415. union {
  416. struct sock_filter insns[0];
  417. struct bpf_insn insnsi[0];
  418. };
  419. };
  420. struct sk_filter {
  421. refcount_t refcnt;
  422. struct rcu_head rcu;
  423. struct bpf_prog *prog;
  424. };
  425. #define BPF_PROG_RUN(filter, ctx) (*(filter)->bpf_func)(ctx, (filter)->insnsi)
  426. #define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN
  427. struct bpf_skb_data_end {
  428. struct qdisc_skb_cb qdisc_cb;
  429. void *data_meta;
  430. void *data_end;
  431. };
  432. struct sk_msg_buff {
  433. void *data;
  434. void *data_end;
  435. __u32 apply_bytes;
  436. __u32 cork_bytes;
  437. int sg_copybreak;
  438. int sg_start;
  439. int sg_curr;
  440. int sg_end;
  441. struct scatterlist sg_data[MAX_SKB_FRAGS];
  442. bool sg_copy[MAX_SKB_FRAGS];
  443. __u32 flags;
  444. struct sock *sk_redir;
  445. struct sock *sk;
  446. struct sk_buff *skb;
  447. struct list_head list;
  448. };
  449. struct bpf_redirect_info {
  450. u32 ifindex;
  451. u32 flags;
  452. struct bpf_map *map;
  453. struct bpf_map *map_to_flush;
  454. u32 kern_flags;
  455. };
  456. DECLARE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
  457. /* flags for bpf_redirect_info kern_flags */
  458. #define BPF_RI_F_RF_NO_DIRECT BIT(0) /* no napi_direct on return_frame */
  459. /* Compute the linear packet data range [data, data_end) which
  460. * will be accessed by various program types (cls_bpf, act_bpf,
  461. * lwt, ...). Subsystems allowing direct data access must (!)
  462. * ensure that cb[] area can be written to when BPF program is
  463. * invoked (otherwise cb[] save/restore is necessary).
  464. */
  465. static inline void bpf_compute_data_pointers(struct sk_buff *skb)
  466. {
  467. struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
  468. BUILD_BUG_ON(sizeof(*cb) > FIELD_SIZEOF(struct sk_buff, cb));
  469. cb->data_meta = skb->data - skb_metadata_len(skb);
  470. cb->data_end = skb->data + skb_headlen(skb);
  471. }
  472. static inline u8 *bpf_skb_cb(struct sk_buff *skb)
  473. {
  474. /* eBPF programs may read/write skb->cb[] area to transfer meta
  475. * data between tail calls. Since this also needs to work with
  476. * tc, that scratch memory is mapped to qdisc_skb_cb's data area.
  477. *
  478. * In some socket filter cases, the cb unfortunately needs to be
  479. * saved/restored so that protocol specific skb->cb[] data won't
  480. * be lost. In any case, due to unpriviledged eBPF programs
  481. * attached to sockets, we need to clear the bpf_skb_cb() area
  482. * to not leak previous contents to user space.
  483. */
  484. BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) != BPF_SKB_CB_LEN);
  485. BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) !=
  486. FIELD_SIZEOF(struct qdisc_skb_cb, data));
  487. return qdisc_skb_cb(skb)->data;
  488. }
  489. static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
  490. struct sk_buff *skb)
  491. {
  492. u8 *cb_data = bpf_skb_cb(skb);
  493. u8 cb_saved[BPF_SKB_CB_LEN];
  494. u32 res;
  495. if (unlikely(prog->cb_access)) {
  496. memcpy(cb_saved, cb_data, sizeof(cb_saved));
  497. memset(cb_data, 0, sizeof(cb_saved));
  498. }
  499. res = BPF_PROG_RUN(prog, skb);
  500. if (unlikely(prog->cb_access))
  501. memcpy(cb_data, cb_saved, sizeof(cb_saved));
  502. return res;
  503. }
  504. static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog,
  505. struct sk_buff *skb)
  506. {
  507. u8 *cb_data = bpf_skb_cb(skb);
  508. if (unlikely(prog->cb_access))
  509. memset(cb_data, 0, BPF_SKB_CB_LEN);
  510. return BPF_PROG_RUN(prog, skb);
  511. }
  512. static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,
  513. struct xdp_buff *xdp)
  514. {
  515. /* Caller needs to hold rcu_read_lock() (!), otherwise program
  516. * can be released while still running, or map elements could be
  517. * freed early while still having concurrent users. XDP fastpath
  518. * already takes rcu_read_lock() when fetching the program, so
  519. * it's not necessary here anymore.
  520. */
  521. return BPF_PROG_RUN(prog, xdp);
  522. }
  523. static inline u32 bpf_prog_insn_size(const struct bpf_prog *prog)
  524. {
  525. return prog->len * sizeof(struct bpf_insn);
  526. }
  527. static inline u32 bpf_prog_tag_scratch_size(const struct bpf_prog *prog)
  528. {
  529. return round_up(bpf_prog_insn_size(prog) +
  530. sizeof(__be64) + 1, SHA_MESSAGE_BYTES);
  531. }
  532. static inline unsigned int bpf_prog_size(unsigned int proglen)
  533. {
  534. return max(sizeof(struct bpf_prog),
  535. offsetof(struct bpf_prog, insns[proglen]));
  536. }
  537. static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
  538. {
  539. /* When classic BPF programs have been loaded and the arch
  540. * does not have a classic BPF JIT (anymore), they have been
  541. * converted via bpf_migrate_filter() to eBPF and thus always
  542. * have an unspec program type.
  543. */
  544. return prog->type == BPF_PROG_TYPE_UNSPEC;
  545. }
  546. static inline u32 bpf_ctx_off_adjust_machine(u32 size)
  547. {
  548. const u32 size_machine = sizeof(unsigned long);
  549. if (size > size_machine && size % size_machine == 0)
  550. size = size_machine;
  551. return size;
  552. }
  553. static inline bool
  554. bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
  555. {
  556. return size <= size_default && (size & (size - 1)) == 0;
  557. }
  558. #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
  559. static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
  560. {
  561. fp->undo_set_mem = 1;
  562. set_memory_ro((unsigned long)fp, fp->pages);
  563. }
  564. static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
  565. {
  566. if (fp->undo_set_mem)
  567. set_memory_rw((unsigned long)fp, fp->pages);
  568. }
  569. static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
  570. {
  571. set_memory_ro((unsigned long)hdr, hdr->pages);
  572. set_memory_x((unsigned long)hdr, hdr->pages);
  573. }
  574. static inline void bpf_jit_binary_unlock_ro(struct bpf_binary_header *hdr)
  575. {
  576. set_memory_rw((unsigned long)hdr, hdr->pages);
  577. }
  578. static inline struct bpf_binary_header *
  579. bpf_jit_binary_hdr(const struct bpf_prog *fp)
  580. {
  581. unsigned long real_start = (unsigned long)fp->bpf_func;
  582. unsigned long addr = real_start & PAGE_MASK;
  583. return (void *)addr;
  584. }
  585. int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
  586. static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
  587. {
  588. return sk_filter_trim_cap(sk, skb, 1);
  589. }
  590. struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err);
  591. void bpf_prog_free(struct bpf_prog *fp);
  592. bool bpf_opcode_in_insntable(u8 code);
  593. struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
  594. struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
  595. gfp_t gfp_extra_flags);
  596. void __bpf_prog_free(struct bpf_prog *fp);
  597. static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
  598. {
  599. bpf_prog_unlock_ro(fp);
  600. __bpf_prog_free(fp);
  601. }
  602. typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
  603. unsigned int flen);
  604. int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
  605. int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
  606. bpf_aux_classic_check_t trans, bool save_orig);
  607. void bpf_prog_destroy(struct bpf_prog *fp);
  608. int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  609. int sk_attach_bpf(u32 ufd, struct sock *sk);
  610. int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  611. int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk);
  612. void sk_reuseport_prog_free(struct bpf_prog *prog);
  613. int sk_detach_filter(struct sock *sk);
  614. int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
  615. unsigned int len);
  616. bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
  617. void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
  618. u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  619. #define __bpf_call_base_args \
  620. ((u64 (*)(u64, u64, u64, u64, u64, const struct bpf_insn *)) \
  621. __bpf_call_base)
  622. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog);
  623. void bpf_jit_compile(struct bpf_prog *prog);
  624. bool bpf_helper_changes_pkt_data(void *func);
  625. static inline bool bpf_dump_raw_ok(void)
  626. {
  627. /* Reconstruction of call-sites is dependent on kallsyms,
  628. * thus make dump the same restriction.
  629. */
  630. return kallsyms_show_value() == 1;
  631. }
  632. struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
  633. const struct bpf_insn *patch, u32 len);
  634. void bpf_clear_redirect_map(struct bpf_map *map);
  635. static inline bool xdp_return_frame_no_direct(void)
  636. {
  637. struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
  638. return ri->kern_flags & BPF_RI_F_RF_NO_DIRECT;
  639. }
  640. static inline void xdp_set_return_frame_no_direct(void)
  641. {
  642. struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
  643. ri->kern_flags |= BPF_RI_F_RF_NO_DIRECT;
  644. }
  645. static inline void xdp_clear_return_frame_no_direct(void)
  646. {
  647. struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
  648. ri->kern_flags &= ~BPF_RI_F_RF_NO_DIRECT;
  649. }
  650. static inline int xdp_ok_fwd_dev(const struct net_device *fwd,
  651. unsigned int pktlen)
  652. {
  653. unsigned int len;
  654. if (unlikely(!(fwd->flags & IFF_UP)))
  655. return -ENETDOWN;
  656. len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
  657. if (pktlen > len)
  658. return -EMSGSIZE;
  659. return 0;
  660. }
  661. /* The pair of xdp_do_redirect and xdp_do_flush_map MUST be called in the
  662. * same cpu context. Further for best results no more than a single map
  663. * for the do_redirect/do_flush pair should be used. This limitation is
  664. * because we only track one map and force a flush when the map changes.
  665. * This does not appear to be a real limitation for existing software.
  666. */
  667. int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
  668. struct xdp_buff *xdp, struct bpf_prog *prog);
  669. int xdp_do_redirect(struct net_device *dev,
  670. struct xdp_buff *xdp,
  671. struct bpf_prog *prog);
  672. void xdp_do_flush_map(void);
  673. void bpf_warn_invalid_xdp_action(u32 act);
  674. struct sock *do_sk_redirect_map(struct sk_buff *skb);
  675. struct sock *do_msg_redirect_map(struct sk_msg_buff *md);
  676. #ifdef CONFIG_INET
  677. struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
  678. struct bpf_prog *prog, struct sk_buff *skb,
  679. u32 hash);
  680. #else
  681. static inline struct sock *
  682. bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
  683. struct bpf_prog *prog, struct sk_buff *skb,
  684. u32 hash)
  685. {
  686. return NULL;
  687. }
  688. #endif
  689. #ifdef CONFIG_BPF_JIT
  690. extern int bpf_jit_enable;
  691. extern int bpf_jit_harden;
  692. extern int bpf_jit_kallsyms;
  693. extern long bpf_jit_limit;
  694. typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
  695. struct bpf_binary_header *
  696. bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
  697. unsigned int alignment,
  698. bpf_jit_fill_hole_t bpf_fill_ill_insns);
  699. void bpf_jit_binary_free(struct bpf_binary_header *hdr);
  700. u64 bpf_jit_alloc_exec_limit(void);
  701. void *bpf_jit_alloc_exec(unsigned long size);
  702. void bpf_jit_free_exec(void *addr);
  703. void bpf_jit_free(struct bpf_prog *fp);
  704. struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *fp);
  705. void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);
  706. static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
  707. u32 pass, void *image)
  708. {
  709. pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen,
  710. proglen, pass, image, current->comm, task_pid_nr(current));
  711. if (image)
  712. print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
  713. 16, 1, image, proglen, false);
  714. }
  715. static inline bool bpf_jit_is_ebpf(void)
  716. {
  717. # ifdef CONFIG_HAVE_EBPF_JIT
  718. return true;
  719. # else
  720. return false;
  721. # endif
  722. }
  723. static inline bool ebpf_jit_enabled(void)
  724. {
  725. return bpf_jit_enable && bpf_jit_is_ebpf();
  726. }
  727. static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
  728. {
  729. return fp->jited && bpf_jit_is_ebpf();
  730. }
  731. static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
  732. {
  733. /* These are the prerequisites, should someone ever have the
  734. * idea to call blinding outside of them, we make sure to
  735. * bail out.
  736. */
  737. if (!bpf_jit_is_ebpf())
  738. return false;
  739. if (!prog->jit_requested)
  740. return false;
  741. if (!bpf_jit_harden)
  742. return false;
  743. if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
  744. return false;
  745. return true;
  746. }
  747. static inline bool bpf_jit_kallsyms_enabled(void)
  748. {
  749. /* There are a couple of corner cases where kallsyms should
  750. * not be enabled f.e. on hardening.
  751. */
  752. if (bpf_jit_harden)
  753. return false;
  754. if (!bpf_jit_kallsyms)
  755. return false;
  756. if (bpf_jit_kallsyms == 1)
  757. return true;
  758. return false;
  759. }
  760. const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
  761. unsigned long *off, char *sym);
  762. bool is_bpf_text_address(unsigned long addr);
  763. int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  764. char *sym);
  765. static inline const char *
  766. bpf_address_lookup(unsigned long addr, unsigned long *size,
  767. unsigned long *off, char **modname, char *sym)
  768. {
  769. const char *ret = __bpf_address_lookup(addr, size, off, sym);
  770. if (ret && modname)
  771. *modname = NULL;
  772. return ret;
  773. }
  774. void bpf_prog_kallsyms_add(struct bpf_prog *fp);
  775. void bpf_prog_kallsyms_del(struct bpf_prog *fp);
  776. #else /* CONFIG_BPF_JIT */
  777. static inline bool ebpf_jit_enabled(void)
  778. {
  779. return false;
  780. }
  781. static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)
  782. {
  783. return false;
  784. }
  785. static inline void bpf_jit_free(struct bpf_prog *fp)
  786. {
  787. bpf_prog_unlock_free(fp);
  788. }
  789. static inline bool bpf_jit_kallsyms_enabled(void)
  790. {
  791. return false;
  792. }
  793. static inline const char *
  794. __bpf_address_lookup(unsigned long addr, unsigned long *size,
  795. unsigned long *off, char *sym)
  796. {
  797. return NULL;
  798. }
  799. static inline bool is_bpf_text_address(unsigned long addr)
  800. {
  801. return false;
  802. }
  803. static inline int bpf_get_kallsym(unsigned int symnum, unsigned long *value,
  804. char *type, char *sym)
  805. {
  806. return -ERANGE;
  807. }
  808. static inline const char *
  809. bpf_address_lookup(unsigned long addr, unsigned long *size,
  810. unsigned long *off, char **modname, char *sym)
  811. {
  812. return NULL;
  813. }
  814. static inline void bpf_prog_kallsyms_add(struct bpf_prog *fp)
  815. {
  816. }
  817. static inline void bpf_prog_kallsyms_del(struct bpf_prog *fp)
  818. {
  819. }
  820. #endif /* CONFIG_BPF_JIT */
  821. void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp);
  822. void bpf_prog_kallsyms_del_all(struct bpf_prog *fp);
  823. #define BPF_ANC BIT(15)
  824. static inline bool bpf_needs_clear_a(const struct sock_filter *first)
  825. {
  826. switch (first->code) {
  827. case BPF_RET | BPF_K:
  828. case BPF_LD | BPF_W | BPF_LEN:
  829. return false;
  830. case BPF_LD | BPF_W | BPF_ABS:
  831. case BPF_LD | BPF_H | BPF_ABS:
  832. case BPF_LD | BPF_B | BPF_ABS:
  833. if (first->k == SKF_AD_OFF + SKF_AD_ALU_XOR_X)
  834. return true;
  835. return false;
  836. default:
  837. return true;
  838. }
  839. }
  840. static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
  841. {
  842. BUG_ON(ftest->code & BPF_ANC);
  843. switch (ftest->code) {
  844. case BPF_LD | BPF_W | BPF_ABS:
  845. case BPF_LD | BPF_H | BPF_ABS:
  846. case BPF_LD | BPF_B | BPF_ABS:
  847. #define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
  848. return BPF_ANC | SKF_AD_##CODE
  849. switch (ftest->k) {
  850. BPF_ANCILLARY(PROTOCOL);
  851. BPF_ANCILLARY(PKTTYPE);
  852. BPF_ANCILLARY(IFINDEX);
  853. BPF_ANCILLARY(NLATTR);
  854. BPF_ANCILLARY(NLATTR_NEST);
  855. BPF_ANCILLARY(MARK);
  856. BPF_ANCILLARY(QUEUE);
  857. BPF_ANCILLARY(HATYPE);
  858. BPF_ANCILLARY(RXHASH);
  859. BPF_ANCILLARY(CPU);
  860. BPF_ANCILLARY(ALU_XOR_X);
  861. BPF_ANCILLARY(VLAN_TAG);
  862. BPF_ANCILLARY(VLAN_TAG_PRESENT);
  863. BPF_ANCILLARY(PAY_OFFSET);
  864. BPF_ANCILLARY(RANDOM);
  865. BPF_ANCILLARY(VLAN_TPID);
  866. }
  867. /* Fallthrough. */
  868. default:
  869. return ftest->code;
  870. }
  871. }
  872. void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
  873. int k, unsigned int size);
  874. static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
  875. unsigned int size, void *buffer)
  876. {
  877. if (k >= 0)
  878. return skb_header_pointer(skb, k, size, buffer);
  879. return bpf_internal_load_pointer_neg_helper(skb, k, size);
  880. }
  881. static inline int bpf_tell_extensions(void)
  882. {
  883. return SKF_AD_MAX;
  884. }
  885. struct bpf_sock_addr_kern {
  886. struct sock *sk;
  887. struct sockaddr *uaddr;
  888. /* Temporary "register" to make indirect stores to nested structures
  889. * defined above. We need three registers to make such a store, but
  890. * only two (src and dst) are available at convert_ctx_access time
  891. */
  892. u64 tmp_reg;
  893. void *t_ctx; /* Attach type specific context. */
  894. };
  895. struct bpf_sock_ops_kern {
  896. struct sock *sk;
  897. u32 op;
  898. union {
  899. u32 args[4];
  900. u32 reply;
  901. u32 replylong[4];
  902. };
  903. u32 is_fullsock;
  904. u64 temp; /* temp and everything after is not
  905. * initialized to 0 before calling
  906. * the BPF program. New fields that
  907. * should be initialized to 0 should
  908. * be inserted before temp.
  909. * temp is scratch storage used by
  910. * sock_ops_convert_ctx_access
  911. * as temporary storage of a register.
  912. */
  913. };
  914. #endif /* __LINUX_FILTER_H__ */