netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next] sysfs: add entry to indicate network interfaces with random MAC address
@ 2010-07-20 10:50 Stefan Assmann
  2010-07-20 11:20 ` Ben Hutchings
  0 siblings, 1 reply; 39+ messages in thread
From: Stefan Assmann @ 2010-07-20 10:50 UTC (permalink / raw)
  To: netdev
  Cc: Linux Kernel Mailing List, davem, Andy Gospodarek,
	Rose, Gregory V, Duyck, Alexander H, Ben Hutchings, Casey Leedom,
	Harald Hoyer

From: Stefan Assmann <sassmann@redhat.com>

Reserve a bit in struct net_device to indicate whether an interface
generates its MAC address randomly, and expose the information via
sysfs.
May look like this:
/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/net/eth0/ifrndmac

By default the value of ifrndmac is 0. Any driver that generates the MAC
address randomly should return a value to 1.

This simplifies the handling of network devices with random MAC addresses
as user-space may just query sysfs to find out if the MAC is real or fake.
Udev may check sysfs for devices that generate their MAC address
randomly and create persistent net rules by using the unique device path
if the value returned is 1.

Also introducing a helper function to assist driver adoption.

Signed-off-by: Stefan Assmann <sassmann@redhat.com>
---
 include/linux/etherdevice.h |   14 ++++++++++++++
 include/linux/netdevice.h   |    1 +
 net/core/net-sysfs.c        |   12 ++++++++++++
 3 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/include/linux/etherdevice.h b/include/linux/etherdevice.h
index 3d7a668..ebb34ac 100644
--- a/include/linux/etherdevice.h
+++ b/include/linux/etherdevice.h
@@ -127,6 +127,20 @@ static inline void random_ether_addr(u8 *addr)
 }

 /**
+ * dev_hw_addr_random - Create random MAC and set device flag
+ * @dev: pointer to net_device structure
+ * @addr: Pointer to a six-byte array containing the Ethernet address
+ *
+ * Generate random MAC to be used by a device and set NETIF_F_RNDMAC flag
+ * so the state can be read by sysfs and be used by udev.
+ */
+static inline void dev_hw_addr_random(struct net_device *dev, u8 *hwaddr)
+{
+	dev->features |= NETIF_F_RNDMAC;
+	random_ether_addr(hwaddr);
+}
+
+/**
  * compare_ether_addr - Compare two Ethernet addresses
  * @addr1: Pointer to a six-byte array containing the Ethernet address
  * @addr2: Pointer other six-byte array containing the Ethernet address
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index b626289..2ea0298 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -845,6 +845,7 @@ struct net_device {
 #define NETIF_F_FCOE_MTU	(1 << 26) /* Supports max FCoE MTU, 2158 bytes*/
 #define NETIF_F_NTUPLE		(1 << 27) /* N-tuple filters supported */
 #define NETIF_F_RXHASH		(1 << 28) /* Receive hashing offload */
+#define NETIF_F_RNDMAC		(1 << 29) /* Interface with random MAC address */

 	/* Segmentation offload features */
 #define NETIF_F_GSO_SHIFT	16
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index d2b5965..91a9808 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -188,6 +188,17 @@ static ssize_t show_dormant(struct device *dev,
 	return -EINVAL;
 }

+static ssize_t show_ifrndmac(struct device *dev,
+			     struct device_attribute *attr, char *buf)
+{
+	struct net_device *net = to_net_dev(dev);
+
+	if (net->features & NETIF_F_RNDMAC)
+		return sprintf(buf, fmt_dec, 1);
+	else
+		return sprintf(buf, fmt_dec, 0);
+}
+
 static const char *const operstates[] = {
 	"unknown",
 	"notpresent", /* currently unused */
@@ -300,6 +311,7 @@ static struct device_attribute net_class_attributes[] = {
 	__ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
 	__ATTR(iflink, S_IRUGO, show_iflink, NULL),
 	__ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
+	__ATTR(ifrndmac, S_IRUGO, show_ifrndmac, NULL),
 	__ATTR(features, S_IRUGO, show_features, NULL),
 	__ATTR(type, S_IRUGO, show_type, NULL),
 	__ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 39+ messages in thread

end of thread, other threads:[~2010-07-25  3:50 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-20 10:50 [PATCH net-next] sysfs: add entry to indicate network interfaces with random MAC address Stefan Assmann
2010-07-20 11:20 ` Ben Hutchings
2010-07-20 11:47   ` Stefan Assmann
2010-07-20 11:58     ` Alex Badea
2010-07-20 12:17       ` Stefan Assmann
2010-07-20 20:18         ` David Miller
2010-07-21  8:10           ` Stefan Assmann
2010-07-21 13:54             ` Ben Hutchings
2010-07-22 12:50               ` [PATCH net-next] sysfs: add attribute to indicate hw address assignment type Stefan Assmann
2010-07-22 14:07                 ` Ben Hutchings
2010-07-22 14:47                   ` Stefan Assmann
2010-07-25  3:50                     ` David Miller
2010-07-20 12:07     ` [PATCH net-next] sysfs: add entry to indicate network interfaces with random MAC address Ben Hutchings
2010-07-20 12:41       ` Stefan Assmann
2010-07-20 14:29         ` Ben Hutchings
2010-07-20 20:17           ` David Miller
2010-07-20 21:18             ` Stephen Hemminger
2010-07-20 21:20               ` David Miller
2010-07-21  6:26                 ` Harald Hoyer
2010-07-21  6:34                   ` David Miller
2010-07-21  6:47                     ` Harald Hoyer
2010-07-21 15:07                       ` Andy Gospodarek
2010-07-21 16:34                         ` Casey Leedom
2010-07-21 17:28                           ` Stephen Hemminger
2010-07-21 17:32                             ` David Miller
2010-07-21 18:29                               ` Casey Leedom
2010-07-21 18:39                                 ` David Miller
2010-07-21 19:25                                   ` Casey Leedom
2010-07-21 18:43                                 ` Rose, Gregory V
2010-07-21 18:48                                   ` David Miller
2010-07-21 18:50                                     ` David Miller
2010-07-21 19:02                                       ` Rose, Gregory V
2010-07-21 19:33                                         ` David Miller
2010-07-21 19:35                                           ` Rose, Gregory V
2010-07-22  7:12                                           ` Ian Campbell
2010-07-22  6:53                                   ` Stefan Assmann
2010-07-23  0:26                                     ` Casey Leedom
2010-07-23  8:08                                       ` Stefan Assmann
2010-07-23 16:35                                         ` Casey Leedom

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).