netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Pratt <mcpratt@protonmail.com>
To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Paolo Abeni <pabeni@redhat.com>, Rafal Milecki <zajec5@gmail.com>,
	Christian Marangi <ansuelsmth@gmail.com>,
	Michael Pratt <mcpratt@pm.me>
Subject: [PATCH 2/2] mac_pton: support MAC addresses without delimiters
Date: Mon, 02 Oct 2023 23:40:07 +0000	[thread overview]
Message-ID: <20231002233946.16703-3-mcpratt@protonmail.com> (raw)
In-Reply-To: <20231002233946.16703-1-mcpratt@protonmail.com>

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



  parent reply	other threads:[~2023-10-02 23:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Michael Pratt [this message]
2023-10-03 14:17 ` [PATCH v1 0/2] mac_pton: support more MAC address formats Simon Horman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231002233946.16703-3-mcpratt@protonmail.com \
    --to=mcpratt@protonmail.com \
    --cc=ansuelsmth@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcpratt@pm.me \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=zajec5@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).