|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +#include <linux/bpf.h> |
| 3 | +#include <linux/netfilter.h> |
| 4 | + |
| 5 | +#include <net/netfilter/nf_bpf_link.h> |
| 6 | +#include <uapi/linux/netfilter_ipv4.h> |
| 7 | + |
| 8 | +static unsigned int nf_hook_run_bpf(void *bpf_prog, struct sk_buff *skb, |
| 9 | + const struct nf_hook_state *s) |
| 10 | +{ |
| 11 | + return NF_ACCEPT; |
| 12 | +} |
| 13 | + |
| 14 | +struct bpf_nf_link { |
| 15 | + struct bpf_link link; |
| 16 | + struct nf_hook_ops hook_ops; |
| 17 | + struct net *net; |
| 18 | + u32 dead; |
| 19 | +}; |
| 20 | + |
| 21 | +static void bpf_nf_link_release(struct bpf_link *link) |
| 22 | +{ |
| 23 | + struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link); |
| 24 | + |
| 25 | + if (nf_link->dead) |
| 26 | + return; |
| 27 | + |
| 28 | + /* prevent hook-not-found warning splat from netfilter core when |
| 29 | + * .detach was already called |
| 30 | + */ |
| 31 | + if (!cmpxchg(&nf_link->dead, 0, 1)) |
| 32 | + nf_unregister_net_hook(nf_link->net, &nf_link->hook_ops); |
| 33 | +} |
| 34 | + |
| 35 | +static void bpf_nf_link_dealloc(struct bpf_link *link) |
| 36 | +{ |
| 37 | + struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link); |
| 38 | + |
| 39 | + kfree(nf_link); |
| 40 | +} |
| 41 | + |
| 42 | +static int bpf_nf_link_detach(struct bpf_link *link) |
| 43 | +{ |
| 44 | + bpf_nf_link_release(link); |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +static void bpf_nf_link_show_info(const struct bpf_link *link, |
| 49 | + struct seq_file *seq) |
| 50 | +{ |
| 51 | + struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link); |
| 52 | + |
| 53 | + seq_printf(seq, "pf:\t%u\thooknum:\t%u\tprio:\t%d\n", |
| 54 | + nf_link->hook_ops.pf, nf_link->hook_ops.hooknum, |
| 55 | + nf_link->hook_ops.priority); |
| 56 | +} |
| 57 | + |
| 58 | +static int bpf_nf_link_fill_link_info(const struct bpf_link *link, |
| 59 | + struct bpf_link_info *info) |
| 60 | +{ |
| 61 | + struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link); |
| 62 | + |
| 63 | + info->netfilter.pf = nf_link->hook_ops.pf; |
| 64 | + info->netfilter.hooknum = nf_link->hook_ops.hooknum; |
| 65 | + info->netfilter.priority = nf_link->hook_ops.priority; |
| 66 | + info->netfilter.flags = 0; |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +static int bpf_nf_link_update(struct bpf_link *link, struct bpf_prog *new_prog, |
| 72 | + struct bpf_prog *old_prog) |
| 73 | +{ |
| 74 | + return -EOPNOTSUPP; |
| 75 | +} |
| 76 | + |
| 77 | +static const struct bpf_link_ops bpf_nf_link_lops = { |
| 78 | + .release = bpf_nf_link_release, |
| 79 | + .dealloc = bpf_nf_link_dealloc, |
| 80 | + .detach = bpf_nf_link_detach, |
| 81 | + .show_fdinfo = bpf_nf_link_show_info, |
| 82 | + .fill_link_info = bpf_nf_link_fill_link_info, |
| 83 | + .update_prog = bpf_nf_link_update, |
| 84 | +}; |
| 85 | + |
| 86 | +static int bpf_nf_check_pf_and_hooks(const union bpf_attr *attr) |
| 87 | +{ |
| 88 | + switch (attr->link_create.netfilter.pf) { |
| 89 | + case NFPROTO_IPV4: |
| 90 | + case NFPROTO_IPV6: |
| 91 | + if (attr->link_create.netfilter.hooknum >= NF_INET_NUMHOOKS) |
| 92 | + return -EPROTO; |
| 93 | + break; |
| 94 | + default: |
| 95 | + return -EAFNOSUPPORT; |
| 96 | + } |
| 97 | + |
| 98 | + if (attr->link_create.netfilter.flags) |
| 99 | + return -EOPNOTSUPP; |
| 100 | + |
| 101 | + /* make sure conntrack confirm is always last. |
| 102 | + * |
| 103 | + * In the future, if userspace can e.g. request defrag, then |
| 104 | + * "defrag_requested && prio before NF_IP_PRI_CONNTRACK_DEFRAG" |
| 105 | + * should fail. |
| 106 | + */ |
| 107 | + switch (attr->link_create.netfilter.priority) { |
| 108 | + case NF_IP_PRI_FIRST: return -ERANGE; /* sabotage_in and other warts */ |
| 109 | + case NF_IP_PRI_LAST: return -ERANGE; /* e.g. conntrack confirm */ |
| 110 | + } |
| 111 | + |
| 112 | + return 0; |
| 113 | +} |
| 114 | + |
| 115 | +int bpf_nf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) |
| 116 | +{ |
| 117 | + struct net *net = current->nsproxy->net_ns; |
| 118 | + struct bpf_link_primer link_primer; |
| 119 | + struct bpf_nf_link *link; |
| 120 | + int err; |
| 121 | + |
| 122 | + if (attr->link_create.flags) |
| 123 | + return -EINVAL; |
| 124 | + |
| 125 | + err = bpf_nf_check_pf_and_hooks(attr); |
| 126 | + if (err) |
| 127 | + return err; |
| 128 | + |
| 129 | + link = kzalloc(sizeof(*link), GFP_USER); |
| 130 | + if (!link) |
| 131 | + return -ENOMEM; |
| 132 | + |
| 133 | + bpf_link_init(&link->link, BPF_LINK_TYPE_NETFILTER, &bpf_nf_link_lops, prog); |
| 134 | + |
| 135 | + link->hook_ops.hook = nf_hook_run_bpf; |
| 136 | + link->hook_ops.hook_ops_type = NF_HOOK_OP_BPF; |
| 137 | + link->hook_ops.priv = prog; |
| 138 | + |
| 139 | + link->hook_ops.pf = attr->link_create.netfilter.pf; |
| 140 | + link->hook_ops.priority = attr->link_create.netfilter.priority; |
| 141 | + link->hook_ops.hooknum = attr->link_create.netfilter.hooknum; |
| 142 | + |
| 143 | + link->net = net; |
| 144 | + link->dead = false; |
| 145 | + |
| 146 | + err = bpf_link_prime(&link->link, &link_primer); |
| 147 | + if (err) { |
| 148 | + kfree(link); |
| 149 | + return err; |
| 150 | + } |
| 151 | + |
| 152 | + err = nf_register_net_hook(net, &link->hook_ops); |
| 153 | + if (err) { |
| 154 | + bpf_link_cleanup(&link_primer); |
| 155 | + return err; |
| 156 | + } |
| 157 | + |
| 158 | + return bpf_link_settle(&link_primer); |
| 159 | +} |
0 commit comments