[PATCH v2 09/10] net: lib: add ether_addr_inc() helper

Oleksij Rempel o.rempel at pengutronix.de
Mon Oct 2 03:16:53 PDT 2023


Add helper function to calculate Ethernet address by incriminating it.
This helper can be used for multiport devices like switches to generate
address for each port based on one stored address.

Signed-off-by: Oleksij Rempel <o.rempel at pengutronix.de>
---
 include/net.h |  1 +
 net/lib.c     | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/include/net.h b/include/net.h
index a0ef8bee04..fe82451cc9 100644
--- a/include/net.h
+++ b/include/net.h
@@ -360,6 +360,7 @@ int setenv_ip(const char *name, IPaddr_t ip);
 
 int string_to_ethaddr(const char *str, u8 enetaddr[6]);
 void ethaddr_to_string(const u8 enetaddr[6], char *str);
+void ether_addr_inc(u8 *dst_addr, const u8 *src_addr, u32 increment);
 
 #ifdef CONFIG_NET_RESOLV
 int resolv(const char *host, IPaddr_t *ip);
diff --git a/net/lib.c b/net/lib.c
index d4536441bd..dc7d83bc6d 100644
--- a/net/lib.c
+++ b/net/lib.c
@@ -44,6 +44,31 @@ void ethaddr_to_string(const u8 enetaddr[ETH_ALEN], char *str)
 		 enetaddr[4], enetaddr[5]);
 }
 
+/**
+ * ether_addr_inc - Increment an Ethernet address.
+ * @dst_addr: Destination address to store the incremented address.
+ * @src_addr: Source address to be incremented.
+ * @increment: Value by which to increment the source address.
+ *
+ * This function increments the given source Ethernet address by
+ * the specified increment value, storing the result in the
+ * destination address.
+ */
+void ether_addr_inc(u8 *dst_addr, const u8 *src_addr, u32 increment)
+{
+	u32 value;
+	int i;
+
+	for(i = 0; i < 6; ++i)
+		dst_addr[i] = src_addr[i];
+
+	for(i = 5; i >= 0 && increment; --i) {
+		value = dst_addr[i] + increment;
+		dst_addr[i] = value & 0xFF;
+		increment = value >> 8;
+	}
+}
+
 int string_to_ip(const char *s, IPaddr_t *ip)
 {
 	IPaddr_t addr = 0;
-- 
2.39.2




More information about the barebox mailing list