Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/module.h> |
| 33 | #include <linux/kernel.h> |
| 34 | #include <linux/netdevice.h> |
| 35 | #include <linux/etherdevice.h> |
Julian Wiedmann | abe9fd5 | 2019-04-12 13:06:13 +0200 | [diff] [blame] | 36 | #include <linux/ethtool.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/init.h> |
| 38 | #include <linux/moduleparam.h> |
Patrick McHardy | 206c9fb | 2007-06-13 12:04:20 -0700 | [diff] [blame] | 39 | #include <linux/rtnetlink.h> |
Ezequiel Lara Gomez | 6df014c | 2017-03-11 20:06:54 +0000 | [diff] [blame] | 40 | #include <linux/net_tstamp.h> |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 41 | #include <net/rtnetlink.h> |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 42 | #include <linux/u64_stats_sync.h> |
Patrick McHardy | 206c9fb | 2007-06-13 12:04:20 -0700 | [diff] [blame] | 43 | |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 44 | #define DRV_NAME "dummy" |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static int numdummies = 1; |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* fake multicast ability */ |
| 49 | static void set_multicast_list(struct net_device *dev) |
| 50 | { |
| 51 | } |
| 52 | |
stephen hemminger | bc1f447 | 2017-01-06 19:12:52 -0800 | [diff] [blame] | 53 | static void dummy_get_stats64(struct net_device *dev, |
| 54 | struct rtnl_link_stats64 *stats) |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 55 | { |
Eric Dumazet | 4a43b1f | 2019-11-07 16:27:19 -0800 | [diff] [blame] | 56 | dev_lstats_read(dev, &stats->tx_packets, &stats->tx_bytes); |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 57 | } |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 58 | |
| 59 | static netdev_tx_t dummy_xmit(struct sk_buff *skb, struct net_device *dev) |
| 60 | { |
Eric Dumazet | 4a43b1f | 2019-11-07 16:27:19 -0800 | [diff] [blame] | 61 | dev_lstats_add(dev, skb->len); |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 62 | |
Ezequiel Lara Gomez | 6df014c | 2017-03-11 20:06:54 +0000 | [diff] [blame] | 63 | skb_tx_timestamp(skb); |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 64 | dev_kfree_skb(skb); |
| 65 | return NETDEV_TX_OK; |
| 66 | } |
| 67 | |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 68 | static int dummy_dev_init(struct net_device *dev) |
| 69 | { |
Eric Dumazet | 4a43b1f | 2019-11-07 16:27:19 -0800 | [diff] [blame] | 70 | dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats); |
| 71 | if (!dev->lstats) |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 72 | return -ENOMEM; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
Hiroaki SHIMODA | 890fdf2 | 2012-04-15 13:26:01 +0000 | [diff] [blame] | 77 | static void dummy_dev_uninit(struct net_device *dev) |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 78 | { |
Eric Dumazet | 4a43b1f | 2019-11-07 16:27:19 -0800 | [diff] [blame] | 79 | free_percpu(dev->lstats); |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Jiri Pirko | 210ab66 | 2012-12-27 23:49:40 +0000 | [diff] [blame] | 82 | static int dummy_change_carrier(struct net_device *dev, bool new_carrier) |
| 83 | { |
| 84 | if (new_carrier) |
| 85 | netif_carrier_on(dev); |
| 86 | else |
| 87 | netif_carrier_off(dev); |
| 88 | return 0; |
| 89 | } |
| 90 | |
Stephen Hemminger | aa18e9e | 2008-11-20 20:28:00 -0800 | [diff] [blame] | 91 | static const struct net_device_ops dummy_netdev_ops = { |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 92 | .ndo_init = dummy_dev_init, |
Hiroaki SHIMODA | 890fdf2 | 2012-04-15 13:26:01 +0000 | [diff] [blame] | 93 | .ndo_uninit = dummy_dev_uninit, |
Stephen Hemminger | aa18e9e | 2008-11-20 20:28:00 -0800 | [diff] [blame] | 94 | .ndo_start_xmit = dummy_xmit, |
| 95 | .ndo_validate_addr = eth_validate_addr, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 96 | .ndo_set_rx_mode = set_multicast_list, |
Jiri Pirko | 0d1632b | 2012-06-29 05:10:08 +0000 | [diff] [blame] | 97 | .ndo_set_mac_address = eth_mac_addr, |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 98 | .ndo_get_stats64 = dummy_get_stats64, |
Jiri Pirko | 210ab66 | 2012-12-27 23:49:40 +0000 | [diff] [blame] | 99 | .ndo_change_carrier = dummy_change_carrier, |
Stephen Hemminger | aa18e9e | 2008-11-20 20:28:00 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 102 | static void dummy_get_drvinfo(struct net_device *dev, |
| 103 | struct ethtool_drvinfo *info) |
| 104 | { |
| 105 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static const struct ethtool_ops dummy_ethtool_ops = { |
| 109 | .get_drvinfo = dummy_get_drvinfo, |
Julian Wiedmann | abe9fd5 | 2019-04-12 13:06:13 +0200 | [diff] [blame] | 110 | .get_ts_info = ethtool_op_get_ts_info, |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 111 | }; |
| 112 | |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 113 | static void dummy_setup(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
Stephen Hemminger | aa18e9e | 2008-11-20 20:28:00 -0800 | [diff] [blame] | 115 | ether_setup(dev); |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /* Initialize the device structure. */ |
Stephen Hemminger | aa18e9e | 2008-11-20 20:28:00 -0800 | [diff] [blame] | 118 | dev->netdev_ops = &dummy_netdev_ops; |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 119 | dev->ethtool_ops = &dummy_ethtool_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 120 | dev->needs_free_netdev = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | /* Fill in device structure with ethernet-generic values. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | dev->flags |= IFF_NOARP; |
| 124 | dev->flags &= ~IFF_MULTICAST; |
Phil Sutter | ff42c02 | 2015-08-18 10:30:30 +0200 | [diff] [blame] | 125 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; |
Eric Dumazet | 8f3af27 | 2015-10-19 20:17:59 -0700 | [diff] [blame] | 126 | dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST; |
David S. Miller | 2082499 | 2017-07-03 06:36:07 -0700 | [diff] [blame] | 127 | dev->features |= NETIF_F_ALL_TSO; |
Michał Mirosław | 34324dc | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 128 | dev->features |= NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_LLTX; |
Eric Dumazet | 8f3af27 | 2015-10-19 20:17:59 -0700 | [diff] [blame] | 129 | dev->features |= NETIF_F_GSO_ENCAP_ALL; |
| 130 | dev->hw_features |= dev->features; |
| 131 | dev->hw_enc_features |= dev->features; |
Danny Kukawka | 7ce5d22 | 2012-02-15 06:45:40 +0000 | [diff] [blame] | 132 | eth_hw_addr_random(dev); |
Zhang Shengju | 25e3e84 | 2016-12-07 17:41:33 +0800 | [diff] [blame] | 133 | |
| 134 | dev->min_mtu = 0; |
Zhang Shengju | e94cd81 | 2017-09-22 23:57:49 +0800 | [diff] [blame] | 135 | dev->max_mtu = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 137 | |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 138 | static int dummy_validate(struct nlattr *tb[], struct nlattr *data[], |
| 139 | struct netlink_ext_ack *extack) |
Patrick McHardy | 0e06877 | 2007-07-11 19:42:31 -0700 | [diff] [blame] | 140 | { |
| 141 | if (tb[IFLA_ADDRESS]) { |
| 142 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 143 | return -EINVAL; |
| 144 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 145 | return -EADDRNOTAVAIL; |
| 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 150 | static struct rtnl_link_ops dummy_link_ops __read_mostly = { |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 151 | .kind = DRV_NAME, |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 152 | .setup = dummy_setup, |
Patrick McHardy | 0e06877 | 2007-07-11 19:42:31 -0700 | [diff] [blame] | 153 | .validate = dummy_validate, |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* Number of dummy devices to be set up by this module. */ |
| 157 | module_param(numdummies, int, 0); |
| 158 | MODULE_PARM_DESC(numdummies, "Number of dummy pseudo devices"); |
| 159 | |
Patrick McHardy | 206c9fb | 2007-06-13 12:04:20 -0700 | [diff] [blame] | 160 | static int __init dummy_init_one(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
| 162 | struct net_device *dev_dummy; |
| 163 | int err; |
| 164 | |
Jakub Kicinski | c336161 | 2017-12-01 15:09:02 -0800 | [diff] [blame] | 165 | dev_dummy = alloc_netdev(0, "dummy%d", NET_NAME_ENUM, dummy_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if (!dev_dummy) |
| 167 | return -ENOMEM; |
| 168 | |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 169 | dev_dummy->rtnl_link_ops = &dummy_link_ops; |
| 170 | err = register_netdevice(dev_dummy); |
| 171 | if (err < 0) |
| 172 | goto err; |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 173 | return 0; |
| 174 | |
| 175 | err: |
| 176 | free_netdev(dev_dummy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return err; |
| 178 | } |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | static int __init dummy_init_module(void) |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 181 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | int i, err = 0; |
Patrick McHardy | 206c9fb | 2007-06-13 12:04:20 -0700 | [diff] [blame] | 183 | |
Kirill Tkhai | 554873e | 2018-03-30 19:38:37 +0300 | [diff] [blame] | 184 | down_write(&pernet_ops_rwsem); |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 185 | rtnl_lock(); |
| 186 | err = __rtnl_link_register(&dummy_link_ops); |
dingtianhong | 2c8a018 | 2013-07-11 19:04:02 +0800 | [diff] [blame] | 187 | if (err < 0) |
| 188 | goto out; |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 189 | |
Eric Dumazet | 16b0dc2 | 2012-06-10 21:11:57 +0000 | [diff] [blame] | 190 | for (i = 0; i < numdummies && !err; i++) { |
Patrick McHardy | 206c9fb | 2007-06-13 12:04:20 -0700 | [diff] [blame] | 191 | err = dummy_init_one(); |
Eric Dumazet | 16b0dc2 | 2012-06-10 21:11:57 +0000 | [diff] [blame] | 192 | cond_resched(); |
| 193 | } |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 194 | if (err < 0) |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 195 | __rtnl_link_unregister(&dummy_link_ops); |
dingtianhong | 2c8a018 | 2013-07-11 19:04:02 +0800 | [diff] [blame] | 196 | |
| 197 | out: |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 198 | rtnl_unlock(); |
Kirill Tkhai | 554873e | 2018-03-30 19:38:37 +0300 | [diff] [blame] | 199 | up_write(&pernet_ops_rwsem); |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | return err; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | static void __exit dummy_cleanup_module(void) |
| 205 | { |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 206 | rtnl_link_unregister(&dummy_link_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | module_init(dummy_init_module); |
| 210 | module_exit(dummy_cleanup_module); |
| 211 | MODULE_LICENSE("GPL"); |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 212 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); |