nlmon.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/netdevice.h>
  4. #include <linux/netlink.h>
  5. #include <net/net_namespace.h>
  6. #include <linux/if_arp.h>
  7. #include <net/rtnetlink.h>
  8. struct pcpu_lstats {
  9. u64 packets;
  10. u64 bytes;
  11. struct u64_stats_sync syncp;
  12. };
  13. static netdev_tx_t nlmon_xmit(struct sk_buff *skb, struct net_device *dev)
  14. {
  15. int len = skb->len;
  16. struct pcpu_lstats *stats = this_cpu_ptr(dev->lstats);
  17. u64_stats_update_begin(&stats->syncp);
  18. stats->bytes += len;
  19. stats->packets++;
  20. u64_stats_update_end(&stats->syncp);
  21. dev_kfree_skb(skb);
  22. return NETDEV_TX_OK;
  23. }
  24. static int nlmon_dev_init(struct net_device *dev)
  25. {
  26. dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
  27. return dev->lstats == NULL ? -ENOMEM : 0;
  28. }
  29. static void nlmon_dev_uninit(struct net_device *dev)
  30. {
  31. free_percpu(dev->lstats);
  32. }
  33. struct nlmon {
  34. struct netlink_tap nt;
  35. };
  36. static int nlmon_open(struct net_device *dev)
  37. {
  38. struct nlmon *nlmon = netdev_priv(dev);
  39. nlmon->nt.dev = dev;
  40. nlmon->nt.module = THIS_MODULE;
  41. return netlink_add_tap(&nlmon->nt);
  42. }
  43. static int nlmon_close(struct net_device *dev)
  44. {
  45. struct nlmon *nlmon = netdev_priv(dev);
  46. return netlink_remove_tap(&nlmon->nt);
  47. }
  48. static void
  49. nlmon_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  50. {
  51. int i;
  52. u64 bytes = 0, packets = 0;
  53. for_each_possible_cpu(i) {
  54. const struct pcpu_lstats *nl_stats;
  55. u64 tbytes, tpackets;
  56. unsigned int start;
  57. nl_stats = per_cpu_ptr(dev->lstats, i);
  58. do {
  59. start = u64_stats_fetch_begin_irq(&nl_stats->syncp);
  60. tbytes = nl_stats->bytes;
  61. tpackets = nl_stats->packets;
  62. } while (u64_stats_fetch_retry_irq(&nl_stats->syncp, start));
  63. packets += tpackets;
  64. bytes += tbytes;
  65. }
  66. stats->rx_packets = packets;
  67. stats->tx_packets = 0;
  68. stats->rx_bytes = bytes;
  69. stats->tx_bytes = 0;
  70. }
  71. static u32 always_on(struct net_device *dev)
  72. {
  73. return 1;
  74. }
  75. static const struct ethtool_ops nlmon_ethtool_ops = {
  76. .get_link = always_on,
  77. };
  78. static const struct net_device_ops nlmon_ops = {
  79. .ndo_init = nlmon_dev_init,
  80. .ndo_uninit = nlmon_dev_uninit,
  81. .ndo_open = nlmon_open,
  82. .ndo_stop = nlmon_close,
  83. .ndo_start_xmit = nlmon_xmit,
  84. .ndo_get_stats64 = nlmon_get_stats64,
  85. };
  86. static void nlmon_setup(struct net_device *dev)
  87. {
  88. dev->type = ARPHRD_NETLINK;
  89. dev->priv_flags |= IFF_NO_QUEUE;
  90. dev->netdev_ops = &nlmon_ops;
  91. dev->ethtool_ops = &nlmon_ethtool_ops;
  92. dev->needs_free_netdev = true;
  93. dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
  94. NETIF_F_HIGHDMA | NETIF_F_LLTX;
  95. dev->flags = IFF_NOARP;
  96. /* That's rather a softlimit here, which, of course,
  97. * can be altered. Not a real MTU, but what is to be
  98. * expected in most cases.
  99. */
  100. dev->mtu = NLMSG_GOODSIZE;
  101. dev->min_mtu = sizeof(struct nlmsghdr);
  102. }
  103. static int nlmon_validate(struct nlattr *tb[], struct nlattr *data[],
  104. struct netlink_ext_ack *extack)
  105. {
  106. if (tb[IFLA_ADDRESS])
  107. return -EINVAL;
  108. return 0;
  109. }
  110. static struct rtnl_link_ops nlmon_link_ops __read_mostly = {
  111. .kind = "nlmon",
  112. .priv_size = sizeof(struct nlmon),
  113. .setup = nlmon_setup,
  114. .validate = nlmon_validate,
  115. };
  116. static __init int nlmon_register(void)
  117. {
  118. return rtnl_link_register(&nlmon_link_ops);
  119. }
  120. static __exit void nlmon_unregister(void)
  121. {
  122. rtnl_link_unregister(&nlmon_link_ops);
  123. }
  124. module_init(nlmon_register);
  125. module_exit(nlmon_unregister);
  126. MODULE_LICENSE("GPL v2");
  127. MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
  128. MODULE_AUTHOR("Mathieu Geli <geli@enseirb.fr>");
  129. MODULE_DESCRIPTION("Netlink monitoring device");
  130. MODULE_ALIAS_RTNL_LINK("nlmon");