diag.c 4.7 KB

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