18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci#include <linux/module.h>
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <net/sock.h>
58c2ecf20Sopenharmony_ci#include <linux/netlink.h>
68c2ecf20Sopenharmony_ci#include <linux/sock_diag.h>
78c2ecf20Sopenharmony_ci#include <linux/netlink_diag.h>
88c2ecf20Sopenharmony_ci#include <linux/rhashtable.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include "af_netlink.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	struct netlink_sock *nlk = nlk_sk(sk);
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	if (nlk->groups == NULL)
178c2ecf20Sopenharmony_ci		return 0;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
208c2ecf20Sopenharmony_ci		       nlk->groups);
218c2ecf20Sopenharmony_ci}
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic int sk_diag_put_flags(struct sock *sk, struct sk_buff *skb)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	struct netlink_sock *nlk = nlk_sk(sk);
268c2ecf20Sopenharmony_ci	u32 flags = 0;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	if (nlk->cb_running)
298c2ecf20Sopenharmony_ci		flags |= NDIAG_FLAG_CB_RUNNING;
308c2ecf20Sopenharmony_ci	if (nlk->flags & NETLINK_F_RECV_PKTINFO)
318c2ecf20Sopenharmony_ci		flags |= NDIAG_FLAG_PKTINFO;
328c2ecf20Sopenharmony_ci	if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
338c2ecf20Sopenharmony_ci		flags |= NDIAG_FLAG_BROADCAST_ERROR;
348c2ecf20Sopenharmony_ci	if (nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)
358c2ecf20Sopenharmony_ci		flags |= NDIAG_FLAG_NO_ENOBUFS;
368c2ecf20Sopenharmony_ci	if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
378c2ecf20Sopenharmony_ci		flags |= NDIAG_FLAG_LISTEN_ALL_NSID;
388c2ecf20Sopenharmony_ci	if (nlk->flags & NETLINK_F_CAP_ACK)
398c2ecf20Sopenharmony_ci		flags |= NDIAG_FLAG_CAP_ACK;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	return nla_put_u32(skb, NETLINK_DIAG_FLAGS, flags);
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
458c2ecf20Sopenharmony_ci			struct netlink_diag_req *req,
468c2ecf20Sopenharmony_ci			u32 portid, u32 seq, u32 flags, int sk_ino)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct nlmsghdr *nlh;
498c2ecf20Sopenharmony_ci	struct netlink_diag_msg *rep;
508c2ecf20Sopenharmony_ci	struct netlink_sock *nlk = nlk_sk(sk);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
538c2ecf20Sopenharmony_ci			flags);
548c2ecf20Sopenharmony_ci	if (!nlh)
558c2ecf20Sopenharmony_ci		return -EMSGSIZE;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	rep = nlmsg_data(nlh);
588c2ecf20Sopenharmony_ci	rep->ndiag_family	= AF_NETLINK;
598c2ecf20Sopenharmony_ci	rep->ndiag_type		= sk->sk_type;
608c2ecf20Sopenharmony_ci	rep->ndiag_protocol	= sk->sk_protocol;
618c2ecf20Sopenharmony_ci	rep->ndiag_state	= sk->sk_state;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	rep->ndiag_ino		= sk_ino;
648c2ecf20Sopenharmony_ci	rep->ndiag_portid	= nlk->portid;
658c2ecf20Sopenharmony_ci	rep->ndiag_dst_portid	= nlk->dst_portid;
668c2ecf20Sopenharmony_ci	rep->ndiag_dst_group	= nlk->dst_group;
678c2ecf20Sopenharmony_ci	sock_diag_save_cookie(sk, rep->ndiag_cookie);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
708c2ecf20Sopenharmony_ci	    sk_diag_dump_groups(sk, skb))
718c2ecf20Sopenharmony_ci		goto out_nlmsg_trim;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
748c2ecf20Sopenharmony_ci	    sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
758c2ecf20Sopenharmony_ci		goto out_nlmsg_trim;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if ((req->ndiag_show & NDIAG_SHOW_FLAGS) &&
788c2ecf20Sopenharmony_ci	    sk_diag_put_flags(sk, skb))
798c2ecf20Sopenharmony_ci		goto out_nlmsg_trim;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	nlmsg_end(skb, nlh);
828c2ecf20Sopenharmony_ci	return 0;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciout_nlmsg_trim:
858c2ecf20Sopenharmony_ci	nlmsg_cancel(skb, nlh);
868c2ecf20Sopenharmony_ci	return -EMSGSIZE;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
908c2ecf20Sopenharmony_ci				int protocol, int s_num)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct rhashtable_iter *hti = (void *)cb->args[2];
938c2ecf20Sopenharmony_ci	struct netlink_table *tbl = &nl_table[protocol];
948c2ecf20Sopenharmony_ci	struct net *net = sock_net(skb->sk);
958c2ecf20Sopenharmony_ci	struct netlink_diag_req *req;
968c2ecf20Sopenharmony_ci	struct netlink_sock *nlsk;
978c2ecf20Sopenharmony_ci	unsigned long flags;
988c2ecf20Sopenharmony_ci	struct sock *sk;
998c2ecf20Sopenharmony_ci	int num = 2;
1008c2ecf20Sopenharmony_ci	int ret = 0;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	req = nlmsg_data(cb->nlh);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (s_num > 1)
1058c2ecf20Sopenharmony_ci		goto mc_list;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	num--;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	if (!hti) {
1108c2ecf20Sopenharmony_ci		hti = kmalloc(sizeof(*hti), GFP_KERNEL);
1118c2ecf20Sopenharmony_ci		if (!hti)
1128c2ecf20Sopenharmony_ci			return -ENOMEM;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci		cb->args[2] = (long)hti;
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (!s_num)
1188c2ecf20Sopenharmony_ci		rhashtable_walk_enter(&tbl->hash, hti);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	rhashtable_walk_start(hti);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	while ((nlsk = rhashtable_walk_next(hti))) {
1238c2ecf20Sopenharmony_ci		if (IS_ERR(nlsk)) {
1248c2ecf20Sopenharmony_ci			ret = PTR_ERR(nlsk);
1258c2ecf20Sopenharmony_ci			if (ret == -EAGAIN) {
1268c2ecf20Sopenharmony_ci				ret = 0;
1278c2ecf20Sopenharmony_ci				continue;
1288c2ecf20Sopenharmony_ci			}
1298c2ecf20Sopenharmony_ci			break;
1308c2ecf20Sopenharmony_ci		}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci		sk = (struct sock *)nlsk;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		if (!net_eq(sock_net(sk), net))
1358c2ecf20Sopenharmony_ci			continue;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci		if (sk_diag_fill(sk, skb, req,
1388c2ecf20Sopenharmony_ci				 NETLINK_CB(cb->skb).portid,
1398c2ecf20Sopenharmony_ci				 cb->nlh->nlmsg_seq,
1408c2ecf20Sopenharmony_ci				 NLM_F_MULTI,
1418c2ecf20Sopenharmony_ci				 sock_i_ino(sk)) < 0) {
1428c2ecf20Sopenharmony_ci			ret = 1;
1438c2ecf20Sopenharmony_ci			break;
1448c2ecf20Sopenharmony_ci		}
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	rhashtable_walk_stop(hti);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	if (ret)
1508c2ecf20Sopenharmony_ci		goto done;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	rhashtable_walk_exit(hti);
1538c2ecf20Sopenharmony_ci	num++;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cimc_list:
1568c2ecf20Sopenharmony_ci	read_lock_irqsave(&nl_table_lock, flags);
1578c2ecf20Sopenharmony_ci	sk_for_each_bound(sk, &tbl->mc_list) {
1588c2ecf20Sopenharmony_ci		if (sk_hashed(sk))
1598c2ecf20Sopenharmony_ci			continue;
1608c2ecf20Sopenharmony_ci		if (!net_eq(sock_net(sk), net))
1618c2ecf20Sopenharmony_ci			continue;
1628c2ecf20Sopenharmony_ci		if (num < s_num) {
1638c2ecf20Sopenharmony_ci			num++;
1648c2ecf20Sopenharmony_ci			continue;
1658c2ecf20Sopenharmony_ci		}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci		if (sk_diag_fill(sk, skb, req,
1688c2ecf20Sopenharmony_ci				 NETLINK_CB(cb->skb).portid,
1698c2ecf20Sopenharmony_ci				 cb->nlh->nlmsg_seq,
1708c2ecf20Sopenharmony_ci				 NLM_F_MULTI,
1718c2ecf20Sopenharmony_ci				 __sock_i_ino(sk)) < 0) {
1728c2ecf20Sopenharmony_ci			ret = 1;
1738c2ecf20Sopenharmony_ci			break;
1748c2ecf20Sopenharmony_ci		}
1758c2ecf20Sopenharmony_ci		num++;
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci	read_unlock_irqrestore(&nl_table_lock, flags);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cidone:
1808c2ecf20Sopenharmony_ci	cb->args[0] = num;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	return ret;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct netlink_diag_req *req;
1888c2ecf20Sopenharmony_ci	int s_num = cb->args[0];
1898c2ecf20Sopenharmony_ci	int err = 0;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	req = nlmsg_data(cb->nlh);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
1948c2ecf20Sopenharmony_ci		int i;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci		for (i = cb->args[1]; i < MAX_LINKS; i++) {
1978c2ecf20Sopenharmony_ci			err = __netlink_diag_dump(skb, cb, i, s_num);
1988c2ecf20Sopenharmony_ci			if (err)
1998c2ecf20Sopenharmony_ci				break;
2008c2ecf20Sopenharmony_ci			s_num = 0;
2018c2ecf20Sopenharmony_ci		}
2028c2ecf20Sopenharmony_ci		cb->args[1] = i;
2038c2ecf20Sopenharmony_ci	} else {
2048c2ecf20Sopenharmony_ci		if (req->sdiag_protocol >= MAX_LINKS)
2058c2ecf20Sopenharmony_ci			return -ENOENT;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci		err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	return err < 0 ? err : skb->len;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int netlink_diag_dump_done(struct netlink_callback *cb)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct rhashtable_iter *hti = (void *)cb->args[2];
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	if (cb->args[0] == 1)
2188c2ecf20Sopenharmony_ci		rhashtable_walk_exit(hti);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	kfree(hti);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	return 0;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	int hdrlen = sizeof(struct netlink_diag_req);
2288c2ecf20Sopenharmony_ci	struct net *net = sock_net(skb->sk);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	if (nlmsg_len(h) < hdrlen)
2318c2ecf20Sopenharmony_ci		return -EINVAL;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	if (h->nlmsg_flags & NLM_F_DUMP) {
2348c2ecf20Sopenharmony_ci		struct netlink_dump_control c = {
2358c2ecf20Sopenharmony_ci			.dump = netlink_diag_dump,
2368c2ecf20Sopenharmony_ci			.done = netlink_diag_dump_done,
2378c2ecf20Sopenharmony_ci		};
2388c2ecf20Sopenharmony_ci		return netlink_dump_start(net->diag_nlsk, skb, h, &c);
2398c2ecf20Sopenharmony_ci	} else
2408c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic const struct sock_diag_handler netlink_diag_handler = {
2448c2ecf20Sopenharmony_ci	.family = AF_NETLINK,
2458c2ecf20Sopenharmony_ci	.dump = netlink_diag_handler_dump,
2468c2ecf20Sopenharmony_ci};
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic int __init netlink_diag_init(void)
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	return sock_diag_register(&netlink_diag_handler);
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic void __exit netlink_diag_exit(void)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	sock_diag_unregister(&netlink_diag_handler);
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cimodule_init(netlink_diag_init);
2598c2ecf20Sopenharmony_cimodule_exit(netlink_diag_exit);
2608c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2618c2ecf20Sopenharmony_ciMODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);
262