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>
6 #include <linux/bpfilter.h>
7 #include <linux/sched.h>
8 #include <linux/sched/signal.h>
10 #include <linux/file.h>
13 extern char bpfilter_umh_start
;
14 extern char bpfilter_umh_end
;
16 static void shutdown_umh(void)
18 struct task_struct
*tsk
;
20 if (bpfilter_ops
.stop
)
23 tsk
= get_pid_task(find_vpid(bpfilter_ops
.info
.pid
), PIDTYPE_PID
);
25 send_sig(SIGKILL
, tsk
, 1);
30 static void __stop_umh(void)
32 if (IS_ENABLED(CONFIG_INET
))
36 static int __bpfilter_process_sockopt(struct sock
*sk
, int optname
,
38 unsigned int optlen
, bool is_set
)
40 struct mbox_request req
;
41 struct mbox_reply reply
;
47 req
.pid
= current
->pid
;
49 req
.addr
= (long __force __user
)optval
;
51 if (!bpfilter_ops
.info
.pid
)
53 n
= __kernel_write(bpfilter_ops
.info
.pipe_to_umh
, &req
, sizeof(req
),
55 if (n
!= sizeof(req
)) {
56 pr_err("write fail %zd\n", n
);
62 n
= kernel_read(bpfilter_ops
.info
.pipe_from_umh
, &reply
, sizeof(reply
),
64 if (n
!= sizeof(reply
)) {
65 pr_err("read fail %zd\n", n
);
75 static int start_umh(void)
79 /* fork usermode process */
80 err
= fork_usermode_blob(&bpfilter_umh_start
,
81 &bpfilter_umh_end
- &bpfilter_umh_start
,
85 bpfilter_ops
.stop
= false;
86 pr_info("Loaded bpfilter_umh pid %d\n", bpfilter_ops
.info
.pid
);
88 /* health check that usermode process started correctly */
89 if (__bpfilter_process_sockopt(NULL
, 0, NULL
, 0, 0) != 0) {
97 static int __init
load_umh(void)
101 mutex_lock(&bpfilter_ops
.lock
);
102 if (!bpfilter_ops
.stop
) {
107 if (!err
&& IS_ENABLED(CONFIG_INET
)) {
108 bpfilter_ops
.sockopt
= &__bpfilter_process_sockopt
;
109 bpfilter_ops
.start
= &start_umh
;
112 mutex_unlock(&bpfilter_ops
.lock
);
116 static void __exit
fini_umh(void)
118 mutex_lock(&bpfilter_ops
.lock
);
119 if (IS_ENABLED(CONFIG_INET
)) {
121 bpfilter_ops
.start
= NULL
;
122 bpfilter_ops
.sockopt
= NULL
;
124 mutex_unlock(&bpfilter_ops
.lock
);
126 module_init(load_umh
);
127 module_exit(fini_umh
);
128 MODULE_LICENSE("GPL");