Merge tag 'for-6.12/block-20240925' of git://git.kernel.dk/linux
[linux-2.6-block.git] / net / netlink / diag.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
eaaa3139
AV
2#include <linux/module.h>
3
4#include <net/sock.h>
5#include <linux/netlink.h>
6#include <linux/sock_diag.h>
7#include <linux/netlink_diag.h>
e341694e 8#include <linux/rhashtable.h>
eaaa3139
AV
9
10#include "af_netlink.h"
11
12static int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
13{
14 struct netlink_sock *nlk = nlk_sk(sk);
15
16 if (nlk->groups == NULL)
17 return 0;
18
19 return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
20 nlk->groups);
21}
22
457c79e5
AV
23static int sk_diag_put_flags(struct sock *sk, struct sk_buff *skb)
24{
25 struct netlink_sock *nlk = nlk_sk(sk);
26 u32 flags = 0;
27
28 if (nlk->cb_running)
29 flags |= NDIAG_FLAG_CB_RUNNING;
8fe08d70 30 if (nlk_test_bit(RECV_PKTINFO, sk))
457c79e5 31 flags |= NDIAG_FLAG_PKTINFO;
8fe08d70 32 if (nlk_test_bit(BROADCAST_SEND_ERROR, sk))
457c79e5 33 flags |= NDIAG_FLAG_BROADCAST_ERROR;
8fe08d70 34 if (nlk_test_bit(RECV_NO_ENOBUFS, sk))
457c79e5 35 flags |= NDIAG_FLAG_NO_ENOBUFS;
8fe08d70 36 if (nlk_test_bit(LISTEN_ALL_NSID, sk))
457c79e5 37 flags |= NDIAG_FLAG_LISTEN_ALL_NSID;
8fe08d70 38 if (nlk_test_bit(CAP_ACK, sk))
457c79e5
AV
39 flags |= NDIAG_FLAG_CAP_ACK;
40
41 return nla_put_u32(skb, NETLINK_DIAG_FLAGS, flags);
42}
43
eaaa3139
AV
44static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
45 struct netlink_diag_req *req,
46 u32 portid, u32 seq, u32 flags, int sk_ino)
47{
48 struct nlmsghdr *nlh;
49 struct netlink_diag_msg *rep;
50 struct netlink_sock *nlk = nlk_sk(sk);
51
52 nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
53 flags);
54 if (!nlh)
55 return -EMSGSIZE;
56
57 rep = nlmsg_data(nlh);
58 rep->ndiag_family = AF_NETLINK;
59 rep->ndiag_type = sk->sk_type;
60 rep->ndiag_protocol = sk->sk_protocol;
61 rep->ndiag_state = sk->sk_state;
62
63 rep->ndiag_ino = sk_ino;
64 rep->ndiag_portid = nlk->portid;
65 rep->ndiag_dst_portid = nlk->dst_portid;
66 rep->ndiag_dst_group = nlk->dst_group;
67 sock_diag_save_cookie(sk, rep->ndiag_cookie);
68
69 if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
70 sk_diag_dump_groups(sk, skb))
71 goto out_nlmsg_trim;
72
73 if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
74 sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
75 goto out_nlmsg_trim;
76
457c79e5
AV
77 if ((req->ndiag_show & NDIAG_SHOW_FLAGS) &&
78 sk_diag_put_flags(sk, skb))
79 goto out_nlmsg_trim;
80
053c095a
JB
81 nlmsg_end(skb, nlh);
82 return 0;
eaaa3139
AV
83
84out_nlmsg_trim:
85 nlmsg_cancel(skb, nlh);
86 return -EMSGSIZE;
87}
88
89static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
90 int protocol, int s_num)
91{
ad202074 92 struct rhashtable_iter *hti = (void *)cb->args[2];
eaaa3139 93 struct netlink_table *tbl = &nl_table[protocol];
eaaa3139
AV
94 struct net *net = sock_net(skb->sk);
95 struct netlink_diag_req *req;
e341694e 96 struct netlink_sock *nlsk;
8d61f926 97 unsigned long flags;
eaaa3139 98 struct sock *sk;
ad202074
HX
99 int num = 2;
100 int ret = 0;
eaaa3139
AV
101
102 req = nlmsg_data(cb->nlh);
103
ad202074
HX
104 if (s_num > 1)
105 goto mc_list;
88d6ed15 106
ad202074 107 num--;
e341694e 108
ad202074
HX
109 if (!hti) {
110 hti = kmalloc(sizeof(*hti), GFP_KERNEL);
111 if (!hti)
112 return -ENOMEM;
113
114 cb->args[2] = (long)hti;
115 }
116
117 if (!s_num)
118 rhashtable_walk_enter(&tbl->hash, hti);
119
97a6ec4a 120 rhashtable_walk_start(hti);
ad202074
HX
121
122 while ((nlsk = rhashtable_walk_next(hti))) {
123 if (IS_ERR(nlsk)) {
124 ret = PTR_ERR(nlsk);
125 if (ret == -EAGAIN) {
126 ret = 0;
eaaa3139
AV
127 continue;
128 }
ad202074
HX
129 break;
130 }
eaaa3139 131
ad202074 132 sk = (struct sock *)nlsk;
eaaa3139 133
ad202074
HX
134 if (!net_eq(sock_net(sk), net))
135 continue;
136
137 if (sk_diag_fill(sk, skb, req,
138 NETLINK_CB(cb->skb).portid,
139 cb->nlh->nlmsg_seq,
140 NLM_F_MULTI,
141 sock_i_ino(sk)) < 0) {
142 ret = 1;
143 break;
eaaa3139
AV
144 }
145 }
146
ad202074 147 rhashtable_walk_stop(hti);
97a6ec4a 148
ad202074
HX
149 if (ret)
150 goto done;
151
152 rhashtable_walk_exit(hti);
ad202074
HX
153 num++;
154
155mc_list:
8d61f926 156 read_lock_irqsave(&nl_table_lock, flags);
eaaa3139
AV
157 sk_for_each_bound(sk, &tbl->mc_list) {
158 if (sk_hashed(sk))
159 continue;
160 if (!net_eq(sock_net(sk), net))
161 continue;
162 if (num < s_num) {
163 num++;
164 continue;
165 }
166
167 if (sk_diag_fill(sk, skb, req,
168 NETLINK_CB(cb->skb).portid,
169 cb->nlh->nlmsg_seq,
170 NLM_F_MULTI,
25a9c8a4 171 __sock_i_ino(sk)) < 0) {
eaaa3139 172 ret = 1;
ad202074 173 break;
eaaa3139
AV
174 }
175 num++;
176 }
8d61f926 177 read_unlock_irqrestore(&nl_table_lock, flags);
ad202074 178
eaaa3139
AV
179done:
180 cb->args[0] = num;
eaaa3139
AV
181
182 return ret;
183}
184
185static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
186{
187 struct netlink_diag_req *req;
188 int s_num = cb->args[0];
ad202074 189 int err = 0;
eaaa3139
AV
190
191 req = nlmsg_data(cb->nlh);
192
eaaa3139
AV
193 if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
194 int i;
195
196 for (i = cb->args[1]; i < MAX_LINKS; i++) {
ad202074
HX
197 err = __netlink_diag_dump(skb, cb, i, s_num);
198 if (err)
eaaa3139
AV
199 break;
200 s_num = 0;
201 }
ad202074 202 cb->args[1] = i;
eaaa3139 203 } else {
93636d1f 204 if (req->sdiag_protocol >= MAX_LINKS)
eaaa3139 205 return -ENOENT;
eaaa3139 206
ad202074 207 err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
eaaa3139
AV
208 }
209
6647b338 210 return err <= 0 ? err : skb->len;
ad202074
HX
211}
212
213static int netlink_diag_dump_done(struct netlink_callback *cb)
214{
215 struct rhashtable_iter *hti = (void *)cb->args[2];
216
217 if (cb->args[0] == 1)
218 rhashtable_walk_exit(hti);
eaaa3139 219
ad202074
HX
220 kfree(hti);
221
222 return 0;
eaaa3139
AV
223}
224
225static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
226{
227 int hdrlen = sizeof(struct netlink_diag_req);
228 struct net *net = sock_net(skb->sk);
229
230 if (nlmsg_len(h) < hdrlen)
231 return -EINVAL;
232
233 if (h->nlmsg_flags & NLM_F_DUMP) {
234 struct netlink_dump_control c = {
235 .dump = netlink_diag_dump,
ad202074 236 .done = netlink_diag_dump_done,
eaaa3139
AV
237 };
238 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
239 } else
240 return -EOPNOTSUPP;
241}
242
243static const struct sock_diag_handler netlink_diag_handler = {
114b4bb1 244 .owner = THIS_MODULE,
eaaa3139
AV
245 .family = AF_NETLINK,
246 .dump = netlink_diag_handler_dump,
247};
248
249static int __init netlink_diag_init(void)
250{
251 return sock_diag_register(&netlink_diag_handler);
252}
253
254static void __exit netlink_diag_exit(void)
255{
256 sock_diag_unregister(&netlink_diag_handler);
257}
258
259module_init(netlink_diag_init);
260module_exit(netlink_diag_exit);
016b9332 261MODULE_DESCRIPTION("Netlink-based socket monitoring/diagnostic interface (sock_diag)");
eaaa3139
AV
262MODULE_LICENSE("GPL");
263MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);