From 1ffba0b8b4aa26c28288f9191782523c7e9b7017 Mon Sep 17 00:00:00 2001 From: Maciej Date: Sat, 28 May 2022 18:46:55 +0200 Subject: [PATCH] NetworkServer: Support setting default gateway This commit adds an IPv4Gateway to Network.ini. If that option is set to value other than 0.0.0.0, the NetworkServer adds a default route (e.g. with address 0.0.0.0/0) with the specified destination. --- Base/usr/share/man/man5/Network.md | 4 +++- Userland/Services/NetworkServer/main.cpp | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Base/usr/share/man/man5/Network.md b/Base/usr/share/man/man5/Network.md index 26a5e24642..d61f56c257 100644 --- a/Base/usr/share/man/man5/Network.md +++ b/Base/usr/share/man/man5/Network.md @@ -20,14 +20,16 @@ The interface that is not listed in this config file is not set up and disabled * `DHCP` (default: `false`) - Whether the DHCP client should be run on this interface. * `IPv4Address` (default: `0.0.0.0`) - The static IPv4 address for the interface. Used only when `DHCP` is `false`. * `IPv4Netmask` (default: `0.0.0.0`) - The static IPv4 netmask for the interface. Used only when `DHCP` is `false`. +* `IPv4Gateway` (default: `0.0.0.0`) - The static IPv4 default gateway for the interface. Used only when `DHCP` is `false`. ## Example ```ini -# Set static IP address to 10.0.0.5 and mask to 255.0.0.0 +# Set static IP address to 10.0.0.5/8 and default gateway to 10.0.0.1 [ep1s0] IPv4Address=10.0.0.5 IPv4Netmask=255.0.0.0 +IPv4Gateway=10.0.0.1 # Try to run DHCP discovery on ep0s8. [ep0s8] diff --git a/Userland/Services/NetworkServer/main.cpp b/Userland/Services/NetworkServer/main.cpp index e476155011..d5e194f5f5 100644 --- a/Userland/Services/NetworkServer/main.cpp +++ b/Userland/Services/NetworkServer/main.cpp @@ -22,6 +22,7 @@ ErrorOr serenity_main(Main::Arguments) TRY(Core::System::unveil("/bin/DHCPClient", "x")); TRY(Core::System::unveil("/etc/Network.ini", "r")); TRY(Core::System::unveil("/bin/ifconfig", "x")); + TRY(Core::System::unveil("/bin/route", "x")); TRY(Core::System::unveil(nullptr, nullptr)); auto config_file = TRY(Core::ConfigFile::open_for_system("Network")); @@ -40,6 +41,7 @@ ErrorOr serenity_main(Main::Arguments) bool dhcp_enabled = false; String ipv4_address = "0.0.0.0"; String ipv4_netmask = "0.0.0.0"; + String ipv4_gateway = "0.0.0.0"; }; Vector interfaces_with_dhcp_enabled; @@ -59,6 +61,7 @@ ErrorOr serenity_main(Main::Arguments) if (!config.dhcp_enabled) { config.ipv4_address = config_file->read_entry(ifname, "IPv4Address", "0.0.0.0"); config.ipv4_netmask = config_file->read_entry(ifname, "IPv4Netmask", "0.0.0.0"); + config.ipv4_gateway = config_file->read_entry(ifname, "IPv4Gateway", "0.0.0.0"); } } if (config.enabled) { @@ -69,6 +72,8 @@ ErrorOr serenity_main(Main::Arguments) // FIXME: Do this asynchronously dbgln("Setting up interface {} statically ({}/{})", ifname, config.ipv4_address, config.ipv4_netmask); MUST(Core::command("ifconfig", { "-a", ifname.characters(), "-i", config.ipv4_address.characters(), "-m", config.ipv4_netmask.characters() }, {})); + if (config.ipv4_gateway != "0.0.0.0") + MUST(Core::command("route", { "add", "-n", "0.0.0.0", "-m", "0.0.0.0", "-g", config.ipv4_gateway, "-i", ifname }, {})); } } });