filter.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Linux Socket Filter Data Structures
  3. */
  4. #ifndef __LINUX_FILTER_H__
  5. #define __LINUX_FILTER_H__
  6. #include <linux/atomic.h>
  7. #include <linux/compat.h>
  8. #include <uapi/linux/filter.h>
  9. #ifdef CONFIG_COMPAT
  10. /*
  11. * A struct sock_filter is architecture independent.
  12. */
  13. struct compat_sock_fprog {
  14. u16 len;
  15. compat_uptr_t filter; /* struct sock_filter * */
  16. };
  17. #endif
  18. struct sk_buff;
  19. struct sock;
  20. struct sk_filter
  21. {
  22. atomic_t refcnt;
  23. unsigned int len; /* Number of filter blocks */
  24. unsigned int (*bpf_func)(const struct sk_buff *skb,
  25. const struct sock_filter *filter);
  26. struct rcu_head rcu;
  27. struct sock_filter insns[0];
  28. };
  29. static inline unsigned int sk_filter_len(const struct sk_filter *fp)
  30. {
  31. return fp->len * sizeof(struct sock_filter) + sizeof(*fp);
  32. }
  33. extern int sk_filter(struct sock *sk, struct sk_buff *skb);
  34. extern unsigned int sk_run_filter(const struct sk_buff *skb,
  35. const struct sock_filter *filter);
  36. extern int sk_unattached_filter_create(struct sk_filter **pfp,
  37. struct sock_fprog *fprog);
  38. extern void sk_unattached_filter_destroy(struct sk_filter *fp);
  39. extern int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  40. extern int sk_detach_filter(struct sock *sk);
  41. extern int sk_chk_filter(struct sock_filter *filter, unsigned int flen);
  42. extern int sk_get_filter(struct sock *sk, struct sock_filter __user *filter, unsigned len);
  43. #ifdef CONFIG_BPF_JIT
  44. #include <linux/linkage.h>
  45. #include <linux/printk.h>
  46. extern void bpf_jit_compile(struct sk_filter *fp);
  47. extern void bpf_jit_free(struct sk_filter *fp);
  48. static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
  49. u32 pass, void *image)
  50. {
  51. pr_err("flen=%u proglen=%u pass=%u image=%p\n",
  52. flen, proglen, pass, image);
  53. if (image)
  54. print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_ADDRESS,
  55. 16, 1, image, proglen, false);
  56. }
  57. #define SK_RUN_FILTER(FILTER, SKB) (*FILTER->bpf_func)(SKB, FILTER->insns)
  58. #else
  59. static inline void bpf_jit_compile(struct sk_filter *fp)
  60. {
  61. }
  62. static inline void bpf_jit_free(struct sk_filter *fp)
  63. {
  64. }
  65. #define SK_RUN_FILTER(FILTER, SKB) sk_run_filter(SKB, FILTER->insns)
  66. #endif
  67. enum {
  68. BPF_S_RET_K = 1,
  69. BPF_S_RET_A,
  70. BPF_S_ALU_ADD_K,
  71. BPF_S_ALU_ADD_X,
  72. BPF_S_ALU_SUB_K,
  73. BPF_S_ALU_SUB_X,
  74. BPF_S_ALU_MUL_K,
  75. BPF_S_ALU_MUL_X,
  76. BPF_S_ALU_DIV_X,
  77. BPF_S_ALU_MOD_K,
  78. BPF_S_ALU_MOD_X,
  79. BPF_S_ALU_AND_K,
  80. BPF_S_ALU_AND_X,
  81. BPF_S_ALU_OR_K,
  82. BPF_S_ALU_OR_X,
  83. BPF_S_ALU_XOR_K,
  84. BPF_S_ALU_XOR_X,
  85. BPF_S_ALU_LSH_K,
  86. BPF_S_ALU_LSH_X,
  87. BPF_S_ALU_RSH_K,
  88. BPF_S_ALU_RSH_X,
  89. BPF_S_ALU_NEG,
  90. BPF_S_LD_W_ABS,
  91. BPF_S_LD_H_ABS,
  92. BPF_S_LD_B_ABS,
  93. BPF_S_LD_W_LEN,
  94. BPF_S_LD_W_IND,
  95. BPF_S_LD_H_IND,
  96. BPF_S_LD_B_IND,
  97. BPF_S_LD_IMM,
  98. BPF_S_LDX_W_LEN,
  99. BPF_S_LDX_B_MSH,
  100. BPF_S_LDX_IMM,
  101. BPF_S_MISC_TAX,
  102. BPF_S_MISC_TXA,
  103. BPF_S_ALU_DIV_K,
  104. BPF_S_LD_MEM,
  105. BPF_S_LDX_MEM,
  106. BPF_S_ST,
  107. BPF_S_STX,
  108. BPF_S_JMP_JA,
  109. BPF_S_JMP_JEQ_K,
  110. BPF_S_JMP_JEQ_X,
  111. BPF_S_JMP_JGE_K,
  112. BPF_S_JMP_JGE_X,
  113. BPF_S_JMP_JGT_K,
  114. BPF_S_JMP_JGT_X,
  115. BPF_S_JMP_JSET_K,
  116. BPF_S_JMP_JSET_X,
  117. /* Ancillary data */
  118. BPF_S_ANC_PROTOCOL,
  119. BPF_S_ANC_PKTTYPE,
  120. BPF_S_ANC_IFINDEX,
  121. BPF_S_ANC_NLATTR,
  122. BPF_S_ANC_NLATTR_NEST,
  123. BPF_S_ANC_MARK,
  124. BPF_S_ANC_QUEUE,
  125. BPF_S_ANC_HATYPE,
  126. BPF_S_ANC_RXHASH,
  127. BPF_S_ANC_CPU,
  128. BPF_S_ANC_ALU_XOR_X,
  129. BPF_S_ANC_SECCOMP_LD_W,
  130. BPF_S_ANC_VLAN_TAG,
  131. BPF_S_ANC_VLAN_TAG_PRESENT,
  132. BPF_S_ANC_PAY_OFFSET,
  133. };
  134. #endif /* __LINUX_FILTER_H__ */