nlmon.c 4.0 KB

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