blob: 8faa20b4d4573f3e2b510f03f3d4a85a70352559 [file] [log] [blame]
Andrey Vagineaaa3132013-03-21 20:33:48 +04001#include <linux/module.h>
2
3#include <net/sock.h>
4#include <linux/netlink.h>
5#include <linux/sock_diag.h>
6#include <linux/netlink_diag.h>
Thomas Grafe3416942014-08-02 11:47:45 +02007#include <linux/rhashtable.h>
Andrey Vagineaaa3132013-03-21 20:33:48 +04008
9#include "af_netlink.h"
10
11static int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
12{
13 struct netlink_sock *nlk = nlk_sk(sk);
14
15 if (nlk->groups == NULL)
16 return 0;
17
18 return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
19 nlk->groups);
20}
21
Andrey Vagin457c79e2017-04-03 18:13:32 -070022static int sk_diag_put_flags(struct sock *sk, struct sk_buff *skb)
23{
24 struct netlink_sock *nlk = nlk_sk(sk);
25 u32 flags = 0;
26
27 if (nlk->cb_running)
28 flags |= NDIAG_FLAG_CB_RUNNING;
29 if (nlk->flags & NETLINK_F_RECV_PKTINFO)
30 flags |= NDIAG_FLAG_PKTINFO;
31 if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
32 flags |= NDIAG_FLAG_BROADCAST_ERROR;
33 if (nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)
34 flags |= NDIAG_FLAG_NO_ENOBUFS;
35 if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
36 flags |= NDIAG_FLAG_LISTEN_ALL_NSID;
37 if (nlk->flags & NETLINK_F_CAP_ACK)
38 flags |= NDIAG_FLAG_CAP_ACK;
39
40 return nla_put_u32(skb, NETLINK_DIAG_FLAGS, flags);
41}
42
Andrey Vagineaaa3132013-03-21 20:33:48 +040043static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
44 struct netlink_diag_req *req,
45 u32 portid, u32 seq, u32 flags, int sk_ino)
46{
47 struct nlmsghdr *nlh;
48 struct netlink_diag_msg *rep;
49 struct netlink_sock *nlk = nlk_sk(sk);
50
51 nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
52 flags);
53 if (!nlh)
54 return -EMSGSIZE;
55
56 rep = nlmsg_data(nlh);
57 rep->ndiag_family = AF_NETLINK;
58 rep->ndiag_type = sk->sk_type;
59 rep->ndiag_protocol = sk->sk_protocol;
60 rep->ndiag_state = sk->sk_state;
61
62 rep->ndiag_ino = sk_ino;
63 rep->ndiag_portid = nlk->portid;
64 rep->ndiag_dst_portid = nlk->dst_portid;
65 rep->ndiag_dst_group = nlk->dst_group;
66 sock_diag_save_cookie(sk, rep->ndiag_cookie);
67
68 if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
69 sk_diag_dump_groups(sk, skb))
70 goto out_nlmsg_trim;
71
72 if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
73 sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
74 goto out_nlmsg_trim;
75
Andrey Vagin457c79e2017-04-03 18:13:32 -070076 if ((req->ndiag_show & NDIAG_SHOW_FLAGS) &&
77 sk_diag_put_flags(sk, skb))
78 goto out_nlmsg_trim;
79
Johannes Berg053c0952015-01-16 22:09:00 +010080 nlmsg_end(skb, nlh);
81 return 0;
Andrey Vagineaaa3132013-03-21 20:33:48 +040082
83out_nlmsg_trim:
84 nlmsg_cancel(skb, nlh);
85 return -EMSGSIZE;
86}
87
88static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
89 int protocol, int s_num)
90{
Herbert Xuad202072016-08-19 16:21:37 +080091 struct rhashtable_iter *hti = (void *)cb->args[2];
Andrey Vagineaaa3132013-03-21 20:33:48 +040092 struct netlink_table *tbl = &nl_table[protocol];
Andrey Vagineaaa3132013-03-21 20:33:48 +040093 struct net *net = sock_net(skb->sk);
94 struct netlink_diag_req *req;
Thomas Grafe3416942014-08-02 11:47:45 +020095 struct netlink_sock *nlsk;
Andrey Vagineaaa3132013-03-21 20:33:48 +040096 struct sock *sk;
Herbert Xuad202072016-08-19 16:21:37 +080097 int num = 2;
98 int ret = 0;
Andrey Vagineaaa3132013-03-21 20:33:48 +040099
100 req = nlmsg_data(cb->nlh);
101
Herbert Xuad202072016-08-19 16:21:37 +0800102 if (s_num > 1)
103 goto mc_list;
Thomas Graf88d6ed12015-01-02 23:00:16 +0100104
Herbert Xuad202072016-08-19 16:21:37 +0800105 num--;
Thomas Grafe3416942014-08-02 11:47:45 +0200106
Herbert Xuad202072016-08-19 16:21:37 +0800107 if (!hti) {
108 hti = kmalloc(sizeof(*hti), GFP_KERNEL);
109 if (!hti)
110 return -ENOMEM;
111
112 cb->args[2] = (long)hti;
113 }
114
115 if (!s_num)
116 rhashtable_walk_enter(&tbl->hash, hti);
117
118 ret = rhashtable_walk_start(hti);
119 if (ret == -EAGAIN)
120 ret = 0;
121 if (ret)
122 goto stop;
123
124 while ((nlsk = rhashtable_walk_next(hti))) {
125 if (IS_ERR(nlsk)) {
126 ret = PTR_ERR(nlsk);
127 if (ret == -EAGAIN) {
128 ret = 0;
Andrey Vagineaaa3132013-03-21 20:33:48 +0400129 continue;
130 }
Herbert Xuad202072016-08-19 16:21:37 +0800131 break;
132 }
Andrey Vagineaaa3132013-03-21 20:33:48 +0400133
Herbert Xuad202072016-08-19 16:21:37 +0800134 sk = (struct sock *)nlsk;
Andrey Vagineaaa3132013-03-21 20:33:48 +0400135
Herbert Xuad202072016-08-19 16:21:37 +0800136 if (!net_eq(sock_net(sk), net))
137 continue;
138
139 if (sk_diag_fill(sk, skb, req,
140 NETLINK_CB(cb->skb).portid,
141 cb->nlh->nlmsg_seq,
142 NLM_F_MULTI,
143 sock_i_ino(sk)) < 0) {
144 ret = 1;
145 break;
Andrey Vagineaaa3132013-03-21 20:33:48 +0400146 }
147 }
148
Herbert Xuad202072016-08-19 16:21:37 +0800149stop:
150 rhashtable_walk_stop(hti);
151 if (ret)
152 goto done;
153
154 rhashtable_walk_exit(hti);
Herbert Xuad202072016-08-19 16:21:37 +0800155 num++;
156
157mc_list:
158 read_lock(&nl_table_lock);
Andrey Vagineaaa3132013-03-21 20:33:48 +0400159 sk_for_each_bound(sk, &tbl->mc_list) {
160 if (sk_hashed(sk))
161 continue;
162 if (!net_eq(sock_net(sk), net))
163 continue;
164 if (num < s_num) {
165 num++;
166 continue;
167 }
168
169 if (sk_diag_fill(sk, skb, req,
170 NETLINK_CB(cb->skb).portid,
171 cb->nlh->nlmsg_seq,
172 NLM_F_MULTI,
173 sock_i_ino(sk)) < 0) {
174 ret = 1;
Herbert Xuad202072016-08-19 16:21:37 +0800175 break;
Andrey Vagineaaa3132013-03-21 20:33:48 +0400176 }
177 num++;
178 }
Herbert Xuad202072016-08-19 16:21:37 +0800179 read_unlock(&nl_table_lock);
180
Andrey Vagineaaa3132013-03-21 20:33:48 +0400181done:
182 cb->args[0] = num;
Andrey Vagineaaa3132013-03-21 20:33:48 +0400183
184 return ret;
185}
186
187static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
188{
189 struct netlink_diag_req *req;
190 int s_num = cb->args[0];
Herbert Xuad202072016-08-19 16:21:37 +0800191 int err = 0;
Andrey Vagineaaa3132013-03-21 20:33:48 +0400192
193 req = nlmsg_data(cb->nlh);
194
Andrey Vagineaaa3132013-03-21 20:33:48 +0400195 if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
196 int i;
197
198 for (i = cb->args[1]; i < MAX_LINKS; i++) {
Herbert Xuad202072016-08-19 16:21:37 +0800199 err = __netlink_diag_dump(skb, cb, i, s_num);
200 if (err)
Andrey Vagineaaa3132013-03-21 20:33:48 +0400201 break;
202 s_num = 0;
203 }
Herbert Xuad202072016-08-19 16:21:37 +0800204 cb->args[1] = i;
Andrey Vagineaaa3132013-03-21 20:33:48 +0400205 } else {
Eric Dumazet93636d12016-11-02 20:21:20 -0700206 if (req->sdiag_protocol >= MAX_LINKS)
Andrey Vagineaaa3132013-03-21 20:33:48 +0400207 return -ENOENT;
Andrey Vagineaaa3132013-03-21 20:33:48 +0400208
Herbert Xuad202072016-08-19 16:21:37 +0800209 err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
Andrey Vagineaaa3132013-03-21 20:33:48 +0400210 }
211
Herbert Xuad202072016-08-19 16:21:37 +0800212 return err < 0 ? err : skb->len;
213}
Andrey Vagineaaa3132013-03-21 20:33:48 +0400214
Herbert Xuad202072016-08-19 16:21:37 +0800215static int netlink_diag_dump_done(struct netlink_callback *cb)
216{
217 struct rhashtable_iter *hti = (void *)cb->args[2];
218
219 if (cb->args[0] == 1)
220 rhashtable_walk_exit(hti);
221
222 kfree(hti);
223
224 return 0;
Andrey Vagineaaa3132013-03-21 20:33:48 +0400225}
226
227static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
228{
229 int hdrlen = sizeof(struct netlink_diag_req);
230 struct net *net = sock_net(skb->sk);
231
232 if (nlmsg_len(h) < hdrlen)
233 return -EINVAL;
234
235 if (h->nlmsg_flags & NLM_F_DUMP) {
236 struct netlink_dump_control c = {
237 .dump = netlink_diag_dump,
Herbert Xuad202072016-08-19 16:21:37 +0800238 .done = netlink_diag_dump_done,
Andrey Vagineaaa3132013-03-21 20:33:48 +0400239 };
240 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
241 } else
242 return -EOPNOTSUPP;
243}
244
245static const struct sock_diag_handler netlink_diag_handler = {
246 .family = AF_NETLINK,
247 .dump = netlink_diag_handler_dump,
248};
249
250static int __init netlink_diag_init(void)
251{
252 return sock_diag_register(&netlink_diag_handler);
253}
254
255static void __exit netlink_diag_exit(void)
256{
257 sock_diag_unregister(&netlink_diag_handler);
258}
259
260module_init(netlink_diag_init);
261module_exit(netlink_diag_exit);
262MODULE_LICENSE("GPL");
263MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);