Skip to content

Commit bc80b0a

Browse files
Krishna Tcarlescufi
authored andcommitted
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 <[email protected]>
1 parent 7952197 commit bc80b0a

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

subsys/net/ip/net_shell.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ LOG_MODULE_REGISTER(net_shell, LOG_LEVEL_DBG);
2727
#include <zephyr/net/ppp.h>
2828
#include <zephyr/net/net_stats.h>
2929
#include <zephyr/sys/printk.h>
30-
#if defined(CONFIG_NET_L2_ETHERNET)
30+
#if defined(CONFIG_NET_L2_ETHERNET) && defined(CONFIG_NET_L2_ETHERNET_MGMT)
3131
#include <zephyr/net/ethernet.h>
32+
#include <zephyr/net/ethernet_mgmt.h>
3233
#endif /* CONFIG_NET_L2_ETHERNET */
3334

3435
#include "route.h"
@@ -3568,12 +3569,14 @@ static int cmd_net_iface(const struct shell *sh, size_t argc, char *argv[])
35683569

35693570
static int cmd_net_set_mac(const struct shell *sh, size_t argc, char *argv[])
35703571
{
3571-
#if !defined(CONFIG_NET_L2_ETHERNET)
3572-
PR_WARNING("Unsupported command, please enable CONFIG_NET_L2_ETHERNET\n");
3572+
#if !defined(CONFIG_NET_L2_ETHERNET) || !defined(CONFIG_NET_L2_ETHERNET_MGMT)
3573+
PR_WARNING("Unsupported command, please enable CONFIG_NET_L2_ETHERNET "
3574+
"and CONFIG_NET_L2_ETHERNET_MGMT\n");
35733575
return -ENOEXEC;
35743576
#else
35753577
struct net_if *iface;
3576-
struct net_eth_addr mac_addr;
3578+
struct ethernet_req_params params;
3579+
char *mac_addr = params.mac_address.addr;
35773580
int idx;
35783581
int ret;
35793582

@@ -3598,15 +3601,15 @@ static int cmd_net_set_mac(const struct shell *sh, size_t argc, char *argv[])
35983601
goto err;
35993602
}
36003603

3601-
if ((net_bytes_from_str(mac_addr.addr, sizeof(mac_addr), argv[2]) < 0) ||
3602-
!net_eth_is_addr_valid(&mac_addr)) {
3604+
if ((net_bytes_from_str(mac_addr, sizeof(params.mac_address), argv[2]) < 0) ||
3605+
!net_eth_is_addr_valid(&params.mac_address)) {
36033606
PR_WARNING("Invalid MAC address: %s\n", argv[2]);
36043607
goto err;
36053608
}
36063609

3607-
ret = net_if_set_link_addr(iface, mac_addr.addr, sizeof(mac_addr), NET_LINK_ETHERNET);
3610+
ret = net_mgmt(NET_REQUEST_ETHERNET_SET_MAC_ADDRESS, iface, &params, sizeof(params));
36083611
if (ret < 0) {
3609-
if (ret == -EPERM) {
3612+
if (ret == -EACCES) {
36103613
PR_WARNING("MAC address cannot be set when interface is operational\n");
36113614
goto err;
36123615
}

0 commit comments

Comments
 (0)