nlmon.c 3.4 KB

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