NetworkAdapter.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/HashTable.h>
  27. #include <AK/Singleton.h>
  28. #include <AK/StringBuilder.h>
  29. #include <Kernel/Heap/kmalloc.h>
  30. #include <Kernel/Lock.h>
  31. #include <Kernel/Net/EtherType.h>
  32. #include <Kernel/Net/EthernetFrameHeader.h>
  33. #include <Kernel/Net/LoopbackAdapter.h>
  34. #include <Kernel/Net/NetworkAdapter.h>
  35. #include <Kernel/Random.h>
  36. #include <Kernel/StdLib.h>
  37. namespace Kernel {
  38. static AK::Singleton<Lockable<HashTable<NetworkAdapter*>>> s_table;
  39. static Lockable<HashTable<NetworkAdapter*>>& all_adapters()
  40. {
  41. return *s_table;
  42. }
  43. void NetworkAdapter::for_each(Function<void(NetworkAdapter&)> callback)
  44. {
  45. LOCKER(all_adapters().lock());
  46. for (auto& it : all_adapters().resource())
  47. callback(*it);
  48. }
  49. RefPtr<NetworkAdapter> NetworkAdapter::from_ipv4_address(const IPv4Address& address)
  50. {
  51. LOCKER(all_adapters().lock());
  52. for (auto* adapter : all_adapters().resource()) {
  53. if (adapter->ipv4_address() == address)
  54. return adapter;
  55. }
  56. if (address[0] == 127)
  57. return LoopbackAdapter::the();
  58. return nullptr;
  59. }
  60. RefPtr<NetworkAdapter> NetworkAdapter::lookup_by_name(const StringView& name)
  61. {
  62. NetworkAdapter* found_adapter = nullptr;
  63. for_each([&](auto& adapter) {
  64. if (adapter.name() == name)
  65. found_adapter = &adapter;
  66. });
  67. return found_adapter;
  68. }
  69. NetworkAdapter::NetworkAdapter()
  70. {
  71. // FIXME: I wanna lock :(
  72. all_adapters().resource().set(this);
  73. }
  74. NetworkAdapter::~NetworkAdapter()
  75. {
  76. // FIXME: I wanna lock :(
  77. all_adapters().resource().remove(this);
  78. }
  79. void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet)
  80. {
  81. size_t size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
  82. auto buffer = ByteBuffer::create_zeroed(size_in_bytes);
  83. auto* eth = (EthernetFrameHeader*)buffer.data();
  84. eth->set_source(mac_address());
  85. eth->set_destination(destination);
  86. eth->set_ether_type(EtherType::ARP);
  87. m_packets_out++;
  88. m_bytes_out += size_in_bytes;
  89. memcpy(eth->payload(), &packet, sizeof(ARPPacket));
  90. send_raw({ (const u8*)eth, size_in_bytes });
  91. }
  92. int NetworkAdapter::send_ipv4(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl)
  93. {
  94. size_t ipv4_packet_size = sizeof(IPv4Packet) + payload_size;
  95. if (ipv4_packet_size > mtu())
  96. return send_ipv4_fragmented(destination_mac, destination_ipv4, protocol, payload, payload_size, ttl);
  97. size_t ethernet_frame_size = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet) + payload_size;
  98. auto buffer = ByteBuffer::create_zeroed(ethernet_frame_size);
  99. auto& eth = *(EthernetFrameHeader*)buffer.data();
  100. eth.set_source(mac_address());
  101. eth.set_destination(destination_mac);
  102. eth.set_ether_type(EtherType::IPv4);
  103. auto& ipv4 = *(IPv4Packet*)eth.payload();
  104. ipv4.set_version(4);
  105. ipv4.set_internet_header_length(5);
  106. ipv4.set_source(ipv4_address());
  107. ipv4.set_destination(destination_ipv4);
  108. ipv4.set_protocol((u8)protocol);
  109. ipv4.set_length(sizeof(IPv4Packet) + payload_size);
  110. ipv4.set_ident(1);
  111. ipv4.set_ttl(ttl);
  112. ipv4.set_checksum(ipv4.compute_checksum());
  113. m_packets_out++;
  114. m_bytes_out += ethernet_frame_size;
  115. if (!payload.read(ipv4.payload(), payload_size))
  116. return -EFAULT;
  117. send_raw({ (const u8*)&eth, ethernet_frame_size });
  118. return 0;
  119. }
  120. int NetworkAdapter::send_ipv4_fragmented(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl)
  121. {
  122. // packets must be split on the 64-bit boundary
  123. auto packet_boundary_size = (mtu() - sizeof(IPv4Packet) - sizeof(EthernetFrameHeader)) & 0xfffffff8;
  124. auto fragment_block_count = (payload_size + packet_boundary_size) / packet_boundary_size;
  125. auto last_block_size = payload_size - packet_boundary_size * (fragment_block_count - 1);
  126. auto number_of_blocks_in_fragment = packet_boundary_size / 8;
  127. auto identification = get_good_random<u16>();
  128. size_t ethernet_frame_size = mtu();
  129. for (size_t packet_index = 0; packet_index < fragment_block_count; ++packet_index) {
  130. auto is_last_block = packet_index + 1 == fragment_block_count;
  131. auto packet_payload_size = is_last_block ? last_block_size : packet_boundary_size;
  132. auto buffer = ByteBuffer::create_zeroed(ethernet_frame_size);
  133. auto& eth = *(EthernetFrameHeader*)buffer.data();
  134. eth.set_source(mac_address());
  135. eth.set_destination(destination_mac);
  136. eth.set_ether_type(EtherType::IPv4);
  137. auto& ipv4 = *(IPv4Packet*)eth.payload();
  138. ipv4.set_version(4);
  139. ipv4.set_internet_header_length(5);
  140. ipv4.set_source(ipv4_address());
  141. ipv4.set_destination(destination_ipv4);
  142. ipv4.set_protocol((u8)protocol);
  143. ipv4.set_length(sizeof(IPv4Packet) + packet_payload_size);
  144. ipv4.set_has_more_fragments(!is_last_block);
  145. ipv4.set_ident(identification);
  146. ipv4.set_ttl(ttl);
  147. ipv4.set_fragment_offset(packet_index * number_of_blocks_in_fragment);
  148. ipv4.set_checksum(ipv4.compute_checksum());
  149. m_packets_out++;
  150. m_bytes_out += ethernet_frame_size;
  151. if (!payload.read(ipv4.payload(), packet_index * packet_boundary_size, packet_payload_size))
  152. return -EFAULT;
  153. send_raw({ (const u8*)&eth, ethernet_frame_size });
  154. }
  155. return 0;
  156. }
  157. void NetworkAdapter::did_receive(ReadonlyBytes payload)
  158. {
  159. InterruptDisabler disabler;
  160. m_packets_in++;
  161. m_bytes_in += payload.size();
  162. Optional<KBuffer> buffer;
  163. if (m_unused_packet_buffers.is_empty()) {
  164. buffer = KBuffer::copy(payload.data(), payload.size());
  165. } else {
  166. buffer = m_unused_packet_buffers.take_first();
  167. --m_unused_packet_buffers_count;
  168. if (payload.size() <= buffer.value().size()) {
  169. memcpy(buffer.value().data(), payload.data(), payload.size());
  170. buffer.value().set_size(payload.size());
  171. } else {
  172. buffer = KBuffer::copy(payload.data(), payload.size());
  173. }
  174. }
  175. m_packet_queue.append(buffer.value());
  176. if (on_receive)
  177. on_receive();
  178. }
  179. size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size)
  180. {
  181. InterruptDisabler disabler;
  182. if (m_packet_queue.is_empty())
  183. return 0;
  184. auto packet = m_packet_queue.take_first();
  185. size_t packet_size = packet.size();
  186. ASSERT(packet_size <= buffer_size);
  187. memcpy(buffer, packet.data(), packet_size);
  188. if (m_unused_packet_buffers_count < 100) {
  189. m_unused_packet_buffers.append(packet);
  190. ++m_unused_packet_buffers_count;
  191. }
  192. return packet_size;
  193. }
  194. void NetworkAdapter::set_ipv4_address(const IPv4Address& address)
  195. {
  196. m_ipv4_address = address;
  197. }
  198. void NetworkAdapter::set_ipv4_netmask(const IPv4Address& netmask)
  199. {
  200. m_ipv4_netmask = netmask;
  201. }
  202. void NetworkAdapter::set_ipv4_gateway(const IPv4Address& gateway)
  203. {
  204. m_ipv4_gateway = gateway;
  205. }
  206. void NetworkAdapter::set_interface_name(const StringView& basename)
  207. {
  208. // FIXME: Find a unique name for this interface, starting with $basename.
  209. StringBuilder builder;
  210. builder.append(basename);
  211. builder.append('0');
  212. m_name = builder.to_string();
  213. }
  214. }