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_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
47 u32 pid, u32 seq, u32 flags, int sk_ino)
49 unsigned char *b = skb_tail_pointer(skb);
51 struct unix_diag_msg *rep;
53 nlh = NLMSG_PUT(skb, pid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep));
54 nlh->nlmsg_flags = flags;
56 rep = NLMSG_DATA(nlh);
58 rep->udiag_family = AF_UNIX;
59 rep->udiag_type = sk->sk_type;
60 rep->udiag_state = sk->sk_state;
61 rep->udiag_ino = sk_ino;
62 sock_diag_save_cookie(sk, rep->udiag_cookie);
64 if ((req->udiag_show & UDIAG_SHOW_NAME) &&
65 sk_diag_dump_name(sk, skb))
68 if ((req->udiag_show & UDIAG_SHOW_VFS) &&
69 sk_diag_dump_vfs(sk, skb))
72 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
80 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
81 u32 pid, u32 seq, u32 flags)
86 sk_ino = sock_i_ino(sk);
87 unix_state_unlock(sk);
92 return sk_diag_fill(sk, skb, req, pid, seq, flags, sk_ino);
95 static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
97 struct unix_diag_req *req;
98 int num, s_num, slot, s_slot;
100 req = NLMSG_DATA(cb->nlh);
102 s_slot = cb->args[0];
103 num = s_num = cb->args[1];
105 spin_lock(&unix_table_lock);
106 for (slot = s_slot; slot <= UNIX_HASH_SIZE; s_num = 0, slot++) {
108 struct hlist_node *node;
111 sk_for_each(sk, node, &unix_socket_table[slot]) {
114 if (!(req->udiag_states & (1 << sk->sk_state)))
116 if (sk_diag_dump(sk, skb, req,
117 NETLINK_CB(cb->skb).pid,
126 spin_unlock(&unix_table_lock);
133 static struct sock *unix_lookup_by_ino(int ino)
138 spin_lock(&unix_table_lock);
139 for (i = 0; i <= UNIX_HASH_SIZE; i++) {
140 struct hlist_node *node;
142 sk_for_each(sk, node, &unix_socket_table[i])
143 if (ino == sock_i_ino(sk)) {
145 spin_unlock(&unix_table_lock);
151 spin_unlock(&unix_table_lock);
155 static int unix_diag_get_exact(struct sk_buff *in_skb,
156 const struct nlmsghdr *nlh,
157 struct unix_diag_req *req)
162 unsigned int extra_len;
164 if (req->udiag_ino == 0)
167 sk = unix_lookup_by_ino(req->udiag_ino);
172 err = sock_diag_check_cookie(sk, req->udiag_cookie);
179 rep = alloc_skb(NLMSG_SPACE((sizeof(struct unix_diag_msg) + extra_len)),
184 err = sk_diag_fill(sk, rep, req, NETLINK_CB(in_skb).pid,
185 nlh->nlmsg_seq, 0, req->udiag_ino);
189 if (extra_len >= PAGE_SIZE)
194 err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
205 static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
207 int hdrlen = sizeof(struct unix_diag_req);
209 if (nlmsg_len(h) < hdrlen)
212 if (h->nlmsg_flags & NLM_F_DUMP)
213 return netlink_dump_start(sock_diag_nlsk, skb, h,
214 unix_diag_dump, NULL, 0);
216 return unix_diag_get_exact(skb, h, (struct unix_diag_req *)NLMSG_DATA(h));
219 static struct sock_diag_handler unix_diag_handler = {
221 .dump = unix_diag_handler_dump,
224 static int __init unix_diag_init(void)
226 return sock_diag_register(&unix_diag_handler);
229 static void __exit unix_diag_exit(void)
231 sock_diag_unregister(&unix_diag_handler);
234 module_init(unix_diag_init);
235 module_exit(unix_diag_exit);
236 MODULE_LICENSE("GPL");
237 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 1 /* AF_LOCAL */);