filter.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 <net/sch_generic.h>
  15. #include <asm/cacheflush.h>
  16. #include <uapi/linux/filter.h>
  17. #include <uapi/linux/bpf.h>
  18. struct sk_buff;
  19. struct sock;
  20. struct seccomp_data;
  21. struct bpf_prog_aux;
  22. /* ArgX, context and stack frame pointer register positions. Note,
  23. * Arg1, Arg2, Arg3, etc are used as argument mappings of function
  24. * calls in BPF_CALL instruction.
  25. */
  26. #define BPF_REG_ARG1 BPF_REG_1
  27. #define BPF_REG_ARG2 BPF_REG_2
  28. #define BPF_REG_ARG3 BPF_REG_3
  29. #define BPF_REG_ARG4 BPF_REG_4
  30. #define BPF_REG_ARG5 BPF_REG_5
  31. #define BPF_REG_CTX BPF_REG_6
  32. #define BPF_REG_FP BPF_REG_10
  33. /* Additional register mappings for converted user programs. */
  34. #define BPF_REG_A BPF_REG_0
  35. #define BPF_REG_X BPF_REG_7
  36. #define BPF_REG_TMP BPF_REG_8
  37. /* BPF program can access up to 512 bytes of stack space. */
  38. #define MAX_BPF_STACK 512
  39. /* Helper macros for filter block array initializers. */
  40. /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
  41. #define BPF_ALU64_REG(OP, DST, SRC) \
  42. ((struct bpf_insn) { \
  43. .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
  44. .dst_reg = DST, \
  45. .src_reg = SRC, \
  46. .off = 0, \
  47. .imm = 0 })
  48. #define BPF_ALU32_REG(OP, DST, SRC) \
  49. ((struct bpf_insn) { \
  50. .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
  51. .dst_reg = DST, \
  52. .src_reg = SRC, \
  53. .off = 0, \
  54. .imm = 0 })
  55. /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
  56. #define BPF_ALU64_IMM(OP, DST, IMM) \
  57. ((struct bpf_insn) { \
  58. .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
  59. .dst_reg = DST, \
  60. .src_reg = 0, \
  61. .off = 0, \
  62. .imm = IMM })
  63. #define BPF_ALU32_IMM(OP, DST, IMM) \
  64. ((struct bpf_insn) { \
  65. .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
  66. .dst_reg = DST, \
  67. .src_reg = 0, \
  68. .off = 0, \
  69. .imm = IMM })
  70. /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
  71. #define BPF_ENDIAN(TYPE, DST, LEN) \
  72. ((struct bpf_insn) { \
  73. .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
  74. .dst_reg = DST, \
  75. .src_reg = 0, \
  76. .off = 0, \
  77. .imm = LEN })
  78. /* Short form of mov, dst_reg = src_reg */
  79. #define BPF_MOV64_REG(DST, SRC) \
  80. ((struct bpf_insn) { \
  81. .code = BPF_ALU64 | BPF_MOV | BPF_X, \
  82. .dst_reg = DST, \
  83. .src_reg = SRC, \
  84. .off = 0, \
  85. .imm = 0 })
  86. #define BPF_MOV32_REG(DST, SRC) \
  87. ((struct bpf_insn) { \
  88. .code = BPF_ALU | BPF_MOV | BPF_X, \
  89. .dst_reg = DST, \
  90. .src_reg = SRC, \
  91. .off = 0, \
  92. .imm = 0 })
  93. /* Short form of mov, dst_reg = imm32 */
  94. #define BPF_MOV64_IMM(DST, IMM) \
  95. ((struct bpf_insn) { \
  96. .code = BPF_ALU64 | BPF_MOV | BPF_K, \
  97. .dst_reg = DST, \
  98. .src_reg = 0, \
  99. .off = 0, \
  100. .imm = IMM })
  101. #define BPF_MOV32_IMM(DST, IMM) \
  102. ((struct bpf_insn) { \
  103. .code = BPF_ALU | BPF_MOV | BPF_K, \
  104. .dst_reg = DST, \
  105. .src_reg = 0, \
  106. .off = 0, \
  107. .imm = IMM })
  108. /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
  109. #define BPF_LD_IMM64(DST, IMM) \
  110. BPF_LD_IMM64_RAW(DST, 0, IMM)
  111. #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
  112. ((struct bpf_insn) { \
  113. .code = BPF_LD | BPF_DW | BPF_IMM, \
  114. .dst_reg = DST, \
  115. .src_reg = SRC, \
  116. .off = 0, \
  117. .imm = (__u32) (IMM) }), \
  118. ((struct bpf_insn) { \
  119. .code = 0, /* zero is reserved opcode */ \
  120. .dst_reg = 0, \
  121. .src_reg = 0, \
  122. .off = 0, \
  123. .imm = ((__u64) (IMM)) >> 32 })
  124. /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
  125. #define BPF_LD_MAP_FD(DST, MAP_FD) \
  126. BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
  127. /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
  128. #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
  129. ((struct bpf_insn) { \
  130. .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
  131. .dst_reg = DST, \
  132. .src_reg = SRC, \
  133. .off = 0, \
  134. .imm = IMM })
  135. #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
  136. ((struct bpf_insn) { \
  137. .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
  138. .dst_reg = DST, \
  139. .src_reg = SRC, \
  140. .off = 0, \
  141. .imm = IMM })
  142. /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
  143. #define BPF_LD_ABS(SIZE, IMM) \
  144. ((struct bpf_insn) { \
  145. .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
  146. .dst_reg = 0, \
  147. .src_reg = 0, \
  148. .off = 0, \
  149. .imm = IMM })
  150. /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
  151. #define BPF_LD_IND(SIZE, SRC, IMM) \
  152. ((struct bpf_insn) { \
  153. .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
  154. .dst_reg = 0, \
  155. .src_reg = SRC, \
  156. .off = 0, \
  157. .imm = IMM })
  158. /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
  159. #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
  160. ((struct bpf_insn) { \
  161. .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
  162. .dst_reg = DST, \
  163. .src_reg = SRC, \
  164. .off = OFF, \
  165. .imm = 0 })
  166. /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
  167. #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
  168. ((struct bpf_insn) { \
  169. .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
  170. .dst_reg = DST, \
  171. .src_reg = SRC, \
  172. .off = OFF, \
  173. .imm = 0 })
  174. /* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
  175. #define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
  176. ((struct bpf_insn) { \
  177. .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
  178. .dst_reg = DST, \
  179. .src_reg = SRC, \
  180. .off = OFF, \
  181. .imm = 0 })
  182. /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
  183. #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
  184. ((struct bpf_insn) { \
  185. .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
  186. .dst_reg = DST, \
  187. .src_reg = 0, \
  188. .off = OFF, \
  189. .imm = IMM })
  190. /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
  191. #define BPF_JMP_REG(OP, DST, SRC, OFF) \
  192. ((struct bpf_insn) { \
  193. .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
  194. .dst_reg = DST, \
  195. .src_reg = SRC, \
  196. .off = OFF, \
  197. .imm = 0 })
  198. /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
  199. #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
  200. ((struct bpf_insn) { \
  201. .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
  202. .dst_reg = DST, \
  203. .src_reg = 0, \
  204. .off = OFF, \
  205. .imm = IMM })
  206. /* Function call */
  207. #define BPF_EMIT_CALL(FUNC) \
  208. ((struct bpf_insn) { \
  209. .code = BPF_JMP | BPF_CALL, \
  210. .dst_reg = 0, \
  211. .src_reg = 0, \
  212. .off = 0, \
  213. .imm = ((FUNC) - __bpf_call_base) })
  214. /* Raw code statement block */
  215. #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
  216. ((struct bpf_insn) { \
  217. .code = CODE, \
  218. .dst_reg = DST, \
  219. .src_reg = SRC, \
  220. .off = OFF, \
  221. .imm = IMM })
  222. /* Program exit */
  223. #define BPF_EXIT_INSN() \
  224. ((struct bpf_insn) { \
  225. .code = BPF_JMP | BPF_EXIT, \
  226. .dst_reg = 0, \
  227. .src_reg = 0, \
  228. .off = 0, \
  229. .imm = 0 })
  230. /* Internal classic blocks for direct assignment */
  231. #define __BPF_STMT(CODE, K) \
  232. ((struct sock_filter) BPF_STMT(CODE, K))
  233. #define __BPF_JUMP(CODE, K, JT, JF) \
  234. ((struct sock_filter) BPF_JUMP(CODE, K, JT, JF))
  235. #define bytes_to_bpf_size(bytes) \
  236. ({ \
  237. int bpf_size = -EINVAL; \
  238. \
  239. if (bytes == sizeof(u8)) \
  240. bpf_size = BPF_B; \
  241. else if (bytes == sizeof(u16)) \
  242. bpf_size = BPF_H; \
  243. else if (bytes == sizeof(u32)) \
  244. bpf_size = BPF_W; \
  245. else if (bytes == sizeof(u64)) \
  246. bpf_size = BPF_DW; \
  247. \
  248. bpf_size; \
  249. })
  250. #ifdef CONFIG_COMPAT
  251. /* A struct sock_filter is architecture independent. */
  252. struct compat_sock_fprog {
  253. u16 len;
  254. compat_uptr_t filter; /* struct sock_filter * */
  255. };
  256. #endif
  257. struct sock_fprog_kern {
  258. u16 len;
  259. struct sock_filter *filter;
  260. };
  261. struct bpf_binary_header {
  262. unsigned int pages;
  263. u8 image[];
  264. };
  265. struct bpf_prog {
  266. u16 pages; /* Number of allocated pages */
  267. kmemcheck_bitfield_begin(meta);
  268. u16 jited:1, /* Is our filter JIT'ed? */
  269. gpl_compatible:1, /* Is filter GPL compatible? */
  270. cb_access:1, /* Is control block accessed? */
  271. dst_needed:1; /* Do we need dst entry? */
  272. kmemcheck_bitfield_end(meta);
  273. u32 len; /* Number of filter blocks */
  274. enum bpf_prog_type type; /* Type of BPF program */
  275. struct bpf_prog_aux *aux; /* Auxiliary fields */
  276. struct sock_fprog_kern *orig_prog; /* Original BPF program */
  277. unsigned int (*bpf_func)(const struct sk_buff *skb,
  278. const struct bpf_insn *filter);
  279. /* Instructions for interpreter */
  280. union {
  281. struct sock_filter insns[0];
  282. struct bpf_insn insnsi[0];
  283. };
  284. };
  285. struct sk_filter {
  286. atomic_t refcnt;
  287. struct rcu_head rcu;
  288. struct bpf_prog *prog;
  289. };
  290. #define BPF_PROG_RUN(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
  291. static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
  292. struct sk_buff *skb)
  293. {
  294. u8 *cb_data = qdisc_skb_cb(skb)->data;
  295. u8 saved_cb[QDISC_CB_PRIV_LEN];
  296. u32 res;
  297. BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) !=
  298. QDISC_CB_PRIV_LEN);
  299. if (unlikely(prog->cb_access)) {
  300. memcpy(saved_cb, cb_data, sizeof(saved_cb));
  301. memset(cb_data, 0, sizeof(saved_cb));
  302. }
  303. res = BPF_PROG_RUN(prog, skb);
  304. if (unlikely(prog->cb_access))
  305. memcpy(cb_data, saved_cb, sizeof(saved_cb));
  306. return res;
  307. }
  308. static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog,
  309. struct sk_buff *skb)
  310. {
  311. u8 *cb_data = qdisc_skb_cb(skb)->data;
  312. if (unlikely(prog->cb_access))
  313. memset(cb_data, 0, QDISC_CB_PRIV_LEN);
  314. return BPF_PROG_RUN(prog, skb);
  315. }
  316. static inline unsigned int bpf_prog_size(unsigned int proglen)
  317. {
  318. return max(sizeof(struct bpf_prog),
  319. offsetof(struct bpf_prog, insns[proglen]));
  320. }
  321. static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
  322. {
  323. /* When classic BPF programs have been loaded and the arch
  324. * does not have a classic BPF JIT (anymore), they have been
  325. * converted via bpf_migrate_filter() to eBPF and thus always
  326. * have an unspec program type.
  327. */
  328. return prog->type == BPF_PROG_TYPE_UNSPEC;
  329. }
  330. #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
  331. #ifdef CONFIG_DEBUG_SET_MODULE_RONX
  332. static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
  333. {
  334. set_memory_ro((unsigned long)fp, fp->pages);
  335. }
  336. static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
  337. {
  338. set_memory_rw((unsigned long)fp, fp->pages);
  339. }
  340. #else
  341. static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
  342. {
  343. }
  344. static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
  345. {
  346. }
  347. #endif /* CONFIG_DEBUG_SET_MODULE_RONX */
  348. int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
  349. static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
  350. {
  351. return sk_filter_trim_cap(sk, skb, 1);
  352. }
  353. int bpf_prog_select_runtime(struct bpf_prog *fp);
  354. void bpf_prog_free(struct bpf_prog *fp);
  355. struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
  356. struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
  357. gfp_t gfp_extra_flags);
  358. void __bpf_prog_free(struct bpf_prog *fp);
  359. static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
  360. {
  361. bpf_prog_unlock_ro(fp);
  362. __bpf_prog_free(fp);
  363. }
  364. typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
  365. unsigned int flen);
  366. int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
  367. int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
  368. bpf_aux_classic_check_t trans, bool save_orig);
  369. void bpf_prog_destroy(struct bpf_prog *fp);
  370. int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  371. int __sk_attach_filter(struct sock_fprog *fprog, struct sock *sk,
  372. bool locked);
  373. int sk_attach_bpf(u32 ufd, struct sock *sk);
  374. int sk_detach_filter(struct sock *sk);
  375. int __sk_detach_filter(struct sock *sk, bool locked);
  376. int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
  377. unsigned int len);
  378. bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
  379. void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
  380. u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  381. void bpf_int_jit_compile(struct bpf_prog *fp);
  382. bool bpf_helper_changes_skb_data(void *func);
  383. struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
  384. const struct bpf_insn *patch, u32 len);
  385. #ifdef CONFIG_BPF_JIT
  386. typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
  387. struct bpf_binary_header *
  388. bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
  389. unsigned int alignment,
  390. bpf_jit_fill_hole_t bpf_fill_ill_insns);
  391. void bpf_jit_binary_free(struct bpf_binary_header *hdr);
  392. void bpf_jit_compile(struct bpf_prog *fp);
  393. void bpf_jit_free(struct bpf_prog *fp);
  394. static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
  395. u32 pass, void *image)
  396. {
  397. pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen,
  398. proglen, pass, image, current->comm, task_pid_nr(current));
  399. if (image)
  400. print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
  401. 16, 1, image, proglen, false);
  402. }
  403. #else
  404. static inline void bpf_jit_compile(struct bpf_prog *fp)
  405. {
  406. }
  407. static inline void bpf_jit_free(struct bpf_prog *fp)
  408. {
  409. bpf_prog_unlock_free(fp);
  410. }
  411. #endif /* CONFIG_BPF_JIT */
  412. #define BPF_ANC BIT(15)
  413. static inline bool bpf_needs_clear_a(const struct sock_filter *first)
  414. {
  415. switch (first->code) {
  416. case BPF_RET | BPF_K:
  417. case BPF_LD | BPF_W | BPF_LEN:
  418. return false;
  419. case BPF_LD | BPF_W | BPF_ABS:
  420. case BPF_LD | BPF_H | BPF_ABS:
  421. case BPF_LD | BPF_B | BPF_ABS:
  422. if (first->k == SKF_AD_OFF + SKF_AD_ALU_XOR_X)
  423. return true;
  424. return false;
  425. default:
  426. return true;
  427. }
  428. }
  429. static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
  430. {
  431. BUG_ON(ftest->code & BPF_ANC);
  432. switch (ftest->code) {
  433. case BPF_LD | BPF_W | BPF_ABS:
  434. case BPF_LD | BPF_H | BPF_ABS:
  435. case BPF_LD | BPF_B | BPF_ABS:
  436. #define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
  437. return BPF_ANC | SKF_AD_##CODE
  438. switch (ftest->k) {
  439. BPF_ANCILLARY(PROTOCOL);
  440. BPF_ANCILLARY(PKTTYPE);
  441. BPF_ANCILLARY(IFINDEX);
  442. BPF_ANCILLARY(NLATTR);
  443. BPF_ANCILLARY(NLATTR_NEST);
  444. BPF_ANCILLARY(MARK);
  445. BPF_ANCILLARY(QUEUE);
  446. BPF_ANCILLARY(HATYPE);
  447. BPF_ANCILLARY(RXHASH);
  448. BPF_ANCILLARY(CPU);
  449. BPF_ANCILLARY(ALU_XOR_X);
  450. BPF_ANCILLARY(VLAN_TAG);
  451. BPF_ANCILLARY(VLAN_TAG_PRESENT);
  452. BPF_ANCILLARY(PAY_OFFSET);
  453. BPF_ANCILLARY(RANDOM);
  454. BPF_ANCILLARY(VLAN_TPID);
  455. }
  456. /* Fallthrough. */
  457. default:
  458. return ftest->code;
  459. }
  460. }
  461. void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
  462. int k, unsigned int size);
  463. static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
  464. unsigned int size, void *buffer)
  465. {
  466. if (k >= 0)
  467. return skb_header_pointer(skb, k, size, buffer);
  468. return bpf_internal_load_pointer_neg_helper(skb, k, size);
  469. }
  470. static inline int bpf_tell_extensions(void)
  471. {
  472. return SKF_AD_MAX;
  473. }
  474. #endif /* __LINUX_FILTER_H__ */