Merge tag 'staging-5.8-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux/fpc-iii.git] / kernel / bpf / net_namespace.c
blob78cf061f817938560117a7a6bbed007b9660810e
1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bpf.h>
4 #include <linux/filter.h>
5 #include <net/net_namespace.h>
7 /*
8 * Functions to manage BPF programs attached to netns
9 */
11 struct bpf_netns_link {
12 struct bpf_link 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.
21 struct net *net;
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);
33 net_link->net = NULL;
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;
41 struct net *net;
43 /* Link auto-detached by dying netns. */
44 if (!net_link->net)
45 return;
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.
53 net = net_link->net;
54 if (!net)
55 goto out_unlock;
57 net->bpf.links[type] = NULL;
58 RCU_INIT_POINTER(net->bpf.progs[type], NULL);
60 out_unlock:
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);
69 kfree(net_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;
79 struct net *net;
80 int ret = 0;
82 if (old_prog && old_prog != link->prog)
83 return -EPERM;
84 if (new_prog->type != link->prog->type)
85 return -EINVAL;
87 mutex_lock(&netns_bpf_mutex);
89 net = net_link->net;
90 if (!net || !check_net(net)) {
91 /* Link auto-detached or netns dying */
92 ret = -ENOLINK;
93 goto out_unlock;
96 old_prog = xchg(&link->prog, new_prog);
97 rcu_assign_pointer(net->bpf.progs[type], new_prog);
98 bpf_prog_put(old_prog);
100 out_unlock:
101 mutex_unlock(&netns_bpf_mutex);
102 return ret;
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;
111 struct net *net;
113 mutex_lock(&netns_bpf_mutex);
114 net = net_link->net;
115 if (net && check_net(net))
116 inum = net->ns.inum;
117 mutex_unlock(&netns_bpf_mutex);
119 info->netns.netns_ino = inum;
120 info->netns.attach_type = net_link->type;
121 return 0;
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);
130 seq_printf(seq,
131 "netns_ino:\t%u\n"
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;
152 struct net *net;
154 if (attr->query.query_flags)
155 return -EINVAL;
157 type = to_netns_bpf_attach_type(attr->query.attach_type);
158 if (type < 0)
159 return -EINVAL;
161 net = get_net_ns_by_fd(attr->query.target_fd);
162 if (IS_ERR(net))
163 return PTR_ERR(net);
165 rcu_read_lock();
166 attached = rcu_dereference(net->bpf.progs[type]);
167 if (attached) {
168 prog_cnt = 1;
169 prog_id = attached->aux->id;
171 rcu_read_unlock();
173 put_net(net);
175 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
176 return -EFAULT;
177 if (copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
178 return -EFAULT;
180 if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
181 return 0;
183 if (copy_to_user(prog_ids, &prog_id, sizeof(u32)))
184 return -EFAULT;
186 return 0;
189 int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
191 enum netns_bpf_attach_type type;
192 struct net *net;
193 int ret;
195 type = to_netns_bpf_attach_type(attr->attach_type);
196 if (type < 0)
197 return -EINVAL;
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]) {
204 ret = -EEXIST;
205 goto out_unlock;
208 switch (type) {
209 case NETNS_BPF_FLOW_DISSECTOR:
210 ret = flow_dissector_bpf_prog_attach(net, prog);
211 break;
212 default:
213 ret = -EINVAL;
214 break;
216 out_unlock:
217 mutex_unlock(&netns_bpf_mutex);
219 return ret;
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])
230 return -EINVAL;
232 attached = rcu_dereference_protected(net->bpf.progs[type],
233 lockdep_is_held(&netns_bpf_mutex));
234 if (!attached)
235 return -ENOENT;
236 RCU_INIT_POINTER(net->bpf.progs[type], NULL);
237 bpf_prog_put(attached);
238 return 0;
241 int netns_bpf_prog_detach(const union bpf_attr *attr)
243 enum netns_bpf_attach_type type;
244 int ret;
246 type = to_netns_bpf_attach_type(attr->attach_type);
247 if (type < 0)
248 return -EINVAL;
250 mutex_lock(&netns_bpf_mutex);
251 ret = __netns_bpf_prog_detach(current->nsproxy->net_ns, type);
252 mutex_unlock(&netns_bpf_mutex);
254 return ret;
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;
261 int err;
263 mutex_lock(&netns_bpf_mutex);
265 /* Allow attaching only one prog or link for now */
266 if (net->bpf.links[type]) {
267 err = -E2BIG;
268 goto out_unlock;
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));
273 if (prog) {
274 err = -EEXIST;
275 goto out_unlock;
278 switch (type) {
279 case NETNS_BPF_FLOW_DISSECTOR:
280 err = flow_dissector_bpf_prog_attach(net, link->prog);
281 break;
282 default:
283 err = -EINVAL;
284 break;
286 if (err)
287 goto out_unlock;
289 net->bpf.links[type] = link;
291 out_unlock:
292 mutex_unlock(&netns_bpf_mutex);
293 return err;
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;
302 struct net *net;
303 int err;
305 if (attr->link_create.flags)
306 return -EINVAL;
308 type = attr->link_create.attach_type;
309 netns_type = to_netns_bpf_attach_type(type);
310 if (netns_type < 0)
311 return -EINVAL;
313 net = get_net_ns_by_fd(attr->link_create.target_fd);
314 if (IS_ERR(net))
315 return PTR_ERR(net);
317 net_link = kzalloc(sizeof(*net_link), GFP_USER);
318 if (!net_link) {
319 err = -ENOMEM;
320 goto out_put_net;
322 bpf_link_init(&net_link->link, BPF_LINK_TYPE_NETNS,
323 &bpf_netns_link_ops, prog);
324 net_link->net = net;
325 net_link->type = type;
326 net_link->netns_type = netns_type;
328 err = bpf_link_prime(&net_link->link, &link_primer);
329 if (err) {
330 kfree(net_link);
331 goto out_put_net;
334 err = netns_bpf_link_attach(net, &net_link->link, netns_type);
335 if (err) {
336 bpf_link_cleanup(&link_primer);
337 goto out_put_net;
340 put_net(net);
341 return bpf_link_settle(&link_primer);
343 out_put_net:
344 put_net(net);
345 return err;
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];
356 if (link)
357 bpf_netns_link_auto_detach(link);
358 else
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);