2019-05-19 12:08:20 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-16 22:20:36 +00:00
|
|
|
/* dummy.c: a dummy net driver
|
|
|
|
|
|
|
|
The purpose of this driver is to provide a device to point a
|
|
|
|
route through, but not to actually transmit packets.
|
|
|
|
|
|
|
|
Why? If you have a machine whose only connection is an occasional
|
|
|
|
PPP/SLIP/PLIP link, you can only connect to your own hostname
|
|
|
|
when the link is up. Otherwise you have to use localhost.
|
|
|
|
This isn't very consistent.
|
|
|
|
|
|
|
|
One solution is to set up a dummy link using PPP/SLIP/PLIP,
|
|
|
|
but this seems (to me) too much overhead for too little gain.
|
|
|
|
This driver provides a small alternative. Thus you can do
|
2006-09-13 17:24:59 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
[when not running slip]
|
|
|
|
ifconfig dummy slip.addr.ess.here up
|
|
|
|
[to go to slip]
|
|
|
|
ifconfig dummy down
|
|
|
|
dip whatever
|
|
|
|
|
|
|
|
This was written by looking at Donald Becker's skeleton driver
|
|
|
|
and the loopback driver. I then threw away anything that didn't
|
|
|
|
apply! Thanks to Alan Cox for the key clue on what to do with
|
|
|
|
misguided packets.
|
|
|
|
|
|
|
|
Nick Holloway, 27th May 1994
|
|
|
|
[I tweaked this explanation a little but that's all]
|
|
|
|
Alan Cox, 30th May 1994
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/etherdevice.h>
|
2019-04-12 11:06:13 +00:00
|
|
|
#include <linux/ethtool.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/moduleparam.h>
|
2007-06-13 19:04:20 +00:00
|
|
|
#include <linux/rtnetlink.h>
|
2017-03-11 20:06:54 +00:00
|
|
|
#include <linux/net_tstamp.h>
|
2007-06-13 19:04:34 +00:00
|
|
|
#include <net/rtnetlink.h>
|
2010-09-27 20:50:33 +00:00
|
|
|
#include <linux/u64_stats_sync.h>
|
2007-06-13 19:04:20 +00:00
|
|
|
|
2014-12-06 00:13:24 +00:00
|
|
|
#define DRV_NAME "dummy"
|
|
|
|
#define DRV_VERSION "1.0"
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static int numdummies = 1;
|
|
|
|
|
|
|
|
/* fake multicast ability */
|
|
|
|
static void set_multicast_list(struct net_device *dev)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-27 20:50:33 +00:00
|
|
|
struct pcpu_dstats {
|
|
|
|
u64 tx_packets;
|
|
|
|
u64 tx_bytes;
|
|
|
|
struct u64_stats_sync syncp;
|
|
|
|
};
|
|
|
|
|
2017-01-07 03:12:52 +00:00
|
|
|
static void dummy_get_stats64(struct net_device *dev,
|
|
|
|
struct rtnl_link_stats64 *stats)
|
2010-09-27 20:50:33 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for_each_possible_cpu(i) {
|
|
|
|
const struct pcpu_dstats *dstats;
|
|
|
|
u64 tbytes, tpackets;
|
|
|
|
unsigned int start;
|
|
|
|
|
|
|
|
dstats = per_cpu_ptr(dev->dstats, i);
|
|
|
|
do {
|
2014-03-14 04:26:42 +00:00
|
|
|
start = u64_stats_fetch_begin_irq(&dstats->syncp);
|
2010-09-27 20:50:33 +00:00
|
|
|
tbytes = dstats->tx_bytes;
|
|
|
|
tpackets = dstats->tx_packets;
|
2014-03-14 04:26:42 +00:00
|
|
|
} while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
|
2010-09-27 20:50:33 +00:00
|
|
|
stats->tx_bytes += tbytes;
|
|
|
|
stats->tx_packets += tpackets;
|
|
|
|
}
|
|
|
|
}
|
2009-08-31 19:50:51 +00:00
|
|
|
|
|
|
|
static netdev_tx_t dummy_xmit(struct sk_buff *skb, struct net_device *dev)
|
|
|
|
{
|
2010-09-27 20:50:33 +00:00
|
|
|
struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
|
|
|
|
|
|
|
|
u64_stats_update_begin(&dstats->syncp);
|
|
|
|
dstats->tx_packets++;
|
|
|
|
dstats->tx_bytes += skb->len;
|
|
|
|
u64_stats_update_end(&dstats->syncp);
|
2009-08-31 19:50:51 +00:00
|
|
|
|
2017-03-11 20:06:54 +00:00
|
|
|
skb_tx_timestamp(skb);
|
2009-08-31 19:50:51 +00:00
|
|
|
dev_kfree_skb(skb);
|
|
|
|
return NETDEV_TX_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-27 20:50:33 +00:00
|
|
|
static int dummy_dev_init(struct net_device *dev)
|
|
|
|
{
|
2014-02-13 19:46:28 +00:00
|
|
|
dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
|
2010-09-27 20:50:33 +00:00
|
|
|
if (!dev->dstats)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-15 13:26:01 +00:00
|
|
|
static void dummy_dev_uninit(struct net_device *dev)
|
2010-09-27 20:50:33 +00:00
|
|
|
{
|
|
|
|
free_percpu(dev->dstats);
|
|
|
|
}
|
|
|
|
|
2012-12-27 23:49:40 +00:00
|
|
|
static int dummy_change_carrier(struct net_device *dev, bool new_carrier)
|
|
|
|
{
|
|
|
|
if (new_carrier)
|
|
|
|
netif_carrier_on(dev);
|
|
|
|
else
|
|
|
|
netif_carrier_off(dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-21 04:28:00 +00:00
|
|
|
static const struct net_device_ops dummy_netdev_ops = {
|
2010-09-27 20:50:33 +00:00
|
|
|
.ndo_init = dummy_dev_init,
|
2012-04-15 13:26:01 +00:00
|
|
|
.ndo_uninit = dummy_dev_uninit,
|
2008-11-21 04:28:00 +00:00
|
|
|
.ndo_start_xmit = dummy_xmit,
|
|
|
|
.ndo_validate_addr = eth_validate_addr,
|
2011-08-16 06:29:01 +00:00
|
|
|
.ndo_set_rx_mode = set_multicast_list,
|
2012-06-29 05:10:08 +00:00
|
|
|
.ndo_set_mac_address = eth_mac_addr,
|
2010-09-27 20:50:33 +00:00
|
|
|
.ndo_get_stats64 = dummy_get_stats64,
|
2012-12-27 23:49:40 +00:00
|
|
|
.ndo_change_carrier = dummy_change_carrier,
|
2008-11-21 04:28:00 +00:00
|
|
|
};
|
|
|
|
|
2014-12-06 00:13:24 +00:00
|
|
|
static void dummy_get_drvinfo(struct net_device *dev,
|
|
|
|
struct ethtool_drvinfo *info)
|
|
|
|
{
|
|
|
|
strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
|
|
|
|
strlcpy(info->version, DRV_VERSION, sizeof(info->version));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct ethtool_ops dummy_ethtool_ops = {
|
|
|
|
.get_drvinfo = dummy_get_drvinfo,
|
2019-04-12 11:06:13 +00:00
|
|
|
.get_ts_info = ethtool_op_get_ts_info,
|
2014-12-06 00:13:24 +00:00
|
|
|
};
|
|
|
|
|
2007-06-13 19:04:34 +00:00
|
|
|
static void dummy_setup(struct net_device *dev)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-11-21 04:28:00 +00:00
|
|
|
ether_setup(dev);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Initialize the device structure. */
|
2008-11-21 04:28:00 +00:00
|
|
|
dev->netdev_ops = &dummy_netdev_ops;
|
2014-12-06 00:13:24 +00:00
|
|
|
dev->ethtool_ops = &dummy_ethtool_ops;
|
net: Fix inconsistent teardown and release of private netdev state.
Network devices can allocate reasources and private memory using
netdev_ops->ndo_init(). However, the release of these resources
can occur in one of two different places.
Either netdev_ops->ndo_uninit() or netdev->destructor().
The decision of which operation frees the resources depends upon
whether it is necessary for all netdev refs to be released before it
is safe to perform the freeing.
netdev_ops->ndo_uninit() presumably can occur right after the
NETDEV_UNREGISTER notifier completes and the unicast and multicast
address lists are flushed.
netdev->destructor(), on the other hand, does not run until the
netdev references all go away.
Further complicating the situation is that netdev->destructor()
almost universally does also a free_netdev().
This creates a problem for the logic in register_netdevice().
Because all callers of register_netdevice() manage the freeing
of the netdev, and invoke free_netdev(dev) if register_netdevice()
fails.
If netdev_ops->ndo_init() succeeds, but something else fails inside
of register_netdevice(), it does call ndo_ops->ndo_uninit(). But
it is not able to invoke netdev->destructor().
This is because netdev->destructor() will do a free_netdev() and
then the caller of register_netdevice() will do the same.
However, this means that the resources that would normally be released
by netdev->destructor() will not be.
Over the years drivers have added local hacks to deal with this, by
invoking their destructor parts by hand when register_netdevice()
fails.
Many drivers do not try to deal with this, and instead we have leaks.
Let's close this hole by formalizing the distinction between what
private things need to be freed up by netdev->destructor() and whether
the driver needs unregister_netdevice() to perform the free_netdev().
netdev->priv_destructor() performs all actions to free up the private
resources that used to be freed by netdev->destructor(), except for
free_netdev().
netdev->needs_free_netdev is a boolean that indicates whether
free_netdev() should be done at the end of unregister_netdevice().
Now, register_netdevice() can sanely release all resources after
ndo_ops->ndo_init() succeeds, by invoking both ndo_ops->ndo_uninit()
and netdev->priv_destructor().
And at the end of unregister_netdevice(), we invoke
netdev->priv_destructor() and optionally call free_netdev().
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-05-08 16:52:56 +00:00
|
|
|
dev->needs_free_netdev = true;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Fill in device structure with ethernet-generic values. */
|
|
|
|
dev->flags |= IFF_NOARP;
|
|
|
|
dev->flags &= ~IFF_MULTICAST;
|
2015-08-18 08:30:30 +00:00
|
|
|
dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
|
2015-10-20 03:17:59 +00:00
|
|
|
dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST;
|
2017-07-03 13:36:07 +00:00
|
|
|
dev->features |= NETIF_F_ALL_TSO;
|
2011-11-15 15:29:55 +00:00
|
|
|
dev->features |= NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_LLTX;
|
2015-10-20 03:17:59 +00:00
|
|
|
dev->features |= NETIF_F_GSO_ENCAP_ALL;
|
|
|
|
dev->hw_features |= dev->features;
|
|
|
|
dev->hw_enc_features |= dev->features;
|
2012-02-15 06:45:40 +00:00
|
|
|
eth_hw_addr_random(dev);
|
2016-12-07 09:41:33 +00:00
|
|
|
|
|
|
|
dev->min_mtu = 0;
|
2017-09-22 15:57:49 +00:00
|
|
|
dev->max_mtu = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2010-09-27 20:50:33 +00:00
|
|
|
|
2017-06-25 21:56:01 +00:00
|
|
|
static int dummy_validate(struct nlattr *tb[], struct nlattr *data[],
|
|
|
|
struct netlink_ext_ack *extack)
|
2007-07-12 02:42:31 +00:00
|
|
|
{
|
|
|
|
if (tb[IFLA_ADDRESS]) {
|
|
|
|
if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
|
|
|
|
return -EINVAL;
|
|
|
|
if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
|
|
|
|
return -EADDRNOTAVAIL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-13 19:04:34 +00:00
|
|
|
static struct rtnl_link_ops dummy_link_ops __read_mostly = {
|
2014-12-06 00:13:24 +00:00
|
|
|
.kind = DRV_NAME,
|
2007-06-13 19:04:34 +00:00
|
|
|
.setup = dummy_setup,
|
2007-07-12 02:42:31 +00:00
|
|
|
.validate = dummy_validate,
|
2007-06-13 19:04:34 +00:00
|
|
|
};
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Number of dummy devices to be set up by this module. */
|
|
|
|
module_param(numdummies, int, 0);
|
|
|
|
MODULE_PARM_DESC(numdummies, "Number of dummy pseudo devices");
|
|
|
|
|
2007-06-13 19:04:20 +00:00
|
|
|
static int __init dummy_init_one(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct net_device *dev_dummy;
|
|
|
|
int err;
|
|
|
|
|
2017-12-01 23:09:02 +00:00
|
|
|
dev_dummy = alloc_netdev(0, "dummy%d", NET_NAME_ENUM, dummy_setup);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!dev_dummy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2007-06-13 19:04:34 +00:00
|
|
|
dev_dummy->rtnl_link_ops = &dummy_link_ops;
|
|
|
|
err = register_netdevice(dev_dummy);
|
|
|
|
if (err < 0)
|
|
|
|
goto err;
|
|
|
|
return 0;
|
2007-06-13 19:04:20 +00:00
|
|
|
|
2007-06-13 19:04:34 +00:00
|
|
|
err:
|
|
|
|
free_netdev(dev_dummy);
|
|
|
|
return err;
|
2006-09-13 17:24:59 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
static int __init dummy_init_module(void)
|
2006-09-13 17:24:59 +00:00
|
|
|
{
|
2005-04-16 22:20:36 +00:00
|
|
|
int i, err = 0;
|
2007-06-13 19:04:20 +00:00
|
|
|
|
2018-03-30 16:38:37 +00:00
|
|
|
down_write(&pernet_ops_rwsem);
|
2007-06-13 19:04:34 +00:00
|
|
|
rtnl_lock();
|
|
|
|
err = __rtnl_link_register(&dummy_link_ops);
|
2013-07-11 11:04:02 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
2007-06-13 19:04:34 +00:00
|
|
|
|
2012-06-10 21:11:57 +00:00
|
|
|
for (i = 0; i < numdummies && !err; i++) {
|
2007-06-13 19:04:20 +00:00
|
|
|
err = dummy_init_one();
|
2012-06-10 21:11:57 +00:00
|
|
|
cond_resched();
|
|
|
|
}
|
2007-07-12 02:42:13 +00:00
|
|
|
if (err < 0)
|
2007-06-13 19:04:34 +00:00
|
|
|
__rtnl_link_unregister(&dummy_link_ops);
|
2013-07-11 11:04:02 +00:00
|
|
|
|
|
|
|
out:
|
2007-06-13 19:04:34 +00:00
|
|
|
rtnl_unlock();
|
2018-03-30 16:38:37 +00:00
|
|
|
up_write(&pernet_ops_rwsem);
|
2007-06-13 19:04:34 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return err;
|
2006-09-13 17:24:59 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
static void __exit dummy_cleanup_module(void)
|
|
|
|
{
|
2007-07-12 02:42:13 +00:00
|
|
|
rtnl_link_unregister(&dummy_link_ops);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(dummy_init_module);
|
|
|
|
module_exit(dummy_cleanup_module);
|
|
|
|
MODULE_LICENSE("GPL");
|
2014-12-06 00:13:24 +00:00
|
|
|
MODULE_ALIAS_RTNL_LINK(DRV_NAME);
|
2014-12-10 00:41:48 +00:00
|
|
|
MODULE_VERSION(DRV_VERSION);
|