diag.c 4.8 KB

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