diag.c 7.0 KB

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