diag.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
  37. u32 pid, u32 seq, u32 flags, int sk_ino)
  38. {
  39. unsigned char *b = skb_tail_pointer(skb);
  40. struct nlmsghdr *nlh;
  41. struct unix_diag_msg *rep;
  42. nlh = NLMSG_PUT(skb, pid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep));
  43. nlh->nlmsg_flags = flags;
  44. rep = NLMSG_DATA(nlh);
  45. rep->udiag_family = AF_UNIX;
  46. rep->udiag_type = sk->sk_type;
  47. rep->udiag_state = sk->sk_state;
  48. rep->udiag_ino = sk_ino;
  49. sock_diag_save_cookie(sk, rep->udiag_cookie);
  50. if ((req->udiag_show & UDIAG_SHOW_NAME) &&
  51. sk_diag_dump_name(sk, skb))
  52. goto nlmsg_failure;
  53. if ((req->udiag_show & UDIAG_SHOW_VFS) &&
  54. sk_diag_dump_vfs(sk, skb))
  55. goto nlmsg_failure;
  56. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  57. return skb->len;
  58. nlmsg_failure:
  59. nlmsg_trim(skb, b);
  60. return -EMSGSIZE;
  61. }
  62. static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
  63. u32 pid, u32 seq, u32 flags)
  64. {
  65. int sk_ino;
  66. unix_state_lock(sk);
  67. sk_ino = sock_i_ino(sk);
  68. unix_state_unlock(sk);
  69. if (!sk_ino)
  70. return 0;
  71. return sk_diag_fill(sk, skb, req, pid, seq, flags, sk_ino);
  72. }
  73. static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  74. {
  75. struct unix_diag_req *req;
  76. int num, s_num, slot, s_slot;
  77. req = NLMSG_DATA(cb->nlh);
  78. s_slot = cb->args[0];
  79. num = s_num = cb->args[1];
  80. spin_lock(&unix_table_lock);
  81. for (slot = s_slot; slot <= UNIX_HASH_SIZE; s_num = 0, slot++) {
  82. struct sock *sk;
  83. struct hlist_node *node;
  84. num = 0;
  85. sk_for_each(sk, node, &unix_socket_table[slot]) {
  86. if (num < s_num)
  87. goto next;
  88. if (!(req->udiag_states & (1 << sk->sk_state)))
  89. goto next;
  90. if (sk_diag_dump(sk, skb, req,
  91. NETLINK_CB(cb->skb).pid,
  92. cb->nlh->nlmsg_seq,
  93. NLM_F_MULTI) < 0)
  94. goto done;
  95. next:
  96. num++;
  97. }
  98. }
  99. done:
  100. spin_unlock(&unix_table_lock);
  101. cb->args[0] = slot;
  102. cb->args[1] = num;
  103. return skb->len;
  104. }
  105. static struct sock *unix_lookup_by_ino(int ino)
  106. {
  107. int i;
  108. struct sock *sk;
  109. spin_lock(&unix_table_lock);
  110. for (i = 0; i <= UNIX_HASH_SIZE; i++) {
  111. struct hlist_node *node;
  112. sk_for_each(sk, node, &unix_socket_table[i])
  113. if (ino == sock_i_ino(sk)) {
  114. sock_hold(sk);
  115. spin_unlock(&unix_table_lock);
  116. return sk;
  117. }
  118. }
  119. spin_unlock(&unix_table_lock);
  120. return NULL;
  121. }
  122. static int unix_diag_get_exact(struct sk_buff *in_skb,
  123. const struct nlmsghdr *nlh,
  124. struct unix_diag_req *req)
  125. {
  126. int err = -EINVAL;
  127. struct sock *sk;
  128. struct sk_buff *rep;
  129. unsigned int extra_len;
  130. if (req->udiag_ino == 0)
  131. goto out_nosk;
  132. sk = unix_lookup_by_ino(req->udiag_ino);
  133. err = -ENOENT;
  134. if (sk == NULL)
  135. goto out_nosk;
  136. err = sock_diag_check_cookie(sk, req->udiag_cookie);
  137. if (err)
  138. goto out;
  139. extra_len = 256;
  140. again:
  141. err = -ENOMEM;
  142. rep = alloc_skb(NLMSG_SPACE((sizeof(struct unix_diag_msg) + extra_len)),
  143. GFP_KERNEL);
  144. if (!rep)
  145. goto out;
  146. err = sk_diag_fill(sk, rep, req, NETLINK_CB(in_skb).pid,
  147. nlh->nlmsg_seq, 0, req->udiag_ino);
  148. if (err < 0) {
  149. kfree_skb(rep);
  150. extra_len += 256;
  151. if (extra_len >= PAGE_SIZE)
  152. goto out;
  153. goto again;
  154. }
  155. err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
  156. MSG_DONTWAIT);
  157. if (err > 0)
  158. err = 0;
  159. out:
  160. if (sk)
  161. sock_put(sk);
  162. out_nosk:
  163. return err;
  164. }
  165. static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
  166. {
  167. int hdrlen = sizeof(struct unix_diag_req);
  168. if (nlmsg_len(h) < hdrlen)
  169. return -EINVAL;
  170. if (h->nlmsg_flags & NLM_F_DUMP)
  171. return netlink_dump_start(sock_diag_nlsk, skb, h,
  172. unix_diag_dump, NULL, 0);
  173. else
  174. return unix_diag_get_exact(skb, h, (struct unix_diag_req *)NLMSG_DATA(h));
  175. }
  176. static struct sock_diag_handler unix_diag_handler = {
  177. .family = AF_UNIX,
  178. .dump = unix_diag_handler_dump,
  179. };
  180. static int __init unix_diag_init(void)
  181. {
  182. return sock_diag_register(&unix_diag_handler);
  183. }
  184. static void __exit unix_diag_exit(void)
  185. {
  186. sock_diag_unregister(&unix_diag_handler);
  187. }
  188. module_init(unix_diag_init);
  189. module_exit(unix_diag_exit);
  190. MODULE_LICENSE("GPL");
  191. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 1 /* AF_LOCAL */);