Skip to content
Permalink
Browse files

net: if: Add userspace support to IPv4 gateway set function

Allow application to call net_if_ipv4_set_gw_by_index()
and set the gateway if enabled by configuration.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
  • Loading branch information...
jukkar committed Jun 19, 2019
1 parent 813ae63 commit 7063921e87b3609b13f2d21a0470ea8ef45fb9d1
Showing with 69 additions and 15 deletions.
  1. +10 −14 include/net/net_if.h
  2. +45 −0 subsys/net/ip/net_if.c
  3. +14 −1 tests/net/iface/src/main.c
@@ -1672,21 +1672,17 @@ __syscall bool net_if_ipv4_set_netmask_by_index(int index,
* @param iface Interface to use.
* @param gw IPv4 address of an gateway
*/
static inline void net_if_ipv4_set_gw(struct net_if *iface,
struct in_addr *gw)
{
#if defined(CONFIG_NET_IPV4)
if (net_if_config_ipv4_get(iface, NULL) < 0) {
return;
}

if (!iface->config.ip.ipv4) {
return;
}
void net_if_ipv4_set_gw(struct net_if *iface, struct in_addr *gw);

net_ipaddr_copy(&iface->config.ip.ipv4->gw, gw);
#endif
}
/**
* @brief Set IPv4 gateway for an interface index.
*
* @param index Network interface index
* @param gw IPv4 address of an gateway
*
* @return True if gateway was added, false otherwise.
*/
__syscall bool net_if_ipv4_set_gw_by_index(int index, const struct in_addr *gw);

/**
* @brief Get a network interface that should be used when sending
@@ -2644,6 +2644,51 @@ Z_SYSCALL_HANDLER(net_if_ipv4_set_netmask_by_index, index, netmask)
}
#endif /* CONFIG_USERSPACE */

void net_if_ipv4_set_gw(struct net_if *iface, struct in_addr *gw)
{
#if defined(CONFIG_NET_IPV4)
if (net_if_config_ipv4_get(iface, NULL) < 0) {
return;
}

if (!iface->config.ip.ipv4) {
return;
}

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

bool z_impl_net_if_ipv4_set_gw_by_index(int index,
const struct in_addr *gw)
{
struct net_if *iface;

iface = net_if_get_by_index(index);
if (!iface) {
return false;
}

net_if_ipv4_set_gw(iface, gw);

return true;
}

#ifdef CONFIG_USERSPACE
Z_SYSCALL_HANDLER(net_if_ipv4_set_gw_by_index, index, gw)
{
#if defined(CONFIG_NET_IF_USERSPACE_ACCESS)
struct in_addr gw_addr;

Z_OOPS(z_user_from_copy(&gw_addr, (void *)gw, sizeof(gw_addr)));

return z_impl_net_if_ipv4_set_gw_by_index(index, &gw_addr);
#else
return false;
#endif
}
#endif /* CONFIG_USERSPACE */

#if defined(CONFIG_NET_IPV4)
static struct net_if_addr *ipv4_addr_find(struct net_if *iface,
struct in_addr *addr)
@@ -790,6 +790,17 @@ static void netmask_addr_add(void)
}
}

static void gw_addr_add(void)
{
struct in_addr my_gw = { { { 192, 0, 2, 254 } } };
bool ret;

if (IS_ENABLED(CONFIG_NET_IF_USERSPACE_ACCESS)) {
ret = net_if_ipv4_set_gw_by_index(1, &my_gw);
zassert_true(ret, "Cannot add IPv4 gateway");
}
}

void test_main(void)
{
ztest_test_suite(net_iface_test,
@@ -819,7 +830,9 @@ void test_main(void)
ztest_user_unit_test(v6_addr_lookup_user),
ztest_user_unit_test(v6_addr_rm_user),
ztest_unit_test(netmask_addr_add),
ztest_user_unit_test(netmask_addr_add)
ztest_user_unit_test(netmask_addr_add),
ztest_unit_test(gw_addr_add),
ztest_user_unit_test(gw_addr_add)
);

ztest_run_test_suite(net_iface_test);

0 comments on commit 7063921

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