filter.h 12 KB

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