Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / net / ipv4 / bpfilter / sockopt.c
blob1b34cb9a7708ecf8c40eeb138223e090a9e27bc7
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/module.h>
4 #include <linux/uaccess.h>
5 #include <linux/bpfilter.h>
6 #include <uapi/linux/bpf.h>
7 #include <linux/wait.h>
8 #include <linux/kmod.h>
9 #include <linux/fs.h>
10 #include <linux/file.h>
12 struct bpfilter_umh_ops bpfilter_ops;
13 EXPORT_SYMBOL_GPL(bpfilter_ops);
15 void bpfilter_umh_cleanup(struct umd_info *info)
17 fput(info->pipe_to_umh);
18 fput(info->pipe_from_umh);
19 put_pid(info->tgid);
20 info->tgid = NULL;
22 EXPORT_SYMBOL_GPL(bpfilter_umh_cleanup);
24 static int bpfilter_mbox_request(struct sock *sk, int optname, sockptr_t optval,
25 unsigned int optlen, bool is_set)
27 int err;
28 mutex_lock(&bpfilter_ops.lock);
29 if (!bpfilter_ops.sockopt) {
30 mutex_unlock(&bpfilter_ops.lock);
31 request_module("bpfilter");
32 mutex_lock(&bpfilter_ops.lock);
34 if (!bpfilter_ops.sockopt) {
35 err = -ENOPROTOOPT;
36 goto out;
39 if (bpfilter_ops.info.tgid &&
40 thread_group_exited(bpfilter_ops.info.tgid))
41 bpfilter_umh_cleanup(&bpfilter_ops.info);
43 if (!bpfilter_ops.info.tgid) {
44 err = bpfilter_ops.start();
45 if (err)
46 goto out;
48 err = bpfilter_ops.sockopt(sk, optname, optval, optlen, is_set);
49 out:
50 mutex_unlock(&bpfilter_ops.lock);
51 return err;
54 int bpfilter_ip_set_sockopt(struct sock *sk, int optname, sockptr_t optval,
55 unsigned int optlen)
57 return bpfilter_mbox_request(sk, optname, optval, optlen, true);
60 int bpfilter_ip_get_sockopt(struct sock *sk, int optname, char __user *optval,
61 int __user *optlen)
63 int len;
65 if (get_user(len, optlen))
66 return -EFAULT;
68 return bpfilter_mbox_request(sk, optname, USER_SOCKPTR(optval), len,
69 false);
72 static int __init bpfilter_sockopt_init(void)
74 mutex_init(&bpfilter_ops.lock);
75 bpfilter_ops.info.tgid = NULL;
76 bpfilter_ops.info.driver_name = "bpfilter_umh";
78 return 0;
80 device_initcall(bpfilter_sockopt_init);