diag.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include <linux/module.h>
  2. #include <net/sock.h>
  3. #include <linux/netlink.h>
  4. #include <linux/sock_diag.h>
  5. #include <linux/netlink_diag.h>
  6. #include <linux/rhashtable.h>
  7. #include "af_netlink.h"
  8. static int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
  9. {
  10. struct netlink_sock *nlk = nlk_sk(sk);
  11. if (nlk->groups == NULL)
  12. return 0;
  13. return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
  14. nlk->groups);
  15. }
  16. static int sk_diag_put_flags(struct sock *sk, struct sk_buff *skb)
  17. {
  18. struct netlink_sock *nlk = nlk_sk(sk);
  19. u32 flags = 0;
  20. if (nlk->cb_running)
  21. flags |= NDIAG_FLAG_CB_RUNNING;
  22. if (nlk->flags & NETLINK_F_RECV_PKTINFO)
  23. flags |= NDIAG_FLAG_PKTINFO;
  24. if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
  25. flags |= NDIAG_FLAG_BROADCAST_ERROR;
  26. if (nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)
  27. flags |= NDIAG_FLAG_NO_ENOBUFS;
  28. if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
  29. flags |= NDIAG_FLAG_LISTEN_ALL_NSID;
  30. if (nlk->flags & NETLINK_F_CAP_ACK)
  31. flags |= NDIAG_FLAG_CAP_ACK;
  32. return nla_put_u32(skb, NETLINK_DIAG_FLAGS, flags);
  33. }
  34. static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
  35. struct netlink_diag_req *req,
  36. u32 portid, u32 seq, u32 flags, int sk_ino)
  37. {
  38. struct nlmsghdr *nlh;
  39. struct netlink_diag_msg *rep;
  40. struct netlink_sock *nlk = nlk_sk(sk);
  41. nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
  42. flags);
  43. if (!nlh)
  44. return -EMSGSIZE;
  45. rep = nlmsg_data(nlh);
  46. rep->ndiag_family = AF_NETLINK;
  47. rep->ndiag_type = sk->sk_type;
  48. rep->ndiag_protocol = sk->sk_protocol;
  49. rep->ndiag_state = sk->sk_state;
  50. rep->ndiag_ino = sk_ino;
  51. rep->ndiag_portid = nlk->portid;
  52. rep->ndiag_dst_portid = nlk->dst_portid;
  53. rep->ndiag_dst_group = nlk->dst_group;
  54. sock_diag_save_cookie(sk, rep->ndiag_cookie);
  55. if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
  56. sk_diag_dump_groups(sk, skb))
  57. goto out_nlmsg_trim;
  58. if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
  59. sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
  60. goto out_nlmsg_trim;
  61. if ((req->ndiag_show & NDIAG_SHOW_FLAGS) &&
  62. sk_diag_put_flags(sk, skb))
  63. goto out_nlmsg_trim;
  64. nlmsg_end(skb, nlh);
  65. return 0;
  66. out_nlmsg_trim:
  67. nlmsg_cancel(skb, nlh);
  68. return -EMSGSIZE;
  69. }
  70. static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  71. int protocol, int s_num)
  72. {
  73. struct rhashtable_iter *hti = (void *)cb->args[2];
  74. struct netlink_table *tbl = &nl_table[protocol];
  75. struct net *net = sock_net(skb->sk);
  76. struct netlink_diag_req *req;
  77. struct netlink_sock *nlsk;
  78. struct sock *sk;
  79. int num = 2;
  80. int ret = 0;
  81. req = nlmsg_data(cb->nlh);
  82. if (s_num > 1)
  83. goto mc_list;
  84. num--;
  85. if (!hti) {
  86. hti = kmalloc(sizeof(*hti), GFP_KERNEL);
  87. if (!hti)
  88. return -ENOMEM;
  89. cb->args[2] = (long)hti;
  90. }
  91. if (!s_num)
  92. rhashtable_walk_enter(&tbl->hash, hti);
  93. rhashtable_walk_start(hti);
  94. while ((nlsk = rhashtable_walk_next(hti))) {
  95. if (IS_ERR(nlsk)) {
  96. ret = PTR_ERR(nlsk);
  97. if (ret == -EAGAIN) {
  98. ret = 0;
  99. continue;
  100. }
  101. break;
  102. }
  103. sk = (struct sock *)nlsk;
  104. if (!net_eq(sock_net(sk), net))
  105. continue;
  106. if (sk_diag_fill(sk, skb, req,
  107. NETLINK_CB(cb->skb).portid,
  108. cb->nlh->nlmsg_seq,
  109. NLM_F_MULTI,
  110. sock_i_ino(sk)) < 0) {
  111. ret = 1;
  112. break;
  113. }
  114. }
  115. rhashtable_walk_stop(hti);
  116. if (ret)
  117. goto done;
  118. rhashtable_walk_exit(hti);
  119. num++;
  120. mc_list:
  121. read_lock(&nl_table_lock);
  122. sk_for_each_bound(sk, &tbl->mc_list) {
  123. if (sk_hashed(sk))
  124. continue;
  125. if (!net_eq(sock_net(sk), net))
  126. continue;
  127. if (num < s_num) {
  128. num++;
  129. continue;
  130. }
  131. if (sk_diag_fill(sk, skb, req,
  132. NETLINK_CB(cb->skb).portid,
  133. cb->nlh->nlmsg_seq,
  134. NLM_F_MULTI,
  135. sock_i_ino(sk)) < 0) {
  136. ret = 1;
  137. break;
  138. }
  139. num++;
  140. }
  141. read_unlock(&nl_table_lock);
  142. done:
  143. cb->args[0] = num;
  144. return ret;
  145. }
  146. static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  147. {
  148. struct netlink_diag_req *req;
  149. int s_num = cb->args[0];
  150. int err = 0;
  151. req = nlmsg_data(cb->nlh);
  152. if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
  153. int i;
  154. for (i = cb->args[1]; i < MAX_LINKS; i++) {
  155. err = __netlink_diag_dump(skb, cb, i, s_num);
  156. if (err)
  157. break;
  158. s_num = 0;
  159. }
  160. cb->args[1] = i;
  161. } else {
  162. if (req->sdiag_protocol >= MAX_LINKS)
  163. return -ENOENT;
  164. err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
  165. }
  166. return err < 0 ? err : skb->len;
  167. }
  168. static int netlink_diag_dump_done(struct netlink_callback *cb)
  169. {
  170. struct rhashtable_iter *hti = (void *)cb->args[2];
  171. if (cb->args[0] == 1)
  172. rhashtable_walk_exit(hti);
  173. kfree(hti);
  174. return 0;
  175. }
  176. static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
  177. {
  178. int hdrlen = sizeof(struct netlink_diag_req);
  179. struct net *net = sock_net(skb->sk);
  180. if (nlmsg_len(h) < hdrlen)
  181. return -EINVAL;
  182. if (h->nlmsg_flags & NLM_F_DUMP) {
  183. struct netlink_dump_control c = {
  184. .dump = netlink_diag_dump,
  185. .done = netlink_diag_dump_done,
  186. };
  187. return netlink_dump_start(net->diag_nlsk, skb, h, &c);
  188. } else
  189. return -EOPNOTSUPP;
  190. }
  191. static const struct sock_diag_handler netlink_diag_handler = {
  192. .family = AF_NETLINK,
  193. .dump = netlink_diag_handler_dump,
  194. };
  195. static int __init netlink_diag_init(void)
  196. {
  197. return sock_diag_register(&netlink_diag_handler);
  198. }
  199. static void __exit netlink_diag_exit(void)
  200. {
  201. sock_diag_unregister(&netlink_diag_handler);
  202. }
  203. module_init(netlink_diag_init);
  204. module_exit(netlink_diag_exit);
  205. MODULE_LICENSE("GPL");
  206. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);