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