From bc80b0a23284336e886dbfad2c74c37e97c1619b Mon Sep 17 00:00:00 2001 From: Krishna T Date: Wed, 11 Jan 2023 23:45:30 +0530 Subject: [PATCH] net: shell: Use net management API for MAC address configuration Fix a bug in setting MAC address using net_if API, the API doesn't do a memcpy but just stores the pointer and shell was passing stack pointer. We can use dynamic allocation but freeing the memory for the MAC address would be trickier, so, use the net management API and let the underlying drivers figure out the MAC address memory management. Signed-off-by: Krishna T --- subsys/net/ip/net_shell.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/subsys/net/ip/net_shell.c b/subsys/net/ip/net_shell.c index 3fbb566cd63..eaa2d449e72 100644 --- a/subsys/net/ip/net_shell.c +++ b/subsys/net/ip/net_shell.c @@ -27,8 +27,9 @@ LOG_MODULE_REGISTER(net_shell, LOG_LEVEL_DBG); #include #include #include -#if defined(CONFIG_NET_L2_ETHERNET) +#if defined(CONFIG_NET_L2_ETHERNET) && defined(CONFIG_NET_L2_ETHERNET_MGMT) #include +#include #endif /* CONFIG_NET_L2_ETHERNET */ #include "route.h" @@ -3568,12 +3569,14 @@ static int cmd_net_iface(const struct shell *sh, size_t argc, char *argv[]) static int cmd_net_set_mac(const struct shell *sh, size_t argc, char *argv[]) { -#if !defined(CONFIG_NET_L2_ETHERNET) - PR_WARNING("Unsupported command, please enable CONFIG_NET_L2_ETHERNET\n"); +#if !defined(CONFIG_NET_L2_ETHERNET) || !defined(CONFIG_NET_L2_ETHERNET_MGMT) + PR_WARNING("Unsupported command, please enable CONFIG_NET_L2_ETHERNET " + "and CONFIG_NET_L2_ETHERNET_MGMT\n"); return -ENOEXEC; #else struct net_if *iface; - struct net_eth_addr mac_addr; + struct ethernet_req_params params; + char *mac_addr = params.mac_address.addr; int idx; int ret; @@ -3598,15 +3601,15 @@ static int cmd_net_set_mac(const struct shell *sh, size_t argc, char *argv[]) goto err; } - if ((net_bytes_from_str(mac_addr.addr, sizeof(mac_addr), argv[2]) < 0) || - !net_eth_is_addr_valid(&mac_addr)) { + if ((net_bytes_from_str(mac_addr, sizeof(params.mac_address), argv[2]) < 0) || + !net_eth_is_addr_valid(¶ms.mac_address)) { PR_WARNING("Invalid MAC address: %s\n", argv[2]); goto err; } - ret = net_if_set_link_addr(iface, mac_addr.addr, sizeof(mac_addr), NET_LINK_ETHERNET); + ret = net_mgmt(NET_REQUEST_ETHERNET_SET_MAC_ADDRESS, iface, ¶ms, sizeof(params)); if (ret < 0) { - if (ret == -EPERM) { + if (ret == -EACCES) { PR_WARNING("MAC address cannot be set when interface is operational\n"); goto err; }