From a8e75627156b56daca813cb4859a775f9fdf4166 Mon Sep 17 00:00:00 2001 From: Maciej Panek Date: Sat, 31 Aug 2024 08:42:19 -0400 Subject: [PATCH] net: l2: ethernet: check if the dev->api->get_capabilities method not NULL Adds missing checks for get_capabilities method not being NULL. Fixes crash with netusb and possibly other drivers. Signed-off-by: Maciej Panek --- include/zephyr/net/ethernet.h | 8 ++++---- subsys/net/l2/ethernet/ethernet.c | 9 +++------ subsys/net/l2/ethernet/ethernet_mgmt.c | 4 ++-- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/include/zephyr/net/ethernet.h b/include/zephyr/net/ethernet.h index 61740734bc6da0..d6f2e445138660 100644 --- a/include/zephyr/net/ethernet.h +++ b/include/zephyr/net/ethernet.h @@ -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); } /** diff --git a/subsys/net/l2/ethernet/ethernet.c b/subsys/net/l2/ethernet/ethernet.c index e7a766084c687f..87fce8f66108f0 100644 --- a/subsys/net/l2/ethernet/ethernet.c +++ b/subsys/net/l2/ethernet/ethernet.c @@ -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 @@ -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; } @@ -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; } diff --git a/subsys/net/l2/ethernet/ethernet_mgmt.c b/subsys/net/l2/ethernet/ethernet_mgmt.c index 5078c07dbcab50..703dbbccb6de99 100644 --- a/subsys/net/l2/ethernet/ethernet_mgmt.c +++ b/subsys/net/l2/ethernet/ethernet_mgmt.c @@ -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,