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>
10 #include <linux/file.h>
12 struct bpfilter_umh_ops bpfilter_ops
;
13 EXPORT_SYMBOL_GPL(bpfilter_ops
);
15 static void bpfilter_umh_cleanup(struct umh_info
*info
)
17 mutex_lock(&bpfilter_ops
.lock
);
18 bpfilter_ops
.stop
= true;
19 fput(info
->pipe_to_umh
);
20 fput(info
->pipe_from_umh
);
22 mutex_unlock(&bpfilter_ops
.lock
);
25 static int bpfilter_mbox_request(struct sock
*sk
, int optname
,
27 unsigned int optlen
, bool is_set
)
30 mutex_lock(&bpfilter_ops
.lock
);
31 if (!bpfilter_ops
.sockopt
) {
32 mutex_unlock(&bpfilter_ops
.lock
);
33 request_module("bpfilter");
34 mutex_lock(&bpfilter_ops
.lock
);
36 if (!bpfilter_ops
.sockopt
) {
41 if (bpfilter_ops
.stop
) {
42 err
= bpfilter_ops
.start();
46 err
= bpfilter_ops
.sockopt(sk
, optname
, optval
, optlen
, is_set
);
48 mutex_unlock(&bpfilter_ops
.lock
);
52 int bpfilter_ip_set_sockopt(struct sock
*sk
, int optname
, char __user
*optval
,
55 return bpfilter_mbox_request(sk
, optname
, optval
, optlen
, true);
58 int bpfilter_ip_get_sockopt(struct sock
*sk
, int optname
, char __user
*optval
,
63 if (get_user(len
, optlen
))
66 return bpfilter_mbox_request(sk
, optname
, optval
, len
, false);
69 static int __init
bpfilter_sockopt_init(void)
71 mutex_init(&bpfilter_ops
.lock
);
72 bpfilter_ops
.stop
= true;
73 bpfilter_ops
.info
.cmdline
= "bpfilter_umh";
74 bpfilter_ops
.info
.cleanup
= &bpfilter_umh_cleanup
;
78 device_initcall(bpfilter_sockopt_init
);