net: if: Replace asserts by proper runtime checks

The asserts were not proper here, replace those by runtime
checks as the functions can be called from applications and
asserts are not meant for error checking.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
This commit is contained in:
Jukka Rissanen 2024-11-26 19:00:29 +02:00 committed by Benjamin Cabé
commit 027760b6d4
2 changed files with 136 additions and 74 deletions

View file

@ -786,8 +786,9 @@ static inline void net_if_tx_unlock(struct net_if *iface)
static inline void net_if_flag_set(struct net_if *iface, static inline void net_if_flag_set(struct net_if *iface,
enum net_if_flag value) enum net_if_flag value)
{ {
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return;
}
atomic_set_bit(iface->if_dev->flags, value); atomic_set_bit(iface->if_dev->flags, value);
} }
@ -803,8 +804,9 @@ static inline void net_if_flag_set(struct net_if *iface,
static inline bool net_if_flag_test_and_set(struct net_if *iface, static inline bool net_if_flag_test_and_set(struct net_if *iface,
enum net_if_flag value) enum net_if_flag value)
{ {
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return false;
}
return atomic_test_and_set_bit(iface->if_dev->flags, value); return atomic_test_and_set_bit(iface->if_dev->flags, value);
} }
@ -818,8 +820,9 @@ static inline bool net_if_flag_test_and_set(struct net_if *iface,
static inline void net_if_flag_clear(struct net_if *iface, static inline void net_if_flag_clear(struct net_if *iface,
enum net_if_flag value) enum net_if_flag value)
{ {
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return;
}
atomic_clear_bit(iface->if_dev->flags, value); atomic_clear_bit(iface->if_dev->flags, value);
} }
@ -835,8 +838,9 @@ static inline void net_if_flag_clear(struct net_if *iface,
static inline bool net_if_flag_test_and_clear(struct net_if *iface, static inline bool net_if_flag_test_and_clear(struct net_if *iface,
enum net_if_flag value) enum net_if_flag value)
{ {
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return false;
}
return atomic_test_and_clear_bit(iface->if_dev->flags, value); return atomic_test_and_clear_bit(iface->if_dev->flags, value);
} }
@ -852,10 +856,7 @@ static inline bool net_if_flag_test_and_clear(struct net_if *iface,
static inline bool net_if_flag_is_set(struct net_if *iface, static inline bool net_if_flag_is_set(struct net_if *iface,
enum net_if_flag value) enum net_if_flag value)
{ {
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev);
if (iface == NULL) {
return false; return false;
} }
@ -873,8 +874,9 @@ static inline bool net_if_flag_is_set(struct net_if *iface,
static inline enum net_if_oper_state net_if_oper_state_set( static inline enum net_if_oper_state net_if_oper_state_set(
struct net_if *iface, enum net_if_oper_state oper_state) struct net_if *iface, enum net_if_oper_state oper_state)
{ {
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return NET_IF_OPER_UNKNOWN;
}
BUILD_ASSERT((enum net_if_oper_state)(-1) > 0 && NET_IF_OPER_UNKNOWN == 0); BUILD_ASSERT((enum net_if_oper_state)(-1) > 0 && NET_IF_OPER_UNKNOWN == 0);
if (oper_state <= NET_IF_OPER_UP) { if (oper_state <= NET_IF_OPER_UP) {
@ -893,8 +895,9 @@ static inline enum net_if_oper_state net_if_oper_state_set(
*/ */
static inline enum net_if_oper_state net_if_oper_state(struct net_if *iface) static inline enum net_if_oper_state net_if_oper_state(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return NET_IF_OPER_UNKNOWN;
}
return iface->if_dev->oper_state; return iface->if_dev->oper_state;
} }
@ -918,7 +921,7 @@ enum net_verdict net_if_send_data(struct net_if *iface, struct net_pkt *pkt);
*/ */
static inline const struct net_l2 *net_if_l2(struct net_if *iface) static inline const struct net_l2 *net_if_l2(struct net_if *iface)
{ {
if (!iface || !iface->if_dev) { if (iface == NULL || iface->if_dev == NULL) {
return NULL; return NULL;
} }
@ -944,8 +947,9 @@ enum net_verdict net_if_recv_data(struct net_if *iface, struct net_pkt *pkt);
*/ */
static inline void *net_if_l2_data(struct net_if *iface) static inline void *net_if_l2_data(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return NULL;
}
return iface->if_dev->l2_data; return iface->if_dev->l2_data;
} }
@ -959,8 +963,9 @@ static inline void *net_if_l2_data(struct net_if *iface)
*/ */
static inline const struct device *net_if_get_device(struct net_if *iface) static inline const struct device *net_if_get_device(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return NULL;
}
return iface->if_dev->dev; return iface->if_dev->dev;
} }
@ -1011,8 +1016,9 @@ bool net_if_is_offloaded(struct net_if *iface);
static inline struct net_offload *net_if_offload(struct net_if *iface) static inline struct net_offload *net_if_offload(struct net_if *iface)
{ {
#if defined(CONFIG_NET_OFFLOAD) #if defined(CONFIG_NET_OFFLOAD)
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return NULL;
}
return iface->if_dev->offload; return iface->if_dev->offload;
#else #else
@ -1032,8 +1038,9 @@ static inline struct net_offload *net_if_offload(struct net_if *iface)
static inline bool net_if_is_socket_offloaded(struct net_if *iface) static inline bool net_if_is_socket_offloaded(struct net_if *iface)
{ {
#if defined(CONFIG_NET_SOCKETS_OFFLOAD) #if defined(CONFIG_NET_SOCKETS_OFFLOAD)
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return false;
}
return (iface->if_dev->socket_offload != NULL); return (iface->if_dev->socket_offload != NULL);
#else #else
@ -1053,8 +1060,9 @@ static inline void net_if_socket_offload_set(
struct net_if *iface, net_socket_create_t socket_offload) struct net_if *iface, net_socket_create_t socket_offload)
{ {
#if defined(CONFIG_NET_SOCKETS_OFFLOAD) #if defined(CONFIG_NET_SOCKETS_OFFLOAD)
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return;
}
iface->if_dev->socket_offload = socket_offload; iface->if_dev->socket_offload = socket_offload;
#else #else
@ -1073,8 +1081,9 @@ static inline void net_if_socket_offload_set(
static inline net_socket_create_t net_if_socket_offload(struct net_if *iface) static inline net_socket_create_t net_if_socket_offload(struct net_if *iface)
{ {
#if defined(CONFIG_NET_SOCKETS_OFFLOAD) #if defined(CONFIG_NET_SOCKETS_OFFLOAD)
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return NULL;
}
return iface->if_dev->socket_offload; return iface->if_dev->socket_offload;
#else #else
@ -1093,8 +1102,9 @@ static inline net_socket_create_t net_if_socket_offload(struct net_if *iface)
*/ */
static inline struct net_linkaddr *net_if_get_link_addr(struct net_if *iface) static inline struct net_linkaddr *net_if_get_link_addr(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL || iface->if_dev == NULL) {
NET_ASSERT(iface->if_dev); return NULL;
}
return &iface->if_dev->link_addr; return &iface->if_dev->link_addr;
} }
@ -1108,7 +1118,9 @@ static inline struct net_linkaddr *net_if_get_link_addr(struct net_if *iface)
*/ */
static inline struct net_if_config *net_if_get_config(struct net_if *iface) static inline struct net_if_config *net_if_get_config(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL) {
return NULL;
}
return &iface->config; return &iface->config;
} }
@ -1252,12 +1264,10 @@ static inline int net_if_set_link_addr(struct net_if *iface,
*/ */
static inline uint16_t net_if_get_mtu(struct net_if *iface) static inline uint16_t net_if_get_mtu(struct net_if *iface)
{ {
if (iface == NULL) { if (iface == NULL || iface->if_dev == NULL) {
return 0U; return 0U;
} }
NET_ASSERT(iface->if_dev);
return iface->if_dev->mtu; return iface->if_dev->mtu;
} }
@ -1270,12 +1280,10 @@ static inline uint16_t net_if_get_mtu(struct net_if *iface)
static inline void net_if_set_mtu(struct net_if *iface, static inline void net_if_set_mtu(struct net_if *iface,
uint16_t mtu) uint16_t mtu)
{ {
if (iface == NULL) { if (iface == NULL || iface->if_dev == NULL) {
return; return;
} }
NET_ASSERT(iface->if_dev);
iface->if_dev->mtu = mtu; iface->if_dev->mtu = mtu;
} }
@ -1288,7 +1296,9 @@ static inline void net_if_set_mtu(struct net_if *iface,
static inline void net_if_addr_set_lf(struct net_if_addr *ifaddr, static inline void net_if_addr_set_lf(struct net_if_addr *ifaddr,
bool is_infinite) bool is_infinite)
{ {
NET_ASSERT(ifaddr); if (ifaddr == NULL) {
return;
}
ifaddr->is_infinite = is_infinite; ifaddr->is_infinite = is_infinite;
} }
@ -1320,7 +1330,9 @@ struct net_if *net_if_lookup_by_dev(const struct device *dev);
*/ */
static inline struct net_if_config *net_if_config_get(struct net_if *iface) static inline struct net_if_config *net_if_config_get(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL) {
return NULL;
}
return &iface->config; return &iface->config;
} }
@ -1651,7 +1663,9 @@ void net_if_ipv6_maddr_join(struct net_if *iface,
*/ */
static inline bool net_if_ipv6_maddr_is_joined(struct net_if_mcast_addr *addr) static inline bool net_if_ipv6_maddr_is_joined(struct net_if_mcast_addr *addr)
{ {
NET_ASSERT(addr); if (addr == NULL) {
return false;
}
return addr->is_joined; return addr->is_joined;
} }
@ -1765,7 +1779,9 @@ bool net_if_ipv6_addr_onlink(struct net_if **iface, struct in6_addr *addr);
#if defined(CONFIG_NET_NATIVE_IPV6) #if defined(CONFIG_NET_NATIVE_IPV6)
static inline struct in6_addr *net_if_router_ipv6(struct net_if_router *router) static inline struct in6_addr *net_if_router_ipv6(struct net_if_router *router)
{ {
NET_ASSERT(router); if (router == NULL) {
return NULL;
}
return &router->address.in6_addr; return &router->address.in6_addr;
} }
@ -1932,7 +1948,9 @@ static inline void net_if_ipv6_set_base_reachable_time(struct net_if *iface,
uint32_t reachable_time) uint32_t reachable_time)
{ {
#if defined(CONFIG_NET_NATIVE_IPV6) #if defined(CONFIG_NET_NATIVE_IPV6)
NET_ASSERT(iface); if (iface == NULL) {
return;
}
if (!iface->config.ip.ipv6) { if (!iface->config.ip.ipv6) {
return; return;
@ -1956,7 +1974,9 @@ static inline void net_if_ipv6_set_base_reachable_time(struct net_if *iface,
static inline uint32_t net_if_ipv6_get_reachable_time(struct net_if *iface) static inline uint32_t net_if_ipv6_get_reachable_time(struct net_if *iface)
{ {
#if defined(CONFIG_NET_NATIVE_IPV6) #if defined(CONFIG_NET_NATIVE_IPV6)
NET_ASSERT(iface); if (iface == NULL) {
return 0;
}
if (!iface->config.ip.ipv6) { if (!iface->config.ip.ipv6) {
return 0; return 0;
@ -2007,7 +2027,9 @@ static inline void net_if_ipv6_set_retrans_timer(struct net_if *iface,
uint32_t retrans_timer) uint32_t retrans_timer)
{ {
#if defined(CONFIG_NET_NATIVE_IPV6) #if defined(CONFIG_NET_NATIVE_IPV6)
NET_ASSERT(iface); if (iface == NULL) {
return;
}
if (!iface->config.ip.ipv6) { if (!iface->config.ip.ipv6) {
return; return;
@ -2030,7 +2052,9 @@ static inline void net_if_ipv6_set_retrans_timer(struct net_if *iface,
static inline uint32_t net_if_ipv6_get_retrans_timer(struct net_if *iface) static inline uint32_t net_if_ipv6_get_retrans_timer(struct net_if *iface)
{ {
#if defined(CONFIG_NET_NATIVE_IPV6) #if defined(CONFIG_NET_NATIVE_IPV6)
NET_ASSERT(iface); if (iface == NULL) {
return 0;
}
if (!iface->config.ip.ipv6) { if (!iface->config.ip.ipv6) {
return 0; return 0;
@ -2367,7 +2391,9 @@ void net_if_ipv4_maddr_join(struct net_if *iface,
*/ */
static inline bool net_if_ipv4_maddr_is_joined(struct net_if_mcast_addr *addr) static inline bool net_if_ipv4_maddr_is_joined(struct net_if_mcast_addr *addr)
{ {
NET_ASSERT(addr); if (addr == NULL) {
return false;
}
return addr->is_joined; return addr->is_joined;
} }
@ -2390,7 +2416,9 @@ void net_if_ipv4_maddr_leave(struct net_if *iface,
#if defined(CONFIG_NET_NATIVE_IPV4) #if defined(CONFIG_NET_NATIVE_IPV4)
static inline struct in_addr *net_if_router_ipv4(struct net_if_router *router) static inline struct in_addr *net_if_router_ipv4(struct net_if_router *router)
{ {
NET_ASSERT(router); if (router == NULL) {
return NULL;
}
return &router->address.in_addr; return &router->address.in_addr;
} }
@ -2837,7 +2865,9 @@ int net_if_up(struct net_if *iface);
*/ */
static inline bool net_if_is_up(struct net_if *iface) static inline bool net_if_is_up(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL) {
return false;
}
return net_if_flag_is_set(iface, NET_IF_UP) && return net_if_flag_is_set(iface, NET_IF_UP) &&
net_if_flag_is_set(iface, NET_IF_RUNNING); net_if_flag_is_set(iface, NET_IF_RUNNING);
@ -2861,7 +2891,9 @@ int net_if_down(struct net_if *iface);
*/ */
static inline bool net_if_is_admin_up(struct net_if *iface) static inline bool net_if_is_admin_up(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL) {
return false;
}
return net_if_flag_is_set(iface, NET_IF_UP); return net_if_flag_is_set(iface, NET_IF_UP);
} }
@ -2895,7 +2927,9 @@ void net_if_carrier_off(struct net_if *iface);
*/ */
static inline bool net_if_is_carrier_ok(struct net_if *iface) static inline bool net_if_is_carrier_ok(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL) {
return false;
}
return net_if_flag_is_set(iface, NET_IF_LOWER_UP); return net_if_flag_is_set(iface, NET_IF_LOWER_UP);
} }
@ -2931,7 +2965,9 @@ void net_if_dormant_off(struct net_if *iface);
*/ */
static inline bool net_if_is_dormant(struct net_if *iface) static inline bool net_if_is_dormant(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL) {
return false;
}
return net_if_flag_is_set(iface, NET_IF_DORMANT); return net_if_flag_is_set(iface, NET_IF_DORMANT);
} }

View file

@ -2023,7 +2023,9 @@ bool net_if_ipv6_addr_rm(struct net_if *iface, const struct in6_addr *addr)
bool result = true; bool result = true;
int ret; int ret;
NET_ASSERT(addr); if (iface == NULL || addr == NULL) {
return false;
}
net_if_lock(iface); net_if_lock(iface);
@ -2254,8 +2256,9 @@ void net_if_ipv6_maddr_foreach(struct net_if *iface, net_if_ip_maddr_cb_t cb,
{ {
struct net_if_ipv6 *ipv6; struct net_if_ipv6 *ipv6;
NET_ASSERT(iface); if (iface == NULL || cb == NULL) {
NET_ASSERT(cb); return;
}
net_if_lock(iface); net_if_lock(iface);
@ -2325,8 +2328,9 @@ out:
void net_if_ipv6_maddr_leave(struct net_if *iface, struct net_if_mcast_addr *addr) void net_if_ipv6_maddr_leave(struct net_if *iface, struct net_if_mcast_addr *addr)
{ {
NET_ASSERT(iface); if (iface == NULL || addr == NULL) {
NET_ASSERT(addr); return;
}
net_if_lock(iface); net_if_lock(iface);
addr->is_joined = false; addr->is_joined = false;
@ -2335,8 +2339,9 @@ void net_if_ipv6_maddr_leave(struct net_if *iface, struct net_if_mcast_addr *add
void net_if_ipv6_maddr_join(struct net_if *iface, struct net_if_mcast_addr *addr) void net_if_ipv6_maddr_join(struct net_if *iface, struct net_if_mcast_addr *addr)
{ {
NET_ASSERT(iface); if (iface == NULL || addr == NULL) {
NET_ASSERT(addr); return;
}
net_if_lock(iface); net_if_lock(iface);
addr->is_joined = true; addr->is_joined = true;
@ -3140,7 +3145,9 @@ const struct in6_addr *net_if_ipv6_select_src_addr_hint(struct net_if *dst_iface
const struct in6_addr *src = NULL; const struct in6_addr *src = NULL;
uint8_t best_match = 0U; uint8_t best_match = 0U;
NET_ASSERT(dst); if (dst == NULL) {
return NULL;
}
if (!net_ipv6_is_ll_addr(dst) && !net_ipv6_is_addr_mcast_link(dst)) { if (!net_ipv6_is_ll_addr(dst) && !net_ipv6_is_addr_mcast_link(dst)) {
struct net_if_ipv6_prefix *prefix; struct net_if_ipv6_prefix *prefix;
@ -3672,7 +3679,9 @@ const struct in_addr *net_if_ipv4_select_src_addr(struct net_if *dst_iface,
const struct in_addr *src = NULL; const struct in_addr *src = NULL;
uint8_t best_match = 0U; uint8_t best_match = 0U;
NET_ASSERT(dst); if (dst == NULL) {
return NULL;
}
if (!net_ipv4_is_ll_addr(dst)) { if (!net_ipv4_is_ll_addr(dst)) {
@ -4374,7 +4383,9 @@ bool net_if_ipv4_addr_rm(struct net_if *iface, const struct in_addr *addr)
bool result = true; bool result = true;
int ret; int ret;
NET_ASSERT(addr); if (iface == NULL || addr == NULL) {
return false;
}
net_if_lock(iface); net_if_lock(iface);
@ -4606,8 +4617,9 @@ void net_if_ipv4_maddr_foreach(struct net_if *iface, net_if_ip_maddr_cb_t cb,
{ {
struct net_if_ipv4 *ipv4; struct net_if_ipv4 *ipv4;
NET_ASSERT(iface); if (iface == NULL || cb == NULL) {
NET_ASSERT(cb); return;
}
net_if_lock(iface); net_if_lock(iface);
@ -4659,8 +4671,9 @@ out:
void net_if_ipv4_maddr_leave(struct net_if *iface, struct net_if_mcast_addr *addr) void net_if_ipv4_maddr_leave(struct net_if *iface, struct net_if_mcast_addr *addr)
{ {
NET_ASSERT(iface); if (iface == NULL || addr == NULL) {
NET_ASSERT(addr); return;
}
net_if_lock(iface); net_if_lock(iface);
addr->is_joined = false; addr->is_joined = false;
@ -4669,8 +4682,9 @@ void net_if_ipv4_maddr_leave(struct net_if *iface, struct net_if_mcast_addr *add
void net_if_ipv4_maddr_join(struct net_if *iface, struct net_if_mcast_addr *addr) void net_if_ipv4_maddr_join(struct net_if *iface, struct net_if_mcast_addr *addr)
{ {
NET_ASSERT(iface); if (iface == NULL || addr == NULL) {
NET_ASSERT(addr); return;
}
net_if_lock(iface); net_if_lock(iface);
addr->is_joined = true; addr->is_joined = true;
@ -5597,7 +5611,9 @@ out:
void net_if_carrier_on(struct net_if *iface) void net_if_carrier_on(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL) {
return;
}
net_if_lock(iface); net_if_lock(iface);
@ -5610,7 +5626,9 @@ void net_if_carrier_on(struct net_if *iface)
void net_if_carrier_off(struct net_if *iface) void net_if_carrier_off(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL) {
return;
}
net_if_lock(iface); net_if_lock(iface);
@ -5623,7 +5641,9 @@ void net_if_carrier_off(struct net_if *iface)
void net_if_dormant_on(struct net_if *iface) void net_if_dormant_on(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL) {
return;
}
net_if_lock(iface); net_if_lock(iface);
@ -5636,7 +5656,9 @@ void net_if_dormant_on(struct net_if *iface)
void net_if_dormant_off(struct net_if *iface) void net_if_dormant_off(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL) {
return;
}
net_if_lock(iface); net_if_lock(iface);
@ -5652,7 +5674,9 @@ static int promisc_mode_set(struct net_if *iface, bool enable)
{ {
enum net_l2_flags l2_flags = 0; enum net_l2_flags l2_flags = 0;
NET_ASSERT(iface); if (iface == NULL) {
return -EINVAL;
}
l2_flags = l2_flags_get(iface); l2_flags = l2_flags_get(iface);
if (!(l2_flags & NET_L2_PROMISC_MODE)) { if (!(l2_flags & NET_L2_PROMISC_MODE)) {
@ -5718,7 +5742,9 @@ out:
bool net_if_is_promisc(struct net_if *iface) bool net_if_is_promisc(struct net_if *iface)
{ {
NET_ASSERT(iface); if (iface == NULL) {
return false;
}
return net_if_flag_is_set(iface, NET_IF_PROMISC); return net_if_flag_is_set(iface, NET_IF_PROMISC);
} }