Skip to content

Commit 7952197

Browse files
Krishna Tcarlescufi
authored andcommitted
net: shell: Add MAC address validation
Validate MAC address before setting, add new helper APIs to cover all cases. Signed-off-by: Krishna T <[email protected]>
1 parent 6cbe9a0 commit 7952197

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

include/zephyr/net/ethernet.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,16 @@ static inline bool net_eth_is_addr_multicast(struct net_eth_addr *addr)
670670
return false;
671671
}
672672

673+
static inline bool net_eth_is_addr_group(struct net_eth_addr *addr)
674+
{
675+
return addr->addr[0] & 0x01;
676+
}
677+
678+
static inline bool net_eth_is_addr_valid(struct net_eth_addr *addr)
679+
{
680+
return !net_eth_is_addr_unspecified(addr) && !net_eth_is_addr_group(addr);
681+
}
682+
673683
static inline bool net_eth_is_addr_lldp_multicast(struct net_eth_addr *addr)
674684
{
675685
#if defined(CONFIG_NET_GPTP) || defined(CONFIG_NET_LLDP)

subsys/net/ip/net_shell.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,8 +3598,9 @@ static int cmd_net_set_mac(const struct shell *sh, size_t argc, char *argv[])
35983598
goto err;
35993599
}
36003600

3601-
if (net_bytes_from_str(mac_addr.addr, sizeof(mac_addr), argv[2]) < 0) {
3602-
PR_WARNING("Invalid MAC address\n");
3601+
if ((net_bytes_from_str(mac_addr.addr, sizeof(mac_addr), argv[2]) < 0) ||
3602+
!net_eth_is_addr_valid(&mac_addr)) {
3603+
PR_WARNING("Invalid MAC address: %s\n", argv[2]);
36033604
goto err;
36043605
}
36053606

@@ -3613,6 +3614,10 @@ static int cmd_net_set_mac(const struct shell *sh, size_t argc, char *argv[])
36133614
goto err;
36143615
}
36153616

3617+
PR_INFO("MAC address set to %s\n",
3618+
net_sprint_ll_addr(net_if_get_link_addr(iface)->addr,
3619+
net_if_get_link_addr(iface)->len));
3620+
36163621
return 0;
36173622
err:
36183623
return -ENOEXEC;

0 commit comments

Comments
 (0)