diag.c 5.2 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. #ifdef CONFIG_NETLINK_MMAP
  9. static int sk_diag_put_ring(struct netlink_ring *ring, int nl_type,
  10. struct sk_buff *nlskb)
  11. {
  12. struct netlink_diag_ring ndr;
  13. ndr.ndr_block_size = ring->pg_vec_pages << PAGE_SHIFT;
  14. ndr.ndr_block_nr = ring->pg_vec_len;
  15. ndr.ndr_frame_size = ring->frame_size;
  16. ndr.ndr_frame_nr = ring->frame_max + 1;
  17. return nla_put(nlskb, nl_type, sizeof(ndr), &ndr);
  18. }
  19. static int sk_diag_put_rings_cfg(struct sock *sk, struct sk_buff *nlskb)
  20. {
  21. struct netlink_sock *nlk = nlk_sk(sk);
  22. int ret;
  23. mutex_lock(&nlk->pg_vec_lock);
  24. ret = sk_diag_put_ring(&nlk->rx_ring, NETLINK_DIAG_RX_RING, nlskb);
  25. if (!ret)
  26. ret = sk_diag_put_ring(&nlk->tx_ring, NETLINK_DIAG_TX_RING,
  27. nlskb);
  28. mutex_unlock(&nlk->pg_vec_lock);
  29. return ret;
  30. }
  31. #else
  32. static int sk_diag_put_rings_cfg(struct sock *sk, struct sk_buff *nlskb)
  33. {
  34. return 0;
  35. }
  36. #endif
  37. static int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
  38. {
  39. struct netlink_sock *nlk = nlk_sk(sk);
  40. if (nlk->groups == NULL)
  41. return 0;
  42. return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
  43. nlk->groups);
  44. }
  45. static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
  46. struct netlink_diag_req *req,
  47. u32 portid, u32 seq, u32 flags, int sk_ino)
  48. {
  49. struct nlmsghdr *nlh;
  50. struct netlink_diag_msg *rep;
  51. struct netlink_sock *nlk = nlk_sk(sk);
  52. nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
  53. flags);
  54. if (!nlh)
  55. return -EMSGSIZE;
  56. rep = nlmsg_data(nlh);
  57. rep->ndiag_family = AF_NETLINK;
  58. rep->ndiag_type = sk->sk_type;
  59. rep->ndiag_protocol = sk->sk_protocol;
  60. rep->ndiag_state = sk->sk_state;
  61. rep->ndiag_ino = sk_ino;
  62. rep->ndiag_portid = nlk->portid;
  63. rep->ndiag_dst_portid = nlk->dst_portid;
  64. rep->ndiag_dst_group = nlk->dst_group;
  65. sock_diag_save_cookie(sk, rep->ndiag_cookie);
  66. if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
  67. sk_diag_dump_groups(sk, skb))
  68. goto out_nlmsg_trim;
  69. if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
  70. sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
  71. goto out_nlmsg_trim;
  72. if ((req->ndiag_show & NDIAG_SHOW_RING_CFG) &&
  73. sk_diag_put_rings_cfg(sk, skb))
  74. goto out_nlmsg_trim;
  75. nlmsg_end(skb, nlh);
  76. return 0;
  77. out_nlmsg_trim:
  78. nlmsg_cancel(skb, nlh);
  79. return -EMSGSIZE;
  80. }
  81. static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  82. int protocol, int s_num)
  83. {
  84. struct netlink_table *tbl = &nl_table[protocol];
  85. struct rhashtable *ht = &tbl->hash;
  86. const struct bucket_table *htbl = rht_dereference_rcu(ht->tbl, ht);
  87. struct net *net = sock_net(skb->sk);
  88. struct netlink_diag_req *req;
  89. struct netlink_sock *nlsk;
  90. struct sock *sk;
  91. int ret = 0, num = 0, i;
  92. req = nlmsg_data(cb->nlh);
  93. for (i = 0; i < htbl->size; i++) {
  94. struct rhash_head *pos;
  95. rht_for_each_entry_rcu(nlsk, pos, htbl, i, node) {
  96. sk = (struct sock *)nlsk;
  97. if (!net_eq(sock_net(sk), net))
  98. continue;
  99. if (num < s_num) {
  100. num++;
  101. continue;
  102. }
  103. if (sk_diag_fill(sk, skb, req,
  104. NETLINK_CB(cb->skb).portid,
  105. cb->nlh->nlmsg_seq,
  106. NLM_F_MULTI,
  107. sock_i_ino(sk)) < 0) {
  108. ret = 1;
  109. goto done;
  110. }
  111. num++;
  112. }
  113. }
  114. sk_for_each_bound(sk, &tbl->mc_list) {
  115. if (sk_hashed(sk))
  116. continue;
  117. if (!net_eq(sock_net(sk), net))
  118. continue;
  119. if (num < s_num) {
  120. num++;
  121. continue;
  122. }
  123. if (sk_diag_fill(sk, skb, req,
  124. NETLINK_CB(cb->skb).portid,
  125. cb->nlh->nlmsg_seq,
  126. NLM_F_MULTI,
  127. sock_i_ino(sk)) < 0) {
  128. ret = 1;
  129. goto done;
  130. }
  131. num++;
  132. }
  133. done:
  134. cb->args[0] = num;
  135. cb->args[1] = protocol;
  136. return ret;
  137. }
  138. static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  139. {
  140. struct netlink_diag_req *req;
  141. int s_num = cb->args[0];
  142. req = nlmsg_data(cb->nlh);
  143. rcu_read_lock();
  144. read_lock(&nl_table_lock);
  145. if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
  146. int i;
  147. for (i = cb->args[1]; i < MAX_LINKS; i++) {
  148. if (__netlink_diag_dump(skb, cb, i, s_num))
  149. break;
  150. s_num = 0;
  151. }
  152. } else {
  153. if (req->sdiag_protocol >= MAX_LINKS) {
  154. read_unlock(&nl_table_lock);
  155. rcu_read_unlock();
  156. return -ENOENT;
  157. }
  158. __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
  159. }
  160. read_unlock(&nl_table_lock);
  161. rcu_read_unlock();
  162. return skb->len;
  163. }
  164. static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
  165. {
  166. int hdrlen = sizeof(struct netlink_diag_req);
  167. struct net *net = sock_net(skb->sk);
  168. if (nlmsg_len(h) < hdrlen)
  169. return -EINVAL;
  170. if (h->nlmsg_flags & NLM_F_DUMP) {
  171. struct netlink_dump_control c = {
  172. .dump = netlink_diag_dump,
  173. };
  174. return netlink_dump_start(net->diag_nlsk, skb, h, &c);
  175. } else
  176. return -EOPNOTSUPP;
  177. }
  178. static const struct sock_diag_handler netlink_diag_handler = {
  179. .family = AF_NETLINK,
  180. .dump = netlink_diag_handler_dump,
  181. };
  182. static int __init netlink_diag_init(void)
  183. {
  184. return sock_diag_register(&netlink_diag_handler);
  185. }
  186. static void __exit netlink_diag_exit(void)
  187. {
  188. sock_diag_unregister(&netlink_diag_handler);
  189. }
  190. module_init(netlink_diag_init);
  191. module_exit(netlink_diag_exit);
  192. MODULE_LICENSE("GPL");
  193. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);