109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2*cc69837fSJakub Kicinski #include <linux/ethtool.h>
3e4fc408eSDaniel Borkmann #include <linux/module.h>
4e4fc408eSDaniel Borkmann #include <linux/kernel.h>
5e4fc408eSDaniel Borkmann #include <linux/netdevice.h>
6e4fc408eSDaniel Borkmann #include <linux/netlink.h>
7e4fc408eSDaniel Borkmann #include <net/net_namespace.h>
8e4fc408eSDaniel Borkmann #include <linux/if_arp.h>
97e6d4da8SJiri Pirko #include <net/rtnetlink.h>
10e4fc408eSDaniel Borkmann
nlmon_xmit(struct sk_buff * skb,struct net_device * dev)11e4fc408eSDaniel Borkmann static netdev_tx_t nlmon_xmit(struct sk_buff *skb, struct net_device *dev)
12e4fc408eSDaniel Borkmann {
133ed91226SEric Dumazet dev_lstats_add(dev, skb->len);
14e4fc408eSDaniel Borkmann
15e4fc408eSDaniel Borkmann dev_kfree_skb(skb);
16e4fc408eSDaniel Borkmann
17e4fc408eSDaniel Borkmann return NETDEV_TX_OK;
18e4fc408eSDaniel Borkmann }
19e4fc408eSDaniel Borkmann
nlmon_dev_init(struct net_device * dev)20e4fc408eSDaniel Borkmann static int nlmon_dev_init(struct net_device *dev)
21e4fc408eSDaniel Borkmann {
221c213bd2SWANG Cong dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
23e4fc408eSDaniel Borkmann return dev->lstats == NULL ? -ENOMEM : 0;
24e4fc408eSDaniel Borkmann }
25e4fc408eSDaniel Borkmann
nlmon_dev_uninit(struct net_device * dev)26e4fc408eSDaniel Borkmann static void nlmon_dev_uninit(struct net_device *dev)
27e4fc408eSDaniel Borkmann {
28e4fc408eSDaniel Borkmann free_percpu(dev->lstats);
29e4fc408eSDaniel Borkmann }
30e4fc408eSDaniel Borkmann
317e6d4da8SJiri Pirko struct nlmon {
327e6d4da8SJiri Pirko struct netlink_tap nt;
337e6d4da8SJiri Pirko };
34e4fc408eSDaniel Borkmann
nlmon_open(struct net_device * dev)35e4fc408eSDaniel Borkmann static int nlmon_open(struct net_device *dev)
36e4fc408eSDaniel Borkmann {
377e6d4da8SJiri Pirko struct nlmon *nlmon = netdev_priv(dev);
387e6d4da8SJiri Pirko
397e6d4da8SJiri Pirko nlmon->nt.dev = dev;
407e6d4da8SJiri Pirko nlmon->nt.module = THIS_MODULE;
417e6d4da8SJiri Pirko return netlink_add_tap(&nlmon->nt);
42e4fc408eSDaniel Borkmann }
43e4fc408eSDaniel Borkmann
nlmon_close(struct net_device * dev)44e4fc408eSDaniel Borkmann static int nlmon_close(struct net_device *dev)
45e4fc408eSDaniel Borkmann {
467e6d4da8SJiri Pirko struct nlmon *nlmon = netdev_priv(dev);
477e6d4da8SJiri Pirko
487e6d4da8SJiri Pirko return netlink_remove_tap(&nlmon->nt);
49e4fc408eSDaniel Borkmann }
50e4fc408eSDaniel Borkmann
51bc1f4470Sstephen hemminger static void
nlmon_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)52e4fc408eSDaniel Borkmann nlmon_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
53e4fc408eSDaniel Borkmann {
543ed91226SEric Dumazet u64 packets, bytes;
55e4fc408eSDaniel Borkmann
563ed91226SEric Dumazet dev_lstats_read(dev, &packets, &bytes);
57e4fc408eSDaniel Borkmann
58e4fc408eSDaniel Borkmann stats->rx_packets = packets;
59e4fc408eSDaniel Borkmann stats->tx_packets = 0;
60e4fc408eSDaniel Borkmann
61e4fc408eSDaniel Borkmann stats->rx_bytes = bytes;
62e4fc408eSDaniel Borkmann stats->tx_bytes = 0;
63e4fc408eSDaniel Borkmann }
64e4fc408eSDaniel Borkmann
always_on(struct net_device * dev)65e4fc408eSDaniel Borkmann static u32 always_on(struct net_device *dev)
66e4fc408eSDaniel Borkmann {
67e4fc408eSDaniel Borkmann return 1;
68e4fc408eSDaniel Borkmann }
69e4fc408eSDaniel Borkmann
70e4fc408eSDaniel Borkmann static const struct ethtool_ops nlmon_ethtool_ops = {
71e4fc408eSDaniel Borkmann .get_link = always_on,
72e4fc408eSDaniel Borkmann };
73e4fc408eSDaniel Borkmann
74e4fc408eSDaniel Borkmann static const struct net_device_ops nlmon_ops = {
75e4fc408eSDaniel Borkmann .ndo_init = nlmon_dev_init,
76e4fc408eSDaniel Borkmann .ndo_uninit = nlmon_dev_uninit,
77e4fc408eSDaniel Borkmann .ndo_open = nlmon_open,
78e4fc408eSDaniel Borkmann .ndo_stop = nlmon_close,
79e4fc408eSDaniel Borkmann .ndo_start_xmit = nlmon_xmit,
80e4fc408eSDaniel Borkmann .ndo_get_stats64 = nlmon_get_stats64,
81e4fc408eSDaniel Borkmann };
82e4fc408eSDaniel Borkmann
nlmon_setup(struct net_device * dev)83e4fc408eSDaniel Borkmann static void nlmon_setup(struct net_device *dev)
84e4fc408eSDaniel Borkmann {
85e4fc408eSDaniel Borkmann dev->type = ARPHRD_NETLINK;
8685773a61SPhil Sutter dev->priv_flags |= IFF_NO_QUEUE;
87e4fc408eSDaniel Borkmann
88e4fc408eSDaniel Borkmann dev->netdev_ops = &nlmon_ops;
89e4fc408eSDaniel Borkmann dev->ethtool_ops = &nlmon_ethtool_ops;
90cf124db5SDavid S. Miller dev->needs_free_netdev = true;
91e4fc408eSDaniel Borkmann
92b7d47ca2SDaniel Borkmann dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
93b7d47ca2SDaniel Borkmann NETIF_F_HIGHDMA | NETIF_F_LLTX;
94e4fc408eSDaniel Borkmann dev->flags = IFF_NOARP;
95e4fc408eSDaniel Borkmann
96e4fc408eSDaniel Borkmann /* That's rather a softlimit here, which, of course,
97e4fc408eSDaniel Borkmann * can be altered. Not a real MTU, but what is to be
98e4fc408eSDaniel Borkmann * expected in most cases.
99e4fc408eSDaniel Borkmann */
100e4fc408eSDaniel Borkmann dev->mtu = NLMSG_GOODSIZE;
101e82621e3SZhang Shengju dev->min_mtu = sizeof(struct nlmsghdr);
102e4fc408eSDaniel Borkmann }
103e4fc408eSDaniel Borkmann
nlmon_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)104a8b8a889SMatthias Schiffer static int nlmon_validate(struct nlattr *tb[], struct nlattr *data[],
105a8b8a889SMatthias Schiffer struct netlink_ext_ack *extack)
1067e6d4da8SJiri Pirko {
1077e6d4da8SJiri Pirko if (tb[IFLA_ADDRESS])
1087e6d4da8SJiri Pirko return -EINVAL;
1097e6d4da8SJiri Pirko return 0;
1107e6d4da8SJiri Pirko }
1117e6d4da8SJiri Pirko
1127e6d4da8SJiri Pirko static struct rtnl_link_ops nlmon_link_ops __read_mostly = {
1137e6d4da8SJiri Pirko .kind = "nlmon",
1147e6d4da8SJiri Pirko .priv_size = sizeof(struct nlmon),
1157e6d4da8SJiri Pirko .setup = nlmon_setup,
1167e6d4da8SJiri Pirko .validate = nlmon_validate,
1177e6d4da8SJiri Pirko };
1187e6d4da8SJiri Pirko
nlmon_register(void)119e4fc408eSDaniel Borkmann static __init int nlmon_register(void)
120e4fc408eSDaniel Borkmann {
1217e6d4da8SJiri Pirko return rtnl_link_register(&nlmon_link_ops);
122e4fc408eSDaniel Borkmann }
123e4fc408eSDaniel Borkmann
nlmon_unregister(void)124e4fc408eSDaniel Borkmann static __exit void nlmon_unregister(void)
125e4fc408eSDaniel Borkmann {
1267e6d4da8SJiri Pirko rtnl_link_unregister(&nlmon_link_ops);
127e4fc408eSDaniel Borkmann }
128e4fc408eSDaniel Borkmann
129e4fc408eSDaniel Borkmann module_init(nlmon_register);
130e4fc408eSDaniel Borkmann module_exit(nlmon_unregister);
131e4fc408eSDaniel Borkmann
132e4fc408eSDaniel Borkmann MODULE_LICENSE("GPL v2");
133e4fc408eSDaniel Borkmann MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
134e4fc408eSDaniel Borkmann MODULE_AUTHOR("Mathieu Geli <geli@enseirb.fr>");
135e4fc408eSDaniel Borkmann MODULE_DESCRIPTION("Netlink monitoring device");
1367e6d4da8SJiri Pirko MODULE_ALIAS_RTNL_LINK("nlmon");
137