1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/filter.h>
5 #include <net/net_namespace.h>
8 * Functions to manage BPF programs attached to netns
11 struct bpf_netns_link {
13 enum bpf_attach_type type;
14 enum netns_bpf_attach_type netns_type;
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.
24 /* Protects updates to netns_bpf */
25 DEFINE_MUTEX(netns_bpf_mutex);
27 /* Must be called with netns_bpf_mutex held. */
28 static void __net_exit bpf_netns_link_auto_detach(struct bpf_link *link)
30 struct bpf_netns_link *net_link =
31 container_of(link, struct bpf_netns_link, link);
36 static void bpf_netns_link_release(struct bpf_link *link)
38 struct bpf_netns_link *net_link =
39 container_of(link, struct bpf_netns_link, link);
40 enum netns_bpf_attach_type type = net_link->netns_type;
43 /* Link auto-detached by dying netns. */
47 mutex_lock(&netns_bpf_mutex);
49 /* Recheck after potential sleep. We can race with cleanup_net
50 * here, but if we see a non-NULL struct net pointer pre_exit
51 * has not happened yet and will block on netns_bpf_mutex.
57 net->bpf.links[type] = NULL;
58 RCU_INIT_POINTER(net->bpf.progs[type], NULL);
61 mutex_unlock(&netns_bpf_mutex);
64 static void bpf_netns_link_dealloc(struct bpf_link *link)
66 struct bpf_netns_link *net_link =
67 container_of(link, struct bpf_netns_link, link);
72 static int bpf_netns_link_update_prog(struct bpf_link *link,
73 struct bpf_prog *new_prog,
74 struct bpf_prog *old_prog)
76 struct bpf_netns_link *net_link =
77 container_of(link, struct bpf_netns_link, link);
78 enum netns_bpf_attach_type type = net_link->netns_type;
82 if (old_prog && old_prog != link->prog)
84 if (new_prog->type != link->prog->type)
87 mutex_lock(&netns_bpf_mutex);
90 if (!net || !check_net(net)) {
91 /* Link auto-detached or netns dying */
96 old_prog = xchg(&link->prog, new_prog);
97 rcu_assign_pointer(net->bpf.progs[type], new_prog);
98 bpf_prog_put(old_prog);
101 mutex_unlock(&netns_bpf_mutex);
105 static int bpf_netns_link_fill_info(const struct bpf_link *link,
106 struct bpf_link_info *info)
108 const struct bpf_netns_link *net_link =
109 container_of(link, struct bpf_netns_link, link);
110 unsigned int inum = 0;
113 mutex_lock(&netns_bpf_mutex);
115 if (net && check_net(net))
117 mutex_unlock(&netns_bpf_mutex);
119 info->netns.netns_ino = inum;
120 info->netns.attach_type = net_link->type;
124 static void bpf_netns_link_show_fdinfo(const struct bpf_link *link,
125 struct seq_file *seq)
127 struct bpf_link_info info = {};
129 bpf_netns_link_fill_info(link, &info);
132 "attach_type:\t%u\n",
133 info.netns.netns_ino,
134 info.netns.attach_type);
137 static const struct bpf_link_ops bpf_netns_link_ops = {
138 .release = bpf_netns_link_release,
139 .dealloc = bpf_netns_link_dealloc,
140 .update_prog = bpf_netns_link_update_prog,
141 .fill_link_info = bpf_netns_link_fill_info,
142 .show_fdinfo = bpf_netns_link_show_fdinfo,
145 int netns_bpf_prog_query(const union bpf_attr *attr,
146 union bpf_attr __user *uattr)
148 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
149 u32 prog_id, prog_cnt = 0, flags = 0;
150 enum netns_bpf_attach_type type;
151 struct bpf_prog *attached;
154 if (attr->query.query_flags)
157 type = to_netns_bpf_attach_type(attr->query.attach_type);
161 net = get_net_ns_by_fd(attr->query.target_fd);
166 attached = rcu_dereference(net->bpf.progs[type]);
169 prog_id = attached->aux->id;
175 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
177 if (copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
180 if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
183 if (copy_to_user(prog_ids, &prog_id, sizeof(u32)))
189 int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
191 enum netns_bpf_attach_type type;
195 type = to_netns_bpf_attach_type(attr->attach_type);
199 net = current->nsproxy->net_ns;
200 mutex_lock(&netns_bpf_mutex);
202 /* Attaching prog directly is not compatible with links */
203 if (net->bpf.links[type]) {
209 case NETNS_BPF_FLOW_DISSECTOR:
210 ret = flow_dissector_bpf_prog_attach(net, prog);
217 mutex_unlock(&netns_bpf_mutex);
222 /* Must be called with netns_bpf_mutex held. */
223 static int __netns_bpf_prog_detach(struct net *net,
224 enum netns_bpf_attach_type type)
226 struct bpf_prog *attached;
228 /* Progs attached via links cannot be detached */
229 if (net->bpf.links[type])
232 attached = rcu_dereference_protected(net->bpf.progs[type],
233 lockdep_is_held(&netns_bpf_mutex));
236 RCU_INIT_POINTER(net->bpf.progs[type], NULL);
237 bpf_prog_put(attached);
241 int netns_bpf_prog_detach(const union bpf_attr *attr)
243 enum netns_bpf_attach_type type;
246 type = to_netns_bpf_attach_type(attr->attach_type);
250 mutex_lock(&netns_bpf_mutex);
251 ret = __netns_bpf_prog_detach(current->nsproxy->net_ns, type);
252 mutex_unlock(&netns_bpf_mutex);
257 static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
258 enum netns_bpf_attach_type type)
260 struct bpf_prog *prog;
263 mutex_lock(&netns_bpf_mutex);
265 /* Allow attaching only one prog or link for now */
266 if (net->bpf.links[type]) {
270 /* Links are not compatible with attaching prog directly */
271 prog = rcu_dereference_protected(net->bpf.progs[type],
272 lockdep_is_held(&netns_bpf_mutex));
279 case NETNS_BPF_FLOW_DISSECTOR:
280 err = flow_dissector_bpf_prog_attach(net, link->prog);
289 net->bpf.links[type] = link;
292 mutex_unlock(&netns_bpf_mutex);
296 int netns_bpf_link_create(const union bpf_attr *attr, struct bpf_prog *prog)
298 enum netns_bpf_attach_type netns_type;
299 struct bpf_link_primer link_primer;
300 struct bpf_netns_link *net_link;
301 enum bpf_attach_type type;
305 if (attr->link_create.flags)
308 type = attr->link_create.attach_type;
309 netns_type = to_netns_bpf_attach_type(type);
313 net = get_net_ns_by_fd(attr->link_create.target_fd);
317 net_link = kzalloc(sizeof(*net_link), GFP_USER);
322 bpf_link_init(&net_link->link, BPF_LINK_TYPE_NETNS,
323 &bpf_netns_link_ops, prog);
325 net_link->type = type;
326 net_link->netns_type = netns_type;
328 err = bpf_link_prime(&net_link->link, &link_primer);
334 err = netns_bpf_link_attach(net, &net_link->link, netns_type);
336 bpf_link_cleanup(&link_primer);
341 return bpf_link_settle(&link_primer);
348 static void __net_exit netns_bpf_pernet_pre_exit(struct net *net)
350 enum netns_bpf_attach_type type;
351 struct bpf_link *link;
353 mutex_lock(&netns_bpf_mutex);
354 for (type = 0; type < MAX_NETNS_BPF_ATTACH_TYPE; type++) {
355 link = net->bpf.links[type];
357 bpf_netns_link_auto_detach(link);
359 __netns_bpf_prog_detach(net, type);
361 mutex_unlock(&netns_bpf_mutex);
364 static struct pernet_operations netns_bpf_pernet_ops __net_initdata = {
365 .pre_exit = netns_bpf_pernet_pre_exit,
368 static int __init netns_bpf_init(void)
370 return register_pernet_subsys(&netns_bpf_pernet_ops);
373 subsys_initcall(netns_bpf_init);