xref: /linux/drivers/net/dummy.c (revision e467705a9fb37f51595aa6deaca085ccb4005454)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* dummy.c: a dummy net driver
3 
4 	The purpose of this driver is to provide a device to point a
5 	route through, but not to actually transmit packets.
6 
7 	Why?  If you have a machine whose only connection is an occasional
8 	PPP/SLIP/PLIP link, you can only connect to your own hostname
9 	when the link is up.  Otherwise you have to use localhost.
10 	This isn't very consistent.
11 
12 	One solution is to set up a dummy link using PPP/SLIP/PLIP,
13 	but this seems (to me) too much overhead for too little gain.
14 	This driver provides a small alternative. Thus you can do
15 
16 	[when not running slip]
17 		ifconfig dummy slip.addr.ess.here up
18 	[to go to slip]
19 		ifconfig dummy down
20 		dip whatever
21 
22 	This was written by looking at Donald Becker's skeleton driver
23 	and the loopback driver.  I then threw away anything that didn't
24 	apply!	Thanks to Alan Cox for the key clue on what to do with
25 	misguided packets.
26 
27 			Nick Holloway, 27th May 1994
28 	[I tweaked this explanation a little but that's all]
29 			Alan Cox, 30th May 1994
30 */
31 
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/ethtool.h>
37 #include <linux/init.h>
38 #include <linux/moduleparam.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/net_tstamp.h>
41 #include <net/rtnetlink.h>
42 #include <linux/u64_stats_sync.h>
43 
44 #define DRV_NAME	"dummy"
45 
46 static int numdummies = 1;
47 
48 /* fake multicast ability */
49 static void set_multicast_list(struct net_device *dev)
50 {
51 }
52 
53 static void dummy_get_stats64(struct net_device *dev,
54 			      struct rtnl_link_stats64 *stats)
55 {
56 	dev_lstats_read(dev, &stats->tx_packets, &stats->tx_bytes);
57 }
58 
59 static netdev_tx_t dummy_xmit(struct sk_buff *skb, struct net_device *dev)
60 {
61 	dev_lstats_add(dev, skb->len);
62 
63 	skb_tx_timestamp(skb);
64 	dev_kfree_skb(skb);
65 	return NETDEV_TX_OK;
66 }
67 
68 static int dummy_dev_init(struct net_device *dev)
69 {
70 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_LSTATS;
71 
72 	netdev_lockdep_set_classes(dev);
73 	return 0;
74 }
75 
76 static int dummy_change_carrier(struct net_device *dev, bool new_carrier)
77 {
78 	if (new_carrier)
79 		netif_carrier_on(dev);
80 	else
81 		netif_carrier_off(dev);
82 	return 0;
83 }
84 
85 static const struct net_device_ops dummy_netdev_ops = {
86 	.ndo_init		= dummy_dev_init,
87 	.ndo_start_xmit		= dummy_xmit,
88 	.ndo_validate_addr	= eth_validate_addr,
89 	.ndo_set_rx_mode	= set_multicast_list,
90 	.ndo_set_mac_address	= eth_mac_addr,
91 	.ndo_get_stats64	= dummy_get_stats64,
92 	.ndo_change_carrier	= dummy_change_carrier,
93 };
94 
95 static const struct ethtool_ops dummy_ethtool_ops = {
96 	.get_ts_info		= ethtool_op_get_ts_info,
97 };
98 
99 static void dummy_setup(struct net_device *dev)
100 {
101 	ether_setup(dev);
102 
103 	/* Initialize the device structure. */
104 	dev->netdev_ops = &dummy_netdev_ops;
105 	dev->ethtool_ops = &dummy_ethtool_ops;
106 	dev->needs_free_netdev = true;
107 
108 	/* Fill in device structure with ethernet-generic values. */
109 	dev->flags |= IFF_NOARP;
110 	dev->flags &= ~IFF_MULTICAST;
111 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
112 	dev->features	|= NETIF_F_SG | NETIF_F_FRAGLIST;
113 	dev->features	|= NETIF_F_GSO_SOFTWARE;
114 	dev->features	|= NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_LLTX;
115 	dev->features	|= NETIF_F_GSO_ENCAP_ALL;
116 	dev->hw_features |= dev->features;
117 	dev->hw_enc_features |= dev->features;
118 	eth_hw_addr_random(dev);
119 
120 	dev->min_mtu = 0;
121 	dev->max_mtu = 0;
122 }
123 
124 static int dummy_validate(struct nlattr *tb[], struct nlattr *data[],
125 			  struct netlink_ext_ack *extack)
126 {
127 	if (tb[IFLA_ADDRESS]) {
128 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
129 			return -EINVAL;
130 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
131 			return -EADDRNOTAVAIL;
132 	}
133 	return 0;
134 }
135 
136 static struct rtnl_link_ops dummy_link_ops __read_mostly = {
137 	.kind		= DRV_NAME,
138 	.setup		= dummy_setup,
139 	.validate	= dummy_validate,
140 };
141 
142 /* Number of dummy devices to be set up by this module. */
143 module_param(numdummies, int, 0);
144 MODULE_PARM_DESC(numdummies, "Number of dummy pseudo devices");
145 
146 static int __init dummy_init_one(void)
147 {
148 	struct net_device *dev_dummy;
149 	int err;
150 
151 	dev_dummy = alloc_netdev(0, "dummy%d", NET_NAME_ENUM, dummy_setup);
152 	if (!dev_dummy)
153 		return -ENOMEM;
154 
155 	dev_dummy->rtnl_link_ops = &dummy_link_ops;
156 	err = register_netdevice(dev_dummy);
157 	if (err < 0)
158 		goto err;
159 	return 0;
160 
161 err:
162 	free_netdev(dev_dummy);
163 	return err;
164 }
165 
166 static int __init dummy_init_module(void)
167 {
168 	int i, err = 0;
169 
170 	down_write(&pernet_ops_rwsem);
171 	rtnl_lock();
172 	err = __rtnl_link_register(&dummy_link_ops);
173 	if (err < 0)
174 		goto out;
175 
176 	for (i = 0; i < numdummies && !err; i++) {
177 		err = dummy_init_one();
178 		cond_resched();
179 	}
180 	if (err < 0)
181 		__rtnl_link_unregister(&dummy_link_ops);
182 
183 out:
184 	rtnl_unlock();
185 	up_write(&pernet_ops_rwsem);
186 
187 	return err;
188 }
189 
190 static void __exit dummy_cleanup_module(void)
191 {
192 	rtnl_link_unregister(&dummy_link_ops);
193 }
194 
195 module_init(dummy_init_module);
196 module_exit(dummy_cleanup_module);
197 MODULE_LICENSE("GPL");
198 MODULE_DESCRIPTION("Dummy netdevice driver which discards all packets sent to it");
199 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
200