🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #23818

[PATCH v6 1/2] lib/net_utils: allow mac_pton() to take a custom delimiter

#23818
From: Liu Chao
Date: Sat, 28 Nov 2026 08:07
90 lines
11789 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>
---
v6: no change.
v5: reword the parse_mac_pton() kernel-doc to say @s must be at least
    MAC_ADDR_STR_LEN characters long and that only the first
    MAC_ADDR_STR_LEN characters are checked, the rest ignored.  No code
    change.
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/20261117090110.3263592-2-liuc63@xiaopeng.com/

 include/linux/hex.h |  7 ++++++-
 lib/net_utils.c     | 19 ++++++++++++++++---
 2 files changed, 22 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..fd76b9ab3f24 100644
--- a/lib/net_utils.c
+++ b/lib/net_utils.c
@@ -5,7 +5,20 @@
 #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 at least 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
+ * ".:".  Only the first MAC_ADDR_STR_LEN characters are checked; anything
+ * after them is ignored.
+ *
+ * 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 +29,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 +37,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: <20261128080722.1469508-2-liuc63@xiaopeng.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373060-linux-kernel-netdev=dewy.ru
References: <20261128080722.1469508-1-liuc63@xiaopeng.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373060-linux-kernel-netdev=dewy.ru
From: liuc63@xiaopeng.com (Liu Chao)
Newsgroups: linux.kernel.netdev
Subject: [PATCH v6 1/2] lib/net_utils: allow mac_pton() to take a custom delimiter
Date: Sat, 28 Nov 2026 16:07:14 +0800
Organization: Usenet Network
Message-ID: <20261128080722.1469508-2-liuc63@xiaopeng.com>
References: <20261128080722.1469508-1-liuc63@xiaopeng.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="910211"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:K/Vm6udsL8m0baESW+IgyHidlqI= sha256:c8CbgTEBR8NKPQoG/okb+KJpPKov84zo/Xo+qyBZLtM=
	sha1:PqOfrQa7lfA/foTIeorMpAAjK2I= sha256:OVFkzX26qIcIVhP9Q6rZdVW60tsigpGEWbsyWP6+bt0=
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 2A2A3125F6F; Sat, 28 Nov 2026 11:08:05 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.049
X-Spam-Status: No, score=-2.049 tagged_above=-9999 required=6.31
	tests=[DCC_CHECK=1.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, URIBL_BLOCKED=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 TK7DkHVn0JyS;
	Sat, 28 Nov 2026 11:08:01 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-373060-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 tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id CCDC7125F72; Sat, 28 Nov 2026 11:07:52 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id B24392B61E; Sat, 28 Nov 2026 08:07:49 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=fail;
	t=1795853262;
	b=RYGG/UPClKePTZnJtOJnpUBwhSKKCUAwfNWMRoVgzL9oyHGCYYMWtly/gYRxJyb2ao46
	 JWyOEYkDhXQhnR6l/0yPtE08FU80/j3A8Pl4xf4BgtWP/m64JlRpg723k4iLqic9KRAgw
	 AjtbjnswglaVHzMjXFUjbpAm+cC5Zee65THy8iKA+oJ5vv8FeNRBR0mpc8W6VzxJQcnGJ
	 EQ+Sp76GoMS/1SzHmMmGYlpfA/0wPYY2PT2dFaBft+iiCymuLED4A+59hAxyi1b7YKvXq
	 WvPEux9bfKDKA09h0me1k4eN5lbETBWs1Ogky2K2czxKZhqc4uvoiQNU6hmSPiYRVTA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795853262;
	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=3pyHOCd7l2P6yeKjjzPj2V6+w4++yAlBV9wM0KG0J/o=;
	b=jey3pEvD6GqVBBRt6fR3fh5eftHXU8NU02JiXuXpYVtIKVl07Fi9o3Amwi+KZ07lgMNH
	 oyjcBRjoT/USuPG+xjtaCi2obqtSQ1sW8suBpyfCR5JwpQSoeoaboFG4NrvI12bdiCsEr
	 8cdHJU6BkMY5V3Eq6EaDaxjZeiuh2C7dWmSpuxsT4UNu41OHdDXxQm2zcRFIlPxu+TU8t
	 DwGMXhANQaQePIviQ/i1/SIOg5gdAQICBVU2kOQEZ9/3zhMes8mw32rR8IBQde1vOpT/D
	 INi32dCNCh7pc0lOouXWkQXJzb8Y0RfBmemwZEeClsFZ4e6Qjb+XUo2kc8oiydux/2w==
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=fdQx6029;
	dmarc=none header.from=xiaopeng.com;
	spf=pass smtp.mailfrom=xiaopeng.com;
	arc=fail 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 A31E3606A69E;
	Sat, 28 Nov 2026 08:07:42 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=fdQx6029
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.28.5
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795853256;
	b=Vv+BP7/G2K+xR4BbWNyapSm4+GBkR0o6EA5EkJX/kYOwGFAOyojyzmOmPiuFOm52ZN8M
	 AlyW89RzAO8Rmf/Dy1QzylkXWvTpO57H5uFJPqdDwU/MxMJBBCkW1SOe7nf0OSxPtPAiJ
	 3Ed+QnR297rDECdO8P6RZerY66LNLDblebYX+kGwiV9y+m+TKPBTZtgIyyxFH9U6lAkrV
	 fkDav0kc/M1yMMFDcVin3pcgAxSl9H39iGdJ6hZfmWcGYS+6NsJxIuubaQcN25t9M7GXs
	 eZQXdWqtSFhPp5/lxiBKenU1jY56/cR/X4Eep6l63EVHHSYtX8zlHXtrj5ym/ceA8Jg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795853256;
	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=3pyHOCd7l2P6yeKjjzPj2V6+w4++yAlBV9wM0KG0J/o=;
	b=e7AJNvwXC0V57aUC1ZmqoGv9FLQ9gQssdsgPH2IWcaA2uxPGYDrfWYhinoaETwtgL4o+
	 Up92tWboE+FkGbw+EhKWQ7CrRHYjzQPjusvLtz1io+UVFm+yaYryq9eMFKE3vw34LyOEn
	 VjPY33+f/2foxC3ZXPmbvefCZOsh5PuNijtAt4rgEnlovRZuihkic+czAGVfBaKn5vhoz
	 r3vW7VpdUydvF+u1QCLqSG4qpuVCaLD4nc00O1tJXkFN98LZGkUvjzQvxWEu0svSJVptO
	 NTlnph2SSiUIOAGL6qew+sez+OHpCAPkUUA0A+VAlyN7opvu6e0b1Gq+/i4Ve2fZLdg==
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=fdQx6029;
	arc=none smtp.remote-ip=115.124.28.5
X-Received: from out28-5.mail.aliyun.com (out28-5.mail.aliyun.com [115.124.28.5])
	(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 6C103606A31F;
	Sat, 28 Nov 2026 08:07:35 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6C103606A31F
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=1795853246; h=From:To:Subject:Date:Message-ID:MIME-Version;
	bh=3pyHOCd7l2P6yeKjjzPj2V6+w4++yAlBV9wM0KG0J/o=;
	b=fdQx6029wblSVeTGs2ly8dQm1uy+iYApJjqhpsdcMTeajYsloaBAaOIrS1t/SAzSyL2aesbFXXC+RBQe3tMclr9pz2NLbEsienzz3dtA1ECebnr0qw3GWA1tJN+SJtgZzlFKggT5ai2FAkSiXCjXzZw52+WRufyTd4vf6cCBHKI=
X-Alimail-AntiSpam: AC=CONTINUE;BC=0.07436259|-1;CH=green;DM=|CONTINUE|false|;DS=CONTINUE|ham_system_inform|0.00415204-3.12034e-05-0.995817;FP=18041251421778642104|0|0|0|0|-1|-1|-1;HT=maildocker-contentspam033045139068;MF=liuc63@xiaopeng.com;NM=1;PH=DS;RN=15;RT=15;SR=0;TI=SMTPD_---.kC0FY3m_1795853244;
X-Received: from localhost(mailfrom:liuc63@xiaopeng.com fp:SMTPD_---.kC0FY3m_1795853244 cluster:ay29)
          by smtp.aliyun-inc.com;
          Sat, 28 Nov 2026 16:07:24 +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: <20261128080722.1469508-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