filter.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Linux Socket Filter Data Structures
  3. */
  4. #ifndef __LINUX_FILTER_H__
  5. #define __LINUX_FILTER_H__
  6. #include <linux/compiler.h>
  7. #include <linux/types.h>
  8. #ifdef __KERNEL__
  9. #include <linux/atomic.h>
  10. #include <linux/compat.h>
  11. #endif
  12. /*
  13. * Current version of the filter code architecture.
  14. */
  15. #define BPF_MAJOR_VERSION 1
  16. #define BPF_MINOR_VERSION 1
  17. /*
  18. * Try and keep these values and structures similar to BSD, especially
  19. * the BPF code definitions which need to match so you can share filters
  20. */
  21. struct sock_filter { /* Filter block */
  22. __u16 code; /* Actual filter code */
  23. __u8 jt; /* Jump true */
  24. __u8 jf; /* Jump false */
  25. __u32 k; /* Generic multiuse field */
  26. };
  27. struct sock_fprog { /* Required for SO_ATTACH_FILTER. */
  28. unsigned short len; /* Number of filter blocks */
  29. struct sock_filter __user *filter;
  30. };
  31. /*
  32. * Instruction classes
  33. */
  34. #define BPF_CLASS(code) ((code) & 0x07)
  35. #define BPF_LD 0x00
  36. #define BPF_LDX 0x01
  37. #define BPF_ST 0x02
  38. #define BPF_STX 0x03
  39. #define BPF_ALU 0x04
  40. #define BPF_JMP 0x05
  41. #define BPF_RET 0x06
  42. #define BPF_MISC 0x07
  43. /* ld/ldx fields */
  44. #define BPF_SIZE(code) ((code) & 0x18)
  45. #define BPF_W 0x00
  46. #define BPF_H 0x08
  47. #define BPF_B 0x10
  48. #define BPF_MODE(code) ((code) & 0xe0)
  49. #define BPF_IMM 0x00
  50. #define BPF_ABS 0x20
  51. #define BPF_IND 0x40
  52. #define BPF_MEM 0x60
  53. #define BPF_LEN 0x80
  54. #define BPF_MSH 0xa0
  55. /* alu/jmp fields */
  56. #define BPF_OP(code) ((code) & 0xf0)
  57. #define BPF_ADD 0x00
  58. #define BPF_SUB 0x10
  59. #define BPF_MUL 0x20
  60. #define BPF_DIV 0x30
  61. #define BPF_OR 0x40
  62. #define BPF_AND 0x50
  63. #define BPF_LSH 0x60
  64. #define BPF_RSH 0x70
  65. #define BPF_NEG 0x80
  66. #define BPF_JA 0x00
  67. #define BPF_JEQ 0x10
  68. #define BPF_JGT 0x20
  69. #define BPF_JGE 0x30
  70. #define BPF_JSET 0x40
  71. #define BPF_SRC(code) ((code) & 0x08)
  72. #define BPF_K 0x00
  73. #define BPF_X 0x08
  74. /* ret - BPF_K and BPF_X also apply */
  75. #define BPF_RVAL(code) ((code) & 0x18)
  76. #define BPF_A 0x10
  77. /* misc */
  78. #define BPF_MISCOP(code) ((code) & 0xf8)
  79. #define BPF_TAX 0x00
  80. #define BPF_TXA 0x80
  81. #ifndef BPF_MAXINSNS
  82. #define BPF_MAXINSNS 4096
  83. #endif
  84. /*
  85. * Macros for filter block array initializers.
  86. */
  87. #ifndef BPF_STMT
  88. #define BPF_STMT(code, k) { (unsigned short)(code), 0, 0, k }
  89. #endif
  90. #ifndef BPF_JUMP
  91. #define BPF_JUMP(code, k, jt, jf) { (unsigned short)(code), jt, jf, k }
  92. #endif
  93. /*
  94. * Number of scratch memory words for: BPF_ST and BPF_STX
  95. */
  96. #define BPF_MEMWORDS 16
  97. /* RATIONALE. Negative offsets are invalid in BPF.
  98. We use them to reference ancillary data.
  99. Unlike introduction new instructions, it does not break
  100. existing compilers/optimizers.
  101. */
  102. #define SKF_AD_OFF (-0x1000)
  103. #define SKF_AD_PROTOCOL 0
  104. #define SKF_AD_PKTTYPE 4
  105. #define SKF_AD_IFINDEX 8
  106. #define SKF_AD_NLATTR 12
  107. #define SKF_AD_NLATTR_NEST 16
  108. #define SKF_AD_MARK 20
  109. #define SKF_AD_QUEUE 24
  110. #define SKF_AD_HATYPE 28
  111. #define SKF_AD_RXHASH 32
  112. #define SKF_AD_CPU 36
  113. #define SKF_AD_ALU_XOR_X 40
  114. #define SKF_AD_MAX 44
  115. #define SKF_NET_OFF (-0x100000)
  116. #define SKF_LL_OFF (-0x200000)
  117. #ifdef __KERNEL__
  118. #ifdef CONFIG_COMPAT
  119. /*
  120. * A struct sock_filter is architecture independent.
  121. */
  122. struct compat_sock_fprog {
  123. u16 len;
  124. compat_uptr_t filter; /* struct sock_filter * */
  125. };
  126. #endif
  127. struct sk_buff;
  128. struct sock;
  129. struct sk_filter
  130. {
  131. atomic_t refcnt;
  132. unsigned int len; /* Number of filter blocks */
  133. unsigned int (*bpf_func)(const struct sk_buff *skb,
  134. const struct sock_filter *filter);
  135. struct rcu_head rcu;
  136. struct sock_filter insns[0];
  137. };
  138. static inline unsigned int sk_filter_len(const struct sk_filter *fp)
  139. {
  140. return fp->len * sizeof(struct sock_filter) + sizeof(*fp);
  141. }
  142. extern int sk_filter(struct sock *sk, struct sk_buff *skb);
  143. extern unsigned int sk_run_filter(const struct sk_buff *skb,
  144. const struct sock_filter *filter);
  145. extern int sk_unattached_filter_create(struct sk_filter **pfp,
  146. struct sock_fprog *fprog);
  147. extern void sk_unattached_filter_destroy(struct sk_filter *fp);
  148. extern int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  149. extern int sk_detach_filter(struct sock *sk);
  150. extern int sk_chk_filter(struct sock_filter *filter, unsigned int flen);
  151. #ifdef CONFIG_BPF_JIT
  152. extern void bpf_jit_compile(struct sk_filter *fp);
  153. extern void bpf_jit_free(struct sk_filter *fp);
  154. #define SK_RUN_FILTER(FILTER, SKB) (*FILTER->bpf_func)(SKB, FILTER->insns)
  155. #else
  156. static inline void bpf_jit_compile(struct sk_filter *fp)
  157. {
  158. }
  159. static inline void bpf_jit_free(struct sk_filter *fp)
  160. {
  161. }
  162. #define SK_RUN_FILTER(FILTER, SKB) sk_run_filter(SKB, FILTER->insns)
  163. #endif
  164. enum {
  165. BPF_S_RET_K = 1,
  166. BPF_S_RET_A,
  167. BPF_S_ALU_ADD_K,
  168. BPF_S_ALU_ADD_X,
  169. BPF_S_ALU_SUB_K,
  170. BPF_S_ALU_SUB_X,
  171. BPF_S_ALU_MUL_K,
  172. BPF_S_ALU_MUL_X,
  173. BPF_S_ALU_DIV_X,
  174. BPF_S_ALU_AND_K,
  175. BPF_S_ALU_AND_X,
  176. BPF_S_ALU_OR_K,
  177. BPF_S_ALU_OR_X,
  178. BPF_S_ALU_LSH_K,
  179. BPF_S_ALU_LSH_X,
  180. BPF_S_ALU_RSH_K,
  181. BPF_S_ALU_RSH_X,
  182. BPF_S_ALU_NEG,
  183. BPF_S_LD_W_ABS,
  184. BPF_S_LD_H_ABS,
  185. BPF_S_LD_B_ABS,
  186. BPF_S_LD_W_LEN,
  187. BPF_S_LD_W_IND,
  188. BPF_S_LD_H_IND,
  189. BPF_S_LD_B_IND,
  190. BPF_S_LD_IMM,
  191. BPF_S_LDX_W_LEN,
  192. BPF_S_LDX_B_MSH,
  193. BPF_S_LDX_IMM,
  194. BPF_S_MISC_TAX,
  195. BPF_S_MISC_TXA,
  196. BPF_S_ALU_DIV_K,
  197. BPF_S_LD_MEM,
  198. BPF_S_LDX_MEM,
  199. BPF_S_ST,
  200. BPF_S_STX,
  201. BPF_S_JMP_JA,
  202. BPF_S_JMP_JEQ_K,
  203. BPF_S_JMP_JEQ_X,
  204. BPF_S_JMP_JGE_K,
  205. BPF_S_JMP_JGE_X,
  206. BPF_S_JMP_JGT_K,
  207. BPF_S_JMP_JGT_X,
  208. BPF_S_JMP_JSET_K,
  209. BPF_S_JMP_JSET_X,
  210. /* Ancillary data */
  211. BPF_S_ANC_PROTOCOL,
  212. BPF_S_ANC_PKTTYPE,
  213. BPF_S_ANC_IFINDEX,
  214. BPF_S_ANC_NLATTR,
  215. BPF_S_ANC_NLATTR_NEST,
  216. BPF_S_ANC_MARK,
  217. BPF_S_ANC_QUEUE,
  218. BPF_S_ANC_HATYPE,
  219. BPF_S_ANC_RXHASH,
  220. BPF_S_ANC_CPU,
  221. BPF_S_ANC_ALU_XOR_X,
  222. BPF_S_ANC_SECCOMP_LD_W,
  223. };
  224. #endif /* __KERNEL__ */
  225. #endif /* __LINUX_FILTER_H__ */