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