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