Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/ethtool.h>
0003 #include <linux/module.h>
0004 #include <linux/kernel.h>
0005 #include <linux/netdevice.h>
0006 #include <linux/netlink.h>
0007 #include <net/net_namespace.h>
0008 #include <linux/if_arp.h>
0009 #include <net/rtnetlink.h>
0010 
0011 static netdev_tx_t nlmon_xmit(struct sk_buff *skb, struct net_device *dev)
0012 {
0013     dev_lstats_add(dev, skb->len);
0014 
0015     dev_kfree_skb(skb);
0016 
0017     return NETDEV_TX_OK;
0018 }
0019 
0020 static int nlmon_dev_init(struct net_device *dev)
0021 {
0022     dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
0023     return dev->lstats == NULL ? -ENOMEM : 0;
0024 }
0025 
0026 static void nlmon_dev_uninit(struct net_device *dev)
0027 {
0028     free_percpu(dev->lstats);
0029 }
0030 
0031 struct nlmon {
0032     struct netlink_tap nt;
0033 };
0034 
0035 static int nlmon_open(struct net_device *dev)
0036 {
0037     struct nlmon *nlmon = netdev_priv(dev);
0038 
0039     nlmon->nt.dev = dev;
0040     nlmon->nt.module = THIS_MODULE;
0041     return netlink_add_tap(&nlmon->nt);
0042 }
0043 
0044 static int nlmon_close(struct net_device *dev)
0045 {
0046     struct nlmon *nlmon = netdev_priv(dev);
0047 
0048     return netlink_remove_tap(&nlmon->nt);
0049 }
0050 
0051 static void
0052 nlmon_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
0053 {
0054     u64 packets, bytes;
0055 
0056     dev_lstats_read(dev, &packets, &bytes);
0057 
0058     stats->rx_packets = packets;
0059     stats->tx_packets = 0;
0060 
0061     stats->rx_bytes = bytes;
0062     stats->tx_bytes = 0;
0063 }
0064 
0065 static u32 always_on(struct net_device *dev)
0066 {
0067     return 1;
0068 }
0069 
0070 static const struct ethtool_ops nlmon_ethtool_ops = {
0071     .get_link = always_on,
0072 };
0073 
0074 static const struct net_device_ops nlmon_ops = {
0075     .ndo_init = nlmon_dev_init,
0076     .ndo_uninit = nlmon_dev_uninit,
0077     .ndo_open = nlmon_open,
0078     .ndo_stop = nlmon_close,
0079     .ndo_start_xmit = nlmon_xmit,
0080     .ndo_get_stats64 = nlmon_get_stats64,
0081 };
0082 
0083 static void nlmon_setup(struct net_device *dev)
0084 {
0085     dev->type = ARPHRD_NETLINK;
0086     dev->priv_flags |= IFF_NO_QUEUE;
0087 
0088     dev->netdev_ops = &nlmon_ops;
0089     dev->ethtool_ops = &nlmon_ethtool_ops;
0090     dev->needs_free_netdev = true;
0091 
0092     dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
0093             NETIF_F_HIGHDMA | NETIF_F_LLTX;
0094     dev->flags = IFF_NOARP;
0095 
0096     /* That's rather a softlimit here, which, of course,
0097      * can be altered. Not a real MTU, but what is to be
0098      * expected in most cases.
0099      */
0100     dev->mtu = NLMSG_GOODSIZE;
0101     dev->min_mtu = sizeof(struct nlmsghdr);
0102 }
0103 
0104 static int nlmon_validate(struct nlattr *tb[], struct nlattr *data[],
0105               struct netlink_ext_ack *extack)
0106 {
0107     if (tb[IFLA_ADDRESS])
0108         return -EINVAL;
0109     return 0;
0110 }
0111 
0112 static struct rtnl_link_ops nlmon_link_ops __read_mostly = {
0113     .kind           = "nlmon",
0114     .priv_size      = sizeof(struct nlmon),
0115     .setup          = nlmon_setup,
0116     .validate       = nlmon_validate,
0117 };
0118 
0119 static __init int nlmon_register(void)
0120 {
0121     return rtnl_link_register(&nlmon_link_ops);
0122 }
0123 
0124 static __exit void nlmon_unregister(void)
0125 {
0126     rtnl_link_unregister(&nlmon_link_ops);
0127 }
0128 
0129 module_init(nlmon_register);
0130 module_exit(nlmon_unregister);
0131 
0132 MODULE_LICENSE("GPL v2");
0133 MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
0134 MODULE_AUTHOR("Mathieu Geli <geli@enseirb.fr>");
0135 MODULE_DESCRIPTION("Netlink monitoring device");
0136 MODULE_ALIAS_RTNL_LINK("nlmon");