inet_diag: Add the SKMEMINFO extension
[deliverable/linux.git] / net / unix / diag.c
CommitLineData
22931d3b
PE
1#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>
2ea744a5 6#include <linux/module.h>
22931d3b
PE
7#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
f5248b48
PE
14static 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
5f7b0569
PE
30static int sk_diag_dump_vfs(struct sock *sk, struct sk_buff *nlskb)
31{
32 struct dentry *dentry = unix_sk(sk)->dentry;
33 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
ac02be8d
PE
47static 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
2aac7a2c
PE
67static 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);
3b0723c1
PE
75 buf = UNIX_DIAG_PUT(nlskb, UNIX_DIAG_ICONS,
76 sk->sk_receive_queue.qlen * sizeof(u32));
2aac7a2c
PE
77 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;
e09e9d18 89 buf[i++] = (peer ? sock_i_ino(peer) : 0);
2aac7a2c
PE
90 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
cbf39195
PE
102static int sk_diag_show_rqlen(struct sock *sk, struct sk_buff *nlskb)
103{
104 RTA_PUT_U32(nlskb, UNIX_DIAG_RQLEN, sk->sk_receive_queue.qlen);
105 return 0;
106
107rtattr_failure:
108 return -EMSGSIZE;
109}
110
45a96b9b
PE
111static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
112 u32 pid, u32 seq, u32 flags, int sk_ino)
113{
114 unsigned char *b = skb_tail_pointer(skb);
115 struct nlmsghdr *nlh;
116 struct unix_diag_msg *rep;
117
118 nlh = NLMSG_PUT(skb, pid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep));
119 nlh->nlmsg_flags = flags;
120
121 rep = NLMSG_DATA(nlh);
122
123 rep->udiag_family = AF_UNIX;
124 rep->udiag_type = sk->sk_type;
125 rep->udiag_state = sk->sk_state;
126 rep->udiag_ino = sk_ino;
127 sock_diag_save_cookie(sk, rep->udiag_cookie);
128
f5248b48
PE
129 if ((req->udiag_show & UDIAG_SHOW_NAME) &&
130 sk_diag_dump_name(sk, skb))
131 goto nlmsg_failure;
132
5f7b0569
PE
133 if ((req->udiag_show & UDIAG_SHOW_VFS) &&
134 sk_diag_dump_vfs(sk, skb))
135 goto nlmsg_failure;
136
ac02be8d
PE
137 if ((req->udiag_show & UDIAG_SHOW_PEER) &&
138 sk_diag_dump_peer(sk, skb))
139 goto nlmsg_failure;
140
2aac7a2c
PE
141 if ((req->udiag_show & UDIAG_SHOW_ICONS) &&
142 sk_diag_dump_icons(sk, skb))
143 goto nlmsg_failure;
144
cbf39195
PE
145 if ((req->udiag_show & UDIAG_SHOW_RQLEN) &&
146 sk_diag_show_rqlen(sk, skb))
147 goto nlmsg_failure;
148
45a96b9b
PE
149 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
150 return skb->len;
151
152nlmsg_failure:
153 nlmsg_trim(skb, b);
154 return -EMSGSIZE;
155}
156
157static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
158 u32 pid, u32 seq, u32 flags)
159{
160 int sk_ino;
161
162 unix_state_lock(sk);
163 sk_ino = sock_i_ino(sk);
164 unix_state_unlock(sk);
165
166 if (!sk_ino)
167 return 0;
168
169 return sk_diag_fill(sk, skb, req, pid, seq, flags, sk_ino);
170}
171
22931d3b
PE
172static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
173{
45a96b9b
PE
174 struct unix_diag_req *req;
175 int num, s_num, slot, s_slot;
176
177 req = NLMSG_DATA(cb->nlh);
178
179 s_slot = cb->args[0];
180 num = s_num = cb->args[1];
181
182 spin_lock(&unix_table_lock);
183 for (slot = s_slot; slot <= UNIX_HASH_SIZE; s_num = 0, slot++) {
184 struct sock *sk;
185 struct hlist_node *node;
186
187 num = 0;
188 sk_for_each(sk, node, &unix_socket_table[slot]) {
189 if (num < s_num)
190 goto next;
191 if (!(req->udiag_states & (1 << sk->sk_state)))
192 goto next;
193 if (sk_diag_dump(sk, skb, req,
194 NETLINK_CB(cb->skb).pid,
195 cb->nlh->nlmsg_seq,
196 NLM_F_MULTI) < 0)
197 goto done;
198next:
199 num++;
200 }
201 }
202done:
203 spin_unlock(&unix_table_lock);
204 cb->args[0] = slot;
205 cb->args[1] = num;
206
207 return skb->len;
22931d3b
PE
208}
209
5d3cae8b
PE
210static struct sock *unix_lookup_by_ino(int ino)
211{
212 int i;
213 struct sock *sk;
214
215 spin_lock(&unix_table_lock);
216 for (i = 0; i <= UNIX_HASH_SIZE; i++) {
217 struct hlist_node *node;
218
219 sk_for_each(sk, node, &unix_socket_table[i])
220 if (ino == sock_i_ino(sk)) {
221 sock_hold(sk);
222 spin_unlock(&unix_table_lock);
223
224 return sk;
225 }
226 }
227
228 spin_unlock(&unix_table_lock);
229 return NULL;
230}
231
22931d3b
PE
232static int unix_diag_get_exact(struct sk_buff *in_skb,
233 const struct nlmsghdr *nlh,
234 struct unix_diag_req *req)
235{
5d3cae8b
PE
236 int err = -EINVAL;
237 struct sock *sk;
238 struct sk_buff *rep;
239 unsigned int extra_len;
240
241 if (req->udiag_ino == 0)
242 goto out_nosk;
243
244 sk = unix_lookup_by_ino(req->udiag_ino);
245 err = -ENOENT;
246 if (sk == NULL)
247 goto out_nosk;
248
249 err = sock_diag_check_cookie(sk, req->udiag_cookie);
250 if (err)
251 goto out;
252
253 extra_len = 256;
254again:
255 err = -ENOMEM;
256 rep = alloc_skb(NLMSG_SPACE((sizeof(struct unix_diag_msg) + extra_len)),
257 GFP_KERNEL);
258 if (!rep)
259 goto out;
260
261 err = sk_diag_fill(sk, rep, req, NETLINK_CB(in_skb).pid,
262 nlh->nlmsg_seq, 0, req->udiag_ino);
263 if (err < 0) {
264 kfree_skb(rep);
265 extra_len += 256;
266 if (extra_len >= PAGE_SIZE)
267 goto out;
268
269 goto again;
270 }
271 err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
272 MSG_DONTWAIT);
273 if (err > 0)
274 err = 0;
275out:
276 if (sk)
277 sock_put(sk);
278out_nosk:
279 return err;
22931d3b
PE
280}
281
282static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
283{
284 int hdrlen = sizeof(struct unix_diag_req);
285
286 if (nlmsg_len(h) < hdrlen)
287 return -EINVAL;
288
289 if (h->nlmsg_flags & NLM_F_DUMP)
290 return netlink_dump_start(sock_diag_nlsk, skb, h,
291 unix_diag_dump, NULL, 0);
292 else
293 return unix_diag_get_exact(skb, h, (struct unix_diag_req *)NLMSG_DATA(h));
294}
295
296static struct sock_diag_handler unix_diag_handler = {
297 .family = AF_UNIX,
298 .dump = unix_diag_handler_dump,
299};
300
301static int __init unix_diag_init(void)
302{
303 return sock_diag_register(&unix_diag_handler);
304}
305
306static void __exit unix_diag_exit(void)
307{
308 sock_diag_unregister(&unix_diag_handler);
309}
310
311module_init(unix_diag_init);
312module_exit(unix_diag_exit);
313MODULE_LICENSE("GPL");
314MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 1 /* AF_LOCAL */);
This page took 0.040205 seconds and 5 git commands to generate.