diag.c 7.1 KB

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