blob: 977ca317550d5a67e35f99f5a313abe236f6440c [file] [log] [blame]
Pavel Emelyanov22931d32011-12-15 02:44:35 +00001#include <linux/types.h>
2#include <linux/spinlock.h>
3#include <linux/sock_diag.h>
4#include <linux/unix_diag.h>
5#include <linux/skbuff.h>
Cyrill Gorcunov2ea744a2011-12-20 04:33:03 +00006#include <linux/module.h>
Pavel Emelyanov22931d32011-12-15 02:44:35 +00007#include <net/netlink.h>
8#include <net/af_unix.h>
9#include <net/tcp_states.h>
10
11#define UNIX_DIAG_PUT(skb, attrtype, attrlen) \
12 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
13
Pavel Emelyanovf5248b42011-12-15 02:45:24 +000014static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
15{
16 struct unix_address *addr = unix_sk(sk)->addr;
17 char *s;
18
19 if (addr) {
20 s = UNIX_DIAG_PUT(nlskb, UNIX_DIAG_NAME, addr->len - sizeof(short));
21 memcpy(s, addr->name->sun_path, addr->len - sizeof(short));
22 }
23
24 return 0;
25
26rtattr_failure:
27 return -EMSGSIZE;
28}
29
Pavel Emelyanov5f7b0562011-12-15 02:45:43 +000030static int sk_diag_dump_vfs(struct sock *sk, struct sk_buff *nlskb)
31{
Al Viro40ffe672012-03-14 21:54:32 -040032 struct dentry *dentry = unix_sk(sk)->path.dentry;
Pavel Emelyanov5f7b0562011-12-15 02:45:43 +000033 struct unix_diag_vfs *uv;
34
35 if (dentry) {
36 uv = UNIX_DIAG_PUT(nlskb, UNIX_DIAG_VFS, sizeof(*uv));
37 uv->udiag_vfs_ino = dentry->d_inode->i_ino;
38 uv->udiag_vfs_dev = dentry->d_sb->s_dev;
39 }
40
41 return 0;
42
43rtattr_failure:
44 return -EMSGSIZE;
45}
46
Pavel Emelyanovac02be82011-12-15 02:45:58 +000047static int sk_diag_dump_peer(struct sock *sk, struct sk_buff *nlskb)
48{
49 struct sock *peer;
50 int ino;
51
52 peer = unix_peer_get(sk);
53 if (peer) {
54 unix_state_lock(peer);
55 ino = sock_i_ino(peer);
56 unix_state_unlock(peer);
57 sock_put(peer);
58
59 RTA_PUT_U32(nlskb, UNIX_DIAG_PEER, ino);
60 }
61
62 return 0;
63rtattr_failure:
64 return -EMSGSIZE;
65}
66
Pavel Emelyanov2aac7a22011-12-15 02:46:14 +000067static int sk_diag_dump_icons(struct sock *sk, struct sk_buff *nlskb)
68{
69 struct sk_buff *skb;
70 u32 *buf;
71 int i;
72
73 if (sk->sk_state == TCP_LISTEN) {
74 spin_lock(&sk->sk_receive_queue.lock);
Pavel Emelyanov3b0723c2011-12-26 14:08:47 -050075 buf = UNIX_DIAG_PUT(nlskb, UNIX_DIAG_ICONS,
76 sk->sk_receive_queue.qlen * sizeof(u32));
Pavel Emelyanov2aac7a22011-12-15 02:46:14 +000077 i = 0;
78 skb_queue_walk(&sk->sk_receive_queue, skb) {
79 struct sock *req, *peer;
80
81 req = skb->sk;
82 /*
83 * The state lock is outer for the same sk's
84 * queue lock. With the other's queue locked it's
85 * OK to lock the state.
86 */
87 unix_state_lock_nested(req);
88 peer = unix_sk(req)->peer;
David S. Millere09e9d12011-12-26 14:41:55 -050089 buf[i++] = (peer ? sock_i_ino(peer) : 0);
Pavel Emelyanov2aac7a22011-12-15 02:46:14 +000090 unix_state_unlock(req);
91 }
92 spin_unlock(&sk->sk_receive_queue.lock);
93 }
94
95 return 0;
96
97rtattr_failure:
98 spin_unlock(&sk->sk_receive_queue.lock);
99 return -EMSGSIZE;
100}
101
Pavel Emelyanovcbf39192011-12-15 02:46:31 +0000102static int sk_diag_show_rqlen(struct sock *sk, struct sk_buff *nlskb)
103{
Pavel Emelyanovc9da99e2011-12-30 00:54:39 +0000104 struct unix_diag_rqlen *rql;
105
106 rql = UNIX_DIAG_PUT(nlskb, UNIX_DIAG_RQLEN, sizeof(*rql));
107
108 if (sk->sk_state == TCP_LISTEN) {
109 rql->udiag_rqueue = sk->sk_receive_queue.qlen;
110 rql->udiag_wqueue = sk->sk_max_ack_backlog;
111 } else {
112 rql->udiag_rqueue = (__u32)unix_inq_len(sk);
113 rql->udiag_wqueue = (__u32)unix_outq_len(sk);
114 }
115
Pavel Emelyanovcbf39192011-12-15 02:46:31 +0000116 return 0;
117
118rtattr_failure:
119 return -EMSGSIZE;
120}
121
Pavel Emelyanov45a96b92011-12-15 02:44:52 +0000122static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
123 u32 pid, u32 seq, u32 flags, int sk_ino)
124{
125 unsigned char *b = skb_tail_pointer(skb);
126 struct nlmsghdr *nlh;
127 struct unix_diag_msg *rep;
128
David S. Millerb61bb012012-06-26 21:41:00 -0700129 nlh = nlmsg_put(skb, pid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep), 0);
130 if (!nlh)
131 goto out_nlmsg_trim;
Pavel Emelyanov45a96b92011-12-15 02:44:52 +0000132 nlh->nlmsg_flags = flags;
133
David S. Millerb61bb012012-06-26 21:41:00 -0700134 rep = nlmsg_data(nlh);
Pavel Emelyanov45a96b92011-12-15 02:44:52 +0000135
136 rep->udiag_family = AF_UNIX;
137 rep->udiag_type = sk->sk_type;
138 rep->udiag_state = sk->sk_state;
139 rep->udiag_ino = sk_ino;
140 sock_diag_save_cookie(sk, rep->udiag_cookie);
141
Pavel Emelyanovf5248b42011-12-15 02:45:24 +0000142 if ((req->udiag_show & UDIAG_SHOW_NAME) &&
Pavel Emelyanov257b5292011-12-30 09:27:43 +0000143 sk_diag_dump_name(sk, skb))
David S. Millerb61bb012012-06-26 21:41:00 -0700144 goto out_nlmsg_trim;
Pavel Emelyanovf5248b42011-12-15 02:45:24 +0000145
Pavel Emelyanov5f7b0562011-12-15 02:45:43 +0000146 if ((req->udiag_show & UDIAG_SHOW_VFS) &&
Pavel Emelyanov257b5292011-12-30 09:27:43 +0000147 sk_diag_dump_vfs(sk, skb))
David S. Millerb61bb012012-06-26 21:41:00 -0700148 goto out_nlmsg_trim;
Pavel Emelyanov5f7b0562011-12-15 02:45:43 +0000149
Pavel Emelyanovac02be82011-12-15 02:45:58 +0000150 if ((req->udiag_show & UDIAG_SHOW_PEER) &&
Pavel Emelyanov257b5292011-12-30 09:27:43 +0000151 sk_diag_dump_peer(sk, skb))
David S. Millerb61bb012012-06-26 21:41:00 -0700152 goto out_nlmsg_trim;
Pavel Emelyanovac02be82011-12-15 02:45:58 +0000153
Pavel Emelyanov2aac7a22011-12-15 02:46:14 +0000154 if ((req->udiag_show & UDIAG_SHOW_ICONS) &&
Pavel Emelyanov257b5292011-12-30 09:27:43 +0000155 sk_diag_dump_icons(sk, skb))
David S. Millerb61bb012012-06-26 21:41:00 -0700156 goto out_nlmsg_trim;
Pavel Emelyanov2aac7a22011-12-15 02:46:14 +0000157
Pavel Emelyanovcbf39192011-12-15 02:46:31 +0000158 if ((req->udiag_show & UDIAG_SHOW_RQLEN) &&
Pavel Emelyanov257b5292011-12-30 09:27:43 +0000159 sk_diag_show_rqlen(sk, skb))
David S. Millerb61bb012012-06-26 21:41:00 -0700160 goto out_nlmsg_trim;
Pavel Emelyanov257b5292011-12-30 09:27:43 +0000161
162 if ((req->udiag_show & UDIAG_SHOW_MEMINFO) &&
163 sock_diag_put_meminfo(sk, skb, UNIX_DIAG_MEMINFO))
David S. Millerb61bb012012-06-26 21:41:00 -0700164 goto out_nlmsg_trim;
Pavel Emelyanovcbf39192011-12-15 02:46:31 +0000165
Pavel Emelyanov45a96b92011-12-15 02:44:52 +0000166 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
167 return skb->len;
168
David S. Millerb61bb012012-06-26 21:41:00 -0700169out_nlmsg_trim:
Pavel Emelyanov45a96b92011-12-15 02:44:52 +0000170 nlmsg_trim(skb, b);
171 return -EMSGSIZE;
172}
173
174static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
175 u32 pid, u32 seq, u32 flags)
176{
177 int sk_ino;
178
179 unix_state_lock(sk);
180 sk_ino = sock_i_ino(sk);
181 unix_state_unlock(sk);
182
183 if (!sk_ino)
184 return 0;
185
186 return sk_diag_fill(sk, skb, req, pid, seq, flags, sk_ino);
187}
188
Pavel Emelyanov22931d32011-12-15 02:44:35 +0000189static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
190{
Pavel Emelyanov45a96b92011-12-15 02:44:52 +0000191 struct unix_diag_req *req;
192 int num, s_num, slot, s_slot;
193
David S. Millerb61bb012012-06-26 21:41:00 -0700194 req = nlmsg_data(cb->nlh);
Pavel Emelyanov45a96b92011-12-15 02:44:52 +0000195
196 s_slot = cb->args[0];
197 num = s_num = cb->args[1];
198
199 spin_lock(&unix_table_lock);
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000200 for (slot = s_slot;
201 slot < ARRAY_SIZE(unix_socket_table);
202 s_num = 0, slot++) {
Pavel Emelyanov45a96b92011-12-15 02:44:52 +0000203 struct sock *sk;
204 struct hlist_node *node;
205
206 num = 0;
207 sk_for_each(sk, node, &unix_socket_table[slot]) {
208 if (num < s_num)
209 goto next;
210 if (!(req->udiag_states & (1 << sk->sk_state)))
211 goto next;
212 if (sk_diag_dump(sk, skb, req,
Pavel Emelyanov257b5292011-12-30 09:27:43 +0000213 NETLINK_CB(cb->skb).pid,
214 cb->nlh->nlmsg_seq,
215 NLM_F_MULTI) < 0)
Pavel Emelyanov45a96b92011-12-15 02:44:52 +0000216 goto done;
217next:
218 num++;
219 }
220 }
221done:
222 spin_unlock(&unix_table_lock);
223 cb->args[0] = slot;
224 cb->args[1] = num;
225
226 return skb->len;
Pavel Emelyanov22931d32011-12-15 02:44:35 +0000227}
228
Pavel Emelyanov5d3cae82011-12-15 02:45:07 +0000229static struct sock *unix_lookup_by_ino(int ino)
230{
231 int i;
232 struct sock *sk;
233
234 spin_lock(&unix_table_lock);
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000235 for (i = 0; i < ARRAY_SIZE(unix_socket_table); i++) {
Pavel Emelyanov5d3cae82011-12-15 02:45:07 +0000236 struct hlist_node *node;
237
238 sk_for_each(sk, node, &unix_socket_table[i])
239 if (ino == sock_i_ino(sk)) {
240 sock_hold(sk);
241 spin_unlock(&unix_table_lock);
242
243 return sk;
244 }
245 }
246
247 spin_unlock(&unix_table_lock);
248 return NULL;
249}
250
Pavel Emelyanov22931d32011-12-15 02:44:35 +0000251static int unix_diag_get_exact(struct sk_buff *in_skb,
252 const struct nlmsghdr *nlh,
253 struct unix_diag_req *req)
254{
Pavel Emelyanov5d3cae82011-12-15 02:45:07 +0000255 int err = -EINVAL;
256 struct sock *sk;
257 struct sk_buff *rep;
258 unsigned int extra_len;
259
260 if (req->udiag_ino == 0)
261 goto out_nosk;
262
263 sk = unix_lookup_by_ino(req->udiag_ino);
264 err = -ENOENT;
265 if (sk == NULL)
266 goto out_nosk;
267
268 err = sock_diag_check_cookie(sk, req->udiag_cookie);
269 if (err)
270 goto out;
271
272 extra_len = 256;
273again:
274 err = -ENOMEM;
275 rep = alloc_skb(NLMSG_SPACE((sizeof(struct unix_diag_msg) + extra_len)),
276 GFP_KERNEL);
277 if (!rep)
278 goto out;
279
280 err = sk_diag_fill(sk, rep, req, NETLINK_CB(in_skb).pid,
281 nlh->nlmsg_seq, 0, req->udiag_ino);
282 if (err < 0) {
283 kfree_skb(rep);
284 extra_len += 256;
285 if (extra_len >= PAGE_SIZE)
286 goto out;
287
288 goto again;
289 }
290 err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
291 MSG_DONTWAIT);
292 if (err > 0)
293 err = 0;
294out:
295 if (sk)
296 sock_put(sk);
297out_nosk:
298 return err;
Pavel Emelyanov22931d32011-12-15 02:44:35 +0000299}
300
301static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
302{
303 int hdrlen = sizeof(struct unix_diag_req);
304
305 if (nlmsg_len(h) < hdrlen)
306 return -EINVAL;
307
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000308 if (h->nlmsg_flags & NLM_F_DUMP) {
309 struct netlink_dump_control c = {
310 .dump = unix_diag_dump,
311 };
312 return netlink_dump_start(sock_diag_nlsk, skb, h, &c);
313 } else
David S. Millerb61bb012012-06-26 21:41:00 -0700314 return unix_diag_get_exact(skb, h, nlmsg_data(h));
Pavel Emelyanov22931d32011-12-15 02:44:35 +0000315}
316
Shan Wei8dcf01f2012-04-24 18:21:07 +0000317static const struct sock_diag_handler unix_diag_handler = {
Pavel Emelyanov22931d32011-12-15 02:44:35 +0000318 .family = AF_UNIX,
319 .dump = unix_diag_handler_dump,
320};
321
322static int __init unix_diag_init(void)
323{
324 return sock_diag_register(&unix_diag_handler);
325}
326
327static void __exit unix_diag_exit(void)
328{
329 sock_diag_unregister(&unix_diag_handler);
330}
331
332module_init(unix_diag_init);
333module_exit(unix_diag_exit);
334MODULE_LICENSE("GPL");
335MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 1 /* AF_LOCAL */);