Thread View: linux.kernel.netdev
1 messages
1 total messages
Started by Liu Chao
Tue, 17 Nov 2026 09:00
[PATCH v5 1/2] lib/net_utils: allow mac_pton() to take a custom delimiter
Author: Liu Chao
Date: Tue, 17 Nov 2026 09:00
Date: Tue, 17 Nov 2026 09:00
89 lines
11834 bytes
11834 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>
---
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/20261106030605.576584-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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads