diag.c 4.9 KB

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