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>
6 #include <net/netlink.h>
7 #include <net/af_unix.h>
8 #include <net/tcp_states.h>
10 #define UNIX_DIAG_PUT(skb, attrtype, attrlen) \
11 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
13 static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
15 struct unix_address *addr = unix_sk(sk)->addr;
19 s = UNIX_DIAG_PUT(nlskb, UNIX_DIAG_NAME, addr->len - sizeof(short));
20 memcpy(s, addr->name->sun_path, addr->len - sizeof(short));
29 static int sk_diag_dump_vfs(struct sock *sk, struct sk_buff *nlskb)
31 struct dentry *dentry = unix_sk(sk)->dentry;
32 struct unix_diag_vfs *uv;
35 uv = UNIX_DIAG_PUT(nlskb, UNIX_DIAG_VFS, sizeof(*uv));
36 uv->udiag_vfs_ino = dentry->d_inode->i_ino;
37 uv->udiag_vfs_dev = dentry->d_sb->s_dev;
46 static int sk_diag_dump_peer(struct sock *sk, struct sk_buff *nlskb)
51 peer = unix_peer_get(sk);
53 unix_state_lock(peer);
54 ino = sock_i_ino(peer);
55 unix_state_unlock(peer);
58 RTA_PUT_U32(nlskb, UNIX_DIAG_PEER, ino);
66 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
67 u32 pid, u32 seq, u32 flags, int sk_ino)
69 unsigned char *b = skb_tail_pointer(skb);
71 struct unix_diag_msg *rep;
73 nlh = NLMSG_PUT(skb, pid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep));
74 nlh->nlmsg_flags = flags;
76 rep = NLMSG_DATA(nlh);
78 rep->udiag_family = AF_UNIX;
79 rep->udiag_type = sk->sk_type;
80 rep->udiag_state = sk->sk_state;
81 rep->udiag_ino = sk_ino;
82 sock_diag_save_cookie(sk, rep->udiag_cookie);
84 if ((req->udiag_show & UDIAG_SHOW_NAME) &&
85 sk_diag_dump_name(sk, skb))
88 if ((req->udiag_show & UDIAG_SHOW_VFS) &&
89 sk_diag_dump_vfs(sk, skb))
92 if ((req->udiag_show & UDIAG_SHOW_PEER) &&
93 sk_diag_dump_peer(sk, skb))
96 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
104 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
105 u32 pid, u32 seq, u32 flags)
110 sk_ino = sock_i_ino(sk);
111 unix_state_unlock(sk);
116 return sk_diag_fill(sk, skb, req, pid, seq, flags, sk_ino);
119 static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
121 struct unix_diag_req *req;
122 int num, s_num, slot, s_slot;
124 req = NLMSG_DATA(cb->nlh);
126 s_slot = cb->args[0];
127 num = s_num = cb->args[1];
129 spin_lock(&unix_table_lock);
130 for (slot = s_slot; slot <= UNIX_HASH_SIZE; s_num = 0, slot++) {
132 struct hlist_node *node;
135 sk_for_each(sk, node, &unix_socket_table[slot]) {
138 if (!(req->udiag_states & (1 << sk->sk_state)))
140 if (sk_diag_dump(sk, skb, req,
141 NETLINK_CB(cb->skb).pid,
150 spin_unlock(&unix_table_lock);
157 static struct sock *unix_lookup_by_ino(int ino)
162 spin_lock(&unix_table_lock);
163 for (i = 0; i <= UNIX_HASH_SIZE; i++) {
164 struct hlist_node *node;
166 sk_for_each(sk, node, &unix_socket_table[i])
167 if (ino == sock_i_ino(sk)) {
169 spin_unlock(&unix_table_lock);
175 spin_unlock(&unix_table_lock);
179 static int unix_diag_get_exact(struct sk_buff *in_skb,
180 const struct nlmsghdr *nlh,
181 struct unix_diag_req *req)
186 unsigned int extra_len;
188 if (req->udiag_ino == 0)
191 sk = unix_lookup_by_ino(req->udiag_ino);
196 err = sock_diag_check_cookie(sk, req->udiag_cookie);
203 rep = alloc_skb(NLMSG_SPACE((sizeof(struct unix_diag_msg) + extra_len)),
208 err = sk_diag_fill(sk, rep, req, NETLINK_CB(in_skb).pid,
209 nlh->nlmsg_seq, 0, req->udiag_ino);
213 if (extra_len >= PAGE_SIZE)
218 err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
229 static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
231 int hdrlen = sizeof(struct unix_diag_req);
233 if (nlmsg_len(h) < hdrlen)
236 if (h->nlmsg_flags & NLM_F_DUMP)
237 return netlink_dump_start(sock_diag_nlsk, skb, h,
238 unix_diag_dump, NULL, 0);
240 return unix_diag_get_exact(skb, h, (struct unix_diag_req *)NLMSG_DATA(h));
243 static struct sock_diag_handler unix_diag_handler = {
245 .dump = unix_diag_handler_dump,
248 static int __init unix_diag_init(void)
250 return sock_diag_register(&unix_diag_handler);
253 static void __exit unix_diag_exit(void)
255 sock_diag_unregister(&unix_diag_handler);
258 module_init(unix_diag_init);
259 module_exit(unix_diag_exit);
260 MODULE_LICENSE("GPL");
261 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 1 /* AF_LOCAL */);