From 3440dbb1fc0b13eb81d25f26ed54f7df4508a459 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Mon, 15 Feb 2021 21:36:57 +0330 Subject: [PATCH] DHCPClient: Don't reject packets smaller than the max size It's acceptable to have less padding in a packet, the only requirement is to have a single byte of 'END' in the DHCP fields. --- Userland/Services/DHCPClient/DHCPv4Client.cpp | 6 ++++-- Userland/Services/DHCPClient/main.cpp | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Userland/Services/DHCPClient/DHCPv4Client.cpp b/Userland/Services/DHCPClient/DHCPv4Client.cpp index ecb76f107d..945b2b54a4 100644 --- a/Userland/Services/DHCPClient/DHCPv4Client.cpp +++ b/Userland/Services/DHCPClient/DHCPv4Client.cpp @@ -53,7 +53,9 @@ static void send(const InterfaceDescriptor& iface, const DHCPv4Packet& packet, C dst.sin_addr.s_addr = IPv4Address { 255, 255, 255, 255 }.to_u32(); memset(&dst.sin_zero, 0, sizeof(dst.sin_zero)); + dbgln_if(DHCPV4CLIENT_DEBUG, "sendto({} bound to {}, ..., {} at {}) = ...?", fd, iface.m_ifname, dst.sin_addr.s_addr, dst.sin_port); auto rc = sendto(fd, &packet, sizeof(packet), 0, (sockaddr*)&dst, sizeof(dst)); + dbgln_if(DHCPV4CLIENT_DEBUG, "sendto({}) = {}", fd, rc); if (rc < 0) { dbgln("sendto failed with {}", strerror(errno)); // FIXME: what do we do here? @@ -113,8 +115,8 @@ DHCPv4Client::DHCPv4Client(Vector ifnames) m_server->on_ready_to_receive = [this] { auto buffer = m_server->receive(sizeof(DHCPv4Packet)); dbgln("Received {} bytes", buffer.size()); - if (buffer.size() != sizeof(DHCPv4Packet)) { - dbgln("we expected {} bytes, this is a bad packet", sizeof(DHCPv4Packet)); + if (buffer.size() < sizeof(DHCPv4Packet) - DHCPV4_OPTION_FIELD_MAX_LENGTH + 1 || buffer.size() > sizeof(DHCPv4Packet)) { + dbgln("we expected {}-{} bytes, this is a bad packet", sizeof(DHCPv4Packet) - DHCPV4_OPTION_FIELD_MAX_LENGTH + 1, sizeof(DHCPv4Packet)); return; } auto& packet = *(DHCPv4Packet*)buffer.data(); diff --git a/Userland/Services/DHCPClient/main.cpp b/Userland/Services/DHCPClient/main.cpp index 62f51f3fbc..9e77dfb816 100644 --- a/Userland/Services/DHCPClient/main.cpp +++ b/Userland/Services/DHCPClient/main.cpp @@ -25,6 +25,7 @@ */ #include "DHCPv4Client.h" +#include #include #include #include @@ -69,7 +70,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) auto file = Core::File::construct("/proc/net/adapters"); if (!file->open(Core::IODevice::ReadOnly)) { - warnln("Error: {}", file->error_string()); + dbgln("Error: {}", file->error_string()); return 1; } @@ -77,7 +78,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) auto json = JsonValue::from_string(file_contents); if (!json.has_value() || !json.value().is_array()) { - warnln("Error: No network adapters available"); + dbgln("Error: No network adapters available"); return 1; } @@ -90,6 +91,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) auto name = if_object.get("name").to_string(); auto mac = if_object.get("mac_address").to_string(); + dbgln_if(DHCPV4_DEBUG, "Found adapter '{}' with mac {}", name, mac); ifnames.append({ name, mac_from_string(mac) }); });