nlmon.c 2.9 KB

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