net: ipv4: Specify default netmask for IPv4 addresses registered

When IPv4 address is added, currently the netmask remains empty. This
makes it impossible to use the address now that the netmask is
considered during address selection if the application does not
specify the netmask manually. Therefore specify a default netmask for
IPv4 addresses added to an interface with Kconfig.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
Robert Lubos 2024-11-28 10:18:08 +01:00 committed by Benjamin Cabé
commit a32d339c34
2 changed files with 17 additions and 4 deletions

View file

@ -31,6 +31,15 @@ config NET_IF_MCAST_IPV4_ADDR_COUNT
default 2 if NET_IPV4_IGMP
default 1
config NET_IPV4_DEFAULT_NETMASK
int "The default netmask length for registered IPv4 addresses"
range 1 32
default 24
help
The default netmask length for the registered IPv4 addresses.
The application can set a custom netmask with
net_if_ipv4_set_netmask_by_addr() API if needed.
if NET_NATIVE_IPV4
config NET_INITIAL_TTL

View file

@ -4287,7 +4287,9 @@ struct net_if_addr *net_if_ipv4_addr_add(struct net_if *iface,
enum net_addr_type addr_type,
uint32_t vlifetime)
{
uint32_t default_netmask = UINT32_MAX << (32 - CONFIG_NET_IPV4_DEFAULT_NETMASK);
struct net_if_addr *ifaddr = NULL;
struct net_if_addr_ipv4 *cur;
struct net_if_ipv4 *ipv4;
int idx;
@ -4304,17 +4306,17 @@ struct net_if_addr *net_if_ipv4_addr_add(struct net_if *iface,
}
ARRAY_FOR_EACH(ipv4->unicast, i) {
struct net_if_addr *cur = &ipv4->unicast[i].ipv4;
cur = &ipv4->unicast[i];
if (addr_type == NET_ADDR_DHCP
&& cur->addr_type == NET_ADDR_OVERRIDABLE) {
ifaddr = cur;
&& cur->ipv4.addr_type == NET_ADDR_OVERRIDABLE) {
ifaddr = &cur->ipv4;
idx = i;
break;
}
if (!ipv4->unicast[i].ipv4.is_used) {
ifaddr = cur;
ifaddr = &cur->ipv4;
idx = i;
break;
}
@ -4352,6 +4354,8 @@ struct net_if_addr *net_if_ipv4_addr_add(struct net_if *iface,
ifaddr->addr_state = NET_ADDR_PREFERRED;
}
cur->netmask.s_addr = htonl(default_netmask);
net_mgmt_event_notify_with_info(NET_EVENT_IPV4_ADDR_ADD, iface,
&ifaddr->address.in_addr,
sizeof(struct in_addr));