mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-12-05 09:56:55 +07:00
7ae457c1e5
clean up names related to socket filtering and bpf in the following way: - everything that deals with sockets keeps 'sk_*' prefix - everything that is pure BPF is changed to 'bpf_*' prefix split 'struct sk_filter' into struct sk_filter { atomic_t refcnt; struct rcu_head rcu; struct bpf_prog *prog; }; and struct bpf_prog { u32 jited:1, len:31; struct sock_fprog_kern *orig_prog; unsigned int (*bpf_func)(const struct sk_buff *skb, const struct bpf_insn *filter); union { struct sock_filter insns[0]; struct bpf_insn insnsi[0]; struct work_struct work; }; }; so that 'struct bpf_prog' can be used independent of sockets and cleans up 'unattached' bpf use cases split SK_RUN_FILTER macro into: SK_RUN_FILTER to be used with 'struct sk_filter *' and BPF_PROG_RUN to be used with 'struct bpf_prog *' __sk_filter_release(struct sk_filter *) gains __bpf_prog_release(struct bpf_prog *) helper function also perform related renames for the functions that work with 'struct bpf_prog *', since they're on the same lines: sk_filter_size -> bpf_prog_size sk_filter_select_runtime -> bpf_prog_select_runtime sk_filter_free -> bpf_prog_free sk_unattached_filter_create -> bpf_prog_create sk_unattached_filter_destroy -> bpf_prog_destroy sk_store_orig_filter -> bpf_prog_store_orig_filter sk_release_orig_filter -> bpf_release_orig_filter __sk_migrate_filter -> bpf_migrate_filter __sk_prepare_filter -> bpf_prepare_filter API for attaching classic BPF to a socket stays the same: sk_attach_filter(prog, struct sock *)/sk_detach_filter(struct sock *) and SK_RUN_FILTER(struct sk_filter *, ctx) to execute a program which is used by sockets, tun, af_packet API for 'unattached' BPF programs becomes: bpf_prog_create(struct bpf_prog **)/bpf_prog_destroy(struct bpf_prog *) and BPF_PROG_RUN(struct bpf_prog *, ctx) to execute a program which is used by isdn, ppp, team, seccomp, ptp, xt_bpf, cls_bpf, test_bpf Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
232 lines
5.5 KiB
C
232 lines
5.5 KiB
C
#include <linux/mutex.h>
|
|
#include <linux/socket.h>
|
|
#include <linux/skbuff.h>
|
|
#include <net/netlink.h>
|
|
#include <net/net_namespace.h>
|
|
#include <linux/module.h>
|
|
#include <net/sock.h>
|
|
|
|
#include <linux/inet_diag.h>
|
|
#include <linux/sock_diag.h>
|
|
|
|
static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
|
|
static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
|
|
static DEFINE_MUTEX(sock_diag_table_mutex);
|
|
|
|
int sock_diag_check_cookie(void *sk, __u32 *cookie)
|
|
{
|
|
if ((cookie[0] != INET_DIAG_NOCOOKIE ||
|
|
cookie[1] != INET_DIAG_NOCOOKIE) &&
|
|
((u32)(unsigned long)sk != cookie[0] ||
|
|
(u32)((((unsigned long)sk) >> 31) >> 1) != cookie[1]))
|
|
return -ESTALE;
|
|
else
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
|
|
|
|
void sock_diag_save_cookie(void *sk, __u32 *cookie)
|
|
{
|
|
cookie[0] = (u32)(unsigned long)sk;
|
|
cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
|
|
|
|
int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
|
|
{
|
|
u32 mem[SK_MEMINFO_VARS];
|
|
|
|
mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
|
|
mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
|
|
mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
|
|
mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
|
|
mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
|
|
mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
|
|
mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
|
|
mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
|
|
|
|
return nla_put(skb, attrtype, sizeof(mem), &mem);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
|
|
|
|
int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
|
|
struct sk_buff *skb, int attrtype)
|
|
{
|
|
struct sock_fprog_kern *fprog;
|
|
struct sk_filter *filter;
|
|
struct nlattr *attr;
|
|
unsigned int flen;
|
|
int err = 0;
|
|
|
|
if (!may_report_filterinfo) {
|
|
nla_reserve(skb, attrtype, 0);
|
|
return 0;
|
|
}
|
|
|
|
rcu_read_lock();
|
|
filter = rcu_dereference(sk->sk_filter);
|
|
if (!filter)
|
|
goto out;
|
|
|
|
fprog = filter->prog->orig_prog;
|
|
flen = bpf_classic_proglen(fprog);
|
|
|
|
attr = nla_reserve(skb, attrtype, flen);
|
|
if (attr == NULL) {
|
|
err = -EMSGSIZE;
|
|
goto out;
|
|
}
|
|
|
|
memcpy(nla_data(attr), fprog->filter, flen);
|
|
out:
|
|
rcu_read_unlock();
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL(sock_diag_put_filterinfo);
|
|
|
|
void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
|
|
{
|
|
mutex_lock(&sock_diag_table_mutex);
|
|
inet_rcv_compat = fn;
|
|
mutex_unlock(&sock_diag_table_mutex);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
|
|
|
|
void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
|
|
{
|
|
mutex_lock(&sock_diag_table_mutex);
|
|
inet_rcv_compat = NULL;
|
|
mutex_unlock(&sock_diag_table_mutex);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
|
|
|
|
int sock_diag_register(const struct sock_diag_handler *hndl)
|
|
{
|
|
int err = 0;
|
|
|
|
if (hndl->family >= AF_MAX)
|
|
return -EINVAL;
|
|
|
|
mutex_lock(&sock_diag_table_mutex);
|
|
if (sock_diag_handlers[hndl->family])
|
|
err = -EBUSY;
|
|
else
|
|
sock_diag_handlers[hndl->family] = hndl;
|
|
mutex_unlock(&sock_diag_table_mutex);
|
|
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sock_diag_register);
|
|
|
|
void sock_diag_unregister(const struct sock_diag_handler *hnld)
|
|
{
|
|
int family = hnld->family;
|
|
|
|
if (family >= AF_MAX)
|
|
return;
|
|
|
|
mutex_lock(&sock_diag_table_mutex);
|
|
BUG_ON(sock_diag_handlers[family] != hnld);
|
|
sock_diag_handlers[family] = NULL;
|
|
mutex_unlock(&sock_diag_table_mutex);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sock_diag_unregister);
|
|
|
|
static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
|
|
{
|
|
int err;
|
|
struct sock_diag_req *req = nlmsg_data(nlh);
|
|
const struct sock_diag_handler *hndl;
|
|
|
|
if (nlmsg_len(nlh) < sizeof(*req))
|
|
return -EINVAL;
|
|
|
|
if (req->sdiag_family >= AF_MAX)
|
|
return -EINVAL;
|
|
|
|
if (sock_diag_handlers[req->sdiag_family] == NULL)
|
|
request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
|
|
NETLINK_SOCK_DIAG, req->sdiag_family);
|
|
|
|
mutex_lock(&sock_diag_table_mutex);
|
|
hndl = sock_diag_handlers[req->sdiag_family];
|
|
if (hndl == NULL)
|
|
err = -ENOENT;
|
|
else
|
|
err = hndl->dump(skb, nlh);
|
|
mutex_unlock(&sock_diag_table_mutex);
|
|
|
|
return err;
|
|
}
|
|
|
|
static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
|
|
{
|
|
int ret;
|
|
|
|
switch (nlh->nlmsg_type) {
|
|
case TCPDIAG_GETSOCK:
|
|
case DCCPDIAG_GETSOCK:
|
|
if (inet_rcv_compat == NULL)
|
|
request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
|
|
NETLINK_SOCK_DIAG, AF_INET);
|
|
|
|
mutex_lock(&sock_diag_table_mutex);
|
|
if (inet_rcv_compat != NULL)
|
|
ret = inet_rcv_compat(skb, nlh);
|
|
else
|
|
ret = -EOPNOTSUPP;
|
|
mutex_unlock(&sock_diag_table_mutex);
|
|
|
|
return ret;
|
|
case SOCK_DIAG_BY_FAMILY:
|
|
return __sock_diag_rcv_msg(skb, nlh);
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
|
|
static DEFINE_MUTEX(sock_diag_mutex);
|
|
|
|
static void sock_diag_rcv(struct sk_buff *skb)
|
|
{
|
|
mutex_lock(&sock_diag_mutex);
|
|
netlink_rcv_skb(skb, &sock_diag_rcv_msg);
|
|
mutex_unlock(&sock_diag_mutex);
|
|
}
|
|
|
|
static int __net_init diag_net_init(struct net *net)
|
|
{
|
|
struct netlink_kernel_cfg cfg = {
|
|
.input = sock_diag_rcv,
|
|
};
|
|
|
|
net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
|
|
return net->diag_nlsk == NULL ? -ENOMEM : 0;
|
|
}
|
|
|
|
static void __net_exit diag_net_exit(struct net *net)
|
|
{
|
|
netlink_kernel_release(net->diag_nlsk);
|
|
net->diag_nlsk = NULL;
|
|
}
|
|
|
|
static struct pernet_operations diag_net_ops = {
|
|
.init = diag_net_init,
|
|
.exit = diag_net_exit,
|
|
};
|
|
|
|
static int __init sock_diag_init(void)
|
|
{
|
|
return register_pernet_subsys(&diag_net_ops);
|
|
}
|
|
|
|
static void __exit sock_diag_exit(void)
|
|
{
|
|
unregister_pernet_subsys(&diag_net_ops);
|
|
}
|
|
|
|
module_init(sock_diag_init);
|
|
module_exit(sock_diag_exit);
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);
|