diag.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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>
  9. #define UNIX_DIAG_PUT(skb, attrtype, attrlen) \
  10. RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
  11. static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
  12. {
  13. struct unix_address *addr = unix_sk(sk)->addr;
  14. char *s;
  15. if (addr) {
  16. s = UNIX_DIAG_PUT(nlskb, UNIX_DIAG_NAME, addr->len - sizeof(short));
  17. memcpy(s, addr->name->sun_path, addr->len - sizeof(short));
  18. }
  19. return 0;
  20. rtattr_failure:
  21. return -EMSGSIZE;
  22. }
  23. static int sk_diag_dump_vfs(struct sock *sk, struct sk_buff *nlskb)
  24. {
  25. struct dentry *dentry = unix_sk(sk)->dentry;
  26. struct unix_diag_vfs *uv;
  27. if (dentry) {
  28. uv = UNIX_DIAG_PUT(nlskb, UNIX_DIAG_VFS, sizeof(*uv));
  29. uv->udiag_vfs_ino = dentry->d_inode->i_ino;
  30. uv->udiag_vfs_dev = dentry->d_sb->s_dev;
  31. }
  32. return 0;
  33. rtattr_failure:
  34. return -EMSGSIZE;
  35. }
  36. static int sk_diag_dump_peer(struct sock *sk, struct sk_buff *nlskb)
  37. {
  38. struct sock *peer;
  39. int ino;
  40. peer = unix_peer_get(sk);
  41. if (peer) {
  42. unix_state_lock(peer);
  43. ino = sock_i_ino(peer);
  44. unix_state_unlock(peer);
  45. sock_put(peer);
  46. RTA_PUT_U32(nlskb, UNIX_DIAG_PEER, ino);
  47. }
  48. return 0;
  49. rtattr_failure:
  50. return -EMSGSIZE;
  51. }
  52. static int sk_diag_dump_icons(struct sock *sk, struct sk_buff *nlskb)
  53. {
  54. struct sk_buff *skb;
  55. u32 *buf;
  56. int i;
  57. if (sk->sk_state == TCP_LISTEN) {
  58. spin_lock(&sk->sk_receive_queue.lock);
  59. buf = UNIX_DIAG_PUT(nlskb, UNIX_DIAG_ICONS, sk->sk_receive_queue.qlen);
  60. i = 0;
  61. skb_queue_walk(&sk->sk_receive_queue, skb) {
  62. struct sock *req, *peer;
  63. req = skb->sk;
  64. /*
  65. * The state lock is outer for the same sk's
  66. * queue lock. With the other's queue locked it's
  67. * OK to lock the state.
  68. */
  69. unix_state_lock_nested(req);
  70. peer = unix_sk(req)->peer;
  71. if (peer)
  72. buf[i++] = sock_i_ino(peer);
  73. unix_state_unlock(req);
  74. }
  75. spin_unlock(&sk->sk_receive_queue.lock);
  76. }
  77. return 0;
  78. rtattr_failure:
  79. spin_unlock(&sk->sk_receive_queue.lock);
  80. return -EMSGSIZE;
  81. }
  82. static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
  83. u32 pid, u32 seq, u32 flags, int sk_ino)
  84. {
  85. unsigned char *b = skb_tail_pointer(skb);
  86. struct nlmsghdr *nlh;
  87. struct unix_diag_msg *rep;
  88. nlh = NLMSG_PUT(skb, pid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep));
  89. nlh->nlmsg_flags = flags;
  90. rep = NLMSG_DATA(nlh);
  91. rep->udiag_family = AF_UNIX;
  92. rep->udiag_type = sk->sk_type;
  93. rep->udiag_state = sk->sk_state;
  94. rep->udiag_ino = sk_ino;
  95. sock_diag_save_cookie(sk, rep->udiag_cookie);
  96. if ((req->udiag_show & UDIAG_SHOW_NAME) &&
  97. sk_diag_dump_name(sk, skb))
  98. goto nlmsg_failure;
  99. if ((req->udiag_show & UDIAG_SHOW_VFS) &&
  100. sk_diag_dump_vfs(sk, skb))
  101. goto nlmsg_failure;
  102. if ((req->udiag_show & UDIAG_SHOW_PEER) &&
  103. sk_diag_dump_peer(sk, skb))
  104. goto nlmsg_failure;
  105. if ((req->udiag_show & UDIAG_SHOW_ICONS) &&
  106. sk_diag_dump_icons(sk, skb))
  107. goto nlmsg_failure;
  108. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  109. return skb->len;
  110. nlmsg_failure:
  111. nlmsg_trim(skb, b);
  112. return -EMSGSIZE;
  113. }
  114. static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
  115. u32 pid, u32 seq, u32 flags)
  116. {
  117. int sk_ino;
  118. unix_state_lock(sk);
  119. sk_ino = sock_i_ino(sk);
  120. unix_state_unlock(sk);
  121. if (!sk_ino)
  122. return 0;
  123. return sk_diag_fill(sk, skb, req, pid, seq, flags, sk_ino);
  124. }
  125. static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  126. {
  127. struct unix_diag_req *req;
  128. int num, s_num, slot, s_slot;
  129. req = NLMSG_DATA(cb->nlh);
  130. s_slot = cb->args[0];
  131. num = s_num = cb->args[1];
  132. spin_lock(&unix_table_lock);
  133. for (slot = s_slot; slot <= UNIX_HASH_SIZE; s_num = 0, slot++) {
  134. struct sock *sk;
  135. struct hlist_node *node;
  136. num = 0;
  137. sk_for_each(sk, node, &unix_socket_table[slot]) {
  138. if (num < s_num)
  139. goto next;
  140. if (!(req->udiag_states & (1 << sk->sk_state)))
  141. goto next;
  142. if (sk_diag_dump(sk, skb, req,
  143. NETLINK_CB(cb->skb).pid,
  144. cb->nlh->nlmsg_seq,
  145. NLM_F_MULTI) < 0)
  146. goto done;
  147. next:
  148. num++;
  149. }
  150. }
  151. done:
  152. spin_unlock(&unix_table_lock);
  153. cb->args[0] = slot;
  154. cb->args[1] = num;
  155. return skb->len;
  156. }
  157. static struct sock *unix_lookup_by_ino(int ino)
  158. {
  159. int i;
  160. struct sock *sk;
  161. spin_lock(&unix_table_lock);
  162. for (i = 0; i <= UNIX_HASH_SIZE; i++) {
  163. struct hlist_node *node;
  164. sk_for_each(sk, node, &unix_socket_table[i])
  165. if (ino == sock_i_ino(sk)) {
  166. sock_hold(sk);
  167. spin_unlock(&unix_table_lock);
  168. return sk;
  169. }
  170. }
  171. spin_unlock(&unix_table_lock);
  172. return NULL;
  173. }
  174. static int unix_diag_get_exact(struct sk_buff *in_skb,
  175. const struct nlmsghdr *nlh,
  176. struct unix_diag_req *req)
  177. {
  178. int err = -EINVAL;
  179. struct sock *sk;
  180. struct sk_buff *rep;
  181. unsigned int extra_len;
  182. if (req->udiag_ino == 0)
  183. goto out_nosk;
  184. sk = unix_lookup_by_ino(req->udiag_ino);
  185. err = -ENOENT;
  186. if (sk == NULL)
  187. goto out_nosk;
  188. err = sock_diag_check_cookie(sk, req->udiag_cookie);
  189. if (err)
  190. goto out;
  191. extra_len = 256;
  192. again:
  193. err = -ENOMEM;
  194. rep = alloc_skb(NLMSG_SPACE((sizeof(struct unix_diag_msg) + extra_len)),
  195. GFP_KERNEL);
  196. if (!rep)
  197. goto out;
  198. err = sk_diag_fill(sk, rep, req, NETLINK_CB(in_skb).pid,
  199. nlh->nlmsg_seq, 0, req->udiag_ino);
  200. if (err < 0) {
  201. kfree_skb(rep);
  202. extra_len += 256;
  203. if (extra_len >= PAGE_SIZE)
  204. goto out;
  205. goto again;
  206. }
  207. err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
  208. MSG_DONTWAIT);
  209. if (err > 0)
  210. err = 0;
  211. out:
  212. if (sk)
  213. sock_put(sk);
  214. out_nosk:
  215. return err;
  216. }
  217. static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
  218. {
  219. int hdrlen = sizeof(struct unix_diag_req);
  220. if (nlmsg_len(h) < hdrlen)
  221. return -EINVAL;
  222. if (h->nlmsg_flags & NLM_F_DUMP)
  223. return netlink_dump_start(sock_diag_nlsk, skb, h,
  224. unix_diag_dump, NULL, 0);
  225. else
  226. return unix_diag_get_exact(skb, h, (struct unix_diag_req *)NLMSG_DATA(h));
  227. }
  228. static struct sock_diag_handler unix_diag_handler = {
  229. .family = AF_UNIX,
  230. .dump = unix_diag_handler_dump,
  231. };
  232. static int __init unix_diag_init(void)
  233. {
  234. return sock_diag_register(&unix_diag_handler);
  235. }
  236. static void __exit unix_diag_exit(void)
  237. {
  238. sock_diag_unregister(&unix_diag_handler);
  239. }
  240. module_init(unix_diag_init);
  241. module_exit(unix_diag_exit);
  242. MODULE_LICENSE("GPL");
  243. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 1 /* AF_LOCAL */);