netlink: Use rhashtable walk interface in diag dump
[linux-2.6-block.git] / net / netlink / diag.c
CommitLineData
eaaa3139
AV
1#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>
e341694e 7#include <linux/rhashtable.h>
eaaa3139
AV
8
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
22static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
23 struct netlink_diag_req *req,
24 u32 portid, u32 seq, u32 flags, int sk_ino)
25{
26 struct nlmsghdr *nlh;
27 struct netlink_diag_msg *rep;
28 struct netlink_sock *nlk = nlk_sk(sk);
29
30 nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
31 flags);
32 if (!nlh)
33 return -EMSGSIZE;
34
35 rep = nlmsg_data(nlh);
36 rep->ndiag_family = AF_NETLINK;
37 rep->ndiag_type = sk->sk_type;
38 rep->ndiag_protocol = sk->sk_protocol;
39 rep->ndiag_state = sk->sk_state;
40
41 rep->ndiag_ino = sk_ino;
42 rep->ndiag_portid = nlk->portid;
43 rep->ndiag_dst_portid = nlk->dst_portid;
44 rep->ndiag_dst_group = nlk->dst_group;
45 sock_diag_save_cookie(sk, rep->ndiag_cookie);
46
47 if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
48 sk_diag_dump_groups(sk, skb))
49 goto out_nlmsg_trim;
50
51 if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
52 sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
53 goto out_nlmsg_trim;
54
053c095a
JB
55 nlmsg_end(skb, nlh);
56 return 0;
eaaa3139
AV
57
58out_nlmsg_trim:
59 nlmsg_cancel(skb, nlh);
60 return -EMSGSIZE;
61}
62
63static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
64 int protocol, int s_num)
65{
ad202074 66 struct rhashtable_iter *hti = (void *)cb->args[2];
eaaa3139 67 struct netlink_table *tbl = &nl_table[protocol];
eaaa3139
AV
68 struct net *net = sock_net(skb->sk);
69 struct netlink_diag_req *req;
e341694e 70 struct netlink_sock *nlsk;
eaaa3139 71 struct sock *sk;
ad202074
HX
72 int num = 2;
73 int ret = 0;
eaaa3139
AV
74
75 req = nlmsg_data(cb->nlh);
76
ad202074
HX
77 if (s_num > 1)
78 goto mc_list;
88d6ed15 79
ad202074 80 num--;
e341694e 81
ad202074
HX
82 if (!hti) {
83 hti = kmalloc(sizeof(*hti), GFP_KERNEL);
84 if (!hti)
85 return -ENOMEM;
86
87 cb->args[2] = (long)hti;
88 }
89
90 if (!s_num)
91 rhashtable_walk_enter(&tbl->hash, hti);
92
93 ret = rhashtable_walk_start(hti);
94 if (ret == -EAGAIN)
95 ret = 0;
96 if (ret)
97 goto stop;
98
99 while ((nlsk = rhashtable_walk_next(hti))) {
100 if (IS_ERR(nlsk)) {
101 ret = PTR_ERR(nlsk);
102 if (ret == -EAGAIN) {
103 ret = 0;
eaaa3139
AV
104 continue;
105 }
ad202074
HX
106 break;
107 }
eaaa3139 108
ad202074 109 sk = (struct sock *)nlsk;
eaaa3139 110
ad202074
HX
111 if (!net_eq(sock_net(sk), net))
112 continue;
113
114 if (sk_diag_fill(sk, skb, req,
115 NETLINK_CB(cb->skb).portid,
116 cb->nlh->nlmsg_seq,
117 NLM_F_MULTI,
118 sock_i_ino(sk)) < 0) {
119 ret = 1;
120 break;
eaaa3139
AV
121 }
122 }
123
ad202074
HX
124stop:
125 rhashtable_walk_stop(hti);
126 if (ret)
127 goto done;
128
129 rhashtable_walk_exit(hti);
130 cb->args[2] = 0;
131 num++;
132
133mc_list:
134 read_lock(&nl_table_lock);
eaaa3139
AV
135 sk_for_each_bound(sk, &tbl->mc_list) {
136 if (sk_hashed(sk))
137 continue;
138 if (!net_eq(sock_net(sk), net))
139 continue;
140 if (num < s_num) {
141 num++;
142 continue;
143 }
144
145 if (sk_diag_fill(sk, skb, req,
146 NETLINK_CB(cb->skb).portid,
147 cb->nlh->nlmsg_seq,
148 NLM_F_MULTI,
149 sock_i_ino(sk)) < 0) {
150 ret = 1;
ad202074 151 break;
eaaa3139
AV
152 }
153 num++;
154 }
ad202074
HX
155 read_unlock(&nl_table_lock);
156
eaaa3139
AV
157done:
158 cb->args[0] = num;
eaaa3139
AV
159
160 return ret;
161}
162
163static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
164{
165 struct netlink_diag_req *req;
166 int s_num = cb->args[0];
ad202074 167 int err = 0;
eaaa3139
AV
168
169 req = nlmsg_data(cb->nlh);
170
eaaa3139
AV
171 if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
172 int i;
173
174 for (i = cb->args[1]; i < MAX_LINKS; i++) {
ad202074
HX
175 err = __netlink_diag_dump(skb, cb, i, s_num);
176 if (err)
eaaa3139
AV
177 break;
178 s_num = 0;
179 }
ad202074 180 cb->args[1] = i;
eaaa3139
AV
181 } else {
182 if (req->sdiag_protocol >= MAX_LINKS) {
183 read_unlock(&nl_table_lock);
c5adde94 184 rcu_read_unlock();
eaaa3139
AV
185 return -ENOENT;
186 }
187
ad202074 188 err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
eaaa3139
AV
189 }
190
ad202074
HX
191 return err < 0 ? err : skb->len;
192}
193
194static int netlink_diag_dump_done(struct netlink_callback *cb)
195{
196 struct rhashtable_iter *hti = (void *)cb->args[2];
197
198 if (cb->args[0] == 1)
199 rhashtable_walk_exit(hti);
eaaa3139 200
ad202074
HX
201 kfree(hti);
202
203 return 0;
eaaa3139
AV
204}
205
206static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
207{
208 int hdrlen = sizeof(struct netlink_diag_req);
209 struct net *net = sock_net(skb->sk);
210
211 if (nlmsg_len(h) < hdrlen)
212 return -EINVAL;
213
214 if (h->nlmsg_flags & NLM_F_DUMP) {
215 struct netlink_dump_control c = {
216 .dump = netlink_diag_dump,
ad202074 217 .done = netlink_diag_dump_done,
eaaa3139
AV
218 };
219 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
220 } else
221 return -EOPNOTSUPP;
222}
223
224static const struct sock_diag_handler netlink_diag_handler = {
225 .family = AF_NETLINK,
226 .dump = netlink_diag_handler_dump,
227};
228
229static int __init netlink_diag_init(void)
230{
231 return sock_diag_register(&netlink_diag_handler);
232}
233
234static void __exit netlink_diag_exit(void)
235{
236 sock_diag_unregister(&netlink_diag_handler);
237}
238
239module_init(netlink_diag_init);
240module_exit(netlink_diag_exit);
241MODULE_LICENSE("GPL");
242MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);