diag.c 4.5 KB

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