Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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