sockopt.c (1881B)
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> 11 12struct bpfilter_umh_ops bpfilter_ops; 13EXPORT_SYMBOL_GPL(bpfilter_ops); 14 15void bpfilter_umh_cleanup(struct umd_info *info) 16{ 17 fput(info->pipe_to_umh); 18 fput(info->pipe_from_umh); 19 put_pid(info->tgid); 20 info->tgid = NULL; 21} 22EXPORT_SYMBOL_GPL(bpfilter_umh_cleanup); 23 24static int bpfilter_mbox_request(struct sock *sk, int optname, sockptr_t optval, 25 unsigned int optlen, bool is_set) 26{ 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); 33 34 if (!bpfilter_ops.sockopt) { 35 err = -ENOPROTOOPT; 36 goto out; 37 } 38 } 39 if (bpfilter_ops.info.tgid && 40 thread_group_exited(bpfilter_ops.info.tgid)) 41 bpfilter_umh_cleanup(&bpfilter_ops.info); 42 43 if (!bpfilter_ops.info.tgid) { 44 err = bpfilter_ops.start(); 45 if (err) 46 goto out; 47 } 48 err = bpfilter_ops.sockopt(sk, optname, optval, optlen, is_set); 49out: 50 mutex_unlock(&bpfilter_ops.lock); 51 return err; 52} 53 54int bpfilter_ip_set_sockopt(struct sock *sk, int optname, sockptr_t optval, 55 unsigned int optlen) 56{ 57 return bpfilter_mbox_request(sk, optname, optval, optlen, true); 58} 59 60int bpfilter_ip_get_sockopt(struct sock *sk, int optname, char __user *optval, 61 int __user *optlen) 62{ 63 int len; 64 65 if (get_user(len, optlen)) 66 return -EFAULT; 67 68 return bpfilter_mbox_request(sk, optname, USER_SOCKPTR(optval), len, 69 false); 70} 71 72static int __init bpfilter_sockopt_init(void) 73{ 74 mutex_init(&bpfilter_ops.lock); 75 bpfilter_ops.info.tgid = NULL; 76 bpfilter_ops.info.driver_name = "bpfilter_umh"; 77 78 return 0; 79} 80device_initcall(bpfilter_sockopt_init);