[PATCH 2/2] mac_pton: support MAC addresses without delimiters
From: Michael Pratt
Date: Mon Oct 02 2023 - 19:40:26 EST
From: Michael Pratt <mcpratt@xxxxx>
Some network hardware vendors may do something unique
when storing the MAC address into hardware in ASCII,
like leaving out delimiters in order to avoid
using more than a single 16-byte logical addressing line.
Allow parsing of MAC addresses without a delimiter.
Signed-off-by: Michael Pratt <mcpratt@xxxxx>
---
lib/net_utils.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/lib/net_utils.c b/lib/net_utils.c
index ecb7625e1dec..f5fd1926af59 100644
--- a/lib/net_utils.c
+++ b/lib/net_utils.c
@@ -7,9 +7,14 @@
bool mac_pton(const char *s, u8 *mac)
{
+ size_t minlen = 2 * ETH_ALEN;
size_t maxlen = 3 * ETH_ALEN - 1;
int i;
+ /* AABBCCDDEEFF */
+ if (strnlen(s, maxlen) == minlen)
+ goto no_delim;
+
/* XX:XX:XX:XX:XX:XX */
if (strnlen(s, maxlen) < maxlen)
return false;
@@ -25,5 +30,15 @@ bool mac_pton(const char *s, u8 *mac)
mac[i] = (hex_to_bin(s[i * 3]) << 4) | hex_to_bin(s[i * 3 + 1]);
}
return true;
+
+no_delim:
+ for (i = 0; i < minlen; i++) {
+ if (!isxdigit(s[i]))
+ return false;
+ }
+ for (i = 0; i < ETH_ALEN; i++) {
+ mac[i] = (hex_to_bin(s[i * 2]) << 4) | hex_to_bin(s[i * 2 + 1]);
+ }
+ return true;
}
EXPORT_SYMBOL(mac_pton);
--
2.30.2