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 <krishna.t@nordicsemi.no>
This commit is contained in:
Krishna T 2023-01-11 23:45:30 +05:30 committed by Carles Cufí
commit bc80b0a232

View file

@ -27,8 +27,9 @@ LOG_MODULE_REGISTER(net_shell, LOG_LEVEL_DBG);
#include <zephyr/net/ppp.h>
#include <zephyr/net/net_stats.h>
#include <zephyr/sys/printk.h>
#if defined(CONFIG_NET_L2_ETHERNET)
#if defined(CONFIG_NET_L2_ETHERNET) && defined(CONFIG_NET_L2_ETHERNET_MGMT)
#include <zephyr/net/ethernet.h>
#include <zephyr/net/ethernet_mgmt.h>
#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(&params.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, &params, 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;
}