diag.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. ret = rhashtable_walk_start(hti);
  94. if (ret == -EAGAIN)
  95. ret = 0;
  96. if (ret)
  97. goto stop;
  98. while ((nlsk = rhashtable_walk_next(hti))) {
  99. if (IS_ERR(nlsk)) {
  100. ret = PTR_ERR(nlsk);
  101. if (ret == -EAGAIN) {
  102. ret = 0;
  103. continue;
  104. }
  105. break;
  106. }
  107. sk = (struct sock *)nlsk;
  108. if (!net_eq(sock_net(sk), net))
  109. continue;
  110. if (sk_diag_fill(sk, skb, req,
  111. NETLINK_CB(cb->skb).portid,
  112. cb->nlh->nlmsg_seq,
  113. NLM_F_MULTI,
  114. sock_i_ino(sk)) < 0) {
  115. ret = 1;
  116. break;
  117. }
  118. }
  119. stop:
  120. rhashtable_walk_stop(hti);
  121. if (ret)
  122. goto done;
  123. rhashtable_walk_exit(hti);
  124. num++;
  125. mc_list:
  126. read_lock(&nl_table_lock);
  127. sk_for_each_bound(sk, &tbl->mc_list) {
  128. if (sk_hashed(sk))
  129. continue;
  130. if (!net_eq(sock_net(sk), net))
  131. continue;
  132. if (num < s_num) {
  133. num++;
  134. continue;
  135. }
  136. if (sk_diag_fill(sk, skb, req,
  137. NETLINK_CB(cb->skb).portid,
  138. cb->nlh->nlmsg_seq,
  139. NLM_F_MULTI,
  140. sock_i_ino(sk)) < 0) {
  141. ret = 1;
  142. break;
  143. }
  144. num++;
  145. }
  146. read_unlock(&nl_table_lock);
  147. done:
  148. cb->args[0] = num;
  149. return ret;
  150. }
  151. static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  152. {
  153. struct netlink_diag_req *req;
  154. int s_num = cb->args[0];
  155. int err = 0;
  156. req = nlmsg_data(cb->nlh);
  157. if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
  158. int i;
  159. for (i = cb->args[1]; i < MAX_LINKS; i++) {
  160. err = __netlink_diag_dump(skb, cb, i, s_num);
  161. if (err)
  162. break;
  163. s_num = 0;
  164. }
  165. cb->args[1] = i;
  166. } else {
  167. if (req->sdiag_protocol >= MAX_LINKS)
  168. return -ENOENT;
  169. err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
  170. }
  171. return err < 0 ? err : skb->len;
  172. }
  173. static int netlink_diag_dump_done(struct netlink_callback *cb)
  174. {
  175. struct rhashtable_iter *hti = (void *)cb->args[2];
  176. if (cb->args[0] == 1)
  177. rhashtable_walk_exit(hti);
  178. kfree(hti);
  179. return 0;
  180. }
  181. static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
  182. {
  183. int hdrlen = sizeof(struct netlink_diag_req);
  184. struct net *net = sock_net(skb->sk);
  185. if (nlmsg_len(h) < hdrlen)
  186. return -EINVAL;
  187. if (h->nlmsg_flags & NLM_F_DUMP) {
  188. struct netlink_dump_control c = {
  189. .dump = netlink_diag_dump,
  190. .done = netlink_diag_dump_done,
  191. };
  192. return netlink_dump_start(net->diag_nlsk, skb, h, &c);
  193. } else
  194. return -EOPNOTSUPP;
  195. }
  196. static const struct sock_diag_handler netlink_diag_handler = {
  197. .family = AF_NETLINK,
  198. .dump = netlink_diag_handler_dump,
  199. };
  200. static int __init netlink_diag_init(void)
  201. {
  202. return sock_diag_register(&netlink_diag_handler);
  203. }
  204. static void __exit netlink_diag_exit(void)
  205. {
  206. sock_diag_unregister(&netlink_diag_handler);
  207. }
  208. module_init(netlink_diag_init);
  209. module_exit(netlink_diag_exit);
  210. MODULE_LICENSE("GPL");
  211. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);