NetworkTask.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include <Kernel/E1000NetworkAdapter.h>
  2. #include <Kernel/EthernetFrameHeader.h>
  3. #include <Kernel/ARP.h>
  4. #include <Kernel/ICMP.h>
  5. #include <Kernel/IPv4.h>
  6. #include <Kernel/Process.h>
  7. #include <Kernel/EtherType.h>
  8. #include <AK/Lock.h>
  9. //#define ETHERNET_DEBUG
  10. //#define IPV4_DEBUG
  11. static void handle_arp(const EthernetFrameHeader&, int frame_size);
  12. static void handle_ipv4(const EthernetFrameHeader&, int frame_size);
  13. static void handle_icmp(const EthernetFrameHeader&, int frame_size);
  14. Lockable<HashMap<IPv4Address, MACAddress>>& arp_table()
  15. {
  16. static Lockable<HashMap<IPv4Address, MACAddress>>* the;
  17. if (!the)
  18. the = new Lockable<HashMap<IPv4Address, MACAddress>>;
  19. return *the;
  20. }
  21. void NetworkTask_main()
  22. {
  23. auto* e1000_ptr = E1000NetworkAdapter::the();
  24. ASSERT(e1000_ptr);
  25. auto& e1000 = *e1000_ptr;
  26. e1000.set_ipv4_address(IPv4Address(192, 168, 5, 2));
  27. kprintf("NetworkTask: Enter main loop.\n");
  28. for (;;) {
  29. auto packet = e1000.dequeue_packet();
  30. if (packet.is_null()) {
  31. sleep(100);
  32. continue;
  33. }
  34. if (packet.size() < (int)(sizeof(EthernetFrameHeader))) {
  35. kprintf("NetworkTask: Packet is too small to be an Ethernet packet! (%d)\n", packet.size());
  36. continue;
  37. }
  38. auto& eth = *(const EthernetFrameHeader*)packet.pointer();
  39. #ifdef ETHERNET_DEBUG
  40. kprintf("NetworkTask: From %s to %s, ether_type=%w, packet_length=%u\n",
  41. eth.source().to_string().characters(),
  42. eth.destination().to_string().characters(),
  43. eth.ether_type(),
  44. packet.size()
  45. );
  46. #endif
  47. switch (eth.ether_type()) {
  48. case EtherType::ARP:
  49. handle_arp(eth, packet.size());
  50. break;
  51. case EtherType::IPv4:
  52. handle_ipv4(eth, packet.size());
  53. break;
  54. }
  55. }
  56. }
  57. void handle_arp(const EthernetFrameHeader& eth, int frame_size)
  58. {
  59. constexpr int minimum_arp_frame_size = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
  60. if (frame_size < minimum_arp_frame_size) {
  61. kprintf("handle_arp: Frame too small (%d, need %d)\n", frame_size, minimum_arp_frame_size);
  62. return;
  63. }
  64. auto& packet = *static_cast<const ARPPacket*>(eth.payload());
  65. if (packet.hardware_type() != 1 || packet.hardware_address_length() != sizeof(MACAddress)) {
  66. kprintf("handle_arp: Hardware type not ethernet (%w, len=%u)\n",
  67. packet.hardware_type(),
  68. packet.hardware_address_length()
  69. );
  70. return;
  71. }
  72. if (packet.protocol_type() != EtherType::IPv4 || packet.protocol_address_length() != sizeof(IPv4Address)) {
  73. kprintf("handle_arp: Protocol type not IPv4 (%w, len=%u)\n",
  74. packet.hardware_type(),
  75. packet.protocol_address_length()
  76. );
  77. return;
  78. }
  79. #ifdef ARP_DEBUG
  80. kprintf("handle_arp: operation=%w, sender=%s/%s, target=%s/%s\n",
  81. packet.operation(),
  82. packet.sender_hardware_address().to_string().characters(),
  83. packet.sender_protocol_address().to_string().characters(),
  84. packet.target_hardware_address().to_string().characters(),
  85. packet.target_protocol_address().to_string().characters()
  86. );
  87. #endif
  88. if (packet.operation() == ARPOperation::Request) {
  89. // Who has this IP address?
  90. if (auto* adapter = NetworkAdapter::from_ipv4_address(packet.target_protocol_address())) {
  91. // We do!
  92. kprintf("handle_arp: Responding to ARP request for my IPv4 address (%s)\n",
  93. adapter->ipv4_address().to_string().characters());
  94. ARPPacket response;
  95. response.set_operation(ARPOperation::Response);
  96. response.set_target_hardware_address(packet.sender_hardware_address());
  97. response.set_target_protocol_address(packet.sender_protocol_address());
  98. response.set_sender_hardware_address(adapter->mac_address());
  99. response.set_sender_protocol_address(adapter->ipv4_address());
  100. adapter->send(packet.sender_hardware_address(), response);
  101. }
  102. return;
  103. }
  104. if (packet.operation() == ARPOperation::Response) {
  105. // Someone has this IPv4 address. I guess we can try to remember that.
  106. // FIXME: Protect against ARP spamming.
  107. // FIXME: Support static ARP table entries.
  108. LOCKER(arp_table().lock());
  109. arp_table().resource().set(packet.sender_protocol_address(), packet.sender_hardware_address());
  110. kprintf("ARP table (%d entries):\n", arp_table().resource().size());
  111. for (auto& it : arp_table().resource()) {
  112. kprintf("%s :: %s\n", it.value.to_string().characters(), it.key.to_string().characters());
  113. }
  114. }
  115. }
  116. void handle_ipv4(const EthernetFrameHeader& eth, int frame_size)
  117. {
  118. constexpr int minimum_ipv4_frame_size = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet);
  119. if (frame_size < minimum_ipv4_frame_size) {
  120. kprintf("handle_ipv4: Frame too small (%d, need %d)\n", frame_size, minimum_ipv4_frame_size);
  121. return;
  122. }
  123. auto& packet = *static_cast<const IPv4Packet*>(eth.payload());
  124. #ifdef IPV4_DEBUG
  125. kprintf("handle_ipv4: source=%s, target=%s\n",
  126. packet.source().to_string().characters(),
  127. packet.destination().to_string().characters()
  128. );
  129. #endif
  130. switch ((IPv4Protocol)packet.protocol()) {
  131. case IPv4Protocol::ICMP:
  132. return handle_icmp(eth, frame_size);
  133. default:
  134. kprintf("handle_ipv4: Unhandled protocol %u\n", packet.protocol());
  135. break;
  136. }
  137. }
  138. void handle_icmp(const EthernetFrameHeader& eth, int frame_size)
  139. {
  140. (void)frame_size;
  141. auto& ipv4_packet = *static_cast<const IPv4Packet*>(eth.payload());
  142. auto& icmp_header = *static_cast<const ICMPHeader*>(ipv4_packet.payload());
  143. #ifdef ICMP_DEBUG
  144. kprintf("handle_icmp: source=%s, destination=%d type=%b, code=%b\n",
  145. ipv4_packet.source().to_string().characters(),
  146. ipv4_packet.destination().to_string().characters(),
  147. icmp_header.type(),
  148. icmp_header.code()
  149. );
  150. #endif
  151. auto* adapter = NetworkAdapter::from_ipv4_address(ipv4_packet.destination());
  152. if (!adapter)
  153. return;
  154. if (icmp_header.type() == ICMPType::EchoRequest) {
  155. auto& request = reinterpret_cast<const ICMPEchoPacket&>(icmp_header);
  156. kprintf("handle_icmp: EchoRequest from %s: id=%u, seq=%u\n",
  157. ipv4_packet.source().to_string().characters(),
  158. (word)request.identifier,
  159. (word)request.sequence_number
  160. );
  161. size_t icmp_packet_size = ipv4_packet.payload_size();
  162. auto buffer = ByteBuffer::create_zeroed(icmp_packet_size);
  163. auto& response = *(ICMPEchoPacket*)buffer.pointer();
  164. response.header.set_type(ICMPType::EchoReply);
  165. response.header.set_code(0);
  166. response.identifier = request.identifier;
  167. response.sequence_number = request.sequence_number;
  168. if (size_t icmp_payload_size = icmp_packet_size - sizeof(ICMPEchoPacket))
  169. memcpy(response.payload(), request.payload(), icmp_payload_size);
  170. response.header.set_checksum(internet_checksum(&response, icmp_packet_size));
  171. adapter->send_ipv4(eth.source(), ipv4_packet.source(), IPv4Protocol::ICMP, move(buffer));
  172. }
  173. }