nlmon.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. struct nlmon {
  17. struct netlink_tap nt;
  18. };
  19. static int nlmon_open(struct net_device *dev)
  20. {
  21. struct nlmon *nlmon = netdev_priv(dev);
  22. nlmon->nt.dev = dev;
  23. nlmon->nt.module = THIS_MODULE;
  24. return netlink_add_tap(&nlmon->nt);
  25. }
  26. static int nlmon_close(struct net_device *dev)
  27. {
  28. struct nlmon *nlmon = netdev_priv(dev);
  29. return netlink_remove_tap(&nlmon->nt);
  30. }
  31. static void
  32. nlmon_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  33. {
  34. dev_lstats_read(dev, &stats->rx_packets, &stats->rx_bytes);
  35. }
  36. static u32 always_on(struct net_device *dev)
  37. {
  38. return 1;
  39. }
  40. static const struct ethtool_ops nlmon_ethtool_ops = {
  41. .get_link = always_on,
  42. };
  43. static const struct net_device_ops nlmon_ops = {
  44. .ndo_open = nlmon_open,
  45. .ndo_stop = nlmon_close,
  46. .ndo_start_xmit = nlmon_xmit,
  47. .ndo_get_stats64 = nlmon_get_stats64,
  48. };
  49. static void nlmon_setup(struct net_device *dev)
  50. {
  51. dev->type = ARPHRD_NETLINK;
  52. dev->priv_flags |= IFF_NO_QUEUE;
  53. dev->lltx = true;
  54. dev->netdev_ops = &nlmon_ops;
  55. dev->ethtool_ops = &nlmon_ethtool_ops;
  56. dev->needs_free_netdev = true;
  57. dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA;
  58. dev->flags = IFF_NOARP;
  59. dev->pcpu_stat_type = NETDEV_PCPU_STAT_LSTATS;
  60. /* That's rather a softlimit here, which, of course,
  61. * can be altered. Not a real MTU, but what is to be
  62. * expected in most cases.
  63. */
  64. dev->mtu = NLMSG_GOODSIZE;
  65. dev->min_mtu = sizeof(struct nlmsghdr);
  66. }
  67. static int nlmon_validate(struct nlattr *tb[], struct nlattr *data[],
  68. struct netlink_ext_ack *extack)
  69. {
  70. if (tb[IFLA_ADDRESS])
  71. return -EINVAL;
  72. return 0;
  73. }
  74. static struct rtnl_link_ops nlmon_link_ops __read_mostly = {
  75. .kind = "nlmon",
  76. .priv_size = sizeof(struct nlmon),
  77. .setup = nlmon_setup,
  78. .validate = nlmon_validate,
  79. };
  80. static __init int nlmon_register(void)
  81. {
  82. return rtnl_link_register(&nlmon_link_ops);
  83. }
  84. static __exit void nlmon_unregister(void)
  85. {
  86. rtnl_link_unregister(&nlmon_link_ops);
  87. }
  88. module_init(nlmon_register);
  89. module_exit(nlmon_unregister);
  90. MODULE_LICENSE("GPL v2");
  91. MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
  92. MODULE_AUTHOR("Mathieu Geli <geli@enseirb.fr>");
  93. MODULE_DESCRIPTION("Netlink monitoring device");
  94. MODULE_ALIAS_RTNL_LINK("nlmon");