2019-05-19 20:08:20 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-10-21 18:03:44 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#include <linux/inet_diag.h>
|
|
|
|
#include <linux/sock_diag.h>
|
|
|
|
|
2016-10-25 23:53:22 +08:00
|
|
|
#include <net/inet_sock.h>
|
2016-10-21 18:03:44 +08:00
|
|
|
#include <net/raw.h>
|
|
|
|
#include <net/rawv6.h>
|
|
|
|
|
|
|
|
#ifdef pr_fmt
|
|
|
|
# undef pr_fmt
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
|
|
|
static struct raw_hashinfo *
|
|
|
|
raw_get_hashinfo(const struct inet_diag_req_v2 *r)
|
|
|
|
{
|
|
|
|
if (r->sdiag_family == AF_INET) {
|
|
|
|
return &raw_v4_hashinfo;
|
|
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
|
|
} else if (r->sdiag_family == AF_INET6) {
|
|
|
|
return &raw_v6_hashinfo;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Due to requirement of not breaking user API we can't simply
|
|
|
|
* rename @pad field in inet_diag_req_v2 structure, instead
|
|
|
|
* use helper to figure it out.
|
|
|
|
*/
|
|
|
|
|
2022-06-18 11:47:04 +08:00
|
|
|
static bool raw_lookup(struct net *net, struct sock *sk,
|
|
|
|
const struct inet_diag_req_v2 *req)
|
2016-10-21 18:03:44 +08:00
|
|
|
{
|
|
|
|
struct inet_diag_req_raw *r = (void *)req;
|
|
|
|
|
|
|
|
if (r->sdiag_family == AF_INET)
|
2022-06-18 11:47:04 +08:00
|
|
|
return raw_v4_match(net, sk, r->sdiag_raw_protocol,
|
|
|
|
r->id.idiag_dst[0],
|
|
|
|
r->id.idiag_src[0],
|
|
|
|
r->id.idiag_if, 0);
|
2016-10-21 18:03:44 +08:00
|
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
|
|
else
|
2022-06-18 11:47:04 +08:00
|
|
|
return raw_v6_match(net, sk, r->sdiag_raw_protocol,
|
|
|
|
(const struct in6_addr *)r->id.idiag_src,
|
|
|
|
(const struct in6_addr *)r->id.idiag_dst,
|
|
|
|
r->id.idiag_if, 0);
|
2016-10-21 18:03:44 +08:00
|
|
|
#endif
|
2022-06-18 11:47:04 +08:00
|
|
|
return false;
|
2016-10-21 18:03:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2 *r)
|
|
|
|
{
|
|
|
|
struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
|
2022-06-18 11:47:05 +08:00
|
|
|
struct hlist_nulls_head *hlist;
|
|
|
|
struct hlist_nulls_node *hnode;
|
2022-06-18 11:47:04 +08:00
|
|
|
struct sock *sk;
|
2016-10-21 18:03:44 +08:00
|
|
|
int slot;
|
|
|
|
|
|
|
|
if (IS_ERR(hashinfo))
|
|
|
|
return ERR_CAST(hashinfo);
|
|
|
|
|
2022-06-18 11:47:05 +08:00
|
|
|
rcu_read_lock();
|
2016-10-21 18:03:44 +08:00
|
|
|
for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) {
|
2022-06-18 11:47:05 +08:00
|
|
|
hlist = &hashinfo->ht[slot];
|
2022-06-20 07:29:27 +08:00
|
|
|
sk_nulls_for_each(sk, hnode, hlist) {
|
2022-06-18 11:47:04 +08:00
|
|
|
if (raw_lookup(net, sk, r)) {
|
2016-10-21 18:03:44 +08:00
|
|
|
/*
|
|
|
|
* Grab it and keep until we fill
|
2022-06-18 11:47:05 +08:00
|
|
|
* diag message to be reported, so
|
2016-10-21 18:03:44 +08:00
|
|
|
* caller should call sock_put then.
|
|
|
|
*/
|
2022-06-18 11:47:05 +08:00
|
|
|
if (refcount_inc_not_zero(&sk->sk_refcnt))
|
|
|
|
goto out_unlock;
|
2016-10-21 18:03:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-18 11:47:04 +08:00
|
|
|
sk = ERR_PTR(-ENOENT);
|
2016-11-02 20:36:32 +08:00
|
|
|
out_unlock:
|
2022-06-18 11:47:05 +08:00
|
|
|
rcu_read_unlock();
|
2016-10-21 18:03:44 +08:00
|
|
|
|
2022-06-18 11:47:04 +08:00
|
|
|
return sk;
|
2016-10-21 18:03:44 +08:00
|
|
|
}
|
|
|
|
|
2020-02-26 07:04:09 +08:00
|
|
|
static int raw_diag_dump_one(struct netlink_callback *cb,
|
2016-10-21 18:03:44 +08:00
|
|
|
const struct inet_diag_req_v2 *r)
|
|
|
|
{
|
2020-02-26 07:04:09 +08:00
|
|
|
struct sk_buff *in_skb = cb->skb;
|
2016-10-21 18:03:44 +08:00
|
|
|
struct sk_buff *rep;
|
|
|
|
struct sock *sk;
|
2020-02-26 07:04:09 +08:00
|
|
|
struct net *net;
|
2016-10-21 18:03:44 +08:00
|
|
|
int err;
|
|
|
|
|
2020-02-26 07:04:09 +08:00
|
|
|
net = sock_net(in_skb->sk);
|
2016-10-21 18:03:44 +08:00
|
|
|
sk = raw_sock_get(net, r);
|
|
|
|
if (IS_ERR(sk))
|
|
|
|
return PTR_ERR(sk);
|
|
|
|
|
2020-03-05 20:33:12 +08:00
|
|
|
rep = nlmsg_new(nla_total_size(sizeof(struct inet_diag_msg)) +
|
|
|
|
inet_diag_msg_attrs_size() +
|
|
|
|
nla_total_size(sizeof(struct inet_diag_meminfo)) + 64,
|
2016-10-21 18:03:44 +08:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!rep) {
|
|
|
|
sock_put(sk);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2020-02-26 07:04:09 +08:00
|
|
|
err = inet_sk_diag_fill(sk, NULL, rep, cb, r, 0,
|
2016-10-21 18:03:44 +08:00
|
|
|
netlink_net_capable(in_skb, CAP_NET_ADMIN));
|
|
|
|
sock_put(sk);
|
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
kfree_skb(rep);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-07-13 10:48:24 +08:00
|
|
|
err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
|
|
|
|
|
2016-10-21 18:03:44 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
|
|
|
|
struct netlink_callback *cb,
|
|
|
|
const struct inet_diag_req_v2 *r,
|
|
|
|
struct nlattr *bc, bool net_admin)
|
|
|
|
{
|
|
|
|
if (!inet_diag_bc_sk(bc, sk))
|
|
|
|
return 0;
|
|
|
|
|
2020-02-26 07:04:09 +08:00
|
|
|
return inet_sk_diag_fill(sk, NULL, skb, cb, r, NLM_F_MULTI, net_admin);
|
2016-10-21 18:03:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
|
2020-02-26 07:04:15 +08:00
|
|
|
const struct inet_diag_req_v2 *r)
|
2016-10-21 18:03:44 +08:00
|
|
|
{
|
|
|
|
bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
|
|
|
|
struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
|
|
|
|
struct net *net = sock_net(skb->sk);
|
2020-02-26 07:04:15 +08:00
|
|
|
struct inet_diag_dump_data *cb_data;
|
2022-06-18 11:47:05 +08:00
|
|
|
struct hlist_nulls_head *hlist;
|
|
|
|
struct hlist_nulls_node *hnode;
|
2016-10-21 18:03:44 +08:00
|
|
|
int num, s_num, slot, s_slot;
|
|
|
|
struct sock *sk = NULL;
|
2020-02-26 07:04:15 +08:00
|
|
|
struct nlattr *bc;
|
2016-10-21 18:03:44 +08:00
|
|
|
|
|
|
|
if (IS_ERR(hashinfo))
|
|
|
|
return;
|
|
|
|
|
2020-02-26 07:04:15 +08:00
|
|
|
cb_data = cb->data;
|
|
|
|
bc = cb_data->inet_diag_nla_bc;
|
2016-10-21 18:03:44 +08:00
|
|
|
s_slot = cb->args[0];
|
|
|
|
num = s_num = cb->args[1];
|
|
|
|
|
2022-06-20 18:05:09 +08:00
|
|
|
rcu_read_lock();
|
2016-10-21 18:03:44 +08:00
|
|
|
for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) {
|
|
|
|
num = 0;
|
|
|
|
|
2022-06-18 11:47:05 +08:00
|
|
|
hlist = &hashinfo->ht[slot];
|
2022-06-20 07:29:27 +08:00
|
|
|
sk_nulls_for_each(sk, hnode, hlist) {
|
2016-10-21 18:03:44 +08:00
|
|
|
struct inet_sock *inet = inet_sk(sk);
|
|
|
|
|
|
|
|
if (!net_eq(sock_net(sk), net))
|
|
|
|
continue;
|
|
|
|
if (num < s_num)
|
|
|
|
goto next;
|
|
|
|
if (sk->sk_family != r->sdiag_family)
|
|
|
|
goto next;
|
|
|
|
if (r->id.idiag_sport != inet->inet_sport &&
|
|
|
|
r->id.idiag_sport)
|
|
|
|
goto next;
|
|
|
|
if (r->id.idiag_dport != inet->inet_dport &&
|
|
|
|
r->id.idiag_dport)
|
|
|
|
goto next;
|
|
|
|
if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0)
|
|
|
|
goto out_unlock;
|
|
|
|
next:
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out_unlock:
|
2022-06-20 18:05:09 +08:00
|
|
|
rcu_read_unlock();
|
2016-10-21 18:03:44 +08:00
|
|
|
|
|
|
|
cb->args[0] = slot;
|
|
|
|
cb->args[1] = num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
|
|
|
|
void *info)
|
|
|
|
{
|
|
|
|
r->idiag_rqueue = sk_rmem_alloc_get(sk);
|
|
|
|
r->idiag_wqueue = sk_wmem_alloc_get(sk);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_INET_DIAG_DESTROY
|
|
|
|
static int raw_diag_destroy(struct sk_buff *in_skb,
|
|
|
|
const struct inet_diag_req_v2 *r)
|
|
|
|
{
|
|
|
|
struct net *net = sock_net(in_skb->sk);
|
|
|
|
struct sock *sk;
|
2016-11-02 20:36:31 +08:00
|
|
|
int err;
|
2016-10-21 18:03:44 +08:00
|
|
|
|
|
|
|
sk = raw_sock_get(net, r);
|
|
|
|
if (IS_ERR(sk))
|
|
|
|
return PTR_ERR(sk);
|
2016-11-02 20:36:31 +08:00
|
|
|
err = sock_diag_destroy(sk, ECONNABORTED);
|
|
|
|
sock_put(sk);
|
|
|
|
return err;
|
2016-10-21 18:03:44 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const struct inet_diag_handler raw_diag_handler = {
|
|
|
|
.dump = raw_diag_dump,
|
|
|
|
.dump_one = raw_diag_dump_one,
|
|
|
|
.idiag_get_info = raw_diag_get_info,
|
|
|
|
.idiag_type = IPPROTO_RAW,
|
|
|
|
.idiag_info_size = 0,
|
|
|
|
#ifdef CONFIG_INET_DIAG_DESTROY
|
|
|
|
.destroy = raw_diag_destroy,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static void __always_unused __check_inet_diag_req_raw(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Make sure the two structures are identical,
|
|
|
|
* except the @pad field.
|
|
|
|
*/
|
|
|
|
#define __offset_mismatch(m1, m2) \
|
|
|
|
(offsetof(struct inet_diag_req_v2, m1) != \
|
|
|
|
offsetof(struct inet_diag_req_raw, m2))
|
|
|
|
|
|
|
|
BUILD_BUG_ON(sizeof(struct inet_diag_req_v2) !=
|
|
|
|
sizeof(struct inet_diag_req_raw));
|
|
|
|
BUILD_BUG_ON(__offset_mismatch(sdiag_family, sdiag_family));
|
|
|
|
BUILD_BUG_ON(__offset_mismatch(sdiag_protocol, sdiag_protocol));
|
|
|
|
BUILD_BUG_ON(__offset_mismatch(idiag_ext, idiag_ext));
|
|
|
|
BUILD_BUG_ON(__offset_mismatch(pad, sdiag_raw_protocol));
|
|
|
|
BUILD_BUG_ON(__offset_mismatch(idiag_states, idiag_states));
|
|
|
|
BUILD_BUG_ON(__offset_mismatch(id, id));
|
|
|
|
#undef __offset_mismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init raw_diag_init(void)
|
|
|
|
{
|
|
|
|
return inet_diag_register(&raw_diag_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit raw_diag_exit(void)
|
|
|
|
{
|
|
|
|
inet_diag_unregister(&raw_diag_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(raw_diag_init);
|
|
|
|
module_exit(raw_diag_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */);
|
|
|
|
MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10-255 /* AF_INET6 - IPPROTO_RAW */);
|