Skip to contentSkip to conversation

mac_pton: support more MAC address formats

PATCH In review Michael Pratt 3 participants 5 messages Oct 2, 2023 – Oct 3, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover mac_pton: support more MAC address formats

Currently, mac_pton() strictly requires the standard ASCII MAC address format with colons as the delimiter, however,
some hardware vendors don't store the address like that.

If there is no delimiter, one could use strtoul() but that would leave out important checks to make sure that each character in the string is hexadecimal.

This series adds support for other delimiters and lack of any delimiter to the mac_pton() function.

Tested with Openwrt on a MIPS system (ar9344).

#1 lore raw

PATCH 1/2 mac_pton: support MAC addresses with other delimiters

From: Michael Pratt <[email protected]>

Some network hardware vendors may do something unique when storing the MAC address into hardware in ASCII, like using hyphens as the delimiter.

Allow parsing of MAC addresses with a non-standard delimiter (punctuation other than a colon).

e.g. aa-bb-cc-dd-ee-ff

---
 lib/net_utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/net_utils.c +1 -1
index 42bb0473fb22..ecb7625e1dec 100644
@@ -18,7 +18,7 @@ bool mac_pton(const char *s, u8 *mac)
 	for (i = 0; i < ETH_ALEN; i++) {
 		if (!isxdigit(s[i * 3]) || !isxdigit(s[i * 3 + 1]))
 			return false;
-		if (i != ETH_ALEN - 1 && s[i * 3 + 2] != ':')
+		if (i != ETH_ALEN - 1 && !ispunct(s[i * 3 + 2]))
 			return false;
 	}
 	for (i = 0; i < ETH_ALEN; i++) {
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 1/2] mac_pton: support MAC addresses with other delimiters

Quoted 21 lines · On Mon, 02 Oct 2023 23:40:02 +0000 Michael Pratt <[email protected]> wrote:

From: Michael Pratt <[email protected]>

Some network hardware vendors may do something unique when storing the MAC address into hardware in ASCII, like using hyphens as the delimiter.

Allow parsing of MAC addresses with a non-standard delimiter (punctuation other than a colon).

e.g. aa-bb-cc-dd-ee-ff

---
 lib/net_utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/net_utils.c +0 -0
index 42bb0473fb22..ecb7625e1dec 100644

lib/net_utils.c bool mac_pton(const char *s, u8 *mac) · line 21

Quoted 5 lines
lib/net_utils.c +1 -1
@@ -18,7 +18,7 @@ bool mac_pton(const char *s, u8 *mac)
 	for (i = 0; i < ETH_ALEN; i++) {
 		if (!isxdigit(s[i * 3]) || !isxdigit(s[i * 3 + 1]))
 			return false;
-		if (i != ETH_ALEN - 1 && s[i * 3 + 2] != ':')
+		if (i != ETH_ALEN - 1 && !ispunct(s[i * 3 + 2]))

Having looked at same thing in DPDK already, this looks overly broad. There are only two common formats in the standards (isn't it fun when standards disagree). IETF uses colon separator and IEEE uses hyphen separator. Linux convention is colon, and Windows convention is hyphen. There is also the old Cisco 3 part format with periods but adding that makes no sense.

Also, it would be bad to allow bogus values where two different types of punctuation are used.

#3 lore raw

PATCH 2/2 mac_pton: support MAC addresses without delimiters

In reply to Michael Pratt

From: Michael Pratt <[email protected]>

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.

---
 lib/net_utils.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
lib/net_utils.c +15 -0
index ecb7625e1dec..f5fd1926af59 100644
@@ -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);
Signature · 1 line
2.30.2
reviewer
#4 lore raw
In reply to Michael Pratt
Quoted 4 lines · Michael Pratt

Currently, mac_pton() strictly requires the standard ASCII MAC address format with colons as the delimiter, however,
some hardware vendors don't store the address like that.

Quoted 8 lines

If there is no delimiter, one could use strtoul() but that would leave out important checks to make sure that each character in the string is hexadecimal.

This series adds support for other delimiters and lack of any delimiter to the mac_pton() function.

Tested with Openwrt on a MIPS system (ar9344).

Hi Michael,

I am wondering if you considered a different approach where, via parameters and/or new helpers, callers can specify the delimiter, or absence thereof.