|
1 | 1 | // SPDX-License-Identifier: GPL-2.0
|
2 | 2 | #include <linux/bpf.h>
|
| 3 | +#include <linux/filter.h> |
3 | 4 | #include <linux/netfilter.h>
|
4 | 5 |
|
5 | 6 | #include <net/netfilter/nf_bpf_link.h>
|
|
8 | 9 | static unsigned int nf_hook_run_bpf(void *bpf_prog, struct sk_buff *skb,
|
9 | 10 | const struct nf_hook_state *s)
|
10 | 11 | {
|
11 |
| - return NF_ACCEPT; |
| 12 | + const struct bpf_prog *prog = bpf_prog; |
| 13 | + struct bpf_nf_ctx ctx = { |
| 14 | + .state = s, |
| 15 | + .skb = skb, |
| 16 | + }; |
| 17 | + |
| 18 | + return bpf_prog_run(prog, &ctx); |
12 | 19 | }
|
13 | 20 |
|
14 | 21 | struct bpf_nf_link {
|
@@ -157,3 +164,64 @@ int bpf_nf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
|
157 | 164 |
|
158 | 165 | return bpf_link_settle(&link_primer);
|
159 | 166 | }
|
| 167 | + |
| 168 | +const struct bpf_prog_ops netfilter_prog_ops = { |
| 169 | +}; |
| 170 | + |
| 171 | +static bool nf_ptr_to_btf_id(struct bpf_insn_access_aux *info, const char *name) |
| 172 | +{ |
| 173 | + struct btf *btf; |
| 174 | + s32 type_id; |
| 175 | + |
| 176 | + btf = bpf_get_btf_vmlinux(); |
| 177 | + if (IS_ERR_OR_NULL(btf)) |
| 178 | + return false; |
| 179 | + |
| 180 | + type_id = btf_find_by_name_kind(btf, name, BTF_KIND_STRUCT); |
| 181 | + if (WARN_ON_ONCE(type_id < 0)) |
| 182 | + return false; |
| 183 | + |
| 184 | + info->btf = btf; |
| 185 | + info->btf_id = type_id; |
| 186 | + info->reg_type = PTR_TO_BTF_ID | PTR_TRUSTED; |
| 187 | + return true; |
| 188 | +} |
| 189 | + |
| 190 | +static bool nf_is_valid_access(int off, int size, enum bpf_access_type type, |
| 191 | + const struct bpf_prog *prog, |
| 192 | + struct bpf_insn_access_aux *info) |
| 193 | +{ |
| 194 | + if (off < 0 || off >= sizeof(struct bpf_nf_ctx)) |
| 195 | + return false; |
| 196 | + |
| 197 | + if (type == BPF_WRITE) |
| 198 | + return false; |
| 199 | + |
| 200 | + switch (off) { |
| 201 | + case bpf_ctx_range(struct bpf_nf_ctx, skb): |
| 202 | + if (size != sizeof_field(struct bpf_nf_ctx, skb)) |
| 203 | + return false; |
| 204 | + |
| 205 | + return nf_ptr_to_btf_id(info, "sk_buff"); |
| 206 | + case bpf_ctx_range(struct bpf_nf_ctx, state): |
| 207 | + if (size != sizeof_field(struct bpf_nf_ctx, state)) |
| 208 | + return false; |
| 209 | + |
| 210 | + return nf_ptr_to_btf_id(info, "nf_hook_state"); |
| 211 | + default: |
| 212 | + return false; |
| 213 | + } |
| 214 | + |
| 215 | + return false; |
| 216 | +} |
| 217 | + |
| 218 | +static const struct bpf_func_proto * |
| 219 | +bpf_nf_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 220 | +{ |
| 221 | + return bpf_base_func_proto(func_id); |
| 222 | +} |
| 223 | + |
| 224 | +const struct bpf_verifier_ops netfilter_verifier_ops = { |
| 225 | + .is_valid_access = nf_is_valid_access, |
| 226 | + .get_func_proto = bpf_nf_func_proto, |
| 227 | +}; |
0 commit comments