diag.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <net/net_namespace.h>
  7. #include <net/sock.h>
  8. #include "internal.h"
  9. static int pdiag_put_info(const struct packet_sock *po, struct sk_buff *nlskb)
  10. {
  11. struct packet_diag_info pinfo;
  12. pinfo.pdi_index = po->ifindex;
  13. pinfo.pdi_version = po->tp_version;
  14. pinfo.pdi_reserve = po->tp_reserve;
  15. pinfo.pdi_copy_thresh = po->copy_thresh;
  16. pinfo.pdi_tstamp = po->tp_tstamp;
  17. pinfo.pdi_flags = 0;
  18. if (po->running)
  19. pinfo.pdi_flags |= PDI_RUNNING;
  20. if (po->auxdata)
  21. pinfo.pdi_flags |= PDI_AUXDATA;
  22. if (po->origdev)
  23. pinfo.pdi_flags |= PDI_ORIGDEV;
  24. if (po->has_vnet_hdr)
  25. pinfo.pdi_flags |= PDI_VNETHDR;
  26. if (po->tp_loss)
  27. pinfo.pdi_flags |= PDI_LOSS;
  28. return nla_put(nlskb, PACKET_DIAG_INFO, sizeof(pinfo), &pinfo);
  29. }
  30. static int pdiag_put_mclist(const struct packet_sock *po, struct sk_buff *nlskb)
  31. {
  32. struct nlattr *mca;
  33. struct packet_mclist *ml;
  34. mca = nla_nest_start(nlskb, PACKET_DIAG_MCLIST);
  35. if (!mca)
  36. return -EMSGSIZE;
  37. rtnl_lock();
  38. for (ml = po->mclist; ml; ml = ml->next) {
  39. struct packet_diag_mclist *dml;
  40. dml = nla_reserve_nohdr(nlskb, sizeof(*dml));
  41. if (!dml) {
  42. rtnl_unlock();
  43. nla_nest_cancel(nlskb, mca);
  44. return -EMSGSIZE;
  45. }
  46. dml->pdmc_index = ml->ifindex;
  47. dml->pdmc_type = ml->type;
  48. dml->pdmc_alen = ml->alen;
  49. dml->pdmc_count = ml->count;
  50. BUILD_BUG_ON(sizeof(dml->pdmc_addr) != sizeof(ml->addr));
  51. memcpy(dml->pdmc_addr, ml->addr, sizeof(ml->addr));
  52. }
  53. rtnl_unlock();
  54. nla_nest_end(nlskb, mca);
  55. return 0;
  56. }
  57. static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct packet_diag_req *req,
  58. u32 pid, u32 seq, u32 flags, int sk_ino)
  59. {
  60. struct nlmsghdr *nlh;
  61. struct packet_diag_msg *rp;
  62. const struct packet_sock *po = pkt_sk(sk);
  63. nlh = nlmsg_put(skb, pid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rp), flags);
  64. if (!nlh)
  65. return -EMSGSIZE;
  66. rp = nlmsg_data(nlh);
  67. rp->pdiag_family = AF_PACKET;
  68. rp->pdiag_type = sk->sk_type;
  69. rp->pdiag_num = ntohs(po->num);
  70. rp->pdiag_ino = sk_ino;
  71. sock_diag_save_cookie(sk, rp->pdiag_cookie);
  72. if ((req->pdiag_show & PACKET_SHOW_INFO) &&
  73. pdiag_put_info(po, skb))
  74. goto out_nlmsg_trim;
  75. if ((req->pdiag_show & PACKET_SHOW_MCLIST) &&
  76. pdiag_put_mclist(po, skb))
  77. goto out_nlmsg_trim;
  78. return nlmsg_end(skb, nlh);
  79. out_nlmsg_trim:
  80. nlmsg_cancel(skb, nlh);
  81. return -EMSGSIZE;
  82. }
  83. static int packet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  84. {
  85. int num = 0, s_num = cb->args[0];
  86. struct packet_diag_req *req;
  87. struct net *net;
  88. struct sock *sk;
  89. struct hlist_node *node;
  90. net = sock_net(skb->sk);
  91. req = nlmsg_data(cb->nlh);
  92. rcu_read_lock();
  93. sk_for_each_rcu(sk, node, &net->packet.sklist) {
  94. if (!net_eq(sock_net(sk), net))
  95. continue;
  96. if (num < s_num)
  97. goto next;
  98. if (sk_diag_fill(sk, skb, req, NETLINK_CB(cb->skb).pid,
  99. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  100. sock_i_ino(sk)) < 0)
  101. goto done;
  102. next:
  103. num++;
  104. }
  105. done:
  106. rcu_read_unlock();
  107. cb->args[0] = num;
  108. return skb->len;
  109. }
  110. static int packet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
  111. {
  112. int hdrlen = sizeof(struct packet_diag_req);
  113. struct net *net = sock_net(skb->sk);
  114. struct packet_diag_req *req;
  115. if (nlmsg_len(h) < hdrlen)
  116. return -EINVAL;
  117. req = nlmsg_data(h);
  118. /* Make it possible to support protocol filtering later */
  119. if (req->sdiag_protocol)
  120. return -EINVAL;
  121. if (h->nlmsg_flags & NLM_F_DUMP) {
  122. struct netlink_dump_control c = {
  123. .dump = packet_diag_dump,
  124. };
  125. return netlink_dump_start(net->diag_nlsk, skb, h, &c);
  126. } else
  127. return -EOPNOTSUPP;
  128. }
  129. static const struct sock_diag_handler packet_diag_handler = {
  130. .family = AF_PACKET,
  131. .dump = packet_diag_handler_dump,
  132. };
  133. static int __init packet_diag_init(void)
  134. {
  135. return sock_diag_register(&packet_diag_handler);
  136. }
  137. static void __exit packet_diag_exit(void)
  138. {
  139. sock_diag_unregister(&packet_diag_handler);
  140. }
  141. module_init(packet_diag_init);
  142. module_exit(packet_diag_exit);
  143. MODULE_LICENSE("GPL");
  144. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 17 /* AF_PACKET */);