Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | #include <linux/bpf.h> |
Jakub Kicinski | 3b80b73 | 2021-12-29 17:27:41 -0800 | [diff] [blame] | 4 | #include <linux/bpf-netns.h> |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 5 | #include <linux/filter.h> |
| 6 | #include <net/net_namespace.h> |
| 7 | |
| 8 | /* |
| 9 | * Functions to manage BPF programs attached to netns |
| 10 | */ |
| 11 | |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 12 | struct bpf_netns_link { |
| 13 | struct bpf_link link; |
| 14 | enum bpf_attach_type type; |
| 15 | enum netns_bpf_attach_type netns_type; |
| 16 | |
| 17 | /* We don't hold a ref to net in order to auto-detach the link |
| 18 | * when netns is going away. Instead we rely on pernet |
| 19 | * pre_exit callback to clear this pointer. Must be accessed |
| 20 | * with netns_bpf_mutex held. |
| 21 | */ |
| 22 | struct net *net; |
Jakub Sitnicki | ab53cad | 2020-06-25 16:13:56 +0200 | [diff] [blame] | 23 | struct list_head node; /* node in list of links attached to net */ |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 24 | }; |
| 25 | |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 26 | /* Protects updates to netns_bpf */ |
| 27 | DEFINE_MUTEX(netns_bpf_mutex); |
| 28 | |
Jakub Sitnicki | 1559b4a | 2020-07-17 12:35:25 +0200 | [diff] [blame] | 29 | static void netns_bpf_attach_type_unneed(enum netns_bpf_attach_type type) |
| 30 | { |
| 31 | switch (type) { |
Jakub Sitnicki | 343ead2 | 2020-07-21 12:07:16 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_INET |
Jakub Sitnicki | 1559b4a | 2020-07-17 12:35:25 +0200 | [diff] [blame] | 33 | case NETNS_BPF_SK_LOOKUP: |
| 34 | static_branch_dec(&bpf_sk_lookup_enabled); |
| 35 | break; |
Jakub Sitnicki | 343ead2 | 2020-07-21 12:07:16 +0200 | [diff] [blame] | 36 | #endif |
Jakub Sitnicki | 1559b4a | 2020-07-17 12:35:25 +0200 | [diff] [blame] | 37 | default: |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | static void netns_bpf_attach_type_need(enum netns_bpf_attach_type type) |
| 43 | { |
| 44 | switch (type) { |
Jakub Sitnicki | 343ead2 | 2020-07-21 12:07:16 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_INET |
Jakub Sitnicki | 1559b4a | 2020-07-17 12:35:25 +0200 | [diff] [blame] | 46 | case NETNS_BPF_SK_LOOKUP: |
| 47 | static_branch_inc(&bpf_sk_lookup_enabled); |
| 48 | break; |
Jakub Sitnicki | 343ead2 | 2020-07-21 12:07:16 +0200 | [diff] [blame] | 49 | #endif |
Jakub Sitnicki | 1559b4a | 2020-07-17 12:35:25 +0200 | [diff] [blame] | 50 | default: |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 55 | /* Must be called with netns_bpf_mutex held. */ |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 56 | static void netns_bpf_run_array_detach(struct net *net, |
| 57 | enum netns_bpf_attach_type type) |
| 58 | { |
| 59 | struct bpf_prog_array *run_array; |
| 60 | |
| 61 | run_array = rcu_replace_pointer(net->bpf.run_array[type], NULL, |
| 62 | lockdep_is_held(&netns_bpf_mutex)); |
| 63 | bpf_prog_array_free(run_array); |
| 64 | } |
| 65 | |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 66 | static int link_index(struct net *net, enum netns_bpf_attach_type type, |
| 67 | struct bpf_netns_link *link) |
| 68 | { |
| 69 | struct bpf_netns_link *pos; |
| 70 | int i = 0; |
| 71 | |
| 72 | list_for_each_entry(pos, &net->bpf.links[type], node) { |
| 73 | if (pos == link) |
| 74 | return i; |
| 75 | i++; |
| 76 | } |
| 77 | return -ENOENT; |
| 78 | } |
| 79 | |
| 80 | static int link_count(struct net *net, enum netns_bpf_attach_type type) |
| 81 | { |
| 82 | struct list_head *pos; |
| 83 | int i = 0; |
| 84 | |
| 85 | list_for_each(pos, &net->bpf.links[type]) |
| 86 | i++; |
| 87 | return i; |
| 88 | } |
| 89 | |
| 90 | static void fill_prog_array(struct net *net, enum netns_bpf_attach_type type, |
| 91 | struct bpf_prog_array *prog_array) |
| 92 | { |
| 93 | struct bpf_netns_link *pos; |
| 94 | unsigned int i = 0; |
| 95 | |
| 96 | list_for_each_entry(pos, &net->bpf.links[type], node) { |
| 97 | prog_array->items[i].prog = pos->link.prog; |
| 98 | i++; |
| 99 | } |
| 100 | } |
| 101 | |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 102 | static void bpf_netns_link_release(struct bpf_link *link) |
| 103 | { |
| 104 | struct bpf_netns_link *net_link = |
| 105 | container_of(link, struct bpf_netns_link, link); |
| 106 | enum netns_bpf_attach_type type = net_link->netns_type; |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 107 | struct bpf_prog_array *old_array, *new_array; |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 108 | struct net *net; |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 109 | int cnt, idx; |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 110 | |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 111 | mutex_lock(&netns_bpf_mutex); |
| 112 | |
Jakub Sitnicki | 2576f87 | 2020-06-30 18:45:41 +0200 | [diff] [blame] | 113 | /* We can race with cleanup_net, but if we see a non-NULL |
| 114 | * struct net pointer, pre_exit has not run yet and wait for |
| 115 | * netns_bpf_mutex. |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 116 | */ |
| 117 | net = net_link->net; |
| 118 | if (!net) |
| 119 | goto out_unlock; |
| 120 | |
Jakub Sitnicki | 1559b4a | 2020-07-17 12:35:25 +0200 | [diff] [blame] | 121 | /* Mark attach point as unused */ |
| 122 | netns_bpf_attach_type_unneed(type); |
| 123 | |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 124 | /* Remember link position in case of safe delete */ |
| 125 | idx = link_index(net, type, net_link); |
Jakub Sitnicki | ab53cad | 2020-06-25 16:13:56 +0200 | [diff] [blame] | 126 | list_del(&net_link->node); |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 127 | |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 128 | cnt = link_count(net, type); |
| 129 | if (!cnt) { |
| 130 | netns_bpf_run_array_detach(net, type); |
| 131 | goto out_unlock; |
| 132 | } |
| 133 | |
| 134 | old_array = rcu_dereference_protected(net->bpf.run_array[type], |
| 135 | lockdep_is_held(&netns_bpf_mutex)); |
| 136 | new_array = bpf_prog_array_alloc(cnt, GFP_KERNEL); |
| 137 | if (!new_array) { |
| 138 | WARN_ON(bpf_prog_array_delete_safe_at(old_array, idx)); |
| 139 | goto out_unlock; |
| 140 | } |
| 141 | fill_prog_array(net, type, new_array); |
| 142 | rcu_assign_pointer(net->bpf.run_array[type], new_array); |
| 143 | bpf_prog_array_free(old_array); |
| 144 | |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 145 | out_unlock: |
Andrii Nakryiko | 73b11c2a | 2020-07-31 11:28:26 -0700 | [diff] [blame] | 146 | net_link->net = NULL; |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 147 | mutex_unlock(&netns_bpf_mutex); |
| 148 | } |
| 149 | |
Andrii Nakryiko | 73b11c2a | 2020-07-31 11:28:26 -0700 | [diff] [blame] | 150 | static int bpf_netns_link_detach(struct bpf_link *link) |
| 151 | { |
| 152 | bpf_netns_link_release(link); |
| 153 | return 0; |
| 154 | } |
| 155 | |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 156 | static void bpf_netns_link_dealloc(struct bpf_link *link) |
| 157 | { |
| 158 | struct bpf_netns_link *net_link = |
| 159 | container_of(link, struct bpf_netns_link, link); |
| 160 | |
| 161 | kfree(net_link); |
| 162 | } |
| 163 | |
| 164 | static int bpf_netns_link_update_prog(struct bpf_link *link, |
| 165 | struct bpf_prog *new_prog, |
| 166 | struct bpf_prog *old_prog) |
| 167 | { |
| 168 | struct bpf_netns_link *net_link = |
| 169 | container_of(link, struct bpf_netns_link, link); |
| 170 | enum netns_bpf_attach_type type = net_link->netns_type; |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 171 | struct bpf_prog_array *run_array; |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 172 | struct net *net; |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 173 | int idx, ret; |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 174 | |
| 175 | if (old_prog && old_prog != link->prog) |
| 176 | return -EPERM; |
| 177 | if (new_prog->type != link->prog->type) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | mutex_lock(&netns_bpf_mutex); |
| 181 | |
| 182 | net = net_link->net; |
| 183 | if (!net || !check_net(net)) { |
| 184 | /* Link auto-detached or netns dying */ |
| 185 | ret = -ENOLINK; |
| 186 | goto out_unlock; |
| 187 | } |
| 188 | |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 189 | run_array = rcu_dereference_protected(net->bpf.run_array[type], |
| 190 | lockdep_is_held(&netns_bpf_mutex)); |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 191 | idx = link_index(net, type, net_link); |
| 192 | ret = bpf_prog_array_update_at(run_array, idx, new_prog); |
| 193 | if (ret) |
| 194 | goto out_unlock; |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 195 | |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 196 | old_prog = xchg(&link->prog, new_prog); |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 197 | bpf_prog_put(old_prog); |
| 198 | |
| 199 | out_unlock: |
| 200 | mutex_unlock(&netns_bpf_mutex); |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | static int bpf_netns_link_fill_info(const struct bpf_link *link, |
| 205 | struct bpf_link_info *info) |
| 206 | { |
| 207 | const struct bpf_netns_link *net_link = |
| 208 | container_of(link, struct bpf_netns_link, link); |
| 209 | unsigned int inum = 0; |
| 210 | struct net *net; |
| 211 | |
| 212 | mutex_lock(&netns_bpf_mutex); |
| 213 | net = net_link->net; |
| 214 | if (net && check_net(net)) |
| 215 | inum = net->ns.inum; |
| 216 | mutex_unlock(&netns_bpf_mutex); |
| 217 | |
| 218 | info->netns.netns_ino = inum; |
| 219 | info->netns.attach_type = net_link->type; |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static void bpf_netns_link_show_fdinfo(const struct bpf_link *link, |
| 224 | struct seq_file *seq) |
| 225 | { |
| 226 | struct bpf_link_info info = {}; |
| 227 | |
| 228 | bpf_netns_link_fill_info(link, &info); |
| 229 | seq_printf(seq, |
| 230 | "netns_ino:\t%u\n" |
| 231 | "attach_type:\t%u\n", |
| 232 | info.netns.netns_ino, |
| 233 | info.netns.attach_type); |
| 234 | } |
| 235 | |
| 236 | static const struct bpf_link_ops bpf_netns_link_ops = { |
| 237 | .release = bpf_netns_link_release, |
| 238 | .dealloc = bpf_netns_link_dealloc, |
Andrii Nakryiko | 73b11c2a | 2020-07-31 11:28:26 -0700 | [diff] [blame] | 239 | .detach = bpf_netns_link_detach, |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 240 | .update_prog = bpf_netns_link_update_prog, |
| 241 | .fill_link_info = bpf_netns_link_fill_info, |
| 242 | .show_fdinfo = bpf_netns_link_show_fdinfo, |
| 243 | }; |
| 244 | |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 245 | /* Must be called with netns_bpf_mutex held. */ |
| 246 | static int __netns_bpf_prog_query(const union bpf_attr *attr, |
| 247 | union bpf_attr __user *uattr, |
| 248 | struct net *net, |
| 249 | enum netns_bpf_attach_type type) |
| 250 | { |
| 251 | __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids); |
| 252 | struct bpf_prog_array *run_array; |
| 253 | u32 prog_cnt = 0, flags = 0; |
| 254 | |
| 255 | run_array = rcu_dereference_protected(net->bpf.run_array[type], |
| 256 | lockdep_is_held(&netns_bpf_mutex)); |
| 257 | if (run_array) |
| 258 | prog_cnt = bpf_prog_array_length(run_array); |
| 259 | |
| 260 | if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags))) |
| 261 | return -EFAULT; |
| 262 | if (copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt))) |
| 263 | return -EFAULT; |
| 264 | if (!attr->query.prog_cnt || !prog_ids || !prog_cnt) |
| 265 | return 0; |
| 266 | |
| 267 | return bpf_prog_array_copy_to_user(run_array, prog_ids, |
| 268 | attr->query.prog_cnt); |
| 269 | } |
| 270 | |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 271 | int netns_bpf_prog_query(const union bpf_attr *attr, |
| 272 | union bpf_attr __user *uattr) |
| 273 | { |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 274 | enum netns_bpf_attach_type type; |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 275 | struct net *net; |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 276 | int ret; |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 277 | |
| 278 | if (attr->query.query_flags) |
| 279 | return -EINVAL; |
| 280 | |
| 281 | type = to_netns_bpf_attach_type(attr->query.attach_type); |
| 282 | if (type < 0) |
| 283 | return -EINVAL; |
| 284 | |
| 285 | net = get_net_ns_by_fd(attr->query.target_fd); |
| 286 | if (IS_ERR(net)) |
| 287 | return PTR_ERR(net); |
| 288 | |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 289 | mutex_lock(&netns_bpf_mutex); |
| 290 | ret = __netns_bpf_prog_query(attr, uattr, net, type); |
| 291 | mutex_unlock(&netns_bpf_mutex); |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 292 | |
| 293 | put_net(net); |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 294 | return ret; |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog) |
| 298 | { |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 299 | struct bpf_prog_array *run_array; |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 300 | enum netns_bpf_attach_type type; |
Jakub Sitnicki | 3b70169 | 2020-06-25 16:13:54 +0200 | [diff] [blame] | 301 | struct bpf_prog *attached; |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 302 | struct net *net; |
| 303 | int ret; |
| 304 | |
Lorenz Bauer | 1b51423 | 2020-06-29 10:56:25 +0100 | [diff] [blame] | 305 | if (attr->target_fd || attr->attach_flags || attr->replace_bpf_fd) |
| 306 | return -EINVAL; |
| 307 | |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 308 | type = to_netns_bpf_attach_type(attr->attach_type); |
| 309 | if (type < 0) |
| 310 | return -EINVAL; |
| 311 | |
| 312 | net = current->nsproxy->net_ns; |
| 313 | mutex_lock(&netns_bpf_mutex); |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 314 | |
| 315 | /* Attaching prog directly is not compatible with links */ |
Jakub Sitnicki | ab53cad | 2020-06-25 16:13:56 +0200 | [diff] [blame] | 316 | if (!list_empty(&net->bpf.links[type])) { |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 317 | ret = -EEXIST; |
| 318 | goto out_unlock; |
| 319 | } |
| 320 | |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 321 | switch (type) { |
| 322 | case NETNS_BPF_FLOW_DISSECTOR: |
Jakub Sitnicki | 3b70169 | 2020-06-25 16:13:54 +0200 | [diff] [blame] | 323 | ret = flow_dissector_bpf_prog_attach_check(net, prog); |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 324 | break; |
| 325 | default: |
| 326 | ret = -EINVAL; |
| 327 | break; |
| 328 | } |
Jakub Sitnicki | 3b70169 | 2020-06-25 16:13:54 +0200 | [diff] [blame] | 329 | if (ret) |
| 330 | goto out_unlock; |
| 331 | |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 332 | attached = net->bpf.progs[type]; |
Jakub Sitnicki | 3b70169 | 2020-06-25 16:13:54 +0200 | [diff] [blame] | 333 | if (attached == prog) { |
| 334 | /* The same program cannot be attached twice */ |
| 335 | ret = -EINVAL; |
| 336 | goto out_unlock; |
| 337 | } |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 338 | |
| 339 | run_array = rcu_dereference_protected(net->bpf.run_array[type], |
| 340 | lockdep_is_held(&netns_bpf_mutex)); |
| 341 | if (run_array) { |
| 342 | WRITE_ONCE(run_array->items[0].prog, prog); |
| 343 | } else { |
| 344 | run_array = bpf_prog_array_alloc(1, GFP_KERNEL); |
| 345 | if (!run_array) { |
| 346 | ret = -ENOMEM; |
| 347 | goto out_unlock; |
| 348 | } |
| 349 | run_array->items[0].prog = prog; |
| 350 | rcu_assign_pointer(net->bpf.run_array[type], run_array); |
| 351 | } |
| 352 | |
| 353 | net->bpf.progs[type] = prog; |
Jakub Sitnicki | 3b70169 | 2020-06-25 16:13:54 +0200 | [diff] [blame] | 354 | if (attached) |
| 355 | bpf_prog_put(attached); |
| 356 | |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 357 | out_unlock: |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 358 | mutex_unlock(&netns_bpf_mutex); |
| 359 | |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | /* Must be called with netns_bpf_mutex held. */ |
| 364 | static int __netns_bpf_prog_detach(struct net *net, |
Lorenz Bauer | 4ac2add | 2020-06-29 10:56:26 +0100 | [diff] [blame] | 365 | enum netns_bpf_attach_type type, |
| 366 | struct bpf_prog *old) |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 367 | { |
| 368 | struct bpf_prog *attached; |
| 369 | |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 370 | /* Progs attached via links cannot be detached */ |
Jakub Sitnicki | ab53cad | 2020-06-25 16:13:56 +0200 | [diff] [blame] | 371 | if (!list_empty(&net->bpf.links[type])) |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 372 | return -EINVAL; |
| 373 | |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 374 | attached = net->bpf.progs[type]; |
Lorenz Bauer | 4ac2add | 2020-06-29 10:56:26 +0100 | [diff] [blame] | 375 | if (!attached || attached != old) |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 376 | return -ENOENT; |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 377 | netns_bpf_run_array_detach(net, type); |
| 378 | net->bpf.progs[type] = NULL; |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 379 | bpf_prog_put(attached); |
| 380 | return 0; |
| 381 | } |
| 382 | |
Lorenz Bauer | 4ac2add | 2020-06-29 10:56:26 +0100 | [diff] [blame] | 383 | int netns_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype) |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 384 | { |
| 385 | enum netns_bpf_attach_type type; |
Lorenz Bauer | 4ac2add | 2020-06-29 10:56:26 +0100 | [diff] [blame] | 386 | struct bpf_prog *prog; |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 387 | int ret; |
| 388 | |
Lorenz Bauer | 4ac2add | 2020-06-29 10:56:26 +0100 | [diff] [blame] | 389 | if (attr->target_fd) |
| 390 | return -EINVAL; |
| 391 | |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 392 | type = to_netns_bpf_attach_type(attr->attach_type); |
| 393 | if (type < 0) |
| 394 | return -EINVAL; |
| 395 | |
Lorenz Bauer | 4ac2add | 2020-06-29 10:56:26 +0100 | [diff] [blame] | 396 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 397 | if (IS_ERR(prog)) |
| 398 | return PTR_ERR(prog); |
| 399 | |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 400 | mutex_lock(&netns_bpf_mutex); |
Lorenz Bauer | 4ac2add | 2020-06-29 10:56:26 +0100 | [diff] [blame] | 401 | ret = __netns_bpf_prog_detach(current->nsproxy->net_ns, type, prog); |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 402 | mutex_unlock(&netns_bpf_mutex); |
| 403 | |
Lorenz Bauer | 4ac2add | 2020-06-29 10:56:26 +0100 | [diff] [blame] | 404 | bpf_prog_put(prog); |
| 405 | |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 406 | return ret; |
| 407 | } |
| 408 | |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 409 | static int netns_bpf_max_progs(enum netns_bpf_attach_type type) |
| 410 | { |
| 411 | switch (type) { |
| 412 | case NETNS_BPF_FLOW_DISSECTOR: |
| 413 | return 1; |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 414 | case NETNS_BPF_SK_LOOKUP: |
| 415 | return 64; |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 416 | default: |
| 417 | return 0; |
| 418 | } |
| 419 | } |
| 420 | |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 421 | static int netns_bpf_link_attach(struct net *net, struct bpf_link *link, |
| 422 | enum netns_bpf_attach_type type) |
| 423 | { |
Jakub Sitnicki | ab53cad | 2020-06-25 16:13:56 +0200 | [diff] [blame] | 424 | struct bpf_netns_link *net_link = |
| 425 | container_of(link, struct bpf_netns_link, link); |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 426 | struct bpf_prog_array *run_array; |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 427 | int cnt, err; |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 428 | |
| 429 | mutex_lock(&netns_bpf_mutex); |
| 430 | |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 431 | cnt = link_count(net, type); |
| 432 | if (cnt >= netns_bpf_max_progs(type)) { |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 433 | err = -E2BIG; |
| 434 | goto out_unlock; |
| 435 | } |
| 436 | /* Links are not compatible with attaching prog directly */ |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 437 | if (net->bpf.progs[type]) { |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 438 | err = -EEXIST; |
| 439 | goto out_unlock; |
| 440 | } |
| 441 | |
| 442 | switch (type) { |
| 443 | case NETNS_BPF_FLOW_DISSECTOR: |
Jakub Sitnicki | 3b70169 | 2020-06-25 16:13:54 +0200 | [diff] [blame] | 444 | err = flow_dissector_bpf_prog_attach_check(net, link->prog); |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 445 | break; |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 446 | case NETNS_BPF_SK_LOOKUP: |
| 447 | err = 0; /* nothing to check */ |
| 448 | break; |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 449 | default: |
| 450 | err = -EINVAL; |
| 451 | break; |
| 452 | } |
| 453 | if (err) |
| 454 | goto out_unlock; |
| 455 | |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 456 | run_array = bpf_prog_array_alloc(cnt + 1, GFP_KERNEL); |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 457 | if (!run_array) { |
| 458 | err = -ENOMEM; |
| 459 | goto out_unlock; |
| 460 | } |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 461 | |
Jakub Sitnicki | ab53cad | 2020-06-25 16:13:56 +0200 | [diff] [blame] | 462 | list_add_tail(&net_link->node, &net->bpf.links[type]); |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 463 | |
Jakub Sitnicki | ce3aa9c | 2020-07-17 12:35:22 +0200 | [diff] [blame] | 464 | fill_prog_array(net, type, run_array); |
| 465 | run_array = rcu_replace_pointer(net->bpf.run_array[type], run_array, |
| 466 | lockdep_is_held(&netns_bpf_mutex)); |
| 467 | bpf_prog_array_free(run_array); |
| 468 | |
Jakub Sitnicki | 1559b4a | 2020-07-17 12:35:25 +0200 | [diff] [blame] | 469 | /* Mark attach point as used */ |
| 470 | netns_bpf_attach_type_need(type); |
| 471 | |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 472 | out_unlock: |
| 473 | mutex_unlock(&netns_bpf_mutex); |
| 474 | return err; |
| 475 | } |
| 476 | |
| 477 | int netns_bpf_link_create(const union bpf_attr *attr, struct bpf_prog *prog) |
| 478 | { |
| 479 | enum netns_bpf_attach_type netns_type; |
| 480 | struct bpf_link_primer link_primer; |
| 481 | struct bpf_netns_link *net_link; |
| 482 | enum bpf_attach_type type; |
| 483 | struct net *net; |
| 484 | int err; |
| 485 | |
| 486 | if (attr->link_create.flags) |
| 487 | return -EINVAL; |
| 488 | |
| 489 | type = attr->link_create.attach_type; |
| 490 | netns_type = to_netns_bpf_attach_type(type); |
| 491 | if (netns_type < 0) |
| 492 | return -EINVAL; |
| 493 | |
| 494 | net = get_net_ns_by_fd(attr->link_create.target_fd); |
| 495 | if (IS_ERR(net)) |
| 496 | return PTR_ERR(net); |
| 497 | |
| 498 | net_link = kzalloc(sizeof(*net_link), GFP_USER); |
| 499 | if (!net_link) { |
| 500 | err = -ENOMEM; |
| 501 | goto out_put_net; |
| 502 | } |
| 503 | bpf_link_init(&net_link->link, BPF_LINK_TYPE_NETNS, |
| 504 | &bpf_netns_link_ops, prog); |
| 505 | net_link->net = net; |
| 506 | net_link->type = type; |
| 507 | net_link->netns_type = netns_type; |
| 508 | |
| 509 | err = bpf_link_prime(&net_link->link, &link_primer); |
| 510 | if (err) { |
| 511 | kfree(net_link); |
| 512 | goto out_put_net; |
| 513 | } |
| 514 | |
| 515 | err = netns_bpf_link_attach(net, &net_link->link, netns_type); |
| 516 | if (err) { |
| 517 | bpf_link_cleanup(&link_primer); |
| 518 | goto out_put_net; |
| 519 | } |
| 520 | |
| 521 | put_net(net); |
| 522 | return bpf_link_settle(&link_primer); |
| 523 | |
| 524 | out_put_net: |
| 525 | put_net(net); |
| 526 | return err; |
| 527 | } |
| 528 | |
Jakub Sitnicki | ab53cad | 2020-06-25 16:13:56 +0200 | [diff] [blame] | 529 | static int __net_init netns_bpf_pernet_init(struct net *net) |
| 530 | { |
| 531 | int type; |
| 532 | |
| 533 | for (type = 0; type < MAX_NETNS_BPF_ATTACH_TYPE; type++) |
| 534 | INIT_LIST_HEAD(&net->bpf.links[type]); |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 539 | static void __net_exit netns_bpf_pernet_pre_exit(struct net *net) |
| 540 | { |
| 541 | enum netns_bpf_attach_type type; |
Jakub Sitnicki | ab53cad | 2020-06-25 16:13:56 +0200 | [diff] [blame] | 542 | struct bpf_netns_link *net_link; |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 543 | |
| 544 | mutex_lock(&netns_bpf_mutex); |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 545 | for (type = 0; type < MAX_NETNS_BPF_ATTACH_TYPE; type++) { |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 546 | netns_bpf_run_array_detach(net, type); |
Jakub Sitnicki | 1559b4a | 2020-07-17 12:35:25 +0200 | [diff] [blame] | 547 | list_for_each_entry(net_link, &net->bpf.links[type], node) { |
Jakub Sitnicki | ab53cad | 2020-06-25 16:13:56 +0200 | [diff] [blame] | 548 | net_link->net = NULL; /* auto-detach link */ |
Jakub Sitnicki | 1559b4a | 2020-07-17 12:35:25 +0200 | [diff] [blame] | 549 | netns_bpf_attach_type_unneed(type); |
| 550 | } |
Jakub Sitnicki | ab53cad | 2020-06-25 16:13:56 +0200 | [diff] [blame] | 551 | if (net->bpf.progs[type]) |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 552 | bpf_prog_put(net->bpf.progs[type]); |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 553 | } |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 554 | mutex_unlock(&netns_bpf_mutex); |
| 555 | } |
| 556 | |
| 557 | static struct pernet_operations netns_bpf_pernet_ops __net_initdata = { |
Jakub Sitnicki | ab53cad | 2020-06-25 16:13:56 +0200 | [diff] [blame] | 558 | .init = netns_bpf_pernet_init, |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 559 | .pre_exit = netns_bpf_pernet_pre_exit, |
| 560 | }; |
| 561 | |
| 562 | static int __init netns_bpf_init(void) |
| 563 | { |
| 564 | return register_pernet_subsys(&netns_bpf_pernet_ops); |
| 565 | } |
| 566 | |
| 567 | subsys_initcall(netns_bpf_init); |