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 void bpfilter_umh_cleanup(struct umd_info
*info
)
17 fput(info
->pipe_to_umh
);
18 fput(info
->pipe_from_umh
);
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
)
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
) {
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();
48 err
= bpfilter_ops
.sockopt(sk
, optname
, optval
, optlen
, is_set
);
50 mutex_unlock(&bpfilter_ops
.lock
);
54 int bpfilter_ip_set_sockopt(struct sock
*sk
, int optname
, sockptr_t optval
,
57 return bpfilter_mbox_request(sk
, optname
, optval
, optlen
, true);
60 int bpfilter_ip_get_sockopt(struct sock
*sk
, int optname
, char __user
*optval
,
65 if (get_user(len
, optlen
))
68 return bpfilter_mbox_request(sk
, optname
, USER_SOCKPTR(optval
), len
,
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";
80 device_initcall(bpfilter_sockopt_init
);