dummy -- support hardware address
Subsystems (3)
PATCH 1/1 dummy -- support hardware address
Dummy device comes in handy for testing, but it always has address of all zero's which isn't a valid ethernet address. This generates a simple fake address and allows changing the address.
drivers/net/dummy.c +19 -0
@@ -86,6 +86,15 @@return dev->priv; } + +static int dummy_set_address (struct net_device *dev,void *p) +{ +struct sockaddr *sa = p; + +memcpy (dev->dev_addr, sa->sa_data, dev->addr_len); +return 0 ; +} +static struct net_device **dummies;/* Number of dummy devices to be set up by this module. */
@@ -101,6 +110,16 @@if (!dev_dummy)return -ENOMEM ; + +/* Generate intial fake hardware address */ + dev_dummy->dev_addr[0 ] =0x00 ; + dev_dummy->dev_addr[1 ] =0xdd ; + dev_dummy->dev_addr[2 ] =0xdd ; + dev_dummy->dev_addr[3 ] = index >>16 ; + dev_dummy->dev_addr[4 ] = index >>8 ; + dev_dummy->dev_addr[5 ] = index; + + dev_dummy->set_mac_address = dummy_set_address;if ((err =register_netdev (dev_dummy))) {free_netdev (dev_dummy);
Stephen, why don't you do what usbnet does? Look at usbnet_init() where it goes:
get_random_bytes (node_id, sizeof node_id); node_id [0] &= 0xfe; // clear multicast bit node_id [0] |= 0x02; // set local assignment bit (IEEE802)
I distinctly remember Alan Cox saying this was the right way do this and avoid conflicts with Vendor assigned IDs.
Quoted 10 lines · On Thu, 8 Apr 2004 14:54:03 -0700 "David S. Miller" <[email protected]> wrote:
Stephen, why don't you do what usbnet does? Look at
usbnet_init()where it goes:get_random_bytes (node_id, sizeof node_id); node_id [0] &= 0xfe; // clear multicast bit node_id [0] |= 0x02; // set local assignment bit (IEEE802)I distinctly remember Alan Cox saying this was the right way do this and avoid conflicts with Vendor assigned IDs.
That would work better, just worried about draining the entropy pool when I create 1000 dummies in a test script.
PATCH 1/1 (1/4) add random_ether_addr to ether_device.h
In reply to David S. MillerSince generating a random ethernet address needs to be done in several drivers, add a random_ether_addr function to etherdevice.h
include/linux/etherdevice.h +14 -0
@@ -25,6 +25,7 @@#define _LINUX_ETHERDEVICE_H#include <linux/if_ether.h> +#include <linux/random.h> #ifdef __KERNEL__ extern int eth_header(struct sk_buff *skb, struct net_device *dev,
@@ -64,6 +65,19 @@return !(addr[0 ]&1 ) &&memcmp ( addr, zaddr,6 ); } +/** +* random_ether_addr - Generate software assigned random Ethernet address +* @addr: Pointer to a six-byte array containing the Ethernet address +* +* Generate a random Ethernet address (MAC) that is not multicast +* and has the local assigned bit set. +*/ +static inline void random_ether_addr (u8 *addr) +{ +get_random_bytes (addr,ETH_ALEN ); + addr [0 ] &=0xfe ;/* clear multicast bit */ + addr [0 ] |=0x02 ;/* set local assignment bit (IEEE802) */ +}#endif #endif /* _LINUX_ETHERDEVICE_H */
PATCH 1/1 (3/4) usb gadget -- use random_ether_addr
In reply to David S. MillerUse new common code in ether_device.h for random_ether_addr. Same exact code just in one common place.
drivers/usb/gadget/ether.c +2 -6
@@ -1804,17 +1804,13 @@/* one random address for the gadget device ... both of these could * reasonably come from an id prom or a module parameter. */ -get_random_bytes (net->dev_addr,ETH_ALEN ); - net->dev_addr [0 ] &=0xfe ;// clear multicast bit - net->dev_addr [0 ] |=0x02 ;// set local assignment bit (IEEE802) +random_ether_addr (net->dev_addr);#ifdef DEV_CONFIG_CDC /* ... another address for the host, on the other end of the * link, gets exported through CDC (see CDC spec table 41) */ -get_random_bytes (node_id,sizeof node_id); - node_id [0 ] &=0xfe ;// clear multicast bit - node_id [0 ] |=0x02 ;// set local assignment bit (IEEE802) +random_ether_addr (node_id);snprintf (ethaddr,sizeof ethaddr,"%02X%02X%02X%02X%02X%02X" , node_id [0 ], node_id [1 ], node_id [2 ], node_id [3 ], node_id [4 ], node_id [5 ]);
PATCH 1/1 (2/4) set random address in dummy
In reply to David S. MillerFix several things in dummy device to make it more useable.
* add MODULE_PARAM_DESC for num_dummies
* set initial random ether address
* allow changing address
drivers/net/dummy.c +15 -0
@@ -32,6 +32,7 @@#include <linux/module.h> #include <linux/kernel.h> #include <linux/netdevice.h> +#include <linux/etherdevice.h> #include <linux/init.h> #include <linux/moduleparam.h>
@@ -40,6 +41,17 @@static int dummy_xmit (struct sk_buff *skb,struct net_device *dev);static struct net_device_stats *dummy_get_stats (struct net_device *dev); +static int dummy_set_address (struct net_device *dev,void *p) +{ +struct sockaddr *sa = p; + +if (!is_valid_ether_addr (sa->sa_data)) +return -EADDRNOTAVAIL ; + +memcpy (dev->dev_addr, sa->sa_data,ETH_ALEN ); +return 0 ; +} +/* fake multicast ability */ static void set_multicast_list (struct net_device *dev) {
@@ -58,6 +70,7 @@ dev->get_stats = dummy_get_stats; dev->hard_start_xmit = dummy_xmit; dev->set_multicast_list = set_multicast_list; + dev->set_mac_address = dummy_set_address;#ifdef CONFIG_NET_FASTROUTE dev->accept_fastpath = dummy_accept_fastpath;#endif
@@ -68,6 +81,7 @@ dev->flags |=IFF_NOARP ; dev->flags &= ~IFF_MULTICAST ;SET_MODULE_OWNER (dev); +random_ether_addr (dev->dev_addr); }static int dummy_xmit (struct sk_buff *skb,struct net_device *dev)
@@ -90,6 +104,7 @@/* Number of dummy devices to be set up by this module. */ module_param (numdummies, int,0 ); +MODULE_PARM_DESC (numdimmies,"Number of dummy psuedo devices" );static int __init dummy_init_one (int index) {
PATCH 1/1 (4/4) usbnet -- use random_ether_addr
In reply to David S. MillerSame code to generate random_ether_address is in ether_device.h (patch1) so use it for usbnet.
drivers/usb/net/usbnet.c +1 -3
@@ -3414,9 +3414,7 @@ < sizeof (struct cdc_state)));#endif -get_random_bytes (node_id,sizeof node_id); - node_id [0 ] &=0xfe ;// clear multicast bit - node_id [0 ] |=0x02 ;// set local assignment bit (IEEE802) +random_ether_addr (node_id);return usb_register (&usbnet_driver); }
Re: [PATCH] (4/4) usbnet -- use random_ether_addr
All 4 patches applied, thanks Stephen.