Skip to content

Commit

Permalink
net: l2: ethernet: check if the dev->api->get_capabilities method not…
Browse files Browse the repository at this point in the history
… NULL

Adds missing checks for get_capabilities method not being NULL.
Fixes crash with netusb and possibly other drivers.

Signed-off-by: Maciej Panek <[email protected]>
  • Loading branch information
panekmaciej committed Sep 1, 2024
1 parent 7cf124b commit a8e7562
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
8 changes: 4 additions & 4 deletions include/zephyr/net/ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -933,14 +933,14 @@ void net_eth_ipv6_mcast_to_mac_addr(const struct in6_addr *ipv6_addr,
static inline
enum ethernet_hw_caps net_eth_get_hw_capabilities(struct net_if *iface)
{
const struct ethernet_api *eth =
(struct ethernet_api *)net_if_get_device(iface)->api;
const struct device *dev = net_if_get_device(iface);
const struct ethernet_api *api = (struct ethernet_api *)dev->api;

if (!eth->get_capabilities) {
if (!api || !api->get_capabilities) {
return (enum ethernet_hw_caps)0;
}

return eth->get_capabilities(net_if_get_device(iface));
return api->get_capabilities(dev);
}

/**
Expand Down
9 changes: 3 additions & 6 deletions subsys/net/l2/ethernet/ethernet.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ static void ethernet_update_rx_stats(struct net_if *iface,

static inline bool eth_is_vlan_tag_stripped(struct net_if *iface)
{
const struct device *dev = net_if_get_device(iface);
const struct ethernet_api *api = dev->api;

return (api->get_capabilities(dev) & ETHERNET_HW_VLAN_TAG_STRIP);
return (net_eth_get_hw_capabilities(iface) & ETHERNET_HW_VLAN_TAG_STRIP);
}

/* Drop packet if it has broadcast destination MAC address but the IP
Expand Down Expand Up @@ -210,7 +207,7 @@ static void ethernet_mcast_monitor_cb(struct net_if *iface, const struct net_add
dev = net_if_get_device(iface);
api = dev->api;

if (!(api->get_capabilities(dev) & ETHERNET_HW_FILTERING) || api->set_config == NULL) {
if (!(net_eth_get_hw_capabilities(iface) & ETHERNET_HW_FILTERING) || api->set_config == NULL) {
return;
}

Expand Down Expand Up @@ -868,7 +865,7 @@ const struct device *net_eth_get_ptp_clock(struct net_if *iface)
return NULL;
}

if (!(api->get_capabilities(dev) & ETHERNET_PTP)) {
if (!(net_eth_get_hw_capabilities(iface) & ETHERNET_PTP)) {
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions subsys/net/l2/ethernet/ethernet_mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ static inline bool is_hw_caps_supported(const struct device *dev,
{
const struct ethernet_api *api = dev->api;

if (!api) {
if (!api || !api->get_capabilities) {
return false;
}

return !!(api->get_capabilities(dev) & caps);
return ((api->get_capabilities(dev) & caps) != 0);
}

static int ethernet_set_config(uint32_t mgmt_request,
Expand Down

0 comments on commit a8e7562

Please sign in to comment.