Skip to content

Commit 7063921

Browse files
committed
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 <[email protected]>
1 parent 813ae63 commit 7063921

File tree

3 files changed

+69
-15
lines changed

3 files changed

+69
-15
lines changed

include/net/net_if.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,21 +1672,17 @@ __syscall bool net_if_ipv4_set_netmask_by_index(int index,
16721672
* @param iface Interface to use.
16731673
* @param gw IPv4 address of an gateway
16741674
*/
1675-
static inline void net_if_ipv4_set_gw(struct net_if *iface,
1676-
struct in_addr *gw)
1677-
{
1678-
#if defined(CONFIG_NET_IPV4)
1679-
if (net_if_config_ipv4_get(iface, NULL) < 0) {
1680-
return;
1681-
}
1682-
1683-
if (!iface->config.ip.ipv4) {
1684-
return;
1685-
}
1675+
void net_if_ipv4_set_gw(struct net_if *iface, struct in_addr *gw);
16861676

1687-
net_ipaddr_copy(&iface->config.ip.ipv4->gw, gw);
1688-
#endif
1689-
}
1677+
/**
1678+
* @brief Set IPv4 gateway for an interface index.
1679+
*
1680+
* @param index Network interface index
1681+
* @param gw IPv4 address of an gateway
1682+
*
1683+
* @return True if gateway was added, false otherwise.
1684+
*/
1685+
__syscall bool net_if_ipv4_set_gw_by_index(int index, const struct in_addr *gw);
16901686

16911687
/**
16921688
* @brief Get a network interface that should be used when sending

subsys/net/ip/net_if.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,6 +2644,51 @@ Z_SYSCALL_HANDLER(net_if_ipv4_set_netmask_by_index, index, netmask)
26442644
}
26452645
#endif /* CONFIG_USERSPACE */
26462646

2647+
void net_if_ipv4_set_gw(struct net_if *iface, struct in_addr *gw)
2648+
{
2649+
#if defined(CONFIG_NET_IPV4)
2650+
if (net_if_config_ipv4_get(iface, NULL) < 0) {
2651+
return;
2652+
}
2653+
2654+
if (!iface->config.ip.ipv4) {
2655+
return;
2656+
}
2657+
2658+
net_ipaddr_copy(&iface->config.ip.ipv4->gw, gw);
2659+
#endif
2660+
}
2661+
2662+
bool z_impl_net_if_ipv4_set_gw_by_index(int index,
2663+
const struct in_addr *gw)
2664+
{
2665+
struct net_if *iface;
2666+
2667+
iface = net_if_get_by_index(index);
2668+
if (!iface) {
2669+
return false;
2670+
}
2671+
2672+
net_if_ipv4_set_gw(iface, gw);
2673+
2674+
return true;
2675+
}
2676+
2677+
#ifdef CONFIG_USERSPACE
2678+
Z_SYSCALL_HANDLER(net_if_ipv4_set_gw_by_index, index, gw)
2679+
{
2680+
#if defined(CONFIG_NET_IF_USERSPACE_ACCESS)
2681+
struct in_addr gw_addr;
2682+
2683+
Z_OOPS(z_user_from_copy(&gw_addr, (void *)gw, sizeof(gw_addr)));
2684+
2685+
return z_impl_net_if_ipv4_set_gw_by_index(index, &gw_addr);
2686+
#else
2687+
return false;
2688+
#endif
2689+
}
2690+
#endif /* CONFIG_USERSPACE */
2691+
26472692
#if defined(CONFIG_NET_IPV4)
26482693
static struct net_if_addr *ipv4_addr_find(struct net_if *iface,
26492694
struct in_addr *addr)

tests/net/iface/src/main.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,17 @@ static void netmask_addr_add(void)
790790
}
791791
}
792792

793+
static void gw_addr_add(void)
794+
{
795+
struct in_addr my_gw = { { { 192, 0, 2, 254 } } };
796+
bool ret;
797+
798+
if (IS_ENABLED(CONFIG_NET_IF_USERSPACE_ACCESS)) {
799+
ret = net_if_ipv4_set_gw_by_index(1, &my_gw);
800+
zassert_true(ret, "Cannot add IPv4 gateway");
801+
}
802+
}
803+
793804
void test_main(void)
794805
{
795806
ztest_test_suite(net_iface_test,
@@ -819,7 +830,9 @@ void test_main(void)
819830
ztest_user_unit_test(v6_addr_lookup_user),
820831
ztest_user_unit_test(v6_addr_rm_user),
821832
ztest_unit_test(netmask_addr_add),
822-
ztest_user_unit_test(netmask_addr_add)
833+
ztest_user_unit_test(netmask_addr_add),
834+
ztest_unit_test(gw_addr_add),
835+
ztest_user_unit_test(gw_addr_add)
823836
);
824837

825838
ztest_run_test_suite(net_iface_test);

0 commit comments

Comments
 (0)