diag.c 6.6 KB

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