netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 0/2] mac_pton: support more MAC address formats
@ 2023-10-02 23:39 Michael Pratt
  2023-10-02 23:40 ` [PATCH 1/2] mac_pton: support MAC addresses with other delimiters Michael Pratt
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Michael Pratt @ 2023-10-02 23:39 UTC (permalink / raw)
  To: netdev, linux-kernel
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Rafal Milecki,
	Christian Marangi

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).




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

* [PATCH 1/2] mac_pton: support MAC addresses with other delimiters
  2023-10-02 23:39 [PATCH v1 0/2] mac_pton: support more MAC address formats Michael Pratt
@ 2023-10-02 23:40 ` Michael Pratt
  2023-10-03 16:58   ` Stephen Hemminger
  2023-10-02 23:40 ` [PATCH 2/2] mac_pton: support MAC addresses without delimiters Michael Pratt
  2023-10-03 14:17 ` [PATCH v1 0/2] mac_pton: support more MAC address formats Simon Horman
  2 siblings, 1 reply; 5+ messages in thread
From: Michael Pratt @ 2023-10-02 23:40 UTC (permalink / raw)
  To: netdev, linux-kernel
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Rafal Milecki,
	Christian Marangi, Michael Pratt

From: Michael Pratt <mcpratt@pm.me>

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

Signed-off-by: Michael Pratt <mcpratt@pm.me>
---
 lib/net_utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/net_utils.c b/lib/net_utils.c
index 42bb0473fb22..ecb7625e1dec 100644
--- a/lib/net_utils.c
+++ b/lib/net_utils.c
@@ -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++) {
-- 
2.30.2



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

* [PATCH 2/2] mac_pton: support MAC addresses without delimiters
  2023-10-02 23:39 [PATCH v1 0/2] mac_pton: support more MAC address formats Michael Pratt
  2023-10-02 23:40 ` [PATCH 1/2] mac_pton: support MAC addresses with other delimiters Michael Pratt
@ 2023-10-02 23:40 ` Michael Pratt
  2023-10-03 14:17 ` [PATCH v1 0/2] mac_pton: support more MAC address formats Simon Horman
  2 siblings, 0 replies; 5+ messages in thread
From: Michael Pratt @ 2023-10-02 23:40 UTC (permalink / raw)
  To: netdev, linux-kernel
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Rafal Milecki,
	Christian Marangi, Michael Pratt

From: Michael Pratt <mcpratt@pm.me>

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@pm.me>
---
 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



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

* Re: [PATCH v1 0/2] mac_pton: support more MAC address formats
  2023-10-02 23:39 [PATCH v1 0/2] mac_pton: support more MAC address formats Michael Pratt
  2023-10-02 23:40 ` [PATCH 1/2] mac_pton: support MAC addresses with other delimiters Michael Pratt
  2023-10-02 23:40 ` [PATCH 2/2] mac_pton: support MAC addresses without delimiters Michael Pratt
@ 2023-10-03 14:17 ` Simon Horman
  2 siblings, 0 replies; 5+ messages in thread
From: Simon Horman @ 2023-10-03 14:17 UTC (permalink / raw)
  To: Michael Pratt
  Cc: netdev, linux-kernel, David S. Miller, Eric Dumazet, Paolo Abeni,
	Rafal Milecki, Christian Marangi

On Mon, Oct 02, 2023 at 11:39:55PM +0000, Michael Pratt wrote:
> 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).

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.

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

* Re: [PATCH 1/2] mac_pton: support MAC addresses with other delimiters
  2023-10-02 23:40 ` [PATCH 1/2] mac_pton: support MAC addresses with other delimiters Michael Pratt
@ 2023-10-03 16:58   ` Stephen Hemminger
  0 siblings, 0 replies; 5+ messages in thread
From: Stephen Hemminger @ 2023-10-03 16:58 UTC (permalink / raw)
  To: Michael Pratt
  Cc: netdev, linux-kernel, David S. Miller, Eric Dumazet, Paolo Abeni,
	Rafal Milecki, Christian Marangi, Michael Pratt

On Mon, 02 Oct 2023 23:40:02 +0000
Michael Pratt <mcpratt@protonmail.com> wrote:

> From: Michael Pratt <mcpratt@pm.me>
> 
> 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
> 
> Signed-off-by: Michael Pratt <mcpratt@pm.me>
> ---
>  lib/net_utils.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/lib/net_utils.c b/lib/net_utils.c
> index 42bb0473fb22..ecb7625e1dec 100644
> --- a/lib/net_utils.c
> +++ b/lib/net_utils.c
> @@ -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.

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

end of thread, other threads:[~2023-10-03 16:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-02 23:39 [PATCH v1 0/2] mac_pton: support more MAC address formats Michael Pratt
2023-10-02 23:40 ` [PATCH 1/2] mac_pton: support MAC addresses with other delimiters Michael Pratt
2023-10-03 16:58   ` Stephen Hemminger
2023-10-02 23:40 ` [PATCH 2/2] mac_pton: support MAC addresses without delimiters Michael Pratt
2023-10-03 14:17 ` [PATCH v1 0/2] mac_pton: support more MAC address formats Simon Horman

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).