Article View: linux.kernel.netdev
Article #11960[PATCH v4 1/2] lib/net_utils: allow mac_pton() to take a custom delimiter
From: Liu Chao
Date: Fri, 06 Nov 2026 03:06
Date: Fri, 06 Nov 2026 03:06
83 lines
11480 bytes
11480 bytes
mac_pton() hardcodes ':' as the only accepted separator, which pushes
callers that need a different notation to open-code their own parser.
Split the body out into parse_mac_pton() taking the set of accepted
delimiters as a string, and turn mac_pton() into a static inline wrapper
passing ":" so that every existing caller keeps its current behaviour.
The strnlen() guard still runs before any delimiter is inspected, so
strchr() is never handed a NUL character.
No functional change intended.
Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Liu Chao <liuc63@xiaopeng.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
---
v4: no change.
v3: pick up Andy's Reviewed-by, no code change.
v2: new patch, split out of the u_ether fix at Andy's suggestion.
Link: https://lore.kernel.org/all/20261105072147.3383565-2-liuc63@xiaopeng.com/
include/linux/hex.h | 7 ++++++-
lib/net_utils.c | 17 ++++++++++++++---
2 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/include/linux/hex.h b/include/linux/hex.h
index 2618382e5b0c..408825478cda 100644
--- a/include/linux/hex.h
+++ b/include/linux/hex.h
@@ -30,6 +30,11 @@ extern int hex_to_bin(unsigned char ch);
extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
extern char *bin2hex(char *dst, const void *src, size_t count);
-bool mac_pton(const char *s, u8 *mac);
+bool parse_mac_pton(const char *s, u8 *mac, const char *delim);
+
+static inline bool mac_pton(const char *s, u8 *mac)
+{
+ return parse_mac_pton(s, mac, ":");
+}
#endif
diff --git a/lib/net_utils.c b/lib/net_utils.c
index 215cda672fee..011857cc978b 100644
--- a/lib/net_utils.c
+++ b/lib/net_utils.c
@@ -5,7 +5,18 @@
#include <linux/export.h>
#include <linux/hex.h>
-bool mac_pton(const char *s, u8 *mac)
+/**
+ * parse_mac_pton - convert a MAC address string using a custom delimiter
+ * @s: string to be parsed
+ * @mac: buffer to store the result, left untouched if @s is malformed
+ * @delim: characters accepted as a separator between the byte pairs
+ *
+ * @s must be MAC_ADDR_STR_LEN characters long and use a character from
+ * @delim at every separator position, e.g. "aa.bb:cc.dd:ee.ff" with ".:".
+ *
+ * Return: true on success, false if @s is malformed.
+ */
+bool parse_mac_pton(const char *s, u8 *mac, const char *delim)
{
int i;
@@ -16,7 +27,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 && !strchr(delim, s[i * 3 + 2]))
return false;
}
for (i = 0; i < ETH_ALEN; i++) {
@@ -24,4 +35,4 @@ bool mac_pton(const char *s, u8 *mac)
}
return true;
}
-EXPORT_SYMBOL(mac_pton);
+EXPORT_SYMBOL(parse_mac_pton);
--
2.50.1
Technical Details
Message-ID:
<20261106030605.576584-2-liuc63@xiaopeng.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-357703-linux-kernel-netdev=dewy.ruReferences:
<20261106030605.576584-1-liuc63@xiaopeng.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-357703-linux-kernel-netdev=dewy.ru
From: liuc63@xiaopeng.com (Liu Chao)
Newsgroups: linux.kernel.netdev
Subject: [PATCH v4 1/2] lib/net_utils: allow mac_pton() to take a custom delimiter
Date: Fri, 6 Nov 2026 11:06:04 +0800
Organization: Usenet Network
Message-ID: <20261106030605.576584-2-liuc63@xiaopeng.com>
References: <20261106030605.576584-1-liuc63@xiaopeng.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="3782583"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:Yp4tJNTXXMDctx0lmkclAOB9Vvk= sha256:pY/E1Tqh3HeiwNLVtNOSGSLN/VtZJG599BHtbG9BuQM=
sha1:ijJQSy2GCboDvrzvGU9mCG/q/yk= sha256:kKkOuqwlbyIxyLMJ0IqnkdjivtUybRV85UflMUgsk18=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
by mail.dewy.ru (Postfix) with ESMTP id 7C5FD113130; Fri, 6 Nov 2026 06:09:40 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.05
X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31
tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001]
autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
header.d=xiaopeng.com
X-Received: from mail.dewy.ru ([127.0.0.1])
by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id ZwX5e7cgfnDp;
Fri, 6 Nov 2026 06:09:36 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-357703-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=xiaopeng.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id 1A8A1113108; Fri, 6 Nov 2026 06:09:22 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sea.lore.kernel.org (Postfix) with ESMTP id 220E030571; Fri, 6 Nov 2026 03:06:20 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
t=1793934380;
b=uJMoroZ3PbDGvCAMQqKzz5i19OAq5Aiy5TXc9q17AAzzSmHklt9x+RXDmGWsDmfZpOD8
K7vRgaxcB1lqrW9MAkQ+8IcinUhPd4g5ymShaf8d9gH7Xvetecm1+LcNpP7ttEKTujUso
wlINzneWxwPGKfqRiWCIhpGub6vgivwfBMN+eTTm8FpidxvGlbypsCnISnQbvU1iExvlv
9CV+ln3U/n8wtkWCENS0AH1crZahewihHkc1N6nRFKns+Yh7DZ/3xmAJYGfhNykjpbQ5w
ziTK4psTkXuJHHr5aPj98Ho+wWzeOH5Ag4P8eYFtbZqzRJ7sH2ha6RtEy0SlRYevIuw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793934380;
h=X-Original-To:Received:DMARC-Filter:DKIM-Signature:
X-Alimail-AntiSpam:Received:From:To:Cc:Subject:Date:Message-ID:
X-Mailer:In-Reply-To:References:X-Mailing-List:List-Id:List-Post:
List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
MIME-Version:Content-Transfer-Encoding;
bh=anavCBK3SCp8Ujq//YSmuDMckikQh71WLxxHmaHr4cw=;
b=ug/M0qeZ83f+/xCzUEkFbMcToWi6O5PO2J37YSXzxvAt7c40AzsFQsvXsIQyuBHyRSHE
X0+AqcQj8gKcfD6cNUtmu0ffK7LkbjIAc4+8FEEwaohnBfB412QyDYpPHgk6MvAJCqcub
fuBTKbSAc1G6kNFmRhFwZXt04PV+uc7XBq8MVv3dBkrVaKkRqQy0c6Iwt2FVY2N3T5n3F
idRc9yioCXvBneaKIhQnATUFYw8vl/JD1Y4wBfIfp58ubKkFfcrrADO3Sc4B5nPjwOzRS
0dTd+BzMqZR5axUDuAFbIaobrSCEek+K+6Npjv6PDD1ys4U/N9a/KPPYX3tTqs9P/uA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=bnihwohJ;
dmarc=none header.from=xiaopeng.com;
spf=pass smtp.mailfrom=xiaopeng.com;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id EA2A7606A336;
Fri, 6 Nov 2026 03:06:19 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (1024-bit key, unprotected) header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=bnihwohJ
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.28.4
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1793934373;
b=CGhgwkmF3lnB6KylHnvwuJc8NT60JuoLtvX5HuIQXUiukjeSxAIi3JQyfqZg+BGa6aY4
kv1S/T5T2KGHmNj3uLfCyyCxWP3a6Cyz+b5dxVnC6HMi0lTQsoqgbevNko7hOejk9d4iU
1slLvJZSlLoeKM0yHhKoYORG6ug1CV9p27nNEC7Mb+5bOm33G23MXapz74gNyCgcjIhYj
My+zYE9exN6UfO2n9eeWA1pJ03RJy8a2+fk9INbWwkD8TFzSel58NvvApspp9O6CpwpaS
RMt12O5XzRkNRFy0+T2JFLUqkewfuLlsKqK/oMDZM2MpfUZo+VwPNnz3kQEuWWH9gQQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793934373;
h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:From:To:Cc:
Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
Content-Transfer-Encoding;
bh=anavCBK3SCp8Ujq//YSmuDMckikQh71WLxxHmaHr4cw=;
b=gA+7rsMQOzFA00LW2uaj8sJMd53ddz+5SNQHm9L8jt0KvujFvYkdNghZlkQGgZ14zMtu
9MVlqbRBZRBcmfvZeN50DWey1L4BKpp/s0kWBd+0CpusYh3vdpT6JzrbZBSWb4bZJ+37g
KwaUwlGLt1uJoD9iLYOYIyTUXABcmTmMRl/0ltMe+9Nq4XZadfEfVQsxQdJBvoAiZI+am
Mtjm6E9Q+7El6ckoNfZbxe8bsYunHVI/0kpAgn+QUfG/JNGqlrN1EwELahbzH1aGLXDX9
DSR/s583j0KoXDljmjmdopFBVeeWmcc6Afyx9RaH0VrdUs0sk05kv9uBmcnLoFY5ljA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=none header.from=xiaopeng.com;
spf=pass smtp.mailfrom=xiaopeng.com;
dkim=pass header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=bnihwohJ;
arc=none smtp.remote-ip=115.124.28.4
X-Received: from out28-4.mail.aliyun.com (out28-4.mail.aliyun.com [115.124.28.4])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72C8E606A326;
Fri, 6 Nov 2026 03:06:10 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 72C8E606A326
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=xiaopeng.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xiaopeng.com
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=xiaopeng.com; s=default;
t=1793934369; h=From:To:Subject:Date:Message-ID:MIME-Version;
bh=anavCBK3SCp8Ujq//YSmuDMckikQh71WLxxHmaHr4cw=;
b=bnihwohJEoU/va55tbf0n1YF50PQrQy4yNIADVahhBBY5jfgEdRCYNv9Wp3FOksxRCxTAhYj8MHWhGLynyBc+6SXwjd+jzZFf3t3O563tk+X7SgTHvNXpaMAE28BHQVq4PF6zGpc1py2yLcozxRCWyB62JyTc/APKJJohnWKBzM=
X-Alimail-AntiSpam: AC=CONTINUE;BC=0.07436259|-1;CH=green;DM=|CONTINUE|false|;DS=CONTINUE|ham_system_inform|0.00337272-2.50226e-05-0.996602;FP=18050258625328350392|14|2|19|0|-1|-1|-1;HT=maildocker-contentspam011083099174;MF=liuc63@xiaopeng.com;NM=1;PH=DS;RN=15;RT=15;SR=0;TI=SMTPD_---.jxsfG50_1793934367;
X-Received: from localhost(mailfrom:liuc63@xiaopeng.com fp:SMTPD_---.jxsfG50_1793934367 cluster:ay29)
by smtp.aliyun-inc.com;
Fri, 06 Nov 2026 11:06:08 +0800
X-Cc: Andy Shevchenko <andriy.shevchenko@intel.com>,
"David S . Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
khtsai@google.com,
fangxy@xiaopeng.com,
Randy Dunlap <rdunlap@infradead.org>,
dbrownell@users.sourceforge.net,
linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org,
netdev@vger.kernel.org
X-Mailer: git-send-email 2.50.1
X-In-Reply-To: <20261106030605.576584-1-liuc63@xiaopeng.com>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list