Skip to content
Permalink
Browse files

net: if: Add net_if_select_src_iface() function

Add a function which returns proper network interface to send either
IPv4 or IPv6 network packet to corresponding destination address.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
  • Loading branch information...
jukkar committed Aug 7, 2018
1 parent 05d9b04 commit d0205693ae403551535b05c4d61b32d5e1f45b6c
Showing with 44 additions and 1 deletion.
  1. +14 −1 include/net/net_if.h
  2. +30 −0 subsys/net/ip/net_if.c
@@ -1502,9 +1502,22 @@ static inline void net_if_ipv4_set_gw(struct net_if *iface,

net_ipaddr_copy(&iface->config.ip.ipv4->gw, gw);
}

#else
#define net_if_ipv4_select_src_iface(...) NULL
#endif /* CONFIG_NET_IPV4 */

/**
* @brief Get a network interface that should be used when sending
* IPv6 or IPv4 network data to destination.
*
* @param dst IPv6 or IPv4 destination address
*
* @return Pointer to network interface to use. Note that the function
* will return the default network interface if the best network interface
* is not found.
*/
struct net_if *net_if_select_src_iface(const struct sockaddr *dst);

/**
* @typedef net_if_link_callback_t
* @brief Define callback that is called after a network packet
@@ -2185,6 +2185,36 @@ struct net_if_mcast_addr *net_if_ipv4_maddr_lookup(const struct in_addr *maddr,
}
#endif /* CONFIG_NET_IPV4 */

struct net_if *net_if_select_src_iface(const struct sockaddr *dst)
{
struct net_if *iface;

if (!dst) {
goto out;
}

if (IS_ENABLED(CONFIG_NET_IPV6) && dst->sa_family == AF_INET6) {
iface = net_if_ipv6_select_src_iface(&net_sin6(dst)->sin6_addr);
if (!iface) {
goto out;
}

return iface;
}

if (IS_ENABLED(CONFIG_NET_IPV4) && dst->sa_family == AF_INET) {
iface = net_if_ipv4_select_src_iface(&net_sin(dst)->sin_addr);
if (!iface) {
goto out;
}

return iface;
}

out:
return net_if_get_default();
}

enum net_verdict net_if_recv_data(struct net_if *iface, struct net_pkt *pkt)
{
if (IS_ENABLED(CONFIG_NET_PROMISCUOUS_MODE) &&

0 comments on commit d020569

Please sign in to comment.
You can’t perform that action at this time.