Skip to content

Commit 6598094

Browse files
committed
net: if: Add helper to get src interface and address from dst address
Instead of calling various network interface API functions to get the network interface and related source IP address, have a single function that can return both data. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 24f35bf commit 6598094

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

include/zephyr/net/net_if.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,32 @@ static inline struct net_if *net_if_ipv6_select_src_iface(
21532153
}
21542154
#endif
21552155

2156+
/**
2157+
* @brief Get a network interface that should be used when sending
2158+
* IPv6 network data to destination. Also return the source IPv6 address from
2159+
* that network interface.
2160+
*
2161+
* @param dst IPv6 destination address
2162+
* @param src_addr IPv6 source address. This can be set to NULL if the source
2163+
* address is not needed.
2164+
*
2165+
* @return Pointer to network interface to use, NULL if no suitable interface
2166+
* could be found.
2167+
*/
2168+
#if defined(CONFIG_NET_IPV6)
2169+
struct net_if *net_if_ipv6_select_src_iface_addr(const struct in6_addr *dst,
2170+
const struct in6_addr **src_addr);
2171+
#else
2172+
static inline struct net_if *net_if_ipv6_select_src_iface_addr(
2173+
const struct in6_addr *dst, const struct in6_addr **src_addr)
2174+
{
2175+
ARG_UNUSED(dst);
2176+
ARG_UNUSED(src_addr);
2177+
2178+
return NULL;
2179+
}
2180+
#endif /* CONFIG_NET_IPV6 */
2181+
21562182
/**
21572183
* @brief Get a IPv6 link local address in a given state.
21582184
*
@@ -2534,6 +2560,32 @@ static inline struct net_if *net_if_ipv4_select_src_iface(
25342560
}
25352561
#endif
25362562

2563+
/**
2564+
* @brief Get a network interface that should be used when sending
2565+
* IPv4 network data to destination. Also return the source IPv4 address from
2566+
* that network interface.
2567+
*
2568+
* @param dst IPv4 destination address
2569+
* @param src_addr IPv4 source address. This can be set to NULL if the source
2570+
* address is not needed.
2571+
*
2572+
* @return Pointer to network interface to use, NULL if no suitable interface
2573+
* could be found.
2574+
*/
2575+
#if defined(CONFIG_NET_IPV4)
2576+
struct net_if *net_if_ipv4_select_src_iface_addr(const struct in_addr *dst,
2577+
const struct in_addr **src_addr);
2578+
#else
2579+
static inline struct net_if *net_if_ipv4_select_src_iface_addr(
2580+
const struct in_addr *dst, const struct in_addr **src_addr)
2581+
{
2582+
ARG_UNUSED(dst);
2583+
ARG_UNUSED(src_addr);
2584+
2585+
return NULL;
2586+
}
2587+
#endif /* CONFIG_NET_IPV4 */
2588+
25372589
/**
25382590
* @brief Get a IPv4 source address that should be used when sending
25392591
* network data to destination.

subsys/net/ip/net_if.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3236,7 +3236,8 @@ const struct in6_addr *net_if_ipv6_select_src_addr(struct net_if *dst_iface,
32363236
IPV6_PREFER_SRC_PUBTMP_DEFAULT);
32373237
}
32383238

3239-
struct net_if *net_if_ipv6_select_src_iface(const struct in6_addr *dst)
3239+
struct net_if *net_if_ipv6_select_src_iface_addr(const struct in6_addr *dst,
3240+
const struct in6_addr **src_addr)
32403241
{
32413242
struct net_if *iface = NULL;
32423243
const struct in6_addr *src;
@@ -3246,13 +3247,22 @@ struct net_if *net_if_ipv6_select_src_iface(const struct in6_addr *dst)
32463247
net_if_ipv6_addr_lookup(src, &iface);
32473248
}
32483249

3250+
if (src_addr != NULL) {
3251+
*src_addr = src;
3252+
}
3253+
32493254
if (iface == NULL) {
32503255
iface = net_if_get_default();
32513256
}
32523257

32533258
return iface;
32543259
}
32553260

3261+
struct net_if *net_if_ipv6_select_src_iface(const struct in6_addr *dst)
3262+
{
3263+
return net_if_ipv6_select_src_iface_addr(dst, NULL);
3264+
}
3265+
32563266
#if defined(CONFIG_NET_NATIVE_IPV6)
32573267

32583268
uint32_t net_if_ipv6_calc_reachable_time(struct net_if_ipv6 *ipv6)
@@ -3561,7 +3571,8 @@ bool net_if_ipv4_is_addr_bcast(struct net_if *iface,
35613571
return ret;
35623572
}
35633573

3564-
struct net_if *net_if_ipv4_select_src_iface(const struct in_addr *dst)
3574+
struct net_if *net_if_ipv4_select_src_iface_addr(const struct in_addr *dst,
3575+
const struct in_addr **src_addr)
35653576
{
35663577
struct net_if *selected = NULL;
35673578
const struct in_addr *src;
@@ -3575,9 +3586,18 @@ struct net_if *net_if_ipv4_select_src_iface(const struct in_addr *dst)
35753586
selected = net_if_get_default();
35763587
}
35773588

3589+
if (src_addr != NULL) {
3590+
*src_addr = src;
3591+
}
3592+
35783593
return selected;
35793594
}
35803595

3596+
struct net_if *net_if_ipv4_select_src_iface(const struct in_addr *dst)
3597+
{
3598+
return net_if_ipv4_select_src_iface_addr(dst, NULL);
3599+
}
3600+
35813601
static uint8_t get_diff_ipv4(const struct in_addr *src,
35823602
const struct in_addr *dst)
35833603
{

0 commit comments

Comments
 (0)