bpfilter: include bpfilter_umh in assembly instead of using objcopy
[linux/fpc-iii.git] / net / bpfilter / bpfilter_kern.c
blobf0fc182d3db77eb311d91f7faef4e8a6f85886b3
1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/umh.h>
6 #include <linux/bpfilter.h>
7 #include <linux/sched.h>
8 #include <linux/sched/signal.h>
9 #include <linux/fs.h>
10 #include <linux/file.h>
11 #include "msgfmt.h"
13 extern char bpfilter_umh_start;
14 extern char bpfilter_umh_end;
16 static struct umh_info info;
17 /* since ip_getsockopt() can run in parallel, serialize access to umh */
18 static DEFINE_MUTEX(bpfilter_lock);
20 static void shutdown_umh(struct umh_info *info)
22 struct task_struct *tsk;
24 if (!info->pid)
25 return;
26 tsk = pid_task(find_vpid(info->pid), PIDTYPE_PID);
27 if (tsk)
28 force_sig(SIGKILL, tsk);
29 fput(info->pipe_to_umh);
30 fput(info->pipe_from_umh);
31 info->pid = 0;
34 static void __stop_umh(void)
36 if (IS_ENABLED(CONFIG_INET)) {
37 bpfilter_process_sockopt = NULL;
38 shutdown_umh(&info);
42 static void stop_umh(void)
44 mutex_lock(&bpfilter_lock);
45 __stop_umh();
46 mutex_unlock(&bpfilter_lock);
49 static int __bpfilter_process_sockopt(struct sock *sk, int optname,
50 char __user *optval,
51 unsigned int optlen, bool is_set)
53 struct mbox_request req;
54 struct mbox_reply reply;
55 loff_t pos;
56 ssize_t n;
57 int ret = -EFAULT;
59 req.is_set = is_set;
60 req.pid = current->pid;
61 req.cmd = optname;
62 req.addr = (long)optval;
63 req.len = optlen;
64 mutex_lock(&bpfilter_lock);
65 if (!info.pid)
66 goto out;
67 n = __kernel_write(info.pipe_to_umh, &req, sizeof(req), &pos);
68 if (n != sizeof(req)) {
69 pr_err("write fail %zd\n", n);
70 __stop_umh();
71 ret = -EFAULT;
72 goto out;
74 pos = 0;
75 n = kernel_read(info.pipe_from_umh, &reply, sizeof(reply), &pos);
76 if (n != sizeof(reply)) {
77 pr_err("read fail %zd\n", n);
78 __stop_umh();
79 ret = -EFAULT;
80 goto out;
82 ret = reply.status;
83 out:
84 mutex_unlock(&bpfilter_lock);
85 return ret;
88 static int __init load_umh(void)
90 int err;
92 /* fork usermode process */
93 err = fork_usermode_blob(&bpfilter_umh_start,
94 &bpfilter_umh_end - &bpfilter_umh_start,
95 &info);
96 if (err)
97 return err;
98 pr_info("Loaded bpfilter_umh pid %d\n", info.pid);
100 /* health check that usermode process started correctly */
101 if (__bpfilter_process_sockopt(NULL, 0, 0, 0, 0) != 0) {
102 stop_umh();
103 return -EFAULT;
105 if (IS_ENABLED(CONFIG_INET))
106 bpfilter_process_sockopt = &__bpfilter_process_sockopt;
108 return 0;
111 static void __exit fini_umh(void)
113 stop_umh();
115 module_init(load_umh);
116 module_exit(fini_umh);
117 MODULE_LICENSE("GPL");