Browse Source

Kernel: Add support for the MSG_DONTROUTE sys$sendmsg flag

Idan Horowitz 3 years ago
parent
commit
5514d60d8d

+ 1 - 0
Kernel/API/POSIX/sys/socket.h

@@ -52,6 +52,7 @@ extern "C" {
 #define MSG_CTRUNC 0x2
 #define MSG_PEEK 0x4
 #define MSG_OOB 0x8
+#define MSG_DONTROUTE 0x10
 #define MSG_DONTWAIT 0x40
 
 typedef uint16_t sa_family_t;

+ 2 - 1
Kernel/Net/IPv4Socket.cpp

@@ -210,7 +210,8 @@ ErrorOr<size_t> IPv4Socket::sendto(OpenFileDescription&, const UserOrKernelBuffe
     if (!is_connected() && m_peer_address.is_zero())
         return set_so_error(EPIPE);
 
-    auto routing_decision = route_to(m_peer_address, m_local_address, bound_interface());
+    auto allow_using_gateway = (flags & MSG_DONTROUTE) ? AllowUsingGateway::No : AllowUsingGateway::Yes;
+    auto routing_decision = route_to(m_peer_address, m_local_address, bound_interface(), allow_using_gateway);
     if (routing_decision.is_zero())
         return set_so_error(EHOSTUNREACH);
 

+ 2 - 2
Kernel/Net/Routing.cpp

@@ -139,7 +139,7 @@ static MACAddress multicast_ethernet_address(IPv4Address const& address)
     return MACAddress { 0x01, 0x00, 0x5e, (u8)(address[1] & 0x7f), address[2], address[3] };
 }
 
-RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, RefPtr<NetworkAdapter> const through)
+RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, RefPtr<NetworkAdapter> const through, AllowUsingGateway allow_using_gateway)
 {
     auto matches = [&](auto& adapter) {
         if (!through)
@@ -206,7 +206,7 @@ RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, R
 
         adapter = local_adapter;
         next_hop_ip = target;
-    } else if (gateway_adapter) {
+    } else if (gateway_adapter && allow_using_gateway == AllowUsingGateway::Yes) {
         dbgln_if(ROUTING_DEBUG, "Routing: Got adapter for route (using gateway {}): {} ({}/{}) for {}",
             gateway_adapter->ipv4_gateway(),
             gateway_adapter->name(),

+ 7 - 1
Kernel/Net/Routing.h

@@ -25,7 +25,13 @@ enum class UpdateArp {
 };
 
 void update_arp_table(IPv4Address const&, MACAddress const&, UpdateArp update);
-RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, RefPtr<NetworkAdapter> const through = nullptr);
+
+enum class AllowUsingGateway {
+    Yes,
+    No,
+};
+
+RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, RefPtr<NetworkAdapter> const through = nullptr, AllowUsingGateway = AllowUsingGateway::Yes);
 
 MutexProtected<HashMap<IPv4Address, MACAddress>>& arp_table();
 

+ 1 - 1
Userland/Utilities/strace.cpp

@@ -607,7 +607,7 @@ static void format_connect(FormattedSyscallBuilder& builder, int socket, const s
 struct MsgOptions : BitflagBase {
     static constexpr auto options = {
         BITFLAG(MSG_TRUNC), BITFLAG(MSG_CTRUNC), BITFLAG(MSG_PEEK),
-        BITFLAG(MSG_OOB), BITFLAG(MSG_DONTWAIT)
+        BITFLAG(MSG_OOB), BITFLAG(MSG_DONTROUTE), BITFLAG(MSG_DONTWAIT)
         // TODO: add MSG_WAITALL once its definition is added
     };
 };