From db80ed3e8df1d662f5e750b5629e5e6e7c1bddd5 Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Tue, 16 Jan 2024 17:38:16 +0100 Subject: [PATCH] net: if: Add function to obtain IPv4 netmask Add a helper function to obtain IPv4 netmask configured on an interface. Signed-off-by: Robert Lubos --- include/zephyr/net/net_if.h | 9 +++++++++ subsys/net/ip/net_if.c | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/zephyr/net/net_if.h b/include/zephyr/net/net_if.h index 22a3e14fa1..d849451d0c 100644 --- a/include/zephyr/net/net_if.h +++ b/include/zephyr/net/net_if.h @@ -2279,6 +2279,15 @@ struct in_addr *net_if_ipv4_get_ll(struct net_if *iface, struct in_addr *net_if_ipv4_get_global_addr(struct net_if *iface, enum net_addr_state addr_state); +/** + * @brief Get IPv4 netmask of an interface. + * + * @param iface Interface to use. + * + * @return The netmask set on the interface, unspecified address if not found. + */ +struct in_addr net_if_ipv4_get_netmask(struct net_if *iface); + /** * @brief Set IPv4 netmask for an interface. * diff --git a/subsys/net/ip/net_if.c b/subsys/net/ip/net_if.c index bb6099b978..a5486ce428 100644 --- a/subsys/net/ip/net_if.c +++ b/subsys/net/ip/net_if.c @@ -3619,6 +3619,27 @@ static inline int z_vrfy_net_if_ipv4_addr_lookup_by_index( #include #endif +struct in_addr net_if_ipv4_get_netmask(struct net_if *iface) +{ + struct in_addr netmask = { 0 }; + + net_if_lock(iface); + + if (net_if_config_ipv4_get(iface, NULL) < 0) { + goto out; + } + + if (!iface->config.ip.ipv4) { + goto out; + } + + netmask = iface->config.ip.ipv4->netmask; +out: + net_if_unlock(iface); + + return netmask; +} + void net_if_ipv4_set_netmask(struct net_if *iface, const struct in_addr *netmask) {