[U-Boot] [PATCHv6 11/28] net: core: Add MAC address helper functions

Olliver Schinagl oliver at schinagl.nl
Mon May 15 08:02:27 UTC 2017


Universally administered and locally administered addresses are
distinguished by setting the second-least-significant bit of the first
octet of the address. Having a function to check and set this U/L bit
from a function makes it nice for boards that want to generate their own
mac address to ensure they are locally administered.

Unicast and multicast addresses are distinguised by setting the
least-significant bit of the first octet of the address. Having a
function to check and set this U/M bit from a function it nice to
make a generated mac address a unicast address.

This patch introduces both these helper functions

Signed-off-by: Olliver Schinagl <oliver at schinagl.nl>
---
 include/net.h | 43 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/include/net.h b/include/net.h
index ad2a8a3678..00677a0a89 100644
--- a/include/net.h
+++ b/include/net.h
@@ -767,6 +767,9 @@ static inline int is_zero_ethaddr(const u8 *addr)
 	return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
 }
 
+/* IEEE802 Unicast/Multicast bit */
+#define IEEE802_UM_BIT 0x1
+
 /**
  * is_multicast_ethaddr - Determine if the Ethernet address is a multicast.
  * @addr: Pointer to a six-byte array containing the Ethernet address
@@ -776,7 +779,41 @@ static inline int is_zero_ethaddr(const u8 *addr)
  */
 static inline int is_multicast_ethaddr(const u8 *addr)
 {
-	return 0x01 & addr[0];
+	return addr[0] & IEEE802_UM_BIT;
+}
+
+/**
+ * set_unicast_ethaddr - Make the supplied Ethernet address an unicast.
+ * @addr:	Pointer to a six-byte array containing the Ethernet address
+ */
+static inline void set_unicast_ethaddr(u8 *addr)
+{
+	addr[0] &= ~IEEE802_UM_BIT;
+}
+
+/* IEEE802 Universal/Local administration bit */
+#define IEEE802_UL_BIT 0x02
+
+/**
+ * is_local_ethaddr - Determine if the Ethernet address is a locally
+ *		      administered MAC address.
+ * @addr:	Pointer to a six-byte array containing the Ethernet address
+ *
+ * Return true if the address is a locally administered address.
+ */
+static inline int is_local_ethaddr(const u8 *addr)
+{
+	return addr[0] & IEEE802_UL_BIT;
+}
+
+/**
+ * set_local_ethaddr - Make the supplied Ethernet address a locally
+ * 		       administered one.
+ * @addr:	Pointer to a six-byte array containing the Ethernet address
+ */
+static inline void set_local_ethaddr(u8 *addr)
+{
+	addr[0] |= IEEE802_UL_BIT;
 }
 
 /*
@@ -822,8 +859,8 @@ static inline void net_random_ethaddr(uchar *addr)
 	for (i = 0; i < ARP_HLEN; i++)
 		addr[i] = rand_r(&seed);
 
-	addr[0] &= 0xfe;	/* clear multicast bit */
-	addr[0] |= 0x02;	/* set local assignment bit (IEEE802) */
+	addr[0] &= ~IEEE802_UM_BIT;
+	set_local_ethaddr(addr);
 }
 
 /* Convert an IP address to a string */
-- 
2.11.0



More information about the U-Boot mailing list