nlmon.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_is_valid_mtu(int new_mtu)
  25. {
  26. /* Note that in netlink we do not really have an upper limit. On
  27. * default, we use NLMSG_GOODSIZE. Here at least we should make
  28. * sure that it's at least the header size.
  29. */
  30. return new_mtu >= (int) sizeof(struct nlmsghdr);
  31. }
  32. static int nlmon_change_mtu(struct net_device *dev, int new_mtu)
  33. {
  34. if (!nlmon_is_valid_mtu(new_mtu))
  35. return -EINVAL;
  36. dev->mtu = new_mtu;
  37. return 0;
  38. }
  39. static int nlmon_dev_init(struct net_device *dev)
  40. {
  41. dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
  42. return dev->lstats == NULL ? -ENOMEM : 0;
  43. }
  44. static void nlmon_dev_uninit(struct net_device *dev)
  45. {
  46. free_percpu(dev->lstats);
  47. }
  48. struct nlmon {
  49. struct netlink_tap nt;
  50. };
  51. static int nlmon_open(struct net_device *dev)
  52. {
  53. struct nlmon *nlmon = netdev_priv(dev);
  54. nlmon->nt.dev = dev;
  55. nlmon->nt.module = THIS_MODULE;
  56. return netlink_add_tap(&nlmon->nt);
  57. }
  58. static int nlmon_close(struct net_device *dev)
  59. {
  60. struct nlmon *nlmon = netdev_priv(dev);
  61. return netlink_remove_tap(&nlmon->nt);
  62. }
  63. static struct rtnl_link_stats64 *
  64. nlmon_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  65. {
  66. int i;
  67. u64 bytes = 0, packets = 0;
  68. for_each_possible_cpu(i) {
  69. const struct pcpu_lstats *nl_stats;
  70. u64 tbytes, tpackets;
  71. unsigned int start;
  72. nl_stats = per_cpu_ptr(dev->lstats, i);
  73. do {
  74. start = u64_stats_fetch_begin_irq(&nl_stats->syncp);
  75. tbytes = nl_stats->bytes;
  76. tpackets = nl_stats->packets;
  77. } while (u64_stats_fetch_retry_irq(&nl_stats->syncp, start));
  78. packets += tpackets;
  79. bytes += tbytes;
  80. }
  81. stats->rx_packets = packets;
  82. stats->tx_packets = 0;
  83. stats->rx_bytes = bytes;
  84. stats->tx_bytes = 0;
  85. return stats;
  86. }
  87. static u32 always_on(struct net_device *dev)
  88. {
  89. return 1;
  90. }
  91. static const struct ethtool_ops nlmon_ethtool_ops = {
  92. .get_link = always_on,
  93. };
  94. static const struct net_device_ops nlmon_ops = {
  95. .ndo_init = nlmon_dev_init,
  96. .ndo_uninit = nlmon_dev_uninit,
  97. .ndo_open = nlmon_open,
  98. .ndo_stop = nlmon_close,
  99. .ndo_start_xmit = nlmon_xmit,
  100. .ndo_get_stats64 = nlmon_get_stats64,
  101. .ndo_change_mtu = nlmon_change_mtu,
  102. };
  103. static void nlmon_setup(struct net_device *dev)
  104. {
  105. dev->type = ARPHRD_NETLINK;
  106. dev->priv_flags |= IFF_NO_QUEUE;
  107. dev->netdev_ops = &nlmon_ops;
  108. dev->ethtool_ops = &nlmon_ethtool_ops;
  109. dev->destructor = free_netdev;
  110. dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
  111. NETIF_F_HIGHDMA | NETIF_F_LLTX;
  112. dev->flags = IFF_NOARP;
  113. /* That's rather a softlimit here, which, of course,
  114. * can be altered. Not a real MTU, but what is to be
  115. * expected in most cases.
  116. */
  117. dev->mtu = NLMSG_GOODSIZE;
  118. }
  119. static int nlmon_validate(struct nlattr *tb[], struct nlattr *data[])
  120. {
  121. if (tb[IFLA_ADDRESS])
  122. return -EINVAL;
  123. return 0;
  124. }
  125. static struct rtnl_link_ops nlmon_link_ops __read_mostly = {
  126. .kind = "nlmon",
  127. .priv_size = sizeof(struct nlmon),
  128. .setup = nlmon_setup,
  129. .validate = nlmon_validate,
  130. };
  131. static __init int nlmon_register(void)
  132. {
  133. return rtnl_link_register(&nlmon_link_ops);
  134. }
  135. static __exit void nlmon_unregister(void)
  136. {
  137. rtnl_link_unregister(&nlmon_link_ops);
  138. }
  139. module_init(nlmon_register);
  140. module_exit(nlmon_unregister);
  141. MODULE_LICENSE("GPL v2");
  142. MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
  143. MODULE_AUTHOR("Mathieu Geli <geli@enseirb.fr>");
  144. MODULE_DESCRIPTION("Netlink monitoring device");
  145. MODULE_ALIAS_RTNL_LINK("nlmon");