diag.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include <linux/module.h>
  2. #include <linux/sock_diag.h>
  3. #include <linux/net.h>
  4. #include <linux/netdevice.h>
  5. #include <linux/packet_diag.h>
  6. #include <linux/percpu.h>
  7. #include <net/net_namespace.h>
  8. #include <net/sock.h>
  9. #include "internal.h"
  10. static int pdiag_put_info(const struct packet_sock *po, struct sk_buff *nlskb)
  11. {
  12. struct packet_diag_info pinfo;
  13. pinfo.pdi_index = po->ifindex;
  14. pinfo.pdi_version = po->tp_version;
  15. pinfo.pdi_reserve = po->tp_reserve;
  16. pinfo.pdi_copy_thresh = po->copy_thresh;
  17. pinfo.pdi_tstamp = po->tp_tstamp;
  18. pinfo.pdi_flags = 0;
  19. if (po->running)
  20. pinfo.pdi_flags |= PDI_RUNNING;
  21. if (po->auxdata)
  22. pinfo.pdi_flags |= PDI_AUXDATA;
  23. if (po->origdev)
  24. pinfo.pdi_flags |= PDI_ORIGDEV;
  25. if (po->has_vnet_hdr)
  26. pinfo.pdi_flags |= PDI_VNETHDR;
  27. if (po->tp_loss)
  28. pinfo.pdi_flags |= PDI_LOSS;
  29. return nla_put(nlskb, PACKET_DIAG_INFO, sizeof(pinfo), &pinfo);
  30. }
  31. static int pdiag_put_mclist(const struct packet_sock *po, struct sk_buff *nlskb)
  32. {
  33. struct nlattr *mca;
  34. struct packet_mclist *ml;
  35. mca = nla_nest_start(nlskb, PACKET_DIAG_MCLIST);
  36. if (!mca)
  37. return -EMSGSIZE;
  38. rtnl_lock();
  39. for (ml = po->mclist; ml; ml = ml->next) {
  40. struct packet_diag_mclist *dml;
  41. dml = nla_reserve_nohdr(nlskb, sizeof(*dml));
  42. if (!dml) {
  43. rtnl_unlock();
  44. nla_nest_cancel(nlskb, mca);
  45. return -EMSGSIZE;
  46. }
  47. dml->pdmc_index = ml->ifindex;
  48. dml->pdmc_type = ml->type;
  49. dml->pdmc_alen = ml->alen;
  50. dml->pdmc_count = ml->count;
  51. BUILD_BUG_ON(sizeof(dml->pdmc_addr) != sizeof(ml->addr));
  52. memcpy(dml->pdmc_addr, ml->addr, sizeof(ml->addr));
  53. }
  54. rtnl_unlock();
  55. nla_nest_end(nlskb, mca);
  56. return 0;
  57. }
  58. static int pdiag_put_ring(struct packet_ring_buffer *ring, int ver, int nl_type,
  59. struct sk_buff *nlskb)
  60. {
  61. struct packet_diag_ring pdr;
  62. if (!ring->pg_vec || ((ver > TPACKET_V2) &&
  63. (nl_type == PACKET_DIAG_TX_RING)))
  64. return 0;
  65. pdr.pdr_block_size = ring->pg_vec_pages << PAGE_SHIFT;
  66. pdr.pdr_block_nr = ring->pg_vec_len;
  67. pdr.pdr_frame_size = ring->frame_size;
  68. pdr.pdr_frame_nr = ring->frame_max + 1;
  69. if (ver > TPACKET_V2) {
  70. pdr.pdr_retire_tmo = ring->prb_bdqc.retire_blk_tov;
  71. pdr.pdr_sizeof_priv = ring->prb_bdqc.blk_sizeof_priv;
  72. pdr.pdr_features = ring->prb_bdqc.feature_req_word;
  73. } else {
  74. pdr.pdr_retire_tmo = 0;
  75. pdr.pdr_sizeof_priv = 0;
  76. pdr.pdr_features = 0;
  77. }
  78. return nla_put(nlskb, nl_type, sizeof(pdr), &pdr);
  79. }
  80. static int pdiag_put_rings_cfg(struct packet_sock *po, struct sk_buff *skb)
  81. {
  82. int ret;
  83. mutex_lock(&po->pg_vec_lock);
  84. ret = pdiag_put_ring(&po->rx_ring, po->tp_version,
  85. PACKET_DIAG_RX_RING, skb);
  86. if (!ret)
  87. ret = pdiag_put_ring(&po->tx_ring, po->tp_version,
  88. PACKET_DIAG_TX_RING, skb);
  89. mutex_unlock(&po->pg_vec_lock);
  90. return ret;
  91. }
  92. static int pdiag_put_fanout(struct packet_sock *po, struct sk_buff *nlskb)
  93. {
  94. int ret = 0;
  95. mutex_lock(&fanout_mutex);
  96. if (po->fanout) {
  97. u32 val;
  98. val = (u32)po->fanout->id | ((u32)po->fanout->type << 16);
  99. ret = nla_put_u32(nlskb, PACKET_DIAG_FANOUT, val);
  100. }
  101. mutex_unlock(&fanout_mutex);
  102. return ret;
  103. }
  104. static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
  105. struct packet_diag_req *req,
  106. bool may_report_filterinfo,
  107. struct user_namespace *user_ns,
  108. u32 portid, u32 seq, u32 flags, int sk_ino)
  109. {
  110. struct nlmsghdr *nlh;
  111. struct packet_diag_msg *rp;
  112. struct packet_sock *po = pkt_sk(sk);
  113. nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rp), flags);
  114. if (!nlh)
  115. return -EMSGSIZE;
  116. rp = nlmsg_data(nlh);
  117. rp->pdiag_family = AF_PACKET;
  118. rp->pdiag_type = sk->sk_type;
  119. rp->pdiag_num = ntohs(po->num);
  120. rp->pdiag_ino = sk_ino;
  121. sock_diag_save_cookie(sk, rp->pdiag_cookie);
  122. if ((req->pdiag_show & PACKET_SHOW_INFO) &&
  123. pdiag_put_info(po, skb))
  124. goto out_nlmsg_trim;
  125. if ((req->pdiag_show & PACKET_SHOW_INFO) &&
  126. nla_put_u32(skb, PACKET_DIAG_UID,
  127. from_kuid_munged(user_ns, sock_i_uid(sk))))
  128. goto out_nlmsg_trim;
  129. if ((req->pdiag_show & PACKET_SHOW_MCLIST) &&
  130. pdiag_put_mclist(po, skb))
  131. goto out_nlmsg_trim;
  132. if ((req->pdiag_show & PACKET_SHOW_RING_CFG) &&
  133. pdiag_put_rings_cfg(po, skb))
  134. goto out_nlmsg_trim;
  135. if ((req->pdiag_show & PACKET_SHOW_FANOUT) &&
  136. pdiag_put_fanout(po, skb))
  137. goto out_nlmsg_trim;
  138. if ((req->pdiag_show & PACKET_SHOW_MEMINFO) &&
  139. sock_diag_put_meminfo(sk, skb, PACKET_DIAG_MEMINFO))
  140. goto out_nlmsg_trim;
  141. if ((req->pdiag_show & PACKET_SHOW_FILTER) &&
  142. sock_diag_put_filterinfo(may_report_filterinfo, sk, skb,
  143. PACKET_DIAG_FILTER))
  144. goto out_nlmsg_trim;
  145. nlmsg_end(skb, nlh);
  146. return 0;
  147. out_nlmsg_trim:
  148. nlmsg_cancel(skb, nlh);
  149. return -EMSGSIZE;
  150. }
  151. static int packet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  152. {
  153. int num = 0, s_num = cb->args[0];
  154. struct packet_diag_req *req;
  155. struct net *net;
  156. struct sock *sk;
  157. bool may_report_filterinfo;
  158. net = sock_net(skb->sk);
  159. req = nlmsg_data(cb->nlh);
  160. may_report_filterinfo = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
  161. mutex_lock(&net->packet.sklist_lock);
  162. sk_for_each(sk, &net->packet.sklist) {
  163. if (!net_eq(sock_net(sk), net))
  164. continue;
  165. if (num < s_num)
  166. goto next;
  167. if (sk_diag_fill(sk, skb, req,
  168. may_report_filterinfo,
  169. sk_user_ns(NETLINK_CB(cb->skb).sk),
  170. NETLINK_CB(cb->skb).portid,
  171. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  172. sock_i_ino(sk)) < 0)
  173. goto done;
  174. next:
  175. num++;
  176. }
  177. done:
  178. mutex_unlock(&net->packet.sklist_lock);
  179. cb->args[0] = num;
  180. return skb->len;
  181. }
  182. static int packet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
  183. {
  184. int hdrlen = sizeof(struct packet_diag_req);
  185. struct net *net = sock_net(skb->sk);
  186. struct packet_diag_req *req;
  187. if (nlmsg_len(h) < hdrlen)
  188. return -EINVAL;
  189. req = nlmsg_data(h);
  190. /* Make it possible to support protocol filtering later */
  191. if (req->sdiag_protocol)
  192. return -EINVAL;
  193. if (h->nlmsg_flags & NLM_F_DUMP) {
  194. struct netlink_dump_control c = {
  195. .dump = packet_diag_dump,
  196. };
  197. return netlink_dump_start(net->diag_nlsk, skb, h, &c);
  198. } else
  199. return -EOPNOTSUPP;
  200. }
  201. static const struct sock_diag_handler packet_diag_handler = {
  202. .family = AF_PACKET,
  203. .dump = packet_diag_handler_dump,
  204. };
  205. static int __init packet_diag_init(void)
  206. {
  207. return sock_diag_register(&packet_diag_handler);
  208. }
  209. static void __exit packet_diag_exit(void)
  210. {
  211. sock_diag_unregister(&packet_diag_handler);
  212. }
  213. module_init(packet_diag_init);
  214. module_exit(packet_diag_exit);
  215. MODULE_LICENSE("GPL");
  216. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 17 /* AF_PACKET */);