Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net, treewide: define and use MAC_ADDR_STR_LEN

There are a few places in the tree which compute the length of the
string representation of a MAC address as 3 * ETH_ALEN - 1. Define a
constant for this and use it where relevant. No functionality changes
are expected.

Signed-off-by: Uday Shankar <ushankar@purestorage.com>
Reviewed-by: Michal Swiatkowski <michal.swiatkowski@linux.intel.com>
Acked-by: Johannes Berg <johannes@sipsolutions.net>
Reviewed-by: Breno Leitao <leitao@debian.org>
Reviewed-by: Simon Horman <horms@verge.net.au>
Link: https://patch.msgid.link/20250312-netconsole-v6-1-3437933e79b8@purestorage.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Uday Shankar and committed by
Paolo Abeni
6d6c1ba7 34e5eded

+11 -9
+1 -1
drivers/net/netconsole.c
··· 739 739 740 740 if (!mac_pton(buf, remote_mac)) 741 741 goto out_unlock; 742 - if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n') 742 + if (buf[MAC_ADDR_STR_LEN] && buf[MAC_ADDR_STR_LEN] != '\n') 743 743 goto out_unlock; 744 744 memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN); 745 745
+1 -1
drivers/nvmem/brcm_nvram.c
··· 100 100 { 101 101 u8 mac[ETH_ALEN]; 102 102 103 - if (bytes != 3 * ETH_ALEN - 1) 103 + if (bytes != MAC_ADDR_STR_LEN) 104 104 return -EINVAL; 105 105 106 106 if (!mac_pton(buf, mac))
+1 -1
drivers/nvmem/layouts/u-boot-env.c
··· 37 37 { 38 38 u8 mac[ETH_ALEN]; 39 39 40 - if (bytes != 3 * ETH_ALEN - 1) 40 + if (bytes != MAC_ADDR_STR_LEN) 41 41 return -EINVAL; 42 42 43 43 if (!mac_pton(buf, mac))
+3
include/linux/if_ether.h
··· 19 19 #include <linux/skbuff.h> 20 20 #include <uapi/linux/if_ether.h> 21 21 22 + /* XX:XX:XX:XX:XX:XX */ 23 + #define MAC_ADDR_STR_LEN (3 * ETH_ALEN - 1) 24 + 22 25 static inline struct ethhdr *eth_hdr(const struct sk_buff *skb) 23 26 { 24 27 return (struct ethhdr *)skb_mac_header(skb);
+1 -3
lib/net_utils.c
··· 7 7 8 8 bool mac_pton(const char *s, u8 *mac) 9 9 { 10 - size_t maxlen = 3 * ETH_ALEN - 1; 11 10 int i; 12 11 13 - /* XX:XX:XX:XX:XX:XX */ 14 - if (strnlen(s, maxlen) < maxlen) 12 + if (strnlen(s, MAC_ADDR_STR_LEN) < MAC_ADDR_STR_LEN) 15 13 return false; 16 14 17 15 /* Don't dirty result unless string is valid MAC. */
+4 -3
net/mac80211/debugfs_sta.c
··· 457 457 size_t count, loff_t *ppos) 458 458 { 459 459 struct link_sta_info *link_sta = file->private_data; 460 - u8 mac[3 * ETH_ALEN + 1]; 460 + u8 mac[MAC_ADDR_STR_LEN + 2]; 461 461 462 462 snprintf(mac, sizeof(mac), "%pM\n", link_sta->pub->addr); 463 463 464 - return simple_read_from_buffer(userbuf, count, ppos, mac, 3 * ETH_ALEN); 464 + return simple_read_from_buffer(userbuf, count, ppos, mac, 465 + MAC_ADDR_STR_LEN + 1); 465 466 } 466 467 467 468 LINK_STA_OPS(addr); ··· 1241 1240 struct ieee80211_local *local = sta->local; 1242 1241 struct ieee80211_sub_if_data *sdata = sta->sdata; 1243 1242 struct dentry *stations_dir = sta->sdata->debugfs.subdir_stations; 1244 - u8 mac[3*ETH_ALEN]; 1243 + u8 mac[MAC_ADDR_STR_LEN + 1]; 1245 1244 1246 1245 if (!stations_dir) 1247 1246 return;