2019-05-19 20:08:20 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-17 06:20:36 +08: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-14 01:24:59 +08:00
|
|
|
|
2005-04-17 06:20:36 +08: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 19:06:13 +08:00
|
|
|
#include <linux/ethtool.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/moduleparam.h>
|
2007-06-14 03:04:20 +08:00
|
|
|
#include <linux/rtnetlink.h>
|
2017-03-12 04:06:54 +08:00
|
|
|
#include <linux/net_tstamp.h>
|
2007-06-14 03:04:34 +08:00
|
|
|
#include <net/rtnetlink.h>
|
2010-09-28 04:50:33 +08:00
|
|
|
#include <linux/u64_stats_sync.h>
|
2007-06-14 03:04:20 +08:00
|
|
|
|
2014-12-06 08:13:24 +08:00
|
|
|
#define DRV_NAME "dummy"
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int numdummies = 1;
|
|
|
|
|
|
|
|
/* fake multicast ability */
|
|
|
|
static void set_multicast_list(struct net_device *dev)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-07 11:12:52 +08:00
|
|
|
static void dummy_get_stats64(struct net_device *dev,
|
|
|
|
struct rtnl_link_stats64 *stats)
|
2010-09-28 04:50:33 +08:00
|
|
|
{
|
2019-11-08 08:27:19 +08:00
|
|
|
dev_lstats_read(dev, &stats->tx_packets, &stats->tx_bytes);
|
2010-09-28 04:50:33 +08:00
|
|
|
}
|
2009-09-01 03:50:51 +08:00
|
|
|
|
|
|
|
static netdev_tx_t dummy_xmit(struct sk_buff *skb, struct net_device *dev)
|
|
|
|
{
|
2019-11-08 08:27:19 +08:00
|
|
|
dev_lstats_add(dev, skb->len);
|
2009-09-01 03:50:51 +08:00
|
|
|
|
2017-03-12 04:06:54 +08:00
|
|
|
skb_tx_timestamp(skb);
|
2009-09-01 03:50:51 +08:00
|
|
|
dev_kfree_skb(skb);
|
|
|
|
return NETDEV_TX_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-28 04:50:33 +08:00
|
|
|
static int dummy_dev_init(struct net_device *dev)
|
|
|
|
{
|
2019-11-08 08:27:19 +08:00
|
|
|
dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
|
|
|
|
if (!dev->lstats)
|
2010-09-28 04:50:33 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-15 21:26:01 +08:00
|
|
|
static void dummy_dev_uninit(struct net_device *dev)
|
2010-09-28 04:50:33 +08:00
|
|
|
{
|
2019-11-08 08:27:19 +08:00
|
|
|
free_percpu(dev->lstats);
|
2010-09-28 04:50:33 +08:00
|
|
|
}
|
|
|
|
|
2012-12-28 07:49:40 +08: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 12:28:00 +08:00
|
|
|
static const struct net_device_ops dummy_netdev_ops = {
|
2010-09-28 04:50:33 +08:00
|
|
|
.ndo_init = dummy_dev_init,
|
2012-04-15 21:26:01 +08:00
|
|
|
.ndo_uninit = dummy_dev_uninit,
|
2008-11-21 12:28:00 +08:00
|
|
|
.ndo_start_xmit = dummy_xmit,
|
|
|
|
.ndo_validate_addr = eth_validate_addr,
|
2011-08-16 14:29:01 +08:00
|
|
|
.ndo_set_rx_mode = set_multicast_list,
|
2012-06-29 13:10:08 +08:00
|
|
|
.ndo_set_mac_address = eth_mac_addr,
|
2010-09-28 04:50:33 +08:00
|
|
|
.ndo_get_stats64 = dummy_get_stats64,
|
2012-12-28 07:49:40 +08:00
|
|
|
.ndo_change_carrier = dummy_change_carrier,
|
2008-11-21 12:28:00 +08:00
|
|
|
};
|
|
|
|
|
2014-12-06 08:13:24 +08:00
|
|
|
static const struct ethtool_ops dummy_ethtool_ops = {
|
2019-04-12 19:06:13 +08:00
|
|
|
.get_ts_info = ethtool_op_get_ts_info,
|
2014-12-06 08:13:24 +08:00
|
|
|
};
|
|
|
|
|
2007-06-14 03:04:34 +08:00
|
|
|
static void dummy_setup(struct net_device *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-11-21 12:28:00 +08:00
|
|
|
ether_setup(dev);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Initialize the device structure. */
|
2008-11-21 12:28:00 +08:00
|
|
|
dev->netdev_ops = &dummy_netdev_ops;
|
2014-12-06 08:13:24 +08: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-09 00:52:56 +08:00
|
|
|
dev->needs_free_netdev = true;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Fill in device structure with ethernet-generic values. */
|
|
|
|
dev->flags |= IFF_NOARP;
|
|
|
|
dev->flags &= ~IFF_MULTICAST;
|
2015-08-18 16:30:30 +08:00
|
|
|
dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
|
2015-10-20 11:17:59 +08:00
|
|
|
dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST;
|
2020-11-01 21:17:17 +08:00
|
|
|
dev->features |= NETIF_F_GSO_SOFTWARE;
|
2011-11-15 23:29:55 +08:00
|
|
|
dev->features |= NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_LLTX;
|
2015-10-20 11:17:59 +08:00
|
|
|
dev->features |= NETIF_F_GSO_ENCAP_ALL;
|
|
|
|
dev->hw_features |= dev->features;
|
|
|
|
dev->hw_enc_features |= dev->features;
|
2012-02-15 14:45:40 +08:00
|
|
|
eth_hw_addr_random(dev);
|
2016-12-07 17:41:33 +08:00
|
|
|
|
|
|
|
dev->min_mtu = 0;
|
2017-09-22 23:57:49 +08:00
|
|
|
dev->max_mtu = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2010-09-28 04:50:33 +08:00
|
|
|
|
2017-06-26 05:56:01 +08:00
|
|
|
static int dummy_validate(struct nlattr *tb[], struct nlattr *data[],
|
|
|
|
struct netlink_ext_ack *extack)
|
2007-07-12 10:42:31 +08: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-14 03:04:34 +08:00
|
|
|
static struct rtnl_link_ops dummy_link_ops __read_mostly = {
|
2014-12-06 08:13:24 +08:00
|
|
|
.kind = DRV_NAME,
|
2007-06-14 03:04:34 +08:00
|
|
|
.setup = dummy_setup,
|
2007-07-12 10:42:31 +08:00
|
|
|
.validate = dummy_validate,
|
2007-06-14 03:04:34 +08:00
|
|
|
};
|
|
|
|
|
2005-04-17 06:20:36 +08: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-14 03:04:20 +08:00
|
|
|
static int __init dummy_init_one(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct net_device *dev_dummy;
|
|
|
|
int err;
|
|
|
|
|
2017-12-02 07:09:02 +08:00
|
|
|
dev_dummy = alloc_netdev(0, "dummy%d", NET_NAME_ENUM, dummy_setup);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!dev_dummy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2007-06-14 03:04:34 +08:00
|
|
|
dev_dummy->rtnl_link_ops = &dummy_link_ops;
|
|
|
|
err = register_netdevice(dev_dummy);
|
|
|
|
if (err < 0)
|
|
|
|
goto err;
|
|
|
|
return 0;
|
2007-06-14 03:04:20 +08:00
|
|
|
|
2007-06-14 03:04:34 +08:00
|
|
|
err:
|
|
|
|
free_netdev(dev_dummy);
|
|
|
|
return err;
|
2006-09-14 01:24:59 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static int __init dummy_init_module(void)
|
2006-09-14 01:24:59 +08:00
|
|
|
{
|
2005-04-17 06:20:36 +08:00
|
|
|
int i, err = 0;
|
2007-06-14 03:04:20 +08:00
|
|
|
|
2018-03-31 00:38:37 +08:00
|
|
|
down_write(&pernet_ops_rwsem);
|
2007-06-14 03:04:34 +08:00
|
|
|
rtnl_lock();
|
|
|
|
err = __rtnl_link_register(&dummy_link_ops);
|
2013-07-11 19:04:02 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
2007-06-14 03:04:34 +08:00
|
|
|
|
2012-06-11 05:11:57 +08:00
|
|
|
for (i = 0; i < numdummies && !err; i++) {
|
2007-06-14 03:04:20 +08:00
|
|
|
err = dummy_init_one();
|
2012-06-11 05:11:57 +08:00
|
|
|
cond_resched();
|
|
|
|
}
|
2007-07-12 10:42:13 +08:00
|
|
|
if (err < 0)
|
2007-06-14 03:04:34 +08:00
|
|
|
__rtnl_link_unregister(&dummy_link_ops);
|
2013-07-11 19:04:02 +08:00
|
|
|
|
|
|
|
out:
|
2007-06-14 03:04:34 +08:00
|
|
|
rtnl_unlock();
|
2018-03-31 00:38:37 +08:00
|
|
|
up_write(&pernet_ops_rwsem);
|
2007-06-14 03:04:34 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
2006-09-14 01:24:59 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static void __exit dummy_cleanup_module(void)
|
|
|
|
{
|
2007-07-12 10:42:13 +08:00
|
|
|
rtnl_link_unregister(&dummy_link_ops);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(dummy_init_module);
|
|
|
|
module_exit(dummy_cleanup_module);
|
|
|
|
MODULE_LICENSE("GPL");
|
2014-12-06 08:13:24 +08:00
|
|
|
MODULE_ALIAS_RTNL_LINK(DRV_NAME);
|