1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/bpf-netns.h>
5 #include <linux/filter.h>
6 #include <net/net_namespace.h>
9 * Functions to manage BPF programs attached to netns
12 struct bpf_netns_link {
14 enum bpf_attach_type type;
15 enum netns_bpf_attach_type netns_type;
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.
23 struct list_head node; /* node in list of links attached to net */
26 /* Protects updates to netns_bpf */
27 DEFINE_MUTEX(netns_bpf_mutex);
29 static void netns_bpf_attach_type_unneed(enum netns_bpf_attach_type type)
33 case NETNS_BPF_SK_LOOKUP:
34 static_branch_dec(&bpf_sk_lookup_enabled);
42 static void netns_bpf_attach_type_need(enum netns_bpf_attach_type type)
46 case NETNS_BPF_SK_LOOKUP:
47 static_branch_inc(&bpf_sk_lookup_enabled);
55 /* Must be called with netns_bpf_mutex held. */
56 static void netns_bpf_run_array_detach(struct net *net,
57 enum netns_bpf_attach_type type)
59 struct bpf_prog_array *run_array;
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);
66 static int link_index(struct net *net, enum netns_bpf_attach_type type,
67 struct bpf_netns_link *link)
69 struct bpf_netns_link *pos;
72 list_for_each_entry(pos, &net->bpf.links[type], node) {
80 static int link_count(struct net *net, enum netns_bpf_attach_type type)
82 struct list_head *pos;
85 list_for_each(pos, &net->bpf.links[type])
90 static void fill_prog_array(struct net *net, enum netns_bpf_attach_type type,
91 struct bpf_prog_array *prog_array)
93 struct bpf_netns_link *pos;
96 list_for_each_entry(pos, &net->bpf.links[type], node) {
97 prog_array->items[i].prog = pos->link.prog;
102 static void bpf_netns_link_release(struct bpf_link *link)
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;
107 struct bpf_prog_array *old_array, *new_array;
111 mutex_lock(&netns_bpf_mutex);
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
121 /* Mark attach point as unused */
122 netns_bpf_attach_type_unneed(type);
124 /* Remember link position in case of safe delete */
125 idx = link_index(net, type, net_link);
126 list_del(&net_link->node);
128 cnt = link_count(net, type);
130 netns_bpf_run_array_detach(net, type);
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);
138 WARN_ON(bpf_prog_array_delete_safe_at(old_array, idx));
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);
146 net_link->net = NULL;
147 mutex_unlock(&netns_bpf_mutex);
150 static int bpf_netns_link_detach(struct bpf_link *link)
152 bpf_netns_link_release(link);
156 static void bpf_netns_link_dealloc(struct bpf_link *link)
158 struct bpf_netns_link *net_link =
159 container_of(link, struct bpf_netns_link, link);
164 static int bpf_netns_link_update_prog(struct bpf_link *link,
165 struct bpf_prog *new_prog,
166 struct bpf_prog *old_prog)
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;
171 struct bpf_prog_array *run_array;
175 if (old_prog && old_prog != link->prog)
177 if (new_prog->type != link->prog->type)
180 mutex_lock(&netns_bpf_mutex);
183 if (!net || !check_net(net)) {
184 /* Link auto-detached or netns dying */
189 run_array = rcu_dereference_protected(net->bpf.run_array[type],
190 lockdep_is_held(&netns_bpf_mutex));
191 idx = link_index(net, type, net_link);
192 ret = bpf_prog_array_update_at(run_array, idx, new_prog);
196 old_prog = xchg(&link->prog, new_prog);
197 bpf_prog_put(old_prog);
200 mutex_unlock(&netns_bpf_mutex);
204 static int bpf_netns_link_fill_info(const struct bpf_link *link,
205 struct bpf_link_info *info)
207 const struct bpf_netns_link *net_link =
208 container_of(link, struct bpf_netns_link, link);
209 unsigned int inum = 0;
212 mutex_lock(&netns_bpf_mutex);
214 if (net && check_net(net))
216 mutex_unlock(&netns_bpf_mutex);
218 info->netns.netns_ino = inum;
219 info->netns.attach_type = net_link->type;
223 static void bpf_netns_link_show_fdinfo(const struct bpf_link *link,
224 struct seq_file *seq)
226 struct bpf_link_info info = {};
228 bpf_netns_link_fill_info(link, &info);
231 "attach_type:\t%u\n",
232 info.netns.netns_ino,
233 info.netns.attach_type);
236 static const struct bpf_link_ops bpf_netns_link_ops = {
237 .release = bpf_netns_link_release,
238 .dealloc = bpf_netns_link_dealloc,
239 .detach = bpf_netns_link_detach,
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,
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,
249 enum netns_bpf_attach_type type)
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;
255 run_array = rcu_dereference_protected(net->bpf.run_array[type],
256 lockdep_is_held(&netns_bpf_mutex));
258 prog_cnt = bpf_prog_array_length(run_array);
260 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
262 if (copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
264 if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
267 return bpf_prog_array_copy_to_user(run_array, prog_ids,
268 attr->query.prog_cnt);
271 int netns_bpf_prog_query(const union bpf_attr *attr,
272 union bpf_attr __user *uattr)
274 enum netns_bpf_attach_type type;
278 if (attr->query.query_flags)
281 type = to_netns_bpf_attach_type(attr->query.attach_type);
285 net = get_net_ns_by_fd(attr->query.target_fd);
289 mutex_lock(&netns_bpf_mutex);
290 ret = __netns_bpf_prog_query(attr, uattr, net, type);
291 mutex_unlock(&netns_bpf_mutex);
297 int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
299 struct bpf_prog_array *run_array;
300 enum netns_bpf_attach_type type;
301 struct bpf_prog *attached;
305 if (attr->target_fd || attr->attach_flags || attr->replace_bpf_fd)
308 type = to_netns_bpf_attach_type(attr->attach_type);
312 net = current->nsproxy->net_ns;
313 mutex_lock(&netns_bpf_mutex);
315 /* Attaching prog directly is not compatible with links */
316 if (!list_empty(&net->bpf.links[type])) {
322 case NETNS_BPF_FLOW_DISSECTOR:
323 ret = flow_dissector_bpf_prog_attach_check(net, prog);
332 attached = net->bpf.progs[type];
333 if (attached == prog) {
334 /* The same program cannot be attached twice */
339 run_array = rcu_dereference_protected(net->bpf.run_array[type],
340 lockdep_is_held(&netns_bpf_mutex));
342 WRITE_ONCE(run_array->items[0].prog, prog);
344 run_array = bpf_prog_array_alloc(1, GFP_KERNEL);
349 run_array->items[0].prog = prog;
350 rcu_assign_pointer(net->bpf.run_array[type], run_array);
353 net->bpf.progs[type] = prog;
355 bpf_prog_put(attached);
358 mutex_unlock(&netns_bpf_mutex);
363 /* Must be called with netns_bpf_mutex held. */
364 static int __netns_bpf_prog_detach(struct net *net,
365 enum netns_bpf_attach_type type,
366 struct bpf_prog *old)
368 struct bpf_prog *attached;
370 /* Progs attached via links cannot be detached */
371 if (!list_empty(&net->bpf.links[type]))
374 attached = net->bpf.progs[type];
375 if (!attached || attached != old)
377 netns_bpf_run_array_detach(net, type);
378 net->bpf.progs[type] = NULL;
379 bpf_prog_put(attached);
383 int netns_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
385 enum netns_bpf_attach_type type;
386 struct bpf_prog *prog;
392 type = to_netns_bpf_attach_type(attr->attach_type);
396 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
398 return PTR_ERR(prog);
400 mutex_lock(&netns_bpf_mutex);
401 ret = __netns_bpf_prog_detach(current->nsproxy->net_ns, type, prog);
402 mutex_unlock(&netns_bpf_mutex);
409 static int netns_bpf_max_progs(enum netns_bpf_attach_type type)
412 case NETNS_BPF_FLOW_DISSECTOR:
414 case NETNS_BPF_SK_LOOKUP:
421 static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
422 enum netns_bpf_attach_type type)
424 struct bpf_netns_link *net_link =
425 container_of(link, struct bpf_netns_link, link);
426 struct bpf_prog_array *run_array;
429 mutex_lock(&netns_bpf_mutex);
431 cnt = link_count(net, type);
432 if (cnt >= netns_bpf_max_progs(type)) {
436 /* Links are not compatible with attaching prog directly */
437 if (net->bpf.progs[type]) {
443 case NETNS_BPF_FLOW_DISSECTOR:
444 err = flow_dissector_bpf_prog_attach_check(net, link->prog);
446 case NETNS_BPF_SK_LOOKUP:
447 err = 0; /* nothing to check */
456 run_array = bpf_prog_array_alloc(cnt + 1, GFP_KERNEL);
462 list_add_tail(&net_link->node, &net->bpf.links[type]);
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);
469 /* Mark attach point as used */
470 netns_bpf_attach_type_need(type);
473 mutex_unlock(&netns_bpf_mutex);
477 int netns_bpf_link_create(const union bpf_attr *attr, struct bpf_prog *prog)
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;
486 if (attr->link_create.flags)
489 type = attr->link_create.attach_type;
490 netns_type = to_netns_bpf_attach_type(type);
494 net = get_net_ns_by_fd(attr->link_create.target_fd);
498 net_link = kzalloc(sizeof(*net_link), GFP_USER);
503 bpf_link_init(&net_link->link, BPF_LINK_TYPE_NETNS,
504 &bpf_netns_link_ops, prog);
506 net_link->type = type;
507 net_link->netns_type = netns_type;
509 err = bpf_link_prime(&net_link->link, &link_primer);
515 err = netns_bpf_link_attach(net, &net_link->link, netns_type);
517 bpf_link_cleanup(&link_primer);
522 return bpf_link_settle(&link_primer);
529 static int __net_init netns_bpf_pernet_init(struct net *net)
533 for (type = 0; type < MAX_NETNS_BPF_ATTACH_TYPE; type++)
534 INIT_LIST_HEAD(&net->bpf.links[type]);
539 static void __net_exit netns_bpf_pernet_pre_exit(struct net *net)
541 enum netns_bpf_attach_type type;
542 struct bpf_netns_link *net_link;
544 mutex_lock(&netns_bpf_mutex);
545 for (type = 0; type < MAX_NETNS_BPF_ATTACH_TYPE; type++) {
546 netns_bpf_run_array_detach(net, type);
547 list_for_each_entry(net_link, &net->bpf.links[type], node) {
548 net_link->net = NULL; /* auto-detach link */
549 netns_bpf_attach_type_unneed(type);
551 if (net->bpf.progs[type])
552 bpf_prog_put(net->bpf.progs[type]);
554 mutex_unlock(&netns_bpf_mutex);
557 static struct pernet_operations netns_bpf_pernet_ops __net_initdata = {
558 .init = netns_bpf_pernet_init,
559 .pre_exit = netns_bpf_pernet_pre_exit,
562 static int __init netns_bpf_init(void)
564 return register_pernet_subsys(&netns_bpf_pernet_ops);
567 subsys_initcall(netns_bpf_init);