From b3a3a8b3a2d81a22339dd6f5a75a8769aa800739 Mon Sep 17 00:00:00 2001 From: Konrad Derda Date: Tue, 12 Sep 2023 21:33:38 +0200 Subject: [PATCH] net: ip: use default interface while matching LL src address While matching source address for a given Link-Local destination the functions iterate over interfaces and return a first result with a valid LL-address. However, they should first try to fetch address of the default interface as it not always the first one on the list. Signed-off-by: Konrad Derda --- subsys/net/ip/net_if.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/subsys/net/ip/net_if.c b/subsys/net/ip/net_if.c index d431cbf1334..f526cac02d1 100644 --- a/subsys/net/ip/net_if.c +++ b/subsys/net/ip/net_if.c @@ -2890,9 +2890,15 @@ const struct in6_addr *net_if_ipv6_select_src_addr(struct net_if *dst_iface, if (dst_iface) { src = net_if_ipv6_get_ll(dst_iface, NET_ADDR_PREFERRED); } else { - STRUCT_SECTION_FOREACH(net_if, iface) { - struct in6_addr *addr; + struct in6_addr *addr; + addr = net_if_ipv6_get_ll(net_if_get_default(), NET_ADDR_PREFERRED); + if (addr) { + src = addr; + goto out; + } + + STRUCT_SECTION_FOREACH(net_if, iface) { addr = net_if_ipv6_get_ll(iface, NET_ADDR_PREFERRED); if (addr) { @@ -2905,7 +2911,6 @@ const struct in6_addr *net_if_ipv6_select_src_addr(struct net_if *dst_iface, if (!src) { src = net_ipv6_unspecified_address(); - goto out; } out: @@ -3427,9 +3432,15 @@ const struct in_addr *net_if_ipv4_select_src_addr(struct net_if *dst_iface, if (dst_iface) { src = net_if_ipv4_get_ll(dst_iface, NET_ADDR_PREFERRED); } else { - STRUCT_SECTION_FOREACH(net_if, iface) { - struct in_addr *addr; + struct in_addr *addr; + addr = net_if_ipv4_get_ll(net_if_get_default(), NET_ADDR_PREFERRED); + if (addr) { + src = addr; + goto out; + } + + STRUCT_SECTION_FOREACH(net_if, iface) { addr = net_if_ipv4_get_ll(iface, NET_ADDR_PREFERRED); if (addr) { @@ -3454,8 +3465,6 @@ const struct in_addr *net_if_ipv4_select_src_addr(struct net_if *dst_iface, if (!src) { src = net_ipv4_unspecified_address(); } - - goto out; } out: