filter.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * Linux Socket Filter Data Structures
  3. */
  4. #ifndef __LINUX_FILTER_H__
  5. #define __LINUX_FILTER_H__
  6. #include <stdarg.h>
  7. #include <linux/atomic.h>
  8. #include <linux/compat.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/linkage.h>
  11. #include <linux/printk.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/sched.h>
  14. #include <linux/capability.h>
  15. #include <net/sch_generic.h>
  16. #include <asm/cacheflush.h>
  17. #include <uapi/linux/filter.h>
  18. #include <uapi/linux/bpf.h>
  19. struct sk_buff;
  20. struct sock;
  21. struct seccomp_data;
  22. struct bpf_prog_aux;
  23. /* ArgX, context and stack frame pointer register positions. Note,
  24. * Arg1, Arg2, Arg3, etc are used as argument mappings of function
  25. * calls in BPF_CALL instruction.
  26. */
  27. #define BPF_REG_ARG1 BPF_REG_1
  28. #define BPF_REG_ARG2 BPF_REG_2
  29. #define BPF_REG_ARG3 BPF_REG_3
  30. #define BPF_REG_ARG4 BPF_REG_4
  31. #define BPF_REG_ARG5 BPF_REG_5
  32. #define BPF_REG_CTX BPF_REG_6
  33. #define BPF_REG_FP BPF_REG_10
  34. /* Additional register mappings for converted user programs. */
  35. #define BPF_REG_A BPF_REG_0
  36. #define BPF_REG_X BPF_REG_7
  37. #define BPF_REG_TMP BPF_REG_8
  38. /* Kernel hidden auxiliary/helper register for hardening step.
  39. * Only used by eBPF JITs. It's nothing more than a temporary
  40. * register that JITs use internally, only that here it's part
  41. * of eBPF instructions that have been rewritten for blinding
  42. * constants. See JIT pre-step in bpf_jit_blind_constants().
  43. */
  44. #define BPF_REG_AX MAX_BPF_REG
  45. #define MAX_BPF_JIT_REG (MAX_BPF_REG + 1)
  46. /* BPF program can access up to 512 bytes of stack space. */
  47. #define MAX_BPF_STACK 512
  48. /* Helper macros for filter block array initializers. */
  49. /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
  50. #define BPF_ALU64_REG(OP, DST, SRC) \
  51. ((struct bpf_insn) { \
  52. .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
  53. .dst_reg = DST, \
  54. .src_reg = SRC, \
  55. .off = 0, \
  56. .imm = 0 })
  57. #define BPF_ALU32_REG(OP, DST, SRC) \
  58. ((struct bpf_insn) { \
  59. .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
  60. .dst_reg = DST, \
  61. .src_reg = SRC, \
  62. .off = 0, \
  63. .imm = 0 })
  64. /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
  65. #define BPF_ALU64_IMM(OP, DST, IMM) \
  66. ((struct bpf_insn) { \
  67. .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
  68. .dst_reg = DST, \
  69. .src_reg = 0, \
  70. .off = 0, \
  71. .imm = IMM })
  72. #define BPF_ALU32_IMM(OP, DST, IMM) \
  73. ((struct bpf_insn) { \
  74. .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
  75. .dst_reg = DST, \
  76. .src_reg = 0, \
  77. .off = 0, \
  78. .imm = IMM })
  79. /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
  80. #define BPF_ENDIAN(TYPE, DST, LEN) \
  81. ((struct bpf_insn) { \
  82. .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
  83. .dst_reg = DST, \
  84. .src_reg = 0, \
  85. .off = 0, \
  86. .imm = LEN })
  87. /* Short form of mov, dst_reg = src_reg */
  88. #define BPF_MOV64_REG(DST, SRC) \
  89. ((struct bpf_insn) { \
  90. .code = BPF_ALU64 | BPF_MOV | BPF_X, \
  91. .dst_reg = DST, \
  92. .src_reg = SRC, \
  93. .off = 0, \
  94. .imm = 0 })
  95. #define BPF_MOV32_REG(DST, SRC) \
  96. ((struct bpf_insn) { \
  97. .code = BPF_ALU | BPF_MOV | BPF_X, \
  98. .dst_reg = DST, \
  99. .src_reg = SRC, \
  100. .off = 0, \
  101. .imm = 0 })
  102. /* Short form of mov, dst_reg = imm32 */
  103. #define BPF_MOV64_IMM(DST, IMM) \
  104. ((struct bpf_insn) { \
  105. .code = BPF_ALU64 | BPF_MOV | BPF_K, \
  106. .dst_reg = DST, \
  107. .src_reg = 0, \
  108. .off = 0, \
  109. .imm = IMM })
  110. #define BPF_MOV32_IMM(DST, IMM) \
  111. ((struct bpf_insn) { \
  112. .code = BPF_ALU | BPF_MOV | BPF_K, \
  113. .dst_reg = DST, \
  114. .src_reg = 0, \
  115. .off = 0, \
  116. .imm = IMM })
  117. /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
  118. #define BPF_LD_IMM64(DST, IMM) \
  119. BPF_LD_IMM64_RAW(DST, 0, IMM)
  120. #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
  121. ((struct bpf_insn) { \
  122. .code = BPF_LD | BPF_DW | BPF_IMM, \
  123. .dst_reg = DST, \
  124. .src_reg = SRC, \
  125. .off = 0, \
  126. .imm = (__u32) (IMM) }), \
  127. ((struct bpf_insn) { \
  128. .code = 0, /* zero is reserved opcode */ \
  129. .dst_reg = 0, \
  130. .src_reg = 0, \
  131. .off = 0, \
  132. .imm = ((__u64) (IMM)) >> 32 })
  133. /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
  134. #define BPF_LD_MAP_FD(DST, MAP_FD) \
  135. BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
  136. /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
  137. #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
  138. ((struct bpf_insn) { \
  139. .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
  140. .dst_reg = DST, \
  141. .src_reg = SRC, \
  142. .off = 0, \
  143. .imm = IMM })
  144. #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
  145. ((struct bpf_insn) { \
  146. .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
  147. .dst_reg = DST, \
  148. .src_reg = SRC, \
  149. .off = 0, \
  150. .imm = IMM })
  151. /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
  152. #define BPF_LD_ABS(SIZE, IMM) \
  153. ((struct bpf_insn) { \
  154. .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
  155. .dst_reg = 0, \
  156. .src_reg = 0, \
  157. .off = 0, \
  158. .imm = IMM })
  159. /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
  160. #define BPF_LD_IND(SIZE, SRC, IMM) \
  161. ((struct bpf_insn) { \
  162. .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
  163. .dst_reg = 0, \
  164. .src_reg = SRC, \
  165. .off = 0, \
  166. .imm = IMM })
  167. /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
  168. #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
  169. ((struct bpf_insn) { \
  170. .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
  171. .dst_reg = DST, \
  172. .src_reg = SRC, \
  173. .off = OFF, \
  174. .imm = 0 })
  175. /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
  176. #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
  177. ((struct bpf_insn) { \
  178. .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
  179. .dst_reg = DST, \
  180. .src_reg = SRC, \
  181. .off = OFF, \
  182. .imm = 0 })
  183. /* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
  184. #define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
  185. ((struct bpf_insn) { \
  186. .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
  187. .dst_reg = DST, \
  188. .src_reg = SRC, \
  189. .off = OFF, \
  190. .imm = 0 })
  191. /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
  192. #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
  193. ((struct bpf_insn) { \
  194. .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
  195. .dst_reg = DST, \
  196. .src_reg = 0, \
  197. .off = OFF, \
  198. .imm = IMM })
  199. /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
  200. #define BPF_JMP_REG(OP, DST, SRC, OFF) \
  201. ((struct bpf_insn) { \
  202. .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
  203. .dst_reg = DST, \
  204. .src_reg = SRC, \
  205. .off = OFF, \
  206. .imm = 0 })
  207. /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
  208. #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
  209. ((struct bpf_insn) { \
  210. .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
  211. .dst_reg = DST, \
  212. .src_reg = 0, \
  213. .off = OFF, \
  214. .imm = IMM })
  215. /* Function call */
  216. #define BPF_EMIT_CALL(FUNC) \
  217. ((struct bpf_insn) { \
  218. .code = BPF_JMP | BPF_CALL, \
  219. .dst_reg = 0, \
  220. .src_reg = 0, \
  221. .off = 0, \
  222. .imm = ((FUNC) - __bpf_call_base) })
  223. /* Raw code statement block */
  224. #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
  225. ((struct bpf_insn) { \
  226. .code = CODE, \
  227. .dst_reg = DST, \
  228. .src_reg = SRC, \
  229. .off = OFF, \
  230. .imm = IMM })
  231. /* Program exit */
  232. #define BPF_EXIT_INSN() \
  233. ((struct bpf_insn) { \
  234. .code = BPF_JMP | BPF_EXIT, \
  235. .dst_reg = 0, \
  236. .src_reg = 0, \
  237. .off = 0, \
  238. .imm = 0 })
  239. /* Internal classic blocks for direct assignment */
  240. #define __BPF_STMT(CODE, K) \
  241. ((struct sock_filter) BPF_STMT(CODE, K))
  242. #define __BPF_JUMP(CODE, K, JT, JF) \
  243. ((struct sock_filter) BPF_JUMP(CODE, K, JT, JF))
  244. #define bytes_to_bpf_size(bytes) \
  245. ({ \
  246. int bpf_size = -EINVAL; \
  247. \
  248. if (bytes == sizeof(u8)) \
  249. bpf_size = BPF_B; \
  250. else if (bytes == sizeof(u16)) \
  251. bpf_size = BPF_H; \
  252. else if (bytes == sizeof(u32)) \
  253. bpf_size = BPF_W; \
  254. else if (bytes == sizeof(u64)) \
  255. bpf_size = BPF_DW; \
  256. \
  257. bpf_size; \
  258. })
  259. #define BPF_SIZEOF(type) \
  260. ({ \
  261. const int __size = bytes_to_bpf_size(sizeof(type)); \
  262. BUILD_BUG_ON(__size < 0); \
  263. __size; \
  264. })
  265. #define BPF_FIELD_SIZEOF(type, field) \
  266. ({ \
  267. const int __size = bytes_to_bpf_size(FIELD_SIZEOF(type, field)); \
  268. BUILD_BUG_ON(__size < 0); \
  269. __size; \
  270. })
  271. #define __BPF_MAP_0(m, v, ...) v
  272. #define __BPF_MAP_1(m, v, t, a, ...) m(t, a)
  273. #define __BPF_MAP_2(m, v, t, a, ...) m(t, a), __BPF_MAP_1(m, v, __VA_ARGS__)
  274. #define __BPF_MAP_3(m, v, t, a, ...) m(t, a), __BPF_MAP_2(m, v, __VA_ARGS__)
  275. #define __BPF_MAP_4(m, v, t, a, ...) m(t, a), __BPF_MAP_3(m, v, __VA_ARGS__)
  276. #define __BPF_MAP_5(m, v, t, a, ...) m(t, a), __BPF_MAP_4(m, v, __VA_ARGS__)
  277. #define __BPF_REG_0(...) __BPF_PAD(5)
  278. #define __BPF_REG_1(...) __BPF_MAP(1, __VA_ARGS__), __BPF_PAD(4)
  279. #define __BPF_REG_2(...) __BPF_MAP(2, __VA_ARGS__), __BPF_PAD(3)
  280. #define __BPF_REG_3(...) __BPF_MAP(3, __VA_ARGS__), __BPF_PAD(2)
  281. #define __BPF_REG_4(...) __BPF_MAP(4, __VA_ARGS__), __BPF_PAD(1)
  282. #define __BPF_REG_5(...) __BPF_MAP(5, __VA_ARGS__)
  283. #define __BPF_MAP(n, ...) __BPF_MAP_##n(__VA_ARGS__)
  284. #define __BPF_REG(n, ...) __BPF_REG_##n(__VA_ARGS__)
  285. #define __BPF_CAST(t, a) \
  286. (__force t) \
  287. (__force \
  288. typeof(__builtin_choose_expr(sizeof(t) == sizeof(unsigned long), \
  289. (unsigned long)0, (t)0))) a
  290. #define __BPF_V void
  291. #define __BPF_N
  292. #define __BPF_DECL_ARGS(t, a) t a
  293. #define __BPF_DECL_REGS(t, a) u64 a
  294. #define __BPF_PAD(n) \
  295. __BPF_MAP(n, __BPF_DECL_ARGS, __BPF_N, u64, __ur_1, u64, __ur_2, \
  296. u64, __ur_3, u64, __ur_4, u64, __ur_5)
  297. #define BPF_CALL_x(x, name, ...) \
  298. static __always_inline \
  299. u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \
  300. u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)); \
  301. u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)) \
  302. { \
  303. return ____##name(__BPF_MAP(x,__BPF_CAST,__BPF_N,__VA_ARGS__));\
  304. } \
  305. static __always_inline \
  306. u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__))
  307. #define BPF_CALL_0(name, ...) BPF_CALL_x(0, name, __VA_ARGS__)
  308. #define BPF_CALL_1(name, ...) BPF_CALL_x(1, name, __VA_ARGS__)
  309. #define BPF_CALL_2(name, ...) BPF_CALL_x(2, name, __VA_ARGS__)
  310. #define BPF_CALL_3(name, ...) BPF_CALL_x(3, name, __VA_ARGS__)
  311. #define BPF_CALL_4(name, ...) BPF_CALL_x(4, name, __VA_ARGS__)
  312. #define BPF_CALL_5(name, ...) BPF_CALL_x(5, name, __VA_ARGS__)
  313. #ifdef CONFIG_COMPAT
  314. /* A struct sock_filter is architecture independent. */
  315. struct compat_sock_fprog {
  316. u16 len;
  317. compat_uptr_t filter; /* struct sock_filter * */
  318. };
  319. #endif
  320. struct sock_fprog_kern {
  321. u16 len;
  322. struct sock_filter *filter;
  323. };
  324. struct bpf_binary_header {
  325. unsigned int pages;
  326. u8 image[];
  327. };
  328. struct bpf_prog {
  329. u16 pages; /* Number of allocated pages */
  330. kmemcheck_bitfield_begin(meta);
  331. u16 jited:1, /* Is our filter JIT'ed? */
  332. gpl_compatible:1, /* Is filter GPL compatible? */
  333. cb_access:1, /* Is control block accessed? */
  334. dst_needed:1; /* Do we need dst entry? */
  335. kmemcheck_bitfield_end(meta);
  336. u32 len; /* Number of filter blocks */
  337. enum bpf_prog_type type; /* Type of BPF program */
  338. struct bpf_prog_aux *aux; /* Auxiliary fields */
  339. struct sock_fprog_kern *orig_prog; /* Original BPF program */
  340. unsigned int (*bpf_func)(const struct sk_buff *skb,
  341. const struct bpf_insn *filter);
  342. /* Instructions for interpreter */
  343. union {
  344. struct sock_filter insns[0];
  345. struct bpf_insn insnsi[0];
  346. };
  347. };
  348. struct sk_filter {
  349. atomic_t refcnt;
  350. struct rcu_head rcu;
  351. struct bpf_prog *prog;
  352. };
  353. #define BPF_PROG_RUN(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
  354. #define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN
  355. struct bpf_skb_data_end {
  356. struct qdisc_skb_cb qdisc_cb;
  357. void *data_end;
  358. };
  359. struct xdp_buff {
  360. void *data;
  361. void *data_end;
  362. };
  363. /* compute the linear packet data range [data, data_end) which
  364. * will be accessed by cls_bpf and act_bpf programs
  365. */
  366. static inline void bpf_compute_data_end(struct sk_buff *skb)
  367. {
  368. struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
  369. BUILD_BUG_ON(sizeof(*cb) > FIELD_SIZEOF(struct sk_buff, cb));
  370. cb->data_end = skb->data + skb_headlen(skb);
  371. }
  372. static inline u8 *bpf_skb_cb(struct sk_buff *skb)
  373. {
  374. /* eBPF programs may read/write skb->cb[] area to transfer meta
  375. * data between tail calls. Since this also needs to work with
  376. * tc, that scratch memory is mapped to qdisc_skb_cb's data area.
  377. *
  378. * In some socket filter cases, the cb unfortunately needs to be
  379. * saved/restored so that protocol specific skb->cb[] data won't
  380. * be lost. In any case, due to unpriviledged eBPF programs
  381. * attached to sockets, we need to clear the bpf_skb_cb() area
  382. * to not leak previous contents to user space.
  383. */
  384. BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) != BPF_SKB_CB_LEN);
  385. BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) !=
  386. FIELD_SIZEOF(struct qdisc_skb_cb, data));
  387. return qdisc_skb_cb(skb)->data;
  388. }
  389. static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
  390. struct sk_buff *skb)
  391. {
  392. u8 *cb_data = bpf_skb_cb(skb);
  393. u8 cb_saved[BPF_SKB_CB_LEN];
  394. u32 res;
  395. if (unlikely(prog->cb_access)) {
  396. memcpy(cb_saved, cb_data, sizeof(cb_saved));
  397. memset(cb_data, 0, sizeof(cb_saved));
  398. }
  399. res = BPF_PROG_RUN(prog, skb);
  400. if (unlikely(prog->cb_access))
  401. memcpy(cb_data, cb_saved, sizeof(cb_saved));
  402. return res;
  403. }
  404. static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog,
  405. struct sk_buff *skb)
  406. {
  407. u8 *cb_data = bpf_skb_cb(skb);
  408. if (unlikely(prog->cb_access))
  409. memset(cb_data, 0, BPF_SKB_CB_LEN);
  410. return BPF_PROG_RUN(prog, skb);
  411. }
  412. static inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,
  413. struct xdp_buff *xdp)
  414. {
  415. u32 ret;
  416. rcu_read_lock();
  417. ret = BPF_PROG_RUN(prog, (void *)xdp);
  418. rcu_read_unlock();
  419. return ret;
  420. }
  421. static inline unsigned int bpf_prog_size(unsigned int proglen)
  422. {
  423. return max(sizeof(struct bpf_prog),
  424. offsetof(struct bpf_prog, insns[proglen]));
  425. }
  426. static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
  427. {
  428. /* When classic BPF programs have been loaded and the arch
  429. * does not have a classic BPF JIT (anymore), they have been
  430. * converted via bpf_migrate_filter() to eBPF and thus always
  431. * have an unspec program type.
  432. */
  433. return prog->type == BPF_PROG_TYPE_UNSPEC;
  434. }
  435. #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
  436. #ifdef CONFIG_DEBUG_SET_MODULE_RONX
  437. static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
  438. {
  439. set_memory_ro((unsigned long)fp, fp->pages);
  440. }
  441. static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
  442. {
  443. set_memory_rw((unsigned long)fp, fp->pages);
  444. }
  445. #else
  446. static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
  447. {
  448. }
  449. static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
  450. {
  451. }
  452. #endif /* CONFIG_DEBUG_SET_MODULE_RONX */
  453. int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
  454. static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
  455. {
  456. return sk_filter_trim_cap(sk, skb, 1);
  457. }
  458. struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err);
  459. void bpf_prog_free(struct bpf_prog *fp);
  460. struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
  461. struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
  462. gfp_t gfp_extra_flags);
  463. void __bpf_prog_free(struct bpf_prog *fp);
  464. static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
  465. {
  466. bpf_prog_unlock_ro(fp);
  467. __bpf_prog_free(fp);
  468. }
  469. typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
  470. unsigned int flen);
  471. int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
  472. int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
  473. bpf_aux_classic_check_t trans, bool save_orig);
  474. void bpf_prog_destroy(struct bpf_prog *fp);
  475. int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  476. int sk_attach_bpf(u32 ufd, struct sock *sk);
  477. int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  478. int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk);
  479. int sk_detach_filter(struct sock *sk);
  480. int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
  481. unsigned int len);
  482. bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
  483. void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
  484. u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  485. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog);
  486. bool bpf_helper_changes_skb_data(void *func);
  487. struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
  488. const struct bpf_insn *patch, u32 len);
  489. void bpf_warn_invalid_xdp_action(u32 act);
  490. #ifdef CONFIG_BPF_JIT
  491. extern int bpf_jit_enable;
  492. extern int bpf_jit_harden;
  493. typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
  494. struct bpf_binary_header *
  495. bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
  496. unsigned int alignment,
  497. bpf_jit_fill_hole_t bpf_fill_ill_insns);
  498. void bpf_jit_binary_free(struct bpf_binary_header *hdr);
  499. void bpf_jit_compile(struct bpf_prog *fp);
  500. void bpf_jit_free(struct bpf_prog *fp);
  501. struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *fp);
  502. void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);
  503. static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
  504. u32 pass, void *image)
  505. {
  506. pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen,
  507. proglen, pass, image, current->comm, task_pid_nr(current));
  508. if (image)
  509. print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
  510. 16, 1, image, proglen, false);
  511. }
  512. static inline bool bpf_jit_is_ebpf(void)
  513. {
  514. # ifdef CONFIG_HAVE_EBPF_JIT
  515. return true;
  516. # else
  517. return false;
  518. # endif
  519. }
  520. static inline bool bpf_jit_blinding_enabled(void)
  521. {
  522. /* These are the prerequisites, should someone ever have the
  523. * idea to call blinding outside of them, we make sure to
  524. * bail out.
  525. */
  526. if (!bpf_jit_is_ebpf())
  527. return false;
  528. if (!bpf_jit_enable)
  529. return false;
  530. if (!bpf_jit_harden)
  531. return false;
  532. if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
  533. return false;
  534. return true;
  535. }
  536. #else
  537. static inline void bpf_jit_compile(struct bpf_prog *fp)
  538. {
  539. }
  540. static inline void bpf_jit_free(struct bpf_prog *fp)
  541. {
  542. bpf_prog_unlock_free(fp);
  543. }
  544. #endif /* CONFIG_BPF_JIT */
  545. #define BPF_ANC BIT(15)
  546. static inline bool bpf_needs_clear_a(const struct sock_filter *first)
  547. {
  548. switch (first->code) {
  549. case BPF_RET | BPF_K:
  550. case BPF_LD | BPF_W | BPF_LEN:
  551. return false;
  552. case BPF_LD | BPF_W | BPF_ABS:
  553. case BPF_LD | BPF_H | BPF_ABS:
  554. case BPF_LD | BPF_B | BPF_ABS:
  555. if (first->k == SKF_AD_OFF + SKF_AD_ALU_XOR_X)
  556. return true;
  557. return false;
  558. default:
  559. return true;
  560. }
  561. }
  562. static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
  563. {
  564. BUG_ON(ftest->code & BPF_ANC);
  565. switch (ftest->code) {
  566. case BPF_LD | BPF_W | BPF_ABS:
  567. case BPF_LD | BPF_H | BPF_ABS:
  568. case BPF_LD | BPF_B | BPF_ABS:
  569. #define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
  570. return BPF_ANC | SKF_AD_##CODE
  571. switch (ftest->k) {
  572. BPF_ANCILLARY(PROTOCOL);
  573. BPF_ANCILLARY(PKTTYPE);
  574. BPF_ANCILLARY(IFINDEX);
  575. BPF_ANCILLARY(NLATTR);
  576. BPF_ANCILLARY(NLATTR_NEST);
  577. BPF_ANCILLARY(MARK);
  578. BPF_ANCILLARY(QUEUE);
  579. BPF_ANCILLARY(HATYPE);
  580. BPF_ANCILLARY(RXHASH);
  581. BPF_ANCILLARY(CPU);
  582. BPF_ANCILLARY(ALU_XOR_X);
  583. BPF_ANCILLARY(VLAN_TAG);
  584. BPF_ANCILLARY(VLAN_TAG_PRESENT);
  585. BPF_ANCILLARY(PAY_OFFSET);
  586. BPF_ANCILLARY(RANDOM);
  587. BPF_ANCILLARY(VLAN_TPID);
  588. }
  589. /* Fallthrough. */
  590. default:
  591. return ftest->code;
  592. }
  593. }
  594. void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
  595. int k, unsigned int size);
  596. static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
  597. unsigned int size, void *buffer)
  598. {
  599. if (k >= 0)
  600. return skb_header_pointer(skb, k, size, buffer);
  601. return bpf_internal_load_pointer_neg_helper(skb, k, size);
  602. }
  603. static inline int bpf_tell_extensions(void)
  604. {
  605. return SKF_AD_MAX;
  606. }
  607. #endif /* __LINUX_FILTER_H__ */