From 0866a0cd1e81600776d034334950390b3c07a734 Mon Sep 17 00:00:00 2001 From: brapru Date: Fri, 29 Apr 2022 21:49:00 -0400 Subject: [PATCH] Kernel+route: Support global routing table deletion --- Kernel/Net/IPv4Socket.cpp | 12 ++++++++++-- Kernel/Net/Routing.cpp | 10 +++++++++- Userland/Utilities/route.cpp | 7 ++----- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index 264c7a34f5..76f851899b 100644 --- a/Kernel/Net/IPv4Socket.cpp +++ b/Kernel/Net/IPv4Socket.cpp @@ -639,8 +639,16 @@ ErrorOr IPv4Socket::ioctl(OpenFileDescription&, unsigned request, Userspac return update_routing_table(destination, gateway, genmask, adapter, UpdateTable::Set); } case SIOCDELRT: - // FIXME: Support gateway deletion - return {}; + if (!Process::current().is_superuser()) + return EPERM; + if (route.rt_gateway.sa_family != AF_INET) + return EAFNOSUPPORT; + + auto destination = IPv4Address(((sockaddr_in&)route.rt_dst).sin_addr.s_addr); + auto gateway = IPv4Address(((sockaddr_in&)route.rt_gateway).sin_addr.s_addr); + auto genmask = IPv4Address(((sockaddr_in&)route.rt_genmask).sin_addr.s_addr); + + return update_routing_table(destination, gateway, genmask, adapter, UpdateTable::Delete); } return EINVAL; diff --git a/Kernel/Net/Routing.cpp b/Kernel/Net/Routing.cpp index feba06a32b..8c2396b3ad 100644 --- a/Kernel/Net/Routing.cpp +++ b/Kernel/Net/Routing.cpp @@ -142,7 +142,6 @@ ErrorOr update_routing_table(IPv4Address const& destination, IPv4Address c return ENOMEM; TRY(routing_table().with([&](auto& table) -> ErrorOr { - // TODO: Add support for deleting routing entries if (update == UpdateTable::Set) { for (auto const& route : table) { if (route == *route_entry) @@ -150,6 +149,15 @@ ErrorOr update_routing_table(IPv4Address const& destination, IPv4Address c } table.append(*route_entry); } + if (update == UpdateTable::Delete) { + for (auto& route : table) { + if (route == *route_entry) { + table.remove(route); + return {}; + } + } + return ESRCH; + } return {}; })); diff --git a/Userland/Utilities/route.cpp b/Userland/Utilities/route.cpp index 2b41e0de1f..881268f513 100644 --- a/Userland/Utilities/route.cpp +++ b/Userland/Utilities/route.cpp @@ -179,11 +179,8 @@ ErrorOr serenity_main(Main::Arguments arguments) if (action_add) TRY(Core::System::ioctl(fd, SIOCADDRT, &rt)); - // FIXME: Add support for route deletion. - if (action_del) { - warnln("Route deletion currently not implemented."); - return 1; - } + if (action_del) + TRY(Core::System::ioctl(fd, SIOCDELRT, &rt)); } return 0;