Routing.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/HashMap.h>
  27. #include <Kernel/Net/LoopbackAdapter.h>
  28. #include <Kernel/Net/Routing.h>
  29. #include <Kernel/Thread.h>
  30. //#define ROUTING_DEBUG
  31. namespace Kernel {
  32. Lockable<HashMap<IPv4Address, MACAddress>>& arp_table()
  33. {
  34. static Lockable<HashMap<IPv4Address, MACAddress>>* the;
  35. if (!the)
  36. the = new Lockable<HashMap<IPv4Address, MACAddress>>;
  37. return *the;
  38. }
  39. bool RoutingDecision::is_zero() const
  40. {
  41. return adapter.is_null() || next_hop.is_zero();
  42. }
  43. RoutingDecision route_to(const IPv4Address& target, const IPv4Address& source)
  44. {
  45. if (target[0] == 127)
  46. return { LoopbackAdapter::the(), LoopbackAdapter::the().mac_address() };
  47. auto target_addr = target.to_u32();
  48. auto source_addr = source.to_u32();
  49. RefPtr<NetworkAdapter> local_adapter = nullptr;
  50. RefPtr<NetworkAdapter> gateway_adapter = nullptr;
  51. NetworkAdapter::for_each([source_addr, &target_addr, &local_adapter, &gateway_adapter](auto& adapter) {
  52. auto adapter_addr = adapter.ipv4_address().to_u32();
  53. auto adapter_mask = adapter.ipv4_netmask().to_u32();
  54. if (source_addr != 0 && source_addr != adapter_addr)
  55. return;
  56. if ((target_addr & adapter_mask) == (adapter_addr & adapter_mask))
  57. local_adapter = adapter;
  58. if (adapter.ipv4_gateway().to_u32() != 0)
  59. gateway_adapter = adapter;
  60. });
  61. if (local_adapter && target == local_adapter->ipv4_address())
  62. return { local_adapter, local_adapter->mac_address() };
  63. if (!local_adapter && !gateway_adapter) {
  64. #ifdef ROUTING_DEBUG
  65. klog() << "Routing: Couldn't find a suitable adapter for route to " << target.to_string().characters();
  66. #endif
  67. return { nullptr, {} };
  68. }
  69. RefPtr<NetworkAdapter> adapter = nullptr;
  70. IPv4Address next_hop_ip;
  71. if (local_adapter) {
  72. #ifdef ROUTING_DEBUG
  73. klog() << "Routing: Got adapter for route (direct): " << local_adapter->name().characters() << " (" << local_adapter->ipv4_address().to_string().characters() << "/" << local_adapter->ipv4_netmask().to_string().characters() << ") for " << target.to_string().characters();
  74. #endif
  75. adapter = local_adapter;
  76. next_hop_ip = target;
  77. } else if (gateway_adapter) {
  78. #ifdef ROUTING_DEBUG
  79. klog() << "Routing: Got adapter for route (using gateway " << gateway_adapter->ipv4_gateway().to_string().characters() << "): " << gateway_adapter->name().characters() << " (" << gateway_adapter->ipv4_address().to_string().characters() << "/" << gateway_adapter->ipv4_netmask().to_string().characters() << ") for " << target.to_string().characters();
  80. #endif
  81. adapter = gateway_adapter;
  82. next_hop_ip = gateway_adapter->ipv4_gateway();
  83. } else {
  84. return { nullptr, {} };
  85. }
  86. {
  87. LOCKER(arp_table().lock());
  88. auto addr = arp_table().resource().get(next_hop_ip);
  89. if (addr.has_value()) {
  90. #ifdef ROUTING_DEBUG
  91. klog() << "Routing: Using cached ARP entry for " << next_hop_ip.to_string().characters() << " (" << addr.value().to_string().characters() << ")";
  92. #endif
  93. return { adapter, addr.value() };
  94. }
  95. }
  96. #ifdef ROUTING_DEBUG
  97. klog() << "Routing: Sending ARP request via adapter " << adapter->name().characters() << " for IPv4 address " << next_hop_ip.to_string().characters();
  98. #endif
  99. ARPPacket request;
  100. request.set_operation(ARPOperation::Request);
  101. request.set_target_hardware_address({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
  102. request.set_target_protocol_address(next_hop_ip);
  103. request.set_sender_hardware_address(adapter->mac_address());
  104. request.set_sender_protocol_address(adapter->ipv4_address());
  105. adapter->send({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, request);
  106. (void)Thread::current->block_until("Routing (ARP)", [next_hop_ip] {
  107. return arp_table().resource().get(next_hop_ip).has_value();
  108. });
  109. {
  110. LOCKER(arp_table().lock());
  111. auto addr = arp_table().resource().get(next_hop_ip);
  112. if (addr.has_value()) {
  113. #ifdef ROUTING_DEBUG
  114. klog() << "Routing: Got ARP response using adapter " << adapter->name().characters() << " for " << next_hop_ip.to_string().characters() << " (" << addr.value().to_string().characters() << ")";
  115. #endif
  116. return { adapter, addr.value() };
  117. }
  118. }
  119. #ifdef ROUTING_DEBUG
  120. klog() << "Routing: Couldn't find route using adapter " << adapter->name().characters() << " for " << target.to_string().characters();
  121. #endif
  122. return { nullptr, {} };
  123. }
  124. }